rs6000: Delete UNSPEC_FRSP
[platform/upstream/gcc.git] / gcc / config / rs6000 / rs6000.c
1 /* Subroutines used for code generation on IBM RS/6000.
2    Copyright (C) 1991-2019 Free Software Foundation, Inc.
3    Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published
9    by the Free Software Foundation; either version 3, or (at your
10    option) any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "memmodel.h"
30 #include "gimple.h"
31 #include "cfghooks.h"
32 #include "cfgloop.h"
33 #include "df.h"
34 #include "tm_p.h"
35 #include "stringpool.h"
36 #include "expmed.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "ira.h"
40 #include "recog.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "insn-attr.h"
44 #include "flags.h"
45 #include "alias.h"
46 #include "fold-const.h"
47 #include "attribs.h"
48 #include "stor-layout.h"
49 #include "calls.h"
50 #include "print-tree.h"
51 #include "varasm.h"
52 #include "explow.h"
53 #include "expr.h"
54 #include "output.h"
55 #include "common/common-target.h"
56 #include "langhooks.h"
57 #include "reload.h"
58 #include "sched-int.h"
59 #include "gimplify.h"
60 #include "gimple-fold.h"
61 #include "gimple-iterator.h"
62 #include "gimple-ssa.h"
63 #include "gimple-walk.h"
64 #include "intl.h"
65 #include "params.h"
66 #include "tm-constrs.h"
67 #include "tree-vectorizer.h"
68 #include "target-globals.h"
69 #include "builtins.h"
70 #include "tree-vector-builder.h"
71 #include "context.h"
72 #include "tree-pass.h"
73 #include "except.h"
74 #if TARGET_XCOFF
75 #include "xcoffout.h"  /* get declarations of xcoff_*_section_name */
76 #endif
77 #include "case-cfn-macros.h"
78 #include "ppc-auxv.h"
79 #include "tree-ssa-propagate.h"
80 #include "tree-vrp.h"
81 #include "tree-ssanames.h"
82 #include "rs6000-internal.h"
83
84 /* This file should be included last.  */
85 #include "target-def.h"
86
87   /* Set -mabi=ieeelongdouble on some old targets.  In the future, power server
88      systems will also set long double to be IEEE 128-bit.  AIX and Darwin
89      explicitly redefine TARGET_IEEEQUAD and TARGET_IEEEQUAD_DEFAULT to 0, so
90      those systems will not pick up this default.  This needs to be after all
91      of the include files, so that POWERPC_LINUX and POWERPC_FREEBSD are
92      properly defined.  */
93 #ifndef TARGET_IEEEQUAD_DEFAULT
94 #if !defined (POWERPC_LINUX) && !defined (POWERPC_FREEBSD)
95 #define TARGET_IEEEQUAD_DEFAULT 1
96 #else
97 #define TARGET_IEEEQUAD_DEFAULT 0
98 #endif
99 #endif
100
101 /* Support targetm.vectorize.builtin_mask_for_load.  */
102 GTY(()) tree altivec_builtin_mask_for_load;
103
104 /* Set to nonzero once AIX common-mode calls have been defined.  */
105 static GTY(()) int common_mode_defined;
106
107 #ifdef USING_ELFOS_H
108 /* Counter for labels which are to be placed in .fixup.  */
109 int fixuplabelno = 0;
110 #endif
111
112 /* Whether to use variant of AIX ABI for PowerPC64 Linux.  */
113 int dot_symbols;
114
115 /* Specify the machine mode that pointers have.  After generation of rtl, the
116    compiler makes no further distinction between pointers and any other objects
117    of this machine mode.  */
118 scalar_int_mode rs6000_pmode;
119
120 #if TARGET_ELF
121 /* Note whether IEEE 128-bit floating point was passed or returned, either as
122    the __float128/_Float128 explicit type, or when long double is IEEE 128-bit
123    floating point.  We changed the default C++ mangling for these types and we
124    may want to generate a weak alias of the old mangling (U10__float128) to the
125    new mangling (u9__ieee128).  */
126 bool rs6000_passes_ieee128 = false;
127 #endif
128
129 /* Generate the manged name (i.e. U10__float128) used in GCC 8.1, and not the
130    name used in current releases (i.e. u9__ieee128).  */
131 static bool ieee128_mangling_gcc_8_1;
132
133 /* Width in bits of a pointer.  */
134 unsigned rs6000_pointer_size;
135
136 #ifdef HAVE_AS_GNU_ATTRIBUTE
137 # ifndef HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE
138 # define HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE 0
139 # endif
140 /* Flag whether floating point values have been passed/returned.
141    Note that this doesn't say whether fprs are used, since the
142    Tag_GNU_Power_ABI_FP .gnu.attributes value this flag controls
143    should be set for soft-float values passed in gprs and ieee128
144    values passed in vsx registers.  */
145 bool rs6000_passes_float = false;
146 bool rs6000_passes_long_double = false;
147 /* Flag whether vector values have been passed/returned.  */
148 bool rs6000_passes_vector = false;
149 /* Flag whether small (<= 8 byte) structures have been returned.  */
150 bool rs6000_returns_struct = false;
151 #endif
152
153 /* Value is TRUE if register/mode pair is acceptable.  */
154 static bool rs6000_hard_regno_mode_ok_p
155   [NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
156
157 /* Maximum number of registers needed for a given register class and mode.  */
158 unsigned char rs6000_class_max_nregs[NUM_MACHINE_MODES][LIM_REG_CLASSES];
159
160 /* How many registers are needed for a given register and mode.  */
161 unsigned char rs6000_hard_regno_nregs[NUM_MACHINE_MODES][FIRST_PSEUDO_REGISTER];
162
163 /* Map register number to register class.  */
164 enum reg_class rs6000_regno_regclass[FIRST_PSEUDO_REGISTER];
165
166 static int dbg_cost_ctrl;
167
168 /* Built in types.  */
169 tree rs6000_builtin_types[RS6000_BTI_MAX];
170 tree rs6000_builtin_decls[RS6000_BUILTIN_COUNT];
171
172 /* Flag to say the TOC is initialized */
173 int toc_initialized, need_toc_init;
174 char toc_label_name[10];
175
176 /* Cached value of rs6000_variable_issue. This is cached in
177    rs6000_variable_issue hook and returned from rs6000_sched_reorder2.  */
178 static short cached_can_issue_more;
179
180 static GTY(()) section *read_only_data_section;
181 static GTY(()) section *private_data_section;
182 static GTY(()) section *tls_data_section;
183 static GTY(()) section *tls_private_data_section;
184 static GTY(()) section *read_only_private_data_section;
185 static GTY(()) section *sdata2_section;
186
187 extern GTY(()) section *toc_section;
188 section *toc_section = 0;
189
190 /* Describe the vector unit used for modes.  */
191 enum rs6000_vector rs6000_vector_unit[NUM_MACHINE_MODES];
192 enum rs6000_vector rs6000_vector_mem[NUM_MACHINE_MODES];
193
194 /* Register classes for various constraints that are based on the target
195    switches.  */
196 enum reg_class rs6000_constraints[RS6000_CONSTRAINT_MAX];
197
198 /* Describe the alignment of a vector.  */
199 int rs6000_vector_align[NUM_MACHINE_MODES];
200
201 /* Map selected modes to types for builtins.  */
202 GTY(()) tree builtin_mode_to_type[MAX_MACHINE_MODE][2];
203
204 /* What modes to automatically generate reciprocal divide estimate (fre) and
205    reciprocal sqrt (frsqrte) for.  */
206 unsigned char rs6000_recip_bits[MAX_MACHINE_MODE];
207
208 /* Masks to determine which reciprocal esitmate instructions to generate
209    automatically.  */
210 enum rs6000_recip_mask {
211   RECIP_SF_DIV          = 0x001,        /* Use divide estimate */
212   RECIP_DF_DIV          = 0x002,
213   RECIP_V4SF_DIV        = 0x004,
214   RECIP_V2DF_DIV        = 0x008,
215
216   RECIP_SF_RSQRT        = 0x010,        /* Use reciprocal sqrt estimate.  */
217   RECIP_DF_RSQRT        = 0x020,
218   RECIP_V4SF_RSQRT      = 0x040,
219   RECIP_V2DF_RSQRT      = 0x080,
220
221   /* Various combination of flags for -mrecip=xxx.  */
222   RECIP_NONE            = 0,
223   RECIP_ALL             = (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
224                            | RECIP_V2DF_DIV | RECIP_SF_RSQRT | RECIP_DF_RSQRT
225                            | RECIP_V4SF_RSQRT | RECIP_V2DF_RSQRT),
226
227   RECIP_HIGH_PRECISION  = RECIP_ALL,
228
229   /* On low precision machines like the power5, don't enable double precision
230      reciprocal square root estimate, since it isn't accurate enough.  */
231   RECIP_LOW_PRECISION   = (RECIP_ALL & ~(RECIP_DF_RSQRT | RECIP_V2DF_RSQRT))
232 };
233
234 /* -mrecip options.  */
235 static struct
236 {
237   const char *string;           /* option name */
238   unsigned int mask;            /* mask bits to set */
239 } recip_options[] = {
240   { "all",       RECIP_ALL },
241   { "none",      RECIP_NONE },
242   { "div",       (RECIP_SF_DIV | RECIP_DF_DIV | RECIP_V4SF_DIV
243                   | RECIP_V2DF_DIV) },
244   { "divf",      (RECIP_SF_DIV | RECIP_V4SF_DIV) },
245   { "divd",      (RECIP_DF_DIV | RECIP_V2DF_DIV) },
246   { "rsqrt",     (RECIP_SF_RSQRT | RECIP_DF_RSQRT | RECIP_V4SF_RSQRT
247                   | RECIP_V2DF_RSQRT) },
248   { "rsqrtf",    (RECIP_SF_RSQRT | RECIP_V4SF_RSQRT) },
249   { "rsqrtd",    (RECIP_DF_RSQRT | RECIP_V2DF_RSQRT) },
250 };
251
252 /* On PowerPC, we have a limited number of target clones that we care about
253    which means we can use an array to hold the options, rather than having more
254    elaborate data structures to identify each possible variation.  Order the
255    clones from the default to the highest ISA.  */
256 enum {
257   CLONE_DEFAULT         = 0,            /* default clone.  */
258   CLONE_ISA_2_05,                       /* ISA 2.05 (power6).  */
259   CLONE_ISA_2_06,                       /* ISA 2.06 (power7).  */
260   CLONE_ISA_2_07,                       /* ISA 2.07 (power8).  */
261   CLONE_ISA_3_00,                       /* ISA 3.00 (power9).  */
262   CLONE_MAX
263 };
264
265 /* Map compiler ISA bits into HWCAP names.  */
266 struct clone_map {
267   HOST_WIDE_INT isa_mask;       /* rs6000_isa mask */
268   const char *name;             /* name to use in __builtin_cpu_supports.  */
269 };
270
271 static const struct clone_map rs6000_clone_map[CLONE_MAX] = {
272   { 0,                          "" },           /* Default options.  */
273   { OPTION_MASK_CMPB,           "arch_2_05" },  /* ISA 2.05 (power6).  */
274   { OPTION_MASK_POPCNTD,        "arch_2_06" },  /* ISA 2.06 (power7).  */
275   { OPTION_MASK_P8_VECTOR,      "arch_2_07" },  /* ISA 2.07 (power8).  */
276   { OPTION_MASK_P9_VECTOR,      "arch_3_00" },  /* ISA 3.00 (power9).  */
277 };
278
279
280 /* Newer LIBCs explicitly export this symbol to declare that they provide
281    the AT_PLATFORM and AT_HWCAP/AT_HWCAP2 values in the TCB.  We emit a
282    reference to this symbol whenever we expand a CPU builtin, so that
283    we never link against an old LIBC.  */
284 const char *tcb_verification_symbol = "__parse_hwcap_and_convert_at_platform";
285
286 /* True if we have expanded a CPU builtin.  */
287 bool cpu_builtin_p = false;
288
289 /* Pointer to function (in rs6000-c.c) that can define or undefine target
290    macros that have changed.  Languages that don't support the preprocessor
291    don't link in rs6000-c.c, so we can't call it directly.  */
292 void (*rs6000_target_modify_macros_ptr) (bool, HOST_WIDE_INT, HOST_WIDE_INT);
293
294 /* Simplfy register classes into simpler classifications.  We assume
295    GPR_REG_TYPE - FPR_REG_TYPE are ordered so that we can use a simple range
296    check for standard register classes (gpr/floating/altivec/vsx) and
297    floating/vector classes (float/altivec/vsx).  */
298
299 enum rs6000_reg_type {
300   NO_REG_TYPE,
301   PSEUDO_REG_TYPE,
302   GPR_REG_TYPE,
303   VSX_REG_TYPE,
304   ALTIVEC_REG_TYPE,
305   FPR_REG_TYPE,
306   SPR_REG_TYPE,
307   CR_REG_TYPE
308 };
309
310 /* Map register class to register type.  */
311 static enum rs6000_reg_type reg_class_to_reg_type[N_REG_CLASSES];
312
313 /* First/last register type for the 'normal' register types (i.e. general
314    purpose, floating point, altivec, and VSX registers).  */
315 #define IS_STD_REG_TYPE(RTYPE) IN_RANGE(RTYPE, GPR_REG_TYPE, FPR_REG_TYPE)
316
317 #define IS_FP_VECT_REG_TYPE(RTYPE) IN_RANGE(RTYPE, VSX_REG_TYPE, FPR_REG_TYPE)
318
319
320 /* Register classes we care about in secondary reload or go if legitimate
321    address.  We only need to worry about GPR, FPR, and Altivec registers here,
322    along an ANY field that is the OR of the 3 register classes.  */
323
324 enum rs6000_reload_reg_type {
325   RELOAD_REG_GPR,                       /* General purpose registers.  */
326   RELOAD_REG_FPR,                       /* Traditional floating point regs.  */
327   RELOAD_REG_VMX,                       /* Altivec (VMX) registers.  */
328   RELOAD_REG_ANY,                       /* OR of GPR, FPR, Altivec masks.  */
329   N_RELOAD_REG
330 };
331
332 /* For setting up register classes, loop through the 3 register classes mapping
333    into real registers, and skip the ANY class, which is just an OR of the
334    bits.  */
335 #define FIRST_RELOAD_REG_CLASS  RELOAD_REG_GPR
336 #define LAST_RELOAD_REG_CLASS   RELOAD_REG_VMX
337
338 /* Map reload register type to a register in the register class.  */
339 struct reload_reg_map_type {
340   const char *name;                     /* Register class name.  */
341   int reg;                              /* Register in the register class.  */
342 };
343
344 static const struct reload_reg_map_type reload_reg_map[N_RELOAD_REG] = {
345   { "Gpr",      FIRST_GPR_REGNO },      /* RELOAD_REG_GPR.  */
346   { "Fpr",      FIRST_FPR_REGNO },      /* RELOAD_REG_FPR.  */
347   { "VMX",      FIRST_ALTIVEC_REGNO },  /* RELOAD_REG_VMX.  */
348   { "Any",      -1 },                   /* RELOAD_REG_ANY.  */
349 };
350
351 /* Mask bits for each register class, indexed per mode.  Historically the
352    compiler has been more restrictive which types can do PRE_MODIFY instead of
353    PRE_INC and PRE_DEC, so keep track of sepaate bits for these two.  */
354 typedef unsigned char addr_mask_type;
355
356 #define RELOAD_REG_VALID        0x01    /* Mode valid in register..  */
357 #define RELOAD_REG_MULTIPLE     0x02    /* Mode takes multiple registers.  */
358 #define RELOAD_REG_INDEXED      0x04    /* Reg+reg addressing.  */
359 #define RELOAD_REG_OFFSET       0x08    /* Reg+offset addressing. */
360 #define RELOAD_REG_PRE_INCDEC   0x10    /* PRE_INC/PRE_DEC valid.  */
361 #define RELOAD_REG_PRE_MODIFY   0x20    /* PRE_MODIFY valid.  */
362 #define RELOAD_REG_AND_M16      0x40    /* AND -16 addressing.  */
363 #define RELOAD_REG_QUAD_OFFSET  0x80    /* quad offset is limited.  */
364
365 /* Register type masks based on the type, of valid addressing modes.  */
366 struct rs6000_reg_addr {
367   enum insn_code reload_load;           /* INSN to reload for loading. */
368   enum insn_code reload_store;          /* INSN to reload for storing.  */
369   enum insn_code reload_fpr_gpr;        /* INSN to move from FPR to GPR.  */
370   enum insn_code reload_gpr_vsx;        /* INSN to move from GPR to VSX.  */
371   enum insn_code reload_vsx_gpr;        /* INSN to move from VSX to GPR.  */
372   addr_mask_type addr_mask[(int)N_RELOAD_REG]; /* Valid address masks.  */
373   bool scalar_in_vmx_p;                 /* Scalar value can go in VMX.  */
374 };
375
376 static struct rs6000_reg_addr reg_addr[NUM_MACHINE_MODES];
377
378 /* Helper function to say whether a mode supports PRE_INC or PRE_DEC.  */
379 static inline bool
380 mode_supports_pre_incdec_p (machine_mode mode)
381 {
382   return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_INCDEC)
383           != 0);
384 }
385
386 /* Helper function to say whether a mode supports PRE_MODIFY.  */
387 static inline bool
388 mode_supports_pre_modify_p (machine_mode mode)
389 {
390   return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_PRE_MODIFY)
391           != 0);
392 }
393
394 /* Return true if we have D-form addressing in altivec registers.  */
395 static inline bool
396 mode_supports_vmx_dform (machine_mode mode)
397 {
398   return ((reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_OFFSET) != 0);
399 }
400
401 /* Return true if we have D-form addressing in VSX registers.  This addressing
402    is more limited than normal d-form addressing in that the offset must be
403    aligned on a 16-byte boundary.  */
404 static inline bool
405 mode_supports_dq_form (machine_mode mode)
406 {
407   return ((reg_addr[mode].addr_mask[RELOAD_REG_ANY] & RELOAD_REG_QUAD_OFFSET)
408           != 0);
409 }
410
411 /* Given that there exists at least one variable that is set (produced)
412    by OUT_INSN and read (consumed) by IN_INSN, return true iff
413    IN_INSN represents one or more memory store operations and none of
414    the variables set by OUT_INSN is used by IN_INSN as the address of a
415    store operation.  If either IN_INSN or OUT_INSN does not represent
416    a "single" RTL SET expression (as loosely defined by the
417    implementation of the single_set function) or a PARALLEL with only
418    SETs, CLOBBERs, and USEs inside, this function returns false.
419
420    This rs6000-specific version of store_data_bypass_p checks for
421    certain conditions that result in assertion failures (and internal
422    compiler errors) in the generic store_data_bypass_p function and
423    returns false rather than calling store_data_bypass_p if one of the
424    problematic conditions is detected.  */
425
426 int
427 rs6000_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
428 {
429   rtx out_set, in_set;
430   rtx out_pat, in_pat;
431   rtx out_exp, in_exp;
432   int i, j;
433
434   in_set = single_set (in_insn);
435   if (in_set)
436     {
437       if (MEM_P (SET_DEST (in_set)))
438         {
439           out_set = single_set (out_insn);
440           if (!out_set)
441             {
442               out_pat = PATTERN (out_insn);
443               if (GET_CODE (out_pat) == PARALLEL)
444                 {
445                   for (i = 0; i < XVECLEN (out_pat, 0); i++)
446                     {
447                       out_exp = XVECEXP (out_pat, 0, i);
448                       if ((GET_CODE (out_exp) == CLOBBER)
449                           || (GET_CODE (out_exp) == USE))
450                         continue;
451                       else if (GET_CODE (out_exp) != SET)
452                         return false;
453                     }
454                 }
455             }
456         }
457     }
458   else
459     {
460       in_pat = PATTERN (in_insn);
461       if (GET_CODE (in_pat) != PARALLEL)
462         return false;
463
464       for (i = 0; i < XVECLEN (in_pat, 0); i++)
465         {
466           in_exp = XVECEXP (in_pat, 0, i);
467           if ((GET_CODE (in_exp) == CLOBBER) || (GET_CODE (in_exp) == USE))
468             continue;
469           else if (GET_CODE (in_exp) != SET)
470             return false;
471
472           if (MEM_P (SET_DEST (in_exp)))
473             {
474               out_set = single_set (out_insn);
475               if (!out_set)
476                 {
477                   out_pat = PATTERN (out_insn);
478                   if (GET_CODE (out_pat) != PARALLEL)
479                     return false;
480                   for (j = 0; j < XVECLEN (out_pat, 0); j++)
481                     {
482                       out_exp = XVECEXP (out_pat, 0, j);
483                       if ((GET_CODE (out_exp) == CLOBBER)
484                           || (GET_CODE (out_exp) == USE))
485                         continue;
486                       else if (GET_CODE (out_exp) != SET)
487                         return false;
488                     }
489                 }
490             }
491         }
492     }
493   return store_data_bypass_p (out_insn, in_insn);
494 }
495
496 \f
497 /* Processor costs (relative to an add) */
498
499 const struct processor_costs *rs6000_cost;
500
501 /* Instruction size costs on 32bit processors.  */
502 static const
503 struct processor_costs size32_cost = {
504   COSTS_N_INSNS (1),    /* mulsi */
505   COSTS_N_INSNS (1),    /* mulsi_const */
506   COSTS_N_INSNS (1),    /* mulsi_const9 */
507   COSTS_N_INSNS (1),    /* muldi */
508   COSTS_N_INSNS (1),    /* divsi */
509   COSTS_N_INSNS (1),    /* divdi */
510   COSTS_N_INSNS (1),    /* fp */
511   COSTS_N_INSNS (1),    /* dmul */
512   COSTS_N_INSNS (1),    /* sdiv */
513   COSTS_N_INSNS (1),    /* ddiv */
514   32,                   /* cache line size */
515   0,                    /* l1 cache */
516   0,                    /* l2 cache */
517   0,                    /* streams */
518   0,                    /* SF->DF convert */
519 };
520
521 /* Instruction size costs on 64bit processors.  */
522 static const
523 struct processor_costs size64_cost = {
524   COSTS_N_INSNS (1),    /* mulsi */
525   COSTS_N_INSNS (1),    /* mulsi_const */
526   COSTS_N_INSNS (1),    /* mulsi_const9 */
527   COSTS_N_INSNS (1),    /* muldi */
528   COSTS_N_INSNS (1),    /* divsi */
529   COSTS_N_INSNS (1),    /* divdi */
530   COSTS_N_INSNS (1),    /* fp */
531   COSTS_N_INSNS (1),    /* dmul */
532   COSTS_N_INSNS (1),    /* sdiv */
533   COSTS_N_INSNS (1),    /* ddiv */
534   128,                  /* cache line size */
535   0,                    /* l1 cache */
536   0,                    /* l2 cache */
537   0,                    /* streams */
538   0,                    /* SF->DF convert */
539 };
540
541 /* Instruction costs on RS64A processors.  */
542 static const
543 struct processor_costs rs64a_cost = {
544   COSTS_N_INSNS (20),   /* mulsi */
545   COSTS_N_INSNS (12),   /* mulsi_const */
546   COSTS_N_INSNS (8),    /* mulsi_const9 */
547   COSTS_N_INSNS (34),   /* muldi */
548   COSTS_N_INSNS (65),   /* divsi */
549   COSTS_N_INSNS (67),   /* divdi */
550   COSTS_N_INSNS (4),    /* fp */
551   COSTS_N_INSNS (4),    /* dmul */
552   COSTS_N_INSNS (31),   /* sdiv */
553   COSTS_N_INSNS (31),   /* ddiv */
554   128,                  /* cache line size */
555   128,                  /* l1 cache */
556   2048,                 /* l2 cache */
557   1,                    /* streams */
558   0,                    /* SF->DF convert */
559 };
560
561 /* Instruction costs on MPCCORE processors.  */
562 static const
563 struct processor_costs mpccore_cost = {
564   COSTS_N_INSNS (2),    /* mulsi */
565   COSTS_N_INSNS (2),    /* mulsi_const */
566   COSTS_N_INSNS (2),    /* mulsi_const9 */
567   COSTS_N_INSNS (2),    /* muldi */
568   COSTS_N_INSNS (6),    /* divsi */
569   COSTS_N_INSNS (6),    /* divdi */
570   COSTS_N_INSNS (4),    /* fp */
571   COSTS_N_INSNS (5),    /* dmul */
572   COSTS_N_INSNS (10),   /* sdiv */
573   COSTS_N_INSNS (17),   /* ddiv */
574   32,                   /* cache line size */
575   4,                    /* l1 cache */
576   16,                   /* l2 cache */
577   1,                    /* streams */
578   0,                    /* SF->DF convert */
579 };
580
581 /* Instruction costs on PPC403 processors.  */
582 static const
583 struct processor_costs ppc403_cost = {
584   COSTS_N_INSNS (4),    /* mulsi */
585   COSTS_N_INSNS (4),    /* mulsi_const */
586   COSTS_N_INSNS (4),    /* mulsi_const9 */
587   COSTS_N_INSNS (4),    /* muldi */
588   COSTS_N_INSNS (33),   /* divsi */
589   COSTS_N_INSNS (33),   /* divdi */
590   COSTS_N_INSNS (11),   /* fp */
591   COSTS_N_INSNS (11),   /* dmul */
592   COSTS_N_INSNS (11),   /* sdiv */
593   COSTS_N_INSNS (11),   /* ddiv */
594   32,                   /* cache line size */
595   4,                    /* l1 cache */
596   16,                   /* l2 cache */
597   1,                    /* streams */
598   0,                    /* SF->DF convert */
599 };
600
601 /* Instruction costs on PPC405 processors.  */
602 static const
603 struct processor_costs ppc405_cost = {
604   COSTS_N_INSNS (5),    /* mulsi */
605   COSTS_N_INSNS (4),    /* mulsi_const */
606   COSTS_N_INSNS (3),    /* mulsi_const9 */
607   COSTS_N_INSNS (5),    /* muldi */
608   COSTS_N_INSNS (35),   /* divsi */
609   COSTS_N_INSNS (35),   /* divdi */
610   COSTS_N_INSNS (11),   /* fp */
611   COSTS_N_INSNS (11),   /* dmul */
612   COSTS_N_INSNS (11),   /* sdiv */
613   COSTS_N_INSNS (11),   /* ddiv */
614   32,                   /* cache line size */
615   16,                   /* l1 cache */
616   128,                  /* l2 cache */
617   1,                    /* streams */
618   0,                    /* SF->DF convert */
619 };
620
621 /* Instruction costs on PPC440 processors.  */
622 static const
623 struct processor_costs ppc440_cost = {
624   COSTS_N_INSNS (3),    /* mulsi */
625   COSTS_N_INSNS (2),    /* mulsi_const */
626   COSTS_N_INSNS (2),    /* mulsi_const9 */
627   COSTS_N_INSNS (3),    /* muldi */
628   COSTS_N_INSNS (34),   /* divsi */
629   COSTS_N_INSNS (34),   /* divdi */
630   COSTS_N_INSNS (5),    /* fp */
631   COSTS_N_INSNS (5),    /* dmul */
632   COSTS_N_INSNS (19),   /* sdiv */
633   COSTS_N_INSNS (33),   /* ddiv */
634   32,                   /* cache line size */
635   32,                   /* l1 cache */
636   256,                  /* l2 cache */
637   1,                    /* streams */
638   0,                    /* SF->DF convert */
639 };
640
641 /* Instruction costs on PPC476 processors.  */
642 static const
643 struct processor_costs ppc476_cost = {
644   COSTS_N_INSNS (4),    /* mulsi */
645   COSTS_N_INSNS (4),    /* mulsi_const */
646   COSTS_N_INSNS (4),    /* mulsi_const9 */
647   COSTS_N_INSNS (4),    /* muldi */
648   COSTS_N_INSNS (11),   /* divsi */
649   COSTS_N_INSNS (11),   /* divdi */
650   COSTS_N_INSNS (6),    /* fp */
651   COSTS_N_INSNS (6),    /* dmul */
652   COSTS_N_INSNS (19),   /* sdiv */
653   COSTS_N_INSNS (33),   /* ddiv */
654   32,                   /* l1 cache line size */
655   32,                   /* l1 cache */
656   512,                  /* l2 cache */
657   1,                    /* streams */
658   0,                    /* SF->DF convert */
659 };
660
661 /* Instruction costs on PPC601 processors.  */
662 static const
663 struct processor_costs ppc601_cost = {
664   COSTS_N_INSNS (5),    /* mulsi */
665   COSTS_N_INSNS (5),    /* mulsi_const */
666   COSTS_N_INSNS (5),    /* mulsi_const9 */
667   COSTS_N_INSNS (5),    /* muldi */
668   COSTS_N_INSNS (36),   /* divsi */
669   COSTS_N_INSNS (36),   /* divdi */
670   COSTS_N_INSNS (4),    /* fp */
671   COSTS_N_INSNS (5),    /* dmul */
672   COSTS_N_INSNS (17),   /* sdiv */
673   COSTS_N_INSNS (31),   /* ddiv */
674   32,                   /* cache line size */
675   32,                   /* l1 cache */
676   256,                  /* l2 cache */
677   1,                    /* streams */
678   0,                    /* SF->DF convert */
679 };
680
681 /* Instruction costs on PPC603 processors.  */
682 static const
683 struct processor_costs ppc603_cost = {
684   COSTS_N_INSNS (5),    /* mulsi */
685   COSTS_N_INSNS (3),    /* mulsi_const */
686   COSTS_N_INSNS (2),    /* mulsi_const9 */
687   COSTS_N_INSNS (5),    /* muldi */
688   COSTS_N_INSNS (37),   /* divsi */
689   COSTS_N_INSNS (37),   /* divdi */
690   COSTS_N_INSNS (3),    /* fp */
691   COSTS_N_INSNS (4),    /* dmul */
692   COSTS_N_INSNS (18),   /* sdiv */
693   COSTS_N_INSNS (33),   /* ddiv */
694   32,                   /* cache line size */
695   8,                    /* l1 cache */
696   64,                   /* l2 cache */
697   1,                    /* streams */
698   0,                    /* SF->DF convert */
699 };
700
701 /* Instruction costs on PPC604 processors.  */
702 static const
703 struct processor_costs ppc604_cost = {
704   COSTS_N_INSNS (4),    /* mulsi */
705   COSTS_N_INSNS (4),    /* mulsi_const */
706   COSTS_N_INSNS (4),    /* mulsi_const9 */
707   COSTS_N_INSNS (4),    /* muldi */
708   COSTS_N_INSNS (20),   /* divsi */
709   COSTS_N_INSNS (20),   /* divdi */
710   COSTS_N_INSNS (3),    /* fp */
711   COSTS_N_INSNS (3),    /* dmul */
712   COSTS_N_INSNS (18),   /* sdiv */
713   COSTS_N_INSNS (32),   /* ddiv */
714   32,                   /* cache line size */
715   16,                   /* l1 cache */
716   512,                  /* l2 cache */
717   1,                    /* streams */
718   0,                    /* SF->DF convert */
719 };
720
721 /* Instruction costs on PPC604e processors.  */
722 static const
723 struct processor_costs ppc604e_cost = {
724   COSTS_N_INSNS (2),    /* mulsi */
725   COSTS_N_INSNS (2),    /* mulsi_const */
726   COSTS_N_INSNS (2),    /* mulsi_const9 */
727   COSTS_N_INSNS (2),    /* muldi */
728   COSTS_N_INSNS (20),   /* divsi */
729   COSTS_N_INSNS (20),   /* divdi */
730   COSTS_N_INSNS (3),    /* fp */
731   COSTS_N_INSNS (3),    /* dmul */
732   COSTS_N_INSNS (18),   /* sdiv */
733   COSTS_N_INSNS (32),   /* ddiv */
734   32,                   /* cache line size */
735   32,                   /* l1 cache */
736   1024,                 /* l2 cache */
737   1,                    /* streams */
738   0,                    /* SF->DF convert */
739 };
740
741 /* Instruction costs on PPC620 processors.  */
742 static const
743 struct processor_costs ppc620_cost = {
744   COSTS_N_INSNS (5),    /* mulsi */
745   COSTS_N_INSNS (4),    /* mulsi_const */
746   COSTS_N_INSNS (3),    /* mulsi_const9 */
747   COSTS_N_INSNS (7),    /* muldi */
748   COSTS_N_INSNS (21),   /* divsi */
749   COSTS_N_INSNS (37),   /* divdi */
750   COSTS_N_INSNS (3),    /* fp */
751   COSTS_N_INSNS (3),    /* dmul */
752   COSTS_N_INSNS (18),   /* sdiv */
753   COSTS_N_INSNS (32),   /* ddiv */
754   128,                  /* cache line size */
755   32,                   /* l1 cache */
756   1024,                 /* l2 cache */
757   1,                    /* streams */
758   0,                    /* SF->DF convert */
759 };
760
761 /* Instruction costs on PPC630 processors.  */
762 static const
763 struct processor_costs ppc630_cost = {
764   COSTS_N_INSNS (5),    /* mulsi */
765   COSTS_N_INSNS (4),    /* mulsi_const */
766   COSTS_N_INSNS (3),    /* mulsi_const9 */
767   COSTS_N_INSNS (7),    /* muldi */
768   COSTS_N_INSNS (21),   /* divsi */
769   COSTS_N_INSNS (37),   /* divdi */
770   COSTS_N_INSNS (3),    /* fp */
771   COSTS_N_INSNS (3),    /* dmul */
772   COSTS_N_INSNS (17),   /* sdiv */
773   COSTS_N_INSNS (21),   /* ddiv */
774   128,                  /* cache line size */
775   64,                   /* l1 cache */
776   1024,                 /* l2 cache */
777   1,                    /* streams */
778   0,                    /* SF->DF convert */
779 };
780
781 /* Instruction costs on Cell processor.  */
782 /* COSTS_N_INSNS (1) ~ one add.  */
783 static const
784 struct processor_costs ppccell_cost = {
785   COSTS_N_INSNS (9/2)+2,    /* mulsi */
786   COSTS_N_INSNS (6/2),    /* mulsi_const */
787   COSTS_N_INSNS (6/2),    /* mulsi_const9 */
788   COSTS_N_INSNS (15/2)+2,   /* muldi */
789   COSTS_N_INSNS (38/2),   /* divsi */
790   COSTS_N_INSNS (70/2),   /* divdi */
791   COSTS_N_INSNS (10/2),   /* fp */
792   COSTS_N_INSNS (10/2),   /* dmul */
793   COSTS_N_INSNS (74/2),   /* sdiv */
794   COSTS_N_INSNS (74/2),   /* ddiv */
795   128,                  /* cache line size */
796   32,                   /* l1 cache */
797   512,                  /* l2 cache */
798   6,                    /* streams */
799   0,                    /* SF->DF convert */
800 };
801
802 /* Instruction costs on PPC750 and PPC7400 processors.  */
803 static const
804 struct processor_costs ppc750_cost = {
805   COSTS_N_INSNS (5),    /* mulsi */
806   COSTS_N_INSNS (3),    /* mulsi_const */
807   COSTS_N_INSNS (2),    /* mulsi_const9 */
808   COSTS_N_INSNS (5),    /* muldi */
809   COSTS_N_INSNS (17),   /* divsi */
810   COSTS_N_INSNS (17),   /* divdi */
811   COSTS_N_INSNS (3),    /* fp */
812   COSTS_N_INSNS (3),    /* dmul */
813   COSTS_N_INSNS (17),   /* sdiv */
814   COSTS_N_INSNS (31),   /* ddiv */
815   32,                   /* cache line size */
816   32,                   /* l1 cache */
817   512,                  /* l2 cache */
818   1,                    /* streams */
819   0,                    /* SF->DF convert */
820 };
821
822 /* Instruction costs on PPC7450 processors.  */
823 static const
824 struct processor_costs ppc7450_cost = {
825   COSTS_N_INSNS (4),    /* mulsi */
826   COSTS_N_INSNS (3),    /* mulsi_const */
827   COSTS_N_INSNS (3),    /* mulsi_const9 */
828   COSTS_N_INSNS (4),    /* muldi */
829   COSTS_N_INSNS (23),   /* divsi */
830   COSTS_N_INSNS (23),   /* divdi */
831   COSTS_N_INSNS (5),    /* fp */
832   COSTS_N_INSNS (5),    /* dmul */
833   COSTS_N_INSNS (21),   /* sdiv */
834   COSTS_N_INSNS (35),   /* ddiv */
835   32,                   /* cache line size */
836   32,                   /* l1 cache */
837   1024,                 /* l2 cache */
838   1,                    /* streams */
839   0,                    /* SF->DF convert */
840 };
841
842 /* Instruction costs on PPC8540 processors.  */
843 static const
844 struct processor_costs ppc8540_cost = {
845   COSTS_N_INSNS (4),    /* mulsi */
846   COSTS_N_INSNS (4),    /* mulsi_const */
847   COSTS_N_INSNS (4),    /* mulsi_const9 */
848   COSTS_N_INSNS (4),    /* muldi */
849   COSTS_N_INSNS (19),   /* divsi */
850   COSTS_N_INSNS (19),   /* divdi */
851   COSTS_N_INSNS (4),    /* fp */
852   COSTS_N_INSNS (4),    /* dmul */
853   COSTS_N_INSNS (29),   /* sdiv */
854   COSTS_N_INSNS (29),   /* ddiv */
855   32,                   /* cache line size */
856   32,                   /* l1 cache */
857   256,                  /* l2 cache */
858   1,                    /* prefetch streams /*/
859   0,                    /* SF->DF convert */
860 };
861
862 /* Instruction costs on E300C2 and E300C3 cores.  */
863 static const
864 struct processor_costs ppce300c2c3_cost = {
865   COSTS_N_INSNS (4),    /* mulsi */
866   COSTS_N_INSNS (4),    /* mulsi_const */
867   COSTS_N_INSNS (4),    /* mulsi_const9 */
868   COSTS_N_INSNS (4),    /* muldi */
869   COSTS_N_INSNS (19),   /* divsi */
870   COSTS_N_INSNS (19),   /* divdi */
871   COSTS_N_INSNS (3),    /* fp */
872   COSTS_N_INSNS (4),    /* dmul */
873   COSTS_N_INSNS (18),   /* sdiv */
874   COSTS_N_INSNS (33),   /* ddiv */
875   32,
876   16,                   /* l1 cache */
877   16,                   /* l2 cache */
878   1,                    /* prefetch streams /*/
879   0,                    /* SF->DF convert */
880 };
881
882 /* Instruction costs on PPCE500MC processors.  */
883 static const
884 struct processor_costs ppce500mc_cost = {
885   COSTS_N_INSNS (4),    /* mulsi */
886   COSTS_N_INSNS (4),    /* mulsi_const */
887   COSTS_N_INSNS (4),    /* mulsi_const9 */
888   COSTS_N_INSNS (4),    /* muldi */
889   COSTS_N_INSNS (14),   /* divsi */
890   COSTS_N_INSNS (14),   /* divdi */
891   COSTS_N_INSNS (8),    /* fp */
892   COSTS_N_INSNS (10),   /* dmul */
893   COSTS_N_INSNS (36),   /* sdiv */
894   COSTS_N_INSNS (66),   /* ddiv */
895   64,                   /* cache line size */
896   32,                   /* l1 cache */
897   128,                  /* l2 cache */
898   1,                    /* prefetch streams /*/
899   0,                    /* SF->DF convert */
900 };
901
902 /* Instruction costs on PPCE500MC64 processors.  */
903 static const
904 struct processor_costs ppce500mc64_cost = {
905   COSTS_N_INSNS (4),    /* mulsi */
906   COSTS_N_INSNS (4),    /* mulsi_const */
907   COSTS_N_INSNS (4),    /* mulsi_const9 */
908   COSTS_N_INSNS (4),    /* muldi */
909   COSTS_N_INSNS (14),   /* divsi */
910   COSTS_N_INSNS (14),   /* divdi */
911   COSTS_N_INSNS (4),    /* fp */
912   COSTS_N_INSNS (10),   /* dmul */
913   COSTS_N_INSNS (36),   /* sdiv */
914   COSTS_N_INSNS (66),   /* ddiv */
915   64,                   /* cache line size */
916   32,                   /* l1 cache */
917   128,                  /* l2 cache */
918   1,                    /* prefetch streams /*/
919   0,                    /* SF->DF convert */
920 };
921
922 /* Instruction costs on PPCE5500 processors.  */
923 static const
924 struct processor_costs ppce5500_cost = {
925   COSTS_N_INSNS (5),    /* mulsi */
926   COSTS_N_INSNS (5),    /* mulsi_const */
927   COSTS_N_INSNS (4),    /* mulsi_const9 */
928   COSTS_N_INSNS (5),    /* muldi */
929   COSTS_N_INSNS (14),   /* divsi */
930   COSTS_N_INSNS (14),   /* divdi */
931   COSTS_N_INSNS (7),    /* fp */
932   COSTS_N_INSNS (10),   /* dmul */
933   COSTS_N_INSNS (36),   /* sdiv */
934   COSTS_N_INSNS (66),   /* ddiv */
935   64,                   /* cache line size */
936   32,                   /* l1 cache */
937   128,                  /* l2 cache */
938   1,                    /* prefetch streams /*/
939   0,                    /* SF->DF convert */
940 };
941
942 /* Instruction costs on PPCE6500 processors.  */
943 static const
944 struct processor_costs ppce6500_cost = {
945   COSTS_N_INSNS (5),    /* mulsi */
946   COSTS_N_INSNS (5),    /* mulsi_const */
947   COSTS_N_INSNS (4),    /* mulsi_const9 */
948   COSTS_N_INSNS (5),    /* muldi */
949   COSTS_N_INSNS (14),   /* divsi */
950   COSTS_N_INSNS (14),   /* divdi */
951   COSTS_N_INSNS (7),    /* fp */
952   COSTS_N_INSNS (10),   /* dmul */
953   COSTS_N_INSNS (36),   /* sdiv */
954   COSTS_N_INSNS (66),   /* ddiv */
955   64,                   /* cache line size */
956   32,                   /* l1 cache */
957   128,                  /* l2 cache */
958   1,                    /* prefetch streams /*/
959   0,                    /* SF->DF convert */
960 };
961
962 /* Instruction costs on AppliedMicro Titan processors.  */
963 static const
964 struct processor_costs titan_cost = {
965   COSTS_N_INSNS (5),    /* mulsi */
966   COSTS_N_INSNS (5),    /* mulsi_const */
967   COSTS_N_INSNS (5),    /* mulsi_const9 */
968   COSTS_N_INSNS (5),    /* muldi */
969   COSTS_N_INSNS (18),   /* divsi */
970   COSTS_N_INSNS (18),   /* divdi */
971   COSTS_N_INSNS (10),   /* fp */
972   COSTS_N_INSNS (10),   /* dmul */
973   COSTS_N_INSNS (46),   /* sdiv */
974   COSTS_N_INSNS (72),   /* ddiv */
975   32,                   /* cache line size */
976   32,                   /* l1 cache */
977   512,                  /* l2 cache */
978   1,                    /* prefetch streams /*/
979   0,                    /* SF->DF convert */
980 };
981
982 /* Instruction costs on POWER4 and POWER5 processors.  */
983 static const
984 struct processor_costs power4_cost = {
985   COSTS_N_INSNS (3),    /* mulsi */
986   COSTS_N_INSNS (2),    /* mulsi_const */
987   COSTS_N_INSNS (2),    /* mulsi_const9 */
988   COSTS_N_INSNS (4),    /* muldi */
989   COSTS_N_INSNS (18),   /* divsi */
990   COSTS_N_INSNS (34),   /* divdi */
991   COSTS_N_INSNS (3),    /* fp */
992   COSTS_N_INSNS (3),    /* dmul */
993   COSTS_N_INSNS (17),   /* sdiv */
994   COSTS_N_INSNS (17),   /* ddiv */
995   128,                  /* cache line size */
996   32,                   /* l1 cache */
997   1024,                 /* l2 cache */
998   8,                    /* prefetch streams /*/
999   0,                    /* SF->DF convert */
1000 };
1001
1002 /* Instruction costs on POWER6 processors.  */
1003 static const
1004 struct processor_costs power6_cost = {
1005   COSTS_N_INSNS (8),    /* mulsi */
1006   COSTS_N_INSNS (8),    /* mulsi_const */
1007   COSTS_N_INSNS (8),    /* mulsi_const9 */
1008   COSTS_N_INSNS (8),    /* muldi */
1009   COSTS_N_INSNS (22),   /* divsi */
1010   COSTS_N_INSNS (28),   /* divdi */
1011   COSTS_N_INSNS (3),    /* fp */
1012   COSTS_N_INSNS (3),    /* dmul */
1013   COSTS_N_INSNS (13),   /* sdiv */
1014   COSTS_N_INSNS (16),   /* ddiv */
1015   128,                  /* cache line size */
1016   64,                   /* l1 cache */
1017   2048,                 /* l2 cache */
1018   16,                   /* prefetch streams */
1019   0,                    /* SF->DF convert */
1020 };
1021
1022 /* Instruction costs on POWER7 processors.  */
1023 static const
1024 struct processor_costs power7_cost = {
1025   COSTS_N_INSNS (2),    /* mulsi */
1026   COSTS_N_INSNS (2),    /* mulsi_const */
1027   COSTS_N_INSNS (2),    /* mulsi_const9 */
1028   COSTS_N_INSNS (2),    /* muldi */
1029   COSTS_N_INSNS (18),   /* divsi */
1030   COSTS_N_INSNS (34),   /* divdi */
1031   COSTS_N_INSNS (3),    /* fp */
1032   COSTS_N_INSNS (3),    /* dmul */
1033   COSTS_N_INSNS (13),   /* sdiv */
1034   COSTS_N_INSNS (16),   /* ddiv */
1035   128,                  /* cache line size */
1036   32,                   /* l1 cache */
1037   256,                  /* l2 cache */
1038   12,                   /* prefetch streams */
1039   COSTS_N_INSNS (3),    /* SF->DF convert */
1040 };
1041
1042 /* Instruction costs on POWER8 processors.  */
1043 static const
1044 struct processor_costs power8_cost = {
1045   COSTS_N_INSNS (3),    /* mulsi */
1046   COSTS_N_INSNS (3),    /* mulsi_const */
1047   COSTS_N_INSNS (3),    /* mulsi_const9 */
1048   COSTS_N_INSNS (3),    /* muldi */
1049   COSTS_N_INSNS (19),   /* divsi */
1050   COSTS_N_INSNS (35),   /* divdi */
1051   COSTS_N_INSNS (3),    /* fp */
1052   COSTS_N_INSNS (3),    /* dmul */
1053   COSTS_N_INSNS (14),   /* sdiv */
1054   COSTS_N_INSNS (17),   /* ddiv */
1055   128,                  /* cache line size */
1056   32,                   /* l1 cache */
1057   256,                  /* l2 cache */
1058   12,                   /* prefetch streams */
1059   COSTS_N_INSNS (3),    /* SF->DF convert */
1060 };
1061
1062 /* Instruction costs on POWER9 processors.  */
1063 static const
1064 struct processor_costs power9_cost = {
1065   COSTS_N_INSNS (3),    /* mulsi */
1066   COSTS_N_INSNS (3),    /* mulsi_const */
1067   COSTS_N_INSNS (3),    /* mulsi_const9 */
1068   COSTS_N_INSNS (3),    /* muldi */
1069   COSTS_N_INSNS (8),    /* divsi */
1070   COSTS_N_INSNS (12),   /* divdi */
1071   COSTS_N_INSNS (3),    /* fp */
1072   COSTS_N_INSNS (3),    /* dmul */
1073   COSTS_N_INSNS (13),   /* sdiv */
1074   COSTS_N_INSNS (18),   /* ddiv */
1075   128,                  /* cache line size */
1076   32,                   /* l1 cache */
1077   512,                  /* l2 cache */
1078   8,                    /* prefetch streams */
1079   COSTS_N_INSNS (3),    /* SF->DF convert */
1080 };
1081
1082 /* Instruction costs on POWER A2 processors.  */
1083 static const
1084 struct processor_costs ppca2_cost = {
1085   COSTS_N_INSNS (16),    /* mulsi */
1086   COSTS_N_INSNS (16),    /* mulsi_const */
1087   COSTS_N_INSNS (16),    /* mulsi_const9 */
1088   COSTS_N_INSNS (16),   /* muldi */
1089   COSTS_N_INSNS (22),   /* divsi */
1090   COSTS_N_INSNS (28),   /* divdi */
1091   COSTS_N_INSNS (3),    /* fp */
1092   COSTS_N_INSNS (3),    /* dmul */
1093   COSTS_N_INSNS (59),   /* sdiv */
1094   COSTS_N_INSNS (72),   /* ddiv */
1095   64,
1096   16,                   /* l1 cache */
1097   2048,                 /* l2 cache */
1098   16,                   /* prefetch streams */
1099   0,                    /* SF->DF convert */
1100 };
1101
1102 /* Support for -mveclibabi=<xxx> to control which vector library to use.  */
1103 static tree (*rs6000_veclib_handler) (combined_fn, tree, tree);
1104
1105 \f
1106 static bool rs6000_debug_legitimate_address_p (machine_mode, rtx, bool);
1107 static tree rs6000_handle_longcall_attribute (tree *, tree, tree, int, bool *);
1108 static tree rs6000_handle_altivec_attribute (tree *, tree, tree, int, bool *);
1109 static tree rs6000_handle_struct_attribute (tree *, tree, tree, int, bool *);
1110 static tree rs6000_builtin_vectorized_libmass (combined_fn, tree, tree);
1111 static void rs6000_emit_set_long_const (rtx, HOST_WIDE_INT);
1112 static int rs6000_memory_move_cost (machine_mode, reg_class_t, bool);
1113 static bool rs6000_debug_rtx_costs (rtx, machine_mode, int, int, int *, bool);
1114 static int rs6000_debug_address_cost (rtx, machine_mode, addr_space_t,
1115                                       bool);
1116 static int rs6000_debug_adjust_cost (rtx_insn *, int, rtx_insn *, int,
1117                                      unsigned int);
1118 static bool is_microcoded_insn (rtx_insn *);
1119 static bool is_nonpipeline_insn (rtx_insn *);
1120 static bool is_cracked_insn (rtx_insn *);
1121 static bool is_load_insn (rtx, rtx *);
1122 static bool is_store_insn (rtx, rtx *);
1123 static bool set_to_load_agen (rtx_insn *,rtx_insn *);
1124 static bool insn_terminates_group_p (rtx_insn *, enum group_termination);
1125 static bool insn_must_be_first_in_group (rtx_insn *);
1126 static bool insn_must_be_last_in_group (rtx_insn *);
1127 int easy_vector_constant (rtx, machine_mode);
1128 static rtx rs6000_debug_legitimize_address (rtx, rtx, machine_mode);
1129 static rtx rs6000_legitimize_tls_address (rtx, enum tls_model);
1130 #if TARGET_MACHO
1131 static tree get_prev_label (tree);
1132 #endif
1133 static bool rs6000_mode_dependent_address (const_rtx);
1134 static bool rs6000_debug_mode_dependent_address (const_rtx);
1135 static bool rs6000_offsettable_memref_p (rtx, machine_mode, bool);
1136 static enum reg_class rs6000_secondary_reload_class (enum reg_class,
1137                                                      machine_mode, rtx);
1138 static enum reg_class rs6000_debug_secondary_reload_class (enum reg_class,
1139                                                            machine_mode,
1140                                                            rtx);
1141 static enum reg_class rs6000_preferred_reload_class (rtx, enum reg_class);
1142 static enum reg_class rs6000_debug_preferred_reload_class (rtx,
1143                                                            enum reg_class);
1144 static bool rs6000_debug_secondary_memory_needed (machine_mode,
1145                                                   reg_class_t,
1146                                                   reg_class_t);
1147 static bool rs6000_debug_can_change_mode_class (machine_mode,
1148                                                 machine_mode,
1149                                                 reg_class_t);
1150
1151 static bool (*rs6000_mode_dependent_address_ptr) (const_rtx)
1152   = rs6000_mode_dependent_address;
1153
1154 enum reg_class (*rs6000_secondary_reload_class_ptr) (enum reg_class,
1155                                                      machine_mode, rtx)
1156   = rs6000_secondary_reload_class;
1157
1158 enum reg_class (*rs6000_preferred_reload_class_ptr) (rtx, enum reg_class)
1159   = rs6000_preferred_reload_class;
1160
1161 const int INSN_NOT_AVAILABLE = -1;
1162
1163 static void rs6000_print_isa_options (FILE *, int, const char *,
1164                                       HOST_WIDE_INT);
1165 static void rs6000_print_builtin_options (FILE *, int, const char *,
1166                                           HOST_WIDE_INT);
1167 static HOST_WIDE_INT rs6000_disable_incompatible_switches (void);
1168
1169 static enum rs6000_reg_type register_to_reg_type (rtx, bool *);
1170 static bool rs6000_secondary_reload_move (enum rs6000_reg_type,
1171                                           enum rs6000_reg_type,
1172                                           machine_mode,
1173                                           secondary_reload_info *,
1174                                           bool);
1175 rtl_opt_pass *make_pass_analyze_swaps (gcc::context*);
1176
1177 /* Hash table stuff for keeping track of TOC entries.  */
1178
1179 struct GTY((for_user)) toc_hash_struct
1180 {
1181   /* `key' will satisfy CONSTANT_P; in fact, it will satisfy
1182      ASM_OUTPUT_SPECIAL_POOL_ENTRY_P.  */
1183   rtx key;
1184   machine_mode key_mode;
1185   int labelno;
1186 };
1187
1188 struct toc_hasher : ggc_ptr_hash<toc_hash_struct>
1189 {
1190   static hashval_t hash (toc_hash_struct *);
1191   static bool equal (toc_hash_struct *, toc_hash_struct *);
1192 };
1193
1194 static GTY (()) hash_table<toc_hasher> *toc_hash_table;
1195
1196
1197 \f
1198 /* Default register names.  */
1199 char rs6000_reg_names[][8] =
1200 {
1201   /* GPRs */
1202       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1203       "8",  "9", "10", "11", "12", "13", "14", "15",
1204      "16", "17", "18", "19", "20", "21", "22", "23",
1205      "24", "25", "26", "27", "28", "29", "30", "31",
1206   /* FPRs */
1207       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1208       "8",  "9", "10", "11", "12", "13", "14", "15",
1209      "16", "17", "18", "19", "20", "21", "22", "23",
1210      "24", "25", "26", "27", "28", "29", "30", "31",
1211   /* VRs */
1212       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1213       "8",  "9", "10", "11", "12", "13", "14", "15",
1214      "16", "17", "18", "19", "20", "21", "22", "23",
1215      "24", "25", "26", "27", "28", "29", "30", "31",
1216   /* lr ctr ca ap */
1217      "lr", "ctr", "ca", "ap",
1218   /* cr0..cr7 */
1219       "0",  "1",  "2",  "3",  "4",  "5",  "6",  "7",
1220   /* vrsave vscr sfp */
1221       "vrsave", "vscr", "sfp",
1222 };
1223
1224 #ifdef TARGET_REGNAMES
1225 static const char alt_reg_names[][8] =
1226 {
1227   /* GPRs */
1228    "%r0",  "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
1229    "%r8",  "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
1230   "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
1231   "%r24", "%r25", "%r26", "%r27", "%r28", "%r29", "%r30", "%r31",
1232   /* FPRs */
1233    "%f0",  "%f1",  "%f2",  "%f3",  "%f4",  "%f5",  "%f6",  "%f7",
1234    "%f8",  "%f9", "%f10", "%f11", "%f12", "%f13", "%f14", "%f15",
1235   "%f16", "%f17", "%f18", "%f19", "%f20", "%f21", "%f22", "%f23",
1236   "%f24", "%f25", "%f26", "%f27", "%f28", "%f29", "%f30", "%f31",
1237   /* VRs */
1238    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6",  "%v7",
1239    "%v8",  "%v9", "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
1240   "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
1241   "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
1242   /* lr ctr ca ap */
1243     "lr",  "ctr",   "ca",   "ap",
1244   /* cr0..cr7 */
1245   "%cr0",  "%cr1", "%cr2", "%cr3", "%cr4", "%cr5", "%cr6", "%cr7",
1246   /* vrsave vscr sfp */
1247   "vrsave", "vscr", "sfp",
1248 };
1249 #endif
1250
1251 /* Table of valid machine attributes.  */
1252
1253 static const struct attribute_spec rs6000_attribute_table[] =
1254 {
1255   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
1256        affects_type_identity, handler, exclude } */
1257   { "altivec",   1, 1, false, true,  false, false,
1258     rs6000_handle_altivec_attribute, NULL },
1259   { "longcall",  0, 0, false, true,  true,  false,
1260     rs6000_handle_longcall_attribute, NULL },
1261   { "shortcall", 0, 0, false, true,  true,  false,
1262     rs6000_handle_longcall_attribute, NULL },
1263   { "ms_struct", 0, 0, false, false, false, false,
1264     rs6000_handle_struct_attribute, NULL },
1265   { "gcc_struct", 0, 0, false, false, false, false,
1266     rs6000_handle_struct_attribute, NULL },
1267 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1268   SUBTARGET_ATTRIBUTE_TABLE,
1269 #endif
1270   { NULL,        0, 0, false, false, false, false, NULL, NULL }
1271 };
1272 \f
1273 #ifndef TARGET_PROFILE_KERNEL
1274 #define TARGET_PROFILE_KERNEL 0
1275 #endif
1276 \f
1277 /* Initialize the GCC target structure.  */
1278 #undef TARGET_ATTRIBUTE_TABLE
1279 #define TARGET_ATTRIBUTE_TABLE rs6000_attribute_table
1280 #undef TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
1281 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES rs6000_set_default_type_attributes
1282 #undef TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P
1283 #define TARGET_ATTRIBUTE_TAKES_IDENTIFIER_P rs6000_attribute_takes_identifier_p
1284
1285 #undef TARGET_ASM_ALIGNED_DI_OP
1286 #define TARGET_ASM_ALIGNED_DI_OP DOUBLE_INT_ASM_OP
1287
1288 /* Default unaligned ops are only provided for ELF.  Find the ops needed
1289    for non-ELF systems.  */
1290 #ifndef OBJECT_FORMAT_ELF
1291 #if TARGET_XCOFF
1292 /* For XCOFF.  rs6000_assemble_integer will handle unaligned DIs on
1293    64-bit targets.  */
1294 #undef TARGET_ASM_UNALIGNED_HI_OP
1295 #define TARGET_ASM_UNALIGNED_HI_OP "\t.vbyte\t2,"
1296 #undef TARGET_ASM_UNALIGNED_SI_OP
1297 #define TARGET_ASM_UNALIGNED_SI_OP "\t.vbyte\t4,"
1298 #undef TARGET_ASM_UNALIGNED_DI_OP
1299 #define TARGET_ASM_UNALIGNED_DI_OP "\t.vbyte\t8,"
1300 #else
1301 /* For Darwin.  */
1302 #undef TARGET_ASM_UNALIGNED_HI_OP
1303 #define TARGET_ASM_UNALIGNED_HI_OP "\t.short\t"
1304 #undef TARGET_ASM_UNALIGNED_SI_OP
1305 #define TARGET_ASM_UNALIGNED_SI_OP "\t.long\t"
1306 #undef TARGET_ASM_UNALIGNED_DI_OP
1307 #define TARGET_ASM_UNALIGNED_DI_OP "\t.quad\t"
1308 #undef TARGET_ASM_ALIGNED_DI_OP
1309 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
1310 #endif
1311 #endif
1312
1313 /* This hook deals with fixups for relocatable code and DI-mode objects
1314    in 64-bit code.  */
1315 #undef TARGET_ASM_INTEGER
1316 #define TARGET_ASM_INTEGER rs6000_assemble_integer
1317
1318 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
1319 #undef TARGET_ASM_ASSEMBLE_VISIBILITY
1320 #define TARGET_ASM_ASSEMBLE_VISIBILITY rs6000_assemble_visibility
1321 #endif
1322
1323 #undef TARGET_SET_UP_BY_PROLOGUE
1324 #define TARGET_SET_UP_BY_PROLOGUE rs6000_set_up_by_prologue
1325
1326 #undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
1327 #define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS rs6000_get_separate_components
1328 #undef TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB
1329 #define TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB rs6000_components_for_bb
1330 #undef TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS
1331 #define TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS rs6000_disqualify_components
1332 #undef TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS
1333 #define TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS rs6000_emit_prologue_components
1334 #undef TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS
1335 #define TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS rs6000_emit_epilogue_components
1336 #undef TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS
1337 #define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS rs6000_set_handled_components
1338
1339 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1340 #define TARGET_EXTRA_LIVE_ON_ENTRY rs6000_live_on_entry
1341
1342 #undef TARGET_INTERNAL_ARG_POINTER
1343 #define TARGET_INTERNAL_ARG_POINTER rs6000_internal_arg_pointer
1344
1345 #undef TARGET_HAVE_TLS
1346 #define TARGET_HAVE_TLS HAVE_AS_TLS
1347
1348 #undef TARGET_CANNOT_FORCE_CONST_MEM
1349 #define TARGET_CANNOT_FORCE_CONST_MEM rs6000_cannot_force_const_mem
1350
1351 #undef TARGET_DELEGITIMIZE_ADDRESS
1352 #define TARGET_DELEGITIMIZE_ADDRESS rs6000_delegitimize_address
1353
1354 #undef TARGET_CONST_NOT_OK_FOR_DEBUG_P
1355 #define TARGET_CONST_NOT_OK_FOR_DEBUG_P rs6000_const_not_ok_for_debug_p
1356
1357 #undef TARGET_LEGITIMATE_COMBINED_INSN
1358 #define TARGET_LEGITIMATE_COMBINED_INSN rs6000_legitimate_combined_insn
1359
1360 #undef TARGET_ASM_FUNCTION_PROLOGUE
1361 #define TARGET_ASM_FUNCTION_PROLOGUE rs6000_output_function_prologue
1362 #undef TARGET_ASM_FUNCTION_EPILOGUE
1363 #define TARGET_ASM_FUNCTION_EPILOGUE rs6000_output_function_epilogue
1364
1365 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
1366 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA rs6000_output_addr_const_extra
1367
1368 #undef TARGET_LEGITIMIZE_ADDRESS
1369 #define TARGET_LEGITIMIZE_ADDRESS rs6000_legitimize_address
1370
1371 #undef  TARGET_SCHED_VARIABLE_ISSUE
1372 #define TARGET_SCHED_VARIABLE_ISSUE rs6000_variable_issue
1373
1374 #undef TARGET_SCHED_ISSUE_RATE
1375 #define TARGET_SCHED_ISSUE_RATE rs6000_issue_rate
1376 #undef TARGET_SCHED_ADJUST_COST
1377 #define TARGET_SCHED_ADJUST_COST rs6000_adjust_cost
1378 #undef TARGET_SCHED_ADJUST_PRIORITY
1379 #define TARGET_SCHED_ADJUST_PRIORITY rs6000_adjust_priority
1380 #undef TARGET_SCHED_IS_COSTLY_DEPENDENCE
1381 #define TARGET_SCHED_IS_COSTLY_DEPENDENCE rs6000_is_costly_dependence
1382 #undef TARGET_SCHED_INIT
1383 #define TARGET_SCHED_INIT rs6000_sched_init
1384 #undef TARGET_SCHED_FINISH
1385 #define TARGET_SCHED_FINISH rs6000_sched_finish
1386 #undef TARGET_SCHED_REORDER
1387 #define TARGET_SCHED_REORDER rs6000_sched_reorder
1388 #undef TARGET_SCHED_REORDER2
1389 #define TARGET_SCHED_REORDER2 rs6000_sched_reorder2
1390
1391 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1392 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD rs6000_use_sched_lookahead
1393
1394 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD
1395 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD rs6000_use_sched_lookahead_guard
1396
1397 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
1398 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT rs6000_alloc_sched_context
1399 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
1400 #define TARGET_SCHED_INIT_SCHED_CONTEXT rs6000_init_sched_context
1401 #undef TARGET_SCHED_SET_SCHED_CONTEXT
1402 #define TARGET_SCHED_SET_SCHED_CONTEXT rs6000_set_sched_context
1403 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
1404 #define TARGET_SCHED_FREE_SCHED_CONTEXT rs6000_free_sched_context
1405
1406 #undef TARGET_SCHED_CAN_SPECULATE_INSN
1407 #define TARGET_SCHED_CAN_SPECULATE_INSN rs6000_sched_can_speculate_insn
1408
1409 #undef TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD
1410 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD rs6000_builtin_mask_for_load
1411 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
1412 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT            \
1413   rs6000_builtin_support_vector_misalignment
1414 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
1415 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE rs6000_vector_alignment_reachable
1416 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
1417 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
1418   rs6000_builtin_vectorization_cost
1419 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
1420 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
1421   rs6000_preferred_simd_mode
1422 #undef TARGET_VECTORIZE_INIT_COST
1423 #define TARGET_VECTORIZE_INIT_COST rs6000_init_cost
1424 #undef TARGET_VECTORIZE_ADD_STMT_COST
1425 #define TARGET_VECTORIZE_ADD_STMT_COST rs6000_add_stmt_cost
1426 #undef TARGET_VECTORIZE_FINISH_COST
1427 #define TARGET_VECTORIZE_FINISH_COST rs6000_finish_cost
1428 #undef TARGET_VECTORIZE_DESTROY_COST_DATA
1429 #define TARGET_VECTORIZE_DESTROY_COST_DATA rs6000_destroy_cost_data
1430
1431 #undef TARGET_INIT_BUILTINS
1432 #define TARGET_INIT_BUILTINS rs6000_init_builtins
1433 #undef TARGET_BUILTIN_DECL
1434 #define TARGET_BUILTIN_DECL rs6000_builtin_decl
1435
1436 #undef TARGET_FOLD_BUILTIN
1437 #define TARGET_FOLD_BUILTIN rs6000_fold_builtin
1438 #undef TARGET_GIMPLE_FOLD_BUILTIN
1439 #define TARGET_GIMPLE_FOLD_BUILTIN rs6000_gimple_fold_builtin
1440
1441 #undef TARGET_EXPAND_BUILTIN
1442 #define TARGET_EXPAND_BUILTIN rs6000_expand_builtin
1443
1444 #undef TARGET_MANGLE_TYPE
1445 #define TARGET_MANGLE_TYPE rs6000_mangle_type
1446
1447 #undef TARGET_INIT_LIBFUNCS
1448 #define TARGET_INIT_LIBFUNCS rs6000_init_libfuncs
1449
1450 #if TARGET_MACHO
1451 #undef TARGET_BINDS_LOCAL_P
1452 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1453 #endif
1454
1455 #undef TARGET_MS_BITFIELD_LAYOUT_P
1456 #define TARGET_MS_BITFIELD_LAYOUT_P rs6000_ms_bitfield_layout_p
1457
1458 #undef TARGET_ASM_OUTPUT_MI_THUNK
1459 #define TARGET_ASM_OUTPUT_MI_THUNK rs6000_output_mi_thunk
1460
1461 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1462 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
1463
1464 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1465 #define TARGET_FUNCTION_OK_FOR_SIBCALL rs6000_function_ok_for_sibcall
1466
1467 #undef TARGET_REGISTER_MOVE_COST
1468 #define TARGET_REGISTER_MOVE_COST rs6000_register_move_cost
1469 #undef TARGET_MEMORY_MOVE_COST
1470 #define TARGET_MEMORY_MOVE_COST rs6000_memory_move_cost
1471 #undef TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS
1472 #define TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS \
1473   rs6000_ira_change_pseudo_allocno_class
1474 #undef TARGET_CANNOT_COPY_INSN_P
1475 #define TARGET_CANNOT_COPY_INSN_P rs6000_cannot_copy_insn_p
1476 #undef TARGET_RTX_COSTS
1477 #define TARGET_RTX_COSTS rs6000_rtx_costs
1478 #undef TARGET_ADDRESS_COST
1479 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
1480 #undef TARGET_INSN_COST
1481 #define TARGET_INSN_COST rs6000_insn_cost
1482
1483 #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA
1484 #define TARGET_INIT_DWARF_REG_SIZES_EXTRA rs6000_init_dwarf_reg_sizes_extra
1485
1486 #undef TARGET_PROMOTE_FUNCTION_MODE
1487 #define TARGET_PROMOTE_FUNCTION_MODE rs6000_promote_function_mode
1488
1489 #undef TARGET_RETURN_IN_MEMORY
1490 #define TARGET_RETURN_IN_MEMORY rs6000_return_in_memory
1491
1492 #undef TARGET_RETURN_IN_MSB
1493 #define TARGET_RETURN_IN_MSB rs6000_return_in_msb
1494
1495 #undef TARGET_SETUP_INCOMING_VARARGS
1496 #define TARGET_SETUP_INCOMING_VARARGS setup_incoming_varargs
1497
1498 /* Always strict argument naming on rs6000.  */
1499 #undef TARGET_STRICT_ARGUMENT_NAMING
1500 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
1501 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
1502 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED hook_bool_CUMULATIVE_ARGS_true
1503 #undef TARGET_SPLIT_COMPLEX_ARG
1504 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
1505 #undef TARGET_MUST_PASS_IN_STACK
1506 #define TARGET_MUST_PASS_IN_STACK rs6000_must_pass_in_stack
1507 #undef TARGET_PASS_BY_REFERENCE
1508 #define TARGET_PASS_BY_REFERENCE rs6000_pass_by_reference
1509 #undef TARGET_ARG_PARTIAL_BYTES
1510 #define TARGET_ARG_PARTIAL_BYTES rs6000_arg_partial_bytes
1511 #undef TARGET_FUNCTION_ARG_ADVANCE
1512 #define TARGET_FUNCTION_ARG_ADVANCE rs6000_function_arg_advance
1513 #undef TARGET_FUNCTION_ARG
1514 #define TARGET_FUNCTION_ARG rs6000_function_arg
1515 #undef TARGET_FUNCTION_ARG_PADDING
1516 #define TARGET_FUNCTION_ARG_PADDING rs6000_function_arg_padding
1517 #undef TARGET_FUNCTION_ARG_BOUNDARY
1518 #define TARGET_FUNCTION_ARG_BOUNDARY rs6000_function_arg_boundary
1519
1520 #undef TARGET_BUILD_BUILTIN_VA_LIST
1521 #define TARGET_BUILD_BUILTIN_VA_LIST rs6000_build_builtin_va_list
1522
1523 #undef TARGET_EXPAND_BUILTIN_VA_START
1524 #define TARGET_EXPAND_BUILTIN_VA_START rs6000_va_start
1525
1526 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1527 #define TARGET_GIMPLIFY_VA_ARG_EXPR rs6000_gimplify_va_arg
1528
1529 #undef TARGET_EH_RETURN_FILTER_MODE
1530 #define TARGET_EH_RETURN_FILTER_MODE rs6000_eh_return_filter_mode
1531
1532 #undef TARGET_TRANSLATE_MODE_ATTRIBUTE
1533 #define TARGET_TRANSLATE_MODE_ATTRIBUTE rs6000_translate_mode_attribute
1534
1535 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1536 #define TARGET_SCALAR_MODE_SUPPORTED_P rs6000_scalar_mode_supported_p
1537
1538 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1539 #define TARGET_VECTOR_MODE_SUPPORTED_P rs6000_vector_mode_supported_p
1540
1541 #undef TARGET_FLOATN_MODE
1542 #define TARGET_FLOATN_MODE rs6000_floatn_mode
1543
1544 #undef TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN
1545 #define TARGET_INVALID_ARG_FOR_UNPROTOTYPED_FN invalid_arg_for_unprototyped_fn
1546
1547 #undef TARGET_MD_ASM_ADJUST
1548 #define TARGET_MD_ASM_ADJUST rs6000_md_asm_adjust
1549
1550 #undef TARGET_OPTION_OVERRIDE
1551 #define TARGET_OPTION_OVERRIDE rs6000_option_override
1552
1553 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
1554 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
1555   rs6000_builtin_vectorized_function
1556
1557 #undef TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION
1558 #define TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION \
1559   rs6000_builtin_md_vectorized_function
1560
1561 #undef TARGET_STACK_PROTECT_GUARD
1562 #define TARGET_STACK_PROTECT_GUARD rs6000_init_stack_protect_guard
1563
1564 #if !TARGET_MACHO
1565 #undef TARGET_STACK_PROTECT_FAIL
1566 #define TARGET_STACK_PROTECT_FAIL rs6000_stack_protect_fail
1567 #endif
1568
1569 #ifdef HAVE_AS_TLS
1570 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1571 #define TARGET_ASM_OUTPUT_DWARF_DTPREL rs6000_output_dwarf_dtprel
1572 #endif
1573
1574 /* Use a 32-bit anchor range.  This leads to sequences like:
1575
1576         addis   tmp,anchor,high
1577         add     dest,tmp,low
1578
1579    where tmp itself acts as an anchor, and can be shared between
1580    accesses to the same 64k page.  */
1581 #undef TARGET_MIN_ANCHOR_OFFSET
1582 #define TARGET_MIN_ANCHOR_OFFSET -0x7fffffff - 1
1583 #undef TARGET_MAX_ANCHOR_OFFSET
1584 #define TARGET_MAX_ANCHOR_OFFSET 0x7fffffff
1585 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1586 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P rs6000_use_blocks_for_constant_p
1587 #undef TARGET_USE_BLOCKS_FOR_DECL_P
1588 #define TARGET_USE_BLOCKS_FOR_DECL_P rs6000_use_blocks_for_decl_p
1589
1590 #undef TARGET_BUILTIN_RECIPROCAL
1591 #define TARGET_BUILTIN_RECIPROCAL rs6000_builtin_reciprocal
1592
1593 #undef TARGET_SECONDARY_RELOAD
1594 #define TARGET_SECONDARY_RELOAD rs6000_secondary_reload
1595 #undef TARGET_SECONDARY_MEMORY_NEEDED
1596 #define TARGET_SECONDARY_MEMORY_NEEDED rs6000_secondary_memory_needed
1597 #undef TARGET_SECONDARY_MEMORY_NEEDED_MODE
1598 #define TARGET_SECONDARY_MEMORY_NEEDED_MODE rs6000_secondary_memory_needed_mode
1599
1600 #undef TARGET_LEGITIMATE_ADDRESS_P
1601 #define TARGET_LEGITIMATE_ADDRESS_P rs6000_legitimate_address_p
1602
1603 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
1604 #define TARGET_MODE_DEPENDENT_ADDRESS_P rs6000_mode_dependent_address_p
1605
1606 #undef TARGET_COMPUTE_PRESSURE_CLASSES
1607 #define TARGET_COMPUTE_PRESSURE_CLASSES rs6000_compute_pressure_classes
1608
1609 #undef TARGET_CAN_ELIMINATE
1610 #define TARGET_CAN_ELIMINATE rs6000_can_eliminate
1611
1612 #undef TARGET_CONDITIONAL_REGISTER_USAGE
1613 #define TARGET_CONDITIONAL_REGISTER_USAGE rs6000_conditional_register_usage
1614
1615 #undef TARGET_SCHED_REASSOCIATION_WIDTH
1616 #define TARGET_SCHED_REASSOCIATION_WIDTH rs6000_reassociation_width
1617
1618 #undef TARGET_TRAMPOLINE_INIT
1619 #define TARGET_TRAMPOLINE_INIT rs6000_trampoline_init
1620
1621 #undef TARGET_FUNCTION_VALUE
1622 #define TARGET_FUNCTION_VALUE rs6000_function_value
1623
1624 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
1625 #define TARGET_OPTION_VALID_ATTRIBUTE_P rs6000_valid_attribute_p
1626
1627 #undef TARGET_OPTION_SAVE
1628 #define TARGET_OPTION_SAVE rs6000_function_specific_save
1629
1630 #undef TARGET_OPTION_RESTORE
1631 #define TARGET_OPTION_RESTORE rs6000_function_specific_restore
1632
1633 #undef TARGET_OPTION_PRINT
1634 #define TARGET_OPTION_PRINT rs6000_function_specific_print
1635
1636 #undef TARGET_CAN_INLINE_P
1637 #define TARGET_CAN_INLINE_P rs6000_can_inline_p
1638
1639 #undef TARGET_SET_CURRENT_FUNCTION
1640 #define TARGET_SET_CURRENT_FUNCTION rs6000_set_current_function
1641
1642 #undef TARGET_LEGITIMATE_CONSTANT_P
1643 #define TARGET_LEGITIMATE_CONSTANT_P rs6000_legitimate_constant_p
1644
1645 #undef TARGET_VECTORIZE_VEC_PERM_CONST
1646 #define TARGET_VECTORIZE_VEC_PERM_CONST rs6000_vectorize_vec_perm_const
1647
1648 #undef TARGET_CAN_USE_DOLOOP_P
1649 #define TARGET_CAN_USE_DOLOOP_P can_use_doloop_if_innermost
1650
1651 #undef TARGET_PREDICT_DOLOOP_P
1652 #define TARGET_PREDICT_DOLOOP_P rs6000_predict_doloop_p
1653
1654 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
1655 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV rs6000_atomic_assign_expand_fenv
1656
1657 #undef TARGET_LIBGCC_CMP_RETURN_MODE
1658 #define TARGET_LIBGCC_CMP_RETURN_MODE rs6000_abi_word_mode
1659 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
1660 #define TARGET_LIBGCC_SHIFT_COUNT_MODE rs6000_abi_word_mode
1661 #undef TARGET_UNWIND_WORD_MODE
1662 #define TARGET_UNWIND_WORD_MODE rs6000_abi_word_mode
1663
1664 #undef TARGET_OFFLOAD_OPTIONS
1665 #define TARGET_OFFLOAD_OPTIONS rs6000_offload_options
1666
1667 #undef TARGET_C_MODE_FOR_SUFFIX
1668 #define TARGET_C_MODE_FOR_SUFFIX rs6000_c_mode_for_suffix
1669
1670 #undef TARGET_INVALID_BINARY_OP
1671 #define TARGET_INVALID_BINARY_OP rs6000_invalid_binary_op
1672
1673 #undef TARGET_OPTAB_SUPPORTED_P
1674 #define TARGET_OPTAB_SUPPORTED_P rs6000_optab_supported_p
1675
1676 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
1677 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
1678
1679 #undef TARGET_COMPARE_VERSION_PRIORITY
1680 #define TARGET_COMPARE_VERSION_PRIORITY rs6000_compare_version_priority
1681
1682 #undef TARGET_GENERATE_VERSION_DISPATCHER_BODY
1683 #define TARGET_GENERATE_VERSION_DISPATCHER_BODY                         \
1684   rs6000_generate_version_dispatcher_body
1685
1686 #undef TARGET_GET_FUNCTION_VERSIONS_DISPATCHER
1687 #define TARGET_GET_FUNCTION_VERSIONS_DISPATCHER                         \
1688   rs6000_get_function_versions_dispatcher
1689
1690 #undef TARGET_OPTION_FUNCTION_VERSIONS
1691 #define TARGET_OPTION_FUNCTION_VERSIONS common_function_versions
1692
1693 #undef TARGET_HARD_REGNO_NREGS
1694 #define TARGET_HARD_REGNO_NREGS rs6000_hard_regno_nregs_hook
1695 #undef TARGET_HARD_REGNO_MODE_OK
1696 #define TARGET_HARD_REGNO_MODE_OK rs6000_hard_regno_mode_ok
1697
1698 #undef TARGET_MODES_TIEABLE_P
1699 #define TARGET_MODES_TIEABLE_P rs6000_modes_tieable_p
1700
1701 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
1702 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
1703   rs6000_hard_regno_call_part_clobbered
1704
1705 #undef TARGET_SLOW_UNALIGNED_ACCESS
1706 #define TARGET_SLOW_UNALIGNED_ACCESS rs6000_slow_unaligned_access
1707
1708 #undef TARGET_CAN_CHANGE_MODE_CLASS
1709 #define TARGET_CAN_CHANGE_MODE_CLASS rs6000_can_change_mode_class
1710
1711 #undef TARGET_CONSTANT_ALIGNMENT
1712 #define TARGET_CONSTANT_ALIGNMENT rs6000_constant_alignment
1713
1714 #undef TARGET_STARTING_FRAME_OFFSET
1715 #define TARGET_STARTING_FRAME_OFFSET rs6000_starting_frame_offset
1716
1717 #if TARGET_ELF && RS6000_WEAK
1718 #undef TARGET_ASM_GLOBALIZE_DECL_NAME
1719 #define TARGET_ASM_GLOBALIZE_DECL_NAME rs6000_globalize_decl_name
1720 #endif
1721
1722 #undef TARGET_SETJMP_PRESERVES_NONVOLATILE_REGS_P
1723 #define TARGET_SETJMP_PRESERVES_NONVOLATILE_REGS_P hook_bool_void_true
1724
1725 #undef TARGET_MANGLE_DECL_ASSEMBLER_NAME
1726 #define TARGET_MANGLE_DECL_ASSEMBLER_NAME rs6000_mangle_decl_assembler_name
1727 \f
1728
1729 /* Processor table.  */
1730 struct rs6000_ptt
1731 {
1732   const char *const name;               /* Canonical processor name.  */
1733   const enum processor_type processor;  /* Processor type enum value.  */
1734   const HOST_WIDE_INT target_enable;    /* Target flags to enable.  */
1735 };
1736
1737 static struct rs6000_ptt const processor_target_table[] =
1738 {
1739 #define RS6000_CPU(NAME, CPU, FLAGS) { NAME, CPU, FLAGS },
1740 #include "rs6000-cpus.def"
1741 #undef RS6000_CPU
1742 };
1743
1744 /* Look up a processor name for -mcpu=xxx and -mtune=xxx.  Return -1 if the
1745    name is invalid.  */
1746
1747 static int
1748 rs6000_cpu_name_lookup (const char *name)
1749 {
1750   size_t i;
1751
1752   if (name != NULL)
1753     {
1754       for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
1755         if (! strcmp (name, processor_target_table[i].name))
1756           return (int)i;
1757     }
1758
1759   return -1;
1760 }
1761
1762 \f
1763 /* Return number of consecutive hard regs needed starting at reg REGNO
1764    to hold something of mode MODE.
1765    This is ordinarily the length in words of a value of mode MODE
1766    but can be less for certain modes in special long registers.
1767
1768    POWER and PowerPC GPRs hold 32 bits worth;
1769    PowerPC64 GPRs and FPRs point register holds 64 bits worth.  */
1770
1771 static int
1772 rs6000_hard_regno_nregs_internal (int regno, machine_mode mode)
1773 {
1774   unsigned HOST_WIDE_INT reg_size;
1775
1776   /* 128-bit floating point usually takes 2 registers, unless it is IEEE
1777      128-bit floating point that can go in vector registers, which has VSX
1778      memory addressing.  */
1779   if (FP_REGNO_P (regno))
1780     reg_size = (VECTOR_MEM_VSX_P (mode) || FLOAT128_VECTOR_P (mode)
1781                 ? UNITS_PER_VSX_WORD
1782                 : UNITS_PER_FP_WORD);
1783
1784   else if (ALTIVEC_REGNO_P (regno))
1785     reg_size = UNITS_PER_ALTIVEC_WORD;
1786
1787   else
1788     reg_size = UNITS_PER_WORD;
1789
1790   return (GET_MODE_SIZE (mode) + reg_size - 1) / reg_size;
1791 }
1792
1793 /* Value is 1 if hard register REGNO can hold a value of machine-mode
1794    MODE.  */
1795 static int
1796 rs6000_hard_regno_mode_ok_uncached (int regno, machine_mode mode)
1797 {
1798   int last_regno = regno + rs6000_hard_regno_nregs[mode][regno] - 1;
1799
1800   if (COMPLEX_MODE_P (mode))
1801     mode = GET_MODE_INNER (mode);
1802
1803   /* PTImode can only go in GPRs.  Quad word memory operations require even/odd
1804      register combinations, and use PTImode where we need to deal with quad
1805      word memory operations.  Don't allow quad words in the argument or frame
1806      pointer registers, just registers 0..31.  */
1807   if (mode == PTImode)
1808     return (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1809             && IN_RANGE (last_regno, FIRST_GPR_REGNO, LAST_GPR_REGNO)
1810             && ((regno & 1) == 0));
1811
1812   /* VSX registers that overlap the FPR registers are larger than for non-VSX
1813      implementations.  Don't allow an item to be split between a FP register
1814      and an Altivec register.  Allow TImode in all VSX registers if the user
1815      asked for it.  */
1816   if (TARGET_VSX && VSX_REGNO_P (regno)
1817       && (VECTOR_MEM_VSX_P (mode)
1818           || FLOAT128_VECTOR_P (mode)
1819           || reg_addr[mode].scalar_in_vmx_p
1820           || mode == TImode
1821           || (TARGET_VADDUQM && mode == V1TImode)))
1822     {
1823       if (FP_REGNO_P (regno))
1824         return FP_REGNO_P (last_regno);
1825
1826       if (ALTIVEC_REGNO_P (regno))
1827         {
1828           if (GET_MODE_SIZE (mode) != 16 && !reg_addr[mode].scalar_in_vmx_p)
1829             return 0;
1830
1831           return ALTIVEC_REGNO_P (last_regno);
1832         }
1833     }
1834
1835   /* The GPRs can hold any mode, but values bigger than one register
1836      cannot go past R31.  */
1837   if (INT_REGNO_P (regno))
1838     return INT_REGNO_P (last_regno);
1839
1840   /* The float registers (except for VSX vector modes) can only hold floating
1841      modes and DImode.  */
1842   if (FP_REGNO_P (regno))
1843     {
1844       if (FLOAT128_VECTOR_P (mode))
1845         return false;
1846
1847       if (SCALAR_FLOAT_MODE_P (mode)
1848           && (mode != TDmode || (regno % 2) == 0)
1849           && FP_REGNO_P (last_regno))
1850         return 1;
1851
1852       if (GET_MODE_CLASS (mode) == MODE_INT)
1853         {
1854           if(GET_MODE_SIZE (mode) == UNITS_PER_FP_WORD)
1855             return 1;
1856
1857           if (TARGET_P8_VECTOR && (mode == SImode))
1858             return 1;
1859
1860           if (TARGET_P9_VECTOR && (mode == QImode || mode == HImode))
1861             return 1;
1862         }
1863
1864       return 0;
1865     }
1866
1867   /* The CR register can only hold CC modes.  */
1868   if (CR_REGNO_P (regno))
1869     return GET_MODE_CLASS (mode) == MODE_CC;
1870
1871   if (CA_REGNO_P (regno))
1872     return mode == Pmode || mode == SImode;
1873
1874   /* AltiVec only in AldyVec registers.  */
1875   if (ALTIVEC_REGNO_P (regno))
1876     return (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode)
1877             || mode == V1TImode);
1878
1879   /* We cannot put non-VSX TImode or PTImode anywhere except general register
1880      and it must be able to fit within the register set.  */
1881
1882   return GET_MODE_SIZE (mode) <= UNITS_PER_WORD;
1883 }
1884
1885 /* Implement TARGET_HARD_REGNO_NREGS.  */
1886
1887 static unsigned int
1888 rs6000_hard_regno_nregs_hook (unsigned int regno, machine_mode mode)
1889 {
1890   return rs6000_hard_regno_nregs[mode][regno];
1891 }
1892
1893 /* Implement TARGET_HARD_REGNO_MODE_OK.  */
1894
1895 static bool
1896 rs6000_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
1897 {
1898   return rs6000_hard_regno_mode_ok_p[mode][regno];
1899 }
1900
1901 /* Implement TARGET_MODES_TIEABLE_P.
1902
1903    PTImode cannot tie with other modes because PTImode is restricted to even
1904    GPR registers, and TImode can go in any GPR as well as VSX registers (PR
1905    57744).
1906
1907    Altivec/VSX vector tests were moved ahead of scalar float mode, so that IEEE
1908    128-bit floating point on VSX systems ties with other vectors.  */
1909
1910 static bool
1911 rs6000_modes_tieable_p (machine_mode mode1, machine_mode mode2)
1912 {
1913   if (mode1 == PTImode)
1914     return mode2 == PTImode;
1915   if (mode2 == PTImode)
1916     return false;
1917
1918   if (ALTIVEC_OR_VSX_VECTOR_MODE (mode1))
1919     return ALTIVEC_OR_VSX_VECTOR_MODE (mode2);
1920   if (ALTIVEC_OR_VSX_VECTOR_MODE (mode2))
1921     return false;
1922
1923   if (SCALAR_FLOAT_MODE_P (mode1))
1924     return SCALAR_FLOAT_MODE_P (mode2);
1925   if (SCALAR_FLOAT_MODE_P (mode2))
1926     return false;
1927
1928   if (GET_MODE_CLASS (mode1) == MODE_CC)
1929     return GET_MODE_CLASS (mode2) == MODE_CC;
1930   if (GET_MODE_CLASS (mode2) == MODE_CC)
1931     return false;
1932
1933   return true;
1934 }
1935
1936 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED.  */
1937
1938 static bool
1939 rs6000_hard_regno_call_part_clobbered (rtx_insn *insn ATTRIBUTE_UNUSED,
1940                                        unsigned int regno, machine_mode mode)
1941 {
1942   if (TARGET_32BIT
1943       && TARGET_POWERPC64
1944       && GET_MODE_SIZE (mode) > 4
1945       && INT_REGNO_P (regno))
1946     return true;
1947
1948   if (TARGET_VSX
1949       && FP_REGNO_P (regno)
1950       && GET_MODE_SIZE (mode) > 8
1951       && !FLOAT128_2REG_P (mode))
1952     return true;
1953
1954   return false;
1955 }
1956
1957 /* Print interesting facts about registers.  */
1958 static void
1959 rs6000_debug_reg_print (int first_regno, int last_regno, const char *reg_name)
1960 {
1961   int r, m;
1962
1963   for (r = first_regno; r <= last_regno; ++r)
1964     {
1965       const char *comma = "";
1966       int len;
1967
1968       if (first_regno == last_regno)
1969         fprintf (stderr, "%s:\t", reg_name);
1970       else
1971         fprintf (stderr, "%s%d:\t", reg_name, r - first_regno);
1972
1973       len = 8;
1974       for (m = 0; m < NUM_MACHINE_MODES; ++m)
1975         if (rs6000_hard_regno_mode_ok_p[m][r] && rs6000_hard_regno_nregs[m][r])
1976           {
1977             if (len > 70)
1978               {
1979                 fprintf (stderr, ",\n\t");
1980                 len = 8;
1981                 comma = "";
1982               }
1983
1984             if (rs6000_hard_regno_nregs[m][r] > 1)
1985               len += fprintf (stderr, "%s%s/%d", comma, GET_MODE_NAME (m),
1986                              rs6000_hard_regno_nregs[m][r]);
1987             else
1988               len += fprintf (stderr, "%s%s", comma, GET_MODE_NAME (m));
1989
1990             comma = ", ";
1991           }
1992
1993       if (call_used_regs[r])
1994         {
1995           if (len > 70)
1996             {
1997               fprintf (stderr, ",\n\t");
1998               len = 8;
1999               comma = "";
2000             }
2001
2002           len += fprintf (stderr, "%s%s", comma, "call-used");
2003           comma = ", ";
2004         }
2005
2006       if (fixed_regs[r])
2007         {
2008           if (len > 70)
2009             {
2010               fprintf (stderr, ",\n\t");
2011               len = 8;
2012               comma = "";
2013             }
2014
2015           len += fprintf (stderr, "%s%s", comma, "fixed");
2016           comma = ", ";
2017         }
2018
2019       if (len > 70)
2020         {
2021           fprintf (stderr, ",\n\t");
2022           comma = "";
2023         }
2024
2025       len += fprintf (stderr, "%sreg-class = %s", comma,
2026                       reg_class_names[(int)rs6000_regno_regclass[r]]);
2027       comma = ", ";
2028
2029       if (len > 70)
2030         {
2031           fprintf (stderr, ",\n\t");
2032           comma = "";
2033         }
2034
2035       fprintf (stderr, "%sregno = %d\n", comma, r);
2036     }
2037 }
2038
2039 static const char *
2040 rs6000_debug_vector_unit (enum rs6000_vector v)
2041 {
2042   const char *ret;
2043
2044   switch (v)
2045     {
2046     case VECTOR_NONE:      ret = "none";      break;
2047     case VECTOR_ALTIVEC:   ret = "altivec";   break;
2048     case VECTOR_VSX:       ret = "vsx";       break;
2049     case VECTOR_P8_VECTOR: ret = "p8_vector"; break;
2050     default:               ret = "unknown";   break;
2051     }
2052
2053   return ret;
2054 }
2055
2056 /* Inner function printing just the address mask for a particular reload
2057    register class.  */
2058 DEBUG_FUNCTION char *
2059 rs6000_debug_addr_mask (addr_mask_type mask, bool keep_spaces)
2060 {
2061   static char ret[8];
2062   char *p = ret;
2063
2064   if ((mask & RELOAD_REG_VALID) != 0)
2065     *p++ = 'v';
2066   else if (keep_spaces)
2067     *p++ = ' ';
2068
2069   if ((mask & RELOAD_REG_MULTIPLE) != 0)
2070     *p++ = 'm';
2071   else if (keep_spaces)
2072     *p++ = ' ';
2073
2074   if ((mask & RELOAD_REG_INDEXED) != 0)
2075     *p++ = 'i';
2076   else if (keep_spaces)
2077     *p++ = ' ';
2078
2079   if ((mask & RELOAD_REG_QUAD_OFFSET) != 0)
2080     *p++ = 'O';
2081   else if ((mask & RELOAD_REG_OFFSET) != 0)
2082     *p++ = 'o';
2083   else if (keep_spaces)
2084     *p++ = ' ';
2085
2086   if ((mask & RELOAD_REG_PRE_INCDEC) != 0)
2087     *p++ = '+';
2088   else if (keep_spaces)
2089     *p++ = ' ';
2090
2091   if ((mask & RELOAD_REG_PRE_MODIFY) != 0)
2092     *p++ = '+';
2093   else if (keep_spaces)
2094     *p++ = ' ';
2095
2096   if ((mask & RELOAD_REG_AND_M16) != 0)
2097     *p++ = '&';
2098   else if (keep_spaces)
2099     *p++ = ' ';
2100
2101   *p = '\0';
2102
2103   return ret;
2104 }
2105
2106 /* Print the address masks in a human readble fashion.  */
2107 DEBUG_FUNCTION void
2108 rs6000_debug_print_mode (ssize_t m)
2109 {
2110   ssize_t rc;
2111   int spaces = 0;
2112
2113   fprintf (stderr, "Mode: %-5s", GET_MODE_NAME (m));
2114   for (rc = 0; rc < N_RELOAD_REG; rc++)
2115     fprintf (stderr, " %s: %s", reload_reg_map[rc].name,
2116              rs6000_debug_addr_mask (reg_addr[m].addr_mask[rc], true));
2117
2118   if ((reg_addr[m].reload_store != CODE_FOR_nothing)
2119       || (reg_addr[m].reload_load != CODE_FOR_nothing))
2120     {
2121       fprintf (stderr, "%*s  Reload=%c%c", spaces, "",
2122                (reg_addr[m].reload_store != CODE_FOR_nothing) ? 's' : '*',
2123                (reg_addr[m].reload_load != CODE_FOR_nothing) ? 'l' : '*');
2124       spaces = 0;
2125     }
2126   else
2127     spaces += sizeof ("  Reload=sl") - 1;
2128
2129   if (reg_addr[m].scalar_in_vmx_p)
2130     {
2131       fprintf (stderr, "%*s  Upper=y", spaces, "");
2132       spaces = 0;
2133     }
2134   else
2135     spaces += sizeof ("  Upper=y") - 1;
2136
2137   if (rs6000_vector_unit[m] != VECTOR_NONE
2138       || rs6000_vector_mem[m] != VECTOR_NONE)
2139     {
2140       fprintf (stderr, "%*s  vector: arith=%-10s mem=%s",
2141                spaces, "",
2142                rs6000_debug_vector_unit (rs6000_vector_unit[m]),
2143                rs6000_debug_vector_unit (rs6000_vector_mem[m]));
2144     }
2145
2146   fputs ("\n", stderr);
2147 }
2148
2149 #define DEBUG_FMT_ID "%-32s= "
2150 #define DEBUG_FMT_D   DEBUG_FMT_ID "%d\n"
2151 #define DEBUG_FMT_WX  DEBUG_FMT_ID "%#.12" HOST_WIDE_INT_PRINT "x: "
2152 #define DEBUG_FMT_S   DEBUG_FMT_ID "%s\n"
2153
2154 /* Print various interesting information with -mdebug=reg.  */
2155 static void
2156 rs6000_debug_reg_global (void)
2157 {
2158   static const char *const tf[2] = { "false", "true" };
2159   const char *nl = (const char *)0;
2160   int m;
2161   size_t m1, m2, v;
2162   char costly_num[20];
2163   char nop_num[20];
2164   char flags_buffer[40];
2165   const char *costly_str;
2166   const char *nop_str;
2167   const char *trace_str;
2168   const char *abi_str;
2169   const char *cmodel_str;
2170   struct cl_target_option cl_opts;
2171
2172   /* Modes we want tieable information on.  */
2173   static const machine_mode print_tieable_modes[] = {
2174     QImode,
2175     HImode,
2176     SImode,
2177     DImode,
2178     TImode,
2179     PTImode,
2180     SFmode,
2181     DFmode,
2182     TFmode,
2183     IFmode,
2184     KFmode,
2185     SDmode,
2186     DDmode,
2187     TDmode,
2188     V16QImode,
2189     V8HImode,
2190     V4SImode,
2191     V2DImode,
2192     V1TImode,
2193     V32QImode,
2194     V16HImode,
2195     V8SImode,
2196     V4DImode,
2197     V2TImode,
2198     V4SFmode,
2199     V2DFmode,
2200     V8SFmode,
2201     V4DFmode,
2202     CCmode,
2203     CCUNSmode,
2204     CCEQmode,
2205   };
2206
2207   /* Virtual regs we are interested in.  */
2208   const static struct {
2209     int regno;                  /* register number.  */
2210     const char *name;           /* register name.  */
2211   } virtual_regs[] = {
2212     { STACK_POINTER_REGNUM,                     "stack pointer:" },
2213     { TOC_REGNUM,                               "toc:          " },
2214     { STATIC_CHAIN_REGNUM,                      "static chain: " },
2215     { RS6000_PIC_OFFSET_TABLE_REGNUM,           "pic offset:   " },
2216     { HARD_FRAME_POINTER_REGNUM,                "hard frame:   " },
2217     { ARG_POINTER_REGNUM,                       "arg pointer:  " },
2218     { FRAME_POINTER_REGNUM,                     "frame pointer:" },
2219     { FIRST_PSEUDO_REGISTER,                    "first pseudo: " },
2220     { FIRST_VIRTUAL_REGISTER,                   "first virtual:" },
2221     { VIRTUAL_INCOMING_ARGS_REGNUM,             "incoming_args:" },
2222     { VIRTUAL_STACK_VARS_REGNUM,                "stack_vars:   " },
2223     { VIRTUAL_STACK_DYNAMIC_REGNUM,             "stack_dynamic:" },
2224     { VIRTUAL_OUTGOING_ARGS_REGNUM,             "outgoing_args:" },
2225     { VIRTUAL_CFA_REGNUM,                       "cfa (frame):  " },
2226     { VIRTUAL_PREFERRED_STACK_BOUNDARY_REGNUM,  "stack boundry:" },
2227     { LAST_VIRTUAL_REGISTER,                    "last virtual: " },
2228   };
2229
2230   fputs ("\nHard register information:\n", stderr);
2231   rs6000_debug_reg_print (FIRST_GPR_REGNO, LAST_GPR_REGNO, "gr");
2232   rs6000_debug_reg_print (FIRST_FPR_REGNO, LAST_FPR_REGNO, "fp");
2233   rs6000_debug_reg_print (FIRST_ALTIVEC_REGNO,
2234                           LAST_ALTIVEC_REGNO,
2235                           "vs");
2236   rs6000_debug_reg_print (LR_REGNO, LR_REGNO, "lr");
2237   rs6000_debug_reg_print (CTR_REGNO, CTR_REGNO, "ctr");
2238   rs6000_debug_reg_print (CR0_REGNO, CR7_REGNO, "cr");
2239   rs6000_debug_reg_print (CA_REGNO, CA_REGNO, "ca");
2240   rs6000_debug_reg_print (VRSAVE_REGNO, VRSAVE_REGNO, "vrsave");
2241   rs6000_debug_reg_print (VSCR_REGNO, VSCR_REGNO, "vscr");
2242
2243   fputs ("\nVirtual/stack/frame registers:\n", stderr);
2244   for (v = 0; v < ARRAY_SIZE (virtual_regs); v++)
2245     fprintf (stderr, "%s regno = %3d\n", virtual_regs[v].name, virtual_regs[v].regno);
2246
2247   fprintf (stderr,
2248            "\n"
2249            "d  reg_class = %s\n"
2250            "f  reg_class = %s\n"
2251            "v  reg_class = %s\n"
2252            "wa reg_class = %s\n"
2253            "we reg_class = %s\n"
2254            "wr reg_class = %s\n"
2255            "wx reg_class = %s\n"
2256            "wA reg_class = %s\n"
2257            "\n",
2258            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_d]],
2259            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_f]],
2260            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_v]],
2261            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wa]],
2262            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_we]],
2263            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wr]],
2264            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wx]],
2265            reg_class_names[rs6000_constraints[RS6000_CONSTRAINT_wA]]);
2266
2267   nl = "\n";
2268   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2269     rs6000_debug_print_mode (m);
2270
2271   fputs ("\n", stderr);
2272
2273   for (m1 = 0; m1 < ARRAY_SIZE (print_tieable_modes); m1++)
2274     {
2275       machine_mode mode1 = print_tieable_modes[m1];
2276       bool first_time = true;
2277
2278       nl = (const char *)0;
2279       for (m2 = 0; m2 < ARRAY_SIZE (print_tieable_modes); m2++)
2280         {
2281           machine_mode mode2 = print_tieable_modes[m2];
2282           if (mode1 != mode2 && rs6000_modes_tieable_p (mode1, mode2))
2283             {
2284               if (first_time)
2285                 {
2286                   fprintf (stderr, "Tieable modes %s:", GET_MODE_NAME (mode1));
2287                   nl = "\n";
2288                   first_time = false;
2289                 }
2290
2291               fprintf (stderr, " %s", GET_MODE_NAME (mode2));
2292             }
2293         }
2294
2295       if (!first_time)
2296         fputs ("\n", stderr);
2297     }
2298
2299   if (nl)
2300     fputs (nl, stderr);
2301
2302   if (rs6000_recip_control)
2303     {
2304       fprintf (stderr, "\nReciprocal mask = 0x%x\n", rs6000_recip_control);
2305
2306       for (m = 0; m < NUM_MACHINE_MODES; ++m)
2307         if (rs6000_recip_bits[m])
2308           {
2309             fprintf (stderr,
2310                      "Reciprocal estimate mode: %-5s divide: %s rsqrt: %s\n",
2311                      GET_MODE_NAME (m),
2312                      (RS6000_RECIP_AUTO_RE_P (m)
2313                       ? "auto"
2314                       : (RS6000_RECIP_HAVE_RE_P (m) ? "have" : "none")),
2315                      (RS6000_RECIP_AUTO_RSQRTE_P (m)
2316                       ? "auto"
2317                       : (RS6000_RECIP_HAVE_RSQRTE_P (m) ? "have" : "none")));
2318           }
2319
2320       fputs ("\n", stderr);
2321     }
2322
2323   if (rs6000_cpu_index >= 0)
2324     {
2325       const char *name = processor_target_table[rs6000_cpu_index].name;
2326       HOST_WIDE_INT flags
2327         = processor_target_table[rs6000_cpu_index].target_enable;
2328
2329       sprintf (flags_buffer, "-mcpu=%s flags", name);
2330       rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2331     }
2332   else
2333     fprintf (stderr, DEBUG_FMT_S, "cpu", "<none>");
2334
2335   if (rs6000_tune_index >= 0)
2336     {
2337       const char *name = processor_target_table[rs6000_tune_index].name;
2338       HOST_WIDE_INT flags
2339         = processor_target_table[rs6000_tune_index].target_enable;
2340
2341       sprintf (flags_buffer, "-mtune=%s flags", name);
2342       rs6000_print_isa_options (stderr, 0, flags_buffer, flags);
2343     }
2344   else
2345     fprintf (stderr, DEBUG_FMT_S, "tune", "<none>");
2346
2347   cl_target_option_save (&cl_opts, &global_options);
2348   rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags",
2349                             rs6000_isa_flags);
2350
2351   rs6000_print_isa_options (stderr, 0, "rs6000_isa_flags_explicit",
2352                             rs6000_isa_flags_explicit);
2353
2354   rs6000_print_builtin_options (stderr, 0, "rs6000_builtin_mask",
2355                                 rs6000_builtin_mask);
2356
2357   rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
2358
2359   fprintf (stderr, DEBUG_FMT_S, "--with-cpu default",
2360            OPTION_TARGET_CPU_DEFAULT ? OPTION_TARGET_CPU_DEFAULT : "<none>");
2361
2362   switch (rs6000_sched_costly_dep)
2363     {
2364     case max_dep_latency:
2365       costly_str = "max_dep_latency";
2366       break;
2367
2368     case no_dep_costly:
2369       costly_str = "no_dep_costly";
2370       break;
2371
2372     case all_deps_costly:
2373       costly_str = "all_deps_costly";
2374       break;
2375
2376     case true_store_to_load_dep_costly:
2377       costly_str = "true_store_to_load_dep_costly";
2378       break;
2379
2380     case store_to_load_dep_costly:
2381       costly_str = "store_to_load_dep_costly";
2382       break;
2383
2384     default:
2385       costly_str = costly_num;
2386       sprintf (costly_num, "%d", (int)rs6000_sched_costly_dep);
2387       break;
2388     }
2389
2390   fprintf (stderr, DEBUG_FMT_S, "sched_costly_dep", costly_str);
2391
2392   switch (rs6000_sched_insert_nops)
2393     {
2394     case sched_finish_regroup_exact:
2395       nop_str = "sched_finish_regroup_exact";
2396       break;
2397
2398     case sched_finish_pad_groups:
2399       nop_str = "sched_finish_pad_groups";
2400       break;
2401
2402     case sched_finish_none:
2403       nop_str = "sched_finish_none";
2404       break;
2405
2406     default:
2407       nop_str = nop_num;
2408       sprintf (nop_num, "%d", (int)rs6000_sched_insert_nops);
2409       break;
2410     }
2411
2412   fprintf (stderr, DEBUG_FMT_S, "sched_insert_nops", nop_str);
2413
2414   switch (rs6000_sdata)
2415     {
2416     default:
2417     case SDATA_NONE:
2418       break;
2419
2420     case SDATA_DATA:
2421       fprintf (stderr, DEBUG_FMT_S, "sdata", "data");
2422       break;
2423
2424     case SDATA_SYSV:
2425       fprintf (stderr, DEBUG_FMT_S, "sdata", "sysv");
2426       break;
2427
2428     case SDATA_EABI:
2429       fprintf (stderr, DEBUG_FMT_S, "sdata", "eabi");
2430       break;
2431
2432     }
2433
2434   switch (rs6000_traceback)
2435     {
2436     case traceback_default:     trace_str = "default";  break;
2437     case traceback_none:        trace_str = "none";     break;
2438     case traceback_part:        trace_str = "part";     break;
2439     case traceback_full:        trace_str = "full";     break;
2440     default:                    trace_str = "unknown";  break;
2441     }
2442
2443   fprintf (stderr, DEBUG_FMT_S, "traceback", trace_str);
2444
2445   switch (rs6000_current_cmodel)
2446     {
2447     case CMODEL_SMALL:  cmodel_str = "small";   break;
2448     case CMODEL_MEDIUM: cmodel_str = "medium";  break;
2449     case CMODEL_LARGE:  cmodel_str = "large";   break;
2450     default:            cmodel_str = "unknown"; break;
2451     }
2452
2453   fprintf (stderr, DEBUG_FMT_S, "cmodel", cmodel_str);
2454
2455   switch (rs6000_current_abi)
2456     {
2457     case ABI_NONE:      abi_str = "none";       break;
2458     case ABI_AIX:       abi_str = "aix";        break;
2459     case ABI_ELFv2:     abi_str = "ELFv2";      break;
2460     case ABI_V4:        abi_str = "V4";         break;
2461     case ABI_DARWIN:    abi_str = "darwin";     break;
2462     default:            abi_str = "unknown";    break;
2463     }
2464
2465   fprintf (stderr, DEBUG_FMT_S, "abi", abi_str);
2466
2467   if (rs6000_altivec_abi)
2468     fprintf (stderr, DEBUG_FMT_S, "altivec_abi", "true");
2469
2470   if (rs6000_darwin64_abi)
2471     fprintf (stderr, DEBUG_FMT_S, "darwin64_abi", "true");
2472
2473   fprintf (stderr, DEBUG_FMT_S, "soft_float",
2474            (TARGET_SOFT_FLOAT ? "true" : "false"));
2475
2476   if (TARGET_LINK_STACK)
2477     fprintf (stderr, DEBUG_FMT_S, "link_stack", "true");
2478
2479   if (TARGET_P8_FUSION)
2480     {
2481       char options[80];
2482
2483       strcpy (options, "power8");
2484       if (TARGET_P8_FUSION_SIGN)
2485         strcat (options, ", sign");
2486
2487       fprintf (stderr, DEBUG_FMT_S, "fusion", options);
2488     }
2489
2490   fprintf (stderr, DEBUG_FMT_S, "plt-format",
2491            TARGET_SECURE_PLT ? "secure" : "bss");
2492   fprintf (stderr, DEBUG_FMT_S, "struct-return",
2493            aix_struct_return ? "aix" : "sysv");
2494   fprintf (stderr, DEBUG_FMT_S, "always_hint", tf[!!rs6000_always_hint]);
2495   fprintf (stderr, DEBUG_FMT_S, "sched_groups", tf[!!rs6000_sched_groups]);
2496   fprintf (stderr, DEBUG_FMT_S, "align_branch",
2497            tf[!!rs6000_align_branch_targets]);
2498   fprintf (stderr, DEBUG_FMT_D, "tls_size", rs6000_tls_size);
2499   fprintf (stderr, DEBUG_FMT_D, "long_double_size",
2500            rs6000_long_double_type_size);
2501   if (rs6000_long_double_type_size > 64)
2502     {
2503       fprintf (stderr, DEBUG_FMT_S, "long double type",
2504                TARGET_IEEEQUAD ? "IEEE" : "IBM");
2505       fprintf (stderr, DEBUG_FMT_S, "default long double type",
2506                TARGET_IEEEQUAD_DEFAULT ? "IEEE" : "IBM");
2507     }
2508   fprintf (stderr, DEBUG_FMT_D, "sched_restricted_insns_priority",
2509            (int)rs6000_sched_restricted_insns_priority);
2510   fprintf (stderr, DEBUG_FMT_D, "Number of standard builtins",
2511            (int)END_BUILTINS);
2512   fprintf (stderr, DEBUG_FMT_D, "Number of rs6000 builtins",
2513            (int)RS6000_BUILTIN_COUNT);
2514
2515   fprintf (stderr, DEBUG_FMT_D, "Enable float128 on VSX",
2516            (int)TARGET_FLOAT128_ENABLE_TYPE);
2517
2518   if (TARGET_VSX)
2519     fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit scalar element",
2520              (int)VECTOR_ELEMENT_SCALAR_64BIT);
2521
2522   if (TARGET_DIRECT_MOVE_128)
2523     fprintf (stderr, DEBUG_FMT_D, "VSX easy 64-bit mfvsrld element",
2524              (int)VECTOR_ELEMENT_MFVSRLD_64BIT);
2525 }
2526
2527 \f
2528 /* Update the addr mask bits in reg_addr to help secondary reload and go if
2529    legitimate address support to figure out the appropriate addressing to
2530    use.  */
2531
2532 static void
2533 rs6000_setup_reg_addr_masks (void)
2534 {
2535   ssize_t rc, reg, m, nregs;
2536   addr_mask_type any_addr_mask, addr_mask;
2537
2538   for (m = 0; m < NUM_MACHINE_MODES; ++m)
2539     {
2540       machine_mode m2 = (machine_mode) m;
2541       bool complex_p = false;
2542       bool small_int_p = (m2 == QImode || m2 == HImode || m2 == SImode);
2543       size_t msize;
2544
2545       if (COMPLEX_MODE_P (m2))
2546         {
2547           complex_p = true;
2548           m2 = GET_MODE_INNER (m2);
2549         }
2550
2551       msize = GET_MODE_SIZE (m2);
2552
2553       /* SDmode is special in that we want to access it only via REG+REG
2554          addressing on power7 and above, since we want to use the LFIWZX and
2555          STFIWZX instructions to load it.  */
2556       bool indexed_only_p = (m == SDmode && TARGET_NO_SDMODE_STACK);
2557
2558       any_addr_mask = 0;
2559       for (rc = FIRST_RELOAD_REG_CLASS; rc <= LAST_RELOAD_REG_CLASS; rc++)
2560         {
2561           addr_mask = 0;
2562           reg = reload_reg_map[rc].reg;
2563
2564           /* Can mode values go in the GPR/FPR/Altivec registers?  */
2565           if (reg >= 0 && rs6000_hard_regno_mode_ok_p[m][reg])
2566             {
2567               bool small_int_vsx_p = (small_int_p
2568                                       && (rc == RELOAD_REG_FPR
2569                                           || rc == RELOAD_REG_VMX));
2570
2571               nregs = rs6000_hard_regno_nregs[m][reg];
2572               addr_mask |= RELOAD_REG_VALID;
2573
2574               /* Indicate if the mode takes more than 1 physical register.  If
2575                  it takes a single register, indicate it can do REG+REG
2576                  addressing.  Small integers in VSX registers can only do
2577                  REG+REG addressing.  */
2578               if (small_int_vsx_p)
2579                 addr_mask |= RELOAD_REG_INDEXED;
2580               else if (nregs > 1 || m == BLKmode || complex_p)
2581                 addr_mask |= RELOAD_REG_MULTIPLE;
2582               else
2583                 addr_mask |= RELOAD_REG_INDEXED;
2584
2585               /* Figure out if we can do PRE_INC, PRE_DEC, or PRE_MODIFY
2586                  addressing.  If we allow scalars into Altivec registers,
2587                  don't allow PRE_INC, PRE_DEC, or PRE_MODIFY.
2588
2589                  For VSX systems, we don't allow update addressing for
2590                  DFmode/SFmode if those registers can go in both the
2591                  traditional floating point registers and Altivec registers.
2592                  The load/store instructions for the Altivec registers do not
2593                  have update forms.  If we allowed update addressing, it seems
2594                  to break IV-OPT code using floating point if the index type is
2595                  int instead of long (PR target/81550 and target/84042).  */
2596
2597               if (TARGET_UPDATE
2598                   && (rc == RELOAD_REG_GPR || rc == RELOAD_REG_FPR)
2599                   && msize <= 8
2600                   && !VECTOR_MODE_P (m2)
2601                   && !FLOAT128_VECTOR_P (m2)
2602                   && !complex_p
2603                   && (m != E_DFmode || !TARGET_VSX)
2604                   && (m != E_SFmode || !TARGET_P8_VECTOR)
2605                   && !small_int_vsx_p)
2606                 {
2607                   addr_mask |= RELOAD_REG_PRE_INCDEC;
2608
2609                   /* PRE_MODIFY is more restricted than PRE_INC/PRE_DEC in that
2610                      we don't allow PRE_MODIFY for some multi-register
2611                      operations.  */
2612                   switch (m)
2613                     {
2614                     default:
2615                       addr_mask |= RELOAD_REG_PRE_MODIFY;
2616                       break;
2617
2618                     case E_DImode:
2619                       if (TARGET_POWERPC64)
2620                         addr_mask |= RELOAD_REG_PRE_MODIFY;
2621                       break;
2622
2623                     case E_DFmode:
2624                     case E_DDmode:
2625                       if (TARGET_HARD_FLOAT)
2626                         addr_mask |= RELOAD_REG_PRE_MODIFY;
2627                       break;
2628                     }
2629                 }
2630             }
2631
2632           /* GPR and FPR registers can do REG+OFFSET addressing, except
2633              possibly for SDmode.  ISA 3.0 (i.e. power9) adds D-form addressing
2634              for 64-bit scalars and 32-bit SFmode to altivec registers.  */
2635           if ((addr_mask != 0) && !indexed_only_p
2636               && msize <= 8
2637               && (rc == RELOAD_REG_GPR
2638                   || ((msize == 8 || m2 == SFmode)
2639                       && (rc == RELOAD_REG_FPR
2640                           || (rc == RELOAD_REG_VMX && TARGET_P9_VECTOR)))))
2641             addr_mask |= RELOAD_REG_OFFSET;
2642
2643           /* VSX registers can do REG+OFFSET addresssing if ISA 3.0
2644              instructions are enabled.  The offset for 128-bit VSX registers is
2645              only 12-bits.  While GPRs can handle the full offset range, VSX
2646              registers can only handle the restricted range.  */
2647           else if ((addr_mask != 0) && !indexed_only_p
2648                    && msize == 16 && TARGET_P9_VECTOR
2649                    && (ALTIVEC_OR_VSX_VECTOR_MODE (m2)
2650                        || (m2 == TImode && TARGET_VSX)))
2651             {
2652               addr_mask |= RELOAD_REG_OFFSET;
2653               if (rc == RELOAD_REG_FPR || rc == RELOAD_REG_VMX)
2654                 addr_mask |= RELOAD_REG_QUAD_OFFSET;
2655             }
2656
2657           /* VMX registers can do (REG & -16) and ((REG+REG) & -16)
2658              addressing on 128-bit types.  */
2659           if (rc == RELOAD_REG_VMX && msize == 16
2660               && (addr_mask & RELOAD_REG_VALID) != 0)
2661             addr_mask |= RELOAD_REG_AND_M16;
2662
2663           reg_addr[m].addr_mask[rc] = addr_mask;
2664           any_addr_mask |= addr_mask;
2665         }
2666
2667       reg_addr[m].addr_mask[RELOAD_REG_ANY] = any_addr_mask;
2668     }
2669 }
2670
2671 \f
2672 /* Initialize the various global tables that are based on register size.  */
2673 static void
2674 rs6000_init_hard_regno_mode_ok (bool global_init_p)
2675 {
2676   ssize_t r, m, c;
2677   int align64;
2678   int align32;
2679
2680   /* Precalculate REGNO_REG_CLASS.  */
2681   rs6000_regno_regclass[0] = GENERAL_REGS;
2682   for (r = 1; r < 32; ++r)
2683     rs6000_regno_regclass[r] = BASE_REGS;
2684
2685   for (r = 32; r < 64; ++r)
2686     rs6000_regno_regclass[r] = FLOAT_REGS;
2687
2688   for (r = 64; HARD_REGISTER_NUM_P (r); ++r)
2689     rs6000_regno_regclass[r] = NO_REGS;
2690
2691   for (r = FIRST_ALTIVEC_REGNO; r <= LAST_ALTIVEC_REGNO; ++r)
2692     rs6000_regno_regclass[r] = ALTIVEC_REGS;
2693
2694   rs6000_regno_regclass[CR0_REGNO] = CR0_REGS;
2695   for (r = CR1_REGNO; r <= CR7_REGNO; ++r)
2696     rs6000_regno_regclass[r] = CR_REGS;
2697
2698   rs6000_regno_regclass[LR_REGNO] = LINK_REGS;
2699   rs6000_regno_regclass[CTR_REGNO] = CTR_REGS;
2700   rs6000_regno_regclass[CA_REGNO] = NO_REGS;
2701   rs6000_regno_regclass[VRSAVE_REGNO] = VRSAVE_REGS;
2702   rs6000_regno_regclass[VSCR_REGNO] = VRSAVE_REGS;
2703   rs6000_regno_regclass[ARG_POINTER_REGNUM] = BASE_REGS;
2704   rs6000_regno_regclass[FRAME_POINTER_REGNUM] = BASE_REGS;
2705
2706   /* Precalculate register class to simpler reload register class.  We don't
2707      need all of the register classes that are combinations of different
2708      classes, just the simple ones that have constraint letters.  */
2709   for (c = 0; c < N_REG_CLASSES; c++)
2710     reg_class_to_reg_type[c] = NO_REG_TYPE;
2711
2712   reg_class_to_reg_type[(int)GENERAL_REGS] = GPR_REG_TYPE;
2713   reg_class_to_reg_type[(int)BASE_REGS] = GPR_REG_TYPE;
2714   reg_class_to_reg_type[(int)VSX_REGS] = VSX_REG_TYPE;
2715   reg_class_to_reg_type[(int)VRSAVE_REGS] = SPR_REG_TYPE;
2716   reg_class_to_reg_type[(int)VSCR_REGS] = SPR_REG_TYPE;
2717   reg_class_to_reg_type[(int)LINK_REGS] = SPR_REG_TYPE;
2718   reg_class_to_reg_type[(int)CTR_REGS] = SPR_REG_TYPE;
2719   reg_class_to_reg_type[(int)LINK_OR_CTR_REGS] = SPR_REG_TYPE;
2720   reg_class_to_reg_type[(int)CR_REGS] = CR_REG_TYPE;
2721   reg_class_to_reg_type[(int)CR0_REGS] = CR_REG_TYPE;
2722
2723   if (TARGET_VSX)
2724     {
2725       reg_class_to_reg_type[(int)FLOAT_REGS] = VSX_REG_TYPE;
2726       reg_class_to_reg_type[(int)ALTIVEC_REGS] = VSX_REG_TYPE;
2727     }
2728   else
2729     {
2730       reg_class_to_reg_type[(int)FLOAT_REGS] = FPR_REG_TYPE;
2731       reg_class_to_reg_type[(int)ALTIVEC_REGS] = ALTIVEC_REG_TYPE;
2732     }
2733
2734   /* Precalculate the valid memory formats as well as the vector information,
2735      this must be set up before the rs6000_hard_regno_nregs_internal calls
2736      below.  */
2737   gcc_assert ((int)VECTOR_NONE == 0);
2738   memset ((void *) &rs6000_vector_unit[0], '\0', sizeof (rs6000_vector_unit));
2739   memset ((void *) &rs6000_vector_mem[0], '\0', sizeof (rs6000_vector_mem));
2740
2741   gcc_assert ((int)CODE_FOR_nothing == 0);
2742   memset ((void *) &reg_addr[0], '\0', sizeof (reg_addr));
2743
2744   gcc_assert ((int)NO_REGS == 0);
2745   memset ((void *) &rs6000_constraints[0], '\0', sizeof (rs6000_constraints));
2746
2747   /* The VSX hardware allows native alignment for vectors, but control whether the compiler
2748      believes it can use native alignment or still uses 128-bit alignment.  */
2749   if (TARGET_VSX && !TARGET_VSX_ALIGN_128)
2750     {
2751       align64 = 64;
2752       align32 = 32;
2753     }
2754   else
2755     {
2756       align64 = 128;
2757       align32 = 128;
2758     }
2759
2760   /* KF mode (IEEE 128-bit in VSX registers).  We do not have arithmetic, so
2761      only set the memory modes.  Include TFmode if -mabi=ieeelongdouble.  */
2762   if (TARGET_FLOAT128_TYPE)
2763     {
2764       rs6000_vector_mem[KFmode] = VECTOR_VSX;
2765       rs6000_vector_align[KFmode] = 128;
2766
2767       if (FLOAT128_IEEE_P (TFmode))
2768         {
2769           rs6000_vector_mem[TFmode] = VECTOR_VSX;
2770           rs6000_vector_align[TFmode] = 128;
2771         }
2772     }
2773
2774   /* V2DF mode, VSX only.  */
2775   if (TARGET_VSX)
2776     {
2777       rs6000_vector_unit[V2DFmode] = VECTOR_VSX;
2778       rs6000_vector_mem[V2DFmode] = VECTOR_VSX;
2779       rs6000_vector_align[V2DFmode] = align64;
2780     }
2781
2782   /* V4SF mode, either VSX or Altivec.  */
2783   if (TARGET_VSX)
2784     {
2785       rs6000_vector_unit[V4SFmode] = VECTOR_VSX;
2786       rs6000_vector_mem[V4SFmode] = VECTOR_VSX;
2787       rs6000_vector_align[V4SFmode] = align32;
2788     }
2789   else if (TARGET_ALTIVEC)
2790     {
2791       rs6000_vector_unit[V4SFmode] = VECTOR_ALTIVEC;
2792       rs6000_vector_mem[V4SFmode] = VECTOR_ALTIVEC;
2793       rs6000_vector_align[V4SFmode] = align32;
2794     }
2795
2796   /* V16QImode, V8HImode, V4SImode are Altivec only, but possibly do VSX loads
2797      and stores. */
2798   if (TARGET_ALTIVEC)
2799     {
2800       rs6000_vector_unit[V4SImode] = VECTOR_ALTIVEC;
2801       rs6000_vector_unit[V8HImode] = VECTOR_ALTIVEC;
2802       rs6000_vector_unit[V16QImode] = VECTOR_ALTIVEC;
2803       rs6000_vector_align[V4SImode] = align32;
2804       rs6000_vector_align[V8HImode] = align32;
2805       rs6000_vector_align[V16QImode] = align32;
2806
2807       if (TARGET_VSX)
2808         {
2809           rs6000_vector_mem[V4SImode] = VECTOR_VSX;
2810           rs6000_vector_mem[V8HImode] = VECTOR_VSX;
2811           rs6000_vector_mem[V16QImode] = VECTOR_VSX;
2812         }
2813       else
2814         {
2815           rs6000_vector_mem[V4SImode] = VECTOR_ALTIVEC;
2816           rs6000_vector_mem[V8HImode] = VECTOR_ALTIVEC;
2817           rs6000_vector_mem[V16QImode] = VECTOR_ALTIVEC;
2818         }
2819     }
2820
2821   /* V2DImode, full mode depends on ISA 2.07 vector mode.  Allow under VSX to
2822      do insert/splat/extract.  Altivec doesn't have 64-bit integer support.  */
2823   if (TARGET_VSX)
2824     {
2825       rs6000_vector_mem[V2DImode] = VECTOR_VSX;
2826       rs6000_vector_unit[V2DImode]
2827         = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2828       rs6000_vector_align[V2DImode] = align64;
2829
2830       rs6000_vector_mem[V1TImode] = VECTOR_VSX;
2831       rs6000_vector_unit[V1TImode]
2832         = (TARGET_P8_VECTOR) ? VECTOR_P8_VECTOR : VECTOR_NONE;
2833       rs6000_vector_align[V1TImode] = 128;
2834     }
2835
2836   /* DFmode, see if we want to use the VSX unit.  Memory is handled
2837      differently, so don't set rs6000_vector_mem.  */
2838   if (TARGET_VSX)
2839     {
2840       rs6000_vector_unit[DFmode] = VECTOR_VSX;
2841       rs6000_vector_align[DFmode] = 64;
2842     }
2843
2844   /* SFmode, see if we want to use the VSX unit.  */
2845   if (TARGET_P8_VECTOR)
2846     {
2847       rs6000_vector_unit[SFmode] = VECTOR_VSX;
2848       rs6000_vector_align[SFmode] = 32;
2849     }
2850
2851   /* Allow TImode in VSX register and set the VSX memory macros.  */
2852   if (TARGET_VSX)
2853     {
2854       rs6000_vector_mem[TImode] = VECTOR_VSX;
2855       rs6000_vector_align[TImode] = align64;
2856     }
2857
2858   /* Register class constraints for the constraints that depend on compile
2859      switches. When the VSX code was added, different constraints were added
2860      based on the type (DFmode, V2DFmode, V4SFmode).  For the vector types, all
2861      of the VSX registers are used.  The register classes for scalar floating
2862      point types is set, based on whether we allow that type into the upper
2863      (Altivec) registers.  GCC has register classes to target the Altivec
2864      registers for load/store operations, to select using a VSX memory
2865      operation instead of the traditional floating point operation.  The
2866      constraints are:
2867
2868         d  - Register class to use with traditional DFmode instructions.
2869         f  - Register class to use with traditional SFmode instructions.
2870         v  - Altivec register.
2871         wa - Any VSX register.
2872         wc - Reserved to represent individual CR bits (used in LLVM).
2873         wn - always NO_REGS.
2874         wr - GPR if 64-bit mode is permitted.
2875         wx - Float register if we can do 32-bit int stores.  */
2876
2877   if (TARGET_HARD_FLOAT)
2878     {
2879       rs6000_constraints[RS6000_CONSTRAINT_f] = FLOAT_REGS;     /* SFmode  */
2880       rs6000_constraints[RS6000_CONSTRAINT_d] = FLOAT_REGS;     /* DFmode  */
2881     }
2882
2883   if (TARGET_VSX)
2884     rs6000_constraints[RS6000_CONSTRAINT_wa] = VSX_REGS;
2885
2886   /* Add conditional constraints based on various options, to allow us to
2887      collapse multiple insn patterns.  */
2888   if (TARGET_ALTIVEC)
2889     rs6000_constraints[RS6000_CONSTRAINT_v] = ALTIVEC_REGS;
2890
2891   if (TARGET_POWERPC64)
2892     {
2893       rs6000_constraints[RS6000_CONSTRAINT_wr] = GENERAL_REGS;
2894       rs6000_constraints[RS6000_CONSTRAINT_wA] = BASE_REGS;
2895     }
2896
2897   if (TARGET_STFIWX)
2898     rs6000_constraints[RS6000_CONSTRAINT_wx] = FLOAT_REGS;      /* DImode  */
2899
2900   /* Support for new direct moves (ISA 3.0 + 64bit).  */
2901   if (TARGET_DIRECT_MOVE_128)
2902     rs6000_constraints[RS6000_CONSTRAINT_we] = VSX_REGS;
2903
2904   /* Set up the reload helper and direct move functions.  */
2905   if (TARGET_VSX || TARGET_ALTIVEC)
2906     {
2907       if (TARGET_64BIT)
2908         {
2909           reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_di_store;
2910           reg_addr[V16QImode].reload_load  = CODE_FOR_reload_v16qi_di_load;
2911           reg_addr[V8HImode].reload_store  = CODE_FOR_reload_v8hi_di_store;
2912           reg_addr[V8HImode].reload_load   = CODE_FOR_reload_v8hi_di_load;
2913           reg_addr[V4SImode].reload_store  = CODE_FOR_reload_v4si_di_store;
2914           reg_addr[V4SImode].reload_load   = CODE_FOR_reload_v4si_di_load;
2915           reg_addr[V2DImode].reload_store  = CODE_FOR_reload_v2di_di_store;
2916           reg_addr[V2DImode].reload_load   = CODE_FOR_reload_v2di_di_load;
2917           reg_addr[V1TImode].reload_store  = CODE_FOR_reload_v1ti_di_store;
2918           reg_addr[V1TImode].reload_load   = CODE_FOR_reload_v1ti_di_load;
2919           reg_addr[V4SFmode].reload_store  = CODE_FOR_reload_v4sf_di_store;
2920           reg_addr[V4SFmode].reload_load   = CODE_FOR_reload_v4sf_di_load;
2921           reg_addr[V2DFmode].reload_store  = CODE_FOR_reload_v2df_di_store;
2922           reg_addr[V2DFmode].reload_load   = CODE_FOR_reload_v2df_di_load;
2923           reg_addr[DFmode].reload_store    = CODE_FOR_reload_df_di_store;
2924           reg_addr[DFmode].reload_load     = CODE_FOR_reload_df_di_load;
2925           reg_addr[DDmode].reload_store    = CODE_FOR_reload_dd_di_store;
2926           reg_addr[DDmode].reload_load     = CODE_FOR_reload_dd_di_load;
2927           reg_addr[SFmode].reload_store    = CODE_FOR_reload_sf_di_store;
2928           reg_addr[SFmode].reload_load     = CODE_FOR_reload_sf_di_load;
2929
2930           if (FLOAT128_VECTOR_P (KFmode))
2931             {
2932               reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_di_store;
2933               reg_addr[KFmode].reload_load  = CODE_FOR_reload_kf_di_load;
2934             }
2935
2936           if (FLOAT128_VECTOR_P (TFmode))
2937             {
2938               reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_di_store;
2939               reg_addr[TFmode].reload_load  = CODE_FOR_reload_tf_di_load;
2940             }
2941
2942           /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
2943              available.  */
2944           if (TARGET_NO_SDMODE_STACK)
2945             {
2946               reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_di_store;
2947               reg_addr[SDmode].reload_load  = CODE_FOR_reload_sd_di_load;
2948             }
2949
2950           if (TARGET_VSX)
2951             {
2952               reg_addr[TImode].reload_store  = CODE_FOR_reload_ti_di_store;
2953               reg_addr[TImode].reload_load   = CODE_FOR_reload_ti_di_load;
2954             }
2955
2956           if (TARGET_DIRECT_MOVE && !TARGET_DIRECT_MOVE_128)
2957             {
2958               reg_addr[TImode].reload_gpr_vsx    = CODE_FOR_reload_gpr_from_vsxti;
2959               reg_addr[V1TImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv1ti;
2960               reg_addr[V2DFmode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv2df;
2961               reg_addr[V2DImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv2di;
2962               reg_addr[V4SFmode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv4sf;
2963               reg_addr[V4SImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv4si;
2964               reg_addr[V8HImode].reload_gpr_vsx  = CODE_FOR_reload_gpr_from_vsxv8hi;
2965               reg_addr[V16QImode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxv16qi;
2966               reg_addr[SFmode].reload_gpr_vsx    = CODE_FOR_reload_gpr_from_vsxsf;
2967
2968               reg_addr[TImode].reload_vsx_gpr    = CODE_FOR_reload_vsx_from_gprti;
2969               reg_addr[V1TImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv1ti;
2970               reg_addr[V2DFmode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv2df;
2971               reg_addr[V2DImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv2di;
2972               reg_addr[V4SFmode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv4sf;
2973               reg_addr[V4SImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv4si;
2974               reg_addr[V8HImode].reload_vsx_gpr  = CODE_FOR_reload_vsx_from_gprv8hi;
2975               reg_addr[V16QImode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprv16qi;
2976               reg_addr[SFmode].reload_vsx_gpr    = CODE_FOR_reload_vsx_from_gprsf;
2977
2978               if (FLOAT128_VECTOR_P (KFmode))
2979                 {
2980                   reg_addr[KFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxkf;
2981                   reg_addr[KFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprkf;
2982                 }
2983
2984               if (FLOAT128_VECTOR_P (TFmode))
2985                 {
2986                   reg_addr[TFmode].reload_gpr_vsx = CODE_FOR_reload_gpr_from_vsxtf;
2987                   reg_addr[TFmode].reload_vsx_gpr = CODE_FOR_reload_vsx_from_gprtf;
2988                 }
2989             }
2990         }
2991       else
2992         {
2993           reg_addr[V16QImode].reload_store = CODE_FOR_reload_v16qi_si_store;
2994           reg_addr[V16QImode].reload_load  = CODE_FOR_reload_v16qi_si_load;
2995           reg_addr[V8HImode].reload_store  = CODE_FOR_reload_v8hi_si_store;
2996           reg_addr[V8HImode].reload_load   = CODE_FOR_reload_v8hi_si_load;
2997           reg_addr[V4SImode].reload_store  = CODE_FOR_reload_v4si_si_store;
2998           reg_addr[V4SImode].reload_load   = CODE_FOR_reload_v4si_si_load;
2999           reg_addr[V2DImode].reload_store  = CODE_FOR_reload_v2di_si_store;
3000           reg_addr[V2DImode].reload_load   = CODE_FOR_reload_v2di_si_load;
3001           reg_addr[V1TImode].reload_store  = CODE_FOR_reload_v1ti_si_store;
3002           reg_addr[V1TImode].reload_load   = CODE_FOR_reload_v1ti_si_load;
3003           reg_addr[V4SFmode].reload_store  = CODE_FOR_reload_v4sf_si_store;
3004           reg_addr[V4SFmode].reload_load   = CODE_FOR_reload_v4sf_si_load;
3005           reg_addr[V2DFmode].reload_store  = CODE_FOR_reload_v2df_si_store;
3006           reg_addr[V2DFmode].reload_load   = CODE_FOR_reload_v2df_si_load;
3007           reg_addr[DFmode].reload_store    = CODE_FOR_reload_df_si_store;
3008           reg_addr[DFmode].reload_load     = CODE_FOR_reload_df_si_load;
3009           reg_addr[DDmode].reload_store    = CODE_FOR_reload_dd_si_store;
3010           reg_addr[DDmode].reload_load     = CODE_FOR_reload_dd_si_load;
3011           reg_addr[SFmode].reload_store    = CODE_FOR_reload_sf_si_store;
3012           reg_addr[SFmode].reload_load     = CODE_FOR_reload_sf_si_load;
3013
3014           if (FLOAT128_VECTOR_P (KFmode))
3015             {
3016               reg_addr[KFmode].reload_store = CODE_FOR_reload_kf_si_store;
3017               reg_addr[KFmode].reload_load  = CODE_FOR_reload_kf_si_load;
3018             }
3019
3020           if (FLOAT128_IEEE_P (TFmode))
3021             {
3022               reg_addr[TFmode].reload_store = CODE_FOR_reload_tf_si_store;
3023               reg_addr[TFmode].reload_load  = CODE_FOR_reload_tf_si_load;
3024             }
3025
3026           /* Only provide a reload handler for SDmode if lfiwzx/stfiwx are
3027              available.  */
3028           if (TARGET_NO_SDMODE_STACK)
3029             {
3030               reg_addr[SDmode].reload_store = CODE_FOR_reload_sd_si_store;
3031               reg_addr[SDmode].reload_load  = CODE_FOR_reload_sd_si_load;
3032             }
3033
3034           if (TARGET_VSX)
3035             {
3036               reg_addr[TImode].reload_store  = CODE_FOR_reload_ti_si_store;
3037               reg_addr[TImode].reload_load   = CODE_FOR_reload_ti_si_load;
3038             }
3039
3040           if (TARGET_DIRECT_MOVE)
3041             {
3042               reg_addr[DImode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdi;
3043               reg_addr[DDmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdd;
3044               reg_addr[DFmode].reload_fpr_gpr = CODE_FOR_reload_fpr_from_gprdf;
3045             }
3046         }
3047
3048       reg_addr[DFmode].scalar_in_vmx_p = true;
3049       reg_addr[DImode].scalar_in_vmx_p = true;
3050
3051       if (TARGET_P8_VECTOR)
3052         {
3053           reg_addr[SFmode].scalar_in_vmx_p = true;
3054           reg_addr[SImode].scalar_in_vmx_p = true;
3055
3056           if (TARGET_P9_VECTOR)
3057             {
3058               reg_addr[HImode].scalar_in_vmx_p = true;
3059               reg_addr[QImode].scalar_in_vmx_p = true;
3060             }
3061         }
3062     }
3063
3064   /* Precalculate HARD_REGNO_NREGS.  */
3065   for (r = 0; HARD_REGISTER_NUM_P (r); ++r)
3066     for (m = 0; m < NUM_MACHINE_MODES; ++m)
3067       rs6000_hard_regno_nregs[m][r]
3068         = rs6000_hard_regno_nregs_internal (r, (machine_mode) m);
3069
3070   /* Precalculate TARGET_HARD_REGNO_MODE_OK.  */
3071   for (r = 0; HARD_REGISTER_NUM_P (r); ++r)
3072     for (m = 0; m < NUM_MACHINE_MODES; ++m)
3073       rs6000_hard_regno_mode_ok_p[m][r]
3074         = rs6000_hard_regno_mode_ok_uncached (r, (machine_mode) m);
3075
3076   /* Precalculate CLASS_MAX_NREGS sizes.  */
3077   for (c = 0; c < LIM_REG_CLASSES; ++c)
3078     {
3079       int reg_size;
3080
3081       if (TARGET_VSX && VSX_REG_CLASS_P (c))
3082         reg_size = UNITS_PER_VSX_WORD;
3083
3084       else if (c == ALTIVEC_REGS)
3085         reg_size = UNITS_PER_ALTIVEC_WORD;
3086
3087       else if (c == FLOAT_REGS)
3088         reg_size = UNITS_PER_FP_WORD;
3089
3090       else
3091         reg_size = UNITS_PER_WORD;
3092
3093       for (m = 0; m < NUM_MACHINE_MODES; ++m)
3094         {
3095           machine_mode m2 = (machine_mode)m;
3096           int reg_size2 = reg_size;
3097
3098           /* TDmode & IBM 128-bit floating point always takes 2 registers, even
3099              in VSX.  */
3100           if (TARGET_VSX && VSX_REG_CLASS_P (c) && FLOAT128_2REG_P (m))
3101             reg_size2 = UNITS_PER_FP_WORD;
3102
3103           rs6000_class_max_nregs[m][c]
3104             = (GET_MODE_SIZE (m2) + reg_size2 - 1) / reg_size2;
3105         }
3106     }
3107
3108   /* Calculate which modes to automatically generate code to use a the
3109      reciprocal divide and square root instructions.  In the future, possibly
3110      automatically generate the instructions even if the user did not specify
3111      -mrecip.  The older machines double precision reciprocal sqrt estimate is
3112      not accurate enough.  */
3113   memset (rs6000_recip_bits, 0, sizeof (rs6000_recip_bits));
3114   if (TARGET_FRES)
3115     rs6000_recip_bits[SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3116   if (TARGET_FRE)
3117     rs6000_recip_bits[DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3118   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3119     rs6000_recip_bits[V4SFmode] = RS6000_RECIP_MASK_HAVE_RE;
3120   if (VECTOR_UNIT_VSX_P (V2DFmode))
3121     rs6000_recip_bits[V2DFmode] = RS6000_RECIP_MASK_HAVE_RE;
3122
3123   if (TARGET_FRSQRTES)
3124     rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3125   if (TARGET_FRSQRTE)
3126     rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3127   if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode))
3128     rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3129   if (VECTOR_UNIT_VSX_P (V2DFmode))
3130     rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_HAVE_RSQRTE;
3131
3132   if (rs6000_recip_control)
3133     {
3134       if (!flag_finite_math_only)
3135         warning (0, "%qs requires %qs or %qs", "-mrecip", "-ffinite-math",
3136                  "-ffast-math");
3137       if (flag_trapping_math)
3138         warning (0, "%qs requires %qs or %qs", "-mrecip",
3139                  "-fno-trapping-math", "-ffast-math");
3140       if (!flag_reciprocal_math)
3141         warning (0, "%qs requires %qs or %qs", "-mrecip", "-freciprocal-math",
3142                  "-ffast-math");
3143       if (flag_finite_math_only && !flag_trapping_math && flag_reciprocal_math)
3144         {
3145           if (RS6000_RECIP_HAVE_RE_P (SFmode)
3146               && (rs6000_recip_control & RECIP_SF_DIV) != 0)
3147             rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3148
3149           if (RS6000_RECIP_HAVE_RE_P (DFmode)
3150               && (rs6000_recip_control & RECIP_DF_DIV) != 0)
3151             rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3152
3153           if (RS6000_RECIP_HAVE_RE_P (V4SFmode)
3154               && (rs6000_recip_control & RECIP_V4SF_DIV) != 0)
3155             rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3156
3157           if (RS6000_RECIP_HAVE_RE_P (V2DFmode)
3158               && (rs6000_recip_control & RECIP_V2DF_DIV) != 0)
3159             rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RE;
3160
3161           if (RS6000_RECIP_HAVE_RSQRTE_P (SFmode)
3162               && (rs6000_recip_control & RECIP_SF_RSQRT) != 0)
3163             rs6000_recip_bits[SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3164
3165           if (RS6000_RECIP_HAVE_RSQRTE_P (DFmode)
3166               && (rs6000_recip_control & RECIP_DF_RSQRT) != 0)
3167             rs6000_recip_bits[DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3168
3169           if (RS6000_RECIP_HAVE_RSQRTE_P (V4SFmode)
3170               && (rs6000_recip_control & RECIP_V4SF_RSQRT) != 0)
3171             rs6000_recip_bits[V4SFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3172
3173           if (RS6000_RECIP_HAVE_RSQRTE_P (V2DFmode)
3174               && (rs6000_recip_control & RECIP_V2DF_RSQRT) != 0)
3175             rs6000_recip_bits[V2DFmode] |= RS6000_RECIP_MASK_AUTO_RSQRTE;
3176         }
3177     }
3178
3179   /* Update the addr mask bits in reg_addr to help secondary reload and go if
3180      legitimate address support to figure out the appropriate addressing to
3181      use.  */
3182   rs6000_setup_reg_addr_masks ();
3183
3184   if (global_init_p || TARGET_DEBUG_TARGET)
3185     {
3186       if (TARGET_DEBUG_REG)
3187         rs6000_debug_reg_global ();
3188
3189       if (TARGET_DEBUG_COST || TARGET_DEBUG_REG)
3190         fprintf (stderr,
3191                  "SImode variable mult cost       = %d\n"
3192                  "SImode constant mult cost       = %d\n"
3193                  "SImode short constant mult cost = %d\n"
3194                  "DImode multipliciation cost     = %d\n"
3195                  "SImode division cost            = %d\n"
3196                  "DImode division cost            = %d\n"
3197                  "Simple fp operation cost        = %d\n"
3198                  "DFmode multiplication cost      = %d\n"
3199                  "SFmode division cost            = %d\n"
3200                  "DFmode division cost            = %d\n"
3201                  "cache line size                 = %d\n"
3202                  "l1 cache size                   = %d\n"
3203                  "l2 cache size                   = %d\n"
3204                  "simultaneous prefetches         = %d\n"
3205                  "\n",
3206                  rs6000_cost->mulsi,
3207                  rs6000_cost->mulsi_const,
3208                  rs6000_cost->mulsi_const9,
3209                  rs6000_cost->muldi,
3210                  rs6000_cost->divsi,
3211                  rs6000_cost->divdi,
3212                  rs6000_cost->fp,
3213                  rs6000_cost->dmul,
3214                  rs6000_cost->sdiv,
3215                  rs6000_cost->ddiv,
3216                  rs6000_cost->cache_line_size,
3217                  rs6000_cost->l1_cache_size,
3218                  rs6000_cost->l2_cache_size,
3219                  rs6000_cost->simultaneous_prefetches);
3220     }
3221 }
3222
3223 #if TARGET_MACHO
3224 /* The Darwin version of SUBTARGET_OVERRIDE_OPTIONS.  */
3225
3226 static void
3227 darwin_rs6000_override_options (void)
3228 {
3229   /* The Darwin ABI always includes AltiVec, can't be (validly) turned
3230      off.  */
3231   rs6000_altivec_abi = 1;
3232   TARGET_ALTIVEC_VRSAVE = 1;
3233   rs6000_current_abi = ABI_DARWIN;
3234
3235   if (DEFAULT_ABI == ABI_DARWIN
3236       && TARGET_64BIT)
3237       darwin_one_byte_bool = 1;
3238
3239   if (TARGET_64BIT && ! TARGET_POWERPC64)
3240     {
3241       rs6000_isa_flags |= OPTION_MASK_POWERPC64;
3242       warning (0, "%qs requires PowerPC64 architecture, enabling", "-m64");
3243     }
3244
3245   /* The linkers [ld64] that support 64Bit do not need the JBSR longcall
3246      optimisation, and will not work with the most generic case (where the
3247      symbol is undefined external, but there is no symbl stub).  */
3248   if (TARGET_64BIT)
3249     rs6000_default_long_calls = 0;
3250
3251   /* ld_classic is (so far) still used for kernel (static) code, and supports
3252      the JBSR longcall / branch islands.  */
3253   if (flag_mkernel)
3254     {
3255       rs6000_default_long_calls = 1;
3256
3257       /* Allow a kext author to do -mkernel -mhard-float.  */
3258       if (! (rs6000_isa_flags_explicit & OPTION_MASK_SOFT_FLOAT))
3259         rs6000_isa_flags |= OPTION_MASK_SOFT_FLOAT;
3260     }
3261
3262   /* Make -m64 imply -maltivec.  Darwin's 64-bit ABI includes
3263      Altivec.  */
3264   if (!flag_mkernel && !flag_apple_kext
3265       && TARGET_64BIT
3266       && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC))
3267     rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3268
3269   /* Unless the user (not the configurer) has explicitly overridden
3270      it with -mcpu=G3 or -mno-altivec, then 10.5+ targets default to
3271      G4 unless targeting the kernel.  */
3272   if (!flag_mkernel
3273       && !flag_apple_kext
3274       && strverscmp (darwin_macosx_version_min, "10.5") >= 0
3275       && ! (rs6000_isa_flags_explicit & OPTION_MASK_ALTIVEC)
3276       && ! global_options_set.x_rs6000_cpu_index)
3277     {
3278       rs6000_isa_flags |= OPTION_MASK_ALTIVEC;
3279     }
3280 }
3281 #endif
3282
3283 /* If not otherwise specified by a target, make 'long double' equivalent to
3284    'double'.  */
3285
3286 #ifndef RS6000_DEFAULT_LONG_DOUBLE_SIZE
3287 #define RS6000_DEFAULT_LONG_DOUBLE_SIZE 64
3288 #endif
3289
3290 /* Return the builtin mask of the various options used that could affect which
3291    builtins were used.  In the past we used target_flags, but we've run out of
3292    bits, and some options are no longer in target_flags.  */
3293
3294 HOST_WIDE_INT
3295 rs6000_builtin_mask_calculate (void)
3296 {
3297   return (((TARGET_ALTIVEC)                 ? RS6000_BTM_ALTIVEC   : 0)
3298           | ((TARGET_CMPB)                  ? RS6000_BTM_CMPB      : 0)
3299           | ((TARGET_VSX)                   ? RS6000_BTM_VSX       : 0)
3300           | ((TARGET_FRE)                   ? RS6000_BTM_FRE       : 0)
3301           | ((TARGET_FRES)                  ? RS6000_BTM_FRES      : 0)
3302           | ((TARGET_FRSQRTE)               ? RS6000_BTM_FRSQRTE   : 0)
3303           | ((TARGET_FRSQRTES)              ? RS6000_BTM_FRSQRTES  : 0)
3304           | ((TARGET_POPCNTD)               ? RS6000_BTM_POPCNTD   : 0)
3305           | ((rs6000_cpu == PROCESSOR_CELL) ? RS6000_BTM_CELL      : 0)
3306           | ((TARGET_P8_VECTOR)             ? RS6000_BTM_P8_VECTOR : 0)
3307           | ((TARGET_P9_VECTOR)             ? RS6000_BTM_P9_VECTOR : 0)
3308           | ((TARGET_P9_MISC)               ? RS6000_BTM_P9_MISC   : 0)
3309           | ((TARGET_MODULO)                ? RS6000_BTM_MODULO    : 0)
3310           | ((TARGET_64BIT)                 ? RS6000_BTM_64BIT     : 0)
3311           | ((TARGET_POWERPC64)             ? RS6000_BTM_POWERPC64 : 0)
3312           | ((TARGET_CRYPTO)                ? RS6000_BTM_CRYPTO    : 0)
3313           | ((TARGET_HTM)                   ? RS6000_BTM_HTM       : 0)
3314           | ((TARGET_DFP)                   ? RS6000_BTM_DFP       : 0)
3315           | ((TARGET_HARD_FLOAT)            ? RS6000_BTM_HARD_FLOAT : 0)
3316           | ((TARGET_LONG_DOUBLE_128
3317               && TARGET_HARD_FLOAT
3318               && !TARGET_IEEEQUAD)          ? RS6000_BTM_LDBL128   : 0)
3319           | ((TARGET_FLOAT128_TYPE)         ? RS6000_BTM_FLOAT128  : 0)
3320           | ((TARGET_FLOAT128_HW)           ? RS6000_BTM_FLOAT128_HW : 0));
3321 }
3322
3323 /* Implement TARGET_MD_ASM_ADJUST.  All asm statements are considered
3324    to clobber the XER[CA] bit because clobbering that bit without telling
3325    the compiler worked just fine with versions of GCC before GCC 5, and
3326    breaking a lot of older code in ways that are hard to track down is
3327    not such a great idea.  */
3328
3329 static rtx_insn *
3330 rs6000_md_asm_adjust (vec<rtx> &/*outputs*/, vec<rtx> &/*inputs*/,
3331                       vec<const char *> &/*constraints*/,
3332                       vec<rtx> &clobbers, HARD_REG_SET &clobbered_regs)
3333 {
3334   clobbers.safe_push (gen_rtx_REG (SImode, CA_REGNO));
3335   SET_HARD_REG_BIT (clobbered_regs, CA_REGNO);
3336   return NULL;
3337 }
3338
3339 /* Override command line options.
3340
3341    Combine build-specific configuration information with options
3342    specified on the command line to set various state variables which
3343    influence code generation, optimization, and expansion of built-in
3344    functions.  Assure that command-line configuration preferences are
3345    compatible with each other and with the build configuration; issue
3346    warnings while adjusting configuration or error messages while
3347    rejecting configuration.
3348
3349    Upon entry to this function:
3350
3351      This function is called once at the beginning of
3352      compilation, and then again at the start and end of compiling
3353      each section of code that has a different configuration, as
3354      indicated, for example, by adding the
3355
3356        __attribute__((__target__("cpu=power9")))
3357
3358      qualifier to a function definition or, for example, by bracketing
3359      code between
3360
3361        #pragma GCC target("altivec")
3362
3363      and
3364
3365        #pragma GCC reset_options
3366
3367      directives.  Parameter global_init_p is true for the initial
3368      invocation, which initializes global variables, and false for all
3369      subsequent invocations.
3370
3371
3372      Various global state information is assumed to be valid.  This
3373      includes OPTION_TARGET_CPU_DEFAULT, representing the name of the
3374      default CPU specified at build configure time, TARGET_DEFAULT,
3375      representing the default set of option flags for the default
3376      target, and global_options_set.x_rs6000_isa_flags, representing
3377      which options were requested on the command line.
3378
3379    Upon return from this function:
3380
3381      rs6000_isa_flags_explicit has a non-zero bit for each flag that
3382      was set by name on the command line.  Additionally, if certain
3383      attributes are automatically enabled or disabled by this function
3384      in order to assure compatibility between options and
3385      configuration, the flags associated with those attributes are
3386      also set.  By setting these "explicit bits", we avoid the risk
3387      that other code might accidentally overwrite these particular
3388      attributes with "default values".
3389
3390      The various bits of rs6000_isa_flags are set to indicate the
3391      target options that have been selected for the most current
3392      compilation efforts.  This has the effect of also turning on the
3393      associated TARGET_XXX values since these are macros which are
3394      generally defined to test the corresponding bit of the
3395      rs6000_isa_flags variable.
3396
3397      The variable rs6000_builtin_mask is set to represent the target
3398      options for the most current compilation efforts, consistent with
3399      the current contents of rs6000_isa_flags.  This variable controls
3400      expansion of built-in functions.
3401
3402      Various other global variables and fields of global structures
3403      (over 50 in all) are initialized to reflect the desired options
3404      for the most current compilation efforts.  */
3405
3406 static bool
3407 rs6000_option_override_internal (bool global_init_p)
3408 {
3409   bool ret = true;
3410
3411   HOST_WIDE_INT set_masks;
3412   HOST_WIDE_INT ignore_masks;
3413   int cpu_index = -1;
3414   int tune_index;
3415   struct cl_target_option *main_target_opt
3416     = ((global_init_p || target_option_default_node == NULL)
3417        ? NULL : TREE_TARGET_OPTION (target_option_default_node));
3418
3419   /* Print defaults.  */
3420   if ((TARGET_DEBUG_REG || TARGET_DEBUG_TARGET) && global_init_p)
3421     rs6000_print_isa_options (stderr, 0, "TARGET_DEFAULT", TARGET_DEFAULT);
3422
3423   /* Remember the explicit arguments.  */
3424   if (global_init_p)
3425     rs6000_isa_flags_explicit = global_options_set.x_rs6000_isa_flags;
3426
3427   /* On 64-bit Darwin, power alignment is ABI-incompatible with some C
3428      library functions, so warn about it. The flag may be useful for
3429      performance studies from time to time though, so don't disable it
3430      entirely.  */
3431   if (global_options_set.x_rs6000_alignment_flags
3432       && rs6000_alignment_flags == MASK_ALIGN_POWER
3433       && DEFAULT_ABI == ABI_DARWIN
3434       && TARGET_64BIT)
3435     warning (0, "%qs is not supported for 64-bit Darwin;"
3436              " it is incompatible with the installed C and C++ libraries",
3437              "-malign-power");
3438
3439   /* Numerous experiment shows that IRA based loop pressure
3440      calculation works better for RTL loop invariant motion on targets
3441      with enough (>= 32) registers.  It is an expensive optimization.
3442      So it is on only for peak performance.  */
3443   if (optimize >= 3 && global_init_p
3444       && !global_options_set.x_flag_ira_loop_pressure)
3445     flag_ira_loop_pressure = 1;
3446
3447   /* -fsanitize=address needs to turn on -fasynchronous-unwind-tables in order
3448      for tracebacks to be complete but not if any -fasynchronous-unwind-tables
3449      options were already specified.  */
3450   if (flag_sanitize & SANITIZE_USER_ADDRESS
3451       && !global_options_set.x_flag_asynchronous_unwind_tables)
3452     flag_asynchronous_unwind_tables = 1;
3453
3454   /* -fvariable-expansion-in-unroller is a win for POWER whenever the
3455      loop unroller is active.  It is only checked during unrolling, so
3456      we can just set it on by default.  */
3457   if (!global_options_set.x_flag_variable_expansion_in_unroller)
3458     flag_variable_expansion_in_unroller = 1;
3459
3460   /* Set the pointer size.  */
3461   if (TARGET_64BIT)
3462     {
3463       rs6000_pmode = DImode;
3464       rs6000_pointer_size = 64;
3465     }
3466   else
3467     {
3468       rs6000_pmode = SImode;
3469       rs6000_pointer_size = 32;
3470     }
3471
3472   /* Some OSs don't support saving the high part of 64-bit registers on context
3473      switch.  Other OSs don't support saving Altivec registers.  On those OSs,
3474      we don't touch the OPTION_MASK_POWERPC64 or OPTION_MASK_ALTIVEC settings;
3475      if the user wants either, the user must explicitly specify them and we
3476      won't interfere with the user's specification.  */
3477
3478   set_masks = POWERPC_MASKS;
3479 #ifdef OS_MISSING_POWERPC64
3480   if (OS_MISSING_POWERPC64)
3481     set_masks &= ~OPTION_MASK_POWERPC64;
3482 #endif
3483 #ifdef OS_MISSING_ALTIVEC
3484   if (OS_MISSING_ALTIVEC)
3485     set_masks &= ~(OPTION_MASK_ALTIVEC | OPTION_MASK_VSX
3486                    | OTHER_VSX_VECTOR_MASKS);
3487 #endif
3488
3489   /* Don't override by the processor default if given explicitly.  */
3490   set_masks &= ~rs6000_isa_flags_explicit;
3491
3492   /* Process the -mcpu=<xxx> and -mtune=<xxx> argument.  If the user changed
3493      the cpu in a target attribute or pragma, but did not specify a tuning
3494      option, use the cpu for the tuning option rather than the option specified
3495      with -mtune on the command line.  Process a '--with-cpu' configuration
3496      request as an implicit --cpu.  */
3497   if (rs6000_cpu_index >= 0)
3498     cpu_index = rs6000_cpu_index;
3499   else if (main_target_opt != NULL && main_target_opt->x_rs6000_cpu_index >= 0)
3500     cpu_index = main_target_opt->x_rs6000_cpu_index;
3501   else if (OPTION_TARGET_CPU_DEFAULT)
3502     cpu_index = rs6000_cpu_name_lookup (OPTION_TARGET_CPU_DEFAULT);
3503
3504   /* If we have a cpu, either through an explicit -mcpu=<xxx> or if the
3505      compiler was configured with --with-cpu=<xxx>, replace all of the ISA bits
3506      with those from the cpu, except for options that were explicitly set.  If
3507      we don't have a cpu, do not override the target bits set in
3508      TARGET_DEFAULT.  */
3509   if (cpu_index >= 0)
3510     {
3511       rs6000_cpu_index = cpu_index;
3512       rs6000_isa_flags &= ~set_masks;
3513       rs6000_isa_flags |= (processor_target_table[cpu_index].target_enable
3514                            & set_masks);
3515     }
3516   else
3517     {
3518       /* If no -mcpu=<xxx>, inherit any default options that were cleared via
3519          POWERPC_MASKS.  Originally, TARGET_DEFAULT was used to initialize
3520          target_flags via the TARGET_DEFAULT_TARGET_FLAGS hook.  When we switched
3521          to using rs6000_isa_flags, we need to do the initialization here.
3522
3523          If there is a TARGET_DEFAULT, use that.  Otherwise fall back to using
3524          -mcpu=powerpc, -mcpu=powerpc64, or -mcpu=powerpc64le defaults.  */
3525       HOST_WIDE_INT flags;
3526       if (TARGET_DEFAULT)
3527         flags = TARGET_DEFAULT;
3528       else
3529         {
3530           /* PowerPC 64-bit LE requires at least ISA 2.07.  */
3531           const char *default_cpu = (!TARGET_POWERPC64
3532                                      ? "powerpc"
3533                                      : (BYTES_BIG_ENDIAN
3534                                         ? "powerpc64"
3535                                         : "powerpc64le"));
3536           int default_cpu_index = rs6000_cpu_name_lookup (default_cpu);
3537           flags = processor_target_table[default_cpu_index].target_enable;
3538         }
3539       rs6000_isa_flags |= (flags & ~rs6000_isa_flags_explicit);
3540     }
3541
3542   if (rs6000_tune_index >= 0)
3543     tune_index = rs6000_tune_index;
3544   else if (cpu_index >= 0)
3545     rs6000_tune_index = tune_index = cpu_index;
3546   else
3547     {
3548       size_t i;
3549       enum processor_type tune_proc
3550         = (TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT);
3551
3552       tune_index = -1;
3553       for (i = 0; i < ARRAY_SIZE (processor_target_table); i++)
3554         if (processor_target_table[i].processor == tune_proc)
3555           {
3556             tune_index = i;
3557             break;
3558           }
3559     }
3560
3561   if (cpu_index >= 0)
3562     rs6000_cpu = processor_target_table[cpu_index].processor;
3563   else
3564     rs6000_cpu = TARGET_POWERPC64 ? PROCESSOR_DEFAULT64 : PROCESSOR_DEFAULT;
3565
3566   gcc_assert (tune_index >= 0);
3567   rs6000_tune = processor_target_table[tune_index].processor;
3568
3569   if (rs6000_cpu == PROCESSOR_PPCE300C2 || rs6000_cpu == PROCESSOR_PPCE300C3
3570       || rs6000_cpu == PROCESSOR_PPCE500MC || rs6000_cpu == PROCESSOR_PPCE500MC64
3571       || rs6000_cpu == PROCESSOR_PPCE5500)
3572     {
3573       if (TARGET_ALTIVEC)
3574         error ("AltiVec not supported in this target");
3575     }
3576
3577   /* If we are optimizing big endian systems for space, use the load/store
3578      multiple instructions.  */
3579   if (BYTES_BIG_ENDIAN && optimize_size)
3580     rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE;
3581
3582   /* Don't allow -mmultiple on little endian systems unless the cpu is a 750,
3583      because the hardware doesn't support the instructions used in little
3584      endian mode, and causes an alignment trap.  The 750 does not cause an
3585      alignment trap (except when the target is unaligned).  */
3586
3587   if (!BYTES_BIG_ENDIAN && rs6000_cpu != PROCESSOR_PPC750 && TARGET_MULTIPLE)
3588     {
3589       rs6000_isa_flags &= ~OPTION_MASK_MULTIPLE;
3590       if ((rs6000_isa_flags_explicit & OPTION_MASK_MULTIPLE) != 0)
3591         warning (0, "%qs is not supported on little endian systems",
3592                  "-mmultiple");
3593     }
3594
3595   /* If little-endian, default to -mstrict-align on older processors.
3596      Testing for htm matches power8 and later.  */
3597   if (!BYTES_BIG_ENDIAN
3598       && !(processor_target_table[tune_index].target_enable & OPTION_MASK_HTM))
3599     rs6000_isa_flags |= ~rs6000_isa_flags_explicit & OPTION_MASK_STRICT_ALIGN;
3600
3601   if (!rs6000_fold_gimple)
3602      fprintf (stderr,
3603               "gimple folding of rs6000 builtins has been disabled.\n");
3604
3605   /* Add some warnings for VSX.  */
3606   if (TARGET_VSX)
3607     {
3608       const char *msg = NULL;
3609       if (!TARGET_HARD_FLOAT)
3610         {
3611           if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3612             msg = N_("%<-mvsx%> requires hardware floating point");
3613           else
3614             {
3615               rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3616               rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3617             }
3618         }
3619       else if (TARGET_AVOID_XFORM > 0)
3620         msg = N_("%<-mvsx%> needs indexed addressing");
3621       else if (!TARGET_ALTIVEC && (rs6000_isa_flags_explicit
3622                                    & OPTION_MASK_ALTIVEC))
3623         {
3624           if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3625             msg = N_("%<-mvsx%> and %<-mno-altivec%> are incompatible");
3626           else
3627             msg = N_("%<-mno-altivec%> disables vsx");
3628         }
3629
3630       if (msg)
3631         {
3632           warning (0, msg);
3633           rs6000_isa_flags &= ~ OPTION_MASK_VSX;
3634           rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3635         }
3636     }
3637
3638   /* If hard-float/altivec/vsx were explicitly turned off then don't allow
3639      the -mcpu setting to enable options that conflict. */
3640   if ((!TARGET_HARD_FLOAT || !TARGET_ALTIVEC || !TARGET_VSX)
3641       && (rs6000_isa_flags_explicit & (OPTION_MASK_SOFT_FLOAT
3642                                        | OPTION_MASK_ALTIVEC
3643                                        | OPTION_MASK_VSX)) != 0)
3644     rs6000_isa_flags &= ~((OPTION_MASK_P8_VECTOR | OPTION_MASK_CRYPTO
3645                            | OPTION_MASK_DIRECT_MOVE)
3646                          & ~rs6000_isa_flags_explicit);
3647
3648   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
3649     rs6000_print_isa_options (stderr, 0, "before defaults", rs6000_isa_flags);
3650
3651   /* Handle explicit -mno-{altivec,vsx,power8-vector,power9-vector} and turn
3652      off all of the options that depend on those flags.  */
3653   ignore_masks = rs6000_disable_incompatible_switches ();
3654
3655   /* For the newer switches (vsx, dfp, etc.) set some of the older options,
3656      unless the user explicitly used the -mno-<option> to disable the code.  */
3657   if (TARGET_P9_VECTOR || TARGET_MODULO || TARGET_P9_MISC)
3658     rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
3659   else if (TARGET_P9_MINMAX)
3660     {
3661       if (cpu_index >= 0)
3662         {
3663           if (cpu_index == PROCESSOR_POWER9)
3664             {
3665               /* legacy behavior: allow -mcpu=power9 with certain
3666                  capabilities explicitly disabled.  */
3667               rs6000_isa_flags |= (ISA_3_0_MASKS_SERVER & ~ignore_masks);
3668             }
3669           else
3670             error ("power9 target option is incompatible with %<%s=<xxx>%> "
3671                    "for <xxx> less than power9", "-mcpu");
3672         }
3673       else if ((ISA_3_0_MASKS_SERVER & rs6000_isa_flags_explicit)
3674                != (ISA_3_0_MASKS_SERVER & rs6000_isa_flags
3675                    & rs6000_isa_flags_explicit))
3676         /* Enforce that none of the ISA_3_0_MASKS_SERVER flags
3677            were explicitly cleared.  */
3678         error ("%qs incompatible with explicitly disabled options",
3679                "-mpower9-minmax");
3680       else
3681         rs6000_isa_flags |= ISA_3_0_MASKS_SERVER;
3682     }
3683   else if (TARGET_P8_VECTOR || TARGET_DIRECT_MOVE || TARGET_CRYPTO)
3684     rs6000_isa_flags |= (ISA_2_7_MASKS_SERVER & ~ignore_masks);
3685   else if (TARGET_VSX)
3686     rs6000_isa_flags |= (ISA_2_6_MASKS_SERVER & ~ignore_masks);
3687   else if (TARGET_POPCNTD)
3688     rs6000_isa_flags |= (ISA_2_6_MASKS_EMBEDDED & ~ignore_masks);
3689   else if (TARGET_DFP)
3690     rs6000_isa_flags |= (ISA_2_5_MASKS_SERVER & ~ignore_masks);
3691   else if (TARGET_CMPB)
3692     rs6000_isa_flags |= (ISA_2_5_MASKS_EMBEDDED & ~ignore_masks);
3693   else if (TARGET_FPRND)
3694     rs6000_isa_flags |= (ISA_2_4_MASKS & ~ignore_masks);
3695   else if (TARGET_POPCNTB)
3696     rs6000_isa_flags |= (ISA_2_2_MASKS & ~ignore_masks);
3697   else if (TARGET_ALTIVEC)
3698     rs6000_isa_flags |= (OPTION_MASK_PPC_GFXOPT & ~ignore_masks);
3699
3700   if (TARGET_CRYPTO && !TARGET_ALTIVEC)
3701     {
3702       if (rs6000_isa_flags_explicit & OPTION_MASK_CRYPTO)
3703         error ("%qs requires %qs", "-mcrypto", "-maltivec");
3704       rs6000_isa_flags &= ~OPTION_MASK_CRYPTO;
3705     }
3706
3707   if (TARGET_DIRECT_MOVE && !TARGET_VSX)
3708     {
3709       if (rs6000_isa_flags_explicit & OPTION_MASK_DIRECT_MOVE)
3710         error ("%qs requires %qs", "-mdirect-move", "-mvsx");
3711       rs6000_isa_flags &= ~OPTION_MASK_DIRECT_MOVE;
3712     }
3713
3714   if (TARGET_P8_VECTOR && !TARGET_ALTIVEC)
3715     {
3716       if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3717         error ("%qs requires %qs", "-mpower8-vector", "-maltivec");
3718       rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3719     }
3720
3721   if (TARGET_P8_VECTOR && !TARGET_VSX)
3722     {
3723       if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3724           && (rs6000_isa_flags_explicit & OPTION_MASK_VSX))
3725         error ("%qs requires %qs", "-mpower8-vector", "-mvsx");
3726       else if ((rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR) == 0)
3727         {
3728           rs6000_isa_flags &= ~OPTION_MASK_P8_VECTOR;
3729           if (rs6000_isa_flags_explicit & OPTION_MASK_VSX)
3730             rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
3731         }
3732       else
3733         {
3734           /* OPTION_MASK_P8_VECTOR is explicit, and OPTION_MASK_VSX is
3735              not explicit.  */
3736           rs6000_isa_flags |= OPTION_MASK_VSX;
3737           rs6000_isa_flags_explicit |= OPTION_MASK_VSX;
3738         }
3739     }
3740
3741   if (TARGET_DFP && !TARGET_HARD_FLOAT)
3742     {
3743       if (rs6000_isa_flags_explicit & OPTION_MASK_DFP)
3744         error ("%qs requires %qs", "-mhard-dfp", "-mhard-float");
3745       rs6000_isa_flags &= ~OPTION_MASK_DFP;
3746     }
3747
3748   /* The quad memory instructions only works in 64-bit mode. In 32-bit mode,
3749      silently turn off quad memory mode.  */
3750   if ((TARGET_QUAD_MEMORY || TARGET_QUAD_MEMORY_ATOMIC) && !TARGET_POWERPC64)
3751     {
3752       if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3753         warning (0, N_("%<-mquad-memory%> requires 64-bit mode"));
3754
3755       if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) != 0)
3756         warning (0, N_("%<-mquad-memory-atomic%> requires 64-bit mode"));
3757
3758       rs6000_isa_flags &= ~(OPTION_MASK_QUAD_MEMORY
3759                             | OPTION_MASK_QUAD_MEMORY_ATOMIC);
3760     }
3761
3762   /* Non-atomic quad memory load/store are disabled for little endian, since
3763      the words are reversed, but atomic operations can still be done by
3764      swapping the words.  */
3765   if (TARGET_QUAD_MEMORY && !WORDS_BIG_ENDIAN)
3766     {
3767       if ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY) != 0)
3768         warning (0, N_("%<-mquad-memory%> is not available in little endian "
3769                        "mode"));
3770
3771       rs6000_isa_flags &= ~OPTION_MASK_QUAD_MEMORY;
3772     }
3773
3774   /* Assume if the user asked for normal quad memory instructions, they want
3775      the atomic versions as well, unless they explicity told us not to use quad
3776      word atomic instructions.  */
3777   if (TARGET_QUAD_MEMORY
3778       && !TARGET_QUAD_MEMORY_ATOMIC
3779       && ((rs6000_isa_flags_explicit & OPTION_MASK_QUAD_MEMORY_ATOMIC) == 0))
3780     rs6000_isa_flags |= OPTION_MASK_QUAD_MEMORY_ATOMIC;
3781
3782   /* If we can shrink-wrap the TOC register save separately, then use
3783      -msave-toc-indirect unless explicitly disabled.  */
3784   if ((rs6000_isa_flags_explicit & OPTION_MASK_SAVE_TOC_INDIRECT) == 0
3785       && flag_shrink_wrap_separate
3786       && optimize_function_for_speed_p (cfun))
3787     rs6000_isa_flags |= OPTION_MASK_SAVE_TOC_INDIRECT;
3788
3789   /* Enable power8 fusion if we are tuning for power8, even if we aren't
3790      generating power8 instructions.  Power9 does not optimize power8 fusion
3791      cases.  */
3792   if (!(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION))
3793     {
3794       if (processor_target_table[tune_index].processor == PROCESSOR_POWER8)
3795         rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
3796       else
3797         rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
3798     }
3799
3800   /* Setting additional fusion flags turns on base fusion.  */
3801   if (!TARGET_P8_FUSION && TARGET_P8_FUSION_SIGN)
3802     {
3803       if (rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION)
3804         {
3805           if (TARGET_P8_FUSION_SIGN)
3806             error ("%qs requires %qs", "-mpower8-fusion-sign",
3807                    "-mpower8-fusion");
3808
3809           rs6000_isa_flags &= ~OPTION_MASK_P8_FUSION;
3810         }
3811       else
3812         rs6000_isa_flags |= OPTION_MASK_P8_FUSION;
3813     }
3814
3815   /* Power8 does not fuse sign extended loads with the addis.  If we are
3816      optimizing at high levels for speed, convert a sign extended load into a
3817      zero extending load, and an explicit sign extension.  */
3818   if (TARGET_P8_FUSION
3819       && !(rs6000_isa_flags_explicit & OPTION_MASK_P8_FUSION_SIGN)
3820       && optimize_function_for_speed_p (cfun)
3821       && optimize >= 3)
3822     rs6000_isa_flags |= OPTION_MASK_P8_FUSION_SIGN;
3823
3824   /* ISA 3.0 vector instructions include ISA 2.07.  */
3825   if (TARGET_P9_VECTOR && !TARGET_P8_VECTOR)
3826     {
3827       /* We prefer to not mention undocumented options in
3828          error messages.  However, if users have managed to select
3829          power9-vector without selecting power8-vector, they
3830          already know about undocumented flags.  */
3831       if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) &&
3832           (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR))
3833         error ("%qs requires %qs", "-mpower9-vector", "-mpower8-vector");
3834       else if ((rs6000_isa_flags_explicit & OPTION_MASK_P9_VECTOR) == 0)
3835         {
3836           rs6000_isa_flags &= ~OPTION_MASK_P9_VECTOR;
3837           if (rs6000_isa_flags_explicit & OPTION_MASK_P8_VECTOR)
3838             rs6000_isa_flags_explicit |= OPTION_MASK_P9_VECTOR;
3839         }
3840       else
3841         {
3842           /* OPTION_MASK_P9_VECTOR is explicit and
3843              OPTION_MASK_P8_VECTOR is not explicit.  */
3844           rs6000_isa_flags |= OPTION_MASK_P8_VECTOR;
3845           rs6000_isa_flags_explicit |= OPTION_MASK_P8_VECTOR;
3846         }
3847     }
3848
3849   /* Set -mallow-movmisalign to explicitly on if we have full ISA 2.07
3850      support. If we only have ISA 2.06 support, and the user did not specify
3851      the switch, leave it set to -1 so the movmisalign patterns are enabled,
3852      but we don't enable the full vectorization support  */
3853   if (TARGET_ALLOW_MOVMISALIGN == -1 && TARGET_P8_VECTOR && TARGET_DIRECT_MOVE)
3854     TARGET_ALLOW_MOVMISALIGN = 1;
3855
3856   else if (TARGET_ALLOW_MOVMISALIGN && !TARGET_VSX)
3857     {
3858       if (TARGET_ALLOW_MOVMISALIGN > 0
3859           && global_options_set.x_TARGET_ALLOW_MOVMISALIGN)
3860         error ("%qs requires %qs", "-mallow-movmisalign", "-mvsx");
3861
3862       TARGET_ALLOW_MOVMISALIGN = 0;
3863     }
3864
3865   /* Determine when unaligned vector accesses are permitted, and when
3866      they are preferred over masked Altivec loads.  Note that if
3867      TARGET_ALLOW_MOVMISALIGN has been disabled by the user, then
3868      TARGET_EFFICIENT_UNALIGNED_VSX must be as well.  The converse is
3869      not true.  */
3870   if (TARGET_EFFICIENT_UNALIGNED_VSX)
3871     {
3872       if (!TARGET_VSX)
3873         {
3874           if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
3875             error ("%qs requires %qs", "-mefficient-unaligned-vsx", "-mvsx");
3876
3877           rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
3878         }
3879
3880       else if (!TARGET_ALLOW_MOVMISALIGN)
3881         {
3882           if (rs6000_isa_flags_explicit & OPTION_MASK_EFFICIENT_UNALIGNED_VSX)
3883             error ("%qs requires %qs", "-munefficient-unaligned-vsx",
3884                    "-mallow-movmisalign");
3885
3886           rs6000_isa_flags &= ~OPTION_MASK_EFFICIENT_UNALIGNED_VSX;
3887         }
3888     }
3889
3890   /* Use long double size to select the appropriate long double.  We use
3891      TYPE_PRECISION to differentiate the 3 different long double types.  We map
3892      128 into the precision used for TFmode.  */
3893   int default_long_double_size = (RS6000_DEFAULT_LONG_DOUBLE_SIZE == 64
3894                                   ? 64
3895                                   : FLOAT_PRECISION_TFmode);
3896
3897   /* Set long double size before the IEEE 128-bit tests.  */
3898   if (!global_options_set.x_rs6000_long_double_type_size)
3899     {
3900       if (main_target_opt != NULL
3901           && (main_target_opt->x_rs6000_long_double_type_size
3902               != default_long_double_size))
3903         error ("target attribute or pragma changes %<long double%> size");
3904       else
3905         rs6000_long_double_type_size = default_long_double_size;
3906     }
3907   else if (rs6000_long_double_type_size == 128)
3908     rs6000_long_double_type_size = FLOAT_PRECISION_TFmode;
3909   else if (global_options_set.x_rs6000_ieeequad)
3910     {
3911       if (global_options.x_rs6000_ieeequad)
3912         error ("%qs requires %qs", "-mabi=ieeelongdouble", "-mlong-double-128");
3913       else
3914         error ("%qs requires %qs", "-mabi=ibmlongdouble", "-mlong-double-128");
3915     }
3916
3917   /* Set -mabi=ieeelongdouble on some old targets.  In the future, power server
3918      systems will also set long double to be IEEE 128-bit.  AIX and Darwin
3919      explicitly redefine TARGET_IEEEQUAD and TARGET_IEEEQUAD_DEFAULT to 0, so
3920      those systems will not pick up this default.  Warn if the user changes the
3921      default unless -Wno-psabi.  */
3922   if (!global_options_set.x_rs6000_ieeequad)
3923     rs6000_ieeequad = TARGET_IEEEQUAD_DEFAULT;
3924
3925   else
3926     {
3927       if (global_options.x_rs6000_ieeequad
3928           && (!TARGET_POPCNTD || !TARGET_VSX))
3929         error ("%qs requires full ISA 2.06 support", "-mabi=ieeelongdouble");
3930
3931       if (rs6000_ieeequad != TARGET_IEEEQUAD_DEFAULT && TARGET_LONG_DOUBLE_128)
3932         {
3933           static bool warned_change_long_double;
3934           if (!warned_change_long_double)
3935             {
3936               warned_change_long_double = true;
3937               if (TARGET_IEEEQUAD)
3938                 warning (OPT_Wpsabi, "Using IEEE extended precision "
3939                          "%<long double%>");
3940               else
3941                 warning (OPT_Wpsabi, "Using IBM extended precision "
3942                          "%<long double%>");
3943             }
3944         }
3945     }
3946
3947   /* Enable the default support for IEEE 128-bit floating point on Linux VSX
3948      sytems.  In GCC 7, we would enable the the IEEE 128-bit floating point
3949      infrastructure (-mfloat128-type) but not enable the actual __float128 type
3950      unless the user used the explicit -mfloat128.  In GCC 8, we enable both
3951      the keyword as well as the type.  */
3952   TARGET_FLOAT128_TYPE = TARGET_FLOAT128_ENABLE_TYPE && TARGET_VSX;
3953
3954   /* IEEE 128-bit floating point requires VSX support.  */
3955   if (TARGET_FLOAT128_KEYWORD)
3956     {
3957       if (!TARGET_VSX)
3958         {
3959           if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) != 0)
3960             error ("%qs requires VSX support", "%<-mfloat128%>");
3961
3962           TARGET_FLOAT128_TYPE = 0;
3963           rs6000_isa_flags &= ~(OPTION_MASK_FLOAT128_KEYWORD
3964                                 | OPTION_MASK_FLOAT128_HW);
3965         }
3966       else if (!TARGET_FLOAT128_TYPE)
3967         {
3968           TARGET_FLOAT128_TYPE = 1;
3969           warning (0, "The %<-mfloat128%> option may not be fully supported");
3970         }
3971     }
3972
3973   /* Enable the __float128 keyword under Linux by default.  */
3974   if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_KEYWORD
3975       && (rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_KEYWORD) == 0)
3976     rs6000_isa_flags |= OPTION_MASK_FLOAT128_KEYWORD;
3977
3978   /* If we have are supporting the float128 type and full ISA 3.0 support,
3979      enable -mfloat128-hardware by default.  However, don't enable the
3980      __float128 keyword if it was explicitly turned off.  64-bit mode is needed
3981      because sometimes the compiler wants to put things in an integer
3982      container, and if we don't have __int128 support, it is impossible.  */
3983   if (TARGET_FLOAT128_TYPE && !TARGET_FLOAT128_HW && TARGET_64BIT
3984       && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) == ISA_3_0_MASKS_IEEE
3985       && !(rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW))
3986     rs6000_isa_flags |= OPTION_MASK_FLOAT128_HW;
3987
3988   if (TARGET_FLOAT128_HW
3989       && (rs6000_isa_flags & ISA_3_0_MASKS_IEEE) != ISA_3_0_MASKS_IEEE)
3990     {
3991       if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
3992         error ("%qs requires full ISA 3.0 support", "%<-mfloat128-hardware%>");
3993
3994       rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
3995     }
3996
3997   if (TARGET_FLOAT128_HW && !TARGET_64BIT)
3998     {
3999       if ((rs6000_isa_flags_explicit & OPTION_MASK_FLOAT128_HW) != 0)
4000         error ("%qs requires %qs", "%<-mfloat128-hardware%>", "-m64");
4001
4002       rs6000_isa_flags &= ~OPTION_MASK_FLOAT128_HW;
4003     }
4004
4005   /* -mprefixed-addr (and hence -mpcrel) requires -mcpu=future.  */
4006   if (TARGET_PREFIXED_ADDR && !TARGET_FUTURE)
4007     {
4008       if ((rs6000_isa_flags_explicit & OPTION_MASK_PCREL) != 0)
4009         error ("%qs requires %qs", "-mpcrel", "-mcpu=future");
4010       else if ((rs6000_isa_flags_explicit & OPTION_MASK_PREFIXED_ADDR) != 0)
4011         error ("%qs requires %qs", "-mprefixed-addr", "-mcpu=future");
4012
4013       rs6000_isa_flags &= ~(OPTION_MASK_PCREL | OPTION_MASK_PREFIXED_ADDR);
4014     }
4015
4016   /* -mpcrel requires prefixed load/store addressing.  */
4017   if (TARGET_PCREL && !TARGET_PREFIXED_ADDR)
4018     {
4019       if ((rs6000_isa_flags_explicit & OPTION_MASK_PCREL) != 0)
4020         error ("%qs requires %qs", "-mpcrel", "-mprefixed-addr");
4021
4022       rs6000_isa_flags &= ~OPTION_MASK_PCREL;
4023     }
4024
4025   /* Print the options after updating the defaults.  */
4026   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4027     rs6000_print_isa_options (stderr, 0, "after defaults", rs6000_isa_flags);
4028
4029   /* E500mc does "better" if we inline more aggressively.  Respect the
4030      user's opinion, though.  */
4031   if (rs6000_block_move_inline_limit == 0
4032       && (rs6000_tune == PROCESSOR_PPCE500MC
4033           || rs6000_tune == PROCESSOR_PPCE500MC64
4034           || rs6000_tune == PROCESSOR_PPCE5500
4035           || rs6000_tune == PROCESSOR_PPCE6500))
4036     rs6000_block_move_inline_limit = 128;
4037
4038   /* store_one_arg depends on expand_block_move to handle at least the
4039      size of reg_parm_stack_space.  */
4040   if (rs6000_block_move_inline_limit < (TARGET_POWERPC64 ? 64 : 32))
4041     rs6000_block_move_inline_limit = (TARGET_POWERPC64 ? 64 : 32);
4042
4043   if (global_init_p)
4044     {
4045       /* If the appropriate debug option is enabled, replace the target hooks
4046          with debug versions that call the real version and then prints
4047          debugging information.  */
4048       if (TARGET_DEBUG_COST)
4049         {
4050           targetm.rtx_costs = rs6000_debug_rtx_costs;
4051           targetm.address_cost = rs6000_debug_address_cost;
4052           targetm.sched.adjust_cost = rs6000_debug_adjust_cost;
4053         }
4054
4055       if (TARGET_DEBUG_ADDR)
4056         {
4057           targetm.legitimate_address_p = rs6000_debug_legitimate_address_p;
4058           targetm.legitimize_address = rs6000_debug_legitimize_address;
4059           rs6000_secondary_reload_class_ptr
4060             = rs6000_debug_secondary_reload_class;
4061           targetm.secondary_memory_needed
4062             = rs6000_debug_secondary_memory_needed;
4063           targetm.can_change_mode_class
4064             = rs6000_debug_can_change_mode_class;
4065           rs6000_preferred_reload_class_ptr
4066             = rs6000_debug_preferred_reload_class;
4067           rs6000_mode_dependent_address_ptr
4068             = rs6000_debug_mode_dependent_address;
4069         }
4070
4071       if (rs6000_veclibabi_name)
4072         {
4073           if (strcmp (rs6000_veclibabi_name, "mass") == 0)
4074             rs6000_veclib_handler = rs6000_builtin_vectorized_libmass;
4075           else
4076             {
4077               error ("unknown vectorization library ABI type (%qs) for "
4078                      "%qs switch", rs6000_veclibabi_name, "-mveclibabi=");
4079               ret = false;
4080             }
4081         }
4082     }
4083
4084   /* Disable VSX and Altivec silently if the user switched cpus to power7 in a
4085      target attribute or pragma which automatically enables both options,
4086      unless the altivec ABI was set.  This is set by default for 64-bit, but
4087      not for 32-bit.  */
4088   if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
4089     {
4090       TARGET_FLOAT128_TYPE = 0;
4091       rs6000_isa_flags &= ~((OPTION_MASK_VSX | OPTION_MASK_ALTIVEC
4092                              | OPTION_MASK_FLOAT128_KEYWORD)
4093                             & ~rs6000_isa_flags_explicit);
4094     }
4095
4096   /* Enable Altivec ABI for AIX -maltivec.  */
4097   if (TARGET_XCOFF && (TARGET_ALTIVEC || TARGET_VSX))
4098     {
4099       if (main_target_opt != NULL && !main_target_opt->x_rs6000_altivec_abi)
4100         error ("target attribute or pragma changes AltiVec ABI");
4101       else
4102         rs6000_altivec_abi = 1;
4103     }
4104
4105   /* The AltiVec ABI is the default for PowerPC-64 GNU/Linux.  For
4106      PowerPC-32 GNU/Linux, -maltivec implies the AltiVec ABI.  It can
4107      be explicitly overridden in either case.  */
4108   if (TARGET_ELF)
4109     {
4110       if (!global_options_set.x_rs6000_altivec_abi
4111           && (TARGET_64BIT || TARGET_ALTIVEC || TARGET_VSX))
4112         {
4113           if (main_target_opt != NULL &&
4114               !main_target_opt->x_rs6000_altivec_abi)
4115             error ("target attribute or pragma changes AltiVec ABI");
4116           else
4117             rs6000_altivec_abi = 1;
4118         }
4119     }
4120
4121   /* Set the Darwin64 ABI as default for 64-bit Darwin.  
4122      So far, the only darwin64 targets are also MACH-O.  */
4123   if (TARGET_MACHO
4124       && DEFAULT_ABI == ABI_DARWIN 
4125       && TARGET_64BIT)
4126     {
4127       if (main_target_opt != NULL && !main_target_opt->x_rs6000_darwin64_abi)
4128         error ("target attribute or pragma changes darwin64 ABI");
4129       else
4130         {
4131           rs6000_darwin64_abi = 1;
4132           /* Default to natural alignment, for better performance.  */
4133           rs6000_alignment_flags = MASK_ALIGN_NATURAL;
4134         }
4135     }
4136
4137   /* Place FP constants in the constant pool instead of TOC
4138      if section anchors enabled.  */
4139   if (flag_section_anchors
4140       && !global_options_set.x_TARGET_NO_FP_IN_TOC)
4141     TARGET_NO_FP_IN_TOC = 1;
4142
4143   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4144     rs6000_print_isa_options (stderr, 0, "before subtarget", rs6000_isa_flags);
4145
4146 #ifdef SUBTARGET_OVERRIDE_OPTIONS
4147   SUBTARGET_OVERRIDE_OPTIONS;
4148 #endif
4149 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
4150   SUBSUBTARGET_OVERRIDE_OPTIONS;
4151 #endif
4152 #ifdef SUB3TARGET_OVERRIDE_OPTIONS
4153   SUB3TARGET_OVERRIDE_OPTIONS;
4154 #endif
4155
4156   /* -mpcrel requires -mcmodel=medium, but we can't check TARGET_CMODEL until
4157       after the subtarget override options are done.  */
4158   if (TARGET_PCREL && TARGET_CMODEL != CMODEL_MEDIUM)
4159     {
4160       if ((rs6000_isa_flags_explicit & OPTION_MASK_PCREL) != 0)
4161         error ("%qs requires %qs", "-mpcrel", "-mcmodel=medium");
4162
4163       rs6000_isa_flags &= ~OPTION_MASK_PCREL;
4164     }
4165
4166   if (TARGET_DEBUG_REG || TARGET_DEBUG_TARGET)
4167     rs6000_print_isa_options (stderr, 0, "after subtarget", rs6000_isa_flags);
4168
4169   rs6000_always_hint = (rs6000_tune != PROCESSOR_POWER4
4170                         && rs6000_tune != PROCESSOR_POWER5
4171                         && rs6000_tune != PROCESSOR_POWER6
4172                         && rs6000_tune != PROCESSOR_POWER7
4173                         && rs6000_tune != PROCESSOR_POWER8
4174                         && rs6000_tune != PROCESSOR_POWER9
4175                         && rs6000_tune != PROCESSOR_FUTURE
4176                         && rs6000_tune != PROCESSOR_PPCA2
4177                         && rs6000_tune != PROCESSOR_CELL
4178                         && rs6000_tune != PROCESSOR_PPC476);
4179   rs6000_sched_groups = (rs6000_tune == PROCESSOR_POWER4
4180                          || rs6000_tune == PROCESSOR_POWER5
4181                          || rs6000_tune == PROCESSOR_POWER7
4182                          || rs6000_tune == PROCESSOR_POWER8);
4183   rs6000_align_branch_targets = (rs6000_tune == PROCESSOR_POWER4
4184                                  || rs6000_tune == PROCESSOR_POWER5
4185                                  || rs6000_tune == PROCESSOR_POWER6
4186                                  || rs6000_tune == PROCESSOR_POWER7
4187                                  || rs6000_tune == PROCESSOR_POWER8
4188                                  || rs6000_tune == PROCESSOR_POWER9
4189                                  || rs6000_tune == PROCESSOR_FUTURE
4190                                  || rs6000_tune == PROCESSOR_PPCE500MC
4191                                  || rs6000_tune == PROCESSOR_PPCE500MC64
4192                                  || rs6000_tune == PROCESSOR_PPCE5500
4193                                  || rs6000_tune == PROCESSOR_PPCE6500);
4194
4195   /* Allow debug switches to override the above settings.  These are set to -1
4196      in rs6000.opt to indicate the user hasn't directly set the switch.  */
4197   if (TARGET_ALWAYS_HINT >= 0)
4198     rs6000_always_hint = TARGET_ALWAYS_HINT;
4199
4200   if (TARGET_SCHED_GROUPS >= 0)
4201     rs6000_sched_groups = TARGET_SCHED_GROUPS;
4202
4203   if (TARGET_ALIGN_BRANCH_TARGETS >= 0)
4204     rs6000_align_branch_targets = TARGET_ALIGN_BRANCH_TARGETS;
4205
4206   rs6000_sched_restricted_insns_priority
4207     = (rs6000_sched_groups ? 1 : 0);
4208
4209   /* Handle -msched-costly-dep option.  */
4210   rs6000_sched_costly_dep
4211     = (rs6000_sched_groups ? true_store_to_load_dep_costly : no_dep_costly);
4212
4213   if (rs6000_sched_costly_dep_str)
4214     {
4215       if (! strcmp (rs6000_sched_costly_dep_str, "no"))
4216         rs6000_sched_costly_dep = no_dep_costly;
4217       else if (! strcmp (rs6000_sched_costly_dep_str, "all"))
4218         rs6000_sched_costly_dep = all_deps_costly;
4219       else if (! strcmp (rs6000_sched_costly_dep_str, "true_store_to_load"))
4220         rs6000_sched_costly_dep = true_store_to_load_dep_costly;
4221       else if (! strcmp (rs6000_sched_costly_dep_str, "store_to_load"))
4222         rs6000_sched_costly_dep = store_to_load_dep_costly;
4223       else
4224         rs6000_sched_costly_dep = ((enum rs6000_dependence_cost)
4225                                    atoi (rs6000_sched_costly_dep_str));
4226     }
4227
4228   /* Handle -minsert-sched-nops option.  */
4229   rs6000_sched_insert_nops
4230     = (rs6000_sched_groups ? sched_finish_regroup_exact : sched_finish_none);
4231
4232   if (rs6000_sched_insert_nops_str)
4233     {
4234       if (! strcmp (rs6000_sched_insert_nops_str, "no"))
4235         rs6000_sched_insert_nops = sched_finish_none;
4236       else if (! strcmp (rs6000_sched_insert_nops_str, "pad"))
4237         rs6000_sched_insert_nops = sched_finish_pad_groups;
4238       else if (! strcmp (rs6000_sched_insert_nops_str, "regroup_exact"))
4239         rs6000_sched_insert_nops = sched_finish_regroup_exact;
4240       else
4241         rs6000_sched_insert_nops = ((enum rs6000_nop_insertion)
4242                                     atoi (rs6000_sched_insert_nops_str));
4243     }
4244
4245   /* Handle stack protector */
4246   if (!global_options_set.x_rs6000_stack_protector_guard)
4247 #ifdef TARGET_THREAD_SSP_OFFSET
4248     rs6000_stack_protector_guard = SSP_TLS;
4249 #else
4250     rs6000_stack_protector_guard = SSP_GLOBAL;
4251 #endif
4252
4253 #ifdef TARGET_THREAD_SSP_OFFSET
4254   rs6000_stack_protector_guard_offset = TARGET_THREAD_SSP_OFFSET;
4255   rs6000_stack_protector_guard_reg = TARGET_64BIT ? 13 : 2;
4256 #endif
4257
4258   if (global_options_set.x_rs6000_stack_protector_guard_offset_str)
4259     {
4260       char *endp;
4261       const char *str = rs6000_stack_protector_guard_offset_str;
4262
4263       errno = 0;
4264       long offset = strtol (str, &endp, 0);
4265       if (!*str || *endp || errno)
4266         error ("%qs is not a valid number in %qs", str,
4267                "-mstack-protector-guard-offset=");
4268
4269       if (!IN_RANGE (offset, -0x8000, 0x7fff)
4270           || (TARGET_64BIT && (offset & 3)))
4271         error ("%qs is not a valid offset in %qs", str,
4272                "-mstack-protector-guard-offset=");
4273
4274       rs6000_stack_protector_guard_offset = offset;
4275     }
4276
4277   if (global_options_set.x_rs6000_stack_protector_guard_reg_str)
4278     {
4279       const char *str = rs6000_stack_protector_guard_reg_str;
4280       int reg = decode_reg_name (str);
4281
4282       if (!IN_RANGE (reg, 1, 31))
4283         error ("%qs is not a valid base register in %qs", str,
4284                "-mstack-protector-guard-reg=");
4285
4286       rs6000_stack_protector_guard_reg = reg;
4287     }
4288
4289   if (rs6000_stack_protector_guard == SSP_TLS
4290       && !IN_RANGE (rs6000_stack_protector_guard_reg, 1, 31))
4291     error ("%qs needs a valid base register", "-mstack-protector-guard=tls");
4292
4293   if (global_init_p)
4294     {
4295 #ifdef TARGET_REGNAMES
4296       /* If the user desires alternate register names, copy in the
4297          alternate names now.  */
4298       if (TARGET_REGNAMES)
4299         memcpy (rs6000_reg_names, alt_reg_names, sizeof (rs6000_reg_names));
4300 #endif
4301
4302       /* Set aix_struct_return last, after the ABI is determined.
4303          If -maix-struct-return or -msvr4-struct-return was explicitly
4304          used, don't override with the ABI default.  */
4305       if (!global_options_set.x_aix_struct_return)
4306         aix_struct_return = (DEFAULT_ABI != ABI_V4 || DRAFT_V4_STRUCT_RET);
4307
4308 #if 0
4309       /* IBM XL compiler defaults to unsigned bitfields.  */
4310       if (TARGET_XL_COMPAT)
4311         flag_signed_bitfields = 0;
4312 #endif
4313
4314       if (TARGET_LONG_DOUBLE_128 && !TARGET_IEEEQUAD)
4315         REAL_MODE_FORMAT (TFmode) = &ibm_extended_format;
4316
4317       ASM_GENERATE_INTERNAL_LABEL (toc_label_name, "LCTOC", 1);
4318
4319       /* We can only guarantee the availability of DI pseudo-ops when
4320          assembling for 64-bit targets.  */
4321       if (!TARGET_64BIT)
4322         {
4323           targetm.asm_out.aligned_op.di = NULL;
4324           targetm.asm_out.unaligned_op.di = NULL;
4325         }
4326
4327
4328       /* Set branch target alignment, if not optimizing for size.  */
4329       if (!optimize_size)
4330         {
4331           /* Cell wants to be aligned 8byte for dual issue.  Titan wants to be
4332              aligned 8byte to avoid misprediction by the branch predictor.  */
4333           if (rs6000_tune == PROCESSOR_TITAN
4334               || rs6000_tune == PROCESSOR_CELL)
4335             {
4336               if (flag_align_functions && !str_align_functions)
4337                 str_align_functions = "8";
4338               if (flag_align_jumps && !str_align_jumps)
4339                 str_align_jumps = "8";
4340               if (flag_align_loops && !str_align_loops)
4341                 str_align_loops = "8";
4342             }
4343           if (rs6000_align_branch_targets)
4344             {
4345               if (flag_align_functions && !str_align_functions)
4346                 str_align_functions = "16";
4347               if (flag_align_jumps && !str_align_jumps)
4348                 str_align_jumps = "16";
4349               if (flag_align_loops && !str_align_loops)
4350                 {
4351                   can_override_loop_align = 1;
4352                   str_align_loops = "16";
4353                 }
4354             }
4355
4356           if (flag_align_jumps && !str_align_jumps)
4357             str_align_jumps = "16";
4358           if (flag_align_loops && !str_align_loops)
4359             str_align_loops = "16";
4360         }
4361
4362       /* Arrange to save and restore machine status around nested functions.  */
4363       init_machine_status = rs6000_init_machine_status;
4364
4365       /* We should always be splitting complex arguments, but we can't break
4366          Linux and Darwin ABIs at the moment.  For now, only AIX is fixed.  */
4367       if (DEFAULT_ABI == ABI_V4 || DEFAULT_ABI == ABI_DARWIN)
4368         targetm.calls.split_complex_arg = NULL;
4369
4370       /* The AIX and ELFv1 ABIs define standard function descriptors.  */
4371       if (DEFAULT_ABI == ABI_AIX)
4372         targetm.calls.custom_function_descriptors = 0;
4373     }
4374
4375   /* Initialize rs6000_cost with the appropriate target costs.  */
4376   if (optimize_size)
4377     rs6000_cost = TARGET_POWERPC64 ? &size64_cost : &size32_cost;
4378   else
4379     switch (rs6000_tune)
4380       {
4381       case PROCESSOR_RS64A:
4382         rs6000_cost = &rs64a_cost;
4383         break;
4384
4385       case PROCESSOR_MPCCORE:
4386         rs6000_cost = &mpccore_cost;
4387         break;
4388
4389       case PROCESSOR_PPC403:
4390         rs6000_cost = &ppc403_cost;
4391         break;
4392
4393       case PROCESSOR_PPC405:
4394         rs6000_cost = &ppc405_cost;
4395         break;
4396
4397       case PROCESSOR_PPC440:
4398         rs6000_cost = &ppc440_cost;
4399         break;
4400
4401       case PROCESSOR_PPC476:
4402         rs6000_cost = &ppc476_cost;
4403         break;
4404
4405       case PROCESSOR_PPC601:
4406         rs6000_cost = &ppc601_cost;
4407         break;
4408
4409       case PROCESSOR_PPC603:
4410         rs6000_cost = &ppc603_cost;
4411         break;
4412
4413       case PROCESSOR_PPC604:
4414         rs6000_cost = &ppc604_cost;
4415         break;
4416
4417       case PROCESSOR_PPC604e:
4418         rs6000_cost = &ppc604e_cost;
4419         break;
4420
4421       case PROCESSOR_PPC620:
4422         rs6000_cost = &ppc620_cost;
4423         break;
4424
4425       case PROCESSOR_PPC630:
4426         rs6000_cost = &ppc630_cost;
4427         break;
4428
4429       case PROCESSOR_CELL:
4430         rs6000_cost = &ppccell_cost;
4431         break;
4432
4433       case PROCESSOR_PPC750:
4434       case PROCESSOR_PPC7400:
4435         rs6000_cost = &ppc750_cost;
4436         break;
4437
4438       case PROCESSOR_PPC7450:
4439         rs6000_cost = &ppc7450_cost;
4440         break;
4441
4442       case PROCESSOR_PPC8540:
4443       case PROCESSOR_PPC8548:
4444         rs6000_cost = &ppc8540_cost;
4445         break;
4446
4447       case PROCESSOR_PPCE300C2:
4448       case PROCESSOR_PPCE300C3:
4449         rs6000_cost = &ppce300c2c3_cost;
4450         break;
4451
4452       case PROCESSOR_PPCE500MC:
4453         rs6000_cost = &ppce500mc_cost;
4454         break;
4455
4456       case PROCESSOR_PPCE500MC64:
4457         rs6000_cost = &ppce500mc64_cost;
4458         break;
4459
4460       case PROCESSOR_PPCE5500:
4461         rs6000_cost = &ppce5500_cost;
4462         break;
4463
4464       case PROCESSOR_PPCE6500:
4465         rs6000_cost = &ppce6500_cost;
4466         break;
4467
4468       case PROCESSOR_TITAN:
4469         rs6000_cost = &titan_cost;
4470         break;
4471
4472       case PROCESSOR_POWER4:
4473       case PROCESSOR_POWER5:
4474         rs6000_cost = &power4_cost;
4475         break;
4476
4477       case PROCESSOR_POWER6:
4478         rs6000_cost = &power6_cost;
4479         break;
4480
4481       case PROCESSOR_POWER7:
4482         rs6000_cost = &power7_cost;
4483         break;
4484
4485       case PROCESSOR_POWER8:
4486         rs6000_cost = &power8_cost;
4487         break;
4488
4489       case PROCESSOR_POWER9:
4490       case PROCESSOR_FUTURE:
4491         rs6000_cost = &power9_cost;
4492         break;
4493
4494       case PROCESSOR_PPCA2:
4495         rs6000_cost = &ppca2_cost;
4496         break;
4497
4498       default:
4499         gcc_unreachable ();
4500       }
4501
4502   if (global_init_p)
4503     {
4504       maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
4505                              rs6000_cost->simultaneous_prefetches,
4506                              global_options.x_param_values,
4507                              global_options_set.x_param_values);
4508       maybe_set_param_value (PARAM_L1_CACHE_SIZE, rs6000_cost->l1_cache_size,
4509                              global_options.x_param_values,
4510                              global_options_set.x_param_values);
4511       maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
4512                              rs6000_cost->cache_line_size,
4513                              global_options.x_param_values,
4514                              global_options_set.x_param_values);
4515       maybe_set_param_value (PARAM_L2_CACHE_SIZE, rs6000_cost->l2_cache_size,
4516                              global_options.x_param_values,
4517                              global_options_set.x_param_values);
4518
4519       /* Increase loop peeling limits based on performance analysis. */
4520       maybe_set_param_value (PARAM_MAX_PEELED_INSNS, 400,
4521                              global_options.x_param_values,
4522                              global_options_set.x_param_values);
4523       maybe_set_param_value (PARAM_MAX_COMPLETELY_PEELED_INSNS, 400,
4524                              global_options.x_param_values,
4525                              global_options_set.x_param_values);
4526
4527       /* Use the 'model' -fsched-pressure algorithm by default.  */
4528       maybe_set_param_value (PARAM_SCHED_PRESSURE_ALGORITHM,
4529                              SCHED_PRESSURE_MODEL,
4530                              global_options.x_param_values,
4531                              global_options_set.x_param_values);
4532
4533       /* If using typedef char *va_list, signal that
4534          __builtin_va_start (&ap, 0) can be optimized to
4535          ap = __builtin_next_arg (0).  */
4536       if (DEFAULT_ABI != ABI_V4)
4537         targetm.expand_builtin_va_start = NULL;
4538     }
4539
4540   /* If not explicitly specified via option, decide whether to generate indexed
4541      load/store instructions.  A value of -1 indicates that the
4542      initial value of this variable has not been overwritten. During
4543      compilation, TARGET_AVOID_XFORM is either 0 or 1. */
4544   if (TARGET_AVOID_XFORM == -1)
4545     /* Avoid indexed addressing when targeting Power6 in order to avoid the
4546      DERAT mispredict penalty.  However the LVE and STVE altivec instructions
4547      need indexed accesses and the type used is the scalar type of the element
4548      being loaded or stored.  */
4549     TARGET_AVOID_XFORM = (rs6000_tune == PROCESSOR_POWER6 && TARGET_CMPB
4550                           && !TARGET_ALTIVEC);
4551
4552   /* Set the -mrecip options.  */
4553   if (rs6000_recip_name)
4554     {
4555       char *p = ASTRDUP (rs6000_recip_name);
4556       char *q;
4557       unsigned int mask, i;
4558       bool invert;
4559
4560       while ((q = strtok (p, ",")) != NULL)
4561         {
4562           p = NULL;
4563           if (*q == '!')
4564             {
4565               invert = true;
4566               q++;
4567             }
4568           else
4569             invert = false;
4570
4571           if (!strcmp (q, "default"))
4572             mask = ((TARGET_RECIP_PRECISION)
4573                     ? RECIP_HIGH_PRECISION : RECIP_LOW_PRECISION);
4574           else
4575             {
4576               for (i = 0; i < ARRAY_SIZE (recip_options); i++)
4577                 if (!strcmp (q, recip_options[i].string))
4578                   {
4579                     mask = recip_options[i].mask;
4580                     break;
4581                   }
4582
4583               if (i == ARRAY_SIZE (recip_options))
4584                 {
4585                   error ("unknown option for %<%s=%s%>", "-mrecip", q);
4586                   invert = false;
4587                   mask = 0;
4588                   ret = false;
4589                 }
4590             }
4591
4592           if (invert)
4593             rs6000_recip_control &= ~mask;
4594           else
4595             rs6000_recip_control |= mask;
4596         }
4597     }
4598
4599   /* Set the builtin mask of the various options used that could affect which
4600      builtins were used.  In the past we used target_flags, but we've run out
4601      of bits, and some options are no longer in target_flags.  */
4602   rs6000_builtin_mask = rs6000_builtin_mask_calculate ();
4603   if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
4604     rs6000_print_builtin_options (stderr, 0, "builtin mask",
4605                                   rs6000_builtin_mask);
4606
4607   /* Initialize all of the registers.  */
4608   rs6000_init_hard_regno_mode_ok (global_init_p);
4609
4610   /* Save the initial options in case the user does function specific options */
4611   if (global_init_p)
4612     target_option_default_node = target_option_current_node
4613       = build_target_option_node (&global_options);
4614
4615   /* If not explicitly specified via option, decide whether to generate the
4616      extra blr's required to preserve the link stack on some cpus (eg, 476).  */
4617   if (TARGET_LINK_STACK == -1)
4618     SET_TARGET_LINK_STACK (rs6000_tune == PROCESSOR_PPC476 && flag_pic);
4619
4620   /* Deprecate use of -mno-speculate-indirect-jumps.  */
4621   if (!rs6000_speculate_indirect_jumps)
4622     warning (0, "%qs is deprecated and not recommended in any circumstances",
4623              "-mno-speculate-indirect-jumps");
4624
4625   return ret;
4626 }
4627
4628 /* Implement TARGET_OPTION_OVERRIDE.  On the RS/6000 this is used to
4629    define the target cpu type.  */
4630
4631 static void
4632 rs6000_option_override (void)
4633 {
4634   (void) rs6000_option_override_internal (true);
4635 }
4636
4637 \f
4638 /* Implement targetm.vectorize.builtin_mask_for_load.  */
4639 static tree
4640 rs6000_builtin_mask_for_load (void)
4641 {
4642   /* Don't use lvsl/vperm for P8 and similarly efficient machines.  */
4643   if ((TARGET_ALTIVEC && !TARGET_VSX)
4644       || (TARGET_VSX && !TARGET_EFFICIENT_UNALIGNED_VSX))
4645     return altivec_builtin_mask_for_load;
4646   else
4647     return 0;
4648 }
4649
4650 /* Implement LOOP_ALIGN. */
4651 align_flags
4652 rs6000_loop_align (rtx label)
4653 {
4654   basic_block bb;
4655   int ninsns;
4656
4657   /* Don't override loop alignment if -falign-loops was specified. */
4658   if (!can_override_loop_align)
4659     return align_loops;
4660
4661   bb = BLOCK_FOR_INSN (label);
4662   ninsns = num_loop_insns(bb->loop_father);
4663
4664   /* Align small loops to 32 bytes to fit in an icache sector, otherwise return default. */
4665   if (ninsns > 4 && ninsns <= 8
4666       && (rs6000_tune == PROCESSOR_POWER4
4667           || rs6000_tune == PROCESSOR_POWER5
4668           || rs6000_tune == PROCESSOR_POWER6
4669           || rs6000_tune == PROCESSOR_POWER7
4670           || rs6000_tune == PROCESSOR_POWER8))
4671     return align_flags (5);
4672   else
4673     return align_loops;
4674 }
4675
4676 /* Return true iff, data reference of TYPE can reach vector alignment (16)
4677    after applying N number of iterations.  This routine does not determine
4678    how may iterations are required to reach desired alignment.  */
4679
4680 static bool
4681 rs6000_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed)
4682 {
4683   if (is_packed)
4684     return false;
4685
4686   if (TARGET_32BIT)
4687     {
4688       if (rs6000_alignment_flags == MASK_ALIGN_NATURAL)
4689         return true;
4690
4691       if (rs6000_alignment_flags ==  MASK_ALIGN_POWER)
4692         return true;
4693
4694       return false;
4695     }
4696   else
4697     {
4698       if (TARGET_MACHO)
4699         return false;
4700
4701       /* Assuming that all other types are naturally aligned. CHECKME!  */
4702       return true;
4703     }
4704 }
4705
4706 /* Return true if the vector misalignment factor is supported by the
4707    target.  */ 
4708 static bool
4709 rs6000_builtin_support_vector_misalignment (machine_mode mode,
4710                                             const_tree type,
4711                                             int misalignment,
4712                                             bool is_packed)
4713 {
4714   if (TARGET_VSX)
4715     {
4716       if (TARGET_EFFICIENT_UNALIGNED_VSX)
4717         return true;
4718
4719       /* Return if movmisalign pattern is not supported for this mode.  */
4720       if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
4721         return false;
4722
4723       if (misalignment == -1)
4724         {
4725           /* Misalignment factor is unknown at compile time but we know
4726              it's word aligned.  */
4727           if (rs6000_vector_alignment_reachable (type, is_packed))
4728             {
4729               int element_size = TREE_INT_CST_LOW (TYPE_SIZE (type));
4730
4731               if (element_size == 64 || element_size == 32)
4732                return true;
4733             }
4734
4735           return false;
4736         }
4737
4738       /* VSX supports word-aligned vector.  */
4739       if (misalignment % 4 == 0)
4740         return true;
4741     }
4742   return false;
4743 }
4744
4745 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
4746 static int
4747 rs6000_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4748                                    tree vectype, int misalign)
4749 {
4750   unsigned elements;
4751   tree elem_type;
4752
4753   switch (type_of_cost)
4754     {
4755       case scalar_stmt:
4756       case scalar_load:
4757       case scalar_store:
4758       case vector_stmt:
4759       case vector_load:
4760       case vector_store:
4761       case vec_to_scalar:
4762       case scalar_to_vec:
4763       case cond_branch_not_taken:
4764         return 1;
4765
4766       case vec_perm:
4767         if (TARGET_VSX)
4768           return 3;
4769         else
4770           return 1;
4771
4772       case vec_promote_demote:
4773         if (TARGET_VSX)
4774           return 4;
4775         else
4776           return 1;
4777
4778       case cond_branch_taken:
4779         return 3;
4780
4781       case unaligned_load:
4782       case vector_gather_load:
4783         if (TARGET_EFFICIENT_UNALIGNED_VSX)
4784           return 1;
4785
4786         if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4787           {
4788             elements = TYPE_VECTOR_SUBPARTS (vectype);
4789             if (elements == 2)
4790               /* Double word aligned.  */
4791               return 2;
4792
4793             if (elements == 4)
4794               {
4795                 switch (misalign)
4796                   {
4797                     case 8:
4798                       /* Double word aligned.  */
4799                       return 2;
4800
4801                     case -1:
4802                       /* Unknown misalignment.  */
4803                     case 4:
4804                     case 12:
4805                       /* Word aligned.  */
4806                       return 22;
4807
4808                     default:
4809                       gcc_unreachable ();
4810                   }
4811               }
4812           }
4813
4814         if (TARGET_ALTIVEC)
4815           /* Misaligned loads are not supported.  */
4816           gcc_unreachable ();
4817
4818         return 2;
4819
4820       case unaligned_store:
4821       case vector_scatter_store:
4822         if (TARGET_EFFICIENT_UNALIGNED_VSX)
4823           return 1;
4824
4825         if (TARGET_VSX && TARGET_ALLOW_MOVMISALIGN)
4826           {
4827             elements = TYPE_VECTOR_SUBPARTS (vectype);
4828             if (elements == 2)
4829               /* Double word aligned.  */
4830               return 2;
4831
4832             if (elements == 4)
4833               {
4834                 switch (misalign)
4835                   {
4836                     case 8:
4837                       /* Double word aligned.  */
4838                       return 2;
4839
4840                     case -1:
4841                       /* Unknown misalignment.  */
4842                     case 4:
4843                     case 12:
4844                       /* Word aligned.  */
4845                       return 23;
4846
4847                     default:
4848                       gcc_unreachable ();
4849                   }
4850               }
4851           }
4852
4853         if (TARGET_ALTIVEC)
4854           /* Misaligned stores are not supported.  */
4855           gcc_unreachable ();
4856
4857         return 2;
4858
4859       case vec_construct:
4860         /* This is a rough approximation assuming non-constant elements
4861            constructed into a vector via element insertion.  FIXME:
4862            vec_construct is not granular enough for uniformly good
4863            decisions.  If the initialization is a splat, this is
4864            cheaper than we estimate.  Improve this someday.  */
4865         elem_type = TREE_TYPE (vectype);
4866         /* 32-bit vectors loaded into registers are stored as double
4867            precision, so we need 2 permutes, 2 converts, and 1 merge
4868            to construct a vector of short floats from them.  */
4869         if (SCALAR_FLOAT_TYPE_P (elem_type)
4870             && TYPE_PRECISION (elem_type) == 32)
4871           return 5;
4872         /* On POWER9, integer vector types are built up in GPRs and then
4873            use a direct move (2 cycles).  For POWER8 this is even worse,
4874            as we need two direct moves and a merge, and the direct moves
4875            are five cycles.  */
4876         else if (INTEGRAL_TYPE_P (elem_type))
4877           {
4878             if (TARGET_P9_VECTOR)
4879               return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 2;
4880             else
4881               return TYPE_VECTOR_SUBPARTS (vectype) - 1 + 5;
4882           }
4883         else
4884           /* V2DFmode doesn't need a direct move.  */
4885           return 2;
4886
4887       default:
4888         gcc_unreachable ();
4889     }
4890 }
4891
4892 /* Implement targetm.vectorize.preferred_simd_mode.  */
4893
4894 static machine_mode
4895 rs6000_preferred_simd_mode (scalar_mode mode)
4896 {
4897   if (TARGET_VSX)
4898     switch (mode)
4899       {
4900       case E_DFmode:
4901         return V2DFmode;
4902       default:;
4903       }
4904   if (TARGET_ALTIVEC || TARGET_VSX)
4905     switch (mode)
4906       {
4907       case E_SFmode:
4908         return V4SFmode;
4909       case E_TImode:
4910         return V1TImode;
4911       case E_DImode:
4912         return V2DImode;
4913       case E_SImode:
4914         return V4SImode;
4915       case E_HImode:
4916         return V8HImode;
4917       case E_QImode:
4918         return V16QImode;
4919       default:;
4920       }
4921   return word_mode;
4922 }
4923
4924 typedef struct _rs6000_cost_data
4925 {
4926   struct loop *loop_info;
4927   unsigned cost[3];
4928 } rs6000_cost_data;
4929
4930 /* Test for likely overcommitment of vector hardware resources.  If a
4931    loop iteration is relatively large, and too large a percentage of
4932    instructions in the loop are vectorized, the cost model may not
4933    adequately reflect delays from unavailable vector resources.
4934    Penalize the loop body cost for this case.  */
4935
4936 static void
4937 rs6000_density_test (rs6000_cost_data *data)
4938 {
4939   const int DENSITY_PCT_THRESHOLD = 85;
4940   const int DENSITY_SIZE_THRESHOLD = 70;
4941   const int DENSITY_PENALTY = 10;
4942   struct loop *loop = data->loop_info;
4943   basic_block *bbs = get_loop_body (loop);
4944   int nbbs = loop->num_nodes;
4945   loop_vec_info loop_vinfo = loop_vec_info_for_loop (data->loop_info);
4946   int vec_cost = data->cost[vect_body], not_vec_cost = 0;
4947   int i, density_pct;
4948
4949   for (i = 0; i < nbbs; i++)
4950     {
4951       basic_block bb = bbs[i];
4952       gimple_stmt_iterator gsi;
4953
4954       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4955         {
4956           gimple *stmt = gsi_stmt (gsi);
4957           stmt_vec_info stmt_info = loop_vinfo->lookup_stmt (stmt);
4958
4959           if (!STMT_VINFO_RELEVANT_P (stmt_info)
4960               && !STMT_VINFO_IN_PATTERN_P (stmt_info))
4961             not_vec_cost++;
4962         }
4963     }
4964
4965   free (bbs);
4966   density_pct = (vec_cost * 100) / (vec_cost + not_vec_cost);
4967
4968   if (density_pct > DENSITY_PCT_THRESHOLD
4969       && vec_cost + not_vec_cost > DENSITY_SIZE_THRESHOLD)
4970     {
4971       data->cost[vect_body] = vec_cost * (100 + DENSITY_PENALTY) / 100;
4972       if (dump_enabled_p ())
4973         dump_printf_loc (MSG_NOTE, vect_location,
4974                          "density %d%%, cost %d exceeds threshold, penalizing "
4975                          "loop body cost by %d%%", density_pct,
4976                          vec_cost + not_vec_cost, DENSITY_PENALTY);
4977     }
4978 }
4979
4980 /* Implement targetm.vectorize.init_cost.  */
4981
4982 /* For each vectorized loop, this var holds TRUE iff a non-memory vector
4983    instruction is needed by the vectorization.  */
4984 static bool rs6000_vect_nonmem;
4985
4986 static void *
4987 rs6000_init_cost (struct loop *loop_info)
4988 {
4989   rs6000_cost_data *data = XNEW (struct _rs6000_cost_data);
4990   data->loop_info = loop_info;
4991   data->cost[vect_prologue] = 0;
4992   data->cost[vect_body]     = 0;
4993   data->cost[vect_epilogue] = 0;
4994   rs6000_vect_nonmem = false;
4995   return data;
4996 }
4997
4998 /* Implement targetm.vectorize.add_stmt_cost.  */
4999
5000 static unsigned
5001 rs6000_add_stmt_cost (void *data, int count, enum vect_cost_for_stmt kind,
5002                       struct _stmt_vec_info *stmt_info, int misalign,
5003                       enum vect_cost_model_location where)
5004 {
5005   rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
5006   unsigned retval = 0;
5007
5008   if (flag_vect_cost_model)
5009     {
5010       tree vectype = stmt_info ? stmt_vectype (stmt_info) : NULL_TREE;
5011       int stmt_cost = rs6000_builtin_vectorization_cost (kind, vectype,
5012                                                          misalign);
5013       /* Statements in an inner loop relative to the loop being
5014          vectorized are weighted more heavily.  The value here is
5015          arbitrary and could potentially be improved with analysis.  */
5016       if (where == vect_body && stmt_info && stmt_in_inner_loop_p (stmt_info))
5017         count *= 50;  /* FIXME.  */
5018
5019       retval = (unsigned) (count * stmt_cost);
5020       cost_data->cost[where] += retval;
5021
5022       /* Check whether we're doing something other than just a copy loop.
5023          Not all such loops may be profitably vectorized; see
5024          rs6000_finish_cost.  */
5025       if ((kind == vec_to_scalar || kind == vec_perm
5026            || kind == vec_promote_demote || kind == vec_construct
5027            || kind == scalar_to_vec)
5028           || (where == vect_body && kind == vector_stmt))
5029         rs6000_vect_nonmem = true;
5030     }
5031
5032   return retval;
5033 }
5034
5035 /* Implement targetm.vectorize.finish_cost.  */
5036
5037 static void
5038 rs6000_finish_cost (void *data, unsigned *prologue_cost,
5039                     unsigned *body_cost, unsigned *epilogue_cost)
5040 {
5041   rs6000_cost_data *cost_data = (rs6000_cost_data*) data;
5042
5043   if (cost_data->loop_info)
5044     rs6000_density_test (cost_data);
5045
5046   /* Don't vectorize minimum-vectorization-factor, simple copy loops
5047      that require versioning for any reason.  The vectorization is at
5048      best a wash inside the loop, and the versioning checks make
5049      profitability highly unlikely and potentially quite harmful.  */
5050   if (cost_data->loop_info)
5051     {
5052       loop_vec_info vec_info = loop_vec_info_for_loop (cost_data->loop_info);
5053       if (!rs6000_vect_nonmem
5054           && LOOP_VINFO_VECT_FACTOR (vec_info) == 2
5055           && LOOP_REQUIRES_VERSIONING (vec_info))
5056         cost_data->cost[vect_body] += 10000;
5057     }
5058
5059   *prologue_cost = cost_data->cost[vect_prologue];
5060   *body_cost     = cost_data->cost[vect_body];
5061   *epilogue_cost = cost_data->cost[vect_epilogue];
5062 }
5063
5064 /* Implement targetm.vectorize.destroy_cost_data.  */
5065
5066 static void
5067 rs6000_destroy_cost_data (void *data)
5068 {
5069   free (data);
5070 }
5071
5072 /* Handler for the Mathematical Acceleration Subsystem (mass) interface to a
5073    library with vectorized intrinsics.  */
5074
5075 static tree
5076 rs6000_builtin_vectorized_libmass (combined_fn fn, tree type_out,
5077                                    tree type_in)
5078 {
5079   char name[32];
5080   const char *suffix = NULL;
5081   tree fntype, new_fndecl, bdecl = NULL_TREE;
5082   int n_args = 1;
5083   const char *bname;
5084   machine_mode el_mode, in_mode;
5085   int n, in_n;
5086
5087   /* Libmass is suitable for unsafe math only as it does not correctly support
5088      parts of IEEE with the required precision such as denormals.  Only support
5089      it if we have VSX to use the simd d2 or f4 functions.
5090      XXX: Add variable length support.  */
5091   if (!flag_unsafe_math_optimizations || !TARGET_VSX)
5092     return NULL_TREE;
5093
5094   el_mode = TYPE_MODE (TREE_TYPE (type_out));
5095   n = TYPE_VECTOR_SUBPARTS (type_out);
5096   in_mode = TYPE_MODE (TREE_TYPE (type_in));
5097   in_n = TYPE_VECTOR_SUBPARTS (type_in);
5098   if (el_mode != in_mode
5099       || n != in_n)
5100     return NULL_TREE;
5101
5102   switch (fn)
5103     {
5104     CASE_CFN_ATAN2:
5105     CASE_CFN_HYPOT:
5106     CASE_CFN_POW:
5107       n_args = 2;
5108       gcc_fallthrough ();
5109
5110     CASE_CFN_ACOS:
5111     CASE_CFN_ACOSH:
5112     CASE_CFN_ASIN:
5113     CASE_CFN_ASINH:
5114     CASE_CFN_ATAN:
5115     CASE_CFN_ATANH:
5116     CASE_CFN_CBRT:
5117     CASE_CFN_COS:
5118     CASE_CFN_COSH:
5119     CASE_CFN_ERF:
5120     CASE_CFN_ERFC:
5121     CASE_CFN_EXP2:
5122     CASE_CFN_EXP:
5123     CASE_CFN_EXPM1:
5124     CASE_CFN_LGAMMA:
5125     CASE_CFN_LOG10:
5126     CASE_CFN_LOG1P:
5127     CASE_CFN_LOG2:
5128     CASE_CFN_LOG:
5129     CASE_CFN_SIN:
5130     CASE_CFN_SINH:
5131     CASE_CFN_SQRT:
5132     CASE_CFN_TAN:
5133     CASE_CFN_TANH:
5134       if (el_mode == DFmode && n == 2)
5135         {
5136           bdecl = mathfn_built_in (double_type_node, fn);
5137           suffix = "d2";                                /* pow -> powd2 */
5138         }
5139       else if (el_mode == SFmode && n == 4)
5140         {
5141           bdecl = mathfn_built_in (float_type_node, fn);
5142           suffix = "4";                                 /* powf -> powf4 */
5143         }
5144       else
5145         return NULL_TREE;
5146       if (!bdecl)
5147         return NULL_TREE;
5148       break;
5149
5150     default:
5151       return NULL_TREE;
5152     }
5153
5154   gcc_assert (suffix != NULL);
5155   bname = IDENTIFIER_POINTER (DECL_NAME (bdecl));
5156   if (!bname)
5157     return NULL_TREE;
5158
5159   strcpy (name, bname + sizeof ("__builtin_") - 1);
5160   strcat (name, suffix);
5161
5162   if (n_args == 1)
5163     fntype = build_function_type_list (type_out, type_in, NULL);
5164   else if (n_args == 2)
5165     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
5166   else
5167     gcc_unreachable ();
5168
5169   /* Build a function declaration for the vectorized function.  */
5170   new_fndecl = build_decl (BUILTINS_LOCATION,
5171                            FUNCTION_DECL, get_identifier (name), fntype);
5172   TREE_PUBLIC (new_fndecl) = 1;
5173   DECL_EXTERNAL (new_fndecl) = 1;
5174   DECL_IS_NOVOPS (new_fndecl) = 1;
5175   TREE_READONLY (new_fndecl) = 1;
5176
5177   return new_fndecl;
5178 }
5179
5180 /* Returns a function decl for a vectorized version of the builtin function
5181    with builtin function code FN and the result vector type TYPE, or NULL_TREE
5182    if it is not available.  */
5183
5184 static tree
5185 rs6000_builtin_vectorized_function (unsigned int fn, tree type_out,
5186                                     tree type_in)
5187 {
5188   machine_mode in_mode, out_mode;
5189   int in_n, out_n;
5190
5191   if (TARGET_DEBUG_BUILTIN)
5192     fprintf (stderr, "rs6000_builtin_vectorized_function (%s, %s, %s)\n",
5193              combined_fn_name (combined_fn (fn)),
5194              GET_MODE_NAME (TYPE_MODE (type_out)),
5195              GET_MODE_NAME (TYPE_MODE (type_in)));
5196
5197   if (TREE_CODE (type_out) != VECTOR_TYPE
5198       || TREE_CODE (type_in) != VECTOR_TYPE)
5199     return NULL_TREE;
5200
5201   out_mode = TYPE_MODE (TREE_TYPE (type_out));
5202   out_n = TYPE_VECTOR_SUBPARTS (type_out);
5203   in_mode = TYPE_MODE (TREE_TYPE (type_in));
5204   in_n = TYPE_VECTOR_SUBPARTS (type_in);
5205
5206   switch (fn)
5207     {
5208     CASE_CFN_COPYSIGN:
5209       if (VECTOR_UNIT_VSX_P (V2DFmode)
5210           && out_mode == DFmode && out_n == 2
5211           && in_mode == DFmode && in_n == 2)
5212         return rs6000_builtin_decls[VSX_BUILTIN_CPSGNDP];
5213       if (VECTOR_UNIT_VSX_P (V4SFmode)
5214           && out_mode == SFmode && out_n == 4
5215           && in_mode == SFmode && in_n == 4)
5216         return rs6000_builtin_decls[VSX_BUILTIN_CPSGNSP];
5217       if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5218           && out_mode == SFmode && out_n == 4
5219           && in_mode == SFmode && in_n == 4)
5220         return rs6000_builtin_decls[ALTIVEC_BUILTIN_COPYSIGN_V4SF];
5221       break;
5222     CASE_CFN_CEIL:
5223       if (VECTOR_UNIT_VSX_P (V2DFmode)
5224           && out_mode == DFmode && out_n == 2
5225           && in_mode == DFmode && in_n == 2)
5226         return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIP];
5227       if (VECTOR_UNIT_VSX_P (V4SFmode)
5228           && out_mode == SFmode && out_n == 4
5229           && in_mode == SFmode && in_n == 4)
5230         return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIP];
5231       if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5232           && out_mode == SFmode && out_n == 4
5233           && in_mode == SFmode && in_n == 4)
5234         return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIP];
5235       break;
5236     CASE_CFN_FLOOR:
5237       if (VECTOR_UNIT_VSX_P (V2DFmode)
5238           && out_mode == DFmode && out_n == 2
5239           && in_mode == DFmode && in_n == 2)
5240         return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIM];
5241       if (VECTOR_UNIT_VSX_P (V4SFmode)
5242           && out_mode == SFmode && out_n == 4
5243           && in_mode == SFmode && in_n == 4)
5244         return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIM];
5245       if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5246           && out_mode == SFmode && out_n == 4
5247           && in_mode == SFmode && in_n == 4)
5248         return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIM];
5249       break;
5250     CASE_CFN_FMA:
5251       if (VECTOR_UNIT_VSX_P (V2DFmode)
5252           && out_mode == DFmode && out_n == 2
5253           && in_mode == DFmode && in_n == 2)
5254         return rs6000_builtin_decls[VSX_BUILTIN_XVMADDDP];
5255       if (VECTOR_UNIT_VSX_P (V4SFmode)
5256           && out_mode == SFmode && out_n == 4
5257           && in_mode == SFmode && in_n == 4)
5258         return rs6000_builtin_decls[VSX_BUILTIN_XVMADDSP];
5259       if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5260           && out_mode == SFmode && out_n == 4
5261           && in_mode == SFmode && in_n == 4)
5262         return rs6000_builtin_decls[ALTIVEC_BUILTIN_VMADDFP];
5263       break;
5264     CASE_CFN_TRUNC:
5265       if (VECTOR_UNIT_VSX_P (V2DFmode)
5266           && out_mode == DFmode && out_n == 2
5267           && in_mode == DFmode && in_n == 2)
5268         return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIZ];
5269       if (VECTOR_UNIT_VSX_P (V4SFmode)
5270           && out_mode == SFmode && out_n == 4
5271           && in_mode == SFmode && in_n == 4)
5272         return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIZ];
5273       if (VECTOR_UNIT_ALTIVEC_P (V4SFmode)
5274           && out_mode == SFmode && out_n == 4
5275           && in_mode == SFmode && in_n == 4)
5276         return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRFIZ];
5277       break;
5278     CASE_CFN_NEARBYINT:
5279       if (VECTOR_UNIT_VSX_P (V2DFmode)
5280           && flag_unsafe_math_optimizations
5281           && out_mode == DFmode && out_n == 2
5282           && in_mode == DFmode && in_n == 2)
5283         return rs6000_builtin_decls[VSX_BUILTIN_XVRDPI];
5284       if (VECTOR_UNIT_VSX_P (V4SFmode)
5285           && flag_unsafe_math_optimizations
5286           && out_mode == SFmode && out_n == 4
5287           && in_mode == SFmode && in_n == 4)
5288         return rs6000_builtin_decls[VSX_BUILTIN_XVRSPI];
5289       break;
5290     CASE_CFN_RINT:
5291       if (VECTOR_UNIT_VSX_P (V2DFmode)
5292           && !flag_trapping_math
5293           && out_mode == DFmode && out_n == 2
5294           && in_mode == DFmode && in_n == 2)
5295         return rs6000_builtin_decls[VSX_BUILTIN_XVRDPIC];
5296       if (VECTOR_UNIT_VSX_P (V4SFmode)
5297           && !flag_trapping_math
5298           && out_mode == SFmode && out_n == 4
5299           && in_mode == SFmode && in_n == 4)
5300         return rs6000_builtin_decls[VSX_BUILTIN_XVRSPIC];
5301       break;
5302     default:
5303       break;
5304     }
5305
5306   /* Generate calls to libmass if appropriate.  */
5307   if (rs6000_veclib_handler)
5308     return rs6000_veclib_handler (combined_fn (fn), type_out, type_in);
5309
5310   return NULL_TREE;
5311 }
5312
5313 /* Implement TARGET_VECTORIZE_BUILTIN_MD_VECTORIZED_FUNCTION.  */
5314
5315 static tree
5316 rs6000_builtin_md_vectorized_function (tree fndecl, tree type_out,
5317                                        tree type_in)
5318 {
5319   machine_mode in_mode, out_mode;
5320   int in_n, out_n;
5321
5322   if (TARGET_DEBUG_BUILTIN)
5323     fprintf (stderr, "rs6000_builtin_md_vectorized_function (%s, %s, %s)\n",
5324              IDENTIFIER_POINTER (DECL_NAME (fndecl)),
5325              GET_MODE_NAME (TYPE_MODE (type_out)),
5326              GET_MODE_NAME (TYPE_MODE (type_in)));
5327
5328   if (TREE_CODE (type_out) != VECTOR_TYPE
5329       || TREE_CODE (type_in) != VECTOR_TYPE)
5330     return NULL_TREE;
5331
5332   out_mode = TYPE_MODE (TREE_TYPE (type_out));
5333   out_n = TYPE_VECTOR_SUBPARTS (type_out);
5334   in_mode = TYPE_MODE (TREE_TYPE (type_in));
5335   in_n = TYPE_VECTOR_SUBPARTS (type_in);
5336
5337   enum rs6000_builtins fn
5338     = (enum rs6000_builtins) DECL_MD_FUNCTION_CODE (fndecl);
5339   switch (fn)
5340     {
5341     case RS6000_BUILTIN_RSQRTF:
5342       if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
5343           && out_mode == SFmode && out_n == 4
5344           && in_mode == SFmode && in_n == 4)
5345         return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRSQRTFP];
5346       break;
5347     case RS6000_BUILTIN_RSQRT:
5348       if (VECTOR_UNIT_VSX_P (V2DFmode)
5349           && out_mode == DFmode && out_n == 2
5350           && in_mode == DFmode && in_n == 2)
5351         return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
5352       break;
5353     case RS6000_BUILTIN_RECIPF:
5354       if (VECTOR_UNIT_ALTIVEC_OR_VSX_P (V4SFmode)
5355           && out_mode == SFmode && out_n == 4
5356           && in_mode == SFmode && in_n == 4)
5357         return rs6000_builtin_decls[ALTIVEC_BUILTIN_VRECIPFP];
5358       break;
5359     case RS6000_BUILTIN_RECIP:
5360       if (VECTOR_UNIT_VSX_P (V2DFmode)
5361           && out_mode == DFmode && out_n == 2
5362           && in_mode == DFmode && in_n == 2)
5363         return rs6000_builtin_decls[VSX_BUILTIN_RECIP_V2DF];
5364       break;
5365     default:
5366       break;
5367     }
5368   return NULL_TREE;
5369 }
5370 \f
5371 /* Default CPU string for rs6000*_file_start functions.  */
5372 static const char *rs6000_default_cpu;
5373
5374 #ifdef USING_ELFOS_H
5375 const char *rs6000_machine;
5376
5377 const char *
5378 rs6000_machine_from_flags (void)
5379 {
5380   HOST_WIDE_INT flags = rs6000_isa_flags;
5381
5382   /* Disable the flags that should never influence the .machine selection.  */
5383   flags &= ~(OPTION_MASK_PPC_GFXOPT | OPTION_MASK_PPC_GPOPT);
5384
5385   if ((flags & (ISA_FUTURE_MASKS_SERVER & ~ISA_3_0_MASKS_SERVER)) != 0)
5386     return "future";
5387   if ((flags & (ISA_3_0_MASKS_SERVER & ~ISA_2_7_MASKS_SERVER)) != 0)
5388     return "power9";
5389   if ((flags & (ISA_2_7_MASKS_SERVER & ~ISA_2_6_MASKS_SERVER)) != 0)
5390     return "power8";
5391   if ((flags & (ISA_2_6_MASKS_SERVER & ~ISA_2_5_MASKS_SERVER)) != 0)
5392     return "power7";
5393   if ((flags & (ISA_2_5_MASKS_SERVER & ~ISA_2_4_MASKS)) != 0)
5394     return "power6";
5395   if ((flags & (ISA_2_4_MASKS & ~ISA_2_1_MASKS)) != 0)
5396     return "power5";
5397   if ((flags & ISA_2_1_MASKS) != 0)
5398     return "power4";
5399   if ((flags & OPTION_MASK_POWERPC64) != 0)
5400     return "ppc64";
5401   return "ppc";
5402 }
5403
5404 void
5405 emit_asm_machine (void)
5406 {
5407   fprintf (asm_out_file, "\t.machine %s\n", rs6000_machine);
5408 }
5409 #endif
5410
5411 /* Do anything needed at the start of the asm file.  */
5412
5413 static void
5414 rs6000_file_start (void)
5415 {
5416   char buffer[80];
5417   const char *start = buffer;
5418   FILE *file = asm_out_file;
5419
5420   rs6000_default_cpu = TARGET_CPU_DEFAULT;
5421
5422   default_file_start ();
5423
5424   if (flag_verbose_asm)
5425     {
5426       sprintf (buffer, "\n%s rs6000/powerpc options:", ASM_COMMENT_START);
5427
5428       if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
5429         {
5430           fprintf (file, "%s --with-cpu=%s", start, rs6000_default_cpu);
5431           start = "";
5432         }
5433
5434       if (global_options_set.x_rs6000_cpu_index)
5435         {
5436           fprintf (file, "%s -mcpu=%s", start,
5437                    processor_target_table[rs6000_cpu_index].name);
5438           start = "";
5439         }
5440
5441       if (global_options_set.x_rs6000_tune_index)
5442         {
5443           fprintf (file, "%s -mtune=%s", start,
5444                    processor_target_table[rs6000_tune_index].name);
5445           start = "";
5446         }
5447
5448       if (PPC405_ERRATUM77)
5449         {
5450           fprintf (file, "%s PPC405CR_ERRATUM77", start);
5451           start = "";
5452         }
5453
5454 #ifdef USING_ELFOS_H
5455       switch (rs6000_sdata)
5456         {
5457         case SDATA_NONE: fprintf (file, "%s -msdata=none", start); start = ""; break;
5458         case SDATA_DATA: fprintf (file, "%s -msdata=data", start); start = ""; break;
5459         case SDATA_SYSV: fprintf (file, "%s -msdata=sysv", start); start = ""; break;
5460         case SDATA_EABI: fprintf (file, "%s -msdata=eabi", start); start = ""; break;
5461         }
5462
5463       if (rs6000_sdata && g_switch_value)
5464         {
5465           fprintf (file, "%s -G %d", start,
5466                    g_switch_value);
5467           start = "";
5468         }
5469 #endif
5470
5471       if (*start == '\0')
5472         putc ('\n', file);
5473     }
5474
5475 #ifdef USING_ELFOS_H
5476   rs6000_machine = rs6000_machine_from_flags ();
5477   emit_asm_machine ();
5478 #endif
5479
5480   if (DEFAULT_ABI == ABI_ELFv2)
5481     fprintf (file, "\t.abiversion 2\n");
5482 }
5483
5484 \f
5485 /* Return nonzero if this function is known to have a null epilogue.  */
5486
5487 int
5488 direct_return (void)
5489 {
5490   if (reload_completed)
5491     {
5492       rs6000_stack_t *info = rs6000_stack_info ();
5493
5494       if (info->first_gp_reg_save == 32
5495           && info->first_fp_reg_save == 64
5496           && info->first_altivec_reg_save == LAST_ALTIVEC_REGNO + 1
5497           && ! info->lr_save_p
5498           && ! info->cr_save_p
5499           && info->vrsave_size == 0
5500           && ! info->push_p)
5501         return 1;
5502     }
5503
5504   return 0;
5505 }
5506
5507 /* Helper for num_insns_constant.  Calculate number of instructions to
5508    load VALUE to a single gpr using combinations of addi, addis, ori,
5509    oris and sldi instructions.  */
5510
5511 static int
5512 num_insns_constant_gpr (HOST_WIDE_INT value)
5513 {
5514   /* signed constant loadable with addi */
5515   if (((unsigned HOST_WIDE_INT) value + 0x8000) < 0x10000)
5516     return 1;
5517
5518   /* constant loadable with addis */
5519   else if ((value & 0xffff) == 0
5520            && (value >> 31 == -1 || value >> 31 == 0))
5521     return 1;
5522
5523   else if (TARGET_POWERPC64)
5524     {
5525       HOST_WIDE_INT low  = ((value & 0xffffffff) ^ 0x80000000) - 0x80000000;
5526       HOST_WIDE_INT high = value >> 31;
5527
5528       if (high == 0 || high == -1)
5529         return 2;
5530
5531       high >>= 1;
5532
5533       if (low == 0)
5534         return num_insns_constant_gpr (high) + 1;
5535       else if (high == 0)
5536         return num_insns_constant_gpr (low) + 1;
5537       else
5538         return (num_insns_constant_gpr (high)
5539                 + num_insns_constant_gpr (low) + 1);
5540     }
5541
5542   else
5543     return 2;
5544 }
5545
5546 /* Helper for num_insns_constant.  Allow constants formed by the
5547    num_insns_constant_gpr sequences, plus li -1, rldicl/rldicr/rlwinm,
5548    and handle modes that require multiple gprs.  */
5549
5550 static int
5551 num_insns_constant_multi (HOST_WIDE_INT value, machine_mode mode)
5552 {
5553   int nregs = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5554   int total = 0;
5555   while (nregs-- > 0)
5556     {
5557       HOST_WIDE_INT low = sext_hwi (value, BITS_PER_WORD);
5558       int insns = num_insns_constant_gpr (low);
5559       if (insns > 2
5560           /* We won't get more than 2 from num_insns_constant_gpr
5561              except when TARGET_POWERPC64 and mode is DImode or
5562              wider, so the register mode must be DImode.  */
5563           && rs6000_is_valid_and_mask (GEN_INT (low), DImode))
5564         insns = 2;
5565       total += insns;
5566       value >>= BITS_PER_WORD;
5567     }
5568   return total;
5569 }
5570
5571 /* Return the number of instructions it takes to form a constant in as
5572    many gprs are needed for MODE.  */
5573
5574 int
5575 num_insns_constant (rtx op, machine_mode mode)
5576 {
5577   HOST_WIDE_INT val;
5578
5579   switch (GET_CODE (op))
5580     {
5581     case CONST_INT:
5582       val = INTVAL (op);
5583       break;
5584
5585     case CONST_WIDE_INT:
5586       {
5587         int insns = 0;
5588         for (int i = 0; i < CONST_WIDE_INT_NUNITS (op); i++)
5589           insns += num_insns_constant_multi (CONST_WIDE_INT_ELT (op, i),
5590                                              DImode);
5591         return insns;
5592       }
5593
5594     case CONST_DOUBLE:
5595       {
5596         const struct real_value *rv = CONST_DOUBLE_REAL_VALUE (op);
5597
5598         if (mode == SFmode || mode == SDmode)
5599           {
5600             long l;
5601
5602             if (mode == SDmode)
5603               REAL_VALUE_TO_TARGET_DECIMAL32 (*rv, l);
5604             else
5605               REAL_VALUE_TO_TARGET_SINGLE (*rv, l);
5606             /* See the first define_split in rs6000.md handling a
5607                const_double_operand.  */
5608             val = l;
5609             mode = SImode;
5610           }
5611         else if (mode == DFmode || mode == DDmode)
5612           {
5613             long l[2];
5614
5615             if (mode == DDmode)
5616               REAL_VALUE_TO_TARGET_DECIMAL64 (*rv, l);
5617             else
5618               REAL_VALUE_TO_TARGET_DOUBLE (*rv, l);
5619
5620             /* See the second (32-bit) and third (64-bit) define_split
5621                in rs6000.md handling a const_double_operand.  */
5622             val = (unsigned HOST_WIDE_INT) l[WORDS_BIG_ENDIAN ? 0 : 1] << 32;
5623             val |= l[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffffUL;
5624             mode = DImode;
5625           }
5626         else if (mode == TFmode || mode == TDmode
5627                  || mode == KFmode || mode == IFmode)
5628           {
5629             long l[4];
5630             int insns;
5631
5632             if (mode == TDmode)
5633               REAL_VALUE_TO_TARGET_DECIMAL128 (*rv, l);
5634             else
5635               REAL_VALUE_TO_TARGET_LONG_DOUBLE (*rv, l);
5636
5637             val = (unsigned HOST_WIDE_INT) l[WORDS_BIG_ENDIAN ? 0 : 3] << 32;
5638             val |= l[WORDS_BIG_ENDIAN ? 1 : 2] & 0xffffffffUL;
5639             insns = num_insns_constant_multi (val, DImode);
5640             val = (unsigned HOST_WIDE_INT) l[WORDS_BIG_ENDIAN ? 2 : 1] << 32;
5641             val |= l[WORDS_BIG_ENDIAN ? 3 : 0] & 0xffffffffUL;
5642             insns += num_insns_constant_multi (val, DImode);
5643             return insns;
5644           }
5645         else
5646           gcc_unreachable ();
5647       }
5648       break;
5649
5650     default:
5651       gcc_unreachable ();
5652     }
5653
5654   return num_insns_constant_multi (val, mode);
5655 }
5656
5657 /* Interpret element ELT of the CONST_VECTOR OP as an integer value.
5658    If the mode of OP is MODE_VECTOR_INT, this simply returns the
5659    corresponding element of the vector, but for V4SFmode, the
5660    corresponding "float" is interpreted as an SImode integer.  */
5661
5662 HOST_WIDE_INT
5663 const_vector_elt_as_int (rtx op, unsigned int elt)
5664 {
5665   rtx tmp;
5666
5667   /* We can't handle V2DImode and V2DFmode vector constants here yet.  */
5668   gcc_assert (GET_MODE (op) != V2DImode
5669               && GET_MODE (op) != V2DFmode);
5670
5671   tmp = CONST_VECTOR_ELT (op, elt);
5672   if (GET_MODE (op) == V4SFmode)
5673     tmp = gen_lowpart (SImode, tmp);
5674   return INTVAL (tmp);
5675 }
5676
5677 /* Return true if OP can be synthesized with a particular vspltisb, vspltish
5678    or vspltisw instruction.  OP is a CONST_VECTOR.  Which instruction is used
5679    depends on STEP and COPIES, one of which will be 1.  If COPIES > 1,
5680    all items are set to the same value and contain COPIES replicas of the
5681    vsplt's operand; if STEP > 1, one in STEP elements is set to the vsplt's
5682    operand and the others are set to the value of the operand's msb.  */
5683
5684 static bool
5685 vspltis_constant (rtx op, unsigned step, unsigned copies)
5686 {
5687   machine_mode mode = GET_MODE (op);
5688   machine_mode inner = GET_MODE_INNER (mode);
5689
5690   unsigned i;
5691   unsigned nunits;
5692   unsigned bitsize;
5693   unsigned mask;
5694
5695   HOST_WIDE_INT val;
5696   HOST_WIDE_INT splat_val;
5697   HOST_WIDE_INT msb_val;
5698
5699   if (mode == V2DImode || mode == V2DFmode || mode == V1TImode)
5700     return false;
5701
5702   nunits = GET_MODE_NUNITS (mode);
5703   bitsize = GET_MODE_BITSIZE (inner);
5704   mask = GET_MODE_MASK (inner);
5705
5706   val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5707   splat_val = val;
5708   msb_val = val >= 0 ? 0 : -1;
5709
5710   /* Construct the value to be splatted, if possible.  If not, return 0.  */
5711   for (i = 2; i <= copies; i *= 2)
5712     {
5713       HOST_WIDE_INT small_val;
5714       bitsize /= 2;
5715       small_val = splat_val >> bitsize;
5716       mask >>= bitsize;
5717       if (splat_val != ((HOST_WIDE_INT)
5718           ((unsigned HOST_WIDE_INT) small_val << bitsize)
5719           | (small_val & mask)))
5720         return false;
5721       splat_val = small_val;
5722     }
5723
5724   /* Check if SPLAT_VAL can really be the operand of a vspltis[bhw].  */
5725   if (EASY_VECTOR_15 (splat_val))
5726     ;
5727
5728   /* Also check if we can splat, and then add the result to itself.  Do so if
5729      the value is positive, of if the splat instruction is using OP's mode;
5730      for splat_val < 0, the splat and the add should use the same mode.  */
5731   else if (EASY_VECTOR_15_ADD_SELF (splat_val)
5732            && (splat_val >= 0 || (step == 1 && copies == 1)))
5733     ;
5734
5735   /* Also check if are loading up the most significant bit which can be done by
5736      loading up -1 and shifting the value left by -1.  */
5737   else if (EASY_VECTOR_MSB (splat_val, inner))
5738     ;
5739
5740   else
5741     return false;
5742
5743   /* Check if VAL is present in every STEP-th element, and the
5744      other elements are filled with its most significant bit.  */
5745   for (i = 1; i < nunits; ++i)
5746     {
5747       HOST_WIDE_INT desired_val;
5748       unsigned elt = BYTES_BIG_ENDIAN ? nunits - 1 - i : i;
5749       if ((i & (step - 1)) == 0)
5750         desired_val = val;
5751       else
5752         desired_val = msb_val;
5753
5754       if (desired_val != const_vector_elt_as_int (op, elt))
5755         return false;
5756     }
5757
5758   return true;
5759 }
5760
5761 /* Like vsplitis_constant, but allow the value to be shifted left with a VSLDOI
5762    instruction, filling in the bottom elements with 0 or -1.
5763
5764    Return 0 if the constant cannot be generated with VSLDOI.  Return positive
5765    for the number of zeroes to shift in, or negative for the number of 0xff
5766    bytes to shift in.
5767
5768    OP is a CONST_VECTOR.  */
5769
5770 int
5771 vspltis_shifted (rtx op)
5772 {
5773   machine_mode mode = GET_MODE (op);
5774   machine_mode inner = GET_MODE_INNER (mode);
5775
5776   unsigned i, j;
5777   unsigned nunits;
5778   unsigned mask;
5779
5780   HOST_WIDE_INT val;
5781
5782   if (mode != V16QImode && mode != V8HImode && mode != V4SImode)
5783     return false;
5784
5785   /* We need to create pseudo registers to do the shift, so don't recognize
5786      shift vector constants after reload.  */
5787   if (!can_create_pseudo_p ())
5788     return false;
5789
5790   nunits = GET_MODE_NUNITS (mode);
5791   mask = GET_MODE_MASK (inner);
5792
5793   val = const_vector_elt_as_int (op, BYTES_BIG_ENDIAN ? 0 : nunits - 1);
5794
5795   /* Check if the value can really be the operand of a vspltis[bhw].  */
5796   if (EASY_VECTOR_15 (val))
5797     ;
5798
5799   /* Also check if we are loading up the most significant bit which can be done
5800      by loading up -1 and shifting the value left by -1.  */
5801   else if (EASY_VECTOR_MSB (val, inner))
5802     ;
5803
5804   else
5805     return 0;
5806
5807   /* Check if VAL is present in every STEP-th element until we find elements
5808      that are 0 or all 1 bits.  */
5809   for (i = 1; i < nunits; ++i)
5810     {
5811       unsigned elt = BYTES_BIG_ENDIAN ? i : nunits - 1 - i;
5812       HOST_WIDE_INT elt_val = const_vector_elt_as_int (op, elt);
5813
5814       /* If the value isn't the splat value, check for the remaining elements
5815          being 0/-1.  */
5816       if (val != elt_val)
5817         {
5818           if (elt_val == 0)
5819             {
5820               for (j = i+1; j < nunits; ++j)
5821                 {
5822                   unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
5823                   if (const_vector_elt_as_int (op, elt2) != 0)
5824                     return 0;
5825                 }
5826
5827               return (nunits - i) * GET_MODE_SIZE (inner);
5828             }
5829
5830           else if ((elt_val & mask) == mask)
5831             {
5832               for (j = i+1; j < nunits; ++j)
5833                 {
5834                   unsigned elt2 = BYTES_BIG_ENDIAN ? j : nunits - 1 - j;
5835                   if ((const_vector_elt_as_int (op, elt2) & mask) != mask)
5836                     return 0;
5837                 }
5838
5839               return -((nunits - i) * GET_MODE_SIZE (inner));
5840             }
5841
5842           else
5843             return 0;
5844         }
5845     }
5846
5847   /* If all elements are equal, we don't need to do VLSDOI.  */
5848   return 0;
5849 }
5850
5851
5852 /* Return true if OP is of the given MODE and can be synthesized
5853    with a vspltisb, vspltish or vspltisw.  */
5854
5855 bool
5856 easy_altivec_constant (rtx op, machine_mode mode)
5857 {
5858   unsigned step, copies;
5859
5860   if (mode == VOIDmode)
5861     mode = GET_MODE (op);
5862   else if (mode != GET_MODE (op))
5863     return false;
5864
5865   /* V2DI/V2DF was added with VSX.  Only allow 0 and all 1's as easy
5866      constants.  */
5867   if (mode == V2DFmode)
5868     return zero_constant (op, mode);
5869
5870   else if (mode == V2DImode)
5871     {
5872       if (!CONST_INT_P (CONST_VECTOR_ELT (op, 0))
5873           || !CONST_INT_P (CONST_VECTOR_ELT (op, 1)))
5874         return false;
5875
5876       if (zero_constant (op, mode))
5877         return true;
5878
5879       if (INTVAL (CONST_VECTOR_ELT (op, 0)) == -1
5880           && INTVAL (CONST_VECTOR_ELT (op, 1)) == -1)
5881         return true;
5882
5883       return false;
5884     }
5885
5886   /* V1TImode is a special container for TImode.  Ignore for now.  */
5887   else if (mode == V1TImode)
5888     return false;
5889
5890   /* Start with a vspltisw.  */
5891   step = GET_MODE_NUNITS (mode) / 4;
5892   copies = 1;
5893
5894   if (vspltis_constant (op, step, copies))
5895     return true;
5896
5897   /* Then try with a vspltish.  */
5898   if (step == 1)
5899     copies <<= 1;
5900   else
5901     step >>= 1;
5902
5903   if (vspltis_constant (op, step, copies))
5904     return true;
5905
5906   /* And finally a vspltisb.  */
5907   if (step == 1)
5908     copies <<= 1;
5909   else
5910     step >>= 1;
5911
5912   if (vspltis_constant (op, step, copies))
5913     return true;
5914
5915   if (vspltis_shifted (op) != 0)
5916     return true;
5917
5918   return false;
5919 }
5920
5921 /* Generate a VEC_DUPLICATE representing a vspltis[bhw] instruction whose
5922    result is OP.  Abort if it is not possible.  */
5923
5924 rtx
5925 gen_easy_altivec_constant (rtx op)
5926 {
5927   machine_mode mode = GET_MODE (op);
5928   int nunits = GET_MODE_NUNITS (mode);
5929   rtx val = CONST_VECTOR_ELT (op, BYTES_BIG_ENDIAN ? nunits - 1 : 0);
5930   unsigned step = nunits / 4;
5931   unsigned copies = 1;
5932
5933   /* Start with a vspltisw.  */
5934   if (vspltis_constant (op, step, copies))
5935     return gen_rtx_VEC_DUPLICATE (V4SImode, gen_lowpart (SImode, val));
5936
5937   /* Then try with a vspltish.  */
5938   if (step == 1)
5939     copies <<= 1;
5940   else
5941     step >>= 1;
5942
5943   if (vspltis_constant (op, step, copies))
5944     return gen_rtx_VEC_DUPLICATE (V8HImode, gen_lowpart (HImode, val));
5945
5946   /* And finally a vspltisb.  */
5947   if (step == 1)
5948     copies <<= 1;
5949   else
5950     step >>= 1;
5951
5952   if (vspltis_constant (op, step, copies))
5953     return gen_rtx_VEC_DUPLICATE (V16QImode, gen_lowpart (QImode, val));
5954
5955   gcc_unreachable ();
5956 }
5957
5958 /* Return true if OP is of the given MODE and can be synthesized with ISA 3.0
5959    instructions (xxspltib, vupkhsb/vextsb2w/vextb2d).
5960
5961    Return the number of instructions needed (1 or 2) into the address pointed
5962    via NUM_INSNS_PTR.
5963
5964    Return the constant that is being split via CONSTANT_PTR.  */
5965
5966 bool
5967 xxspltib_constant_p (rtx op,
5968                      machine_mode mode,
5969                      int *num_insns_ptr,
5970                      int *constant_ptr)
5971 {
5972   size_t nunits = GET_MODE_NUNITS (mode);
5973   size_t i;
5974   HOST_WIDE_INT value;
5975   rtx element;
5976
5977   /* Set the returned values to out of bound values.  */
5978   *num_insns_ptr = -1;
5979   *constant_ptr = 256;
5980
5981   if (!TARGET_P9_VECTOR)
5982     return false;
5983
5984   if (mode == VOIDmode)
5985     mode = GET_MODE (op);
5986
5987   else if (mode != GET_MODE (op) && GET_MODE (op) != VOIDmode)
5988     return false;
5989
5990   /* Handle (vec_duplicate <constant>).  */
5991   if (GET_CODE (op) == VEC_DUPLICATE)
5992     {
5993       if (mode != V16QImode && mode != V8HImode && mode != V4SImode
5994           && mode != V2DImode)
5995         return false;
5996
5997       element = XEXP (op, 0);
5998       if (!CONST_INT_P (element))
5999         return false;
6000
6001       value = INTVAL (element);
6002       if (!IN_RANGE (value, -128, 127))
6003         return false;
6004     }
6005
6006   /* Handle (const_vector [...]).  */
6007   else if (GET_CODE (op) == CONST_VECTOR)
6008     {
6009       if (mode != V16QImode && mode != V8HImode && mode != V4SImode
6010           && mode != V2DImode)
6011         return false;
6012
6013       element = CONST_VECTOR_ELT (op, 0);
6014       if (!CONST_INT_P (element))
6015         return false;
6016
6017       value = INTVAL (element);
6018       if (!IN_RANGE (value, -128, 127))
6019         return false;
6020
6021       for (i = 1; i < nunits; i++)
6022         {
6023           element = CONST_VECTOR_ELT (op, i);
6024           if (!CONST_INT_P (element))
6025             return false;
6026
6027           if (value != INTVAL (element))
6028             return false;
6029         }
6030     }
6031
6032   /* Handle integer constants being loaded into the upper part of the VSX
6033      register as a scalar.  If the value isn't 0/-1, only allow it if the mode
6034      can go in Altivec registers.  Prefer VSPLTISW/VUPKHSW over XXSPLITIB.  */
6035   else if (CONST_INT_P (op))
6036     {
6037       if (!SCALAR_INT_MODE_P (mode))
6038         return false;
6039
6040       value = INTVAL (op);
6041       if (!IN_RANGE (value, -128, 127))
6042         return false;
6043
6044       if (!IN_RANGE (value, -1, 0))
6045         {
6046           if (!(reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID))
6047             return false;
6048
6049           if (EASY_VECTOR_15 (value))
6050             return false;
6051         }
6052     }
6053
6054   else
6055     return false;
6056
6057   /* See if we could generate vspltisw/vspltish directly instead of xxspltib +
6058      sign extend.  Special case 0/-1 to allow getting any VSX register instead
6059      of an Altivec register.  */
6060   if ((mode == V4SImode || mode == V8HImode) && !IN_RANGE (value, -1, 0)
6061       && EASY_VECTOR_15 (value))
6062     return false;
6063
6064   /* Return # of instructions and the constant byte for XXSPLTIB.  */
6065   if (mode == V16QImode)
6066     *num_insns_ptr = 1;
6067
6068   else if (IN_RANGE (value, -1, 0))
6069     *num_insns_ptr = 1;
6070
6071   else
6072     *num_insns_ptr = 2;
6073
6074   *constant_ptr = (int) value;
6075   return true;
6076 }
6077
6078 const char *
6079 output_vec_const_move (rtx *operands)
6080 {
6081   int shift;
6082   machine_mode mode;
6083   rtx dest, vec;
6084
6085   dest = operands[0];
6086   vec = operands[1];
6087   mode = GET_MODE (dest);
6088
6089   if (TARGET_VSX)
6090     {
6091       bool dest_vmx_p = ALTIVEC_REGNO_P (REGNO (dest));
6092       int xxspltib_value = 256;
6093       int num_insns = -1;
6094
6095       if (zero_constant (vec, mode))
6096         {
6097           if (TARGET_P9_VECTOR)
6098             return "xxspltib %x0,0";
6099
6100           else if (dest_vmx_p)
6101             return "vspltisw %0,0";
6102
6103           else
6104             return "xxlxor %x0,%x0,%x0";
6105         }
6106
6107       if (all_ones_constant (vec, mode))
6108         {
6109           if (TARGET_P9_VECTOR)
6110             return "xxspltib %x0,255";
6111
6112           else if (dest_vmx_p)
6113             return "vspltisw %0,-1";
6114
6115           else if (TARGET_P8_VECTOR)
6116             return "xxlorc %x0,%x0,%x0";
6117
6118           else
6119             gcc_unreachable ();
6120         }
6121
6122       if (TARGET_P9_VECTOR
6123           && xxspltib_constant_p (vec, mode, &num_insns, &xxspltib_value))
6124         {
6125           if (num_insns == 1)
6126             {
6127               operands[2] = GEN_INT (xxspltib_value & 0xff);
6128               return "xxspltib %x0,%2";
6129             }
6130
6131           return "#";
6132         }
6133     }
6134
6135   if (TARGET_ALTIVEC)
6136     {
6137       rtx splat_vec;
6138
6139       gcc_assert (ALTIVEC_REGNO_P (REGNO (dest)));
6140       if (zero_constant (vec, mode))
6141         return "vspltisw %0,0";
6142
6143       if (all_ones_constant (vec, mode))
6144         return "vspltisw %0,-1";
6145
6146       /* Do we need to construct a value using VSLDOI?  */
6147       shift = vspltis_shifted (vec);
6148       if (shift != 0)
6149         return "#";
6150
6151       splat_vec = gen_easy_altivec_constant (vec);
6152       gcc_assert (GET_CODE (splat_vec) == VEC_DUPLICATE);
6153       operands[1] = XEXP (splat_vec, 0);
6154       if (!EASY_VECTOR_15 (INTVAL (operands[1])))
6155         return "#";
6156
6157       switch (GET_MODE (splat_vec))
6158         {
6159         case E_V4SImode:
6160           return "vspltisw %0,%1";
6161
6162         case E_V8HImode:
6163           return "vspltish %0,%1";
6164
6165         case E_V16QImode:
6166           return "vspltisb %0,%1";
6167
6168         default:
6169           gcc_unreachable ();
6170         }
6171     }
6172
6173   gcc_unreachable ();
6174 }
6175
6176 /* Initialize vector TARGET to VALS.  */
6177
6178 void
6179 rs6000_expand_vector_init (rtx target, rtx vals)
6180 {
6181   machine_mode mode = GET_MODE (target);
6182   machine_mode inner_mode = GET_MODE_INNER (mode);
6183   int n_elts = GET_MODE_NUNITS (mode);
6184   int n_var = 0, one_var = -1;
6185   bool all_same = true, all_const_zero = true;
6186   rtx x, mem;
6187   int i;
6188
6189   for (i = 0; i < n_elts; ++i)
6190     {
6191       x = XVECEXP (vals, 0, i);
6192       if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
6193         ++n_var, one_var = i;
6194       else if (x != CONST0_RTX (inner_mode))
6195         all_const_zero = false;
6196
6197       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
6198         all_same = false;
6199     }
6200
6201   if (n_var == 0)
6202     {
6203       rtx const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0));
6204       bool int_vector_p = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
6205       if ((int_vector_p || TARGET_VSX) && all_const_zero)
6206         {
6207           /* Zero register.  */
6208           emit_move_insn (target, CONST0_RTX (mode));
6209           return;
6210         }
6211       else if (int_vector_p && easy_vector_constant (const_vec, mode))
6212         {
6213           /* Splat immediate.  */
6214           emit_insn (gen_rtx_SET (target, const_vec));
6215           return;
6216         }
6217       else
6218         {
6219           /* Load from constant pool.  */
6220           emit_move_insn (target, const_vec);
6221           return;
6222         }
6223     }
6224
6225   /* Double word values on VSX can use xxpermdi or lxvdsx.  */
6226   if (VECTOR_MEM_VSX_P (mode) && (mode == V2DFmode || mode == V2DImode))
6227     {
6228       rtx op[2];
6229       size_t i;
6230       size_t num_elements = all_same ? 1 : 2;
6231       for (i = 0; i < num_elements; i++)
6232         {
6233           op[i] = XVECEXP (vals, 0, i);
6234           /* Just in case there is a SUBREG with a smaller mode, do a
6235              conversion.  */
6236           if (GET_MODE (op[i]) != inner_mode)
6237             {
6238               rtx tmp = gen_reg_rtx (inner_mode);
6239               convert_move (tmp, op[i], 0);
6240               op[i] = tmp;
6241             }
6242           /* Allow load with splat double word.  */
6243           else if (MEM_P (op[i]))
6244             {
6245               if (!all_same)
6246                 op[i] = force_reg (inner_mode, op[i]);
6247             }
6248           else if (!REG_P (op[i]))
6249             op[i] = force_reg (inner_mode, op[i]);
6250         }
6251
6252       if (all_same)
6253         {
6254           if (mode == V2DFmode)
6255             emit_insn (gen_vsx_splat_v2df (target, op[0]));
6256           else
6257             emit_insn (gen_vsx_splat_v2di (target, op[0]));
6258         }
6259       else
6260         {
6261           if (mode == V2DFmode)
6262             emit_insn (gen_vsx_concat_v2df (target, op[0], op[1]));
6263           else
6264             emit_insn (gen_vsx_concat_v2di (target, op[0], op[1]));
6265         }
6266       return;
6267     }
6268
6269   /* Special case initializing vector int if we are on 64-bit systems with
6270      direct move or we have the ISA 3.0 instructions.  */
6271   if (mode == V4SImode  && VECTOR_MEM_VSX_P (V4SImode)
6272       && TARGET_DIRECT_MOVE_64BIT)
6273     {
6274       if (all_same)
6275         {
6276           rtx element0 = XVECEXP (vals, 0, 0);
6277           if (MEM_P (element0))
6278             element0 = rs6000_force_indexed_or_indirect_mem (element0);
6279           else
6280             element0 = force_reg (SImode, element0);
6281
6282           if (TARGET_P9_VECTOR)
6283             emit_insn (gen_vsx_splat_v4si (target, element0));
6284           else
6285             {
6286               rtx tmp = gen_reg_rtx (DImode);
6287               emit_insn (gen_zero_extendsidi2 (tmp, element0));
6288               emit_insn (gen_vsx_splat_v4si_di (target, tmp));
6289             }
6290           return;
6291         }
6292       else
6293         {
6294           rtx elements[4];
6295           size_t i;
6296
6297           for (i = 0; i < 4; i++)
6298             elements[i] = force_reg (SImode, XVECEXP (vals, 0, i));
6299
6300           emit_insn (gen_vsx_init_v4si (target, elements[0], elements[1],
6301                                         elements[2], elements[3]));
6302           return;
6303         }
6304     }
6305
6306   /* With single precision floating point on VSX, know that internally single
6307      precision is actually represented as a double, and either make 2 V2DF
6308      vectors, and convert these vectors to single precision, or do one
6309      conversion, and splat the result to the other elements.  */
6310   if (mode == V4SFmode && VECTOR_MEM_VSX_P (V4SFmode))
6311     {
6312       if (all_same)
6313         {
6314           rtx element0 = XVECEXP (vals, 0, 0);
6315
6316           if (TARGET_P9_VECTOR)
6317             {
6318               if (MEM_P (element0))
6319                 element0 = rs6000_force_indexed_or_indirect_mem (element0);
6320
6321               emit_insn (gen_vsx_splat_v4sf (target, element0));
6322             }
6323
6324           else
6325             {
6326               rtx freg = gen_reg_rtx (V4SFmode);
6327               rtx sreg = force_reg (SFmode, element0);
6328               rtx cvt  = (TARGET_XSCVDPSPN
6329                           ? gen_vsx_xscvdpspn_scalar (freg, sreg)
6330                           : gen_vsx_xscvdpsp_scalar (freg, sreg));
6331
6332               emit_insn (cvt);
6333               emit_insn (gen_vsx_xxspltw_v4sf_direct (target, freg,
6334                                                       const0_rtx));
6335             }
6336         }
6337       else
6338         {
6339           rtx dbl_even = gen_reg_rtx (V2DFmode);
6340           rtx dbl_odd  = gen_reg_rtx (V2DFmode);
6341           rtx flt_even = gen_reg_rtx (V4SFmode);
6342           rtx flt_odd  = gen_reg_rtx (V4SFmode);
6343           rtx op0 = force_reg (SFmode, XVECEXP (vals, 0, 0));
6344           rtx op1 = force_reg (SFmode, XVECEXP (vals, 0, 1));
6345           rtx op2 = force_reg (SFmode, XVECEXP (vals, 0, 2));
6346           rtx op3 = force_reg (SFmode, XVECEXP (vals, 0, 3));
6347
6348           /* Use VMRGEW if we can instead of doing a permute.  */
6349           if (TARGET_P8_VECTOR)
6350             {
6351               emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op2));
6352               emit_insn (gen_vsx_concat_v2sf (dbl_odd, op1, op3));
6353               emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
6354               emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
6355               if (BYTES_BIG_ENDIAN)
6356                 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_even, flt_odd));
6357               else
6358                 emit_insn (gen_p8_vmrgew_v4sf_direct (target, flt_odd, flt_even));
6359             }
6360           else
6361             {
6362               emit_insn (gen_vsx_concat_v2sf (dbl_even, op0, op1));
6363               emit_insn (gen_vsx_concat_v2sf (dbl_odd, op2, op3));
6364               emit_insn (gen_vsx_xvcvdpsp (flt_even, dbl_even));
6365               emit_insn (gen_vsx_xvcvdpsp (flt_odd, dbl_odd));
6366               rs6000_expand_extract_even (target, flt_even, flt_odd);
6367             }
6368         }
6369       return;
6370     }
6371
6372   /* Special case initializing vector short/char that are splats if we are on
6373      64-bit systems with direct move.  */
6374   if (all_same && TARGET_DIRECT_MOVE_64BIT
6375       && (mode == V16QImode || mode == V8HImode))
6376     {
6377       rtx op0 = XVECEXP (vals, 0, 0);
6378       rtx di_tmp = gen_reg_rtx (DImode);
6379
6380       if (!REG_P (op0))
6381         op0 = force_reg (GET_MODE_INNER (mode), op0);
6382
6383       if (mode == V16QImode)
6384         {
6385           emit_insn (gen_zero_extendqidi2 (di_tmp, op0));
6386           emit_insn (gen_vsx_vspltb_di (target, di_tmp));
6387           return;
6388         }
6389
6390       if (mode == V8HImode)
6391         {
6392           emit_insn (gen_zero_extendhidi2 (di_tmp, op0));
6393           emit_insn (gen_vsx_vsplth_di (target, di_tmp));
6394           return;
6395         }
6396     }
6397
6398   /* Store value to stack temp.  Load vector element.  Splat.  However, splat
6399      of 64-bit items is not supported on Altivec.  */
6400   if (all_same && GET_MODE_SIZE (inner_mode) <= 4)
6401     {
6402       mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
6403       emit_move_insn (adjust_address_nv (mem, inner_mode, 0),
6404                       XVECEXP (vals, 0, 0));
6405       x = gen_rtx_UNSPEC (VOIDmode,
6406                           gen_rtvec (1, const0_rtx), UNSPEC_LVE);
6407       emit_insn (gen_rtx_PARALLEL (VOIDmode,
6408                                    gen_rtvec (2,
6409                                               gen_rtx_SET (target, mem),
6410                                               x)));
6411       x = gen_rtx_VEC_SELECT (inner_mode, target,
6412                               gen_rtx_PARALLEL (VOIDmode,
6413                                                 gen_rtvec (1, const0_rtx)));
6414       emit_insn (gen_rtx_SET (target, gen_rtx_VEC_DUPLICATE (mode, x)));
6415       return;
6416     }
6417
6418   /* One field is non-constant.  Load constant then overwrite
6419      varying field.  */
6420   if (n_var == 1)
6421     {
6422       rtx copy = copy_rtx (vals);
6423
6424       /* Load constant part of vector, substitute neighboring value for
6425          varying element.  */
6426       XVECEXP (copy, 0, one_var) = XVECEXP (vals, 0, (one_var + 1) % n_elts);
6427       rs6000_expand_vector_init (target, copy);
6428
6429       /* Insert variable.  */
6430       rs6000_expand_vector_set (target, XVECEXP (vals, 0, one_var), one_var);
6431       return;
6432     }
6433
6434   /* Construct the vector in memory one field at a time
6435      and load the whole vector.  */
6436   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
6437   for (i = 0; i < n_elts; i++)
6438     emit_move_insn (adjust_address_nv (mem, inner_mode,
6439                                     i * GET_MODE_SIZE (inner_mode)),
6440                     XVECEXP (vals, 0, i));
6441   emit_move_insn (target, mem);
6442 }
6443
6444 /* Set field ELT of TARGET to VAL.  */
6445
6446 void
6447 rs6000_expand_vector_set (rtx target, rtx val, int elt)
6448 {
6449   machine_mode mode = GET_MODE (target);
6450   machine_mode inner_mode = GET_MODE_INNER (mode);
6451   rtx reg = gen_reg_rtx (mode);
6452   rtx mask, mem, x;
6453   int width = GET_MODE_SIZE (inner_mode);
6454   int i;
6455
6456   val = force_reg (GET_MODE (val), val);
6457
6458   if (VECTOR_MEM_VSX_P (mode))
6459     {
6460       rtx insn = NULL_RTX;
6461       rtx elt_rtx = GEN_INT (elt);
6462
6463       if (mode == V2DFmode)
6464         insn = gen_vsx_set_v2df (target, target, val, elt_rtx);
6465
6466       else if (mode == V2DImode)
6467         insn = gen_vsx_set_v2di (target, target, val, elt_rtx);
6468
6469       else if (TARGET_P9_VECTOR && TARGET_POWERPC64)
6470         {
6471           if (mode == V4SImode)
6472             insn = gen_vsx_set_v4si_p9 (target, target, val, elt_rtx);
6473           else if (mode == V8HImode)
6474             insn = gen_vsx_set_v8hi_p9 (target, target, val, elt_rtx);
6475           else if (mode == V16QImode)
6476             insn = gen_vsx_set_v16qi_p9 (target, target, val, elt_rtx);
6477           else if (mode == V4SFmode)
6478             insn = gen_vsx_set_v4sf_p9 (target, target, val, elt_rtx);
6479         }
6480
6481       if (insn)
6482         {
6483           emit_insn (insn);
6484           return;
6485         }
6486     }
6487
6488   /* Simplify setting single element vectors like V1TImode.  */
6489   if (GET_MODE_SIZE (mode) == GET_MODE_SIZE (inner_mode) && elt == 0)
6490     {
6491       emit_move_insn (target, gen_lowpart (mode, val));
6492       return;
6493     }
6494
6495   /* Load single variable value.  */
6496   mem = assign_stack_temp (mode, GET_MODE_SIZE (inner_mode));
6497   emit_move_insn (adjust_address_nv (mem, inner_mode, 0), val);
6498   x = gen_rtx_UNSPEC (VOIDmode,
6499                       gen_rtvec (1, const0_rtx), UNSPEC_LVE);
6500   emit_insn (gen_rtx_PARALLEL (VOIDmode,
6501                                gen_rtvec (2,
6502                                           gen_rtx_SET (reg, mem),
6503                                           x)));
6504
6505   /* Linear sequence.  */
6506   mask = gen_rtx_PARALLEL (V16QImode, rtvec_alloc (16));
6507   for (i = 0; i < 16; ++i)
6508     XVECEXP (mask, 0, i) = GEN_INT (i);
6509
6510   /* Set permute mask to insert element into target.  */
6511   for (i = 0; i < width; ++i)
6512     XVECEXP (mask, 0, elt*width + i)
6513       = GEN_INT (i + 0x10);
6514   x = gen_rtx_CONST_VECTOR (V16QImode, XVEC (mask, 0));
6515
6516   if (BYTES_BIG_ENDIAN)
6517     x = gen_rtx_UNSPEC (mode,
6518                         gen_rtvec (3, target, reg,
6519                                    force_reg (V16QImode, x)),
6520                         UNSPEC_VPERM);
6521   else
6522     {
6523       if (TARGET_P9_VECTOR)
6524         x = gen_rtx_UNSPEC (mode,
6525                             gen_rtvec (3, reg, target,
6526                                        force_reg (V16QImode, x)),
6527                             UNSPEC_VPERMR);
6528       else
6529         {
6530           /* Invert selector.  We prefer to generate VNAND on P8 so
6531              that future fusion opportunities can kick in, but must
6532              generate VNOR elsewhere.  */
6533           rtx notx = gen_rtx_NOT (V16QImode, force_reg (V16QImode, x));
6534           rtx iorx = (TARGET_P8_VECTOR
6535                       ? gen_rtx_IOR (V16QImode, notx, notx)
6536                       : gen_rtx_AND (V16QImode, notx, notx));
6537           rtx tmp = gen_reg_rtx (V16QImode);
6538           emit_insn (gen_rtx_SET (tmp, iorx));
6539
6540           /* Permute with operands reversed and adjusted selector.  */
6541           x = gen_rtx_UNSPEC (mode, gen_rtvec (3, reg, target, tmp),
6542                               UNSPEC_VPERM);
6543         }
6544     }
6545
6546   emit_insn (gen_rtx_SET (target, x));
6547 }
6548
6549 /* Extract field ELT from VEC into TARGET.  */
6550
6551 void
6552 rs6000_expand_vector_extract (rtx target, rtx vec, rtx elt)
6553 {
6554   machine_mode mode = GET_MODE (vec);
6555   machine_mode inner_mode = GET_MODE_INNER (mode);
6556   rtx mem;
6557
6558   if (VECTOR_MEM_VSX_P (mode) && CONST_INT_P (elt))
6559     {
6560       switch (mode)
6561         {
6562         default:
6563           break;
6564         case E_V1TImode:
6565           emit_move_insn (target, gen_lowpart (TImode, vec));
6566           break;
6567         case E_V2DFmode:
6568           emit_insn (gen_vsx_extract_v2df (target, vec, elt));
6569           return;
6570         case E_V2DImode:
6571           emit_insn (gen_vsx_extract_v2di (target, vec, elt));
6572           return;
6573         case E_V4SFmode:
6574           emit_insn (gen_vsx_extract_v4sf (target, vec, elt));
6575           return;
6576         case E_V16QImode:
6577           if (TARGET_DIRECT_MOVE_64BIT)
6578             {
6579               emit_insn (gen_vsx_extract_v16qi (target, vec, elt));
6580               return;
6581             }
6582           else
6583             break;
6584         case E_V8HImode:
6585           if (TARGET_DIRECT_MOVE_64BIT)
6586             {
6587               emit_insn (gen_vsx_extract_v8hi (target, vec, elt));
6588               return;
6589             }
6590           else
6591             break;
6592         case E_V4SImode:
6593           if (TARGET_DIRECT_MOVE_64BIT)
6594             {
6595               emit_insn (gen_vsx_extract_v4si (target, vec, elt));
6596               return;
6597             }
6598           break;
6599         }
6600     }
6601   else if (VECTOR_MEM_VSX_P (mode) && !CONST_INT_P (elt)
6602            && TARGET_DIRECT_MOVE_64BIT)
6603     {
6604       if (GET_MODE (elt) != DImode)
6605         {
6606           rtx tmp = gen_reg_rtx (DImode);
6607           convert_move (tmp, elt, 0);
6608           elt = tmp;
6609         }
6610       else if (!REG_P (elt))
6611         elt = force_reg (DImode, elt);
6612
6613       switch (mode)
6614         {
6615         case E_V1TImode:
6616           emit_move_insn (target, gen_lowpart (TImode, vec));
6617           return;
6618
6619         case E_V2DFmode:
6620           emit_insn (gen_vsx_extract_v2df_var (target, vec, elt));
6621           return;
6622
6623         case E_V2DImode:
6624           emit_insn (gen_vsx_extract_v2di_var (target, vec, elt));
6625           return;
6626
6627         case E_V4SFmode:
6628           emit_insn (gen_vsx_extract_v4sf_var (target, vec, elt));
6629           return;
6630
6631         case E_V4SImode:
6632           emit_insn (gen_vsx_extract_v4si_var (target, vec, elt));
6633           return;
6634
6635         case E_V8HImode:
6636           emit_insn (gen_vsx_extract_v8hi_var (target, vec, elt));
6637           return;
6638
6639         case E_V16QImode:
6640           emit_insn (gen_vsx_extract_v16qi_var (target, vec, elt));
6641           return;
6642
6643         default:
6644           gcc_unreachable ();
6645         }
6646     }
6647
6648   /* Allocate mode-sized buffer.  */
6649   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
6650
6651   emit_move_insn (mem, vec);
6652   if (CONST_INT_P (elt))
6653     {
6654       int modulo_elt = INTVAL (elt) % GET_MODE_NUNITS (mode);
6655
6656       /* Add offset to field within buffer matching vector element.  */
6657       mem = adjust_address_nv (mem, inner_mode,
6658                                modulo_elt * GET_MODE_SIZE (inner_mode));
6659       emit_move_insn (target, adjust_address_nv (mem, inner_mode, 0));
6660     }
6661   else
6662     {
6663       unsigned int ele_size = GET_MODE_SIZE (inner_mode);
6664       rtx num_ele_m1 = GEN_INT (GET_MODE_NUNITS (mode) - 1);
6665       rtx new_addr = gen_reg_rtx (Pmode);
6666
6667       elt = gen_rtx_AND (Pmode, elt, num_ele_m1);
6668       if (ele_size > 1)
6669         elt = gen_rtx_MULT (Pmode, elt, GEN_INT (ele_size));
6670       new_addr = gen_rtx_PLUS (Pmode, XEXP (mem, 0), elt);
6671       new_addr = change_address (mem, inner_mode, new_addr);
6672       emit_move_insn (target, new_addr);
6673     }
6674 }
6675
6676 /* Adjust a memory address (MEM) of a vector type to point to a scalar field
6677    within the vector (ELEMENT) with a mode (SCALAR_MODE).  Use a base register
6678    temporary (BASE_TMP) to fixup the address.  Return the new memory address
6679    that is valid for reads or writes to a given register (SCALAR_REG).  */
6680
6681 rtx
6682 rs6000_adjust_vec_address (rtx scalar_reg,
6683                            rtx mem,
6684                            rtx element,
6685                            rtx base_tmp,
6686                            machine_mode scalar_mode)
6687 {
6688   unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
6689   rtx addr = XEXP (mem, 0);
6690   rtx element_offset;
6691   rtx new_addr;
6692   bool valid_addr_p;
6693
6694   /* Vector addresses should not have PRE_INC, PRE_DEC, or PRE_MODIFY.  */
6695   gcc_assert (GET_RTX_CLASS (GET_CODE (addr)) != RTX_AUTOINC);
6696
6697   /* Calculate what we need to add to the address to get the element
6698      address.  */
6699   if (CONST_INT_P (element))
6700     element_offset = GEN_INT (INTVAL (element) * scalar_size);
6701   else
6702     {
6703       int byte_shift = exact_log2 (scalar_size);
6704       gcc_assert (byte_shift >= 0);
6705
6706       if (byte_shift == 0)
6707         element_offset = element;
6708
6709       else
6710         {
6711           if (TARGET_POWERPC64)
6712             emit_insn (gen_ashldi3 (base_tmp, element, GEN_INT (byte_shift)));
6713           else
6714             emit_insn (gen_ashlsi3 (base_tmp, element, GEN_INT (byte_shift)));
6715
6716           element_offset = base_tmp;
6717         }
6718     }
6719
6720   /* Create the new address pointing to the element within the vector.  If we
6721      are adding 0, we don't have to change the address.  */
6722   if (element_offset == const0_rtx)
6723     new_addr = addr;
6724
6725   /* A simple indirect address can be converted into a reg + offset
6726      address.  */
6727   else if (REG_P (addr) || SUBREG_P (addr))
6728     new_addr = gen_rtx_PLUS (Pmode, addr, element_offset);
6729
6730   /* Optimize D-FORM addresses with constant offset with a constant element, to
6731      include the element offset in the address directly.  */
6732   else if (GET_CODE (addr) == PLUS)
6733     {
6734       rtx op0 = XEXP (addr, 0);
6735       rtx op1 = XEXP (addr, 1);
6736       rtx insn;
6737
6738       gcc_assert (REG_P (op0) || SUBREG_P (op0));
6739       if (CONST_INT_P (op1) && CONST_INT_P (element_offset))
6740         {
6741           HOST_WIDE_INT offset = INTVAL (op1) + INTVAL (element_offset);
6742           rtx offset_rtx = GEN_INT (offset);
6743
6744           if (IN_RANGE (offset, -32768, 32767)
6745               && (scalar_size < 8 || (offset & 0x3) == 0))
6746             new_addr = gen_rtx_PLUS (Pmode, op0, offset_rtx);
6747           else
6748             {
6749               emit_move_insn (base_tmp, offset_rtx);
6750               new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
6751             }
6752         }
6753       else
6754         {
6755           bool op1_reg_p = (REG_P (op1) || SUBREG_P (op1));
6756           bool ele_reg_p = (REG_P (element_offset) || SUBREG_P (element_offset));
6757
6758           /* Note, ADDI requires the register being added to be a base
6759              register.  If the register was R0, load it up into the temporary
6760              and do the add.  */
6761           if (op1_reg_p
6762               && (ele_reg_p || reg_or_subregno (op1) != FIRST_GPR_REGNO))
6763             {
6764               insn = gen_add3_insn (base_tmp, op1, element_offset);
6765               gcc_assert (insn != NULL_RTX);
6766               emit_insn (insn);
6767             }
6768
6769           else if (ele_reg_p
6770                    && reg_or_subregno (element_offset) != FIRST_GPR_REGNO)
6771             {
6772               insn = gen_add3_insn (base_tmp, element_offset, op1);
6773               gcc_assert (insn != NULL_RTX);
6774               emit_insn (insn);
6775             }
6776
6777           else
6778             {
6779               emit_move_insn (base_tmp, op1);
6780               emit_insn (gen_add2_insn (base_tmp, element_offset));
6781             }
6782
6783           new_addr = gen_rtx_PLUS (Pmode, op0, base_tmp);
6784         }
6785     }
6786
6787   else
6788     {
6789       emit_move_insn (base_tmp, addr);
6790       new_addr = gen_rtx_PLUS (Pmode, base_tmp, element_offset);
6791     }
6792
6793   /* If we have a PLUS, we need to see whether the particular register class
6794      allows for D-FORM or X-FORM addressing.  */
6795   if (GET_CODE (new_addr) == PLUS)
6796     {
6797       rtx op1 = XEXP (new_addr, 1);
6798       addr_mask_type addr_mask;
6799       unsigned int scalar_regno = reg_or_subregno (scalar_reg);
6800
6801       gcc_assert (HARD_REGISTER_NUM_P (scalar_regno));
6802       if (INT_REGNO_P (scalar_regno))
6803         addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_GPR];
6804
6805       else if (FP_REGNO_P (scalar_regno))
6806         addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_FPR];
6807
6808       else if (ALTIVEC_REGNO_P (scalar_regno))
6809         addr_mask = reg_addr[scalar_mode].addr_mask[RELOAD_REG_VMX];
6810
6811       else
6812         gcc_unreachable ();
6813
6814       if (REG_P (op1) || SUBREG_P (op1))
6815         valid_addr_p = (addr_mask & RELOAD_REG_INDEXED) != 0;
6816       else
6817         valid_addr_p = (addr_mask & RELOAD_REG_OFFSET) != 0;
6818     }
6819
6820   else if (REG_P (new_addr) || SUBREG_P (new_addr))
6821     valid_addr_p = true;
6822
6823   else
6824     valid_addr_p = false;
6825
6826   if (!valid_addr_p)
6827     {
6828       emit_move_insn (base_tmp, new_addr);
6829       new_addr = base_tmp;
6830     }
6831
6832   return change_address (mem, scalar_mode, new_addr);
6833 }
6834
6835 /* Split a variable vec_extract operation into the component instructions.  */
6836
6837 void
6838 rs6000_split_vec_extract_var (rtx dest, rtx src, rtx element, rtx tmp_gpr,
6839                               rtx tmp_altivec)
6840 {
6841   machine_mode mode = GET_MODE (src);
6842   machine_mode scalar_mode = GET_MODE_INNER (GET_MODE (src));
6843   unsigned scalar_size = GET_MODE_SIZE (scalar_mode);
6844   int byte_shift = exact_log2 (scalar_size);
6845
6846   gcc_assert (byte_shift >= 0);
6847
6848   /* If we are given a memory address, optimize to load just the element.  We
6849      don't have to adjust the vector element number on little endian
6850      systems.  */
6851   if (MEM_P (src))
6852     {
6853       int num_elements = GET_MODE_NUNITS (mode);
6854       rtx num_ele_m1 = GEN_INT (num_elements - 1);
6855
6856       emit_insn (gen_anddi3 (element, element, num_ele_m1));
6857       gcc_assert (REG_P (tmp_gpr));
6858       emit_move_insn (dest, rs6000_adjust_vec_address (dest, src, element,
6859                                                        tmp_gpr, scalar_mode));
6860       return;
6861     }
6862
6863   else if (REG_P (src) || SUBREG_P (src))
6864     {
6865       int num_elements = GET_MODE_NUNITS (mode);
6866       int bits_in_element = mode_to_bits (GET_MODE_INNER (mode));
6867       int bit_shift = 7 - exact_log2 (num_elements);
6868       rtx element2;
6869       unsigned int dest_regno = reg_or_subregno (dest);
6870       unsigned int src_regno = reg_or_subregno (src);
6871       unsigned int element_regno = reg_or_subregno (element);
6872
6873       gcc_assert (REG_P (tmp_gpr));
6874
6875       /* See if we want to generate VEXTU{B,H,W}{L,R}X if the destination is in
6876          a general purpose register.  */
6877       if (TARGET_P9_VECTOR
6878           && (mode == V16QImode || mode == V8HImode || mode == V4SImode)
6879           && INT_REGNO_P (dest_regno)
6880           && ALTIVEC_REGNO_P (src_regno)
6881           && INT_REGNO_P (element_regno))
6882         {
6883           rtx dest_si = gen_rtx_REG (SImode, dest_regno);
6884           rtx element_si = gen_rtx_REG (SImode, element_regno);
6885
6886           if (mode == V16QImode)
6887             emit_insn (BYTES_BIG_ENDIAN
6888                        ? gen_vextublx (dest_si, element_si, src)
6889                        : gen_vextubrx (dest_si, element_si, src));
6890
6891           else if (mode == V8HImode)
6892             {
6893               rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
6894               emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const1_rtx));
6895               emit_insn (BYTES_BIG_ENDIAN
6896                          ? gen_vextuhlx (dest_si, tmp_gpr_si, src)
6897                          : gen_vextuhrx (dest_si, tmp_gpr_si, src));
6898             }
6899
6900
6901           else
6902             {
6903               rtx tmp_gpr_si = gen_rtx_REG (SImode, REGNO (tmp_gpr));
6904               emit_insn (gen_ashlsi3 (tmp_gpr_si, element_si, const2_rtx));
6905               emit_insn (BYTES_BIG_ENDIAN
6906                          ? gen_vextuwlx (dest_si, tmp_gpr_si, src)
6907                          : gen_vextuwrx (dest_si, tmp_gpr_si, src));
6908             }
6909
6910           return;
6911         }
6912
6913
6914       gcc_assert (REG_P (tmp_altivec));
6915
6916       /* For little endian, adjust element ordering.  For V2DI/V2DF, we can use
6917          an XOR, otherwise we need to subtract.  The shift amount is so VSLO
6918          will shift the element into the upper position (adding 3 to convert a
6919          byte shift into a bit shift).  */
6920       if (scalar_size == 8)
6921         {
6922           if (!BYTES_BIG_ENDIAN)
6923             {
6924               emit_insn (gen_xordi3 (tmp_gpr, element, const1_rtx));
6925               element2 = tmp_gpr;
6926             }
6927           else
6928             element2 = element;
6929
6930           /* Generate RLDIC directly to shift left 6 bits and retrieve 1
6931              bit.  */
6932           emit_insn (gen_rtx_SET (tmp_gpr,
6933                                   gen_rtx_AND (DImode,
6934                                                gen_rtx_ASHIFT (DImode,
6935                                                                element2,
6936                                                                GEN_INT (6)),
6937                                                GEN_INT (64))));
6938         }
6939       else
6940         {
6941           if (!BYTES_BIG_ENDIAN)
6942             {
6943               rtx num_ele_m1 = GEN_INT (num_elements - 1);
6944
6945               emit_insn (gen_anddi3 (tmp_gpr, element, num_ele_m1));
6946               emit_insn (gen_subdi3 (tmp_gpr, num_ele_m1, tmp_gpr));
6947               element2 = tmp_gpr;
6948             }
6949           else
6950             element2 = element;
6951
6952           emit_insn (gen_ashldi3 (tmp_gpr, element2, GEN_INT (bit_shift)));
6953         }
6954
6955       /* Get the value into the lower byte of the Altivec register where VSLO
6956          expects it.  */
6957       if (TARGET_P9_VECTOR)
6958         emit_insn (gen_vsx_splat_v2di (tmp_altivec, tmp_gpr));
6959       else if (can_create_pseudo_p ())
6960         emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_gpr, tmp_gpr));
6961       else
6962         {
6963           rtx tmp_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
6964           emit_move_insn (tmp_di, tmp_gpr);
6965           emit_insn (gen_vsx_concat_v2di (tmp_altivec, tmp_di, tmp_di));
6966         }
6967
6968       /* Do the VSLO to get the value into the final location.  */
6969       switch (mode)
6970         {
6971         case E_V2DFmode:
6972           emit_insn (gen_vsx_vslo_v2df (dest, src, tmp_altivec));
6973           return;
6974
6975         case E_V2DImode:
6976           emit_insn (gen_vsx_vslo_v2di (dest, src, tmp_altivec));
6977           return;
6978
6979         case E_V4SFmode:
6980           {
6981             rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
6982             rtx tmp_altivec_v4sf = gen_rtx_REG (V4SFmode, REGNO (tmp_altivec));
6983             rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
6984             emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
6985                                           tmp_altivec));
6986
6987             emit_insn (gen_vsx_xscvspdp_scalar2 (dest, tmp_altivec_v4sf));
6988             return;
6989           }
6990
6991         case E_V4SImode:
6992         case E_V8HImode:
6993         case E_V16QImode:
6994           {
6995             rtx tmp_altivec_di = gen_rtx_REG (DImode, REGNO (tmp_altivec));
6996             rtx src_v2di = gen_rtx_REG (V2DImode, REGNO (src));
6997             rtx tmp_gpr_di = gen_rtx_REG (DImode, REGNO (dest));
6998             emit_insn (gen_vsx_vslo_v2di (tmp_altivec_di, src_v2di,
6999                                           tmp_altivec));
7000             emit_move_insn (tmp_gpr_di, tmp_altivec_di);
7001             emit_insn (gen_lshrdi3 (tmp_gpr_di, tmp_gpr_di,
7002                                     GEN_INT (64 - bits_in_element)));
7003             return;
7004           }
7005
7006         default:
7007           gcc_unreachable ();
7008         }
7009
7010       return;
7011     }
7012   else
7013     gcc_unreachable ();
7014  }
7015
7016 /* Return alignment of TYPE.  Existing alignment is ALIGN.  HOW
7017    selects whether the alignment is abi mandated, optional, or
7018    both abi and optional alignment.  */
7019    
7020 unsigned int
7021 rs6000_data_alignment (tree type, unsigned int align, enum data_align how)
7022 {
7023   if (how != align_opt)
7024     {
7025       if (TREE_CODE (type) == VECTOR_TYPE && align < 128)
7026         align = 128;
7027     }
7028
7029   if (how != align_abi)
7030     {
7031       if (TREE_CODE (type) == ARRAY_TYPE
7032           && TYPE_MODE (TREE_TYPE (type)) == QImode)
7033         {
7034           if (align < BITS_PER_WORD)
7035             align = BITS_PER_WORD;
7036         }
7037     }
7038
7039   return align;
7040 }
7041
7042 /* Implement TARGET_SLOW_UNALIGNED_ACCESS.  Altivec vector memory
7043    instructions simply ignore the low bits; VSX memory instructions
7044    are aligned to 4 or 8 bytes.  */
7045
7046 static bool
7047 rs6000_slow_unaligned_access (machine_mode mode, unsigned int align)
7048 {
7049   return (STRICT_ALIGNMENT
7050           || (!TARGET_EFFICIENT_UNALIGNED_VSX
7051               && ((SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode) && align < 32)
7052                   || ((VECTOR_MODE_P (mode) || FLOAT128_VECTOR_P (mode))
7053                       && (int) align < VECTOR_ALIGN (mode)))));
7054 }
7055
7056 /* Previous GCC releases forced all vector types to have 16-byte alignment.  */
7057
7058 bool
7059 rs6000_special_adjust_field_align_p (tree type, unsigned int computed)
7060 {
7061   if (TARGET_ALTIVEC && TREE_CODE (type) == VECTOR_TYPE)
7062     {
7063       if (computed != 128)
7064         {
7065           static bool warned;
7066           if (!warned && warn_psabi)
7067             {
7068               warned = true;
7069               inform (input_location,
7070                       "the layout of aggregates containing vectors with"
7071                       " %d-byte alignment has changed in GCC 5",
7072                       computed / BITS_PER_UNIT);
7073             }
7074         }
7075       /* In current GCC there is no special case.  */
7076       return false;
7077     }
7078
7079   return false;
7080 }
7081
7082 /* AIX increases natural record alignment to doubleword if the first
7083    field is an FP double while the FP fields remain word aligned.  */
7084
7085 unsigned int
7086 rs6000_special_round_type_align (tree type, unsigned int computed,
7087                                  unsigned int specified)
7088 {
7089   unsigned int align = MAX (computed, specified);
7090   tree field = TYPE_FIELDS (type);
7091
7092   /* Skip all non field decls */
7093   while (field != NULL && TREE_CODE (field) != FIELD_DECL)
7094     field = DECL_CHAIN (field);
7095
7096   if (field != NULL && field != type)
7097     {
7098       type = TREE_TYPE (field);
7099       while (TREE_CODE (type) == ARRAY_TYPE)
7100         type = TREE_TYPE (type);
7101
7102       if (type != error_mark_node && TYPE_MODE (type) == DFmode)
7103         align = MAX (align, 64);
7104     }
7105
7106   return align;
7107 }
7108
7109 /* Darwin increases record alignment to the natural alignment of
7110    the first field.  */
7111
7112 unsigned int
7113 darwin_rs6000_special_round_type_align (tree type, unsigned int computed,
7114                                         unsigned int specified)
7115 {
7116   unsigned int align = MAX (computed, specified);
7117
7118   if (TYPE_PACKED (type))
7119     return align;
7120
7121   /* Find the first field, looking down into aggregates.  */
7122   do {
7123     tree field = TYPE_FIELDS (type);
7124     /* Skip all non field decls */
7125     while (field != NULL && TREE_CODE (field) != FIELD_DECL)
7126       field = DECL_CHAIN (field);
7127     if (! field)
7128       break;
7129     /* A packed field does not contribute any extra alignment.  */
7130     if (DECL_PACKED (field))
7131       return align;
7132     type = TREE_TYPE (field);
7133     while (TREE_CODE (type) == ARRAY_TYPE)
7134       type = TREE_TYPE (type);
7135   } while (AGGREGATE_TYPE_P (type));
7136
7137   if (! AGGREGATE_TYPE_P (type) && type != error_mark_node)
7138     align = MAX (align, TYPE_ALIGN (type));
7139
7140   return align;
7141 }
7142
7143 /* Return 1 for an operand in small memory on V.4/eabi.  */
7144
7145 int
7146 small_data_operand (rtx op ATTRIBUTE_UNUSED,
7147                     machine_mode mode ATTRIBUTE_UNUSED)
7148 {
7149 #if TARGET_ELF
7150   rtx sym_ref;
7151
7152   if (rs6000_sdata == SDATA_NONE || rs6000_sdata == SDATA_DATA)
7153     return 0;
7154
7155   if (DEFAULT_ABI != ABI_V4)
7156     return 0;
7157
7158   if (SYMBOL_REF_P (op))
7159     sym_ref = op;
7160
7161   else if (GET_CODE (op) != CONST
7162            || GET_CODE (XEXP (op, 0)) != PLUS
7163            || !SYMBOL_REF_P (XEXP (XEXP (op, 0), 0))
7164            || !CONST_INT_P (XEXP (XEXP (op, 0), 1)))
7165     return 0;
7166
7167   else
7168     {
7169       rtx sum = XEXP (op, 0);
7170       HOST_WIDE_INT summand;
7171
7172       /* We have to be careful here, because it is the referenced address
7173          that must be 32k from _SDA_BASE_, not just the symbol.  */
7174       summand = INTVAL (XEXP (sum, 1));
7175       if (summand < 0 || summand > g_switch_value)
7176         return 0;
7177
7178       sym_ref = XEXP (sum, 0);
7179     }
7180
7181   return SYMBOL_REF_SMALL_P (sym_ref);
7182 #else
7183   return 0;
7184 #endif
7185 }
7186
7187 /* Return true if either operand is a general purpose register.  */
7188
7189 bool
7190 gpr_or_gpr_p (rtx op0, rtx op1)
7191 {
7192   return ((REG_P (op0) && INT_REGNO_P (REGNO (op0)))
7193           || (REG_P (op1) && INT_REGNO_P (REGNO (op1))));
7194 }
7195
7196 /* Return true if this is a move direct operation between GPR registers and
7197    floating point/VSX registers.  */
7198
7199 bool
7200 direct_move_p (rtx op0, rtx op1)
7201 {
7202   if (!REG_P (op0) || !REG_P (op1))
7203     return false;
7204
7205   if (!TARGET_DIRECT_MOVE)
7206     return false;
7207
7208   int regno0 = REGNO (op0);
7209   int regno1 = REGNO (op1);
7210   if (!HARD_REGISTER_NUM_P (regno0) || !HARD_REGISTER_NUM_P (regno1))
7211     return false;
7212
7213   if (INT_REGNO_P (regno0) && VSX_REGNO_P (regno1))
7214     return true;
7215
7216   if (VSX_REGNO_P (regno0) && INT_REGNO_P (regno1))
7217     return true;
7218
7219   return false;
7220 }
7221
7222 /* Return true if the ADDR is an acceptable address for a quad memory
7223    operation of mode MODE (either LQ/STQ for general purpose registers, or
7224    LXV/STXV for vector registers under ISA 3.0.  GPR_P is true if this address
7225    is intended for LQ/STQ.  If it is false, the address is intended for the ISA
7226    3.0 LXV/STXV instruction.  */
7227
7228 bool
7229 quad_address_p (rtx addr, machine_mode mode, bool strict)
7230 {
7231   rtx op0, op1;
7232
7233   if (GET_MODE_SIZE (mode) != 16)
7234     return false;
7235
7236   if (legitimate_indirect_address_p (addr, strict))
7237     return true;
7238
7239   if (VECTOR_MODE_P (mode) && !mode_supports_dq_form (mode))
7240     return false;
7241
7242   if (GET_CODE (addr) != PLUS)
7243     return false;
7244
7245   op0 = XEXP (addr, 0);
7246   if (!REG_P (op0) || !INT_REG_OK_FOR_BASE_P (op0, strict))
7247     return false;
7248
7249   op1 = XEXP (addr, 1);
7250   if (!CONST_INT_P (op1))
7251     return false;
7252
7253   return quad_address_offset_p (INTVAL (op1));
7254 }
7255
7256 /* Return true if this is a load or store quad operation.  This function does
7257    not handle the atomic quad memory instructions.  */
7258
7259 bool
7260 quad_load_store_p (rtx op0, rtx op1)
7261 {
7262   bool ret;
7263
7264   if (!TARGET_QUAD_MEMORY)
7265     ret = false;
7266
7267   else if (REG_P (op0) && MEM_P (op1))
7268     ret = (quad_int_reg_operand (op0, GET_MODE (op0))
7269            && quad_memory_operand (op1, GET_MODE (op1))
7270            && !reg_overlap_mentioned_p (op0, op1));
7271
7272   else if (MEM_P (op0) && REG_P (op1))
7273     ret = (quad_memory_operand (op0, GET_MODE (op0))
7274            && quad_int_reg_operand (op1, GET_MODE (op1)));
7275
7276   else
7277     ret = false;
7278
7279   if (TARGET_DEBUG_ADDR)
7280     {
7281       fprintf (stderr, "\n========== quad_load_store, return %s\n",
7282                ret ? "true" : "false");
7283       debug_rtx (gen_rtx_SET (op0, op1));
7284     }
7285
7286   return ret;
7287 }
7288
7289 /* Given an address, return a constant offset term if one exists.  */
7290
7291 static rtx
7292 address_offset (rtx op)
7293 {
7294   if (GET_CODE (op) == PRE_INC
7295       || GET_CODE (op) == PRE_DEC)
7296     op = XEXP (op, 0);
7297   else if (GET_CODE (op) == PRE_MODIFY
7298            || GET_CODE (op) == LO_SUM)
7299     op = XEXP (op, 1);
7300
7301   if (GET_CODE (op) == CONST)
7302     op = XEXP (op, 0);
7303
7304   if (GET_CODE (op) == PLUS)
7305     op = XEXP (op, 1);
7306
7307   if (CONST_INT_P (op))
7308     return op;
7309
7310   return NULL_RTX;
7311 }
7312
7313 /* Return true if the MEM operand is a memory operand suitable for use
7314    with a (full width, possibly multiple) gpr load/store.  On
7315    powerpc64 this means the offset must be divisible by 4.
7316    Implements 'Y' constraint.
7317
7318    Accept direct, indexed, offset, lo_sum and tocref.  Since this is
7319    a constraint function we know the operand has satisfied a suitable
7320    memory predicate.
7321
7322    Offsetting a lo_sum should not be allowed, except where we know by
7323    alignment that a 32k boundary is not crossed.  Note that by
7324    "offsetting" here we mean a further offset to access parts of the
7325    MEM.  It's fine to have a lo_sum where the inner address is offset
7326    from a sym, since the same sym+offset will appear in the high part
7327    of the address calculation.  */
7328
7329 bool
7330 mem_operand_gpr (rtx op, machine_mode mode)
7331 {
7332   unsigned HOST_WIDE_INT offset;
7333   int extra;
7334   rtx addr = XEXP (op, 0);
7335
7336   /* PR85755: Allow PRE_INC and PRE_DEC addresses.  */
7337   if (TARGET_UPDATE
7338       && (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
7339       && mode_supports_pre_incdec_p (mode)
7340       && legitimate_indirect_address_p (XEXP (addr, 0), false))
7341     return true;
7342
7343   /* Don't allow non-offsettable addresses.  See PRs 83969 and 84279.  */
7344   if (!rs6000_offsettable_memref_p (op, mode, false))
7345     return false;
7346
7347   op = address_offset (addr);
7348   if (op == NULL_RTX)
7349     return true;
7350
7351   offset = INTVAL (op);
7352   if (TARGET_POWERPC64 && (offset & 3) != 0)
7353     return false;
7354
7355   extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
7356   if (extra < 0)
7357     extra = 0;
7358
7359   if (GET_CODE (addr) == LO_SUM)
7360     /* For lo_sum addresses, we must allow any offset except one that
7361        causes a wrap, so test only the low 16 bits.  */
7362     offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
7363
7364   return offset + 0x8000 < 0x10000u - extra;
7365 }
7366
7367 /* As above, but for DS-FORM VSX insns.  Unlike mem_operand_gpr,
7368    enforce an offset divisible by 4 even for 32-bit.  */
7369
7370 bool
7371 mem_operand_ds_form (rtx op, machine_mode mode)
7372 {
7373   unsigned HOST_WIDE_INT offset;
7374   int extra;
7375   rtx addr = XEXP (op, 0);
7376
7377   if (!offsettable_address_p (false, mode, addr))
7378     return false;
7379
7380   op = address_offset (addr);
7381   if (op == NULL_RTX)
7382     return true;
7383
7384   offset = INTVAL (op);
7385   if ((offset & 3) != 0)
7386     return false;
7387
7388   extra = GET_MODE_SIZE (mode) - UNITS_PER_WORD;
7389   if (extra < 0)
7390     extra = 0;
7391
7392   if (GET_CODE (addr) == LO_SUM)
7393     /* For lo_sum addresses, we must allow any offset except one that
7394        causes a wrap, so test only the low 16 bits.  */
7395     offset = ((offset & 0xffff) ^ 0x8000) - 0x8000;
7396
7397   return offset + 0x8000 < 0x10000u - extra;
7398 }
7399 \f
7400 /* Subroutines of rs6000_legitimize_address and rs6000_legitimate_address_p.  */
7401
7402 static bool
7403 reg_offset_addressing_ok_p (machine_mode mode)
7404 {
7405   switch (mode)
7406     {
7407     case E_V16QImode:
7408     case E_V8HImode:
7409     case E_V4SFmode:
7410     case E_V4SImode:
7411     case E_V2DFmode:
7412     case E_V2DImode:
7413     case E_V1TImode:
7414     case E_TImode:
7415     case E_TFmode:
7416     case E_KFmode:
7417       /* AltiVec/VSX vector modes.  Only reg+reg addressing was valid until the
7418          ISA 3.0 vector d-form addressing mode was added.  While TImode is not
7419          a vector mode, if we want to use the VSX registers to move it around,
7420          we need to restrict ourselves to reg+reg addressing.  Similarly for
7421          IEEE 128-bit floating point that is passed in a single vector
7422          register.  */
7423       if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode))
7424         return mode_supports_dq_form (mode);
7425       break;
7426
7427     case E_SDmode:
7428       /* If we can do direct load/stores of SDmode, restrict it to reg+reg
7429          addressing for the LFIWZX and STFIWX instructions.  */
7430       if (TARGET_NO_SDMODE_STACK)
7431         return false;
7432       break;
7433
7434     default:
7435       break;
7436     }
7437
7438   return true;
7439 }
7440
7441 static bool
7442 virtual_stack_registers_memory_p (rtx op)
7443 {
7444   int regnum;
7445
7446   if (REG_P (op))
7447     regnum = REGNO (op);
7448
7449   else if (GET_CODE (op) == PLUS
7450            && REG_P (XEXP (op, 0))
7451            && CONST_INT_P (XEXP (op, 1)))
7452     regnum = REGNO (XEXP (op, 0));
7453
7454   else
7455     return false;
7456
7457   return (regnum >= FIRST_VIRTUAL_REGISTER
7458           && regnum <= LAST_VIRTUAL_POINTER_REGISTER);
7459 }
7460
7461 /* Return true if a MODE sized memory accesses to OP plus OFFSET
7462    is known to not straddle a 32k boundary.  This function is used
7463    to determine whether -mcmodel=medium code can use TOC pointer
7464    relative addressing for OP.  This means the alignment of the TOC
7465    pointer must also be taken into account, and unfortunately that is
7466    only 8 bytes.  */ 
7467
7468 #ifndef POWERPC64_TOC_POINTER_ALIGNMENT
7469 #define POWERPC64_TOC_POINTER_ALIGNMENT 8
7470 #endif
7471
7472 static bool
7473 offsettable_ok_by_alignment (rtx op, HOST_WIDE_INT offset,
7474                              machine_mode mode)
7475 {
7476   tree decl;
7477   unsigned HOST_WIDE_INT dsize, dalign, lsb, mask;
7478
7479   if (!SYMBOL_REF_P (op))
7480     return false;
7481
7482   /* ISA 3.0 vector d-form addressing is restricted, don't allow
7483      SYMBOL_REF.  */
7484   if (mode_supports_dq_form (mode))
7485     return false;
7486
7487   dsize = GET_MODE_SIZE (mode);
7488   decl = SYMBOL_REF_DECL (op);
7489   if (!decl)
7490     {
7491       if (dsize == 0)
7492         return false;
7493
7494       /* -fsection-anchors loses the original SYMBOL_REF_DECL when
7495          replacing memory addresses with an anchor plus offset.  We
7496          could find the decl by rummaging around in the block->objects
7497          VEC for the given offset but that seems like too much work.  */
7498       dalign = BITS_PER_UNIT;
7499       if (SYMBOL_REF_HAS_BLOCK_INFO_P (op)
7500           && SYMBOL_REF_ANCHOR_P (op)
7501           && SYMBOL_REF_BLOCK (op) != NULL)
7502         {
7503           struct object_block *block = SYMBOL_REF_BLOCK (op);
7504
7505           dalign = block->alignment;
7506           offset += SYMBOL_REF_BLOCK_OFFSET (op);
7507         }
7508       else if (CONSTANT_POOL_ADDRESS_P (op))
7509         {
7510           /* It would be nice to have get_pool_align()..  */
7511           machine_mode cmode = get_pool_mode (op);
7512
7513           dalign = GET_MODE_ALIGNMENT (cmode);
7514         }
7515     }
7516   else if (DECL_P (decl))
7517     {
7518       dalign = DECL_ALIGN (decl);
7519
7520       if (dsize == 0)
7521         {
7522           /* Allow BLKmode when the entire object is known to not
7523              cross a 32k boundary.  */
7524           if (!DECL_SIZE_UNIT (decl))
7525             return false;
7526
7527           if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
7528             return false;
7529
7530           dsize = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7531           if (dsize > 32768)
7532             return false;
7533
7534           dalign /= BITS_PER_UNIT;
7535           if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
7536             dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
7537           return dalign >= dsize;
7538         }
7539     }
7540   else
7541     gcc_unreachable ();
7542
7543   /* Find how many bits of the alignment we know for this access.  */
7544   dalign /= BITS_PER_UNIT;
7545   if (dalign > POWERPC64_TOC_POINTER_ALIGNMENT)
7546     dalign = POWERPC64_TOC_POINTER_ALIGNMENT;
7547   mask = dalign - 1;
7548   lsb = offset & -offset;
7549   mask &= lsb - 1;
7550   dalign = mask + 1;
7551
7552   return dalign >= dsize;
7553 }
7554
7555 static bool
7556 constant_pool_expr_p (rtx op)
7557 {
7558   rtx base, offset;
7559
7560   split_const (op, &base, &offset);
7561   return (SYMBOL_REF_P (base)
7562           && CONSTANT_POOL_ADDRESS_P (base)
7563           && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (base), Pmode));
7564 }
7565
7566 /* Create a TOC reference for symbol_ref SYMBOL.  If LARGETOC_REG is non-null,
7567    use that as the register to put the HIGH value into if register allocation
7568    is already done.  */
7569
7570 rtx
7571 create_TOC_reference (rtx symbol, rtx largetoc_reg)
7572 {
7573   rtx tocrel, tocreg, hi;
7574
7575   gcc_assert (TARGET_TOC);
7576
7577   if (TARGET_DEBUG_ADDR)
7578     {
7579       if (SYMBOL_REF_P (symbol))
7580         fprintf (stderr, "\ncreate_TOC_reference, (symbol_ref %s)\n",
7581                  XSTR (symbol, 0));
7582       else
7583         {
7584           fprintf (stderr, "\ncreate_TOC_reference, code %s:\n",
7585                    GET_RTX_NAME (GET_CODE (symbol)));
7586           debug_rtx (symbol);
7587         }
7588     }
7589
7590   if (!can_create_pseudo_p ())
7591     df_set_regs_ever_live (TOC_REGISTER, true);
7592
7593   tocreg = gen_rtx_REG (Pmode, TOC_REGISTER);
7594   tocrel = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, symbol, tocreg), UNSPEC_TOCREL);
7595   if (TARGET_CMODEL == CMODEL_SMALL || can_create_pseudo_p ())
7596     return tocrel;
7597
7598   hi = gen_rtx_HIGH (Pmode, copy_rtx (tocrel));
7599   if (largetoc_reg != NULL)
7600     {
7601       emit_move_insn (largetoc_reg, hi);
7602       hi = largetoc_reg;
7603     }
7604   return gen_rtx_LO_SUM (Pmode, hi, tocrel);
7605 }
7606
7607 /* These are only used to pass through from print_operand/print_operand_address
7608    to rs6000_output_addr_const_extra over the intervening function
7609    output_addr_const which is not target code.  */
7610 static const_rtx tocrel_base_oac, tocrel_offset_oac;
7611
7612 /* Return true if OP is a toc pointer relative address (the output
7613    of create_TOC_reference).  If STRICT, do not match non-split
7614    -mcmodel=large/medium toc pointer relative addresses.  If the pointers 
7615    are non-NULL, place base and offset pieces in TOCREL_BASE_RET and 
7616    TOCREL_OFFSET_RET respectively.  */
7617
7618 bool
7619 toc_relative_expr_p (const_rtx op, bool strict, const_rtx *tocrel_base_ret,
7620                      const_rtx *tocrel_offset_ret)
7621 {
7622   if (!TARGET_TOC)
7623     return false;
7624
7625   if (TARGET_CMODEL != CMODEL_SMALL)
7626     {
7627       /* When strict ensure we have everything tidy.  */
7628       if (strict
7629           && !(GET_CODE (op) == LO_SUM
7630                && REG_P (XEXP (op, 0))
7631                && INT_REG_OK_FOR_BASE_P (XEXP (op, 0), strict)))
7632         return false;
7633
7634       /* When not strict, allow non-split TOC addresses and also allow
7635          (lo_sum (high ..)) TOC addresses created during reload.  */
7636       if (GET_CODE (op) == LO_SUM)
7637         op = XEXP (op, 1);
7638     }
7639
7640   const_rtx tocrel_base = op;
7641   const_rtx tocrel_offset = const0_rtx;
7642
7643   if (GET_CODE (op) == PLUS && add_cint_operand (XEXP (op, 1), GET_MODE (op)))
7644     {
7645       tocrel_base = XEXP (op, 0);
7646       tocrel_offset = XEXP (op, 1);
7647     }
7648
7649   if (tocrel_base_ret)
7650     *tocrel_base_ret = tocrel_base;
7651   if (tocrel_offset_ret)
7652     *tocrel_offset_ret = tocrel_offset;
7653
7654   return (GET_CODE (tocrel_base) == UNSPEC
7655           && XINT (tocrel_base, 1) == UNSPEC_TOCREL
7656           && REG_P (XVECEXP (tocrel_base, 0, 1))
7657           && REGNO (XVECEXP (tocrel_base, 0, 1)) == TOC_REGISTER);
7658 }
7659
7660 /* Return true if X is a constant pool address, and also for cmodel=medium
7661    if X is a toc-relative address known to be offsettable within MODE.  */
7662
7663 bool
7664 legitimate_constant_pool_address_p (const_rtx x, machine_mode mode,
7665                                     bool strict)
7666 {
7667   const_rtx tocrel_base, tocrel_offset;
7668   return (toc_relative_expr_p (x, strict, &tocrel_base, &tocrel_offset)
7669           && (TARGET_CMODEL != CMODEL_MEDIUM
7670               || constant_pool_expr_p (XVECEXP (tocrel_base, 0, 0))
7671               || mode == QImode
7672               || offsettable_ok_by_alignment (XVECEXP (tocrel_base, 0, 0),
7673                                               INTVAL (tocrel_offset), mode)));
7674 }
7675
7676 static bool
7677 legitimate_small_data_p (machine_mode mode, rtx x)
7678 {
7679   return (DEFAULT_ABI == ABI_V4
7680           && !flag_pic && !TARGET_TOC
7681           && (SYMBOL_REF_P (x) || GET_CODE (x) == CONST)
7682           && small_data_operand (x, mode));
7683 }
7684
7685 bool
7686 rs6000_legitimate_offset_address_p (machine_mode mode, rtx x,
7687                                     bool strict, bool worst_case)
7688 {
7689   unsigned HOST_WIDE_INT offset;
7690   unsigned int extra;
7691
7692   if (GET_CODE (x) != PLUS)
7693     return false;
7694   if (!REG_P (XEXP (x, 0)))
7695     return false;
7696   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
7697     return false;
7698   if (mode_supports_dq_form (mode))
7699     return quad_address_p (x, mode, strict);
7700   if (!reg_offset_addressing_ok_p (mode))
7701     return virtual_stack_registers_memory_p (x);
7702   if (legitimate_constant_pool_address_p (x, mode, strict || lra_in_progress))
7703     return true;
7704   if (!CONST_INT_P (XEXP (x, 1)))
7705     return false;
7706
7707   offset = INTVAL (XEXP (x, 1));
7708   extra = 0;
7709   switch (mode)
7710     {
7711     case E_DFmode:
7712     case E_DDmode:
7713     case E_DImode:
7714       /* If we are using VSX scalar loads, restrict ourselves to reg+reg
7715          addressing.  */
7716       if (VECTOR_MEM_VSX_P (mode))
7717         return false;
7718
7719       if (!worst_case)
7720         break;
7721       if (!TARGET_POWERPC64)
7722         extra = 4;
7723       else if (offset & 3)
7724         return false;
7725       break;
7726
7727     case E_TFmode:
7728     case E_IFmode:
7729     case E_KFmode:
7730     case E_TDmode:
7731     case E_TImode:
7732     case E_PTImode:
7733       extra = 8;
7734       if (!worst_case)
7735         break;
7736       if (!TARGET_POWERPC64)
7737         extra = 12;
7738       else if (offset & 3)
7739         return false;
7740       break;
7741
7742     default:
7743       break;
7744     }
7745
7746   offset += 0x8000;
7747   return offset < 0x10000 - extra;
7748 }
7749
7750 bool
7751 legitimate_indexed_address_p (rtx x, int strict)
7752 {
7753   rtx op0, op1;
7754
7755   if (GET_CODE (x) != PLUS)
7756     return false;
7757
7758   op0 = XEXP (x, 0);
7759   op1 = XEXP (x, 1);
7760
7761   return (REG_P (op0) && REG_P (op1)
7762           && ((INT_REG_OK_FOR_BASE_P (op0, strict)
7763                && INT_REG_OK_FOR_INDEX_P (op1, strict))
7764               || (INT_REG_OK_FOR_BASE_P (op1, strict)
7765                   && INT_REG_OK_FOR_INDEX_P (op0, strict))));
7766 }
7767
7768 bool
7769 avoiding_indexed_address_p (machine_mode mode)
7770 {
7771   /* Avoid indexed addressing for modes that have non-indexed
7772      load/store instruction forms.  */
7773   return (TARGET_AVOID_XFORM && VECTOR_MEM_NONE_P (mode));
7774 }
7775
7776 bool
7777 legitimate_indirect_address_p (rtx x, int strict)
7778 {
7779   return REG_P (x) && INT_REG_OK_FOR_BASE_P (x, strict);
7780 }
7781
7782 bool
7783 macho_lo_sum_memory_operand (rtx x, machine_mode mode)
7784 {
7785   if (!TARGET_MACHO || !flag_pic
7786       || mode != SImode || !MEM_P (x))
7787     return false;
7788   x = XEXP (x, 0);
7789
7790   if (GET_CODE (x) != LO_SUM)
7791     return false;
7792   if (!REG_P (XEXP (x, 0)))
7793     return false;
7794   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), 0))
7795     return false;
7796   x = XEXP (x, 1);
7797
7798   return CONSTANT_P (x);
7799 }
7800
7801 static bool
7802 legitimate_lo_sum_address_p (machine_mode mode, rtx x, int strict)
7803 {
7804   if (GET_CODE (x) != LO_SUM)
7805     return false;
7806   if (!REG_P (XEXP (x, 0)))
7807     return false;
7808   if (!INT_REG_OK_FOR_BASE_P (XEXP (x, 0), strict))
7809     return false;
7810   /* quad word addresses are restricted, and we can't use LO_SUM.  */
7811   if (mode_supports_dq_form (mode))
7812     return false;
7813   x = XEXP (x, 1);
7814
7815   if (TARGET_ELF || TARGET_MACHO)
7816     {
7817       bool large_toc_ok;
7818
7819       if (DEFAULT_ABI == ABI_V4 && flag_pic)
7820         return false;
7821       /* LRA doesn't use LEGITIMIZE_RELOAD_ADDRESS as it usually calls
7822          push_reload from reload pass code.  LEGITIMIZE_RELOAD_ADDRESS
7823          recognizes some LO_SUM addresses as valid although this
7824          function says opposite.  In most cases, LRA through different
7825          transformations can generate correct code for address reloads.
7826          It cannot manage only some LO_SUM cases.  So we need to add
7827          code here saying that some addresses are still valid.  */
7828       large_toc_ok = (lra_in_progress && TARGET_CMODEL != CMODEL_SMALL
7829                       && small_toc_ref (x, VOIDmode));
7830       if (TARGET_TOC && ! large_toc_ok)
7831         return false;
7832       if (GET_MODE_NUNITS (mode) != 1)
7833         return false;
7834       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
7835           && !(/* ??? Assume floating point reg based on mode?  */
7836                TARGET_HARD_FLOAT && (mode == DFmode || mode == DDmode)))
7837         return false;
7838
7839       return CONSTANT_P (x) || large_toc_ok;
7840     }
7841
7842   return false;
7843 }
7844
7845
7846 /* Try machine-dependent ways of modifying an illegitimate address
7847    to be legitimate.  If we find one, return the new, valid address.
7848    This is used from only one place: `memory_address' in explow.c.
7849
7850    OLDX is the address as it was before break_out_memory_refs was
7851    called.  In some cases it is useful to look at this to decide what
7852    needs to be done.
7853
7854    It is always safe for this function to do nothing.  It exists to
7855    recognize opportunities to optimize the output.
7856
7857    On RS/6000, first check for the sum of a register with a constant
7858    integer that is out of range.  If so, generate code to add the
7859    constant with the low-order 16 bits masked to the register and force
7860    this result into another register (this can be done with `cau').
7861    Then generate an address of REG+(CONST&0xffff), allowing for the
7862    possibility of bit 16 being a one.
7863
7864    Then check for the sum of a register and something not constant, try to
7865    load the other things into a register and return the sum.  */
7866
7867 static rtx
7868 rs6000_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
7869                            machine_mode mode)
7870 {
7871   unsigned int extra;
7872
7873   if (!reg_offset_addressing_ok_p (mode)
7874       || mode_supports_dq_form (mode))
7875     {
7876       if (virtual_stack_registers_memory_p (x))
7877         return x;
7878
7879       /* In theory we should not be seeing addresses of the form reg+0,
7880          but just in case it is generated, optimize it away.  */
7881       if (GET_CODE (x) == PLUS && XEXP (x, 1) == const0_rtx)
7882         return force_reg (Pmode, XEXP (x, 0));
7883
7884       /* For TImode with load/store quad, restrict addresses to just a single
7885          pointer, so it works with both GPRs and VSX registers.  */
7886       /* Make sure both operands are registers.  */
7887       else if (GET_CODE (x) == PLUS
7888                && (mode != TImode || !TARGET_VSX))
7889         return gen_rtx_PLUS (Pmode,
7890                              force_reg (Pmode, XEXP (x, 0)),
7891                              force_reg (Pmode, XEXP (x, 1)));
7892       else
7893         return force_reg (Pmode, x);
7894     }
7895   if (SYMBOL_REF_P (x))
7896     {
7897       enum tls_model model = SYMBOL_REF_TLS_MODEL (x);
7898       if (model != 0)
7899         return rs6000_legitimize_tls_address (x, model);
7900     }
7901
7902   extra = 0;
7903   switch (mode)
7904     {
7905     case E_TFmode:
7906     case E_TDmode:
7907     case E_TImode:
7908     case E_PTImode:
7909     case E_IFmode:
7910     case E_KFmode:
7911       /* As in legitimate_offset_address_p we do not assume
7912          worst-case.  The mode here is just a hint as to the registers
7913          used.  A TImode is usually in gprs, but may actually be in
7914          fprs.  Leave worst-case scenario for reload to handle via
7915          insn constraints.  PTImode is only GPRs.  */
7916       extra = 8;
7917       break;
7918     default:
7919       break;
7920     }
7921
7922   if (GET_CODE (x) == PLUS
7923       && REG_P (XEXP (x, 0))
7924       && CONST_INT_P (XEXP (x, 1))
7925       && ((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 1)) + 0x8000)
7926           >= 0x10000 - extra))
7927     {
7928       HOST_WIDE_INT high_int, low_int;
7929       rtx sum;
7930       low_int = ((INTVAL (XEXP (x, 1)) & 0xffff) ^ 0x8000) - 0x8000;
7931       if (low_int >= 0x8000 - extra)
7932         low_int = 0;
7933       high_int = INTVAL (XEXP (x, 1)) - low_int;
7934       sum = force_operand (gen_rtx_PLUS (Pmode, XEXP (x, 0),
7935                                          GEN_INT (high_int)), 0);
7936       return plus_constant (Pmode, sum, low_int);
7937     }
7938   else if (GET_CODE (x) == PLUS
7939            && REG_P (XEXP (x, 0))
7940            && !CONST_INT_P (XEXP (x, 1))
7941            && GET_MODE_NUNITS (mode) == 1
7942            && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
7943                || (/* ??? Assume floating point reg based on mode?  */
7944                    TARGET_HARD_FLOAT && (mode == DFmode || mode == DDmode)))
7945            && !avoiding_indexed_address_p (mode))
7946     {
7947       return gen_rtx_PLUS (Pmode, XEXP (x, 0),
7948                            force_reg (Pmode, force_operand (XEXP (x, 1), 0)));
7949     }
7950   else if ((TARGET_ELF
7951 #if TARGET_MACHO
7952             || !MACHO_DYNAMIC_NO_PIC_P
7953 #endif
7954             )
7955            && TARGET_32BIT
7956            && TARGET_NO_TOC_OR_PCREL
7957            && !flag_pic
7958            && !CONST_INT_P (x)
7959            && !CONST_WIDE_INT_P (x)
7960            && !CONST_DOUBLE_P (x)
7961            && CONSTANT_P (x)
7962            && GET_MODE_NUNITS (mode) == 1
7963            && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
7964                || (/* ??? Assume floating point reg based on mode?  */
7965                    TARGET_HARD_FLOAT && (mode == DFmode || mode == DDmode))))
7966     {
7967       rtx reg = gen_reg_rtx (Pmode);
7968       if (TARGET_ELF)
7969         emit_insn (gen_elf_high (reg, x));
7970       else
7971         emit_insn (gen_macho_high (reg, x));
7972       return gen_rtx_LO_SUM (Pmode, reg, x);
7973     }
7974   else if (TARGET_TOC
7975            && SYMBOL_REF_P (x)
7976            && constant_pool_expr_p (x)
7977            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (x), Pmode))
7978     return create_TOC_reference (x, NULL_RTX);
7979   else
7980     return x;
7981 }
7982
7983 /* Debug version of rs6000_legitimize_address.  */
7984 static rtx
7985 rs6000_debug_legitimize_address (rtx x, rtx oldx, machine_mode mode)
7986 {
7987   rtx ret;
7988   rtx_insn *insns;
7989
7990   start_sequence ();
7991   ret = rs6000_legitimize_address (x, oldx, mode);
7992   insns = get_insns ();
7993   end_sequence ();
7994
7995   if (ret != x)
7996     {
7997       fprintf (stderr,
7998                "\nrs6000_legitimize_address: mode %s, old code %s, "
7999                "new code %s, modified\n",
8000                GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)),
8001                GET_RTX_NAME (GET_CODE (ret)));
8002
8003       fprintf (stderr, "Original address:\n");
8004       debug_rtx (x);
8005
8006       fprintf (stderr, "oldx:\n");
8007       debug_rtx (oldx);
8008
8009       fprintf (stderr, "New address:\n");
8010       debug_rtx (ret);
8011
8012       if (insns)
8013         {
8014           fprintf (stderr, "Insns added:\n");
8015           debug_rtx_list (insns, 20);
8016         }
8017     }
8018   else
8019     {
8020       fprintf (stderr,
8021                "\nrs6000_legitimize_address: mode %s, code %s, no change:\n",
8022                GET_MODE_NAME (mode), GET_RTX_NAME (GET_CODE (x)));
8023
8024       debug_rtx (x);
8025     }
8026
8027   if (insns)
8028     emit_insn (insns);
8029
8030   return ret;
8031 }
8032
8033 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
8034    We need to emit DTP-relative relocations.  */
8035
8036 static void rs6000_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
8037 static void
8038 rs6000_output_dwarf_dtprel (FILE *file, int size, rtx x)
8039 {
8040   switch (size)
8041     {
8042     case 4:
8043       fputs ("\t.long\t", file);
8044       break;
8045     case 8:
8046       fputs (DOUBLE_INT_ASM_OP, file);
8047       break;
8048     default:
8049       gcc_unreachable ();
8050     }
8051   output_addr_const (file, x);
8052   if (TARGET_ELF)
8053     fputs ("@dtprel+0x8000", file);
8054   else if (TARGET_XCOFF && SYMBOL_REF_P (x))
8055     {
8056       switch (SYMBOL_REF_TLS_MODEL (x))
8057         {
8058         case 0:
8059           break;
8060         case TLS_MODEL_LOCAL_EXEC:
8061           fputs ("@le", file);
8062           break;
8063         case TLS_MODEL_INITIAL_EXEC:
8064           fputs ("@ie", file);
8065           break;
8066         case TLS_MODEL_GLOBAL_DYNAMIC:
8067         case TLS_MODEL_LOCAL_DYNAMIC:
8068           fputs ("@m", file);
8069           break;
8070         default:
8071           gcc_unreachable ();
8072         }
8073     }
8074 }
8075
8076 /* Return true if X is a symbol that refers to real (rather than emulated)
8077    TLS.  */
8078
8079 static bool
8080 rs6000_real_tls_symbol_ref_p (rtx x)
8081 {
8082   return (SYMBOL_REF_P (x)
8083           && SYMBOL_REF_TLS_MODEL (x) >= TLS_MODEL_REAL);
8084 }
8085
8086 /* In the name of slightly smaller debug output, and to cater to
8087    general assembler lossage, recognize various UNSPEC sequences
8088    and turn them back into a direct symbol reference.  */
8089
8090 static rtx
8091 rs6000_delegitimize_address (rtx orig_x)
8092 {
8093   rtx x, y, offset;
8094
8095   if (GET_CODE (orig_x) == UNSPEC && XINT (orig_x, 1) == UNSPEC_FUSION_GPR)
8096     orig_x = XVECEXP (orig_x, 0, 0);
8097
8098   orig_x = delegitimize_mem_from_attrs (orig_x);
8099
8100   x = orig_x;
8101   if (MEM_P (x))
8102     x = XEXP (x, 0);
8103
8104   y = x;
8105   if (TARGET_CMODEL != CMODEL_SMALL && GET_CODE (y) == LO_SUM)
8106     y = XEXP (y, 1);
8107
8108   offset = NULL_RTX;
8109   if (GET_CODE (y) == PLUS
8110       && GET_MODE (y) == Pmode
8111       && CONST_INT_P (XEXP (y, 1)))
8112     {
8113       offset = XEXP (y, 1);
8114       y = XEXP (y, 0);
8115     }
8116
8117   if (GET_CODE (y) == UNSPEC && XINT (y, 1) == UNSPEC_TOCREL)
8118     {
8119       y = XVECEXP (y, 0, 0);
8120
8121 #ifdef HAVE_AS_TLS
8122       /* Do not associate thread-local symbols with the original
8123          constant pool symbol.  */
8124       if (TARGET_XCOFF
8125           && SYMBOL_REF_P (y)
8126           && CONSTANT_POOL_ADDRESS_P (y)
8127           && rs6000_real_tls_symbol_ref_p (get_pool_constant (y)))
8128         return orig_x;
8129 #endif
8130
8131       if (offset != NULL_RTX)
8132         y = gen_rtx_PLUS (Pmode, y, offset);
8133       if (!MEM_P (orig_x))
8134         return y;
8135       else
8136         return replace_equiv_address_nv (orig_x, y);
8137     }
8138
8139   if (TARGET_MACHO
8140       && GET_CODE (orig_x) == LO_SUM
8141       && GET_CODE (XEXP (orig_x, 1)) == CONST)
8142     {
8143       y = XEXP (XEXP (orig_x, 1), 0);
8144       if (GET_CODE (y) == UNSPEC && XINT (y, 1) == UNSPEC_MACHOPIC_OFFSET)
8145         return XVECEXP (y, 0, 0);
8146     }
8147
8148   return orig_x;
8149 }
8150
8151 /* Return true if X shouldn't be emitted into the debug info.
8152    The linker doesn't like .toc section references from
8153    .debug_* sections, so reject .toc section symbols.  */
8154
8155 static bool
8156 rs6000_const_not_ok_for_debug_p (rtx x)
8157 {
8158   if (GET_CODE (x) == UNSPEC)
8159     return true;
8160   if (SYMBOL_REF_P (x)
8161       && CONSTANT_POOL_ADDRESS_P (x))
8162     {
8163       rtx c = get_pool_constant (x);
8164       machine_mode cmode = get_pool_mode (x);
8165       if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (c, cmode))
8166         return true;
8167     }
8168
8169   return false;
8170 }
8171
8172 /* Implement the TARGET_LEGITIMATE_COMBINED_INSN hook.  */
8173
8174 static bool
8175 rs6000_legitimate_combined_insn (rtx_insn *insn)
8176 {
8177   int icode = INSN_CODE (insn);
8178
8179   /* Reject creating doloop insns.  Combine should not be allowed
8180      to create these for a number of reasons:
8181      1) In a nested loop, if combine creates one of these in an
8182      outer loop and the register allocator happens to allocate ctr
8183      to the outer loop insn, then the inner loop can't use ctr.
8184      Inner loops ought to be more highly optimized.
8185      2) Combine often wants to create one of these from what was
8186      originally a three insn sequence, first combining the three
8187      insns to two, then to ctrsi/ctrdi.  When ctrsi/ctrdi is not
8188      allocated ctr, the splitter takes use back to the three insn
8189      sequence.  It's better to stop combine at the two insn
8190      sequence.
8191      3) Faced with not being able to allocate ctr for ctrsi/crtdi
8192      insns, the register allocator sometimes uses floating point
8193      or vector registers for the pseudo.  Since ctrsi/ctrdi is a
8194      jump insn and output reloads are not implemented for jumps,
8195      the ctrsi/ctrdi splitters need to handle all possible cases.
8196      That's a pain, and it gets to be seriously difficult when a
8197      splitter that runs after reload needs memory to transfer from
8198      a gpr to fpr.  See PR70098 and PR71763 which are not fixed
8199      for the difficult case.  It's better to not create problems
8200      in the first place.  */
8201   if (icode != CODE_FOR_nothing
8202       && (icode == CODE_FOR_bdz_si
8203           || icode == CODE_FOR_bdz_di
8204           || icode == CODE_FOR_bdnz_si
8205           || icode == CODE_FOR_bdnz_di
8206           || icode == CODE_FOR_bdztf_si
8207           || icode == CODE_FOR_bdztf_di
8208           || icode == CODE_FOR_bdnztf_si
8209           || icode == CODE_FOR_bdnztf_di))
8210     return false;
8211
8212   return true;
8213 }
8214
8215 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
8216
8217 static GTY(()) rtx rs6000_tls_symbol;
8218 static rtx
8219 rs6000_tls_get_addr (void)
8220 {
8221   if (!rs6000_tls_symbol)
8222     rs6000_tls_symbol = init_one_libfunc ("__tls_get_addr");
8223
8224   return rs6000_tls_symbol;
8225 }
8226
8227 /* Construct the SYMBOL_REF for TLS GOT references.  */
8228
8229 static GTY(()) rtx rs6000_got_symbol;
8230 rtx
8231 rs6000_got_sym (void)
8232 {
8233   if (!rs6000_got_symbol)
8234     {
8235       rs6000_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
8236       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_LOCAL;
8237       SYMBOL_REF_FLAGS (rs6000_got_symbol) |= SYMBOL_FLAG_EXTERNAL;
8238     }
8239
8240   return rs6000_got_symbol;
8241 }
8242
8243 /* AIX Thread-Local Address support.  */
8244
8245 static rtx
8246 rs6000_legitimize_tls_address_aix (rtx addr, enum tls_model model)
8247 {
8248   rtx sym, mem, tocref, tlsreg, tmpreg, dest, tlsaddr;
8249   const char *name;
8250   char *tlsname;
8251
8252   name = XSTR (addr, 0);
8253   /* Append TLS CSECT qualifier, unless the symbol already is qualified
8254      or the symbol will be in TLS private data section.  */
8255   if (name[strlen (name) - 1] != ']'
8256       && (TREE_PUBLIC (SYMBOL_REF_DECL (addr))
8257           || bss_initializer_p (SYMBOL_REF_DECL (addr))))
8258     {
8259       tlsname = XALLOCAVEC (char, strlen (name) + 4);
8260       strcpy (tlsname, name);
8261       strcat (tlsname,
8262               bss_initializer_p (SYMBOL_REF_DECL (addr)) ? "[UL]" : "[TL]");
8263       tlsaddr = copy_rtx (addr);
8264       XSTR (tlsaddr, 0) = ggc_strdup (tlsname);
8265     }
8266   else
8267     tlsaddr = addr;
8268
8269   /* Place addr into TOC constant pool.  */
8270   sym = force_const_mem (GET_MODE (tlsaddr), tlsaddr);
8271
8272   /* Output the TOC entry and create the MEM referencing the value.  */
8273   if (constant_pool_expr_p (XEXP (sym, 0))
8274       && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (XEXP (sym, 0)), Pmode))
8275     {
8276       tocref = create_TOC_reference (XEXP (sym, 0), NULL_RTX);
8277       mem = gen_const_mem (Pmode, tocref);
8278       set_mem_alias_set (mem, get_TOC_alias_set ());
8279     }
8280   else
8281     return sym;
8282
8283   /* Use global-dynamic for local-dynamic.  */
8284   if (model == TLS_MODEL_GLOBAL_DYNAMIC
8285       || model == TLS_MODEL_LOCAL_DYNAMIC)
8286     {
8287       /* Create new TOC reference for @m symbol.  */
8288       name = XSTR (XVECEXP (XEXP (mem, 0), 0, 0), 0);
8289       tlsname = XALLOCAVEC (char, strlen (name) + 1);
8290       strcpy (tlsname, "*LCM");
8291       strcat (tlsname, name + 3);
8292       rtx modaddr = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (tlsname));
8293       SYMBOL_REF_FLAGS (modaddr) |= SYMBOL_FLAG_LOCAL;
8294       tocref = create_TOC_reference (modaddr, NULL_RTX);
8295       rtx modmem = gen_const_mem (Pmode, tocref);
8296       set_mem_alias_set (modmem, get_TOC_alias_set ());
8297       
8298       rtx modreg = gen_reg_rtx (Pmode);
8299       emit_insn (gen_rtx_SET (modreg, modmem));
8300
8301       tmpreg = gen_reg_rtx (Pmode);
8302       emit_insn (gen_rtx_SET (tmpreg, mem));
8303
8304       dest = gen_reg_rtx (Pmode);
8305       if (TARGET_32BIT)
8306         emit_insn (gen_tls_get_addrsi (dest, modreg, tmpreg));
8307       else
8308         emit_insn (gen_tls_get_addrdi (dest, modreg, tmpreg));
8309       return dest;
8310     }
8311   /* Obtain TLS pointer: 32 bit call or 64 bit GPR 13.  */
8312   else if (TARGET_32BIT)
8313     {
8314       tlsreg = gen_reg_rtx (SImode);
8315       emit_insn (gen_tls_get_tpointer (tlsreg));
8316     }
8317   else
8318     tlsreg = gen_rtx_REG (DImode, 13);
8319
8320   /* Load the TOC value into temporary register.  */
8321   tmpreg = gen_reg_rtx (Pmode);
8322   emit_insn (gen_rtx_SET (tmpreg, mem));
8323   set_unique_reg_note (get_last_insn (), REG_EQUAL,
8324                        gen_rtx_MINUS (Pmode, addr, tlsreg));
8325
8326   /* Add TOC symbol value to TLS pointer.  */
8327   dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tmpreg, tlsreg));
8328
8329   return dest;
8330 }
8331
8332 /* Output arg setup instructions for a !TARGET_TLS_MARKERS
8333    __tls_get_addr call.  */
8334
8335 void
8336 rs6000_output_tlsargs (rtx *operands)
8337 {
8338   /* Set up operands for output_asm_insn, without modifying OPERANDS.  */
8339   rtx op[3];
8340
8341   /* The set dest of the call, ie. r3, which is also the first arg reg.  */
8342   op[0] = operands[0];
8343   /* The TLS symbol from global_tlsarg stashed as CALL operand 2.  */
8344   op[1] = XVECEXP (operands[2], 0, 0);
8345   if (XINT (operands[2], 1) == UNSPEC_TLSGD)
8346     {
8347       /* The GOT register.  */
8348       op[2] = XVECEXP (operands[2], 0, 1);
8349       if (TARGET_CMODEL != CMODEL_SMALL)
8350         output_asm_insn ("addis %0,%2,%1@got@tlsgd@ha\n\t"
8351                          "addi %0,%0,%1@got@tlsgd@l", op);
8352       else
8353         output_asm_insn ("addi %0,%2,%1@got@tlsgd", op);
8354     }
8355   else if (XINT (operands[2], 1) == UNSPEC_TLSLD)
8356     {
8357       if (TARGET_CMODEL != CMODEL_SMALL)
8358         output_asm_insn ("addis %0,%1,%&@got@tlsld@ha\n\t"
8359                          "addi %0,%0,%&@got@tlsld@l", op);
8360       else
8361         output_asm_insn ("addi %0,%1,%&@got@tlsld", op);
8362     }
8363   else
8364     gcc_unreachable ();
8365 }
8366
8367 /* Passes the tls arg value for global dynamic and local dynamic
8368    emit_library_call_value in rs6000_legitimize_tls_address to
8369    rs6000_call_aix and rs6000_call_sysv.  This is used to emit the
8370    marker relocs put on __tls_get_addr calls.  */
8371 static rtx global_tlsarg;
8372
8373 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
8374    this (thread-local) address.  */
8375
8376 static rtx
8377 rs6000_legitimize_tls_address (rtx addr, enum tls_model model)
8378 {
8379   rtx dest, insn;
8380
8381   if (TARGET_XCOFF)
8382     return rs6000_legitimize_tls_address_aix (addr, model);
8383
8384   dest = gen_reg_rtx (Pmode);
8385   if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 16)
8386     {
8387       rtx tlsreg;
8388
8389       if (TARGET_64BIT)
8390         {
8391           tlsreg = gen_rtx_REG (Pmode, 13);
8392           insn = gen_tls_tprel_64 (dest, tlsreg, addr);
8393         }
8394       else
8395         {
8396           tlsreg = gen_rtx_REG (Pmode, 2);
8397           insn = gen_tls_tprel_32 (dest, tlsreg, addr);
8398         }
8399       emit_insn (insn);
8400     }
8401   else if (model == TLS_MODEL_LOCAL_EXEC && rs6000_tls_size == 32)
8402     {
8403       rtx tlsreg, tmp;
8404
8405       tmp = gen_reg_rtx (Pmode);
8406       if (TARGET_64BIT)
8407         {
8408           tlsreg = gen_rtx_REG (Pmode, 13);
8409           insn = gen_tls_tprel_ha_64 (tmp, tlsreg, addr);
8410         }
8411       else
8412         {
8413           tlsreg = gen_rtx_REG (Pmode, 2);
8414           insn = gen_tls_tprel_ha_32 (tmp, tlsreg, addr);
8415         }
8416       emit_insn (insn);
8417       if (TARGET_64BIT)
8418         insn = gen_tls_tprel_lo_64 (dest, tmp, addr);
8419       else
8420         insn = gen_tls_tprel_lo_32 (dest, tmp, addr);
8421       emit_insn (insn);
8422     }
8423   else
8424     {
8425       rtx got, tga, tmp1, tmp2;
8426
8427       /* We currently use relocations like @got@tlsgd for tls, which
8428          means the linker will handle allocation of tls entries, placing
8429          them in the .got section.  So use a pointer to the .got section,
8430          not one to secondary TOC sections used by 64-bit -mminimal-toc,
8431          or to secondary GOT sections used by 32-bit -fPIC.  */
8432       if (TARGET_64BIT)
8433         got = gen_rtx_REG (Pmode, 2);
8434       else
8435         {
8436           if (flag_pic == 1)
8437             got = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
8438           else
8439             {
8440               rtx gsym = rs6000_got_sym ();
8441               got = gen_reg_rtx (Pmode);
8442               if (flag_pic == 0)
8443                 rs6000_emit_move (got, gsym, Pmode);
8444               else
8445                 {
8446                   rtx mem, lab;
8447
8448                   tmp1 = gen_reg_rtx (Pmode);
8449                   tmp2 = gen_reg_rtx (Pmode);
8450                   mem = gen_const_mem (Pmode, tmp1);
8451                   lab = gen_label_rtx ();
8452                   emit_insn (gen_load_toc_v4_PIC_1b (gsym, lab));
8453                   emit_move_insn (tmp1, gen_rtx_REG (Pmode, LR_REGNO));
8454                   if (TARGET_LINK_STACK)
8455                     emit_insn (gen_addsi3 (tmp1, tmp1, GEN_INT (4)));
8456                   emit_move_insn (tmp2, mem);
8457                   rtx_insn *last = emit_insn (gen_addsi3 (got, tmp1, tmp2));
8458                   set_unique_reg_note (last, REG_EQUAL, gsym);
8459                 }
8460             }
8461         }
8462
8463       if (model == TLS_MODEL_GLOBAL_DYNAMIC)
8464         {
8465           rtx arg = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, addr, got),
8466                                     UNSPEC_TLSGD);
8467           tga = rs6000_tls_get_addr ();
8468           global_tlsarg = arg;
8469           if (TARGET_TLS_MARKERS)
8470             {
8471               rtx argreg = gen_rtx_REG (Pmode, 3);
8472               emit_insn (gen_rtx_SET (argreg, arg));
8473               emit_library_call_value (tga, dest, LCT_CONST, Pmode,
8474                                        argreg, Pmode);
8475             }
8476           else
8477             emit_library_call_value (tga, dest, LCT_CONST, Pmode);
8478           global_tlsarg = NULL_RTX;
8479
8480           /* Make a note so that the result of this call can be CSEd.  */
8481           rtvec vec = gen_rtvec (1, copy_rtx (arg));
8482           rtx uns = gen_rtx_UNSPEC (Pmode, vec, UNSPEC_TLS_GET_ADDR);
8483           set_unique_reg_note (get_last_insn (), REG_EQUAL, uns);
8484         }
8485       else if (model == TLS_MODEL_LOCAL_DYNAMIC)
8486         {
8487           rtx arg = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, got), UNSPEC_TLSLD);
8488           tga = rs6000_tls_get_addr ();
8489           tmp1 = gen_reg_rtx (Pmode);
8490           global_tlsarg = arg;
8491           if (TARGET_TLS_MARKERS)
8492             {
8493               rtx argreg = gen_rtx_REG (Pmode, 3);
8494               emit_insn (gen_rtx_SET (argreg, arg));
8495               emit_library_call_value (tga, tmp1, LCT_CONST, Pmode,
8496                                        argreg, Pmode);
8497             }
8498           else
8499             emit_library_call_value (tga, tmp1, LCT_CONST, Pmode);
8500           global_tlsarg = NULL_RTX;
8501
8502           /* Make a note so that the result of this call can be CSEd.  */
8503           rtvec vec = gen_rtvec (1, copy_rtx (arg));
8504           rtx uns = gen_rtx_UNSPEC (Pmode, vec, UNSPEC_TLS_GET_ADDR);
8505           set_unique_reg_note (get_last_insn (), REG_EQUAL, uns);
8506
8507           if (rs6000_tls_size == 16)
8508             {
8509               if (TARGET_64BIT)
8510                 insn = gen_tls_dtprel_64 (dest, tmp1, addr);
8511               else
8512                 insn = gen_tls_dtprel_32 (dest, tmp1, addr);
8513             }
8514           else if (rs6000_tls_size == 32)
8515             {
8516               tmp2 = gen_reg_rtx (Pmode);
8517               if (TARGET_64BIT)
8518                 insn = gen_tls_dtprel_ha_64 (tmp2, tmp1, addr);
8519               else
8520                 insn = gen_tls_dtprel_ha_32 (tmp2, tmp1, addr);
8521               emit_insn (insn);
8522               if (TARGET_64BIT)
8523                 insn = gen_tls_dtprel_lo_64 (dest, tmp2, addr);
8524               else
8525                 insn = gen_tls_dtprel_lo_32 (dest, tmp2, addr);
8526             }
8527           else
8528             {
8529               tmp2 = gen_reg_rtx (Pmode);
8530               if (TARGET_64BIT)
8531                 insn = gen_tls_got_dtprel_64 (tmp2, got, addr);
8532               else
8533                 insn = gen_tls_got_dtprel_32 (tmp2, got, addr);
8534               emit_insn (insn);
8535               insn = gen_rtx_SET (dest, gen_rtx_PLUS (Pmode, tmp2, tmp1));
8536             }
8537           emit_insn (insn);
8538         }
8539       else
8540         {
8541           /* IE, or 64-bit offset LE.  */
8542           tmp2 = gen_reg_rtx (Pmode);
8543           if (TARGET_64BIT)
8544             insn = gen_tls_got_tprel_64 (tmp2, got, addr);
8545           else
8546             insn = gen_tls_got_tprel_32 (tmp2, got, addr);
8547           emit_insn (insn);
8548           if (TARGET_64BIT)
8549             insn = gen_tls_tls_64 (dest, tmp2, addr);
8550           else
8551             insn = gen_tls_tls_32 (dest, tmp2, addr);
8552           emit_insn (insn);
8553         }
8554     }
8555
8556   return dest;
8557 }
8558
8559 /* Only create the global variable for the stack protect guard if we are using
8560    the global flavor of that guard.  */
8561 static tree
8562 rs6000_init_stack_protect_guard (void)
8563 {
8564   if (rs6000_stack_protector_guard == SSP_GLOBAL)
8565     return default_stack_protect_guard ();
8566
8567   return NULL_TREE;
8568 }
8569
8570 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
8571
8572 static bool
8573 rs6000_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
8574 {
8575   if (GET_CODE (x) == HIGH
8576       && GET_CODE (XEXP (x, 0)) == UNSPEC)
8577     return true;
8578
8579   /* A TLS symbol in the TOC cannot contain a sum.  */
8580   if (GET_CODE (x) == CONST
8581       && GET_CODE (XEXP (x, 0)) == PLUS
8582       && SYMBOL_REF_P (XEXP (XEXP (x, 0), 0))
8583       && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) != 0)
8584     return true;
8585
8586   /* Do not place an ELF TLS symbol in the constant pool.  */
8587   return TARGET_ELF && tls_referenced_p (x);
8588 }
8589
8590 /* Return true iff the given SYMBOL_REF refers to a constant pool entry
8591    that we have put in the TOC, or for cmodel=medium, if the SYMBOL_REF
8592    can be addressed relative to the toc pointer.  */
8593
8594 static bool
8595 use_toc_relative_ref (rtx sym, machine_mode mode)
8596 {
8597   return ((constant_pool_expr_p (sym)
8598            && ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (get_pool_constant (sym),
8599                                                get_pool_mode (sym)))
8600           || (TARGET_CMODEL == CMODEL_MEDIUM
8601               && SYMBOL_REF_LOCAL_P (sym)
8602               && GET_MODE_SIZE (mode) <= POWERPC64_TOC_POINTER_ALIGNMENT));
8603 }
8604
8605 /* TARGET_LEGITIMATE_ADDRESS_P recognizes an RTL expression
8606    that is a valid memory address for an instruction.
8607    The MODE argument is the machine mode for the MEM expression
8608    that wants to use this address.
8609
8610    On the RS/6000, there are four valid address: a SYMBOL_REF that
8611    refers to a constant pool entry of an address (or the sum of it
8612    plus a constant), a short (16-bit signed) constant plus a register,
8613    the sum of two registers, or a register indirect, possibly with an
8614    auto-increment.  For DFmode, DDmode and DImode with a constant plus
8615    register, we must ensure that both words are addressable or PowerPC64
8616    with offset word aligned.
8617
8618    For modes spanning multiple registers (DFmode and DDmode in 32-bit GPRs,
8619    32-bit DImode, TImode, TFmode, TDmode), indexed addressing cannot be used
8620    because adjacent memory cells are accessed by adding word-sized offsets
8621    during assembly output.  */
8622 static bool
8623 rs6000_legitimate_address_p (machine_mode mode, rtx x, bool reg_ok_strict)
8624 {
8625   bool reg_offset_p = reg_offset_addressing_ok_p (mode);
8626   bool quad_offset_p = mode_supports_dq_form (mode);
8627
8628   /* If this is an unaligned stvx/ldvx type address, discard the outer AND.  */
8629   if (VECTOR_MEM_ALTIVEC_P (mode)
8630       && GET_CODE (x) == AND
8631       && CONST_INT_P (XEXP (x, 1))
8632       && INTVAL (XEXP (x, 1)) == -16)
8633     x = XEXP (x, 0);
8634
8635   if (TARGET_ELF && RS6000_SYMBOL_REF_TLS_P (x))
8636     return 0;
8637   if (legitimate_indirect_address_p (x, reg_ok_strict))
8638     return 1;
8639   if (TARGET_UPDATE
8640       && (GET_CODE (x) == PRE_INC || GET_CODE (x) == PRE_DEC)
8641       && mode_supports_pre_incdec_p (mode)
8642       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict))
8643     return 1;
8644   /* Handle restricted vector d-form offsets in ISA 3.0.  */
8645   if (quad_offset_p)
8646     {
8647       if (quad_address_p (x, mode, reg_ok_strict))
8648         return 1;
8649     }
8650   else if (virtual_stack_registers_memory_p (x))
8651     return 1;
8652
8653   else if (reg_offset_p)
8654     {
8655       if (legitimate_small_data_p (mode, x))
8656         return 1;
8657       if (legitimate_constant_pool_address_p (x, mode,
8658                                              reg_ok_strict || lra_in_progress))
8659         return 1;
8660     }
8661
8662   /* For TImode, if we have TImode in VSX registers, only allow register
8663      indirect addresses.  This will allow the values to go in either GPRs
8664      or VSX registers without reloading.  The vector types would tend to
8665      go into VSX registers, so we allow REG+REG, while TImode seems
8666      somewhat split, in that some uses are GPR based, and some VSX based.  */
8667   /* FIXME: We could loosen this by changing the following to
8668        if (mode == TImode && TARGET_QUAD_MEMORY && TARGET_VSX)
8669      but currently we cannot allow REG+REG addressing for TImode.  See
8670      PR72827 for complete details on how this ends up hoodwinking DSE.  */
8671   if (mode == TImode && TARGET_VSX)
8672     return 0;
8673   /* If not REG_OK_STRICT (before reload) let pass any stack offset.  */
8674   if (! reg_ok_strict
8675       && reg_offset_p
8676       && GET_CODE (x) == PLUS
8677       && REG_P (XEXP (x, 0))
8678       && (XEXP (x, 0) == virtual_stack_vars_rtx
8679           || XEXP (x, 0) == arg_pointer_rtx)
8680       && CONST_INT_P (XEXP (x, 1)))
8681     return 1;
8682   if (rs6000_legitimate_offset_address_p (mode, x, reg_ok_strict, false))
8683     return 1;
8684   if (!FLOAT128_2REG_P (mode)
8685       && (TARGET_HARD_FLOAT
8686           || TARGET_POWERPC64
8687           || (mode != DFmode && mode != DDmode))
8688       && (TARGET_POWERPC64 || mode != DImode)
8689       && (mode != TImode || VECTOR_MEM_VSX_P (TImode))
8690       && mode != PTImode
8691       && !avoiding_indexed_address_p (mode)
8692       && legitimate_indexed_address_p (x, reg_ok_strict))
8693     return 1;
8694   if (TARGET_UPDATE && GET_CODE (x) == PRE_MODIFY
8695       && mode_supports_pre_modify_p (mode)
8696       && legitimate_indirect_address_p (XEXP (x, 0), reg_ok_strict)
8697       && (rs6000_legitimate_offset_address_p (mode, XEXP (x, 1),
8698                                               reg_ok_strict, false)
8699           || (!avoiding_indexed_address_p (mode)
8700               && legitimate_indexed_address_p (XEXP (x, 1), reg_ok_strict)))
8701       && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
8702     return 1;
8703   if (reg_offset_p && !quad_offset_p
8704       && legitimate_lo_sum_address_p (mode, x, reg_ok_strict))
8705     return 1;
8706   return 0;
8707 }
8708
8709 /* Debug version of rs6000_legitimate_address_p.  */
8710 static bool
8711 rs6000_debug_legitimate_address_p (machine_mode mode, rtx x,
8712                                    bool reg_ok_strict)
8713 {
8714   bool ret = rs6000_legitimate_address_p (mode, x, reg_ok_strict);
8715   fprintf (stderr,
8716            "\nrs6000_legitimate_address_p: return = %s, mode = %s, "
8717            "strict = %d, reload = %s, code = %s\n",
8718            ret ? "true" : "false",
8719            GET_MODE_NAME (mode),
8720            reg_ok_strict,
8721            (reload_completed ? "after" : "before"),
8722            GET_RTX_NAME (GET_CODE (x)));
8723   debug_rtx (x);
8724
8725   return ret;
8726 }
8727
8728 /* Implement TARGET_MODE_DEPENDENT_ADDRESS_P.  */
8729
8730 static bool
8731 rs6000_mode_dependent_address_p (const_rtx addr,
8732                                  addr_space_t as ATTRIBUTE_UNUSED)
8733 {
8734   return rs6000_mode_dependent_address_ptr (addr);
8735 }
8736
8737 /* Go to LABEL if ADDR (a legitimate address expression)
8738    has an effect that depends on the machine mode it is used for.
8739
8740    On the RS/6000 this is true of all integral offsets (since AltiVec
8741    and VSX modes don't allow them) or is a pre-increment or decrement.
8742
8743    ??? Except that due to conceptual problems in offsettable_address_p
8744    we can't really report the problems of integral offsets.  So leave
8745    this assuming that the adjustable offset must be valid for the
8746    sub-words of a TFmode operand, which is what we had before.  */
8747
8748 static bool
8749 rs6000_mode_dependent_address (const_rtx addr)
8750 {
8751   switch (GET_CODE (addr))
8752     {
8753     case PLUS:
8754       /* Any offset from virtual_stack_vars_rtx and arg_pointer_rtx
8755          is considered a legitimate address before reload, so there
8756          are no offset restrictions in that case.  Note that this
8757          condition is safe in strict mode because any address involving
8758          virtual_stack_vars_rtx or arg_pointer_rtx would already have
8759          been rejected as illegitimate.  */
8760       if (XEXP (addr, 0) != virtual_stack_vars_rtx
8761           && XEXP (addr, 0) != arg_pointer_rtx
8762           && CONST_INT_P (XEXP (addr, 1)))
8763         {
8764           unsigned HOST_WIDE_INT val = INTVAL (XEXP (addr, 1));
8765           return val + 0x8000 >= 0x10000 - (TARGET_POWERPC64 ? 8 : 12);
8766         }
8767       break;
8768
8769     case LO_SUM:
8770       /* Anything in the constant pool is sufficiently aligned that
8771          all bytes have the same high part address.  */
8772       return !legitimate_constant_pool_address_p (addr, QImode, false);
8773
8774     /* Auto-increment cases are now treated generically in recog.c.  */
8775     case PRE_MODIFY:
8776       return TARGET_UPDATE;
8777
8778     /* AND is only allowed in Altivec loads.  */
8779     case AND:
8780       return true;
8781
8782     default:
8783       break;
8784     }
8785
8786   return false;
8787 }
8788
8789 /* Debug version of rs6000_mode_dependent_address.  */
8790 static bool
8791 rs6000_debug_mode_dependent_address (const_rtx addr)
8792 {
8793   bool ret = rs6000_mode_dependent_address (addr);
8794
8795   fprintf (stderr, "\nrs6000_mode_dependent_address: ret = %s\n",
8796            ret ? "true" : "false");
8797   debug_rtx (addr);
8798
8799   return ret;
8800 }
8801
8802 /* Implement FIND_BASE_TERM.  */
8803
8804 rtx
8805 rs6000_find_base_term (rtx op)
8806 {
8807   rtx base;
8808
8809   base = op;
8810   if (GET_CODE (base) == CONST)
8811     base = XEXP (base, 0);
8812   if (GET_CODE (base) == PLUS)
8813     base = XEXP (base, 0);
8814   if (GET_CODE (base) == UNSPEC)
8815     switch (XINT (base, 1))
8816       {
8817       case UNSPEC_TOCREL:
8818       case UNSPEC_MACHOPIC_OFFSET:
8819         /* OP represents SYM [+ OFFSET] - ANCHOR.  SYM is the base term
8820            for aliasing purposes.  */
8821         return XVECEXP (base, 0, 0);
8822       }
8823
8824   return op;
8825 }
8826
8827 /* More elaborate version of recog's offsettable_memref_p predicate
8828    that works around the ??? note of rs6000_mode_dependent_address.
8829    In particular it accepts
8830
8831      (mem:DI (plus:SI (reg/f:SI 31 31) (const_int 32760 [0x7ff8])))
8832
8833    in 32-bit mode, that the recog predicate rejects.  */
8834
8835 static bool
8836 rs6000_offsettable_memref_p (rtx op, machine_mode reg_mode, bool strict)
8837 {
8838   bool worst_case;
8839
8840   if (!MEM_P (op))
8841     return false;
8842
8843   /* First mimic offsettable_memref_p.  */
8844   if (offsettable_address_p (strict, GET_MODE (op), XEXP (op, 0)))
8845     return true;
8846
8847   /* offsettable_address_p invokes rs6000_mode_dependent_address, but
8848      the latter predicate knows nothing about the mode of the memory
8849      reference and, therefore, assumes that it is the largest supported
8850      mode (TFmode).  As a consequence, legitimate offsettable memory
8851      references are rejected.  rs6000_legitimate_offset_address_p contains
8852      the correct logic for the PLUS case of rs6000_mode_dependent_address,
8853      at least with a little bit of help here given that we know the
8854      actual registers used.  */
8855   worst_case = ((TARGET_POWERPC64 && GET_MODE_CLASS (reg_mode) == MODE_INT)
8856                 || GET_MODE_SIZE (reg_mode) == 4);
8857   return rs6000_legitimate_offset_address_p (GET_MODE (op), XEXP (op, 0),
8858                                              strict, worst_case);
8859 }
8860
8861 /* Determine the reassociation width to be used in reassociate_bb.
8862    This takes into account how many parallel operations we
8863    can actually do of a given type, and also the latency.
8864    P8:
8865      int add/sub 6/cycle     
8866          mul 2/cycle
8867      vect add/sub/mul 2/cycle
8868      fp   add/sub/mul 2/cycle
8869      dfp  1/cycle
8870 */
8871  
8872 static int
8873 rs6000_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
8874                             machine_mode mode)
8875 {
8876   switch (rs6000_tune)
8877     {
8878     case PROCESSOR_POWER8:
8879     case PROCESSOR_POWER9:
8880     case PROCESSOR_FUTURE:
8881       if (DECIMAL_FLOAT_MODE_P (mode))
8882         return 1;
8883       if (VECTOR_MODE_P (mode))
8884         return 4;
8885       if (INTEGRAL_MODE_P (mode)) 
8886         return 1;
8887       if (FLOAT_MODE_P (mode))
8888         return 4;
8889       break;
8890     default:
8891       break;
8892     }
8893   return 1;
8894 }
8895
8896 /* Change register usage conditional on target flags.  */
8897 static void
8898 rs6000_conditional_register_usage (void)
8899 {
8900   int i;
8901
8902   if (TARGET_DEBUG_TARGET)
8903     fprintf (stderr, "rs6000_conditional_register_usage called\n");
8904
8905   /* 64-bit AIX and Linux reserve GPR13 for thread-private data.  */
8906   if (TARGET_64BIT)
8907     fixed_regs[13] = call_used_regs[13]
8908       = call_really_used_regs[13] = 1;
8909
8910   /* Conditionally disable FPRs.  */
8911   if (TARGET_SOFT_FLOAT)
8912     for (i = 32; i < 64; i++)
8913       fixed_regs[i] = call_used_regs[i]
8914         = call_really_used_regs[i] = 1;
8915
8916   /* The TOC register is not killed across calls in a way that is
8917      visible to the compiler.  */
8918   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
8919     call_really_used_regs[2] = 0;
8920
8921   if (DEFAULT_ABI == ABI_V4 && flag_pic == 2)
8922     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8923
8924   if (DEFAULT_ABI == ABI_V4 && flag_pic == 1)
8925     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
8926       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
8927       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8928
8929   if (DEFAULT_ABI == ABI_DARWIN && flag_pic)
8930     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
8931       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
8932       = call_really_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8933
8934   if (TARGET_TOC && TARGET_MINIMAL_TOC)
8935     fixed_regs[RS6000_PIC_OFFSET_TABLE_REGNUM]
8936       = call_used_regs[RS6000_PIC_OFFSET_TABLE_REGNUM] = 1;
8937
8938   if (!TARGET_ALTIVEC && !TARGET_VSX)
8939     {
8940       for (i = FIRST_ALTIVEC_REGNO; i <= LAST_ALTIVEC_REGNO; ++i)
8941         fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
8942       call_really_used_regs[VRSAVE_REGNO] = 1;
8943     }
8944
8945   if (TARGET_ALTIVEC || TARGET_VSX)
8946     global_regs[VSCR_REGNO] = 1;
8947
8948   if (TARGET_ALTIVEC_ABI)
8949     {
8950       for (i = FIRST_ALTIVEC_REGNO; i < FIRST_ALTIVEC_REGNO + 20; ++i)
8951         call_used_regs[i] = call_really_used_regs[i] = 1;
8952
8953       /* AIX reserves VR20:31 in non-extended ABI mode.  */
8954       if (TARGET_XCOFF)
8955         for (i = FIRST_ALTIVEC_REGNO + 20; i < FIRST_ALTIVEC_REGNO + 32; ++i)
8956           fixed_regs[i] = call_used_regs[i] = call_really_used_regs[i] = 1;
8957     }
8958 }
8959
8960 \f
8961 /* Output insns to set DEST equal to the constant SOURCE as a series of
8962    lis, ori and shl instructions and return TRUE.  */
8963
8964 bool
8965 rs6000_emit_set_const (rtx dest, rtx source)
8966 {
8967   machine_mode mode = GET_MODE (dest);
8968   rtx temp, set;
8969   rtx_insn *insn;
8970   HOST_WIDE_INT c;
8971
8972   gcc_checking_assert (CONST_INT_P (source));
8973   c = INTVAL (source);
8974   switch (mode)
8975     {
8976     case E_QImode:
8977     case E_HImode:
8978       emit_insn (gen_rtx_SET (dest, source));
8979       return true;
8980
8981     case E_SImode:
8982       temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (SImode);
8983
8984       emit_insn (gen_rtx_SET (copy_rtx (temp),
8985                               GEN_INT (c & ~(HOST_WIDE_INT) 0xffff)));
8986       emit_insn (gen_rtx_SET (dest,
8987                               gen_rtx_IOR (SImode, copy_rtx (temp),
8988                                            GEN_INT (c & 0xffff))));
8989       break;
8990
8991     case E_DImode:
8992       if (!TARGET_POWERPC64)
8993         {
8994           rtx hi, lo;
8995
8996           hi = operand_subword_force (copy_rtx (dest), WORDS_BIG_ENDIAN == 0,
8997                                       DImode);
8998           lo = operand_subword_force (dest, WORDS_BIG_ENDIAN != 0,
8999                                       DImode);
9000           emit_move_insn (hi, GEN_INT (c >> 32));
9001           c = ((c & 0xffffffff) ^ 0x80000000) - 0x80000000;
9002           emit_move_insn (lo, GEN_INT (c));
9003         }
9004       else
9005         rs6000_emit_set_long_const (dest, c);
9006       break;
9007
9008     default:
9009       gcc_unreachable ();
9010     }
9011
9012   insn = get_last_insn ();
9013   set = single_set (insn);
9014   if (! CONSTANT_P (SET_SRC (set)))
9015     set_unique_reg_note (insn, REG_EQUAL, GEN_INT (c));
9016
9017   return true;
9018 }
9019
9020 /* Subroutine of rs6000_emit_set_const, handling PowerPC64 DImode.
9021    Output insns to set DEST equal to the constant C as a series of
9022    lis, ori and shl instructions.  */
9023
9024 static void
9025 rs6000_emit_set_long_const (rtx dest, HOST_WIDE_INT c)
9026 {
9027   rtx temp;
9028   HOST_WIDE_INT ud1, ud2, ud3, ud4;
9029
9030   ud1 = c & 0xffff;
9031   c = c >> 16;
9032   ud2 = c & 0xffff;
9033   c = c >> 16;
9034   ud3 = c & 0xffff;
9035   c = c >> 16;
9036   ud4 = c & 0xffff;
9037
9038   if ((ud4 == 0xffff && ud3 == 0xffff && ud2 == 0xffff && (ud1 & 0x8000))
9039       || (ud4 == 0 && ud3 == 0 && ud2 == 0 && ! (ud1 & 0x8000)))
9040     emit_move_insn (dest, GEN_INT ((ud1 ^ 0x8000) - 0x8000));
9041
9042   else if ((ud4 == 0xffff && ud3 == 0xffff && (ud2 & 0x8000))
9043            || (ud4 == 0 && ud3 == 0 && ! (ud2 & 0x8000)))
9044     {
9045       temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9046
9047       emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
9048                       GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
9049       if (ud1 != 0)
9050         emit_move_insn (dest,
9051                         gen_rtx_IOR (DImode, copy_rtx (temp),
9052                                      GEN_INT (ud1)));
9053     }
9054   else if (ud3 == 0 && ud4 == 0)
9055     {
9056       temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9057
9058       gcc_assert (ud2 & 0x8000);
9059       emit_move_insn (copy_rtx (temp),
9060                       GEN_INT (((ud2 << 16) ^ 0x80000000) - 0x80000000));
9061       if (ud1 != 0)
9062         emit_move_insn (copy_rtx (temp),
9063                         gen_rtx_IOR (DImode, copy_rtx (temp),
9064                                      GEN_INT (ud1)));
9065       emit_move_insn (dest,
9066                       gen_rtx_ZERO_EXTEND (DImode,
9067                                            gen_lowpart (SImode,
9068                                                         copy_rtx (temp))));
9069     }
9070   else if ((ud4 == 0xffff && (ud3 & 0x8000))
9071            || (ud4 == 0 && ! (ud3 & 0x8000)))
9072     {
9073       temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9074
9075       emit_move_insn (copy_rtx (temp),
9076                       GEN_INT (((ud3 << 16) ^ 0x80000000) - 0x80000000));
9077       if (ud2 != 0)
9078         emit_move_insn (copy_rtx (temp),
9079                         gen_rtx_IOR (DImode, copy_rtx (temp),
9080                                      GEN_INT (ud2)));
9081       emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
9082                       gen_rtx_ASHIFT (DImode, copy_rtx (temp),
9083                                       GEN_INT (16)));
9084       if (ud1 != 0)
9085         emit_move_insn (dest,
9086                         gen_rtx_IOR (DImode, copy_rtx (temp),
9087                                      GEN_INT (ud1)));
9088     }
9089   else
9090     {
9091       temp = !can_create_pseudo_p () ? dest : gen_reg_rtx (DImode);
9092
9093       emit_move_insn (copy_rtx (temp),
9094                       GEN_INT (((ud4 << 16) ^ 0x80000000) - 0x80000000));
9095       if (ud3 != 0)
9096         emit_move_insn (copy_rtx (temp),
9097                         gen_rtx_IOR (DImode, copy_rtx (temp),
9098                                      GEN_INT (ud3)));
9099
9100       emit_move_insn (ud2 != 0 || ud1 != 0 ? copy_rtx (temp) : dest,
9101                       gen_rtx_ASHIFT (DImode, copy_rtx (temp),
9102                                       GEN_INT (32)));
9103       if (ud2 != 0)
9104         emit_move_insn (ud1 != 0 ? copy_rtx (temp) : dest,
9105                         gen_rtx_IOR (DImode, copy_rtx (temp),
9106                                      GEN_INT (ud2 << 16)));
9107       if (ud1 != 0)
9108         emit_move_insn (dest,
9109                         gen_rtx_IOR (DImode, copy_rtx (temp),
9110                                      GEN_INT (ud1)));
9111     }
9112 }
9113
9114 /* Helper for the following.  Get rid of [r+r] memory refs
9115    in cases where it won't work (TImode, TFmode, TDmode, PTImode).  */
9116
9117 static void
9118 rs6000_eliminate_indexed_memrefs (rtx operands[2])
9119 {
9120   if (MEM_P (operands[0])
9121       && !REG_P (XEXP (operands[0], 0))
9122       && ! legitimate_constant_pool_address_p (XEXP (operands[0], 0),
9123                                                GET_MODE (operands[0]), false))
9124     operands[0]
9125       = replace_equiv_address (operands[0],
9126                                copy_addr_to_reg (XEXP (operands[0], 0)));
9127
9128   if (MEM_P (operands[1])
9129       && !REG_P (XEXP (operands[1], 0))
9130       && ! legitimate_constant_pool_address_p (XEXP (operands[1], 0),
9131                                                GET_MODE (operands[1]), false))
9132     operands[1]
9133       = replace_equiv_address (operands[1],
9134                                copy_addr_to_reg (XEXP (operands[1], 0)));
9135 }
9136
9137 /* Generate a vector of constants to permute MODE for a little-endian
9138    storage operation by swapping the two halves of a vector.  */
9139 static rtvec
9140 rs6000_const_vec (machine_mode mode)
9141 {
9142   int i, subparts;
9143   rtvec v;
9144
9145   switch (mode)
9146     {
9147     case E_V1TImode:
9148       subparts = 1;
9149       break;
9150     case E_V2DFmode:
9151     case E_V2DImode:
9152       subparts = 2;
9153       break;
9154     case E_V4SFmode:
9155     case E_V4SImode:
9156       subparts = 4;
9157       break;
9158     case E_V8HImode:
9159       subparts = 8;
9160       break;
9161     case E_V16QImode:
9162       subparts = 16;
9163       break;
9164     default:
9165       gcc_unreachable();
9166     }
9167
9168   v = rtvec_alloc (subparts);
9169
9170   for (i = 0; i < subparts / 2; ++i)
9171     RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i + subparts / 2);
9172   for (i = subparts / 2; i < subparts; ++i)
9173     RTVEC_ELT (v, i) = gen_rtx_CONST_INT (DImode, i - subparts / 2);
9174
9175   return v;
9176 }
9177
9178 /* Emit an lxvd2x, stxvd2x, or xxpermdi instruction for a VSX load or
9179    store operation.  */
9180 void
9181 rs6000_emit_le_vsx_permute (rtx dest, rtx source, machine_mode mode)
9182 {
9183   /* Scalar permutations are easier to express in integer modes rather than
9184      floating-point modes, so cast them here.  We use V1TImode instead
9185      of TImode to ensure that the values don't go through GPRs.  */
9186   if (FLOAT128_VECTOR_P (mode))
9187     {
9188       dest = gen_lowpart (V1TImode, dest);
9189       source = gen_lowpart (V1TImode, source);
9190       mode = V1TImode;
9191     }
9192
9193   /* Use ROTATE instead of VEC_SELECT if the mode contains only a single
9194      scalar.  */
9195   if (mode == TImode || mode == V1TImode)
9196     emit_insn (gen_rtx_SET (dest, gen_rtx_ROTATE (mode, source,
9197                                                   GEN_INT (64))));
9198   else
9199     {
9200       rtx par = gen_rtx_PARALLEL (VOIDmode, rs6000_const_vec (mode));
9201       emit_insn (gen_rtx_SET (dest, gen_rtx_VEC_SELECT (mode, source, par)));
9202     }
9203 }
9204
9205 /* Emit a little-endian load from vector memory location SOURCE to VSX
9206    register DEST in mode MODE.  The load is done with two permuting
9207    insn's that represent an lxvd2x and xxpermdi.  */
9208 void
9209 rs6000_emit_le_vsx_load (rtx dest, rtx source, machine_mode mode)
9210 {
9211   /* Use V2DImode to do swaps of types with 128-bit scalare parts (TImode,
9212      V1TImode).  */
9213   if (mode == TImode || mode == V1TImode)
9214     {
9215       mode = V2DImode;
9216       dest = gen_lowpart (V2DImode, dest);
9217       source = adjust_address (source, V2DImode, 0);
9218     }
9219
9220   rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (dest) : dest;
9221   rs6000_emit_le_vsx_permute (tmp, source, mode);
9222   rs6000_emit_le_vsx_permute (dest, tmp, mode);
9223 }
9224
9225 /* Emit a little-endian store to vector memory location DEST from VSX
9226    register SOURCE in mode MODE.  The store is done with two permuting
9227    insn's that represent an xxpermdi and an stxvd2x.  */
9228 void
9229 rs6000_emit_le_vsx_store (rtx dest, rtx source, machine_mode mode)
9230 {
9231   /* This should never be called during or after LRA, because it does
9232      not re-permute the source register.  It is intended only for use
9233      during expand.  */
9234   gcc_assert (!lra_in_progress && !reload_completed);
9235
9236   /* Use V2DImode to do swaps of types with 128-bit scalar parts (TImode,
9237      V1TImode).  */
9238   if (mode == TImode || mode == V1TImode)
9239     {
9240       mode = V2DImode;
9241       dest = adjust_address (dest, V2DImode, 0);
9242       source = gen_lowpart (V2DImode, source);
9243     }
9244
9245   rtx tmp = can_create_pseudo_p () ? gen_reg_rtx_and_attrs (source) : source;
9246   rs6000_emit_le_vsx_permute (tmp, source, mode);
9247   rs6000_emit_le_vsx_permute (dest, tmp, mode);
9248 }
9249
9250 /* Emit a sequence representing a little-endian VSX load or store,
9251    moving data from SOURCE to DEST in mode MODE.  This is done
9252    separately from rs6000_emit_move to ensure it is called only
9253    during expand.  LE VSX loads and stores introduced later are
9254    handled with a split.  The expand-time RTL generation allows
9255    us to optimize away redundant pairs of register-permutes.  */
9256 void
9257 rs6000_emit_le_vsx_move (rtx dest, rtx source, machine_mode mode)
9258 {
9259   gcc_assert (!BYTES_BIG_ENDIAN
9260               && VECTOR_MEM_VSX_P (mode)
9261               && !TARGET_P9_VECTOR
9262               && !gpr_or_gpr_p (dest, source)
9263               && (MEM_P (source) ^ MEM_P (dest)));
9264
9265   if (MEM_P (source))
9266     {
9267       gcc_assert (REG_P (dest) || SUBREG_P (dest));
9268       rs6000_emit_le_vsx_load (dest, source, mode);
9269     }
9270   else
9271     {
9272       if (!REG_P (source))
9273         source = force_reg (mode, source);
9274       rs6000_emit_le_vsx_store (dest, source, mode);
9275     }
9276 }
9277
9278 /* Return whether a SFmode or SImode move can be done without converting one
9279    mode to another.  This arrises when we have:
9280
9281         (SUBREG:SF (REG:SI ...))
9282         (SUBREG:SI (REG:SF ...))
9283
9284    and one of the values is in a floating point/vector register, where SFmode
9285    scalars are stored in DFmode format.  */
9286
9287 bool
9288 valid_sf_si_move (rtx dest, rtx src, machine_mode mode)
9289 {
9290   if (TARGET_ALLOW_SF_SUBREG)
9291     return true;
9292
9293   if (mode != SFmode && GET_MODE_CLASS (mode) != MODE_INT)
9294     return true;
9295
9296   if (!SUBREG_P (src) || !sf_subreg_operand (src, mode))
9297     return true;
9298
9299   /*.  Allow (set (SUBREG:SI (REG:SF)) (SUBREG:SI (REG:SF))).  */
9300   if (SUBREG_P (dest))
9301     {
9302       rtx dest_subreg = SUBREG_REG (dest);
9303       rtx src_subreg = SUBREG_REG (src);
9304       return GET_MODE (dest_subreg) == GET_MODE (src_subreg);
9305     }
9306
9307   return false;
9308 }
9309
9310
9311 /* Helper function to change moves with:
9312
9313         (SUBREG:SF (REG:SI)) and
9314         (SUBREG:SI (REG:SF))
9315
9316    into separate UNSPEC insns.  In the PowerPC architecture, scalar SFmode
9317    values are stored as DFmode values in the VSX registers.  We need to convert
9318    the bits before we can use a direct move or operate on the bits in the
9319    vector register as an integer type.
9320
9321    Skip things like (set (SUBREG:SI (...) (SUBREG:SI (...)).  */
9322
9323 static bool
9324 rs6000_emit_move_si_sf_subreg (rtx dest, rtx source, machine_mode mode)
9325 {
9326   if (TARGET_DIRECT_MOVE_64BIT && !reload_completed
9327       && (!SUBREG_P (dest) || !sf_subreg_operand (dest, mode))
9328       && SUBREG_P (source) && sf_subreg_operand (source, mode))
9329     {
9330       rtx inner_source = SUBREG_REG (source);
9331       machine_mode inner_mode = GET_MODE (inner_source);
9332
9333       if (mode == SImode && inner_mode == SFmode)
9334         {
9335           emit_insn (gen_movsi_from_sf (dest, inner_source));
9336           return true;
9337         }
9338
9339       if (mode == SFmode && inner_mode == SImode)
9340         {
9341           emit_insn (gen_movsf_from_si (dest, inner_source));
9342           return true;
9343         }
9344     }
9345
9346   return false;
9347 }
9348
9349 /* Emit a move from SOURCE to DEST in mode MODE.  */
9350 void
9351 rs6000_emit_move (rtx dest, rtx source, machine_mode mode)
9352 {
9353   rtx operands[2];
9354   operands[0] = dest;
9355   operands[1] = source;
9356
9357   if (TARGET_DEBUG_ADDR)
9358     {
9359       fprintf (stderr,
9360                "\nrs6000_emit_move: mode = %s, lra_in_progress = %d, "
9361                "reload_completed = %d, can_create_pseudos = %d.\ndest:\n",
9362                GET_MODE_NAME (mode),
9363                lra_in_progress,
9364                reload_completed,
9365                can_create_pseudo_p ());
9366       debug_rtx (dest);
9367       fprintf (stderr, "source:\n");
9368       debug_rtx (source);
9369     }
9370
9371   /* Check that we get CONST_WIDE_INT only when we should.  */
9372   if (CONST_WIDE_INT_P (operands[1])
9373       && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9374     gcc_unreachable ();
9375
9376 #ifdef HAVE_AS_GNU_ATTRIBUTE
9377   /* If we use a long double type, set the flags in .gnu_attribute that say
9378      what the long double type is.  This is to allow the linker's warning
9379      message for the wrong long double to be useful, even if the function does
9380      not do a call (for example, doing a 128-bit add on power9 if the long
9381      double type is IEEE 128-bit.  Do not set this if __ibm128 or __floa128 are
9382      used if they aren't the default long dobule type.  */
9383   if (rs6000_gnu_attr && (HAVE_LD_PPC_GNU_ATTR_LONG_DOUBLE || TARGET_64BIT))
9384     {
9385       if (TARGET_LONG_DOUBLE_128 && (mode == TFmode || mode == TCmode))
9386         rs6000_passes_float = rs6000_passes_long_double = true;
9387
9388       else if (!TARGET_LONG_DOUBLE_128 && (mode == DFmode || mode == DCmode))
9389         rs6000_passes_float = rs6000_passes_long_double = true;
9390     }
9391 #endif
9392
9393   /* See if we need to special case SImode/SFmode SUBREG moves.  */
9394   if ((mode == SImode || mode == SFmode) && SUBREG_P (source)
9395       && rs6000_emit_move_si_sf_subreg (dest, source, mode))
9396     return;
9397
9398   /* Check if GCC is setting up a block move that will end up using FP
9399      registers as temporaries.  We must make sure this is acceptable.  */
9400   if (MEM_P (operands[0])
9401       && MEM_P (operands[1])
9402       && mode == DImode
9403       && (rs6000_slow_unaligned_access (DImode, MEM_ALIGN (operands[0]))
9404           || rs6000_slow_unaligned_access (DImode, MEM_ALIGN (operands[1])))
9405       && ! (rs6000_slow_unaligned_access (SImode,
9406                                           (MEM_ALIGN (operands[0]) > 32
9407                                            ? 32 : MEM_ALIGN (operands[0])))
9408             || rs6000_slow_unaligned_access (SImode,
9409                                              (MEM_ALIGN (operands[1]) > 32
9410                                               ? 32 : MEM_ALIGN (operands[1]))))
9411       && ! MEM_VOLATILE_P (operands [0])
9412       && ! MEM_VOLATILE_P (operands [1]))
9413     {
9414       emit_move_insn (adjust_address (operands[0], SImode, 0),
9415                       adjust_address (operands[1], SImode, 0));
9416       emit_move_insn (adjust_address (copy_rtx (operands[0]), SImode, 4),
9417                       adjust_address (copy_rtx (operands[1]), SImode, 4));
9418       return;
9419     }
9420
9421   if (can_create_pseudo_p () && MEM_P (operands[0])
9422       && !gpc_reg_operand (operands[1], mode))
9423     operands[1] = force_reg (mode, operands[1]);
9424
9425   /* Recognize the case where operand[1] is a reference to thread-local
9426      data and load its address to a register.  */
9427   if (tls_referenced_p (operands[1]))
9428     {
9429       enum tls_model model;
9430       rtx tmp = operands[1];
9431       rtx addend = NULL;
9432
9433       if (GET_CODE (tmp) == CONST && GET_CODE (XEXP (tmp, 0)) == PLUS)
9434         {
9435           addend = XEXP (XEXP (tmp, 0), 1);
9436           tmp = XEXP (XEXP (tmp, 0), 0);
9437         }
9438
9439       gcc_assert (SYMBOL_REF_P (tmp));
9440       model = SYMBOL_REF_TLS_MODEL (tmp);
9441       gcc_assert (model != 0);
9442
9443       tmp = rs6000_legitimize_tls_address (tmp, model);
9444       if (addend)
9445         {
9446           tmp = gen_rtx_PLUS (mode, tmp, addend);
9447           tmp = force_operand (tmp, operands[0]);
9448         }
9449       operands[1] = tmp;
9450     }
9451
9452   /* 128-bit constant floating-point values on Darwin should really be loaded
9453      as two parts.  However, this premature splitting is a problem when DFmode
9454      values can go into Altivec registers.  */
9455   if (TARGET_MACHO && CONST_DOUBLE_P (operands[1]) && FLOAT128_IBM_P (mode)
9456       && !reg_addr[DFmode].scalar_in_vmx_p)
9457     {
9458       rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode, 0),
9459                         simplify_gen_subreg (DFmode, operands[1], mode, 0),
9460                         DFmode);
9461       rs6000_emit_move (simplify_gen_subreg (DFmode, operands[0], mode,
9462                                              GET_MODE_SIZE (DFmode)),
9463                         simplify_gen_subreg (DFmode, operands[1], mode,
9464                                              GET_MODE_SIZE (DFmode)),
9465                         DFmode);
9466       return;
9467     }
9468
9469   /* Transform (p0:DD, (SUBREG:DD p1:SD)) to ((SUBREG:SD p0:DD),
9470      p1:SD) if p1 is not of floating point class and p0 is spilled as
9471      we can have no analogous movsd_store for this.  */
9472   if (lra_in_progress && mode == DDmode
9473       && REG_P (operands[0]) && !HARD_REGISTER_P (operands[0])
9474       && reg_preferred_class (REGNO (operands[0])) == NO_REGS
9475       && SUBREG_P (operands[1]) && REG_P (SUBREG_REG (operands[1]))
9476       && GET_MODE (SUBREG_REG (operands[1])) == SDmode)
9477     {
9478       enum reg_class cl;
9479       int regno = REGNO (SUBREG_REG (operands[1]));
9480
9481       if (!HARD_REGISTER_NUM_P (regno))
9482         {
9483           cl = reg_preferred_class (regno);
9484           regno = reg_renumber[regno];
9485           if (regno < 0)
9486             regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][1];
9487         }
9488       if (regno >= 0 && ! FP_REGNO_P (regno))
9489         {
9490           mode = SDmode;
9491           operands[0] = gen_lowpart_SUBREG (SDmode, operands[0]);
9492           operands[1] = SUBREG_REG (operands[1]);
9493         }
9494     }
9495   if (lra_in_progress
9496       && mode == SDmode
9497       && REG_P (operands[0]) && !HARD_REGISTER_P (operands[0])
9498       && reg_preferred_class (REGNO (operands[0])) == NO_REGS
9499       && (REG_P (operands[1])
9500           || (SUBREG_P (operands[1]) && REG_P (SUBREG_REG (operands[1])))))
9501     {
9502       int regno = reg_or_subregno (operands[1]);
9503       enum reg_class cl;
9504
9505       if (!HARD_REGISTER_NUM_P (regno))
9506         {
9507           cl = reg_preferred_class (regno);
9508           gcc_assert (cl != NO_REGS);
9509           regno = reg_renumber[regno];
9510           if (regno < 0)
9511             regno = ira_class_hard_regs[cl][0];
9512         }
9513       if (FP_REGNO_P (regno))
9514         {
9515           if (GET_MODE (operands[0]) != DDmode)
9516             operands[0] = gen_rtx_SUBREG (DDmode, operands[0], 0);
9517           emit_insn (gen_movsd_store (operands[0], operands[1]));
9518         }
9519       else if (INT_REGNO_P (regno))
9520         emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
9521       else
9522         gcc_unreachable();
9523       return;
9524     }
9525   /* Transform ((SUBREG:DD p0:SD), p1:DD) to (p0:SD, (SUBREG:SD
9526      p:DD)) if p0 is not of floating point class and p1 is spilled as
9527      we can have no analogous movsd_load for this.  */
9528   if (lra_in_progress && mode == DDmode
9529       && SUBREG_P (operands[0]) && REG_P (SUBREG_REG (operands[0]))
9530       && GET_MODE (SUBREG_REG (operands[0])) == SDmode
9531       && REG_P (operands[1]) && !HARD_REGISTER_P (operands[1])
9532       && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
9533     {
9534       enum reg_class cl;
9535       int regno = REGNO (SUBREG_REG (operands[0]));
9536
9537       if (!HARD_REGISTER_NUM_P (regno))
9538         {
9539           cl = reg_preferred_class (regno);
9540           regno = reg_renumber[regno];
9541           if (regno < 0)
9542             regno = cl == NO_REGS ? -1 : ira_class_hard_regs[cl][0];
9543         }
9544       if (regno >= 0 && ! FP_REGNO_P (regno))
9545         {
9546           mode = SDmode;
9547           operands[0] = SUBREG_REG (operands[0]);
9548           operands[1] = gen_lowpart_SUBREG (SDmode, operands[1]);
9549         }
9550     }
9551   if (lra_in_progress
9552       && mode == SDmode
9553       && (REG_P (operands[0])
9554           || (SUBREG_P (operands[0]) && REG_P (SUBREG_REG (operands[0]))))
9555       && REG_P (operands[1]) && !HARD_REGISTER_P (operands[1])
9556       && reg_preferred_class (REGNO (operands[1])) == NO_REGS)
9557     {
9558       int regno = reg_or_subregno (operands[0]);
9559       enum reg_class cl;
9560
9561       if (!HARD_REGISTER_NUM_P (regno))
9562         {
9563           cl = reg_preferred_class (regno);
9564           gcc_assert (cl != NO_REGS);
9565           regno = reg_renumber[regno];
9566           if (regno < 0)
9567             regno = ira_class_hard_regs[cl][0];
9568         }
9569       if (FP_REGNO_P (regno))
9570         {
9571           if (GET_MODE (operands[1]) != DDmode)
9572             operands[1] = gen_rtx_SUBREG (DDmode, operands[1], 0);
9573           emit_insn (gen_movsd_load (operands[0], operands[1]));
9574         }
9575       else if (INT_REGNO_P (regno))
9576         emit_insn (gen_movsd_hardfloat (operands[0], operands[1]));
9577       else
9578         gcc_unreachable();
9579       return;
9580     }
9581
9582   /* FIXME:  In the long term, this switch statement should go away
9583      and be replaced by a sequence of tests based on things like
9584      mode == Pmode.  */
9585   switch (mode)
9586     {
9587     case E_HImode:
9588     case E_QImode:
9589       if (CONSTANT_P (operands[1])
9590           && !CONST_INT_P (operands[1]))
9591         operands[1] = force_const_mem (mode, operands[1]);
9592       break;
9593
9594     case E_TFmode:
9595     case E_TDmode:
9596     case E_IFmode:
9597     case E_KFmode:
9598       if (FLOAT128_2REG_P (mode))
9599         rs6000_eliminate_indexed_memrefs (operands);
9600       /* fall through */
9601
9602     case E_DFmode:
9603     case E_DDmode:
9604     case E_SFmode:
9605     case E_SDmode:
9606       if (CONSTANT_P (operands[1])
9607           && ! easy_fp_constant (operands[1], mode))
9608         operands[1] = force_const_mem (mode, operands[1]);
9609       break;
9610
9611     case E_V16QImode:
9612     case E_V8HImode:
9613     case E_V4SFmode:
9614     case E_V4SImode:
9615     case E_V2DFmode:
9616     case E_V2DImode:
9617     case E_V1TImode:
9618       if (CONSTANT_P (operands[1])
9619           && !easy_vector_constant (operands[1], mode))
9620         operands[1] = force_const_mem (mode, operands[1]);
9621       break;
9622
9623     case E_SImode:
9624     case E_DImode:
9625       /* Use default pattern for address of ELF small data */
9626       if (TARGET_ELF
9627           && mode == Pmode
9628           && DEFAULT_ABI == ABI_V4
9629           && (SYMBOL_REF_P (operands[1])
9630               || GET_CODE (operands[1]) == CONST)
9631           && small_data_operand (operands[1], mode))
9632         {
9633           emit_insn (gen_rtx_SET (operands[0], operands[1]));
9634           return;
9635         }
9636
9637       if (DEFAULT_ABI == ABI_V4
9638           && mode == Pmode && mode == SImode
9639           && flag_pic == 1 && got_operand (operands[1], mode))
9640         {
9641           emit_insn (gen_movsi_got (operands[0], operands[1]));
9642           return;
9643         }
9644
9645       if ((TARGET_ELF || DEFAULT_ABI == ABI_DARWIN)
9646           && TARGET_NO_TOC_OR_PCREL
9647           && ! flag_pic
9648           && mode == Pmode
9649           && CONSTANT_P (operands[1])
9650           && GET_CODE (operands[1]) != HIGH
9651           && !CONST_INT_P (operands[1]))
9652         {
9653           rtx target = (!can_create_pseudo_p ()
9654                         ? operands[0]
9655                         : gen_reg_rtx (mode));
9656
9657           /* If this is a function address on -mcall-aixdesc,
9658              convert it to the address of the descriptor.  */
9659           if (DEFAULT_ABI == ABI_AIX
9660               && SYMBOL_REF_P (operands[1])
9661               && XSTR (operands[1], 0)[0] == '.')
9662             {
9663               const char *name = XSTR (operands[1], 0);
9664               rtx new_ref;
9665               while (*name == '.')
9666                 name++;
9667               new_ref = gen_rtx_SYMBOL_REF (Pmode, name);
9668               CONSTANT_POOL_ADDRESS_P (new_ref)
9669                 = CONSTANT_POOL_ADDRESS_P (operands[1]);
9670               SYMBOL_REF_FLAGS (new_ref) = SYMBOL_REF_FLAGS (operands[1]);
9671               SYMBOL_REF_USED (new_ref) = SYMBOL_REF_USED (operands[1]);
9672               SYMBOL_REF_DATA (new_ref) = SYMBOL_REF_DATA (operands[1]);
9673               operands[1] = new_ref;
9674             }
9675
9676           if (DEFAULT_ABI == ABI_DARWIN)
9677             {
9678 #if TARGET_MACHO
9679               if (MACHO_DYNAMIC_NO_PIC_P)
9680                 {
9681                   /* Take care of any required data indirection.  */
9682                   operands[1] = rs6000_machopic_legitimize_pic_address (
9683                                   operands[1], mode, operands[0]);
9684                   if (operands[0] != operands[1])
9685                     emit_insn (gen_rtx_SET (operands[0], operands[1]));
9686                   return;
9687                 }
9688 #endif
9689               emit_insn (gen_macho_high (target, operands[1]));
9690               emit_insn (gen_macho_low (operands[0], target, operands[1]));
9691               return;
9692             }
9693
9694           emit_insn (gen_elf_high (target, operands[1]));
9695           emit_insn (gen_elf_low (operands[0], target, operands[1]));
9696           return;
9697         }
9698
9699       /* If this is a SYMBOL_REF that refers to a constant pool entry,
9700          and we have put it in the TOC, we just need to make a TOC-relative
9701          reference to it.  */
9702       if (TARGET_TOC
9703           && SYMBOL_REF_P (operands[1])
9704           && use_toc_relative_ref (operands[1], mode))
9705         operands[1] = create_TOC_reference (operands[1], operands[0]);
9706       else if (mode == Pmode
9707                && CONSTANT_P (operands[1])
9708                && GET_CODE (operands[1]) != HIGH
9709                && ((REG_P (operands[0])
9710                     && FP_REGNO_P (REGNO (operands[0])))
9711                    || !CONST_INT_P (operands[1])
9712                    || (num_insns_constant (operands[1], mode)
9713                        > (TARGET_CMODEL != CMODEL_SMALL ? 3 : 2)))
9714                && !toc_relative_expr_p (operands[1], false, NULL, NULL)
9715                && (TARGET_CMODEL == CMODEL_SMALL
9716                    || can_create_pseudo_p ()
9717                    || (REG_P (operands[0])
9718                        && INT_REG_OK_FOR_BASE_P (operands[0], true))))
9719         {
9720
9721 #if TARGET_MACHO
9722           /* Darwin uses a special PIC legitimizer.  */
9723           if (DEFAULT_ABI == ABI_DARWIN && MACHOPIC_INDIRECT)
9724             {
9725               operands[1] =
9726                 rs6000_machopic_legitimize_pic_address (operands[1], mode,
9727                                                         operands[0]);
9728               if (operands[0] != operands[1])
9729                 emit_insn (gen_rtx_SET (operands[0], operands[1]));
9730               return;
9731             }
9732 #endif
9733
9734           /* If we are to limit the number of things we put in the TOC and
9735              this is a symbol plus a constant we can add in one insn,
9736              just put the symbol in the TOC and add the constant.  */
9737           if (GET_CODE (operands[1]) == CONST
9738               && TARGET_NO_SUM_IN_TOC
9739               && GET_CODE (XEXP (operands[1], 0)) == PLUS
9740               && add_operand (XEXP (XEXP (operands[1], 0), 1), mode)
9741               && (GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
9742                   || SYMBOL_REF_P (XEXP (XEXP (operands[1], 0), 0)))
9743               && ! side_effects_p (operands[0]))
9744             {
9745               rtx sym =
9746                 force_const_mem (mode, XEXP (XEXP (operands[1], 0), 0));
9747               rtx other = XEXP (XEXP (operands[1], 0), 1);
9748
9749               sym = force_reg (mode, sym);
9750               emit_insn (gen_add3_insn (operands[0], sym, other));
9751               return;
9752             }
9753
9754           operands[1] = force_const_mem (mode, operands[1]);
9755
9756           if (TARGET_TOC
9757               && SYMBOL_REF_P (XEXP (operands[1], 0))
9758               && use_toc_relative_ref (XEXP (operands[1], 0), mode))
9759             {
9760               rtx tocref = create_TOC_reference (XEXP (operands[1], 0),
9761                                                  operands[0]);
9762               operands[1] = gen_const_mem (mode, tocref);
9763               set_mem_alias_set (operands[1], get_TOC_alias_set ());
9764             }
9765         }
9766       break;
9767
9768     case E_TImode:
9769       if (!VECTOR_MEM_VSX_P (TImode))
9770         rs6000_eliminate_indexed_memrefs (operands);
9771       break;
9772
9773     case E_PTImode:
9774       rs6000_eliminate_indexed_memrefs (operands);
9775       break;
9776
9777     default:
9778       fatal_insn ("bad move", gen_rtx_SET (dest, source));
9779     }
9780
9781   /* Above, we may have called force_const_mem which may have returned
9782      an invalid address.  If we can, fix this up; otherwise, reload will
9783      have to deal with it.  */
9784   if (MEM_P (operands[1]))
9785     operands[1] = validize_mem (operands[1]);
9786
9787   emit_insn (gen_rtx_SET (operands[0], operands[1]));
9788 }
9789 \f
9790
9791 /* Set up AIX/Darwin/64-bit Linux quad floating point routines.  */
9792 static void
9793 init_float128_ibm (machine_mode mode)
9794 {
9795   if (!TARGET_XL_COMPAT)
9796     {
9797       set_optab_libfunc (add_optab, mode, "__gcc_qadd");
9798       set_optab_libfunc (sub_optab, mode, "__gcc_qsub");
9799       set_optab_libfunc (smul_optab, mode, "__gcc_qmul");
9800       set_optab_libfunc (sdiv_optab, mode, "__gcc_qdiv");
9801
9802       if (!TARGET_HARD_FLOAT)
9803         {
9804           set_optab_libfunc (neg_optab, mode, "__gcc_qneg");
9805           set_optab_libfunc (eq_optab, mode, "__gcc_qeq");
9806           set_optab_libfunc (ne_optab, mode, "__gcc_qne");
9807           set_optab_libfunc (gt_optab, mode, "__gcc_qgt");
9808           set_optab_libfunc (ge_optab, mode, "__gcc_qge");
9809           set_optab_libfunc (lt_optab, mode, "__gcc_qlt");
9810           set_optab_libfunc (le_optab, mode, "__gcc_qle");
9811           set_optab_libfunc (unord_optab, mode, "__gcc_qunord");
9812
9813           set_conv_libfunc (sext_optab, mode, SFmode, "__gcc_stoq");
9814           set_conv_libfunc (sext_optab, mode, DFmode, "__gcc_dtoq");
9815           set_conv_libfunc (trunc_optab, SFmode, mode, "__gcc_qtos");
9816           set_conv_libfunc (trunc_optab, DFmode, mode, "__gcc_qtod");
9817           set_conv_libfunc (sfix_optab, SImode, mode, "__gcc_qtoi");
9818           set_conv_libfunc (ufix_optab, SImode, mode, "__gcc_qtou");
9819           set_conv_libfunc (sfloat_optab, mode, SImode, "__gcc_itoq");
9820           set_conv_libfunc (ufloat_optab, mode, SImode, "__gcc_utoq");
9821         }
9822     }
9823   else
9824     {
9825       set_optab_libfunc (add_optab, mode, "_xlqadd");
9826       set_optab_libfunc (sub_optab, mode, "_xlqsub");
9827       set_optab_libfunc (smul_optab, mode, "_xlqmul");
9828       set_optab_libfunc (sdiv_optab, mode, "_xlqdiv");
9829     }
9830
9831   /* Add various conversions for IFmode to use the traditional TFmode
9832      names.  */
9833   if (mode == IFmode)
9834     {
9835       set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdtf");
9836       set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddtf");
9837       set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctdtf");
9838       set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunctfsd");
9839       set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunctfdd");
9840       set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendtftd");
9841
9842       if (TARGET_POWERPC64)
9843         {
9844           set_conv_libfunc (sfix_optab, TImode, mode, "__fixtfti");
9845           set_conv_libfunc (ufix_optab, TImode, mode, "__fixunstfti");
9846           set_conv_libfunc (sfloat_optab, mode, TImode, "__floattitf");
9847           set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntitf");
9848         }
9849     }
9850 }
9851
9852 /* Create a decl for either complex long double multiply or complex long double
9853    divide when long double is IEEE 128-bit floating point.  We can't use
9854    __multc3 and __divtc3 because the original long double using IBM extended
9855    double used those names.  The complex multiply/divide functions are encoded
9856    as builtin functions with a complex result and 4 scalar inputs.  */
9857
9858 static void
9859 create_complex_muldiv (const char *name, built_in_function fncode, tree fntype)
9860 {
9861   tree fndecl = add_builtin_function (name, fntype, fncode, BUILT_IN_NORMAL,
9862                                       name, NULL_TREE);
9863
9864   set_builtin_decl (fncode, fndecl, true);
9865
9866   if (TARGET_DEBUG_BUILTIN)
9867     fprintf (stderr, "create complex %s, fncode: %d\n", name, (int) fncode);
9868
9869   return;
9870 }
9871
9872 /* Set up IEEE 128-bit floating point routines.  Use different names if the
9873    arguments can be passed in a vector register.  The historical PowerPC
9874    implementation of IEEE 128-bit floating point used _q_<op> for the names, so
9875    continue to use that if we aren't using vector registers to pass IEEE
9876    128-bit floating point.  */
9877
9878 static void
9879 init_float128_ieee (machine_mode mode)
9880 {
9881   if (FLOAT128_VECTOR_P (mode))
9882     {
9883       static bool complex_muldiv_init_p = false;
9884
9885       /* Set up to call __mulkc3 and __divkc3 under -mabi=ieeelongdouble.  If
9886          we have clone or target attributes, this will be called a second
9887          time.  We want to create the built-in function only once.  */
9888      if (mode == TFmode && TARGET_IEEEQUAD && !complex_muldiv_init_p)
9889        {
9890          complex_muldiv_init_p = true;
9891          built_in_function fncode_mul =
9892            (built_in_function) (BUILT_IN_COMPLEX_MUL_MIN + TCmode
9893                                 - MIN_MODE_COMPLEX_FLOAT);
9894          built_in_function fncode_div =
9895            (built_in_function) (BUILT_IN_COMPLEX_DIV_MIN + TCmode
9896                                 - MIN_MODE_COMPLEX_FLOAT);
9897
9898          tree fntype = build_function_type_list (complex_long_double_type_node,
9899                                                  long_double_type_node,
9900                                                  long_double_type_node,
9901                                                  long_double_type_node,
9902                                                  long_double_type_node,
9903                                                  NULL_TREE);
9904
9905          create_complex_muldiv ("__mulkc3", fncode_mul, fntype);
9906          create_complex_muldiv ("__divkc3", fncode_div, fntype);
9907        }
9908
9909       set_optab_libfunc (add_optab, mode, "__addkf3");
9910       set_optab_libfunc (sub_optab, mode, "__subkf3");
9911       set_optab_libfunc (neg_optab, mode, "__negkf2");
9912       set_optab_libfunc (smul_optab, mode, "__mulkf3");
9913       set_optab_libfunc (sdiv_optab, mode, "__divkf3");
9914       set_optab_libfunc (sqrt_optab, mode, "__sqrtkf2");
9915       set_optab_libfunc (abs_optab, mode, "__abskf2");
9916       set_optab_libfunc (powi_optab, mode, "__powikf2");
9917
9918       set_optab_libfunc (eq_optab, mode, "__eqkf2");
9919       set_optab_libfunc (ne_optab, mode, "__nekf2");
9920       set_optab_libfunc (gt_optab, mode, "__gtkf2");
9921       set_optab_libfunc (ge_optab, mode, "__gekf2");
9922       set_optab_libfunc (lt_optab, mode, "__ltkf2");
9923       set_optab_libfunc (le_optab, mode, "__lekf2");
9924       set_optab_libfunc (unord_optab, mode, "__unordkf2");
9925
9926       set_conv_libfunc (sext_optab, mode, SFmode, "__extendsfkf2");
9927       set_conv_libfunc (sext_optab, mode, DFmode, "__extenddfkf2");
9928       set_conv_libfunc (trunc_optab, SFmode, mode, "__trunckfsf2");
9929       set_conv_libfunc (trunc_optab, DFmode, mode, "__trunckfdf2");
9930
9931       set_conv_libfunc (sext_optab, mode, IFmode, "__trunctfkf2");
9932       if (mode != TFmode && FLOAT128_IBM_P (TFmode))
9933         set_conv_libfunc (sext_optab, mode, TFmode, "__trunctfkf2");
9934
9935       set_conv_libfunc (trunc_optab, IFmode, mode, "__extendkftf2");
9936       if (mode != TFmode && FLOAT128_IBM_P (TFmode))
9937         set_conv_libfunc (trunc_optab, TFmode, mode, "__extendkftf2");
9938
9939       set_conv_libfunc (sext_optab, mode, SDmode, "__dpd_extendsdkf");
9940       set_conv_libfunc (sext_optab, mode, DDmode, "__dpd_extendddkf");
9941       set_conv_libfunc (trunc_optab, mode, TDmode, "__dpd_trunctdkf");
9942       set_conv_libfunc (trunc_optab, SDmode, mode, "__dpd_trunckfsd");
9943       set_conv_libfunc (trunc_optab, DDmode, mode, "__dpd_trunckfdd");
9944       set_conv_libfunc (sext_optab, TDmode, mode, "__dpd_extendkftd");
9945
9946       set_conv_libfunc (sfix_optab, SImode, mode, "__fixkfsi");
9947       set_conv_libfunc (ufix_optab, SImode, mode, "__fixunskfsi");
9948       set_conv_libfunc (sfix_optab, DImode, mode, "__fixkfdi");
9949       set_conv_libfunc (ufix_optab, DImode, mode, "__fixunskfdi");
9950
9951       set_conv_libfunc (sfloat_optab, mode, SImode, "__floatsikf");
9952       set_conv_libfunc (ufloat_optab, mode, SImode, "__floatunsikf");
9953       set_conv_libfunc (sfloat_optab, mode, DImode, "__floatdikf");
9954       set_conv_libfunc (ufloat_optab, mode, DImode, "__floatundikf");
9955
9956       if (TARGET_POWERPC64)
9957         {
9958           set_conv_libfunc (sfix_optab, TImode, mode, "__fixkfti");
9959           set_conv_libfunc (ufix_optab, TImode, mode, "__fixunskfti");
9960           set_conv_libfunc (sfloat_optab, mode, TImode, "__floattikf");
9961           set_conv_libfunc (ufloat_optab, mode, TImode, "__floatuntikf");
9962         }
9963     }
9964
9965   else
9966     {
9967       set_optab_libfunc (add_optab, mode, "_q_add");
9968       set_optab_libfunc (sub_optab, mode, "_q_sub");
9969       set_optab_libfunc (neg_optab, mode, "_q_neg");
9970       set_optab_libfunc (smul_optab, mode, "_q_mul");
9971       set_optab_libfunc (sdiv_optab, mode, "_q_div");
9972       if (TARGET_PPC_GPOPT)
9973         set_optab_libfunc (sqrt_optab, mode, "_q_sqrt");
9974
9975       set_optab_libfunc (eq_optab, mode, "_q_feq");
9976       set_optab_libfunc (ne_optab, mode, "_q_fne");
9977       set_optab_libfunc (gt_optab, mode, "_q_fgt");
9978       set_optab_libfunc (ge_optab, mode, "_q_fge");
9979       set_optab_libfunc (lt_optab, mode, "_q_flt");
9980       set_optab_libfunc (le_optab, mode, "_q_fle");
9981
9982       set_conv_libfunc (sext_optab, mode, SFmode, "_q_stoq");
9983       set_conv_libfunc (sext_optab, mode, DFmode, "_q_dtoq");
9984       set_conv_libfunc (trunc_optab, SFmode, mode, "_q_qtos");
9985       set_conv_libfunc (trunc_optab, DFmode, mode, "_q_qtod");
9986       set_conv_libfunc (sfix_optab, SImode, mode, "_q_qtoi");
9987       set_conv_libfunc (ufix_optab, SImode, mode, "_q_qtou");
9988       set_conv_libfunc (sfloat_optab, mode, SImode, "_q_itoq");
9989       set_conv_libfunc (ufloat_optab, mode, SImode, "_q_utoq");
9990     }
9991 }
9992
9993 static void
9994 rs6000_init_libfuncs (void)
9995 {
9996   /* __float128 support.  */
9997   if (TARGET_FLOAT128_TYPE)
9998     {
9999       init_float128_ibm (IFmode);
10000       init_float128_ieee (KFmode);
10001     }
10002
10003   /* AIX/Darwin/64-bit Linux quad floating point routines.  */
10004   if (TARGET_LONG_DOUBLE_128)
10005     {
10006       if (!TARGET_IEEEQUAD)
10007         init_float128_ibm (TFmode);
10008
10009       /* IEEE 128-bit including 32-bit SVR4 quad floating point routines.  */
10010       else
10011         init_float128_ieee (TFmode);
10012     }
10013 }
10014
10015 /* Emit a potentially record-form instruction, setting DST from SRC.
10016    If DOT is 0, that is all; otherwise, set CCREG to the result of the
10017    signed comparison of DST with zero.  If DOT is 1, the generated RTL
10018    doesn't care about the DST result; if DOT is 2, it does.  If CCREG
10019    is CR0 do a single dot insn (as a PARALLEL); otherwise, do a SET and
10020    a separate COMPARE.  */
10021
10022 void
10023 rs6000_emit_dot_insn (rtx dst, rtx src, int dot, rtx ccreg)
10024 {
10025   if (dot == 0)
10026     {
10027       emit_move_insn (dst, src);
10028       return;
10029     }
10030
10031   if (cc_reg_not_cr0_operand (ccreg, CCmode))
10032     {
10033       emit_move_insn (dst, src);
10034       emit_move_insn (ccreg, gen_rtx_COMPARE (CCmode, dst, const0_rtx));
10035       return;
10036     }
10037
10038   rtx ccset = gen_rtx_SET (ccreg, gen_rtx_COMPARE (CCmode, src, const0_rtx));
10039   if (dot == 1)
10040     {
10041       rtx clobber = gen_rtx_CLOBBER (VOIDmode, dst);
10042       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, clobber)));
10043     }
10044   else
10045     {
10046       rtx set = gen_rtx_SET (dst, src);
10047       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, ccset, set)));
10048     }
10049 }
10050
10051 \f
10052 /* A validation routine: say whether CODE, a condition code, and MODE
10053    match.  The other alternatives either don't make sense or should
10054    never be generated.  */
10055
10056 void
10057 validate_condition_mode (enum rtx_code code, machine_mode mode)
10058 {
10059   gcc_assert ((GET_RTX_CLASS (code) == RTX_COMPARE
10060                || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
10061               && GET_MODE_CLASS (mode) == MODE_CC);
10062
10063   /* These don't make sense.  */
10064   gcc_assert ((code != GT && code != LT && code != GE && code != LE)
10065               || mode != CCUNSmode);
10066
10067   gcc_assert ((code != GTU && code != LTU && code != GEU && code != LEU)
10068               || mode == CCUNSmode);
10069
10070   gcc_assert (mode == CCFPmode
10071               || (code != ORDERED && code != UNORDERED
10072                   && code != UNEQ && code != LTGT
10073                   && code != UNGT && code != UNLT
10074                   && code != UNGE && code != UNLE));
10075
10076   /* These should never be generated except for
10077      flag_finite_math_only.  */
10078   gcc_assert (mode != CCFPmode
10079               || flag_finite_math_only
10080               || (code != LE && code != GE
10081                   && code != UNEQ && code != LTGT
10082                   && code != UNGT && code != UNLT));
10083
10084   /* These are invalid; the information is not there.  */
10085   gcc_assert (mode != CCEQmode || code == EQ || code == NE);
10086 }
10087
10088 \f
10089 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm,
10090    rldicl, rldicr, or rldic instruction in mode MODE.  If so, if E is
10091    not zero, store there the bit offset (counted from the right) where
10092    the single stretch of 1 bits begins; and similarly for B, the bit
10093    offset where it ends.  */
10094
10095 bool
10096 rs6000_is_valid_mask (rtx mask, int *b, int *e, machine_mode mode)
10097 {
10098   unsigned HOST_WIDE_INT val = INTVAL (mask);
10099   unsigned HOST_WIDE_INT bit;
10100   int nb, ne;
10101   int n = GET_MODE_PRECISION (mode);
10102
10103   if (mode != DImode && mode != SImode)
10104     return false;
10105
10106   if (INTVAL (mask) >= 0)
10107     {
10108       bit = val & -val;
10109       ne = exact_log2 (bit);
10110       nb = exact_log2 (val + bit);
10111     }
10112   else if (val + 1 == 0)
10113     {
10114       nb = n;
10115       ne = 0;
10116     }
10117   else if (val & 1)
10118     {
10119       val = ~val;
10120       bit = val & -val;
10121       nb = exact_log2 (bit);
10122       ne = exact_log2 (val + bit);
10123     }
10124   else
10125     {
10126       bit = val & -val;
10127       ne = exact_log2 (bit);
10128       if (val + bit == 0)
10129         nb = n;
10130       else
10131         nb = 0;
10132     }
10133
10134   nb--;
10135
10136   if (nb < 0 || ne < 0 || nb >= n || ne >= n)
10137     return false;
10138
10139   if (b)
10140     *b = nb;
10141   if (e)
10142     *e = ne;
10143
10144   return true;
10145 }
10146
10147 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwinm, rldicl,
10148    or rldicr instruction, to implement an AND with it in mode MODE.  */
10149
10150 bool
10151 rs6000_is_valid_and_mask (rtx mask, machine_mode mode)
10152 {
10153   int nb, ne;
10154
10155   if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
10156     return false;
10157
10158   /* For DImode, we need a rldicl, rldicr, or a rlwinm with mask that
10159      does not wrap.  */
10160   if (mode == DImode)
10161     return (ne == 0 || nb == 63 || (nb < 32 && ne <= nb));
10162
10163   /* For SImode, rlwinm can do everything.  */
10164   if (mode == SImode)
10165     return (nb < 32 && ne < 32);
10166
10167   return false;
10168 }
10169
10170 /* Return the instruction template for an AND with mask in mode MODE, with
10171    operands OPERANDS.  If DOT is true, make it a record-form instruction.  */
10172
10173 const char *
10174 rs6000_insn_for_and_mask (machine_mode mode, rtx *operands, bool dot)
10175 {
10176   int nb, ne;
10177
10178   if (!rs6000_is_valid_mask (operands[2], &nb, &ne, mode))
10179     gcc_unreachable ();
10180
10181   if (mode == DImode && ne == 0)
10182     {
10183       operands[3] = GEN_INT (63 - nb);
10184       if (dot)
10185         return "rldicl. %0,%1,0,%3";
10186       return "rldicl %0,%1,0,%3";
10187     }
10188
10189   if (mode == DImode && nb == 63)
10190     {
10191       operands[3] = GEN_INT (63 - ne);
10192       if (dot)
10193         return "rldicr. %0,%1,0,%3";
10194       return "rldicr %0,%1,0,%3";
10195     }
10196
10197   if (nb < 32 && ne < 32)
10198     {
10199       operands[3] = GEN_INT (31 - nb);
10200       operands[4] = GEN_INT (31 - ne);
10201       if (dot)
10202         return "rlwinm. %0,%1,0,%3,%4";
10203       return "rlwinm %0,%1,0,%3,%4";
10204     }
10205
10206   gcc_unreachable ();
10207 }
10208
10209 /* Return whether MASK (a CONST_INT) is a valid mask for any rlw[i]nm,
10210    rld[i]cl, rld[i]cr, or rld[i]c instruction, to implement an AND with
10211    shift SHIFT (a ROTATE, ASHIFT, or LSHIFTRT) in mode MODE.  */
10212
10213 bool
10214 rs6000_is_valid_shift_mask (rtx mask, rtx shift, machine_mode mode)
10215 {
10216   int nb, ne;
10217
10218   if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
10219     return false;
10220
10221   int n = GET_MODE_PRECISION (mode);
10222   int sh = -1;
10223
10224   if (CONST_INT_P (XEXP (shift, 1)))
10225     {
10226       sh = INTVAL (XEXP (shift, 1));
10227       if (sh < 0 || sh >= n)
10228         return false;
10229     }
10230
10231   rtx_code code = GET_CODE (shift);
10232
10233   /* Convert any shift by 0 to a rotate, to simplify below code.  */
10234   if (sh == 0)
10235     code = ROTATE;
10236
10237   /* Convert rotate to simple shift if we can, to make analysis simpler.  */
10238   if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
10239     code = ASHIFT;
10240   if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
10241     {
10242       code = LSHIFTRT;
10243       sh = n - sh;
10244     }
10245
10246   /* DImode rotates need rld*.  */
10247   if (mode == DImode && code == ROTATE)
10248     return (nb == 63 || ne == 0 || ne == sh);
10249
10250   /* SImode rotates need rlw*.  */
10251   if (mode == SImode && code == ROTATE)
10252     return (nb < 32 && ne < 32 && sh < 32);
10253
10254   /* Wrap-around masks are only okay for rotates.  */
10255   if (ne > nb)
10256     return false;
10257
10258   /* Variable shifts are only okay for rotates.  */
10259   if (sh < 0)
10260     return false;
10261
10262   /* Don't allow ASHIFT if the mask is wrong for that.  */
10263   if (code == ASHIFT && ne < sh)
10264     return false;
10265
10266   /* If we can do it with an rlw*, we can do it.  Don't allow LSHIFTRT
10267      if the mask is wrong for that.  */
10268   if (nb < 32 && ne < 32 && sh < 32
10269       && !(code == LSHIFTRT && nb >= 32 - sh))
10270     return true;
10271
10272   /* If we can do it with an rld*, we can do it.  Don't allow LSHIFTRT
10273      if the mask is wrong for that.  */
10274   if (code == LSHIFTRT)
10275     sh = 64 - sh;
10276   if (nb == 63 || ne == 0 || ne == sh)
10277     return !(code == LSHIFTRT && nb >= sh);
10278
10279   return false;
10280 }
10281
10282 /* Return the instruction template for a shift with mask in mode MODE, with
10283    operands OPERANDS.  If DOT is true, make it a record-form instruction.  */
10284
10285 const char *
10286 rs6000_insn_for_shift_mask (machine_mode mode, rtx *operands, bool dot)
10287 {
10288   int nb, ne;
10289
10290   if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
10291     gcc_unreachable ();
10292
10293   if (mode == DImode && ne == 0)
10294     {
10295       if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
10296         operands[2] = GEN_INT (64 - INTVAL (operands[2]));
10297       operands[3] = GEN_INT (63 - nb);
10298       if (dot)
10299         return "rld%I2cl. %0,%1,%2,%3";
10300       return "rld%I2cl %0,%1,%2,%3";
10301     }
10302
10303   if (mode == DImode && nb == 63)
10304     {
10305       operands[3] = GEN_INT (63 - ne);
10306       if (dot)
10307         return "rld%I2cr. %0,%1,%2,%3";
10308       return "rld%I2cr %0,%1,%2,%3";
10309     }
10310
10311   if (mode == DImode
10312       && GET_CODE (operands[4]) != LSHIFTRT
10313       && CONST_INT_P (operands[2])
10314       && ne == INTVAL (operands[2]))
10315     {
10316       operands[3] = GEN_INT (63 - nb);
10317       if (dot)
10318         return "rld%I2c. %0,%1,%2,%3";
10319       return "rld%I2c %0,%1,%2,%3";
10320     }
10321
10322   if (nb < 32 && ne < 32)
10323     {
10324       if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
10325         operands[2] = GEN_INT (32 - INTVAL (operands[2]));
10326       operands[3] = GEN_INT (31 - nb);
10327       operands[4] = GEN_INT (31 - ne);
10328       /* This insn can also be a 64-bit rotate with mask that really makes
10329          it just a shift right (with mask); the %h below are to adjust for
10330          that situation (shift count is >= 32 in that case).  */
10331       if (dot)
10332         return "rlw%I2nm. %0,%1,%h2,%3,%4";
10333       return "rlw%I2nm %0,%1,%h2,%3,%4";
10334     }
10335
10336   gcc_unreachable ();
10337 }
10338
10339 /* Return whether MASK (a CONST_INT) is a valid mask for any rlwimi or
10340    rldimi instruction, to implement an insert with shift SHIFT (a ROTATE,
10341    ASHIFT, or LSHIFTRT) in mode MODE.  */
10342
10343 bool
10344 rs6000_is_valid_insert_mask (rtx mask, rtx shift, machine_mode mode)
10345 {
10346   int nb, ne;
10347
10348   if (!rs6000_is_valid_mask (mask, &nb, &ne, mode))
10349     return false;
10350
10351   int n = GET_MODE_PRECISION (mode);
10352
10353   int sh = INTVAL (XEXP (shift, 1));
10354   if (sh < 0 || sh >= n)
10355     return false;
10356
10357   rtx_code code = GET_CODE (shift);
10358
10359   /* Convert any shift by 0 to a rotate, to simplify below code.  */
10360   if (sh == 0)
10361     code = ROTATE;
10362
10363   /* Convert rotate to simple shift if we can, to make analysis simpler.  */
10364   if (code == ROTATE && sh >= 0 && nb >= ne && ne >= sh)
10365     code = ASHIFT;
10366   if (code == ROTATE && sh >= 0 && nb >= ne && nb < sh)
10367     {
10368       code = LSHIFTRT;
10369       sh = n - sh;
10370     }
10371
10372   /* DImode rotates need rldimi.  */
10373   if (mode == DImode && code == ROTATE)
10374     return (ne == sh);
10375
10376   /* SImode rotates need rlwimi.  */
10377   if (mode == SImode && code == ROTATE)
10378     return (nb < 32 && ne < 32 && sh < 32);
10379
10380   /* Wrap-around masks are only okay for rotates.  */
10381   if (ne > nb)
10382     return false;
10383
10384   /* Don't allow ASHIFT if the mask is wrong for that.  */
10385   if (code == ASHIFT && ne < sh)
10386     return false;
10387
10388   /* If we can do it with an rlwimi, we can do it.  Don't allow LSHIFTRT
10389      if the mask is wrong for that.  */
10390   if (nb < 32 && ne < 32 && sh < 32
10391       && !(code == LSHIFTRT && nb >= 32 - sh))
10392     return true;
10393
10394   /* If we can do it with an rldimi, we can do it.  Don't allow LSHIFTRT
10395      if the mask is wrong for that.  */
10396   if (code == LSHIFTRT)
10397     sh = 64 - sh;
10398   if (ne == sh)
10399     return !(code == LSHIFTRT && nb >= sh);
10400
10401   return false;
10402 }
10403
10404 /* Return the instruction template for an insert with mask in mode MODE, with
10405    operands OPERANDS.  If DOT is true, make it a record-form instruction.  */
10406
10407 const char *
10408 rs6000_insn_for_insert_mask (machine_mode mode, rtx *operands, bool dot)
10409 {
10410   int nb, ne;
10411
10412   if (!rs6000_is_valid_mask (operands[3], &nb, &ne, mode))
10413     gcc_unreachable ();
10414
10415   /* Prefer rldimi because rlwimi is cracked.  */
10416   if (TARGET_POWERPC64
10417       && (!dot || mode == DImode)
10418       && GET_CODE (operands[4]) != LSHIFTRT
10419       && ne == INTVAL (operands[2]))
10420     {
10421       operands[3] = GEN_INT (63 - nb);
10422       if (dot)
10423         return "rldimi. %0,%1,%2,%3";
10424       return "rldimi %0,%1,%2,%3";
10425     }
10426
10427   if (nb < 32 && ne < 32)
10428     {
10429       if (GET_CODE (operands[4]) == LSHIFTRT && INTVAL (operands[2]))
10430         operands[2] = GEN_INT (32 - INTVAL (operands[2]));
10431       operands[3] = GEN_INT (31 - nb);
10432       operands[4] = GEN_INT (31 - ne);
10433       if (dot)
10434         return "rlwimi. %0,%1,%2,%3,%4";
10435       return "rlwimi %0,%1,%2,%3,%4";
10436     }
10437
10438   gcc_unreachable ();
10439 }
10440
10441 /* Return whether an AND with C (a CONST_INT) in mode MODE can be done
10442    using two machine instructions.  */
10443
10444 bool
10445 rs6000_is_valid_2insn_and (rtx c, machine_mode mode)
10446 {
10447   /* There are two kinds of AND we can handle with two insns:
10448      1) those we can do with two rl* insn;
10449      2) ori[s];xori[s].
10450
10451      We do not handle that last case yet.  */
10452
10453   /* If there is just one stretch of ones, we can do it.  */
10454   if (rs6000_is_valid_mask (c, NULL, NULL, mode))
10455     return true;
10456
10457   /* Otherwise, fill in the lowest "hole"; if we can do the result with
10458      one insn, we can do the whole thing with two.  */
10459   unsigned HOST_WIDE_INT val = INTVAL (c);
10460   unsigned HOST_WIDE_INT bit1 = val & -val;
10461   unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
10462   unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
10463   unsigned HOST_WIDE_INT bit3 = val1 & -val1;
10464   return rs6000_is_valid_and_mask (GEN_INT (val + bit3 - bit2), mode);
10465 }
10466
10467 /* Emit the two insns to do an AND in mode MODE, with operands OPERANDS.
10468    If EXPAND is true, split rotate-and-mask instructions we generate to
10469    their constituent parts as well (this is used during expand); if DOT
10470    is 1, make the last insn a record-form instruction clobbering the
10471    destination GPR and setting the CC reg (from operands[3]); if 2, set
10472    that GPR as well as the CC reg.  */
10473
10474 void
10475 rs6000_emit_2insn_and (machine_mode mode, rtx *operands, bool expand, int dot)
10476 {
10477   gcc_assert (!(expand && dot));
10478
10479   unsigned HOST_WIDE_INT val = INTVAL (operands[2]);
10480
10481   /* If it is one stretch of ones, it is DImode; shift left, mask, then
10482      shift right.  This generates better code than doing the masks without
10483      shifts, or shifting first right and then left.  */
10484   int nb, ne;
10485   if (rs6000_is_valid_mask (operands[2], &nb, &ne, mode) && nb >= ne)
10486     {
10487       gcc_assert (mode == DImode);
10488
10489       int shift = 63 - nb;
10490       if (expand)
10491         {
10492           rtx tmp1 = gen_reg_rtx (DImode);
10493           rtx tmp2 = gen_reg_rtx (DImode);
10494           emit_insn (gen_ashldi3 (tmp1, operands[1], GEN_INT (shift)));
10495           emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (val << shift)));
10496           emit_insn (gen_lshrdi3 (operands[0], tmp2, GEN_INT (shift)));
10497         }
10498       else
10499         {
10500           rtx tmp = gen_rtx_ASHIFT (mode, operands[1], GEN_INT (shift));
10501           tmp = gen_rtx_AND (mode, tmp, GEN_INT (val << shift));
10502           emit_move_insn (operands[0], tmp);
10503           tmp = gen_rtx_LSHIFTRT (mode, operands[0], GEN_INT (shift));
10504           rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
10505         }
10506       return;
10507     }
10508
10509   /* Otherwise, make a mask2 that cuts out the lowest "hole", and a mask1
10510      that does the rest.  */
10511   unsigned HOST_WIDE_INT bit1 = val & -val;
10512   unsigned HOST_WIDE_INT bit2 = (val + bit1) & ~val;
10513   unsigned HOST_WIDE_INT val1 = (val + bit1) & val;
10514   unsigned HOST_WIDE_INT bit3 = val1 & -val1;
10515
10516   unsigned HOST_WIDE_INT mask1 = -bit3 + bit2 - 1;
10517   unsigned HOST_WIDE_INT mask2 = val + bit3 - bit2;
10518
10519   gcc_assert (rs6000_is_valid_and_mask (GEN_INT (mask2), mode));
10520
10521   /* Two "no-rotate"-and-mask instructions, for SImode.  */
10522   if (rs6000_is_valid_and_mask (GEN_INT (mask1), mode))
10523     {
10524       gcc_assert (mode == SImode);
10525
10526       rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
10527       rtx tmp = gen_rtx_AND (mode, operands[1], GEN_INT (mask1));
10528       emit_move_insn (reg, tmp);
10529       tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
10530       rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
10531       return;
10532     }
10533
10534   gcc_assert (mode == DImode);
10535
10536   /* Two "no-rotate"-and-mask instructions, for DImode: both are rlwinm
10537      insns; we have to do the first in SImode, because it wraps.  */
10538   if (mask2 <= 0xffffffff
10539       && rs6000_is_valid_and_mask (GEN_INT (mask1), SImode))
10540     {
10541       rtx reg = expand ? gen_reg_rtx (mode) : operands[0];
10542       rtx tmp = gen_rtx_AND (SImode, gen_lowpart (SImode, operands[1]),
10543                              GEN_INT (mask1));
10544       rtx reg_low = gen_lowpart (SImode, reg);
10545       emit_move_insn (reg_low, tmp);
10546       tmp = gen_rtx_AND (mode, reg, GEN_INT (mask2));
10547       rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
10548       return;
10549     }
10550
10551   /* Two rld* insns: rotate, clear the hole in the middle (which now is
10552      at the top end), rotate back and clear the other hole.  */
10553   int right = exact_log2 (bit3);
10554   int left = 64 - right;
10555
10556   /* Rotate the mask too.  */
10557   mask1 = (mask1 >> right) | ((bit2 - 1) << left);
10558
10559   if (expand)
10560     {
10561       rtx tmp1 = gen_reg_rtx (DImode);
10562       rtx tmp2 = gen_reg_rtx (DImode);
10563       rtx tmp3 = gen_reg_rtx (DImode);
10564       emit_insn (gen_rotldi3 (tmp1, operands[1], GEN_INT (left)));
10565       emit_insn (gen_anddi3 (tmp2, tmp1, GEN_INT (mask1)));
10566       emit_insn (gen_rotldi3 (tmp3, tmp2, GEN_INT (right)));
10567       emit_insn (gen_anddi3 (operands[0], tmp3, GEN_INT (mask2)));
10568     }
10569   else
10570     {
10571       rtx tmp = gen_rtx_ROTATE (mode, operands[1], GEN_INT (left));
10572       tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask1));
10573       emit_move_insn (operands[0], tmp);
10574       tmp = gen_rtx_ROTATE (mode, operands[0], GEN_INT (right));
10575       tmp = gen_rtx_AND (mode, tmp, GEN_INT (mask2));
10576       rs6000_emit_dot_insn (operands[0], tmp, dot, dot ? operands[3] : 0);
10577     }
10578 }
10579 \f
10580 /* Return 1 if REGNO (reg1) == REGNO (reg2) - 1 making them candidates
10581    for lfq and stfq insns iff the registers are hard registers.   */
10582
10583 int
10584 registers_ok_for_quad_peep (rtx reg1, rtx reg2)
10585 {
10586   /* We might have been passed a SUBREG.  */
10587   if (!REG_P (reg1) || !REG_P (reg2))
10588     return 0;
10589
10590   /* We might have been passed non floating point registers.  */
10591   if (!FP_REGNO_P (REGNO (reg1))
10592       || !FP_REGNO_P (REGNO (reg2)))
10593     return 0;
10594
10595   return (REGNO (reg1) == REGNO (reg2) - 1);
10596 }
10597
10598 /* Return 1 if addr1 and addr2 are suitable for lfq or stfq insn.
10599    addr1 and addr2 must be in consecutive memory locations
10600    (addr2 == addr1 + 8).  */
10601
10602 int
10603 mems_ok_for_quad_peep (rtx mem1, rtx mem2)
10604 {
10605   rtx addr1, addr2;
10606   unsigned int reg1, reg2;
10607   int offset1, offset2;
10608
10609   /* The mems cannot be volatile.  */
10610   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
10611     return 0;
10612
10613   addr1 = XEXP (mem1, 0);
10614   addr2 = XEXP (mem2, 0);
10615
10616   /* Extract an offset (if used) from the first addr.  */
10617   if (GET_CODE (addr1) == PLUS)
10618     {
10619       /* If not a REG, return zero.  */
10620       if (!REG_P (XEXP (addr1, 0)))
10621         return 0;
10622       else
10623         {
10624           reg1 = REGNO (XEXP (addr1, 0));
10625           /* The offset must be constant!  */
10626           if (!CONST_INT_P (XEXP (addr1, 1)))
10627             return 0;
10628           offset1 = INTVAL (XEXP (addr1, 1));
10629         }
10630     }
10631   else if (!REG_P (addr1))
10632     return 0;
10633   else
10634     {
10635       reg1 = REGNO (addr1);
10636       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
10637       offset1 = 0;
10638     }
10639
10640   /* And now for the second addr.  */
10641   if (GET_CODE (addr2) == PLUS)
10642     {
10643       /* If not a REG, return zero.  */
10644       if (!REG_P (XEXP (addr2, 0)))
10645         return 0;
10646       else
10647         {
10648           reg2 = REGNO (XEXP (addr2, 0));
10649           /* The offset must be constant. */
10650           if (!CONST_INT_P (XEXP (addr2, 1)))
10651             return 0;
10652           offset2 = INTVAL (XEXP (addr2, 1));
10653         }
10654     }
10655   else if (!REG_P (addr2))
10656     return 0;
10657   else
10658     {
10659       reg2 = REGNO (addr2);
10660       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
10661       offset2 = 0;
10662     }
10663
10664   /* Both of these must have the same base register.  */
10665   if (reg1 != reg2)
10666     return 0;
10667
10668   /* The offset for the second addr must be 8 more than the first addr.  */
10669   if (offset2 != offset1 + 8)
10670     return 0;
10671
10672   /* All the tests passed.  addr1 and addr2 are valid for lfq or stfq
10673      instructions.  */
10674   return 1;
10675 }
10676 \f
10677 /* Implement TARGET_SECONDARY_RELOAD_NEEDED_MODE.  For SDmode values we
10678    need to use DDmode, in all other cases we can use the same mode.  */
10679 static machine_mode
10680 rs6000_secondary_memory_needed_mode (machine_mode mode)
10681 {
10682   if (lra_in_progress && mode == SDmode)
10683     return DDmode;
10684   return mode;
10685 }
10686
10687 /* Classify a register type.  Because the FMRGOW/FMRGEW instructions only work
10688    on traditional floating point registers, and the VMRGOW/VMRGEW instructions
10689    only work on the traditional altivec registers, note if an altivec register
10690    was chosen.  */
10691
10692 static enum rs6000_reg_type
10693 register_to_reg_type (rtx reg, bool *is_altivec)
10694 {
10695   HOST_WIDE_INT regno;
10696   enum reg_class rclass;
10697
10698   if (SUBREG_P (reg))
10699     reg = SUBREG_REG (reg);
10700
10701   if (!REG_P (reg))
10702     return NO_REG_TYPE;
10703
10704   regno = REGNO (reg);
10705   if (!HARD_REGISTER_NUM_P (regno))
10706     {
10707       if (!lra_in_progress && !reload_completed)
10708         return PSEUDO_REG_TYPE;
10709
10710       regno = true_regnum (reg);
10711       if (regno < 0 || !HARD_REGISTER_NUM_P (regno))
10712         return PSEUDO_REG_TYPE;
10713     }
10714
10715   gcc_assert (regno >= 0);
10716
10717   if (is_altivec && ALTIVEC_REGNO_P (regno))
10718     *is_altivec = true;
10719
10720   rclass = rs6000_regno_regclass[regno];
10721   return reg_class_to_reg_type[(int)rclass];
10722 }
10723
10724 /* Helper function to return the cost of adding a TOC entry address.  */
10725
10726 static inline int
10727 rs6000_secondary_reload_toc_costs (addr_mask_type addr_mask)
10728 {
10729   int ret;
10730
10731   if (TARGET_CMODEL != CMODEL_SMALL)
10732     ret = ((addr_mask & RELOAD_REG_OFFSET) == 0) ? 1 : 2;
10733
10734   else
10735     ret = (TARGET_MINIMAL_TOC) ? 6 : 3;
10736
10737   return ret;
10738 }
10739
10740 /* Helper function for rs6000_secondary_reload to determine whether the memory
10741    address (ADDR) with a given register class (RCLASS) and machine mode (MODE)
10742    needs reloading.  Return negative if the memory is not handled by the memory
10743    helper functions and to try a different reload method, 0 if no additional
10744    instructions are need, and positive to give the extra cost for the
10745    memory.  */
10746
10747 static int
10748 rs6000_secondary_reload_memory (rtx addr,
10749                                 enum reg_class rclass,
10750                                 machine_mode mode)
10751 {
10752   int extra_cost = 0;
10753   rtx reg, and_arg, plus_arg0, plus_arg1;
10754   addr_mask_type addr_mask;
10755   const char *type = NULL;
10756   const char *fail_msg = NULL;
10757
10758   if (GPR_REG_CLASS_P (rclass))
10759     addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
10760
10761   else if (rclass == FLOAT_REGS)
10762     addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
10763
10764   else if (rclass == ALTIVEC_REGS)
10765     addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
10766
10767   /* For the combined VSX_REGS, turn off Altivec AND -16.  */
10768   else if (rclass == VSX_REGS)
10769     addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_VMX]
10770                  & ~RELOAD_REG_AND_M16);
10771
10772   /* If the register allocator hasn't made up its mind yet on the register
10773      class to use, settle on defaults to use.  */
10774   else if (rclass == NO_REGS)
10775     {
10776       addr_mask = (reg_addr[mode].addr_mask[RELOAD_REG_ANY]
10777                    & ~RELOAD_REG_AND_M16);
10778
10779       if ((addr_mask & RELOAD_REG_MULTIPLE) != 0)
10780         addr_mask &= ~(RELOAD_REG_INDEXED
10781                        | RELOAD_REG_PRE_INCDEC
10782                        | RELOAD_REG_PRE_MODIFY);
10783     }
10784
10785   else
10786     addr_mask = 0;
10787
10788   /* If the register isn't valid in this register class, just return now.  */
10789   if ((addr_mask & RELOAD_REG_VALID) == 0)
10790     {
10791       if (TARGET_DEBUG_ADDR)
10792         {
10793           fprintf (stderr,
10794                    "rs6000_secondary_reload_memory: mode = %s, class = %s, "
10795                    "not valid in class\n",
10796                    GET_MODE_NAME (mode), reg_class_names[rclass]);
10797           debug_rtx (addr);
10798         }
10799
10800       return -1;
10801     }
10802
10803   switch (GET_CODE (addr))
10804     {
10805       /* Does the register class supports auto update forms for this mode?  We
10806          don't need a scratch register, since the powerpc only supports
10807          PRE_INC, PRE_DEC, and PRE_MODIFY.  */
10808     case PRE_INC:
10809     case PRE_DEC:
10810       reg = XEXP (addr, 0);
10811       if (!base_reg_operand (addr, GET_MODE (reg)))
10812         {
10813           fail_msg = "no base register #1";
10814           extra_cost = -1;
10815         }
10816
10817       else if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
10818         {
10819           extra_cost = 1;
10820           type = "update";
10821         }
10822       break;
10823
10824     case PRE_MODIFY:
10825       reg = XEXP (addr, 0);
10826       plus_arg1 = XEXP (addr, 1);
10827       if (!base_reg_operand (reg, GET_MODE (reg))
10828           || GET_CODE (plus_arg1) != PLUS
10829           || !rtx_equal_p (reg, XEXP (plus_arg1, 0)))
10830         {
10831           fail_msg = "bad PRE_MODIFY";
10832           extra_cost = -1;
10833         }
10834
10835       else if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
10836         {
10837           extra_cost = 1;
10838           type = "update";
10839         }
10840       break;
10841
10842       /* Do we need to simulate AND -16 to clear the bottom address bits used
10843          in VMX load/stores?  Only allow the AND for vector sizes.  */
10844     case AND:
10845       and_arg = XEXP (addr, 0);
10846       if (GET_MODE_SIZE (mode) != 16
10847           || !CONST_INT_P (XEXP (addr, 1))
10848           || INTVAL (XEXP (addr, 1)) != -16)
10849         {
10850           fail_msg = "bad Altivec AND #1";
10851           extra_cost = -1;
10852         }
10853
10854       if (rclass != ALTIVEC_REGS)
10855         {
10856           if (legitimate_indirect_address_p (and_arg, false))
10857             extra_cost = 1;
10858
10859           else if (legitimate_indexed_address_p (and_arg, false))
10860             extra_cost = 2;
10861
10862           else
10863             {
10864               fail_msg = "bad Altivec AND #2";
10865               extra_cost = -1;
10866             }
10867
10868           type = "and";
10869         }
10870       break;
10871
10872       /* If this is an indirect address, make sure it is a base register.  */
10873     case REG:
10874     case SUBREG:
10875       if (!legitimate_indirect_address_p (addr, false))
10876         {
10877           extra_cost = 1;
10878           type = "move";
10879         }
10880       break;
10881
10882       /* If this is an indexed address, make sure the register class can handle
10883          indexed addresses for this mode.  */
10884     case PLUS:
10885       plus_arg0 = XEXP (addr, 0);
10886       plus_arg1 = XEXP (addr, 1);
10887
10888       /* (plus (plus (reg) (constant)) (constant)) is generated during
10889          push_reload processing, so handle it now.  */
10890       if (GET_CODE (plus_arg0) == PLUS && CONST_INT_P (plus_arg1))
10891         {
10892           if ((addr_mask & RELOAD_REG_OFFSET) == 0)
10893             {
10894               extra_cost = 1;
10895               type = "offset";
10896             }
10897         }
10898
10899       /* (plus (plus (reg) (constant)) (reg)) is also generated during
10900          push_reload processing, so handle it now.  */
10901       else if (GET_CODE (plus_arg0) == PLUS && REG_P (plus_arg1))
10902         {
10903           if ((addr_mask & RELOAD_REG_INDEXED) == 0)
10904             {
10905               extra_cost = 1;
10906               type = "indexed #2";
10907             }
10908         }
10909
10910       else if (!base_reg_operand (plus_arg0, GET_MODE (plus_arg0)))
10911         {
10912           fail_msg = "no base register #2";
10913           extra_cost = -1;
10914         }
10915
10916       else if (int_reg_operand (plus_arg1, GET_MODE (plus_arg1)))
10917         {
10918           if ((addr_mask & RELOAD_REG_INDEXED) == 0
10919               || !legitimate_indexed_address_p (addr, false))
10920             {
10921               extra_cost = 1;
10922               type = "indexed";
10923             }
10924         }
10925
10926       else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0
10927                && CONST_INT_P (plus_arg1))
10928         {
10929           if (!quad_address_offset_p (INTVAL (plus_arg1)))
10930             {
10931               extra_cost = 1;
10932               type = "vector d-form offset";
10933             }
10934         }
10935
10936       /* Make sure the register class can handle offset addresses.  */
10937       else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
10938         {
10939           if ((addr_mask & RELOAD_REG_OFFSET) == 0)
10940             {
10941               extra_cost = 1;
10942               type = "offset #2";
10943             }
10944         }
10945
10946       else
10947         {
10948           fail_msg = "bad PLUS";
10949           extra_cost = -1;
10950         }
10951
10952       break;
10953
10954     case LO_SUM:
10955       /* Quad offsets are restricted and can't handle normal addresses.  */
10956       if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
10957         {
10958           extra_cost = -1;
10959           type = "vector d-form lo_sum";
10960         }
10961
10962       else if (!legitimate_lo_sum_address_p (mode, addr, false))
10963         {
10964           fail_msg = "bad LO_SUM";
10965           extra_cost = -1;
10966         }
10967
10968       if ((addr_mask & RELOAD_REG_OFFSET) == 0)
10969         {
10970           extra_cost = 1;
10971           type = "lo_sum";
10972         }
10973       break;
10974
10975       /* Static addresses need to create a TOC entry.  */
10976     case CONST:
10977     case SYMBOL_REF:
10978     case LABEL_REF:
10979       if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
10980         {
10981           extra_cost = -1;
10982           type = "vector d-form lo_sum #2";
10983         }
10984
10985       else
10986         {
10987           type = "address";
10988           extra_cost = rs6000_secondary_reload_toc_costs (addr_mask);
10989         }
10990       break;
10991
10992       /* TOC references look like offsetable memory.  */
10993     case UNSPEC:
10994       if (TARGET_CMODEL == CMODEL_SMALL || XINT (addr, 1) != UNSPEC_TOCREL)
10995         {
10996           fail_msg = "bad UNSPEC";
10997           extra_cost = -1;
10998         }
10999
11000       else if ((addr_mask & RELOAD_REG_QUAD_OFFSET) != 0)
11001         {
11002           extra_cost = -1;
11003           type = "vector d-form lo_sum #3";
11004         }
11005
11006       else if ((addr_mask & RELOAD_REG_OFFSET) == 0)
11007         {
11008           extra_cost = 1;
11009           type = "toc reference";
11010         }
11011       break;
11012
11013     default:
11014         {
11015           fail_msg = "bad address";
11016           extra_cost = -1;
11017         }
11018     }
11019
11020   if (TARGET_DEBUG_ADDR /* && extra_cost != 0 */)
11021     {
11022       if (extra_cost < 0)
11023         fprintf (stderr,
11024                  "rs6000_secondary_reload_memory error: mode = %s, "
11025                  "class = %s, addr_mask = '%s', %s\n",
11026                  GET_MODE_NAME (mode),
11027                  reg_class_names[rclass],
11028                  rs6000_debug_addr_mask (addr_mask, false),
11029                  (fail_msg != NULL) ? fail_msg : "<bad address>");
11030
11031       else
11032         fprintf (stderr,
11033                  "rs6000_secondary_reload_memory: mode = %s, class = %s, "
11034                  "addr_mask = '%s', extra cost = %d, %s\n",
11035                  GET_MODE_NAME (mode),
11036                  reg_class_names[rclass],
11037                  rs6000_debug_addr_mask (addr_mask, false),
11038                  extra_cost,
11039                  (type) ? type : "<none>");
11040
11041       debug_rtx (addr);
11042     }
11043
11044   return extra_cost;
11045 }
11046
11047 /* Helper function for rs6000_secondary_reload to return true if a move to a
11048    different register classe is really a simple move.  */
11049
11050 static bool
11051 rs6000_secondary_reload_simple_move (enum rs6000_reg_type to_type,
11052                                      enum rs6000_reg_type from_type,
11053                                      machine_mode mode)
11054 {
11055   int size = GET_MODE_SIZE (mode);
11056
11057   /* Add support for various direct moves available.  In this function, we only
11058      look at cases where we don't need any extra registers, and one or more
11059      simple move insns are issued.  Originally small integers are not allowed
11060      in FPR/VSX registers.  Single precision binary floating is not a simple
11061      move because we need to convert to the single precision memory layout.
11062      The 4-byte SDmode can be moved.  TDmode values are disallowed since they
11063      need special direct move handling, which we do not support yet.  */
11064   if (TARGET_DIRECT_MOVE
11065       && ((to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
11066           || (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)))
11067     {
11068       if (TARGET_POWERPC64)
11069         {
11070           /* ISA 2.07: MTVSRD or MVFVSRD.  */
11071           if (size == 8)
11072             return true;
11073
11074           /* ISA 3.0: MTVSRDD or MFVSRD + MFVSRLD.  */
11075           if (size == 16 && TARGET_P9_VECTOR && mode != TDmode)
11076             return true;
11077         }
11078
11079       /* ISA 2.07: MTVSRWZ or  MFVSRWZ.  */
11080       if (TARGET_P8_VECTOR)
11081         {
11082           if (mode == SImode)
11083             return true;
11084
11085           if (TARGET_P9_VECTOR && (mode == HImode || mode == QImode))
11086             return true;
11087         }
11088
11089       /* ISA 2.07: MTVSRWZ or  MFVSRWZ.  */
11090       if (mode == SDmode)
11091         return true;
11092     }
11093
11094   /* Move to/from SPR.  */
11095   else if ((size == 4 || (TARGET_POWERPC64 && size == 8))
11096            && ((to_type == GPR_REG_TYPE && from_type == SPR_REG_TYPE)
11097                || (to_type == SPR_REG_TYPE && from_type == GPR_REG_TYPE)))
11098     return true;
11099
11100   return false;
11101 }
11102
11103 /* Direct move helper function for rs6000_secondary_reload, handle all of the
11104    special direct moves that involve allocating an extra register, return the
11105    insn code of the helper function if there is such a function or
11106    CODE_FOR_nothing if not.  */
11107
11108 static bool
11109 rs6000_secondary_reload_direct_move (enum rs6000_reg_type to_type,
11110                                      enum rs6000_reg_type from_type,
11111                                      machine_mode mode,
11112                                      secondary_reload_info *sri,
11113                                      bool altivec_p)
11114 {
11115   bool ret = false;
11116   enum insn_code icode = CODE_FOR_nothing;
11117   int cost = 0;
11118   int size = GET_MODE_SIZE (mode);
11119
11120   if (TARGET_POWERPC64 && size == 16)
11121     {
11122       /* Handle moving 128-bit values from GPRs to VSX point registers on
11123          ISA 2.07 (power8, power9) when running in 64-bit mode using
11124          XXPERMDI to glue the two 64-bit values back together.  */
11125       if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
11126         {
11127           cost = 3;                     /* 2 mtvsrd's, 1 xxpermdi.  */
11128           icode = reg_addr[mode].reload_vsx_gpr;
11129         }
11130
11131       /* Handle moving 128-bit values from VSX point registers to GPRs on
11132          ISA 2.07 when running in 64-bit mode using XXPERMDI to get access to the
11133          bottom 64-bit value.  */
11134       else if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
11135         {
11136           cost = 3;                     /* 2 mfvsrd's, 1 xxpermdi.  */
11137           icode = reg_addr[mode].reload_gpr_vsx;
11138         }
11139     }
11140
11141   else if (TARGET_POWERPC64 && mode == SFmode)
11142     {
11143       if (to_type == GPR_REG_TYPE && from_type == VSX_REG_TYPE)
11144         {
11145           cost = 3;                     /* xscvdpspn, mfvsrd, and.  */
11146           icode = reg_addr[mode].reload_gpr_vsx;
11147         }
11148
11149       else if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE)
11150         {
11151           cost = 2;                     /* mtvsrz, xscvspdpn.  */
11152           icode = reg_addr[mode].reload_vsx_gpr;
11153         }
11154     }
11155
11156   else if (!TARGET_POWERPC64 && size == 8)
11157     {
11158       /* Handle moving 64-bit values from GPRs to floating point registers on
11159          ISA 2.07 when running in 32-bit mode using FMRGOW to glue the two
11160          32-bit values back together.  Altivec register classes must be handled
11161          specially since a different instruction is used, and the secondary
11162          reload support requires a single instruction class in the scratch
11163          register constraint.  However, right now TFmode is not allowed in
11164          Altivec registers, so the pattern will never match.  */
11165       if (to_type == VSX_REG_TYPE && from_type == GPR_REG_TYPE && !altivec_p)
11166         {
11167           cost = 3;                     /* 2 mtvsrwz's, 1 fmrgow.  */
11168           icode = reg_addr[mode].reload_fpr_gpr;
11169         }
11170     }
11171
11172   if (icode != CODE_FOR_nothing)
11173     {
11174       ret = true;
11175       if (sri)
11176         {
11177           sri->icode = icode;
11178           sri->extra_cost = cost;
11179         }
11180     }
11181
11182   return ret;
11183 }
11184
11185 /* Return whether a move between two register classes can be done either
11186    directly (simple move) or via a pattern that uses a single extra temporary
11187    (using ISA 2.07's direct move in this case.  */
11188
11189 static bool
11190 rs6000_secondary_reload_move (enum rs6000_reg_type to_type,
11191                               enum rs6000_reg_type from_type,
11192                               machine_mode mode,
11193                               secondary_reload_info *sri,
11194                               bool altivec_p)
11195 {
11196   /* Fall back to load/store reloads if either type is not a register.  */
11197   if (to_type == NO_REG_TYPE || from_type == NO_REG_TYPE)
11198     return false;
11199
11200   /* If we haven't allocated registers yet, assume the move can be done for the
11201      standard register types.  */
11202   if ((to_type == PSEUDO_REG_TYPE && from_type == PSEUDO_REG_TYPE)
11203       || (to_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (from_type))
11204       || (from_type == PSEUDO_REG_TYPE && IS_STD_REG_TYPE (to_type)))
11205     return true;
11206
11207   /* Moves to the same set of registers is a simple move for non-specialized
11208      registers.  */
11209   if (to_type == from_type && IS_STD_REG_TYPE (to_type))
11210     return true;
11211
11212   /* Check whether a simple move can be done directly.  */
11213   if (rs6000_secondary_reload_simple_move (to_type, from_type, mode))
11214     {
11215       if (sri)
11216         {
11217           sri->icode = CODE_FOR_nothing;
11218           sri->extra_cost = 0;
11219         }
11220       return true;
11221     }
11222
11223   /* Now check if we can do it in a few steps.  */
11224   return rs6000_secondary_reload_direct_move (to_type, from_type, mode, sri,
11225                                               altivec_p);
11226 }
11227
11228 /* Inform reload about cases where moving X with a mode MODE to a register in
11229    RCLASS requires an extra scratch or immediate register.  Return the class
11230    needed for the immediate register.
11231
11232    For VSX and Altivec, we may need a register to convert sp+offset into
11233    reg+sp.
11234
11235    For misaligned 64-bit gpr loads and stores we need a register to
11236    convert an offset address to indirect.  */
11237
11238 static reg_class_t
11239 rs6000_secondary_reload (bool in_p,
11240                          rtx x,
11241                          reg_class_t rclass_i,
11242                          machine_mode mode,
11243                          secondary_reload_info *sri)
11244 {
11245   enum reg_class rclass = (enum reg_class) rclass_i;
11246   reg_class_t ret = ALL_REGS;
11247   enum insn_code icode;
11248   bool default_p = false;
11249   bool done_p = false;
11250
11251   /* Allow subreg of memory before/during reload.  */
11252   bool memory_p = (MEM_P (x)
11253                    || (!reload_completed && SUBREG_P (x)
11254                        && MEM_P (SUBREG_REG (x))));
11255
11256   sri->icode = CODE_FOR_nothing;
11257   sri->t_icode = CODE_FOR_nothing;
11258   sri->extra_cost = 0;
11259   icode = ((in_p)
11260            ? reg_addr[mode].reload_load
11261            : reg_addr[mode].reload_store);
11262
11263   if (REG_P (x) || register_operand (x, mode))
11264     {
11265       enum rs6000_reg_type to_type = reg_class_to_reg_type[(int)rclass];
11266       bool altivec_p = (rclass == ALTIVEC_REGS);
11267       enum rs6000_reg_type from_type = register_to_reg_type (x, &altivec_p);
11268
11269       if (!in_p)
11270         std::swap (to_type, from_type);
11271
11272       /* Can we do a direct move of some sort?  */
11273       if (rs6000_secondary_reload_move (to_type, from_type, mode, sri,
11274                                         altivec_p))
11275         {
11276           icode = (enum insn_code)sri->icode;
11277           default_p = false;
11278           done_p = true;
11279           ret = NO_REGS;
11280         }
11281     }
11282
11283   /* Make sure 0.0 is not reloaded or forced into memory.  */
11284   if (x == CONST0_RTX (mode) && VSX_REG_CLASS_P (rclass))
11285     {
11286       ret = NO_REGS;
11287       default_p = false;
11288       done_p = true;
11289     }
11290
11291   /* If this is a scalar floating point value and we want to load it into the
11292      traditional Altivec registers, do it via a move via a traditional floating
11293      point register, unless we have D-form addressing.  Also make sure that
11294      non-zero constants use a FPR.  */
11295   if (!done_p && reg_addr[mode].scalar_in_vmx_p
11296       && !mode_supports_vmx_dform (mode)
11297       && (rclass == VSX_REGS || rclass == ALTIVEC_REGS)
11298       && (memory_p || CONST_DOUBLE_P (x)))
11299     {
11300       ret = FLOAT_REGS;
11301       default_p = false;
11302       done_p = true;
11303     }
11304
11305   /* Handle reload of load/stores if we have reload helper functions.  */
11306   if (!done_p && icode != CODE_FOR_nothing && memory_p)
11307     {
11308       int extra_cost = rs6000_secondary_reload_memory (XEXP (x, 0), rclass,
11309                                                        mode);
11310
11311       if (extra_cost >= 0)
11312         {
11313           done_p = true;
11314           ret = NO_REGS;
11315           if (extra_cost > 0)
11316             {
11317               sri->extra_cost = extra_cost;
11318               sri->icode = icode;
11319             }
11320         }
11321     }
11322
11323   /* Handle unaligned loads and stores of integer registers.  */
11324   if (!done_p && TARGET_POWERPC64
11325       && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
11326       && memory_p
11327       && GET_MODE_SIZE (GET_MODE (x)) >= UNITS_PER_WORD)
11328     {
11329       rtx addr = XEXP (x, 0);
11330       rtx off = address_offset (addr);
11331
11332       if (off != NULL_RTX)
11333         {
11334           unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
11335           unsigned HOST_WIDE_INT offset = INTVAL (off);
11336
11337           /* We need a secondary reload when our legitimate_address_p
11338              says the address is good (as otherwise the entire address
11339              will be reloaded), and the offset is not a multiple of
11340              four or we have an address wrap.  Address wrap will only
11341              occur for LO_SUMs since legitimate_offset_address_p
11342              rejects addresses for 16-byte mems that will wrap.  */
11343           if (GET_CODE (addr) == LO_SUM
11344               ? (1 /* legitimate_address_p allows any offset for lo_sum */
11345                  && ((offset & 3) != 0
11346                      || ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra))
11347               : (offset + 0x8000 < 0x10000 - extra /* legitimate_address_p */
11348                  && (offset & 3) != 0))
11349             {
11350               /* -m32 -mpowerpc64 needs to use a 32-bit scratch register.  */
11351               if (in_p)
11352                 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_load
11353                               : CODE_FOR_reload_di_load);
11354               else
11355                 sri->icode = ((TARGET_32BIT) ? CODE_FOR_reload_si_store
11356                               : CODE_FOR_reload_di_store);
11357               sri->extra_cost = 2;
11358               ret = NO_REGS;
11359               done_p = true;
11360             }
11361           else
11362             default_p = true;
11363         }
11364       else
11365         default_p = true;
11366     }
11367
11368   if (!done_p && !TARGET_POWERPC64
11369       && reg_class_to_reg_type[(int)rclass] == GPR_REG_TYPE
11370       && memory_p
11371       && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
11372     {
11373       rtx addr = XEXP (x, 0);
11374       rtx off = address_offset (addr);
11375
11376       if (off != NULL_RTX)
11377         {
11378           unsigned int extra = GET_MODE_SIZE (GET_MODE (x)) - UNITS_PER_WORD;
11379           unsigned HOST_WIDE_INT offset = INTVAL (off);
11380
11381           /* We need a secondary reload when our legitimate_address_p
11382              says the address is good (as otherwise the entire address
11383              will be reloaded), and we have a wrap.
11384
11385              legitimate_lo_sum_address_p allows LO_SUM addresses to
11386              have any offset so test for wrap in the low 16 bits.
11387
11388              legitimate_offset_address_p checks for the range
11389              [-0x8000,0x7fff] for mode size of 8 and [-0x8000,0x7ff7]
11390              for mode size of 16.  We wrap at [0x7ffc,0x7fff] and
11391              [0x7ff4,0x7fff] respectively, so test for the
11392              intersection of these ranges, [0x7ffc,0x7fff] and
11393              [0x7ff4,0x7ff7] respectively.
11394
11395              Note that the address we see here may have been
11396              manipulated by legitimize_reload_address.  */
11397           if (GET_CODE (addr) == LO_SUM
11398               ? ((offset & 0xffff) ^ 0x8000) >= 0x10000 - extra
11399               : offset - (0x8000 - extra) < UNITS_PER_WORD)
11400             {
11401               if (in_p)
11402                 sri->icode = CODE_FOR_reload_si_load;
11403               else
11404                 sri->icode = CODE_FOR_reload_si_store;
11405               sri->extra_cost = 2;
11406               ret = NO_REGS;
11407               done_p = true;
11408             }
11409           else
11410             default_p = true;
11411         }
11412       else
11413         default_p = true;
11414     }
11415
11416   if (!done_p)
11417     default_p = true;
11418
11419   if (default_p)
11420     ret = default_secondary_reload (in_p, x, rclass, mode, sri);
11421
11422   gcc_assert (ret != ALL_REGS);
11423
11424   if (TARGET_DEBUG_ADDR)
11425     {
11426       fprintf (stderr,
11427                "\nrs6000_secondary_reload, return %s, in_p = %s, rclass = %s, "
11428                "mode = %s",
11429                reg_class_names[ret],
11430                in_p ? "true" : "false",
11431                reg_class_names[rclass],
11432                GET_MODE_NAME (mode));
11433
11434       if (reload_completed)
11435         fputs (", after reload", stderr);
11436
11437       if (!done_p)
11438         fputs (", done_p not set", stderr);
11439
11440       if (default_p)
11441         fputs (", default secondary reload", stderr);
11442
11443       if (sri->icode != CODE_FOR_nothing)
11444         fprintf (stderr, ", reload func = %s, extra cost = %d",
11445                  insn_data[sri->icode].name, sri->extra_cost);
11446
11447       else if (sri->extra_cost > 0)
11448         fprintf (stderr, ", extra cost = %d", sri->extra_cost);
11449
11450       fputs ("\n", stderr);
11451       debug_rtx (x);
11452     }
11453
11454   return ret;
11455 }
11456
11457 /* Better tracing for rs6000_secondary_reload_inner.  */
11458
11459 static void
11460 rs6000_secondary_reload_trace (int line, rtx reg, rtx mem, rtx scratch,
11461                                bool store_p)
11462 {
11463   rtx set, clobber;
11464
11465   gcc_assert (reg != NULL_RTX && mem != NULL_RTX && scratch != NULL_RTX);
11466
11467   fprintf (stderr, "rs6000_secondary_reload_inner:%d, type = %s\n", line,
11468            store_p ? "store" : "load");
11469
11470   if (store_p)
11471     set = gen_rtx_SET (mem, reg);
11472   else
11473     set = gen_rtx_SET (reg, mem);
11474
11475   clobber = gen_rtx_CLOBBER (VOIDmode, scratch);
11476   debug_rtx (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, clobber)));
11477 }
11478
11479 static void rs6000_secondary_reload_fail (int, rtx, rtx, rtx, bool)
11480   ATTRIBUTE_NORETURN;
11481
11482 static void
11483 rs6000_secondary_reload_fail (int line, rtx reg, rtx mem, rtx scratch,
11484                               bool store_p)
11485 {
11486   rs6000_secondary_reload_trace (line, reg, mem, scratch, store_p);
11487   gcc_unreachable ();
11488 }
11489
11490 /* Fixup reload addresses for values in GPR, FPR, and VMX registers that have
11491    reload helper functions.  These were identified in
11492    rs6000_secondary_reload_memory, and if reload decided to use the secondary
11493    reload, it calls the insns:
11494         reload_<RELOAD:mode>_<P:mptrsize>_store
11495         reload_<RELOAD:mode>_<P:mptrsize>_load
11496
11497    which in turn calls this function, to do whatever is necessary to create
11498    valid addresses.  */
11499
11500 void
11501 rs6000_secondary_reload_inner (rtx reg, rtx mem, rtx scratch, bool store_p)
11502 {
11503   int regno = true_regnum (reg);
11504   machine_mode mode = GET_MODE (reg);
11505   addr_mask_type addr_mask;
11506   rtx addr;
11507   rtx new_addr;
11508   rtx op_reg, op0, op1;
11509   rtx and_op;
11510   rtx cc_clobber;
11511   rtvec rv;
11512
11513   if (regno < 0 || !HARD_REGISTER_NUM_P (regno) || !MEM_P (mem)
11514       || !base_reg_operand (scratch, GET_MODE (scratch)))
11515     rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11516
11517   if (IN_RANGE (regno, FIRST_GPR_REGNO, LAST_GPR_REGNO))
11518     addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_GPR];
11519
11520   else if (IN_RANGE (regno, FIRST_FPR_REGNO, LAST_FPR_REGNO))
11521     addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_FPR];
11522
11523   else if (IN_RANGE (regno, FIRST_ALTIVEC_REGNO, LAST_ALTIVEC_REGNO))
11524     addr_mask = reg_addr[mode].addr_mask[RELOAD_REG_VMX];
11525
11526   else
11527     rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11528
11529   /* Make sure the mode is valid in this register class.  */
11530   if ((addr_mask & RELOAD_REG_VALID) == 0)
11531     rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11532
11533   if (TARGET_DEBUG_ADDR)
11534     rs6000_secondary_reload_trace (__LINE__, reg, mem, scratch, store_p);
11535
11536   new_addr = addr = XEXP (mem, 0);
11537   switch (GET_CODE (addr))
11538     {
11539       /* Does the register class support auto update forms for this mode?  If
11540          not, do the update now.  We don't need a scratch register, since the
11541          powerpc only supports PRE_INC, PRE_DEC, and PRE_MODIFY.  */
11542     case PRE_INC:
11543     case PRE_DEC:
11544       op_reg = XEXP (addr, 0);
11545       if (!base_reg_operand (op_reg, Pmode))
11546         rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11547
11548       if ((addr_mask & RELOAD_REG_PRE_INCDEC) == 0)
11549         {
11550           int delta = GET_MODE_SIZE (mode);
11551           if (GET_CODE (addr) == PRE_DEC)
11552             delta = -delta;
11553           emit_insn (gen_add2_insn (op_reg, GEN_INT (delta)));
11554           new_addr = op_reg;
11555         }
11556       break;
11557
11558     case PRE_MODIFY:
11559       op0 = XEXP (addr, 0);
11560       op1 = XEXP (addr, 1);
11561       if (!base_reg_operand (op0, Pmode)
11562           || GET_CODE (op1) != PLUS
11563           || !rtx_equal_p (op0, XEXP (op1, 0)))
11564         rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11565
11566       if ((addr_mask & RELOAD_REG_PRE_MODIFY) == 0)
11567         {
11568           emit_insn (gen_rtx_SET (op0, op1));
11569           new_addr = reg;
11570         }
11571       break;
11572
11573       /* Do we need to simulate AND -16 to clear the bottom address bits used
11574          in VMX load/stores?  */
11575     case AND:
11576       op0 = XEXP (addr, 0);
11577       op1 = XEXP (addr, 1);
11578       if ((addr_mask & RELOAD_REG_AND_M16) == 0)
11579         {
11580           if (REG_P (op0) || SUBREG_P (op0))
11581             op_reg = op0;
11582
11583           else if (GET_CODE (op1) == PLUS)
11584             {
11585               emit_insn (gen_rtx_SET (scratch, op1));
11586               op_reg = scratch;
11587             }
11588
11589           else
11590             rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11591
11592           and_op = gen_rtx_AND (GET_MODE (scratch), op_reg, op1);
11593           cc_clobber = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (CCmode));
11594           rv = gen_rtvec (2, gen_rtx_SET (scratch, and_op), cc_clobber);
11595           emit_insn (gen_rtx_PARALLEL (VOIDmode, rv));
11596           new_addr = scratch;
11597         }
11598       break;
11599
11600       /* If this is an indirect address, make sure it is a base register.  */
11601     case REG:
11602     case SUBREG:
11603       if (!base_reg_operand (addr, GET_MODE (addr)))
11604         {
11605           emit_insn (gen_rtx_SET (scratch, addr));
11606           new_addr = scratch;
11607         }
11608       break;
11609
11610       /* If this is an indexed address, make sure the register class can handle
11611          indexed addresses for this mode.  */
11612     case PLUS:
11613       op0 = XEXP (addr, 0);
11614       op1 = XEXP (addr, 1);
11615       if (!base_reg_operand (op0, Pmode))
11616         rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11617
11618       else if (int_reg_operand (op1, Pmode))
11619         {
11620           if ((addr_mask & RELOAD_REG_INDEXED) == 0)
11621             {
11622               emit_insn (gen_rtx_SET (scratch, addr));
11623               new_addr = scratch;
11624             }
11625         }
11626
11627       else if (mode_supports_dq_form (mode) && CONST_INT_P (op1))
11628         {
11629           if (((addr_mask & RELOAD_REG_QUAD_OFFSET) == 0)
11630               || !quad_address_p (addr, mode, false))
11631             {
11632               emit_insn (gen_rtx_SET (scratch, addr));
11633               new_addr = scratch;
11634             }
11635         }
11636
11637       /* Make sure the register class can handle offset addresses.  */
11638       else if (rs6000_legitimate_offset_address_p (mode, addr, false, true))
11639         {
11640           if ((addr_mask & RELOAD_REG_OFFSET) == 0)
11641             {
11642               emit_insn (gen_rtx_SET (scratch, addr));
11643               new_addr = scratch;
11644             }
11645         }
11646
11647       else
11648         rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11649
11650       break;
11651
11652     case LO_SUM:
11653       op0 = XEXP (addr, 0);
11654       op1 = XEXP (addr, 1);
11655       if (!base_reg_operand (op0, Pmode))
11656         rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11657
11658       else if (int_reg_operand (op1, Pmode))
11659         {
11660           if ((addr_mask & RELOAD_REG_INDEXED) == 0)
11661             {
11662               emit_insn (gen_rtx_SET (scratch, addr));
11663               new_addr = scratch;
11664             }
11665         }
11666
11667       /* Quad offsets are restricted and can't handle normal addresses.  */
11668       else if (mode_supports_dq_form (mode))
11669         {
11670           emit_insn (gen_rtx_SET (scratch, addr));
11671           new_addr = scratch;
11672         }
11673
11674       /* Make sure the register class can handle offset addresses.  */
11675       else if (legitimate_lo_sum_address_p (mode, addr, false))
11676         {
11677           if ((addr_mask & RELOAD_REG_OFFSET) == 0)
11678             {
11679               emit_insn (gen_rtx_SET (scratch, addr));
11680               new_addr = scratch;
11681             }
11682         }
11683
11684       else
11685         rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11686
11687       break;
11688
11689     case SYMBOL_REF:
11690     case CONST:
11691     case LABEL_REF:
11692       rs6000_emit_move (scratch, addr, Pmode);
11693       new_addr = scratch;
11694       break;
11695
11696     default:
11697       rs6000_secondary_reload_fail (__LINE__, reg, mem, scratch, store_p);
11698     }
11699
11700   /* Adjust the address if it changed.  */
11701   if (addr != new_addr)
11702     {
11703       mem = replace_equiv_address_nv (mem, new_addr);
11704       if (TARGET_DEBUG_ADDR)
11705         fprintf (stderr, "\nrs6000_secondary_reload_inner, mem adjusted.\n");
11706     }
11707
11708   /* Now create the move.  */
11709   if (store_p)
11710     emit_insn (gen_rtx_SET (mem, reg));
11711   else
11712     emit_insn (gen_rtx_SET (reg, mem));
11713
11714   return;
11715 }
11716
11717 /* Convert reloads involving 64-bit gprs and misaligned offset
11718    addressing, or multiple 32-bit gprs and offsets that are too large,
11719    to use indirect addressing.  */
11720
11721 void
11722 rs6000_secondary_reload_gpr (rtx reg, rtx mem, rtx scratch, bool store_p)
11723 {
11724   int regno = true_regnum (reg);
11725   enum reg_class rclass;
11726   rtx addr;
11727   rtx scratch_or_premodify = scratch;
11728
11729   if (TARGET_DEBUG_ADDR)
11730     {
11731       fprintf (stderr, "\nrs6000_secondary_reload_gpr, type = %s\n",
11732                store_p ? "store" : "load");
11733       fprintf (stderr, "reg:\n");
11734       debug_rtx (reg);
11735       fprintf (stderr, "mem:\n");
11736       debug_rtx (mem);
11737       fprintf (stderr, "scratch:\n");
11738       debug_rtx (scratch);
11739     }
11740
11741   gcc_assert (regno >= 0 && HARD_REGISTER_NUM_P (regno));
11742   gcc_assert (MEM_P (mem));
11743   rclass = REGNO_REG_CLASS (regno);
11744   gcc_assert (rclass == GENERAL_REGS || rclass == BASE_REGS);
11745   addr = XEXP (mem, 0);
11746
11747   if (GET_CODE (addr) == PRE_MODIFY)
11748     {
11749       gcc_assert (REG_P (XEXP (addr, 0))
11750                   && GET_CODE (XEXP (addr, 1)) == PLUS
11751                   && XEXP (XEXP (addr, 1), 0) == XEXP (addr, 0));
11752       scratch_or_premodify = XEXP (addr, 0);
11753       addr = XEXP (addr, 1);
11754     }
11755   gcc_assert (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM);
11756
11757   rs6000_emit_move (scratch_or_premodify, addr, Pmode);
11758
11759   mem = replace_equiv_address_nv (mem, scratch_or_premodify);
11760
11761   /* Now create the move.  */
11762   if (store_p)
11763     emit_insn (gen_rtx_SET (mem, reg));
11764   else
11765     emit_insn (gen_rtx_SET (reg, mem));
11766
11767   return;
11768 }
11769
11770 /* Given an rtx X being reloaded into a reg required to be
11771    in class CLASS, return the class of reg to actually use.
11772    In general this is just CLASS; but on some machines
11773    in some cases it is preferable to use a more restrictive class.
11774
11775    On the RS/6000, we have to return NO_REGS when we want to reload a
11776    floating-point CONST_DOUBLE to force it to be copied to memory.
11777
11778    We also don't want to reload integer values into floating-point
11779    registers if we can at all help it.  In fact, this can
11780    cause reload to die, if it tries to generate a reload of CTR
11781    into a FP register and discovers it doesn't have the memory location
11782    required.
11783
11784    ??? Would it be a good idea to have reload do the converse, that is
11785    try to reload floating modes into FP registers if possible?
11786  */
11787
11788 static enum reg_class
11789 rs6000_preferred_reload_class (rtx x, enum reg_class rclass)
11790 {
11791   machine_mode mode = GET_MODE (x);
11792   bool is_constant = CONSTANT_P (x);
11793
11794   /* If a mode can't go in FPR/ALTIVEC/VSX registers, don't return a preferred
11795      reload class for it.  */
11796   if ((rclass == ALTIVEC_REGS || rclass == VSX_REGS)
11797       && (reg_addr[mode].addr_mask[RELOAD_REG_VMX] & RELOAD_REG_VALID) == 0)
11798     return NO_REGS;
11799
11800   if ((rclass == FLOAT_REGS || rclass == VSX_REGS)
11801       && (reg_addr[mode].addr_mask[RELOAD_REG_FPR] & RELOAD_REG_VALID) == 0)
11802     return NO_REGS;
11803
11804   /* For VSX, see if we should prefer FLOAT_REGS or ALTIVEC_REGS.  Do not allow
11805      the reloading of address expressions using PLUS into floating point
11806      registers.  */
11807   if (TARGET_VSX && VSX_REG_CLASS_P (rclass) && GET_CODE (x) != PLUS)
11808     {
11809       if (is_constant)
11810         {
11811           /* Zero is always allowed in all VSX registers.  */
11812           if (x == CONST0_RTX (mode))
11813             return rclass;
11814
11815           /* If this is a vector constant that can be formed with a few Altivec
11816              instructions, we want altivec registers.  */
11817           if (GET_CODE (x) == CONST_VECTOR && easy_vector_constant (x, mode))
11818             return ALTIVEC_REGS;
11819
11820           /* If this is an integer constant that can easily be loaded into
11821              vector registers, allow it.  */
11822           if (CONST_INT_P (x))
11823             {
11824               HOST_WIDE_INT value = INTVAL (x);
11825
11826               /* ISA 2.07 can generate -1 in all registers with XXLORC.  ISA
11827                  2.06 can generate it in the Altivec registers with
11828                  VSPLTI<x>.  */
11829               if (value == -1)
11830                 {
11831                   if (TARGET_P8_VECTOR)
11832                     return rclass;
11833                   else if (rclass == ALTIVEC_REGS || rclass == VSX_REGS)
11834                     return ALTIVEC_REGS;
11835                   else
11836                     return NO_REGS;
11837                 }
11838
11839               /* ISA 3.0 can load -128..127 using the XXSPLTIB instruction and
11840                  a sign extend in the Altivec registers.  */
11841               if (IN_RANGE (value, -128, 127) && TARGET_P9_VECTOR
11842                   && (rclass == ALTIVEC_REGS || rclass == VSX_REGS))
11843                 return ALTIVEC_REGS;
11844             }
11845
11846           /* Force constant to memory.  */
11847           return NO_REGS;
11848         }
11849
11850       /* D-form addressing can easily reload the value.  */
11851       if (mode_supports_vmx_dform (mode)
11852           || mode_supports_dq_form (mode))
11853         return rclass;
11854
11855       /* If this is a scalar floating point value and we don't have D-form
11856          addressing, prefer the traditional floating point registers so that we
11857          can use D-form (register+offset) addressing.  */
11858       if (rclass == VSX_REGS
11859           && (mode == SFmode || GET_MODE_SIZE (mode) == 8))
11860         return FLOAT_REGS;
11861
11862       /* Prefer the Altivec registers if Altivec is handling the vector
11863          operations (i.e. V16QI, V8HI, and V4SI), or if we prefer Altivec
11864          loads.  */
11865       if (VECTOR_UNIT_ALTIVEC_P (mode) || VECTOR_MEM_ALTIVEC_P (mode)
11866           || mode == V1TImode)
11867         return ALTIVEC_REGS;
11868
11869       return rclass;
11870     }
11871
11872   if (is_constant || GET_CODE (x) == PLUS)
11873     {
11874       if (reg_class_subset_p (GENERAL_REGS, rclass))
11875         return GENERAL_REGS;
11876       if (reg_class_subset_p (BASE_REGS, rclass))
11877         return BASE_REGS;
11878       return NO_REGS;
11879     }
11880
11881   if (GET_MODE_CLASS (mode) == MODE_INT && rclass == GEN_OR_FLOAT_REGS)
11882     return GENERAL_REGS;
11883
11884   return rclass;
11885 }
11886
11887 /* Debug version of rs6000_preferred_reload_class.  */
11888 static enum reg_class
11889 rs6000_debug_preferred_reload_class (rtx x, enum reg_class rclass)
11890 {
11891   enum reg_class ret = rs6000_preferred_reload_class (x, rclass);
11892
11893   fprintf (stderr,
11894            "\nrs6000_preferred_reload_class, return %s, rclass = %s, "
11895            "mode = %s, x:\n",
11896            reg_class_names[ret], reg_class_names[rclass],
11897            GET_MODE_NAME (GET_MODE (x)));
11898   debug_rtx (x);
11899
11900   return ret;
11901 }
11902
11903 /* If we are copying between FP or AltiVec registers and anything else, we need
11904    a memory location.  The exception is when we are targeting ppc64 and the
11905    move to/from fpr to gpr instructions are available.  Also, under VSX, you
11906    can copy vector registers from the FP register set to the Altivec register
11907    set and vice versa.  */
11908
11909 static bool
11910 rs6000_secondary_memory_needed (machine_mode mode,
11911                                 reg_class_t from_class,
11912                                 reg_class_t to_class)
11913 {
11914   enum rs6000_reg_type from_type, to_type;
11915   bool altivec_p = ((from_class == ALTIVEC_REGS)
11916                     || (to_class == ALTIVEC_REGS));
11917
11918   /* If a simple/direct move is available, we don't need secondary memory  */
11919   from_type = reg_class_to_reg_type[(int)from_class];
11920   to_type = reg_class_to_reg_type[(int)to_class];
11921
11922   if (rs6000_secondary_reload_move (to_type, from_type, mode,
11923                                     (secondary_reload_info *)0, altivec_p))
11924     return false;
11925
11926   /* If we have a floating point or vector register class, we need to use
11927      memory to transfer the data.  */
11928   if (IS_FP_VECT_REG_TYPE (from_type) || IS_FP_VECT_REG_TYPE (to_type))
11929     return true;
11930
11931   return false;
11932 }
11933
11934 /* Debug version of rs6000_secondary_memory_needed.  */
11935 static bool
11936 rs6000_debug_secondary_memory_needed (machine_mode mode,
11937                                       reg_class_t from_class,
11938                                       reg_class_t to_class)
11939 {
11940   bool ret = rs6000_secondary_memory_needed (mode, from_class, to_class);
11941
11942   fprintf (stderr,
11943            "rs6000_secondary_memory_needed, return: %s, from_class = %s, "
11944            "to_class = %s, mode = %s\n",
11945            ret ? "true" : "false",
11946            reg_class_names[from_class],
11947            reg_class_names[to_class],
11948            GET_MODE_NAME (mode));
11949
11950   return ret;
11951 }
11952
11953 /* Return the register class of a scratch register needed to copy IN into
11954    or out of a register in RCLASS in MODE.  If it can be done directly,
11955    NO_REGS is returned.  */
11956
11957 static enum reg_class
11958 rs6000_secondary_reload_class (enum reg_class rclass, machine_mode mode,
11959                                rtx in)
11960 {
11961   int regno;
11962
11963   if (TARGET_ELF || (DEFAULT_ABI == ABI_DARWIN
11964 #if TARGET_MACHO
11965                      && MACHOPIC_INDIRECT
11966 #endif
11967                      ))
11968     {
11969       /* We cannot copy a symbolic operand directly into anything
11970          other than BASE_REGS for TARGET_ELF.  So indicate that a
11971          register from BASE_REGS is needed as an intermediate
11972          register.
11973
11974          On Darwin, pic addresses require a load from memory, which
11975          needs a base register.  */
11976       if (rclass != BASE_REGS
11977           && (SYMBOL_REF_P (in)
11978               || GET_CODE (in) == HIGH
11979               || GET_CODE (in) == LABEL_REF
11980               || GET_CODE (in) == CONST))
11981         return BASE_REGS;
11982     }
11983
11984   if (REG_P (in))
11985     {
11986       regno = REGNO (in);
11987       if (!HARD_REGISTER_NUM_P (regno))
11988         {
11989           regno = true_regnum (in);
11990           if (!HARD_REGISTER_NUM_P (regno))
11991             regno = -1;
11992         }
11993     }
11994   else if (SUBREG_P (in))
11995     {
11996       regno = true_regnum (in);
11997       if (!HARD_REGISTER_NUM_P (regno))
11998         regno = -1;
11999     }
12000   else
12001     regno = -1;
12002
12003   /* If we have VSX register moves, prefer moving scalar values between
12004      Altivec registers and GPR by going via an FPR (and then via memory)
12005      instead of reloading the secondary memory address for Altivec moves.  */
12006   if (TARGET_VSX
12007       && GET_MODE_SIZE (mode) < 16
12008       && !mode_supports_vmx_dform (mode)
12009       && (((rclass == GENERAL_REGS || rclass == BASE_REGS)
12010            && (regno >= 0 && ALTIVEC_REGNO_P (regno)))
12011           || ((rclass == VSX_REGS || rclass == ALTIVEC_REGS)
12012               && (regno >= 0 && INT_REGNO_P (regno)))))
12013     return FLOAT_REGS;
12014
12015   /* We can place anything into GENERAL_REGS and can put GENERAL_REGS
12016      into anything.  */
12017   if (rclass == GENERAL_REGS || rclass == BASE_REGS
12018       || (regno >= 0 && INT_REGNO_P (regno)))
12019     return NO_REGS;
12020
12021   /* Constants, memory, and VSX registers can go into VSX registers (both the
12022      traditional floating point and the altivec registers).  */
12023   if (rclass == VSX_REGS
12024       && (regno == -1 || VSX_REGNO_P (regno)))
12025     return NO_REGS;
12026
12027   /* Constants, memory, and FP registers can go into FP registers.  */
12028   if ((regno == -1 || FP_REGNO_P (regno))
12029       && (rclass == FLOAT_REGS || rclass == GEN_OR_FLOAT_REGS))
12030     return (mode != SDmode || lra_in_progress) ? NO_REGS : GENERAL_REGS;
12031
12032   /* Memory, and AltiVec registers can go into AltiVec registers.  */
12033   if ((regno == -1 || ALTIVEC_REGNO_P (regno))
12034       && rclass == ALTIVEC_REGS)
12035     return NO_REGS;
12036
12037   /* We can copy among the CR registers.  */
12038   if ((rclass == CR_REGS || rclass == CR0_REGS)
12039       && regno >= 0 && CR_REGNO_P (regno))
12040     return NO_REGS;
12041
12042   /* Otherwise, we need GENERAL_REGS.  */
12043   return GENERAL_REGS;
12044 }
12045
12046 /* Debug version of rs6000_secondary_reload_class.  */
12047 static enum reg_class
12048 rs6000_debug_secondary_reload_class (enum reg_class rclass,
12049                                      machine_mode mode, rtx in)
12050 {
12051   enum reg_class ret = rs6000_secondary_reload_class (rclass, mode, in);
12052   fprintf (stderr,
12053            "\nrs6000_secondary_reload_class, return %s, rclass = %s, "
12054            "mode = %s, input rtx:\n",
12055            reg_class_names[ret], reg_class_names[rclass],
12056            GET_MODE_NAME (mode));
12057   debug_rtx (in);
12058
12059   return ret;
12060 }
12061
12062 /* Implement TARGET_CAN_CHANGE_MODE_CLASS.  */
12063
12064 static bool
12065 rs6000_can_change_mode_class (machine_mode from,
12066                               machine_mode to,
12067                               reg_class_t rclass)
12068 {
12069   unsigned from_size = GET_MODE_SIZE (from);
12070   unsigned to_size = GET_MODE_SIZE (to);
12071
12072   if (from_size != to_size)
12073     {
12074       enum reg_class xclass = (TARGET_VSX) ? VSX_REGS : FLOAT_REGS;
12075
12076       if (reg_classes_intersect_p (xclass, rclass))
12077         {
12078           unsigned to_nregs = hard_regno_nregs (FIRST_FPR_REGNO, to);
12079           unsigned from_nregs = hard_regno_nregs (FIRST_FPR_REGNO, from);
12080           bool to_float128_vector_p = FLOAT128_VECTOR_P (to);
12081           bool from_float128_vector_p = FLOAT128_VECTOR_P (from);
12082
12083           /* Don't allow 64-bit types to overlap with 128-bit types that take a
12084              single register under VSX because the scalar part of the register
12085              is in the upper 64-bits, and not the lower 64-bits.  Types like
12086              TFmode/TDmode that take 2 scalar register can overlap.  128-bit
12087              IEEE floating point can't overlap, and neither can small
12088              values.  */
12089
12090           if (to_float128_vector_p && from_float128_vector_p)
12091             return true;
12092
12093           else if (to_float128_vector_p || from_float128_vector_p)
12094             return false;
12095
12096           /* TDmode in floating-mode registers must always go into a register
12097              pair with the most significant word in the even-numbered register
12098              to match ISA requirements.  In little-endian mode, this does not
12099              match subreg numbering, so we cannot allow subregs.  */
12100           if (!BYTES_BIG_ENDIAN && (to == TDmode || from == TDmode))
12101             return false;
12102
12103           if (from_size < 8 || to_size < 8)
12104             return false;
12105
12106           if (from_size == 8 && (8 * to_nregs) != to_size)
12107             return false;
12108
12109           if (to_size == 8 && (8 * from_nregs) != from_size)
12110             return false;
12111
12112           return true;
12113         }
12114       else
12115         return true;
12116     }
12117
12118   /* Since the VSX register set includes traditional floating point registers
12119      and altivec registers, just check for the size being different instead of
12120      trying to check whether the modes are vector modes.  Otherwise it won't
12121      allow say DF and DI to change classes.  For types like TFmode and TDmode
12122      that take 2 64-bit registers, rather than a single 128-bit register, don't
12123      allow subregs of those types to other 128 bit types.  */
12124   if (TARGET_VSX && VSX_REG_CLASS_P (rclass))
12125     {
12126       unsigned num_regs = (from_size + 15) / 16;
12127       if (hard_regno_nregs (FIRST_FPR_REGNO, to) > num_regs
12128           || hard_regno_nregs (FIRST_FPR_REGNO, from) > num_regs)
12129         return false;
12130
12131       return (from_size == 8 || from_size == 16);
12132     }
12133
12134   if (TARGET_ALTIVEC && rclass == ALTIVEC_REGS
12135       && (ALTIVEC_VECTOR_MODE (from) + ALTIVEC_VECTOR_MODE (to)) == 1)
12136     return false;
12137
12138   return true;
12139 }
12140
12141 /* Debug version of rs6000_can_change_mode_class.  */
12142 static bool
12143 rs6000_debug_can_change_mode_class (machine_mode from,
12144                                     machine_mode to,
12145                                     reg_class_t rclass)
12146 {
12147   bool ret = rs6000_can_change_mode_class (from, to, rclass);
12148
12149   fprintf (stderr,
12150            "rs6000_can_change_mode_class, return %s, from = %s, "
12151            "to = %s, rclass = %s\n",
12152            ret ? "true" : "false",
12153            GET_MODE_NAME (from), GET_MODE_NAME (to),
12154            reg_class_names[rclass]);
12155
12156   return ret;
12157 }
12158 \f
12159 /* Return a string to do a move operation of 128 bits of data.  */
12160
12161 const char *
12162 rs6000_output_move_128bit (rtx operands[])
12163 {
12164   rtx dest = operands[0];
12165   rtx src = operands[1];
12166   machine_mode mode = GET_MODE (dest);
12167   int dest_regno;
12168   int src_regno;
12169   bool dest_gpr_p, dest_fp_p, dest_vmx_p, dest_vsx_p;
12170   bool src_gpr_p, src_fp_p, src_vmx_p, src_vsx_p;
12171
12172   if (REG_P (dest))
12173     {
12174       dest_regno = REGNO (dest);
12175       dest_gpr_p = INT_REGNO_P (dest_regno);
12176       dest_fp_p = FP_REGNO_P (dest_regno);
12177       dest_vmx_p = ALTIVEC_REGNO_P (dest_regno);
12178       dest_vsx_p = dest_fp_p | dest_vmx_p;
12179     }
12180   else
12181     {
12182       dest_regno = -1;
12183       dest_gpr_p = dest_fp_p = dest_vmx_p = dest_vsx_p = false;
12184     }
12185
12186   if (REG_P (src))
12187     {
12188       src_regno = REGNO (src);
12189       src_gpr_p = INT_REGNO_P (src_regno);
12190       src_fp_p = FP_REGNO_P (src_regno);
12191       src_vmx_p = ALTIVEC_REGNO_P (src_regno);
12192       src_vsx_p = src_fp_p | src_vmx_p;
12193     }
12194   else
12195     {
12196       src_regno = -1;
12197       src_gpr_p = src_fp_p = src_vmx_p = src_vsx_p = false;
12198     }
12199
12200   /* Register moves.  */
12201   if (dest_regno >= 0 && src_regno >= 0)
12202     {
12203       if (dest_gpr_p)
12204         {
12205           if (src_gpr_p)
12206             return "#";
12207
12208           if (TARGET_DIRECT_MOVE_128 && src_vsx_p)
12209             return (WORDS_BIG_ENDIAN
12210                     ? "mfvsrd %0,%x1\n\tmfvsrld %L0,%x1"
12211                     : "mfvsrd %L0,%x1\n\tmfvsrld %0,%x1");
12212
12213           else if (TARGET_VSX && TARGET_DIRECT_MOVE && src_vsx_p)
12214             return "#";
12215         }
12216
12217       else if (TARGET_VSX && dest_vsx_p)
12218         {
12219           if (src_vsx_p)
12220             return "xxlor %x0,%x1,%x1";
12221
12222           else if (TARGET_DIRECT_MOVE_128 && src_gpr_p)
12223             return (WORDS_BIG_ENDIAN
12224                     ? "mtvsrdd %x0,%1,%L1"
12225                     : "mtvsrdd %x0,%L1,%1");
12226
12227           else if (TARGET_DIRECT_MOVE && src_gpr_p)
12228             return "#";
12229         }
12230
12231       else if (TARGET_ALTIVEC && dest_vmx_p && src_vmx_p)
12232         return "vor %0,%1,%1";
12233
12234       else if (dest_fp_p && src_fp_p)
12235         return "#";
12236     }
12237
12238   /* Loads.  */
12239   else if (dest_regno >= 0 && MEM_P (src))
12240     {
12241       if (dest_gpr_p)
12242         {
12243           if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
12244             return "lq %0,%1";
12245           else
12246             return "#";
12247         }
12248
12249       else if (TARGET_ALTIVEC && dest_vmx_p
12250                && altivec_indexed_or_indirect_operand (src, mode))
12251         return "lvx %0,%y1";
12252
12253       else if (TARGET_VSX && dest_vsx_p)
12254         {
12255           if (mode_supports_dq_form (mode)
12256               && quad_address_p (XEXP (src, 0), mode, true))
12257             return "lxv %x0,%1";
12258
12259           else if (TARGET_P9_VECTOR)
12260             return "lxvx %x0,%y1";
12261
12262           else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
12263             return "lxvw4x %x0,%y1";
12264
12265           else
12266             return "lxvd2x %x0,%y1";
12267         }
12268
12269       else if (TARGET_ALTIVEC && dest_vmx_p)
12270         return "lvx %0,%y1";
12271
12272       else if (dest_fp_p)
12273         return "#";
12274     }
12275
12276   /* Stores.  */
12277   else if (src_regno >= 0 && MEM_P (dest))
12278     {
12279       if (src_gpr_p)
12280         {
12281           if (TARGET_QUAD_MEMORY && quad_load_store_p (dest, src))
12282             return "stq %1,%0";
12283           else
12284             return "#";
12285         }
12286
12287       else if (TARGET_ALTIVEC && src_vmx_p
12288                && altivec_indexed_or_indirect_operand (dest, mode))
12289         return "stvx %1,%y0";
12290
12291       else if (TARGET_VSX && src_vsx_p)
12292         {
12293           if (mode_supports_dq_form (mode)
12294               && quad_address_p (XEXP (dest, 0), mode, true))
12295             return "stxv %x1,%0";
12296
12297           else if (TARGET_P9_VECTOR)
12298             return "stxvx %x1,%y0";
12299
12300           else if (mode == V16QImode || mode == V8HImode || mode == V4SImode)
12301             return "stxvw4x %x1,%y0";
12302
12303           else
12304             return "stxvd2x %x1,%y0";
12305         }
12306
12307       else if (TARGET_ALTIVEC && src_vmx_p)
12308         return "stvx %1,%y0";
12309
12310       else if (src_fp_p)
12311         return "#";
12312     }
12313
12314   /* Constants.  */
12315   else if (dest_regno >= 0
12316            && (CONST_INT_P (src)
12317                || CONST_WIDE_INT_P (src)
12318                || CONST_DOUBLE_P (src)
12319                || GET_CODE (src) == CONST_VECTOR))
12320     {
12321       if (dest_gpr_p)
12322         return "#";
12323
12324       else if ((dest_vmx_p && TARGET_ALTIVEC)
12325                || (dest_vsx_p && TARGET_VSX))
12326         return output_vec_const_move (operands);
12327     }
12328
12329   fatal_insn ("Bad 128-bit move", gen_rtx_SET (dest, src));
12330 }
12331
12332 /* Validate a 128-bit move.  */
12333 bool
12334 rs6000_move_128bit_ok_p (rtx operands[])
12335 {
12336   machine_mode mode = GET_MODE (operands[0]);
12337   return (gpc_reg_operand (operands[0], mode)
12338           || gpc_reg_operand (operands[1], mode));
12339 }
12340
12341 /* Return true if a 128-bit move needs to be split.  */
12342 bool
12343 rs6000_split_128bit_ok_p (rtx operands[])
12344 {
12345   if (!reload_completed)
12346     return false;
12347
12348   if (!gpr_or_gpr_p (operands[0], operands[1]))
12349     return false;
12350
12351   if (quad_load_store_p (operands[0], operands[1]))
12352     return false;
12353
12354   return true;
12355 }
12356
12357 \f
12358 /* Given a comparison operation, return the bit number in CCR to test.  We
12359    know this is a valid comparison.
12360
12361    SCC_P is 1 if this is for an scc.  That means that %D will have been
12362    used instead of %C, so the bits will be in different places.
12363
12364    Return -1 if OP isn't a valid comparison for some reason.  */
12365
12366 int
12367 ccr_bit (rtx op, int scc_p)
12368 {
12369   enum rtx_code code = GET_CODE (op);
12370   machine_mode cc_mode;
12371   int cc_regnum;
12372   int base_bit;
12373   rtx reg;
12374
12375   if (!COMPARISON_P (op))
12376     return -1;
12377
12378   reg = XEXP (op, 0);
12379
12380   if (!REG_P (reg) || !CR_REGNO_P (REGNO (reg)))
12381     return -1;
12382
12383   cc_mode = GET_MODE (reg);
12384   cc_regnum = REGNO (reg);
12385   base_bit = 4 * (cc_regnum - CR0_REGNO);
12386
12387   validate_condition_mode (code, cc_mode);
12388
12389   /* When generating a sCOND operation, only positive conditions are
12390      allowed.  */
12391   if (scc_p)
12392     switch (code)
12393       {
12394       case EQ:
12395       case GT:
12396       case LT:
12397       case UNORDERED:
12398       case GTU:
12399       case LTU:
12400         break;
12401       default:
12402         return -1;
12403       }
12404
12405   switch (code)
12406     {
12407     case NE:
12408       return scc_p ? base_bit + 3 : base_bit + 2;
12409     case EQ:
12410       return base_bit + 2;
12411     case GT:  case GTU:  case UNLE:
12412       return base_bit + 1;
12413     case LT:  case LTU:  case UNGE:
12414       return base_bit;
12415     case ORDERED:  case UNORDERED:
12416       return base_bit + 3;
12417
12418     case GE:  case GEU:
12419       /* If scc, we will have done a cror to put the bit in the
12420          unordered position.  So test that bit.  For integer, this is ! LT
12421          unless this is an scc insn.  */
12422       return scc_p ? base_bit + 3 : base_bit;
12423
12424     case LE:  case LEU:
12425       return scc_p ? base_bit + 3 : base_bit + 1;
12426
12427     default:
12428       return -1;
12429     }
12430 }
12431 \f
12432 /* Return the GOT register.  */
12433
12434 rtx
12435 rs6000_got_register (rtx value ATTRIBUTE_UNUSED)
12436 {
12437   /* The second flow pass currently (June 1999) can't update
12438      regs_ever_live without disturbing other parts of the compiler, so
12439      update it here to make the prolog/epilogue code happy.  */
12440   if (!can_create_pseudo_p ()
12441       && !df_regs_ever_live_p (RS6000_PIC_OFFSET_TABLE_REGNUM))
12442     df_set_regs_ever_live (RS6000_PIC_OFFSET_TABLE_REGNUM, true);
12443
12444   crtl->uses_pic_offset_table = 1;
12445
12446   return pic_offset_table_rtx;
12447 }
12448 \f
12449 #define INT_P(X) (CONST_INT_P (X) && GET_MODE (X) == VOIDmode)
12450
12451 /* Write out a function code label.  */
12452
12453 void
12454 rs6000_output_function_entry (FILE *file, const char *fname)
12455 {
12456   if (fname[0] != '.')
12457     {
12458       switch (DEFAULT_ABI)
12459         {
12460         default:
12461           gcc_unreachable ();
12462
12463         case ABI_AIX:
12464           if (DOT_SYMBOLS)
12465             putc ('.', file);
12466           else
12467             ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "L.");
12468           break;
12469
12470         case ABI_ELFv2:
12471         case ABI_V4:
12472         case ABI_DARWIN:
12473           break;
12474         }
12475     }
12476
12477   RS6000_OUTPUT_BASENAME (file, fname);
12478 }
12479
12480 /* Print an operand.  Recognize special options, documented below.  */
12481
12482 #if TARGET_ELF
12483 /* Access to .sdata2 through r2 (see -msdata=eabi in invoke.texi) is
12484    only introduced by the linker, when applying the sda21
12485    relocation.  */
12486 #define SMALL_DATA_RELOC ((rs6000_sdata == SDATA_EABI) ? "sda21" : "sdarel")
12487 #define SMALL_DATA_REG ((rs6000_sdata == SDATA_EABI) ? 0 : 13)
12488 #else
12489 #define SMALL_DATA_RELOC "sda21"
12490 #define SMALL_DATA_REG 0
12491 #endif
12492
12493 void
12494 print_operand (FILE *file, rtx x, int code)
12495 {
12496   int i;
12497   unsigned HOST_WIDE_INT uval;
12498
12499   switch (code)
12500     {
12501       /* %a is output_address.  */
12502
12503       /* %c is output_addr_const if a CONSTANT_ADDRESS_P, otherwise
12504          output_operand.  */
12505
12506     case 'D':
12507       /* Like 'J' but get to the GT bit only.  */
12508       if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
12509         {
12510           output_operand_lossage ("invalid %%D value");
12511           return;
12512         }
12513
12514       /* Bit 1 is GT bit.  */
12515       i = 4 * (REGNO (x) - CR0_REGNO) + 1;
12516
12517       /* Add one for shift count in rlinm for scc.  */
12518       fprintf (file, "%d", i + 1);
12519       return;
12520
12521     case 'e':
12522       /* If the low 16 bits are 0, but some other bit is set, write 's'.  */
12523       if (! INT_P (x))
12524         {
12525           output_operand_lossage ("invalid %%e value");
12526           return;
12527         }
12528
12529       uval = INTVAL (x);
12530       if ((uval & 0xffff) == 0 && uval != 0)
12531         putc ('s', file);
12532       return;
12533
12534     case 'E':
12535       /* X is a CR register.  Print the number of the EQ bit of the CR */
12536       if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
12537         output_operand_lossage ("invalid %%E value");
12538       else
12539         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO) + 2);
12540       return;
12541
12542     case 'f':
12543       /* X is a CR register.  Print the shift count needed to move it
12544          to the high-order four bits.  */
12545       if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
12546         output_operand_lossage ("invalid %%f value");
12547       else
12548         fprintf (file, "%d", 4 * (REGNO (x) - CR0_REGNO));
12549       return;
12550
12551     case 'F':
12552       /* Similar, but print the count for the rotate in the opposite
12553          direction.  */
12554       if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
12555         output_operand_lossage ("invalid %%F value");
12556       else
12557         fprintf (file, "%d", 32 - 4 * (REGNO (x) - CR0_REGNO));
12558       return;
12559
12560     case 'G':
12561       /* X is a constant integer.  If it is negative, print "m",
12562          otherwise print "z".  This is to make an aze or ame insn.  */
12563       if (!CONST_INT_P (x))
12564         output_operand_lossage ("invalid %%G value");
12565       else if (INTVAL (x) >= 0)
12566         putc ('z', file);
12567       else
12568         putc ('m', file);
12569       return;
12570
12571     case 'h':
12572       /* If constant, output low-order five bits.  Otherwise, write
12573          normally.  */
12574       if (INT_P (x))
12575         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 31);
12576       else
12577         print_operand (file, x, 0);
12578       return;
12579
12580     case 'H':
12581       /* If constant, output low-order six bits.  Otherwise, write
12582          normally.  */
12583       if (INT_P (x))
12584         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 63);
12585       else
12586         print_operand (file, x, 0);
12587       return;
12588
12589     case 'I':
12590       /* Print `i' if this is a constant, else nothing.  */
12591       if (INT_P (x))
12592         putc ('i', file);
12593       return;
12594
12595     case 'j':
12596       /* Write the bit number in CCR for jump.  */
12597       i = ccr_bit (x, 0);
12598       if (i == -1)
12599         output_operand_lossage ("invalid %%j code");
12600       else
12601         fprintf (file, "%d", i);
12602       return;
12603
12604     case 'J':
12605       /* Similar, but add one for shift count in rlinm for scc and pass
12606          scc flag to `ccr_bit'.  */
12607       i = ccr_bit (x, 1);
12608       if (i == -1)
12609         output_operand_lossage ("invalid %%J code");
12610       else
12611         /* If we want bit 31, write a shift count of zero, not 32.  */
12612         fprintf (file, "%d", i == 31 ? 0 : i + 1);
12613       return;
12614
12615     case 'k':
12616       /* X must be a constant.  Write the 1's complement of the
12617          constant.  */
12618       if (! INT_P (x))
12619         output_operand_lossage ("invalid %%k value");
12620       else
12621         fprintf (file, HOST_WIDE_INT_PRINT_DEC, ~ INTVAL (x));
12622       return;
12623
12624     case 'K':
12625       /* X must be a symbolic constant on ELF.  Write an
12626          expression suitable for an 'addi' that adds in the low 16
12627          bits of the MEM.  */
12628       if (GET_CODE (x) == CONST)
12629         {
12630           if (GET_CODE (XEXP (x, 0)) != PLUS
12631               || (!SYMBOL_REF_P (XEXP (XEXP (x, 0), 0))
12632                   && GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
12633               || !CONST_INT_P (XEXP (XEXP (x, 0), 1)))
12634             output_operand_lossage ("invalid %%K value");
12635         }
12636       print_operand_address (file, x);
12637       fputs ("@l", file);
12638       return;
12639
12640       /* %l is output_asm_label.  */
12641
12642     case 'L':
12643       /* Write second word of DImode or DFmode reference.  Works on register
12644          or non-indexed memory only.  */
12645       if (REG_P (x))
12646         fputs (reg_names[REGNO (x) + 1], file);
12647       else if (MEM_P (x))
12648         {
12649           machine_mode mode = GET_MODE (x);
12650           /* Handle possible auto-increment.  Since it is pre-increment and
12651              we have already done it, we can just use an offset of word.  */
12652           if (GET_CODE (XEXP (x, 0)) == PRE_INC
12653               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
12654             output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
12655                                                  UNITS_PER_WORD));
12656           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
12657             output_address (mode, plus_constant (Pmode, XEXP (XEXP (x, 0), 0),
12658                                                  UNITS_PER_WORD));
12659           else
12660             output_address (mode, XEXP (adjust_address_nv (x, SImode,
12661                                                            UNITS_PER_WORD),
12662                                   0));
12663
12664           if (small_data_operand (x, GET_MODE (x)))
12665             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
12666                      reg_names[SMALL_DATA_REG]);
12667         }
12668       return;
12669
12670     case 'N': /* Unused */
12671       /* Write the number of elements in the vector times 4.  */
12672       if (GET_CODE (x) != PARALLEL)
12673         output_operand_lossage ("invalid %%N value");
12674       else
12675         fprintf (file, "%d", XVECLEN (x, 0) * 4);
12676       return;
12677
12678     case 'O': /* Unused */
12679       /* Similar, but subtract 1 first.  */
12680       if (GET_CODE (x) != PARALLEL)
12681         output_operand_lossage ("invalid %%O value");
12682       else
12683         fprintf (file, "%d", (XVECLEN (x, 0) - 1) * 4);
12684       return;
12685
12686     case 'p':
12687       /* X is a CONST_INT that is a power of two.  Output the logarithm.  */
12688       if (! INT_P (x)
12689           || INTVAL (x) < 0
12690           || (i = exact_log2 (INTVAL (x))) < 0)
12691         output_operand_lossage ("invalid %%p value");
12692       else
12693         fprintf (file, "%d", i);
12694       return;
12695
12696     case 'P':
12697       /* The operand must be an indirect memory reference.  The result
12698          is the register name.  */
12699       if (!MEM_P (x) || !REG_P (XEXP (x, 0))
12700           || REGNO (XEXP (x, 0)) >= 32)
12701         output_operand_lossage ("invalid %%P value");
12702       else
12703         fputs (reg_names[REGNO (XEXP (x, 0))], file);
12704       return;
12705
12706     case 'q':
12707       /* This outputs the logical code corresponding to a boolean
12708          expression.  The expression may have one or both operands
12709          negated (if one, only the first one).  For condition register
12710          logical operations, it will also treat the negated
12711          CR codes as NOTs, but not handle NOTs of them.  */
12712       {
12713         const char *const *t = 0;
12714         const char *s;
12715         enum rtx_code code = GET_CODE (x);
12716         static const char * const tbl[3][3] = {
12717           { "and", "andc", "nor" },
12718           { "or", "orc", "nand" },
12719           { "xor", "eqv", "xor" } };
12720
12721         if (code == AND)
12722           t = tbl[0];
12723         else if (code == IOR)
12724           t = tbl[1];
12725         else if (code == XOR)
12726           t = tbl[2];
12727         else
12728           output_operand_lossage ("invalid %%q value");
12729
12730         if (GET_CODE (XEXP (x, 0)) != NOT)
12731           s = t[0];
12732         else
12733           {
12734             if (GET_CODE (XEXP (x, 1)) == NOT)
12735               s = t[2];
12736             else
12737               s = t[1];
12738           }
12739
12740         fputs (s, file);
12741       }
12742       return;
12743
12744     case 'Q':
12745       if (! TARGET_MFCRF)
12746         return;
12747       fputc (',', file);
12748       /* FALLTHRU */
12749
12750     case 'R':
12751       /* X is a CR register.  Print the mask for `mtcrf'.  */
12752       if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
12753         output_operand_lossage ("invalid %%R value");
12754       else
12755         fprintf (file, "%d", 128 >> (REGNO (x) - CR0_REGNO));
12756       return;
12757
12758     case 's':
12759       /* Low 5 bits of 32 - value */
12760       if (! INT_P (x))
12761         output_operand_lossage ("invalid %%s value");
12762       else
12763         fprintf (file, HOST_WIDE_INT_PRINT_DEC, (32 - INTVAL (x)) & 31);
12764       return;
12765
12766     case 't':
12767       /* Like 'J' but get to the OVERFLOW/UNORDERED bit.  */
12768       if (!REG_P (x) || !CR_REGNO_P (REGNO (x)))
12769         {
12770           output_operand_lossage ("invalid %%t value");
12771           return;
12772         }
12773
12774       /* Bit 3 is OV bit.  */
12775       i = 4 * (REGNO (x) - CR0_REGNO) + 3;
12776
12777       /* If we want bit 31, write a shift count of zero, not 32.  */
12778       fprintf (file, "%d", i == 31 ? 0 : i + 1);
12779       return;
12780
12781     case 'T':
12782       /* Print the symbolic name of a branch target register.  */
12783       if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PLTSEQ)
12784         x = XVECEXP (x, 0, 0);
12785       if (!REG_P (x) || (REGNO (x) != LR_REGNO
12786                          && REGNO (x) != CTR_REGNO))
12787         output_operand_lossage ("invalid %%T value");
12788       else if (REGNO (x) == LR_REGNO)
12789         fputs ("lr", file);
12790       else
12791         fputs ("ctr", file);
12792       return;
12793
12794     case 'u':
12795       /* High-order or low-order 16 bits of constant, whichever is non-zero,
12796          for use in unsigned operand.  */
12797       if (! INT_P (x))
12798         {
12799           output_operand_lossage ("invalid %%u value");
12800           return;
12801         }
12802
12803       uval = INTVAL (x);
12804       if ((uval & 0xffff) == 0)
12805         uval >>= 16;
12806
12807       fprintf (file, HOST_WIDE_INT_PRINT_HEX, uval & 0xffff);
12808       return;
12809
12810     case 'v':
12811       /* High-order 16 bits of constant for use in signed operand.  */
12812       if (! INT_P (x))
12813         output_operand_lossage ("invalid %%v value");
12814       else
12815         fprintf (file, HOST_WIDE_INT_PRINT_HEX,
12816                  (INTVAL (x) >> 16) & 0xffff);
12817       return;
12818
12819     case 'U':
12820       /* Print `u' if this has an auto-increment or auto-decrement.  */
12821       if (MEM_P (x)
12822           && (GET_CODE (XEXP (x, 0)) == PRE_INC
12823               || GET_CODE (XEXP (x, 0)) == PRE_DEC
12824               || GET_CODE (XEXP (x, 0)) == PRE_MODIFY))
12825         putc ('u', file);
12826       return;
12827
12828     case 'V':
12829       /* Print the trap code for this operand.  */
12830       switch (GET_CODE (x))
12831         {
12832         case EQ:
12833           fputs ("eq", file);   /* 4 */
12834           break;
12835         case NE:
12836           fputs ("ne", file);   /* 24 */
12837           break;
12838         case LT:
12839           fputs ("lt", file);   /* 16 */
12840           break;
12841         case LE:
12842           fputs ("le", file);   /* 20 */
12843           break;
12844         case GT:
12845           fputs ("gt", file);   /* 8 */
12846           break;
12847         case GE:
12848           fputs ("ge", file);   /* 12 */
12849           break;
12850         case LTU:
12851           fputs ("llt", file);  /* 2 */
12852           break;
12853         case LEU:
12854           fputs ("lle", file);  /* 6 */
12855           break;
12856         case GTU:
12857           fputs ("lgt", file);  /* 1 */
12858           break;
12859         case GEU:
12860           fputs ("lge", file);  /* 5 */
12861           break;
12862         default:
12863           output_operand_lossage ("invalid %%V value");
12864         }
12865       break;
12866
12867     case 'w':
12868       /* If constant, low-order 16 bits of constant, signed.  Otherwise, write
12869          normally.  */
12870       if (INT_P (x))
12871         fprintf (file, HOST_WIDE_INT_PRINT_DEC,
12872                  ((INTVAL (x) & 0xffff) ^ 0x8000) - 0x8000);
12873       else
12874         print_operand (file, x, 0);
12875       return;
12876
12877     case 'x':
12878       /* X is a FPR or Altivec register used in a VSX context.  */
12879       if (!REG_P (x) || !VSX_REGNO_P (REGNO (x)))
12880         output_operand_lossage ("invalid %%x value");
12881       else
12882         {
12883           int reg = REGNO (x);
12884           int vsx_reg = (FP_REGNO_P (reg)
12885                          ? reg - 32
12886                          : reg - FIRST_ALTIVEC_REGNO + 32);
12887
12888 #ifdef TARGET_REGNAMES      
12889           if (TARGET_REGNAMES)
12890             fprintf (file, "%%vs%d", vsx_reg);
12891           else
12892 #endif
12893             fprintf (file, "%d", vsx_reg);
12894         }
12895       return;
12896
12897     case 'X':
12898       if (MEM_P (x)
12899           && (legitimate_indexed_address_p (XEXP (x, 0), 0)
12900               || (GET_CODE (XEXP (x, 0)) == PRE_MODIFY
12901                   && legitimate_indexed_address_p (XEXP (XEXP (x, 0), 1), 0))))
12902         putc ('x', file);
12903       return;
12904
12905     case 'Y':
12906       /* Like 'L', for third word of TImode/PTImode  */
12907       if (REG_P (x))
12908         fputs (reg_names[REGNO (x) + 2], file);
12909       else if (MEM_P (x))
12910         {
12911           machine_mode mode = GET_MODE (x);
12912           if (GET_CODE (XEXP (x, 0)) == PRE_INC
12913               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
12914             output_address (mode, plus_constant (Pmode,
12915                                                  XEXP (XEXP (x, 0), 0), 8));
12916           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
12917             output_address (mode, plus_constant (Pmode,
12918                                                  XEXP (XEXP (x, 0), 0), 8));
12919           else
12920             output_address (mode, XEXP (adjust_address_nv (x, SImode, 8), 0));
12921           if (small_data_operand (x, GET_MODE (x)))
12922             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
12923                      reg_names[SMALL_DATA_REG]);
12924         }
12925       return;
12926
12927     case 'z':
12928       if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PLTSEQ)
12929         x = XVECEXP (x, 0, 1);
12930       /* X is a SYMBOL_REF.  Write out the name preceded by a
12931          period and without any trailing data in brackets.  Used for function
12932          names.  If we are configured for System V (or the embedded ABI) on
12933          the PowerPC, do not emit the period, since those systems do not use
12934          TOCs and the like.  */
12935       if (!SYMBOL_REF_P (x))
12936         {
12937           output_operand_lossage ("invalid %%z value");
12938           return;
12939         }
12940
12941       /* For macho, check to see if we need a stub.  */
12942       if (TARGET_MACHO)
12943         {
12944           const char *name = XSTR (x, 0);
12945 #if TARGET_MACHO
12946           if (darwin_symbol_stubs
12947               && MACHOPIC_INDIRECT
12948               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
12949             name = machopic_indirection_name (x, /*stub_p=*/true);
12950 #endif
12951           assemble_name (file, name);
12952         }
12953       else if (!DOT_SYMBOLS)
12954         assemble_name (file, XSTR (x, 0));
12955       else
12956         rs6000_output_function_entry (file, XSTR (x, 0));
12957       return;
12958
12959     case 'Z':
12960       /* Like 'L', for last word of TImode/PTImode.  */
12961       if (REG_P (x))
12962         fputs (reg_names[REGNO (x) + 3], file);
12963       else if (MEM_P (x))
12964         {
12965           machine_mode mode = GET_MODE (x);
12966           if (GET_CODE (XEXP (x, 0)) == PRE_INC
12967               || GET_CODE (XEXP (x, 0)) == PRE_DEC)
12968             output_address (mode, plus_constant (Pmode,
12969                                                  XEXP (XEXP (x, 0), 0), 12));
12970           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
12971             output_address (mode, plus_constant (Pmode,
12972                                                  XEXP (XEXP (x, 0), 0), 12));
12973           else
12974             output_address (mode, XEXP (adjust_address_nv (x, SImode, 12), 0));
12975           if (small_data_operand (x, GET_MODE (x)))
12976             fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
12977                      reg_names[SMALL_DATA_REG]);
12978         }
12979       return;
12980
12981       /* Print AltiVec memory operand.  */
12982     case 'y':
12983       {
12984         rtx tmp;
12985
12986         gcc_assert (MEM_P (x));
12987
12988         tmp = XEXP (x, 0);
12989
12990         if (VECTOR_MEM_ALTIVEC_OR_VSX_P (GET_MODE (x))
12991             && GET_CODE (tmp) == AND
12992             && CONST_INT_P (XEXP (tmp, 1))
12993             && INTVAL (XEXP (tmp, 1)) == -16)
12994           tmp = XEXP (tmp, 0);
12995         else if (VECTOR_MEM_VSX_P (GET_MODE (x))
12996                  && GET_CODE (tmp) == PRE_MODIFY)
12997           tmp = XEXP (tmp, 1);
12998         if (REG_P (tmp))
12999           fprintf (file, "0,%s", reg_names[REGNO (tmp)]);
13000         else
13001           {
13002             if (GET_CODE (tmp) != PLUS
13003                 || !REG_P (XEXP (tmp, 0))
13004                 || !REG_P (XEXP (tmp, 1)))
13005               {
13006                 output_operand_lossage ("invalid %%y value, try using the 'Z' constraint");
13007                 break;
13008               }
13009
13010             if (REGNO (XEXP (tmp, 0)) == 0)
13011               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 1)) ],
13012                        reg_names[ REGNO (XEXP (tmp, 0)) ]);
13013             else
13014               fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (tmp, 0)) ],
13015                        reg_names[ REGNO (XEXP (tmp, 1)) ]);
13016           }
13017         break;
13018       }
13019
13020     case 0:
13021       if (REG_P (x))
13022         fprintf (file, "%s", reg_names[REGNO (x)]);
13023       else if (MEM_P (x))
13024         {
13025           /* We need to handle PRE_INC and PRE_DEC here, since we need to
13026              know the width from the mode.  */
13027           if (GET_CODE (XEXP (x, 0)) == PRE_INC)
13028             fprintf (file, "%d(%s)", GET_MODE_SIZE (GET_MODE (x)),
13029                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
13030           else if (GET_CODE (XEXP (x, 0)) == PRE_DEC)
13031             fprintf (file, "%d(%s)", - GET_MODE_SIZE (GET_MODE (x)),
13032                      reg_names[REGNO (XEXP (XEXP (x, 0), 0))]);
13033           else if (GET_CODE (XEXP (x, 0)) == PRE_MODIFY)
13034             output_address (GET_MODE (x), XEXP (XEXP (x, 0), 1));
13035           else
13036             output_address (GET_MODE (x), XEXP (x, 0));
13037         }
13038       else if (toc_relative_expr_p (x, false,
13039                                     &tocrel_base_oac, &tocrel_offset_oac))
13040         /* This hack along with a corresponding hack in
13041            rs6000_output_addr_const_extra arranges to output addends
13042            where the assembler expects to find them.  eg.
13043            (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 4)
13044            without this hack would be output as "x@toc+4".  We
13045            want "x+4@toc".  */
13046         output_addr_const (file, CONST_CAST_RTX (tocrel_base_oac));
13047       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLSGD)
13048         output_addr_const (file, XVECEXP (x, 0, 0));
13049       else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PLTSEQ)
13050         output_addr_const (file, XVECEXP (x, 0, 1));
13051       else
13052         output_addr_const (file, x);
13053       return;
13054
13055     case '&':
13056       if (const char *name = get_some_local_dynamic_name ())
13057         assemble_name (file, name);
13058       else
13059         output_operand_lossage ("'%%&' used without any "
13060                                 "local dynamic TLS references");
13061       return;
13062
13063     default:
13064       output_operand_lossage ("invalid %%xn code");
13065     }
13066 }
13067 \f
13068 /* Print the address of an operand.  */
13069
13070 void
13071 print_operand_address (FILE *file, rtx x)
13072 {
13073   if (REG_P (x))
13074     fprintf (file, "0(%s)", reg_names[ REGNO (x) ]);
13075
13076   /* Is it a pc-relative address?  */
13077   else if (pcrel_address (x, Pmode))
13078     {
13079       HOST_WIDE_INT offset;
13080
13081       if (GET_CODE (x) == CONST)
13082         x = XEXP (x, 0);
13083
13084       if (GET_CODE (x) == PLUS)
13085         {
13086           offset = INTVAL (XEXP (x, 1));
13087           x = XEXP (x, 0);
13088         }
13089       else
13090         offset = 0;
13091
13092       output_addr_const (file, x);
13093
13094       if (offset)
13095         fprintf (file, "%+" PRId64, offset);
13096
13097       fputs ("@pcrel", file);
13098     }
13099   else if (SYMBOL_REF_P (x) || GET_CODE (x) == CONST
13100            || GET_CODE (x) == LABEL_REF)
13101     {
13102       output_addr_const (file, x);
13103       if (small_data_operand (x, GET_MODE (x)))
13104         fprintf (file, "@%s(%s)", SMALL_DATA_RELOC,
13105                  reg_names[SMALL_DATA_REG]);
13106       else
13107         gcc_assert (!TARGET_TOC);
13108     }
13109   else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
13110            && REG_P (XEXP (x, 1)))
13111     {
13112       if (REGNO (XEXP (x, 0)) == 0)
13113         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 1)) ],
13114                  reg_names[ REGNO (XEXP (x, 0)) ]);
13115       else
13116         fprintf (file, "%s,%s", reg_names[ REGNO (XEXP (x, 0)) ],
13117                  reg_names[ REGNO (XEXP (x, 1)) ]);
13118     }
13119   else if (GET_CODE (x) == PLUS && REG_P (XEXP (x, 0))
13120            && CONST_INT_P (XEXP (x, 1)))
13121     fprintf (file, HOST_WIDE_INT_PRINT_DEC "(%s)",
13122              INTVAL (XEXP (x, 1)), reg_names[ REGNO (XEXP (x, 0)) ]);
13123 #if TARGET_MACHO
13124   else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
13125            && CONSTANT_P (XEXP (x, 1)))
13126     {
13127       fprintf (file, "lo16(");
13128       output_addr_const (file, XEXP (x, 1));
13129       fprintf (file, ")(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
13130     }
13131 #endif
13132 #if TARGET_ELF
13133   else if (GET_CODE (x) == LO_SUM && REG_P (XEXP (x, 0))
13134            && CONSTANT_P (XEXP (x, 1)))
13135     {
13136       output_addr_const (file, XEXP (x, 1));
13137       fprintf (file, "@l(%s)", reg_names[ REGNO (XEXP (x, 0)) ]);
13138     }
13139 #endif
13140   else if (toc_relative_expr_p (x, false, &tocrel_base_oac, &tocrel_offset_oac))
13141     {
13142       /* This hack along with a corresponding hack in
13143          rs6000_output_addr_const_extra arranges to output addends
13144          where the assembler expects to find them.  eg.
13145          (lo_sum (reg 9)
13146          .       (plus (unspec [(symbol_ref ("x")) (reg 2)] tocrel) 8))
13147          without this hack would be output as "x@toc+8@l(9)".  We
13148          want "x+8@toc@l(9)".  */
13149       output_addr_const (file, CONST_CAST_RTX (tocrel_base_oac));
13150       if (GET_CODE (x) == LO_SUM)
13151         fprintf (file, "@l(%s)", reg_names[REGNO (XEXP (x, 0))]);
13152       else
13153         fprintf (file, "(%s)", reg_names[REGNO (XVECEXP (tocrel_base_oac, 0, 1))]);
13154     }
13155   else
13156     output_addr_const (file, x);
13157 }
13158 \f
13159 /* Implement TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
13160
13161 bool
13162 rs6000_output_addr_const_extra (FILE *file, rtx x)
13163 {
13164   if (GET_CODE (x) == UNSPEC)
13165     switch (XINT (x, 1))
13166       {
13167       case UNSPEC_TOCREL:
13168         gcc_checking_assert (SYMBOL_REF_P (XVECEXP (x, 0, 0))
13169                              && REG_P (XVECEXP (x, 0, 1))
13170                              && REGNO (XVECEXP (x, 0, 1)) == TOC_REGISTER);
13171         output_addr_const (file, XVECEXP (x, 0, 0));
13172         if (x == tocrel_base_oac && tocrel_offset_oac != const0_rtx)
13173           {
13174             if (INTVAL (tocrel_offset_oac) >= 0)
13175               fprintf (file, "+");
13176             output_addr_const (file, CONST_CAST_RTX (tocrel_offset_oac));
13177           }
13178         if (!TARGET_AIX || (TARGET_ELF && TARGET_MINIMAL_TOC))
13179           {
13180             putc ('-', file);
13181             assemble_name (file, toc_label_name);
13182             need_toc_init = 1;
13183           }
13184         else if (TARGET_ELF)
13185           fputs ("@toc", file);
13186         return true;
13187
13188 #if TARGET_MACHO
13189       case UNSPEC_MACHOPIC_OFFSET:
13190         output_addr_const (file, XVECEXP (x, 0, 0));
13191         putc ('-', file);
13192         machopic_output_function_base_name (file);
13193         return true;
13194 #endif
13195       }
13196   return false;
13197 }
13198 \f
13199 /* Target hook for assembling integer objects.  The PowerPC version has
13200    to handle fixup entries for relocatable code if RELOCATABLE_NEEDS_FIXUP
13201    is defined.  It also needs to handle DI-mode objects on 64-bit
13202    targets.  */
13203
13204 static bool
13205 rs6000_assemble_integer (rtx x, unsigned int size, int aligned_p)
13206 {
13207 #ifdef RELOCATABLE_NEEDS_FIXUP
13208   /* Special handling for SI values.  */
13209   if (RELOCATABLE_NEEDS_FIXUP && size == 4 && aligned_p)
13210     {
13211       static int recurse = 0;
13212
13213       /* For -mrelocatable, we mark all addresses that need to be fixed up in
13214          the .fixup section.  Since the TOC section is already relocated, we
13215          don't need to mark it here.  We used to skip the text section, but it
13216          should never be valid for relocated addresses to be placed in the text
13217          section.  */
13218       if (DEFAULT_ABI == ABI_V4
13219           && (TARGET_RELOCATABLE || flag_pic > 1)
13220           && in_section != toc_section
13221           && !recurse
13222           && !CONST_SCALAR_INT_P (x)
13223           && CONSTANT_P (x))
13224         {
13225           char buf[256];
13226
13227           recurse = 1;
13228           ASM_GENERATE_INTERNAL_LABEL (buf, "LCP", fixuplabelno);
13229           fixuplabelno++;
13230           ASM_OUTPUT_LABEL (asm_out_file, buf);
13231           fprintf (asm_out_file, "\t.long\t(");
13232           output_addr_const (asm_out_file, x);
13233           fprintf (asm_out_file, ")@fixup\n");
13234           fprintf (asm_out_file, "\t.section\t\".fixup\",\"aw\"\n");
13235           ASM_OUTPUT_ALIGN (asm_out_file, 2);
13236           fprintf (asm_out_file, "\t.long\t");
13237           assemble_name (asm_out_file, buf);
13238           fprintf (asm_out_file, "\n\t.previous\n");
13239           recurse = 0;
13240           return true;
13241         }
13242       /* Remove initial .'s to turn a -mcall-aixdesc function
13243          address into the address of the descriptor, not the function
13244          itself.  */
13245       else if (SYMBOL_REF_P (x)
13246                && XSTR (x, 0)[0] == '.'
13247                && DEFAULT_ABI == ABI_AIX)
13248         {
13249           const char *name = XSTR (x, 0);
13250           while (*name == '.')
13251             name++;
13252
13253           fprintf (asm_out_file, "\t.long\t%s\n", name);
13254           return true;
13255         }
13256     }
13257 #endif /* RELOCATABLE_NEEDS_FIXUP */
13258   return default_assemble_integer (x, size, aligned_p);
13259 }
13260
13261 /* Return a template string for assembly to emit when making an
13262    external call.  FUNOP is the call mem argument operand number.  */
13263
13264 static const char *
13265 rs6000_call_template_1 (rtx *operands, unsigned int funop, bool sibcall)
13266 {
13267   /* -Wformat-overflow workaround, without which gcc thinks that %u
13268       might produce 10 digits.  */
13269   gcc_assert (funop <= MAX_RECOG_OPERANDS);
13270
13271   char arg[12];
13272   arg[0] = 0;
13273   if (TARGET_TLS_MARKERS && GET_CODE (operands[funop + 1]) == UNSPEC)
13274     {
13275       if (XINT (operands[funop + 1], 1) == UNSPEC_TLSGD)
13276         sprintf (arg, "(%%%u@tlsgd)", funop + 1);
13277       else if (XINT (operands[funop + 1], 1) == UNSPEC_TLSLD)
13278         sprintf (arg, "(%%&@tlsld)");
13279       else
13280         gcc_unreachable ();
13281     }
13282
13283   /* The magic 32768 offset here corresponds to the offset of
13284      r30 in .got2, as given by LCTOC1.  See sysv4.h:toc_section.  */
13285   char z[11];
13286   sprintf (z, "%%z%u%s", funop,
13287            (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic == 2
13288             ? "+32768" : ""));
13289
13290   static char str[32];  /* 1 spare */
13291   if (rs6000_pcrel_p (cfun))
13292     sprintf (str, "b%s %s@notoc%s", sibcall ? "" : "l", z, arg);
13293   else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
13294     sprintf (str, "b%s %s%s%s", sibcall ? "" : "l", z, arg,
13295              sibcall ? "" : "\n\tnop");
13296   else if (DEFAULT_ABI == ABI_V4)
13297     sprintf (str, "b%s %s%s%s", sibcall ? "" : "l", z, arg,
13298              flag_pic ? "@plt" : "");
13299 #if TARGET_MACHO
13300   /* If/when we remove the mlongcall opt, we can share the AIX/ELGv2 case. */
13301    else if (DEFAULT_ABI == ABI_DARWIN)
13302     {
13303       /* The cookie is in operand func+2.  */
13304       gcc_checking_assert (GET_CODE (operands[funop + 2]) == CONST_INT);
13305       int cookie = INTVAL (operands[funop + 2]);
13306       if (cookie & CALL_LONG)
13307         {
13308           tree funname = get_identifier (XSTR (operands[funop], 0));
13309           tree labelname = get_prev_label (funname);
13310           gcc_checking_assert (labelname && !sibcall);
13311
13312           /* "jbsr foo, L42" is Mach-O for "Link as 'bl foo' if a 'bl'
13313              instruction will reach 'foo', otherwise link as 'bl L42'".
13314              "L42" should be a 'branch island', that will do a far jump to
13315              'foo'.  Branch islands are generated in
13316              macho_branch_islands().  */
13317           sprintf (str, "jbsr %%z%u,%.10s", funop,
13318                    IDENTIFIER_POINTER (labelname));
13319         }
13320       else
13321         /* Same as AIX or ELFv2, except to keep backwards compat, no nop
13322            after the call.  */
13323         sprintf (str, "b%s %s%s", sibcall ? "" : "l", z, arg);
13324     }
13325 #endif
13326   else
13327     gcc_unreachable ();
13328   return str;
13329 }
13330
13331 const char *
13332 rs6000_call_template (rtx *operands, unsigned int funop)
13333 {
13334   return rs6000_call_template_1 (operands, funop, false);
13335 }
13336
13337 const char *
13338 rs6000_sibcall_template (rtx *operands, unsigned int funop)
13339 {
13340   return rs6000_call_template_1 (operands, funop, true);
13341 }
13342
13343 /* As above, for indirect calls.  */
13344
13345 static const char *
13346 rs6000_indirect_call_template_1 (rtx *operands, unsigned int funop,
13347                                  bool sibcall)
13348 {
13349   /* -Wformat-overflow workaround, without which gcc thinks that %u
13350      might produce 10 digits.  Note that -Wformat-overflow will not
13351      currently warn here for str[], so do not rely on a warning to
13352      ensure str[] is correctly sized.  */
13353   gcc_assert (funop <= MAX_RECOG_OPERANDS);
13354
13355   /* Currently, funop is either 0 or 1.  The maximum string is always
13356      a !speculate 64-bit __tls_get_addr call.
13357
13358      ABI_ELFv2, pcrel:
13359      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13360      . 35       .reloc .,R_PPC64_PLTSEQ_NOTOC,%z1\n\t
13361      .  9       crset 2\n\t
13362      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13363      . 36       .reloc .,R_PPC64_PLTCALL_NOTOC,%z1\n\t
13364      .  8       beq%T1l-
13365      .---
13366      .142
13367
13368      ABI_AIX:
13369      .  9       ld 2,%3\n\t
13370      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13371      . 29       .reloc .,R_PPC64_PLTSEQ,%z1\n\t
13372      .  9       crset 2\n\t
13373      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13374      . 30       .reloc .,R_PPC64_PLTCALL,%z1\n\t
13375      . 10       beq%T1l-\n\t
13376      . 10       ld 2,%4(1)
13377      .---
13378      .151
13379
13380      ABI_ELFv2:
13381      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13382      . 29       .reloc .,R_PPC64_PLTSEQ,%z1\n\t
13383      .  9       crset 2\n\t
13384      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13385      . 30       .reloc .,R_PPC64_PLTCALL,%z1\n\t
13386      . 10       beq%T1l-\n\t
13387      . 10       ld 2,%3(1)
13388      .---
13389      .142
13390
13391      ABI_V4:
13392      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13393      . 35       .reloc .,R_PPC64_PLTSEQ,%z1+32768\n\t
13394      .  9       crset 2\n\t
13395      . 27       .reloc .,R_PPC64_TLSGD,%2\n\t
13396      . 36       .reloc .,R_PPC64_PLTCALL,%z1+32768\n\t
13397      .  8       beq%T1l-
13398      .---
13399      .141  */
13400   static char str[160];  /* 8 spare */
13401   char *s = str;
13402   const char *ptrload = TARGET_64BIT ? "d" : "wz";
13403
13404   if (DEFAULT_ABI == ABI_AIX)
13405     s += sprintf (s,
13406                   "l%s 2,%%%u\n\t",
13407                   ptrload, funop + 2);
13408
13409   /* We don't need the extra code to stop indirect call speculation if
13410      calling via LR.  */
13411   bool speculate = (TARGET_MACHO
13412                     || rs6000_speculate_indirect_jumps
13413                     || (REG_P (operands[funop])
13414                         && REGNO (operands[funop]) == LR_REGNO));
13415
13416   if (TARGET_PLTSEQ && GET_CODE (operands[funop]) == UNSPEC)
13417     {
13418       const char *rel64 = TARGET_64BIT ? "64" : "";
13419       char tls[29];
13420       tls[0] = 0;
13421       if (TARGET_TLS_MARKERS && GET_CODE (operands[funop + 1]) == UNSPEC)
13422         {
13423           if (XINT (operands[funop + 1], 1) == UNSPEC_TLSGD)
13424             sprintf (tls, ".reloc .,R_PPC%s_TLSGD,%%%u\n\t",
13425                      rel64, funop + 1);
13426           else if (XINT (operands[funop + 1], 1) == UNSPEC_TLSLD)
13427             sprintf (tls, ".reloc .,R_PPC%s_TLSLD,%%&\n\t",
13428                      rel64);
13429           else
13430             gcc_unreachable ();
13431         }
13432
13433       const char *notoc = rs6000_pcrel_p (cfun) ? "_NOTOC" : "";
13434       const char *addend = (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT
13435                             && flag_pic == 2 ? "+32768" : "");
13436       if (!speculate)
13437         {
13438           s += sprintf (s,
13439                         "%s.reloc .,R_PPC%s_PLTSEQ%s,%%z%u%s\n\t",
13440                         tls, rel64, notoc, funop, addend);
13441           s += sprintf (s, "crset 2\n\t");
13442         }
13443       s += sprintf (s,
13444                     "%s.reloc .,R_PPC%s_PLTCALL%s,%%z%u%s\n\t",
13445                     tls, rel64, notoc, funop, addend);
13446     }
13447   else if (!speculate)
13448     s += sprintf (s, "crset 2\n\t");
13449
13450   if (rs6000_pcrel_p (cfun))
13451     {
13452       if (speculate)
13453         sprintf (s, "b%%T%ul", funop);
13454       else
13455         sprintf (s, "beq%%T%ul-", funop);
13456     }
13457   else if (DEFAULT_ABI == ABI_AIX)
13458     {
13459       if (speculate)
13460         sprintf (s,
13461                  "b%%T%ul\n\t"
13462                  "l%s 2,%%%u(1)",
13463                  funop, ptrload, funop + 3);
13464       else
13465         sprintf (s,
13466                  "beq%%T%ul-\n\t"
13467                  "l%s 2,%%%u(1)",
13468                  funop, ptrload, funop + 3);
13469     }
13470   else if (DEFAULT_ABI == ABI_ELFv2)
13471     {
13472       if (speculate)
13473         sprintf (s,
13474                  "b%%T%ul\n\t"
13475                  "l%s 2,%%%u(1)",
13476                  funop, ptrload, funop + 2);
13477       else
13478         sprintf (s,
13479                  "beq%%T%ul-\n\t"
13480                  "l%s 2,%%%u(1)",
13481                  funop, ptrload, funop + 2);
13482     }
13483   else
13484     {
13485       if (speculate)
13486         sprintf (s,
13487                  "b%%T%u%s",
13488                  funop, sibcall ? "" : "l");
13489       else
13490         sprintf (s,
13491                  "beq%%T%u%s-%s",
13492                  funop, sibcall ? "" : "l", sibcall ? "\n\tb $" : "");
13493     }
13494   return str;
13495 }
13496
13497 const char *
13498 rs6000_indirect_call_template (rtx *operands, unsigned int funop)
13499 {
13500   return rs6000_indirect_call_template_1 (operands, funop, false);
13501 }
13502
13503 const char *
13504 rs6000_indirect_sibcall_template (rtx *operands, unsigned int funop)
13505 {
13506   return rs6000_indirect_call_template_1 (operands, funop, true);
13507 }
13508
13509 #if HAVE_AS_PLTSEQ
13510 /* Output indirect call insns.  WHICH identifies the type of sequence.  */
13511 const char *
13512 rs6000_pltseq_template (rtx *operands, int which)
13513 {
13514   const char *rel64 = TARGET_64BIT ? "64" : "";
13515   char tls[30];
13516   tls[0] = 0;
13517   if (TARGET_TLS_MARKERS && GET_CODE (operands[3]) == UNSPEC)
13518     {
13519       char off = which == RS6000_PLTSEQ_PLT_PCREL34 ? '8' : '4';
13520       if (XINT (operands[3], 1) == UNSPEC_TLSGD)
13521         sprintf (tls, ".reloc .-%c,R_PPC%s_TLSGD,%%3\n\t",
13522                  off, rel64);
13523       else if (XINT (operands[3], 1) == UNSPEC_TLSLD)
13524         sprintf (tls, ".reloc .-%c,R_PPC%s_TLSLD,%%&\n\t",
13525                  off, rel64);
13526       else
13527         gcc_unreachable ();
13528     }
13529
13530   gcc_assert (DEFAULT_ABI == ABI_ELFv2 || DEFAULT_ABI == ABI_V4);
13531   static char str[96];  /* 10 spare */
13532   char off = WORDS_BIG_ENDIAN ? '2' : '4';
13533   const char *addend = (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT
13534                         && flag_pic == 2 ? "+32768" : "");
13535   switch (which)
13536     {
13537     case RS6000_PLTSEQ_TOCSAVE:
13538       sprintf (str,
13539                "st%s\n\t"
13540                "%s.reloc .-4,R_PPC%s_PLTSEQ,%%z2",
13541                TARGET_64BIT ? "d 2,24(1)" : "w 2,12(1)",
13542                tls, rel64);
13543       break;
13544     case RS6000_PLTSEQ_PLT16_HA:
13545       if (DEFAULT_ABI == ABI_V4 && !flag_pic)
13546         sprintf (str,
13547                  "lis %%0,0\n\t"
13548                  "%s.reloc .-%c,R_PPC%s_PLT16_HA,%%z2",
13549                  tls, off, rel64);
13550       else
13551         sprintf (str,
13552                  "addis %%0,%%1,0\n\t"
13553                  "%s.reloc .-%c,R_PPC%s_PLT16_HA,%%z2%s",
13554                  tls, off, rel64, addend);
13555       break;
13556     case RS6000_PLTSEQ_PLT16_LO:
13557       sprintf (str,
13558                "l%s %%0,0(%%1)\n\t"
13559                "%s.reloc .-%c,R_PPC%s_PLT16_LO%s,%%z2%s",
13560                TARGET_64BIT ? "d" : "wz",
13561                tls, off, rel64, TARGET_64BIT ? "_DS" : "", addend);
13562       break;
13563     case RS6000_PLTSEQ_MTCTR:
13564       sprintf (str,
13565                "mtctr %%1\n\t"
13566                "%s.reloc .-4,R_PPC%s_PLTSEQ,%%z2%s",
13567                tls, rel64, addend);
13568       break;
13569     case RS6000_PLTSEQ_PLT_PCREL34:
13570       sprintf (str,
13571                "pl%s %%0,0(0),1\n\t"
13572                "%s.reloc .-8,R_PPC%s_PLT_PCREL34_NOTOC,%%z2",
13573                TARGET_64BIT ? "d" : "wz",
13574                tls, rel64);
13575       break;
13576     default:
13577       gcc_unreachable ();
13578     }
13579   return str;
13580 }
13581 #endif
13582
13583 /* Helper function to return whether a MODE can do prefixed loads/stores.
13584    VOIDmode is used when we are loading the pc-relative address into a base
13585    register, but we are not using it as part of a memory operation.  As modes
13586    add support for prefixed memory, they will be added here.  */
13587
13588 static bool
13589 mode_supports_prefixed_address_p (machine_mode mode)
13590 {
13591   return mode == VOIDmode;
13592 }
13593
13594 /* Function to return true if ADDR is a valid prefixed memory address that uses
13595    mode MODE.  */
13596
13597 bool
13598 rs6000_prefixed_address_mode_p (rtx addr, machine_mode mode)
13599 {
13600   if (!TARGET_PREFIXED_ADDR || !mode_supports_prefixed_address_p (mode))
13601     return false;
13602
13603   /* Check for PC-relative addresses.  */
13604   if (pcrel_address (addr, Pmode))
13605     return true;
13606
13607   /* Check for prefixed memory addresses that have a large numeric offset,
13608      or an offset that can't be used for a DS/DQ-form memory operation.  */
13609   if (GET_CODE (addr) == PLUS)
13610     {
13611       rtx op0 = XEXP (addr, 0);
13612       rtx op1 = XEXP (addr, 1);
13613
13614       if (!base_reg_operand (op0, Pmode) || !CONST_INT_P (op1))
13615         return false;
13616
13617       HOST_WIDE_INT value = INTVAL (op1);
13618       if (!SIGNED_34BIT_OFFSET_P (value))
13619         return false;
13620
13621       /* Offset larger than 16-bits?  */
13622       if (!SIGNED_16BIT_OFFSET_P (value))
13623         return true;
13624
13625       /* DQ instruction (bottom 4 bits must be 0) for vectors.  */
13626       HOST_WIDE_INT mask;
13627       if (GET_MODE_SIZE (mode) >= 16)
13628         mask = 15;
13629
13630       /* DS instruction (bottom 2 bits must be 0).  For 32-bit integers, we
13631          need to use DS instructions if we are sign-extending the value with
13632          LWA.  For 32-bit floating point, we need DS instructions to load and
13633          store values to the traditional Altivec registers.  */
13634       else if (GET_MODE_SIZE (mode) >= 4)
13635         mask = 3;
13636
13637       /* QImode/HImode has no restrictions.  */
13638       else
13639         return true;
13640
13641       /* Return true if we must use a prefixed instruction.  */
13642       return (value & mask) != 0;
13643     }
13644
13645   return false;
13646 }
13647 \f
13648 #if defined (HAVE_GAS_HIDDEN) && !TARGET_MACHO
13649 /* Emit an assembler directive to set symbol visibility for DECL to
13650    VISIBILITY_TYPE.  */
13651
13652 static void
13653 rs6000_assemble_visibility (tree decl, int vis)
13654 {
13655   if (TARGET_XCOFF)
13656     return;
13657
13658   /* Functions need to have their entry point symbol visibility set as
13659      well as their descriptor symbol visibility.  */
13660   if (DEFAULT_ABI == ABI_AIX
13661       && DOT_SYMBOLS
13662       && TREE_CODE (decl) == FUNCTION_DECL)
13663     {
13664       static const char * const visibility_types[] = {
13665         NULL, "protected", "hidden", "internal"
13666       };
13667
13668       const char *name, *type;
13669
13670       name = ((* targetm.strip_name_encoding)
13671               (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
13672       type = visibility_types[vis];
13673
13674       fprintf (asm_out_file, "\t.%s\t%s\n", type, name);
13675       fprintf (asm_out_file, "\t.%s\t.%s\n", type, name);
13676     }
13677   else
13678     default_assemble_visibility (decl, vis);
13679 }
13680 #endif
13681 \f
13682 enum rtx_code
13683 rs6000_reverse_condition (machine_mode mode, enum rtx_code code)
13684 {
13685   /* Reversal of FP compares takes care -- an ordered compare
13686      becomes an unordered compare and vice versa.  */
13687   if (mode == CCFPmode
13688       && (!flag_finite_math_only
13689           || code == UNLT || code == UNLE || code == UNGT || code == UNGE
13690           || code == UNEQ || code == LTGT))
13691     return reverse_condition_maybe_unordered (code);
13692   else
13693     return reverse_condition (code);
13694 }
13695
13696 /* Generate a compare for CODE.  Return a brand-new rtx that
13697    represents the result of the compare.  */
13698
13699 static rtx
13700 rs6000_generate_compare (rtx cmp, machine_mode mode)
13701 {
13702   machine_mode comp_mode;
13703   rtx compare_result;
13704   enum rtx_code code = GET_CODE (cmp);
13705   rtx op0 = XEXP (cmp, 0);
13706   rtx op1 = XEXP (cmp, 1);
13707
13708   if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
13709     comp_mode = CCmode;
13710   else if (FLOAT_MODE_P (mode))
13711     comp_mode = CCFPmode;
13712   else if (code == GTU || code == LTU
13713            || code == GEU || code == LEU)
13714     comp_mode = CCUNSmode;
13715   else if ((code == EQ || code == NE)
13716            && unsigned_reg_p (op0)
13717            && (unsigned_reg_p (op1)
13718                || (CONST_INT_P (op1) && INTVAL (op1) != 0)))
13719     /* These are unsigned values, perhaps there will be a later
13720        ordering compare that can be shared with this one.  */
13721     comp_mode = CCUNSmode;
13722   else
13723     comp_mode = CCmode;
13724
13725   /* If we have an unsigned compare, make sure we don't have a signed value as
13726      an immediate.  */
13727   if (comp_mode == CCUNSmode && CONST_INT_P (op1)
13728       && INTVAL (op1) < 0)
13729     {
13730       op0 = copy_rtx_if_shared (op0);
13731       op1 = force_reg (GET_MODE (op0), op1);
13732       cmp = gen_rtx_fmt_ee (code, GET_MODE (cmp), op0, op1);
13733     }
13734
13735   /* First, the compare.  */
13736   compare_result = gen_reg_rtx (comp_mode);
13737
13738   /* IEEE 128-bit support in VSX registers when we do not have hardware
13739      support.  */
13740   if (!TARGET_FLOAT128_HW && FLOAT128_VECTOR_P (mode))
13741     {
13742       rtx libfunc = NULL_RTX;
13743       bool check_nan = false;
13744       rtx dest;
13745
13746       switch (code)
13747         {
13748         case EQ:
13749         case NE:
13750           libfunc = optab_libfunc (eq_optab, mode);
13751           break;
13752
13753         case GT:
13754         case GE:
13755           libfunc = optab_libfunc (ge_optab, mode);
13756           break;
13757
13758         case LT:
13759         case LE:
13760           libfunc = optab_libfunc (le_optab, mode);
13761           break;
13762
13763         case UNORDERED:
13764         case ORDERED:
13765           libfunc = optab_libfunc (unord_optab, mode);
13766           code = (code == UNORDERED) ? NE : EQ;
13767           break;
13768
13769         case UNGE:
13770         case UNGT:
13771           check_nan = true;
13772           libfunc = optab_libfunc (ge_optab, mode);
13773           code = (code == UNGE) ? GE : GT;
13774           break;
13775
13776         case UNLE:
13777         case UNLT:
13778           check_nan = true;
13779           libfunc = optab_libfunc (le_optab, mode);
13780           code = (code == UNLE) ? LE : LT;
13781           break;
13782
13783         case UNEQ:
13784         case LTGT:
13785           check_nan = true;
13786           libfunc = optab_libfunc (eq_optab, mode);
13787           code = (code = UNEQ) ? EQ : NE;
13788           break;
13789
13790         default:
13791           gcc_unreachable ();
13792         }
13793
13794       gcc_assert (libfunc);
13795
13796       if (!check_nan)
13797         dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
13798                                         SImode, op0, mode, op1, mode);
13799
13800       /* The library signals an exception for signalling NaNs, so we need to
13801          handle isgreater, etc. by first checking isordered.  */
13802       else
13803         {
13804           rtx ne_rtx, normal_dest, unord_dest;
13805           rtx unord_func = optab_libfunc (unord_optab, mode);
13806           rtx join_label = gen_label_rtx ();
13807           rtx join_ref = gen_rtx_LABEL_REF (VOIDmode, join_label);
13808           rtx unord_cmp = gen_reg_rtx (comp_mode);
13809
13810
13811           /* Test for either value being a NaN.  */
13812           gcc_assert (unord_func);
13813           unord_dest = emit_library_call_value (unord_func, NULL_RTX, LCT_CONST,
13814                                                 SImode, op0, mode, op1, mode);
13815
13816           /* Set value (0) if either value is a NaN, and jump to the join
13817              label.  */
13818           dest = gen_reg_rtx (SImode);
13819           emit_move_insn (dest, const1_rtx);
13820           emit_insn (gen_rtx_SET (unord_cmp,
13821                                   gen_rtx_COMPARE (comp_mode, unord_dest,
13822                                                    const0_rtx)));
13823
13824           ne_rtx = gen_rtx_NE (comp_mode, unord_cmp, const0_rtx);
13825           emit_jump_insn (gen_rtx_SET (pc_rtx,
13826                                        gen_rtx_IF_THEN_ELSE (VOIDmode, ne_rtx,
13827                                                              join_ref,
13828                                                              pc_rtx)));
13829
13830           /* Do the normal comparison, knowing that the values are not
13831              NaNs.  */
13832           normal_dest = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
13833                                                  SImode, op0, mode, op1, mode);
13834
13835           emit_insn (gen_cstoresi4 (dest,
13836                                     gen_rtx_fmt_ee (code, SImode, normal_dest,
13837                                                     const0_rtx),
13838                                     normal_dest, const0_rtx));
13839
13840           /* Join NaN and non-Nan paths.  Compare dest against 0.  */
13841           emit_label (join_label);
13842           code = NE;
13843         }
13844
13845       emit_insn (gen_rtx_SET (compare_result,
13846                               gen_rtx_COMPARE (comp_mode, dest, const0_rtx)));
13847     }
13848
13849   else
13850     {
13851       /* Generate XLC-compatible TFmode compare as PARALLEL with extra
13852          CLOBBERs to match cmptf_internal2 pattern.  */
13853       if (comp_mode == CCFPmode && TARGET_XL_COMPAT
13854           && FLOAT128_IBM_P (GET_MODE (op0))
13855           && TARGET_HARD_FLOAT)
13856         emit_insn (gen_rtx_PARALLEL (VOIDmode,
13857           gen_rtvec (10,
13858                      gen_rtx_SET (compare_result,
13859                                   gen_rtx_COMPARE (comp_mode, op0, op1)),
13860                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13861                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13862                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13863                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13864                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13865                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13866                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13867                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (DFmode)),
13868                      gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (Pmode)))));
13869       else if (GET_CODE (op1) == UNSPEC
13870                && XINT (op1, 1) == UNSPEC_SP_TEST)
13871         {
13872           rtx op1b = XVECEXP (op1, 0, 0);
13873           comp_mode = CCEQmode;
13874           compare_result = gen_reg_rtx (CCEQmode);
13875           if (TARGET_64BIT)
13876             emit_insn (gen_stack_protect_testdi (compare_result, op0, op1b));
13877           else
13878             emit_insn (gen_stack_protect_testsi (compare_result, op0, op1b));
13879         }
13880       else
13881         emit_insn (gen_rtx_SET (compare_result,
13882                                 gen_rtx_COMPARE (comp_mode, op0, op1)));
13883     }
13884
13885   /* Some kinds of FP comparisons need an OR operation;
13886      under flag_finite_math_only we don't bother.  */
13887   if (FLOAT_MODE_P (mode)
13888       && (!FLOAT128_IEEE_P (mode) || TARGET_FLOAT128_HW)
13889       && !flag_finite_math_only
13890       && (code == LE || code == GE
13891           || code == UNEQ || code == LTGT
13892           || code == UNGT || code == UNLT))
13893     {
13894       enum rtx_code or1, or2;
13895       rtx or1_rtx, or2_rtx, compare2_rtx;
13896       rtx or_result = gen_reg_rtx (CCEQmode);
13897
13898       switch (code)
13899         {
13900         case LE: or1 = LT;  or2 = EQ;  break;
13901         case GE: or1 = GT;  or2 = EQ;  break;
13902         case UNEQ: or1 = UNORDERED;  or2 = EQ;  break;
13903         case LTGT: or1 = LT;  or2 = GT;  break;
13904         case UNGT: or1 = UNORDERED;  or2 = GT;  break;
13905         case UNLT: or1 = UNORDERED;  or2 = LT;  break;
13906         default:  gcc_unreachable ();
13907         }
13908       validate_condition_mode (or1, comp_mode);
13909       validate_condition_mode (or2, comp_mode);
13910       or1_rtx = gen_rtx_fmt_ee (or1, SImode, compare_result, const0_rtx);
13911       or2_rtx = gen_rtx_fmt_ee (or2, SImode, compare_result, const0_rtx);
13912       compare2_rtx = gen_rtx_COMPARE (CCEQmode,
13913                                       gen_rtx_IOR (SImode, or1_rtx, or2_rtx),
13914                                       const_true_rtx);
13915       emit_insn (gen_rtx_SET (or_result, compare2_rtx));
13916
13917       compare_result = or_result;
13918       code = EQ;
13919     }
13920
13921   validate_condition_mode (code, GET_MODE (compare_result));
13922
13923   return gen_rtx_fmt_ee (code, VOIDmode, compare_result, const0_rtx);
13924 }
13925
13926 \f
13927 /* Return the diagnostic message string if the binary operation OP is
13928    not permitted on TYPE1 and TYPE2, NULL otherwise.  */
13929
13930 static const char*
13931 rs6000_invalid_binary_op (int op ATTRIBUTE_UNUSED,
13932                           const_tree type1,
13933                           const_tree type2)
13934 {
13935   machine_mode mode1 = TYPE_MODE (type1);
13936   machine_mode mode2 = TYPE_MODE (type2);
13937
13938   /* For complex modes, use the inner type.  */
13939   if (COMPLEX_MODE_P (mode1))
13940     mode1 = GET_MODE_INNER (mode1);
13941
13942   if (COMPLEX_MODE_P (mode2))
13943     mode2 = GET_MODE_INNER (mode2);
13944
13945   /* Don't allow IEEE 754R 128-bit binary floating point and IBM extended
13946      double to intermix unless -mfloat128-convert.  */
13947   if (mode1 == mode2)
13948     return NULL;
13949
13950   if (!TARGET_FLOAT128_CVT)
13951     {
13952       if ((mode1 == KFmode && mode2 == IFmode)
13953           || (mode1 == IFmode && mode2 == KFmode))
13954         return N_("__float128 and __ibm128 cannot be used in the same "
13955                   "expression");
13956
13957       if (TARGET_IEEEQUAD
13958           && ((mode1 == IFmode && mode2 == TFmode)
13959               || (mode1 == TFmode && mode2 == IFmode)))
13960         return N_("__ibm128 and long double cannot be used in the same "
13961                   "expression");
13962
13963       if (!TARGET_IEEEQUAD
13964           && ((mode1 == KFmode && mode2 == TFmode)
13965               || (mode1 == TFmode && mode2 == KFmode)))
13966         return N_("__float128 and long double cannot be used in the same "
13967                   "expression");
13968     }
13969
13970   return NULL;
13971 }
13972
13973 \f
13974 /* Expand floating point conversion to/from __float128 and __ibm128.  */
13975
13976 void
13977 rs6000_expand_float128_convert (rtx dest, rtx src, bool unsigned_p)
13978 {
13979   machine_mode dest_mode = GET_MODE (dest);
13980   machine_mode src_mode = GET_MODE (src);
13981   convert_optab cvt = unknown_optab;
13982   bool do_move = false;
13983   rtx libfunc = NULL_RTX;
13984   rtx dest2;
13985   typedef rtx (*rtx_2func_t) (rtx, rtx);
13986   rtx_2func_t hw_convert = (rtx_2func_t)0;
13987   size_t kf_or_tf;
13988
13989   struct hw_conv_t {
13990     rtx_2func_t from_df;
13991     rtx_2func_t from_sf;
13992     rtx_2func_t from_si_sign;
13993     rtx_2func_t from_si_uns;
13994     rtx_2func_t from_di_sign;
13995     rtx_2func_t from_di_uns;
13996     rtx_2func_t to_df;
13997     rtx_2func_t to_sf;
13998     rtx_2func_t to_si_sign;
13999     rtx_2func_t to_si_uns;
14000     rtx_2func_t to_di_sign;
14001     rtx_2func_t to_di_uns;
14002   } hw_conversions[2] = {
14003     /* convertions to/from KFmode */
14004     {
14005       gen_extenddfkf2_hw,               /* KFmode <- DFmode.  */
14006       gen_extendsfkf2_hw,               /* KFmode <- SFmode.  */
14007       gen_float_kfsi2_hw,               /* KFmode <- SImode (signed).  */
14008       gen_floatuns_kfsi2_hw,            /* KFmode <- SImode (unsigned).  */
14009       gen_float_kfdi2_hw,               /* KFmode <- DImode (signed).  */
14010       gen_floatuns_kfdi2_hw,            /* KFmode <- DImode (unsigned).  */
14011       gen_trunckfdf2_hw,                /* DFmode <- KFmode.  */
14012       gen_trunckfsf2_hw,                /* SFmode <- KFmode.  */
14013       gen_fix_kfsi2_hw,                 /* SImode <- KFmode (signed).  */
14014       gen_fixuns_kfsi2_hw,              /* SImode <- KFmode (unsigned).  */
14015       gen_fix_kfdi2_hw,                 /* DImode <- KFmode (signed).  */
14016       gen_fixuns_kfdi2_hw,              /* DImode <- KFmode (unsigned).  */
14017     },
14018
14019     /* convertions to/from TFmode */
14020     {
14021       gen_extenddftf2_hw,               /* TFmode <- DFmode.  */
14022       gen_extendsftf2_hw,               /* TFmode <- SFmode.  */
14023       gen_float_tfsi2_hw,               /* TFmode <- SImode (signed).  */
14024       gen_floatuns_tfsi2_hw,            /* TFmode <- SImode (unsigned).  */
14025       gen_float_tfdi2_hw,               /* TFmode <- DImode (signed).  */
14026       gen_floatuns_tfdi2_hw,            /* TFmode <- DImode (unsigned).  */
14027       gen_trunctfdf2_hw,                /* DFmode <- TFmode.  */
14028       gen_trunctfsf2_hw,                /* SFmode <- TFmode.  */
14029       gen_fix_tfsi2_hw,                 /* SImode <- TFmode (signed).  */
14030       gen_fixuns_tfsi2_hw,              /* SImode <- TFmode (unsigned).  */
14031       gen_fix_tfdi2_hw,                 /* DImode <- TFmode (signed).  */
14032       gen_fixuns_tfdi2_hw,              /* DImode <- TFmode (unsigned).  */
14033     },
14034   };
14035
14036   if (dest_mode == src_mode)
14037     gcc_unreachable ();
14038
14039   /* Eliminate memory operations.  */
14040   if (MEM_P (src))
14041     src = force_reg (src_mode, src);
14042
14043   if (MEM_P (dest))
14044     {
14045       rtx tmp = gen_reg_rtx (dest_mode);
14046       rs6000_expand_float128_convert (tmp, src, unsigned_p);
14047       rs6000_emit_move (dest, tmp, dest_mode);
14048       return;
14049     }
14050
14051   /* Convert to IEEE 128-bit floating point.  */
14052   if (FLOAT128_IEEE_P (dest_mode))
14053     {
14054       if (dest_mode == KFmode)
14055         kf_or_tf = 0;
14056       else if (dest_mode == TFmode)
14057         kf_or_tf = 1;
14058       else
14059         gcc_unreachable ();
14060
14061       switch (src_mode)
14062         {
14063         case E_DFmode:
14064           cvt = sext_optab;
14065           hw_convert = hw_conversions[kf_or_tf].from_df;
14066           break;
14067
14068         case E_SFmode:
14069           cvt = sext_optab;
14070           hw_convert = hw_conversions[kf_or_tf].from_sf;
14071           break;
14072
14073         case E_KFmode:
14074         case E_IFmode:
14075         case E_TFmode:
14076           if (FLOAT128_IBM_P (src_mode))
14077             cvt = sext_optab;
14078           else
14079             do_move = true;
14080           break;
14081
14082         case E_SImode:
14083           if (unsigned_p)
14084             {
14085               cvt = ufloat_optab;
14086               hw_convert = hw_conversions[kf_or_tf].from_si_uns;
14087             }
14088           else
14089             {
14090               cvt = sfloat_optab;
14091               hw_convert = hw_conversions[kf_or_tf].from_si_sign;
14092             }
14093           break;
14094
14095         case E_DImode:
14096           if (unsigned_p)
14097             {
14098               cvt = ufloat_optab;
14099               hw_convert = hw_conversions[kf_or_tf].from_di_uns;
14100             }
14101           else
14102             {
14103               cvt = sfloat_optab;
14104               hw_convert = hw_conversions[kf_or_tf].from_di_sign;
14105             }
14106           break;
14107
14108         default:
14109           gcc_unreachable ();
14110         }
14111     }
14112
14113   /* Convert from IEEE 128-bit floating point.  */
14114   else if (FLOAT128_IEEE_P (src_mode))
14115     {
14116       if (src_mode == KFmode)
14117         kf_or_tf = 0;
14118       else if (src_mode == TFmode)
14119         kf_or_tf = 1;
14120       else
14121         gcc_unreachable ();
14122
14123       switch (dest_mode)
14124         {
14125         case E_DFmode:
14126           cvt = trunc_optab;
14127           hw_convert = hw_conversions[kf_or_tf].to_df;
14128           break;
14129
14130         case E_SFmode:
14131           cvt = trunc_optab;
14132           hw_convert = hw_conversions[kf_or_tf].to_sf;
14133           break;
14134
14135         case E_KFmode:
14136         case E_IFmode:
14137         case E_TFmode:
14138           if (FLOAT128_IBM_P (dest_mode))
14139             cvt = trunc_optab;
14140           else
14141             do_move = true;
14142           break;
14143
14144         case E_SImode:
14145           if (unsigned_p)
14146             {
14147               cvt = ufix_optab;
14148               hw_convert = hw_conversions[kf_or_tf].to_si_uns;
14149             }
14150           else
14151             {
14152               cvt = sfix_optab;
14153               hw_convert = hw_conversions[kf_or_tf].to_si_sign;
14154             }
14155           break;
14156
14157         case E_DImode:
14158           if (unsigned_p)
14159             {
14160               cvt = ufix_optab;
14161               hw_convert = hw_conversions[kf_or_tf].to_di_uns;
14162             }
14163           else
14164             {
14165               cvt = sfix_optab;
14166               hw_convert = hw_conversions[kf_or_tf].to_di_sign;
14167             }
14168           break;
14169
14170         default:
14171           gcc_unreachable ();
14172         }
14173     }
14174
14175   /* Both IBM format.  */
14176   else if (FLOAT128_IBM_P (dest_mode) && FLOAT128_IBM_P (src_mode))
14177     do_move = true;
14178
14179   else
14180     gcc_unreachable ();
14181
14182   /* Handle conversion between TFmode/KFmode/IFmode.  */
14183   if (do_move)
14184     emit_insn (gen_rtx_SET (dest, gen_rtx_FLOAT_EXTEND (dest_mode, src)));
14185
14186   /* Handle conversion if we have hardware support.  */
14187   else if (TARGET_FLOAT128_HW && hw_convert)
14188     emit_insn ((hw_convert) (dest, src));
14189
14190   /* Call an external function to do the conversion.  */
14191   else if (cvt != unknown_optab)
14192     {
14193       libfunc = convert_optab_libfunc (cvt, dest_mode, src_mode);
14194       gcc_assert (libfunc != NULL_RTX);
14195
14196       dest2 = emit_library_call_value (libfunc, dest, LCT_CONST, dest_mode,
14197                                        src, src_mode);
14198
14199       gcc_assert (dest2 != NULL_RTX);
14200       if (!rtx_equal_p (dest, dest2))
14201         emit_move_insn (dest, dest2);
14202     }
14203
14204   else
14205     gcc_unreachable ();
14206
14207   return;
14208 }
14209
14210 \f
14211 /* Emit RTL that sets a register to zero if OP1 and OP2 are equal.  SCRATCH
14212    can be used as that dest register.  Return the dest register.  */
14213
14214 rtx
14215 rs6000_emit_eqne (machine_mode mode, rtx op1, rtx op2, rtx scratch)
14216 {
14217   if (op2 == const0_rtx)
14218     return op1;
14219
14220   if (GET_CODE (scratch) == SCRATCH)
14221     scratch = gen_reg_rtx (mode);
14222
14223   if (logical_operand (op2, mode))
14224     emit_insn (gen_rtx_SET (scratch, gen_rtx_XOR (mode, op1, op2)));
14225   else
14226     emit_insn (gen_rtx_SET (scratch,
14227                             gen_rtx_PLUS (mode, op1, negate_rtx (mode, op2))));
14228
14229   return scratch;
14230 }
14231
14232 void
14233 rs6000_emit_sCOND (machine_mode mode, rtx operands[])
14234 {
14235   rtx condition_rtx;
14236   machine_mode op_mode;
14237   enum rtx_code cond_code;
14238   rtx result = operands[0];
14239
14240   condition_rtx = rs6000_generate_compare (operands[1], mode);
14241   cond_code = GET_CODE (condition_rtx);
14242
14243   if (cond_code == NE
14244       || cond_code == GE || cond_code == LE
14245       || cond_code == GEU || cond_code == LEU
14246       || cond_code == ORDERED || cond_code == UNGE || cond_code == UNLE)
14247     {
14248       rtx not_result = gen_reg_rtx (CCEQmode);
14249       rtx not_op, rev_cond_rtx;
14250       machine_mode cc_mode;
14251
14252       cc_mode = GET_MODE (XEXP (condition_rtx, 0));
14253
14254       rev_cond_rtx = gen_rtx_fmt_ee (rs6000_reverse_condition (cc_mode, cond_code),
14255                                      SImode, XEXP (condition_rtx, 0), const0_rtx);
14256       not_op = gen_rtx_COMPARE (CCEQmode, rev_cond_rtx, const0_rtx);
14257       emit_insn (gen_rtx_SET (not_result, not_op));
14258       condition_rtx = gen_rtx_EQ (VOIDmode, not_result, const0_rtx);
14259     }
14260
14261   op_mode = GET_MODE (XEXP (operands[1], 0));
14262   if (op_mode == VOIDmode)
14263     op_mode = GET_MODE (XEXP (operands[1], 1));
14264
14265   if (TARGET_POWERPC64 && (op_mode == DImode || FLOAT_MODE_P (mode)))
14266     {
14267       PUT_MODE (condition_rtx, DImode);
14268       convert_move (result, condition_rtx, 0);
14269     }
14270   else
14271     {
14272       PUT_MODE (condition_rtx, SImode);
14273       emit_insn (gen_rtx_SET (result, condition_rtx));
14274     }
14275 }
14276
14277 /* Emit a branch of kind CODE to location LOC.  */
14278
14279 void
14280 rs6000_emit_cbranch (machine_mode mode, rtx operands[])
14281 {
14282   rtx condition_rtx, loc_ref;
14283
14284   condition_rtx = rs6000_generate_compare (operands[0], mode);
14285   loc_ref = gen_rtx_LABEL_REF (VOIDmode, operands[3]);
14286   emit_jump_insn (gen_rtx_SET (pc_rtx,
14287                                gen_rtx_IF_THEN_ELSE (VOIDmode, condition_rtx,
14288                                                      loc_ref, pc_rtx)));
14289 }
14290
14291 /* Return the string to output a conditional branch to LABEL, which is
14292    the operand template of the label, or NULL if the branch is really a
14293    conditional return.
14294
14295    OP is the conditional expression.  XEXP (OP, 0) is assumed to be a
14296    condition code register and its mode specifies what kind of
14297    comparison we made.
14298
14299    REVERSED is nonzero if we should reverse the sense of the comparison.
14300
14301    INSN is the insn.  */
14302
14303 char *
14304 output_cbranch (rtx op, const char *label, int reversed, rtx_insn *insn)
14305 {
14306   static char string[64];
14307   enum rtx_code code = GET_CODE (op);
14308   rtx cc_reg = XEXP (op, 0);
14309   machine_mode mode = GET_MODE (cc_reg);
14310   int cc_regno = REGNO (cc_reg) - CR0_REGNO;
14311   int need_longbranch = label != NULL && get_attr_length (insn) == 8;
14312   int really_reversed = reversed ^ need_longbranch;
14313   char *s = string;
14314   const char *ccode;
14315   const char *pred;
14316   rtx note;
14317
14318   validate_condition_mode (code, mode);
14319
14320   /* Work out which way this really branches.  We could use
14321      reverse_condition_maybe_unordered here always but this
14322      makes the resulting assembler clearer.  */
14323   if (really_reversed)
14324     {
14325       /* Reversal of FP compares takes care -- an ordered compare
14326          becomes an unordered compare and vice versa.  */
14327       if (mode == CCFPmode)
14328         code = reverse_condition_maybe_unordered (code);
14329       else
14330         code = reverse_condition (code);
14331     }
14332
14333   switch (code)
14334     {
14335       /* Not all of these are actually distinct opcodes, but
14336          we distinguish them for clarity of the resulting assembler.  */
14337     case NE: case LTGT:
14338       ccode = "ne"; break;
14339     case EQ: case UNEQ:
14340       ccode = "eq"; break;
14341     case GE: case GEU:
14342       ccode = "ge"; break;
14343     case GT: case GTU: case UNGT:
14344       ccode = "gt"; break;
14345     case LE: case LEU:
14346       ccode = "le"; break;
14347     case LT: case LTU: case UNLT:
14348       ccode = "lt"; break;
14349     case UNORDERED: ccode = "un"; break;
14350     case ORDERED: ccode = "nu"; break;
14351     case UNGE: ccode = "nl"; break;
14352     case UNLE: ccode = "ng"; break;
14353     default:
14354       gcc_unreachable ();
14355     }
14356
14357   /* Maybe we have a guess as to how likely the branch is.  */
14358   pred = "";
14359   note = find_reg_note (insn, REG_BR_PROB, NULL_RTX);
14360   if (note != NULL_RTX)
14361     {
14362       /* PROB is the difference from 50%.  */
14363       int prob = profile_probability::from_reg_br_prob_note (XINT (note, 0))
14364                    .to_reg_br_prob_base () - REG_BR_PROB_BASE / 2;
14365
14366       /* Only hint for highly probable/improbable branches on newer cpus when
14367          we have real profile data, as static prediction overrides processor
14368          dynamic prediction.  For older cpus we may as well always hint, but
14369          assume not taken for branches that are very close to 50% as a
14370          mispredicted taken branch is more expensive than a
14371          mispredicted not-taken branch.  */
14372       if (rs6000_always_hint
14373           || (abs (prob) > REG_BR_PROB_BASE / 100 * 48
14374               && (profile_status_for_fn (cfun) != PROFILE_GUESSED)
14375               && br_prob_note_reliable_p (note)))
14376         {
14377           if (abs (prob) > REG_BR_PROB_BASE / 20
14378               && ((prob > 0) ^ need_longbranch))
14379             pred = "+";
14380           else
14381             pred = "-";
14382         }
14383     }
14384
14385   if (label == NULL)
14386     s += sprintf (s, "b%slr%s ", ccode, pred);
14387   else
14388     s += sprintf (s, "b%s%s ", ccode, pred);
14389
14390   /* We need to escape any '%' characters in the reg_names string.
14391      Assume they'd only be the first character....  */
14392   if (reg_names[cc_regno + CR0_REGNO][0] == '%')
14393     *s++ = '%';
14394   s += sprintf (s, "%s", reg_names[cc_regno + CR0_REGNO]);
14395
14396   if (label != NULL)
14397     {
14398       /* If the branch distance was too far, we may have to use an
14399          unconditional branch to go the distance.  */
14400       if (need_longbranch)
14401         s += sprintf (s, ",$+8\n\tb %s", label);
14402       else
14403         s += sprintf (s, ",%s", label);
14404     }
14405
14406   return string;
14407 }
14408
14409 /* Return insn for VSX or Altivec comparisons.  */
14410
14411 static rtx
14412 rs6000_emit_vector_compare_inner (enum rtx_code code, rtx op0, rtx op1)
14413 {
14414   rtx mask;
14415   machine_mode mode = GET_MODE (op0);
14416
14417   switch (code)
14418     {
14419     default:
14420       break;
14421
14422     case GE:
14423       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14424         return NULL_RTX;
14425       /* FALLTHRU */
14426
14427     case EQ:
14428     case GT:
14429     case GTU:
14430     case ORDERED:
14431     case UNORDERED:
14432     case UNEQ:
14433     case LTGT:
14434       mask = gen_reg_rtx (mode);
14435       emit_insn (gen_rtx_SET (mask, gen_rtx_fmt_ee (code, mode, op0, op1)));
14436       return mask;
14437     }
14438
14439   return NULL_RTX;
14440 }
14441
14442 /* Emit vector compare for operands OP0 and OP1 using code RCODE.
14443    DMODE is expected destination mode. This is a recursive function.  */
14444
14445 static rtx
14446 rs6000_emit_vector_compare (enum rtx_code rcode,
14447                             rtx op0, rtx op1,
14448                             machine_mode dmode)
14449 {
14450   rtx mask;
14451   bool swap_operands = false;
14452   bool try_again = false;
14453
14454   gcc_assert (VECTOR_UNIT_ALTIVEC_OR_VSX_P (dmode));
14455   gcc_assert (GET_MODE (op0) == GET_MODE (op1));
14456
14457   /* See if the comparison works as is.  */
14458   mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
14459   if (mask)
14460     return mask;
14461
14462   switch (rcode)
14463     {
14464     case LT:
14465       rcode = GT;
14466       swap_operands = true;
14467       try_again = true;
14468       break;
14469     case LTU:
14470       rcode = GTU;
14471       swap_operands = true;
14472       try_again = true;
14473       break;
14474     case NE:
14475     case UNLE:
14476     case UNLT:
14477     case UNGE:
14478     case UNGT:
14479       /* Invert condition and try again.
14480          e.g., A != B becomes ~(A==B).  */
14481       {
14482         enum rtx_code rev_code;
14483         enum insn_code nor_code;
14484         rtx mask2;
14485
14486         rev_code = reverse_condition_maybe_unordered (rcode);
14487         if (rev_code == UNKNOWN)
14488           return NULL_RTX;
14489
14490         nor_code = optab_handler (one_cmpl_optab, dmode);
14491         if (nor_code == CODE_FOR_nothing)
14492           return NULL_RTX;
14493
14494         mask2 = rs6000_emit_vector_compare (rev_code, op0, op1, dmode);
14495         if (!mask2)
14496           return NULL_RTX;
14497
14498         mask = gen_reg_rtx (dmode);
14499         emit_insn (GEN_FCN (nor_code) (mask, mask2));
14500         return mask;
14501       }
14502       break;
14503     case GE:
14504     case GEU:
14505     case LE:
14506     case LEU:
14507       /* Try GT/GTU/LT/LTU OR EQ */
14508       {
14509         rtx c_rtx, eq_rtx;
14510         enum insn_code ior_code;
14511         enum rtx_code new_code;
14512
14513         switch (rcode)
14514           {
14515           case  GE:
14516             new_code = GT;
14517             break;
14518
14519           case GEU:
14520             new_code = GTU;
14521             break;
14522
14523           case LE:
14524             new_code = LT;
14525             break;
14526
14527           case LEU:
14528             new_code = LTU;
14529             break;
14530
14531           default:
14532             gcc_unreachable ();
14533           }
14534
14535         ior_code = optab_handler (ior_optab, dmode);
14536         if (ior_code == CODE_FOR_nothing)
14537           return NULL_RTX;
14538
14539         c_rtx = rs6000_emit_vector_compare (new_code, op0, op1, dmode);
14540         if (!c_rtx)
14541           return NULL_RTX;
14542
14543         eq_rtx = rs6000_emit_vector_compare (EQ, op0, op1, dmode);
14544         if (!eq_rtx)
14545           return NULL_RTX;
14546
14547         mask = gen_reg_rtx (dmode);
14548         emit_insn (GEN_FCN (ior_code) (mask, c_rtx, eq_rtx));
14549         return mask;
14550       }
14551       break;
14552     default:
14553       return NULL_RTX;
14554     }
14555
14556   if (try_again)
14557     {
14558       if (swap_operands)
14559         std::swap (op0, op1);
14560
14561       mask = rs6000_emit_vector_compare_inner (rcode, op0, op1);
14562       if (mask)
14563         return mask;
14564     }
14565
14566   /* You only get two chances.  */
14567   return NULL_RTX;
14568 }
14569
14570 /* Emit vector conditional expression.  DEST is destination. OP_TRUE and
14571    OP_FALSE are two VEC_COND_EXPR operands.  CC_OP0 and CC_OP1 are the two
14572    operands for the relation operation COND.  */
14573
14574 int
14575 rs6000_emit_vector_cond_expr (rtx dest, rtx op_true, rtx op_false,
14576                               rtx cond, rtx cc_op0, rtx cc_op1)
14577 {
14578   machine_mode dest_mode = GET_MODE (dest);
14579   machine_mode mask_mode = GET_MODE (cc_op0);
14580   enum rtx_code rcode = GET_CODE (cond);
14581   machine_mode cc_mode = CCmode;
14582   rtx mask;
14583   rtx cond2;
14584   bool invert_move = false;
14585
14586   if (VECTOR_UNIT_NONE_P (dest_mode))
14587     return 0;
14588
14589   gcc_assert (GET_MODE_SIZE (dest_mode) == GET_MODE_SIZE (mask_mode)
14590               && GET_MODE_NUNITS (dest_mode) == GET_MODE_NUNITS (mask_mode));
14591
14592   switch (rcode)
14593     {
14594       /* Swap operands if we can, and fall back to doing the operation as
14595          specified, and doing a NOR to invert the test.  */
14596     case NE:
14597     case UNLE:
14598     case UNLT:
14599     case UNGE:
14600     case UNGT:
14601       /* Invert condition and try again.
14602          e.g., A  = (B != C) ? D : E becomes A = (B == C) ? E : D.  */
14603       invert_move = true;
14604       rcode = reverse_condition_maybe_unordered (rcode);
14605       if (rcode == UNKNOWN)
14606         return 0;
14607       break;
14608
14609     case GE:
14610     case LE:
14611       if (GET_MODE_CLASS (mask_mode) == MODE_VECTOR_INT)
14612         {
14613           /* Invert condition to avoid compound test.  */
14614           invert_move = true;
14615           rcode = reverse_condition (rcode);
14616         }
14617       break;
14618
14619     case GTU:
14620     case GEU:
14621     case LTU:
14622     case LEU:
14623       /* Mark unsigned tests with CCUNSmode.  */
14624       cc_mode = CCUNSmode;
14625
14626       /* Invert condition to avoid compound test if necessary.  */
14627       if (rcode == GEU || rcode == LEU)
14628         {
14629           invert_move = true;
14630           rcode = reverse_condition (rcode);
14631         }
14632       break;
14633
14634     default:
14635       break;
14636     }
14637
14638   /* Get the vector mask for the given relational operations.  */
14639   mask = rs6000_emit_vector_compare (rcode, cc_op0, cc_op1, mask_mode);
14640
14641   if (!mask)
14642     return 0;
14643
14644   if (invert_move)
14645     std::swap (op_true, op_false);
14646
14647   /* Optimize vec1 == vec2, to know the mask generates -1/0.  */
14648   if (GET_MODE_CLASS (dest_mode) == MODE_VECTOR_INT
14649       && (GET_CODE (op_true) == CONST_VECTOR
14650           || GET_CODE (op_false) == CONST_VECTOR))
14651     {
14652       rtx constant_0 = CONST0_RTX (dest_mode);
14653       rtx constant_m1 = CONSTM1_RTX (dest_mode);
14654
14655       if (op_true == constant_m1 && op_false == constant_0)
14656         {
14657           emit_move_insn (dest, mask);
14658           return 1;
14659         }
14660
14661       else if (op_true == constant_0 && op_false == constant_m1)
14662         {
14663           emit_insn (gen_rtx_SET (dest, gen_rtx_NOT (dest_mode, mask)));
14664           return 1;
14665         }
14666
14667       /* If we can't use the vector comparison directly, perhaps we can use
14668          the mask for the true or false fields, instead of loading up a
14669          constant.  */
14670       if (op_true == constant_m1)
14671         op_true = mask;
14672
14673       if (op_false == constant_0)
14674         op_false = mask;
14675     }
14676
14677   if (!REG_P (op_true) && !SUBREG_P (op_true))
14678     op_true = force_reg (dest_mode, op_true);
14679
14680   if (!REG_P (op_false) && !SUBREG_P (op_false))
14681     op_false = force_reg (dest_mode, op_false);
14682
14683   cond2 = gen_rtx_fmt_ee (NE, cc_mode, gen_lowpart (dest_mode, mask),
14684                           CONST0_RTX (dest_mode));
14685   emit_insn (gen_rtx_SET (dest,
14686                           gen_rtx_IF_THEN_ELSE (dest_mode,
14687                                                 cond2,
14688                                                 op_true,
14689                                                 op_false)));
14690   return 1;
14691 }
14692
14693 /* ISA 3.0 (power9) minmax subcase to emit a XSMAXCDP or XSMINCDP instruction
14694    for SF/DF scalars.  Move TRUE_COND to DEST if OP of the operands of the last
14695    comparison is nonzero/true, FALSE_COND if it is zero/false.  Return 0 if the
14696    hardware has no such operation.  */
14697
14698 static int
14699 rs6000_emit_p9_fp_minmax (rtx dest, rtx op, rtx true_cond, rtx false_cond)
14700 {
14701   enum rtx_code code = GET_CODE (op);
14702   rtx op0 = XEXP (op, 0);
14703   rtx op1 = XEXP (op, 1);
14704   machine_mode compare_mode = GET_MODE (op0);
14705   machine_mode result_mode = GET_MODE (dest);
14706   bool max_p = false;
14707
14708   if (result_mode != compare_mode)
14709     return 0;
14710
14711   if (code == GE || code == GT)
14712     max_p = true;
14713   else if (code == LE || code == LT)
14714     max_p = false;
14715   else
14716     return 0;
14717
14718   if (rtx_equal_p (op0, true_cond) && rtx_equal_p (op1, false_cond))
14719     ;
14720
14721   else if (rtx_equal_p (op1, true_cond) && rtx_equal_p (op0, false_cond))
14722     max_p = !max_p;
14723
14724   else
14725     return 0;
14726
14727   rs6000_emit_minmax (dest, max_p ? SMAX : SMIN, op0, op1);
14728   return 1;
14729 }
14730
14731 /* ISA 3.0 (power9) conditional move subcase to emit XSCMP{EQ,GE,GT,NE}DP and
14732    XXSEL instructions for SF/DF scalars.  Move TRUE_COND to DEST if OP of the
14733    operands of the last comparison is nonzero/true, FALSE_COND if it is
14734    zero/false.  Return 0 if the hardware has no such operation.  */
14735
14736 static int
14737 rs6000_emit_p9_fp_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
14738 {
14739   enum rtx_code code = GET_CODE (op);
14740   rtx op0 = XEXP (op, 0);
14741   rtx op1 = XEXP (op, 1);
14742   machine_mode result_mode = GET_MODE (dest);
14743   rtx compare_rtx;
14744   rtx cmove_rtx;
14745   rtx clobber_rtx;
14746
14747   if (!can_create_pseudo_p ())
14748     return 0;
14749
14750   switch (code)
14751     {
14752     case EQ:
14753     case GE:
14754     case GT:
14755       break;
14756
14757     case NE:
14758     case LT:
14759     case LE:
14760       code = swap_condition (code);
14761       std::swap (op0, op1);
14762       break;
14763
14764     default:
14765       return 0;
14766     }
14767
14768   /* Generate:  [(parallel [(set (dest)
14769                                  (if_then_else (op (cmp1) (cmp2))
14770                                                (true)
14771                                                (false)))
14772                             (clobber (scratch))])].  */
14773
14774   compare_rtx = gen_rtx_fmt_ee (code, CCFPmode, op0, op1);
14775   cmove_rtx = gen_rtx_SET (dest,
14776                            gen_rtx_IF_THEN_ELSE (result_mode,
14777                                                  compare_rtx,
14778                                                  true_cond,
14779                                                  false_cond));
14780
14781   clobber_rtx = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (V2DImode));
14782   emit_insn (gen_rtx_PARALLEL (VOIDmode,
14783                                gen_rtvec (2, cmove_rtx, clobber_rtx)));
14784
14785   return 1;
14786 }
14787
14788 /* Emit a conditional move: move TRUE_COND to DEST if OP of the
14789    operands of the last comparison is nonzero/true, FALSE_COND if it
14790    is zero/false.  Return 0 if the hardware has no such operation.  */
14791
14792 int
14793 rs6000_emit_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
14794 {
14795   enum rtx_code code = GET_CODE (op);
14796   rtx op0 = XEXP (op, 0);
14797   rtx op1 = XEXP (op, 1);
14798   machine_mode compare_mode = GET_MODE (op0);
14799   machine_mode result_mode = GET_MODE (dest);
14800   rtx temp;
14801   bool is_against_zero;
14802
14803   /* These modes should always match.  */
14804   if (GET_MODE (op1) != compare_mode
14805       /* In the isel case however, we can use a compare immediate, so
14806          op1 may be a small constant.  */
14807       && (!TARGET_ISEL || !short_cint_operand (op1, VOIDmode)))
14808     return 0;
14809   if (GET_MODE (true_cond) != result_mode)
14810     return 0;
14811   if (GET_MODE (false_cond) != result_mode)
14812     return 0;
14813
14814   /* See if we can use the ISA 3.0 (power9) min/max/compare functions.  */
14815   if (TARGET_P9_MINMAX
14816       && (compare_mode == SFmode || compare_mode == DFmode)
14817       && (result_mode == SFmode || result_mode == DFmode))
14818     {
14819       if (rs6000_emit_p9_fp_minmax (dest, op, true_cond, false_cond))
14820         return 1;
14821
14822       if (rs6000_emit_p9_fp_cmove (dest, op, true_cond, false_cond))
14823         return 1;
14824     }
14825
14826   /* Don't allow using floating point comparisons for integer results for
14827      now.  */
14828   if (FLOAT_MODE_P (compare_mode) && !FLOAT_MODE_P (result_mode))
14829     return 0;
14830
14831   /* First, work out if the hardware can do this at all, or
14832      if it's too slow....  */
14833   if (!FLOAT_MODE_P (compare_mode))
14834     {
14835       if (TARGET_ISEL)
14836         return rs6000_emit_int_cmove (dest, op, true_cond, false_cond);
14837       return 0;
14838     }
14839
14840   is_against_zero = op1 == CONST0_RTX (compare_mode);
14841
14842   /* A floating-point subtract might overflow, underflow, or produce
14843      an inexact result, thus changing the floating-point flags, so it
14844      can't be generated if we care about that.  It's safe if one side
14845      of the construct is zero, since then no subtract will be
14846      generated.  */
14847   if (SCALAR_FLOAT_MODE_P (compare_mode)
14848       && flag_trapping_math && ! is_against_zero)
14849     return 0;
14850
14851   /* Eliminate half of the comparisons by switching operands, this
14852      makes the remaining code simpler.  */
14853   if (code == UNLT || code == UNGT || code == UNORDERED || code == NE
14854       || code == LTGT || code == LT || code == UNLE)
14855     {
14856       code = reverse_condition_maybe_unordered (code);
14857       temp = true_cond;
14858       true_cond = false_cond;
14859       false_cond = temp;
14860     }
14861
14862   /* UNEQ and LTGT take four instructions for a comparison with zero,
14863      it'll probably be faster to use a branch here too.  */
14864   if (code == UNEQ && HONOR_NANS (compare_mode))
14865     return 0;
14866
14867   /* We're going to try to implement comparisons by performing
14868      a subtract, then comparing against zero.  Unfortunately,
14869      Inf - Inf is NaN which is not zero, and so if we don't
14870      know that the operand is finite and the comparison
14871      would treat EQ different to UNORDERED, we can't do it.  */
14872   if (HONOR_INFINITIES (compare_mode)
14873       && code != GT && code != UNGE
14874       && (!CONST_DOUBLE_P (op1)
14875           || real_isinf (CONST_DOUBLE_REAL_VALUE (op1)))
14876       /* Constructs of the form (a OP b ? a : b) are safe.  */
14877       && ((! rtx_equal_p (op0, false_cond) && ! rtx_equal_p (op1, false_cond))
14878           || (! rtx_equal_p (op0, true_cond)
14879               && ! rtx_equal_p (op1, true_cond))))
14880     return 0;
14881
14882   /* At this point we know we can use fsel.  */
14883
14884   /* Reduce the comparison to a comparison against zero.  */
14885   if (! is_against_zero)
14886     {
14887       temp = gen_reg_rtx (compare_mode);
14888       emit_insn (gen_rtx_SET (temp, gen_rtx_MINUS (compare_mode, op0, op1)));
14889       op0 = temp;
14890       op1 = CONST0_RTX (compare_mode);
14891     }
14892
14893   /* If we don't care about NaNs we can reduce some of the comparisons
14894      down to faster ones.  */
14895   if (! HONOR_NANS (compare_mode))
14896     switch (code)
14897       {
14898       case GT:
14899         code = LE;
14900         temp = true_cond;
14901         true_cond = false_cond;
14902         false_cond = temp;
14903         break;
14904       case UNGE:
14905         code = GE;
14906         break;
14907       case UNEQ:
14908         code = EQ;
14909         break;
14910       default:
14911         break;
14912       }
14913
14914   /* Now, reduce everything down to a GE.  */
14915   switch (code)
14916     {
14917     case GE:
14918       break;
14919
14920     case LE:
14921       temp = gen_reg_rtx (compare_mode);
14922       emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
14923       op0 = temp;
14924       break;
14925
14926     case ORDERED:
14927       temp = gen_reg_rtx (compare_mode);
14928       emit_insn (gen_rtx_SET (temp, gen_rtx_ABS (compare_mode, op0)));
14929       op0 = temp;
14930       break;
14931
14932     case EQ:
14933       temp = gen_reg_rtx (compare_mode);
14934       emit_insn (gen_rtx_SET (temp,
14935                               gen_rtx_NEG (compare_mode,
14936                                            gen_rtx_ABS (compare_mode, op0))));
14937       op0 = temp;
14938       break;
14939
14940     case UNGE:
14941       /* a UNGE 0 <-> (a GE 0 || -a UNLT 0) */
14942       temp = gen_reg_rtx (result_mode);
14943       emit_insn (gen_rtx_SET (temp,
14944                               gen_rtx_IF_THEN_ELSE (result_mode,
14945                                                     gen_rtx_GE (VOIDmode,
14946                                                                 op0, op1),
14947                                                     true_cond, false_cond)));
14948       false_cond = true_cond;
14949       true_cond = temp;
14950
14951       temp = gen_reg_rtx (compare_mode);
14952       emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
14953       op0 = temp;
14954       break;
14955
14956     case GT:
14957       /* a GT 0 <-> (a GE 0 && -a UNLT 0) */
14958       temp = gen_reg_rtx (result_mode);
14959       emit_insn (gen_rtx_SET (temp,
14960                               gen_rtx_IF_THEN_ELSE (result_mode,
14961                                                     gen_rtx_GE (VOIDmode,
14962                                                                 op0, op1),
14963                                                     true_cond, false_cond)));
14964       true_cond = false_cond;
14965       false_cond = temp;
14966
14967       temp = gen_reg_rtx (compare_mode);
14968       emit_insn (gen_rtx_SET (temp, gen_rtx_NEG (compare_mode, op0)));
14969       op0 = temp;
14970       break;
14971
14972     default:
14973       gcc_unreachable ();
14974     }
14975
14976   emit_insn (gen_rtx_SET (dest,
14977                           gen_rtx_IF_THEN_ELSE (result_mode,
14978                                                 gen_rtx_GE (VOIDmode,
14979                                                             op0, op1),
14980                                                 true_cond, false_cond)));
14981   return 1;
14982 }
14983
14984 /* Same as above, but for ints (isel).  */
14985
14986 int
14987 rs6000_emit_int_cmove (rtx dest, rtx op, rtx true_cond, rtx false_cond)
14988 {
14989   rtx condition_rtx, cr;
14990   machine_mode mode = GET_MODE (dest);
14991   enum rtx_code cond_code;
14992   rtx (*isel_func) (rtx, rtx, rtx, rtx, rtx);
14993   bool signedp;
14994
14995   if (mode != SImode && (!TARGET_POWERPC64 || mode != DImode))
14996     return 0;
14997
14998   /* We still have to do the compare, because isel doesn't do a
14999      compare, it just looks at the CRx bits set by a previous compare
15000      instruction.  */
15001   condition_rtx = rs6000_generate_compare (op, mode);
15002   cond_code = GET_CODE (condition_rtx);
15003   cr = XEXP (condition_rtx, 0);
15004   signedp = GET_MODE (cr) == CCmode;
15005
15006   isel_func = (mode == SImode
15007                ? (signedp ? gen_isel_signed_si : gen_isel_unsigned_si)
15008                : (signedp ? gen_isel_signed_di : gen_isel_unsigned_di));
15009
15010   switch (cond_code)
15011     {
15012     case LT: case GT: case LTU: case GTU: case EQ:
15013       /* isel handles these directly.  */
15014       break;
15015
15016     default:
15017       /* We need to swap the sense of the comparison.  */
15018       {
15019         std::swap (false_cond, true_cond);
15020         PUT_CODE (condition_rtx, reverse_condition (cond_code));
15021       }
15022       break;
15023     }
15024
15025   false_cond = force_reg (mode, false_cond);
15026   if (true_cond != const0_rtx)
15027     true_cond = force_reg (mode, true_cond);
15028
15029   emit_insn (isel_func (dest, condition_rtx, true_cond, false_cond, cr));
15030
15031   return 1;
15032 }
15033
15034 void
15035 rs6000_emit_minmax (rtx dest, enum rtx_code code, rtx op0, rtx op1)
15036 {
15037   machine_mode mode = GET_MODE (op0);
15038   enum rtx_code c;
15039   rtx target;
15040
15041   /* VSX/altivec have direct min/max insns.  */
15042   if ((code == SMAX || code == SMIN)
15043       && (VECTOR_UNIT_ALTIVEC_OR_VSX_P (mode)
15044           || (mode == SFmode && VECTOR_UNIT_VSX_P (DFmode))))
15045     {
15046       emit_insn (gen_rtx_SET (dest, gen_rtx_fmt_ee (code, mode, op0, op1)));
15047       return;
15048     }
15049
15050   if (code == SMAX || code == SMIN)
15051     c = GE;
15052   else
15053     c = GEU;
15054
15055   if (code == SMAX || code == UMAX)
15056     target = emit_conditional_move (dest, c, op0, op1, mode,
15057                                     op0, op1, mode, 0);
15058   else
15059     target = emit_conditional_move (dest, c, op0, op1, mode,
15060                                     op1, op0, mode, 0);
15061   gcc_assert (target);
15062   if (target != dest)
15063     emit_move_insn (dest, target);
15064 }
15065
15066 /* A subroutine of the atomic operation splitters.  Jump to LABEL if
15067    COND is true.  Mark the jump as unlikely to be taken.  */
15068
15069 static void
15070 emit_unlikely_jump (rtx cond, rtx label)
15071 {
15072   rtx x = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, label, pc_rtx);
15073   rtx_insn *insn = emit_jump_insn (gen_rtx_SET (pc_rtx, x));
15074   add_reg_br_prob_note (insn, profile_probability::very_unlikely ());
15075 }
15076
15077 /* A subroutine of the atomic operation splitters.  Emit a load-locked
15078    instruction in MODE.  For QI/HImode, possibly use a pattern than includes
15079    the zero_extend operation.  */
15080
15081 static void
15082 emit_load_locked (machine_mode mode, rtx reg, rtx mem)
15083 {
15084   rtx (*fn) (rtx, rtx) = NULL;
15085
15086   switch (mode)
15087     {
15088     case E_QImode:
15089       fn = gen_load_lockedqi;
15090       break;
15091     case E_HImode:
15092       fn = gen_load_lockedhi;
15093       break;
15094     case E_SImode:
15095       if (GET_MODE (mem) == QImode)
15096         fn = gen_load_lockedqi_si;
15097       else if (GET_MODE (mem) == HImode)
15098         fn = gen_load_lockedhi_si;
15099       else
15100         fn = gen_load_lockedsi;
15101       break;
15102     case E_DImode:
15103       fn = gen_load_lockeddi;
15104       break;
15105     case E_TImode:
15106       fn = gen_load_lockedti;
15107       break;
15108     default:
15109       gcc_unreachable ();
15110     }
15111   emit_insn (fn (reg, mem));
15112 }
15113
15114 /* A subroutine of the atomic operation splitters.  Emit a store-conditional
15115    instruction in MODE.  */
15116
15117 static void
15118 emit_store_conditional (machine_mode mode, rtx res, rtx mem, rtx val)
15119 {
15120   rtx (*fn) (rtx, rtx, rtx) = NULL;
15121
15122   switch (mode)
15123     {
15124     case E_QImode:
15125       fn = gen_store_conditionalqi;
15126       break;
15127     case E_HImode:
15128       fn = gen_store_conditionalhi;
15129       break;
15130     case E_SImode:
15131       fn = gen_store_conditionalsi;
15132       break;
15133     case E_DImode:
15134       fn = gen_store_conditionaldi;
15135       break;
15136     case E_TImode:
15137       fn = gen_store_conditionalti;
15138       break;
15139     default:
15140       gcc_unreachable ();
15141     }
15142
15143   /* Emit sync before stwcx. to address PPC405 Erratum.  */
15144   if (PPC405_ERRATUM77)
15145     emit_insn (gen_hwsync ());
15146
15147   emit_insn (fn (res, mem, val));
15148 }
15149
15150 /* Expand barriers before and after a load_locked/store_cond sequence.  */
15151
15152 static rtx
15153 rs6000_pre_atomic_barrier (rtx mem, enum memmodel model)
15154 {
15155   rtx addr = XEXP (mem, 0);
15156
15157   if (!legitimate_indirect_address_p (addr, reload_completed)
15158       && !legitimate_indexed_address_p (addr, reload_completed))
15159     {
15160       addr = force_reg (Pmode, addr);
15161       mem = replace_equiv_address_nv (mem, addr);
15162     }
15163
15164   switch (model)
15165     {
15166     case MEMMODEL_RELAXED:
15167     case MEMMODEL_CONSUME:
15168     case MEMMODEL_ACQUIRE:
15169       break;
15170     case MEMMODEL_RELEASE:
15171     case MEMMODEL_ACQ_REL:
15172       emit_insn (gen_lwsync ());
15173       break;
15174     case MEMMODEL_SEQ_CST:
15175       emit_insn (gen_hwsync ());
15176       break;
15177     default:
15178       gcc_unreachable ();
15179     }
15180   return mem;
15181 }
15182
15183 static void
15184 rs6000_post_atomic_barrier (enum memmodel model)
15185 {
15186   switch (model)
15187     {
15188     case MEMMODEL_RELAXED:
15189     case MEMMODEL_CONSUME:
15190     case MEMMODEL_RELEASE:
15191       break;
15192     case MEMMODEL_ACQUIRE:
15193     case MEMMODEL_ACQ_REL:
15194     case MEMMODEL_SEQ_CST:
15195       emit_insn (gen_isync ());
15196       break;
15197     default:
15198       gcc_unreachable ();
15199     }
15200 }
15201
15202 /* A subroutine of the various atomic expanders.  For sub-word operations,
15203    we must adjust things to operate on SImode.  Given the original MEM,
15204    return a new aligned memory.  Also build and return the quantities by
15205    which to shift and mask.  */
15206
15207 static rtx
15208 rs6000_adjust_atomic_subword (rtx orig_mem, rtx *pshift, rtx *pmask)
15209 {
15210   rtx addr, align, shift, mask, mem;
15211   HOST_WIDE_INT shift_mask;
15212   machine_mode mode = GET_MODE (orig_mem);
15213
15214   /* For smaller modes, we have to implement this via SImode.  */
15215   shift_mask = (mode == QImode ? 0x18 : 0x10);
15216
15217   addr = XEXP (orig_mem, 0);
15218   addr = force_reg (GET_MODE (addr), addr);
15219
15220   /* Aligned memory containing subword.  Generate a new memory.  We
15221      do not want any of the existing MEM_ATTR data, as we're now
15222      accessing memory outside the original object.  */
15223   align = expand_simple_binop (Pmode, AND, addr, GEN_INT (-4),
15224                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
15225   mem = gen_rtx_MEM (SImode, align);
15226   MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (orig_mem);
15227   if (MEM_ALIAS_SET (orig_mem) == ALIAS_SET_MEMORY_BARRIER)
15228     set_mem_alias_set (mem, ALIAS_SET_MEMORY_BARRIER);
15229
15230   /* Shift amount for subword relative to aligned word.  */
15231   shift = gen_reg_rtx (SImode);
15232   addr = gen_lowpart (SImode, addr);
15233   rtx tmp = gen_reg_rtx (SImode);
15234   emit_insn (gen_ashlsi3 (tmp, addr, GEN_INT (3)));
15235   emit_insn (gen_andsi3 (shift, tmp, GEN_INT (shift_mask)));
15236   if (BYTES_BIG_ENDIAN)
15237     shift = expand_simple_binop (SImode, XOR, shift, GEN_INT (shift_mask),
15238                                  shift, 1, OPTAB_LIB_WIDEN);
15239   *pshift = shift;
15240
15241   /* Mask for insertion.  */
15242   mask = expand_simple_binop (SImode, ASHIFT, GEN_INT (GET_MODE_MASK (mode)),
15243                               shift, NULL_RTX, 1, OPTAB_LIB_WIDEN);
15244   *pmask = mask;
15245
15246   return mem;
15247 }
15248
15249 /* A subroutine of the various atomic expanders.  For sub-word operands,
15250    combine OLDVAL and NEWVAL via MASK.  Returns a new pseduo.  */
15251
15252 static rtx
15253 rs6000_mask_atomic_subword (rtx oldval, rtx newval, rtx mask)
15254 {
15255   rtx x;
15256
15257   x = gen_reg_rtx (SImode);
15258   emit_insn (gen_rtx_SET (x, gen_rtx_AND (SImode,
15259                                           gen_rtx_NOT (SImode, mask),
15260                                           oldval)));
15261
15262   x = expand_simple_binop (SImode, IOR, newval, x, x, 1, OPTAB_LIB_WIDEN);
15263
15264   return x;
15265 }
15266
15267 /* A subroutine of the various atomic expanders.  For sub-word operands,
15268    extract WIDE to NARROW via SHIFT.  */
15269
15270 static void
15271 rs6000_finish_atomic_subword (rtx narrow, rtx wide, rtx shift)
15272 {
15273   wide = expand_simple_binop (SImode, LSHIFTRT, wide, shift,
15274                               wide, 1, OPTAB_LIB_WIDEN);
15275   emit_move_insn (narrow, gen_lowpart (GET_MODE (narrow), wide));
15276 }
15277
15278 /* Expand an atomic compare and swap operation.  */
15279
15280 void
15281 rs6000_expand_atomic_compare_and_swap (rtx operands[])
15282 {
15283   rtx boolval, retval, mem, oldval, newval, cond;
15284   rtx label1, label2, x, mask, shift;
15285   machine_mode mode, orig_mode;
15286   enum memmodel mod_s, mod_f;
15287   bool is_weak;
15288
15289   boolval = operands[0];
15290   retval = operands[1];
15291   mem = operands[2];
15292   oldval = operands[3];
15293   newval = operands[4];
15294   is_weak = (INTVAL (operands[5]) != 0);
15295   mod_s = memmodel_base (INTVAL (operands[6]));
15296   mod_f = memmodel_base (INTVAL (operands[7]));
15297   orig_mode = mode = GET_MODE (mem);
15298
15299   mask = shift = NULL_RTX;
15300   if (mode == QImode || mode == HImode)
15301     {
15302       /* Before power8, we didn't have access to lbarx/lharx, so generate a
15303          lwarx and shift/mask operations.  With power8, we need to do the
15304          comparison in SImode, but the store is still done in QI/HImode.  */
15305       oldval = convert_modes (SImode, mode, oldval, 1);
15306
15307       if (!TARGET_SYNC_HI_QI)
15308         {
15309           mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
15310
15311           /* Shift and mask OLDVAL into position with the word.  */
15312           oldval = expand_simple_binop (SImode, ASHIFT, oldval, shift,
15313                                         NULL_RTX, 1, OPTAB_LIB_WIDEN);
15314
15315           /* Shift and mask NEWVAL into position within the word.  */
15316           newval = convert_modes (SImode, mode, newval, 1);
15317           newval = expand_simple_binop (SImode, ASHIFT, newval, shift,
15318                                         NULL_RTX, 1, OPTAB_LIB_WIDEN);
15319         }
15320
15321       /* Prepare to adjust the return value.  */
15322       retval = gen_reg_rtx (SImode);
15323       mode = SImode;
15324     }
15325   else if (reg_overlap_mentioned_p (retval, oldval))
15326     oldval = copy_to_reg (oldval);
15327
15328   if (mode != TImode && !reg_or_short_operand (oldval, mode))
15329     oldval = copy_to_mode_reg (mode, oldval);
15330
15331   if (reg_overlap_mentioned_p (retval, newval))
15332     newval = copy_to_reg (newval);
15333
15334   mem = rs6000_pre_atomic_barrier (mem, mod_s);
15335
15336   label1 = NULL_RTX;
15337   if (!is_weak)
15338     {
15339       label1 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
15340       emit_label (XEXP (label1, 0));
15341     }
15342   label2 = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
15343
15344   emit_load_locked (mode, retval, mem);
15345
15346   x = retval;
15347   if (mask)
15348     x = expand_simple_binop (SImode, AND, retval, mask,
15349                              NULL_RTX, 1, OPTAB_LIB_WIDEN);
15350
15351   cond = gen_reg_rtx (CCmode);
15352   /* If we have TImode, synthesize a comparison.  */
15353   if (mode != TImode)
15354     x = gen_rtx_COMPARE (CCmode, x, oldval);
15355   else
15356     {
15357       rtx xor1_result = gen_reg_rtx (DImode);
15358       rtx xor2_result = gen_reg_rtx (DImode);
15359       rtx or_result = gen_reg_rtx (DImode);
15360       rtx new_word0 = simplify_gen_subreg (DImode, x, TImode, 0);
15361       rtx new_word1 = simplify_gen_subreg (DImode, x, TImode, 8);
15362       rtx old_word0 = simplify_gen_subreg (DImode, oldval, TImode, 0);
15363       rtx old_word1 = simplify_gen_subreg (DImode, oldval, TImode, 8);
15364
15365       emit_insn (gen_xordi3 (xor1_result, new_word0, old_word0));
15366       emit_insn (gen_xordi3 (xor2_result, new_word1, old_word1));
15367       emit_insn (gen_iordi3 (or_result, xor1_result, xor2_result));
15368       x = gen_rtx_COMPARE (CCmode, or_result, const0_rtx);
15369     }
15370
15371   emit_insn (gen_rtx_SET (cond, x));
15372
15373   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
15374   emit_unlikely_jump (x, label2);
15375
15376   x = newval;
15377   if (mask)
15378     x = rs6000_mask_atomic_subword (retval, newval, mask);
15379
15380   emit_store_conditional (orig_mode, cond, mem, x);
15381
15382   if (!is_weak)
15383     {
15384       x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
15385       emit_unlikely_jump (x, label1);
15386     }
15387
15388   if (!is_mm_relaxed (mod_f))
15389     emit_label (XEXP (label2, 0));
15390
15391   rs6000_post_atomic_barrier (mod_s);
15392
15393   if (is_mm_relaxed (mod_f))
15394     emit_label (XEXP (label2, 0));
15395
15396   if (shift)
15397     rs6000_finish_atomic_subword (operands[1], retval, shift);
15398   else if (mode != GET_MODE (operands[1]))
15399     convert_move (operands[1], retval, 1);
15400
15401   /* In all cases, CR0 contains EQ on success, and NE on failure.  */
15402   x = gen_rtx_EQ (SImode, cond, const0_rtx);
15403   emit_insn (gen_rtx_SET (boolval, x));
15404 }
15405
15406 /* Expand an atomic exchange operation.  */
15407
15408 void
15409 rs6000_expand_atomic_exchange (rtx operands[])
15410 {
15411   rtx retval, mem, val, cond;
15412   machine_mode mode;
15413   enum memmodel model;
15414   rtx label, x, mask, shift;
15415
15416   retval = operands[0];
15417   mem = operands[1];
15418   val = operands[2];
15419   model = memmodel_base (INTVAL (operands[3]));
15420   mode = GET_MODE (mem);
15421
15422   mask = shift = NULL_RTX;
15423   if (!TARGET_SYNC_HI_QI && (mode == QImode || mode == HImode))
15424     {
15425       mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
15426
15427       /* Shift and mask VAL into position with the word.  */
15428       val = convert_modes (SImode, mode, val, 1);
15429       val = expand_simple_binop (SImode, ASHIFT, val, shift,
15430                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
15431
15432       /* Prepare to adjust the return value.  */
15433       retval = gen_reg_rtx (SImode);
15434       mode = SImode;
15435     }
15436
15437   mem = rs6000_pre_atomic_barrier (mem, model);
15438
15439   label = gen_rtx_LABEL_REF (VOIDmode, gen_label_rtx ());
15440   emit_label (XEXP (label, 0));
15441
15442   emit_load_locked (mode, retval, mem);
15443
15444   x = val;
15445   if (mask)
15446     x = rs6000_mask_atomic_subword (retval, val, mask);
15447
15448   cond = gen_reg_rtx (CCmode);
15449   emit_store_conditional (mode, cond, mem, x);
15450
15451   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
15452   emit_unlikely_jump (x, label);
15453
15454   rs6000_post_atomic_barrier (model);
15455
15456   if (shift)
15457     rs6000_finish_atomic_subword (operands[0], retval, shift);
15458 }
15459
15460 /* Expand an atomic fetch-and-operate pattern.  CODE is the binary operation
15461    to perform.  MEM is the memory on which to operate.  VAL is the second
15462    operand of the binary operator.  BEFORE and AFTER are optional locations to
15463    return the value of MEM either before of after the operation.  MODEL_RTX
15464    is a CONST_INT containing the memory model to use.  */
15465
15466 void
15467 rs6000_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
15468                          rtx orig_before, rtx orig_after, rtx model_rtx)
15469 {
15470   enum memmodel model = memmodel_base (INTVAL (model_rtx));
15471   machine_mode mode = GET_MODE (mem);
15472   machine_mode store_mode = mode;
15473   rtx label, x, cond, mask, shift;
15474   rtx before = orig_before, after = orig_after;
15475
15476   mask = shift = NULL_RTX;
15477   /* On power8, we want to use SImode for the operation.  On previous systems,
15478      use the operation in a subword and shift/mask to get the proper byte or
15479      halfword.  */
15480   if (mode == QImode || mode == HImode)
15481     {
15482       if (TARGET_SYNC_HI_QI)
15483         {
15484           val = convert_modes (SImode, mode, val, 1);
15485
15486           /* Prepare to adjust the return value.  */
15487           before = gen_reg_rtx (SImode);
15488           if (after)
15489             after = gen_reg_rtx (SImode);
15490           mode = SImode;
15491         }
15492       else
15493         {
15494           mem = rs6000_adjust_atomic_subword (mem, &shift, &mask);
15495
15496           /* Shift and mask VAL into position with the word.  */
15497           val = convert_modes (SImode, mode, val, 1);
15498           val = expand_simple_binop (SImode, ASHIFT, val, shift,
15499                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
15500
15501           switch (code)
15502             {
15503             case IOR:
15504             case XOR:
15505               /* We've already zero-extended VAL.  That is sufficient to
15506                  make certain that it does not affect other bits.  */
15507               mask = NULL;
15508               break;
15509
15510             case AND:
15511               /* If we make certain that all of the other bits in VAL are
15512                  set, that will be sufficient to not affect other bits.  */
15513               x = gen_rtx_NOT (SImode, mask);
15514               x = gen_rtx_IOR (SImode, x, val);
15515               emit_insn (gen_rtx_SET (val, x));
15516               mask = NULL;
15517               break;
15518
15519             case NOT:
15520             case PLUS:
15521             case MINUS:
15522               /* These will all affect bits outside the field and need
15523                  adjustment via MASK within the loop.  */
15524               break;
15525
15526             default:
15527               gcc_unreachable ();
15528             }
15529
15530           /* Prepare to adjust the return value.  */
15531           before = gen_reg_rtx (SImode);
15532           if (after)
15533             after = gen_reg_rtx (SImode);
15534           store_mode = mode = SImode;
15535         }
15536     }
15537
15538   mem = rs6000_pre_atomic_barrier (mem, model);
15539
15540   label = gen_label_rtx ();
15541   emit_label (label);
15542   label = gen_rtx_LABEL_REF (VOIDmode, label);
15543
15544   if (before == NULL_RTX)
15545     before = gen_reg_rtx (mode);
15546
15547   emit_load_locked (mode, before, mem);
15548
15549   if (code == NOT)
15550     {
15551       x = expand_simple_binop (mode, AND, before, val,
15552                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
15553       after = expand_simple_unop (mode, NOT, x, after, 1);
15554     }
15555   else
15556     {
15557       after = expand_simple_binop (mode, code, before, val,
15558                                    after, 1, OPTAB_LIB_WIDEN);
15559     }
15560
15561   x = after;
15562   if (mask)
15563     {
15564       x = expand_simple_binop (SImode, AND, after, mask,
15565                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
15566       x = rs6000_mask_atomic_subword (before, x, mask);
15567     }
15568   else if (store_mode != mode)
15569     x = convert_modes (store_mode, mode, x, 1);
15570
15571   cond = gen_reg_rtx (CCmode);
15572   emit_store_conditional (store_mode, cond, mem, x);
15573
15574   x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
15575   emit_unlikely_jump (x, label);
15576
15577   rs6000_post_atomic_barrier (model);
15578
15579   if (shift)
15580     {
15581       /* QImode/HImode on machines without lbarx/lharx where we do a lwarx and
15582          then do the calcuations in a SImode register.  */
15583       if (orig_before)
15584         rs6000_finish_atomic_subword (orig_before, before, shift);
15585       if (orig_after)
15586         rs6000_finish_atomic_subword (orig_after, after, shift);
15587     }
15588   else if (store_mode != mode)
15589     {
15590       /* QImode/HImode on machines with lbarx/lharx where we do the native
15591          operation and then do the calcuations in a SImode register.  */
15592       if (orig_before)
15593         convert_move (orig_before, before, 1);
15594       if (orig_after)
15595         convert_move (orig_after, after, 1);
15596     }
15597   else if (orig_after && after != orig_after)
15598     emit_move_insn (orig_after, after);
15599 }
15600
15601 /* Emit instructions to move SRC to DST.  Called by splitters for
15602    multi-register moves.  It will emit at most one instruction for
15603    each register that is accessed; that is, it won't emit li/lis pairs
15604    (or equivalent for 64-bit code).  One of SRC or DST must be a hard
15605    register.  */
15606
15607 void
15608 rs6000_split_multireg_move (rtx dst, rtx src)
15609 {
15610   /* The register number of the first register being moved.  */
15611   int reg;
15612   /* The mode that is to be moved.  */
15613   machine_mode mode;
15614   /* The mode that the move is being done in, and its size.  */
15615   machine_mode reg_mode;
15616   int reg_mode_size;
15617   /* The number of registers that will be moved.  */
15618   int nregs;
15619
15620   reg = REG_P (dst) ? REGNO (dst) : REGNO (src);
15621   mode = GET_MODE (dst);
15622   nregs = hard_regno_nregs (reg, mode);
15623   if (FP_REGNO_P (reg))
15624     reg_mode = DECIMAL_FLOAT_MODE_P (mode) ? DDmode :
15625         (TARGET_HARD_FLOAT ? DFmode : SFmode);
15626   else if (ALTIVEC_REGNO_P (reg))
15627     reg_mode = V16QImode;
15628   else
15629     reg_mode = word_mode;
15630   reg_mode_size = GET_MODE_SIZE (reg_mode);
15631
15632   gcc_assert (reg_mode_size * nregs == GET_MODE_SIZE (mode));
15633
15634   /* TDmode residing in FP registers is special, since the ISA requires that
15635      the lower-numbered word of a register pair is always the most significant
15636      word, even in little-endian mode.  This does not match the usual subreg
15637      semantics, so we cannnot use simplify_gen_subreg in those cases.  Access
15638      the appropriate constituent registers "by hand" in little-endian mode.
15639
15640      Note we do not need to check for destructive overlap here since TDmode
15641      can only reside in even/odd register pairs.  */
15642   if (FP_REGNO_P (reg) && DECIMAL_FLOAT_MODE_P (mode) && !BYTES_BIG_ENDIAN)
15643     {
15644       rtx p_src, p_dst;
15645       int i;
15646
15647       for (i = 0; i < nregs; i++)
15648         {
15649           if (REG_P (src) && FP_REGNO_P (REGNO (src)))
15650             p_src = gen_rtx_REG (reg_mode, REGNO (src) + nregs - 1 - i);
15651           else
15652             p_src = simplify_gen_subreg (reg_mode, src, mode,
15653                                          i * reg_mode_size);
15654
15655           if (REG_P (dst) && FP_REGNO_P (REGNO (dst)))
15656             p_dst = gen_rtx_REG (reg_mode, REGNO (dst) + nregs - 1 - i);
15657           else
15658             p_dst = simplify_gen_subreg (reg_mode, dst, mode,
15659                                          i * reg_mode_size);
15660
15661           emit_insn (gen_rtx_SET (p_dst, p_src));
15662         }
15663
15664       return;
15665     }
15666
15667   if (REG_P (src) && REG_P (dst) && (REGNO (src) < REGNO (dst)))
15668     {
15669       /* Move register range backwards, if we might have destructive
15670          overlap.  */
15671       int i;
15672       for (i = nregs - 1; i >= 0; i--)
15673         emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
15674                                                      i * reg_mode_size),
15675                                 simplify_gen_subreg (reg_mode, src, mode,
15676                                                      i * reg_mode_size)));
15677     }
15678   else
15679     {
15680       int i;
15681       int j = -1;
15682       bool used_update = false;
15683       rtx restore_basereg = NULL_RTX;
15684
15685       if (MEM_P (src) && INT_REGNO_P (reg))
15686         {
15687           rtx breg;
15688
15689           if (GET_CODE (XEXP (src, 0)) == PRE_INC
15690               || GET_CODE (XEXP (src, 0)) == PRE_DEC)
15691             {
15692               rtx delta_rtx;
15693               breg = XEXP (XEXP (src, 0), 0);
15694               delta_rtx = (GET_CODE (XEXP (src, 0)) == PRE_INC
15695                            ? GEN_INT (GET_MODE_SIZE (GET_MODE (src)))
15696                            : GEN_INT (-GET_MODE_SIZE (GET_MODE (src))));
15697               emit_insn (gen_add3_insn (breg, breg, delta_rtx));
15698               src = replace_equiv_address (src, breg);
15699             }
15700           else if (! rs6000_offsettable_memref_p (src, reg_mode, true))
15701             {
15702               if (GET_CODE (XEXP (src, 0)) == PRE_MODIFY)
15703                 {
15704                   rtx basereg = XEXP (XEXP (src, 0), 0);
15705                   if (TARGET_UPDATE)
15706                     {
15707                       rtx ndst = simplify_gen_subreg (reg_mode, dst, mode, 0);
15708                       emit_insn (gen_rtx_SET (ndst,
15709                                               gen_rtx_MEM (reg_mode,
15710                                                            XEXP (src, 0))));
15711                       used_update = true;
15712                     }
15713                   else
15714                     emit_insn (gen_rtx_SET (basereg,
15715                                             XEXP (XEXP (src, 0), 1)));
15716                   src = replace_equiv_address (src, basereg);
15717                 }
15718               else
15719                 {
15720                   rtx basereg = gen_rtx_REG (Pmode, reg);
15721                   emit_insn (gen_rtx_SET (basereg, XEXP (src, 0)));
15722                   src = replace_equiv_address (src, basereg);
15723                 }
15724             }
15725
15726           breg = XEXP (src, 0);
15727           if (GET_CODE (breg) == PLUS || GET_CODE (breg) == LO_SUM)
15728             breg = XEXP (breg, 0);
15729
15730           /* If the base register we are using to address memory is
15731              also a destination reg, then change that register last.  */
15732           if (REG_P (breg)
15733               && REGNO (breg) >= REGNO (dst)
15734               && REGNO (breg) < REGNO (dst) + nregs)
15735             j = REGNO (breg) - REGNO (dst);
15736         }
15737       else if (MEM_P (dst) && INT_REGNO_P (reg))
15738         {
15739           rtx breg;
15740
15741           if (GET_CODE (XEXP (dst, 0)) == PRE_INC
15742               || GET_CODE (XEXP (dst, 0)) == PRE_DEC)
15743             {
15744               rtx delta_rtx;
15745               breg = XEXP (XEXP (dst, 0), 0);
15746               delta_rtx = (GET_CODE (XEXP (dst, 0)) == PRE_INC
15747                            ? GEN_INT (GET_MODE_SIZE (GET_MODE (dst)))
15748                            : GEN_INT (-GET_MODE_SIZE (GET_MODE (dst))));
15749
15750               /* We have to update the breg before doing the store.
15751                  Use store with update, if available.  */
15752
15753               if (TARGET_UPDATE)
15754                 {
15755                   rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
15756                   emit_insn (TARGET_32BIT
15757                              ? (TARGET_POWERPC64
15758                                 ? gen_movdi_si_update (breg, breg, delta_rtx, nsrc)
15759                                 : gen_movsi_si_update (breg, breg, delta_rtx, nsrc))
15760                              : gen_movdi_di_update (breg, breg, delta_rtx, nsrc));
15761                   used_update = true;
15762                 }
15763               else
15764                 emit_insn (gen_add3_insn (breg, breg, delta_rtx));
15765               dst = replace_equiv_address (dst, breg);
15766             }
15767           else if (!rs6000_offsettable_memref_p (dst, reg_mode, true)
15768                    && GET_CODE (XEXP (dst, 0)) != LO_SUM)
15769             {
15770               if (GET_CODE (XEXP (dst, 0)) == PRE_MODIFY)
15771                 {
15772                   rtx basereg = XEXP (XEXP (dst, 0), 0);
15773                   if (TARGET_UPDATE)
15774                     {
15775                       rtx nsrc = simplify_gen_subreg (reg_mode, src, mode, 0);
15776                       emit_insn (gen_rtx_SET (gen_rtx_MEM (reg_mode,
15777                                                            XEXP (dst, 0)),
15778                                               nsrc));
15779                       used_update = true;
15780                     }
15781                   else
15782                     emit_insn (gen_rtx_SET (basereg,
15783                                             XEXP (XEXP (dst, 0), 1)));
15784                   dst = replace_equiv_address (dst, basereg);
15785                 }
15786               else
15787                 {
15788                   rtx basereg = XEXP (XEXP (dst, 0), 0);
15789                   rtx offsetreg = XEXP (XEXP (dst, 0), 1);
15790                   gcc_assert (GET_CODE (XEXP (dst, 0)) == PLUS
15791                               && REG_P (basereg)
15792                               && REG_P (offsetreg)
15793                               && REGNO (basereg) != REGNO (offsetreg));
15794                   if (REGNO (basereg) == 0)
15795                     {
15796                       rtx tmp = offsetreg;
15797                       offsetreg = basereg;
15798                       basereg = tmp;
15799                     }
15800                   emit_insn (gen_add3_insn (basereg, basereg, offsetreg));
15801                   restore_basereg = gen_sub3_insn (basereg, basereg, offsetreg);
15802                   dst = replace_equiv_address (dst, basereg);
15803                 }
15804             }
15805           else if (GET_CODE (XEXP (dst, 0)) != LO_SUM)
15806             gcc_assert (rs6000_offsettable_memref_p (dst, reg_mode, true));
15807         }
15808
15809       for (i = 0; i < nregs; i++)
15810         {
15811           /* Calculate index to next subword.  */
15812           ++j;
15813           if (j == nregs)
15814             j = 0;
15815
15816           /* If compiler already emitted move of first word by
15817              store with update, no need to do anything.  */
15818           if (j == 0 && used_update)
15819             continue;
15820
15821           emit_insn (gen_rtx_SET (simplify_gen_subreg (reg_mode, dst, mode,
15822                                                        j * reg_mode_size),
15823                                   simplify_gen_subreg (reg_mode, src, mode,
15824                                                        j * reg_mode_size)));
15825         }
15826       if (restore_basereg != NULL_RTX)
15827         emit_insn (restore_basereg);
15828     }
15829 }
15830
15831 static GTY(()) alias_set_type TOC_alias_set = -1;
15832
15833 alias_set_type
15834 get_TOC_alias_set (void)
15835 {
15836   if (TOC_alias_set == -1)
15837     TOC_alias_set = new_alias_set ();
15838   return TOC_alias_set;
15839 }
15840
15841 /* The mode the ABI uses for a word.  This is not the same as word_mode
15842    for -m32 -mpowerpc64.  This is used to implement various target hooks.  */
15843
15844 static scalar_int_mode
15845 rs6000_abi_word_mode (void)
15846 {
15847   return TARGET_32BIT ? SImode : DImode;
15848 }
15849
15850 /* Implement the TARGET_OFFLOAD_OPTIONS hook.  */
15851 static char *
15852 rs6000_offload_options (void)
15853 {
15854   if (TARGET_64BIT)
15855     return xstrdup ("-foffload-abi=lp64");
15856   else
15857     return xstrdup ("-foffload-abi=ilp32");
15858 }
15859
15860 \f
15861 /* A quick summary of the various types of 'constant-pool tables'
15862    under PowerPC:
15863
15864    Target       Flags           Name            One table per
15865    AIX          (none)          AIX TOC         object file
15866    AIX          -mfull-toc      AIX TOC         object file
15867    AIX          -mminimal-toc   AIX minimal TOC translation unit
15868    SVR4/EABI    (none)          SVR4 SDATA      object file
15869    SVR4/EABI    -fpic           SVR4 pic        object file
15870    SVR4/EABI    -fPIC           SVR4 PIC        translation unit
15871    SVR4/EABI    -mrelocatable   EABI TOC        function
15872    SVR4/EABI    -maix           AIX TOC         object file
15873    SVR4/EABI    -maix -mminimal-toc
15874                                 AIX minimal TOC translation unit
15875
15876    Name                 Reg.    Set by  entries       contains:
15877                                         made by  addrs? fp?     sum?
15878
15879    AIX TOC              2       crt0    as       Y      option  option
15880    AIX minimal TOC      30      prolog  gcc      Y      Y       option
15881    SVR4 SDATA           13      crt0    gcc      N      Y       N
15882    SVR4 pic             30      prolog  ld       Y      not yet N
15883    SVR4 PIC             30      prolog  gcc      Y      option  option
15884    EABI TOC             30      prolog  gcc      Y      option  option
15885
15886 */
15887
15888 /* Hash functions for the hash table.  */
15889
15890 static unsigned
15891 rs6000_hash_constant (rtx k)
15892 {
15893   enum rtx_code code = GET_CODE (k);
15894   machine_mode mode = GET_MODE (k);
15895   unsigned result = (code << 3) ^ mode;
15896   const char *format;
15897   int flen, fidx;
15898
15899   format = GET_RTX_FORMAT (code);
15900   flen = strlen (format);
15901   fidx = 0;
15902
15903   switch (code)
15904     {
15905     case LABEL_REF:
15906       return result * 1231 + (unsigned) INSN_UID (XEXP (k, 0));
15907
15908     case CONST_WIDE_INT:
15909       {
15910         int i;
15911         flen = CONST_WIDE_INT_NUNITS (k);
15912         for (i = 0; i < flen; i++)
15913           result = result * 613 + CONST_WIDE_INT_ELT (k, i);
15914         return result;
15915       }
15916
15917     case CONST_DOUBLE:
15918       return real_hash (CONST_DOUBLE_REAL_VALUE (k)) * result;
15919
15920     case CODE_LABEL:
15921       fidx = 3;
15922       break;
15923
15924     default:
15925       break;
15926     }
15927
15928   for (; fidx < flen; fidx++)
15929     switch (format[fidx])
15930       {
15931       case 's':
15932         {
15933           unsigned i, len;
15934           const char *str = XSTR (k, fidx);
15935           len = strlen (str);
15936           result = result * 613 + len;
15937           for (i = 0; i < len; i++)
15938             result = result * 613 + (unsigned) str[i];
15939           break;
15940         }
15941       case 'u':
15942       case 'e':
15943         result = result * 1231 + rs6000_hash_constant (XEXP (k, fidx));
15944         break;
15945       case 'i':
15946       case 'n':
15947         result = result * 613 + (unsigned) XINT (k, fidx);
15948         break;
15949       case 'w':
15950         if (sizeof (unsigned) >= sizeof (HOST_WIDE_INT))
15951           result = result * 613 + (unsigned) XWINT (k, fidx);
15952         else
15953           {
15954             size_t i;
15955             for (i = 0; i < sizeof (HOST_WIDE_INT) / sizeof (unsigned); i++)
15956               result = result * 613 + (unsigned) (XWINT (k, fidx)
15957                                                   >> CHAR_BIT * i);
15958           }
15959         break;
15960       case '0':
15961         break;
15962       default:
15963         gcc_unreachable ();
15964       }
15965
15966   return result;
15967 }
15968
15969 hashval_t
15970 toc_hasher::hash (toc_hash_struct *thc)
15971 {
15972   return rs6000_hash_constant (thc->key) ^ thc->key_mode;
15973 }
15974
15975 /* Compare H1 and H2 for equivalence.  */
15976
15977 bool
15978 toc_hasher::equal (toc_hash_struct *h1, toc_hash_struct *h2)
15979 {
15980   rtx r1 = h1->key;
15981   rtx r2 = h2->key;
15982
15983   if (h1->key_mode != h2->key_mode)
15984     return 0;
15985
15986   return rtx_equal_p (r1, r2);
15987 }
15988
15989 /* These are the names given by the C++ front-end to vtables, and
15990    vtable-like objects.  Ideally, this logic should not be here;
15991    instead, there should be some programmatic way of inquiring as
15992    to whether or not an object is a vtable.  */
15993
15994 #define VTABLE_NAME_P(NAME)                             \
15995   (strncmp ("_vt.", name, strlen ("_vt.")) == 0         \
15996   || strncmp ("_ZTV", name, strlen ("_ZTV")) == 0       \
15997   || strncmp ("_ZTT", name, strlen ("_ZTT")) == 0       \
15998   || strncmp ("_ZTI", name, strlen ("_ZTI")) == 0       \
15999   || strncmp ("_ZTC", name, strlen ("_ZTC")) == 0)
16000
16001 #ifdef NO_DOLLAR_IN_LABEL
16002 /* Return a GGC-allocated character string translating dollar signs in
16003    input NAME to underscores.  Used by XCOFF ASM_OUTPUT_LABELREF.  */
16004
16005 const char *
16006 rs6000_xcoff_strip_dollar (const char *name)
16007 {
16008   char *strip, *p;
16009   const char *q;
16010   size_t len;
16011
16012   q = (const char *) strchr (name, '$');
16013
16014   if (q == 0 || q == name)
16015     return name;
16016
16017   len = strlen (name);
16018   strip = XALLOCAVEC (char, len + 1);
16019   strcpy (strip, name);
16020   p = strip + (q - name);
16021   while (p)
16022     {
16023       *p = '_';
16024       p = strchr (p + 1, '$');
16025     }
16026
16027   return ggc_alloc_string (strip, len);
16028 }
16029 #endif
16030
16031 void
16032 rs6000_output_symbol_ref (FILE *file, rtx x)
16033 {
16034   const char *name = XSTR (x, 0);
16035
16036   /* Currently C++ toc references to vtables can be emitted before it
16037      is decided whether the vtable is public or private.  If this is
16038      the case, then the linker will eventually complain that there is
16039      a reference to an unknown section.  Thus, for vtables only,
16040      we emit the TOC reference to reference the identifier and not the
16041      symbol.  */
16042   if (VTABLE_NAME_P (name))
16043     {
16044       RS6000_OUTPUT_BASENAME (file, name);
16045     }
16046   else
16047     assemble_name (file, name);
16048 }
16049
16050 /* Output a TOC entry.  We derive the entry name from what is being
16051    written.  */
16052
16053 void
16054 output_toc (FILE *file, rtx x, int labelno, machine_mode mode)
16055 {
16056   char buf[256];
16057   const char *name = buf;
16058   rtx base = x;
16059   HOST_WIDE_INT offset = 0;
16060
16061   gcc_assert (!TARGET_NO_TOC_OR_PCREL);
16062
16063   /* When the linker won't eliminate them, don't output duplicate
16064      TOC entries (this happens on AIX if there is any kind of TOC,
16065      and on SVR4 under -fPIC or -mrelocatable).  Don't do this for
16066      CODE_LABELs.  */
16067   if (TARGET_TOC && GET_CODE (x) != LABEL_REF)
16068     {
16069       struct toc_hash_struct *h;
16070
16071       /* Create toc_hash_table.  This can't be done at TARGET_OPTION_OVERRIDE
16072          time because GGC is not initialized at that point.  */
16073       if (toc_hash_table == NULL)
16074         toc_hash_table = hash_table<toc_hasher>::create_ggc (1021);
16075
16076       h = ggc_alloc<toc_hash_struct> ();
16077       h->key = x;
16078       h->key_mode = mode;
16079       h->labelno = labelno;
16080
16081       toc_hash_struct **found = toc_hash_table->find_slot (h, INSERT);
16082       if (*found == NULL)
16083         *found = h;
16084       else  /* This is indeed a duplicate.
16085                Set this label equal to that label.  */
16086         {
16087           fputs ("\t.set ", file);
16088           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
16089           fprintf (file, "%d,", labelno);
16090           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LC");
16091           fprintf (file, "%d\n", ((*found)->labelno));
16092
16093 #ifdef HAVE_AS_TLS
16094           if (TARGET_XCOFF && SYMBOL_REF_P (x)
16095               && (SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_GLOBAL_DYNAMIC
16096                   || SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC))
16097             {
16098               fputs ("\t.set ", file);
16099               ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
16100               fprintf (file, "%d,", labelno);
16101               ASM_OUTPUT_INTERNAL_LABEL_PREFIX (file, "LCM");
16102               fprintf (file, "%d\n", ((*found)->labelno));
16103             }
16104 #endif
16105           return;
16106         }
16107     }
16108
16109   /* If we're going to put a double constant in the TOC, make sure it's
16110      aligned properly when strict alignment is on.  */
16111   if ((CONST_DOUBLE_P (x) || CONST_WIDE_INT_P (x))
16112       && STRICT_ALIGNMENT
16113       && GET_MODE_BITSIZE (mode) >= 64
16114       && ! (TARGET_NO_FP_IN_TOC && ! TARGET_MINIMAL_TOC)) {
16115     ASM_OUTPUT_ALIGN (file, 3);
16116   }
16117
16118   (*targetm.asm_out.internal_label) (file, "LC", labelno);
16119
16120   /* Handle FP constants specially.  Note that if we have a minimal
16121      TOC, things we put here aren't actually in the TOC, so we can allow
16122      FP constants.  */
16123   if (CONST_DOUBLE_P (x)
16124       && (GET_MODE (x) == TFmode || GET_MODE (x) == TDmode
16125           || GET_MODE (x) == IFmode || GET_MODE (x) == KFmode))
16126     {
16127       long k[4];
16128
16129       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
16130         REAL_VALUE_TO_TARGET_DECIMAL128 (*CONST_DOUBLE_REAL_VALUE (x), k);
16131       else
16132         REAL_VALUE_TO_TARGET_LONG_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
16133
16134       if (TARGET_64BIT)
16135         {
16136           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16137             fputs (DOUBLE_INT_ASM_OP, file);
16138           else
16139             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
16140                      k[0] & 0xffffffff, k[1] & 0xffffffff,
16141                      k[2] & 0xffffffff, k[3] & 0xffffffff);
16142           fprintf (file, "0x%lx%08lx,0x%lx%08lx\n",
16143                    k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
16144                    k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff,
16145                    k[WORDS_BIG_ENDIAN ? 2 : 3] & 0xffffffff,
16146                    k[WORDS_BIG_ENDIAN ? 3 : 2] & 0xffffffff);
16147           return;
16148         }
16149       else
16150         {
16151           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16152             fputs ("\t.long ", file);
16153           else
16154             fprintf (file, "\t.tc FT_%lx_%lx_%lx_%lx[TC],",
16155                      k[0] & 0xffffffff, k[1] & 0xffffffff,
16156                      k[2] & 0xffffffff, k[3] & 0xffffffff);
16157           fprintf (file, "0x%lx,0x%lx,0x%lx,0x%lx\n",
16158                    k[0] & 0xffffffff, k[1] & 0xffffffff,
16159                    k[2] & 0xffffffff, k[3] & 0xffffffff);
16160           return;
16161         }
16162     }
16163   else if (CONST_DOUBLE_P (x)
16164            && (GET_MODE (x) == DFmode || GET_MODE (x) == DDmode))
16165     {
16166       long k[2];
16167
16168       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
16169         REAL_VALUE_TO_TARGET_DECIMAL64 (*CONST_DOUBLE_REAL_VALUE (x), k);
16170       else
16171         REAL_VALUE_TO_TARGET_DOUBLE (*CONST_DOUBLE_REAL_VALUE (x), k);
16172
16173       if (TARGET_64BIT)
16174         {
16175           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16176             fputs (DOUBLE_INT_ASM_OP, file);
16177           else
16178             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
16179                      k[0] & 0xffffffff, k[1] & 0xffffffff);
16180           fprintf (file, "0x%lx%08lx\n",
16181                    k[WORDS_BIG_ENDIAN ? 0 : 1] & 0xffffffff,
16182                    k[WORDS_BIG_ENDIAN ? 1 : 0] & 0xffffffff);
16183           return;
16184         }
16185       else
16186         {
16187           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16188             fputs ("\t.long ", file);
16189           else
16190             fprintf (file, "\t.tc FD_%lx_%lx[TC],",
16191                      k[0] & 0xffffffff, k[1] & 0xffffffff);
16192           fprintf (file, "0x%lx,0x%lx\n",
16193                    k[0] & 0xffffffff, k[1] & 0xffffffff);
16194           return;
16195         }
16196     }
16197   else if (CONST_DOUBLE_P (x)
16198            && (GET_MODE (x) == SFmode || GET_MODE (x) == SDmode))
16199     {
16200       long l;
16201
16202       if (DECIMAL_FLOAT_MODE_P (GET_MODE (x)))
16203         REAL_VALUE_TO_TARGET_DECIMAL32 (*CONST_DOUBLE_REAL_VALUE (x), l);
16204       else
16205         REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
16206
16207       if (TARGET_64BIT)
16208         {
16209           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16210             fputs (DOUBLE_INT_ASM_OP, file);
16211           else
16212             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
16213           if (WORDS_BIG_ENDIAN)
16214             fprintf (file, "0x%lx00000000\n", l & 0xffffffff);
16215           else
16216             fprintf (file, "0x%lx\n", l & 0xffffffff);
16217           return;
16218         }
16219       else
16220         {
16221           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16222             fputs ("\t.long ", file);
16223           else
16224             fprintf (file, "\t.tc FS_%lx[TC],", l & 0xffffffff);
16225           fprintf (file, "0x%lx\n", l & 0xffffffff);
16226           return;
16227         }
16228     }
16229   else if (GET_MODE (x) == VOIDmode && CONST_INT_P (x))
16230     {
16231       unsigned HOST_WIDE_INT low;
16232       HOST_WIDE_INT high;
16233
16234       low = INTVAL (x) & 0xffffffff;
16235       high = (HOST_WIDE_INT) INTVAL (x) >> 32;
16236
16237       /* TOC entries are always Pmode-sized, so when big-endian
16238          smaller integer constants in the TOC need to be padded.
16239          (This is still a win over putting the constants in
16240          a separate constant pool, because then we'd have
16241          to have both a TOC entry _and_ the actual constant.)
16242
16243          For a 32-bit target, CONST_INT values are loaded and shifted
16244          entirely within `low' and can be stored in one TOC entry.  */
16245
16246       /* It would be easy to make this work, but it doesn't now.  */
16247       gcc_assert (!TARGET_64BIT || POINTER_SIZE >= GET_MODE_BITSIZE (mode));
16248
16249       if (WORDS_BIG_ENDIAN && POINTER_SIZE > GET_MODE_BITSIZE (mode))
16250         {
16251           low |= high << 32;
16252           low <<= POINTER_SIZE - GET_MODE_BITSIZE (mode);
16253           high = (HOST_WIDE_INT) low >> 32;
16254           low &= 0xffffffff;
16255         }
16256
16257       if (TARGET_64BIT)
16258         {
16259           if (TARGET_ELF || TARGET_MINIMAL_TOC)
16260             fputs (DOUBLE_INT_ASM_OP, file);
16261           else
16262             fprintf (file, "\t.tc ID_%lx_%lx[TC],",
16263                      (long) high & 0xffffffff, (long) low & 0xffffffff);
16264           fprintf (file, "0x%lx%08lx\n",
16265                    (long) high & 0xffffffff, (long) low & 0xffffffff);
16266           return;
16267         }
16268       else
16269         {
16270           if (POINTER_SIZE < GET_MODE_BITSIZE (mode))
16271             {
16272               if (TARGET_ELF || TARGET_MINIMAL_TOC)
16273                 fputs ("\t.long ", file);
16274               else
16275                 fprintf (file, "\t.tc ID_%lx_%lx[TC],",
16276                          (long) high & 0xffffffff, (long) low & 0xffffffff);
16277               fprintf (file, "0x%lx,0x%lx\n",
16278                        (long) high & 0xffffffff, (long) low & 0xffffffff);
16279             }
16280           else
16281             {
16282               if (TARGET_ELF || TARGET_MINIMAL_TOC)
16283                 fputs ("\t.long ", file);
16284               else
16285                 fprintf (file, "\t.tc IS_%lx[TC],", (long) low & 0xffffffff);
16286               fprintf (file, "0x%lx\n", (long) low & 0xffffffff);
16287             }
16288           return;
16289         }
16290     }
16291
16292   if (GET_CODE (x) == CONST)
16293     {
16294       gcc_assert (GET_CODE (XEXP (x, 0)) == PLUS
16295                   && CONST_INT_P (XEXP (XEXP (x, 0), 1)));
16296
16297       base = XEXP (XEXP (x, 0), 0);
16298       offset = INTVAL (XEXP (XEXP (x, 0), 1));
16299     }
16300
16301   switch (GET_CODE (base))
16302     {
16303     case SYMBOL_REF:
16304       name = XSTR (base, 0);
16305       break;
16306
16307     case LABEL_REF:
16308       ASM_GENERATE_INTERNAL_LABEL (buf, "L",
16309                                    CODE_LABEL_NUMBER (XEXP (base, 0)));
16310       break;
16311
16312     case CODE_LABEL:
16313       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (base));
16314       break;
16315
16316     default:
16317       gcc_unreachable ();
16318     }
16319
16320   if (TARGET_ELF || TARGET_MINIMAL_TOC)
16321     fputs (TARGET_32BIT ? "\t.long " : DOUBLE_INT_ASM_OP, file);
16322   else
16323     {
16324       fputs ("\t.tc ", file);
16325       RS6000_OUTPUT_BASENAME (file, name);
16326
16327       if (offset < 0)
16328         fprintf (file, ".N" HOST_WIDE_INT_PRINT_UNSIGNED, - offset);
16329       else if (offset)
16330         fprintf (file, ".P" HOST_WIDE_INT_PRINT_UNSIGNED, offset);
16331
16332       /* Mark large TOC symbols on AIX with [TE] so they are mapped
16333          after other TOC symbols, reducing overflow of small TOC access
16334          to [TC] symbols.  */
16335       fputs (TARGET_XCOFF && TARGET_CMODEL != CMODEL_SMALL
16336              ? "[TE]," : "[TC],", file);
16337     }
16338
16339   /* Currently C++ toc references to vtables can be emitted before it
16340      is decided whether the vtable is public or private.  If this is
16341      the case, then the linker will eventually complain that there is
16342      a TOC reference to an unknown section.  Thus, for vtables only,
16343      we emit the TOC reference to reference the symbol and not the
16344      section.  */
16345   if (VTABLE_NAME_P (name))
16346     {
16347       RS6000_OUTPUT_BASENAME (file, name);
16348       if (offset < 0)
16349         fprintf (file, HOST_WIDE_INT_PRINT_DEC, offset);
16350       else if (offset > 0)
16351         fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
16352     }
16353   else
16354     output_addr_const (file, x);
16355
16356 #if HAVE_AS_TLS
16357   if (TARGET_XCOFF && SYMBOL_REF_P (base))
16358     {
16359       switch (SYMBOL_REF_TLS_MODEL (base))
16360         {
16361         case 0:
16362           break;
16363         case TLS_MODEL_LOCAL_EXEC:
16364           fputs ("@le", file);
16365           break;
16366         case TLS_MODEL_INITIAL_EXEC:
16367           fputs ("@ie", file);
16368           break;
16369         /* Use global-dynamic for local-dynamic.  */
16370         case TLS_MODEL_GLOBAL_DYNAMIC:
16371         case TLS_MODEL_LOCAL_DYNAMIC:
16372           putc ('\n', file);
16373           (*targetm.asm_out.internal_label) (file, "LCM", labelno);
16374           fputs ("\t.tc .", file);
16375           RS6000_OUTPUT_BASENAME (file, name);
16376           fputs ("[TC],", file);
16377           output_addr_const (file, x);
16378           fputs ("@m", file);
16379           break;
16380         default:
16381           gcc_unreachable ();
16382         }
16383     }
16384 #endif
16385
16386   putc ('\n', file);
16387 }
16388 \f
16389 /* Output an assembler pseudo-op to write an ASCII string of N characters
16390    starting at P to FILE.
16391
16392    On the RS/6000, we have to do this using the .byte operation and
16393    write out special characters outside the quoted string.
16394    Also, the assembler is broken; very long strings are truncated,
16395    so we must artificially break them up early.  */
16396
16397 void
16398 output_ascii (FILE *file, const char *p, int n)
16399 {
16400   char c;
16401   int i, count_string;
16402   const char *for_string = "\t.byte \"";
16403   const char *for_decimal = "\t.byte ";
16404   const char *to_close = NULL;
16405
16406   count_string = 0;
16407   for (i = 0; i < n; i++)
16408     {
16409       c = *p++;
16410       if (c >= ' ' && c < 0177)
16411         {
16412           if (for_string)
16413             fputs (for_string, file);
16414           putc (c, file);
16415
16416           /* Write two quotes to get one.  */
16417           if (c == '"')
16418             {
16419               putc (c, file);
16420               ++count_string;
16421             }
16422
16423           for_string = NULL;
16424           for_decimal = "\"\n\t.byte ";
16425           to_close = "\"\n";
16426           ++count_string;
16427
16428           if (count_string >= 512)
16429             {
16430               fputs (to_close, file);
16431
16432               for_string = "\t.byte \"";
16433               for_decimal = "\t.byte ";
16434               to_close = NULL;
16435               count_string = 0;
16436             }
16437         }
16438       else
16439         {
16440           if (for_decimal)
16441             fputs (for_decimal, file);
16442           fprintf (file, "%d", c);
16443
16444           for_string = "\n\t.byte \"";
16445           for_decimal = ", ";
16446           to_close = "\n";
16447           count_string = 0;
16448         }
16449     }
16450
16451   /* Now close the string if we have written one.  Then end the line.  */
16452   if (to_close)
16453     fputs (to_close, file);
16454 }
16455 \f
16456 /* Generate a unique section name for FILENAME for a section type
16457    represented by SECTION_DESC.  Output goes into BUF.
16458
16459    SECTION_DESC can be any string, as long as it is different for each
16460    possible section type.
16461
16462    We name the section in the same manner as xlc.  The name begins with an
16463    underscore followed by the filename (after stripping any leading directory
16464    names) with the last period replaced by the string SECTION_DESC.  If
16465    FILENAME does not contain a period, SECTION_DESC is appended to the end of
16466    the name.  */
16467
16468 void
16469 rs6000_gen_section_name (char **buf, const char *filename,
16470                          const char *section_desc)
16471 {
16472   const char *q, *after_last_slash, *last_period = 0;
16473   char *p;
16474   int len;
16475
16476   after_last_slash = filename;
16477   for (q = filename; *q; q++)
16478     {
16479       if (*q == '/')
16480         after_last_slash = q + 1;
16481       else if (*q == '.')
16482         last_period = q;
16483     }
16484
16485   len = strlen (after_last_slash) + strlen (section_desc) + 2;
16486   *buf = (char *) xmalloc (len);
16487
16488   p = *buf;
16489   *p++ = '_';
16490
16491   for (q = after_last_slash; *q; q++)
16492     {
16493       if (q == last_period)
16494         {
16495           strcpy (p, section_desc);
16496           p += strlen (section_desc);
16497           break;
16498         }
16499
16500       else if (ISALNUM (*q))
16501         *p++ = *q;
16502     }
16503
16504   if (last_period == 0)
16505     strcpy (p, section_desc);
16506   else
16507     *p = '\0';
16508 }
16509 \f
16510 /* Emit profile function.  */
16511
16512 void
16513 output_profile_hook (int labelno ATTRIBUTE_UNUSED)
16514 {
16515   /* Non-standard profiling for kernels, which just saves LR then calls
16516      _mcount without worrying about arg saves.  The idea is to change
16517      the function prologue as little as possible as it isn't easy to
16518      account for arg save/restore code added just for _mcount.  */
16519   if (TARGET_PROFILE_KERNEL)
16520     return;
16521
16522   if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
16523     {
16524 #ifndef NO_PROFILE_COUNTERS
16525 # define NO_PROFILE_COUNTERS 0
16526 #endif
16527       if (NO_PROFILE_COUNTERS)
16528         emit_library_call (init_one_libfunc (RS6000_MCOUNT),
16529                            LCT_NORMAL, VOIDmode);
16530       else
16531         {
16532           char buf[30];
16533           const char *label_name;
16534           rtx fun;
16535
16536           ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
16537           label_name = ggc_strdup ((*targetm.strip_name_encoding) (buf));
16538           fun = gen_rtx_SYMBOL_REF (Pmode, label_name);
16539
16540           emit_library_call (init_one_libfunc (RS6000_MCOUNT),
16541                              LCT_NORMAL, VOIDmode, fun, Pmode);
16542         }
16543     }
16544   else if (DEFAULT_ABI == ABI_DARWIN)
16545     {
16546       const char *mcount_name = RS6000_MCOUNT;
16547       int caller_addr_regno = LR_REGNO;
16548
16549       /* Be conservative and always set this, at least for now.  */
16550       crtl->uses_pic_offset_table = 1;
16551
16552 #if TARGET_MACHO
16553       /* For PIC code, set up a stub and collect the caller's address
16554          from r0, which is where the prologue puts it.  */
16555       if (MACHOPIC_INDIRECT
16556           && crtl->uses_pic_offset_table)
16557         caller_addr_regno = 0;
16558 #endif
16559       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, mcount_name),
16560                          LCT_NORMAL, VOIDmode,
16561                          gen_rtx_REG (Pmode, caller_addr_regno), Pmode);
16562     }
16563 }
16564
16565 /* Write function profiler code.  */
16566
16567 void
16568 output_function_profiler (FILE *file, int labelno)
16569 {
16570   char buf[100];
16571
16572   switch (DEFAULT_ABI)
16573     {
16574     default:
16575       gcc_unreachable ();
16576
16577     case ABI_V4:
16578       if (!TARGET_32BIT)
16579         {
16580           warning (0, "no profiling of 64-bit code for this ABI");
16581           return;
16582         }
16583       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
16584       fprintf (file, "\tmflr %s\n", reg_names[0]);
16585       if (NO_PROFILE_COUNTERS)
16586         {
16587           asm_fprintf (file, "\tstw %s,4(%s)\n",
16588                        reg_names[0], reg_names[1]);
16589         }
16590       else if (TARGET_SECURE_PLT && flag_pic)
16591         {
16592           if (TARGET_LINK_STACK)
16593             {
16594               char name[32];
16595               get_ppc476_thunk_name (name);
16596               asm_fprintf (file, "\tbl %s\n", name);
16597             }
16598           else
16599             asm_fprintf (file, "\tbcl 20,31,1f\n1:\n");
16600           asm_fprintf (file, "\tstw %s,4(%s)\n",
16601                        reg_names[0], reg_names[1]);
16602           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
16603           asm_fprintf (file, "\taddis %s,%s,",
16604                        reg_names[12], reg_names[12]);
16605           assemble_name (file, buf);
16606           asm_fprintf (file, "-1b@ha\n\tla %s,", reg_names[0]);
16607           assemble_name (file, buf);
16608           asm_fprintf (file, "-1b@l(%s)\n", reg_names[12]);
16609         }
16610       else if (flag_pic == 1)
16611         {
16612           fputs ("\tbl _GLOBAL_OFFSET_TABLE_@local-4\n", file);
16613           asm_fprintf (file, "\tstw %s,4(%s)\n",
16614                        reg_names[0], reg_names[1]);
16615           asm_fprintf (file, "\tmflr %s\n", reg_names[12]);
16616           asm_fprintf (file, "\tlwz %s,", reg_names[0]);
16617           assemble_name (file, buf);
16618           asm_fprintf (file, "@got(%s)\n", reg_names[12]);
16619         }
16620       else if (flag_pic > 1)
16621         {
16622           asm_fprintf (file, "\tstw %s,4(%s)\n",
16623                        reg_names[0], reg_names[1]);
16624           /* Now, we need to get the address of the label.  */
16625           if (TARGET_LINK_STACK)
16626             {
16627               char name[32];
16628               get_ppc476_thunk_name (name);
16629               asm_fprintf (file, "\tbl %s\n\tb 1f\n\t.long ", name);
16630               assemble_name (file, buf);
16631               fputs ("-.\n1:", file);
16632               asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
16633               asm_fprintf (file, "\taddi %s,%s,4\n",
16634                            reg_names[11], reg_names[11]);
16635             }
16636           else
16637             {
16638               fputs ("\tbcl 20,31,1f\n\t.long ", file);
16639               assemble_name (file, buf);
16640               fputs ("-.\n1:", file);
16641               asm_fprintf (file, "\tmflr %s\n", reg_names[11]);
16642             }
16643           asm_fprintf (file, "\tlwz %s,0(%s)\n",
16644                        reg_names[0], reg_names[11]);
16645           asm_fprintf (file, "\tadd %s,%s,%s\n",
16646                        reg_names[0], reg_names[0], reg_names[11]);
16647         }
16648       else
16649         {
16650           asm_fprintf (file, "\tlis %s,", reg_names[12]);
16651           assemble_name (file, buf);
16652           fputs ("@ha\n", file);
16653           asm_fprintf (file, "\tstw %s,4(%s)\n",
16654                        reg_names[0], reg_names[1]);
16655           asm_fprintf (file, "\tla %s,", reg_names[0]);
16656           assemble_name (file, buf);
16657           asm_fprintf (file, "@l(%s)\n", reg_names[12]);
16658         }
16659
16660       /* ABI_V4 saves the static chain reg with ASM_OUTPUT_REG_PUSH.  */
16661       fprintf (file, "\tbl %s%s\n",
16662                RS6000_MCOUNT, flag_pic ? "@plt" : "");
16663       break;
16664
16665     case ABI_AIX:
16666     case ABI_ELFv2:
16667     case ABI_DARWIN:
16668       /* Don't do anything, done in output_profile_hook ().  */
16669       break;
16670     }
16671 }
16672
16673 \f
16674
16675 /* The following variable value is the last issued insn.  */
16676
16677 static rtx_insn *last_scheduled_insn;
16678
16679 /* The following variable helps to balance issuing of load and
16680    store instructions */
16681
16682 static int load_store_pendulum;
16683
16684 /* The following variable helps pair divide insns during scheduling.  */
16685 static int divide_cnt;
16686 /* The following variable helps pair and alternate vector and vector load
16687    insns during scheduling.  */
16688 static int vec_pairing;
16689
16690
16691 /* Power4 load update and store update instructions are cracked into a
16692    load or store and an integer insn which are executed in the same cycle.
16693    Branches have their own dispatch slot which does not count against the
16694    GCC issue rate, but it changes the program flow so there are no other
16695    instructions to issue in this cycle.  */
16696
16697 static int
16698 rs6000_variable_issue_1 (rtx_insn *insn, int more)
16699 {
16700   last_scheduled_insn = insn;
16701   if (GET_CODE (PATTERN (insn)) == USE
16702       || GET_CODE (PATTERN (insn)) == CLOBBER)
16703     {
16704       cached_can_issue_more = more;
16705       return cached_can_issue_more;
16706     }
16707
16708   if (insn_terminates_group_p (insn, current_group))
16709     {
16710       cached_can_issue_more = 0;
16711       return cached_can_issue_more;
16712     }
16713
16714   /* If no reservation, but reach here */
16715   if (recog_memoized (insn) < 0)
16716     return more;
16717
16718   if (rs6000_sched_groups)
16719     {
16720       if (is_microcoded_insn (insn))
16721         cached_can_issue_more = 0;
16722       else if (is_cracked_insn (insn))
16723         cached_can_issue_more = more > 2 ? more - 2 : 0;
16724       else
16725         cached_can_issue_more = more - 1;
16726
16727       return cached_can_issue_more;
16728     }
16729
16730   if (rs6000_tune == PROCESSOR_CELL && is_nonpipeline_insn (insn))
16731     return 0;
16732
16733   cached_can_issue_more = more - 1;
16734   return cached_can_issue_more;
16735 }
16736
16737 static int
16738 rs6000_variable_issue (FILE *stream, int verbose, rtx_insn *insn, int more)
16739 {
16740   int r = rs6000_variable_issue_1 (insn, more);
16741   if (verbose)
16742     fprintf (stream, "// rs6000_variable_issue (more = %d) = %d\n", more, r);
16743   return r;
16744 }
16745
16746 /* Adjust the cost of a scheduling dependency.  Return the new cost of
16747    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
16748
16749 static int
16750 rs6000_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
16751                     unsigned int)
16752 {
16753   enum attr_type attr_type;
16754
16755   if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
16756     return cost;
16757
16758   switch (dep_type)
16759     {
16760     case REG_DEP_TRUE:
16761       {
16762         /* Data dependency; DEP_INSN writes a register that INSN reads
16763            some cycles later.  */
16764
16765         /* Separate a load from a narrower, dependent store.  */
16766         if ((rs6000_sched_groups || rs6000_tune == PROCESSOR_POWER9
16767              || rs6000_tune == PROCESSOR_FUTURE)
16768             && GET_CODE (PATTERN (insn)) == SET
16769             && GET_CODE (PATTERN (dep_insn)) == SET
16770             && MEM_P (XEXP (PATTERN (insn), 1))
16771             && MEM_P (XEXP (PATTERN (dep_insn), 0))
16772             && (GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (insn), 1)))
16773                 > GET_MODE_SIZE (GET_MODE (XEXP (PATTERN (dep_insn), 0)))))
16774           return cost + 14;
16775
16776         attr_type = get_attr_type (insn);
16777
16778         switch (attr_type)
16779           {
16780           case TYPE_JMPREG:
16781             /* Tell the first scheduling pass about the latency between
16782                a mtctr and bctr (and mtlr and br/blr).  The first
16783                scheduling pass will not know about this latency since
16784                the mtctr instruction, which has the latency associated
16785                to it, will be generated by reload.  */
16786             return 4;
16787           case TYPE_BRANCH:
16788             /* Leave some extra cycles between a compare and its
16789                dependent branch, to inhibit expensive mispredicts.  */
16790             if ((rs6000_tune == PROCESSOR_PPC603
16791                  || rs6000_tune == PROCESSOR_PPC604
16792                  || rs6000_tune == PROCESSOR_PPC604e
16793                  || rs6000_tune == PROCESSOR_PPC620
16794                  || rs6000_tune == PROCESSOR_PPC630
16795                  || rs6000_tune == PROCESSOR_PPC750
16796                  || rs6000_tune == PROCESSOR_PPC7400
16797                  || rs6000_tune == PROCESSOR_PPC7450
16798                  || rs6000_tune == PROCESSOR_PPCE5500
16799                  || rs6000_tune == PROCESSOR_PPCE6500
16800                  || rs6000_tune == PROCESSOR_POWER4
16801                  || rs6000_tune == PROCESSOR_POWER5
16802                  || rs6000_tune == PROCESSOR_POWER7
16803                  || rs6000_tune == PROCESSOR_POWER8
16804                  || rs6000_tune == PROCESSOR_POWER9
16805                  || rs6000_tune == PROCESSOR_FUTURE
16806                  || rs6000_tune == PROCESSOR_CELL)
16807                 && recog_memoized (dep_insn)
16808                 && (INSN_CODE (dep_insn) >= 0))
16809
16810               switch (get_attr_type (dep_insn))
16811                 {
16812                 case TYPE_CMP:
16813                 case TYPE_FPCOMPARE:
16814                 case TYPE_CR_LOGICAL:
16815                   return cost + 2;
16816                 case TYPE_EXTS:
16817                 case TYPE_MUL:
16818                   if (get_attr_dot (dep_insn) == DOT_YES)
16819                     return cost + 2;
16820                   else
16821                     break;
16822                 case TYPE_SHIFT:
16823                   if (get_attr_dot (dep_insn) == DOT_YES
16824                       && get_attr_var_shift (dep_insn) == VAR_SHIFT_NO)
16825                     return cost + 2;
16826                   else
16827                     break;
16828                 default:
16829                   break;
16830                 }
16831             break;
16832
16833           case TYPE_STORE:
16834           case TYPE_FPSTORE:
16835             if ((rs6000_tune == PROCESSOR_POWER6)
16836                 && recog_memoized (dep_insn)
16837                 && (INSN_CODE (dep_insn) >= 0))
16838               {
16839
16840                 if (GET_CODE (PATTERN (insn)) != SET)
16841                   /* If this happens, we have to extend this to schedule
16842                      optimally.  Return default for now.  */
16843                   return cost;
16844
16845                 /* Adjust the cost for the case where the value written
16846                    by a fixed point operation is used as the address
16847                    gen value on a store. */
16848                 switch (get_attr_type (dep_insn))
16849                   {
16850                   case TYPE_LOAD:
16851                   case TYPE_CNTLZ:
16852                     {
16853                       if (! rs6000_store_data_bypass_p (dep_insn, insn))
16854                         return get_attr_sign_extend (dep_insn)
16855                                == SIGN_EXTEND_YES ? 6 : 4;
16856                       break;
16857                     }
16858                   case TYPE_SHIFT:
16859                     {
16860                       if (! rs6000_store_data_bypass_p (dep_insn, insn))
16861                         return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
16862                                6 : 3;
16863                       break;
16864                     }
16865                   case TYPE_INTEGER:
16866                   case TYPE_ADD:
16867                   case TYPE_LOGICAL:
16868                   case TYPE_EXTS:
16869                   case TYPE_INSERT:
16870                     {
16871                       if (! rs6000_store_data_bypass_p (dep_insn, insn))
16872                         return 3;
16873                       break;
16874                     }
16875                   case TYPE_STORE:
16876                   case TYPE_FPLOAD:
16877                   case TYPE_FPSTORE:
16878                     {
16879                       if (get_attr_update (dep_insn) == UPDATE_YES
16880                           && ! rs6000_store_data_bypass_p (dep_insn, insn))
16881                         return 3;
16882                       break;
16883                     }
16884                   case TYPE_MUL:
16885                     {
16886                       if (! rs6000_store_data_bypass_p (dep_insn, insn))
16887                         return 17;
16888                       break;
16889                     }
16890                   case TYPE_DIV:
16891                     {
16892                       if (! rs6000_store_data_bypass_p (dep_insn, insn))
16893                         return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
16894                       break;
16895                     }
16896                   default:
16897                     break;
16898                   }
16899               }
16900             break;
16901
16902           case TYPE_LOAD:
16903             if ((rs6000_tune == PROCESSOR_POWER6)
16904                 && recog_memoized (dep_insn)
16905                 && (INSN_CODE (dep_insn) >= 0))
16906               {
16907
16908                 /* Adjust the cost for the case where the value written
16909                    by a fixed point instruction is used within the address
16910                    gen portion of a subsequent load(u)(x) */
16911                 switch (get_attr_type (dep_insn))
16912                   {
16913                   case TYPE_LOAD:
16914                   case TYPE_CNTLZ:
16915                     {
16916                       if (set_to_load_agen (dep_insn, insn))
16917                         return get_attr_sign_extend (dep_insn)
16918                                == SIGN_EXTEND_YES ? 6 : 4;
16919                       break;
16920                     }
16921                   case TYPE_SHIFT:
16922                     {
16923                       if (set_to_load_agen (dep_insn, insn))
16924                         return get_attr_var_shift (dep_insn) == VAR_SHIFT_YES ?
16925                                6 : 3;
16926                       break;
16927                     }
16928                   case TYPE_INTEGER:
16929                   case TYPE_ADD:
16930                   case TYPE_LOGICAL:
16931                   case TYPE_EXTS:
16932                   case TYPE_INSERT:
16933                     {
16934                       if (set_to_load_agen (dep_insn, insn))
16935                         return 3;
16936                       break;
16937                     }
16938                   case TYPE_STORE:
16939                   case TYPE_FPLOAD:
16940                   case TYPE_FPSTORE:
16941                     {
16942                       if (get_attr_update (dep_insn) == UPDATE_YES
16943                           && set_to_load_agen (dep_insn, insn))
16944                         return 3;
16945                       break;
16946                     }
16947                   case TYPE_MUL:
16948                     {
16949                       if (set_to_load_agen (dep_insn, insn))
16950                         return 17;
16951                       break;
16952                     }
16953                   case TYPE_DIV:
16954                     {
16955                       if (set_to_load_agen (dep_insn, insn))
16956                         return get_attr_size (dep_insn) == SIZE_32 ? 45 : 57;
16957                       break;
16958                     }
16959                   default:
16960                     break;
16961                   }
16962               }
16963             break;
16964
16965           case TYPE_FPLOAD:
16966             if ((rs6000_tune == PROCESSOR_POWER6)
16967                 && get_attr_update (insn) == UPDATE_NO
16968                 && recog_memoized (dep_insn)
16969                 && (INSN_CODE (dep_insn) >= 0)
16970                 && (get_attr_type (dep_insn) == TYPE_MFFGPR))
16971               return 2;
16972
16973           default:
16974             break;
16975           }
16976
16977         /* Fall out to return default cost.  */
16978       }
16979       break;
16980
16981     case REG_DEP_OUTPUT:
16982       /* Output dependency; DEP_INSN writes a register that INSN writes some
16983          cycles later.  */
16984       if ((rs6000_tune == PROCESSOR_POWER6)
16985           && recog_memoized (dep_insn)
16986           && (INSN_CODE (dep_insn) >= 0))
16987         {
16988           attr_type = get_attr_type (insn);
16989
16990           switch (attr_type)
16991             {
16992             case TYPE_FP:
16993             case TYPE_FPSIMPLE:
16994               if (get_attr_type (dep_insn) == TYPE_FP
16995                   || get_attr_type (dep_insn) == TYPE_FPSIMPLE)
16996                 return 1;
16997               break;
16998             case TYPE_FPLOAD:
16999               if (get_attr_update (insn) == UPDATE_NO
17000                   && get_attr_type (dep_insn) == TYPE_MFFGPR)
17001                 return 2;
17002               break;
17003             default:
17004               break;
17005             }
17006         }
17007       /* Fall through, no cost for output dependency.  */
17008       /* FALLTHRU */
17009
17010     case REG_DEP_ANTI:
17011       /* Anti dependency; DEP_INSN reads a register that INSN writes some
17012          cycles later.  */
17013       return 0;
17014
17015     default:
17016       gcc_unreachable ();
17017     }
17018
17019   return cost;
17020 }
17021
17022 /* Debug version of rs6000_adjust_cost.  */
17023
17024 static int
17025 rs6000_debug_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn,
17026                           int cost, unsigned int dw)
17027 {
17028   int ret = rs6000_adjust_cost (insn, dep_type, dep_insn, cost, dw);
17029
17030   if (ret != cost)
17031     {
17032       const char *dep;
17033
17034       switch (dep_type)
17035         {
17036         default:             dep = "unknown depencency"; break;
17037         case REG_DEP_TRUE:   dep = "data dependency";    break;
17038         case REG_DEP_OUTPUT: dep = "output dependency";  break;
17039         case REG_DEP_ANTI:   dep = "anti depencency";    break;
17040         }
17041
17042       fprintf (stderr,
17043                "\nrs6000_adjust_cost, final cost = %d, orig cost = %d, "
17044                "%s, insn:\n", ret, cost, dep);
17045
17046       debug_rtx (insn);
17047     }
17048
17049   return ret;
17050 }
17051
17052 /* The function returns a true if INSN is microcoded.
17053    Return false otherwise.  */
17054
17055 static bool
17056 is_microcoded_insn (rtx_insn *insn)
17057 {
17058   if (!insn || !NONDEBUG_INSN_P (insn)
17059       || GET_CODE (PATTERN (insn)) == USE
17060       || GET_CODE (PATTERN (insn)) == CLOBBER)
17061     return false;
17062
17063   if (rs6000_tune == PROCESSOR_CELL)
17064     return get_attr_cell_micro (insn) == CELL_MICRO_ALWAYS;
17065
17066   if (rs6000_sched_groups
17067       && (rs6000_tune == PROCESSOR_POWER4 || rs6000_tune == PROCESSOR_POWER5))
17068     {
17069       enum attr_type type = get_attr_type (insn);
17070       if ((type == TYPE_LOAD
17071            && get_attr_update (insn) == UPDATE_YES
17072            && get_attr_sign_extend (insn) == SIGN_EXTEND_YES)
17073           || ((type == TYPE_LOAD || type == TYPE_STORE)
17074               && get_attr_update (insn) == UPDATE_YES
17075               && get_attr_indexed (insn) == INDEXED_YES)
17076           || type == TYPE_MFCR)
17077         return true;
17078     }
17079
17080   return false;
17081 }
17082
17083 /* The function returns true if INSN is cracked into 2 instructions
17084    by the processor (and therefore occupies 2 issue slots).  */
17085
17086 static bool
17087 is_cracked_insn (rtx_insn *insn)
17088 {
17089   if (!insn || !NONDEBUG_INSN_P (insn)
17090       || GET_CODE (PATTERN (insn)) == USE
17091       || GET_CODE (PATTERN (insn)) == CLOBBER)
17092     return false;
17093
17094   if (rs6000_sched_groups
17095       && (rs6000_tune == PROCESSOR_POWER4 || rs6000_tune == PROCESSOR_POWER5))
17096     {
17097       enum attr_type type = get_attr_type (insn);
17098       if ((type == TYPE_LOAD
17099            && get_attr_sign_extend (insn) == SIGN_EXTEND_YES
17100            && get_attr_update (insn) == UPDATE_NO)
17101           || (type == TYPE_LOAD
17102               && get_attr_sign_extend (insn) == SIGN_EXTEND_NO
17103               && get_attr_update (insn) == UPDATE_YES
17104               && get_attr_indexed (insn) == INDEXED_NO)
17105           || (type == TYPE_STORE
17106               && get_attr_update (insn) == UPDATE_YES
17107               && get_attr_indexed (insn) == INDEXED_NO)
17108           || ((type == TYPE_FPLOAD || type == TYPE_FPSTORE)
17109               && get_attr_update (insn) == UPDATE_YES)
17110           || (type == TYPE_CR_LOGICAL
17111               && get_attr_cr_logical_3op (insn) == CR_LOGICAL_3OP_YES)
17112           || (type == TYPE_EXTS
17113               && get_attr_dot (insn) == DOT_YES)
17114           || (type == TYPE_SHIFT
17115               && get_attr_dot (insn) == DOT_YES
17116               && get_attr_var_shift (insn) == VAR_SHIFT_NO)
17117           || (type == TYPE_MUL
17118               && get_attr_dot (insn) == DOT_YES)
17119           || type == TYPE_DIV
17120           || (type == TYPE_INSERT
17121               && get_attr_size (insn) == SIZE_32))
17122         return true;
17123     }
17124
17125   return false;
17126 }
17127
17128 /* The function returns true if INSN can be issued only from
17129    the branch slot.  */
17130
17131 static bool
17132 is_branch_slot_insn (rtx_insn *insn)
17133 {
17134   if (!insn || !NONDEBUG_INSN_P (insn)
17135       || GET_CODE (PATTERN (insn)) == USE
17136       || GET_CODE (PATTERN (insn)) == CLOBBER)
17137     return false;
17138
17139   if (rs6000_sched_groups)
17140     {
17141       enum attr_type type = get_attr_type (insn);
17142       if (type == TYPE_BRANCH || type == TYPE_JMPREG)
17143         return true;
17144       return false;
17145     }
17146
17147   return false;
17148 }
17149
17150 /* The function returns true if out_inst sets a value that is
17151    used in the address generation computation of in_insn */
17152 static bool
17153 set_to_load_agen (rtx_insn *out_insn, rtx_insn *in_insn)
17154 {
17155   rtx out_set, in_set;
17156
17157   /* For performance reasons, only handle the simple case where
17158      both loads are a single_set. */
17159   out_set = single_set (out_insn);
17160   if (out_set)
17161     {
17162       in_set = single_set (in_insn);
17163       if (in_set)
17164         return reg_mentioned_p (SET_DEST (out_set), SET_SRC (in_set));
17165     }
17166
17167   return false;
17168 }
17169
17170 /* Try to determine base/offset/size parts of the given MEM.
17171    Return true if successful, false if all the values couldn't
17172    be determined.
17173
17174    This function only looks for REG or REG+CONST address forms.
17175    REG+REG address form will return false. */
17176
17177 static bool
17178 get_memref_parts (rtx mem, rtx *base, HOST_WIDE_INT *offset,
17179                   HOST_WIDE_INT *size)
17180 {
17181   rtx addr_rtx;
17182   if MEM_SIZE_KNOWN_P (mem)
17183     *size = MEM_SIZE (mem);
17184   else
17185     return false;
17186
17187   addr_rtx = (XEXP (mem, 0));
17188   if (GET_CODE (addr_rtx) == PRE_MODIFY)
17189     addr_rtx = XEXP (addr_rtx, 1);
17190
17191   *offset = 0;
17192   while (GET_CODE (addr_rtx) == PLUS
17193          && CONST_INT_P (XEXP (addr_rtx, 1)))
17194     {
17195       *offset += INTVAL (XEXP (addr_rtx, 1));
17196       addr_rtx = XEXP (addr_rtx, 0);
17197     }
17198   if (!REG_P (addr_rtx))
17199     return false;
17200
17201   *base = addr_rtx;
17202   return true;
17203 }
17204
17205 /* The function returns true if the target storage location of
17206    mem1 is adjacent to the target storage location of mem2 */
17207 /* Return 1 if memory locations are adjacent.  */
17208
17209 static bool
17210 adjacent_mem_locations (rtx mem1, rtx mem2)
17211 {
17212   rtx reg1, reg2;
17213   HOST_WIDE_INT off1, size1, off2, size2;
17214
17215   if (get_memref_parts (mem1, &reg1, &off1, &size1)
17216       && get_memref_parts (mem2, &reg2, &off2, &size2))
17217     return ((REGNO (reg1) == REGNO (reg2))
17218             && ((off1 + size1 == off2)
17219                 || (off2 + size2 == off1)));
17220
17221   return false;
17222 }
17223
17224 /* This function returns true if it can be determined that the two MEM
17225    locations overlap by at least 1 byte based on base reg/offset/size. */
17226
17227 static bool
17228 mem_locations_overlap (rtx mem1, rtx mem2)
17229 {
17230   rtx reg1, reg2;
17231   HOST_WIDE_INT off1, size1, off2, size2;
17232
17233   if (get_memref_parts (mem1, &reg1, &off1, &size1)
17234       && get_memref_parts (mem2, &reg2, &off2, &size2))
17235     return ((REGNO (reg1) == REGNO (reg2))
17236             && (((off1 <= off2) && (off1 + size1 > off2))
17237                 || ((off2 <= off1) && (off2 + size2 > off1))));
17238
17239   return false;
17240 }
17241
17242 /* A C statement (sans semicolon) to update the integer scheduling
17243    priority INSN_PRIORITY (INSN). Increase the priority to execute the
17244    INSN earlier, reduce the priority to execute INSN later.  Do not
17245    define this macro if you do not need to adjust the scheduling
17246    priorities of insns.  */
17247
17248 static int
17249 rs6000_adjust_priority (rtx_insn *insn ATTRIBUTE_UNUSED, int priority)
17250 {
17251   rtx load_mem, str_mem;
17252   /* On machines (like the 750) which have asymmetric integer units,
17253      where one integer unit can do multiply and divides and the other
17254      can't, reduce the priority of multiply/divide so it is scheduled
17255      before other integer operations.  */
17256
17257 #if 0
17258   if (! INSN_P (insn))
17259     return priority;
17260
17261   if (GET_CODE (PATTERN (insn)) == USE)
17262     return priority;
17263
17264   switch (rs6000_tune) {
17265   case PROCESSOR_PPC750:
17266     switch (get_attr_type (insn))
17267       {
17268       default:
17269         break;
17270
17271       case TYPE_MUL:
17272       case TYPE_DIV:
17273         fprintf (stderr, "priority was %#x (%d) before adjustment\n",
17274                  priority, priority);
17275         if (priority >= 0 && priority < 0x01000000)
17276           priority >>= 3;
17277         break;
17278       }
17279   }
17280 #endif
17281
17282   if (insn_must_be_first_in_group (insn)
17283       && reload_completed
17284       && current_sched_info->sched_max_insns_priority
17285       && rs6000_sched_restricted_insns_priority)
17286     {
17287
17288       /* Prioritize insns that can be dispatched only in the first
17289          dispatch slot.  */
17290       if (rs6000_sched_restricted_insns_priority == 1)
17291         /* Attach highest priority to insn. This means that in
17292            haifa-sched.c:ready_sort(), dispatch-slot restriction considerations
17293            precede 'priority' (critical path) considerations.  */
17294         return current_sched_info->sched_max_insns_priority;
17295       else if (rs6000_sched_restricted_insns_priority == 2)
17296         /* Increase priority of insn by a minimal amount. This means that in
17297            haifa-sched.c:ready_sort(), only 'priority' (critical path)
17298            considerations precede dispatch-slot restriction considerations.  */
17299         return (priority + 1);
17300     }
17301
17302   if (rs6000_tune == PROCESSOR_POWER6
17303       && ((load_store_pendulum == -2 && is_load_insn (insn, &load_mem))
17304           || (load_store_pendulum == 2 && is_store_insn (insn, &str_mem))))
17305     /* Attach highest priority to insn if the scheduler has just issued two
17306        stores and this instruction is a load, or two loads and this instruction
17307        is a store. Power6 wants loads and stores scheduled alternately
17308        when possible */
17309     return current_sched_info->sched_max_insns_priority;
17310
17311   return priority;
17312 }
17313
17314 /* Return true if the instruction is nonpipelined on the Cell. */
17315 static bool
17316 is_nonpipeline_insn (rtx_insn *insn)
17317 {
17318   enum attr_type type;
17319   if (!insn || !NONDEBUG_INSN_P (insn)
17320       || GET_CODE (PATTERN (insn)) == USE
17321       || GET_CODE (PATTERN (insn)) == CLOBBER)
17322     return false;
17323
17324   type = get_attr_type (insn);
17325   if (type == TYPE_MUL
17326       || type == TYPE_DIV
17327       || type == TYPE_SDIV
17328       || type == TYPE_DDIV
17329       || type == TYPE_SSQRT
17330       || type == TYPE_DSQRT
17331       || type == TYPE_MFCR
17332       || type == TYPE_MFCRF
17333       || type == TYPE_MFJMPR)
17334     {
17335       return true;
17336     }
17337   return false;
17338 }
17339
17340
17341 /* Return how many instructions the machine can issue per cycle.  */
17342
17343 static int
17344 rs6000_issue_rate (void)
17345 {
17346   /* Unless scheduling for register pressure, use issue rate of 1 for
17347      first scheduling pass to decrease degradation.  */
17348   if (!reload_completed && !flag_sched_pressure)
17349     return 1;
17350
17351   switch (rs6000_tune) {
17352   case PROCESSOR_RS64A:
17353   case PROCESSOR_PPC601: /* ? */
17354   case PROCESSOR_PPC7450:
17355     return 3;
17356   case PROCESSOR_PPC440:
17357   case PROCESSOR_PPC603:
17358   case PROCESSOR_PPC750:
17359   case PROCESSOR_PPC7400:
17360   case PROCESSOR_PPC8540:
17361   case PROCESSOR_PPC8548:
17362   case PROCESSOR_CELL:
17363   case PROCESSOR_PPCE300C2:
17364   case PROCESSOR_PPCE300C3:
17365   case PROCESSOR_PPCE500MC:
17366   case PROCESSOR_PPCE500MC64:
17367   case PROCESSOR_PPCE5500:
17368   case PROCESSOR_PPCE6500:
17369   case PROCESSOR_TITAN:
17370     return 2;
17371   case PROCESSOR_PPC476:
17372   case PROCESSOR_PPC604:
17373   case PROCESSOR_PPC604e:
17374   case PROCESSOR_PPC620:
17375   case PROCESSOR_PPC630:
17376     return 4;
17377   case PROCESSOR_POWER4:
17378   case PROCESSOR_POWER5:
17379   case PROCESSOR_POWER6:
17380   case PROCESSOR_POWER7:
17381     return 5;
17382   case PROCESSOR_POWER8:
17383     return 7;
17384   case PROCESSOR_POWER9:
17385   case PROCESSOR_FUTURE:
17386     return 6;
17387   default:
17388     return 1;
17389   }
17390 }
17391
17392 /* Return how many instructions to look ahead for better insn
17393    scheduling.  */
17394
17395 static int
17396 rs6000_use_sched_lookahead (void)
17397 {
17398   switch (rs6000_tune)
17399     {
17400     case PROCESSOR_PPC8540:
17401     case PROCESSOR_PPC8548:
17402       return 4;
17403
17404     case PROCESSOR_CELL:
17405       return (reload_completed ? 8 : 0);
17406
17407     default:
17408       return 0;
17409     }
17410 }
17411
17412 /* We are choosing insn from the ready queue.  Return zero if INSN can be
17413    chosen.  */
17414 static int
17415 rs6000_use_sched_lookahead_guard (rtx_insn *insn, int ready_index)
17416 {
17417   if (ready_index == 0)
17418     return 0;
17419
17420   if (rs6000_tune != PROCESSOR_CELL)
17421     return 0;
17422
17423   gcc_assert (insn != NULL_RTX && INSN_P (insn));
17424
17425   if (!reload_completed
17426       || is_nonpipeline_insn (insn)
17427       || is_microcoded_insn (insn))
17428     return 1;
17429
17430   return 0;
17431 }
17432
17433 /* Determine if PAT refers to memory. If so, set MEM_REF to the MEM rtx
17434    and return true.  */
17435
17436 static bool
17437 find_mem_ref (rtx pat, rtx *mem_ref)
17438 {
17439   const char * fmt;
17440   int i, j;
17441
17442   /* stack_tie does not produce any real memory traffic.  */
17443   if (tie_operand (pat, VOIDmode))
17444     return false;
17445
17446   if (MEM_P (pat))
17447     {
17448       *mem_ref = pat;
17449       return true;
17450     }
17451
17452   /* Recursively process the pattern.  */
17453   fmt = GET_RTX_FORMAT (GET_CODE (pat));
17454
17455   for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
17456     {
17457       if (fmt[i] == 'e')
17458         {
17459           if (find_mem_ref (XEXP (pat, i), mem_ref))
17460             return true;
17461         }
17462       else if (fmt[i] == 'E')
17463         for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
17464           {
17465             if (find_mem_ref (XVECEXP (pat, i, j), mem_ref))
17466               return true;
17467           }
17468     }
17469
17470   return false;
17471 }
17472
17473 /* Determine if PAT is a PATTERN of a load insn.  */
17474
17475 static bool
17476 is_load_insn1 (rtx pat, rtx *load_mem)
17477 {
17478   if (!pat || pat == NULL_RTX)
17479     return false;
17480
17481   if (GET_CODE (pat) == SET)
17482     return find_mem_ref (SET_SRC (pat), load_mem);
17483
17484   if (GET_CODE (pat) == PARALLEL)
17485     {
17486       int i;
17487
17488       for (i = 0; i < XVECLEN (pat, 0); i++)
17489         if (is_load_insn1 (XVECEXP (pat, 0, i), load_mem))
17490           return true;
17491     }
17492
17493   return false;
17494 }
17495
17496 /* Determine if INSN loads from memory.  */
17497
17498 static bool
17499 is_load_insn (rtx insn, rtx *load_mem)
17500 {
17501   if (!insn || !INSN_P (insn))
17502     return false;
17503
17504   if (CALL_P (insn))
17505     return false;
17506
17507   return is_load_insn1 (PATTERN (insn), load_mem);
17508 }
17509
17510 /* Determine if PAT is a PATTERN of a store insn.  */
17511
17512 static bool
17513 is_store_insn1 (rtx pat, rtx *str_mem)
17514 {
17515   if (!pat || pat == NULL_RTX)
17516     return false;
17517
17518   if (GET_CODE (pat) == SET)
17519     return find_mem_ref (SET_DEST (pat), str_mem);
17520
17521   if (GET_CODE (pat) == PARALLEL)
17522     {
17523       int i;
17524
17525       for (i = 0; i < XVECLEN (pat, 0); i++)
17526         if (is_store_insn1 (XVECEXP (pat, 0, i), str_mem))
17527           return true;
17528     }
17529
17530   return false;
17531 }
17532
17533 /* Determine if INSN stores to memory.  */
17534
17535 static bool
17536 is_store_insn (rtx insn, rtx *str_mem)
17537 {
17538   if (!insn || !INSN_P (insn))
17539     return false;
17540
17541   return is_store_insn1 (PATTERN (insn), str_mem);
17542 }
17543
17544 /* Return whether TYPE is a Power9 pairable vector instruction type.  */
17545
17546 static bool
17547 is_power9_pairable_vec_type (enum attr_type type)
17548 {
17549   switch (type)
17550     {
17551       case TYPE_VECSIMPLE:
17552       case TYPE_VECCOMPLEX:
17553       case TYPE_VECDIV:
17554       case TYPE_VECCMP:
17555       case TYPE_VECPERM:
17556       case TYPE_VECFLOAT:
17557       case TYPE_VECFDIV:
17558       case TYPE_VECDOUBLE:
17559         return true;
17560       default:
17561         break;
17562     }
17563   return false;
17564 }
17565
17566 /* Returns whether the dependence between INSN and NEXT is considered
17567    costly by the given target.  */
17568
17569 static bool
17570 rs6000_is_costly_dependence (dep_t dep, int cost, int distance)
17571 {
17572   rtx insn;
17573   rtx next;
17574   rtx load_mem, str_mem;
17575
17576   /* If the flag is not enabled - no dependence is considered costly;
17577      allow all dependent insns in the same group.
17578      This is the most aggressive option.  */
17579   if (rs6000_sched_costly_dep == no_dep_costly)
17580     return false;
17581
17582   /* If the flag is set to 1 - a dependence is always considered costly;
17583      do not allow dependent instructions in the same group.
17584      This is the most conservative option.  */
17585   if (rs6000_sched_costly_dep == all_deps_costly)
17586     return true;
17587
17588   insn = DEP_PRO (dep);
17589   next = DEP_CON (dep);
17590
17591   if (rs6000_sched_costly_dep == store_to_load_dep_costly
17592       && is_load_insn (next, &load_mem)
17593       && is_store_insn (insn, &str_mem))
17594     /* Prevent load after store in the same group.  */
17595     return true;
17596
17597   if (rs6000_sched_costly_dep == true_store_to_load_dep_costly
17598       && is_load_insn (next, &load_mem)
17599       && is_store_insn (insn, &str_mem)
17600       && DEP_TYPE (dep) == REG_DEP_TRUE
17601       && mem_locations_overlap(str_mem, load_mem))
17602      /* Prevent load after store in the same group if it is a true
17603         dependence.  */
17604      return true;
17605
17606   /* The flag is set to X; dependences with latency >= X are considered costly,
17607      and will not be scheduled in the same group.  */
17608   if (rs6000_sched_costly_dep <= max_dep_latency
17609       && ((cost - distance) >= (int)rs6000_sched_costly_dep))
17610     return true;
17611
17612   return false;
17613 }
17614
17615 /* Return the next insn after INSN that is found before TAIL is reached,
17616    skipping any "non-active" insns - insns that will not actually occupy
17617    an issue slot.  Return NULL_RTX if such an insn is not found.  */
17618
17619 static rtx_insn *
17620 get_next_active_insn (rtx_insn *insn, rtx_insn *tail)
17621 {
17622   if (insn == NULL_RTX || insn == tail)
17623     return NULL;
17624
17625   while (1)
17626     {
17627       insn = NEXT_INSN (insn);
17628       if (insn == NULL_RTX || insn == tail)
17629         return NULL;
17630
17631       if (CALL_P (insn)
17632           || JUMP_P (insn) || JUMP_TABLE_DATA_P (insn)
17633           || (NONJUMP_INSN_P (insn)
17634               && GET_CODE (PATTERN (insn)) != USE
17635               && GET_CODE (PATTERN (insn)) != CLOBBER
17636               && INSN_CODE (insn) != CODE_FOR_stack_tie))
17637         break;
17638     }
17639   return insn;
17640 }
17641
17642 /* Do Power9 specific sched_reorder2 reordering of ready list.  */
17643
17644 static int
17645 power9_sched_reorder2 (rtx_insn **ready, int lastpos)
17646 {
17647   int pos;
17648   int i;
17649   rtx_insn *tmp;
17650   enum attr_type type, type2;
17651
17652   type = get_attr_type (last_scheduled_insn);
17653
17654   /* Try to issue fixed point divides back-to-back in pairs so they will be
17655      routed to separate execution units and execute in parallel.  */
17656   if (type == TYPE_DIV && divide_cnt == 0)
17657     {
17658       /* First divide has been scheduled.  */
17659       divide_cnt = 1;
17660
17661       /* Scan the ready list looking for another divide, if found move it
17662          to the end of the list so it is chosen next.  */
17663       pos = lastpos;
17664       while (pos >= 0)
17665         {
17666           if (recog_memoized (ready[pos]) >= 0
17667               && get_attr_type (ready[pos]) == TYPE_DIV)
17668             {
17669               tmp = ready[pos];
17670               for (i = pos; i < lastpos; i++)
17671                 ready[i] = ready[i + 1];
17672               ready[lastpos] = tmp;
17673               break;
17674             }
17675           pos--;
17676         }
17677     }
17678   else
17679     {
17680       /* Last insn was the 2nd divide or not a divide, reset the counter.  */
17681       divide_cnt = 0;
17682
17683       /* The best dispatch throughput for vector and vector load insns can be
17684          achieved by interleaving a vector and vector load such that they'll
17685          dispatch to the same superslice. If this pairing cannot be achieved
17686          then it is best to pair vector insns together and vector load insns
17687          together.
17688
17689          To aid in this pairing, vec_pairing maintains the current state with
17690          the following values:
17691
17692              0  : Initial state, no vecload/vector pairing has been started.
17693
17694              1  : A vecload or vector insn has been issued and a candidate for
17695                   pairing has been found and moved to the end of the ready
17696                   list.  */
17697       if (type == TYPE_VECLOAD)
17698         {
17699           /* Issued a vecload.  */
17700           if (vec_pairing == 0)
17701             {
17702               int vecload_pos = -1;
17703               /* We issued a single vecload, look for a vector insn to pair it
17704                  with.  If one isn't found, try to pair another vecload.  */
17705               pos = lastpos;
17706               while (pos >= 0)
17707                 {
17708                   if (recog_memoized (ready[pos]) >= 0)
17709                     {
17710                       type2 = get_attr_type (ready[pos]);
17711                       if (is_power9_pairable_vec_type (type2))
17712                         {
17713                           /* Found a vector insn to pair with, move it to the
17714                              end of the ready list so it is scheduled next.  */
17715                           tmp = ready[pos];
17716                           for (i = pos; i < lastpos; i++)
17717                             ready[i] = ready[i + 1];
17718                           ready[lastpos] = tmp;
17719                           vec_pairing = 1;
17720                           return cached_can_issue_more;
17721                         }
17722                       else if (type2 == TYPE_VECLOAD && vecload_pos == -1)
17723                         /* Remember position of first vecload seen.  */
17724                         vecload_pos = pos;
17725                     }
17726                   pos--;
17727                 }
17728               if (vecload_pos >= 0)
17729                 {
17730                   /* Didn't find a vector to pair with but did find a vecload,
17731                      move it to the end of the ready list.  */
17732                   tmp = ready[vecload_pos];
17733                   for (i = vecload_pos; i < lastpos; i++)
17734                     ready[i] = ready[i + 1];
17735                   ready[lastpos] = tmp;
17736                   vec_pairing = 1;
17737                   return cached_can_issue_more;
17738                 }
17739             }
17740         }
17741       else if (is_power9_pairable_vec_type (type))
17742         {
17743           /* Issued a vector operation.  */
17744           if (vec_pairing == 0)
17745             {
17746               int vec_pos = -1;
17747               /* We issued a single vector insn, look for a vecload to pair it
17748                  with.  If one isn't found, try to pair another vector.  */
17749               pos = lastpos;
17750               while (pos >= 0)
17751                 {
17752                   if (recog_memoized (ready[pos]) >= 0)
17753                     {
17754                       type2 = get_attr_type (ready[pos]);
17755                       if (type2 == TYPE_VECLOAD)
17756                         {
17757                           /* Found a vecload insn to pair with, move it to the
17758                              end of the ready list so it is scheduled next.  */
17759                           tmp = ready[pos];
17760                           for (i = pos; i < lastpos; i++)
17761                             ready[i] = ready[i + 1];
17762                           ready[lastpos] = tmp;
17763                           vec_pairing = 1;
17764                           return cached_can_issue_more;
17765                         }
17766                       else if (is_power9_pairable_vec_type (type2)
17767                                && vec_pos == -1)
17768                         /* Remember position of first vector insn seen.  */
17769                         vec_pos = pos;
17770                     }
17771                   pos--;
17772                 }
17773               if (vec_pos >= 0)
17774                 {
17775                   /* Didn't find a vecload to pair with but did find a vector
17776                      insn, move it to the end of the ready list.  */
17777                   tmp = ready[vec_pos];
17778                   for (i = vec_pos; i < lastpos; i++)
17779                     ready[i] = ready[i + 1];
17780                   ready[lastpos] = tmp;
17781                   vec_pairing = 1;
17782                   return cached_can_issue_more;
17783                 }
17784             }
17785         }
17786
17787       /* We've either finished a vec/vecload pair, couldn't find an insn to
17788          continue the current pair, or the last insn had nothing to do with
17789          with pairing.  In any case, reset the state.  */
17790       vec_pairing = 0;
17791     }
17792
17793   return cached_can_issue_more;
17794 }
17795
17796 /* We are about to begin issuing insns for this clock cycle. */
17797
17798 static int
17799 rs6000_sched_reorder (FILE *dump ATTRIBUTE_UNUSED, int sched_verbose,
17800                         rtx_insn **ready ATTRIBUTE_UNUSED,
17801                         int *pn_ready ATTRIBUTE_UNUSED,
17802                         int clock_var ATTRIBUTE_UNUSED)
17803 {
17804   int n_ready = *pn_ready;
17805
17806   if (sched_verbose)
17807     fprintf (dump, "// rs6000_sched_reorder :\n");
17808
17809   /* Reorder the ready list, if the second to last ready insn
17810      is a nonepipeline insn.  */
17811   if (rs6000_tune == PROCESSOR_CELL && n_ready > 1)
17812   {
17813     if (is_nonpipeline_insn (ready[n_ready - 1])
17814         && (recog_memoized (ready[n_ready - 2]) > 0))
17815       /* Simply swap first two insns.  */
17816       std::swap (ready[n_ready - 1], ready[n_ready - 2]);
17817   }
17818
17819   if (rs6000_tune == PROCESSOR_POWER6)
17820     load_store_pendulum = 0;
17821
17822   return rs6000_issue_rate ();
17823 }
17824
17825 /* Like rs6000_sched_reorder, but called after issuing each insn.  */
17826
17827 static int
17828 rs6000_sched_reorder2 (FILE *dump, int sched_verbose, rtx_insn **ready,
17829                          int *pn_ready, int clock_var ATTRIBUTE_UNUSED)
17830 {
17831   if (sched_verbose)
17832     fprintf (dump, "// rs6000_sched_reorder2 :\n");
17833
17834   /* For Power6, we need to handle some special cases to try and keep the
17835      store queue from overflowing and triggering expensive flushes.
17836
17837      This code monitors how load and store instructions are being issued
17838      and skews the ready list one way or the other to increase the likelihood
17839      that a desired instruction is issued at the proper time.
17840
17841      A couple of things are done.  First, we maintain a "load_store_pendulum"
17842      to track the current state of load/store issue.
17843
17844        - If the pendulum is at zero, then no loads or stores have been
17845          issued in the current cycle so we do nothing.
17846
17847        - If the pendulum is 1, then a single load has been issued in this
17848          cycle and we attempt to locate another load in the ready list to
17849          issue with it.
17850
17851        - If the pendulum is -2, then two stores have already been
17852          issued in this cycle, so we increase the priority of the first load
17853          in the ready list to increase it's likelihood of being chosen first
17854          in the next cycle.
17855
17856        - If the pendulum is -1, then a single store has been issued in this
17857          cycle and we attempt to locate another store in the ready list to
17858          issue with it, preferring a store to an adjacent memory location to
17859          facilitate store pairing in the store queue.
17860
17861        - If the pendulum is 2, then two loads have already been
17862          issued in this cycle, so we increase the priority of the first store
17863          in the ready list to increase it's likelihood of being chosen first
17864          in the next cycle.
17865
17866        - If the pendulum < -2 or > 2, then do nothing.
17867
17868        Note: This code covers the most common scenarios.  There exist non
17869              load/store instructions which make use of the LSU and which
17870              would need to be accounted for to strictly model the behavior
17871              of the machine.  Those instructions are currently unaccounted
17872              for to help minimize compile time overhead of this code.
17873    */
17874   if (rs6000_tune == PROCESSOR_POWER6 && last_scheduled_insn)
17875     {
17876       int pos;
17877       int i;
17878       rtx_insn *tmp;
17879       rtx load_mem, str_mem;
17880
17881       if (is_store_insn (last_scheduled_insn, &str_mem))
17882         /* Issuing a store, swing the load_store_pendulum to the left */
17883         load_store_pendulum--;
17884       else if (is_load_insn (last_scheduled_insn, &load_mem))
17885         /* Issuing a load, swing the load_store_pendulum to the right */
17886         load_store_pendulum++;
17887       else
17888         return cached_can_issue_more;
17889
17890       /* If the pendulum is balanced, or there is only one instruction on
17891          the ready list, then all is well, so return. */
17892       if ((load_store_pendulum == 0) || (*pn_ready <= 1))
17893         return cached_can_issue_more;
17894
17895       if (load_store_pendulum == 1)
17896         {
17897           /* A load has been issued in this cycle.  Scan the ready list
17898              for another load to issue with it */
17899           pos = *pn_ready-1;
17900
17901           while (pos >= 0)
17902             {
17903               if (is_load_insn (ready[pos], &load_mem))
17904                 {
17905                   /* Found a load.  Move it to the head of the ready list,
17906                      and adjust it's priority so that it is more likely to
17907                      stay there */
17908                   tmp = ready[pos];
17909                   for (i=pos; i<*pn_ready-1; i++)
17910                     ready[i] = ready[i + 1];
17911                   ready[*pn_ready-1] = tmp;
17912
17913                   if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
17914                     INSN_PRIORITY (tmp)++;
17915                   break;
17916                 }
17917               pos--;
17918             }
17919         }
17920       else if (load_store_pendulum == -2)
17921         {
17922           /* Two stores have been issued in this cycle.  Increase the
17923              priority of the first load in the ready list to favor it for
17924              issuing in the next cycle. */
17925           pos = *pn_ready-1;
17926
17927           while (pos >= 0)
17928             {
17929               if (is_load_insn (ready[pos], &load_mem)
17930                   && !sel_sched_p ()
17931                   && INSN_PRIORITY_KNOWN (ready[pos]))
17932                 {
17933                   INSN_PRIORITY (ready[pos])++;
17934
17935                   /* Adjust the pendulum to account for the fact that a load
17936                      was found and increased in priority.  This is to prevent
17937                      increasing the priority of multiple loads */
17938                   load_store_pendulum--;
17939
17940                   break;
17941                 }
17942               pos--;
17943             }
17944         }
17945       else if (load_store_pendulum == -1)
17946         {
17947           /* A store has been issued in this cycle.  Scan the ready list for
17948              another store to issue with it, preferring a store to an adjacent
17949              memory location */
17950           int first_store_pos = -1;
17951
17952           pos = *pn_ready-1;
17953
17954           while (pos >= 0)
17955             {
17956               if (is_store_insn (ready[pos], &str_mem))
17957                 {
17958                   rtx str_mem2;
17959                   /* Maintain the index of the first store found on the
17960                      list */
17961                   if (first_store_pos == -1)
17962                     first_store_pos = pos;
17963
17964                   if (is_store_insn (last_scheduled_insn, &str_mem2)
17965                       && adjacent_mem_locations (str_mem, str_mem2))
17966                     {
17967                       /* Found an adjacent store.  Move it to the head of the
17968                          ready list, and adjust it's priority so that it is
17969                          more likely to stay there */
17970                       tmp = ready[pos];
17971                       for (i=pos; i<*pn_ready-1; i++)
17972                         ready[i] = ready[i + 1];
17973                       ready[*pn_ready-1] = tmp;
17974
17975                       if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
17976                         INSN_PRIORITY (tmp)++;
17977
17978                       first_store_pos = -1;
17979
17980                       break;
17981                     };
17982                 }
17983               pos--;
17984             }
17985
17986           if (first_store_pos >= 0)
17987             {
17988               /* An adjacent store wasn't found, but a non-adjacent store was,
17989                  so move the non-adjacent store to the front of the ready
17990                  list, and adjust its priority so that it is more likely to
17991                  stay there. */
17992               tmp = ready[first_store_pos];
17993               for (i=first_store_pos; i<*pn_ready-1; i++)
17994                 ready[i] = ready[i + 1];
17995               ready[*pn_ready-1] = tmp;
17996               if (!sel_sched_p () && INSN_PRIORITY_KNOWN (tmp))
17997                 INSN_PRIORITY (tmp)++;
17998             }
17999         }
18000       else if (load_store_pendulum == 2)
18001        {
18002            /* Two loads have been issued in this cycle.  Increase the priority
18003               of the first store in the ready list to favor it for issuing in
18004               the next cycle. */
18005           pos = *pn_ready-1;
18006
18007           while (pos >= 0)
18008             {
18009               if (is_store_insn (ready[pos], &str_mem)
18010                   && !sel_sched_p ()
18011                   && INSN_PRIORITY_KNOWN (ready[pos]))
18012                 {
18013                   INSN_PRIORITY (ready[pos])++;
18014
18015                   /* Adjust the pendulum to account for the fact that a store
18016                      was found and increased in priority.  This is to prevent
18017                      increasing the priority of multiple stores */
18018                   load_store_pendulum++;
18019
18020                   break;
18021                 }
18022               pos--;
18023             }
18024         }
18025     }
18026
18027   /* Do Power9 dependent reordering if necessary.  */
18028   if (rs6000_tune == PROCESSOR_POWER9 && last_scheduled_insn
18029       && recog_memoized (last_scheduled_insn) >= 0)
18030     return power9_sched_reorder2 (ready, *pn_ready - 1);
18031
18032   return cached_can_issue_more;
18033 }
18034
18035 /* Return whether the presence of INSN causes a dispatch group termination
18036    of group WHICH_GROUP.
18037
18038    If WHICH_GROUP == current_group, this function will return true if INSN
18039    causes the termination of the current group (i.e, the dispatch group to
18040    which INSN belongs). This means that INSN will be the last insn in the
18041    group it belongs to.
18042
18043    If WHICH_GROUP == previous_group, this function will return true if INSN
18044    causes the termination of the previous group (i.e, the dispatch group that
18045    precedes the group to which INSN belongs).  This means that INSN will be
18046    the first insn in the group it belongs to).  */
18047
18048 static bool
18049 insn_terminates_group_p (rtx_insn *insn, enum group_termination which_group)
18050 {
18051   bool first, last;
18052
18053   if (! insn)
18054     return false;
18055
18056   first = insn_must_be_first_in_group (insn);
18057   last = insn_must_be_last_in_group (insn);
18058
18059   if (first && last)
18060     return true;
18061
18062   if (which_group == current_group)
18063     return last;
18064   else if (which_group == previous_group)
18065     return first;
18066
18067   return false;
18068 }
18069
18070
18071 static bool
18072 insn_must_be_first_in_group (rtx_insn *insn)
18073 {
18074   enum attr_type type;
18075
18076   if (!insn
18077       || NOTE_P (insn)
18078       || DEBUG_INSN_P (insn)
18079       || GET_CODE (PATTERN (insn)) == USE
18080       || GET_CODE (PATTERN (insn)) == CLOBBER)
18081     return false;
18082
18083   switch (rs6000_tune)
18084     {
18085     case PROCESSOR_POWER5:
18086       if (is_cracked_insn (insn))
18087         return true;
18088       /* FALLTHRU */
18089     case PROCESSOR_POWER4:
18090       if (is_microcoded_insn (insn))
18091         return true;
18092
18093       if (!rs6000_sched_groups)
18094         return false;
18095
18096       type = get_attr_type (insn);
18097
18098       switch (type)
18099         {
18100         case TYPE_MFCR:
18101         case TYPE_MFCRF:
18102         case TYPE_MTCR:
18103         case TYPE_CR_LOGICAL:
18104         case TYPE_MTJMPR:
18105         case TYPE_MFJMPR:
18106         case TYPE_DIV:
18107         case TYPE_LOAD_L:
18108         case TYPE_STORE_C:
18109         case TYPE_ISYNC:
18110         case TYPE_SYNC:
18111           return true;
18112         default:
18113           break;
18114         }
18115       break;
18116     case PROCESSOR_POWER6:
18117       type = get_attr_type (insn);
18118
18119       switch (type)
18120         {
18121         case TYPE_EXTS:
18122         case TYPE_CNTLZ:
18123         case TYPE_TRAP:
18124         case TYPE_MUL:
18125         case TYPE_INSERT:
18126         case TYPE_FPCOMPARE:
18127         case TYPE_MFCR:
18128         case TYPE_MTCR:
18129         case TYPE_MFJMPR:
18130         case TYPE_MTJMPR:
18131         case TYPE_ISYNC:
18132         case TYPE_SYNC:
18133         case TYPE_LOAD_L:
18134         case TYPE_STORE_C:
18135           return true;
18136         case TYPE_SHIFT:
18137           if (get_attr_dot (insn) == DOT_NO
18138               || get_attr_var_shift (insn) == VAR_SHIFT_NO)
18139             return true;
18140           else
18141             break;
18142         case TYPE_DIV:
18143           if (get_attr_size (insn) == SIZE_32)
18144             return true;
18145           else
18146             break;
18147         case TYPE_LOAD:
18148         case TYPE_STORE:
18149         case TYPE_FPLOAD:
18150         case TYPE_FPSTORE:
18151           if (get_attr_update (insn) == UPDATE_YES)
18152             return true;
18153           else
18154             break;
18155         default:
18156           break;
18157         }
18158       break;
18159     case PROCESSOR_POWER7:
18160       type = get_attr_type (insn);
18161
18162       switch (type)
18163         {
18164         case TYPE_CR_LOGICAL:
18165         case TYPE_MFCR:
18166         case TYPE_MFCRF:
18167         case TYPE_MTCR:
18168         case TYPE_DIV:
18169         case TYPE_ISYNC:
18170         case TYPE_LOAD_L:
18171         case TYPE_STORE_C:
18172         case TYPE_MFJMPR:
18173         case TYPE_MTJMPR:
18174           return true;
18175         case TYPE_MUL:
18176         case TYPE_SHIFT:
18177         case TYPE_EXTS:
18178           if (get_attr_dot (insn) == DOT_YES)
18179             return true;
18180           else
18181             break;
18182         case TYPE_LOAD:
18183           if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
18184               || get_attr_update (insn) == UPDATE_YES)
18185             return true;
18186           else
18187             break;
18188         case TYPE_STORE:
18189         case TYPE_FPLOAD:
18190         case TYPE_FPSTORE:
18191           if (get_attr_update (insn) == UPDATE_YES)
18192             return true;
18193           else
18194             break;
18195         default:
18196           break;
18197         }
18198       break;
18199     case PROCESSOR_POWER8:
18200       type = get_attr_type (insn);
18201
18202       switch (type)
18203         {
18204         case TYPE_CR_LOGICAL:
18205         case TYPE_MFCR:
18206         case TYPE_MFCRF:
18207         case TYPE_MTCR:
18208         case TYPE_SYNC:
18209         case TYPE_ISYNC:
18210         case TYPE_LOAD_L:
18211         case TYPE_STORE_C:
18212         case TYPE_VECSTORE:
18213         case TYPE_MFJMPR:
18214         case TYPE_MTJMPR:
18215           return true;
18216         case TYPE_SHIFT:
18217         case TYPE_EXTS:
18218         case TYPE_MUL:
18219           if (get_attr_dot (insn) == DOT_YES)
18220             return true;
18221           else
18222             break;
18223         case TYPE_LOAD:
18224           if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
18225               || get_attr_update (insn) == UPDATE_YES)
18226             return true;
18227           else
18228             break;
18229         case TYPE_STORE:
18230           if (get_attr_update (insn) == UPDATE_YES
18231               && get_attr_indexed (insn) == INDEXED_YES)
18232             return true;
18233           else
18234             break;
18235         default:
18236           break;
18237         }
18238       break;
18239     default:
18240       break;
18241     }
18242
18243   return false;
18244 }
18245
18246 static bool
18247 insn_must_be_last_in_group (rtx_insn *insn)
18248 {
18249   enum attr_type type;
18250
18251   if (!insn
18252       || NOTE_P (insn)
18253       || DEBUG_INSN_P (insn)
18254       || GET_CODE (PATTERN (insn)) == USE
18255       || GET_CODE (PATTERN (insn)) == CLOBBER)
18256     return false;
18257
18258   switch (rs6000_tune) {
18259   case PROCESSOR_POWER4:
18260   case PROCESSOR_POWER5:
18261     if (is_microcoded_insn (insn))
18262       return true;
18263
18264     if (is_branch_slot_insn (insn))
18265       return true;
18266
18267     break;
18268   case PROCESSOR_POWER6:
18269     type = get_attr_type (insn);
18270
18271     switch (type)
18272       {
18273       case TYPE_EXTS:
18274       case TYPE_CNTLZ:
18275       case TYPE_TRAP:
18276       case TYPE_MUL:
18277       case TYPE_FPCOMPARE:
18278       case TYPE_MFCR:
18279       case TYPE_MTCR:
18280       case TYPE_MFJMPR:
18281       case TYPE_MTJMPR:
18282       case TYPE_ISYNC:
18283       case TYPE_SYNC:
18284       case TYPE_LOAD_L:
18285       case TYPE_STORE_C:
18286         return true;
18287       case TYPE_SHIFT:
18288         if (get_attr_dot (insn) == DOT_NO
18289             || get_attr_var_shift (insn) == VAR_SHIFT_NO)
18290           return true;
18291         else
18292           break;
18293       case TYPE_DIV:
18294         if (get_attr_size (insn) == SIZE_32)
18295           return true;
18296         else
18297           break;
18298       default:
18299         break;
18300     }
18301     break;
18302   case PROCESSOR_POWER7:
18303     type = get_attr_type (insn);
18304
18305     switch (type)
18306       {
18307       case TYPE_ISYNC:
18308       case TYPE_SYNC:
18309       case TYPE_LOAD_L:
18310       case TYPE_STORE_C:
18311         return true;
18312       case TYPE_LOAD:
18313         if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
18314             && get_attr_update (insn) == UPDATE_YES)
18315           return true;
18316         else
18317           break;
18318       case TYPE_STORE:
18319         if (get_attr_update (insn) == UPDATE_YES
18320             && get_attr_indexed (insn) == INDEXED_YES)
18321           return true;
18322         else
18323           break;
18324       default:
18325         break;
18326     }
18327     break;
18328   case PROCESSOR_POWER8:
18329     type = get_attr_type (insn);
18330
18331     switch (type)
18332       {
18333       case TYPE_MFCR:
18334       case TYPE_MTCR:
18335       case TYPE_ISYNC:
18336       case TYPE_SYNC:
18337       case TYPE_LOAD_L:
18338       case TYPE_STORE_C:
18339         return true;
18340       case TYPE_LOAD:
18341         if (get_attr_sign_extend (insn) == SIGN_EXTEND_YES
18342             && get_attr_update (insn) == UPDATE_YES)
18343           return true;
18344         else
18345           break;
18346       case TYPE_STORE:
18347         if (get_attr_update (insn) == UPDATE_YES
18348             && get_attr_indexed (insn) == INDEXED_YES)
18349           return true;
18350         else
18351           break;
18352       default:
18353         break;
18354     }
18355     break;
18356   default:
18357     break;
18358   }
18359
18360   return false;
18361 }
18362
18363 /* Return true if it is recommended to keep NEXT_INSN "far" (in a separate
18364    dispatch group) from the insns in GROUP_INSNS.  Return false otherwise.  */
18365
18366 static bool
18367 is_costly_group (rtx *group_insns, rtx next_insn)
18368 {
18369   int i;
18370   int issue_rate = rs6000_issue_rate ();
18371
18372   for (i = 0; i < issue_rate; i++)
18373     {
18374       sd_iterator_def sd_it;
18375       dep_t dep;
18376       rtx insn = group_insns[i];
18377
18378       if (!insn)
18379         continue;
18380
18381       FOR_EACH_DEP (insn, SD_LIST_RES_FORW, sd_it, dep)
18382         {
18383           rtx next = DEP_CON (dep);
18384
18385           if (next == next_insn
18386               && rs6000_is_costly_dependence (dep, dep_cost (dep), 0))
18387             return true;
18388         }
18389     }
18390
18391   return false;
18392 }
18393
18394 /* Utility of the function redefine_groups.
18395    Check if it is too costly to schedule NEXT_INSN together with GROUP_INSNS
18396    in the same dispatch group.  If so, insert nops before NEXT_INSN, in order
18397    to keep it "far" (in a separate group) from GROUP_INSNS, following
18398    one of the following schemes, depending on the value of the flag
18399    -minsert_sched_nops = X:
18400    (1) X == sched_finish_regroup_exact: insert exactly as many nops as needed
18401        in order to force NEXT_INSN into a separate group.
18402    (2) X < sched_finish_regroup_exact: insert exactly X nops.
18403    GROUP_END, CAN_ISSUE_MORE and GROUP_COUNT record the state after nop
18404    insertion (has a group just ended, how many vacant issue slots remain in the
18405    last group, and how many dispatch groups were encountered so far).  */
18406
18407 static int
18408 force_new_group (int sched_verbose, FILE *dump, rtx *group_insns,
18409                  rtx_insn *next_insn, bool *group_end, int can_issue_more,
18410                  int *group_count)
18411 {
18412   rtx nop;
18413   bool force;
18414   int issue_rate = rs6000_issue_rate ();
18415   bool end = *group_end;
18416   int i;
18417
18418   if (next_insn == NULL_RTX || DEBUG_INSN_P (next_insn))
18419     return can_issue_more;
18420
18421   if (rs6000_sched_insert_nops > sched_finish_regroup_exact)
18422     return can_issue_more;
18423
18424   force = is_costly_group (group_insns, next_insn);
18425   if (!force)
18426     return can_issue_more;
18427
18428   if (sched_verbose > 6)
18429     fprintf (dump,"force: group count = %d, can_issue_more = %d\n",
18430              *group_count ,can_issue_more);
18431
18432   if (rs6000_sched_insert_nops == sched_finish_regroup_exact)
18433     {
18434       if (*group_end)
18435         can_issue_more = 0;
18436
18437       /* Since only a branch can be issued in the last issue_slot, it is
18438          sufficient to insert 'can_issue_more - 1' nops if next_insn is not
18439          a branch. If next_insn is a branch, we insert 'can_issue_more' nops;
18440          in this case the last nop will start a new group and the branch
18441          will be forced to the new group.  */
18442       if (can_issue_more && !is_branch_slot_insn (next_insn))
18443         can_issue_more--;
18444
18445       /* Do we have a special group ending nop? */
18446       if (rs6000_tune == PROCESSOR_POWER6 || rs6000_tune == PROCESSOR_POWER7
18447           || rs6000_tune == PROCESSOR_POWER8)
18448         {
18449           nop = gen_group_ending_nop ();
18450           emit_insn_before (nop, next_insn);
18451           can_issue_more = 0;
18452         }
18453       else
18454         while (can_issue_more > 0)
18455           {
18456             nop = gen_nop ();
18457             emit_insn_before (nop, next_insn);
18458             can_issue_more--;
18459           }
18460
18461       *group_end = true;
18462       return 0;
18463     }
18464
18465   if (rs6000_sched_insert_nops < sched_finish_regroup_exact)
18466     {
18467       int n_nops = rs6000_sched_insert_nops;
18468
18469       /* Nops can't be issued from the branch slot, so the effective
18470          issue_rate for nops is 'issue_rate - 1'.  */
18471       if (can_issue_more == 0)
18472         can_issue_more = issue_rate;
18473       can_issue_more--;
18474       if (can_issue_more == 0)
18475         {
18476           can_issue_more = issue_rate - 1;
18477           (*group_count)++;
18478           end = true;
18479           for (i = 0; i < issue_rate; i++)
18480             {
18481               group_insns[i] = 0;
18482             }
18483         }
18484
18485       while (n_nops > 0)
18486         {
18487           nop = gen_nop ();
18488           emit_insn_before (nop, next_insn);
18489           if (can_issue_more == issue_rate - 1) /* new group begins */
18490             end = false;
18491           can_issue_more--;
18492           if (can_issue_more == 0)
18493             {
18494               can_issue_more = issue_rate - 1;
18495               (*group_count)++;
18496               end = true;
18497               for (i = 0; i < issue_rate; i++)
18498                 {
18499                   group_insns[i] = 0;
18500                 }
18501             }
18502           n_nops--;
18503         }
18504
18505       /* Scale back relative to 'issue_rate' (instead of 'issue_rate - 1').  */
18506       can_issue_more++;
18507
18508       /* Is next_insn going to start a new group?  */
18509       *group_end
18510         = (end
18511            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
18512            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
18513            || (can_issue_more < issue_rate &&
18514                insn_terminates_group_p (next_insn, previous_group)));
18515       if (*group_end && end)
18516         (*group_count)--;
18517
18518       if (sched_verbose > 6)
18519         fprintf (dump, "done force: group count = %d, can_issue_more = %d\n",
18520                  *group_count, can_issue_more);
18521       return can_issue_more;
18522     }
18523
18524   return can_issue_more;
18525 }
18526
18527 /* This function tries to synch the dispatch groups that the compiler "sees"
18528    with the dispatch groups that the processor dispatcher is expected to
18529    form in practice.  It tries to achieve this synchronization by forcing the
18530    estimated processor grouping on the compiler (as opposed to the function
18531    'pad_goups' which tries to force the scheduler's grouping on the processor).
18532
18533    The function scans the insn sequence between PREV_HEAD_INSN and TAIL and
18534    examines the (estimated) dispatch groups that will be formed by the processor
18535    dispatcher.  It marks these group boundaries to reflect the estimated
18536    processor grouping, overriding the grouping that the scheduler had marked.
18537    Depending on the value of the flag '-minsert-sched-nops' this function can
18538    force certain insns into separate groups or force a certain distance between
18539    them by inserting nops, for example, if there exists a "costly dependence"
18540    between the insns.
18541
18542    The function estimates the group boundaries that the processor will form as
18543    follows:  It keeps track of how many vacant issue slots are available after
18544    each insn.  A subsequent insn will start a new group if one of the following
18545    4 cases applies:
18546    - no more vacant issue slots remain in the current dispatch group.
18547    - only the last issue slot, which is the branch slot, is vacant, but the next
18548      insn is not a branch.
18549    - only the last 2 or less issue slots, including the branch slot, are vacant,
18550      which means that a cracked insn (which occupies two issue slots) can't be
18551      issued in this group.
18552    - less than 'issue_rate' slots are vacant, and the next insn always needs to
18553      start a new group.  */
18554
18555 static int
18556 redefine_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
18557                  rtx_insn *tail)
18558 {
18559   rtx_insn *insn, *next_insn;
18560   int issue_rate;
18561   int can_issue_more;
18562   int slot, i;
18563   bool group_end;
18564   int group_count = 0;
18565   rtx *group_insns;
18566
18567   /* Initialize.  */
18568   issue_rate = rs6000_issue_rate ();
18569   group_insns = XALLOCAVEC (rtx, issue_rate);
18570   for (i = 0; i < issue_rate; i++)
18571     {
18572       group_insns[i] = 0;
18573     }
18574   can_issue_more = issue_rate;
18575   slot = 0;
18576   insn = get_next_active_insn (prev_head_insn, tail);
18577   group_end = false;
18578
18579   while (insn != NULL_RTX)
18580     {
18581       slot = (issue_rate - can_issue_more);
18582       group_insns[slot] = insn;
18583       can_issue_more =
18584         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
18585       if (insn_terminates_group_p (insn, current_group))
18586         can_issue_more = 0;
18587
18588       next_insn = get_next_active_insn (insn, tail);
18589       if (next_insn == NULL_RTX)
18590         return group_count + 1;
18591
18592       /* Is next_insn going to start a new group?  */
18593       group_end
18594         = (can_issue_more == 0
18595            || (can_issue_more == 1 && !is_branch_slot_insn (next_insn))
18596            || (can_issue_more <= 2 && is_cracked_insn (next_insn))
18597            || (can_issue_more < issue_rate &&
18598                insn_terminates_group_p (next_insn, previous_group)));
18599
18600       can_issue_more = force_new_group (sched_verbose, dump, group_insns,
18601                                         next_insn, &group_end, can_issue_more,
18602                                         &group_count);
18603
18604       if (group_end)
18605         {
18606           group_count++;
18607           can_issue_more = 0;
18608           for (i = 0; i < issue_rate; i++)
18609             {
18610               group_insns[i] = 0;
18611             }
18612         }
18613
18614       if (GET_MODE (next_insn) == TImode && can_issue_more)
18615         PUT_MODE (next_insn, VOIDmode);
18616       else if (!can_issue_more && GET_MODE (next_insn) != TImode)
18617         PUT_MODE (next_insn, TImode);
18618
18619       insn = next_insn;
18620       if (can_issue_more == 0)
18621         can_issue_more = issue_rate;
18622     } /* while */
18623
18624   return group_count;
18625 }
18626
18627 /* Scan the insn sequence between PREV_HEAD_INSN and TAIL and examine the
18628    dispatch group boundaries that the scheduler had marked.  Pad with nops
18629    any dispatch groups which have vacant issue slots, in order to force the
18630    scheduler's grouping on the processor dispatcher.  The function
18631    returns the number of dispatch groups found.  */
18632
18633 static int
18634 pad_groups (FILE *dump, int sched_verbose, rtx_insn *prev_head_insn,
18635             rtx_insn *tail)
18636 {
18637   rtx_insn *insn, *next_insn;
18638   rtx nop;
18639   int issue_rate;
18640   int can_issue_more;
18641   int group_end;
18642   int group_count = 0;
18643
18644   /* Initialize issue_rate.  */
18645   issue_rate = rs6000_issue_rate ();
18646   can_issue_more = issue_rate;
18647
18648   insn = get_next_active_insn (prev_head_insn, tail);
18649   next_insn = get_next_active_insn (insn, tail);
18650
18651   while (insn != NULL_RTX)
18652     {
18653       can_issue_more =
18654         rs6000_variable_issue (dump, sched_verbose, insn, can_issue_more);
18655
18656       group_end = (next_insn == NULL_RTX || GET_MODE (next_insn) == TImode);
18657
18658       if (next_insn == NULL_RTX)
18659         break;
18660
18661       if (group_end)
18662         {
18663           /* If the scheduler had marked group termination at this location
18664              (between insn and next_insn), and neither insn nor next_insn will
18665              force group termination, pad the group with nops to force group
18666              termination.  */
18667           if (can_issue_more
18668               && (rs6000_sched_insert_nops == sched_finish_pad_groups)
18669               && !insn_terminates_group_p (insn, current_group)
18670               && !insn_terminates_group_p (next_insn, previous_group))
18671             {
18672               if (!is_branch_slot_insn (next_insn))
18673                 can_issue_more--;
18674
18675               while (can_issue_more)
18676                 {
18677                   nop = gen_nop ();
18678                   emit_insn_before (nop, next_insn);
18679                   can_issue_more--;
18680                 }
18681             }
18682
18683           can_issue_more = issue_rate;
18684           group_count++;
18685         }
18686
18687       insn = next_insn;
18688       next_insn = get_next_active_insn (insn, tail);
18689     }
18690
18691   return group_count;
18692 }
18693
18694 /* We're beginning a new block.  Initialize data structures as necessary.  */
18695
18696 static void
18697 rs6000_sched_init (FILE *dump ATTRIBUTE_UNUSED,
18698                      int sched_verbose ATTRIBUTE_UNUSED,
18699                      int max_ready ATTRIBUTE_UNUSED)
18700 {
18701   last_scheduled_insn = NULL;
18702   load_store_pendulum = 0;
18703   divide_cnt = 0;
18704   vec_pairing = 0;
18705 }
18706
18707 /* The following function is called at the end of scheduling BB.
18708    After reload, it inserts nops at insn group bundling.  */
18709
18710 static void
18711 rs6000_sched_finish (FILE *dump, int sched_verbose)
18712 {
18713   int n_groups;
18714
18715   if (sched_verbose)
18716     fprintf (dump, "=== Finishing schedule.\n");
18717
18718   if (reload_completed && rs6000_sched_groups)
18719     {
18720       /* Do not run sched_finish hook when selective scheduling enabled.  */
18721       if (sel_sched_p ())
18722         return;
18723
18724       if (rs6000_sched_insert_nops == sched_finish_none)
18725         return;
18726
18727       if (rs6000_sched_insert_nops == sched_finish_pad_groups)
18728         n_groups = pad_groups (dump, sched_verbose,
18729                                current_sched_info->prev_head,
18730                                current_sched_info->next_tail);
18731       else
18732         n_groups = redefine_groups (dump, sched_verbose,
18733                                     current_sched_info->prev_head,
18734                                     current_sched_info->next_tail);
18735
18736       if (sched_verbose >= 6)
18737         {
18738           fprintf (dump, "ngroups = %d\n", n_groups);
18739           print_rtl (dump, current_sched_info->prev_head);
18740           fprintf (dump, "Done finish_sched\n");
18741         }
18742     }
18743 }
18744
18745 struct rs6000_sched_context
18746 {
18747   short cached_can_issue_more;
18748   rtx_insn *last_scheduled_insn;
18749   int load_store_pendulum;
18750   int divide_cnt;
18751   int vec_pairing;
18752 };
18753
18754 typedef struct rs6000_sched_context rs6000_sched_context_def;
18755 typedef rs6000_sched_context_def *rs6000_sched_context_t;
18756
18757 /* Allocate store for new scheduling context.  */
18758 static void *
18759 rs6000_alloc_sched_context (void)
18760 {
18761   return xmalloc (sizeof (rs6000_sched_context_def));
18762 }
18763
18764 /* If CLEAN_P is true then initializes _SC with clean data,
18765    and from the global context otherwise.  */
18766 static void
18767 rs6000_init_sched_context (void *_sc, bool clean_p)
18768 {
18769   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
18770
18771   if (clean_p)
18772     {
18773       sc->cached_can_issue_more = 0;
18774       sc->last_scheduled_insn = NULL;
18775       sc->load_store_pendulum = 0;
18776       sc->divide_cnt = 0;
18777       sc->vec_pairing = 0;
18778     }
18779   else
18780     {
18781       sc->cached_can_issue_more = cached_can_issue_more;
18782       sc->last_scheduled_insn = last_scheduled_insn;
18783       sc->load_store_pendulum = load_store_pendulum;
18784       sc->divide_cnt = divide_cnt;
18785       sc->vec_pairing = vec_pairing;
18786     }
18787 }
18788
18789 /* Sets the global scheduling context to the one pointed to by _SC.  */
18790 static void
18791 rs6000_set_sched_context (void *_sc)
18792 {
18793   rs6000_sched_context_t sc = (rs6000_sched_context_t) _sc;
18794
18795   gcc_assert (sc != NULL);
18796
18797   cached_can_issue_more = sc->cached_can_issue_more;
18798   last_scheduled_insn = sc->last_scheduled_insn;
18799   load_store_pendulum = sc->load_store_pendulum;
18800   divide_cnt = sc->divide_cnt;
18801   vec_pairing = sc->vec_pairing;
18802 }
18803
18804 /* Free _SC.  */
18805 static void
18806 rs6000_free_sched_context (void *_sc)
18807 {
18808   gcc_assert (_sc != NULL);
18809
18810   free (_sc);
18811 }
18812
18813 static bool
18814 rs6000_sched_can_speculate_insn (rtx_insn *insn)
18815 {
18816   switch (get_attr_type (insn))
18817     {
18818     case TYPE_DIV:
18819     case TYPE_SDIV:
18820     case TYPE_DDIV:
18821     case TYPE_VECDIV:
18822     case TYPE_SSQRT:
18823     case TYPE_DSQRT:
18824       return false;
18825
18826     default:
18827       return true;
18828   }
18829 }
18830 \f
18831 /* Length in units of the trampoline for entering a nested function.  */
18832
18833 int
18834 rs6000_trampoline_size (void)
18835 {
18836   int ret = 0;
18837
18838   switch (DEFAULT_ABI)
18839     {
18840     default:
18841       gcc_unreachable ();
18842
18843     case ABI_AIX:
18844       ret = (TARGET_32BIT) ? 12 : 24;
18845       break;
18846
18847     case ABI_ELFv2:
18848       gcc_assert (!TARGET_32BIT);
18849       ret = 32;
18850       break;
18851
18852     case ABI_DARWIN:
18853     case ABI_V4:
18854       ret = (TARGET_32BIT) ? 40 : 48;
18855       break;
18856     }
18857
18858   return ret;
18859 }
18860
18861 /* Emit RTL insns to initialize the variable parts of a trampoline.
18862    FNADDR is an RTX for the address of the function's pure code.
18863    CXT is an RTX for the static chain value for the function.  */
18864
18865 static void
18866 rs6000_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
18867 {
18868   int regsize = (TARGET_32BIT) ? 4 : 8;
18869   rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
18870   rtx ctx_reg = force_reg (Pmode, cxt);
18871   rtx addr = force_reg (Pmode, XEXP (m_tramp, 0));
18872
18873   switch (DEFAULT_ABI)
18874     {
18875     default:
18876       gcc_unreachable ();
18877
18878     /* Under AIX, just build the 3 word function descriptor */
18879     case ABI_AIX:
18880       {
18881         rtx fnmem, fn_reg, toc_reg;
18882
18883         if (!TARGET_POINTERS_TO_NESTED_FUNCTIONS)
18884           error ("you cannot take the address of a nested function if you use "
18885                  "the %qs option", "-mno-pointers-to-nested-functions");
18886
18887         fnmem = gen_const_mem (Pmode, force_reg (Pmode, fnaddr));
18888         fn_reg = gen_reg_rtx (Pmode);
18889         toc_reg = gen_reg_rtx (Pmode);
18890
18891   /* Macro to shorten the code expansions below.  */
18892 # define MEM_PLUS(MEM, OFFSET) adjust_address (MEM, Pmode, OFFSET)
18893
18894         m_tramp = replace_equiv_address (m_tramp, addr);
18895
18896         emit_move_insn (fn_reg, MEM_PLUS (fnmem, 0));
18897         emit_move_insn (toc_reg, MEM_PLUS (fnmem, regsize));
18898         emit_move_insn (MEM_PLUS (m_tramp, 0), fn_reg);
18899         emit_move_insn (MEM_PLUS (m_tramp, regsize), toc_reg);
18900         emit_move_insn (MEM_PLUS (m_tramp, 2*regsize), ctx_reg);
18901
18902 # undef MEM_PLUS
18903       }
18904       break;
18905
18906     /* Under V.4/eabi/darwin, __trampoline_setup does the real work.  */
18907     case ABI_ELFv2:
18908     case ABI_DARWIN:
18909     case ABI_V4:
18910       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__trampoline_setup"),
18911                          LCT_NORMAL, VOIDmode,
18912                          addr, Pmode,
18913                          GEN_INT (rs6000_trampoline_size ()), SImode,
18914                          fnaddr, Pmode,
18915                          ctx_reg, Pmode);
18916       break;
18917     }
18918 }
18919
18920 \f
18921 /* Returns TRUE iff the target attribute indicated by ATTR_ID takes a plain
18922    identifier as an argument, so the front end shouldn't look it up.  */
18923
18924 static bool
18925 rs6000_attribute_takes_identifier_p (const_tree attr_id)
18926 {
18927   return is_attribute_p ("altivec", attr_id);
18928 }
18929
18930 /* Handle the "altivec" attribute.  The attribute may have
18931    arguments as follows:
18932
18933         __attribute__((altivec(vector__)))
18934         __attribute__((altivec(pixel__)))       (always followed by 'unsigned short')
18935         __attribute__((altivec(bool__)))        (always followed by 'unsigned')
18936
18937   and may appear more than once (e.g., 'vector bool char') in a
18938   given declaration.  */
18939
18940 static tree
18941 rs6000_handle_altivec_attribute (tree *node,
18942                                  tree name ATTRIBUTE_UNUSED,
18943                                  tree args,
18944                                  int flags ATTRIBUTE_UNUSED,
18945                                  bool *no_add_attrs)
18946 {
18947   tree type = *node, result = NULL_TREE;
18948   machine_mode mode;
18949   int unsigned_p;
18950   char altivec_type
18951     = ((args && TREE_CODE (args) == TREE_LIST && TREE_VALUE (args)
18952         && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
18953        ? *IDENTIFIER_POINTER (TREE_VALUE (args))
18954        : '?');
18955
18956   while (POINTER_TYPE_P (type)
18957          || TREE_CODE (type) == FUNCTION_TYPE
18958          || TREE_CODE (type) == METHOD_TYPE
18959          || TREE_CODE (type) == ARRAY_TYPE)
18960     type = TREE_TYPE (type);
18961
18962   mode = TYPE_MODE (type);
18963
18964   /* Check for invalid AltiVec type qualifiers.  */
18965   if (type == long_double_type_node)
18966     error ("use of %<long double%> in AltiVec types is invalid");
18967   else if (type == boolean_type_node)
18968     error ("use of boolean types in AltiVec types is invalid");
18969   else if (TREE_CODE (type) == COMPLEX_TYPE)
18970     error ("use of %<complex%> in AltiVec types is invalid");
18971   else if (DECIMAL_FLOAT_MODE_P (mode))
18972     error ("use of decimal floating point types in AltiVec types is invalid");
18973   else if (!TARGET_VSX)
18974     {
18975       if (type == long_unsigned_type_node || type == long_integer_type_node)
18976         {
18977           if (TARGET_64BIT)
18978             error ("use of %<long%> in AltiVec types is invalid for "
18979                    "64-bit code without %qs", "-mvsx");
18980           else if (rs6000_warn_altivec_long)
18981             warning (0, "use of %<long%> in AltiVec types is deprecated; "
18982                      "use %<int%>");
18983         }
18984       else if (type == long_long_unsigned_type_node
18985                || type == long_long_integer_type_node)
18986         error ("use of %<long long%> in AltiVec types is invalid without %qs",
18987                "-mvsx");
18988       else if (type == double_type_node)
18989         error ("use of %<double%> in AltiVec types is invalid without %qs",
18990                "-mvsx");
18991     }
18992
18993   switch (altivec_type)
18994     {
18995     case 'v':
18996       unsigned_p = TYPE_UNSIGNED (type);
18997       switch (mode)
18998         {
18999         case E_TImode:
19000           result = (unsigned_p ? unsigned_V1TI_type_node : V1TI_type_node);
19001           break;
19002         case E_DImode:
19003           result = (unsigned_p ? unsigned_V2DI_type_node : V2DI_type_node);
19004           break;
19005         case E_SImode:
19006           result = (unsigned_p ? unsigned_V4SI_type_node : V4SI_type_node);
19007           break;
19008         case E_HImode:
19009           result = (unsigned_p ? unsigned_V8HI_type_node : V8HI_type_node);
19010           break;
19011         case E_QImode:
19012           result = (unsigned_p ? unsigned_V16QI_type_node : V16QI_type_node);
19013           break;
19014         case E_SFmode: result = V4SF_type_node; break;
19015         case E_DFmode: result = V2DF_type_node; break;
19016           /* If the user says 'vector int bool', we may be handed the 'bool'
19017              attribute _before_ the 'vector' attribute, and so select the
19018              proper type in the 'b' case below.  */
19019         case E_V4SImode: case E_V8HImode: case E_V16QImode: case E_V4SFmode:
19020         case E_V2DImode: case E_V2DFmode:
19021           result = type;
19022         default: break;
19023         }
19024       break;
19025     case 'b':
19026       switch (mode)
19027         {
19028         case E_DImode: case E_V2DImode: result = bool_V2DI_type_node; break;
19029         case E_SImode: case E_V4SImode: result = bool_V4SI_type_node; break;
19030         case E_HImode: case E_V8HImode: result = bool_V8HI_type_node; break;
19031         case E_QImode: case E_V16QImode: result = bool_V16QI_type_node;
19032         default: break;
19033         }
19034       break;
19035     case 'p':
19036       switch (mode)
19037         {
19038         case E_V8HImode: result = pixel_V8HI_type_node;
19039         default: break;
19040         }
19041     default: break;
19042     }
19043
19044   /* Propagate qualifiers attached to the element type
19045      onto the vector type.  */
19046   if (result && result != type && TYPE_QUALS (type))
19047     result = build_qualified_type (result, TYPE_QUALS (type));
19048
19049   *no_add_attrs = true;  /* No need to hang on to the attribute.  */
19050
19051   if (result)
19052     *node = lang_hooks.types.reconstruct_complex_type (*node, result);
19053
19054   return NULL_TREE;
19055 }
19056
19057 /* AltiVec defines five built-in scalar types that serve as vector
19058    elements; we must teach the compiler how to mangle them.  The 128-bit
19059    floating point mangling is target-specific as well.  */
19060
19061 static const char *
19062 rs6000_mangle_type (const_tree type)
19063 {
19064   type = TYPE_MAIN_VARIANT (type);
19065
19066   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
19067       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
19068     return NULL;
19069
19070   if (type == bool_char_type_node) return "U6__boolc";
19071   if (type == bool_short_type_node) return "U6__bools";
19072   if (type == pixel_type_node) return "u7__pixel";
19073   if (type == bool_int_type_node) return "U6__booli";
19074   if (type == bool_long_long_type_node) return "U6__boolx";
19075
19076   if (SCALAR_FLOAT_TYPE_P (type) && FLOAT128_IBM_P (TYPE_MODE (type)))
19077     return "g";
19078   if (SCALAR_FLOAT_TYPE_P (type) && FLOAT128_IEEE_P (TYPE_MODE (type)))
19079     return ieee128_mangling_gcc_8_1 ? "U10__float128" : "u9__ieee128";
19080
19081   /* For all other types, use the default mangling.  */
19082   return NULL;
19083 }
19084
19085 /* Handle a "longcall" or "shortcall" attribute; arguments as in
19086    struct attribute_spec.handler.  */
19087
19088 static tree
19089 rs6000_handle_longcall_attribute (tree *node, tree name,
19090                                   tree args ATTRIBUTE_UNUSED,
19091                                   int flags ATTRIBUTE_UNUSED,
19092                                   bool *no_add_attrs)
19093 {
19094   if (TREE_CODE (*node) != FUNCTION_TYPE
19095       && TREE_CODE (*node) != FIELD_DECL
19096       && TREE_CODE (*node) != TYPE_DECL)
19097     {
19098       warning (OPT_Wattributes, "%qE attribute only applies to functions",
19099                name);
19100       *no_add_attrs = true;
19101     }
19102
19103   return NULL_TREE;
19104 }
19105
19106 /* Set longcall attributes on all functions declared when
19107    rs6000_default_long_calls is true.  */
19108 static void
19109 rs6000_set_default_type_attributes (tree type)
19110 {
19111   if (rs6000_default_long_calls
19112       && (TREE_CODE (type) == FUNCTION_TYPE
19113           || TREE_CODE (type) == METHOD_TYPE))
19114     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("longcall"),
19115                                         NULL_TREE,
19116                                         TYPE_ATTRIBUTES (type));
19117
19118 #if TARGET_MACHO
19119   darwin_set_default_type_attributes (type);
19120 #endif
19121 }
19122
19123 /* Return a reference suitable for calling a function with the
19124    longcall attribute.  */
19125
19126 static rtx
19127 rs6000_longcall_ref (rtx call_ref, rtx arg)
19128 {
19129   /* System V adds '.' to the internal name, so skip them.  */
19130   const char *call_name = XSTR (call_ref, 0);
19131   if (*call_name == '.')
19132     {
19133       while (*call_name == '.')
19134         call_name++;
19135
19136       tree node = get_identifier (call_name);
19137       call_ref = gen_rtx_SYMBOL_REF (VOIDmode, IDENTIFIER_POINTER (node));
19138     }
19139
19140   if (TARGET_PLTSEQ)
19141     {
19142       rtx base = const0_rtx;
19143       int regno = 12;
19144       if (rs6000_pcrel_p (cfun))
19145         {
19146           rtx reg = gen_rtx_REG (Pmode, regno);
19147           rtx u = gen_rtx_UNSPEC (Pmode, gen_rtvec (3, base, call_ref, arg),
19148                                   UNSPEC_PLT_PCREL);
19149           emit_insn (gen_rtx_SET (reg, u));
19150           return reg;
19151         }
19152
19153       if (DEFAULT_ABI == ABI_ELFv2)
19154         base = gen_rtx_REG (Pmode, TOC_REGISTER);
19155       else
19156         {
19157           if (flag_pic)
19158             base = gen_rtx_REG (Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
19159           regno = 11;
19160         }
19161       /* Reg must match that used by linker PLT stubs.  For ELFv2, r12
19162          may be used by a function global entry point.  For SysV4, r11
19163          is used by __glink_PLTresolve lazy resolver entry.  */
19164       rtx reg = gen_rtx_REG (Pmode, regno);
19165       rtx hi = gen_rtx_UNSPEC (Pmode, gen_rtvec (3, base, call_ref, arg),
19166                                UNSPEC_PLT16_HA);
19167       rtx lo = gen_rtx_UNSPEC (Pmode, gen_rtvec (3, reg, call_ref, arg),
19168                                UNSPEC_PLT16_LO);
19169       emit_insn (gen_rtx_SET (reg, hi));
19170       emit_insn (gen_rtx_SET (reg, lo));
19171       return reg;
19172     }
19173
19174   return force_reg (Pmode, call_ref);
19175 }
19176 \f
19177 #ifndef TARGET_USE_MS_BITFIELD_LAYOUT
19178 #define TARGET_USE_MS_BITFIELD_LAYOUT 0
19179 #endif
19180
19181 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
19182    struct attribute_spec.handler.  */
19183 static tree
19184 rs6000_handle_struct_attribute (tree *node, tree name,
19185                                 tree args ATTRIBUTE_UNUSED,
19186                                 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
19187 {
19188   tree *type = NULL;
19189   if (DECL_P (*node))
19190     {
19191       if (TREE_CODE (*node) == TYPE_DECL)
19192         type = &TREE_TYPE (*node);
19193     }
19194   else
19195     type = node;
19196
19197   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
19198                  || TREE_CODE (*type) == UNION_TYPE)))
19199     {
19200       warning (OPT_Wattributes, "%qE attribute ignored", name);
19201       *no_add_attrs = true;
19202     }
19203
19204   else if ((is_attribute_p ("ms_struct", name)
19205             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
19206            || ((is_attribute_p ("gcc_struct", name)
19207                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
19208     {
19209       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
19210                name);
19211       *no_add_attrs = true;
19212     }
19213
19214   return NULL_TREE;
19215 }
19216
19217 static bool
19218 rs6000_ms_bitfield_layout_p (const_tree record_type)
19219 {
19220   return (TARGET_USE_MS_BITFIELD_LAYOUT &&
19221           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
19222     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
19223 }
19224 \f
19225 #ifdef USING_ELFOS_H
19226
19227 /* A get_unnamed_section callback, used for switching to toc_section.  */
19228
19229 static void
19230 rs6000_elf_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
19231 {
19232   if ((DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
19233       && TARGET_MINIMAL_TOC)
19234     {
19235       if (!toc_initialized)
19236         {
19237           fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
19238           ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
19239           (*targetm.asm_out.internal_label) (asm_out_file, "LCTOC", 0);
19240           fprintf (asm_out_file, "\t.tc ");
19241           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1[TC],");
19242           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
19243           fprintf (asm_out_file, "\n");
19244
19245           fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
19246           ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
19247           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
19248           fprintf (asm_out_file, " = .+32768\n");
19249           toc_initialized = 1;
19250         }
19251       else
19252         fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
19253     }
19254   else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
19255     {
19256       fprintf (asm_out_file, "%s\n", TOC_SECTION_ASM_OP);
19257       if (!toc_initialized)
19258         {
19259           ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
19260           toc_initialized = 1;
19261         }
19262     }
19263   else
19264     {
19265       fprintf (asm_out_file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
19266       if (!toc_initialized)
19267         {
19268           ASM_OUTPUT_ALIGN (asm_out_file, TARGET_64BIT ? 3 : 2);
19269           ASM_OUTPUT_INTERNAL_LABEL_PREFIX (asm_out_file, "LCTOC1");
19270           fprintf (asm_out_file, " = .+32768\n");
19271           toc_initialized = 1;
19272         }
19273     }
19274 }
19275
19276 /* Implement TARGET_ASM_INIT_SECTIONS.  */
19277
19278 static void
19279 rs6000_elf_asm_init_sections (void)
19280 {
19281   toc_section
19282     = get_unnamed_section (0, rs6000_elf_output_toc_section_asm_op, NULL);
19283
19284   sdata2_section
19285     = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
19286                            SDATA2_SECTION_ASM_OP);
19287 }
19288
19289 /* Implement TARGET_SELECT_RTX_SECTION.  */
19290
19291 static section *
19292 rs6000_elf_select_rtx_section (machine_mode mode, rtx x,
19293                                unsigned HOST_WIDE_INT align)
19294 {
19295   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
19296     return toc_section;
19297   else
19298     return default_elf_select_rtx_section (mode, x, align);
19299 }
19300 \f
19301 /* For a SYMBOL_REF, set generic flags and then perform some
19302    target-specific processing.
19303
19304    When the AIX ABI is requested on a non-AIX system, replace the
19305    function name with the real name (with a leading .) rather than the
19306    function descriptor name.  This saves a lot of overriding code to
19307    read the prefixes.  */
19308
19309 static void rs6000_elf_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
19310 static void
19311 rs6000_elf_encode_section_info (tree decl, rtx rtl, int first)
19312 {
19313   default_encode_section_info (decl, rtl, first);
19314
19315   if (first
19316       && TREE_CODE (decl) == FUNCTION_DECL
19317       && !TARGET_AIX
19318       && DEFAULT_ABI == ABI_AIX)
19319     {
19320       rtx sym_ref = XEXP (rtl, 0);
19321       size_t len = strlen (XSTR (sym_ref, 0));
19322       char *str = XALLOCAVEC (char, len + 2);
19323       str[0] = '.';
19324       memcpy (str + 1, XSTR (sym_ref, 0), len + 1);
19325       XSTR (sym_ref, 0) = ggc_alloc_string (str, len + 1);
19326     }
19327 }
19328
19329 static inline bool
19330 compare_section_name (const char *section, const char *templ)
19331 {
19332   int len;
19333
19334   len = strlen (templ);
19335   return (strncmp (section, templ, len) == 0
19336           && (section[len] == 0 || section[len] == '.'));
19337 }
19338
19339 bool
19340 rs6000_elf_in_small_data_p (const_tree decl)
19341 {
19342   if (rs6000_sdata == SDATA_NONE)
19343     return false;
19344
19345   /* We want to merge strings, so we never consider them small data.  */
19346   if (TREE_CODE (decl) == STRING_CST)
19347     return false;
19348
19349   /* Functions are never in the small data area.  */
19350   if (TREE_CODE (decl) == FUNCTION_DECL)
19351     return false;
19352
19353   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl))
19354     {
19355       const char *section = DECL_SECTION_NAME (decl);
19356       if (compare_section_name (section, ".sdata")
19357           || compare_section_name (section, ".sdata2")
19358           || compare_section_name (section, ".gnu.linkonce.s")
19359           || compare_section_name (section, ".sbss")
19360           || compare_section_name (section, ".sbss2")
19361           || compare_section_name (section, ".gnu.linkonce.sb")
19362           || strcmp (section, ".PPC.EMB.sdata0") == 0
19363           || strcmp (section, ".PPC.EMB.sbss0") == 0)
19364         return true;
19365     }
19366   else
19367     {
19368       /* If we are told not to put readonly data in sdata, then don't.  */
19369       if (TREE_READONLY (decl) && rs6000_sdata != SDATA_EABI
19370           && !rs6000_readonly_in_sdata)
19371         return false;
19372
19373       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (decl));
19374
19375       if (size > 0
19376           && size <= g_switch_value
19377           /* If it's not public, and we're not going to reference it there,
19378              there's no need to put it in the small data section.  */
19379           && (rs6000_sdata != SDATA_DATA || TREE_PUBLIC (decl)))
19380         return true;
19381     }
19382
19383   return false;
19384 }
19385
19386 #endif /* USING_ELFOS_H */
19387 \f
19388 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  */
19389
19390 static bool
19391 rs6000_use_blocks_for_constant_p (machine_mode mode, const_rtx x)
19392 {
19393   return !ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode);
19394 }
19395
19396 /* Do not place thread-local symbols refs in the object blocks.  */
19397
19398 static bool
19399 rs6000_use_blocks_for_decl_p (const_tree decl)
19400 {
19401   return !DECL_THREAD_LOCAL_P (decl);
19402 }
19403 \f
19404 /* Return a REG that occurs in ADDR with coefficient 1.
19405    ADDR can be effectively incremented by incrementing REG.
19406
19407    r0 is special and we must not select it as an address
19408    register by this routine since our caller will try to
19409    increment the returned register via an "la" instruction.  */
19410
19411 rtx
19412 find_addr_reg (rtx addr)
19413 {
19414   while (GET_CODE (addr) == PLUS)
19415     {
19416       if (REG_P (XEXP (addr, 0))
19417           && REGNO (XEXP (addr, 0)) != 0)
19418         addr = XEXP (addr, 0);
19419       else if (REG_P (XEXP (addr, 1))
19420                && REGNO (XEXP (addr, 1)) != 0)
19421         addr = XEXP (addr, 1);
19422       else if (CONSTANT_P (XEXP (addr, 0)))
19423         addr = XEXP (addr, 1);
19424       else if (CONSTANT_P (XEXP (addr, 1)))
19425         addr = XEXP (addr, 0);
19426       else
19427         gcc_unreachable ();
19428     }
19429   gcc_assert (REG_P (addr) && REGNO (addr) != 0);
19430   return addr;
19431 }
19432
19433 void
19434 rs6000_fatal_bad_address (rtx op)
19435 {
19436   fatal_insn ("bad address", op);
19437 }
19438
19439 #if TARGET_MACHO
19440
19441 vec<branch_island, va_gc> *branch_islands;
19442
19443 /* Remember to generate a branch island for far calls to the given
19444    function.  */
19445
19446 static void
19447 add_compiler_branch_island (tree label_name, tree function_name,
19448                             int line_number)
19449 {
19450   branch_island bi = {function_name, label_name, line_number};
19451   vec_safe_push (branch_islands, bi);
19452 }
19453
19454 /* NO_PREVIOUS_DEF checks in the link list whether the function name is
19455    already there or not.  */
19456
19457 static int
19458 no_previous_def (tree function_name)
19459 {
19460   branch_island *bi;
19461   unsigned ix;
19462
19463   FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
19464     if (function_name == bi->function_name)
19465       return 0;
19466   return 1;
19467 }
19468
19469 /* GET_PREV_LABEL gets the label name from the previous definition of
19470    the function.  */
19471
19472 static tree
19473 get_prev_label (tree function_name)
19474 {
19475   branch_island *bi;
19476   unsigned ix;
19477
19478   FOR_EACH_VEC_SAFE_ELT (branch_islands, ix, bi)
19479     if (function_name == bi->function_name)
19480       return bi->label_name;
19481   return NULL_TREE;
19482 }
19483
19484 /* Generate external symbol indirection stubs (PIC and non-PIC).  */
19485
19486 void
19487 machopic_output_stub (FILE *file, const char *symb, const char *stub)
19488 {
19489   unsigned int length;
19490   char *symbol_name, *lazy_ptr_name;
19491   char *local_label_0;
19492   static unsigned label = 0;
19493
19494   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
19495   symb = (*targetm.strip_name_encoding) (symb);
19496
19497
19498   length = strlen (symb);
19499   symbol_name = XALLOCAVEC (char, length + 32);
19500   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
19501
19502   lazy_ptr_name = XALLOCAVEC (char, length + 32);
19503   GEN_LAZY_PTR_NAME_FOR_SYMBOL (lazy_ptr_name, symb, length);
19504
19505   if (flag_pic == 2)
19506     switch_to_section (darwin_sections[machopic_picsymbol_stub1_section]);
19507   else
19508     switch_to_section (darwin_sections[machopic_symbol_stub1_section]);
19509
19510   if (flag_pic == 2)
19511     {
19512       fprintf (file, "\t.align 5\n");
19513
19514       fprintf (file, "%s:\n", stub);
19515       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19516
19517       label++;
19518       local_label_0 = XALLOCAVEC (char, 16);
19519       sprintf (local_label_0, "L%u$spb", label);
19520
19521       fprintf (file, "\tmflr r0\n");
19522       if (TARGET_LINK_STACK)
19523         {
19524           char name[32];
19525           get_ppc476_thunk_name (name);
19526           fprintf (file, "\tbl %s\n", name);
19527           fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
19528         }
19529       else
19530         {
19531           fprintf (file, "\tbcl 20,31,%s\n", local_label_0);
19532           fprintf (file, "%s:\n\tmflr r11\n", local_label_0);
19533         }
19534       fprintf (file, "\taddis r11,r11,ha16(%s-%s)\n",
19535                lazy_ptr_name, local_label_0);
19536       fprintf (file, "\tmtlr r0\n");
19537       fprintf (file, "\t%s r12,lo16(%s-%s)(r11)\n",
19538                (TARGET_64BIT ? "ldu" : "lwzu"),
19539                lazy_ptr_name, local_label_0);
19540       fprintf (file, "\tmtctr r12\n");
19541       fprintf (file, "\tbctr\n");
19542     }
19543   else
19544     {
19545       fprintf (file, "\t.align 4\n");
19546
19547       fprintf (file, "%s:\n", stub);
19548       fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19549
19550       fprintf (file, "\tlis r11,ha16(%s)\n", lazy_ptr_name);
19551       fprintf (file, "\t%s r12,lo16(%s)(r11)\n",
19552                (TARGET_64BIT ? "ldu" : "lwzu"),
19553                lazy_ptr_name);
19554       fprintf (file, "\tmtctr r12\n");
19555       fprintf (file, "\tbctr\n");
19556     }
19557
19558   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
19559   fprintf (file, "%s:\n", lazy_ptr_name);
19560   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19561   fprintf (file, "%sdyld_stub_binding_helper\n",
19562            (TARGET_64BIT ? DOUBLE_INT_ASM_OP : "\t.long\t"));
19563 }
19564
19565 /* Legitimize PIC addresses.  If the address is already
19566    position-independent, we return ORIG.  Newly generated
19567    position-independent addresses go into a reg.  This is REG if non
19568    zero, otherwise we allocate register(s) as necessary.  */
19569
19570 #define SMALL_INT(X) ((UINTVAL (X) + 0x8000) < 0x10000)
19571
19572 rtx
19573 rs6000_machopic_legitimize_pic_address (rtx orig, machine_mode mode,
19574                                         rtx reg)
19575 {
19576   rtx base, offset;
19577
19578   if (reg == NULL && !reload_completed)
19579     reg = gen_reg_rtx (Pmode);
19580
19581   if (GET_CODE (orig) == CONST)
19582     {
19583       rtx reg_temp;
19584
19585       if (GET_CODE (XEXP (orig, 0)) == PLUS
19586           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
19587         return orig;
19588
19589       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
19590
19591       /* Use a different reg for the intermediate value, as
19592          it will be marked UNCHANGING.  */
19593       reg_temp = !can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode);
19594       base = rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 0),
19595                                                      Pmode, reg_temp);
19596       offset =
19597         rs6000_machopic_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
19598                                                 Pmode, reg);
19599
19600       if (CONST_INT_P (offset))
19601         {
19602           if (SMALL_INT (offset))
19603             return plus_constant (Pmode, base, INTVAL (offset));
19604           else if (!reload_completed)
19605             offset = force_reg (Pmode, offset);
19606           else
19607             {
19608               rtx mem = force_const_mem (Pmode, orig);
19609               return machopic_legitimize_pic_address (mem, Pmode, reg);
19610             }
19611         }
19612       return gen_rtx_PLUS (Pmode, base, offset);
19613     }
19614
19615   /* Fall back on generic machopic code.  */
19616   return machopic_legitimize_pic_address (orig, mode, reg);
19617 }
19618
19619 /* Output a .machine directive for the Darwin assembler, and call
19620    the generic start_file routine.  */
19621
19622 static void
19623 rs6000_darwin_file_start (void)
19624 {
19625   static const struct
19626   {
19627     const char *arg;
19628     const char *name;
19629     HOST_WIDE_INT if_set;
19630   } mapping[] = {
19631     { "ppc64", "ppc64", MASK_64BIT },
19632     { "970", "ppc970", MASK_PPC_GPOPT | MASK_MFCRF | MASK_POWERPC64 },
19633     { "power4", "ppc970", 0 },
19634     { "G5", "ppc970", 0 },
19635     { "7450", "ppc7450", 0 },
19636     { "7400", "ppc7400", MASK_ALTIVEC },
19637     { "G4", "ppc7400", 0 },
19638     { "750", "ppc750", 0 },
19639     { "740", "ppc750", 0 },
19640     { "G3", "ppc750", 0 },
19641     { "604e", "ppc604e", 0 },
19642     { "604", "ppc604", 0 },
19643     { "603e", "ppc603", 0 },
19644     { "603", "ppc603", 0 },
19645     { "601", "ppc601", 0 },
19646     { NULL, "ppc", 0 } };
19647   const char *cpu_id = "";
19648   size_t i;
19649
19650   rs6000_file_start ();
19651   darwin_file_start ();
19652
19653   /* Determine the argument to -mcpu=.  Default to G3 if not specified.  */
19654   
19655   if (rs6000_default_cpu != 0 && rs6000_default_cpu[0] != '\0')
19656     cpu_id = rs6000_default_cpu;
19657
19658   if (global_options_set.x_rs6000_cpu_index)
19659     cpu_id = processor_target_table[rs6000_cpu_index].name;
19660
19661   /* Look through the mapping array.  Pick the first name that either
19662      matches the argument, has a bit set in IF_SET that is also set
19663      in the target flags, or has a NULL name.  */
19664
19665   i = 0;
19666   while (mapping[i].arg != NULL
19667          && strcmp (mapping[i].arg, cpu_id) != 0
19668          && (mapping[i].if_set & rs6000_isa_flags) == 0)
19669     i++;
19670
19671   fprintf (asm_out_file, "\t.machine %s\n", mapping[i].name);
19672 }
19673
19674 #endif /* TARGET_MACHO */
19675
19676 #if TARGET_ELF
19677 static int
19678 rs6000_elf_reloc_rw_mask (void)
19679 {
19680   if (flag_pic)
19681     return 3;
19682   else if (DEFAULT_ABI == ABI_AIX || DEFAULT_ABI == ABI_ELFv2)
19683     return 2;
19684   else
19685     return 0;
19686 }
19687
19688 /* Record an element in the table of global constructors.  SYMBOL is
19689    a SYMBOL_REF of the function to be called; PRIORITY is a number
19690    between 0 and MAX_INIT_PRIORITY.
19691
19692    This differs from default_named_section_asm_out_constructor in
19693    that we have special handling for -mrelocatable.  */
19694
19695 static void rs6000_elf_asm_out_constructor (rtx, int) ATTRIBUTE_UNUSED;
19696 static void
19697 rs6000_elf_asm_out_constructor (rtx symbol, int priority)
19698 {
19699   const char *section = ".ctors";
19700   char buf[18];
19701
19702   if (priority != DEFAULT_INIT_PRIORITY)
19703     {
19704       sprintf (buf, ".ctors.%.5u",
19705                /* Invert the numbering so the linker puts us in the proper
19706                   order; constructors are run from right to left, and the
19707                   linker sorts in increasing order.  */
19708                MAX_INIT_PRIORITY - priority);
19709       section = buf;
19710     }
19711
19712   switch_to_section (get_section (section, SECTION_WRITE, NULL));
19713   assemble_align (POINTER_SIZE);
19714
19715   if (DEFAULT_ABI == ABI_V4
19716       && (TARGET_RELOCATABLE || flag_pic > 1))
19717     {
19718       fputs ("\t.long (", asm_out_file);
19719       output_addr_const (asm_out_file, symbol);
19720       fputs (")@fixup\n", asm_out_file);
19721     }
19722   else
19723     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
19724 }
19725
19726 static void rs6000_elf_asm_out_destructor (rtx, int) ATTRIBUTE_UNUSED;
19727 static void
19728 rs6000_elf_asm_out_destructor (rtx symbol, int priority)
19729 {
19730   const char *section = ".dtors";
19731   char buf[18];
19732
19733   if (priority != DEFAULT_INIT_PRIORITY)
19734     {
19735       sprintf (buf, ".dtors.%.5u",
19736                /* Invert the numbering so the linker puts us in the proper
19737                   order; constructors are run from right to left, and the
19738                   linker sorts in increasing order.  */
19739                MAX_INIT_PRIORITY - priority);
19740       section = buf;
19741     }
19742
19743   switch_to_section (get_section (section, SECTION_WRITE, NULL));
19744   assemble_align (POINTER_SIZE);
19745
19746   if (DEFAULT_ABI == ABI_V4
19747       && (TARGET_RELOCATABLE || flag_pic > 1))
19748     {
19749       fputs ("\t.long (", asm_out_file);
19750       output_addr_const (asm_out_file, symbol);
19751       fputs (")@fixup\n", asm_out_file);
19752     }
19753   else
19754     assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
19755 }
19756
19757 void
19758 rs6000_elf_declare_function_name (FILE *file, const char *name, tree decl)
19759 {
19760   if (TARGET_64BIT && DEFAULT_ABI != ABI_ELFv2)
19761     {
19762       fputs ("\t.section\t\".opd\",\"aw\"\n\t.align 3\n", file);
19763       ASM_OUTPUT_LABEL (file, name);
19764       fputs (DOUBLE_INT_ASM_OP, file);
19765       rs6000_output_function_entry (file, name);
19766       fputs (",.TOC.@tocbase,0\n\t.previous\n", file);
19767       if (DOT_SYMBOLS)
19768         {
19769           fputs ("\t.size\t", file);
19770           assemble_name (file, name);
19771           fputs (",24\n\t.type\t.", file);
19772           assemble_name (file, name);
19773           fputs (",@function\n", file);
19774           if (TREE_PUBLIC (decl) && ! DECL_WEAK (decl))
19775             {
19776               fputs ("\t.globl\t.", file);
19777               assemble_name (file, name);
19778               putc ('\n', file);
19779             }
19780         }
19781       else
19782         ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
19783       ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
19784       rs6000_output_function_entry (file, name);
19785       fputs (":\n", file);
19786       return;
19787     }
19788
19789   int uses_toc;
19790   if (DEFAULT_ABI == ABI_V4
19791       && (TARGET_RELOCATABLE || flag_pic > 1)
19792       && !TARGET_SECURE_PLT
19793       && (!constant_pool_empty_p () || crtl->profile)
19794       && (uses_toc = uses_TOC ()))
19795     {
19796       char buf[256];
19797
19798       if (uses_toc == 2)
19799         switch_to_other_text_partition ();
19800       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
19801
19802       fprintf (file, "\t.long ");
19803       assemble_name (file, toc_label_name);
19804       need_toc_init = 1;
19805       putc ('-', file);
19806       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
19807       assemble_name (file, buf);
19808       putc ('\n', file);
19809       if (uses_toc == 2)
19810         switch_to_other_text_partition ();
19811     }
19812
19813   ASM_OUTPUT_TYPE_DIRECTIVE (file, name, "function");
19814   ASM_DECLARE_RESULT (file, DECL_RESULT (decl));
19815
19816   if (TARGET_CMODEL == CMODEL_LARGE
19817       && rs6000_global_entry_point_prologue_needed_p ())
19818     {
19819       char buf[256];
19820
19821       (*targetm.asm_out.internal_label) (file, "LCL", rs6000_pic_labelno);
19822
19823       fprintf (file, "\t.quad .TOC.-");
19824       ASM_GENERATE_INTERNAL_LABEL (buf, "LCF", rs6000_pic_labelno);
19825       assemble_name (file, buf);
19826       putc ('\n', file);
19827     }
19828
19829   if (DEFAULT_ABI == ABI_AIX)
19830     {
19831       const char *desc_name, *orig_name;
19832
19833       orig_name = (*targetm.strip_name_encoding) (name);
19834       desc_name = orig_name;
19835       while (*desc_name == '.')
19836         desc_name++;
19837
19838       if (TREE_PUBLIC (decl))
19839         fprintf (file, "\t.globl %s\n", desc_name);
19840
19841       fprintf (file, "%s\n", MINIMAL_TOC_SECTION_ASM_OP);
19842       fprintf (file, "%s:\n", desc_name);
19843       fprintf (file, "\t.long %s\n", orig_name);
19844       fputs ("\t.long _GLOBAL_OFFSET_TABLE_\n", file);
19845       fputs ("\t.long 0\n", file);
19846       fprintf (file, "\t.previous\n");
19847     }
19848   ASM_OUTPUT_LABEL (file, name);
19849 }
19850
19851 static void rs6000_elf_file_end (void) ATTRIBUTE_UNUSED;
19852 static void
19853 rs6000_elf_file_end (void)
19854 {
19855 #ifdef HAVE_AS_GNU_ATTRIBUTE
19856   /* ??? The value emitted depends on options active at file end.
19857      Assume anyone using #pragma or attributes that might change
19858      options knows what they are doing.  */
19859   if ((TARGET_64BIT || DEFAULT_ABI == ABI_V4)
19860       && rs6000_passes_float)
19861     {
19862       int fp;
19863
19864       if (TARGET_HARD_FLOAT)
19865         fp = 1;
19866       else
19867         fp = 2;
19868       if (rs6000_passes_long_double)
19869         {
19870           if (!TARGET_LONG_DOUBLE_128)
19871             fp |= 2 * 4;
19872           else if (TARGET_IEEEQUAD)
19873             fp |= 3 * 4;
19874           else
19875             fp |= 1 * 4;
19876         }
19877       fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", fp);
19878     }
19879   if (TARGET_32BIT && DEFAULT_ABI == ABI_V4)
19880     {
19881       if (rs6000_passes_vector)
19882         fprintf (asm_out_file, "\t.gnu_attribute 8, %d\n",
19883                  (TARGET_ALTIVEC_ABI ? 2 : 1));
19884       if (rs6000_returns_struct)
19885         fprintf (asm_out_file, "\t.gnu_attribute 12, %d\n",
19886                  aix_struct_return ? 2 : 1);
19887     }
19888 #endif
19889 #if defined (POWERPC_LINUX) || defined (POWERPC_FREEBSD)
19890   if (TARGET_32BIT || DEFAULT_ABI == ABI_ELFv2)
19891     file_end_indicate_exec_stack ();
19892 #endif
19893
19894   if (flag_split_stack)
19895     file_end_indicate_split_stack ();
19896
19897   if (cpu_builtin_p)
19898     {
19899       /* We have expanded a CPU builtin, so we need to emit a reference to
19900          the special symbol that LIBC uses to declare it supports the
19901          AT_PLATFORM and AT_HWCAP/AT_HWCAP2 in the TCB feature.  */
19902       switch_to_section (data_section);
19903       fprintf (asm_out_file, "\t.align %u\n", TARGET_32BIT ? 2 : 3);
19904       fprintf (asm_out_file, "\t%s %s\n",
19905                TARGET_32BIT ? ".long" : ".quad", tcb_verification_symbol);
19906     }
19907 }
19908 #endif
19909
19910 #if TARGET_XCOFF
19911
19912 #ifndef HAVE_XCOFF_DWARF_EXTRAS
19913 #define HAVE_XCOFF_DWARF_EXTRAS 0
19914 #endif
19915
19916 static enum unwind_info_type
19917 rs6000_xcoff_debug_unwind_info (void)
19918 {
19919   return UI_NONE;
19920 }
19921
19922 static void
19923 rs6000_xcoff_asm_output_anchor (rtx symbol)
19924 {
19925   char buffer[100];
19926
19927   sprintf (buffer, "$ + " HOST_WIDE_INT_PRINT_DEC,
19928            SYMBOL_REF_BLOCK_OFFSET (symbol));
19929   fprintf (asm_out_file, "%s", SET_ASM_OP);
19930   RS6000_OUTPUT_BASENAME (asm_out_file, XSTR (symbol, 0));
19931   fprintf (asm_out_file, ",");
19932   RS6000_OUTPUT_BASENAME (asm_out_file, buffer);
19933   fprintf (asm_out_file, "\n");
19934 }
19935
19936 static void
19937 rs6000_xcoff_asm_globalize_label (FILE *stream, const char *name)
19938 {
19939   fputs (GLOBAL_ASM_OP, stream);
19940   RS6000_OUTPUT_BASENAME (stream, name);
19941   putc ('\n', stream);
19942 }
19943
19944 /* A get_unnamed_decl callback, used for read-only sections.  PTR
19945    points to the section string variable.  */
19946
19947 static void
19948 rs6000_xcoff_output_readonly_section_asm_op (const void *directive)
19949 {
19950   fprintf (asm_out_file, "\t.csect %s[RO],%s\n",
19951            *(const char *const *) directive,
19952            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
19953 }
19954
19955 /* Likewise for read-write sections.  */
19956
19957 static void
19958 rs6000_xcoff_output_readwrite_section_asm_op (const void *directive)
19959 {
19960   fprintf (asm_out_file, "\t.csect %s[RW],%s\n",
19961            *(const char *const *) directive,
19962            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
19963 }
19964
19965 static void
19966 rs6000_xcoff_output_tls_section_asm_op (const void *directive)
19967 {
19968   fprintf (asm_out_file, "\t.csect %s[TL],%s\n",
19969            *(const char *const *) directive,
19970            XCOFF_CSECT_DEFAULT_ALIGNMENT_STR);
19971 }
19972
19973 /* A get_unnamed_section callback, used for switching to toc_section.  */
19974
19975 static void
19976 rs6000_xcoff_output_toc_section_asm_op (const void *data ATTRIBUTE_UNUSED)
19977 {
19978   if (TARGET_MINIMAL_TOC)
19979     {
19980       /* toc_section is always selected at least once from
19981          rs6000_xcoff_file_start, so this is guaranteed to
19982          always be defined once and only once in each file.  */
19983       if (!toc_initialized)
19984         {
19985           fputs ("\t.toc\nLCTOC..1:\n", asm_out_file);
19986           fputs ("\t.tc toc_table[TC],toc_table[RW]\n", asm_out_file);
19987           toc_initialized = 1;
19988         }
19989       fprintf (asm_out_file, "\t.csect toc_table[RW]%s\n",
19990                (TARGET_32BIT ? "" : ",3"));
19991     }
19992   else
19993     fputs ("\t.toc\n", asm_out_file);
19994 }
19995
19996 /* Implement TARGET_ASM_INIT_SECTIONS.  */
19997
19998 static void
19999 rs6000_xcoff_asm_init_sections (void)
20000 {
20001   read_only_data_section
20002     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
20003                            &xcoff_read_only_section_name);
20004
20005   private_data_section
20006     = get_unnamed_section (SECTION_WRITE,
20007                            rs6000_xcoff_output_readwrite_section_asm_op,
20008                            &xcoff_private_data_section_name);
20009
20010   read_only_private_data_section
20011     = get_unnamed_section (0, rs6000_xcoff_output_readonly_section_asm_op,
20012                            &xcoff_private_rodata_section_name);
20013
20014   tls_data_section
20015     = get_unnamed_section (SECTION_TLS,
20016                            rs6000_xcoff_output_tls_section_asm_op,
20017                            &xcoff_tls_data_section_name);
20018
20019   tls_private_data_section
20020     = get_unnamed_section (SECTION_TLS,
20021                            rs6000_xcoff_output_tls_section_asm_op,
20022                            &xcoff_private_data_section_name);
20023
20024   toc_section
20025     = get_unnamed_section (0, rs6000_xcoff_output_toc_section_asm_op, NULL);
20026
20027   readonly_data_section = read_only_data_section;
20028 }
20029
20030 static int
20031 rs6000_xcoff_reloc_rw_mask (void)
20032 {
20033   return 3;
20034 }
20035
20036 static void
20037 rs6000_xcoff_asm_named_section (const char *name, unsigned int flags,
20038                                 tree decl ATTRIBUTE_UNUSED)
20039 {
20040   int smclass;
20041   static const char * const suffix[5] = { "PR", "RO", "RW", "TL", "XO" };
20042
20043   if (flags & SECTION_EXCLUDE)
20044     smclass = 4;
20045   else if (flags & SECTION_DEBUG)
20046     {
20047       fprintf (asm_out_file, "\t.dwsect %s\n", name);
20048       return;
20049     }
20050   else if (flags & SECTION_CODE)
20051     smclass = 0;
20052   else if (flags & SECTION_TLS)
20053     smclass = 3;
20054   else if (flags & SECTION_WRITE)
20055     smclass = 2;
20056   else
20057     smclass = 1;
20058
20059   fprintf (asm_out_file, "\t.csect %s%s[%s],%u\n",
20060            (flags & SECTION_CODE) ? "." : "",
20061            name, suffix[smclass], flags & SECTION_ENTSIZE);
20062 }
20063
20064 #define IN_NAMED_SECTION(DECL) \
20065   ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
20066    && DECL_SECTION_NAME (DECL) != NULL)
20067
20068 static section *
20069 rs6000_xcoff_select_section (tree decl, int reloc,
20070                              unsigned HOST_WIDE_INT align)
20071 {
20072   /* Place variables with alignment stricter than BIGGEST_ALIGNMENT into
20073      named section.  */
20074   if (align > BIGGEST_ALIGNMENT)
20075     {
20076       resolve_unique_section (decl, reloc, true);
20077       if (IN_NAMED_SECTION (decl))
20078         return get_named_section (decl, NULL, reloc);
20079     }
20080
20081   if (decl_readonly_section (decl, reloc))
20082     {
20083       if (TREE_PUBLIC (decl))
20084         return read_only_data_section;
20085       else
20086         return read_only_private_data_section;
20087     }
20088   else
20089     {
20090 #if HAVE_AS_TLS
20091       if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
20092         {
20093           if (TREE_PUBLIC (decl))
20094             return tls_data_section;
20095           else if (bss_initializer_p (decl))
20096             {
20097               /* Convert to COMMON to emit in BSS.  */
20098               DECL_COMMON (decl) = 1;
20099               return tls_comm_section;
20100             }
20101           else
20102             return tls_private_data_section;
20103         }
20104       else
20105 #endif
20106         if (TREE_PUBLIC (decl))
20107         return data_section;
20108       else
20109         return private_data_section;
20110     }
20111 }
20112
20113 static void
20114 rs6000_xcoff_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED)
20115 {
20116   const char *name;
20117
20118   /* Use select_section for private data and uninitialized data with
20119      alignment <= BIGGEST_ALIGNMENT.  */
20120   if (!TREE_PUBLIC (decl)
20121       || DECL_COMMON (decl)
20122       || (DECL_INITIAL (decl) == NULL_TREE
20123           && DECL_ALIGN (decl) <= BIGGEST_ALIGNMENT)
20124       || DECL_INITIAL (decl) == error_mark_node
20125       || (flag_zero_initialized_in_bss
20126           && initializer_zerop (DECL_INITIAL (decl))))
20127     return;
20128
20129   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
20130   name = (*targetm.strip_name_encoding) (name);
20131   set_decl_section_name (decl, name);
20132 }
20133
20134 /* Select section for constant in constant pool.
20135
20136    On RS/6000, all constants are in the private read-only data area.
20137    However, if this is being placed in the TOC it must be output as a
20138    toc entry.  */
20139
20140 static section *
20141 rs6000_xcoff_select_rtx_section (machine_mode mode, rtx x,
20142                                  unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
20143 {
20144   if (ASM_OUTPUT_SPECIAL_POOL_ENTRY_P (x, mode))
20145     return toc_section;
20146   else
20147     return read_only_private_data_section;
20148 }
20149
20150 /* Remove any trailing [DS] or the like from the symbol name.  */
20151
20152 static const char *
20153 rs6000_xcoff_strip_name_encoding (const char *name)
20154 {
20155   size_t len;
20156   if (*name == '*')
20157     name++;
20158   len = strlen (name);
20159   if (name[len - 1] == ']')
20160     return ggc_alloc_string (name, len - 4);
20161   else
20162     return name;
20163 }
20164
20165 /* Section attributes.  AIX is always PIC.  */
20166
20167 static unsigned int
20168 rs6000_xcoff_section_type_flags (tree decl, const char *name, int reloc)
20169 {
20170   unsigned int align;
20171   unsigned int flags = default_section_type_flags (decl, name, reloc);
20172
20173   /* Align to at least UNIT size.  */
20174   if ((flags & SECTION_CODE) != 0 || !decl || !DECL_P (decl))
20175     align = MIN_UNITS_PER_WORD;
20176   else
20177     /* Increase alignment of large objects if not already stricter.  */
20178     align = MAX ((DECL_ALIGN (decl) / BITS_PER_UNIT),
20179                  int_size_in_bytes (TREE_TYPE (decl)) > MIN_UNITS_PER_WORD
20180                  ? UNITS_PER_FP_WORD : MIN_UNITS_PER_WORD);
20181
20182   return flags | (exact_log2 (align) & SECTION_ENTSIZE);
20183 }
20184
20185 /* Output at beginning of assembler file.
20186
20187    Initialize the section names for the RS/6000 at this point.
20188
20189    Specify filename, including full path, to assembler.
20190
20191    We want to go into the TOC section so at least one .toc will be emitted.
20192    Also, in order to output proper .bs/.es pairs, we need at least one static
20193    [RW] section emitted.
20194
20195    Finally, declare mcount when profiling to make the assembler happy.  */
20196
20197 static void
20198 rs6000_xcoff_file_start (void)
20199 {
20200   rs6000_gen_section_name (&xcoff_bss_section_name,
20201                            main_input_filename, ".bss_");
20202   rs6000_gen_section_name (&xcoff_private_data_section_name,
20203                            main_input_filename, ".rw_");
20204   rs6000_gen_section_name (&xcoff_private_rodata_section_name,
20205                            main_input_filename, ".rop_");
20206   rs6000_gen_section_name (&xcoff_read_only_section_name,
20207                            main_input_filename, ".ro_");
20208   rs6000_gen_section_name (&xcoff_tls_data_section_name,
20209                            main_input_filename, ".tls_");
20210   rs6000_gen_section_name (&xcoff_tbss_section_name,
20211                            main_input_filename, ".tbss_[UL]");
20212
20213   fputs ("\t.file\t", asm_out_file);
20214   output_quoted_string (asm_out_file, main_input_filename);
20215   fputc ('\n', asm_out_file);
20216   if (write_symbols != NO_DEBUG)
20217     switch_to_section (private_data_section);
20218   switch_to_section (toc_section);
20219   switch_to_section (text_section);
20220   if (profile_flag)
20221     fprintf (asm_out_file, "\t.extern %s\n", RS6000_MCOUNT);
20222   rs6000_file_start ();
20223 }
20224
20225 /* Output at end of assembler file.
20226    On the RS/6000, referencing data should automatically pull in text.  */
20227
20228 static void
20229 rs6000_xcoff_file_end (void)
20230 {
20231   switch_to_section (text_section);
20232   fputs ("_section_.text:\n", asm_out_file);
20233   switch_to_section (data_section);
20234   fputs (TARGET_32BIT
20235          ? "\t.long _section_.text\n" : "\t.llong _section_.text\n",
20236          asm_out_file);
20237 }
20238
20239 struct declare_alias_data
20240 {
20241   FILE *file;
20242   bool function_descriptor;
20243 };
20244
20245 /* Declare alias N.  A helper function for for_node_and_aliases.  */
20246
20247 static bool
20248 rs6000_declare_alias (struct symtab_node *n, void *d)
20249 {
20250   struct declare_alias_data *data = (struct declare_alias_data *)d;
20251   /* Main symbol is output specially, because varasm machinery does part of
20252      the job for us - we do not need to declare .globl/lglobs and such.  */
20253   if (!n->alias || n->weakref)
20254     return false;
20255
20256   if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (n->decl)))
20257     return false;
20258
20259   /* Prevent assemble_alias from trying to use .set pseudo operation
20260      that does not behave as expected by the middle-end.  */
20261   TREE_ASM_WRITTEN (n->decl) = true;
20262
20263   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl));
20264   char *buffer = (char *) alloca (strlen (name) + 2);
20265   char *p;
20266   int dollar_inside = 0;
20267
20268   strcpy (buffer, name);
20269   p = strchr (buffer, '$');
20270   while (p) {
20271     *p = '_';
20272     dollar_inside++;
20273     p = strchr (p + 1, '$');
20274   }
20275   if (TREE_PUBLIC (n->decl))
20276     {
20277       if (!RS6000_WEAK || !DECL_WEAK (n->decl))
20278         {
20279           if (dollar_inside) {
20280               if (data->function_descriptor)
20281                 fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
20282               fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
20283             }
20284           if (data->function_descriptor)
20285             {
20286               fputs ("\t.globl .", data->file);
20287               RS6000_OUTPUT_BASENAME (data->file, buffer);
20288               putc ('\n', data->file);
20289             }
20290           fputs ("\t.globl ", data->file);
20291           RS6000_OUTPUT_BASENAME (data->file, buffer);
20292           putc ('\n', data->file);
20293         }
20294 #ifdef ASM_WEAKEN_DECL
20295       else if (DECL_WEAK (n->decl) && !data->function_descriptor)
20296         ASM_WEAKEN_DECL (data->file, n->decl, name, NULL);
20297 #endif
20298     }
20299   else
20300     {
20301       if (dollar_inside)
20302         {
20303           if (data->function_descriptor)
20304             fprintf(data->file, "\t.rename .%s,\".%s\"\n", buffer, name);
20305           fprintf(data->file, "\t.rename %s,\"%s\"\n", buffer, name);
20306         }
20307       if (data->function_descriptor)
20308         {
20309           fputs ("\t.lglobl .", data->file);
20310           RS6000_OUTPUT_BASENAME (data->file, buffer);
20311           putc ('\n', data->file);
20312         }
20313       fputs ("\t.lglobl ", data->file);
20314       RS6000_OUTPUT_BASENAME (data->file, buffer);
20315       putc ('\n', data->file);
20316     }
20317   if (data->function_descriptor)
20318     fputs (".", data->file);
20319   RS6000_OUTPUT_BASENAME (data->file, buffer);
20320   fputs (":\n", data->file);
20321   return false;
20322 }
20323
20324
20325 #ifdef HAVE_GAS_HIDDEN
20326 /* Helper function to calculate visibility of a DECL
20327    and return the value as a const string.  */
20328
20329 static const char *
20330 rs6000_xcoff_visibility (tree decl)
20331 {
20332   static const char * const visibility_types[] = {
20333     "", ",protected", ",hidden", ",internal"
20334   };
20335
20336   enum symbol_visibility vis = DECL_VISIBILITY (decl);
20337   return visibility_types[vis];
20338 }
20339 #endif
20340
20341
20342 /* This macro produces the initial definition of a function name.
20343    On the RS/6000, we need to place an extra '.' in the function name and
20344    output the function descriptor.
20345    Dollar signs are converted to underscores.
20346
20347    The csect for the function will have already been created when
20348    text_section was selected.  We do have to go back to that csect, however.
20349
20350    The third and fourth parameters to the .function pseudo-op (16 and 044)
20351    are placeholders which no longer have any use.
20352
20353    Because AIX assembler's .set command has unexpected semantics, we output
20354    all aliases as alternative labels in front of the definition.  */
20355
20356 void
20357 rs6000_xcoff_declare_function_name (FILE *file, const char *name, tree decl)
20358 {
20359   char *buffer = (char *) alloca (strlen (name) + 1);
20360   char *p;
20361   int dollar_inside = 0;
20362   struct declare_alias_data data = {file, false};
20363
20364   strcpy (buffer, name);
20365   p = strchr (buffer, '$');
20366   while (p) {
20367     *p = '_';
20368     dollar_inside++;
20369     p = strchr (p + 1, '$');
20370   }
20371   if (TREE_PUBLIC (decl))
20372     {
20373       if (!RS6000_WEAK || !DECL_WEAK (decl))
20374         {
20375           if (dollar_inside) {
20376               fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
20377               fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
20378             }
20379           fputs ("\t.globl .", file);
20380           RS6000_OUTPUT_BASENAME (file, buffer);
20381 #ifdef HAVE_GAS_HIDDEN
20382           fputs (rs6000_xcoff_visibility (decl), file);
20383 #endif
20384           putc ('\n', file);
20385         }
20386     }
20387   else
20388     {
20389       if (dollar_inside) {
20390           fprintf(file, "\t.rename .%s,\".%s\"\n", buffer, name);
20391           fprintf(file, "\t.rename %s,\"%s\"\n", buffer, name);
20392         }
20393       fputs ("\t.lglobl .", file);
20394       RS6000_OUTPUT_BASENAME (file, buffer);
20395       putc ('\n', file);
20396     }
20397   fputs ("\t.csect ", file);
20398   RS6000_OUTPUT_BASENAME (file, buffer);
20399   fputs (TARGET_32BIT ? "[DS]\n" : "[DS],3\n", file);
20400   RS6000_OUTPUT_BASENAME (file, buffer);
20401   fputs (":\n", file);
20402   symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
20403                                                         &data, true);
20404   fputs (TARGET_32BIT ? "\t.long ." : "\t.llong .", file);
20405   RS6000_OUTPUT_BASENAME (file, buffer);
20406   fputs (", TOC[tc0], 0\n", file);
20407   in_section = NULL;
20408   switch_to_section (function_section (decl));
20409   putc ('.', file);
20410   RS6000_OUTPUT_BASENAME (file, buffer);
20411   fputs (":\n", file);
20412   data.function_descriptor = true;
20413   symtab_node::get (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
20414                                                         &data, true);
20415   if (!DECL_IGNORED_P (decl))
20416     {
20417       if (write_symbols == DBX_DEBUG || write_symbols == XCOFF_DEBUG)
20418         xcoffout_declare_function (file, decl, buffer);
20419       else if (write_symbols == DWARF2_DEBUG)
20420         {
20421           name = (*targetm.strip_name_encoding) (name);
20422           fprintf (file, "\t.function .%s,.%s,2,0\n", name, name);
20423         }
20424     }
20425   return;
20426 }
20427
20428
20429 /* Output assembly language to globalize a symbol from a DECL,
20430    possibly with visibility.  */
20431
20432 void
20433 rs6000_xcoff_asm_globalize_decl_name (FILE *stream, tree decl)
20434 {
20435   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
20436   fputs (GLOBAL_ASM_OP, stream);
20437   RS6000_OUTPUT_BASENAME (stream, name);
20438 #ifdef HAVE_GAS_HIDDEN
20439   fputs (rs6000_xcoff_visibility (decl), stream);
20440 #endif
20441   putc ('\n', stream);
20442 }
20443
20444 /* Output assembly language to define a symbol as COMMON from a DECL,
20445    possibly with visibility.  */
20446
20447 void
20448 rs6000_xcoff_asm_output_aligned_decl_common (FILE *stream,
20449                                              tree decl ATTRIBUTE_UNUSED,
20450                                              const char *name,
20451                                              unsigned HOST_WIDE_INT size,
20452                                              unsigned HOST_WIDE_INT align)
20453 {
20454   unsigned HOST_WIDE_INT align2 = 2;
20455
20456   if (align > 32)
20457     align2 = floor_log2 (align / BITS_PER_UNIT);
20458   else if (size > 4)
20459     align2 = 3;
20460
20461   fputs (COMMON_ASM_OP, stream);
20462   RS6000_OUTPUT_BASENAME (stream, name);
20463
20464   fprintf (stream,
20465            "," HOST_WIDE_INT_PRINT_UNSIGNED "," HOST_WIDE_INT_PRINT_UNSIGNED,
20466            size, align2);
20467
20468 #ifdef HAVE_GAS_HIDDEN
20469   if (decl != NULL)
20470     fputs (rs6000_xcoff_visibility (decl), stream);
20471 #endif
20472   putc ('\n', stream);
20473 }
20474
20475 /* This macro produces the initial definition of a object (variable) name.
20476    Because AIX assembler's .set command has unexpected semantics, we output
20477    all aliases as alternative labels in front of the definition.  */
20478
20479 void
20480 rs6000_xcoff_declare_object_name (FILE *file, const char *name, tree decl)
20481 {
20482   struct declare_alias_data data = {file, false};
20483   RS6000_OUTPUT_BASENAME (file, name);
20484   fputs (":\n", file);
20485   symtab_node::get_create (decl)->call_for_symbol_and_aliases (rs6000_declare_alias,
20486                                                                &data, true);
20487 }
20488
20489 /* Overide the default 'SYMBOL-.' syntax with AIX compatible 'SYMBOL-$'. */
20490
20491 void
20492 rs6000_asm_output_dwarf_pcrel (FILE *file, int size, const char *label)
20493 {
20494   fputs (integer_asm_op (size, FALSE), file);
20495   assemble_name (file, label);
20496   fputs ("-$", file);
20497 }
20498
20499 /* Output a symbol offset relative to the dbase for the current object.
20500    We use __gcc_unwind_dbase as an arbitrary base for dbase and assume
20501    signed offsets.
20502
20503    __gcc_unwind_dbase is embedded in all executables/libraries through
20504    libgcc/config/rs6000/crtdbase.S.  */
20505
20506 void
20507 rs6000_asm_output_dwarf_datarel (FILE *file, int size, const char *label)
20508 {
20509   fputs (integer_asm_op (size, FALSE), file);
20510   assemble_name (file, label);
20511   fputs("-__gcc_unwind_dbase", file);
20512 }
20513
20514 #ifdef HAVE_AS_TLS
20515 static void
20516 rs6000_xcoff_encode_section_info (tree decl, rtx rtl, int first)
20517 {
20518   rtx symbol;
20519   int flags;
20520   const char *symname;
20521
20522   default_encode_section_info (decl, rtl, first);
20523
20524   /* Careful not to prod global register variables.  */
20525   if (!MEM_P (rtl))
20526     return;
20527   symbol = XEXP (rtl, 0);
20528   if (!SYMBOL_REF_P (symbol))
20529     return;
20530
20531   flags = SYMBOL_REF_FLAGS (symbol);
20532
20533   if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
20534     flags &= ~SYMBOL_FLAG_HAS_BLOCK_INFO;
20535
20536   SYMBOL_REF_FLAGS (symbol) = flags;
20537
20538   /* Append mapping class to extern decls.  */
20539   symname = XSTR (symbol, 0);
20540   if (decl /* sync condition with assemble_external () */
20541       && DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
20542       && ((TREE_CODE (decl) == VAR_DECL && !DECL_THREAD_LOCAL_P (decl))
20543           || TREE_CODE (decl) == FUNCTION_DECL)
20544       && symname[strlen (symname) - 1] != ']')
20545     {
20546       char *newname = (char *) alloca (strlen (symname) + 5);
20547       strcpy (newname, symname);
20548       strcat (newname, (TREE_CODE (decl) == FUNCTION_DECL
20549                         ? "[DS]" : "[UA]"));
20550       XSTR (symbol, 0) = ggc_strdup (newname);
20551     }
20552 }
20553 #endif /* HAVE_AS_TLS */
20554 #endif /* TARGET_XCOFF */
20555
20556 void
20557 rs6000_asm_weaken_decl (FILE *stream, tree decl,
20558                         const char *name, const char *val)
20559 {
20560   fputs ("\t.weak\t", stream);
20561   RS6000_OUTPUT_BASENAME (stream, name);
20562   if (decl && TREE_CODE (decl) == FUNCTION_DECL
20563       && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
20564     {
20565       if (TARGET_XCOFF)                                         
20566         fputs ("[DS]", stream);
20567 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
20568       if (TARGET_XCOFF)
20569         fputs (rs6000_xcoff_visibility (decl), stream);
20570 #endif
20571       fputs ("\n\t.weak\t.", stream);
20572       RS6000_OUTPUT_BASENAME (stream, name);
20573     }
20574 #if TARGET_XCOFF && HAVE_GAS_HIDDEN
20575   if (TARGET_XCOFF)
20576     fputs (rs6000_xcoff_visibility (decl), stream);
20577 #endif
20578   fputc ('\n', stream);
20579   if (val)
20580     {
20581 #ifdef ASM_OUTPUT_DEF
20582       ASM_OUTPUT_DEF (stream, name, val);
20583 #endif
20584       if (decl && TREE_CODE (decl) == FUNCTION_DECL
20585           && DEFAULT_ABI == ABI_AIX && DOT_SYMBOLS)
20586         {
20587           fputs ("\t.set\t.", stream);
20588           RS6000_OUTPUT_BASENAME (stream, name);
20589           fputs (",.", stream);
20590           RS6000_OUTPUT_BASENAME (stream, val);
20591           fputc ('\n', stream);
20592         }
20593     }
20594 }
20595
20596
20597 /* Return true if INSN should not be copied.  */
20598
20599 static bool
20600 rs6000_cannot_copy_insn_p (rtx_insn *insn)
20601 {
20602   return recog_memoized (insn) >= 0
20603          && get_attr_cannot_copy (insn);
20604 }
20605
20606 /* Compute a (partial) cost for rtx X.  Return true if the complete
20607    cost has been computed, and false if subexpressions should be
20608    scanned.  In either case, *TOTAL contains the cost result.  */
20609
20610 static bool
20611 rs6000_rtx_costs (rtx x, machine_mode mode, int outer_code,
20612                   int opno ATTRIBUTE_UNUSED, int *total, bool speed)
20613 {
20614   int code = GET_CODE (x);
20615
20616   switch (code)
20617     {
20618       /* On the RS/6000, if it is valid in the insn, it is free.  */
20619     case CONST_INT:
20620       if (((outer_code == SET
20621             || outer_code == PLUS
20622             || outer_code == MINUS)
20623            && (satisfies_constraint_I (x)
20624                || satisfies_constraint_L (x)))
20625           || (outer_code == AND
20626               && (satisfies_constraint_K (x)
20627                   || (mode == SImode
20628                       ? satisfies_constraint_L (x)
20629                       : satisfies_constraint_J (x))))
20630           || ((outer_code == IOR || outer_code == XOR)
20631               && (satisfies_constraint_K (x)
20632                   || (mode == SImode
20633                       ? satisfies_constraint_L (x)
20634                       : satisfies_constraint_J (x))))
20635           || outer_code == ASHIFT
20636           || outer_code == ASHIFTRT
20637           || outer_code == LSHIFTRT
20638           || outer_code == ROTATE
20639           || outer_code == ROTATERT
20640           || outer_code == ZERO_EXTRACT
20641           || (outer_code == MULT
20642               && satisfies_constraint_I (x))
20643           || ((outer_code == DIV || outer_code == UDIV
20644                || outer_code == MOD || outer_code == UMOD)
20645               && exact_log2 (INTVAL (x)) >= 0)
20646           || (outer_code == COMPARE
20647               && (satisfies_constraint_I (x)
20648                   || satisfies_constraint_K (x)))
20649           || ((outer_code == EQ || outer_code == NE)
20650               && (satisfies_constraint_I (x)
20651                   || satisfies_constraint_K (x)
20652                   || (mode == SImode
20653                       ? satisfies_constraint_L (x)
20654                       : satisfies_constraint_J (x))))
20655           || (outer_code == GTU
20656               && satisfies_constraint_I (x))
20657           || (outer_code == LTU
20658               && satisfies_constraint_P (x)))
20659         {
20660           *total = 0;
20661           return true;
20662         }
20663       else if ((outer_code == PLUS
20664                 && reg_or_add_cint_operand (x, VOIDmode))
20665                || (outer_code == MINUS
20666                    && reg_or_sub_cint_operand (x, VOIDmode))
20667                || ((outer_code == SET
20668                     || outer_code == IOR
20669                     || outer_code == XOR)
20670                    && (INTVAL (x)
20671                        & ~ (unsigned HOST_WIDE_INT) 0xffffffff) == 0))
20672         {
20673           *total = COSTS_N_INSNS (1);
20674           return true;
20675         }
20676       /* FALLTHRU */
20677
20678     case CONST_DOUBLE:
20679     case CONST_WIDE_INT:
20680     case CONST:
20681     case HIGH:
20682     case SYMBOL_REF:
20683       *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
20684       return true;
20685
20686     case MEM:
20687       /* When optimizing for size, MEM should be slightly more expensive
20688          than generating address, e.g., (plus (reg) (const)).
20689          L1 cache latency is about two instructions.  */
20690       *total = !speed ? COSTS_N_INSNS (1) + 1 : COSTS_N_INSNS (2);
20691       if (rs6000_slow_unaligned_access (mode, MEM_ALIGN (x)))
20692         *total += COSTS_N_INSNS (100);
20693       return true;
20694
20695     case LABEL_REF:
20696       *total = 0;
20697       return true;
20698
20699     case PLUS:
20700     case MINUS:
20701       if (FLOAT_MODE_P (mode))
20702         *total = rs6000_cost->fp;
20703       else
20704         *total = COSTS_N_INSNS (1);
20705       return false;
20706
20707     case MULT:
20708       if (CONST_INT_P (XEXP (x, 1))
20709           && satisfies_constraint_I (XEXP (x, 1)))
20710         {
20711           if (INTVAL (XEXP (x, 1)) >= -256
20712               && INTVAL (XEXP (x, 1)) <= 255)
20713             *total = rs6000_cost->mulsi_const9;
20714           else
20715             *total = rs6000_cost->mulsi_const;
20716         }
20717       else if (mode == SFmode)
20718         *total = rs6000_cost->fp;
20719       else if (FLOAT_MODE_P (mode))
20720         *total = rs6000_cost->dmul;
20721       else if (mode == DImode)
20722         *total = rs6000_cost->muldi;
20723       else
20724         *total = rs6000_cost->mulsi;
20725       return false;
20726
20727     case FMA:
20728       if (mode == SFmode)
20729         *total = rs6000_cost->fp;
20730       else
20731         *total = rs6000_cost->dmul;
20732       break;
20733
20734     case DIV:
20735     case MOD:
20736       if (FLOAT_MODE_P (mode))
20737         {
20738           *total = mode == DFmode ? rs6000_cost->ddiv
20739                                   : rs6000_cost->sdiv;
20740           return false;
20741         }
20742       /* FALLTHRU */
20743
20744     case UDIV:
20745     case UMOD:
20746       if (CONST_INT_P (XEXP (x, 1))
20747           && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
20748         {
20749           if (code == DIV || code == MOD)
20750             /* Shift, addze */
20751             *total = COSTS_N_INSNS (2);
20752           else
20753             /* Shift */
20754             *total = COSTS_N_INSNS (1);
20755         }
20756       else
20757         {
20758           if (GET_MODE (XEXP (x, 1)) == DImode)
20759             *total = rs6000_cost->divdi;
20760           else
20761             *total = rs6000_cost->divsi;
20762         }
20763       /* Add in shift and subtract for MOD unless we have a mod instruction. */
20764       if (!TARGET_MODULO && (code == MOD || code == UMOD))
20765         *total += COSTS_N_INSNS (2);
20766       return false;
20767
20768     case CTZ:
20769       *total = COSTS_N_INSNS (TARGET_CTZ ? 1 : 4);
20770       return false;
20771
20772     case FFS:
20773       *total = COSTS_N_INSNS (4);
20774       return false;
20775
20776     case POPCOUNT:
20777       *total = COSTS_N_INSNS (TARGET_POPCNTD ? 1 : 6);
20778       return false;
20779
20780     case PARITY:
20781       *total = COSTS_N_INSNS (TARGET_CMPB ? 2 : 6);
20782       return false;
20783
20784     case NOT:
20785       if (outer_code == AND || outer_code == IOR || outer_code == XOR)
20786         *total = 0;
20787       else
20788         *total = COSTS_N_INSNS (1);
20789       return false;
20790
20791     case AND:
20792       if (CONST_INT_P (XEXP (x, 1)))
20793         {
20794           rtx left = XEXP (x, 0);
20795           rtx_code left_code = GET_CODE (left);
20796
20797           /* rotate-and-mask: 1 insn.  */
20798           if ((left_code == ROTATE
20799                || left_code == ASHIFT
20800                || left_code == LSHIFTRT)
20801               && rs6000_is_valid_shift_mask (XEXP (x, 1), left, mode))
20802             {
20803               *total = rtx_cost (XEXP (left, 0), mode, left_code, 0, speed);
20804               if (!CONST_INT_P (XEXP (left, 1)))
20805                 *total += rtx_cost (XEXP (left, 1), SImode, left_code, 1, speed);
20806               *total += COSTS_N_INSNS (1);
20807               return true;
20808             }
20809
20810           /* rotate-and-mask (no rotate), andi., andis.: 1 insn.  */
20811           HOST_WIDE_INT val = INTVAL (XEXP (x, 1));
20812           if (rs6000_is_valid_and_mask (XEXP (x, 1), mode)
20813               || (val & 0xffff) == val
20814               || (val & 0xffff0000) == val
20815               || ((val & 0xffff) == 0 && mode == SImode))
20816             {
20817               *total = rtx_cost (left, mode, AND, 0, speed);
20818               *total += COSTS_N_INSNS (1);
20819               return true;
20820             }
20821
20822           /* 2 insns.  */
20823           if (rs6000_is_valid_2insn_and (XEXP (x, 1), mode))
20824             {
20825               *total = rtx_cost (left, mode, AND, 0, speed);
20826               *total += COSTS_N_INSNS (2);
20827               return true;
20828             }
20829         }
20830
20831       *total = COSTS_N_INSNS (1);
20832       return false;
20833
20834     case IOR:
20835       /* FIXME */
20836       *total = COSTS_N_INSNS (1);
20837       return true;
20838
20839     case CLZ:
20840     case XOR:
20841     case ZERO_EXTRACT:
20842       *total = COSTS_N_INSNS (1);
20843       return false;
20844
20845     case ASHIFT:
20846       /* The EXTSWSLI instruction is a combined instruction.  Don't count both
20847          the sign extend and shift separately within the insn.  */
20848       if (TARGET_EXTSWSLI && mode == DImode
20849           && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
20850           && GET_MODE (XEXP (XEXP (x, 0), 0)) == SImode)
20851         {
20852           *total = 0;
20853           return false;
20854         }
20855       /* fall through */
20856           
20857     case ASHIFTRT:
20858     case LSHIFTRT:
20859     case ROTATE:
20860     case ROTATERT:
20861       /* Handle mul_highpart.  */
20862       if (outer_code == TRUNCATE
20863           && GET_CODE (XEXP (x, 0)) == MULT)
20864         {
20865           if (mode == DImode)
20866             *total = rs6000_cost->muldi;
20867           else
20868             *total = rs6000_cost->mulsi;
20869           return true;
20870         }
20871       else if (outer_code == AND)
20872         *total = 0;
20873       else
20874         *total = COSTS_N_INSNS (1);
20875       return false;
20876
20877     case SIGN_EXTEND:
20878     case ZERO_EXTEND:
20879       if (MEM_P (XEXP (x, 0)))
20880         *total = 0;
20881       else
20882         *total = COSTS_N_INSNS (1);
20883       return false;
20884
20885     case COMPARE:
20886     case NEG:
20887     case ABS:
20888       if (!FLOAT_MODE_P (mode))
20889         {
20890           *total = COSTS_N_INSNS (1);
20891           return false;
20892         }
20893       /* FALLTHRU */
20894
20895     case FLOAT:
20896     case UNSIGNED_FLOAT:
20897     case FIX:
20898     case UNSIGNED_FIX:
20899     case FLOAT_TRUNCATE:
20900       *total = rs6000_cost->fp;
20901       return false;
20902
20903     case FLOAT_EXTEND:
20904       if (mode == DFmode)
20905         *total = rs6000_cost->sfdf_convert;
20906       else
20907         *total = rs6000_cost->fp;
20908       return false;
20909
20910     case CALL:
20911     case IF_THEN_ELSE:
20912       if (!speed)
20913         {
20914           *total = COSTS_N_INSNS (1);
20915           return true;
20916         }
20917       else if (FLOAT_MODE_P (mode) && TARGET_PPC_GFXOPT && TARGET_HARD_FLOAT)
20918         {
20919           *total = rs6000_cost->fp;
20920           return false;
20921         }
20922       break;
20923
20924     case NE:
20925     case EQ:
20926     case GTU:
20927     case LTU:
20928       /* Carry bit requires mode == Pmode.
20929          NEG or PLUS already counted so only add one.  */
20930       if (mode == Pmode
20931           && (outer_code == NEG || outer_code == PLUS))
20932         {
20933           *total = COSTS_N_INSNS (1);
20934           return true;
20935         }
20936       /* FALLTHRU */
20937
20938     case GT:
20939     case LT:
20940     case UNORDERED:
20941       if (outer_code == SET)
20942         {
20943           if (XEXP (x, 1) == const0_rtx)
20944             {
20945               *total = COSTS_N_INSNS (2);
20946               return true;
20947             }
20948           else
20949             {
20950               *total = COSTS_N_INSNS (3);
20951               return false;
20952             }
20953         }
20954       /* CC COMPARE.  */
20955       if (outer_code == COMPARE)
20956         {
20957           *total = 0;
20958           return true;
20959         }
20960       break;
20961
20962     default:
20963       break;
20964     }
20965
20966   return false;
20967 }
20968
20969 /* Debug form of r6000_rtx_costs that is selected if -mdebug=cost.  */
20970
20971 static bool
20972 rs6000_debug_rtx_costs (rtx x, machine_mode mode, int outer_code,
20973                         int opno, int *total, bool speed)
20974 {
20975   bool ret = rs6000_rtx_costs (x, mode, outer_code, opno, total, speed);
20976
20977   fprintf (stderr,
20978            "\nrs6000_rtx_costs, return = %s, mode = %s, outer_code = %s, "
20979            "opno = %d, total = %d, speed = %s, x:\n",
20980            ret ? "complete" : "scan inner",
20981            GET_MODE_NAME (mode),
20982            GET_RTX_NAME (outer_code),
20983            opno,
20984            *total,
20985            speed ? "true" : "false");
20986
20987   debug_rtx (x);
20988
20989   return ret;
20990 }
20991
20992 static int
20993 rs6000_insn_cost (rtx_insn *insn, bool speed)
20994 {
20995   if (recog_memoized (insn) < 0)
20996     return 0;
20997
20998   if (!speed)
20999     return get_attr_length (insn);
21000
21001   int cost = get_attr_cost (insn);
21002   if (cost > 0)
21003     return cost;
21004
21005   int n = get_attr_length (insn) / 4;
21006   enum attr_type type = get_attr_type (insn);
21007
21008   switch (type)
21009     {
21010     case TYPE_LOAD:
21011     case TYPE_FPLOAD:
21012     case TYPE_VECLOAD:
21013       cost = COSTS_N_INSNS (n + 1);
21014       break;
21015
21016     case TYPE_MUL:
21017       switch (get_attr_size (insn))
21018         {
21019         case SIZE_8:
21020           cost = COSTS_N_INSNS (n - 1) + rs6000_cost->mulsi_const9;
21021           break;
21022         case SIZE_16:
21023           cost = COSTS_N_INSNS (n - 1) + rs6000_cost->mulsi_const;
21024           break;
21025         case SIZE_32:
21026           cost = COSTS_N_INSNS (n - 1) + rs6000_cost->mulsi;
21027           break;
21028         case SIZE_64:
21029           cost = COSTS_N_INSNS (n - 1) + rs6000_cost->muldi;
21030           break;
21031         default:
21032           gcc_unreachable ();
21033         }
21034       break;
21035     case TYPE_DIV:
21036       switch (get_attr_size (insn))
21037         {
21038         case SIZE_32:
21039           cost = COSTS_N_INSNS (n - 1) + rs6000_cost->divsi;
21040           break;
21041         case SIZE_64:
21042           cost = COSTS_N_INSNS (n - 1) + rs6000_cost->divdi;
21043           break;
21044         default:
21045           gcc_unreachable ();
21046         }
21047       break;
21048
21049     case TYPE_FP:
21050       cost = n * rs6000_cost->fp;
21051       break;
21052     case TYPE_DMUL:
21053       cost = n * rs6000_cost->dmul;
21054       break;
21055     case TYPE_SDIV:
21056       cost = n * rs6000_cost->sdiv;
21057       break;
21058     case TYPE_DDIV:
21059       cost = n * rs6000_cost->ddiv;
21060       break;
21061
21062     case TYPE_SYNC:
21063     case TYPE_LOAD_L:
21064     case TYPE_MFCR:
21065     case TYPE_MFCRF:
21066       cost = COSTS_N_INSNS (n + 2);
21067       break;
21068
21069     default:
21070       cost = COSTS_N_INSNS (n);
21071     }
21072
21073   return cost;
21074 }
21075
21076 /* Debug form of ADDRESS_COST that is selected if -mdebug=cost.  */
21077
21078 static int
21079 rs6000_debug_address_cost (rtx x, machine_mode mode,
21080                            addr_space_t as, bool speed)
21081 {
21082   int ret = TARGET_ADDRESS_COST (x, mode, as, speed);
21083
21084   fprintf (stderr, "\nrs6000_address_cost, return = %d, speed = %s, x:\n",
21085            ret, speed ? "true" : "false");
21086   debug_rtx (x);
21087
21088   return ret;
21089 }
21090
21091
21092 /* A C expression returning the cost of moving data from a register of class
21093    CLASS1 to one of CLASS2.  */
21094
21095 static int
21096 rs6000_register_move_cost (machine_mode mode,
21097                            reg_class_t from, reg_class_t to)
21098 {
21099   int ret;
21100   reg_class_t rclass;
21101
21102   if (TARGET_DEBUG_COST)
21103     dbg_cost_ctrl++;
21104
21105   /* If we have VSX, we can easily move between FPR or Altivec registers,
21106      otherwise we can only easily move within classes.
21107      Do this first so we give best-case answers for union classes
21108      containing both gprs and vsx regs.  */
21109   HARD_REG_SET to_vsx, from_vsx;
21110   COPY_HARD_REG_SET (to_vsx, reg_class_contents[to]);
21111   AND_HARD_REG_SET (to_vsx, reg_class_contents[VSX_REGS]);
21112   COPY_HARD_REG_SET (from_vsx, reg_class_contents[from]);
21113   AND_HARD_REG_SET (from_vsx, reg_class_contents[VSX_REGS]);
21114   if (!hard_reg_set_empty_p (to_vsx)
21115       && !hard_reg_set_empty_p (from_vsx)
21116       && (TARGET_VSX
21117           || hard_reg_set_intersect_p (to_vsx, from_vsx)))
21118     {
21119       int reg = FIRST_FPR_REGNO;
21120       if (TARGET_VSX
21121           || (TEST_HARD_REG_BIT (to_vsx, FIRST_ALTIVEC_REGNO)
21122               && TEST_HARD_REG_BIT (from_vsx, FIRST_ALTIVEC_REGNO)))
21123         reg = FIRST_ALTIVEC_REGNO;
21124       ret = 2 * hard_regno_nregs (reg, mode);
21125     }
21126
21127   /*  Moves from/to GENERAL_REGS.  */
21128   else if ((rclass = from, reg_classes_intersect_p (to, GENERAL_REGS))
21129            || (rclass = to, reg_classes_intersect_p (from, GENERAL_REGS)))
21130     {
21131       if (rclass == FLOAT_REGS || rclass == ALTIVEC_REGS || rclass == VSX_REGS)
21132         {
21133           if (TARGET_DIRECT_MOVE)
21134             {
21135               /* Keep the cost for direct moves above that for within
21136                  a register class even if the actual processor cost is
21137                  comparable.  We do this because a direct move insn
21138                  can't be a nop, whereas with ideal register
21139                  allocation a move within the same class might turn
21140                  out to be a nop.  */
21141               if (rs6000_tune == PROCESSOR_POWER9
21142                   || rs6000_tune == PROCESSOR_FUTURE)
21143                 ret = 3 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
21144               else
21145                 ret = 4 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
21146               /* SFmode requires a conversion when moving between gprs
21147                  and vsx.  */
21148               if (mode == SFmode)
21149                 ret += 2;
21150             }
21151           else
21152             ret = (rs6000_memory_move_cost (mode, rclass, false)
21153                    + rs6000_memory_move_cost (mode, GENERAL_REGS, false));
21154         }
21155
21156       /* It's more expensive to move CR_REGS than CR0_REGS because of the
21157          shift.  */
21158       else if (rclass == CR_REGS)
21159         ret = 4;
21160
21161       /* For those processors that have slow LR/CTR moves, make them more
21162          expensive than memory in order to bias spills to memory .*/
21163       else if ((rs6000_tune == PROCESSOR_POWER6
21164                 || rs6000_tune == PROCESSOR_POWER7
21165                 || rs6000_tune == PROCESSOR_POWER8
21166                 || rs6000_tune == PROCESSOR_POWER9)
21167                && reg_class_subset_p (rclass, SPECIAL_REGS))
21168         ret = 6 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
21169
21170       else
21171         /* A move will cost one instruction per GPR moved.  */
21172         ret = 2 * hard_regno_nregs (FIRST_GPR_REGNO, mode);
21173     }
21174
21175   /* Everything else has to go through GENERAL_REGS.  */
21176   else
21177     ret = (rs6000_register_move_cost (mode, GENERAL_REGS, to)
21178            + rs6000_register_move_cost (mode, from, GENERAL_REGS));
21179
21180   if (TARGET_DEBUG_COST)
21181     {
21182       if (dbg_cost_ctrl == 1)
21183         fprintf (stderr,
21184                  "rs6000_register_move_cost: ret=%d, mode=%s, from=%s, to=%s\n",
21185                  ret, GET_MODE_NAME (mode), reg_class_names[from],
21186                  reg_class_names[to]);
21187       dbg_cost_ctrl--;
21188     }
21189
21190   return ret;
21191 }
21192
21193 /* A C expressions returning the cost of moving data of MODE from a register to
21194    or from memory.  */
21195
21196 static int
21197 rs6000_memory_move_cost (machine_mode mode, reg_class_t rclass,
21198                          bool in ATTRIBUTE_UNUSED)
21199 {
21200   int ret;
21201
21202   if (TARGET_DEBUG_COST)
21203     dbg_cost_ctrl++;
21204
21205   if (reg_classes_intersect_p (rclass, GENERAL_REGS))
21206     ret = 4 * hard_regno_nregs (0, mode);
21207   else if ((reg_classes_intersect_p (rclass, FLOAT_REGS)
21208             || reg_classes_intersect_p (rclass, VSX_REGS)))
21209     ret = 4 * hard_regno_nregs (32, mode);
21210   else if (reg_classes_intersect_p (rclass, ALTIVEC_REGS))
21211     ret = 4 * hard_regno_nregs (FIRST_ALTIVEC_REGNO, mode);
21212   else
21213     ret = 4 + rs6000_register_move_cost (mode, rclass, GENERAL_REGS);
21214
21215   if (TARGET_DEBUG_COST)
21216     {
21217       if (dbg_cost_ctrl == 1)
21218         fprintf (stderr,
21219                  "rs6000_memory_move_cost: ret=%d, mode=%s, rclass=%s, in=%d\n",
21220                  ret, GET_MODE_NAME (mode), reg_class_names[rclass], in);
21221       dbg_cost_ctrl--;
21222     }
21223
21224   return ret;
21225 }
21226
21227 /* Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS.
21228
21229    The register allocator chooses GEN_OR_VSX_REGS for the allocno
21230    class if GENERAL_REGS and VSX_REGS cost is lower than the memory
21231    cost.  This happens a lot when TARGET_DIRECT_MOVE makes the register
21232    move cost between GENERAL_REGS and VSX_REGS low.
21233
21234    It might seem reasonable to use a union class.  After all, if usage
21235    of vsr is low and gpr high, it might make sense to spill gpr to vsr
21236    rather than memory.  However, in cases where register pressure of
21237    both is high, like the cactus_adm spec test, allowing
21238    GEN_OR_VSX_REGS as the allocno class results in bad decisions in
21239    the first scheduling pass.  This is partly due to an allocno of
21240    GEN_OR_VSX_REGS wrongly contributing to the GENERAL_REGS pressure
21241    class, which gives too high a pressure for GENERAL_REGS and too low
21242    for VSX_REGS.  So, force a choice of the subclass here.
21243
21244    The best class is also the union if GENERAL_REGS and VSX_REGS have
21245    the same cost.  In that case we do use GEN_OR_VSX_REGS as the
21246    allocno class, since trying to narrow down the class by regno mode
21247    is prone to error.  For example, SImode is allowed in VSX regs and
21248    in some cases (eg. gcc.target/powerpc/p9-xxbr-3.c do_bswap32_vect)
21249    it would be wrong to choose an allocno of GENERAL_REGS based on
21250    SImode.  */
21251
21252 static reg_class_t
21253 rs6000_ira_change_pseudo_allocno_class (int regno ATTRIBUTE_UNUSED,
21254                                         reg_class_t allocno_class,
21255                                         reg_class_t best_class)
21256 {
21257   switch (allocno_class)
21258     {
21259     case GEN_OR_VSX_REGS:
21260       /* best_class must be a subset of allocno_class.  */
21261       gcc_checking_assert (best_class == GEN_OR_VSX_REGS
21262                            || best_class == GEN_OR_FLOAT_REGS
21263                            || best_class == VSX_REGS
21264                            || best_class == ALTIVEC_REGS
21265                            || best_class == FLOAT_REGS
21266                            || best_class == GENERAL_REGS
21267                            || best_class == BASE_REGS);
21268       /* Use best_class but choose wider classes when copying from the
21269          wider class to best_class is cheap.  This mimics IRA choice
21270          of allocno class.  */
21271       if (best_class == BASE_REGS)
21272         return GENERAL_REGS;
21273       if (TARGET_VSX
21274           && (best_class == FLOAT_REGS || best_class == ALTIVEC_REGS))
21275         return VSX_REGS;
21276       return best_class;
21277
21278     default:
21279       break;
21280     }
21281
21282   return allocno_class;
21283 }
21284
21285 /* Returns a code for a target-specific builtin that implements
21286    reciprocal of the function, or NULL_TREE if not available.  */
21287
21288 static tree
21289 rs6000_builtin_reciprocal (tree fndecl)
21290 {
21291   switch (DECL_MD_FUNCTION_CODE (fndecl))
21292     {
21293     case VSX_BUILTIN_XVSQRTDP:
21294       if (!RS6000_RECIP_AUTO_RSQRTE_P (V2DFmode))
21295         return NULL_TREE;
21296
21297       return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_2DF];
21298
21299     case VSX_BUILTIN_XVSQRTSP:
21300       if (!RS6000_RECIP_AUTO_RSQRTE_P (V4SFmode))
21301         return NULL_TREE;
21302
21303       return rs6000_builtin_decls[VSX_BUILTIN_RSQRT_4SF];
21304
21305     default:
21306       return NULL_TREE;
21307     }
21308 }
21309
21310 /* Load up a constant.  If the mode is a vector mode, splat the value across
21311    all of the vector elements.  */
21312
21313 static rtx
21314 rs6000_load_constant_and_splat (machine_mode mode, REAL_VALUE_TYPE dconst)
21315 {
21316   rtx reg;
21317
21318   if (mode == SFmode || mode == DFmode)
21319     {
21320       rtx d = const_double_from_real_value (dconst, mode);
21321       reg = force_reg (mode, d);
21322     }
21323   else if (mode == V4SFmode)
21324     {
21325       rtx d = const_double_from_real_value (dconst, SFmode);
21326       rtvec v = gen_rtvec (4, d, d, d, d);
21327       reg = gen_reg_rtx (mode);
21328       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
21329     }
21330   else if (mode == V2DFmode)
21331     {
21332       rtx d = const_double_from_real_value (dconst, DFmode);
21333       rtvec v = gen_rtvec (2, d, d);
21334       reg = gen_reg_rtx (mode);
21335       rs6000_expand_vector_init (reg, gen_rtx_PARALLEL (mode, v));
21336     }
21337   else
21338     gcc_unreachable ();
21339
21340   return reg;
21341 }
21342
21343 /* Generate an FMA instruction.  */
21344
21345 static void
21346 rs6000_emit_madd (rtx target, rtx m1, rtx m2, rtx a)
21347 {
21348   machine_mode mode = GET_MODE (target);
21349   rtx dst;
21350
21351   dst = expand_ternary_op (mode, fma_optab, m1, m2, a, target, 0);
21352   gcc_assert (dst != NULL);
21353
21354   if (dst != target)
21355     emit_move_insn (target, dst);
21356 }
21357
21358 /* Generate a FNMSUB instruction: dst = -fma(m1, m2, -a).  */
21359
21360 static void
21361 rs6000_emit_nmsub (rtx dst, rtx m1, rtx m2, rtx a)
21362 {
21363   machine_mode mode = GET_MODE (dst);
21364   rtx r;
21365
21366   /* This is a tad more complicated, since the fnma_optab is for
21367      a different expression: fma(-m1, m2, a), which is the same
21368      thing except in the case of signed zeros.
21369
21370      Fortunately we know that if FMA is supported that FNMSUB is
21371      also supported in the ISA.  Just expand it directly.  */
21372
21373   gcc_assert (optab_handler (fma_optab, mode) != CODE_FOR_nothing);
21374
21375   r = gen_rtx_NEG (mode, a);
21376   r = gen_rtx_FMA (mode, m1, m2, r);
21377   r = gen_rtx_NEG (mode, r);
21378   emit_insn (gen_rtx_SET (dst, r));
21379 }
21380
21381 /* Newton-Raphson approximation of floating point divide DST = N/D.  If NOTE_P,
21382    add a reg_note saying that this was a division.  Support both scalar and
21383    vector divide.  Assumes no trapping math and finite arguments.  */
21384
21385 void
21386 rs6000_emit_swdiv (rtx dst, rtx n, rtx d, bool note_p)
21387 {
21388   machine_mode mode = GET_MODE (dst);
21389   rtx one, x0, e0, x1, xprev, eprev, xnext, enext, u, v;
21390   int i;
21391
21392   /* Low precision estimates guarantee 5 bits of accuracy.  High
21393      precision estimates guarantee 14 bits of accuracy.  SFmode
21394      requires 23 bits of accuracy.  DFmode requires 52 bits of
21395      accuracy.  Each pass at least doubles the accuracy, leading
21396      to the following.  */
21397   int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
21398   if (mode == DFmode || mode == V2DFmode)
21399     passes++;
21400
21401   enum insn_code code = optab_handler (smul_optab, mode);
21402   insn_gen_fn gen_mul = GEN_FCN (code);
21403
21404   gcc_assert (code != CODE_FOR_nothing);
21405
21406   one = rs6000_load_constant_and_splat (mode, dconst1);
21407
21408   /* x0 = 1./d estimate */
21409   x0 = gen_reg_rtx (mode);
21410   emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, d),
21411                                               UNSPEC_FRES)));
21412
21413   /* Each iteration but the last calculates x_(i+1) = x_i * (2 - d * x_i).  */
21414   if (passes > 1) {
21415
21416     /* e0 = 1. - d * x0  */
21417     e0 = gen_reg_rtx (mode);
21418     rs6000_emit_nmsub (e0, d, x0, one);
21419
21420     /* x1 = x0 + e0 * x0  */
21421     x1 = gen_reg_rtx (mode);
21422     rs6000_emit_madd (x1, e0, x0, x0);
21423
21424     for (i = 0, xprev = x1, eprev = e0; i < passes - 2;
21425          ++i, xprev = xnext, eprev = enext) {
21426       
21427       /* enext = eprev * eprev  */
21428       enext = gen_reg_rtx (mode);
21429       emit_insn (gen_mul (enext, eprev, eprev));
21430
21431       /* xnext = xprev + enext * xprev  */
21432       xnext = gen_reg_rtx (mode);
21433       rs6000_emit_madd (xnext, enext, xprev, xprev);
21434     }
21435
21436   } else
21437     xprev = x0;
21438
21439   /* The last iteration calculates x_(i+1) = n * x_i * (2 - d * x_i).  */
21440
21441   /* u = n * xprev  */
21442   u = gen_reg_rtx (mode);
21443   emit_insn (gen_mul (u, n, xprev));
21444
21445   /* v = n - (d * u)  */
21446   v = gen_reg_rtx (mode);
21447   rs6000_emit_nmsub (v, d, u, n);
21448
21449   /* dst = (v * xprev) + u  */
21450   rs6000_emit_madd (dst, v, xprev, u);
21451
21452   if (note_p)
21453     add_reg_note (get_last_insn (), REG_EQUAL, gen_rtx_DIV (mode, n, d));
21454 }
21455
21456 /* Goldschmidt's Algorithm for single/double-precision floating point
21457    sqrt and rsqrt.  Assumes no trapping math and finite arguments.  */
21458
21459 void
21460 rs6000_emit_swsqrt (rtx dst, rtx src, bool recip)
21461 {
21462   machine_mode mode = GET_MODE (src);
21463   rtx e = gen_reg_rtx (mode);
21464   rtx g = gen_reg_rtx (mode);
21465   rtx h = gen_reg_rtx (mode);
21466
21467   /* Low precision estimates guarantee 5 bits of accuracy.  High
21468      precision estimates guarantee 14 bits of accuracy.  SFmode
21469      requires 23 bits of accuracy.  DFmode requires 52 bits of
21470      accuracy.  Each pass at least doubles the accuracy, leading
21471      to the following.  */
21472   int passes = (TARGET_RECIP_PRECISION) ? 1 : 3;
21473   if (mode == DFmode || mode == V2DFmode)
21474     passes++;
21475
21476   int i;
21477   rtx mhalf;
21478   enum insn_code code = optab_handler (smul_optab, mode);
21479   insn_gen_fn gen_mul = GEN_FCN (code);
21480
21481   gcc_assert (code != CODE_FOR_nothing);
21482
21483   mhalf = rs6000_load_constant_and_splat (mode, dconsthalf);
21484
21485   /* e = rsqrt estimate */
21486   emit_insn (gen_rtx_SET (e, gen_rtx_UNSPEC (mode, gen_rtvec (1, src),
21487                                              UNSPEC_RSQRT)));
21488
21489   /* If (src == 0.0) filter infinity to prevent NaN for sqrt(0.0).  */
21490   if (!recip)
21491     {
21492       rtx zero = force_reg (mode, CONST0_RTX (mode));
21493
21494       if (mode == SFmode)
21495         {
21496           rtx target = emit_conditional_move (e, GT, src, zero, mode,
21497                                               e, zero, mode, 0);
21498           if (target != e)
21499             emit_move_insn (e, target);
21500         }
21501       else
21502         {
21503           rtx cond = gen_rtx_GT (VOIDmode, e, zero);
21504           rs6000_emit_vector_cond_expr (e, e, zero, cond, src, zero);
21505         }
21506     }
21507
21508   /* g = sqrt estimate.  */
21509   emit_insn (gen_mul (g, e, src));
21510   /* h = 1/(2*sqrt) estimate.  */
21511   emit_insn (gen_mul (h, e, mhalf));
21512
21513   if (recip)
21514     {
21515       if (passes == 1)
21516         {
21517           rtx t = gen_reg_rtx (mode);
21518           rs6000_emit_nmsub (t, g, h, mhalf);
21519           /* Apply correction directly to 1/rsqrt estimate.  */
21520           rs6000_emit_madd (dst, e, t, e);
21521         }
21522       else
21523         {
21524           for (i = 0; i < passes; i++)
21525             {
21526               rtx t1 = gen_reg_rtx (mode);
21527               rtx g1 = gen_reg_rtx (mode);
21528               rtx h1 = gen_reg_rtx (mode);
21529
21530               rs6000_emit_nmsub (t1, g, h, mhalf);
21531               rs6000_emit_madd (g1, g, t1, g);
21532               rs6000_emit_madd (h1, h, t1, h);
21533
21534               g = g1;
21535               h = h1;
21536             }
21537           /* Multiply by 2 for 1/rsqrt.  */
21538           emit_insn (gen_add3_insn (dst, h, h));
21539         }
21540     }
21541   else
21542     {
21543       rtx t = gen_reg_rtx (mode);
21544       rs6000_emit_nmsub (t, g, h, mhalf);
21545       rs6000_emit_madd (dst, g, t, g);
21546     }
21547
21548   return;
21549 }
21550
21551 /* Emit popcount intrinsic on TARGET_POPCNTB (Power5) and TARGET_POPCNTD
21552    (Power7) targets.  DST is the target, and SRC is the argument operand.  */
21553
21554 void
21555 rs6000_emit_popcount (rtx dst, rtx src)
21556 {
21557   machine_mode mode = GET_MODE (dst);
21558   rtx tmp1, tmp2;
21559
21560   /* Use the PPC ISA 2.06 popcnt{w,d} instruction if we can.  */
21561   if (TARGET_POPCNTD)
21562     {
21563       if (mode == SImode)
21564         emit_insn (gen_popcntdsi2 (dst, src));
21565       else
21566         emit_insn (gen_popcntddi2 (dst, src));
21567       return;
21568     }
21569
21570   tmp1 = gen_reg_rtx (mode);
21571
21572   if (mode == SImode)
21573     {
21574       emit_insn (gen_popcntbsi2 (tmp1, src));
21575       tmp2 = expand_mult (SImode, tmp1, GEN_INT (0x01010101),
21576                            NULL_RTX, 0);
21577       tmp2 = force_reg (SImode, tmp2);
21578       emit_insn (gen_lshrsi3 (dst, tmp2, GEN_INT (24)));
21579     }
21580   else
21581     {
21582       emit_insn (gen_popcntbdi2 (tmp1, src));
21583       tmp2 = expand_mult (DImode, tmp1,
21584                           GEN_INT ((HOST_WIDE_INT)
21585                                    0x01010101 << 32 | 0x01010101),
21586                           NULL_RTX, 0);
21587       tmp2 = force_reg (DImode, tmp2);
21588       emit_insn (gen_lshrdi3 (dst, tmp2, GEN_INT (56)));
21589     }
21590 }
21591
21592
21593 /* Emit parity intrinsic on TARGET_POPCNTB targets.  DST is the
21594    target, and SRC is the argument operand.  */
21595
21596 void
21597 rs6000_emit_parity (rtx dst, rtx src)
21598 {
21599   machine_mode mode = GET_MODE (dst);
21600   rtx tmp;
21601
21602   tmp = gen_reg_rtx (mode);
21603
21604   /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can.  */
21605   if (TARGET_CMPB)
21606     {
21607       if (mode == SImode)
21608         {
21609           emit_insn (gen_popcntbsi2 (tmp, src));
21610           emit_insn (gen_paritysi2_cmpb (dst, tmp));
21611         }
21612       else
21613         {
21614           emit_insn (gen_popcntbdi2 (tmp, src));
21615           emit_insn (gen_paritydi2_cmpb (dst, tmp));
21616         }
21617       return;
21618     }
21619
21620   if (mode == SImode)
21621     {
21622       /* Is mult+shift >= shift+xor+shift+xor?  */
21623       if (rs6000_cost->mulsi_const >= COSTS_N_INSNS (3))
21624         {
21625           rtx tmp1, tmp2, tmp3, tmp4;
21626
21627           tmp1 = gen_reg_rtx (SImode);
21628           emit_insn (gen_popcntbsi2 (tmp1, src));
21629
21630           tmp2 = gen_reg_rtx (SImode);
21631           emit_insn (gen_lshrsi3 (tmp2, tmp1, GEN_INT (16)));
21632           tmp3 = gen_reg_rtx (SImode);
21633           emit_insn (gen_xorsi3 (tmp3, tmp1, tmp2));
21634
21635           tmp4 = gen_reg_rtx (SImode);
21636           emit_insn (gen_lshrsi3 (tmp4, tmp3, GEN_INT (8)));
21637           emit_insn (gen_xorsi3 (tmp, tmp3, tmp4));
21638         }
21639       else
21640         rs6000_emit_popcount (tmp, src);
21641       emit_insn (gen_andsi3 (dst, tmp, const1_rtx));
21642     }
21643   else
21644     {
21645       /* Is mult+shift >= shift+xor+shift+xor+shift+xor?  */
21646       if (rs6000_cost->muldi >= COSTS_N_INSNS (5))
21647         {
21648           rtx tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
21649
21650           tmp1 = gen_reg_rtx (DImode);
21651           emit_insn (gen_popcntbdi2 (tmp1, src));
21652
21653           tmp2 = gen_reg_rtx (DImode);
21654           emit_insn (gen_lshrdi3 (tmp2, tmp1, GEN_INT (32)));
21655           tmp3 = gen_reg_rtx (DImode);
21656           emit_insn (gen_xordi3 (tmp3, tmp1, tmp2));
21657
21658           tmp4 = gen_reg_rtx (DImode);
21659           emit_insn (gen_lshrdi3 (tmp4, tmp3, GEN_INT (16)));
21660           tmp5 = gen_reg_rtx (DImode);
21661           emit_insn (gen_xordi3 (tmp5, tmp3, tmp4));
21662
21663           tmp6 = gen_reg_rtx (DImode);
21664           emit_insn (gen_lshrdi3 (tmp6, tmp5, GEN_INT (8)));
21665           emit_insn (gen_xordi3 (tmp, tmp5, tmp6));
21666         }
21667       else
21668         rs6000_emit_popcount (tmp, src);
21669       emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
21670     }
21671 }
21672
21673 /* Expand an Altivec constant permutation for little endian mode.
21674    OP0 and OP1 are the input vectors and TARGET is the output vector.
21675    SEL specifies the constant permutation vector.
21676
21677    There are two issues: First, the two input operands must be
21678    swapped so that together they form a double-wide array in LE
21679    order.  Second, the vperm instruction has surprising behavior
21680    in LE mode:  it interprets the elements of the source vectors
21681    in BE mode ("left to right") and interprets the elements of
21682    the destination vector in LE mode ("right to left").  To
21683    correct for this, we must subtract each element of the permute
21684    control vector from 31.
21685
21686    For example, suppose we want to concatenate vr10 = {0, 1, 2, 3}
21687    with vr11 = {4, 5, 6, 7} and extract {0, 2, 4, 6} using a vperm.
21688    We place {0,1,2,3,8,9,10,11,16,17,18,19,24,25,26,27} in vr12 to
21689    serve as the permute control vector.  Then, in BE mode,
21690
21691      vperm 9,10,11,12
21692
21693    places the desired result in vr9.  However, in LE mode the 
21694    vector contents will be
21695
21696      vr10 = 00000003 00000002 00000001 00000000
21697      vr11 = 00000007 00000006 00000005 00000004
21698
21699    The result of the vperm using the same permute control vector is
21700
21701      vr9  = 05000000 07000000 01000000 03000000
21702
21703    That is, the leftmost 4 bytes of vr10 are interpreted as the
21704    source for the rightmost 4 bytes of vr9, and so on.
21705
21706    If we change the permute control vector to
21707
21708      vr12 = {31,20,29,28,23,22,21,20,15,14,13,12,7,6,5,4}
21709
21710    and issue
21711
21712      vperm 9,11,10,12
21713
21714    we get the desired
21715
21716    vr9  = 00000006 00000004 00000002 00000000.  */
21717
21718 static void
21719 altivec_expand_vec_perm_const_le (rtx target, rtx op0, rtx op1,
21720                                   const vec_perm_indices &sel)
21721 {
21722   unsigned int i;
21723   rtx perm[16];
21724   rtx constv, unspec;
21725
21726   /* Unpack and adjust the constant selector.  */
21727   for (i = 0; i < 16; ++i)
21728     {
21729       unsigned int elt = 31 - (sel[i] & 31);
21730       perm[i] = GEN_INT (elt);
21731     }
21732
21733   /* Expand to a permute, swapping the inputs and using the
21734      adjusted selector.  */
21735   if (!REG_P (op0))
21736     op0 = force_reg (V16QImode, op0);
21737   if (!REG_P (op1))
21738     op1 = force_reg (V16QImode, op1);
21739
21740   constv = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, perm));
21741   constv = force_reg (V16QImode, constv);
21742   unspec = gen_rtx_UNSPEC (V16QImode, gen_rtvec (3, op1, op0, constv),
21743                            UNSPEC_VPERM);
21744   if (!REG_P (target))
21745     {
21746       rtx tmp = gen_reg_rtx (V16QImode);
21747       emit_move_insn (tmp, unspec);
21748       unspec = tmp;
21749     }
21750
21751   emit_move_insn (target, unspec);
21752 }
21753
21754 /* Similarly to altivec_expand_vec_perm_const_le, we must adjust the
21755    permute control vector.  But here it's not a constant, so we must
21756    generate a vector NAND or NOR to do the adjustment.  */
21757
21758 void
21759 altivec_expand_vec_perm_le (rtx operands[4])
21760 {
21761   rtx notx, iorx, unspec;
21762   rtx target = operands[0];
21763   rtx op0 = operands[1];
21764   rtx op1 = operands[2];
21765   rtx sel = operands[3];
21766   rtx tmp = target;
21767   rtx norreg = gen_reg_rtx (V16QImode);
21768   machine_mode mode = GET_MODE (target);
21769
21770   /* Get everything in regs so the pattern matches.  */
21771   if (!REG_P (op0))
21772     op0 = force_reg (mode, op0);
21773   if (!REG_P (op1))
21774     op1 = force_reg (mode, op1);
21775   if (!REG_P (sel))
21776     sel = force_reg (V16QImode, sel);
21777   if (!REG_P (target))
21778     tmp = gen_reg_rtx (mode);
21779
21780   if (TARGET_P9_VECTOR)
21781     {
21782       unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, sel),
21783                                UNSPEC_VPERMR);
21784     }
21785   else
21786     {
21787       /* Invert the selector with a VNAND if available, else a VNOR.
21788          The VNAND is preferred for future fusion opportunities.  */
21789       notx = gen_rtx_NOT (V16QImode, sel);
21790       iorx = (TARGET_P8_VECTOR
21791               ? gen_rtx_IOR (V16QImode, notx, notx)
21792               : gen_rtx_AND (V16QImode, notx, notx));
21793       emit_insn (gen_rtx_SET (norreg, iorx));
21794
21795       /* Permute with operands reversed and adjusted selector.  */
21796       unspec = gen_rtx_UNSPEC (mode, gen_rtvec (3, op1, op0, norreg),
21797                                UNSPEC_VPERM);
21798     }
21799
21800   /* Copy into target, possibly by way of a register.  */
21801   if (!REG_P (target))
21802     {
21803       emit_move_insn (tmp, unspec);
21804       unspec = tmp;
21805     }
21806
21807   emit_move_insn (target, unspec);
21808 }
21809
21810 /* Expand an Altivec constant permutation.  Return true if we match
21811    an efficient implementation; false to fall back to VPERM.
21812
21813    OP0 and OP1 are the input vectors and TARGET is the output vector.
21814    SEL specifies the constant permutation vector.  */
21815
21816 static bool
21817 altivec_expand_vec_perm_const (rtx target, rtx op0, rtx op1,
21818                                const vec_perm_indices &sel)
21819 {
21820   struct altivec_perm_insn {
21821     HOST_WIDE_INT mask;
21822     enum insn_code impl;
21823     unsigned char perm[16];
21824   };
21825   static const struct altivec_perm_insn patterns[] = {
21826     { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuhum_direct,
21827       {  1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
21828     { OPTION_MASK_ALTIVEC, CODE_FOR_altivec_vpkuwum_direct,
21829       {  2,  3,  6,  7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
21830     { OPTION_MASK_ALTIVEC, 
21831       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghb_direct
21832        : CODE_FOR_altivec_vmrglb_direct),
21833       {  0, 16,  1, 17,  2, 18,  3, 19,  4, 20,  5, 21,  6, 22,  7, 23 } },
21834     { OPTION_MASK_ALTIVEC,
21835       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghh_direct
21836        : CODE_FOR_altivec_vmrglh_direct),
21837       {  0,  1, 16, 17,  2,  3, 18, 19,  4,  5, 20, 21,  6,  7, 22, 23 } },
21838     { OPTION_MASK_ALTIVEC,
21839       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrghw_direct
21840        : CODE_FOR_altivec_vmrglw_direct),
21841       {  0,  1,  2,  3, 16, 17, 18, 19,  4,  5,  6,  7, 20, 21, 22, 23 } },
21842     { OPTION_MASK_ALTIVEC,
21843       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglb_direct
21844        : CODE_FOR_altivec_vmrghb_direct),
21845       {  8, 24,  9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
21846     { OPTION_MASK_ALTIVEC,
21847       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglh_direct
21848        : CODE_FOR_altivec_vmrghh_direct),
21849       {  8,  9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
21850     { OPTION_MASK_ALTIVEC,
21851       (BYTES_BIG_ENDIAN ? CODE_FOR_altivec_vmrglw_direct
21852        : CODE_FOR_altivec_vmrghw_direct),
21853       {  8,  9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
21854     { OPTION_MASK_P8_VECTOR,
21855       (BYTES_BIG_ENDIAN ? CODE_FOR_p8_vmrgew_v4sf_direct
21856        : CODE_FOR_p8_vmrgow_v4sf_direct),
21857       {  0,  1,  2,  3, 16, 17, 18, 19,  8,  9, 10, 11, 24, 25, 26, 27 } },
21858     { OPTION_MASK_P8_VECTOR,
21859       (BYTES_BIG_ENDIAN ? CODE_FOR_p8_vmrgow_v4sf_direct
21860        : CODE_FOR_p8_vmrgew_v4sf_direct),
21861       {  4,  5,  6,  7, 20, 21, 22, 23, 12, 13, 14, 15, 28, 29, 30, 31 } }
21862   };
21863
21864   unsigned int i, j, elt, which;
21865   unsigned char perm[16];
21866   rtx x;
21867   bool one_vec;
21868
21869   /* Unpack the constant selector.  */
21870   for (i = which = 0; i < 16; ++i)
21871     {
21872       elt = sel[i] & 31;
21873       which |= (elt < 16 ? 1 : 2);
21874       perm[i] = elt;
21875     }
21876
21877   /* Simplify the constant selector based on operands.  */
21878   switch (which)
21879     {
21880     default:
21881       gcc_unreachable ();
21882
21883     case 3:
21884       one_vec = false;
21885       if (!rtx_equal_p (op0, op1))
21886         break;
21887       /* FALLTHRU */
21888
21889     case 2:
21890       for (i = 0; i < 16; ++i)
21891         perm[i] &= 15;
21892       op0 = op1;
21893       one_vec = true;
21894       break;
21895
21896     case 1:
21897       op1 = op0;
21898       one_vec = true;
21899       break;
21900     }
21901  
21902   /* Look for splat patterns.  */
21903   if (one_vec)
21904     {
21905       elt = perm[0];
21906
21907       for (i = 0; i < 16; ++i)
21908         if (perm[i] != elt)
21909           break;
21910       if (i == 16)
21911         {
21912           if (!BYTES_BIG_ENDIAN)
21913             elt = 15 - elt;
21914           emit_insn (gen_altivec_vspltb_direct (target, op0, GEN_INT (elt)));
21915           return true;
21916         }
21917
21918       if (elt % 2 == 0)
21919         {
21920           for (i = 0; i < 16; i += 2)
21921             if (perm[i] != elt || perm[i + 1] != elt + 1)
21922               break;
21923           if (i == 16)
21924             {
21925               int field = BYTES_BIG_ENDIAN ? elt / 2 : 7 - elt / 2;
21926               x = gen_reg_rtx (V8HImode);
21927               emit_insn (gen_altivec_vsplth_direct (x, gen_lowpart (V8HImode, op0),
21928                                                     GEN_INT (field)));
21929               emit_move_insn (target, gen_lowpart (V16QImode, x));
21930               return true;
21931             }
21932         }
21933
21934       if (elt % 4 == 0)
21935         {
21936           for (i = 0; i < 16; i += 4)
21937             if (perm[i] != elt
21938                 || perm[i + 1] != elt + 1
21939                 || perm[i + 2] != elt + 2
21940                 || perm[i + 3] != elt + 3)
21941               break;
21942           if (i == 16)
21943             {
21944               int field = BYTES_BIG_ENDIAN ? elt / 4 : 3 - elt / 4;
21945               x = gen_reg_rtx (V4SImode);
21946               emit_insn (gen_altivec_vspltw_direct (x, gen_lowpart (V4SImode, op0),
21947                                                     GEN_INT (field)));
21948               emit_move_insn (target, gen_lowpart (V16QImode, x));
21949               return true;
21950             }
21951         }
21952     }
21953
21954   /* Look for merge and pack patterns.  */
21955   for (j = 0; j < ARRAY_SIZE (patterns); ++j)
21956     {
21957       bool swapped;
21958
21959       if ((patterns[j].mask & rs6000_isa_flags) == 0)
21960         continue;
21961
21962       elt = patterns[j].perm[0];
21963       if (perm[0] == elt)
21964         swapped = false;
21965       else if (perm[0] == elt + 16)
21966         swapped = true;
21967       else
21968         continue;
21969       for (i = 1; i < 16; ++i)
21970         {
21971           elt = patterns[j].perm[i];
21972           if (swapped)
21973             elt = (elt >= 16 ? elt - 16 : elt + 16);
21974           else if (one_vec && elt >= 16)
21975             elt -= 16;
21976           if (perm[i] != elt)
21977             break;
21978         }
21979       if (i == 16)
21980         {
21981           enum insn_code icode = patterns[j].impl;
21982           machine_mode omode = insn_data[icode].operand[0].mode;
21983           machine_mode imode = insn_data[icode].operand[1].mode;
21984
21985           /* For little-endian, don't use vpkuwum and vpkuhum if the
21986              underlying vector type is not V4SI and V8HI, respectively.
21987              For example, using vpkuwum with a V8HI picks up the even
21988              halfwords (BE numbering) when the even halfwords (LE
21989              numbering) are what we need.  */
21990           if (!BYTES_BIG_ENDIAN
21991               && icode == CODE_FOR_altivec_vpkuwum_direct
21992               && ((REG_P (op0)
21993                    && GET_MODE (op0) != V4SImode)
21994                   || (SUBREG_P (op0)
21995                       && GET_MODE (XEXP (op0, 0)) != V4SImode)))
21996             continue;
21997           if (!BYTES_BIG_ENDIAN
21998               && icode == CODE_FOR_altivec_vpkuhum_direct
21999               && ((REG_P (op0)
22000                    && GET_MODE (op0) != V8HImode)
22001                   || (SUBREG_P (op0)
22002                       && GET_MODE (XEXP (op0, 0)) != V8HImode)))
22003             continue;
22004
22005           /* For little-endian, the two input operands must be swapped
22006              (or swapped back) to ensure proper right-to-left numbering
22007              from 0 to 2N-1.  */
22008           if (swapped ^ !BYTES_BIG_ENDIAN)
22009             std::swap (op0, op1);
22010           if (imode != V16QImode)
22011             {
22012               op0 = gen_lowpart (imode, op0);
22013               op1 = gen_lowpart (imode, op1);
22014             }
22015           if (omode == V16QImode)
22016             x = target;
22017           else
22018             x = gen_reg_rtx (omode);
22019           emit_insn (GEN_FCN (icode) (x, op0, op1));
22020           if (omode != V16QImode)
22021             emit_move_insn (target, gen_lowpart (V16QImode, x));
22022           return true;
22023         }
22024     }
22025
22026   if (!BYTES_BIG_ENDIAN)
22027     {
22028       altivec_expand_vec_perm_const_le (target, op0, op1, sel);
22029       return true;
22030     }
22031
22032   return false;
22033 }
22034
22035 /* Expand a VSX Permute Doubleword constant permutation.
22036    Return true if we match an efficient implementation.  */
22037
22038 static bool
22039 rs6000_expand_vec_perm_const_1 (rtx target, rtx op0, rtx op1,
22040                                 unsigned char perm0, unsigned char perm1)
22041 {
22042   rtx x;
22043
22044   /* If both selectors come from the same operand, fold to single op.  */
22045   if ((perm0 & 2) == (perm1 & 2))
22046     {
22047       if (perm0 & 2)
22048         op0 = op1;
22049       else
22050         op1 = op0;
22051     }
22052   /* If both operands are equal, fold to simpler permutation.  */
22053   if (rtx_equal_p (op0, op1))
22054     {
22055       perm0 = perm0 & 1;
22056       perm1 = (perm1 & 1) + 2;
22057     }
22058   /* If the first selector comes from the second operand, swap.  */
22059   else if (perm0 & 2)
22060     {
22061       if (perm1 & 2)
22062         return false;
22063       perm0 -= 2;
22064       perm1 += 2;
22065       std::swap (op0, op1);
22066     }
22067   /* If the second selector does not come from the second operand, fail.  */
22068   else if ((perm1 & 2) == 0)
22069     return false;
22070
22071   /* Success! */
22072   if (target != NULL)
22073     {
22074       machine_mode vmode, dmode;
22075       rtvec v;
22076
22077       vmode = GET_MODE (target);
22078       gcc_assert (GET_MODE_NUNITS (vmode) == 2);
22079       dmode = mode_for_vector (GET_MODE_INNER (vmode), 4).require ();
22080       x = gen_rtx_VEC_CONCAT (dmode, op0, op1);
22081       v = gen_rtvec (2, GEN_INT (perm0), GEN_INT (perm1));
22082       x = gen_rtx_VEC_SELECT (vmode, x, gen_rtx_PARALLEL (VOIDmode, v));
22083       emit_insn (gen_rtx_SET (target, x));
22084     }
22085   return true;
22086 }
22087
22088 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST.  */
22089
22090 static bool
22091 rs6000_vectorize_vec_perm_const (machine_mode vmode, rtx target, rtx op0,
22092                                  rtx op1, const vec_perm_indices &sel)
22093 {
22094   bool testing_p = !target;
22095
22096   /* AltiVec (and thus VSX) can handle arbitrary permutations.  */
22097   if (TARGET_ALTIVEC && testing_p)
22098     return true;
22099
22100   /* Check for ps_merge* or xxpermdi insns.  */
22101   if ((vmode == V2DFmode || vmode == V2DImode) && VECTOR_MEM_VSX_P (vmode))
22102     {
22103       if (testing_p)
22104         {
22105           op0 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 1);
22106           op1 = gen_raw_REG (vmode, LAST_VIRTUAL_REGISTER + 2);
22107         }
22108       if (rs6000_expand_vec_perm_const_1 (target, op0, op1, sel[0], sel[1]))
22109         return true;
22110     }
22111
22112   if (TARGET_ALTIVEC)
22113     {
22114       /* Force the target-independent code to lower to V16QImode.  */
22115       if (vmode != V16QImode)
22116         return false;
22117       if (altivec_expand_vec_perm_const (target, op0, op1, sel))
22118         return true;
22119     }
22120
22121   return false;
22122 }
22123
22124 /* A subroutine for rs6000_expand_extract_even & rs6000_expand_interleave.
22125    OP0 and OP1 are the input vectors and TARGET is the output vector.
22126    PERM specifies the constant permutation vector.  */
22127
22128 static void
22129 rs6000_do_expand_vec_perm (rtx target, rtx op0, rtx op1,
22130                            machine_mode vmode, const vec_perm_builder &perm)
22131 {
22132   rtx x = expand_vec_perm_const (vmode, op0, op1, perm, BLKmode, target);
22133   if (x != target)
22134     emit_move_insn (target, x);
22135 }
22136
22137 /* Expand an extract even operation.  */
22138
22139 void
22140 rs6000_expand_extract_even (rtx target, rtx op0, rtx op1)
22141 {
22142   machine_mode vmode = GET_MODE (target);
22143   unsigned i, nelt = GET_MODE_NUNITS (vmode);
22144   vec_perm_builder perm (nelt, nelt, 1);
22145
22146   for (i = 0; i < nelt; i++)
22147     perm.quick_push (i * 2);
22148
22149   rs6000_do_expand_vec_perm (target, op0, op1, vmode, perm);
22150 }
22151
22152 /* Expand a vector interleave operation.  */
22153
22154 void
22155 rs6000_expand_interleave (rtx target, rtx op0, rtx op1, bool highp)
22156 {
22157   machine_mode vmode = GET_MODE (target);
22158   unsigned i, high, nelt = GET_MODE_NUNITS (vmode);
22159   vec_perm_builder perm (nelt, nelt, 1);
22160
22161   high = (highp ? 0 : nelt / 2);
22162   for (i = 0; i < nelt / 2; i++)
22163     {
22164       perm.quick_push (i + high);
22165       perm.quick_push (i + nelt + high);
22166     }
22167
22168   rs6000_do_expand_vec_perm (target, op0, op1, vmode, perm);
22169 }
22170
22171 /* Scale a V2DF vector SRC by two to the SCALE and place in TGT.  */
22172 void
22173 rs6000_scale_v2df (rtx tgt, rtx src, int scale)
22174 {
22175   HOST_WIDE_INT hwi_scale (scale);
22176   REAL_VALUE_TYPE r_pow;
22177   rtvec v = rtvec_alloc (2);
22178   rtx elt;
22179   rtx scale_vec = gen_reg_rtx (V2DFmode);
22180   (void)real_powi (&r_pow, DFmode, &dconst2, hwi_scale);
22181   elt = const_double_from_real_value (r_pow, DFmode);
22182   RTVEC_ELT (v, 0) = elt;
22183   RTVEC_ELT (v, 1) = elt;
22184   rs6000_expand_vector_init (scale_vec, gen_rtx_PARALLEL (V2DFmode, v));
22185   emit_insn (gen_mulv2df3 (tgt, src, scale_vec));
22186 }
22187
22188 /* Return an RTX representing where to find the function value of a
22189    function returning MODE.  */
22190 static rtx
22191 rs6000_complex_function_value (machine_mode mode)
22192 {
22193   unsigned int regno;
22194   rtx r1, r2;
22195   machine_mode inner = GET_MODE_INNER (mode);
22196   unsigned int inner_bytes = GET_MODE_UNIT_SIZE (mode);
22197
22198   if (TARGET_FLOAT128_TYPE
22199       && (mode == KCmode
22200           || (mode == TCmode && TARGET_IEEEQUAD)))
22201     regno = ALTIVEC_ARG_RETURN;
22202
22203   else if (FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
22204     regno = FP_ARG_RETURN;
22205
22206   else
22207     {
22208       regno = GP_ARG_RETURN;
22209
22210       /* 32-bit is OK since it'll go in r3/r4.  */
22211       if (TARGET_32BIT && inner_bytes >= 4)
22212         return gen_rtx_REG (mode, regno);
22213     }
22214
22215   if (inner_bytes >= 8)
22216     return gen_rtx_REG (mode, regno);
22217
22218   r1 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno),
22219                           const0_rtx);
22220   r2 = gen_rtx_EXPR_LIST (inner, gen_rtx_REG (inner, regno + 1),
22221                           GEN_INT (inner_bytes));
22222   return gen_rtx_PARALLEL (mode, gen_rtvec (2, r1, r2));
22223 }
22224
22225 /* Return an rtx describing a return value of MODE as a PARALLEL
22226    in N_ELTS registers, each of mode ELT_MODE, starting at REGNO,
22227    stride REG_STRIDE.  */
22228
22229 static rtx
22230 rs6000_parallel_return (machine_mode mode,
22231                         int n_elts, machine_mode elt_mode,
22232                         unsigned int regno, unsigned int reg_stride)
22233 {
22234   rtx par = gen_rtx_PARALLEL (mode, rtvec_alloc (n_elts));
22235
22236   int i;
22237   for (i = 0; i < n_elts; i++)
22238     {
22239       rtx r = gen_rtx_REG (elt_mode, regno);
22240       rtx off = GEN_INT (i * GET_MODE_SIZE (elt_mode));
22241       XVECEXP (par, 0, i) = gen_rtx_EXPR_LIST (VOIDmode, r, off);
22242       regno += reg_stride;
22243     }
22244
22245   return par;
22246 }
22247
22248 /* Target hook for TARGET_FUNCTION_VALUE.
22249
22250    An integer value is in r3 and a floating-point value is in fp1,
22251    unless -msoft-float.  */
22252
22253 static rtx
22254 rs6000_function_value (const_tree valtype,
22255                        const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
22256                        bool outgoing ATTRIBUTE_UNUSED)
22257 {
22258   machine_mode mode;
22259   unsigned int regno;
22260   machine_mode elt_mode;
22261   int n_elts;
22262
22263   /* Special handling for structs in darwin64.  */
22264   if (TARGET_MACHO 
22265       && rs6000_darwin64_struct_check_p (TYPE_MODE (valtype), valtype))
22266     {
22267       CUMULATIVE_ARGS valcum;
22268       rtx valret;
22269
22270       valcum.words = 0;
22271       valcum.fregno = FP_ARG_MIN_REG;
22272       valcum.vregno = ALTIVEC_ARG_MIN_REG;
22273       /* Do a trial code generation as if this were going to be passed as
22274          an argument; if any part goes in memory, we return NULL.  */
22275       valret = rs6000_darwin64_record_arg (&valcum, valtype, true, /* retval= */ true);
22276       if (valret)
22277         return valret;
22278       /* Otherwise fall through to standard ABI rules.  */
22279     }
22280
22281   mode = TYPE_MODE (valtype);
22282
22283   /* The ELFv2 ABI returns homogeneous VFP aggregates in registers.  */
22284   if (rs6000_discover_homogeneous_aggregate (mode, valtype, &elt_mode, &n_elts))
22285     {
22286       int first_reg, n_regs;
22287
22288       if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (elt_mode))
22289         {
22290           /* _Decimal128 must use even/odd register pairs.  */
22291           first_reg = (elt_mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
22292           n_regs = (GET_MODE_SIZE (elt_mode) + 7) >> 3;
22293         }
22294       else
22295         {
22296           first_reg = ALTIVEC_ARG_RETURN;
22297           n_regs = 1;
22298         }
22299
22300       return rs6000_parallel_return (mode, n_elts, elt_mode, first_reg, n_regs);
22301     }
22302
22303   /* Some return value types need be split in -mpowerpc64, 32bit ABI.  */
22304   if (TARGET_32BIT && TARGET_POWERPC64)
22305     switch (mode)
22306       {
22307       default:
22308         break;
22309       case E_DImode:
22310       case E_SCmode:
22311       case E_DCmode:
22312       case E_TCmode:
22313         int count = GET_MODE_SIZE (mode) / 4;
22314         return rs6000_parallel_return (mode, count, SImode, GP_ARG_RETURN, 1);
22315       }
22316
22317   if ((INTEGRAL_TYPE_P (valtype)
22318        && GET_MODE_BITSIZE (mode) < (TARGET_32BIT ? 32 : 64))
22319       || POINTER_TYPE_P (valtype))
22320     mode = TARGET_32BIT ? SImode : DImode;
22321
22322   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
22323     /* _Decimal128 must use an even/odd register pair.  */
22324     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
22325   else if (SCALAR_FLOAT_TYPE_P (valtype) && TARGET_HARD_FLOAT
22326            && !FLOAT128_VECTOR_P (mode))
22327     regno = FP_ARG_RETURN;
22328   else if (TREE_CODE (valtype) == COMPLEX_TYPE
22329            && targetm.calls.split_complex_arg)
22330     return rs6000_complex_function_value (mode);
22331   /* VSX is a superset of Altivec and adds V2DImode/V2DFmode.  Since the same
22332      return register is used in both cases, and we won't see V2DImode/V2DFmode
22333      for pure altivec, combine the two cases.  */
22334   else if ((TREE_CODE (valtype) == VECTOR_TYPE || FLOAT128_VECTOR_P (mode))
22335            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI
22336            && ALTIVEC_OR_VSX_VECTOR_MODE (mode))
22337     regno = ALTIVEC_ARG_RETURN;
22338   else
22339     regno = GP_ARG_RETURN;
22340
22341   return gen_rtx_REG (mode, regno);
22342 }
22343
22344 /* Define how to find the value returned by a library function
22345    assuming the value has mode MODE.  */
22346 rtx
22347 rs6000_libcall_value (machine_mode mode)
22348 {
22349   unsigned int regno;
22350
22351   /* Long long return value need be split in -mpowerpc64, 32bit ABI.  */
22352   if (TARGET_32BIT && TARGET_POWERPC64 && mode == DImode)
22353     return rs6000_parallel_return (mode, 2, SImode, GP_ARG_RETURN, 1);
22354
22355   if (DECIMAL_FLOAT_MODE_P (mode) && TARGET_HARD_FLOAT)
22356     /* _Decimal128 must use an even/odd register pair.  */
22357     regno = (mode == TDmode) ? FP_ARG_RETURN + 1 : FP_ARG_RETURN;
22358   else if (SCALAR_FLOAT_MODE_NOT_VECTOR_P (mode) && TARGET_HARD_FLOAT)
22359     regno = FP_ARG_RETURN;
22360   /* VSX is a superset of Altivec and adds V2DImode/V2DFmode.  Since the same
22361      return register is used in both cases, and we won't see V2DImode/V2DFmode
22362      for pure altivec, combine the two cases.  */
22363   else if (ALTIVEC_OR_VSX_VECTOR_MODE (mode)
22364            && TARGET_ALTIVEC && TARGET_ALTIVEC_ABI)
22365     regno = ALTIVEC_ARG_RETURN;
22366   else if (COMPLEX_MODE_P (mode) && targetm.calls.split_complex_arg)
22367     return rs6000_complex_function_value (mode);
22368   else
22369     regno = GP_ARG_RETURN;
22370
22371   return gen_rtx_REG (mode, regno);
22372 }
22373
22374 /* Compute register pressure classes.  We implement the target hook to avoid
22375    IRA picking something like GEN_OR_FLOAT_REGS as a pressure class, which can
22376    lead to incorrect estimates of number of available registers and therefor
22377    increased register pressure/spill.   */
22378 static int
22379 rs6000_compute_pressure_classes (enum reg_class *pressure_classes)
22380 {
22381   int n;
22382
22383   n = 0;
22384   pressure_classes[n++] = GENERAL_REGS;
22385   if (TARGET_VSX)
22386     pressure_classes[n++] = VSX_REGS;
22387   else
22388     {
22389       if (TARGET_ALTIVEC)
22390         pressure_classes[n++] = ALTIVEC_REGS;
22391       if (TARGET_HARD_FLOAT)
22392         pressure_classes[n++] = FLOAT_REGS;
22393     }
22394   pressure_classes[n++] = CR_REGS;
22395   pressure_classes[n++] = SPECIAL_REGS;
22396
22397   return n;
22398 }
22399
22400 /* Given FROM and TO register numbers, say whether this elimination is allowed.
22401    Frame pointer elimination is automatically handled.
22402
22403    For the RS/6000, if frame pointer elimination is being done, we would like
22404    to convert ap into fp, not sp.
22405
22406    We need r30 if -mminimal-toc was specified, and there are constant pool
22407    references.  */
22408
22409 static bool
22410 rs6000_can_eliminate (const int from, const int to)
22411 {
22412   return (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM
22413           ? ! frame_pointer_needed
22414           : from == RS6000_PIC_OFFSET_TABLE_REGNUM
22415             ? ! TARGET_MINIMAL_TOC || TARGET_NO_TOC_OR_PCREL
22416                 || constant_pool_empty_p ()
22417             : true);
22418 }
22419
22420 /* Define the offset between two registers, FROM to be eliminated and its
22421    replacement TO, at the start of a routine.  */
22422 HOST_WIDE_INT
22423 rs6000_initial_elimination_offset (int from, int to)
22424 {
22425   rs6000_stack_t *info = rs6000_stack_info ();
22426   HOST_WIDE_INT offset;
22427
22428   if (from == HARD_FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
22429     offset = info->push_p ? 0 : -info->total_size;
22430   else if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
22431     {
22432       offset = info->push_p ? 0 : -info->total_size;
22433       if (FRAME_GROWS_DOWNWARD)
22434         offset += info->fixed_size + info->vars_size + info->parm_size;
22435     }
22436   else if (from == FRAME_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
22437     offset = FRAME_GROWS_DOWNWARD
22438              ? info->fixed_size + info->vars_size + info->parm_size
22439              : 0;
22440   else if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
22441     offset = info->total_size;
22442   else if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
22443     offset = info->push_p ? info->total_size : 0;
22444   else if (from == RS6000_PIC_OFFSET_TABLE_REGNUM)
22445     offset = 0;
22446   else
22447     gcc_unreachable ();
22448
22449   return offset;
22450 }
22451
22452 /* Fill in sizes of registers used by unwinder.  */
22453
22454 static void
22455 rs6000_init_dwarf_reg_sizes_extra (tree address)
22456 {
22457   if (TARGET_MACHO && ! TARGET_ALTIVEC)
22458     {
22459       int i;
22460       machine_mode mode = TYPE_MODE (char_type_node);
22461       rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL);
22462       rtx mem = gen_rtx_MEM (BLKmode, addr);
22463       rtx value = gen_int_mode (16, mode);
22464
22465       /* On Darwin, libgcc may be built to run on both G3 and G4/5.
22466          The unwinder still needs to know the size of Altivec registers.  */
22467
22468       for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++)
22469         {
22470           int column = DWARF_REG_TO_UNWIND_COLUMN
22471                 (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true));
22472           HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode);
22473
22474           emit_move_insn (adjust_address (mem, mode, offset), value);
22475         }
22476     }
22477 }
22478
22479 /* Map internal gcc register numbers to debug format register numbers.
22480    FORMAT specifies the type of debug register number to use:
22481      0 -- debug information, except for frame-related sections
22482      1 -- DWARF .debug_frame section
22483      2 -- DWARF .eh_frame section  */
22484
22485 unsigned int
22486 rs6000_dbx_register_number (unsigned int regno, unsigned int format)
22487 {
22488   /* On some platforms, we use the standard DWARF register
22489      numbering for .debug_info and .debug_frame.  */
22490   if ((format == 0 && write_symbols == DWARF2_DEBUG) || format == 1)
22491     {
22492 #ifdef RS6000_USE_DWARF_NUMBERING
22493       if (regno <= 31)
22494         return regno;
22495       if (FP_REGNO_P (regno))
22496         return regno - FIRST_FPR_REGNO + 32;
22497       if (ALTIVEC_REGNO_P (regno))
22498         return regno - FIRST_ALTIVEC_REGNO + 1124;
22499       if (regno == LR_REGNO)
22500         return 108;
22501       if (regno == CTR_REGNO)
22502         return 109;
22503       if (regno == CA_REGNO)
22504         return 101;  /* XER */
22505       /* Special handling for CR for .debug_frame: rs6000_emit_prologue has
22506          translated any combination of CR2, CR3, CR4 saves to a save of CR2.
22507          The actual code emitted saves the whole of CR, so we map CR2_REGNO
22508          to the DWARF reg for CR.  */
22509       if (format == 1 && regno == CR2_REGNO)
22510         return 64;
22511       if (CR_REGNO_P (regno))
22512         return regno - CR0_REGNO + 86;
22513       if (regno == VRSAVE_REGNO)
22514         return 356;
22515       if (regno == VSCR_REGNO)
22516         return 67;
22517
22518       /* These do not make much sense.  */
22519       if (regno == FRAME_POINTER_REGNUM)
22520         return 111;
22521       if (regno == ARG_POINTER_REGNUM)
22522         return 67;
22523       if (regno == 64)
22524         return 100;
22525
22526       gcc_unreachable ();
22527 #endif
22528     }
22529
22530   /* We use the GCC 7 (and before) internal number for non-DWARF debug
22531      information, and also for .eh_frame.  */
22532   /* Translate the regnos to their numbers in GCC 7 (and before).  */
22533   if (regno <= 31)
22534     return regno;
22535   if (FP_REGNO_P (regno))
22536     return regno - FIRST_FPR_REGNO + 32;
22537   if (ALTIVEC_REGNO_P (regno))
22538     return regno - FIRST_ALTIVEC_REGNO + 77;
22539   if (regno == LR_REGNO)
22540     return 65;
22541   if (regno == CTR_REGNO)
22542     return 66;
22543   if (regno == CA_REGNO)
22544     return 76;  /* XER */
22545   if (CR_REGNO_P (regno))
22546     return regno - CR0_REGNO + 68;
22547   if (regno == VRSAVE_REGNO)
22548     return 109;
22549   if (regno == VSCR_REGNO)
22550     return 110;
22551
22552   if (regno == FRAME_POINTER_REGNUM)
22553     return 111;
22554   if (regno == ARG_POINTER_REGNUM)
22555     return 67;
22556   if (regno == 64)
22557     return 64;
22558
22559   gcc_unreachable ();
22560 }
22561
22562 /* target hook eh_return_filter_mode */
22563 static scalar_int_mode
22564 rs6000_eh_return_filter_mode (void)
22565 {
22566   return TARGET_32BIT ? SImode : word_mode;
22567 }
22568
22569 /* Target hook for translate_mode_attribute.  */
22570 static machine_mode
22571 rs6000_translate_mode_attribute (machine_mode mode)
22572 {
22573   if ((FLOAT128_IEEE_P (mode)
22574        && ieee128_float_type_node == long_double_type_node)
22575       || (FLOAT128_IBM_P (mode)
22576           && ibm128_float_type_node == long_double_type_node))
22577     return COMPLEX_MODE_P (mode) ? E_TCmode : E_TFmode;
22578   return mode;
22579 }
22580
22581 /* Target hook for scalar_mode_supported_p.  */
22582 static bool
22583 rs6000_scalar_mode_supported_p (scalar_mode mode)
22584 {
22585   /* -m32 does not support TImode.  This is the default, from
22586      default_scalar_mode_supported_p.  For -m32 -mpowerpc64 we want the
22587      same ABI as for -m32.  But default_scalar_mode_supported_p allows
22588      integer modes of precision 2 * BITS_PER_WORD, which matches TImode
22589      for -mpowerpc64.  */
22590   if (TARGET_32BIT && mode == TImode)
22591     return false;
22592
22593   if (DECIMAL_FLOAT_MODE_P (mode))
22594     return default_decimal_float_supported_p ();
22595   else if (TARGET_FLOAT128_TYPE && (mode == KFmode || mode == IFmode))
22596     return true;
22597   else
22598     return default_scalar_mode_supported_p (mode);
22599 }
22600
22601 /* Target hook for vector_mode_supported_p.  */
22602 static bool
22603 rs6000_vector_mode_supported_p (machine_mode mode)
22604 {
22605   /* There is no vector form for IEEE 128-bit.  If we return true for IEEE
22606      128-bit, the compiler might try to widen IEEE 128-bit to IBM
22607      double-double.  */
22608   if (VECTOR_MEM_ALTIVEC_OR_VSX_P (mode) && !FLOAT128_IEEE_P (mode))
22609     return true;
22610
22611   else
22612     return false;
22613 }
22614
22615 /* Target hook for floatn_mode.  */
22616 static opt_scalar_float_mode
22617 rs6000_floatn_mode (int n, bool extended)
22618 {
22619   if (extended)
22620     {
22621       switch (n)
22622         {
22623         case 32:
22624           return DFmode;
22625
22626         case 64:
22627           if (TARGET_FLOAT128_TYPE)
22628             return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
22629           else
22630             return opt_scalar_float_mode ();
22631
22632         case 128:
22633           return opt_scalar_float_mode ();
22634
22635         default:
22636           /* Those are the only valid _FloatNx types.  */
22637           gcc_unreachable ();
22638         }
22639     }
22640   else
22641     {
22642       switch (n)
22643         {
22644         case 32:
22645           return SFmode;
22646
22647         case 64:
22648           return DFmode;
22649
22650         case 128:
22651           if (TARGET_FLOAT128_TYPE)
22652             return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
22653           else
22654             return opt_scalar_float_mode ();
22655
22656         default:
22657           return opt_scalar_float_mode ();
22658         }
22659     }
22660
22661 }
22662
22663 /* Target hook for c_mode_for_suffix.  */
22664 static machine_mode
22665 rs6000_c_mode_for_suffix (char suffix)
22666 {
22667   if (TARGET_FLOAT128_TYPE)
22668     {
22669       if (suffix == 'q' || suffix == 'Q')
22670         return (FLOAT128_IEEE_P (TFmode)) ? TFmode : KFmode;
22671
22672       /* At the moment, we are not defining a suffix for IBM extended double.
22673          If/when the default for -mabi=ieeelongdouble is changed, and we want
22674          to support __ibm128 constants in legacy library code, we may need to
22675          re-evalaute this decision.  Currently, c-lex.c only supports 'w' and
22676          'q' as machine dependent suffixes.  The x86_64 port uses 'w' for
22677          __float80 constants.  */
22678     }
22679
22680   return VOIDmode;
22681 }
22682
22683 /* Target hook for invalid_arg_for_unprototyped_fn. */
22684 static const char *
22685 invalid_arg_for_unprototyped_fn (const_tree typelist, const_tree funcdecl, const_tree val)
22686 {
22687   return (!rs6000_darwin64_abi
22688           && typelist == 0
22689           && TREE_CODE (TREE_TYPE (val)) == VECTOR_TYPE
22690           && (funcdecl == NULL_TREE
22691               || (TREE_CODE (funcdecl) == FUNCTION_DECL
22692                   && DECL_BUILT_IN_CLASS (funcdecl) != BUILT_IN_MD)))
22693           ? N_("AltiVec argument passed to unprototyped function")
22694           : NULL;
22695 }
22696
22697 /* For TARGET_SECURE_PLT 32-bit PIC code we can save PIC register
22698    setup by using __stack_chk_fail_local hidden function instead of
22699    calling __stack_chk_fail directly.  Otherwise it is better to call
22700    __stack_chk_fail directly.  */
22701
22702 static tree ATTRIBUTE_UNUSED
22703 rs6000_stack_protect_fail (void)
22704 {
22705   return (DEFAULT_ABI == ABI_V4 && TARGET_SECURE_PLT && flag_pic)
22706          ? default_hidden_stack_protect_fail ()
22707          : default_external_stack_protect_fail ();
22708 }
22709
22710 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook.  */
22711
22712 #if TARGET_ELF
22713 static unsigned HOST_WIDE_INT
22714 rs6000_asan_shadow_offset (void)
22715 {
22716   return (unsigned HOST_WIDE_INT) 1 << (TARGET_64BIT ? 41 : 29);
22717 }
22718 #endif
22719 \f
22720 /* Mask options that we want to support inside of attribute((target)) and
22721    #pragma GCC target operations.  Note, we do not include things like
22722    64/32-bit, endianness, hard/soft floating point, etc. that would have
22723    different calling sequences.  */
22724
22725 struct rs6000_opt_mask {
22726   const char *name;             /* option name */
22727   HOST_WIDE_INT mask;           /* mask to set */
22728   bool invert;                  /* invert sense of mask */
22729   bool valid_target;            /* option is a target option */
22730 };
22731
22732 static struct rs6000_opt_mask const rs6000_opt_masks[] =
22733 {
22734   { "altivec",                  OPTION_MASK_ALTIVEC,            false, true  },
22735   { "cmpb",                     OPTION_MASK_CMPB,               false, true  },
22736   { "crypto",                   OPTION_MASK_CRYPTO,             false, true  },
22737   { "direct-move",              OPTION_MASK_DIRECT_MOVE,        false, true  },
22738   { "dlmzb",                    OPTION_MASK_DLMZB,              false, true  },
22739   { "efficient-unaligned-vsx",  OPTION_MASK_EFFICIENT_UNALIGNED_VSX,
22740                                                                 false, true  },
22741   { "float128",                 OPTION_MASK_FLOAT128_KEYWORD,   false, true  },
22742   { "float128-hardware",        OPTION_MASK_FLOAT128_HW,        false, true  },
22743   { "fprnd",                    OPTION_MASK_FPRND,              false, true  },
22744   { "future",                   OPTION_MASK_FUTURE,             false, true  },
22745   { "hard-dfp",                 OPTION_MASK_DFP,                false, true  },
22746   { "htm",                      OPTION_MASK_HTM,                false, true  },
22747   { "isel",                     OPTION_MASK_ISEL,               false, true  },
22748   { "mfcrf",                    OPTION_MASK_MFCRF,              false, true  },
22749   { "mfpgpr",                   0,                              false, true  },
22750   { "modulo",                   OPTION_MASK_MODULO,             false, true  },
22751   { "mulhw",                    OPTION_MASK_MULHW,              false, true  },
22752   { "multiple",                 OPTION_MASK_MULTIPLE,           false, true  },
22753   { "pcrel",                    OPTION_MASK_PCREL,              false, true  },
22754   { "popcntb",                  OPTION_MASK_POPCNTB,            false, true  },
22755   { "popcntd",                  OPTION_MASK_POPCNTD,            false, true  },
22756   { "power8-fusion",            OPTION_MASK_P8_FUSION,          false, true  },
22757   { "power8-fusion-sign",       OPTION_MASK_P8_FUSION_SIGN,     false, true  },
22758   { "power8-vector",            OPTION_MASK_P8_VECTOR,          false, true  },
22759   { "power9-minmax",            OPTION_MASK_P9_MINMAX,          false, true  },
22760   { "power9-misc",              OPTION_MASK_P9_MISC,            false, true  },
22761   { "power9-vector",            OPTION_MASK_P9_VECTOR,          false, true  },
22762   { "powerpc-gfxopt",           OPTION_MASK_PPC_GFXOPT,         false, true  },
22763   { "powerpc-gpopt",            OPTION_MASK_PPC_GPOPT,          false, true  },
22764   { "prefixed-addr",            OPTION_MASK_PREFIXED_ADDR,      false, true  },
22765   { "quad-memory",              OPTION_MASK_QUAD_MEMORY,        false, true  },
22766   { "quad-memory-atomic",       OPTION_MASK_QUAD_MEMORY_ATOMIC, false, true  },
22767   { "recip-precision",          OPTION_MASK_RECIP_PRECISION,    false, true  },
22768   { "save-toc-indirect",        OPTION_MASK_SAVE_TOC_INDIRECT,  false, true  },
22769   { "string",                   0,                              false, true  },
22770   { "update",                   OPTION_MASK_NO_UPDATE,          true , true  },
22771   { "vsx",                      OPTION_MASK_VSX,                false, true  },
22772 #ifdef OPTION_MASK_64BIT
22773 #if TARGET_AIX_OS
22774   { "aix64",                    OPTION_MASK_64BIT,              false, false },
22775   { "aix32",                    OPTION_MASK_64BIT,              true,  false },
22776 #else
22777   { "64",                       OPTION_MASK_64BIT,              false, false },
22778   { "32",                       OPTION_MASK_64BIT,              true,  false },
22779 #endif
22780 #endif
22781 #ifdef OPTION_MASK_EABI
22782   { "eabi",                     OPTION_MASK_EABI,               false, false },
22783 #endif
22784 #ifdef OPTION_MASK_LITTLE_ENDIAN
22785   { "little",                   OPTION_MASK_LITTLE_ENDIAN,      false, false },
22786   { "big",                      OPTION_MASK_LITTLE_ENDIAN,      true,  false },
22787 #endif
22788 #ifdef OPTION_MASK_RELOCATABLE
22789   { "relocatable",              OPTION_MASK_RELOCATABLE,        false, false },
22790 #endif
22791 #ifdef OPTION_MASK_STRICT_ALIGN
22792   { "strict-align",             OPTION_MASK_STRICT_ALIGN,       false, false },
22793 #endif
22794   { "soft-float",               OPTION_MASK_SOFT_FLOAT,         false, false },
22795   { "string",                   0,                              false, false },
22796 };
22797
22798 /* Builtin mask mapping for printing the flags.  */
22799 static struct rs6000_opt_mask const rs6000_builtin_mask_names[] =
22800 {
22801   { "altivec",           RS6000_BTM_ALTIVEC,    false, false },
22802   { "vsx",               RS6000_BTM_VSX,        false, false },
22803   { "fre",               RS6000_BTM_FRE,        false, false },
22804   { "fres",              RS6000_BTM_FRES,       false, false },
22805   { "frsqrte",           RS6000_BTM_FRSQRTE,    false, false },
22806   { "frsqrtes",          RS6000_BTM_FRSQRTES,   false, false },
22807   { "popcntd",           RS6000_BTM_POPCNTD,    false, false },
22808   { "cell",              RS6000_BTM_CELL,       false, false },
22809   { "power8-vector",     RS6000_BTM_P8_VECTOR,  false, false },
22810   { "power9-vector",     RS6000_BTM_P9_VECTOR,  false, false },
22811   { "power9-misc",       RS6000_BTM_P9_MISC,    false, false },
22812   { "crypto",            RS6000_BTM_CRYPTO,     false, false },
22813   { "htm",               RS6000_BTM_HTM,        false, false },
22814   { "hard-dfp",          RS6000_BTM_DFP,        false, false },
22815   { "hard-float",        RS6000_BTM_HARD_FLOAT, false, false },
22816   { "long-double-128",   RS6000_BTM_LDBL128,    false, false },
22817   { "powerpc64",         RS6000_BTM_POWERPC64,  false, false },
22818   { "float128",          RS6000_BTM_FLOAT128,   false, false },
22819   { "float128-hw",       RS6000_BTM_FLOAT128_HW,false, false },
22820 };
22821
22822 /* Option variables that we want to support inside attribute((target)) and
22823    #pragma GCC target operations.  */
22824
22825 struct rs6000_opt_var {
22826   const char *name;             /* option name */
22827   size_t global_offset;         /* offset of the option in global_options.  */
22828   size_t target_offset;         /* offset of the option in target options.  */
22829 };
22830
22831 static struct rs6000_opt_var const rs6000_opt_vars[] =
22832 {
22833   { "friz",
22834     offsetof (struct gcc_options, x_TARGET_FRIZ),
22835     offsetof (struct cl_target_option, x_TARGET_FRIZ), },
22836   { "avoid-indexed-addresses",
22837     offsetof (struct gcc_options, x_TARGET_AVOID_XFORM),
22838     offsetof (struct cl_target_option, x_TARGET_AVOID_XFORM) },
22839   { "longcall",
22840     offsetof (struct gcc_options, x_rs6000_default_long_calls),
22841     offsetof (struct cl_target_option, x_rs6000_default_long_calls), },
22842   { "optimize-swaps",
22843     offsetof (struct gcc_options, x_rs6000_optimize_swaps),
22844     offsetof (struct cl_target_option, x_rs6000_optimize_swaps), },
22845   { "allow-movmisalign",
22846     offsetof (struct gcc_options, x_TARGET_ALLOW_MOVMISALIGN),
22847     offsetof (struct cl_target_option, x_TARGET_ALLOW_MOVMISALIGN), },
22848   { "sched-groups",
22849     offsetof (struct gcc_options, x_TARGET_SCHED_GROUPS),
22850     offsetof (struct cl_target_option, x_TARGET_SCHED_GROUPS), },
22851   { "always-hint",
22852     offsetof (struct gcc_options, x_TARGET_ALWAYS_HINT),
22853     offsetof (struct cl_target_option, x_TARGET_ALWAYS_HINT), },
22854   { "align-branch-targets",
22855     offsetof (struct gcc_options, x_TARGET_ALIGN_BRANCH_TARGETS),
22856     offsetof (struct cl_target_option, x_TARGET_ALIGN_BRANCH_TARGETS), },
22857   { "tls-markers",
22858     offsetof (struct gcc_options, x_tls_markers),
22859     offsetof (struct cl_target_option, x_tls_markers), },
22860   { "sched-prolog",
22861     offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
22862     offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
22863   { "sched-epilog",
22864     offsetof (struct gcc_options, x_TARGET_SCHED_PROLOG),
22865     offsetof (struct cl_target_option, x_TARGET_SCHED_PROLOG), },
22866   { "speculate-indirect-jumps",
22867     offsetof (struct gcc_options, x_rs6000_speculate_indirect_jumps),
22868     offsetof (struct cl_target_option, x_rs6000_speculate_indirect_jumps), },
22869 };
22870
22871 /* Inner function to handle attribute((target("..."))) and #pragma GCC target
22872    parsing.  Return true if there were no errors.  */
22873
22874 static bool
22875 rs6000_inner_target_options (tree args, bool attr_p)
22876 {
22877   bool ret = true;
22878
22879   if (args == NULL_TREE)
22880     ;
22881
22882   else if (TREE_CODE (args) == STRING_CST)
22883     {
22884       char *p = ASTRDUP (TREE_STRING_POINTER (args));
22885       char *q;
22886
22887       while ((q = strtok (p, ",")) != NULL)
22888         {
22889           bool error_p = false;
22890           bool not_valid_p = false;
22891           const char *cpu_opt = NULL;
22892
22893           p = NULL;
22894           if (strncmp (q, "cpu=", 4) == 0)
22895             {
22896               int cpu_index = rs6000_cpu_name_lookup (q+4);
22897               if (cpu_index >= 0)
22898                 rs6000_cpu_index = cpu_index;
22899               else
22900                 {
22901                   error_p = true;
22902                   cpu_opt = q+4;
22903                 }
22904             }
22905           else if (strncmp (q, "tune=", 5) == 0)
22906             {
22907               int tune_index = rs6000_cpu_name_lookup (q+5);
22908               if (tune_index >= 0)
22909                 rs6000_tune_index = tune_index;
22910               else
22911                 {
22912                   error_p = true;
22913                   cpu_opt = q+5;
22914                 }
22915             }
22916           else
22917             {
22918               size_t i;
22919               bool invert = false;
22920               char *r = q;
22921
22922               error_p = true;
22923               if (strncmp (r, "no-", 3) == 0)
22924                 {
22925                   invert = true;
22926                   r += 3;
22927                 }
22928
22929               for (i = 0; i < ARRAY_SIZE (rs6000_opt_masks); i++)
22930                 if (strcmp (r, rs6000_opt_masks[i].name) == 0)
22931                   {
22932                     HOST_WIDE_INT mask = rs6000_opt_masks[i].mask;
22933
22934                     if (!rs6000_opt_masks[i].valid_target)
22935                       not_valid_p = true;
22936                     else
22937                       {
22938                         error_p = false;
22939                         rs6000_isa_flags_explicit |= mask;
22940
22941                         /* VSX needs altivec, so -mvsx automagically sets
22942                            altivec and disables -mavoid-indexed-addresses.  */
22943                         if (!invert)
22944                           {
22945                             if (mask == OPTION_MASK_VSX)
22946                               {
22947                                 mask |= OPTION_MASK_ALTIVEC;
22948                                 TARGET_AVOID_XFORM = 0;
22949                               }
22950                           }
22951
22952                         if (rs6000_opt_masks[i].invert)
22953                           invert = !invert;
22954
22955                         if (invert)
22956                           rs6000_isa_flags &= ~mask;
22957                         else
22958                           rs6000_isa_flags |= mask;
22959                       }
22960                     break;
22961                   }
22962
22963               if (error_p && !not_valid_p)
22964                 {
22965                   for (i = 0; i < ARRAY_SIZE (rs6000_opt_vars); i++)
22966                     if (strcmp (r, rs6000_opt_vars[i].name) == 0)
22967                       {
22968                         size_t j = rs6000_opt_vars[i].global_offset;
22969                         *((int *) ((char *)&global_options + j)) = !invert;
22970                         error_p = false;
22971                         not_valid_p = false;
22972                         break;
22973                       }
22974                 }
22975             }
22976
22977           if (error_p)
22978             {
22979               const char *eprefix, *esuffix;
22980
22981               ret = false;
22982               if (attr_p)
22983                 {
22984                   eprefix = "__attribute__((__target__(";
22985                   esuffix = ")))";
22986                 }
22987               else
22988                 {
22989                   eprefix = "#pragma GCC target ";
22990                   esuffix = "";
22991                 }
22992
22993               if (cpu_opt)
22994                 error ("invalid cpu %qs for %s%qs%s", cpu_opt, eprefix,
22995                        q, esuffix);
22996               else if (not_valid_p)
22997                 error ("%s%qs%s is not allowed", eprefix, q, esuffix);
22998               else
22999                 error ("%s%qs%s is invalid", eprefix, q, esuffix);
23000             }
23001         }
23002     }
23003
23004   else if (TREE_CODE (args) == TREE_LIST)
23005     {
23006       do
23007         {
23008           tree value = TREE_VALUE (args);
23009           if (value)
23010             {
23011               bool ret2 = rs6000_inner_target_options (value, attr_p);
23012               if (!ret2)
23013                 ret = false;
23014             }
23015           args = TREE_CHAIN (args);
23016         }
23017       while (args != NULL_TREE);
23018     }
23019
23020   else
23021     {
23022       error ("attribute %<target%> argument not a string");
23023       return false;
23024     }
23025
23026   return ret;
23027 }
23028
23029 /* Print out the target options as a list for -mdebug=target.  */
23030
23031 static void
23032 rs6000_debug_target_options (tree args, const char *prefix)
23033 {
23034   if (args == NULL_TREE)
23035     fprintf (stderr, "%s<NULL>", prefix);
23036
23037   else if (TREE_CODE (args) == STRING_CST)
23038     {
23039       char *p = ASTRDUP (TREE_STRING_POINTER (args));
23040       char *q;
23041
23042       while ((q = strtok (p, ",")) != NULL)
23043         {
23044           p = NULL;
23045           fprintf (stderr, "%s\"%s\"", prefix, q);
23046           prefix = ", ";
23047         }
23048     }
23049
23050   else if (TREE_CODE (args) == TREE_LIST)
23051     {
23052       do
23053         {
23054           tree value = TREE_VALUE (args);
23055           if (value)
23056             {
23057               rs6000_debug_target_options (value, prefix);
23058               prefix = ", ";
23059             }
23060           args = TREE_CHAIN (args);
23061         }
23062       while (args != NULL_TREE);
23063     }
23064
23065   else
23066     gcc_unreachable ();
23067
23068   return;
23069 }
23070
23071 \f
23072 /* Hook to validate attribute((target("..."))).  */
23073
23074 static bool
23075 rs6000_valid_attribute_p (tree fndecl,
23076                           tree ARG_UNUSED (name),
23077                           tree args,
23078                           int flags)
23079 {
23080   struct cl_target_option cur_target;
23081   bool ret;
23082   tree old_optimize;
23083   tree new_target, new_optimize;
23084   tree func_optimize;
23085
23086   gcc_assert ((fndecl != NULL_TREE) && (args != NULL_TREE));
23087
23088   if (TARGET_DEBUG_TARGET)
23089     {
23090       tree tname = DECL_NAME (fndecl);
23091       fprintf (stderr, "\n==================== rs6000_valid_attribute_p:\n");
23092       if (tname)
23093         fprintf (stderr, "function: %.*s\n",
23094                  (int) IDENTIFIER_LENGTH (tname),
23095                  IDENTIFIER_POINTER (tname));
23096       else
23097         fprintf (stderr, "function: unknown\n");
23098   
23099       fprintf (stderr, "args:");
23100       rs6000_debug_target_options (args, " ");
23101       fprintf (stderr, "\n");
23102
23103       if (flags)
23104         fprintf (stderr, "flags: 0x%x\n", flags);
23105
23106       fprintf (stderr, "--------------------\n");
23107     }
23108
23109   /* attribute((target("default"))) does nothing, beyond
23110      affecting multi-versioning.  */
23111   if (TREE_VALUE (args)
23112       && TREE_CODE (TREE_VALUE (args)) == STRING_CST
23113       && TREE_CHAIN (args) == NULL_TREE
23114       && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "default") == 0)
23115     return true;
23116
23117   old_optimize = build_optimization_node (&global_options);
23118   func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
23119
23120   /* If the function changed the optimization levels as well as setting target
23121      options, start with the optimizations specified.  */
23122   if (func_optimize && func_optimize != old_optimize)
23123     cl_optimization_restore (&global_options,
23124                              TREE_OPTIMIZATION (func_optimize));
23125
23126   /* The target attributes may also change some optimization flags, so update
23127      the optimization options if necessary.  */
23128   cl_target_option_save (&cur_target, &global_options);
23129   rs6000_cpu_index = rs6000_tune_index = -1;
23130   ret = rs6000_inner_target_options (args, true);
23131
23132   /* Set up any additional state.  */
23133   if (ret)
23134     {
23135       ret = rs6000_option_override_internal (false);
23136       new_target = build_target_option_node (&global_options);
23137     }
23138   else
23139     new_target = NULL;
23140
23141   new_optimize = build_optimization_node (&global_options);
23142
23143   if (!new_target)
23144     ret = false;
23145
23146   else if (fndecl)
23147     {
23148       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
23149
23150       if (old_optimize != new_optimize)
23151         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
23152     }
23153
23154   cl_target_option_restore (&global_options, &cur_target);
23155
23156   if (old_optimize != new_optimize)
23157     cl_optimization_restore (&global_options,
23158                              TREE_OPTIMIZATION (old_optimize));
23159
23160   return ret;
23161 }
23162
23163 \f
23164 /* Hook to validate the current #pragma GCC target and set the state, and
23165    update the macros based on what was changed.  If ARGS is NULL, then
23166    POP_TARGET is used to reset the options.  */
23167
23168 bool
23169 rs6000_pragma_target_parse (tree args, tree pop_target)
23170 {
23171   tree prev_tree = build_target_option_node (&global_options);
23172   tree cur_tree;
23173   struct cl_target_option *prev_opt, *cur_opt;
23174   HOST_WIDE_INT prev_flags, cur_flags, diff_flags;
23175   HOST_WIDE_INT prev_bumask, cur_bumask, diff_bumask;
23176
23177   if (TARGET_DEBUG_TARGET)
23178     {
23179       fprintf (stderr, "\n==================== rs6000_pragma_target_parse\n");
23180       fprintf (stderr, "args:");
23181       rs6000_debug_target_options (args, " ");
23182       fprintf (stderr, "\n");
23183
23184       if (pop_target)
23185         {
23186           fprintf (stderr, "pop_target:\n");
23187           debug_tree (pop_target);
23188         }
23189       else
23190         fprintf (stderr, "pop_target: <NULL>\n");
23191
23192       fprintf (stderr, "--------------------\n");
23193     }
23194
23195   if (! args)
23196     {
23197       cur_tree = ((pop_target)
23198                   ? pop_target
23199                   : target_option_default_node);
23200       cl_target_option_restore (&global_options,
23201                                 TREE_TARGET_OPTION (cur_tree));
23202     }
23203   else
23204     {
23205       rs6000_cpu_index = rs6000_tune_index = -1;
23206       if (!rs6000_inner_target_options (args, false)
23207           || !rs6000_option_override_internal (false)
23208           || (cur_tree = build_target_option_node (&global_options))
23209              == NULL_TREE)
23210         {
23211           if (TARGET_DEBUG_BUILTIN || TARGET_DEBUG_TARGET)
23212             fprintf (stderr, "invalid pragma\n");
23213
23214           return false;
23215         }
23216     }
23217
23218   target_option_current_node = cur_tree;
23219   rs6000_activate_target_options (target_option_current_node);
23220
23221   /* If we have the preprocessor linked in (i.e. C or C++ languages), possibly
23222      change the macros that are defined.  */
23223   if (rs6000_target_modify_macros_ptr)
23224     {
23225       prev_opt    = TREE_TARGET_OPTION (prev_tree);
23226       prev_bumask = prev_opt->x_rs6000_builtin_mask;
23227       prev_flags  = prev_opt->x_rs6000_isa_flags;
23228
23229       cur_opt     = TREE_TARGET_OPTION (cur_tree);
23230       cur_flags   = cur_opt->x_rs6000_isa_flags;
23231       cur_bumask  = cur_opt->x_rs6000_builtin_mask;
23232
23233       diff_bumask = (prev_bumask ^ cur_bumask);
23234       diff_flags  = (prev_flags ^ cur_flags);
23235
23236       if ((diff_flags != 0) || (diff_bumask != 0))
23237         {
23238           /* Delete old macros.  */
23239           rs6000_target_modify_macros_ptr (false,
23240                                            prev_flags & diff_flags,
23241                                            prev_bumask & diff_bumask);
23242
23243           /* Define new macros.  */
23244           rs6000_target_modify_macros_ptr (true,
23245                                            cur_flags & diff_flags,
23246                                            cur_bumask & diff_bumask);
23247         }
23248     }
23249
23250   return true;
23251 }
23252
23253 \f
23254 /* Remember the last target of rs6000_set_current_function.  */
23255 static GTY(()) tree rs6000_previous_fndecl;
23256
23257 /* Restore target's globals from NEW_TREE and invalidate the
23258    rs6000_previous_fndecl cache.  */
23259
23260 void
23261 rs6000_activate_target_options (tree new_tree)
23262 {
23263   cl_target_option_restore (&global_options, TREE_TARGET_OPTION (new_tree));
23264   if (TREE_TARGET_GLOBALS (new_tree))
23265     restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
23266   else if (new_tree == target_option_default_node)
23267     restore_target_globals (&default_target_globals);
23268   else
23269     TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
23270   rs6000_previous_fndecl = NULL_TREE;
23271 }
23272
23273 /* Establish appropriate back-end context for processing the function
23274    FNDECL.  The argument might be NULL to indicate processing at top
23275    level, outside of any function scope.  */
23276 static void
23277 rs6000_set_current_function (tree fndecl)
23278 {
23279   if (TARGET_DEBUG_TARGET)
23280     {
23281       fprintf (stderr, "\n==================== rs6000_set_current_function");
23282
23283       if (fndecl)
23284         fprintf (stderr, ", fndecl %s (%p)",
23285                  (DECL_NAME (fndecl)
23286                   ? IDENTIFIER_POINTER (DECL_NAME (fndecl))
23287                   : "<unknown>"), (void *)fndecl);
23288
23289       if (rs6000_previous_fndecl)
23290         fprintf (stderr, ", prev_fndecl (%p)", (void *)rs6000_previous_fndecl);
23291
23292       fprintf (stderr, "\n");
23293     }
23294
23295   /* Only change the context if the function changes.  This hook is called
23296      several times in the course of compiling a function, and we don't want to
23297      slow things down too much or call target_reinit when it isn't safe.  */
23298   if (fndecl == rs6000_previous_fndecl)
23299     return;
23300
23301   tree old_tree;
23302   if (rs6000_previous_fndecl == NULL_TREE)
23303     old_tree = target_option_current_node;
23304   else if (DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl))
23305     old_tree = DECL_FUNCTION_SPECIFIC_TARGET (rs6000_previous_fndecl);
23306   else
23307     old_tree = target_option_default_node;
23308
23309   tree new_tree;
23310   if (fndecl == NULL_TREE)
23311     {
23312       if (old_tree != target_option_current_node)
23313         new_tree = target_option_current_node;
23314       else
23315         new_tree = NULL_TREE;
23316     }
23317   else
23318     {
23319       new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
23320       if (new_tree == NULL_TREE)
23321         new_tree = target_option_default_node;
23322     }
23323
23324   if (TARGET_DEBUG_TARGET)
23325     {
23326       if (new_tree)
23327         {
23328           fprintf (stderr, "\nnew fndecl target specific options:\n");
23329           debug_tree (new_tree);
23330         }
23331
23332       if (old_tree)
23333         {
23334           fprintf (stderr, "\nold fndecl target specific options:\n");
23335           debug_tree (old_tree);
23336         }
23337
23338       if (old_tree != NULL_TREE || new_tree != NULL_TREE)
23339         fprintf (stderr, "--------------------\n");
23340     }
23341
23342   if (new_tree && old_tree != new_tree)
23343     rs6000_activate_target_options (new_tree);
23344
23345   if (fndecl)
23346     rs6000_previous_fndecl = fndecl;
23347 }
23348
23349 \f
23350 /* Save the current options */
23351
23352 static void
23353 rs6000_function_specific_save (struct cl_target_option *ptr,
23354                                struct gcc_options *opts)
23355 {
23356   ptr->x_rs6000_isa_flags = opts->x_rs6000_isa_flags;
23357   ptr->x_rs6000_isa_flags_explicit = opts->x_rs6000_isa_flags_explicit;
23358 }
23359
23360 /* Restore the current options */
23361
23362 static void
23363 rs6000_function_specific_restore (struct gcc_options *opts,
23364                                   struct cl_target_option *ptr)
23365                                   
23366 {
23367   opts->x_rs6000_isa_flags = ptr->x_rs6000_isa_flags;
23368   opts->x_rs6000_isa_flags_explicit = ptr->x_rs6000_isa_flags_explicit;
23369   (void) rs6000_option_override_internal (false);
23370 }
23371
23372 /* Print the current options */
23373
23374 static void
23375 rs6000_function_specific_print (FILE *file, int indent,
23376                                 struct cl_target_option *ptr)
23377 {
23378   rs6000_print_isa_options (file, indent, "Isa options set",
23379                             ptr->x_rs6000_isa_flags);
23380
23381   rs6000_print_isa_options (file, indent, "Isa options explicit",
23382                             ptr->x_rs6000_isa_flags_explicit);
23383 }
23384
23385 /* Helper function to print the current isa or misc options on a line.  */
23386
23387 static void
23388 rs6000_print_options_internal (FILE *file,
23389                                int indent,
23390                                const char *string,
23391                                HOST_WIDE_INT flags,
23392                                const char *prefix,
23393                                const struct rs6000_opt_mask *opts,
23394                                size_t num_elements)
23395 {
23396   size_t i;
23397   size_t start_column = 0;
23398   size_t cur_column;
23399   size_t max_column = 120;
23400   size_t prefix_len = strlen (prefix);
23401   size_t comma_len = 0;
23402   const char *comma = "";
23403
23404   if (indent)
23405     start_column += fprintf (file, "%*s", indent, "");
23406
23407   if (!flags)
23408     {
23409       fprintf (stderr, DEBUG_FMT_S, string, "<none>");
23410       return;
23411     }
23412
23413   start_column += fprintf (stderr, DEBUG_FMT_WX, string, flags);
23414
23415   /* Print the various mask options.  */
23416   cur_column = start_column;
23417   for (i = 0; i < num_elements; i++)
23418     {
23419       bool invert = opts[i].invert;
23420       const char *name = opts[i].name;
23421       const char *no_str = "";
23422       HOST_WIDE_INT mask = opts[i].mask;
23423       size_t len = comma_len + prefix_len + strlen (name);
23424
23425       if (!invert)
23426         {
23427           if ((flags & mask) == 0)
23428             {
23429               no_str = "no-";
23430               len += sizeof ("no-") - 1;
23431             }
23432
23433           flags &= ~mask;
23434         }
23435
23436       else
23437         {
23438           if ((flags & mask) != 0)
23439             {
23440               no_str = "no-";
23441               len += sizeof ("no-") - 1;
23442             }
23443
23444           flags |= mask;
23445         }
23446
23447       cur_column += len;
23448       if (cur_column > max_column)
23449         {
23450           fprintf (stderr, ", \\\n%*s", (int)start_column, "");
23451           cur_column = start_column + len;
23452           comma = "";
23453         }
23454
23455       fprintf (file, "%s%s%s%s", comma, prefix, no_str, name);
23456       comma = ", ";
23457       comma_len = sizeof (", ") - 1;
23458     }
23459
23460   fputs ("\n", file);
23461 }
23462
23463 /* Helper function to print the current isa options on a line.  */
23464
23465 static void
23466 rs6000_print_isa_options (FILE *file, int indent, const char *string,
23467                           HOST_WIDE_INT flags)
23468 {
23469   rs6000_print_options_internal (file, indent, string, flags, "-m",
23470                                  &rs6000_opt_masks[0],
23471                                  ARRAY_SIZE (rs6000_opt_masks));
23472 }
23473
23474 static void
23475 rs6000_print_builtin_options (FILE *file, int indent, const char *string,
23476                               HOST_WIDE_INT flags)
23477 {
23478   rs6000_print_options_internal (file, indent, string, flags, "",
23479                                  &rs6000_builtin_mask_names[0],
23480                                  ARRAY_SIZE (rs6000_builtin_mask_names));
23481 }
23482
23483 /* If the user used -mno-vsx, we need turn off all of the implicit ISA 2.06,
23484    2.07, and 3.0 options that relate to the vector unit (-mdirect-move,
23485    -mupper-regs-df, etc.).
23486
23487    If the user used -mno-power8-vector, we need to turn off all of the implicit
23488    ISA 2.07 and 3.0 options that relate to the vector unit.
23489
23490    If the user used -mno-power9-vector, we need to turn off all of the implicit
23491    ISA 3.0 options that relate to the vector unit.
23492
23493    This function does not handle explicit options such as the user specifying
23494    -mdirect-move.  These are handled in rs6000_option_override_internal, and
23495    the appropriate error is given if needed.
23496
23497    We return a mask of all of the implicit options that should not be enabled
23498    by default.  */
23499
23500 static HOST_WIDE_INT
23501 rs6000_disable_incompatible_switches (void)
23502 {
23503   HOST_WIDE_INT ignore_masks = rs6000_isa_flags_explicit;
23504   size_t i, j;
23505
23506   static const struct {
23507     const HOST_WIDE_INT no_flag;        /* flag explicitly turned off.  */
23508     const HOST_WIDE_INT dep_flags;      /* flags that depend on this option.  */
23509     const char *const name;             /* name of the switch.  */
23510   } flags[] = {
23511     { OPTION_MASK_FUTURE,       OTHER_FUTURE_MASKS,     "future"        },
23512     { OPTION_MASK_P9_VECTOR,    OTHER_P9_VECTOR_MASKS,  "power9-vector" },
23513     { OPTION_MASK_P8_VECTOR,    OTHER_P8_VECTOR_MASKS,  "power8-vector" },
23514     { OPTION_MASK_VSX,          OTHER_VSX_VECTOR_MASKS, "vsx"           },
23515   };
23516
23517   for (i = 0; i < ARRAY_SIZE (flags); i++)
23518     {
23519       HOST_WIDE_INT no_flag = flags[i].no_flag;
23520
23521       if ((rs6000_isa_flags & no_flag) == 0
23522           && (rs6000_isa_flags_explicit & no_flag) != 0)
23523         {
23524           HOST_WIDE_INT dep_flags = flags[i].dep_flags;
23525           HOST_WIDE_INT set_flags = (rs6000_isa_flags_explicit
23526                                      & rs6000_isa_flags
23527                                      & dep_flags);
23528
23529           if (set_flags)
23530             {
23531               for (j = 0; j < ARRAY_SIZE (rs6000_opt_masks); j++)
23532                 if ((set_flags & rs6000_opt_masks[j].mask) != 0)
23533                   {
23534                     set_flags &= ~rs6000_opt_masks[j].mask;
23535                     error ("%<-mno-%s%> turns off %<-m%s%>",
23536                            flags[i].name,
23537                            rs6000_opt_masks[j].name);
23538                   }
23539
23540               gcc_assert (!set_flags);
23541             }
23542
23543           rs6000_isa_flags &= ~dep_flags;
23544           ignore_masks |= no_flag | dep_flags;
23545         }
23546     }
23547
23548   return ignore_masks;
23549 }
23550
23551 \f
23552 /* Helper function for printing the function name when debugging.  */
23553
23554 static const char *
23555 get_decl_name (tree fn)
23556 {
23557   tree name;
23558
23559   if (!fn)
23560     return "<null>";
23561
23562   name = DECL_NAME (fn);
23563   if (!name)
23564     return "<no-name>";
23565
23566   return IDENTIFIER_POINTER (name);
23567 }
23568
23569 /* Return the clone id of the target we are compiling code for in a target
23570    clone.  The clone id is ordered from 0 (default) to CLONE_MAX-1 and gives
23571    the priority list for the target clones (ordered from lowest to
23572    highest).  */
23573
23574 static int
23575 rs6000_clone_priority (tree fndecl)
23576 {
23577   tree fn_opts = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
23578   HOST_WIDE_INT isa_masks;
23579   int ret = CLONE_DEFAULT;
23580   tree attrs = lookup_attribute ("target", DECL_ATTRIBUTES (fndecl));
23581   const char *attrs_str = NULL;
23582
23583   attrs = TREE_VALUE (TREE_VALUE (attrs));
23584   attrs_str = TREE_STRING_POINTER (attrs);
23585
23586   /* Return priority zero for default function.  Return the ISA needed for the
23587      function if it is not the default.  */
23588   if (strcmp (attrs_str, "default") != 0)
23589     {
23590       if (fn_opts == NULL_TREE)
23591         fn_opts = target_option_default_node;
23592
23593       if (!fn_opts || !TREE_TARGET_OPTION (fn_opts))
23594         isa_masks = rs6000_isa_flags;
23595       else
23596         isa_masks = TREE_TARGET_OPTION (fn_opts)->x_rs6000_isa_flags;
23597
23598       for (ret = CLONE_MAX - 1; ret != 0; ret--)
23599         if ((rs6000_clone_map[ret].isa_mask & isa_masks) != 0)
23600           break;
23601     }
23602
23603   if (TARGET_DEBUG_TARGET)
23604     fprintf (stderr, "rs6000_get_function_version_priority (%s) => %d\n",
23605              get_decl_name (fndecl), ret);
23606
23607   return ret;
23608 }
23609
23610 /* This compares the priority of target features in function DECL1 and DECL2.
23611    It returns positive value if DECL1 is higher priority, negative value if
23612    DECL2 is higher priority and 0 if they are the same.  Note, priorities are
23613    ordered from lowest (CLONE_DEFAULT) to highest (currently CLONE_ISA_3_0).  */
23614
23615 static int
23616 rs6000_compare_version_priority (tree decl1, tree decl2)
23617 {
23618   int priority1 = rs6000_clone_priority (decl1);
23619   int priority2 = rs6000_clone_priority (decl2);
23620   int ret = priority1 - priority2;
23621
23622   if (TARGET_DEBUG_TARGET)
23623     fprintf (stderr, "rs6000_compare_version_priority (%s, %s) => %d\n",
23624              get_decl_name (decl1), get_decl_name (decl2), ret);
23625
23626   return ret;
23627 }
23628
23629 /* Make a dispatcher declaration for the multi-versioned function DECL.
23630    Calls to DECL function will be replaced with calls to the dispatcher
23631    by the front-end.  Returns the decl of the dispatcher function.  */
23632
23633 static tree
23634 rs6000_get_function_versions_dispatcher (void *decl)
23635 {
23636   tree fn = (tree) decl;
23637   struct cgraph_node *node = NULL;
23638   struct cgraph_node *default_node = NULL;
23639   struct cgraph_function_version_info *node_v = NULL;
23640   struct cgraph_function_version_info *first_v = NULL;
23641
23642   tree dispatch_decl = NULL;
23643
23644   struct cgraph_function_version_info *default_version_info = NULL;
23645   gcc_assert (fn != NULL && DECL_FUNCTION_VERSIONED (fn));
23646
23647   if (TARGET_DEBUG_TARGET)
23648     fprintf (stderr, "rs6000_get_function_versions_dispatcher (%s)\n",
23649              get_decl_name (fn));
23650
23651   node = cgraph_node::get (fn);
23652   gcc_assert (node != NULL);
23653
23654   node_v = node->function_version ();
23655   gcc_assert (node_v != NULL);
23656
23657   if (node_v->dispatcher_resolver != NULL)
23658     return node_v->dispatcher_resolver;
23659
23660   /* Find the default version and make it the first node.  */
23661   first_v = node_v;
23662   /* Go to the beginning of the chain.  */
23663   while (first_v->prev != NULL)
23664     first_v = first_v->prev;
23665
23666   default_version_info = first_v;
23667   while (default_version_info != NULL)
23668     {
23669       const tree decl2 = default_version_info->this_node->decl;
23670       if (is_function_default_version (decl2))
23671         break;
23672       default_version_info = default_version_info->next;
23673     }
23674
23675   /* If there is no default node, just return NULL.  */
23676   if (default_version_info == NULL)
23677     return NULL;
23678
23679   /* Make default info the first node.  */
23680   if (first_v != default_version_info)
23681     {
23682       default_version_info->prev->next = default_version_info->next;
23683       if (default_version_info->next)
23684         default_version_info->next->prev = default_version_info->prev;
23685       first_v->prev = default_version_info;
23686       default_version_info->next = first_v;
23687       default_version_info->prev = NULL;
23688     }
23689
23690   default_node = default_version_info->this_node;
23691
23692 #ifndef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
23693   error_at (DECL_SOURCE_LOCATION (default_node->decl),
23694             "%<target_clones%> attribute needs GLIBC (2.23 and newer) that "
23695             "exports hardware capability bits");
23696 #else
23697
23698   if (targetm.has_ifunc_p ())
23699     {
23700       struct cgraph_function_version_info *it_v = NULL;
23701       struct cgraph_node *dispatcher_node = NULL;
23702       struct cgraph_function_version_info *dispatcher_version_info = NULL;
23703
23704       /* Right now, the dispatching is done via ifunc.  */
23705       dispatch_decl = make_dispatcher_decl (default_node->decl);
23706
23707       dispatcher_node = cgraph_node::get_create (dispatch_decl);
23708       gcc_assert (dispatcher_node != NULL);
23709       dispatcher_node->dispatcher_function = 1;
23710       dispatcher_version_info
23711         = dispatcher_node->insert_new_function_version ();
23712       dispatcher_version_info->next = default_version_info;
23713       dispatcher_node->definition = 1;
23714
23715       /* Set the dispatcher for all the versions.  */
23716       it_v = default_version_info;
23717       while (it_v != NULL)
23718         {
23719           it_v->dispatcher_resolver = dispatch_decl;
23720           it_v = it_v->next;
23721         }
23722     }
23723   else
23724     {
23725       error_at (DECL_SOURCE_LOCATION (default_node->decl),
23726                 "multiversioning needs ifunc which is not supported "
23727                 "on this target");
23728     }
23729 #endif
23730
23731   return dispatch_decl;
23732 }
23733
23734 /* Make the resolver function decl to dispatch the versions of a multi-
23735    versioned function, DEFAULT_DECL.  Create an empty basic block in the
23736    resolver and store the pointer in EMPTY_BB.  Return the decl of the resolver
23737    function.  */
23738
23739 static tree
23740 make_resolver_func (const tree default_decl,
23741                     const tree dispatch_decl,
23742                     basic_block *empty_bb)
23743 {
23744   /* Make the resolver function static.  The resolver function returns
23745      void *.  */
23746   tree decl_name = clone_function_name (default_decl, "resolver");
23747   const char *resolver_name = IDENTIFIER_POINTER (decl_name);
23748   tree type = build_function_type_list (ptr_type_node, NULL_TREE);
23749   tree decl = build_fn_decl (resolver_name, type);
23750   SET_DECL_ASSEMBLER_NAME (decl, decl_name);
23751
23752   DECL_NAME (decl) = decl_name;
23753   TREE_USED (decl) = 1;
23754   DECL_ARTIFICIAL (decl) = 1;
23755   DECL_IGNORED_P (decl) = 0;
23756   TREE_PUBLIC (decl) = 0;
23757   DECL_UNINLINABLE (decl) = 1;
23758
23759   /* Resolver is not external, body is generated.  */
23760   DECL_EXTERNAL (decl) = 0;
23761   DECL_EXTERNAL (dispatch_decl) = 0;
23762
23763   DECL_CONTEXT (decl) = NULL_TREE;
23764   DECL_INITIAL (decl) = make_node (BLOCK);
23765   DECL_STATIC_CONSTRUCTOR (decl) = 0;
23766
23767   /* Build result decl and add to function_decl.  */
23768   tree t = build_decl (UNKNOWN_LOCATION, RESULT_DECL, NULL_TREE, ptr_type_node);
23769   DECL_CONTEXT (t) = decl;
23770   DECL_ARTIFICIAL (t) = 1;
23771   DECL_IGNORED_P (t) = 1;
23772   DECL_RESULT (decl) = t;
23773
23774   gimplify_function_tree (decl);
23775   push_cfun (DECL_STRUCT_FUNCTION (decl));
23776   *empty_bb = init_lowered_empty_function (decl, false,
23777                                            profile_count::uninitialized ());
23778
23779   cgraph_node::add_new_function (decl, true);
23780   symtab->call_cgraph_insertion_hooks (cgraph_node::get_create (decl));
23781
23782   pop_cfun ();
23783
23784   /* Mark dispatch_decl as "ifunc" with resolver as resolver_name.  */
23785   DECL_ATTRIBUTES (dispatch_decl)
23786     = make_attribute ("ifunc", resolver_name, DECL_ATTRIBUTES (dispatch_decl));
23787
23788   cgraph_node::create_same_body_alias (dispatch_decl, decl);
23789
23790   return decl;
23791 }
23792
23793 /* This adds a condition to the basic_block NEW_BB in function FUNCTION_DECL to
23794    return a pointer to VERSION_DECL if we are running on a machine that
23795    supports the index CLONE_ISA hardware architecture bits.  This function will
23796    be called during version dispatch to decide which function version to
23797    execute.  It returns the basic block at the end, to which more conditions
23798    can be added.  */
23799
23800 static basic_block
23801 add_condition_to_bb (tree function_decl, tree version_decl,
23802                      int clone_isa, basic_block new_bb)
23803 {
23804   push_cfun (DECL_STRUCT_FUNCTION (function_decl));
23805
23806   gcc_assert (new_bb != NULL);
23807   gimple_seq gseq = bb_seq (new_bb);
23808
23809
23810   tree convert_expr = build1 (CONVERT_EXPR, ptr_type_node,
23811                               build_fold_addr_expr (version_decl));
23812   tree result_var = create_tmp_var (ptr_type_node);
23813   gimple *convert_stmt = gimple_build_assign (result_var, convert_expr);
23814   gimple *return_stmt = gimple_build_return (result_var);
23815
23816   if (clone_isa == CLONE_DEFAULT)
23817     {
23818       gimple_seq_add_stmt (&gseq, convert_stmt);
23819       gimple_seq_add_stmt (&gseq, return_stmt);
23820       set_bb_seq (new_bb, gseq);
23821       gimple_set_bb (convert_stmt, new_bb);
23822       gimple_set_bb (return_stmt, new_bb);
23823       pop_cfun ();
23824       return new_bb;
23825     }
23826
23827   tree bool_zero = build_int_cst (bool_int_type_node, 0);
23828   tree cond_var = create_tmp_var (bool_int_type_node);
23829   tree predicate_decl = rs6000_builtin_decls [(int) RS6000_BUILTIN_CPU_SUPPORTS];
23830   const char *arg_str = rs6000_clone_map[clone_isa].name;
23831   tree predicate_arg = build_string_literal (strlen (arg_str) + 1, arg_str);
23832   gimple *call_cond_stmt = gimple_build_call (predicate_decl, 1, predicate_arg);
23833   gimple_call_set_lhs (call_cond_stmt, cond_var);
23834
23835   gimple_set_block (call_cond_stmt, DECL_INITIAL (function_decl));
23836   gimple_set_bb (call_cond_stmt, new_bb);
23837   gimple_seq_add_stmt (&gseq, call_cond_stmt);
23838
23839   gimple *if_else_stmt = gimple_build_cond (NE_EXPR, cond_var, bool_zero,
23840                                             NULL_TREE, NULL_TREE);
23841   gimple_set_block (if_else_stmt, DECL_INITIAL (function_decl));
23842   gimple_set_bb (if_else_stmt, new_bb);
23843   gimple_seq_add_stmt (&gseq, if_else_stmt);
23844
23845   gimple_seq_add_stmt (&gseq, convert_stmt);
23846   gimple_seq_add_stmt (&gseq, return_stmt);
23847   set_bb_seq (new_bb, gseq);
23848
23849   basic_block bb1 = new_bb;
23850   edge e12 = split_block (bb1, if_else_stmt);
23851   basic_block bb2 = e12->dest;
23852   e12->flags &= ~EDGE_FALLTHRU;
23853   e12->flags |= EDGE_TRUE_VALUE;
23854
23855   edge e23 = split_block (bb2, return_stmt);
23856   gimple_set_bb (convert_stmt, bb2);
23857   gimple_set_bb (return_stmt, bb2);
23858
23859   basic_block bb3 = e23->dest;
23860   make_edge (bb1, bb3, EDGE_FALSE_VALUE);
23861
23862   remove_edge (e23);
23863   make_edge (bb2, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
23864
23865   pop_cfun ();
23866   return bb3;
23867 }
23868
23869 /* This function generates the dispatch function for multi-versioned functions.
23870    DISPATCH_DECL is the function which will contain the dispatch logic.
23871    FNDECLS are the function choices for dispatch, and is a tree chain.
23872    EMPTY_BB is the basic block pointer in DISPATCH_DECL in which the dispatch
23873    code is generated.  */
23874
23875 static int
23876 dispatch_function_versions (tree dispatch_decl,
23877                             void *fndecls_p,
23878                             basic_block *empty_bb)
23879 {
23880   int ix;
23881   tree ele;
23882   vec<tree> *fndecls;
23883   tree clones[CLONE_MAX];
23884
23885   if (TARGET_DEBUG_TARGET)
23886     fputs ("dispatch_function_versions, top\n", stderr);
23887
23888   gcc_assert (dispatch_decl != NULL
23889               && fndecls_p != NULL
23890               && empty_bb != NULL);
23891
23892   /* fndecls_p is actually a vector.  */
23893   fndecls = static_cast<vec<tree> *> (fndecls_p);
23894
23895   /* At least one more version other than the default.  */
23896   gcc_assert (fndecls->length () >= 2);
23897
23898   /* The first version in the vector is the default decl.  */
23899   memset ((void *) clones, '\0', sizeof (clones));
23900   clones[CLONE_DEFAULT] = (*fndecls)[0];
23901
23902   /* On the PowerPC, we do not need to call __builtin_cpu_init, which is a NOP
23903      on the PowerPC (on the x86_64, it is not a NOP).  The builtin function
23904      __builtin_cpu_support ensures that the TOC fields are setup by requiring a
23905      recent glibc.  If we ever need to call __builtin_cpu_init, we would need
23906      to insert the code here to do the call.  */
23907
23908   for (ix = 1; fndecls->iterate (ix, &ele); ++ix)
23909     {
23910       int priority = rs6000_clone_priority (ele);
23911       if (!clones[priority])
23912         clones[priority] = ele;
23913     }
23914
23915   for (ix = CLONE_MAX - 1; ix >= 0; ix--)
23916     if (clones[ix])
23917       {
23918         if (TARGET_DEBUG_TARGET)
23919           fprintf (stderr, "dispatch_function_versions, clone %d, %s\n",
23920                    ix, get_decl_name (clones[ix]));
23921
23922         *empty_bb = add_condition_to_bb (dispatch_decl, clones[ix], ix,
23923                                          *empty_bb);
23924       }
23925
23926   return 0;
23927 }
23928
23929 /* Generate the dispatching code body to dispatch multi-versioned function
23930    DECL.  The target hook is called to process the "target" attributes and
23931    provide the code to dispatch the right function at run-time.  NODE points
23932    to the dispatcher decl whose body will be created.  */
23933
23934 static tree
23935 rs6000_generate_version_dispatcher_body (void *node_p)
23936 {
23937   tree resolver;
23938   basic_block empty_bb;
23939   struct cgraph_node *node = (cgraph_node *) node_p;
23940   struct cgraph_function_version_info *ninfo = node->function_version ();
23941
23942   if (ninfo->dispatcher_resolver)
23943     return ninfo->dispatcher_resolver;
23944
23945   /* node is going to be an alias, so remove the finalized bit.  */
23946   node->definition = false;
23947
23948   /* The first version in the chain corresponds to the default version.  */
23949   ninfo->dispatcher_resolver = resolver
23950     = make_resolver_func (ninfo->next->this_node->decl, node->decl, &empty_bb);
23951
23952   if (TARGET_DEBUG_TARGET)
23953     fprintf (stderr, "rs6000_get_function_versions_dispatcher, %s\n",
23954              get_decl_name (resolver));
23955
23956   push_cfun (DECL_STRUCT_FUNCTION (resolver));
23957   auto_vec<tree, 2> fn_ver_vec;
23958
23959   for (struct cgraph_function_version_info *vinfo = ninfo->next;
23960        vinfo;
23961        vinfo = vinfo->next)
23962     {
23963       struct cgraph_node *version = vinfo->this_node;
23964       /* Check for virtual functions here again, as by this time it should
23965          have been determined if this function needs a vtable index or
23966          not.  This happens for methods in derived classes that override
23967          virtual methods in base classes but are not explicitly marked as
23968          virtual.  */
23969       if (DECL_VINDEX (version->decl))
23970         sorry ("Virtual function multiversioning not supported");
23971
23972       fn_ver_vec.safe_push (version->decl);
23973     }
23974
23975   dispatch_function_versions (resolver, &fn_ver_vec, &empty_bb);
23976   cgraph_edge::rebuild_edges ();
23977   pop_cfun ();
23978   return resolver;
23979 }
23980
23981 \f
23982 /* Hook to determine if one function can safely inline another.  */
23983
23984 static bool
23985 rs6000_can_inline_p (tree caller, tree callee)
23986 {
23987   bool ret = false;
23988   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
23989   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
23990
23991   /* If callee has no option attributes, then it is ok to inline.  */
23992   if (!callee_tree)
23993     ret = true;
23994
23995   /* If caller has no option attributes, but callee does then it is not ok to
23996      inline.  */
23997   else if (!caller_tree)
23998     ret = false;
23999
24000   else
24001     {
24002       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
24003       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
24004
24005       /* Callee's options should a subset of the caller's, i.e. a vsx function
24006          can inline an altivec function but a non-vsx function can't inline a
24007          vsx function.  */
24008       if ((caller_opts->x_rs6000_isa_flags & callee_opts->x_rs6000_isa_flags)
24009           == callee_opts->x_rs6000_isa_flags)
24010         ret = true;
24011     }
24012
24013   if (TARGET_DEBUG_TARGET)
24014     fprintf (stderr, "rs6000_can_inline_p:, caller %s, callee %s, %s inline\n",
24015              get_decl_name (caller), get_decl_name (callee),
24016              (ret ? "can" : "cannot"));
24017
24018   return ret;
24019 }
24020 \f
24021 /* Allocate a stack temp and fixup the address so it meets the particular
24022    memory requirements (either offetable or REG+REG addressing).  */
24023
24024 rtx
24025 rs6000_allocate_stack_temp (machine_mode mode,
24026                             bool offsettable_p,
24027                             bool reg_reg_p)
24028 {
24029   rtx stack = assign_stack_temp (mode, GET_MODE_SIZE (mode));
24030   rtx addr = XEXP (stack, 0);
24031   int strict_p = reload_completed;
24032
24033   if (!legitimate_indirect_address_p (addr, strict_p))
24034     {
24035       if (offsettable_p
24036           && !rs6000_legitimate_offset_address_p (mode, addr, strict_p, true))
24037         stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
24038
24039       else if (reg_reg_p && !legitimate_indexed_address_p (addr, strict_p))
24040         stack = replace_equiv_address (stack, copy_addr_to_reg (addr));
24041     }
24042
24043   return stack;
24044 }
24045
24046 /* Given a memory reference, if it is not a reg or reg+reg addressing,
24047    convert to such a form to deal with memory reference instructions
24048    like STFIWX and LDBRX that only take reg+reg addressing.  */
24049
24050 rtx
24051 rs6000_force_indexed_or_indirect_mem (rtx x)
24052 {
24053   machine_mode mode = GET_MODE (x);
24054
24055   gcc_assert (MEM_P (x));
24056   if (can_create_pseudo_p () && !indexed_or_indirect_operand (x, mode))
24057     {
24058       rtx addr = XEXP (x, 0);
24059       if (GET_CODE (addr) == PRE_INC || GET_CODE (addr) == PRE_DEC)
24060         {
24061           rtx reg = XEXP (addr, 0);
24062           HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (x));
24063           rtx size_rtx = GEN_INT ((GET_CODE (addr) == PRE_DEC) ? -size : size);
24064           gcc_assert (REG_P (reg));
24065           emit_insn (gen_add3_insn (reg, reg, size_rtx));
24066           addr = reg;
24067         }
24068       else if (GET_CODE (addr) == PRE_MODIFY)
24069         {
24070           rtx reg = XEXP (addr, 0);
24071           rtx expr = XEXP (addr, 1);
24072           gcc_assert (REG_P (reg));
24073           gcc_assert (GET_CODE (expr) == PLUS);
24074           emit_insn (gen_add3_insn (reg, XEXP (expr, 0), XEXP (expr, 1)));
24075           addr = reg;
24076         }
24077
24078       if (GET_CODE (addr) == PLUS)
24079         {
24080           rtx op0 = XEXP (addr, 0);
24081           rtx op1 = XEXP (addr, 1);
24082           op0 = force_reg (Pmode, op0);
24083           op1 = force_reg (Pmode, op1);
24084           x = replace_equiv_address (x, gen_rtx_PLUS (Pmode, op0, op1));
24085         }
24086       else
24087         x = replace_equiv_address (x, force_reg (Pmode, addr));
24088     }
24089
24090   return x;
24091 }
24092
24093 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
24094
24095    On the RS/6000, all integer constants are acceptable, most won't be valid
24096    for particular insns, though.  Only easy FP constants are acceptable.  */
24097
24098 static bool
24099 rs6000_legitimate_constant_p (machine_mode mode, rtx x)
24100 {
24101   if (TARGET_ELF && tls_referenced_p (x))
24102     return false;
24103
24104   if (CONST_DOUBLE_P (x))
24105     return easy_fp_constant (x, mode);
24106
24107   if (GET_CODE (x) == CONST_VECTOR)
24108     return easy_vector_constant (x, mode);
24109
24110   return true;
24111 }
24112
24113 \f
24114 /* Return TRUE iff the sequence ending in LAST sets the static chain.  */
24115
24116 static bool
24117 chain_already_loaded (rtx_insn *last)
24118 {
24119   for (; last != NULL; last = PREV_INSN (last))
24120     {
24121       if (NONJUMP_INSN_P (last))
24122         {
24123           rtx patt = PATTERN (last);
24124
24125           if (GET_CODE (patt) == SET)
24126             {
24127               rtx lhs = XEXP (patt, 0);
24128
24129               if (REG_P (lhs) && REGNO (lhs) == STATIC_CHAIN_REGNUM)
24130                 return true;
24131             }
24132         }
24133     }
24134   return false;
24135 }
24136
24137 /* Expand code to perform a call under the AIX or ELFv2 ABI.  */
24138
24139 void
24140 rs6000_call_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
24141 {
24142   rtx func = func_desc;
24143   rtx toc_reg = gen_rtx_REG (Pmode, TOC_REGNUM);
24144   rtx toc_load = NULL_RTX;
24145   rtx toc_restore = NULL_RTX;
24146   rtx func_addr;
24147   rtx abi_reg = NULL_RTX;
24148   rtx call[4];
24149   int n_call;
24150   rtx insn;
24151   bool is_pltseq_longcall;
24152
24153   if (global_tlsarg)
24154     tlsarg = global_tlsarg;
24155
24156   /* Handle longcall attributes.  */
24157   is_pltseq_longcall = false;
24158   if ((INTVAL (cookie) & CALL_LONG) != 0
24159       && GET_CODE (func_desc) == SYMBOL_REF)
24160     {
24161       func = rs6000_longcall_ref (func_desc, tlsarg);
24162       if (TARGET_PLTSEQ)
24163         is_pltseq_longcall = true;
24164     }
24165
24166   /* Handle indirect calls.  */
24167   if (!SYMBOL_REF_P (func)
24168       || (DEFAULT_ABI == ABI_AIX && !SYMBOL_REF_FUNCTION_P (func)))
24169     {
24170       if (!rs6000_pcrel_p (cfun))
24171         {
24172           /* Save the TOC into its reserved slot before the call,
24173              and prepare to restore it after the call.  */
24174           rtx stack_toc_offset = GEN_INT (RS6000_TOC_SAVE_SLOT);
24175           rtx stack_toc_unspec = gen_rtx_UNSPEC (Pmode,
24176                                                  gen_rtvec (1, stack_toc_offset),
24177                                                  UNSPEC_TOCSLOT);
24178           toc_restore = gen_rtx_SET (toc_reg, stack_toc_unspec);
24179
24180           /* Can we optimize saving the TOC in the prologue or
24181              do we need to do it at every call?  */
24182           if (TARGET_SAVE_TOC_INDIRECT && !cfun->calls_alloca)
24183             cfun->machine->save_toc_in_prologue = true;
24184           else
24185             {
24186               rtx stack_ptr = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
24187               rtx stack_toc_mem = gen_frame_mem (Pmode,
24188                                                  gen_rtx_PLUS (Pmode, stack_ptr,
24189                                                                stack_toc_offset));
24190               MEM_VOLATILE_P (stack_toc_mem) = 1;
24191               if (is_pltseq_longcall)
24192                 {
24193                   rtvec v = gen_rtvec (3, toc_reg, func_desc, tlsarg);
24194                   rtx mark_toc_reg = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24195                   emit_insn (gen_rtx_SET (stack_toc_mem, mark_toc_reg));
24196                 }
24197               else
24198                 emit_move_insn (stack_toc_mem, toc_reg);
24199             }
24200         }
24201
24202       if (DEFAULT_ABI == ABI_ELFv2)
24203         {
24204           /* A function pointer in the ELFv2 ABI is just a plain address, but
24205              the ABI requires it to be loaded into r12 before the call.  */
24206           func_addr = gen_rtx_REG (Pmode, 12);
24207           if (!rtx_equal_p (func_addr, func))
24208             emit_move_insn (func_addr, func);
24209           abi_reg = func_addr;
24210           /* Indirect calls via CTR are strongly preferred over indirect
24211              calls via LR, so move the address there.  Needed to mark
24212              this insn for linker plt sequence editing too.  */
24213           func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
24214           if (is_pltseq_longcall)
24215             {
24216               rtvec v = gen_rtvec (3, abi_reg, func_desc, tlsarg);
24217               rtx mark_func = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24218               emit_insn (gen_rtx_SET (func_addr, mark_func));
24219               v = gen_rtvec (2, func_addr, func_desc);
24220               func_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24221             }
24222           else
24223             emit_move_insn (func_addr, abi_reg);
24224         }
24225       else
24226         {
24227           /* A function pointer under AIX is a pointer to a data area whose
24228              first word contains the actual address of the function, whose
24229              second word contains a pointer to its TOC, and whose third word
24230              contains a value to place in the static chain register (r11).
24231              Note that if we load the static chain, our "trampoline" need
24232              not have any executable code.  */
24233
24234           /* Load up address of the actual function.  */
24235           func = force_reg (Pmode, func);
24236           func_addr = gen_reg_rtx (Pmode);
24237           emit_move_insn (func_addr, gen_rtx_MEM (Pmode, func));
24238
24239           /* Indirect calls via CTR are strongly preferred over indirect
24240              calls via LR, so move the address there.  */
24241           rtx ctr_reg = gen_rtx_REG (Pmode, CTR_REGNO);
24242           emit_move_insn (ctr_reg, func_addr);
24243           func_addr = ctr_reg;
24244
24245           /* Prepare to load the TOC of the called function.  Note that the
24246              TOC load must happen immediately before the actual call so
24247              that unwinding the TOC registers works correctly.  See the
24248              comment in frob_update_context.  */
24249           rtx func_toc_offset = GEN_INT (GET_MODE_SIZE (Pmode));
24250           rtx func_toc_mem = gen_rtx_MEM (Pmode,
24251                                           gen_rtx_PLUS (Pmode, func,
24252                                                         func_toc_offset));
24253           toc_load = gen_rtx_USE (VOIDmode, func_toc_mem);
24254
24255           /* If we have a static chain, load it up.  But, if the call was
24256              originally direct, the 3rd word has not been written since no
24257              trampoline has been built, so we ought not to load it, lest we
24258              override a static chain value.  */
24259           if (!(GET_CODE (func_desc) == SYMBOL_REF
24260                 && SYMBOL_REF_FUNCTION_P (func_desc))
24261               && TARGET_POINTERS_TO_NESTED_FUNCTIONS
24262               && !chain_already_loaded (get_current_sequence ()->next->last))
24263             {
24264               rtx sc_reg = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
24265               rtx func_sc_offset = GEN_INT (2 * GET_MODE_SIZE (Pmode));
24266               rtx func_sc_mem = gen_rtx_MEM (Pmode,
24267                                              gen_rtx_PLUS (Pmode, func,
24268                                                            func_sc_offset));
24269               emit_move_insn (sc_reg, func_sc_mem);
24270               abi_reg = sc_reg;
24271             }
24272         }
24273     }
24274   else
24275     {
24276       /* No TOC register needed for calls from PC-relative callers.  */
24277       if (!rs6000_pcrel_p (cfun))
24278         /* Direct calls use the TOC: for local calls, the callee will
24279            assume the TOC register is set; for non-local calls, the
24280            PLT stub needs the TOC register.  */
24281         abi_reg = toc_reg;
24282       func_addr = func;
24283     }
24284
24285   /* Create the call.  */
24286   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
24287   if (value != NULL_RTX)
24288     call[0] = gen_rtx_SET (value, call[0]);
24289   n_call = 1;
24290
24291   if (toc_load)
24292     call[n_call++] = toc_load;
24293   if (toc_restore)
24294     call[n_call++] = toc_restore;
24295
24296   call[n_call++] = gen_hard_reg_clobber (Pmode, LR_REGNO);
24297
24298   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n_call, call));
24299   insn = emit_call_insn (insn);
24300
24301   /* Mention all registers defined by the ABI to hold information
24302      as uses in CALL_INSN_FUNCTION_USAGE.  */
24303   if (abi_reg)
24304     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
24305 }
24306
24307 /* Expand code to perform a sibling call under the AIX or ELFv2 ABI.  */
24308
24309 void
24310 rs6000_sibcall_aix (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
24311 {
24312   rtx call[2];
24313   rtx insn;
24314
24315   gcc_assert (INTVAL (cookie) == 0);
24316
24317   if (global_tlsarg)
24318     tlsarg = global_tlsarg;
24319
24320   /* Create the call.  */
24321   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), tlsarg);
24322   if (value != NULL_RTX)
24323     call[0] = gen_rtx_SET (value, call[0]);
24324
24325   call[1] = simple_return_rtx;
24326
24327   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (2, call));
24328   insn = emit_call_insn (insn);
24329
24330   /* Note use of the TOC register.  */
24331   if (!rs6000_pcrel_p (cfun))
24332     use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
24333              gen_rtx_REG (Pmode, TOC_REGNUM));
24334 }
24335
24336 /* Expand code to perform a call under the SYSV4 ABI.  */
24337
24338 void
24339 rs6000_call_sysv (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
24340 {
24341   rtx func = func_desc;
24342   rtx func_addr;
24343   rtx call[4];
24344   rtx insn;
24345   rtx abi_reg = NULL_RTX;
24346   int n;
24347
24348   if (global_tlsarg)
24349     tlsarg = global_tlsarg;
24350
24351   /* Handle longcall attributes.  */
24352   if ((INTVAL (cookie) & CALL_LONG) != 0
24353       && GET_CODE (func_desc) == SYMBOL_REF)
24354     {
24355       func = rs6000_longcall_ref (func_desc, tlsarg);
24356       /* If the longcall was implemented as an inline PLT call using
24357          PLT unspecs then func will be REG:r11.  If not, func will be
24358          a pseudo reg.  The inline PLT call sequence supports lazy
24359          linking (and longcalls to functions in dlopen'd libraries).
24360          The other style of longcalls don't.  The lazy linking entry
24361          to the dynamic symbol resolver requires r11 be the function
24362          address (as it is for linker generated PLT stubs).  Ensure
24363          r11 stays valid to the bctrl by marking r11 used by the call.  */
24364       if (TARGET_PLTSEQ)
24365         abi_reg = func;
24366     }
24367
24368   /* Handle indirect calls.  */
24369   if (GET_CODE (func) != SYMBOL_REF)
24370     {
24371       func = force_reg (Pmode, func);
24372
24373       /* Indirect calls via CTR are strongly preferred over indirect
24374          calls via LR, so move the address there.  That can't be left
24375          to reload because we want to mark every instruction in an
24376          inline PLT call sequence with a reloc, enabling the linker to
24377          edit the sequence back to a direct call when that makes sense.  */
24378       func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
24379       if (abi_reg)
24380         {
24381           rtvec v = gen_rtvec (3, func, func_desc, tlsarg);
24382           rtx mark_func = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24383           emit_insn (gen_rtx_SET (func_addr, mark_func));
24384           v = gen_rtvec (2, func_addr, func_desc);
24385           func_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24386         }
24387       else
24388         emit_move_insn (func_addr, func);
24389     }
24390   else
24391     func_addr = func;
24392
24393   /* Create the call.  */
24394   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
24395   if (value != NULL_RTX)
24396     call[0] = gen_rtx_SET (value, call[0]);
24397
24398   call[1] = gen_rtx_USE (VOIDmode, cookie);
24399   n = 2;
24400   if (TARGET_SECURE_PLT
24401       && flag_pic
24402       && GET_CODE (func_addr) == SYMBOL_REF
24403       && !SYMBOL_REF_LOCAL_P (func_addr))
24404     call[n++] = gen_rtx_USE (VOIDmode, pic_offset_table_rtx);
24405
24406   call[n++] = gen_hard_reg_clobber (Pmode, LR_REGNO);
24407
24408   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (n, call));
24409   insn = emit_call_insn (insn);
24410   if (abi_reg)
24411     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
24412 }
24413
24414 /* Expand code to perform a sibling call under the SysV4 ABI.  */
24415
24416 void
24417 rs6000_sibcall_sysv (rtx value, rtx func_desc, rtx tlsarg, rtx cookie)
24418 {
24419   rtx func = func_desc;
24420   rtx func_addr;
24421   rtx call[3];
24422   rtx insn;
24423   rtx abi_reg = NULL_RTX;
24424
24425   if (global_tlsarg)
24426     tlsarg = global_tlsarg;
24427
24428   /* Handle longcall attributes.  */
24429   if ((INTVAL (cookie) & CALL_LONG) != 0
24430       && GET_CODE (func_desc) == SYMBOL_REF)
24431     {
24432       func = rs6000_longcall_ref (func_desc, tlsarg);
24433       /* If the longcall was implemented as an inline PLT call using
24434          PLT unspecs then func will be REG:r11.  If not, func will be
24435          a pseudo reg.  The inline PLT call sequence supports lazy
24436          linking (and longcalls to functions in dlopen'd libraries).
24437          The other style of longcalls don't.  The lazy linking entry
24438          to the dynamic symbol resolver requires r11 be the function
24439          address (as it is for linker generated PLT stubs).  Ensure
24440          r11 stays valid to the bctr by marking r11 used by the call.  */
24441       if (TARGET_PLTSEQ)
24442         abi_reg = func;
24443     }
24444
24445   /* Handle indirect calls.  */
24446   if (GET_CODE (func) != SYMBOL_REF)
24447     {
24448       func = force_reg (Pmode, func);
24449
24450       /* Indirect sibcalls must go via CTR.  That can't be left to
24451          reload because we want to mark every instruction in an inline
24452          PLT call sequence with a reloc, enabling the linker to edit
24453          the sequence back to a direct call when that makes sense.  */
24454       func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
24455       if (abi_reg)
24456         {
24457           rtvec v = gen_rtvec (3, func, func_desc, tlsarg);
24458           rtx mark_func = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24459           emit_insn (gen_rtx_SET (func_addr, mark_func));
24460           v = gen_rtvec (2, func_addr, func_desc);
24461           func_addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_PLTSEQ);
24462         }
24463       else
24464         emit_move_insn (func_addr, func);
24465     }
24466   else
24467     func_addr = func;
24468
24469   /* Create the call.  */
24470   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
24471   if (value != NULL_RTX)
24472     call[0] = gen_rtx_SET (value, call[0]);
24473
24474   call[1] = gen_rtx_USE (VOIDmode, cookie);
24475   call[2] = simple_return_rtx;
24476
24477   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (3, call));
24478   insn = emit_call_insn (insn);
24479   if (abi_reg)
24480     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), abi_reg);
24481 }
24482
24483 #if TARGET_MACHO
24484
24485 /* Expand code to perform a call under the Darwin ABI.
24486    Modulo handling of mlongcall, this is much the same as sysv.
24487    if/when the longcall optimisation is removed, we could drop this
24488    code and use the sysv case (taking care to avoid the tls stuff).
24489
24490    We can use this for sibcalls too, if needed.  */
24491
24492 void
24493 rs6000_call_darwin_1 (rtx value, rtx func_desc, rtx tlsarg,
24494                       rtx cookie, bool sibcall)
24495 {
24496   rtx func = func_desc;
24497   rtx func_addr;
24498   rtx call[3];
24499   rtx insn;
24500   int cookie_val = INTVAL (cookie);
24501   bool make_island = false;
24502
24503   /* Handle longcall attributes, there are two cases for Darwin:
24504      1) Newer linkers are capable of synthesising any branch islands needed.
24505      2) We need a helper branch island synthesised by the compiler.
24506      The second case has mostly been retired and we don't use it for m64.
24507      In fact, it's is an optimisation, we could just indirect as sysv does..
24508      ... however, backwards compatibility for now.
24509      If we're going to use this, then we need to keep the CALL_LONG bit set,
24510      so that we can pick up the special insn form later.  */
24511   if ((cookie_val & CALL_LONG) != 0
24512       && GET_CODE (func_desc) == SYMBOL_REF)
24513     {
24514       /* FIXME: the longcall opt should not hang off this flag, it is most
24515          likely incorrect for kernel-mode code-generation.  */
24516       if (darwin_symbol_stubs && TARGET_32BIT)
24517         make_island = true; /* Do nothing yet, retain the CALL_LONG flag.  */
24518       else
24519         {
24520           /* The linker is capable of doing this, but the user explicitly
24521              asked for -mlongcall, so we'll do the 'normal' version.  */
24522           func = rs6000_longcall_ref (func_desc, NULL_RTX);
24523           cookie_val &= ~CALL_LONG; /* Handled, zap it.  */
24524         }
24525     }
24526
24527   /* Handle indirect calls.  */
24528   if (GET_CODE (func) != SYMBOL_REF)
24529     {
24530       func = force_reg (Pmode, func);
24531
24532       /* Indirect calls via CTR are strongly preferred over indirect
24533          calls via LR, and are required for indirect sibcalls, so move
24534          the address there.   */
24535       func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
24536       emit_move_insn (func_addr, func);
24537     }
24538   else
24539     func_addr = func;
24540
24541   /* Create the call.  */
24542   call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
24543   if (value != NULL_RTX)
24544     call[0] = gen_rtx_SET (value, call[0]);
24545
24546   call[1] = gen_rtx_USE (VOIDmode, GEN_INT (cookie_val));
24547
24548   if (sibcall)
24549     call[2] = simple_return_rtx;
24550   else
24551     call[2] = gen_hard_reg_clobber (Pmode, LR_REGNO);
24552
24553   insn = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (3, call));
24554   insn = emit_call_insn (insn);
24555   /* Now we have the debug info in the insn, we can set up the branch island
24556      if we're using one.  */
24557   if (make_island)
24558     {
24559       tree funname = get_identifier (XSTR (func_desc, 0));
24560
24561       if (no_previous_def (funname))
24562         {
24563           rtx label_rtx = gen_label_rtx ();
24564           char *label_buf, temp_buf[256];
24565           ASM_GENERATE_INTERNAL_LABEL (temp_buf, "L",
24566                                        CODE_LABEL_NUMBER (label_rtx));
24567           label_buf = temp_buf[0] == '*' ? temp_buf + 1 : temp_buf;
24568           tree labelname = get_identifier (label_buf);
24569           add_compiler_branch_island (labelname, funname,
24570                                      insn_line ((const rtx_insn*)insn));
24571         }
24572      }
24573 }
24574 #endif
24575
24576 void
24577 rs6000_call_darwin (rtx value ATTRIBUTE_UNUSED, rtx func_desc ATTRIBUTE_UNUSED,
24578                     rtx tlsarg ATTRIBUTE_UNUSED, rtx cookie ATTRIBUTE_UNUSED)
24579 {
24580 #if TARGET_MACHO
24581   rs6000_call_darwin_1 (value, func_desc, tlsarg, cookie, false);
24582 #else
24583   gcc_unreachable();
24584 #endif
24585 }
24586
24587
24588 void
24589 rs6000_sibcall_darwin (rtx value ATTRIBUTE_UNUSED, rtx func_desc ATTRIBUTE_UNUSED,
24590                        rtx tlsarg ATTRIBUTE_UNUSED, rtx cookie ATTRIBUTE_UNUSED)
24591 {
24592 #if TARGET_MACHO
24593   rs6000_call_darwin_1 (value, func_desc, tlsarg, cookie, true);
24594 #else
24595   gcc_unreachable();
24596 #endif
24597 }
24598
24599 /* Return whether we should generate PC-relative code for FNDECL.  */
24600 bool
24601 rs6000_fndecl_pcrel_p (const_tree fndecl)
24602 {
24603   if (DEFAULT_ABI != ABI_ELFv2)
24604     return false;
24605
24606   struct cl_target_option *opts = target_opts_for_fn (fndecl);
24607
24608   return ((opts->x_rs6000_isa_flags & OPTION_MASK_PCREL) != 0
24609           && TARGET_CMODEL == CMODEL_MEDIUM);
24610 }
24611
24612 /* Return whether we should generate PC-relative code for *FN.  */
24613 bool
24614 rs6000_pcrel_p (struct function *fn)
24615 {
24616   if (DEFAULT_ABI != ABI_ELFv2)
24617     return false;
24618
24619   /* Optimize usual case.  */
24620   if (fn == cfun)
24621     return ((rs6000_isa_flags & OPTION_MASK_PCREL) != 0
24622             && TARGET_CMODEL == CMODEL_MEDIUM);
24623
24624   return rs6000_fndecl_pcrel_p (fn->decl);
24625 }
24626
24627 #ifdef HAVE_GAS_HIDDEN
24628 # define USE_HIDDEN_LINKONCE 1
24629 #else
24630 # define USE_HIDDEN_LINKONCE 0
24631 #endif
24632
24633 /* Fills in the label name that should be used for a 476 link stack thunk.  */
24634
24635 void
24636 get_ppc476_thunk_name (char name[32])
24637 {
24638   gcc_assert (TARGET_LINK_STACK);
24639
24640   if (USE_HIDDEN_LINKONCE)
24641     sprintf (name, "__ppc476.get_thunk");
24642   else
24643     ASM_GENERATE_INTERNAL_LABEL (name, "LPPC476_", 0);
24644 }
24645
24646 /* This function emits the simple thunk routine that is used to preserve
24647    the link stack on the 476 cpu.  */
24648
24649 static void rs6000_code_end (void) ATTRIBUTE_UNUSED;
24650 static void
24651 rs6000_code_end (void)
24652 {
24653   char name[32];
24654   tree decl;
24655
24656   if (!TARGET_LINK_STACK)
24657     return;
24658
24659   get_ppc476_thunk_name (name);
24660
24661   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, get_identifier (name),
24662                      build_function_type_list (void_type_node, NULL_TREE));
24663   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
24664                                    NULL_TREE, void_type_node);
24665   TREE_PUBLIC (decl) = 1;
24666   TREE_STATIC (decl) = 1;
24667
24668 #if RS6000_WEAK
24669   if (USE_HIDDEN_LINKONCE && !TARGET_XCOFF)
24670     {
24671       cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
24672       targetm.asm_out.unique_section (decl, 0);
24673       switch_to_section (get_named_section (decl, NULL, 0));
24674       DECL_WEAK (decl) = 1;
24675       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
24676       targetm.asm_out.globalize_label (asm_out_file, name);
24677       targetm.asm_out.assemble_visibility (decl, VISIBILITY_HIDDEN);
24678       ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
24679     }
24680   else
24681 #endif
24682     {
24683       switch_to_section (text_section);
24684       ASM_OUTPUT_LABEL (asm_out_file, name);
24685     }
24686
24687   DECL_INITIAL (decl) = make_node (BLOCK);
24688   current_function_decl = decl;
24689   allocate_struct_function (decl, false);
24690   init_function_start (decl);
24691   first_function_block_is_cold = false;
24692   /* Make sure unwind info is emitted for the thunk if needed.  */
24693   final_start_function (emit_barrier (), asm_out_file, 1);
24694
24695   fputs ("\tblr\n", asm_out_file);
24696
24697   final_end_function ();
24698   init_insn_lengths ();
24699   free_after_compilation (cfun);
24700   set_cfun (NULL);
24701   current_function_decl = NULL;
24702 }
24703
24704 /* Add r30 to hard reg set if the prologue sets it up and it is not
24705    pic_offset_table_rtx.  */
24706
24707 static void
24708 rs6000_set_up_by_prologue (struct hard_reg_set_container *set)
24709 {
24710   if (!TARGET_SINGLE_PIC_BASE
24711       && TARGET_TOC
24712       && TARGET_MINIMAL_TOC
24713       && !constant_pool_empty_p ())
24714     add_to_hard_reg_set (&set->set, Pmode, RS6000_PIC_OFFSET_TABLE_REGNUM);
24715   if (cfun->machine->split_stack_argp_used)
24716     add_to_hard_reg_set (&set->set, Pmode, 12);
24717
24718   /* Make sure the hard reg set doesn't include r2, which was possibly added
24719      via PIC_OFFSET_TABLE_REGNUM.  */
24720   if (TARGET_TOC)
24721     remove_from_hard_reg_set (&set->set, Pmode, TOC_REGNUM);
24722 }
24723
24724 \f
24725 /* Helper function for rs6000_split_logical to emit a logical instruction after
24726    spliting the operation to single GPR registers.
24727
24728    DEST is the destination register.
24729    OP1 and OP2 are the input source registers.
24730    CODE is the base operation (AND, IOR, XOR, NOT).
24731    MODE is the machine mode.
24732    If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
24733    If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
24734    If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.  */
24735
24736 static void
24737 rs6000_split_logical_inner (rtx dest,
24738                             rtx op1,
24739                             rtx op2,
24740                             enum rtx_code code,
24741                             machine_mode mode,
24742                             bool complement_final_p,
24743                             bool complement_op1_p,
24744                             bool complement_op2_p)
24745 {
24746   rtx bool_rtx;
24747
24748   /* Optimize AND of 0/0xffffffff and IOR/XOR of 0.  */
24749   if (op2 && CONST_INT_P (op2)
24750       && (mode == SImode || (mode == DImode && TARGET_POWERPC64))
24751       && !complement_final_p && !complement_op1_p && !complement_op2_p)
24752     {
24753       HOST_WIDE_INT mask = GET_MODE_MASK (mode);
24754       HOST_WIDE_INT value = INTVAL (op2) & mask;
24755
24756       /* Optimize AND of 0 to just set 0.  Optimize AND of -1 to be a move.  */
24757       if (code == AND)
24758         {
24759           if (value == 0)
24760             {
24761               emit_insn (gen_rtx_SET (dest, const0_rtx));
24762               return;
24763             }
24764
24765           else if (value == mask)
24766             {
24767               if (!rtx_equal_p (dest, op1))
24768                 emit_insn (gen_rtx_SET (dest, op1));
24769               return;
24770             }
24771         }
24772
24773       /* Optimize IOR/XOR of 0 to be a simple move.  Split large operations
24774          into separate ORI/ORIS or XORI/XORIS instrucitons.  */
24775       else if (code == IOR || code == XOR)
24776         {
24777           if (value == 0)
24778             {
24779               if (!rtx_equal_p (dest, op1))
24780                 emit_insn (gen_rtx_SET (dest, op1));
24781               return;
24782             }
24783         }
24784     }
24785
24786   if (code == AND && mode == SImode
24787       && !complement_final_p && !complement_op1_p && !complement_op2_p)
24788     {
24789       emit_insn (gen_andsi3 (dest, op1, op2));
24790       return;
24791     }
24792
24793   if (complement_op1_p)
24794     op1 = gen_rtx_NOT (mode, op1);
24795
24796   if (complement_op2_p)
24797     op2 = gen_rtx_NOT (mode, op2);
24798
24799   /* For canonical RTL, if only one arm is inverted it is the first.  */
24800   if (!complement_op1_p && complement_op2_p)
24801     std::swap (op1, op2);
24802
24803   bool_rtx = ((code == NOT)
24804               ? gen_rtx_NOT (mode, op1)
24805               : gen_rtx_fmt_ee (code, mode, op1, op2));
24806
24807   if (complement_final_p)
24808     bool_rtx = gen_rtx_NOT (mode, bool_rtx);
24809
24810   emit_insn (gen_rtx_SET (dest, bool_rtx));
24811 }
24812
24813 /* Split a DImode AND/IOR/XOR with a constant on a 32-bit system.  These
24814    operations are split immediately during RTL generation to allow for more
24815    optimizations of the AND/IOR/XOR.
24816
24817    OPERANDS is an array containing the destination and two input operands.
24818    CODE is the base operation (AND, IOR, XOR, NOT).
24819    MODE is the machine mode.
24820    If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
24821    If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
24822    If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.
24823    CLOBBER_REG is either NULL or a scratch register of type CC to allow
24824    formation of the AND instructions.  */
24825
24826 static void
24827 rs6000_split_logical_di (rtx operands[3],
24828                          enum rtx_code code,
24829                          bool complement_final_p,
24830                          bool complement_op1_p,
24831                          bool complement_op2_p)
24832 {
24833   const HOST_WIDE_INT lower_32bits = HOST_WIDE_INT_C(0xffffffff);
24834   const HOST_WIDE_INT upper_32bits = ~ lower_32bits;
24835   const HOST_WIDE_INT sign_bit = HOST_WIDE_INT_C(0x80000000);
24836   enum hi_lo { hi = 0, lo = 1 };
24837   rtx op0_hi_lo[2], op1_hi_lo[2], op2_hi_lo[2];
24838   size_t i;
24839
24840   op0_hi_lo[hi] = gen_highpart (SImode, operands[0]);
24841   op1_hi_lo[hi] = gen_highpart (SImode, operands[1]);
24842   op0_hi_lo[lo] = gen_lowpart (SImode, operands[0]);
24843   op1_hi_lo[lo] = gen_lowpart (SImode, operands[1]);
24844
24845   if (code == NOT)
24846     op2_hi_lo[hi] = op2_hi_lo[lo] = NULL_RTX;
24847   else
24848     {
24849       if (!CONST_INT_P (operands[2]))
24850         {
24851           op2_hi_lo[hi] = gen_highpart_mode (SImode, DImode, operands[2]);
24852           op2_hi_lo[lo] = gen_lowpart (SImode, operands[2]);
24853         }
24854       else
24855         {
24856           HOST_WIDE_INT value = INTVAL (operands[2]);
24857           HOST_WIDE_INT value_hi_lo[2];
24858
24859           gcc_assert (!complement_final_p);
24860           gcc_assert (!complement_op1_p);
24861           gcc_assert (!complement_op2_p);
24862
24863           value_hi_lo[hi] = value >> 32;
24864           value_hi_lo[lo] = value & lower_32bits;
24865
24866           for (i = 0; i < 2; i++)
24867             {
24868               HOST_WIDE_INT sub_value = value_hi_lo[i];
24869
24870               if (sub_value & sign_bit)
24871                 sub_value |= upper_32bits;
24872
24873               op2_hi_lo[i] = GEN_INT (sub_value);
24874
24875               /* If this is an AND instruction, check to see if we need to load
24876                  the value in a register.  */
24877               if (code == AND && sub_value != -1 && sub_value != 0
24878                   && !and_operand (op2_hi_lo[i], SImode))
24879                 op2_hi_lo[i] = force_reg (SImode, op2_hi_lo[i]);
24880             }
24881         }
24882     }
24883
24884   for (i = 0; i < 2; i++)
24885     {
24886       /* Split large IOR/XOR operations.  */
24887       if ((code == IOR || code == XOR)
24888           && CONST_INT_P (op2_hi_lo[i])
24889           && !complement_final_p
24890           && !complement_op1_p
24891           && !complement_op2_p
24892           && !logical_const_operand (op2_hi_lo[i], SImode))
24893         {
24894           HOST_WIDE_INT value = INTVAL (op2_hi_lo[i]);
24895           HOST_WIDE_INT hi_16bits = value & HOST_WIDE_INT_C(0xffff0000);
24896           HOST_WIDE_INT lo_16bits = value & HOST_WIDE_INT_C(0x0000ffff);
24897           rtx tmp = gen_reg_rtx (SImode);
24898
24899           /* Make sure the constant is sign extended.  */
24900           if ((hi_16bits & sign_bit) != 0)
24901             hi_16bits |= upper_32bits;
24902
24903           rs6000_split_logical_inner (tmp, op1_hi_lo[i], GEN_INT (hi_16bits),
24904                                       code, SImode, false, false, false);
24905
24906           rs6000_split_logical_inner (op0_hi_lo[i], tmp, GEN_INT (lo_16bits),
24907                                       code, SImode, false, false, false);
24908         }
24909       else
24910         rs6000_split_logical_inner (op0_hi_lo[i], op1_hi_lo[i], op2_hi_lo[i],
24911                                     code, SImode, complement_final_p,
24912                                     complement_op1_p, complement_op2_p);
24913     }
24914
24915   return;
24916 }
24917
24918 /* Split the insns that make up boolean operations operating on multiple GPR
24919    registers.  The boolean MD patterns ensure that the inputs either are
24920    exactly the same as the output registers, or there is no overlap.
24921
24922    OPERANDS is an array containing the destination and two input operands.
24923    CODE is the base operation (AND, IOR, XOR, NOT).
24924    If COMPLEMENT_FINAL_P is true, wrap the whole operation with NOT.
24925    If COMPLEMENT_OP1_P is true, wrap operand1 with NOT.
24926    If COMPLEMENT_OP2_P is true, wrap operand2 with NOT.  */
24927
24928 void
24929 rs6000_split_logical (rtx operands[3],
24930                       enum rtx_code code,
24931                       bool complement_final_p,
24932                       bool complement_op1_p,
24933                       bool complement_op2_p)
24934 {
24935   machine_mode mode = GET_MODE (operands[0]);
24936   machine_mode sub_mode;
24937   rtx op0, op1, op2;
24938   int sub_size, regno0, regno1, nregs, i;
24939
24940   /* If this is DImode, use the specialized version that can run before
24941      register allocation.  */
24942   if (mode == DImode && !TARGET_POWERPC64)
24943     {
24944       rs6000_split_logical_di (operands, code, complement_final_p,
24945                                complement_op1_p, complement_op2_p);
24946       return;
24947     }
24948
24949   op0 = operands[0];
24950   op1 = operands[1];
24951   op2 = (code == NOT) ? NULL_RTX : operands[2];
24952   sub_mode = (TARGET_POWERPC64) ? DImode : SImode;
24953   sub_size = GET_MODE_SIZE (sub_mode);
24954   regno0 = REGNO (op0);
24955   regno1 = REGNO (op1);
24956
24957   gcc_assert (reload_completed);
24958   gcc_assert (IN_RANGE (regno0, FIRST_GPR_REGNO, LAST_GPR_REGNO));
24959   gcc_assert (IN_RANGE (regno1, FIRST_GPR_REGNO, LAST_GPR_REGNO));
24960
24961   nregs = rs6000_hard_regno_nregs[(int)mode][regno0];
24962   gcc_assert (nregs > 1);
24963
24964   if (op2 && REG_P (op2))
24965     gcc_assert (IN_RANGE (REGNO (op2), FIRST_GPR_REGNO, LAST_GPR_REGNO));
24966
24967   for (i = 0; i < nregs; i++)
24968     {
24969       int offset = i * sub_size;
24970       rtx sub_op0 = simplify_subreg (sub_mode, op0, mode, offset);
24971       rtx sub_op1 = simplify_subreg (sub_mode, op1, mode, offset);
24972       rtx sub_op2 = ((code == NOT)
24973                      ? NULL_RTX
24974                      : simplify_subreg (sub_mode, op2, mode, offset));
24975
24976       rs6000_split_logical_inner (sub_op0, sub_op1, sub_op2, code, sub_mode,
24977                                   complement_final_p, complement_op1_p,
24978                                   complement_op2_p);
24979     }
24980
24981   return;
24982 }
24983
24984 \f
24985 /* Return true if the peephole2 can combine a load involving a combination of
24986    an addis instruction and a load with an offset that can be fused together on
24987    a power8.  */
24988
24989 bool
24990 fusion_gpr_load_p (rtx addis_reg,       /* register set via addis.  */
24991                    rtx addis_value,     /* addis value.  */
24992                    rtx target,          /* target register that is loaded.  */
24993                    rtx mem)             /* bottom part of the memory addr.  */
24994 {
24995   rtx addr;
24996   rtx base_reg;
24997
24998   /* Validate arguments.  */
24999   if (!base_reg_operand (addis_reg, GET_MODE (addis_reg)))
25000     return false;
25001
25002   if (!base_reg_operand (target, GET_MODE (target)))
25003     return false;
25004
25005   if (!fusion_gpr_addis (addis_value, GET_MODE (addis_value)))
25006     return false;
25007
25008   /* Allow sign/zero extension.  */
25009   if (GET_CODE (mem) == ZERO_EXTEND
25010       || (GET_CODE (mem) == SIGN_EXTEND && TARGET_P8_FUSION_SIGN))
25011     mem = XEXP (mem, 0);
25012
25013   if (!MEM_P (mem))
25014     return false;
25015
25016   if (!fusion_gpr_mem_load (mem, GET_MODE (mem)))
25017     return false;
25018
25019   addr = XEXP (mem, 0);                 /* either PLUS or LO_SUM.  */
25020   if (GET_CODE (addr) != PLUS && GET_CODE (addr) != LO_SUM)
25021     return false;
25022
25023   /* Validate that the register used to load the high value is either the
25024      register being loaded, or we can safely replace its use.
25025
25026      This function is only called from the peephole2 pass and we assume that
25027      there are 2 instructions in the peephole (addis and load), so we want to
25028      check if the target register was not used in the memory address and the
25029      register to hold the addis result is dead after the peephole.  */
25030   if (REGNO (addis_reg) != REGNO (target))
25031     {
25032       if (reg_mentioned_p (target, mem))
25033         return false;
25034
25035       if (!peep2_reg_dead_p (2, addis_reg))
25036         return false;
25037
25038       /* If the target register being loaded is the stack pointer, we must
25039          avoid loading any other value into it, even temporarily.  */
25040       if (REG_P (target) && REGNO (target) == STACK_POINTER_REGNUM)
25041         return false;
25042     }
25043
25044   base_reg = XEXP (addr, 0);
25045   return REGNO (addis_reg) == REGNO (base_reg);
25046 }
25047
25048 /* During the peephole2 pass, adjust and expand the insns for a load fusion
25049    sequence.  We adjust the addis register to use the target register.  If the
25050    load sign extends, we adjust the code to do the zero extending load, and an
25051    explicit sign extension later since the fusion only covers zero extending
25052    loads.
25053
25054    The operands are:
25055         operands[0]     register set with addis (to be replaced with target)
25056         operands[1]     value set via addis
25057         operands[2]     target register being loaded
25058         operands[3]     D-form memory reference using operands[0].  */
25059
25060 void
25061 expand_fusion_gpr_load (rtx *operands)
25062 {
25063   rtx addis_value = operands[1];
25064   rtx target = operands[2];
25065   rtx orig_mem = operands[3];
25066   rtx  new_addr, new_mem, orig_addr, offset;
25067   enum rtx_code plus_or_lo_sum;
25068   machine_mode target_mode = GET_MODE (target);
25069   machine_mode extend_mode = target_mode;
25070   machine_mode ptr_mode = Pmode;
25071   enum rtx_code extend = UNKNOWN;
25072
25073   if (GET_CODE (orig_mem) == ZERO_EXTEND
25074       || (TARGET_P8_FUSION_SIGN && GET_CODE (orig_mem) == SIGN_EXTEND))
25075     {
25076       extend = GET_CODE (orig_mem);
25077       orig_mem = XEXP (orig_mem, 0);
25078       target_mode = GET_MODE (orig_mem);
25079     }
25080
25081   gcc_assert (MEM_P (orig_mem));
25082
25083   orig_addr = XEXP (orig_mem, 0);
25084   plus_or_lo_sum = GET_CODE (orig_addr);
25085   gcc_assert (plus_or_lo_sum == PLUS || plus_or_lo_sum == LO_SUM);
25086
25087   offset = XEXP (orig_addr, 1);
25088   new_addr = gen_rtx_fmt_ee (plus_or_lo_sum, ptr_mode, addis_value, offset);
25089   new_mem = replace_equiv_address_nv (orig_mem, new_addr, false);
25090
25091   if (extend != UNKNOWN)
25092     new_mem = gen_rtx_fmt_e (ZERO_EXTEND, extend_mode, new_mem);
25093
25094   new_mem = gen_rtx_UNSPEC (extend_mode, gen_rtvec (1, new_mem),
25095                             UNSPEC_FUSION_GPR);
25096   emit_insn (gen_rtx_SET (target, new_mem));
25097
25098   if (extend == SIGN_EXTEND)
25099     {
25100       int sub_off = ((BYTES_BIG_ENDIAN)
25101                      ? GET_MODE_SIZE (extend_mode) - GET_MODE_SIZE (target_mode)
25102                      : 0);
25103       rtx sign_reg
25104         = simplify_subreg (target_mode, target, extend_mode, sub_off);
25105
25106       emit_insn (gen_rtx_SET (target,
25107                               gen_rtx_SIGN_EXTEND (extend_mode, sign_reg)));
25108     }
25109
25110   return;
25111 }
25112
25113 /* Emit the addis instruction that will be part of a fused instruction
25114    sequence.  */
25115
25116 void
25117 emit_fusion_addis (rtx target, rtx addis_value)
25118 {
25119   rtx fuse_ops[10];
25120   const char *addis_str = NULL;
25121
25122   /* Emit the addis instruction.  */
25123   fuse_ops[0] = target;
25124   if (satisfies_constraint_L (addis_value))
25125     {
25126       fuse_ops[1] = addis_value;
25127       addis_str = "lis %0,%v1";
25128     }
25129
25130   else if (GET_CODE (addis_value) == PLUS)
25131     {
25132       rtx op0 = XEXP (addis_value, 0);
25133       rtx op1 = XEXP (addis_value, 1);
25134
25135       if (REG_P (op0) && CONST_INT_P (op1)
25136           && satisfies_constraint_L (op1))
25137         {
25138           fuse_ops[1] = op0;
25139           fuse_ops[2] = op1;
25140           addis_str = "addis %0,%1,%v2";
25141         }
25142     }
25143
25144   else if (GET_CODE (addis_value) == HIGH)
25145     {
25146       rtx value = XEXP (addis_value, 0);
25147       if (GET_CODE (value) == UNSPEC && XINT (value, 1) == UNSPEC_TOCREL)
25148         {
25149           fuse_ops[1] = XVECEXP (value, 0, 0);          /* symbol ref.  */
25150           fuse_ops[2] = XVECEXP (value, 0, 1);          /* TOC register.  */
25151           if (TARGET_ELF)
25152             addis_str = "addis %0,%2,%1@toc@ha";
25153
25154           else if (TARGET_XCOFF)
25155             addis_str = "addis %0,%1@u(%2)";
25156
25157           else
25158             gcc_unreachable ();
25159         }
25160
25161       else if (GET_CODE (value) == PLUS)
25162         {
25163           rtx op0 = XEXP (value, 0);
25164           rtx op1 = XEXP (value, 1);
25165
25166           if (GET_CODE (op0) == UNSPEC
25167               && XINT (op0, 1) == UNSPEC_TOCREL
25168               && CONST_INT_P (op1))
25169             {
25170               fuse_ops[1] = XVECEXP (op0, 0, 0);        /* symbol ref.  */
25171               fuse_ops[2] = XVECEXP (op0, 0, 1);        /* TOC register.  */
25172               fuse_ops[3] = op1;
25173               if (TARGET_ELF)
25174                 addis_str = "addis %0,%2,%1+%3@toc@ha";
25175
25176               else if (TARGET_XCOFF)
25177                 addis_str = "addis %0,%1+%3@u(%2)";
25178
25179               else
25180                 gcc_unreachable ();
25181             }
25182         }
25183
25184       else if (satisfies_constraint_L (value))
25185         {
25186           fuse_ops[1] = value;
25187           addis_str = "lis %0,%v1";
25188         }
25189
25190       else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (value))
25191         {
25192           fuse_ops[1] = value;
25193           addis_str = "lis %0,%1@ha";
25194         }
25195     }
25196
25197   if (!addis_str)
25198     fatal_insn ("Could not generate addis value for fusion", addis_value);
25199
25200   output_asm_insn (addis_str, fuse_ops);
25201 }
25202
25203 /* Emit a D-form load or store instruction that is the second instruction
25204    of a fusion sequence.  */
25205
25206 static void
25207 emit_fusion_load (rtx load_reg, rtx addis_reg, rtx offset, const char *insn_str)
25208 {
25209   rtx fuse_ops[10];
25210   char insn_template[80];
25211
25212   fuse_ops[0] = load_reg;
25213   fuse_ops[1] = addis_reg;
25214
25215   if (CONST_INT_P (offset) && satisfies_constraint_I (offset))
25216     {
25217       sprintf (insn_template, "%s %%0,%%2(%%1)", insn_str);
25218       fuse_ops[2] = offset;
25219       output_asm_insn (insn_template, fuse_ops);
25220     }
25221
25222   else if (GET_CODE (offset) == UNSPEC
25223            && XINT (offset, 1) == UNSPEC_TOCREL)
25224     {
25225       if (TARGET_ELF)
25226         sprintf (insn_template, "%s %%0,%%2@toc@l(%%1)", insn_str);
25227
25228       else if (TARGET_XCOFF)
25229         sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
25230
25231       else
25232         gcc_unreachable ();
25233
25234       fuse_ops[2] = XVECEXP (offset, 0, 0);
25235       output_asm_insn (insn_template, fuse_ops);
25236     }
25237
25238   else if (GET_CODE (offset) == PLUS
25239            && GET_CODE (XEXP (offset, 0)) == UNSPEC
25240            && XINT (XEXP (offset, 0), 1) == UNSPEC_TOCREL
25241            && CONST_INT_P (XEXP (offset, 1)))
25242     {
25243       rtx tocrel_unspec = XEXP (offset, 0);
25244       if (TARGET_ELF)
25245         sprintf (insn_template, "%s %%0,%%2+%%3@toc@l(%%1)", insn_str);
25246
25247       else if (TARGET_XCOFF)
25248         sprintf (insn_template, "%s %%0,%%2+%%3@l(%%1)", insn_str);
25249
25250       else
25251         gcc_unreachable ();
25252
25253       fuse_ops[2] = XVECEXP (tocrel_unspec, 0, 0);
25254       fuse_ops[3] = XEXP (offset, 1);
25255       output_asm_insn (insn_template, fuse_ops);
25256     }
25257
25258   else if (TARGET_ELF && !TARGET_POWERPC64 && CONSTANT_P (offset))
25259     {
25260       sprintf (insn_template, "%s %%0,%%2@l(%%1)", insn_str);
25261
25262       fuse_ops[2] = offset;
25263       output_asm_insn (insn_template, fuse_ops);
25264     }
25265
25266   else
25267     fatal_insn ("Unable to generate load/store offset for fusion", offset);
25268
25269   return;
25270 }
25271
25272 /* Given an address, convert it into the addis and load offset parts.  Addresses
25273    created during the peephole2 process look like:
25274         (lo_sum (high (unspec [(sym)] UNSPEC_TOCREL))
25275                 (unspec [(...)] UNSPEC_TOCREL))  */
25276
25277 static void
25278 fusion_split_address (rtx addr, rtx *p_hi, rtx *p_lo)
25279 {
25280   rtx hi, lo;
25281
25282   if (GET_CODE (addr) == PLUS || GET_CODE (addr) == LO_SUM)
25283     {
25284       hi = XEXP (addr, 0);
25285       lo = XEXP (addr, 1);
25286     }
25287   else
25288     gcc_unreachable ();
25289
25290   *p_hi = hi;
25291   *p_lo = lo;
25292 }
25293
25294 /* Return a string to fuse an addis instruction with a gpr load to the same
25295    register that we loaded up the addis instruction.  The address that is used
25296    is the logical address that was formed during peephole2:
25297         (lo_sum (high) (low-part))
25298
25299    The code is complicated, so we call output_asm_insn directly, and just
25300    return "".  */
25301
25302 const char *
25303 emit_fusion_gpr_load (rtx target, rtx mem)
25304 {
25305   rtx addis_value;
25306   rtx addr;
25307   rtx load_offset;
25308   const char *load_str = NULL;
25309   machine_mode mode;
25310
25311   if (GET_CODE (mem) == ZERO_EXTEND)
25312     mem = XEXP (mem, 0);
25313
25314   gcc_assert (REG_P (target) && MEM_P (mem));
25315
25316   addr = XEXP (mem, 0);
25317   fusion_split_address (addr, &addis_value, &load_offset);
25318
25319   /* Now emit the load instruction to the same register.  */
25320   mode = GET_MODE (mem);
25321   switch (mode)
25322     {
25323     case E_QImode:
25324       load_str = "lbz";
25325       break;
25326
25327     case E_HImode:
25328       load_str = "lhz";
25329       break;
25330
25331     case E_SImode:
25332     case E_SFmode:
25333       load_str = "lwz";
25334       break;
25335
25336     case E_DImode:
25337     case E_DFmode:
25338       gcc_assert (TARGET_POWERPC64);
25339       load_str = "ld";
25340       break;
25341
25342     default:
25343       fatal_insn ("Bad GPR fusion", gen_rtx_SET (target, mem));
25344     }
25345
25346   /* Emit the addis instruction.  */
25347   emit_fusion_addis (target, addis_value);
25348
25349   /* Emit the D-form load instruction.  */
25350   emit_fusion_load (target, target, load_offset, load_str);
25351
25352   return "";
25353 }
25354 \f
25355
25356 #ifdef RS6000_GLIBC_ATOMIC_FENV
25357 /* Function declarations for rs6000_atomic_assign_expand_fenv.  */
25358 static tree atomic_hold_decl, atomic_clear_decl, atomic_update_decl;
25359 #endif
25360
25361 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook.  */
25362
25363 static void
25364 rs6000_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
25365 {
25366   if (!TARGET_HARD_FLOAT)
25367     {
25368 #ifdef RS6000_GLIBC_ATOMIC_FENV
25369       if (atomic_hold_decl == NULL_TREE)
25370         {
25371           atomic_hold_decl
25372             = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
25373                           get_identifier ("__atomic_feholdexcept"),
25374                           build_function_type_list (void_type_node,
25375                                                     double_ptr_type_node,
25376                                                     NULL_TREE));
25377           TREE_PUBLIC (atomic_hold_decl) = 1;
25378           DECL_EXTERNAL (atomic_hold_decl) = 1;
25379         }
25380
25381       if (atomic_clear_decl == NULL_TREE)
25382         {
25383           atomic_clear_decl
25384             = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
25385                           get_identifier ("__atomic_feclearexcept"),
25386                           build_function_type_list (void_type_node,
25387                                                     NULL_TREE));
25388           TREE_PUBLIC (atomic_clear_decl) = 1;
25389           DECL_EXTERNAL (atomic_clear_decl) = 1;
25390         }
25391
25392       tree const_double = build_qualified_type (double_type_node,
25393                                                 TYPE_QUAL_CONST);
25394       tree const_double_ptr = build_pointer_type (const_double);
25395       if (atomic_update_decl == NULL_TREE)
25396         {
25397           atomic_update_decl
25398             = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
25399                           get_identifier ("__atomic_feupdateenv"),
25400                           build_function_type_list (void_type_node,
25401                                                     const_double_ptr,
25402                                                     NULL_TREE));
25403           TREE_PUBLIC (atomic_update_decl) = 1;
25404           DECL_EXTERNAL (atomic_update_decl) = 1;
25405         }
25406
25407       tree fenv_var = create_tmp_var_raw (double_type_node);
25408       TREE_ADDRESSABLE (fenv_var) = 1;
25409       tree fenv_addr = build1 (ADDR_EXPR, double_ptr_type_node, fenv_var);
25410
25411       *hold = build_call_expr (atomic_hold_decl, 1, fenv_addr);
25412       *clear = build_call_expr (atomic_clear_decl, 0);
25413       *update = build_call_expr (atomic_update_decl, 1,
25414                                  fold_convert (const_double_ptr, fenv_addr));
25415 #endif
25416       return;
25417     }
25418
25419   tree mffs = rs6000_builtin_decls[RS6000_BUILTIN_MFFS];
25420   tree mtfsf = rs6000_builtin_decls[RS6000_BUILTIN_MTFSF];
25421   tree call_mffs = build_call_expr (mffs, 0);
25422
25423   /* Generates the equivalent of feholdexcept (&fenv_var)
25424
25425      *fenv_var = __builtin_mffs ();
25426      double fenv_hold;
25427      *(uint64_t*)&fenv_hold = *(uint64_t*)fenv_var & 0xffffffff00000007LL;
25428      __builtin_mtfsf (0xff, fenv_hold);  */
25429
25430   /* Mask to clear everything except for the rounding modes and non-IEEE
25431      arithmetic flag.  */
25432   const unsigned HOST_WIDE_INT hold_exception_mask =
25433     HOST_WIDE_INT_C (0xffffffff00000007);
25434
25435   tree fenv_var = create_tmp_var_raw (double_type_node);
25436
25437   tree hold_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_var, call_mffs);
25438
25439   tree fenv_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_var);
25440   tree fenv_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
25441                               build_int_cst (uint64_type_node,
25442                                              hold_exception_mask));
25443
25444   tree fenv_hold_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
25445                                  fenv_llu_and);
25446
25447   tree hold_mtfsf = build_call_expr (mtfsf, 2,
25448                                      build_int_cst (unsigned_type_node, 0xff),
25449                                      fenv_hold_mtfsf);
25450
25451   *hold = build2 (COMPOUND_EXPR, void_type_node, hold_mffs, hold_mtfsf);
25452
25453   /* Generates the equivalent of feclearexcept (FE_ALL_EXCEPT):
25454
25455      double fenv_clear = __builtin_mffs ();
25456      *(uint64_t)&fenv_clear &= 0xffffffff00000000LL;
25457      __builtin_mtfsf (0xff, fenv_clear);  */
25458
25459   /* Mask to clear everything except for the rounding modes and non-IEEE
25460      arithmetic flag.  */
25461   const unsigned HOST_WIDE_INT clear_exception_mask =
25462     HOST_WIDE_INT_C (0xffffffff00000000);
25463
25464   tree fenv_clear = create_tmp_var_raw (double_type_node);
25465
25466   tree clear_mffs = build2 (MODIFY_EXPR, void_type_node, fenv_clear, call_mffs);
25467
25468   tree fenv_clean_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, fenv_clear);
25469   tree fenv_clear_llu_and = build2 (BIT_AND_EXPR, uint64_type_node,
25470                                     fenv_clean_llu,
25471                                     build_int_cst (uint64_type_node,
25472                                                    clear_exception_mask));
25473
25474   tree fenv_clear_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
25475                                   fenv_clear_llu_and);
25476
25477   tree clear_mtfsf = build_call_expr (mtfsf, 2,
25478                                       build_int_cst (unsigned_type_node, 0xff),
25479                                       fenv_clear_mtfsf);
25480
25481   *clear = build2 (COMPOUND_EXPR, void_type_node, clear_mffs, clear_mtfsf);
25482
25483   /* Generates the equivalent of feupdateenv (&fenv_var)
25484
25485      double old_fenv = __builtin_mffs ();
25486      double fenv_update;
25487      *(uint64_t*)&fenv_update = (*(uint64_t*)&old & 0xffffffff1fffff00LL) |
25488                                 (*(uint64_t*)fenv_var 0x1ff80fff);
25489      __builtin_mtfsf (0xff, fenv_update);  */
25490
25491   const unsigned HOST_WIDE_INT update_exception_mask =
25492     HOST_WIDE_INT_C (0xffffffff1fffff00);
25493   const unsigned HOST_WIDE_INT new_exception_mask =
25494     HOST_WIDE_INT_C (0x1ff80fff);
25495
25496   tree old_fenv = create_tmp_var_raw (double_type_node);
25497   tree update_mffs = build2 (MODIFY_EXPR, void_type_node, old_fenv, call_mffs);
25498
25499   tree old_llu = build1 (VIEW_CONVERT_EXPR, uint64_type_node, old_fenv);
25500   tree old_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, old_llu,
25501                              build_int_cst (uint64_type_node,
25502                                             update_exception_mask));
25503
25504   tree new_llu_and = build2 (BIT_AND_EXPR, uint64_type_node, fenv_llu,
25505                              build_int_cst (uint64_type_node,
25506                                             new_exception_mask));
25507
25508   tree new_llu_mask = build2 (BIT_IOR_EXPR, uint64_type_node,
25509                               old_llu_and, new_llu_and);
25510
25511   tree fenv_update_mtfsf = build1 (VIEW_CONVERT_EXPR, double_type_node,
25512                                    new_llu_mask);
25513
25514   tree update_mtfsf = build_call_expr (mtfsf, 2,
25515                                        build_int_cst (unsigned_type_node, 0xff),
25516                                        fenv_update_mtfsf);
25517
25518   *update = build2 (COMPOUND_EXPR, void_type_node, update_mffs, update_mtfsf);
25519 }
25520
25521 void
25522 rs6000_generate_float2_double_code (rtx dst, rtx src1, rtx src2)
25523 {
25524   rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
25525
25526   rtx_tmp0 = gen_reg_rtx (V2DFmode);
25527   rtx_tmp1 = gen_reg_rtx (V2DFmode);
25528
25529   /* The destination of the vmrgew instruction layout is:
25530      rtx_tmp2[0] rtx_tmp3[0] rtx_tmp2[1] rtx_tmp3[0].
25531      Setup rtx_tmp0 and rtx_tmp1 to ensure the order of the elements after the
25532      vmrgew instruction will be correct.  */
25533   if (BYTES_BIG_ENDIAN)
25534     {
25535        emit_insn (gen_vsx_xxpermdi_v2df_be (rtx_tmp0, src1, src2,
25536                                             GEN_INT (0)));
25537        emit_insn (gen_vsx_xxpermdi_v2df_be (rtx_tmp1, src1, src2,
25538                                             GEN_INT (3)));
25539     }
25540   else
25541     {
25542        emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp0, src1, src2, GEN_INT (3)));
25543        emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp1, src1, src2, GEN_INT (0)));
25544     }
25545
25546   rtx_tmp2 = gen_reg_rtx (V4SFmode);
25547   rtx_tmp3 = gen_reg_rtx (V4SFmode);
25548
25549   emit_insn (gen_vsx_xvcdpsp (rtx_tmp2, rtx_tmp0));
25550   emit_insn (gen_vsx_xvcdpsp (rtx_tmp3, rtx_tmp1));
25551
25552   if (BYTES_BIG_ENDIAN)
25553     emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp2, rtx_tmp3));
25554   else
25555     emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp3, rtx_tmp2));
25556 }
25557
25558 void
25559 rs6000_generate_float2_code (bool signed_convert, rtx dst, rtx src1, rtx src2)
25560 {
25561   rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
25562
25563   rtx_tmp0 = gen_reg_rtx (V2DImode);
25564   rtx_tmp1 = gen_reg_rtx (V2DImode);
25565
25566   /* The destination of the vmrgew instruction layout is:
25567      rtx_tmp2[0] rtx_tmp3[0] rtx_tmp2[1] rtx_tmp3[0].
25568      Setup rtx_tmp0 and rtx_tmp1 to ensure the order of the elements after the
25569      vmrgew instruction will be correct.  */
25570   if (BYTES_BIG_ENDIAN)
25571     {
25572       emit_insn (gen_vsx_xxpermdi_v2di_be (rtx_tmp0, src1, src2, GEN_INT (0)));
25573       emit_insn (gen_vsx_xxpermdi_v2di_be (rtx_tmp1, src1, src2, GEN_INT (3)));
25574     }
25575   else
25576     {
25577       emit_insn (gen_vsx_xxpermdi_v2di (rtx_tmp0, src1, src2, GEN_INT (3)));
25578       emit_insn (gen_vsx_xxpermdi_v2di (rtx_tmp1, src1, src2, GEN_INT (0)));
25579     }
25580
25581   rtx_tmp2 = gen_reg_rtx (V4SFmode);
25582   rtx_tmp3 = gen_reg_rtx (V4SFmode);
25583
25584   if (signed_convert)
25585     {
25586       emit_insn (gen_vsx_xvcvsxdsp (rtx_tmp2, rtx_tmp0));
25587       emit_insn (gen_vsx_xvcvsxdsp (rtx_tmp3, rtx_tmp1));
25588     }
25589   else
25590     {
25591        emit_insn (gen_vsx_xvcvuxdsp (rtx_tmp2, rtx_tmp0));
25592        emit_insn (gen_vsx_xvcvuxdsp (rtx_tmp3, rtx_tmp1));
25593     }
25594
25595   if (BYTES_BIG_ENDIAN)
25596     emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp2, rtx_tmp3));
25597   else
25598     emit_insn (gen_p8_vmrgew_v4sf (dst, rtx_tmp3, rtx_tmp2));
25599 }
25600
25601 void
25602 rs6000_generate_vsigned2_code (bool signed_convert, rtx dst, rtx src1,
25603                                rtx src2)
25604 {
25605   rtx rtx_tmp0, rtx_tmp1, rtx_tmp2, rtx_tmp3;
25606
25607   rtx_tmp0 = gen_reg_rtx (V2DFmode);
25608   rtx_tmp1 = gen_reg_rtx (V2DFmode);
25609
25610   emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp0, src1, src2, GEN_INT (0)));
25611   emit_insn (gen_vsx_xxpermdi_v2df (rtx_tmp1, src1, src2, GEN_INT (3)));
25612
25613   rtx_tmp2 = gen_reg_rtx (V4SImode);
25614   rtx_tmp3 = gen_reg_rtx (V4SImode);
25615
25616   if (signed_convert)
25617     {
25618       emit_insn (gen_vsx_xvcvdpsxws (rtx_tmp2, rtx_tmp0));
25619       emit_insn (gen_vsx_xvcvdpsxws (rtx_tmp3, rtx_tmp1));
25620     }
25621   else
25622     {
25623       emit_insn (gen_vsx_xvcvdpuxws (rtx_tmp2, rtx_tmp0));
25624       emit_insn (gen_vsx_xvcvdpuxws (rtx_tmp3, rtx_tmp1));
25625     }
25626
25627   emit_insn (gen_p8_vmrgew_v4si (dst, rtx_tmp2, rtx_tmp3));
25628 }
25629
25630 /* Implement the TARGET_OPTAB_SUPPORTED_P hook.  */
25631
25632 static bool
25633 rs6000_optab_supported_p (int op, machine_mode mode1, machine_mode,
25634                           optimization_type opt_type)
25635 {
25636   switch (op)
25637     {
25638     case rsqrt_optab:
25639       return (opt_type == OPTIMIZE_FOR_SPEED
25640               && RS6000_RECIP_AUTO_RSQRTE_P (mode1));
25641
25642     default:
25643       return true;
25644     }
25645 }
25646
25647 /* Implement TARGET_CONSTANT_ALIGNMENT.  */
25648
25649 static HOST_WIDE_INT
25650 rs6000_constant_alignment (const_tree exp, HOST_WIDE_INT align)
25651 {
25652   if (TREE_CODE (exp) == STRING_CST
25653       && (STRICT_ALIGNMENT || !optimize_size))
25654     return MAX (align, BITS_PER_WORD);
25655   return align;
25656 }
25657
25658 /* Implement TARGET_STARTING_FRAME_OFFSET.  */
25659
25660 static HOST_WIDE_INT
25661 rs6000_starting_frame_offset (void)
25662 {
25663   if (FRAME_GROWS_DOWNWARD)
25664     return 0;
25665   return RS6000_STARTING_FRAME_OFFSET;
25666 }
25667 \f
25668
25669 /* Create an alias for a mangled name where we have changed the mangling (in
25670    GCC 8.1, we used U10__float128, and now we use u9__ieee128).  This is called
25671    via the target hook TARGET_ASM_GLOBALIZE_DECL_NAME.  */
25672
25673 #if TARGET_ELF && RS6000_WEAK
25674 static void
25675 rs6000_globalize_decl_name (FILE * stream, tree decl)
25676 {
25677   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
25678
25679   targetm.asm_out.globalize_label (stream, name);
25680
25681   if (rs6000_passes_ieee128 && name[0] == '_' && name[1] == 'Z')
25682     {
25683       tree save_asm_name = DECL_ASSEMBLER_NAME (decl);
25684       const char *old_name;
25685
25686       ieee128_mangling_gcc_8_1 = true;
25687       lang_hooks.set_decl_assembler_name (decl);
25688       old_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
25689       SET_DECL_ASSEMBLER_NAME (decl, save_asm_name);
25690       ieee128_mangling_gcc_8_1 = false;
25691
25692       if (strcmp (name, old_name) != 0)
25693         {
25694           fprintf (stream, "\t.weak %s\n", old_name);
25695           fprintf (stream, "\t.set %s,%s\n", old_name, name);
25696         }
25697     }
25698 }
25699 #endif
25700
25701 \f
25702 /* On 64-bit Linux and Freebsd systems, possibly switch the long double library
25703    function names from <foo>l to <foo>f128 if the default long double type is
25704    IEEE 128-bit.  Typically, with the C and C++ languages, the standard math.h
25705    include file switches the names on systems that support long double as IEEE
25706    128-bit, but that doesn't work if the user uses __builtin_<foo>l directly.
25707    In the future, glibc will export names like __ieee128_sinf128 and we can
25708    switch to using those instead of using sinf128, which pollutes the user's
25709    namespace.
25710
25711    This will switch the names for Fortran math functions as well (which doesn't
25712    use math.h).  However, Fortran needs other changes to the compiler and
25713    library before you can switch the real*16 type at compile time.
25714
25715    We use the TARGET_MANGLE_DECL_ASSEMBLER_NAME hook to change this name.  We
25716    only do this if the default is that long double is IBM extended double, and
25717    the user asked for IEEE 128-bit.  */
25718
25719 static tree
25720 rs6000_mangle_decl_assembler_name (tree decl, tree id)
25721 {
25722   if (!TARGET_IEEEQUAD_DEFAULT && TARGET_IEEEQUAD && TARGET_LONG_DOUBLE_128
25723       && TREE_CODE (decl) == FUNCTION_DECL && DECL_IS_BUILTIN (decl) )
25724     {
25725       size_t len = IDENTIFIER_LENGTH (id);
25726       const char *name = IDENTIFIER_POINTER (id);
25727
25728       if (name[len - 1] == 'l')
25729         {
25730           bool uses_ieee128_p = false;
25731           tree type = TREE_TYPE (decl);
25732           machine_mode ret_mode = TYPE_MODE (type);
25733
25734           /* See if the function returns a IEEE 128-bit floating point type or
25735              complex type.  */
25736           if (ret_mode == TFmode || ret_mode == TCmode)
25737             uses_ieee128_p = true;
25738           else
25739             {
25740               function_args_iterator args_iter;
25741               tree arg;
25742
25743               /* See if the function passes a IEEE 128-bit floating point type
25744                  or complex type.  */
25745               FOREACH_FUNCTION_ARGS (type, arg, args_iter)
25746                 {
25747                   machine_mode arg_mode = TYPE_MODE (arg);
25748                   if (arg_mode == TFmode || arg_mode == TCmode)
25749                     {
25750                       uses_ieee128_p = true;
25751                       break;
25752                     }
25753                 }
25754             }
25755
25756           /* If we passed or returned an IEEE 128-bit floating point type,
25757              change the name.  */
25758           if (uses_ieee128_p)
25759             {
25760               char *name2 = (char *) alloca (len + 4);
25761               memcpy (name2, name, len - 1);
25762               strcpy (name2 + len - 1, "f128");
25763               id = get_identifier (name2);
25764             }
25765         }
25766     }
25767
25768   return id;
25769 }
25770
25771 /* Predict whether the given loop in gimple will be transformed in the RTL
25772    doloop_optimize pass.  */
25773
25774 static bool
25775 rs6000_predict_doloop_p (struct loop *loop)
25776 {
25777   gcc_assert (loop);
25778
25779   /* On rs6000, targetm.can_use_doloop_p is actually
25780      can_use_doloop_if_innermost.  Just ensure the loop is innermost.  */
25781   if (loop->inner != NULL)
25782     {
25783       if (dump_file && (dump_flags & TDF_DETAILS))
25784         fprintf (dump_file, "Predict doloop failure due to"
25785                             " loop nesting.\n");
25786       return false;
25787     }
25788
25789   return true;
25790 }
25791
25792 struct gcc_target targetm = TARGET_INITIALIZER;
25793
25794 #include "gt-rs6000.h"