OSDN Git Service

Backport from mainline
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-codes.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "except.h"
38 #include "function.h"
39 #include "recog.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "common/common-target.h"
49 #include "langhooks.h"
50 #include "reload.h"
51 #include "cgraph.h"
52 #include "gimple.h"
53 #include "dwarf2.h"
54 #include "df.h"
55 #include "tm-constrs.h"
56 #include "params.h"
57 #include "cselib.h"
58 #include "debug.h"
59 #include "sched-int.h"
60 #include "sbitmap.h"
61 #include "fibheap.h"
62 #include "opts.h"
63 #include "diagnostic.h"
64
65 enum upper_128bits_state
66 {
67   unknown = 0,
68   unused,
69   used
70 };
71
72 typedef struct block_info_def
73 {
74   /* State of the upper 128bits of AVX registers at exit.  */
75   enum upper_128bits_state state;
76   /* TRUE if state of the upper 128bits of AVX registers is unchanged
77      in this block.  */
78   bool unchanged;
79   /* TRUE if block has been processed.  */
80   bool processed;
81   /* TRUE if block has been scanned.  */
82   bool scanned;
83   /* Previous state of the upper 128bits of AVX registers at entry.  */
84   enum upper_128bits_state prev;
85 } *block_info;
86
87 #define BLOCK_INFO(B)   ((block_info) (B)->aux)
88
89 enum call_avx256_state
90 {
91   /* Callee returns 256bit AVX register.  */
92   callee_return_avx256 = -1,
93   /* Callee returns and passes 256bit AVX register.  */
94   callee_return_pass_avx256,
95   /* Callee passes 256bit AVX register.  */
96   callee_pass_avx256,
97   /* Callee doesn't return nor passe 256bit AVX register, or no
98      256bit AVX register in function return.  */
99   call_no_avx256,
100   /* vzeroupper intrinsic.  */
101   vzeroupper_intrinsic
102 };
103
104 /* Check if a 256bit AVX register is referenced in stores.   */
105
106 static void
107 check_avx256_stores (rtx dest, const_rtx set, void *data)
108 {
109   if ((REG_P (dest)
110        && VALID_AVX256_REG_MODE (GET_MODE (dest)))
111       || (GET_CODE (set) == SET
112           && REG_P (SET_SRC (set))
113           && VALID_AVX256_REG_MODE (GET_MODE (SET_SRC (set)))))
114     {
115       enum upper_128bits_state *state
116         = (enum upper_128bits_state *) data;
117       *state = used;
118     }
119 }
120
121 /* Helper function for move_or_delete_vzeroupper_1.  Look for vzeroupper
122    in basic block BB.  Delete it if upper 128bit AVX registers are
123    unused.  If it isn't deleted, move it to just before a jump insn.
124
125    STATE is state of the upper 128bits of AVX registers at entry.  */
126
127 static void
128 move_or_delete_vzeroupper_2 (basic_block bb,
129                              enum upper_128bits_state state)
130 {
131   rtx insn, bb_end;
132   rtx vzeroupper_insn = NULL_RTX;
133   rtx pat;
134   int avx256;
135   bool unchanged;
136
137   if (BLOCK_INFO (bb)->unchanged)
138     {
139       if (dump_file)
140         fprintf (dump_file, " [bb %i] unchanged: upper 128bits: %d\n",
141                  bb->index, state);
142
143       BLOCK_INFO (bb)->state = state;
144       return;
145     }
146
147   if (BLOCK_INFO (bb)->scanned && BLOCK_INFO (bb)->prev == state)
148     {
149       if (dump_file)
150         fprintf (dump_file, " [bb %i] scanned: upper 128bits: %d\n",
151                  bb->index, BLOCK_INFO (bb)->state);
152       return;
153     }
154
155   BLOCK_INFO (bb)->prev = state;
156
157   if (dump_file)
158     fprintf (dump_file, " [bb %i] entry: upper 128bits: %d\n",
159              bb->index, state);
160
161   unchanged = true;
162
163   /* BB_END changes when it is deleted.  */
164   bb_end = BB_END (bb);
165   insn = BB_HEAD (bb);
166   while (insn != bb_end)
167     {
168       insn = NEXT_INSN (insn);
169
170       if (!NONDEBUG_INSN_P (insn))
171         continue;
172
173       /* Move vzeroupper before jump/call.  */
174       if (JUMP_P (insn) || CALL_P (insn))
175         {
176           if (!vzeroupper_insn)
177             continue;
178
179           if (PREV_INSN (insn) != vzeroupper_insn)
180             {
181               if (dump_file)
182                 {
183                   fprintf (dump_file, "Move vzeroupper after:\n");
184                   print_rtl_single (dump_file, PREV_INSN (insn));
185                   fprintf (dump_file, "before:\n");
186                   print_rtl_single (dump_file, insn);
187                 }
188               reorder_insns_nobb (vzeroupper_insn, vzeroupper_insn,
189                                   PREV_INSN (insn));
190             }
191           vzeroupper_insn = NULL_RTX;
192           continue;
193         }
194
195       pat = PATTERN (insn);
196
197       /* Check insn for vzeroupper intrinsic.  */
198       if (GET_CODE (pat) == UNSPEC_VOLATILE
199           && XINT (pat, 1) == UNSPECV_VZEROUPPER)
200         {
201           if (dump_file)
202             {
203               /* Found vzeroupper intrinsic.  */
204               fprintf (dump_file, "Found vzeroupper:\n");
205               print_rtl_single (dump_file, insn);
206             }
207         }
208       else
209         {
210           /* Check insn for vzeroall intrinsic.  */
211           if (GET_CODE (pat) == PARALLEL
212               && GET_CODE (XVECEXP (pat, 0, 0)) == UNSPEC_VOLATILE
213               && XINT (XVECEXP (pat, 0, 0), 1) == UNSPECV_VZEROALL)
214             {
215               state = unused;
216               unchanged = false;
217
218               /* Delete pending vzeroupper insertion.  */
219               if (vzeroupper_insn)
220                 {
221                   delete_insn (vzeroupper_insn);
222                   vzeroupper_insn = NULL_RTX;
223                 }
224             }
225           else if (state != used)
226             {
227               note_stores (pat, check_avx256_stores, &state);
228               if (state == used)
229                 unchanged = false;
230             }
231           continue;
232         }
233
234       /* Process vzeroupper intrinsic.  */
235       avx256 = INTVAL (XVECEXP (pat, 0, 0));
236
237       if (state == unused)
238         {
239           /* Since the upper 128bits are cleared, callee must not pass
240              256bit AVX register.  We only need to check if callee
241              returns 256bit AVX register.  */
242           if (avx256 == callee_return_avx256)
243             {
244               state = used;
245               unchanged = false;
246             }
247
248           /* Remove unnecessary vzeroupper since upper 128bits are
249              cleared.  */
250           if (dump_file)
251             {
252               fprintf (dump_file, "Delete redundant vzeroupper:\n");
253               print_rtl_single (dump_file, insn);
254             }
255           delete_insn (insn);
256         }
257       else
258         {
259           /* Set state to UNUSED if callee doesn't return 256bit AVX
260              register.  */
261           if (avx256 != callee_return_pass_avx256)
262             state = unused;
263
264           if (avx256 == callee_return_pass_avx256
265               || avx256 == callee_pass_avx256)
266             {
267               /* Must remove vzeroupper since callee passes in 256bit
268                  AVX register.  */
269               if (dump_file)
270                 {
271                   fprintf (dump_file, "Delete callee pass vzeroupper:\n");
272                   print_rtl_single (dump_file, insn);
273                 }
274               delete_insn (insn);
275             }
276           else
277             {
278               vzeroupper_insn = insn;
279               unchanged = false;
280             }
281         }
282     }
283
284   BLOCK_INFO (bb)->state = state;
285   BLOCK_INFO (bb)->unchanged = unchanged;
286   BLOCK_INFO (bb)->scanned = true;
287
288   if (dump_file)
289     fprintf (dump_file, " [bb %i] exit: %s: upper 128bits: %d\n",
290              bb->index, unchanged ? "unchanged" : "changed",
291              state);
292 }
293
294 /* Helper function for move_or_delete_vzeroupper.  Process vzeroupper
295    in BLOCK and check its predecessor blocks.  Treat UNKNOWN state
296    as USED if UNKNOWN_IS_UNUSED is true.  Return TRUE if the exit
297    state is changed.  */
298
299 static bool
300 move_or_delete_vzeroupper_1 (basic_block block, bool unknown_is_unused)
301 {
302   edge e;
303   edge_iterator ei;
304   enum upper_128bits_state state, old_state, new_state;
305   bool seen_unknown;
306
307   if (dump_file)
308     fprintf (dump_file, " Process [bb %i]: status: %d\n",
309              block->index, BLOCK_INFO (block)->processed);
310
311   if (BLOCK_INFO (block)->processed)
312     return false;
313
314   state = unused;
315
316   /* Check all predecessor edges of this block.  */
317   seen_unknown = false;
318   FOR_EACH_EDGE (e, ei, block->preds)
319     {
320       if (e->src == block)
321         continue;
322       switch (BLOCK_INFO (e->src)->state)
323         {
324         case unknown:
325           if (!unknown_is_unused)
326             seen_unknown = true;
327         case unused:
328           break;
329         case used:
330           state = used;
331           goto done;
332         }
333     }
334
335   if (seen_unknown)
336     state = unknown;
337
338 done:
339   old_state = BLOCK_INFO (block)->state;
340   move_or_delete_vzeroupper_2 (block, state);
341   new_state = BLOCK_INFO (block)->state;
342
343   if (state != unknown || new_state == used)
344     BLOCK_INFO (block)->processed = true;
345
346   /* Need to rescan if the upper 128bits of AVX registers are changed
347      to USED at exit.  */
348   if (new_state != old_state)
349     {
350       if (new_state == used)
351         cfun->machine->rescan_vzeroupper_p = 1;
352       return true;
353     }
354   else
355     return false;
356 }
357
358 /* Go through the instruction stream looking for vzeroupper.  Delete
359    it if upper 128bit AVX registers are unused.  If it isn't deleted,
360    move it to just before a jump insn.  */
361
362 static void
363 move_or_delete_vzeroupper (void)
364 {
365   edge e;
366   edge_iterator ei;
367   basic_block bb;
368   fibheap_t worklist, pending, fibheap_swap;
369   sbitmap visited, in_worklist, in_pending, sbitmap_swap;
370   int *bb_order;
371   int *rc_order;
372   int i;
373
374   /* Set up block info for each basic block.  */
375   alloc_aux_for_blocks (sizeof (struct block_info_def));
376
377   /* Process outgoing edges of entry point.  */
378   if (dump_file)
379     fprintf (dump_file, "Process outgoing edges of entry point\n");
380
381   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
382     {
383       move_or_delete_vzeroupper_2 (e->dest,
384                                    cfun->machine->caller_pass_avx256_p
385                                    ? used : unused);
386       BLOCK_INFO (e->dest)->processed = true;
387     }
388
389   /* Compute reverse completion order of depth first search of the CFG
390      so that the data-flow runs faster.  */
391   rc_order = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
392   bb_order = XNEWVEC (int, last_basic_block);
393   pre_and_rev_post_order_compute (NULL, rc_order, false);
394   for (i = 0; i < n_basic_blocks - NUM_FIXED_BLOCKS; i++)
395     bb_order[rc_order[i]] = i;
396   free (rc_order);
397
398   worklist = fibheap_new ();
399   pending = fibheap_new ();
400   visited = sbitmap_alloc (last_basic_block);
401   in_worklist = sbitmap_alloc (last_basic_block);
402   in_pending = sbitmap_alloc (last_basic_block);
403   sbitmap_zero (in_worklist);
404
405   /* Don't check outgoing edges of entry point.  */
406   sbitmap_ones (in_pending);
407   FOR_EACH_BB (bb)
408     if (BLOCK_INFO (bb)->processed)
409       RESET_BIT (in_pending, bb->index);
410     else
411       {
412         move_or_delete_vzeroupper_1 (bb, false);
413         fibheap_insert (pending, bb_order[bb->index], bb);
414       }
415
416   if (dump_file)
417     fprintf (dump_file, "Check remaining basic blocks\n");
418
419   while (!fibheap_empty (pending))
420     {
421       fibheap_swap = pending;
422       pending = worklist;
423       worklist = fibheap_swap;
424       sbitmap_swap = in_pending;
425       in_pending = in_worklist;
426       in_worklist = sbitmap_swap;
427
428       sbitmap_zero (visited);
429
430       cfun->machine->rescan_vzeroupper_p = 0;
431
432       while (!fibheap_empty (worklist))
433         {
434           bb = (basic_block) fibheap_extract_min (worklist);
435           RESET_BIT (in_worklist, bb->index);
436           gcc_assert (!TEST_BIT (visited, bb->index));
437           if (!TEST_BIT (visited, bb->index))
438             {
439               edge_iterator ei;
440
441               SET_BIT (visited, bb->index);
442
443               if (move_or_delete_vzeroupper_1 (bb, false))
444                 FOR_EACH_EDGE (e, ei, bb->succs)
445                   {
446                     if (e->dest == EXIT_BLOCK_PTR
447                         || BLOCK_INFO (e->dest)->processed)
448                       continue;
449
450                     if (TEST_BIT (visited, e->dest->index))
451                       {
452                         if (!TEST_BIT (in_pending, e->dest->index))
453                           {
454                             /* Send E->DEST to next round.  */
455                             SET_BIT (in_pending, e->dest->index);
456                             fibheap_insert (pending,
457                                             bb_order[e->dest->index],
458                                             e->dest);
459                           }
460                       }
461                     else if (!TEST_BIT (in_worklist, e->dest->index))
462                       {
463                         /* Add E->DEST to current round.  */
464                         SET_BIT (in_worklist, e->dest->index);
465                         fibheap_insert (worklist, bb_order[e->dest->index],
466                                         e->dest);
467                       }
468                   }
469             }
470         }
471
472       if (!cfun->machine->rescan_vzeroupper_p)
473         break;
474     }
475
476   free (bb_order);
477   fibheap_delete (worklist);
478   fibheap_delete (pending);
479   sbitmap_free (visited);
480   sbitmap_free (in_worklist);
481   sbitmap_free (in_pending);
482
483   if (dump_file)
484     fprintf (dump_file, "Process remaining basic blocks\n");
485
486   FOR_EACH_BB (bb)
487     move_or_delete_vzeroupper_1 (bb, true);
488
489   free_aux_for_blocks ();
490 }
491
492 static rtx legitimize_dllimport_symbol (rtx, bool);
493
494 #ifndef CHECK_STACK_LIMIT
495 #define CHECK_STACK_LIMIT (-1)
496 #endif
497
498 /* Return index of given mode in mult and division cost tables.  */
499 #define MODE_INDEX(mode)                                        \
500   ((mode) == QImode ? 0                                         \
501    : (mode) == HImode ? 1                                       \
502    : (mode) == SImode ? 2                                       \
503    : (mode) == DImode ? 3                                       \
504    : 4)
505
506 /* Processor costs (relative to an add) */
507 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
508 #define COSTS_N_BYTES(N) ((N) * 2)
509
510 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
511
512 const
513 struct processor_costs ix86_size_cost = {/* costs for tuning for size */
514   COSTS_N_BYTES (2),                    /* cost of an add instruction */
515   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
516   COSTS_N_BYTES (2),                    /* variable shift costs */
517   COSTS_N_BYTES (3),                    /* constant shift costs */
518   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
519    COSTS_N_BYTES (3),                   /*                               HI */
520    COSTS_N_BYTES (3),                   /*                               SI */
521    COSTS_N_BYTES (3),                   /*                               DI */
522    COSTS_N_BYTES (5)},                  /*                            other */
523   0,                                    /* cost of multiply per each bit set */
524   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
525    COSTS_N_BYTES (3),                   /*                          HI */
526    COSTS_N_BYTES (3),                   /*                          SI */
527    COSTS_N_BYTES (3),                   /*                          DI */
528    COSTS_N_BYTES (5)},                  /*                          other */
529   COSTS_N_BYTES (3),                    /* cost of movsx */
530   COSTS_N_BYTES (3),                    /* cost of movzx */
531   0,                                    /* "large" insn */
532   2,                                    /* MOVE_RATIO */
533   2,                                 /* cost for loading QImode using movzbl */
534   {2, 2, 2},                            /* cost of loading integer registers
535                                            in QImode, HImode and SImode.
536                                            Relative to reg-reg move (2).  */
537   {2, 2, 2},                            /* cost of storing integer registers */
538   2,                                    /* cost of reg,reg fld/fst */
539   {2, 2, 2},                            /* cost of loading fp registers
540                                            in SFmode, DFmode and XFmode */
541   {2, 2, 2},                            /* cost of storing fp registers
542                                            in SFmode, DFmode and XFmode */
543   3,                                    /* cost of moving MMX register */
544   {3, 3},                               /* cost of loading MMX registers
545                                            in SImode and DImode */
546   {3, 3},                               /* cost of storing MMX registers
547                                            in SImode and DImode */
548   3,                                    /* cost of moving SSE register */
549   {3, 3, 3},                            /* cost of loading SSE registers
550                                            in SImode, DImode and TImode */
551   {3, 3, 3},                            /* cost of storing SSE registers
552                                            in SImode, DImode and TImode */
553   3,                                    /* MMX or SSE register to integer */
554   0,                                    /* size of l1 cache  */
555   0,                                    /* size of l2 cache  */
556   0,                                    /* size of prefetch block */
557   0,                                    /* number of parallel prefetches */
558   2,                                    /* Branch cost */
559   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
560   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
561   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
562   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
563   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
564   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
565   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
566    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
567   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
568    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
569   1,                                    /* scalar_stmt_cost.  */
570   1,                                    /* scalar load_cost.  */
571   1,                                    /* scalar_store_cost.  */
572   1,                                    /* vec_stmt_cost.  */
573   1,                                    /* vec_to_scalar_cost.  */
574   1,                                    /* scalar_to_vec_cost.  */
575   1,                                    /* vec_align_load_cost.  */
576   1,                                    /* vec_unalign_load_cost.  */
577   1,                                    /* vec_store_cost.  */
578   1,                                    /* cond_taken_branch_cost.  */
579   1,                                    /* cond_not_taken_branch_cost.  */
580 };
581
582 /* Processor costs (relative to an add) */
583 static const
584 struct processor_costs i386_cost = {    /* 386 specific costs */
585   COSTS_N_INSNS (1),                    /* cost of an add instruction */
586   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
587   COSTS_N_INSNS (3),                    /* variable shift costs */
588   COSTS_N_INSNS (2),                    /* constant shift costs */
589   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
590    COSTS_N_INSNS (6),                   /*                               HI */
591    COSTS_N_INSNS (6),                   /*                               SI */
592    COSTS_N_INSNS (6),                   /*                               DI */
593    COSTS_N_INSNS (6)},                  /*                            other */
594   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
595   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
596    COSTS_N_INSNS (23),                  /*                          HI */
597    COSTS_N_INSNS (23),                  /*                          SI */
598    COSTS_N_INSNS (23),                  /*                          DI */
599    COSTS_N_INSNS (23)},                 /*                          other */
600   COSTS_N_INSNS (3),                    /* cost of movsx */
601   COSTS_N_INSNS (2),                    /* cost of movzx */
602   15,                                   /* "large" insn */
603   3,                                    /* MOVE_RATIO */
604   4,                                 /* cost for loading QImode using movzbl */
605   {2, 4, 2},                            /* cost of loading integer registers
606                                            in QImode, HImode and SImode.
607                                            Relative to reg-reg move (2).  */
608   {2, 4, 2},                            /* cost of storing integer registers */
609   2,                                    /* cost of reg,reg fld/fst */
610   {8, 8, 8},                            /* cost of loading fp registers
611                                            in SFmode, DFmode and XFmode */
612   {8, 8, 8},                            /* cost of storing fp registers
613                                            in SFmode, DFmode and XFmode */
614   2,                                    /* cost of moving MMX register */
615   {4, 8},                               /* cost of loading MMX registers
616                                            in SImode and DImode */
617   {4, 8},                               /* cost of storing MMX registers
618                                            in SImode and DImode */
619   2,                                    /* cost of moving SSE register */
620   {4, 8, 16},                           /* cost of loading SSE registers
621                                            in SImode, DImode and TImode */
622   {4, 8, 16},                           /* cost of storing SSE registers
623                                            in SImode, DImode and TImode */
624   3,                                    /* MMX or SSE register to integer */
625   0,                                    /* size of l1 cache  */
626   0,                                    /* size of l2 cache  */
627   0,                                    /* size of prefetch block */
628   0,                                    /* number of parallel prefetches */
629   1,                                    /* Branch cost */
630   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
631   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
632   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
633   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
634   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
635   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
636   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
637    DUMMY_STRINGOP_ALGS},
638   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
639    DUMMY_STRINGOP_ALGS},
640   1,                                    /* scalar_stmt_cost.  */
641   1,                                    /* scalar load_cost.  */
642   1,                                    /* scalar_store_cost.  */
643   1,                                    /* vec_stmt_cost.  */
644   1,                                    /* vec_to_scalar_cost.  */
645   1,                                    /* scalar_to_vec_cost.  */
646   1,                                    /* vec_align_load_cost.  */
647   2,                                    /* vec_unalign_load_cost.  */
648   1,                                    /* vec_store_cost.  */
649   3,                                    /* cond_taken_branch_cost.  */
650   1,                                    /* cond_not_taken_branch_cost.  */
651 };
652
653 static const
654 struct processor_costs i486_cost = {    /* 486 specific costs */
655   COSTS_N_INSNS (1),                    /* cost of an add instruction */
656   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
657   COSTS_N_INSNS (3),                    /* variable shift costs */
658   COSTS_N_INSNS (2),                    /* constant shift costs */
659   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
660    COSTS_N_INSNS (12),                  /*                               HI */
661    COSTS_N_INSNS (12),                  /*                               SI */
662    COSTS_N_INSNS (12),                  /*                               DI */
663    COSTS_N_INSNS (12)},                 /*                            other */
664   1,                                    /* cost of multiply per each bit set */
665   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
666    COSTS_N_INSNS (40),                  /*                          HI */
667    COSTS_N_INSNS (40),                  /*                          SI */
668    COSTS_N_INSNS (40),                  /*                          DI */
669    COSTS_N_INSNS (40)},                 /*                          other */
670   COSTS_N_INSNS (3),                    /* cost of movsx */
671   COSTS_N_INSNS (2),                    /* cost of movzx */
672   15,                                   /* "large" insn */
673   3,                                    /* MOVE_RATIO */
674   4,                                 /* cost for loading QImode using movzbl */
675   {2, 4, 2},                            /* cost of loading integer registers
676                                            in QImode, HImode and SImode.
677                                            Relative to reg-reg move (2).  */
678   {2, 4, 2},                            /* cost of storing integer registers */
679   2,                                    /* cost of reg,reg fld/fst */
680   {8, 8, 8},                            /* cost of loading fp registers
681                                            in SFmode, DFmode and XFmode */
682   {8, 8, 8},                            /* cost of storing fp registers
683                                            in SFmode, DFmode and XFmode */
684   2,                                    /* cost of moving MMX register */
685   {4, 8},                               /* cost of loading MMX registers
686                                            in SImode and DImode */
687   {4, 8},                               /* cost of storing MMX registers
688                                            in SImode and DImode */
689   2,                                    /* cost of moving SSE register */
690   {4, 8, 16},                           /* cost of loading SSE registers
691                                            in SImode, DImode and TImode */
692   {4, 8, 16},                           /* cost of storing SSE registers
693                                            in SImode, DImode and TImode */
694   3,                                    /* MMX or SSE register to integer */
695   4,                                    /* size of l1 cache.  486 has 8kB cache
696                                            shared for code and data, so 4kB is
697                                            not really precise.  */
698   4,                                    /* size of l2 cache  */
699   0,                                    /* size of prefetch block */
700   0,                                    /* number of parallel prefetches */
701   1,                                    /* Branch cost */
702   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
703   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
704   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
705   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
706   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
707   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
708   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
709    DUMMY_STRINGOP_ALGS},
710   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
711    DUMMY_STRINGOP_ALGS},
712   1,                                    /* scalar_stmt_cost.  */
713   1,                                    /* scalar load_cost.  */
714   1,                                    /* scalar_store_cost.  */
715   1,                                    /* vec_stmt_cost.  */
716   1,                                    /* vec_to_scalar_cost.  */
717   1,                                    /* scalar_to_vec_cost.  */
718   1,                                    /* vec_align_load_cost.  */
719   2,                                    /* vec_unalign_load_cost.  */
720   1,                                    /* vec_store_cost.  */
721   3,                                    /* cond_taken_branch_cost.  */
722   1,                                    /* cond_not_taken_branch_cost.  */
723 };
724
725 static const
726 struct processor_costs pentium_cost = {
727   COSTS_N_INSNS (1),                    /* cost of an add instruction */
728   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
729   COSTS_N_INSNS (4),                    /* variable shift costs */
730   COSTS_N_INSNS (1),                    /* constant shift costs */
731   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
732    COSTS_N_INSNS (11),                  /*                               HI */
733    COSTS_N_INSNS (11),                  /*                               SI */
734    COSTS_N_INSNS (11),                  /*                               DI */
735    COSTS_N_INSNS (11)},                 /*                            other */
736   0,                                    /* cost of multiply per each bit set */
737   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
738    COSTS_N_INSNS (25),                  /*                          HI */
739    COSTS_N_INSNS (25),                  /*                          SI */
740    COSTS_N_INSNS (25),                  /*                          DI */
741    COSTS_N_INSNS (25)},                 /*                          other */
742   COSTS_N_INSNS (3),                    /* cost of movsx */
743   COSTS_N_INSNS (2),                    /* cost of movzx */
744   8,                                    /* "large" insn */
745   6,                                    /* MOVE_RATIO */
746   6,                                 /* cost for loading QImode using movzbl */
747   {2, 4, 2},                            /* cost of loading integer registers
748                                            in QImode, HImode and SImode.
749                                            Relative to reg-reg move (2).  */
750   {2, 4, 2},                            /* cost of storing integer registers */
751   2,                                    /* cost of reg,reg fld/fst */
752   {2, 2, 6},                            /* cost of loading fp registers
753                                            in SFmode, DFmode and XFmode */
754   {4, 4, 6},                            /* cost of storing fp registers
755                                            in SFmode, DFmode and XFmode */
756   8,                                    /* cost of moving MMX register */
757   {8, 8},                               /* cost of loading MMX registers
758                                            in SImode and DImode */
759   {8, 8},                               /* cost of storing MMX registers
760                                            in SImode and DImode */
761   2,                                    /* cost of moving SSE register */
762   {4, 8, 16},                           /* cost of loading SSE registers
763                                            in SImode, DImode and TImode */
764   {4, 8, 16},                           /* cost of storing SSE registers
765                                            in SImode, DImode and TImode */
766   3,                                    /* MMX or SSE register to integer */
767   8,                                    /* size of l1 cache.  */
768   8,                                    /* size of l2 cache  */
769   0,                                    /* size of prefetch block */
770   0,                                    /* number of parallel prefetches */
771   2,                                    /* Branch cost */
772   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
773   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
774   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
775   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
776   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
777   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
778   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
779    DUMMY_STRINGOP_ALGS},
780   {{libcall, {{-1, rep_prefix_4_byte}}},
781    DUMMY_STRINGOP_ALGS},
782   1,                                    /* scalar_stmt_cost.  */
783   1,                                    /* scalar load_cost.  */
784   1,                                    /* scalar_store_cost.  */
785   1,                                    /* vec_stmt_cost.  */
786   1,                                    /* vec_to_scalar_cost.  */
787   1,                                    /* scalar_to_vec_cost.  */
788   1,                                    /* vec_align_load_cost.  */
789   2,                                    /* vec_unalign_load_cost.  */
790   1,                                    /* vec_store_cost.  */
791   3,                                    /* cond_taken_branch_cost.  */
792   1,                                    /* cond_not_taken_branch_cost.  */
793 };
794
795 static const
796 struct processor_costs pentiumpro_cost = {
797   COSTS_N_INSNS (1),                    /* cost of an add instruction */
798   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
799   COSTS_N_INSNS (1),                    /* variable shift costs */
800   COSTS_N_INSNS (1),                    /* constant shift costs */
801   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
802    COSTS_N_INSNS (4),                   /*                               HI */
803    COSTS_N_INSNS (4),                   /*                               SI */
804    COSTS_N_INSNS (4),                   /*                               DI */
805    COSTS_N_INSNS (4)},                  /*                            other */
806   0,                                    /* cost of multiply per each bit set */
807   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
808    COSTS_N_INSNS (17),                  /*                          HI */
809    COSTS_N_INSNS (17),                  /*                          SI */
810    COSTS_N_INSNS (17),                  /*                          DI */
811    COSTS_N_INSNS (17)},                 /*                          other */
812   COSTS_N_INSNS (1),                    /* cost of movsx */
813   COSTS_N_INSNS (1),                    /* cost of movzx */
814   8,                                    /* "large" insn */
815   6,                                    /* MOVE_RATIO */
816   2,                                 /* cost for loading QImode using movzbl */
817   {4, 4, 4},                            /* cost of loading integer registers
818                                            in QImode, HImode and SImode.
819                                            Relative to reg-reg move (2).  */
820   {2, 2, 2},                            /* cost of storing integer registers */
821   2,                                    /* cost of reg,reg fld/fst */
822   {2, 2, 6},                            /* cost of loading fp registers
823                                            in SFmode, DFmode and XFmode */
824   {4, 4, 6},                            /* cost of storing fp registers
825                                            in SFmode, DFmode and XFmode */
826   2,                                    /* cost of moving MMX register */
827   {2, 2},                               /* cost of loading MMX registers
828                                            in SImode and DImode */
829   {2, 2},                               /* cost of storing MMX registers
830                                            in SImode and DImode */
831   2,                                    /* cost of moving SSE register */
832   {2, 2, 8},                            /* cost of loading SSE registers
833                                            in SImode, DImode and TImode */
834   {2, 2, 8},                            /* cost of storing SSE registers
835                                            in SImode, DImode and TImode */
836   3,                                    /* MMX or SSE register to integer */
837   8,                                    /* size of l1 cache.  */
838   256,                                  /* size of l2 cache  */
839   32,                                   /* size of prefetch block */
840   6,                                    /* number of parallel prefetches */
841   2,                                    /* Branch cost */
842   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
843   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
844   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
845   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
846   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
847   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
848   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes
849      (we ensure the alignment).  For small blocks inline loop is still a
850      noticeable win, for bigger blocks either rep movsl or rep movsb is
851      way to go.  Rep movsb has apparently more expensive startup time in CPU,
852      but after 4K the difference is down in the noise.  */
853   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
854                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
855    DUMMY_STRINGOP_ALGS},
856   {{rep_prefix_4_byte, {{1024, unrolled_loop},
857                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
858    DUMMY_STRINGOP_ALGS},
859   1,                                    /* scalar_stmt_cost.  */
860   1,                                    /* scalar load_cost.  */
861   1,                                    /* scalar_store_cost.  */
862   1,                                    /* vec_stmt_cost.  */
863   1,                                    /* vec_to_scalar_cost.  */
864   1,                                    /* scalar_to_vec_cost.  */
865   1,                                    /* vec_align_load_cost.  */
866   2,                                    /* vec_unalign_load_cost.  */
867   1,                                    /* vec_store_cost.  */
868   3,                                    /* cond_taken_branch_cost.  */
869   1,                                    /* cond_not_taken_branch_cost.  */
870 };
871
872 static const
873 struct processor_costs geode_cost = {
874   COSTS_N_INSNS (1),                    /* cost of an add instruction */
875   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
876   COSTS_N_INSNS (2),                    /* variable shift costs */
877   COSTS_N_INSNS (1),                    /* constant shift costs */
878   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
879    COSTS_N_INSNS (4),                   /*                               HI */
880    COSTS_N_INSNS (7),                   /*                               SI */
881    COSTS_N_INSNS (7),                   /*                               DI */
882    COSTS_N_INSNS (7)},                  /*                            other */
883   0,                                    /* cost of multiply per each bit set */
884   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
885    COSTS_N_INSNS (23),                  /*                          HI */
886    COSTS_N_INSNS (39),                  /*                          SI */
887    COSTS_N_INSNS (39),                  /*                          DI */
888    COSTS_N_INSNS (39)},                 /*                          other */
889   COSTS_N_INSNS (1),                    /* cost of movsx */
890   COSTS_N_INSNS (1),                    /* cost of movzx */
891   8,                                    /* "large" insn */
892   4,                                    /* MOVE_RATIO */
893   1,                                 /* cost for loading QImode using movzbl */
894   {1, 1, 1},                            /* cost of loading integer registers
895                                            in QImode, HImode and SImode.
896                                            Relative to reg-reg move (2).  */
897   {1, 1, 1},                            /* cost of storing integer registers */
898   1,                                    /* cost of reg,reg fld/fst */
899   {1, 1, 1},                            /* cost of loading fp registers
900                                            in SFmode, DFmode and XFmode */
901   {4, 6, 6},                            /* cost of storing fp registers
902                                            in SFmode, DFmode and XFmode */
903
904   1,                                    /* cost of moving MMX register */
905   {1, 1},                               /* cost of loading MMX registers
906                                            in SImode and DImode */
907   {1, 1},                               /* cost of storing MMX registers
908                                            in SImode and DImode */
909   1,                                    /* cost of moving SSE register */
910   {1, 1, 1},                            /* cost of loading SSE registers
911                                            in SImode, DImode and TImode */
912   {1, 1, 1},                            /* cost of storing SSE registers
913                                            in SImode, DImode and TImode */
914   1,                                    /* MMX or SSE register to integer */
915   64,                                   /* size of l1 cache.  */
916   128,                                  /* size of l2 cache.  */
917   32,                                   /* size of prefetch block */
918   1,                                    /* number of parallel prefetches */
919   1,                                    /* Branch cost */
920   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
921   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
922   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
923   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
924   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
925   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
926   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
927    DUMMY_STRINGOP_ALGS},
928   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
929    DUMMY_STRINGOP_ALGS},
930   1,                                    /* scalar_stmt_cost.  */
931   1,                                    /* scalar load_cost.  */
932   1,                                    /* scalar_store_cost.  */
933   1,                                    /* vec_stmt_cost.  */
934   1,                                    /* vec_to_scalar_cost.  */
935   1,                                    /* scalar_to_vec_cost.  */
936   1,                                    /* vec_align_load_cost.  */
937   2,                                    /* vec_unalign_load_cost.  */
938   1,                                    /* vec_store_cost.  */
939   3,                                    /* cond_taken_branch_cost.  */
940   1,                                    /* cond_not_taken_branch_cost.  */
941 };
942
943 static const
944 struct processor_costs k6_cost = {
945   COSTS_N_INSNS (1),                    /* cost of an add instruction */
946   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
947   COSTS_N_INSNS (1),                    /* variable shift costs */
948   COSTS_N_INSNS (1),                    /* constant shift costs */
949   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
950    COSTS_N_INSNS (3),                   /*                               HI */
951    COSTS_N_INSNS (3),                   /*                               SI */
952    COSTS_N_INSNS (3),                   /*                               DI */
953    COSTS_N_INSNS (3)},                  /*                            other */
954   0,                                    /* cost of multiply per each bit set */
955   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
956    COSTS_N_INSNS (18),                  /*                          HI */
957    COSTS_N_INSNS (18),                  /*                          SI */
958    COSTS_N_INSNS (18),                  /*                          DI */
959    COSTS_N_INSNS (18)},                 /*                          other */
960   COSTS_N_INSNS (2),                    /* cost of movsx */
961   COSTS_N_INSNS (2),                    /* cost of movzx */
962   8,                                    /* "large" insn */
963   4,                                    /* MOVE_RATIO */
964   3,                                 /* cost for loading QImode using movzbl */
965   {4, 5, 4},                            /* cost of loading integer registers
966                                            in QImode, HImode and SImode.
967                                            Relative to reg-reg move (2).  */
968   {2, 3, 2},                            /* cost of storing integer registers */
969   4,                                    /* cost of reg,reg fld/fst */
970   {6, 6, 6},                            /* cost of loading fp registers
971                                            in SFmode, DFmode and XFmode */
972   {4, 4, 4},                            /* cost of storing fp registers
973                                            in SFmode, DFmode and XFmode */
974   2,                                    /* cost of moving MMX register */
975   {2, 2},                               /* cost of loading MMX registers
976                                            in SImode and DImode */
977   {2, 2},                               /* cost of storing MMX registers
978                                            in SImode and DImode */
979   2,                                    /* cost of moving SSE register */
980   {2, 2, 8},                            /* cost of loading SSE registers
981                                            in SImode, DImode and TImode */
982   {2, 2, 8},                            /* cost of storing SSE registers
983                                            in SImode, DImode and TImode */
984   6,                                    /* MMX or SSE register to integer */
985   32,                                   /* size of l1 cache.  */
986   32,                                   /* size of l2 cache.  Some models
987                                            have integrated l2 cache, but
988                                            optimizing for k6 is not important
989                                            enough to worry about that.  */
990   32,                                   /* size of prefetch block */
991   1,                                    /* number of parallel prefetches */
992   1,                                    /* Branch cost */
993   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
994   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
995   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
996   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
997   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
998   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
999   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
1000    DUMMY_STRINGOP_ALGS},
1001   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
1002    DUMMY_STRINGOP_ALGS},
1003   1,                                    /* scalar_stmt_cost.  */
1004   1,                                    /* scalar load_cost.  */
1005   1,                                    /* scalar_store_cost.  */
1006   1,                                    /* vec_stmt_cost.  */
1007   1,                                    /* vec_to_scalar_cost.  */
1008   1,                                    /* scalar_to_vec_cost.  */
1009   1,                                    /* vec_align_load_cost.  */
1010   2,                                    /* vec_unalign_load_cost.  */
1011   1,                                    /* vec_store_cost.  */
1012   3,                                    /* cond_taken_branch_cost.  */
1013   1,                                    /* cond_not_taken_branch_cost.  */
1014 };
1015
1016 static const
1017 struct processor_costs athlon_cost = {
1018   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1019   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1020   COSTS_N_INSNS (1),                    /* variable shift costs */
1021   COSTS_N_INSNS (1),                    /* constant shift costs */
1022   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
1023    COSTS_N_INSNS (5),                   /*                               HI */
1024    COSTS_N_INSNS (5),                   /*                               SI */
1025    COSTS_N_INSNS (5),                   /*                               DI */
1026    COSTS_N_INSNS (5)},                  /*                            other */
1027   0,                                    /* cost of multiply per each bit set */
1028   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1029    COSTS_N_INSNS (26),                  /*                          HI */
1030    COSTS_N_INSNS (42),                  /*                          SI */
1031    COSTS_N_INSNS (74),                  /*                          DI */
1032    COSTS_N_INSNS (74)},                 /*                          other */
1033   COSTS_N_INSNS (1),                    /* cost of movsx */
1034   COSTS_N_INSNS (1),                    /* cost of movzx */
1035   8,                                    /* "large" insn */
1036   9,                                    /* MOVE_RATIO */
1037   4,                                 /* cost for loading QImode using movzbl */
1038   {3, 4, 3},                            /* cost of loading integer registers
1039                                            in QImode, HImode and SImode.
1040                                            Relative to reg-reg move (2).  */
1041   {3, 4, 3},                            /* cost of storing integer registers */
1042   4,                                    /* cost of reg,reg fld/fst */
1043   {4, 4, 12},                           /* cost of loading fp registers
1044                                            in SFmode, DFmode and XFmode */
1045   {6, 6, 8},                            /* cost of storing fp registers
1046                                            in SFmode, DFmode and XFmode */
1047   2,                                    /* cost of moving MMX register */
1048   {4, 4},                               /* cost of loading MMX registers
1049                                            in SImode and DImode */
1050   {4, 4},                               /* cost of storing MMX registers
1051                                            in SImode and DImode */
1052   2,                                    /* cost of moving SSE register */
1053   {4, 4, 6},                            /* cost of loading SSE registers
1054                                            in SImode, DImode and TImode */
1055   {4, 4, 5},                            /* cost of storing SSE registers
1056                                            in SImode, DImode and TImode */
1057   5,                                    /* MMX or SSE register to integer */
1058   64,                                   /* size of l1 cache.  */
1059   256,                                  /* size of l2 cache.  */
1060   64,                                   /* size of prefetch block */
1061   6,                                    /* number of parallel prefetches */
1062   5,                                    /* Branch cost */
1063   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1064   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1065   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
1066   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1067   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1068   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1069   /* For some reason, Athlon deals better with REP prefix (relative to loops)
1070      compared to K8. Alignment becomes important after 8 bytes for memcpy and
1071      128 bytes for memset.  */
1072   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
1073    DUMMY_STRINGOP_ALGS},
1074   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
1075    DUMMY_STRINGOP_ALGS},
1076   1,                                    /* scalar_stmt_cost.  */
1077   1,                                    /* scalar load_cost.  */
1078   1,                                    /* scalar_store_cost.  */
1079   1,                                    /* vec_stmt_cost.  */
1080   1,                                    /* vec_to_scalar_cost.  */
1081   1,                                    /* scalar_to_vec_cost.  */
1082   1,                                    /* vec_align_load_cost.  */
1083   2,                                    /* vec_unalign_load_cost.  */
1084   1,                                    /* vec_store_cost.  */
1085   3,                                    /* cond_taken_branch_cost.  */
1086   1,                                    /* cond_not_taken_branch_cost.  */
1087 };
1088
1089 static const
1090 struct processor_costs k8_cost = {
1091   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1092   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1093   COSTS_N_INSNS (1),                    /* variable shift costs */
1094   COSTS_N_INSNS (1),                    /* constant shift costs */
1095   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1096    COSTS_N_INSNS (4),                   /*                               HI */
1097    COSTS_N_INSNS (3),                   /*                               SI */
1098    COSTS_N_INSNS (4),                   /*                               DI */
1099    COSTS_N_INSNS (5)},                  /*                            other */
1100   0,                                    /* cost of multiply per each bit set */
1101   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1102    COSTS_N_INSNS (26),                  /*                          HI */
1103    COSTS_N_INSNS (42),                  /*                          SI */
1104    COSTS_N_INSNS (74),                  /*                          DI */
1105    COSTS_N_INSNS (74)},                 /*                          other */
1106   COSTS_N_INSNS (1),                    /* cost of movsx */
1107   COSTS_N_INSNS (1),                    /* cost of movzx */
1108   8,                                    /* "large" insn */
1109   9,                                    /* MOVE_RATIO */
1110   4,                                 /* cost for loading QImode using movzbl */
1111   {3, 4, 3},                            /* cost of loading integer registers
1112                                            in QImode, HImode and SImode.
1113                                            Relative to reg-reg move (2).  */
1114   {3, 4, 3},                            /* cost of storing integer registers */
1115   4,                                    /* cost of reg,reg fld/fst */
1116   {4, 4, 12},                           /* cost of loading fp registers
1117                                            in SFmode, DFmode and XFmode */
1118   {6, 6, 8},                            /* cost of storing fp registers
1119                                            in SFmode, DFmode and XFmode */
1120   2,                                    /* cost of moving MMX register */
1121   {3, 3},                               /* cost of loading MMX registers
1122                                            in SImode and DImode */
1123   {4, 4},                               /* cost of storing MMX registers
1124                                            in SImode and DImode */
1125   2,                                    /* cost of moving SSE register */
1126   {4, 3, 6},                            /* cost of loading SSE registers
1127                                            in SImode, DImode and TImode */
1128   {4, 4, 5},                            /* cost of storing SSE registers
1129                                            in SImode, DImode and TImode */
1130   5,                                    /* MMX or SSE register to integer */
1131   64,                                   /* size of l1 cache.  */
1132   512,                                  /* size of l2 cache.  */
1133   64,                                   /* size of prefetch block */
1134   /* New AMD processors never drop prefetches; if they cannot be performed
1135      immediately, they are queued.  We set number of simultaneous prefetches
1136      to a large constant to reflect this (it probably is not a good idea not
1137      to limit number of prefetches at all, as their execution also takes some
1138      time).  */
1139   100,                                  /* number of parallel prefetches */
1140   3,                                    /* Branch cost */
1141   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1142   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1143   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1144   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1145   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1146   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1147   /* K8 has optimized REP instruction for medium sized blocks, but for very
1148      small blocks it is better to use loop. For large blocks, libcall can
1149      do nontemporary accesses and beat inline considerably.  */
1150   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1151    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1152   {{libcall, {{8, loop}, {24, unrolled_loop},
1153               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1154    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1155   4,                                    /* scalar_stmt_cost.  */
1156   2,                                    /* scalar load_cost.  */
1157   2,                                    /* scalar_store_cost.  */
1158   5,                                    /* vec_stmt_cost.  */
1159   0,                                    /* vec_to_scalar_cost.  */
1160   2,                                    /* scalar_to_vec_cost.  */
1161   2,                                    /* vec_align_load_cost.  */
1162   3,                                    /* vec_unalign_load_cost.  */
1163   3,                                    /* vec_store_cost.  */
1164   3,                                    /* cond_taken_branch_cost.  */
1165   2,                                    /* cond_not_taken_branch_cost.  */
1166 };
1167
1168 struct processor_costs amdfam10_cost = {
1169   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1170   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1171   COSTS_N_INSNS (1),                    /* variable shift costs */
1172   COSTS_N_INSNS (1),                    /* constant shift costs */
1173   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1174    COSTS_N_INSNS (4),                   /*                               HI */
1175    COSTS_N_INSNS (3),                   /*                               SI */
1176    COSTS_N_INSNS (4),                   /*                               DI */
1177    COSTS_N_INSNS (5)},                  /*                            other */
1178   0,                                    /* cost of multiply per each bit set */
1179   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1180    COSTS_N_INSNS (35),                  /*                          HI */
1181    COSTS_N_INSNS (51),                  /*                          SI */
1182    COSTS_N_INSNS (83),                  /*                          DI */
1183    COSTS_N_INSNS (83)},                 /*                          other */
1184   COSTS_N_INSNS (1),                    /* cost of movsx */
1185   COSTS_N_INSNS (1),                    /* cost of movzx */
1186   8,                                    /* "large" insn */
1187   9,                                    /* MOVE_RATIO */
1188   4,                                 /* cost for loading QImode using movzbl */
1189   {3, 4, 3},                            /* cost of loading integer registers
1190                                            in QImode, HImode and SImode.
1191                                            Relative to reg-reg move (2).  */
1192   {3, 4, 3},                            /* cost of storing integer registers */
1193   4,                                    /* cost of reg,reg fld/fst */
1194   {4, 4, 12},                           /* cost of loading fp registers
1195                                            in SFmode, DFmode and XFmode */
1196   {6, 6, 8},                            /* cost of storing fp registers
1197                                            in SFmode, DFmode and XFmode */
1198   2,                                    /* cost of moving MMX register */
1199   {3, 3},                               /* cost of loading MMX registers
1200                                            in SImode and DImode */
1201   {4, 4},                               /* cost of storing MMX registers
1202                                            in SImode and DImode */
1203   2,                                    /* cost of moving SSE register */
1204   {4, 4, 3},                            /* cost of loading SSE registers
1205                                            in SImode, DImode and TImode */
1206   {4, 4, 5},                            /* cost of storing SSE registers
1207                                            in SImode, DImode and TImode */
1208   3,                                    /* MMX or SSE register to integer */
1209                                         /* On K8:
1210                                             MOVD reg64, xmmreg Double FSTORE 4
1211                                             MOVD reg32, xmmreg Double FSTORE 4
1212                                            On AMDFAM10:
1213                                             MOVD reg64, xmmreg Double FADD 3
1214                                                                1/1  1/1
1215                                             MOVD reg32, xmmreg Double FADD 3
1216                                                                1/1  1/1 */
1217   64,                                   /* size of l1 cache.  */
1218   512,                                  /* size of l2 cache.  */
1219   64,                                   /* size of prefetch block */
1220   /* New AMD processors never drop prefetches; if they cannot be performed
1221      immediately, they are queued.  We set number of simultaneous prefetches
1222      to a large constant to reflect this (it probably is not a good idea not
1223      to limit number of prefetches at all, as their execution also takes some
1224      time).  */
1225   100,                                  /* number of parallel prefetches */
1226   2,                                    /* Branch cost */
1227   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1228   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1229   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1230   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1231   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1232   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1233
1234   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
1235      very small blocks it is better to use loop. For large blocks, libcall can
1236      do nontemporary accesses and beat inline considerably.  */
1237   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1238    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1239   {{libcall, {{8, loop}, {24, unrolled_loop},
1240               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1241    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1242   4,                                    /* scalar_stmt_cost.  */
1243   2,                                    /* scalar load_cost.  */
1244   2,                                    /* scalar_store_cost.  */
1245   6,                                    /* vec_stmt_cost.  */
1246   0,                                    /* vec_to_scalar_cost.  */
1247   2,                                    /* scalar_to_vec_cost.  */
1248   2,                                    /* vec_align_load_cost.  */
1249   2,                                    /* vec_unalign_load_cost.  */
1250   2,                                    /* vec_store_cost.  */
1251   2,                                    /* cond_taken_branch_cost.  */
1252   1,                                    /* cond_not_taken_branch_cost.  */
1253 };
1254
1255 struct processor_costs bdver1_cost = {
1256   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1257   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1258   COSTS_N_INSNS (1),                    /* variable shift costs */
1259   COSTS_N_INSNS (1),                    /* constant shift costs */
1260   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
1261    COSTS_N_INSNS (4),                   /*                               HI */
1262    COSTS_N_INSNS (4),                   /*                               SI */
1263    COSTS_N_INSNS (6),                   /*                               DI */
1264    COSTS_N_INSNS (6)},                  /*                            other */
1265   0,                                    /* cost of multiply per each bit set */
1266   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1267    COSTS_N_INSNS (35),                  /*                          HI */
1268    COSTS_N_INSNS (51),                  /*                          SI */
1269    COSTS_N_INSNS (83),                  /*                          DI */
1270    COSTS_N_INSNS (83)},                 /*                          other */
1271   COSTS_N_INSNS (1),                    /* cost of movsx */
1272   COSTS_N_INSNS (1),                    /* cost of movzx */
1273   8,                                    /* "large" insn */
1274   9,                                    /* MOVE_RATIO */
1275   4,                                 /* cost for loading QImode using movzbl */
1276   {5, 5, 4},                            /* cost of loading integer registers
1277                                            in QImode, HImode and SImode.
1278                                            Relative to reg-reg move (2).  */
1279   {4, 4, 4},                            /* cost of storing integer registers */
1280   2,                                    /* cost of reg,reg fld/fst */
1281   {5, 5, 12},                           /* cost of loading fp registers
1282                                            in SFmode, DFmode and XFmode */
1283   {4, 4, 8},                            /* cost of storing fp registers
1284                                            in SFmode, DFmode and XFmode */
1285   2,                                    /* cost of moving MMX register */
1286   {4, 4},                               /* cost of loading MMX registers
1287                                            in SImode and DImode */
1288   {4, 4},                               /* cost of storing MMX registers
1289                                            in SImode and DImode */
1290   2,                                    /* cost of moving SSE register */
1291   {4, 4, 4},                            /* cost of loading SSE registers
1292                                            in SImode, DImode and TImode */
1293   {4, 4, 4},                            /* cost of storing SSE registers
1294                                            in SImode, DImode and TImode */
1295   2,                                    /* MMX or SSE register to integer */
1296                                         /* On K8:
1297                                             MOVD reg64, xmmreg Double FSTORE 4
1298                                             MOVD reg32, xmmreg Double FSTORE 4
1299                                            On AMDFAM10:
1300                                             MOVD reg64, xmmreg Double FADD 3
1301                                                                1/1  1/1
1302                                             MOVD reg32, xmmreg Double FADD 3
1303                                                                1/1  1/1 */
1304   16,                                   /* size of l1 cache.  */
1305   2048,                                 /* size of l2 cache.  */
1306   64,                                   /* size of prefetch block */
1307   /* New AMD processors never drop prefetches; if they cannot be performed
1308      immediately, they are queued.  We set number of simultaneous prefetches
1309      to a large constant to reflect this (it probably is not a good idea not
1310      to limit number of prefetches at all, as their execution also takes some
1311      time).  */
1312   100,                                  /* number of parallel prefetches */
1313   2,                                    /* Branch cost */
1314   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1315   COSTS_N_INSNS (6),                    /* cost of FMUL instruction.  */
1316   COSTS_N_INSNS (42),                   /* cost of FDIV instruction.  */
1317   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1318   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1319   COSTS_N_INSNS (52),                   /* cost of FSQRT instruction.  */
1320
1321   /*  BDVER1 has optimized REP instruction for medium sized blocks, but for
1322       very small blocks it is better to use loop. For large blocks, libcall
1323       can do nontemporary accesses and beat inline considerably.  */
1324   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1325    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1326   {{libcall, {{8, loop}, {24, unrolled_loop},
1327               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1328    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1329   6,                                    /* scalar_stmt_cost.  */
1330   4,                                    /* scalar load_cost.  */
1331   4,                                    /* scalar_store_cost.  */
1332   6,                                    /* vec_stmt_cost.  */
1333   0,                                    /* vec_to_scalar_cost.  */
1334   2,                                    /* scalar_to_vec_cost.  */
1335   4,                                    /* vec_align_load_cost.  */
1336   4,                                    /* vec_unalign_load_cost.  */
1337   4,                                    /* vec_store_cost.  */
1338   2,                                    /* cond_taken_branch_cost.  */
1339   1,                                    /* cond_not_taken_branch_cost.  */
1340 };
1341
1342 struct processor_costs bdver2_cost = {
1343   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1344   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1345   COSTS_N_INSNS (1),                    /* variable shift costs */
1346   COSTS_N_INSNS (1),                    /* constant shift costs */
1347   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
1348    COSTS_N_INSNS (4),                   /*                               HI */
1349    COSTS_N_INSNS (4),                   /*                               SI */
1350    COSTS_N_INSNS (6),                   /*                               DI */
1351    COSTS_N_INSNS (6)},                  /*                            other */
1352   0,                                    /* cost of multiply per each bit set */
1353   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1354    COSTS_N_INSNS (35),                  /*                          HI */
1355    COSTS_N_INSNS (51),                  /*                          SI */
1356    COSTS_N_INSNS (83),                  /*                          DI */
1357    COSTS_N_INSNS (83)},                 /*                          other */
1358   COSTS_N_INSNS (1),                    /* cost of movsx */
1359   COSTS_N_INSNS (1),                    /* cost of movzx */
1360   8,                                    /* "large" insn */
1361   9,                                    /* MOVE_RATIO */
1362   4,                                 /* cost for loading QImode using movzbl */
1363   {5, 5, 4},                            /* cost of loading integer registers
1364                                            in QImode, HImode and SImode.
1365                                            Relative to reg-reg move (2).  */
1366   {4, 4, 4},                            /* cost of storing integer registers */
1367   2,                                    /* cost of reg,reg fld/fst */
1368   {5, 5, 12},                           /* cost of loading fp registers
1369                                            in SFmode, DFmode and XFmode */
1370   {4, 4, 8},                            /* cost of storing fp registers
1371                                            in SFmode, DFmode and XFmode */
1372   2,                                    /* cost of moving MMX register */
1373   {4, 4},                               /* cost of loading MMX registers
1374                                            in SImode and DImode */
1375   {4, 4},                               /* cost of storing MMX registers
1376                                            in SImode and DImode */
1377   2,                                    /* cost of moving SSE register */
1378   {4, 4, 4},                            /* cost of loading SSE registers
1379                                            in SImode, DImode and TImode */
1380   {4, 4, 4},                            /* cost of storing SSE registers
1381                                            in SImode, DImode and TImode */
1382   2,                                    /* MMX or SSE register to integer */
1383                                         /* On K8:
1384                                             MOVD reg64, xmmreg Double FSTORE 4
1385                                             MOVD reg32, xmmreg Double FSTORE 4
1386                                            On AMDFAM10:
1387                                             MOVD reg64, xmmreg Double FADD 3
1388                                                                1/1  1/1
1389                                             MOVD reg32, xmmreg Double FADD 3
1390                                                                1/1  1/1 */
1391   16,                                   /* size of l1 cache.  */
1392   2048,                                 /* size of l2 cache.  */
1393   64,                                   /* size of prefetch block */
1394   /* New AMD processors never drop prefetches; if they cannot be performed
1395      immediately, they are queued.  We set number of simultaneous prefetches
1396      to a large constant to reflect this (it probably is not a good idea not
1397      to limit number of prefetches at all, as their execution also takes some
1398      time).  */
1399   100,                                  /* number of parallel prefetches */
1400   2,                                    /* Branch cost */
1401   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1402   COSTS_N_INSNS (6),                    /* cost of FMUL instruction.  */
1403   COSTS_N_INSNS (42),                   /* cost of FDIV instruction.  */
1404   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1405   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1406   COSTS_N_INSNS (52),                   /* cost of FSQRT instruction.  */
1407
1408   /*  BDVER2 has optimized REP instruction for medium sized blocks, but for
1409       very small blocks it is better to use loop. For large blocks, libcall
1410       can do nontemporary accesses and beat inline considerably.  */
1411   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1412    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1413   {{libcall, {{8, loop}, {24, unrolled_loop},
1414               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1415    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1416   6,                                    /* scalar_stmt_cost.  */
1417   4,                                    /* scalar load_cost.  */
1418   4,                                    /* scalar_store_cost.  */
1419   6,                                    /* vec_stmt_cost.  */
1420   0,                                    /* vec_to_scalar_cost.  */
1421   2,                                    /* scalar_to_vec_cost.  */
1422   4,                                    /* vec_align_load_cost.  */
1423   4,                                    /* vec_unalign_load_cost.  */
1424   4,                                    /* vec_store_cost.  */
1425   2,                                    /* cond_taken_branch_cost.  */
1426   1,                                    /* cond_not_taken_branch_cost.  */
1427 };
1428
1429 struct processor_costs btver1_cost = {
1430   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1431   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1432   COSTS_N_INSNS (1),                    /* variable shift costs */
1433   COSTS_N_INSNS (1),                    /* constant shift costs */
1434   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1435    COSTS_N_INSNS (4),                   /*                               HI */
1436    COSTS_N_INSNS (3),                   /*                               SI */
1437    COSTS_N_INSNS (4),                   /*                               DI */
1438    COSTS_N_INSNS (5)},                  /*                            other */
1439   0,                                    /* cost of multiply per each bit set */
1440   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1441    COSTS_N_INSNS (35),                  /*                          HI */
1442    COSTS_N_INSNS (51),                  /*                          SI */
1443    COSTS_N_INSNS (83),                  /*                          DI */
1444    COSTS_N_INSNS (83)},                 /*                          other */
1445   COSTS_N_INSNS (1),                    /* cost of movsx */
1446   COSTS_N_INSNS (1),                    /* cost of movzx */
1447   8,                                    /* "large" insn */
1448   9,                                    /* MOVE_RATIO */
1449   4,                                 /* cost for loading QImode using movzbl */
1450   {3, 4, 3},                            /* cost of loading integer registers
1451                                            in QImode, HImode and SImode.
1452                                            Relative to reg-reg move (2).  */
1453   {3, 4, 3},                            /* cost of storing integer registers */
1454   4,                                    /* cost of reg,reg fld/fst */
1455   {4, 4, 12},                           /* cost of loading fp registers
1456                                            in SFmode, DFmode and XFmode */
1457   {6, 6, 8},                            /* cost of storing fp registers
1458                                            in SFmode, DFmode and XFmode */
1459   2,                                    /* cost of moving MMX register */
1460   {3, 3},                               /* cost of loading MMX registers
1461                                            in SImode and DImode */
1462   {4, 4},                               /* cost of storing MMX registers
1463                                            in SImode and DImode */
1464   2,                                    /* cost of moving SSE register */
1465   {4, 4, 3},                            /* cost of loading SSE registers
1466                                            in SImode, DImode and TImode */
1467   {4, 4, 5},                            /* cost of storing SSE registers
1468                                            in SImode, DImode and TImode */
1469   3,                                    /* MMX or SSE register to integer */
1470                                         /* On K8:
1471                                            MOVD reg64, xmmreg Double FSTORE 4
1472                                            MOVD reg32, xmmreg Double FSTORE 4
1473                                            On AMDFAM10:
1474                                            MOVD reg64, xmmreg Double FADD 3
1475                                                                1/1  1/1
1476                                             MOVD reg32, xmmreg Double FADD 3
1477                                                                1/1  1/1 */
1478   32,                                   /* size of l1 cache.  */
1479   512,                                  /* size of l2 cache.  */
1480   64,                                   /* size of prefetch block */
1481   100,                                  /* number of parallel prefetches */
1482   2,                                    /* Branch cost */
1483   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1484   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1485   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1486   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1487   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1488   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1489
1490   /* BTVER1 has optimized REP instruction for medium sized blocks, but for
1491      very small blocks it is better to use loop. For large blocks, libcall can
1492      do nontemporary accesses and beat inline considerably.  */
1493   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1494    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1495   {{libcall, {{8, loop}, {24, unrolled_loop},
1496               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1497    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1498   4,                                    /* scalar_stmt_cost.  */
1499   2,                                    /* scalar load_cost.  */
1500   2,                                    /* scalar_store_cost.  */
1501   6,                                    /* vec_stmt_cost.  */
1502   0,                                    /* vec_to_scalar_cost.  */
1503   2,                                    /* scalar_to_vec_cost.  */
1504   2,                                    /* vec_align_load_cost.  */
1505   2,                                    /* vec_unalign_load_cost.  */
1506   2,                                    /* vec_store_cost.  */
1507   2,                                    /* cond_taken_branch_cost.  */
1508   1,                                    /* cond_not_taken_branch_cost.  */
1509 };
1510
1511 static const
1512 struct processor_costs pentium4_cost = {
1513   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1514   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
1515   COSTS_N_INSNS (4),                    /* variable shift costs */
1516   COSTS_N_INSNS (4),                    /* constant shift costs */
1517   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
1518    COSTS_N_INSNS (15),                  /*                               HI */
1519    COSTS_N_INSNS (15),                  /*                               SI */
1520    COSTS_N_INSNS (15),                  /*                               DI */
1521    COSTS_N_INSNS (15)},                 /*                            other */
1522   0,                                    /* cost of multiply per each bit set */
1523   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
1524    COSTS_N_INSNS (56),                  /*                          HI */
1525    COSTS_N_INSNS (56),                  /*                          SI */
1526    COSTS_N_INSNS (56),                  /*                          DI */
1527    COSTS_N_INSNS (56)},                 /*                          other */
1528   COSTS_N_INSNS (1),                    /* cost of movsx */
1529   COSTS_N_INSNS (1),                    /* cost of movzx */
1530   16,                                   /* "large" insn */
1531   6,                                    /* MOVE_RATIO */
1532   2,                                 /* cost for loading QImode using movzbl */
1533   {4, 5, 4},                            /* cost of loading integer registers
1534                                            in QImode, HImode and SImode.
1535                                            Relative to reg-reg move (2).  */
1536   {2, 3, 2},                            /* cost of storing integer registers */
1537   2,                                    /* cost of reg,reg fld/fst */
1538   {2, 2, 6},                            /* cost of loading fp registers
1539                                            in SFmode, DFmode and XFmode */
1540   {4, 4, 6},                            /* cost of storing fp registers
1541                                            in SFmode, DFmode and XFmode */
1542   2,                                    /* cost of moving MMX register */
1543   {2, 2},                               /* cost of loading MMX registers
1544                                            in SImode and DImode */
1545   {2, 2},                               /* cost of storing MMX registers
1546                                            in SImode and DImode */
1547   12,                                   /* cost of moving SSE register */
1548   {12, 12, 12},                         /* cost of loading SSE registers
1549                                            in SImode, DImode and TImode */
1550   {2, 2, 8},                            /* cost of storing SSE registers
1551                                            in SImode, DImode and TImode */
1552   10,                                   /* MMX or SSE register to integer */
1553   8,                                    /* size of l1 cache.  */
1554   256,                                  /* size of l2 cache.  */
1555   64,                                   /* size of prefetch block */
1556   6,                                    /* number of parallel prefetches */
1557   2,                                    /* Branch cost */
1558   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
1559   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
1560   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
1561   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1562   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1563   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
1564   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1565    DUMMY_STRINGOP_ALGS},
1566   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1567    {-1, libcall}}},
1568    DUMMY_STRINGOP_ALGS},
1569   1,                                    /* scalar_stmt_cost.  */
1570   1,                                    /* scalar load_cost.  */
1571   1,                                    /* scalar_store_cost.  */
1572   1,                                    /* vec_stmt_cost.  */
1573   1,                                    /* vec_to_scalar_cost.  */
1574   1,                                    /* scalar_to_vec_cost.  */
1575   1,                                    /* vec_align_load_cost.  */
1576   2,                                    /* vec_unalign_load_cost.  */
1577   1,                                    /* vec_store_cost.  */
1578   3,                                    /* cond_taken_branch_cost.  */
1579   1,                                    /* cond_not_taken_branch_cost.  */
1580 };
1581
1582 static const
1583 struct processor_costs nocona_cost = {
1584   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1585   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1586   COSTS_N_INSNS (1),                    /* variable shift costs */
1587   COSTS_N_INSNS (1),                    /* constant shift costs */
1588   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
1589    COSTS_N_INSNS (10),                  /*                               HI */
1590    COSTS_N_INSNS (10),                  /*                               SI */
1591    COSTS_N_INSNS (10),                  /*                               DI */
1592    COSTS_N_INSNS (10)},                 /*                            other */
1593   0,                                    /* cost of multiply per each bit set */
1594   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
1595    COSTS_N_INSNS (66),                  /*                          HI */
1596    COSTS_N_INSNS (66),                  /*                          SI */
1597    COSTS_N_INSNS (66),                  /*                          DI */
1598    COSTS_N_INSNS (66)},                 /*                          other */
1599   COSTS_N_INSNS (1),                    /* cost of movsx */
1600   COSTS_N_INSNS (1),                    /* cost of movzx */
1601   16,                                   /* "large" insn */
1602   17,                                   /* MOVE_RATIO */
1603   4,                                 /* cost for loading QImode using movzbl */
1604   {4, 4, 4},                            /* cost of loading integer registers
1605                                            in QImode, HImode and SImode.
1606                                            Relative to reg-reg move (2).  */
1607   {4, 4, 4},                            /* cost of storing integer registers */
1608   3,                                    /* cost of reg,reg fld/fst */
1609   {12, 12, 12},                         /* cost of loading fp registers
1610                                            in SFmode, DFmode and XFmode */
1611   {4, 4, 4},                            /* cost of storing fp registers
1612                                            in SFmode, DFmode and XFmode */
1613   6,                                    /* cost of moving MMX register */
1614   {12, 12},                             /* cost of loading MMX registers
1615                                            in SImode and DImode */
1616   {12, 12},                             /* cost of storing MMX registers
1617                                            in SImode and DImode */
1618   6,                                    /* cost of moving SSE register */
1619   {12, 12, 12},                         /* cost of loading SSE registers
1620                                            in SImode, DImode and TImode */
1621   {12, 12, 12},                         /* cost of storing SSE registers
1622                                            in SImode, DImode and TImode */
1623   8,                                    /* MMX or SSE register to integer */
1624   8,                                    /* size of l1 cache.  */
1625   1024,                                 /* size of l2 cache.  */
1626   128,                                  /* size of prefetch block */
1627   8,                                    /* number of parallel prefetches */
1628   1,                                    /* Branch cost */
1629   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1630   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1631   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
1632   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
1633   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
1634   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
1635   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1636    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
1637               {100000, unrolled_loop}, {-1, libcall}}}},
1638   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1639    {-1, libcall}}},
1640    {libcall, {{24, loop}, {64, unrolled_loop},
1641               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1642   1,                                    /* scalar_stmt_cost.  */
1643   1,                                    /* scalar load_cost.  */
1644   1,                                    /* scalar_store_cost.  */
1645   1,                                    /* vec_stmt_cost.  */
1646   1,                                    /* vec_to_scalar_cost.  */
1647   1,                                    /* scalar_to_vec_cost.  */
1648   1,                                    /* vec_align_load_cost.  */
1649   2,                                    /* vec_unalign_load_cost.  */
1650   1,                                    /* vec_store_cost.  */
1651   3,                                    /* cond_taken_branch_cost.  */
1652   1,                                    /* cond_not_taken_branch_cost.  */
1653 };
1654
1655 static const
1656 struct processor_costs atom_cost = {
1657   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1658   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1659   COSTS_N_INSNS (1),                    /* variable shift costs */
1660   COSTS_N_INSNS (1),                    /* constant shift costs */
1661   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1662    COSTS_N_INSNS (4),                   /*                               HI */
1663    COSTS_N_INSNS (3),                   /*                               SI */
1664    COSTS_N_INSNS (4),                   /*                               DI */
1665    COSTS_N_INSNS (2)},                  /*                            other */
1666   0,                                    /* cost of multiply per each bit set */
1667   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1668    COSTS_N_INSNS (26),                  /*                          HI */
1669    COSTS_N_INSNS (42),                  /*                          SI */
1670    COSTS_N_INSNS (74),                  /*                          DI */
1671    COSTS_N_INSNS (74)},                 /*                          other */
1672   COSTS_N_INSNS (1),                    /* cost of movsx */
1673   COSTS_N_INSNS (1),                    /* cost of movzx */
1674   8,                                    /* "large" insn */
1675   17,                                   /* MOVE_RATIO */
1676   4,                                    /* cost for loading QImode using movzbl */
1677   {4, 4, 4},                            /* cost of loading integer registers
1678                                            in QImode, HImode and SImode.
1679                                            Relative to reg-reg move (2).  */
1680   {4, 4, 4},                            /* cost of storing integer registers */
1681   4,                                    /* cost of reg,reg fld/fst */
1682   {12, 12, 12},                         /* cost of loading fp registers
1683                                            in SFmode, DFmode and XFmode */
1684   {6, 6, 8},                            /* cost of storing fp registers
1685                                            in SFmode, DFmode and XFmode */
1686   2,                                    /* cost of moving MMX register */
1687   {8, 8},                               /* cost of loading MMX registers
1688                                            in SImode and DImode */
1689   {8, 8},                               /* cost of storing MMX registers
1690                                            in SImode and DImode */
1691   2,                                    /* cost of moving SSE register */
1692   {8, 8, 8},                            /* cost of loading SSE registers
1693                                            in SImode, DImode and TImode */
1694   {8, 8, 8},                            /* cost of storing SSE registers
1695                                            in SImode, DImode and TImode */
1696   5,                                    /* MMX or SSE register to integer */
1697   32,                                   /* size of l1 cache.  */
1698   256,                                  /* size of l2 cache.  */
1699   64,                                   /* size of prefetch block */
1700   6,                                    /* number of parallel prefetches */
1701   3,                                    /* Branch cost */
1702   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1703   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1704   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1705   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1706   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1707   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1708   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1709    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1710           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1711   {{libcall, {{8, loop}, {15, unrolled_loop},
1712           {2048, rep_prefix_4_byte}, {-1, libcall}}},
1713    {libcall, {{24, loop}, {32, unrolled_loop},
1714           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1715   1,                                    /* scalar_stmt_cost.  */
1716   1,                                    /* scalar load_cost.  */
1717   1,                                    /* scalar_store_cost.  */
1718   1,                                    /* vec_stmt_cost.  */
1719   1,                                    /* vec_to_scalar_cost.  */
1720   1,                                    /* scalar_to_vec_cost.  */
1721   1,                                    /* vec_align_load_cost.  */
1722   2,                                    /* vec_unalign_load_cost.  */
1723   1,                                    /* vec_store_cost.  */
1724   3,                                    /* cond_taken_branch_cost.  */
1725   1,                                    /* cond_not_taken_branch_cost.  */
1726 };
1727
1728 /* Generic64 should produce code tuned for Nocona and K8.  */
1729 static const
1730 struct processor_costs generic64_cost = {
1731   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1732   /* On all chips taken into consideration lea is 2 cycles and more.  With
1733      this cost however our current implementation of synth_mult results in
1734      use of unnecessary temporary registers causing regression on several
1735      SPECfp benchmarks.  */
1736   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1737   COSTS_N_INSNS (1),                    /* variable shift costs */
1738   COSTS_N_INSNS (1),                    /* constant shift costs */
1739   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1740    COSTS_N_INSNS (4),                   /*                               HI */
1741    COSTS_N_INSNS (3),                   /*                               SI */
1742    COSTS_N_INSNS (4),                   /*                               DI */
1743    COSTS_N_INSNS (2)},                  /*                            other */
1744   0,                                    /* cost of multiply per each bit set */
1745   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1746    COSTS_N_INSNS (26),                  /*                          HI */
1747    COSTS_N_INSNS (42),                  /*                          SI */
1748    COSTS_N_INSNS (74),                  /*                          DI */
1749    COSTS_N_INSNS (74)},                 /*                          other */
1750   COSTS_N_INSNS (1),                    /* cost of movsx */
1751   COSTS_N_INSNS (1),                    /* cost of movzx */
1752   8,                                    /* "large" insn */
1753   17,                                   /* MOVE_RATIO */
1754   4,                                 /* cost for loading QImode using movzbl */
1755   {4, 4, 4},                            /* cost of loading integer registers
1756                                            in QImode, HImode and SImode.
1757                                            Relative to reg-reg move (2).  */
1758   {4, 4, 4},                            /* cost of storing integer registers */
1759   4,                                    /* cost of reg,reg fld/fst */
1760   {12, 12, 12},                         /* cost of loading fp registers
1761                                            in SFmode, DFmode and XFmode */
1762   {6, 6, 8},                            /* cost of storing fp registers
1763                                            in SFmode, DFmode and XFmode */
1764   2,                                    /* cost of moving MMX register */
1765   {8, 8},                               /* cost of loading MMX registers
1766                                            in SImode and DImode */
1767   {8, 8},                               /* cost of storing MMX registers
1768                                            in SImode and DImode */
1769   2,                                    /* cost of moving SSE register */
1770   {8, 8, 8},                            /* cost of loading SSE registers
1771                                            in SImode, DImode and TImode */
1772   {8, 8, 8},                            /* cost of storing SSE registers
1773                                            in SImode, DImode and TImode */
1774   5,                                    /* MMX or SSE register to integer */
1775   32,                                   /* size of l1 cache.  */
1776   512,                                  /* size of l2 cache.  */
1777   64,                                   /* size of prefetch block */
1778   6,                                    /* number of parallel prefetches */
1779   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this
1780      value is increased to perhaps more appropriate value of 5.  */
1781   3,                                    /* Branch cost */
1782   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1783   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1784   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1785   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1786   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1787   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1788   {DUMMY_STRINGOP_ALGS,
1789    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1790   {DUMMY_STRINGOP_ALGS,
1791    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1792   1,                                    /* scalar_stmt_cost.  */
1793   1,                                    /* scalar load_cost.  */
1794   1,                                    /* scalar_store_cost.  */
1795   1,                                    /* vec_stmt_cost.  */
1796   1,                                    /* vec_to_scalar_cost.  */
1797   1,                                    /* scalar_to_vec_cost.  */
1798   1,                                    /* vec_align_load_cost.  */
1799   2,                                    /* vec_unalign_load_cost.  */
1800   1,                                    /* vec_store_cost.  */
1801   3,                                    /* cond_taken_branch_cost.  */
1802   1,                                    /* cond_not_taken_branch_cost.  */
1803 };
1804
1805 /* Generic32 should produce code tuned for PPro, Pentium4, Nocona,
1806    Athlon and K8.  */
1807 static const
1808 struct processor_costs generic32_cost = {
1809   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1810   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1811   COSTS_N_INSNS (1),                    /* variable shift costs */
1812   COSTS_N_INSNS (1),                    /* constant shift costs */
1813   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1814    COSTS_N_INSNS (4),                   /*                               HI */
1815    COSTS_N_INSNS (3),                   /*                               SI */
1816    COSTS_N_INSNS (4),                   /*                               DI */
1817    COSTS_N_INSNS (2)},                  /*                            other */
1818   0,                                    /* cost of multiply per each bit set */
1819   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1820    COSTS_N_INSNS (26),                  /*                          HI */
1821    COSTS_N_INSNS (42),                  /*                          SI */
1822    COSTS_N_INSNS (74),                  /*                          DI */
1823    COSTS_N_INSNS (74)},                 /*                          other */
1824   COSTS_N_INSNS (1),                    /* cost of movsx */
1825   COSTS_N_INSNS (1),                    /* cost of movzx */
1826   8,                                    /* "large" insn */
1827   17,                                   /* MOVE_RATIO */
1828   4,                                 /* cost for loading QImode using movzbl */
1829   {4, 4, 4},                            /* cost of loading integer registers
1830                                            in QImode, HImode and SImode.
1831                                            Relative to reg-reg move (2).  */
1832   {4, 4, 4},                            /* cost of storing integer registers */
1833   4,                                    /* cost of reg,reg fld/fst */
1834   {12, 12, 12},                         /* cost of loading fp registers
1835                                            in SFmode, DFmode and XFmode */
1836   {6, 6, 8},                            /* cost of storing fp registers
1837                                            in SFmode, DFmode and XFmode */
1838   2,                                    /* cost of moving MMX register */
1839   {8, 8},                               /* cost of loading MMX registers
1840                                            in SImode and DImode */
1841   {8, 8},                               /* cost of storing MMX registers
1842                                            in SImode and DImode */
1843   2,                                    /* cost of moving SSE register */
1844   {8, 8, 8},                            /* cost of loading SSE registers
1845                                            in SImode, DImode and TImode */
1846   {8, 8, 8},                            /* cost of storing SSE registers
1847                                            in SImode, DImode and TImode */
1848   5,                                    /* MMX or SSE register to integer */
1849   32,                                   /* size of l1 cache.  */
1850   256,                                  /* size of l2 cache.  */
1851   64,                                   /* size of prefetch block */
1852   6,                                    /* number of parallel prefetches */
1853   3,                                    /* Branch cost */
1854   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1855   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1856   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1857   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1858   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1859   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1860   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1861    DUMMY_STRINGOP_ALGS},
1862   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1863    DUMMY_STRINGOP_ALGS},
1864   1,                                    /* scalar_stmt_cost.  */
1865   1,                                    /* scalar load_cost.  */
1866   1,                                    /* scalar_store_cost.  */
1867   1,                                    /* vec_stmt_cost.  */
1868   1,                                    /* vec_to_scalar_cost.  */
1869   1,                                    /* scalar_to_vec_cost.  */
1870   1,                                    /* vec_align_load_cost.  */
1871   2,                                    /* vec_unalign_load_cost.  */
1872   1,                                    /* vec_store_cost.  */
1873   3,                                    /* cond_taken_branch_cost.  */
1874   1,                                    /* cond_not_taken_branch_cost.  */
1875 };
1876
1877 const struct processor_costs *ix86_cost = &pentium_cost;
1878
1879 /* Processor feature/optimization bitmasks.  */
1880 #define m_386 (1<<PROCESSOR_I386)
1881 #define m_486 (1<<PROCESSOR_I486)
1882 #define m_PENT (1<<PROCESSOR_PENTIUM)
1883 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
1884 #define m_PENT4 (1<<PROCESSOR_PENTIUM4)
1885 #define m_NOCONA (1<<PROCESSOR_NOCONA)
1886 #define m_P4_NOCONA (m_PENT4 | m_NOCONA)
1887 #define m_CORE2_32 (1<<PROCESSOR_CORE2_32)
1888 #define m_CORE2_64 (1<<PROCESSOR_CORE2_64)
1889 #define m_COREI7_32 (1<<PROCESSOR_COREI7_32)
1890 #define m_COREI7_64 (1<<PROCESSOR_COREI7_64)
1891 #define m_COREI7 (m_COREI7_32 | m_COREI7_64)
1892 #define m_CORE2I7_32 (m_CORE2_32 | m_COREI7_32)
1893 #define m_CORE2I7_64 (m_CORE2_64 | m_COREI7_64)
1894 #define m_CORE2I7 (m_CORE2I7_32 | m_CORE2I7_64)
1895 #define m_ATOM (1<<PROCESSOR_ATOM)
1896
1897 #define m_GEODE (1<<PROCESSOR_GEODE)
1898 #define m_K6 (1<<PROCESSOR_K6)
1899 #define m_K6_GEODE (m_K6 | m_GEODE)
1900 #define m_K8 (1<<PROCESSOR_K8)
1901 #define m_ATHLON (1<<PROCESSOR_ATHLON)
1902 #define m_ATHLON_K8 (m_K8 | m_ATHLON)
1903 #define m_AMDFAM10 (1<<PROCESSOR_AMDFAM10)
1904 #define m_BDVER1 (1<<PROCESSOR_BDVER1)
1905 #define m_BDVER2 (1<<PROCESSOR_BDVER2)
1906 #define m_BDVER (m_BDVER1 | m_BDVER2)
1907 #define m_BTVER1 (1<<PROCESSOR_BTVER1)
1908 #define m_AMD_MULTIPLE (m_ATHLON_K8 | m_AMDFAM10 | m_BDVER | m_BTVER1)
1909
1910 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1911 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1912
1913 /* Generic instruction choice should be common subset of supported CPUs
1914    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1915 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1916
1917 /* Feature tests against the various tunings.  */
1918 unsigned char ix86_tune_features[X86_TUNE_LAST];
1919
1920 /* Feature tests against the various tunings used to create ix86_tune_features
1921    based on the processor mask.  */
1922 static unsigned int initial_ix86_tune_features[X86_TUNE_LAST] = {
1923   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1924      negatively, so enabling for Generic64 seems like good code size
1925      tradeoff.  We can't enable it for 32bit generic because it does not
1926      work well with PPro base chips.  */
1927   m_386 | m_CORE2I7_64 | m_K6_GEODE | m_AMD_MULTIPLE | m_GENERIC64,
1928
1929   /* X86_TUNE_PUSH_MEMORY */
1930   m_386 | m_P4_NOCONA | m_CORE2I7 | m_K6_GEODE | m_AMD_MULTIPLE | m_GENERIC,
1931
1932   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1933   m_486 | m_PENT,
1934
1935   /* X86_TUNE_UNROLL_STRLEN */
1936   m_486 | m_PENT | m_PPRO | m_ATOM | m_CORE2I7 | m_K6 | m_AMD_MULTIPLE | m_GENERIC,
1937
1938   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1939      on simulation result. But after P4 was made, no performance benefit
1940      was observed with branch hints.  It also increases the code size.
1941      As a result, icc never generates branch hints.  */
1942   0,
1943
1944   /* X86_TUNE_DOUBLE_WITH_ADD */
1945   ~m_386,
1946
1947   /* X86_TUNE_USE_SAHF */
1948   m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_BDVER | m_BTVER1 | m_GENERIC,
1949
1950   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1951      partial dependencies.  */
1952   m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_GEODE | m_AMD_MULTIPLE  | m_GENERIC,
1953
1954   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1955      register stalls on Generic32 compilation setting as well.  However
1956      in current implementation the partial register stalls are not eliminated
1957      very well - they can be introduced via subregs synthesized by combine
1958      and can happen in caller/callee saving sequences.  Because this option
1959      pays back little on PPro based chips and is in conflict with partial reg
1960      dependencies used by Athlon/P4 based chips, it is better to leave it off
1961      for generic32 for now.  */
1962   m_PPRO,
1963
1964   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1965   m_CORE2I7 | m_GENERIC,
1966
1967   /* X86_TUNE_USE_HIMODE_FIOP */
1968   m_386 | m_486 | m_K6_GEODE,
1969
1970   /* X86_TUNE_USE_SIMODE_FIOP */
1971   ~(m_PENT | m_PPRO | m_CORE2I7 | m_ATOM | m_AMD_MULTIPLE | m_GENERIC),
1972
1973   /* X86_TUNE_USE_MOV0 */
1974   m_K6,
1975
1976   /* X86_TUNE_USE_CLTD */
1977   ~(m_PENT | m_CORE2I7 | m_ATOM | m_K6 | m_GENERIC),
1978
1979   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1980   m_PENT4,
1981
1982   /* X86_TUNE_SPLIT_LONG_MOVES */
1983   m_PPRO,
1984
1985   /* X86_TUNE_READ_MODIFY_WRITE */
1986   ~m_PENT,
1987
1988   /* X86_TUNE_READ_MODIFY */
1989   ~(m_PENT | m_PPRO),
1990
1991   /* X86_TUNE_PROMOTE_QIMODE */
1992   m_386 | m_486 | m_PENT | m_CORE2I7 | m_ATOM | m_K6_GEODE | m_AMD_MULTIPLE | m_GENERIC,
1993
1994   /* X86_TUNE_FAST_PREFIX */
1995   ~(m_386 | m_486 | m_PENT),
1996
1997   /* X86_TUNE_SINGLE_STRINGOP */
1998   m_386 | m_P4_NOCONA,
1999
2000   /* X86_TUNE_QIMODE_MATH */
2001   ~0,
2002
2003   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
2004      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
2005      might be considered for Generic32 if our scheme for avoiding partial
2006      stalls was more effective.  */
2007   ~m_PPRO,
2008
2009   /* X86_TUNE_PROMOTE_QI_REGS */
2010   0,
2011
2012   /* X86_TUNE_PROMOTE_HI_REGS */
2013   m_PPRO,
2014
2015   /* X86_TUNE_SINGLE_POP: Enable if single pop insn is preferred
2016      over esp addition.  */
2017   m_386 | m_486 | m_PENT | m_PPRO,
2018
2019   /* X86_TUNE_DOUBLE_POP: Enable if double pop insn is preferred
2020      over esp addition.  */
2021   m_PENT,
2022
2023   /* X86_TUNE_SINGLE_PUSH: Enable if single push insn is preferred
2024      over esp subtraction.  */
2025   m_386 | m_486 | m_PENT | m_K6_GEODE,
2026
2027   /* X86_TUNE_DOUBLE_PUSH. Enable if double push insn is preferred
2028      over esp subtraction.  */
2029   m_PENT | m_K6_GEODE,
2030
2031   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
2032      for DFmode copies */
2033   ~(m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_GEODE | m_AMD_MULTIPLE | m_ATOM | m_GENERIC),
2034
2035   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
2036   m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_AMD_MULTIPLE | m_GENERIC,
2037
2038   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
2039      conflict here in between PPro/Pentium4 based chips that thread 128bit
2040      SSE registers as single units versus K8 based chips that divide SSE
2041      registers to two 64bit halves.  This knob promotes all store destinations
2042      to be 128bit to allow register renaming on 128bit SSE units, but usually
2043      results in one extra microop on 64bit SSE units.  Experimental results
2044      shows that disabling this option on P4 brings over 20% SPECfp regression,
2045      while enabling it on K8 brings roughly 2.4% regression that can be partly
2046      masked by careful scheduling of moves.  */
2047   m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM  | m_AMDFAM10 | m_BDVER | m_GENERIC,
2048
2049   /* X86_TUNE_SSE_UNALIGNED_LOAD_OPTIMAL */
2050   m_COREI7 | m_AMDFAM10 | m_BDVER | m_BTVER1,
2051
2052   /* X86_TUNE_SSE_UNALIGNED_STORE_OPTIMAL */
2053   m_COREI7 | m_BDVER,
2054
2055   /* X86_TUNE_SSE_PACKED_SINGLE_INSN_OPTIMAL */
2056   m_BDVER ,
2057
2058   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
2059      are resolved on SSE register parts instead of whole registers, so we may
2060      maintain just lower part of scalar values in proper format leaving the
2061      upper part undefined.  */
2062   m_ATHLON_K8,
2063
2064   /* X86_TUNE_SSE_TYPELESS_STORES */
2065   m_AMD_MULTIPLE,
2066
2067   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
2068   m_PPRO | m_P4_NOCONA,
2069
2070   /* X86_TUNE_MEMORY_MISMATCH_STALL */
2071   m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_AMD_MULTIPLE | m_GENERIC,
2072
2073   /* X86_TUNE_PROLOGUE_USING_MOVE */
2074   m_PPRO | m_CORE2I7 | m_ATOM | m_ATHLON_K8 | m_GENERIC,
2075
2076   /* X86_TUNE_EPILOGUE_USING_MOVE */
2077   m_PPRO | m_CORE2I7 | m_ATOM | m_ATHLON_K8 | m_GENERIC,
2078
2079   /* X86_TUNE_SHIFT1 */
2080   ~m_486,
2081
2082   /* X86_TUNE_USE_FFREEP */
2083   m_AMD_MULTIPLE,
2084
2085   /* X86_TUNE_INTER_UNIT_MOVES */
2086   ~(m_AMD_MULTIPLE | m_GENERIC),
2087
2088   /* X86_TUNE_INTER_UNIT_CONVERSIONS */
2089   ~(m_AMDFAM10 | m_BDVER ),
2090
2091   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
2092      than 4 branch instructions in the 16 byte window.  */
2093   m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_AMD_MULTIPLE | m_GENERIC,
2094
2095   /* X86_TUNE_SCHEDULE */
2096   m_PENT | m_PPRO | m_CORE2I7 | m_ATOM | m_K6_GEODE | m_AMD_MULTIPLE | m_GENERIC,
2097
2098   /* X86_TUNE_USE_BT */
2099   m_CORE2I7 | m_ATOM | m_AMD_MULTIPLE | m_GENERIC,
2100
2101   /* X86_TUNE_USE_INCDEC */
2102   ~(m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_GENERIC),
2103
2104   /* X86_TUNE_PAD_RETURNS */
2105   m_CORE2I7 | m_AMD_MULTIPLE | m_GENERIC,
2106
2107   /* X86_TUNE_PAD_SHORT_FUNCTION: Pad short funtion.  */
2108   m_ATOM,
2109
2110   /* X86_TUNE_EXT_80387_CONSTANTS */
2111   m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_K6_GEODE | m_ATHLON_K8 | m_GENERIC,
2112
2113   /* X86_TUNE_SHORTEN_X87_SSE */
2114   ~m_K8,
2115
2116   /* X86_TUNE_AVOID_VECTOR_DECODE */
2117   m_CORE2I7_64 | m_K8 | m_GENERIC64,
2118
2119   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
2120      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
2121   ~(m_386 | m_486),
2122
2123   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
2124      vector path on AMD machines.  */
2125   m_CORE2I7_64 | m_K8 | m_AMDFAM10 | m_BDVER | m_BTVER1 | m_GENERIC64,
2126
2127   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
2128      machines.  */
2129   m_CORE2I7_64 | m_K8 | m_AMDFAM10 | m_BDVER | m_BTVER1 | m_GENERIC64,
2130
2131   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
2132      than a MOV.  */
2133   m_PENT,
2134
2135   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
2136      but one byte longer.  */
2137   m_PENT,
2138
2139   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
2140      operand that cannot be represented using a modRM byte.  The XOR
2141      replacement is long decoded, so this split helps here as well.  */
2142   m_K6,
2143
2144   /* X86_TUNE_USE_VECTOR_FP_CONVERTS: Prefer vector packed SSE conversion
2145      from FP to FP. */
2146   m_CORE2I7 | m_AMDFAM10 | m_GENERIC,
2147
2148   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
2149      from integer to FP. */
2150   m_AMDFAM10,
2151
2152   /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction
2153      with a subsequent conditional jump instruction into a single
2154      compare-and-branch uop.  */
2155   m_BDVER,
2156
2157   /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag
2158      will impact LEA instruction selection. */
2159   m_ATOM,
2160
2161   /* X86_TUNE_VECTORIZE_DOUBLE: Enable double precision vector
2162      instructions.  */
2163   ~m_ATOM,
2164
2165   /* X86_SOFTARE_PREFETCHING_BENEFICIAL: Enable software prefetching
2166      at -O3.  For the moment, the prefetching seems badly tuned for Intel
2167      chips.  */
2168   m_K6_GEODE | m_AMD_MULTIPLE,
2169
2170   /* X86_TUNE_AVX128_OPTIMAL: Enable 128-bit AVX instruction generation for
2171      the auto-vectorizer.  */
2172   m_BDVER,
2173
2174   /* X86_TUNE_REASSOC_INT_TO_PARALLEL: Try to produce parallel computations
2175      during reassociation of integer computation.  */
2176   m_ATOM,
2177
2178   /* X86_TUNE_REASSOC_FP_TO_PARALLEL: Try to produce parallel computations
2179      during reassociation of fp computation.  */
2180   m_ATOM
2181 };
2182
2183 /* Feature tests against the various architecture variations.  */
2184 unsigned char ix86_arch_features[X86_ARCH_LAST];
2185
2186 /* Feature tests against the various architecture variations, used to create
2187    ix86_arch_features based on the processor mask.  */
2188 static unsigned int initial_ix86_arch_features[X86_ARCH_LAST] = {
2189   /* X86_ARCH_CMOV: Conditional move was added for pentiumpro.  */
2190   ~(m_386 | m_486 | m_PENT | m_K6),
2191
2192   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
2193   ~m_386,
2194
2195   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
2196   ~(m_386 | m_486),
2197
2198   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
2199   ~m_386,
2200
2201   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
2202   ~m_386,
2203 };
2204
2205 static const unsigned int x86_accumulate_outgoing_args
2206   = m_PPRO | m_P4_NOCONA | m_ATOM | m_CORE2I7 | m_AMD_MULTIPLE | m_GENERIC;
2207
2208 static const unsigned int x86_arch_always_fancy_math_387
2209   = m_PENT | m_PPRO | m_P4_NOCONA | m_CORE2I7 | m_ATOM | m_AMD_MULTIPLE | m_GENERIC;
2210
2211 static const unsigned int x86_avx256_split_unaligned_load
2212   = m_COREI7 | m_GENERIC;
2213
2214 static const unsigned int x86_avx256_split_unaligned_store
2215   = m_COREI7 | m_BDVER | m_GENERIC;
2216
2217 /* In case the average insn count for single function invocation is
2218    lower than this constant, emit fast (but longer) prologue and
2219    epilogue code.  */
2220 #define FAST_PROLOGUE_INSN_COUNT 20
2221
2222 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
2223 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
2224 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
2225 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
2226
2227 /* Array of the smallest class containing reg number REGNO, indexed by
2228    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
2229
2230 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
2231 {
2232   /* ax, dx, cx, bx */
2233   AREG, DREG, CREG, BREG,
2234   /* si, di, bp, sp */
2235   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
2236   /* FP registers */
2237   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
2238   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
2239   /* arg pointer */
2240   NON_Q_REGS,
2241   /* flags, fpsr, fpcr, frame */
2242   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
2243   /* SSE registers */
2244   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
2245   SSE_REGS, SSE_REGS,
2246   /* MMX registers */
2247   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
2248   MMX_REGS, MMX_REGS,
2249   /* REX registers */
2250   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
2251   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
2252   /* SSE REX registers */
2253   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
2254   SSE_REGS, SSE_REGS,
2255 };
2256
2257 /* The "default" register map used in 32bit mode.  */
2258
2259 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
2260 {
2261   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
2262   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
2263   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
2264   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
2265   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
2266   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
2267   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
2268 };
2269
2270 /* The "default" register map used in 64bit mode.  */
2271
2272 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
2273 {
2274   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
2275   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
2276   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
2277   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
2278   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
2279   8,9,10,11,12,13,14,15,                /* extended integer registers */
2280   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
2281 };
2282
2283 /* Define the register numbers to be used in Dwarf debugging information.
2284    The SVR4 reference port C compiler uses the following register numbers
2285    in its Dwarf output code:
2286         0 for %eax (gcc regno = 0)
2287         1 for %ecx (gcc regno = 2)
2288         2 for %edx (gcc regno = 1)
2289         3 for %ebx (gcc regno = 3)
2290         4 for %esp (gcc regno = 7)
2291         5 for %ebp (gcc regno = 6)
2292         6 for %esi (gcc regno = 4)
2293         7 for %edi (gcc regno = 5)
2294    The following three DWARF register numbers are never generated by
2295    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
2296    believes these numbers have these meanings.
2297         8  for %eip    (no gcc equivalent)
2298         9  for %eflags (gcc regno = 17)
2299         10 for %trapno (no gcc equivalent)
2300    It is not at all clear how we should number the FP stack registers
2301    for the x86 architecture.  If the version of SDB on x86/svr4 were
2302    a bit less brain dead with respect to floating-point then we would
2303    have a precedent to follow with respect to DWARF register numbers
2304    for x86 FP registers, but the SDB on x86/svr4 is so completely
2305    broken with respect to FP registers that it is hardly worth thinking
2306    of it as something to strive for compatibility with.
2307    The version of x86/svr4 SDB I have at the moment does (partially)
2308    seem to believe that DWARF register number 11 is associated with
2309    the x86 register %st(0), but that's about all.  Higher DWARF
2310    register numbers don't seem to be associated with anything in
2311    particular, and even for DWARF regno 11, SDB only seems to under-
2312    stand that it should say that a variable lives in %st(0) (when
2313    asked via an `=' command) if we said it was in DWARF regno 11,
2314    but SDB still prints garbage when asked for the value of the
2315    variable in question (via a `/' command).
2316    (Also note that the labels SDB prints for various FP stack regs
2317    when doing an `x' command are all wrong.)
2318    Note that these problems generally don't affect the native SVR4
2319    C compiler because it doesn't allow the use of -O with -g and
2320    because when it is *not* optimizing, it allocates a memory
2321    location for each floating-point variable, and the memory
2322    location is what gets described in the DWARF AT_location
2323    attribute for the variable in question.
2324    Regardless of the severe mental illness of the x86/svr4 SDB, we
2325    do something sensible here and we use the following DWARF
2326    register numbers.  Note that these are all stack-top-relative
2327    numbers.
2328         11 for %st(0) (gcc regno = 8)
2329         12 for %st(1) (gcc regno = 9)
2330         13 for %st(2) (gcc regno = 10)
2331         14 for %st(3) (gcc regno = 11)
2332         15 for %st(4) (gcc regno = 12)
2333         16 for %st(5) (gcc regno = 13)
2334         17 for %st(6) (gcc regno = 14)
2335         18 for %st(7) (gcc regno = 15)
2336 */
2337 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
2338 {
2339   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
2340   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
2341   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
2342   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
2343   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
2344   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
2345   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
2346 };
2347
2348 /* Define parameter passing and return registers.  */
2349
2350 static int const x86_64_int_parameter_registers[6] =
2351 {
2352   DI_REG, SI_REG, DX_REG, CX_REG, R8_REG, R9_REG
2353 };
2354
2355 static int const x86_64_ms_abi_int_parameter_registers[4] =
2356 {
2357   CX_REG, DX_REG, R8_REG, R9_REG
2358 };
2359
2360 static int const x86_64_int_return_registers[4] =
2361 {
2362   AX_REG, DX_REG, DI_REG, SI_REG
2363 };
2364
2365 /* Define the structure for the machine field in struct function.  */
2366
2367 struct GTY(()) stack_local_entry {
2368   unsigned short mode;
2369   unsigned short n;
2370   rtx rtl;
2371   struct stack_local_entry *next;
2372 };
2373
2374 /* Structure describing stack frame layout.
2375    Stack grows downward:
2376
2377    [arguments]
2378                                         <- ARG_POINTER
2379    saved pc
2380
2381    saved static chain                   if ix86_static_chain_on_stack
2382
2383    saved frame pointer                  if frame_pointer_needed
2384                                         <- HARD_FRAME_POINTER
2385    [saved regs]
2386                                         <- regs_save_offset
2387    [padding0]
2388
2389    [saved SSE regs]
2390                                         <- sse_regs_save_offset
2391    [padding1]          |
2392                        |                <- FRAME_POINTER
2393    [va_arg registers]  |
2394                        |
2395    [frame]             |
2396                        |
2397    [padding2]          | = to_allocate
2398                                         <- STACK_POINTER
2399   */
2400 struct ix86_frame
2401 {
2402   int nsseregs;
2403   int nregs;
2404   int va_arg_size;
2405   int red_zone_size;
2406   int outgoing_arguments_size;
2407   HOST_WIDE_INT frame;
2408
2409   /* The offsets relative to ARG_POINTER.  */
2410   HOST_WIDE_INT frame_pointer_offset;
2411   HOST_WIDE_INT hard_frame_pointer_offset;
2412   HOST_WIDE_INT stack_pointer_offset;
2413   HOST_WIDE_INT hfp_save_offset;
2414   HOST_WIDE_INT reg_save_offset;
2415   HOST_WIDE_INT sse_reg_save_offset;
2416
2417   /* When save_regs_using_mov is set, emit prologue using
2418      move instead of push instructions.  */
2419   bool save_regs_using_mov;
2420 };
2421
2422 /* Which cpu are we scheduling for.  */
2423 enum attr_cpu ix86_schedule;
2424
2425 /* Which cpu are we optimizing for.  */
2426 enum processor_type ix86_tune;
2427
2428 /* Which instruction set architecture to use.  */
2429 enum processor_type ix86_arch;
2430
2431 /* true if sse prefetch instruction is not NOOP.  */
2432 int x86_prefetch_sse;
2433
2434 /* -mstackrealign option */
2435 static const char ix86_force_align_arg_pointer_string[]
2436   = "force_align_arg_pointer";
2437
2438 static rtx (*ix86_gen_leave) (void);
2439 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
2440 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
2441 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
2442 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
2443 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
2444 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
2445 static rtx (*ix86_gen_allocate_stack_worker) (rtx, rtx);
2446 static rtx (*ix86_gen_adjust_stack_and_probe) (rtx, rtx, rtx);
2447 static rtx (*ix86_gen_probe_stack_range) (rtx, rtx, rtx);
2448
2449 /* Preferred alignment for stack boundary in bits.  */
2450 unsigned int ix86_preferred_stack_boundary;
2451
2452 /* Alignment for incoming stack boundary in bits specified at
2453    command line.  */
2454 static unsigned int ix86_user_incoming_stack_boundary;
2455
2456 /* Default alignment for incoming stack boundary in bits.  */
2457 static unsigned int ix86_default_incoming_stack_boundary;
2458
2459 /* Alignment for incoming stack boundary in bits.  */
2460 unsigned int ix86_incoming_stack_boundary;
2461
2462 /* Calling abi specific va_list type nodes.  */
2463 static GTY(()) tree sysv_va_list_type_node;
2464 static GTY(()) tree ms_va_list_type_node;
2465
2466 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
2467 char internal_label_prefix[16];
2468 int internal_label_prefix_len;
2469
2470 /* Fence to use after loop using movnt.  */
2471 tree x86_mfence;
2472
2473 /* Register class used for passing given 64bit part of the argument.
2474    These represent classes as documented by the PS ABI, with the exception
2475    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
2476    use SF or DFmode move instead of DImode to avoid reformatting penalties.
2477
2478    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
2479    whenever possible (upper half does contain padding).  */
2480 enum x86_64_reg_class
2481   {
2482     X86_64_NO_CLASS,
2483     X86_64_INTEGER_CLASS,
2484     X86_64_INTEGERSI_CLASS,
2485     X86_64_SSE_CLASS,
2486     X86_64_SSESF_CLASS,
2487     X86_64_SSEDF_CLASS,
2488     X86_64_SSEUP_CLASS,
2489     X86_64_X87_CLASS,
2490     X86_64_X87UP_CLASS,
2491     X86_64_COMPLEX_X87_CLASS,
2492     X86_64_MEMORY_CLASS
2493   };
2494
2495 #define MAX_CLASSES 4
2496
2497 /* Table of constants used by fldpi, fldln2, etc....  */
2498 static REAL_VALUE_TYPE ext_80387_constants_table [5];
2499 static bool ext_80387_constants_init = 0;
2500
2501 \f
2502 static struct machine_function * ix86_init_machine_status (void);
2503 static rtx ix86_function_value (const_tree, const_tree, bool);
2504 static bool ix86_function_value_regno_p (const unsigned int);
2505 static unsigned int ix86_function_arg_boundary (enum machine_mode,
2506                                                 const_tree);
2507 static rtx ix86_static_chain (const_tree, bool);
2508 static int ix86_function_regparm (const_tree, const_tree);
2509 static void ix86_compute_frame_layout (struct ix86_frame *);
2510 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
2511                                                  rtx, rtx, int);
2512 static void ix86_add_new_builtins (HOST_WIDE_INT);
2513 static tree ix86_canonical_va_list_type (tree);
2514 static void predict_jump (int);
2515 static unsigned int split_stack_prologue_scratch_regno (void);
2516 static bool i386_asm_output_addr_const_extra (FILE *, rtx);
2517
2518 enum ix86_function_specific_strings
2519 {
2520   IX86_FUNCTION_SPECIFIC_ARCH,
2521   IX86_FUNCTION_SPECIFIC_TUNE,
2522   IX86_FUNCTION_SPECIFIC_MAX
2523 };
2524
2525 static char *ix86_target_string (HOST_WIDE_INT, int, const char *,
2526                                  const char *, enum fpmath_unit, bool);
2527 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
2528 static void ix86_function_specific_save (struct cl_target_option *);
2529 static void ix86_function_specific_restore (struct cl_target_option *);
2530 static void ix86_function_specific_print (FILE *, int,
2531                                           struct cl_target_option *);
2532 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
2533 static bool ix86_valid_target_attribute_inner_p (tree, char *[],
2534                                                  struct gcc_options *);
2535 static bool ix86_can_inline_p (tree, tree);
2536 static void ix86_set_current_function (tree);
2537 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
2538
2539 static enum calling_abi ix86_function_abi (const_tree);
2540
2541 \f
2542 #ifndef SUBTARGET32_DEFAULT_CPU
2543 #define SUBTARGET32_DEFAULT_CPU "i386"
2544 #endif
2545
2546 /* The svr4 ABI for the i386 says that records and unions are returned
2547    in memory.  */
2548 #ifndef DEFAULT_PCC_STRUCT_RETURN
2549 #define DEFAULT_PCC_STRUCT_RETURN 1
2550 #endif
2551
2552 /* Whether -mtune= or -march= were specified */
2553 static int ix86_tune_defaulted;
2554 static int ix86_arch_specified;
2555
2556 /* Vectorization library interface and handlers.  */
2557 static tree (*ix86_veclib_handler) (enum built_in_function, tree, tree);
2558
2559 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2560 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2561
2562 /* Processor target table, indexed by processor number */
2563 struct ptt
2564 {
2565   const struct processor_costs *cost;           /* Processor costs */
2566   const int align_loop;                         /* Default alignments.  */
2567   const int align_loop_max_skip;
2568   const int align_jump;
2569   const int align_jump_max_skip;
2570   const int align_func;
2571 };
2572
2573 static const struct ptt processor_target_table[PROCESSOR_max] =
2574 {
2575   {&i386_cost, 4, 3, 4, 3, 4},
2576   {&i486_cost, 16, 15, 16, 15, 16},
2577   {&pentium_cost, 16, 7, 16, 7, 16},
2578   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2579   {&geode_cost, 0, 0, 0, 0, 0},
2580   {&k6_cost, 32, 7, 32, 7, 32},
2581   {&athlon_cost, 16, 7, 16, 7, 16},
2582   {&pentium4_cost, 0, 0, 0, 0, 0},
2583   {&k8_cost, 16, 7, 16, 7, 16},
2584   {&nocona_cost, 0, 0, 0, 0, 0},
2585   /* Core 2 32-bit.  */
2586   {&generic32_cost, 16, 10, 16, 10, 16},
2587   /* Core 2 64-bit.  */
2588   {&generic64_cost, 16, 10, 16, 10, 16},
2589   /* Core i7 32-bit.  */
2590   {&generic32_cost, 16, 10, 16, 10, 16},
2591   /* Core i7 64-bit.  */
2592   {&generic64_cost, 16, 10, 16, 10, 16},
2593   {&generic32_cost, 16, 7, 16, 7, 16},
2594   {&generic64_cost, 16, 10, 16, 10, 16},
2595   {&amdfam10_cost, 32, 24, 32, 7, 32},
2596   {&bdver1_cost, 32, 24, 32, 7, 32},
2597   {&bdver2_cost, 32, 24, 32, 7, 32},
2598   {&btver1_cost, 32, 24, 32, 7, 32},
2599   {&atom_cost, 16, 15, 16, 7, 16}
2600 };
2601
2602 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2603 {
2604   "generic",
2605   "i386",
2606   "i486",
2607   "pentium",
2608   "pentium-mmx",
2609   "pentiumpro",
2610   "pentium2",
2611   "pentium3",
2612   "pentium4",
2613   "pentium-m",
2614   "prescott",
2615   "nocona",
2616   "core2",
2617   "corei7",
2618   "atom",
2619   "geode",
2620   "k6",
2621   "k6-2",
2622   "k6-3",
2623   "athlon",
2624   "athlon-4",
2625   "k8",
2626   "amdfam10",
2627   "bdver1",
2628   "bdver2",
2629   "btver1"
2630 };
2631 \f
2632 /* Return true if a red-zone is in use.  */
2633
2634 static inline bool
2635 ix86_using_red_zone (void)
2636 {
2637   return TARGET_RED_ZONE && !TARGET_64BIT_MS_ABI;
2638 }
2639 \f
2640 /* Return a string that documents the current -m options.  The caller is
2641    responsible for freeing the string.  */
2642
2643 static char *
2644 ix86_target_string (HOST_WIDE_INT isa, int flags, const char *arch,
2645                     const char *tune, enum fpmath_unit fpmath,
2646                     bool add_nl_p)
2647 {
2648   struct ix86_target_opts
2649   {
2650     const char *option;         /* option string */
2651     HOST_WIDE_INT mask;         /* isa mask options */
2652   };
2653
2654   /* This table is ordered so that options like -msse4.2 that imply
2655      preceding options while match those first.  */
2656   static struct ix86_target_opts isa_opts[] =
2657   {
2658     { "-m64",           OPTION_MASK_ISA_64BIT },
2659     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2660     { "-mfma",          OPTION_MASK_ISA_FMA },
2661     { "-mxop",          OPTION_MASK_ISA_XOP },
2662     { "-mlwp",          OPTION_MASK_ISA_LWP },
2663     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2664     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2665     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2666     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2667     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2668     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2669     { "-msse",          OPTION_MASK_ISA_SSE },
2670     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2671     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2672     { "-mmmx",          OPTION_MASK_ISA_MMX },
2673     { "-mabm",          OPTION_MASK_ISA_ABM },
2674     { "-mbmi",          OPTION_MASK_ISA_BMI },
2675     { "-mbmi2",         OPTION_MASK_ISA_BMI2 },
2676     { "-mlzcnt",        OPTION_MASK_ISA_LZCNT },
2677     { "-mtbm",          OPTION_MASK_ISA_TBM },
2678     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2679     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2680     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2681     { "-maes",          OPTION_MASK_ISA_AES },
2682     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2683     { "-mfsgsbase",     OPTION_MASK_ISA_FSGSBASE },
2684     { "-mrdrnd",        OPTION_MASK_ISA_RDRND },
2685     { "-mf16c",         OPTION_MASK_ISA_F16C },
2686   };
2687
2688   /* Flag options.  */
2689   static struct ix86_target_opts flag_opts[] =
2690   {
2691     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
2692     { "-m80387",                        MASK_80387 },
2693     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
2694     { "-malign-double",                 MASK_ALIGN_DOUBLE },
2695     { "-mcld",                          MASK_CLD },
2696     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
2697     { "-mieee-fp",                      MASK_IEEE_FP },
2698     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
2699     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
2700     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
2701     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
2702     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
2703     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
2704     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
2705     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
2706     { "-mrecip",                        MASK_RECIP },
2707     { "-mrtd",                          MASK_RTD },
2708     { "-msseregparm",                   MASK_SSEREGPARM },
2709     { "-mstack-arg-probe",              MASK_STACK_PROBE },
2710     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
2711     { "-mvect8-ret-in-mem",             MASK_VECT8_RETURNS },
2712     { "-m8bit-idiv",                    MASK_USE_8BIT_IDIV },
2713     { "-mvzeroupper",                   MASK_VZEROUPPER },
2714     { "-mavx256-split-unaligned-load",  MASK_AVX256_SPLIT_UNALIGNED_LOAD},
2715     { "-mavx256-split-unaligned-store", MASK_AVX256_SPLIT_UNALIGNED_STORE},
2716     { "-mprefer-avx128",                MASK_PREFER_AVX128},
2717   };
2718
2719   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
2720
2721   char isa_other[40];
2722   char target_other[40];
2723   unsigned num = 0;
2724   unsigned i, j;
2725   char *ret;
2726   char *ptr;
2727   size_t len;
2728   size_t line_len;
2729   size_t sep_len;
2730
2731   memset (opts, '\0', sizeof (opts));
2732
2733   /* Add -march= option.  */
2734   if (arch)
2735     {
2736       opts[num][0] = "-march=";
2737       opts[num++][1] = arch;
2738     }
2739
2740   /* Add -mtune= option.  */
2741   if (tune)
2742     {
2743       opts[num][0] = "-mtune=";
2744       opts[num++][1] = tune;
2745     }
2746
2747   /* Pick out the options in isa options.  */
2748   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
2749     {
2750       if ((isa & isa_opts[i].mask) != 0)
2751         {
2752           opts[num++][0] = isa_opts[i].option;
2753           isa &= ~ isa_opts[i].mask;
2754         }
2755     }
2756
2757   if (isa && add_nl_p)
2758     {
2759       opts[num++][0] = isa_other;
2760       sprintf (isa_other, "(other isa: %#" HOST_WIDE_INT_PRINT "x)",
2761                isa);
2762     }
2763
2764   /* Add flag options.  */
2765   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
2766     {
2767       if ((flags & flag_opts[i].mask) != 0)
2768         {
2769           opts[num++][0] = flag_opts[i].option;
2770           flags &= ~ flag_opts[i].mask;
2771         }
2772     }
2773
2774   if (flags && add_nl_p)
2775     {
2776       opts[num++][0] = target_other;
2777       sprintf (target_other, "(other flags: %#x)", flags);
2778     }
2779
2780   /* Add -fpmath= option.  */
2781   if (fpmath)
2782     {
2783       opts[num][0] = "-mfpmath=";
2784       switch ((int) fpmath)
2785         {
2786         case FPMATH_387:
2787           opts[num++][1] = "387";
2788           break;
2789
2790         case FPMATH_SSE:
2791           opts[num++][1] = "sse";
2792           break;
2793
2794         case FPMATH_387 | FPMATH_SSE:
2795           opts[num++][1] = "sse+387";
2796           break;
2797
2798         default:
2799           gcc_unreachable ();
2800         }
2801     }
2802
2803   /* Any options?  */
2804   if (num == 0)
2805     return NULL;
2806
2807   gcc_assert (num < ARRAY_SIZE (opts));
2808
2809   /* Size the string.  */
2810   len = 0;
2811   sep_len = (add_nl_p) ? 3 : 1;
2812   for (i = 0; i < num; i++)
2813     {
2814       len += sep_len;
2815       for (j = 0; j < 2; j++)
2816         if (opts[i][j])
2817           len += strlen (opts[i][j]);
2818     }
2819
2820   /* Build the string.  */
2821   ret = ptr = (char *) xmalloc (len);
2822   line_len = 0;
2823
2824   for (i = 0; i < num; i++)
2825     {
2826       size_t len2[2];
2827
2828       for (j = 0; j < 2; j++)
2829         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
2830
2831       if (i != 0)
2832         {
2833           *ptr++ = ' ';
2834           line_len++;
2835
2836           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
2837             {
2838               *ptr++ = '\\';
2839               *ptr++ = '\n';
2840               line_len = 0;
2841             }
2842         }
2843
2844       for (j = 0; j < 2; j++)
2845         if (opts[i][j])
2846           {
2847             memcpy (ptr, opts[i][j], len2[j]);
2848             ptr += len2[j];
2849             line_len += len2[j];
2850           }
2851     }
2852
2853   *ptr = '\0';
2854   gcc_assert (ret + len >= ptr);
2855
2856   return ret;
2857 }
2858
2859 /* Return true, if profiling code should be emitted before
2860    prologue. Otherwise it returns false.
2861    Note: For x86 with "hotfix" it is sorried.  */
2862 static bool
2863 ix86_profile_before_prologue (void)
2864 {
2865   return flag_fentry != 0;
2866 }
2867
2868 /* Function that is callable from the debugger to print the current
2869    options.  */
2870 void
2871 ix86_debug_options (void)
2872 {
2873   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
2874                                    ix86_arch_string, ix86_tune_string,
2875                                    ix86_fpmath, true);
2876
2877   if (opts)
2878     {
2879       fprintf (stderr, "%s\n\n", opts);
2880       free (opts);
2881     }
2882   else
2883     fputs ("<no options>\n\n", stderr);
2884
2885   return;
2886 }
2887 \f
2888 /* Override various settings based on options.  If MAIN_ARGS_P, the
2889    options are from the command line, otherwise they are from
2890    attributes.  */
2891
2892 static void
2893 ix86_option_override_internal (bool main_args_p)
2894 {
2895   int i;
2896   unsigned int ix86_arch_mask, ix86_tune_mask;
2897   const bool ix86_tune_specified = (ix86_tune_string != NULL);
2898   const char *prefix;
2899   const char *suffix;
2900   const char *sw;
2901
2902 #define PTA_3DNOW               (HOST_WIDE_INT_1 << 0)
2903 #define PTA_3DNOW_A             (HOST_WIDE_INT_1 << 1)
2904 #define PTA_64BIT               (HOST_WIDE_INT_1 << 2)
2905 #define PTA_ABM                 (HOST_WIDE_INT_1 << 3)
2906 #define PTA_AES                 (HOST_WIDE_INT_1 << 4)
2907 #define PTA_AVX                 (HOST_WIDE_INT_1 << 5)
2908 #define PTA_BMI                 (HOST_WIDE_INT_1 << 6)
2909 #define PTA_CX16                (HOST_WIDE_INT_1 << 7)
2910 #define PTA_F16C                (HOST_WIDE_INT_1 << 8)
2911 #define PTA_FMA                 (HOST_WIDE_INT_1 << 9)
2912 #define PTA_FMA4                (HOST_WIDE_INT_1 << 10)
2913 #define PTA_FSGSBASE            (HOST_WIDE_INT_1 << 11)
2914 #define PTA_LWP                 (HOST_WIDE_INT_1 << 12)
2915 #define PTA_LZCNT               (HOST_WIDE_INT_1 << 13)
2916 #define PTA_MMX                 (HOST_WIDE_INT_1 << 14)
2917 #define PTA_MOVBE               (HOST_WIDE_INT_1 << 15)
2918 #define PTA_NO_SAHF             (HOST_WIDE_INT_1 << 16)
2919 #define PTA_PCLMUL              (HOST_WIDE_INT_1 << 17)
2920 #define PTA_POPCNT              (HOST_WIDE_INT_1 << 18)
2921 #define PTA_PREFETCH_SSE        (HOST_WIDE_INT_1 << 19)
2922 #define PTA_RDRND               (HOST_WIDE_INT_1 << 20)
2923 #define PTA_SSE                 (HOST_WIDE_INT_1 << 21)
2924 #define PTA_SSE2                (HOST_WIDE_INT_1 << 22)
2925 #define PTA_SSE3                (HOST_WIDE_INT_1 << 23)
2926 #define PTA_SSE4_1              (HOST_WIDE_INT_1 << 24)
2927 #define PTA_SSE4_2              (HOST_WIDE_INT_1 << 25)
2928 #define PTA_SSE4A               (HOST_WIDE_INT_1 << 26)
2929 #define PTA_SSSE3               (HOST_WIDE_INT_1 << 27)
2930 #define PTA_TBM                 (HOST_WIDE_INT_1 << 28)
2931 #define PTA_XOP                 (HOST_WIDE_INT_1 << 29)
2932 #define PTA_AVX2                (HOST_WIDE_INT_1 << 30)
2933 #define PTA_BMI2                (HOST_WIDE_INT_1 << 31)
2934 /* if this reaches 64, need to widen struct pta flags below */
2935
2936   static struct pta
2937     {
2938       const char *const name;           /* processor name or nickname.  */
2939       const enum processor_type processor;
2940       const enum attr_cpu schedule;
2941       const unsigned HOST_WIDE_INT flags;
2942     }
2943   const processor_alias_table[] =
2944     {
2945       {"i386", PROCESSOR_I386, CPU_NONE, 0},
2946       {"i486", PROCESSOR_I486, CPU_NONE, 0},
2947       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2948       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2949       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
2950       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
2951       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2952       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2953       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
2954       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2955       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2956       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
2957       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2958         PTA_MMX | PTA_SSE},
2959       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2960         PTA_MMX | PTA_SSE},
2961       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2962         PTA_MMX | PTA_SSE | PTA_SSE2},
2963       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
2964         PTA_MMX |PTA_SSE | PTA_SSE2},
2965       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
2966         PTA_MMX | PTA_SSE | PTA_SSE2},
2967       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
2968         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
2969       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
2970         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2971         | PTA_CX16 | PTA_NO_SAHF},
2972       {"core2", PROCESSOR_CORE2_64, CPU_CORE2,
2973         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2974         | PTA_SSSE3 | PTA_CX16},
2975       {"corei7", PROCESSOR_COREI7_64, CPU_COREI7,
2976         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2977         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_CX16},
2978       {"corei7-avx", PROCESSOR_COREI7_64, CPU_COREI7,
2979         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2980         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AVX
2981         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL},
2982       {"core-avx-i", PROCESSOR_COREI7_64, CPU_COREI7,
2983         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2984         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AVX
2985         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL | PTA_FSGSBASE
2986         | PTA_RDRND | PTA_F16C},
2987       {"core-avx2", PROCESSOR_COREI7_64, CPU_COREI7,
2988         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2989         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AVX | PTA_AVX2
2990         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL | PTA_FSGSBASE
2991         | PTA_RDRND | PTA_F16C | PTA_BMI | PTA_BMI2 | PTA_LZCNT
2992         | PTA_FMA | PTA_MOVBE},
2993       {"atom", PROCESSOR_ATOM, CPU_ATOM,
2994         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2995         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
2996       {"geode", PROCESSOR_GEODE, CPU_GEODE,
2997         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A |PTA_PREFETCH_SSE},
2998       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
2999       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3000       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3001       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
3002         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3003       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
3004         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3005       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
3006         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3007       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
3008         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3009       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
3010         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3011       {"x86-64", PROCESSOR_K8, CPU_K8,
3012         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
3013       {"k8", PROCESSOR_K8, CPU_K8,
3014         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3015         | PTA_SSE2 | PTA_NO_SAHF},
3016       {"k8-sse3", PROCESSOR_K8, CPU_K8,
3017         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3018         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3019       {"opteron", PROCESSOR_K8, CPU_K8,
3020         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3021         | PTA_SSE2 | PTA_NO_SAHF},
3022       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
3023         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3024         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3025       {"athlon64", PROCESSOR_K8, CPU_K8,
3026         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3027         | PTA_SSE2 | PTA_NO_SAHF},
3028       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
3029         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3030         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3031       {"athlon-fx", PROCESSOR_K8, CPU_K8,
3032         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3033         | PTA_SSE2 | PTA_NO_SAHF},
3034       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3035         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3036         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3037       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3038         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3039         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3040       {"bdver1", PROCESSOR_BDVER1, CPU_BDVER1,
3041         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3042         | PTA_SSE4A | PTA_CX16 | PTA_ABM | PTA_SSSE3 | PTA_SSE4_1
3043         | PTA_SSE4_2 | PTA_AES | PTA_PCLMUL | PTA_AVX | PTA_FMA4
3044         | PTA_XOP | PTA_LWP},
3045       {"bdver2", PROCESSOR_BDVER2, CPU_BDVER2,
3046         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3047         | PTA_SSE4A | PTA_CX16 | PTA_ABM | PTA_SSSE3 | PTA_SSE4_1
3048         | PTA_SSE4_2 | PTA_AES | PTA_PCLMUL | PTA_AVX
3049         | PTA_XOP | PTA_LWP | PTA_BMI | PTA_TBM | PTA_F16C
3050         | PTA_FMA},
3051       {"btver1", PROCESSOR_BTVER1, CPU_GENERIC64,
3052         PTA_64BIT | PTA_MMX |  PTA_SSE  | PTA_SSE2 | PTA_SSE3
3053         | PTA_SSSE3 | PTA_SSE4A |PTA_ABM | PTA_CX16},
3054       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
3055         0 /* flags are only used for -march switch.  */ },
3056       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
3057         PTA_64BIT /* flags are only used for -march switch.  */ },
3058     };
3059
3060   /* -mrecip options.  */
3061   static struct
3062     {
3063       const char *string;           /* option name */
3064       unsigned int mask;            /* mask bits to set */
3065     }
3066   const recip_options[] =
3067     {
3068       { "all",       RECIP_MASK_ALL },
3069       { "none",      RECIP_MASK_NONE },
3070       { "div",       RECIP_MASK_DIV },
3071       { "sqrt",      RECIP_MASK_SQRT },
3072       { "vec-div",   RECIP_MASK_VEC_DIV },
3073       { "vec-sqrt",  RECIP_MASK_VEC_SQRT },
3074     };
3075
3076   int const pta_size = ARRAY_SIZE (processor_alias_table);
3077
3078   /* Set up prefix/suffix so the error messages refer to either the command
3079      line argument, or the attribute(target).  */
3080   if (main_args_p)
3081     {
3082       prefix = "-m";
3083       suffix = "";
3084       sw = "switch";
3085     }
3086   else
3087     {
3088       prefix = "option(\"";
3089       suffix = "\")";
3090       sw = "attribute";
3091     }
3092
3093 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3094   SUBTARGET_OVERRIDE_OPTIONS;
3095 #endif
3096
3097 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3098   SUBSUBTARGET_OVERRIDE_OPTIONS;
3099 #endif
3100
3101   if (TARGET_X32)
3102     ix86_isa_flags |= OPTION_MASK_ISA_64BIT;
3103
3104   /* -fPIC is the default for x86_64.  */
3105   if (TARGET_MACHO && TARGET_64BIT)
3106     flag_pic = 2;
3107
3108   /* Need to check -mtune=generic first.  */
3109   if (ix86_tune_string)
3110     {
3111       if (!strcmp (ix86_tune_string, "generic")
3112           || !strcmp (ix86_tune_string, "i686")
3113           /* As special support for cross compilers we read -mtune=native
3114              as -mtune=generic.  With native compilers we won't see the
3115              -mtune=native, as it was changed by the driver.  */
3116           || !strcmp (ix86_tune_string, "native"))
3117         {
3118           if (TARGET_64BIT)
3119             ix86_tune_string = "generic64";
3120           else
3121             ix86_tune_string = "generic32";
3122         }
3123       /* If this call is for setting the option attribute, allow the
3124          generic32/generic64 that was previously set.  */
3125       else if (!main_args_p
3126                && (!strcmp (ix86_tune_string, "generic32")
3127                    || !strcmp (ix86_tune_string, "generic64")))
3128         ;
3129       else if (!strncmp (ix86_tune_string, "generic", 7))
3130         error ("bad value (%s) for %stune=%s %s",
3131                ix86_tune_string, prefix, suffix, sw);
3132       else if (!strcmp (ix86_tune_string, "x86-64"))
3133         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated; use "
3134                  "%stune=k8%s or %stune=generic%s instead as appropriate",
3135                  prefix, suffix, prefix, suffix, prefix, suffix);
3136     }
3137   else
3138     {
3139       if (ix86_arch_string)
3140         ix86_tune_string = ix86_arch_string;
3141       if (!ix86_tune_string)
3142         {
3143           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
3144           ix86_tune_defaulted = 1;
3145         }
3146
3147       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
3148          need to use a sensible tune option.  */
3149       if (!strcmp (ix86_tune_string, "generic")
3150           || !strcmp (ix86_tune_string, "x86-64")
3151           || !strcmp (ix86_tune_string, "i686"))
3152         {
3153           if (TARGET_64BIT)
3154             ix86_tune_string = "generic64";
3155           else
3156             ix86_tune_string = "generic32";
3157         }
3158     }
3159
3160   if (ix86_stringop_alg == rep_prefix_8_byte && !TARGET_64BIT)
3161     {
3162       /* rep; movq isn't available in 32-bit code.  */
3163       error ("-mstringop-strategy=rep_8byte not supported for 32-bit code");
3164       ix86_stringop_alg = no_stringop;
3165     }
3166
3167   if (!ix86_arch_string)
3168     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
3169   else
3170     ix86_arch_specified = 1;
3171
3172   if (!global_options_set.x_ix86_abi)
3173     ix86_abi = DEFAULT_ABI;
3174
3175   if (global_options_set.x_ix86_cmodel)
3176     {
3177       switch (ix86_cmodel)
3178         {
3179         case CM_SMALL:
3180         case CM_SMALL_PIC:
3181           if (flag_pic)
3182             ix86_cmodel = CM_SMALL_PIC;
3183           if (!TARGET_64BIT)
3184             error ("code model %qs not supported in the %s bit mode",
3185                    "small", "32");
3186           break;
3187
3188         case CM_MEDIUM:
3189         case CM_MEDIUM_PIC:
3190           if (flag_pic)
3191             ix86_cmodel = CM_MEDIUM_PIC;
3192           if (!TARGET_64BIT)
3193             error ("code model %qs not supported in the %s bit mode",
3194                    "medium", "32");
3195           else if (TARGET_X32)
3196             error ("code model %qs not supported in x32 mode",
3197                    "medium");
3198           break;
3199
3200         case CM_LARGE:
3201         case CM_LARGE_PIC:
3202           if (flag_pic)
3203             ix86_cmodel = CM_LARGE_PIC;
3204           if (!TARGET_64BIT)
3205             error ("code model %qs not supported in the %s bit mode",
3206                    "large", "32");
3207           else if (TARGET_X32)
3208             error ("code model %qs not supported in x32 mode",
3209                    "large");
3210           break;
3211
3212         case CM_32:
3213           if (flag_pic)
3214             error ("code model %s does not support PIC mode", "32");
3215           if (TARGET_64BIT)
3216             error ("code model %qs not supported in the %s bit mode",
3217                    "32", "64");
3218           break;
3219
3220         case CM_KERNEL:
3221           if (flag_pic)
3222             {
3223               error ("code model %s does not support PIC mode", "kernel");
3224               ix86_cmodel = CM_32;
3225             }
3226           if (!TARGET_64BIT)
3227             error ("code model %qs not supported in the %s bit mode",
3228                    "kernel", "32");
3229           break;
3230
3231         default:
3232           gcc_unreachable ();
3233         }
3234     }
3235   else
3236     {
3237       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
3238          use of rip-relative addressing.  This eliminates fixups that
3239          would otherwise be needed if this object is to be placed in a
3240          DLL, and is essentially just as efficient as direct addressing.  */
3241       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
3242         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
3243       else if (TARGET_64BIT)
3244         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3245       else
3246         ix86_cmodel = CM_32;
3247     }
3248   if (TARGET_MACHO && ix86_asm_dialect == ASM_INTEL)
3249     {
3250       error ("-masm=intel not supported in this configuration");
3251       ix86_asm_dialect = ASM_ATT;
3252     }
3253   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
3254     sorry ("%i-bit mode not compiled in",
3255            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
3256
3257   for (i = 0; i < pta_size; i++)
3258     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
3259       {
3260         ix86_schedule = processor_alias_table[i].schedule;
3261         ix86_arch = processor_alias_table[i].processor;
3262         /* Default cpu tuning to the architecture.  */
3263         ix86_tune = ix86_arch;
3264
3265         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3266           error ("CPU you selected does not support x86-64 "
3267                  "instruction set");
3268
3269         if (processor_alias_table[i].flags & PTA_MMX
3270             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
3271           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
3272         if (processor_alias_table[i].flags & PTA_3DNOW
3273             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
3274           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
3275         if (processor_alias_table[i].flags & PTA_3DNOW_A
3276             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
3277           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
3278         if (processor_alias_table[i].flags & PTA_SSE
3279             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
3280           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
3281         if (processor_alias_table[i].flags & PTA_SSE2
3282             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
3283           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
3284         if (processor_alias_table[i].flags & PTA_SSE3
3285             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
3286           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
3287         if (processor_alias_table[i].flags & PTA_SSSE3
3288             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
3289           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
3290         if (processor_alias_table[i].flags & PTA_SSE4_1
3291             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
3292           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
3293         if (processor_alias_table[i].flags & PTA_SSE4_2
3294             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
3295           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
3296         if (processor_alias_table[i].flags & PTA_AVX
3297             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
3298           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
3299         if (processor_alias_table[i].flags & PTA_AVX2
3300             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX2))
3301           ix86_isa_flags |= OPTION_MASK_ISA_AVX2;
3302         if (processor_alias_table[i].flags & PTA_FMA
3303             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
3304           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
3305         if (processor_alias_table[i].flags & PTA_SSE4A
3306             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3307           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3308         if (processor_alias_table[i].flags & PTA_FMA4
3309             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3310           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3311         if (processor_alias_table[i].flags & PTA_XOP
3312             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3313           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3314         if (processor_alias_table[i].flags & PTA_LWP
3315             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3316           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3317         if (processor_alias_table[i].flags & PTA_ABM
3318             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3319           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3320         if (processor_alias_table[i].flags & PTA_BMI
3321             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_BMI))
3322           ix86_isa_flags |= OPTION_MASK_ISA_BMI;
3323         if (processor_alias_table[i].flags & (PTA_LZCNT | PTA_ABM)
3324             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LZCNT))
3325           ix86_isa_flags |= OPTION_MASK_ISA_LZCNT;
3326         if (processor_alias_table[i].flags & PTA_TBM
3327             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_TBM))
3328           ix86_isa_flags |= OPTION_MASK_ISA_TBM;
3329         if (processor_alias_table[i].flags & PTA_BMI2
3330             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_BMI2))
3331           ix86_isa_flags |= OPTION_MASK_ISA_BMI2;
3332         if (processor_alias_table[i].flags & PTA_CX16
3333             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3334           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3335         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3336             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3337           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3338         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3339             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3340           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3341         if (processor_alias_table[i].flags & PTA_MOVBE
3342             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3343           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3344         if (processor_alias_table[i].flags & PTA_AES
3345             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3346           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3347         if (processor_alias_table[i].flags & PTA_PCLMUL
3348             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3349           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3350         if (processor_alias_table[i].flags & PTA_FSGSBASE
3351             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FSGSBASE))
3352           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE;
3353         if (processor_alias_table[i].flags & PTA_RDRND
3354             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_RDRND))
3355           ix86_isa_flags |= OPTION_MASK_ISA_RDRND;
3356         if (processor_alias_table[i].flags & PTA_F16C
3357             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_F16C))
3358           ix86_isa_flags |= OPTION_MASK_ISA_F16C;
3359         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3360           x86_prefetch_sse = true;
3361
3362         break;
3363       }
3364
3365   if (!strcmp (ix86_arch_string, "generic"))
3366     error ("generic CPU can be used only for %stune=%s %s",
3367            prefix, suffix, sw);
3368   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3369     error ("bad value (%s) for %sarch=%s %s",
3370            ix86_arch_string, prefix, suffix, sw);
3371
3372   ix86_arch_mask = 1u << ix86_arch;
3373   for (i = 0; i < X86_ARCH_LAST; ++i)
3374     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3375
3376   for (i = 0; i < pta_size; i++)
3377     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3378       {
3379         ix86_schedule = processor_alias_table[i].schedule;
3380         ix86_tune = processor_alias_table[i].processor;
3381         if (TARGET_64BIT)
3382           {
3383             if (!(processor_alias_table[i].flags & PTA_64BIT))
3384               {
3385                 if (ix86_tune_defaulted)
3386                   {
3387                     ix86_tune_string = "x86-64";
3388                     for (i = 0; i < pta_size; i++)
3389                       if (! strcmp (ix86_tune_string,
3390                                     processor_alias_table[i].name))
3391                         break;
3392                     ix86_schedule = processor_alias_table[i].schedule;
3393                     ix86_tune = processor_alias_table[i].processor;
3394                   }
3395                 else
3396                   error ("CPU you selected does not support x86-64 "
3397                          "instruction set");
3398               }
3399           }
3400         else
3401           {
3402             /* Adjust tuning when compiling for 32-bit ABI.  */
3403             switch (ix86_tune)
3404               {
3405               case PROCESSOR_GENERIC64:
3406                 ix86_tune = PROCESSOR_GENERIC32;
3407                 ix86_schedule = CPU_PENTIUMPRO;
3408                 break;
3409
3410               case PROCESSOR_CORE2_64:
3411                 ix86_tune = PROCESSOR_CORE2_32;
3412                 break;
3413
3414               case PROCESSOR_COREI7_64:
3415                 ix86_tune = PROCESSOR_COREI7_32;
3416                 break;
3417
3418               default:
3419                 break;
3420               }
3421           }
3422         /* Intel CPUs have always interpreted SSE prefetch instructions as
3423            NOPs; so, we can enable SSE prefetch instructions even when
3424            -mtune (rather than -march) points us to a processor that has them.
3425            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3426            higher processors.  */
3427         if (TARGET_CMOV
3428             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3429           x86_prefetch_sse = true;
3430         break;
3431       }
3432
3433   if (ix86_tune_specified && i == pta_size)
3434     error ("bad value (%s) for %stune=%s %s",
3435            ix86_tune_string, prefix, suffix, sw);
3436
3437   ix86_tune_mask = 1u << ix86_tune;
3438   for (i = 0; i < X86_TUNE_LAST; ++i)
3439     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3440
3441 #ifndef USE_IX86_FRAME_POINTER
3442 #define USE_IX86_FRAME_POINTER 0
3443 #endif
3444
3445 #ifndef USE_X86_64_FRAME_POINTER
3446 #define USE_X86_64_FRAME_POINTER 0
3447 #endif
3448
3449   /* Set the default values for switches whose default depends on TARGET_64BIT
3450      in case they weren't overwritten by command line options.  */
3451   if (TARGET_64BIT)
3452     {
3453       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3454         flag_omit_frame_pointer = !USE_X86_64_FRAME_POINTER;
3455       if (flag_asynchronous_unwind_tables == 2)
3456         flag_unwind_tables = flag_asynchronous_unwind_tables = 1;
3457       if (flag_pcc_struct_return == 2)
3458         flag_pcc_struct_return = 0;
3459     }
3460   else
3461     {
3462       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3463         flag_omit_frame_pointer = !(USE_IX86_FRAME_POINTER || optimize_size);
3464       if (flag_asynchronous_unwind_tables == 2)
3465         flag_asynchronous_unwind_tables = !USE_IX86_FRAME_POINTER;
3466       if (flag_pcc_struct_return == 2)
3467         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
3468     }
3469
3470   if (optimize_size)
3471     ix86_cost = &ix86_size_cost;
3472   else
3473     ix86_cost = processor_target_table[ix86_tune].cost;
3474
3475   /* Arrange to set up i386_stack_locals for all functions.  */
3476   init_machine_status = ix86_init_machine_status;
3477
3478   /* Validate -mregparm= value.  */
3479   if (global_options_set.x_ix86_regparm)
3480     {
3481       if (TARGET_64BIT)
3482         warning (0, "-mregparm is ignored in 64-bit mode");
3483       if (ix86_regparm > REGPARM_MAX)
3484         {
3485           error ("-mregparm=%d is not between 0 and %d",
3486                  ix86_regparm, REGPARM_MAX);
3487           ix86_regparm = 0;
3488         }
3489     }
3490   if (TARGET_64BIT)
3491     ix86_regparm = REGPARM_MAX;
3492
3493   /* Default align_* from the processor table.  */
3494   if (align_loops == 0)
3495     {
3496       align_loops = processor_target_table[ix86_tune].align_loop;
3497       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3498     }
3499   if (align_jumps == 0)
3500     {
3501       align_jumps = processor_target_table[ix86_tune].align_jump;
3502       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3503     }
3504   if (align_functions == 0)
3505     {
3506       align_functions = processor_target_table[ix86_tune].align_func;
3507     }
3508
3509   /* Provide default for -mbranch-cost= value.  */
3510   if (!global_options_set.x_ix86_branch_cost)
3511     ix86_branch_cost = ix86_cost->branch_cost;
3512
3513   if (TARGET_64BIT)
3514     {
3515       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3516
3517       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3518          explicitly disable any of these.  In particular, disabling SSE and
3519          MMX for kernel code is extremely useful.  */
3520       if (!ix86_arch_specified)
3521       ix86_isa_flags
3522         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3523              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3524
3525       if (TARGET_RTD)
3526         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3527     }
3528   else
3529     {
3530       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3531
3532       if (!ix86_arch_specified)
3533       ix86_isa_flags
3534         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3535
3536       /* i386 ABI does not specify red zone.  It still makes sense to use it
3537          when programmer takes care to stack from being destroyed.  */
3538       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3539         target_flags |= MASK_NO_RED_ZONE;
3540     }
3541
3542   /* Keep nonleaf frame pointers.  */
3543   if (flag_omit_frame_pointer)
3544     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3545   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3546     flag_omit_frame_pointer = 1;
3547
3548   /* If we're doing fast math, we don't care about comparison order
3549      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3550   if (flag_finite_math_only)
3551     target_flags &= ~MASK_IEEE_FP;
3552
3553   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3554      since the insns won't need emulation.  */
3555   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3556     target_flags &= ~MASK_NO_FANCY_MATH_387;
3557
3558   /* Likewise, if the target doesn't have a 387, or we've specified
3559      software floating point, don't use 387 inline intrinsics.  */
3560   if (!TARGET_80387)
3561     target_flags |= MASK_NO_FANCY_MATH_387;
3562
3563   /* Turn on MMX builtins for -msse.  */
3564   if (TARGET_SSE)
3565     {
3566       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3567       x86_prefetch_sse = true;
3568     }
3569
3570   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3571   if (TARGET_SSE4_2 || TARGET_ABM)
3572     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3573
3574   /* Turn on lzcnt instruction for -mabm.  */
3575   if (TARGET_ABM)
3576     ix86_isa_flags |= OPTION_MASK_ISA_LZCNT & ~ix86_isa_flags_explicit;
3577
3578   /* Validate -mpreferred-stack-boundary= value or default it to
3579      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3580   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3581   if (global_options_set.x_ix86_preferred_stack_boundary_arg)
3582     {
3583       int min = (TARGET_64BIT ? 4 : 2);
3584       int max = (TARGET_SEH ? 4 : 12);
3585
3586       if (ix86_preferred_stack_boundary_arg < min
3587           || ix86_preferred_stack_boundary_arg > max)
3588         {
3589           if (min == max)
3590             error ("-mpreferred-stack-boundary is not supported "
3591                    "for this target");
3592           else
3593             error ("-mpreferred-stack-boundary=%d is not between %d and %d",
3594                    ix86_preferred_stack_boundary_arg, min, max);
3595         }
3596       else
3597         ix86_preferred_stack_boundary
3598           = (1 << ix86_preferred_stack_boundary_arg) * BITS_PER_UNIT;
3599     }
3600
3601   /* Set the default value for -mstackrealign.  */
3602   if (ix86_force_align_arg_pointer == -1)
3603     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3604
3605   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3606
3607   /* Validate -mincoming-stack-boundary= value or default it to
3608      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3609   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3610   if (global_options_set.x_ix86_incoming_stack_boundary_arg)
3611     {
3612       if (ix86_incoming_stack_boundary_arg < (TARGET_64BIT ? 4 : 2)
3613           || ix86_incoming_stack_boundary_arg > 12)
3614         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3615                ix86_incoming_stack_boundary_arg, TARGET_64BIT ? 4 : 2);
3616       else
3617         {
3618           ix86_user_incoming_stack_boundary
3619             = (1 << ix86_incoming_stack_boundary_arg) * BITS_PER_UNIT;
3620           ix86_incoming_stack_boundary
3621             = ix86_user_incoming_stack_boundary;
3622         }
3623     }
3624
3625   /* Accept -msseregparm only if at least SSE support is enabled.  */
3626   if (TARGET_SSEREGPARM
3627       && ! TARGET_SSE)
3628     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3629
3630   if (global_options_set.x_ix86_fpmath)
3631     {
3632       if (ix86_fpmath & FPMATH_SSE)
3633         {
3634           if (!TARGET_SSE)
3635             {
3636               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3637               ix86_fpmath = FPMATH_387;
3638             }
3639           else if ((ix86_fpmath & FPMATH_387) && !TARGET_80387)
3640             {
3641               warning (0, "387 instruction set disabled, using SSE arithmetics");
3642               ix86_fpmath = FPMATH_SSE;
3643             }
3644         }
3645     }
3646   else
3647     ix86_fpmath = TARGET_FPMATH_DEFAULT;
3648
3649   /* If the i387 is disabled, then do not return values in it. */
3650   if (!TARGET_80387)
3651     target_flags &= ~MASK_FLOAT_RETURNS;
3652
3653   /* Use external vectorized library in vectorizing intrinsics.  */
3654   if (global_options_set.x_ix86_veclibabi_type)
3655     switch (ix86_veclibabi_type)
3656       {
3657       case ix86_veclibabi_type_svml:
3658         ix86_veclib_handler = ix86_veclibabi_svml;
3659         break;
3660
3661       case ix86_veclibabi_type_acml:
3662         ix86_veclib_handler = ix86_veclibabi_acml;
3663         break;
3664
3665       default:
3666         gcc_unreachable ();
3667       }
3668
3669   if ((!USE_IX86_FRAME_POINTER
3670        || (x86_accumulate_outgoing_args & ix86_tune_mask))
3671       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3672       && !optimize_size)
3673     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3674
3675   /* ??? Unwind info is not correct around the CFG unless either a frame
3676      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
3677      unwind info generation to be aware of the CFG and propagating states
3678      around edges.  */
3679   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
3680        || flag_exceptions || flag_non_call_exceptions)
3681       && flag_omit_frame_pointer
3682       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3683     {
3684       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3685         warning (0, "unwind tables currently require either a frame pointer "
3686                  "or %saccumulate-outgoing-args%s for correctness",
3687                  prefix, suffix);
3688       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3689     }
3690
3691   /* If stack probes are required, the space used for large function
3692      arguments on the stack must also be probed, so enable
3693      -maccumulate-outgoing-args so this happens in the prologue.  */
3694   if (TARGET_STACK_PROBE
3695       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3696     {
3697       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3698         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
3699                  "for correctness", prefix, suffix);
3700       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3701     }
3702
3703   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
3704   {
3705     char *p;
3706     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
3707     p = strchr (internal_label_prefix, 'X');
3708     internal_label_prefix_len = p - internal_label_prefix;
3709     *p = '\0';
3710   }
3711
3712   /* When scheduling description is not available, disable scheduler pass
3713      so it won't slow down the compilation and make x87 code slower.  */
3714   if (!TARGET_SCHEDULE)
3715     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
3716
3717   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3718                          ix86_cost->simultaneous_prefetches,
3719                          global_options.x_param_values,
3720                          global_options_set.x_param_values);
3721   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, ix86_cost->prefetch_block,
3722                          global_options.x_param_values,
3723                          global_options_set.x_param_values);
3724   maybe_set_param_value (PARAM_L1_CACHE_SIZE, ix86_cost->l1_cache_size,
3725                          global_options.x_param_values,
3726                          global_options_set.x_param_values);
3727   maybe_set_param_value (PARAM_L2_CACHE_SIZE, ix86_cost->l2_cache_size,
3728                          global_options.x_param_values,
3729                          global_options_set.x_param_values);
3730
3731   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
3732   if (flag_prefetch_loop_arrays < 0
3733       && HAVE_prefetch
3734       && optimize >= 3
3735       && TARGET_SOFTWARE_PREFETCHING_BENEFICIAL)
3736     flag_prefetch_loop_arrays = 1;
3737
3738   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3739      can be optimized to ap = __builtin_next_arg (0).  */
3740   if (!TARGET_64BIT && !flag_split_stack)
3741     targetm.expand_builtin_va_start = NULL;
3742
3743   if (TARGET_64BIT)
3744     {
3745       ix86_gen_leave = gen_leave_rex64;
3746       ix86_gen_add3 = gen_adddi3;
3747       ix86_gen_sub3 = gen_subdi3;
3748       ix86_gen_sub3_carry = gen_subdi3_carry;
3749       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
3750       ix86_gen_monitor = gen_sse3_monitor64;
3751       ix86_gen_andsp = gen_anddi3;
3752       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_di;
3753       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probedi;
3754       ix86_gen_probe_stack_range = gen_probe_stack_rangedi;
3755     }
3756   else
3757     {
3758       ix86_gen_leave = gen_leave;
3759       ix86_gen_add3 = gen_addsi3;
3760       ix86_gen_sub3 = gen_subsi3;
3761       ix86_gen_sub3_carry = gen_subsi3_carry;
3762       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
3763       ix86_gen_monitor = gen_sse3_monitor;
3764       ix86_gen_andsp = gen_andsi3;
3765       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_si;
3766       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probesi;
3767       ix86_gen_probe_stack_range = gen_probe_stack_rangesi;
3768     }
3769
3770 #ifdef USE_IX86_CLD
3771   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
3772   if (!TARGET_64BIT)
3773     target_flags |= MASK_CLD & ~target_flags_explicit;
3774 #endif
3775
3776   if (!TARGET_64BIT && flag_pic)
3777     {
3778       if (flag_fentry > 0)
3779         sorry ("-mfentry isn%'t supported for 32-bit in combination "
3780                "with -fpic");
3781       flag_fentry = 0;
3782     }
3783   else if (TARGET_SEH)
3784     {
3785       if (flag_fentry == 0)
3786         sorry ("-mno-fentry isn%'t compatible with SEH");
3787       flag_fentry = 1;
3788     }
3789   else if (flag_fentry < 0)
3790    {
3791 #if defined(PROFILE_BEFORE_PROLOGUE)
3792      flag_fentry = 1;
3793 #else
3794      flag_fentry = 0;
3795 #endif
3796    }
3797
3798   if (TARGET_AVX)
3799     {
3800       /* When not optimize for size, enable vzeroupper optimization for
3801          TARGET_AVX with -fexpensive-optimizations and split 32-byte
3802          AVX unaligned load/store.  */
3803       if (!optimize_size)
3804         {
3805           if (flag_expensive_optimizations
3806               && !(target_flags_explicit & MASK_VZEROUPPER))
3807             target_flags |= MASK_VZEROUPPER;
3808           if ((x86_avx256_split_unaligned_load & ix86_tune_mask)
3809               && !(target_flags_explicit & MASK_AVX256_SPLIT_UNALIGNED_LOAD))
3810             target_flags |= MASK_AVX256_SPLIT_UNALIGNED_LOAD;
3811           if ((x86_avx256_split_unaligned_store & ix86_tune_mask)
3812               && !(target_flags_explicit & MASK_AVX256_SPLIT_UNALIGNED_STORE))
3813             target_flags |= MASK_AVX256_SPLIT_UNALIGNED_STORE;
3814           /* Enable 128-bit AVX instruction generation for the auto-vectorizer.  */
3815           if (TARGET_AVX128_OPTIMAL && !(target_flags_explicit & MASK_PREFER_AVX128))
3816             target_flags |= MASK_PREFER_AVX128;
3817         }
3818     }
3819   else
3820     {
3821       /* Disable vzeroupper pass if TARGET_AVX is disabled.  */
3822       target_flags &= ~MASK_VZEROUPPER;
3823     }
3824
3825   if (ix86_recip_name)
3826     {
3827       char *p = ASTRDUP (ix86_recip_name);
3828       char *q;
3829       unsigned int mask, i;
3830       bool invert;
3831
3832       while ((q = strtok (p, ",")) != NULL)
3833         {
3834           p = NULL;
3835           if (*q == '!')
3836             {
3837               invert = true;
3838               q++;
3839             }
3840           else
3841             invert = false;
3842
3843           if (!strcmp (q, "default"))
3844             mask = RECIP_MASK_ALL;
3845           else
3846             {
3847               for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3848                 if (!strcmp (q, recip_options[i].string))
3849                   {
3850                     mask = recip_options[i].mask;
3851                     break;
3852                   }
3853
3854               if (i == ARRAY_SIZE (recip_options))
3855                 {
3856                   error ("unknown option for -mrecip=%s", q);
3857                   invert = false;
3858                   mask = RECIP_MASK_NONE;
3859                 }
3860             }
3861
3862           recip_mask_explicit |= mask;
3863           if (invert)
3864             recip_mask &= ~mask;
3865           else
3866             recip_mask |= mask;
3867         }
3868     }
3869
3870   if (TARGET_RECIP)
3871     recip_mask |= RECIP_MASK_ALL & ~recip_mask_explicit;
3872   else if (target_flags_explicit & MASK_RECIP)
3873     recip_mask &= ~(RECIP_MASK_ALL & ~recip_mask_explicit);
3874
3875   /* Save the initial options in case the user does function specific
3876      options.  */
3877   if (main_args_p)
3878     target_option_default_node = target_option_current_node
3879       = build_target_option_node ();
3880 }
3881
3882 /* Return TRUE if VAL is passed in register with 256bit AVX modes.  */
3883
3884 static bool
3885 function_pass_avx256_p (const_rtx val)
3886 {
3887   if (!val)
3888     return false;
3889
3890   if (REG_P (val) && VALID_AVX256_REG_MODE (GET_MODE (val)))
3891     return true;
3892
3893   if (GET_CODE (val) == PARALLEL)
3894     {
3895       int i;
3896       rtx r;
3897
3898       for (i = XVECLEN (val, 0) - 1; i >= 0; i--)
3899         {
3900           r = XVECEXP (val, 0, i);
3901           if (GET_CODE (r) == EXPR_LIST
3902               && XEXP (r, 0)
3903               && REG_P (XEXP (r, 0))
3904               && (GET_MODE (XEXP (r, 0)) == OImode
3905                   || VALID_AVX256_REG_MODE (GET_MODE (XEXP (r, 0)))))
3906             return true;
3907         }
3908     }
3909
3910   return false;
3911 }
3912
3913 /* Implement the TARGET_OPTION_OVERRIDE hook.  */
3914
3915 static void
3916 ix86_option_override (void)
3917 {
3918   ix86_option_override_internal (true);
3919 }
3920
3921 /* Update register usage after having seen the compiler flags.  */
3922
3923 static void
3924 ix86_conditional_register_usage (void)
3925 {
3926   int i;
3927   unsigned int j;
3928
3929   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3930     {
3931       if (fixed_regs[i] > 1)
3932         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
3933       if (call_used_regs[i] > 1)
3934         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
3935     }
3936
3937   /* The PIC register, if it exists, is fixed.  */
3938   j = PIC_OFFSET_TABLE_REGNUM;
3939   if (j != INVALID_REGNUM)
3940     fixed_regs[j] = call_used_regs[j] = 1;
3941
3942   /* The 64-bit MS_ABI changes the set of call-used registers.  */
3943   if (TARGET_64BIT_MS_ABI)
3944     {
3945       call_used_regs[SI_REG] = 0;
3946       call_used_regs[DI_REG] = 0;
3947       call_used_regs[XMM6_REG] = 0;
3948       call_used_regs[XMM7_REG] = 0;
3949       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3950         call_used_regs[i] = 0;
3951     }
3952
3953   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
3954      other call-clobbered regs for 64-bit.  */
3955   if (TARGET_64BIT)
3956     {
3957       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
3958
3959       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3960         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
3961             && call_used_regs[i])
3962           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
3963     }
3964
3965   /* If MMX is disabled, squash the registers.  */
3966   if (! TARGET_MMX)
3967     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3968       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
3969         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3970
3971   /* If SSE is disabled, squash the registers.  */
3972   if (! TARGET_SSE)
3973     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3974       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
3975         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3976
3977   /* If the FPU is disabled, squash the registers.  */
3978   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
3979     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3980       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
3981         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3982
3983   /* If 32-bit, squash the 64-bit registers.  */
3984   if (! TARGET_64BIT)
3985     {
3986       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
3987         reg_names[i] = "";
3988       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3989         reg_names[i] = "";
3990     }
3991 }
3992
3993 \f
3994 /* Save the current options */
3995
3996 static void
3997 ix86_function_specific_save (struct cl_target_option *ptr)
3998 {
3999   ptr->arch = ix86_arch;
4000   ptr->schedule = ix86_schedule;
4001   ptr->tune = ix86_tune;
4002   ptr->branch_cost = ix86_branch_cost;
4003   ptr->tune_defaulted = ix86_tune_defaulted;
4004   ptr->arch_specified = ix86_arch_specified;
4005   ptr->x_ix86_isa_flags_explicit = ix86_isa_flags_explicit;
4006   ptr->ix86_target_flags_explicit = target_flags_explicit;
4007   ptr->x_recip_mask_explicit = recip_mask_explicit;
4008
4009   /* The fields are char but the variables are not; make sure the
4010      values fit in the fields.  */
4011   gcc_assert (ptr->arch == ix86_arch);
4012   gcc_assert (ptr->schedule == ix86_schedule);
4013   gcc_assert (ptr->tune == ix86_tune);
4014   gcc_assert (ptr->branch_cost == ix86_branch_cost);
4015 }
4016
4017 /* Restore the current options */
4018
4019 static void
4020 ix86_function_specific_restore (struct cl_target_option *ptr)
4021 {
4022   enum processor_type old_tune = ix86_tune;
4023   enum processor_type old_arch = ix86_arch;
4024   unsigned int ix86_arch_mask, ix86_tune_mask;
4025   int i;
4026
4027   ix86_arch = (enum processor_type) ptr->arch;
4028   ix86_schedule = (enum attr_cpu) ptr->schedule;
4029   ix86_tune = (enum processor_type) ptr->tune;
4030   ix86_branch_cost = ptr->branch_cost;
4031   ix86_tune_defaulted = ptr->tune_defaulted;
4032   ix86_arch_specified = ptr->arch_specified;
4033   ix86_isa_flags_explicit = ptr->x_ix86_isa_flags_explicit;
4034   target_flags_explicit = ptr->ix86_target_flags_explicit;
4035   recip_mask_explicit = ptr->x_recip_mask_explicit;
4036
4037   /* Recreate the arch feature tests if the arch changed */
4038   if (old_arch != ix86_arch)
4039     {
4040       ix86_arch_mask = 1u << ix86_arch;
4041       for (i = 0; i < X86_ARCH_LAST; ++i)
4042         ix86_arch_features[i]
4043           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
4044     }
4045
4046   /* Recreate the tune optimization tests */
4047   if (old_tune != ix86_tune)
4048     {
4049       ix86_tune_mask = 1u << ix86_tune;
4050       for (i = 0; i < X86_TUNE_LAST; ++i)
4051         ix86_tune_features[i]
4052           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
4053     }
4054 }
4055
4056 /* Print the current options */
4057
4058 static void
4059 ix86_function_specific_print (FILE *file, int indent,
4060                               struct cl_target_option *ptr)
4061 {
4062   char *target_string
4063     = ix86_target_string (ptr->x_ix86_isa_flags, ptr->x_target_flags,
4064                           NULL, NULL, ptr->x_ix86_fpmath, false);
4065
4066   fprintf (file, "%*sarch = %d (%s)\n",
4067            indent, "",
4068            ptr->arch,
4069            ((ptr->arch < TARGET_CPU_DEFAULT_max)
4070             ? cpu_names[ptr->arch]
4071             : "<unknown>"));
4072
4073   fprintf (file, "%*stune = %d (%s)\n",
4074            indent, "",
4075            ptr->tune,
4076            ((ptr->tune < TARGET_CPU_DEFAULT_max)
4077             ? cpu_names[ptr->tune]
4078             : "<unknown>"));
4079
4080   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
4081
4082   if (target_string)
4083     {
4084       fprintf (file, "%*s%s\n", indent, "", target_string);
4085       free (target_string);
4086     }
4087 }
4088
4089 \f
4090 /* Inner function to process the attribute((target(...))), take an argument and
4091    set the current options from the argument. If we have a list, recursively go
4092    over the list.  */
4093
4094 static bool
4095 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[],
4096                                      struct gcc_options *enum_opts_set)
4097 {
4098   char *next_optstr;
4099   bool ret = true;
4100
4101 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
4102 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
4103 #define IX86_ATTR_ENUM(S,O)  { S, sizeof (S)-1, ix86_opt_enum, O, 0 }
4104 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
4105 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
4106
4107   enum ix86_opt_type
4108   {
4109     ix86_opt_unknown,
4110     ix86_opt_yes,
4111     ix86_opt_no,
4112     ix86_opt_str,
4113     ix86_opt_enum,
4114     ix86_opt_isa
4115   };
4116
4117   static const struct
4118   {
4119     const char *string;
4120     size_t len;
4121     enum ix86_opt_type type;
4122     int opt;
4123     int mask;
4124   } attrs[] = {
4125     /* isa options */
4126     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
4127     IX86_ATTR_ISA ("abm",       OPT_mabm),
4128     IX86_ATTR_ISA ("bmi",       OPT_mbmi),
4129     IX86_ATTR_ISA ("bmi2",      OPT_mbmi2),
4130     IX86_ATTR_ISA ("lzcnt",     OPT_mlzcnt),
4131     IX86_ATTR_ISA ("tbm",       OPT_mtbm),
4132     IX86_ATTR_ISA ("aes",       OPT_maes),
4133     IX86_ATTR_ISA ("avx",       OPT_mavx),
4134     IX86_ATTR_ISA ("avx2",      OPT_mavx2),
4135     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
4136     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
4137     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
4138     IX86_ATTR_ISA ("sse",       OPT_msse),
4139     IX86_ATTR_ISA ("sse2",      OPT_msse2),
4140     IX86_ATTR_ISA ("sse3",      OPT_msse3),
4141     IX86_ATTR_ISA ("sse4",      OPT_msse4),
4142     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
4143     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
4144     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
4145     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
4146     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
4147     IX86_ATTR_ISA ("fma",       OPT_mfma),
4148     IX86_ATTR_ISA ("xop",       OPT_mxop),
4149     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
4150     IX86_ATTR_ISA ("fsgsbase",  OPT_mfsgsbase),
4151     IX86_ATTR_ISA ("rdrnd",     OPT_mrdrnd),
4152     IX86_ATTR_ISA ("f16c",      OPT_mf16c),
4153
4154     /* enum options */
4155     IX86_ATTR_ENUM ("fpmath=",  OPT_mfpmath_),
4156
4157     /* string options */
4158     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
4159     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
4160
4161     /* flag options */
4162     IX86_ATTR_YES ("cld",
4163                    OPT_mcld,
4164                    MASK_CLD),
4165
4166     IX86_ATTR_NO ("fancy-math-387",
4167                   OPT_mfancy_math_387,
4168                   MASK_NO_FANCY_MATH_387),
4169
4170     IX86_ATTR_YES ("ieee-fp",
4171                    OPT_mieee_fp,
4172                    MASK_IEEE_FP),
4173
4174     IX86_ATTR_YES ("inline-all-stringops",
4175                    OPT_minline_all_stringops,
4176                    MASK_INLINE_ALL_STRINGOPS),
4177
4178     IX86_ATTR_YES ("inline-stringops-dynamically",
4179                    OPT_minline_stringops_dynamically,
4180                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
4181
4182     IX86_ATTR_NO ("align-stringops",
4183                   OPT_mno_align_stringops,
4184                   MASK_NO_ALIGN_STRINGOPS),
4185
4186     IX86_ATTR_YES ("recip",
4187                    OPT_mrecip,
4188                    MASK_RECIP),
4189
4190   };
4191
4192   /* If this is a list, recurse to get the options.  */
4193   if (TREE_CODE (args) == TREE_LIST)
4194     {
4195       bool ret = true;
4196
4197       for (; args; args = TREE_CHAIN (args))
4198         if (TREE_VALUE (args)
4199             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args),
4200                                                      p_strings, enum_opts_set))
4201           ret = false;
4202
4203       return ret;
4204     }
4205
4206   else if (TREE_CODE (args) != STRING_CST)
4207     gcc_unreachable ();
4208
4209   /* Handle multiple arguments separated by commas.  */
4210   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
4211
4212   while (next_optstr && *next_optstr != '\0')
4213     {
4214       char *p = next_optstr;
4215       char *orig_p = p;
4216       char *comma = strchr (next_optstr, ',');
4217       const char *opt_string;
4218       size_t len, opt_len;
4219       int opt;
4220       bool opt_set_p;
4221       char ch;
4222       unsigned i;
4223       enum ix86_opt_type type = ix86_opt_unknown;
4224       int mask = 0;
4225
4226       if (comma)
4227         {
4228           *comma = '\0';
4229           len = comma - next_optstr;
4230           next_optstr = comma + 1;
4231         }
4232       else
4233         {
4234           len = strlen (p);
4235           next_optstr = NULL;
4236         }
4237
4238       /* Recognize no-xxx.  */
4239       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
4240         {
4241           opt_set_p = false;
4242           p += 3;
4243           len -= 3;
4244         }
4245       else
4246         opt_set_p = true;
4247
4248       /* Find the option.  */
4249       ch = *p;
4250       opt = N_OPTS;
4251       for (i = 0; i < ARRAY_SIZE (attrs); i++)
4252         {
4253           type = attrs[i].type;
4254           opt_len = attrs[i].len;
4255           if (ch == attrs[i].string[0]
4256               && ((type != ix86_opt_str && type != ix86_opt_enum)
4257                   ? len == opt_len
4258                   : len > opt_len)
4259               && memcmp (p, attrs[i].string, opt_len) == 0)
4260             {
4261               opt = attrs[i].opt;
4262               mask = attrs[i].mask;
4263               opt_string = attrs[i].string;
4264               break;
4265             }
4266         }
4267
4268       /* Process the option.  */
4269       if (opt == N_OPTS)
4270         {
4271           error ("attribute(target(\"%s\")) is unknown", orig_p);
4272           ret = false;
4273         }
4274
4275       else if (type == ix86_opt_isa)
4276         {
4277           struct cl_decoded_option decoded;
4278
4279           generate_option (opt, NULL, opt_set_p, CL_TARGET, &decoded);
4280           ix86_handle_option (&global_options, &global_options_set,
4281                               &decoded, input_location);
4282         }
4283
4284       else if (type == ix86_opt_yes || type == ix86_opt_no)
4285         {
4286           if (type == ix86_opt_no)
4287             opt_set_p = !opt_set_p;
4288
4289           if (opt_set_p)
4290             target_flags |= mask;
4291           else
4292             target_flags &= ~mask;
4293         }
4294
4295       else if (type == ix86_opt_str)
4296         {
4297           if (p_strings[opt])
4298             {
4299               error ("option(\"%s\") was already specified", opt_string);
4300               ret = false;
4301             }
4302           else
4303             p_strings[opt] = xstrdup (p + opt_len);
4304         }
4305
4306       else if (type == ix86_opt_enum)
4307         {
4308           bool arg_ok;
4309           int value;
4310
4311           arg_ok = opt_enum_arg_to_value (opt, p + opt_len, &value, CL_TARGET);
4312           if (arg_ok)
4313             set_option (&global_options, enum_opts_set, opt, value,
4314                         p + opt_len, DK_UNSPECIFIED, input_location,
4315                         global_dc);
4316           else
4317             {
4318               error ("attribute(target(\"%s\")) is unknown", orig_p);
4319               ret = false;
4320             }
4321         }
4322
4323       else
4324         gcc_unreachable ();
4325     }
4326
4327   return ret;
4328 }
4329
4330 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
4331
4332 tree
4333 ix86_valid_target_attribute_tree (tree args)
4334 {
4335   const char *orig_arch_string = ix86_arch_string;
4336   const char *orig_tune_string = ix86_tune_string;
4337   enum fpmath_unit orig_fpmath_set = global_options_set.x_ix86_fpmath;
4338   int orig_tune_defaulted = ix86_tune_defaulted;
4339   int orig_arch_specified = ix86_arch_specified;
4340   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL };
4341   tree t = NULL_TREE;
4342   int i;
4343   struct cl_target_option *def
4344     = TREE_TARGET_OPTION (target_option_default_node);
4345   struct gcc_options enum_opts_set;
4346
4347   memset (&enum_opts_set, 0, sizeof (enum_opts_set));
4348
4349   /* Process each of the options on the chain.  */
4350   if (! ix86_valid_target_attribute_inner_p (args, option_strings,
4351                                              &enum_opts_set))
4352     return NULL_TREE;
4353
4354   /* If the changed options are different from the default, rerun
4355      ix86_option_override_internal, and then save the options away.
4356      The string options are are attribute options, and will be undone
4357      when we copy the save structure.  */
4358   if (ix86_isa_flags != def->x_ix86_isa_flags
4359       || target_flags != def->x_target_flags
4360       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
4361       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
4362       || enum_opts_set.x_ix86_fpmath)
4363     {
4364       /* If we are using the default tune= or arch=, undo the string assigned,
4365          and use the default.  */
4366       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
4367         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
4368       else if (!orig_arch_specified)
4369         ix86_arch_string = NULL;
4370
4371       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
4372         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
4373       else if (orig_tune_defaulted)
4374         ix86_tune_string = NULL;
4375
4376       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
4377       if (enum_opts_set.x_ix86_fpmath)
4378         global_options_set.x_ix86_fpmath = (enum fpmath_unit) 1;
4379       else if (!TARGET_64BIT && TARGET_SSE)
4380         {
4381           ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
4382           global_options_set.x_ix86_fpmath = (enum fpmath_unit) 1;
4383         }
4384
4385       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
4386       ix86_option_override_internal (false);
4387
4388       /* Add any builtin functions with the new isa if any.  */
4389       ix86_add_new_builtins (ix86_isa_flags);
4390
4391       /* Save the current options unless we are validating options for
4392          #pragma.  */
4393       t = build_target_option_node ();
4394
4395       ix86_arch_string = orig_arch_string;
4396       ix86_tune_string = orig_tune_string;
4397       global_options_set.x_ix86_fpmath = orig_fpmath_set;
4398
4399       /* Free up memory allocated to hold the strings */
4400       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
4401         free (option_strings[i]);
4402     }
4403
4404   return t;
4405 }
4406
4407 /* Hook to validate attribute((target("string"))).  */
4408
4409 static bool
4410 ix86_valid_target_attribute_p (tree fndecl,
4411                                tree ARG_UNUSED (name),
4412                                tree args,
4413                                int ARG_UNUSED (flags))
4414 {
4415   struct cl_target_option cur_target;
4416   bool ret = true;
4417   tree old_optimize = build_optimization_node ();
4418   tree new_target, new_optimize;
4419   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
4420
4421   /* If the function changed the optimization levels as well as setting target
4422      options, start with the optimizations specified.  */
4423   if (func_optimize && func_optimize != old_optimize)
4424     cl_optimization_restore (&global_options,
4425                              TREE_OPTIMIZATION (func_optimize));
4426
4427   /* The target attributes may also change some optimization flags, so update
4428      the optimization options if necessary.  */
4429   cl_target_option_save (&cur_target, &global_options);
4430   new_target = ix86_valid_target_attribute_tree (args);
4431   new_optimize = build_optimization_node ();
4432
4433   if (!new_target)
4434     ret = false;
4435
4436   else if (fndecl)
4437     {
4438       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
4439
4440       if (old_optimize != new_optimize)
4441         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
4442     }
4443
4444   cl_target_option_restore (&global_options, &cur_target);
4445
4446   if (old_optimize != new_optimize)
4447     cl_optimization_restore (&global_options,
4448                              TREE_OPTIMIZATION (old_optimize));
4449
4450   return ret;
4451 }
4452
4453 \f
4454 /* Hook to determine if one function can safely inline another.  */
4455
4456 static bool
4457 ix86_can_inline_p (tree caller, tree callee)
4458 {
4459   bool ret = false;
4460   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
4461   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
4462
4463   /* If callee has no option attributes, then it is ok to inline.  */
4464   if (!callee_tree)
4465     ret = true;
4466
4467   /* If caller has no option attributes, but callee does then it is not ok to
4468      inline.  */
4469   else if (!caller_tree)
4470     ret = false;
4471
4472   else
4473     {
4474       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
4475       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
4476
4477       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
4478          can inline a SSE2 function but a SSE2 function can't inline a SSE4
4479          function.  */
4480       if ((caller_opts->x_ix86_isa_flags & callee_opts->x_ix86_isa_flags)
4481           != callee_opts->x_ix86_isa_flags)
4482         ret = false;
4483
4484       /* See if we have the same non-isa options.  */
4485       else if (caller_opts->x_target_flags != callee_opts->x_target_flags)
4486         ret = false;
4487
4488       /* See if arch, tune, etc. are the same.  */
4489       else if (caller_opts->arch != callee_opts->arch)
4490         ret = false;
4491
4492       else if (caller_opts->tune != callee_opts->tune)
4493         ret = false;
4494
4495       else if (caller_opts->x_ix86_fpmath != callee_opts->x_ix86_fpmath)
4496         ret = false;
4497
4498       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4499         ret = false;
4500
4501       else
4502         ret = true;
4503     }
4504
4505   return ret;
4506 }
4507
4508 \f
4509 /* Remember the last target of ix86_set_current_function.  */
4510 static GTY(()) tree ix86_previous_fndecl;
4511
4512 /* Establish appropriate back-end context for processing the function
4513    FNDECL.  The argument might be NULL to indicate processing at top
4514    level, outside of any function scope.  */
4515 static void
4516 ix86_set_current_function (tree fndecl)
4517 {
4518   /* Only change the context if the function changes.  This hook is called
4519      several times in the course of compiling a function, and we don't want to
4520      slow things down too much or call target_reinit when it isn't safe.  */
4521   if (fndecl && fndecl != ix86_previous_fndecl)
4522     {
4523       tree old_tree = (ix86_previous_fndecl
4524                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4525                        : NULL_TREE);
4526
4527       tree new_tree = (fndecl
4528                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4529                        : NULL_TREE);
4530
4531       ix86_previous_fndecl = fndecl;
4532       if (old_tree == new_tree)
4533         ;
4534
4535       else if (new_tree)
4536         {
4537           cl_target_option_restore (&global_options,
4538                                     TREE_TARGET_OPTION (new_tree));
4539           target_reinit ();
4540         }
4541
4542       else if (old_tree)
4543         {
4544           struct cl_target_option *def
4545             = TREE_TARGET_OPTION (target_option_current_node);
4546
4547           cl_target_option_restore (&global_options, def);
4548           target_reinit ();
4549         }
4550     }
4551 }
4552
4553 \f
4554 /* Return true if this goes in large data/bss.  */
4555
4556 static bool
4557 ix86_in_large_data_p (tree exp)
4558 {
4559   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4560     return false;
4561
4562   /* Functions are never large data.  */
4563   if (TREE_CODE (exp) == FUNCTION_DECL)
4564     return false;
4565
4566   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4567     {
4568       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4569       if (strcmp (section, ".ldata") == 0
4570           || strcmp (section, ".lbss") == 0)
4571         return true;
4572       return false;
4573     }
4574   else
4575     {
4576       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4577
4578       /* If this is an incomplete type with size 0, then we can't put it
4579          in data because it might be too big when completed.  */
4580       if (!size || size > ix86_section_threshold)
4581         return true;
4582     }
4583
4584   return false;
4585 }
4586
4587 /* Switch to the appropriate section for output of DECL.
4588    DECL is either a `VAR_DECL' node or a constant of some sort.
4589    RELOC indicates whether forming the initial value of DECL requires
4590    link-time relocations.  */
4591
4592 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4593         ATTRIBUTE_UNUSED;
4594
4595 static section *
4596 x86_64_elf_select_section (tree decl, int reloc,
4597                            unsigned HOST_WIDE_INT align)
4598 {
4599   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4600       && ix86_in_large_data_p (decl))
4601     {
4602       const char *sname = NULL;
4603       unsigned int flags = SECTION_WRITE;
4604       switch (categorize_decl_for_section (decl, reloc))
4605         {
4606         case SECCAT_DATA:
4607           sname = ".ldata";
4608           break;
4609         case SECCAT_DATA_REL:
4610           sname = ".ldata.rel";
4611           break;
4612         case SECCAT_DATA_REL_LOCAL:
4613           sname = ".ldata.rel.local";
4614           break;
4615         case SECCAT_DATA_REL_RO:
4616           sname = ".ldata.rel.ro";
4617           break;
4618         case SECCAT_DATA_REL_RO_LOCAL:
4619           sname = ".ldata.rel.ro.local";
4620           break;
4621         case SECCAT_BSS:
4622           sname = ".lbss";
4623           flags |= SECTION_BSS;
4624           break;
4625         case SECCAT_RODATA:
4626         case SECCAT_RODATA_MERGE_STR:
4627         case SECCAT_RODATA_MERGE_STR_INIT:
4628         case SECCAT_RODATA_MERGE_CONST:
4629           sname = ".lrodata";
4630           flags = 0;
4631           break;
4632         case SECCAT_SRODATA:
4633         case SECCAT_SDATA:
4634         case SECCAT_SBSS:
4635           gcc_unreachable ();
4636         case SECCAT_TEXT:
4637         case SECCAT_TDATA:
4638         case SECCAT_TBSS:
4639           /* We don't split these for medium model.  Place them into
4640              default sections and hope for best.  */
4641           break;
4642         }
4643       if (sname)
4644         {
4645           /* We might get called with string constants, but get_named_section
4646              doesn't like them as they are not DECLs.  Also, we need to set
4647              flags in that case.  */
4648           if (!DECL_P (decl))
4649             return get_section (sname, flags, NULL);
4650           return get_named_section (decl, sname, reloc);
4651         }
4652     }
4653   return default_elf_select_section (decl, reloc, align);
4654 }
4655
4656 /* Build up a unique section name, expressed as a
4657    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4658    RELOC indicates whether the initial value of EXP requires
4659    link-time relocations.  */
4660
4661 static void ATTRIBUTE_UNUSED
4662 x86_64_elf_unique_section (tree decl, int reloc)
4663 {
4664   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4665       && ix86_in_large_data_p (decl))
4666     {
4667       const char *prefix = NULL;
4668       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4669       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4670
4671       switch (categorize_decl_for_section (decl, reloc))
4672         {
4673         case SECCAT_DATA:
4674         case SECCAT_DATA_REL:
4675         case SECCAT_DATA_REL_LOCAL:
4676         case SECCAT_DATA_REL_RO:
4677         case SECCAT_DATA_REL_RO_LOCAL:
4678           prefix = one_only ? ".ld" : ".ldata";
4679           break;
4680         case SECCAT_BSS:
4681           prefix = one_only ? ".lb" : ".lbss";
4682           break;
4683         case SECCAT_RODATA:
4684         case SECCAT_RODATA_MERGE_STR:
4685         case SECCAT_RODATA_MERGE_STR_INIT:
4686         case SECCAT_RODATA_MERGE_CONST:
4687           prefix = one_only ? ".lr" : ".lrodata";
4688           break;
4689         case SECCAT_SRODATA:
4690         case SECCAT_SDATA:
4691         case SECCAT_SBSS:
4692           gcc_unreachable ();
4693         case SECCAT_TEXT:
4694         case SECCAT_TDATA:
4695         case SECCAT_TBSS:
4696           /* We don't split these for medium model.  Place them into
4697              default sections and hope for best.  */
4698           break;
4699         }
4700       if (prefix)
4701         {
4702           const char *name, *linkonce;
4703           char *string;
4704
4705           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4706           name = targetm.strip_name_encoding (name);
4707
4708           /* If we're using one_only, then there needs to be a .gnu.linkonce
4709              prefix to the section name.  */
4710           linkonce = one_only ? ".gnu.linkonce" : "";
4711
4712           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4713
4714           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4715           return;
4716         }
4717     }
4718   default_unique_section (decl, reloc);
4719 }
4720
4721 #ifdef COMMON_ASM_OP
4722 /* This says how to output assembler code to declare an
4723    uninitialized external linkage data object.
4724
4725    For medium model x86-64 we need to use .largecomm opcode for
4726    large objects.  */
4727 void
4728 x86_elf_aligned_common (FILE *file,
4729                         const char *name, unsigned HOST_WIDE_INT size,
4730                         int align)
4731 {
4732   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4733       && size > (unsigned int)ix86_section_threshold)
4734     fputs (".largecomm\t", file);
4735   else
4736     fputs (COMMON_ASM_OP, file);
4737   assemble_name (file, name);
4738   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
4739            size, align / BITS_PER_UNIT);
4740 }
4741 #endif
4742
4743 /* Utility function for targets to use in implementing
4744    ASM_OUTPUT_ALIGNED_BSS.  */
4745
4746 void
4747 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
4748                         const char *name, unsigned HOST_WIDE_INT size,
4749                         int align)
4750 {
4751   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4752       && size > (unsigned int)ix86_section_threshold)
4753     switch_to_section (get_named_section (decl, ".lbss", 0));
4754   else
4755     switch_to_section (bss_section);
4756   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
4757 #ifdef ASM_DECLARE_OBJECT_NAME
4758   last_assemble_variable_decl = decl;
4759   ASM_DECLARE_OBJECT_NAME (file, name, decl);
4760 #else
4761   /* Standard thing is just output label for the object.  */
4762   ASM_OUTPUT_LABEL (file, name);
4763 #endif /* ASM_DECLARE_OBJECT_NAME */
4764   ASM_OUTPUT_SKIP (file, size ? size : 1);
4765 }
4766 \f
4767 /* Decide whether we must probe the stack before any space allocation
4768    on this target.  It's essentially TARGET_STACK_PROBE except when
4769    -fstack-check causes the stack to be already probed differently.  */
4770
4771 bool
4772 ix86_target_stack_probe (void)
4773 {
4774   /* Do not probe the stack twice if static stack checking is enabled.  */
4775   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
4776     return false;
4777
4778   return TARGET_STACK_PROBE;
4779 }
4780 \f
4781 /* Decide whether we can make a sibling call to a function.  DECL is the
4782    declaration of the function being targeted by the call and EXP is the
4783    CALL_EXPR representing the call.  */
4784
4785 static bool
4786 ix86_function_ok_for_sibcall (tree decl, tree exp)
4787 {
4788   tree type, decl_or_type;
4789   rtx a, b;
4790
4791   /* If we are generating position-independent code, we cannot sibcall
4792      optimize any indirect call, or a direct call to a global function,
4793      as the PLT requires %ebx be live. (Darwin does not have a PLT.)  */
4794   if (!TARGET_MACHO
4795       && !TARGET_64BIT
4796       && flag_pic
4797       && (!decl || !targetm.binds_local_p (decl)))
4798     return false;
4799
4800   /* If we need to align the outgoing stack, then sibcalling would
4801      unalign the stack, which may break the called function.  */
4802   if (ix86_minimum_incoming_stack_boundary (true)
4803       < PREFERRED_STACK_BOUNDARY)
4804     return false;
4805
4806   if (decl)
4807     {
4808       decl_or_type = decl;
4809       type = TREE_TYPE (decl);
4810     }
4811   else
4812     {
4813       /* We're looking at the CALL_EXPR, we need the type of the function.  */
4814       type = CALL_EXPR_FN (exp);                /* pointer expression */
4815       type = TREE_TYPE (type);                  /* pointer type */
4816       type = TREE_TYPE (type);                  /* function type */
4817       decl_or_type = type;
4818     }
4819
4820   /* Check that the return value locations are the same.  Like
4821      if we are returning floats on the 80387 register stack, we cannot
4822      make a sibcall from a function that doesn't return a float to a
4823      function that does or, conversely, from a function that does return
4824      a float to a function that doesn't; the necessary stack adjustment
4825      would not be executed.  This is also the place we notice
4826      differences in the return value ABI.  Note that it is ok for one
4827      of the functions to have void return type as long as the return
4828      value of the other is passed in a register.  */
4829   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
4830   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4831                            cfun->decl, false);
4832   if (STACK_REG_P (a) || STACK_REG_P (b))
4833     {
4834       if (!rtx_equal_p (a, b))
4835         return false;
4836     }
4837   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4838     {
4839       /* Disable sibcall if we need to generate vzeroupper after
4840          callee returns.  */
4841       if (TARGET_VZEROUPPER
4842           && cfun->machine->callee_return_avx256_p
4843           && !cfun->machine->caller_return_avx256_p)
4844         return false;
4845     }
4846   else if (!rtx_equal_p (a, b))
4847     return false;
4848
4849   if (TARGET_64BIT)
4850     {
4851       /* The SYSV ABI has more call-clobbered registers;
4852          disallow sibcalls from MS to SYSV.  */
4853       if (cfun->machine->call_abi == MS_ABI
4854           && ix86_function_type_abi (type) == SYSV_ABI)
4855         return false;
4856     }
4857   else
4858     {
4859       /* If this call is indirect, we'll need to be able to use a
4860          call-clobbered register for the address of the target function.
4861          Make sure that all such registers are not used for passing
4862          parameters.  Note that DLLIMPORT functions are indirect.  */
4863       if (!decl
4864           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
4865         {
4866           if (ix86_function_regparm (type, NULL) >= 3)
4867             {
4868               /* ??? Need to count the actual number of registers to be used,
4869                  not the possible number of registers.  Fix later.  */
4870               return false;
4871             }
4872         }
4873     }
4874
4875   /* Otherwise okay.  That also includes certain types of indirect calls.  */
4876   return true;
4877 }
4878
4879 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
4880    and "sseregparm" calling convention attributes;
4881    arguments as in struct attribute_spec.handler.  */
4882
4883 static tree
4884 ix86_handle_cconv_attribute (tree *node, tree name,
4885                                    tree args,
4886                                    int flags ATTRIBUTE_UNUSED,
4887                                    bool *no_add_attrs)
4888 {
4889   if (TREE_CODE (*node) != FUNCTION_TYPE
4890       && TREE_CODE (*node) != METHOD_TYPE
4891       && TREE_CODE (*node) != FIELD_DECL
4892       && TREE_CODE (*node) != TYPE_DECL)
4893     {
4894       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4895                name);
4896       *no_add_attrs = true;
4897       return NULL_TREE;
4898     }
4899
4900   /* Can combine regparm with all attributes but fastcall, and thiscall.  */
4901   if (is_attribute_p ("regparm", name))
4902     {
4903       tree cst;
4904
4905       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4906         {
4907           error ("fastcall and regparm attributes are not compatible");
4908         }
4909
4910       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4911         {
4912           error ("regparam and thiscall attributes are not compatible");
4913         }
4914
4915       cst = TREE_VALUE (args);
4916       if (TREE_CODE (cst) != INTEGER_CST)
4917         {
4918           warning (OPT_Wattributes,
4919                    "%qE attribute requires an integer constant argument",
4920                    name);
4921           *no_add_attrs = true;
4922         }
4923       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
4924         {
4925           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
4926                    name, REGPARM_MAX);
4927           *no_add_attrs = true;
4928         }
4929
4930       return NULL_TREE;
4931     }
4932
4933   if (TARGET_64BIT)
4934     {
4935       /* Do not warn when emulating the MS ABI.  */
4936       if ((TREE_CODE (*node) != FUNCTION_TYPE
4937            && TREE_CODE (*node) != METHOD_TYPE)
4938           || ix86_function_type_abi (*node) != MS_ABI)
4939         warning (OPT_Wattributes, "%qE attribute ignored",
4940                  name);
4941       *no_add_attrs = true;
4942       return NULL_TREE;
4943     }
4944
4945   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
4946   if (is_attribute_p ("fastcall", name))
4947     {
4948       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4949         {
4950           error ("fastcall and cdecl attributes are not compatible");
4951         }
4952       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4953         {
4954           error ("fastcall and stdcall attributes are not compatible");
4955         }
4956       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
4957         {
4958           error ("fastcall and regparm attributes are not compatible");
4959         }
4960       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4961         {
4962           error ("fastcall and thiscall attributes are not compatible");
4963         }
4964     }
4965
4966   /* Can combine stdcall with fastcall (redundant), regparm and
4967      sseregparm.  */
4968   else if (is_attribute_p ("stdcall", name))
4969     {
4970       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4971         {
4972           error ("stdcall and cdecl attributes are not compatible");
4973         }
4974       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4975         {
4976           error ("stdcall and fastcall attributes are not compatible");
4977         }
4978       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4979         {
4980           error ("stdcall and thiscall attributes are not compatible");
4981         }
4982     }
4983
4984   /* Can combine cdecl with regparm and sseregparm.  */
4985   else if (is_attribute_p ("cdecl", name))
4986     {
4987       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4988         {
4989           error ("stdcall and cdecl attributes are not compatible");
4990         }
4991       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4992         {
4993           error ("fastcall and cdecl attributes are not compatible");
4994         }
4995       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4996         {
4997           error ("cdecl and thiscall attributes are not compatible");
4998         }
4999     }
5000   else if (is_attribute_p ("thiscall", name))
5001     {
5002       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
5003         warning (OPT_Wattributes, "%qE attribute is used for none class-method",
5004                  name);
5005       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5006         {
5007           error ("stdcall and thiscall attributes are not compatible");
5008         }
5009       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5010         {
5011           error ("fastcall and thiscall attributes are not compatible");
5012         }
5013       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5014         {
5015           error ("cdecl and thiscall attributes are not compatible");
5016         }
5017     }
5018
5019   /* Can combine sseregparm with all attributes.  */
5020
5021   return NULL_TREE;
5022 }
5023
5024 /* The transactional memory builtins are implicitly regparm or fastcall
5025    depending on the ABI.  Override the generic do-nothing attribute that
5026    these builtins were declared with, and replace it with one of the two
5027    attributes that we expect elsewhere.  */
5028
5029 static tree
5030 ix86_handle_tm_regparm_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
5031                                   tree args ATTRIBUTE_UNUSED,
5032                                   int flags ATTRIBUTE_UNUSED,
5033                                   bool *no_add_attrs)
5034 {
5035   tree alt;
5036
5037   /* In no case do we want to add the placeholder attribute.  */
5038   *no_add_attrs = true;
5039
5040   /* The 64-bit ABI is unchanged for transactional memory.  */
5041   if (TARGET_64BIT)
5042     return NULL_TREE;
5043
5044   /* ??? Is there a better way to validate 32-bit windows?  We have
5045      cfun->machine->call_abi, but that seems to be set only for 64-bit.  */
5046   if (CHECK_STACK_LIMIT > 0)
5047     alt = tree_cons (get_identifier ("fastcall"), NULL, NULL);
5048   else
5049     {
5050       alt = tree_cons (NULL, build_int_cst (NULL, 2), NULL);
5051       alt = tree_cons (get_identifier ("regparm"), alt, NULL);
5052     }
5053   decl_attributes (node, alt, flags);
5054
5055   return NULL_TREE;
5056 }
5057
5058 /* This function determines from TYPE the calling-convention.  */
5059
5060 unsigned int
5061 ix86_get_callcvt (const_tree type)
5062 {
5063   unsigned int ret = 0;
5064   bool is_stdarg;
5065   tree attrs;
5066
5067   if (TARGET_64BIT)
5068     return IX86_CALLCVT_CDECL;
5069
5070   attrs = TYPE_ATTRIBUTES (type);
5071   if (attrs != NULL_TREE)
5072     {
5073       if (lookup_attribute ("cdecl", attrs))
5074         ret |= IX86_CALLCVT_CDECL;
5075       else if (lookup_attribute ("stdcall", attrs))
5076         ret |= IX86_CALLCVT_STDCALL;
5077       else if (lookup_attribute ("fastcall", attrs))
5078         ret |= IX86_CALLCVT_FASTCALL;
5079       else if (lookup_attribute ("thiscall", attrs))
5080         ret |= IX86_CALLCVT_THISCALL;
5081
5082       /* Regparam isn't allowed for thiscall and fastcall.  */
5083       if ((ret & (IX86_CALLCVT_THISCALL | IX86_CALLCVT_FASTCALL)) == 0)
5084         {
5085           if (lookup_attribute ("regparm", attrs))
5086             ret |= IX86_CALLCVT_REGPARM;
5087           if (lookup_attribute ("sseregparm", attrs))
5088             ret |= IX86_CALLCVT_SSEREGPARM;
5089         }
5090
5091       if (IX86_BASE_CALLCVT(ret) != 0)
5092         return ret;
5093     }
5094
5095   is_stdarg = stdarg_p (type);
5096   if (TARGET_RTD && !is_stdarg)
5097     return IX86_CALLCVT_STDCALL | ret;
5098
5099   if (ret != 0
5100       || is_stdarg
5101       || TREE_CODE (type) != METHOD_TYPE
5102       || ix86_function_type_abi (type) != MS_ABI)
5103     return IX86_CALLCVT_CDECL | ret;
5104
5105   return IX86_CALLCVT_THISCALL;
5106 }
5107
5108 /* Return 0 if the attributes for two types are incompatible, 1 if they
5109    are compatible, and 2 if they are nearly compatible (which causes a
5110    warning to be generated).  */
5111
5112 static int
5113 ix86_comp_type_attributes (const_tree type1, const_tree type2)
5114 {
5115   unsigned int ccvt1, ccvt2;
5116
5117   if (TREE_CODE (type1) != FUNCTION_TYPE
5118       && TREE_CODE (type1) != METHOD_TYPE)
5119     return 1;
5120
5121   ccvt1 = ix86_get_callcvt (type1);
5122   ccvt2 = ix86_get_callcvt (type2);
5123   if (ccvt1 != ccvt2)
5124     return 0;
5125   if (ix86_function_regparm (type1, NULL)
5126       != ix86_function_regparm (type2, NULL))
5127     return 0;
5128
5129   return 1;
5130 }
5131 \f
5132 /* Return the regparm value for a function with the indicated TYPE and DECL.
5133    DECL may be NULL when calling function indirectly
5134    or considering a libcall.  */
5135
5136 static int
5137 ix86_function_regparm (const_tree type, const_tree decl)
5138 {
5139   tree attr;
5140   int regparm;
5141   unsigned int ccvt;
5142
5143   if (TARGET_64BIT)
5144     return (ix86_function_type_abi (type) == SYSV_ABI
5145             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
5146   ccvt = ix86_get_callcvt (type);
5147   regparm = ix86_regparm;
5148
5149   if ((ccvt & IX86_CALLCVT_REGPARM) != 0)
5150     {
5151       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
5152       if (attr)
5153         {
5154           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
5155           return regparm;
5156         }
5157     }
5158   else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
5159     return 2;
5160   else if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
5161     return 1;
5162
5163   /* Use register calling convention for local functions when possible.  */
5164   if (decl
5165       && TREE_CODE (decl) == FUNCTION_DECL
5166       && optimize
5167       && !(profile_flag && !flag_fentry))
5168     {
5169       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5170       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
5171       if (i && i->local && i->can_change_signature)
5172         {
5173           int local_regparm, globals = 0, regno;
5174
5175           /* Make sure no regparm register is taken by a
5176              fixed register variable.  */
5177           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
5178             if (fixed_regs[local_regparm])
5179               break;
5180
5181           /* We don't want to use regparm(3) for nested functions as
5182              these use a static chain pointer in the third argument.  */
5183           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
5184             local_regparm = 2;
5185
5186           /* In 32-bit mode save a register for the split stack.  */
5187           if (!TARGET_64BIT && local_regparm == 3 && flag_split_stack)
5188             local_regparm = 2;
5189
5190           /* Each fixed register usage increases register pressure,
5191              so less registers should be used for argument passing.
5192              This functionality can be overriden by an explicit
5193              regparm value.  */
5194           for (regno = 0; regno <= DI_REG; regno++)
5195             if (fixed_regs[regno])
5196               globals++;
5197
5198           local_regparm
5199             = globals < local_regparm ? local_regparm - globals : 0;
5200
5201           if (local_regparm > regparm)
5202             regparm = local_regparm;
5203         }
5204     }
5205
5206   return regparm;
5207 }
5208
5209 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
5210    DFmode (2) arguments in SSE registers for a function with the
5211    indicated TYPE and DECL.  DECL may be NULL when calling function
5212    indirectly or considering a libcall.  Otherwise return 0.  */
5213
5214 static int
5215 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
5216 {
5217   gcc_assert (!TARGET_64BIT);
5218
5219   /* Use SSE registers to pass SFmode and DFmode arguments if requested
5220      by the sseregparm attribute.  */
5221   if (TARGET_SSEREGPARM
5222       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
5223     {
5224       if (!TARGET_SSE)
5225         {
5226           if (warn)
5227             {
5228               if (decl)
5229                 error ("calling %qD with attribute sseregparm without "
5230                        "SSE/SSE2 enabled", decl);
5231               else
5232                 error ("calling %qT with attribute sseregparm without "
5233                        "SSE/SSE2 enabled", type);
5234             }
5235           return 0;
5236         }
5237
5238       return 2;
5239     }
5240
5241   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
5242      (and DFmode for SSE2) arguments in SSE registers.  */
5243   if (decl && TARGET_SSE_MATH && optimize
5244       && !(profile_flag && !flag_fentry))
5245     {
5246       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5247       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
5248       if (i && i->local && i->can_change_signature)
5249         return TARGET_SSE2 ? 2 : 1;
5250     }
5251
5252   return 0;
5253 }
5254
5255 /* Return true if EAX is live at the start of the function.  Used by
5256    ix86_expand_prologue to determine if we need special help before
5257    calling allocate_stack_worker.  */
5258
5259 static bool
5260 ix86_eax_live_at_start_p (void)
5261 {
5262   /* Cheat.  Don't bother working forward from ix86_function_regparm
5263      to the function type to whether an actual argument is located in
5264      eax.  Instead just look at cfg info, which is still close enough
5265      to correct at this point.  This gives false positives for broken
5266      functions that might use uninitialized data that happens to be
5267      allocated in eax, but who cares?  */
5268   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
5269 }
5270
5271 static bool
5272 ix86_keep_aggregate_return_pointer (tree fntype)
5273 {
5274   tree attr;
5275
5276   if (!TARGET_64BIT)
5277     {
5278       attr = lookup_attribute ("callee_pop_aggregate_return",
5279                                TYPE_ATTRIBUTES (fntype));
5280       if (attr)
5281         return (TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr))) == 0);
5282
5283       /* For 32-bit MS-ABI the default is to keep aggregate
5284          return pointer.  */
5285       if (ix86_function_type_abi (fntype) == MS_ABI)
5286         return true;
5287     }
5288   return KEEP_AGGREGATE_RETURN_POINTER != 0;
5289 }
5290
5291 /* Value is the number of bytes of arguments automatically
5292    popped when returning from a subroutine call.
5293    FUNDECL is the declaration node of the function (as a tree),
5294    FUNTYPE is the data type of the function (as a tree),
5295    or for a library call it is an identifier node for the subroutine name.
5296    SIZE is the number of bytes of arguments passed on the stack.
5297
5298    On the 80386, the RTD insn may be used to pop them if the number
5299      of args is fixed, but if the number is variable then the caller
5300      must pop them all.  RTD can't be used for library calls now
5301      because the library is compiled with the Unix compiler.
5302    Use of RTD is a selectable option, since it is incompatible with
5303    standard Unix calling sequences.  If the option is not selected,
5304    the caller must always pop the args.
5305
5306    The attribute stdcall is equivalent to RTD on a per module basis.  */
5307
5308 static int
5309 ix86_return_pops_args (tree fundecl, tree funtype, int size)
5310 {
5311   unsigned int ccvt;
5312
5313   /* None of the 64-bit ABIs pop arguments.  */
5314   if (TARGET_64BIT)
5315     return 0;
5316
5317   ccvt = ix86_get_callcvt (funtype);
5318
5319   if ((ccvt & (IX86_CALLCVT_STDCALL | IX86_CALLCVT_FASTCALL
5320                | IX86_CALLCVT_THISCALL)) != 0
5321       && ! stdarg_p (funtype))
5322     return size;
5323
5324   /* Lose any fake structure return argument if it is passed on the stack.  */
5325   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
5326       && !ix86_keep_aggregate_return_pointer (funtype))
5327     {
5328       int nregs = ix86_function_regparm (funtype, fundecl);
5329       if (nregs == 0)
5330         return GET_MODE_SIZE (Pmode);
5331     }
5332
5333   return 0;
5334 }
5335 \f
5336 /* Argument support functions.  */
5337
5338 /* Return true when register may be used to pass function parameters.  */
5339 bool
5340 ix86_function_arg_regno_p (int regno)
5341 {
5342   int i;
5343   const int *parm_regs;
5344
5345   if (!TARGET_64BIT)
5346     {
5347       if (TARGET_MACHO)
5348         return (regno < REGPARM_MAX
5349                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
5350       else
5351         return (regno < REGPARM_MAX
5352                 || (TARGET_MMX && MMX_REGNO_P (regno)
5353                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
5354                 || (TARGET_SSE && SSE_REGNO_P (regno)
5355                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
5356     }
5357
5358   if (TARGET_MACHO)
5359     {
5360       if (SSE_REGNO_P (regno) && TARGET_SSE)
5361         return true;
5362     }
5363   else
5364     {
5365       if (TARGET_SSE && SSE_REGNO_P (regno)
5366           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
5367         return true;
5368     }
5369
5370   /* TODO: The function should depend on current function ABI but
5371      builtins.c would need updating then. Therefore we use the
5372      default ABI.  */
5373
5374   /* RAX is used as hidden argument to va_arg functions.  */
5375   if (ix86_abi == SYSV_ABI && regno == AX_REG)
5376     return true;
5377
5378   if (ix86_abi == MS_ABI)
5379     parm_regs = x86_64_ms_abi_int_parameter_registers;
5380   else
5381     parm_regs = x86_64_int_parameter_registers;
5382   for (i = 0; i < (ix86_abi == MS_ABI
5383                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
5384     if (regno == parm_regs[i])
5385       return true;
5386   return false;
5387 }
5388
5389 /* Return if we do not know how to pass TYPE solely in registers.  */
5390
5391 static bool
5392 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
5393 {
5394   if (must_pass_in_stack_var_size_or_pad (mode, type))
5395     return true;
5396
5397   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
5398      The layout_type routine is crafty and tries to trick us into passing
5399      currently unsupported vector types on the stack by using TImode.  */
5400   return (!TARGET_64BIT && mode == TImode
5401           && type && TREE_CODE (type) != VECTOR_TYPE);
5402 }
5403
5404 /* It returns the size, in bytes, of the area reserved for arguments passed
5405    in registers for the function represented by fndecl dependent to the used
5406    abi format.  */
5407 int
5408 ix86_reg_parm_stack_space (const_tree fndecl)
5409 {
5410   enum calling_abi call_abi = SYSV_ABI;
5411   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
5412     call_abi = ix86_function_abi (fndecl);
5413   else
5414     call_abi = ix86_function_type_abi (fndecl);
5415   if (TARGET_64BIT && call_abi == MS_ABI)
5416     return 32;
5417   return 0;
5418 }
5419
5420 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
5421    call abi used.  */
5422 enum calling_abi
5423 ix86_function_type_abi (const_tree fntype)
5424 {
5425   if (fntype != NULL_TREE && TYPE_ATTRIBUTES (fntype) != NULL_TREE)
5426     {
5427       enum calling_abi abi = ix86_abi;
5428       if (abi == SYSV_ABI)
5429         {
5430           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
5431             abi = MS_ABI;
5432         }
5433       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
5434         abi = SYSV_ABI;
5435       return abi;
5436     }
5437   return ix86_abi;
5438 }
5439
5440 static bool
5441 ix86_function_ms_hook_prologue (const_tree fn)
5442 {
5443   if (fn && lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fn)))
5444     {
5445       if (decl_function_context (fn) != NULL_TREE)
5446         error_at (DECL_SOURCE_LOCATION (fn),
5447                   "ms_hook_prologue is not compatible with nested function");
5448       else
5449         return true;
5450     }
5451   return false;
5452 }
5453
5454 static enum calling_abi
5455 ix86_function_abi (const_tree fndecl)
5456 {
5457   if (! fndecl)
5458     return ix86_abi;
5459   return ix86_function_type_abi (TREE_TYPE (fndecl));
5460 }
5461
5462 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
5463    call abi used.  */
5464 enum calling_abi
5465 ix86_cfun_abi (void)
5466 {
5467   if (! cfun)
5468     return ix86_abi;
5469   return cfun->machine->call_abi;
5470 }
5471
5472 /* Write the extra assembler code needed to declare a function properly.  */
5473
5474 void
5475 ix86_asm_output_function_label (FILE *asm_out_file, const char *fname,
5476                                 tree decl)
5477 {
5478   bool is_ms_hook = ix86_function_ms_hook_prologue (decl);
5479
5480   if (is_ms_hook)
5481     {
5482       int i, filler_count = (TARGET_64BIT ? 32 : 16);
5483       unsigned int filler_cc = 0xcccccccc;
5484
5485       for (i = 0; i < filler_count; i += 4)
5486         fprintf (asm_out_file, ASM_LONG " %#x\n", filler_cc);
5487     }
5488
5489 #ifdef SUBTARGET_ASM_UNWIND_INIT
5490   SUBTARGET_ASM_UNWIND_INIT (asm_out_file);
5491 #endif
5492
5493   ASM_OUTPUT_LABEL (asm_out_file, fname);
5494
5495   /* Output magic byte marker, if hot-patch attribute is set.  */
5496   if (is_ms_hook)
5497     {
5498       if (TARGET_64BIT)
5499         {
5500           /* leaq [%rsp + 0], %rsp  */
5501           asm_fprintf (asm_out_file, ASM_BYTE
5502                        "0x48, 0x8d, 0xa4, 0x24, 0x00, 0x00, 0x00, 0x00\n");
5503         }
5504       else
5505         {
5506           /* movl.s %edi, %edi
5507              push   %ebp
5508              movl.s %esp, %ebp */
5509           asm_fprintf (asm_out_file, ASM_BYTE
5510                        "0x8b, 0xff, 0x55, 0x8b, 0xec\n");
5511         }
5512     }
5513 }
5514
5515 /* regclass.c  */
5516 extern void init_regs (void);
5517
5518 /* Implementation of call abi switching target hook. Specific to FNDECL
5519    the specific call register sets are set.  See also
5520    ix86_conditional_register_usage for more details.  */
5521 void
5522 ix86_call_abi_override (const_tree fndecl)
5523 {
5524   if (fndecl == NULL_TREE)
5525     cfun->machine->call_abi = ix86_abi;
5526   else
5527     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
5528 }
5529
5530 /* 64-bit MS and SYSV ABI have different set of call used registers.  Avoid
5531    expensive re-initialization of init_regs each time we switch function context
5532    since this is needed only during RTL expansion.  */
5533 static void
5534 ix86_maybe_switch_abi (void)
5535 {
5536   if (TARGET_64BIT &&
5537       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
5538     reinit_regs ();
5539 }
5540
5541 /* Initialize a variable CUM of type CUMULATIVE_ARGS
5542    for a call to a function whose data type is FNTYPE.
5543    For a library call, FNTYPE is 0.  */
5544
5545 void
5546 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
5547                       tree fntype,      /* tree ptr for function decl */
5548                       rtx libname,      /* SYMBOL_REF of library name or 0 */
5549                       tree fndecl,
5550                       int caller)
5551 {
5552   struct cgraph_local_info *i;
5553   tree fnret_type;
5554
5555   memset (cum, 0, sizeof (*cum));
5556
5557   /* Initialize for the current callee.  */
5558   if (caller)
5559     {
5560       cfun->machine->callee_pass_avx256_p = false;
5561       cfun->machine->callee_return_avx256_p = false;
5562     }
5563
5564   if (fndecl)
5565     {
5566       i = cgraph_local_info (fndecl);
5567       cum->call_abi = ix86_function_abi (fndecl);
5568       fnret_type = TREE_TYPE (TREE_TYPE (fndecl));
5569     }
5570   else
5571     {
5572       i = NULL;
5573       cum->call_abi = ix86_function_type_abi (fntype);
5574       if (fntype)
5575         fnret_type = TREE_TYPE (fntype);
5576       else
5577         fnret_type = NULL;
5578     }
5579
5580   if (TARGET_VZEROUPPER && fnret_type)
5581     {
5582       rtx fnret_value = ix86_function_value (fnret_type, fntype,
5583                                              false);
5584       if (function_pass_avx256_p (fnret_value))
5585         {
5586           /* The return value of this function uses 256bit AVX modes.  */
5587           if (caller)
5588             cfun->machine->callee_return_avx256_p = true;
5589           else
5590             cfun->machine->caller_return_avx256_p = true;
5591         }
5592     }
5593
5594   cum->caller = caller;
5595
5596   /* Set up the number of registers to use for passing arguments.  */
5597
5598   if (TARGET_64BIT && cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
5599     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
5600            "or subtarget optimization implying it");
5601   cum->nregs = ix86_regparm;
5602   if (TARGET_64BIT)
5603     {
5604       cum->nregs = (cum->call_abi == SYSV_ABI
5605                    ? X86_64_REGPARM_MAX
5606                    : X86_64_MS_REGPARM_MAX);
5607     }
5608   if (TARGET_SSE)
5609     {
5610       cum->sse_nregs = SSE_REGPARM_MAX;
5611       if (TARGET_64BIT)
5612         {
5613           cum->sse_nregs = (cum->call_abi == SYSV_ABI
5614                            ? X86_64_SSE_REGPARM_MAX
5615                            : X86_64_MS_SSE_REGPARM_MAX);
5616         }
5617     }
5618   if (TARGET_MMX)
5619     cum->mmx_nregs = MMX_REGPARM_MAX;
5620   cum->warn_avx = true;
5621   cum->warn_sse = true;
5622   cum->warn_mmx = true;
5623
5624   /* Because type might mismatch in between caller and callee, we need to
5625      use actual type of function for local calls.
5626      FIXME: cgraph_analyze can be told to actually record if function uses
5627      va_start so for local functions maybe_vaarg can be made aggressive
5628      helping K&R code.
5629      FIXME: once typesytem is fixed, we won't need this code anymore.  */
5630   if (i && i->local && i->can_change_signature)
5631     fntype = TREE_TYPE (fndecl);
5632   cum->maybe_vaarg = (fntype
5633                       ? (!prototype_p (fntype) || stdarg_p (fntype))
5634                       : !libname);
5635
5636   if (!TARGET_64BIT)
5637     {
5638       /* If there are variable arguments, then we won't pass anything
5639          in registers in 32-bit mode. */
5640       if (stdarg_p (fntype))
5641         {
5642           cum->nregs = 0;
5643           cum->sse_nregs = 0;
5644           cum->mmx_nregs = 0;
5645           cum->warn_avx = 0;
5646           cum->warn_sse = 0;
5647           cum->warn_mmx = 0;
5648           return;
5649         }
5650
5651       /* Use ecx and edx registers if function has fastcall attribute,
5652          else look for regparm information.  */
5653       if (fntype)
5654         {
5655           unsigned int ccvt = ix86_get_callcvt (fntype);
5656           if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
5657             {
5658               cum->nregs = 1;
5659               cum->fastcall = 1; /* Same first register as in fastcall.  */
5660             }
5661           else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
5662             {
5663               cum->nregs = 2;
5664               cum->fastcall = 1;
5665             }
5666           else
5667             cum->nregs = ix86_function_regparm (fntype, fndecl);
5668         }
5669
5670       /* Set up the number of SSE registers used for passing SFmode
5671          and DFmode arguments.  Warn for mismatching ABI.  */
5672       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
5673     }
5674 }
5675
5676 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
5677    But in the case of vector types, it is some vector mode.
5678
5679    When we have only some of our vector isa extensions enabled, then there
5680    are some modes for which vector_mode_supported_p is false.  For these
5681    modes, the generic vector support in gcc will choose some non-vector mode
5682    in order to implement the type.  By computing the natural mode, we'll
5683    select the proper ABI location for the operand and not depend on whatever
5684    the middle-end decides to do with these vector types.
5685
5686    The midde-end can't deal with the vector types > 16 bytes.  In this
5687    case, we return the original mode and warn ABI change if CUM isn't
5688    NULL.  */
5689
5690 static enum machine_mode
5691 type_natural_mode (const_tree type, const CUMULATIVE_ARGS *cum)
5692 {
5693   enum machine_mode mode = TYPE_MODE (type);
5694
5695   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5696     {
5697       HOST_WIDE_INT size = int_size_in_bytes (type);
5698       if ((size == 8 || size == 16 || size == 32)
5699           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5700           && TYPE_VECTOR_SUBPARTS (type) > 1)
5701         {
5702           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5703
5704           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5705             mode = MIN_MODE_VECTOR_FLOAT;
5706           else
5707             mode = MIN_MODE_VECTOR_INT;
5708
5709           /* Get the mode which has this inner mode and number of units.  */
5710           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5711             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5712                 && GET_MODE_INNER (mode) == innermode)
5713               {
5714                 if (size == 32 && !TARGET_AVX)
5715                   {
5716                     static bool warnedavx;
5717
5718                     if (cum
5719                         && !warnedavx
5720                         && cum->warn_avx)
5721                       {
5722                         warnedavx = true;
5723                         warning (0, "AVX vector argument without AVX "
5724                                  "enabled changes the ABI");
5725                       }
5726                     return TYPE_MODE (type);
5727                   }
5728                 else
5729                   return mode;
5730               }
5731
5732           gcc_unreachable ();
5733         }
5734     }
5735
5736   return mode;
5737 }
5738
5739 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5740    this may not agree with the mode that the type system has chosen for the
5741    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5742    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5743
5744 static rtx
5745 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5746                      unsigned int regno)
5747 {
5748   rtx tmp;
5749
5750   if (orig_mode != BLKmode)
5751     tmp = gen_rtx_REG (orig_mode, regno);
5752   else
5753     {
5754       tmp = gen_rtx_REG (mode, regno);
5755       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5756       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5757     }
5758
5759   return tmp;
5760 }
5761
5762 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5763    of this code is to classify each 8bytes of incoming argument by the register
5764    class and assign registers accordingly.  */
5765
5766 /* Return the union class of CLASS1 and CLASS2.
5767    See the x86-64 PS ABI for details.  */
5768
5769 static enum x86_64_reg_class
5770 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5771 {
5772   /* Rule #1: If both classes are equal, this is the resulting class.  */
5773   if (class1 == class2)
5774     return class1;
5775
5776   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5777      the other class.  */
5778   if (class1 == X86_64_NO_CLASS)
5779     return class2;
5780   if (class2 == X86_64_NO_CLASS)
5781     return class1;
5782
5783   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
5784   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
5785     return X86_64_MEMORY_CLASS;
5786
5787   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
5788   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
5789       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
5790     return X86_64_INTEGERSI_CLASS;
5791   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
5792       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
5793     return X86_64_INTEGER_CLASS;
5794
5795   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
5796      MEMORY is used.  */
5797   if (class1 == X86_64_X87_CLASS
5798       || class1 == X86_64_X87UP_CLASS
5799       || class1 == X86_64_COMPLEX_X87_CLASS
5800       || class2 == X86_64_X87_CLASS
5801       || class2 == X86_64_X87UP_CLASS
5802       || class2 == X86_64_COMPLEX_X87_CLASS)
5803     return X86_64_MEMORY_CLASS;
5804
5805   /* Rule #6: Otherwise class SSE is used.  */
5806   return X86_64_SSE_CLASS;
5807 }
5808
5809 /* Classify the argument of type TYPE and mode MODE.
5810    CLASSES will be filled by the register class used to pass each word
5811    of the operand.  The number of words is returned.  In case the parameter
5812    should be passed in memory, 0 is returned. As a special case for zero
5813    sized containers, classes[0] will be NO_CLASS and 1 is returned.
5814
5815    BIT_OFFSET is used internally for handling records and specifies offset
5816    of the offset in bits modulo 256 to avoid overflow cases.
5817
5818    See the x86-64 PS ABI for details.
5819 */
5820
5821 static int
5822 classify_argument (enum machine_mode mode, const_tree type,
5823                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
5824 {
5825   HOST_WIDE_INT bytes =
5826     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5827   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5828
5829   /* Variable sized entities are always passed/returned in memory.  */
5830   if (bytes < 0)
5831     return 0;
5832
5833   if (mode != VOIDmode
5834       && targetm.calls.must_pass_in_stack (mode, type))
5835     return 0;
5836
5837   if (type && AGGREGATE_TYPE_P (type))
5838     {
5839       int i;
5840       tree field;
5841       enum x86_64_reg_class subclasses[MAX_CLASSES];
5842
5843       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
5844       if (bytes > 32)
5845         return 0;
5846
5847       for (i = 0; i < words; i++)
5848         classes[i] = X86_64_NO_CLASS;
5849
5850       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
5851          signalize memory class, so handle it as special case.  */
5852       if (!words)
5853         {
5854           classes[0] = X86_64_NO_CLASS;
5855           return 1;
5856         }
5857
5858       /* Classify each field of record and merge classes.  */
5859       switch (TREE_CODE (type))
5860         {
5861         case RECORD_TYPE:
5862           /* And now merge the fields of structure.  */
5863           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5864             {
5865               if (TREE_CODE (field) == FIELD_DECL)
5866                 {
5867                   int num;
5868
5869                   if (TREE_TYPE (field) == error_mark_node)
5870                     continue;
5871
5872                   /* Bitfields are always classified as integer.  Handle them
5873                      early, since later code would consider them to be
5874                      misaligned integers.  */
5875                   if (DECL_BIT_FIELD (field))
5876                     {
5877                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5878                            i < ((int_bit_position (field) + (bit_offset % 64))
5879                                 + tree_low_cst (DECL_SIZE (field), 0)
5880                                 + 63) / 8 / 8; i++)
5881                         classes[i] =
5882                           merge_classes (X86_64_INTEGER_CLASS,
5883                                          classes[i]);
5884                     }
5885                   else
5886                     {
5887                       int pos;
5888
5889                       type = TREE_TYPE (field);
5890
5891                       /* Flexible array member is ignored.  */
5892                       if (TYPE_MODE (type) == BLKmode
5893                           && TREE_CODE (type) == ARRAY_TYPE
5894                           && TYPE_SIZE (type) == NULL_TREE
5895                           && TYPE_DOMAIN (type) != NULL_TREE
5896                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5897                               == NULL_TREE))
5898                         {
5899                           static bool warned;
5900
5901                           if (!warned && warn_psabi)
5902                             {
5903                               warned = true;
5904                               inform (input_location,
5905                                       "the ABI of passing struct with"
5906                                       " a flexible array member has"
5907                                       " changed in GCC 4.4");
5908                             }
5909                           continue;
5910                         }
5911                       num = classify_argument (TYPE_MODE (type), type,
5912                                                subclasses,
5913                                                (int_bit_position (field)
5914                                                 + bit_offset) % 256);
5915                       if (!num)
5916                         return 0;
5917                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5918                       for (i = 0; i < num && (i + pos) < words; i++)
5919                         classes[i + pos] =
5920                           merge_classes (subclasses[i], classes[i + pos]);
5921                     }
5922                 }
5923             }
5924           break;
5925
5926         case ARRAY_TYPE:
5927           /* Arrays are handled as small records.  */
5928           {
5929             int num;
5930             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
5931                                      TREE_TYPE (type), subclasses, bit_offset);
5932             if (!num)
5933               return 0;
5934
5935             /* The partial classes are now full classes.  */
5936             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
5937               subclasses[0] = X86_64_SSE_CLASS;
5938             if (subclasses[0] == X86_64_INTEGERSI_CLASS
5939                 && !((bit_offset % 64) == 0 && bytes == 4))
5940               subclasses[0] = X86_64_INTEGER_CLASS;
5941
5942             for (i = 0; i < words; i++)
5943               classes[i] = subclasses[i % num];
5944
5945             break;
5946           }
5947         case UNION_TYPE:
5948         case QUAL_UNION_TYPE:
5949           /* Unions are similar to RECORD_TYPE but offset is always 0.
5950              */
5951           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5952             {
5953               if (TREE_CODE (field) == FIELD_DECL)
5954                 {
5955                   int num;
5956
5957                   if (TREE_TYPE (field) == error_mark_node)
5958                     continue;
5959
5960                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
5961                                            TREE_TYPE (field), subclasses,
5962                                            bit_offset);
5963                   if (!num)
5964                     return 0;
5965                   for (i = 0; i < num; i++)
5966                     classes[i] = merge_classes (subclasses[i], classes[i]);
5967                 }
5968             }
5969           break;
5970
5971         default:
5972           gcc_unreachable ();
5973         }
5974
5975       if (words > 2)
5976         {
5977           /* When size > 16 bytes, if the first one isn't
5978              X86_64_SSE_CLASS or any other ones aren't
5979              X86_64_SSEUP_CLASS, everything should be passed in
5980              memory.  */
5981           if (classes[0] != X86_64_SSE_CLASS)
5982               return 0;
5983
5984           for (i = 1; i < words; i++)
5985             if (classes[i] != X86_64_SSEUP_CLASS)
5986               return 0;
5987         }
5988
5989       /* Final merger cleanup.  */
5990       for (i = 0; i < words; i++)
5991         {
5992           /* If one class is MEMORY, everything should be passed in
5993              memory.  */
5994           if (classes[i] == X86_64_MEMORY_CLASS)
5995             return 0;
5996
5997           /* The X86_64_SSEUP_CLASS should be always preceded by
5998              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
5999           if (classes[i] == X86_64_SSEUP_CLASS
6000               && classes[i - 1] != X86_64_SSE_CLASS
6001               && classes[i - 1] != X86_64_SSEUP_CLASS)
6002             {
6003               /* The first one should never be X86_64_SSEUP_CLASS.  */
6004               gcc_assert (i != 0);
6005               classes[i] = X86_64_SSE_CLASS;
6006             }
6007
6008           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
6009                everything should be passed in memory.  */
6010           if (classes[i] == X86_64_X87UP_CLASS
6011               && (classes[i - 1] != X86_64_X87_CLASS))
6012             {
6013               static bool warned;
6014
6015               /* The first one should never be X86_64_X87UP_CLASS.  */
6016               gcc_assert (i != 0);
6017               if (!warned && warn_psabi)
6018                 {
6019                   warned = true;
6020                   inform (input_location,
6021                           "the ABI of passing union with long double"
6022                           " has changed in GCC 4.4");
6023                 }
6024               return 0;
6025             }
6026         }
6027       return words;
6028     }
6029
6030   /* Compute alignment needed.  We align all types to natural boundaries with
6031      exception of XFmode that is aligned to 64bits.  */
6032   if (mode != VOIDmode && mode != BLKmode)
6033     {
6034       int mode_alignment = GET_MODE_BITSIZE (mode);
6035
6036       if (mode == XFmode)
6037         mode_alignment = 128;
6038       else if (mode == XCmode)
6039         mode_alignment = 256;
6040       if (COMPLEX_MODE_P (mode))
6041         mode_alignment /= 2;
6042       /* Misaligned fields are always returned in memory.  */
6043       if (bit_offset % mode_alignment)
6044         return 0;
6045     }
6046
6047   /* for V1xx modes, just use the base mode */
6048   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
6049       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
6050     mode = GET_MODE_INNER (mode);
6051
6052   /* Classification of atomic types.  */
6053   switch (mode)
6054     {
6055     case SDmode:
6056     case DDmode:
6057       classes[0] = X86_64_SSE_CLASS;
6058       return 1;
6059     case TDmode:
6060       classes[0] = X86_64_SSE_CLASS;
6061       classes[1] = X86_64_SSEUP_CLASS;
6062       return 2;
6063     case DImode:
6064     case SImode:
6065     case HImode:
6066     case QImode:
6067     case CSImode:
6068     case CHImode:
6069     case CQImode:
6070       {
6071         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
6072
6073         if (size <= 32)
6074           {
6075             classes[0] = X86_64_INTEGERSI_CLASS;
6076             return 1;
6077           }
6078         else if (size <= 64)
6079           {
6080             classes[0] = X86_64_INTEGER_CLASS;
6081             return 1;
6082           }
6083         else if (size <= 64+32)
6084           {
6085             classes[0] = X86_64_INTEGER_CLASS;
6086             classes[1] = X86_64_INTEGERSI_CLASS;
6087             return 2;
6088           }
6089         else if (size <= 64+64)
6090           {
6091             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6092             return 2;
6093           }
6094         else
6095           gcc_unreachable ();
6096       }
6097     case CDImode:
6098     case TImode:
6099       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6100       return 2;
6101     case COImode:
6102     case OImode:
6103       /* OImode shouldn't be used directly.  */
6104       gcc_unreachable ();
6105     case CTImode:
6106       return 0;
6107     case SFmode:
6108       if (!(bit_offset % 64))
6109         classes[0] = X86_64_SSESF_CLASS;
6110       else
6111         classes[0] = X86_64_SSE_CLASS;
6112       return 1;
6113     case DFmode:
6114       classes[0] = X86_64_SSEDF_CLASS;
6115       return 1;
6116     case XFmode:
6117       classes[0] = X86_64_X87_CLASS;
6118       classes[1] = X86_64_X87UP_CLASS;
6119       return 2;
6120     case TFmode:
6121       classes[0] = X86_64_SSE_CLASS;
6122       classes[1] = X86_64_SSEUP_CLASS;
6123       return 2;
6124     case SCmode:
6125       classes[0] = X86_64_SSE_CLASS;
6126       if (!(bit_offset % 64))
6127         return 1;
6128       else
6129         {
6130           static bool warned;
6131
6132           if (!warned && warn_psabi)
6133             {
6134               warned = true;
6135               inform (input_location,
6136                       "the ABI of passing structure with complex float"
6137                       " member has changed in GCC 4.4");
6138             }
6139           classes[1] = X86_64_SSESF_CLASS;
6140           return 2;
6141         }
6142     case DCmode:
6143       classes[0] = X86_64_SSEDF_CLASS;
6144       classes[1] = X86_64_SSEDF_CLASS;
6145       return 2;
6146     case XCmode:
6147       classes[0] = X86_64_COMPLEX_X87_CLASS;
6148       return 1;
6149     case TCmode:
6150       /* This modes is larger than 16 bytes.  */
6151       return 0;
6152     case V8SFmode:
6153     case V8SImode:
6154     case V32QImode:
6155     case V16HImode:
6156     case V4DFmode:
6157     case V4DImode:
6158       classes[0] = X86_64_SSE_CLASS;
6159       classes[1] = X86_64_SSEUP_CLASS;
6160       classes[2] = X86_64_SSEUP_CLASS;
6161       classes[3] = X86_64_SSEUP_CLASS;
6162       return 4;
6163     case V4SFmode:
6164     case V4SImode:
6165     case V16QImode:
6166     case V8HImode:
6167     case V2DFmode:
6168     case V2DImode:
6169       classes[0] = X86_64_SSE_CLASS;
6170       classes[1] = X86_64_SSEUP_CLASS;
6171       return 2;
6172     case V1TImode:
6173     case V1DImode:
6174     case V2SFmode:
6175     case V2SImode:
6176     case V4HImode:
6177     case V8QImode:
6178       classes[0] = X86_64_SSE_CLASS;
6179       return 1;
6180     case BLKmode:
6181     case VOIDmode:
6182       return 0;
6183     default:
6184       gcc_assert (VECTOR_MODE_P (mode));
6185
6186       if (bytes > 16)
6187         return 0;
6188
6189       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
6190
6191       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
6192         classes[0] = X86_64_INTEGERSI_CLASS;
6193       else
6194         classes[0] = X86_64_INTEGER_CLASS;
6195       classes[1] = X86_64_INTEGER_CLASS;
6196       return 1 + (bytes > 8);
6197     }
6198 }
6199
6200 /* Examine the argument and return set number of register required in each
6201    class.  Return 0 iff parameter should be passed in memory.  */
6202 static int
6203 examine_argument (enum machine_mode mode, const_tree type, int in_return,
6204                   int *int_nregs, int *sse_nregs)
6205 {
6206   enum x86_64_reg_class regclass[MAX_CLASSES];
6207   int n = classify_argument (mode, type, regclass, 0);
6208
6209   *int_nregs = 0;
6210   *sse_nregs = 0;
6211   if (!n)
6212     return 0;
6213   for (n--; n >= 0; n--)
6214     switch (regclass[n])
6215       {
6216       case X86_64_INTEGER_CLASS:
6217       case X86_64_INTEGERSI_CLASS:
6218         (*int_nregs)++;
6219         break;
6220       case X86_64_SSE_CLASS:
6221       case X86_64_SSESF_CLASS:
6222       case X86_64_SSEDF_CLASS:
6223         (*sse_nregs)++;
6224         break;
6225       case X86_64_NO_CLASS:
6226       case X86_64_SSEUP_CLASS:
6227         break;
6228       case X86_64_X87_CLASS:
6229       case X86_64_X87UP_CLASS:
6230         if (!in_return)
6231           return 0;
6232         break;
6233       case X86_64_COMPLEX_X87_CLASS:
6234         return in_return ? 2 : 0;
6235       case X86_64_MEMORY_CLASS:
6236         gcc_unreachable ();
6237       }
6238   return 1;
6239 }
6240
6241 /* Construct container for the argument used by GCC interface.  See
6242    FUNCTION_ARG for the detailed description.  */
6243
6244 static rtx
6245 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
6246                      const_tree type, int in_return, int nintregs, int nsseregs,
6247                      const int *intreg, int sse_regno)
6248 {
6249   /* The following variables hold the static issued_error state.  */
6250   static bool issued_sse_arg_error;
6251   static bool issued_sse_ret_error;
6252   static bool issued_x87_ret_error;
6253
6254   enum machine_mode tmpmode;
6255   int bytes =
6256     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
6257   enum x86_64_reg_class regclass[MAX_CLASSES];
6258   int n;
6259   int i;
6260   int nexps = 0;
6261   int needed_sseregs, needed_intregs;
6262   rtx exp[MAX_CLASSES];
6263   rtx ret;
6264
6265   n = classify_argument (mode, type, regclass, 0);
6266   if (!n)
6267     return NULL;
6268   if (!examine_argument (mode, type, in_return, &needed_intregs,
6269                          &needed_sseregs))
6270     return NULL;
6271   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
6272     return NULL;
6273
6274   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
6275      some less clueful developer tries to use floating-point anyway.  */
6276   if (needed_sseregs && !TARGET_SSE)
6277     {
6278       if (in_return)
6279         {
6280           if (!issued_sse_ret_error)
6281             {
6282               error ("SSE register return with SSE disabled");
6283               issued_sse_ret_error = true;
6284             }
6285         }
6286       else if (!issued_sse_arg_error)
6287         {
6288           error ("SSE register argument with SSE disabled");
6289           issued_sse_arg_error = true;
6290         }
6291       return NULL;
6292     }
6293
6294   /* Likewise, error if the ABI requires us to return values in the
6295      x87 registers and the user specified -mno-80387.  */
6296   if (!TARGET_80387 && in_return)
6297     for (i = 0; i < n; i++)
6298       if (regclass[i] == X86_64_X87_CLASS
6299           || regclass[i] == X86_64_X87UP_CLASS
6300           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
6301         {
6302           if (!issued_x87_ret_error)
6303             {
6304               error ("x87 register return with x87 disabled");
6305               issued_x87_ret_error = true;
6306             }
6307           return NULL;
6308         }
6309
6310   /* First construct simple cases.  Avoid SCmode, since we want to use
6311      single register to pass this type.  */
6312   if (n == 1 && mode != SCmode)
6313     switch (regclass[0])
6314       {
6315       case X86_64_INTEGER_CLASS:
6316       case X86_64_INTEGERSI_CLASS:
6317         return gen_rtx_REG (mode, intreg[0]);
6318       case X86_64_SSE_CLASS:
6319       case X86_64_SSESF_CLASS:
6320       case X86_64_SSEDF_CLASS:
6321         if (mode != BLKmode)
6322           return gen_reg_or_parallel (mode, orig_mode,
6323                                       SSE_REGNO (sse_regno));
6324         break;
6325       case X86_64_X87_CLASS:
6326       case X86_64_COMPLEX_X87_CLASS:
6327         return gen_rtx_REG (mode, FIRST_STACK_REG);
6328       case X86_64_NO_CLASS:
6329         /* Zero sized array, struct or class.  */
6330         return NULL;
6331       default:
6332         gcc_unreachable ();
6333       }
6334   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
6335       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
6336     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6337   if (n == 4
6338       && regclass[0] == X86_64_SSE_CLASS
6339       && regclass[1] == X86_64_SSEUP_CLASS
6340       && regclass[2] == X86_64_SSEUP_CLASS
6341       && regclass[3] == X86_64_SSEUP_CLASS
6342       && mode != BLKmode)
6343     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6344
6345   if (n == 2
6346       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
6347     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
6348   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
6349       && regclass[1] == X86_64_INTEGER_CLASS
6350       && (mode == CDImode || mode == TImode || mode == TFmode)
6351       && intreg[0] + 1 == intreg[1])
6352     return gen_rtx_REG (mode, intreg[0]);
6353
6354   /* Otherwise figure out the entries of the PARALLEL.  */
6355   for (i = 0; i < n; i++)
6356     {
6357       int pos;
6358
6359       switch (regclass[i])
6360         {
6361           case X86_64_NO_CLASS:
6362             break;
6363           case X86_64_INTEGER_CLASS:
6364           case X86_64_INTEGERSI_CLASS:
6365             /* Merge TImodes on aligned occasions here too.  */
6366             if (i * 8 + 8 > bytes)
6367               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
6368             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
6369               tmpmode = SImode;
6370             else
6371               tmpmode = DImode;
6372             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
6373             if (tmpmode == BLKmode)
6374               tmpmode = DImode;
6375             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6376                                                gen_rtx_REG (tmpmode, *intreg),
6377                                                GEN_INT (i*8));
6378             intreg++;
6379             break;
6380           case X86_64_SSESF_CLASS:
6381             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6382                                                gen_rtx_REG (SFmode,
6383                                                             SSE_REGNO (sse_regno)),
6384                                                GEN_INT (i*8));
6385             sse_regno++;
6386             break;
6387           case X86_64_SSEDF_CLASS:
6388             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6389                                                gen_rtx_REG (DFmode,
6390                                                             SSE_REGNO (sse_regno)),
6391                                                GEN_INT (i*8));
6392             sse_regno++;
6393             break;
6394           case X86_64_SSE_CLASS:
6395             pos = i;
6396             switch (n)
6397               {
6398               case 1:
6399                 tmpmode = DImode;
6400                 break;
6401               case 2:
6402                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
6403                   {
6404                     tmpmode = TImode;
6405                     i++;
6406                   }
6407                 else
6408                   tmpmode = DImode;
6409                 break;
6410               case 4:
6411                 gcc_assert (i == 0
6412                             && regclass[1] == X86_64_SSEUP_CLASS
6413                             && regclass[2] == X86_64_SSEUP_CLASS
6414                             && regclass[3] == X86_64_SSEUP_CLASS);
6415                 tmpmode = OImode;
6416                 i += 3;
6417                 break;
6418               default:
6419                 gcc_unreachable ();
6420               }
6421             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6422                                                gen_rtx_REG (tmpmode,
6423                                                             SSE_REGNO (sse_regno)),
6424                                                GEN_INT (pos*8));
6425             sse_regno++;
6426             break;
6427           default:
6428             gcc_unreachable ();
6429         }
6430     }
6431
6432   /* Empty aligned struct, union or class.  */
6433   if (nexps == 0)
6434     return NULL;
6435
6436   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
6437   for (i = 0; i < nexps; i++)
6438     XVECEXP (ret, 0, i) = exp [i];
6439   return ret;
6440 }
6441
6442 /* Update the data in CUM to advance over an argument of mode MODE
6443    and data type TYPE.  (TYPE is null for libcalls where that information
6444    may not be available.)  */
6445
6446 static void
6447 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6448                          const_tree type, HOST_WIDE_INT bytes,
6449                          HOST_WIDE_INT words)
6450 {
6451   switch (mode)
6452     {
6453     default:
6454       break;
6455
6456     case BLKmode:
6457       if (bytes < 0)
6458         break;
6459       /* FALLTHRU */
6460
6461     case DImode:
6462     case SImode:
6463     case HImode:
6464     case QImode:
6465       cum->words += words;
6466       cum->nregs -= words;
6467       cum->regno += words;
6468
6469       if (cum->nregs <= 0)
6470         {
6471           cum->nregs = 0;
6472           cum->regno = 0;
6473         }
6474       break;
6475
6476     case OImode:
6477       /* OImode shouldn't be used directly.  */
6478       gcc_unreachable ();
6479
6480     case DFmode:
6481       if (cum->float_in_sse < 2)
6482         break;
6483     case SFmode:
6484       if (cum->float_in_sse < 1)
6485         break;
6486       /* FALLTHRU */
6487
6488     case V8SFmode:
6489     case V8SImode:
6490     case V32QImode:
6491     case V16HImode:
6492     case V4DFmode:
6493     case V4DImode:
6494     case TImode:
6495     case V16QImode:
6496     case V8HImode:
6497     case V4SImode:
6498     case V2DImode:
6499     case V4SFmode:
6500     case V2DFmode:
6501       if (!type || !AGGREGATE_TYPE_P (type))
6502         {
6503           cum->sse_words += words;
6504           cum->sse_nregs -= 1;
6505           cum->sse_regno += 1;
6506           if (cum->sse_nregs <= 0)
6507             {
6508               cum->sse_nregs = 0;
6509               cum->sse_regno = 0;
6510             }
6511         }
6512       break;
6513
6514     case V8QImode:
6515     case V4HImode:
6516     case V2SImode:
6517     case V2SFmode:
6518     case V1TImode:
6519     case V1DImode:
6520       if (!type || !AGGREGATE_TYPE_P (type))
6521         {
6522           cum->mmx_words += words;
6523           cum->mmx_nregs -= 1;
6524           cum->mmx_regno += 1;
6525           if (cum->mmx_nregs <= 0)
6526             {
6527               cum->mmx_nregs = 0;
6528               cum->mmx_regno = 0;
6529             }
6530         }
6531       break;
6532     }
6533 }
6534
6535 static void
6536 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6537                          const_tree type, HOST_WIDE_INT words, bool named)
6538 {
6539   int int_nregs, sse_nregs;
6540
6541   /* Unnamed 256bit vector mode parameters are passed on stack.  */
6542   if (!named && VALID_AVX256_REG_MODE (mode))
6543     return;
6544
6545   if (examine_argument (mode, type, 0, &int_nregs, &sse_nregs)
6546       && sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
6547     {
6548       cum->nregs -= int_nregs;
6549       cum->sse_nregs -= sse_nregs;
6550       cum->regno += int_nregs;
6551       cum->sse_regno += sse_nregs;
6552     }
6553   else
6554     {
6555       int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD;
6556       cum->words = (cum->words + align - 1) & ~(align - 1);
6557       cum->words += words;
6558     }
6559 }
6560
6561 static void
6562 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
6563                             HOST_WIDE_INT words)
6564 {
6565   /* Otherwise, this should be passed indirect.  */
6566   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
6567
6568   cum->words += words;
6569   if (cum->nregs > 0)
6570     {
6571       cum->nregs -= 1;
6572       cum->regno += 1;
6573     }
6574 }
6575
6576 /* Update the data in CUM to advance over an argument of mode MODE and
6577    data type TYPE.  (TYPE is null for libcalls where that information
6578    may not be available.)  */
6579
6580 static void
6581 ix86_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
6582                            const_tree type, bool named)
6583 {
6584   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6585   HOST_WIDE_INT bytes, words;
6586
6587   if (mode == BLKmode)
6588     bytes = int_size_in_bytes (type);
6589   else
6590     bytes = GET_MODE_SIZE (mode);
6591   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6592
6593   if (type)
6594     mode = type_natural_mode (type, NULL);
6595
6596   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6597     function_arg_advance_ms_64 (cum, bytes, words);
6598   else if (TARGET_64BIT)
6599     function_arg_advance_64 (cum, mode, type, words, named);
6600   else
6601     function_arg_advance_32 (cum, mode, type, bytes, words);
6602 }
6603
6604 /* Define where to put the arguments to a function.
6605    Value is zero to push the argument on the stack,
6606    or a hard register in which to store the argument.
6607
6608    MODE is the argument's machine mode.
6609    TYPE is the data type of the argument (as a tree).
6610     This is null for libcalls where that information may
6611     not be available.
6612    CUM is a variable of type CUMULATIVE_ARGS which gives info about
6613     the preceding args and about the function being called.
6614    NAMED is nonzero if this argument is a named parameter
6615     (otherwise it is an extra parameter matching an ellipsis).  */
6616
6617 static rtx
6618 function_arg_32 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6619                  enum machine_mode orig_mode, const_tree type,
6620                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
6621 {
6622   static bool warnedsse, warnedmmx;
6623
6624   /* Avoid the AL settings for the Unix64 ABI.  */
6625   if (mode == VOIDmode)
6626     return constm1_rtx;
6627
6628   switch (mode)
6629     {
6630     default:
6631       break;
6632
6633     case BLKmode:
6634       if (bytes < 0)
6635         break;
6636       /* FALLTHRU */
6637     case DImode:
6638     case SImode:
6639     case HImode:
6640     case QImode:
6641       if (words <= cum->nregs)
6642         {
6643           int regno = cum->regno;
6644
6645           /* Fastcall allocates the first two DWORD (SImode) or
6646             smaller arguments to ECX and EDX if it isn't an
6647             aggregate type .  */
6648           if (cum->fastcall)
6649             {
6650               if (mode == BLKmode
6651                   || mode == DImode
6652                   || (type && AGGREGATE_TYPE_P (type)))
6653                 break;
6654
6655               /* ECX not EAX is the first allocated register.  */
6656               if (regno == AX_REG)
6657                 regno = CX_REG;
6658             }
6659           return gen_rtx_REG (mode, regno);
6660         }
6661       break;
6662
6663     case DFmode:
6664       if (cum->float_in_sse < 2)
6665         break;
6666     case SFmode:
6667       if (cum->float_in_sse < 1)
6668         break;
6669       /* FALLTHRU */
6670     case TImode:
6671       /* In 32bit, we pass TImode in xmm registers.  */
6672     case V16QImode:
6673     case V8HImode:
6674     case V4SImode:
6675     case V2DImode:
6676     case V4SFmode:
6677     case V2DFmode:
6678       if (!type || !AGGREGATE_TYPE_P (type))
6679         {
6680           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
6681             {
6682               warnedsse = true;
6683               warning (0, "SSE vector argument without SSE enabled "
6684                        "changes the ABI");
6685             }
6686           if (cum->sse_nregs)
6687             return gen_reg_or_parallel (mode, orig_mode,
6688                                         cum->sse_regno + FIRST_SSE_REG);
6689         }
6690       break;
6691
6692     case OImode:
6693       /* OImode shouldn't be used directly.  */
6694       gcc_unreachable ();
6695
6696     case V8SFmode:
6697     case V8SImode:
6698     case V32QImode:
6699     case V16HImode:
6700     case V4DFmode:
6701     case V4DImode:
6702       if (!type || !AGGREGATE_TYPE_P (type))
6703         {
6704           if (cum->sse_nregs)
6705             return gen_reg_or_parallel (mode, orig_mode,
6706                                         cum->sse_regno + FIRST_SSE_REG);
6707         }
6708       break;
6709
6710     case V8QImode:
6711     case V4HImode:
6712     case V2SImode:
6713     case V2SFmode:
6714     case V1TImode:
6715     case V1DImode:
6716       if (!type || !AGGREGATE_TYPE_P (type))
6717         {
6718           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6719             {
6720               warnedmmx = true;
6721               warning (0, "MMX vector argument without MMX enabled "
6722                        "changes the ABI");
6723             }
6724           if (cum->mmx_nregs)
6725             return gen_reg_or_parallel (mode, orig_mode,
6726                                         cum->mmx_regno + FIRST_MMX_REG);
6727         }
6728       break;
6729     }
6730
6731   return NULL_RTX;
6732 }
6733
6734 static rtx
6735 function_arg_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6736                  enum machine_mode orig_mode, const_tree type, bool named)
6737 {
6738   /* Handle a hidden AL argument containing number of registers
6739      for varargs x86-64 functions.  */
6740   if (mode == VOIDmode)
6741     return GEN_INT (cum->maybe_vaarg
6742                     ? (cum->sse_nregs < 0
6743                        ? X86_64_SSE_REGPARM_MAX
6744                        : cum->sse_regno)
6745                     : -1);
6746
6747   switch (mode)
6748     {
6749     default:
6750       break;
6751
6752     case V8SFmode:
6753     case V8SImode:
6754     case V32QImode:
6755     case V16HImode:
6756     case V4DFmode:
6757     case V4DImode:
6758       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6759       if (!named)
6760         return NULL;
6761       break;
6762     }
6763
6764   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6765                               cum->sse_nregs,
6766                               &x86_64_int_parameter_registers [cum->regno],
6767                               cum->sse_regno);
6768 }
6769
6770 static rtx
6771 function_arg_ms_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6772                     enum machine_mode orig_mode, bool named,
6773                     HOST_WIDE_INT bytes)
6774 {
6775   unsigned int regno;
6776
6777   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6778      We use value of -2 to specify that current function call is MSABI.  */
6779   if (mode == VOIDmode)
6780     return GEN_INT (-2);
6781
6782   /* If we've run out of registers, it goes on the stack.  */
6783   if (cum->nregs == 0)
6784     return NULL_RTX;
6785
6786   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
6787
6788   /* Only floating point modes are passed in anything but integer regs.  */
6789   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
6790     {
6791       if (named)
6792         regno = cum->regno + FIRST_SSE_REG;
6793       else
6794         {
6795           rtx t1, t2;
6796
6797           /* Unnamed floating parameters are passed in both the
6798              SSE and integer registers.  */
6799           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
6800           t2 = gen_rtx_REG (mode, regno);
6801           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
6802           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
6803           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
6804         }
6805     }
6806   /* Handle aggregated types passed in register.  */
6807   if (orig_mode == BLKmode)
6808     {
6809       if (bytes > 0 && bytes <= 8)
6810         mode = (bytes > 4 ? DImode : SImode);
6811       if (mode == BLKmode)
6812         mode = DImode;
6813     }
6814
6815   return gen_reg_or_parallel (mode, orig_mode, regno);
6816 }
6817
6818 /* Return where to put the arguments to a function.
6819    Return zero to push the argument on the stack, or a hard register in which to store the argument.
6820
6821    MODE is the argument's machine mode.  TYPE is the data type of the
6822    argument.  It is null for libcalls where that information may not be
6823    available.  CUM gives information about the preceding args and about
6824    the function being called.  NAMED is nonzero if this argument is a
6825    named parameter (otherwise it is an extra parameter matching an
6826    ellipsis).  */
6827
6828 static rtx
6829 ix86_function_arg (cumulative_args_t cum_v, enum machine_mode omode,
6830                    const_tree type, bool named)
6831 {
6832   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6833   enum machine_mode mode = omode;
6834   HOST_WIDE_INT bytes, words;
6835   rtx arg;
6836
6837   if (mode == BLKmode)
6838     bytes = int_size_in_bytes (type);
6839   else
6840     bytes = GET_MODE_SIZE (mode);
6841   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6842
6843   /* To simplify the code below, represent vector types with a vector mode
6844      even if MMX/SSE are not active.  */
6845   if (type && TREE_CODE (type) == VECTOR_TYPE)
6846     mode = type_natural_mode (type, cum);
6847
6848   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6849     arg = function_arg_ms_64 (cum, mode, omode, named, bytes);
6850   else if (TARGET_64BIT)
6851     arg = function_arg_64 (cum, mode, omode, type, named);
6852   else
6853     arg = function_arg_32 (cum, mode, omode, type, bytes, words);
6854
6855   if (TARGET_VZEROUPPER && function_pass_avx256_p (arg))
6856     {
6857       /* This argument uses 256bit AVX modes.  */
6858       if (cum->caller)
6859         cfun->machine->callee_pass_avx256_p = true;
6860       else
6861         cfun->machine->caller_pass_avx256_p = true;
6862     }
6863
6864   return arg;
6865 }
6866
6867 /* A C expression that indicates when an argument must be passed by
6868    reference.  If nonzero for an argument, a copy of that argument is
6869    made in memory and a pointer to the argument is passed instead of
6870    the argument itself.  The pointer is passed in whatever way is
6871    appropriate for passing a pointer to that type.  */
6872
6873 static bool
6874 ix86_pass_by_reference (cumulative_args_t cum_v ATTRIBUTE_UNUSED,
6875                         enum machine_mode mode ATTRIBUTE_UNUSED,
6876                         const_tree type, bool named ATTRIBUTE_UNUSED)
6877 {
6878   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6879
6880   /* See Windows x64 Software Convention.  */
6881   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6882     {
6883       int msize = (int) GET_MODE_SIZE (mode);
6884       if (type)
6885         {
6886           /* Arrays are passed by reference.  */
6887           if (TREE_CODE (type) == ARRAY_TYPE)
6888             return true;
6889
6890           if (AGGREGATE_TYPE_P (type))
6891             {
6892               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
6893                  are passed by reference.  */
6894               msize = int_size_in_bytes (type);
6895             }
6896         }
6897
6898       /* __m128 is passed by reference.  */
6899       switch (msize) {
6900       case 1: case 2: case 4: case 8:
6901         break;
6902       default:
6903         return true;
6904       }
6905     }
6906   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
6907     return 1;
6908
6909   return 0;
6910 }
6911
6912 /* Return true when TYPE should be 128bit aligned for 32bit argument
6913    passing ABI.  XXX: This function is obsolete and is only used for
6914    checking psABI compatibility with previous versions of GCC.  */
6915
6916 static bool
6917 ix86_compat_aligned_value_p (const_tree type)
6918 {
6919   enum machine_mode mode = TYPE_MODE (type);
6920   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
6921        || mode == TDmode
6922        || mode == TFmode
6923        || mode == TCmode)
6924       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
6925     return true;
6926   if (TYPE_ALIGN (type) < 128)
6927     return false;
6928
6929   if (AGGREGATE_TYPE_P (type))
6930     {
6931       /* Walk the aggregates recursively.  */
6932       switch (TREE_CODE (type))
6933         {
6934         case RECORD_TYPE:
6935         case UNION_TYPE:
6936         case QUAL_UNION_TYPE:
6937           {
6938             tree field;
6939
6940             /* Walk all the structure fields.  */
6941             for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6942               {
6943                 if (TREE_CODE (field) == FIELD_DECL
6944                     && ix86_compat_aligned_value_p (TREE_TYPE (field)))
6945                   return true;
6946               }
6947             break;
6948           }
6949
6950         case ARRAY_TYPE:
6951           /* Just for use if some languages passes arrays by value.  */
6952           if (ix86_compat_aligned_value_p (TREE_TYPE (type)))
6953             return true;
6954           break;
6955
6956         default:
6957           gcc_unreachable ();
6958         }
6959     }
6960   return false;
6961 }
6962
6963 /* Return the alignment boundary for MODE and TYPE with alignment ALIGN.
6964    XXX: This function is obsolete and is only used for checking psABI
6965    compatibility with previous versions of GCC.  */
6966
6967 static unsigned int
6968 ix86_compat_function_arg_boundary (enum machine_mode mode,
6969                                    const_tree type, unsigned int align)
6970 {
6971   /* In 32bit, only _Decimal128 and __float128 are aligned to their
6972      natural boundaries.  */
6973   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
6974     {
6975       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
6976          make an exception for SSE modes since these require 128bit
6977          alignment.
6978
6979          The handling here differs from field_alignment.  ICC aligns MMX
6980          arguments to 4 byte boundaries, while structure fields are aligned
6981          to 8 byte boundaries.  */
6982       if (!type)
6983         {
6984           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
6985             align = PARM_BOUNDARY;
6986         }
6987       else
6988         {
6989           if (!ix86_compat_aligned_value_p (type))
6990             align = PARM_BOUNDARY;
6991         }
6992     }
6993   if (align > BIGGEST_ALIGNMENT)
6994     align = BIGGEST_ALIGNMENT;
6995   return align;
6996 }
6997
6998 /* Return true when TYPE should be 128bit aligned for 32bit argument
6999    passing ABI.  */
7000
7001 static bool
7002 ix86_contains_aligned_value_p (const_tree type)
7003 {
7004   enum machine_mode mode = TYPE_MODE (type);
7005
7006   if (mode == XFmode || mode == XCmode)
7007     return false;
7008
7009   if (TYPE_ALIGN (type) < 128)
7010     return false;
7011
7012   if (AGGREGATE_TYPE_P (type))
7013     {
7014       /* Walk the aggregates recursively.  */
7015       switch (TREE_CODE (type))
7016         {
7017         case RECORD_TYPE:
7018         case UNION_TYPE:
7019         case QUAL_UNION_TYPE:
7020           {
7021             tree field;
7022
7023             /* Walk all the structure fields.  */
7024             for (field = TYPE_FIELDS (type);
7025                  field;
7026                  field = DECL_CHAIN (field))
7027               {
7028                 if (TREE_CODE (field) == FIELD_DECL
7029                     && ix86_contains_aligned_value_p (TREE_TYPE (field)))
7030                   return true;
7031               }
7032             break;
7033           }
7034
7035         case ARRAY_TYPE:
7036           /* Just for use if some languages passes arrays by value.  */
7037           if (ix86_contains_aligned_value_p (TREE_TYPE (type)))
7038             return true;
7039           break;
7040
7041         default:
7042           gcc_unreachable ();
7043         }
7044     }
7045   else
7046     return TYPE_ALIGN (type) >= 128;
7047
7048   return false;
7049 }
7050
7051 /* Gives the alignment boundary, in bits, of an argument with the
7052    specified mode and type.  */
7053
7054 static unsigned int
7055 ix86_function_arg_boundary (enum machine_mode mode, const_tree type)
7056 {
7057   unsigned int align;
7058   if (type)
7059     {
7060       /* Since the main variant type is used for call, we convert it to
7061          the main variant type.  */
7062       type = TYPE_MAIN_VARIANT (type);
7063       align = TYPE_ALIGN (type);
7064     }
7065   else
7066     align = GET_MODE_ALIGNMENT (mode);
7067   if (align < PARM_BOUNDARY)
7068     align = PARM_BOUNDARY;
7069   else
7070     {
7071       static bool warned;
7072       unsigned int saved_align = align;
7073
7074       if (!TARGET_64BIT)
7075         {
7076           /* i386 ABI defines XFmode arguments to be 4 byte aligned.  */
7077           if (!type)
7078             {
7079               if (mode == XFmode || mode == XCmode)
7080                 align = PARM_BOUNDARY;
7081             }
7082           else if (!ix86_contains_aligned_value_p (type))
7083             align = PARM_BOUNDARY;
7084
7085           if (align < 128)
7086             align = PARM_BOUNDARY;
7087         }
7088
7089       if (warn_psabi
7090           && !warned
7091           && align != ix86_compat_function_arg_boundary (mode, type,
7092                                                          saved_align))
7093         {
7094           warned = true;
7095           inform (input_location,
7096                   "The ABI for passing parameters with %d-byte"
7097                   " alignment has changed in GCC 4.6",
7098                   align / BITS_PER_UNIT);
7099         }
7100     }
7101
7102   return align;
7103 }
7104
7105 /* Return true if N is a possible register number of function value.  */
7106
7107 static bool
7108 ix86_function_value_regno_p (const unsigned int regno)
7109 {
7110   switch (regno)
7111     {
7112     case AX_REG:
7113       return true;
7114
7115     case FIRST_FLOAT_REG:
7116       /* TODO: The function should depend on current function ABI but
7117        builtins.c would need updating then. Therefore we use the
7118        default ABI.  */
7119       if (TARGET_64BIT && ix86_abi == MS_ABI)
7120         return false;
7121       return TARGET_FLOAT_RETURNS_IN_80387;
7122
7123     case FIRST_SSE_REG:
7124       return TARGET_SSE;
7125
7126     case FIRST_MMX_REG:
7127       if (TARGET_MACHO || TARGET_64BIT)
7128         return false;
7129       return TARGET_MMX;
7130     }
7131
7132   return false;
7133 }
7134
7135 /* Define how to find the value returned by a function.
7136    VALTYPE is the data type of the value (as a tree).
7137    If the precise function being called is known, FUNC is its FUNCTION_DECL;
7138    otherwise, FUNC is 0.  */
7139
7140 static rtx
7141 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
7142                    const_tree fntype, const_tree fn)
7143 {
7144   unsigned int regno;
7145
7146   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
7147      we normally prevent this case when mmx is not available.  However
7148      some ABIs may require the result to be returned like DImode.  */
7149   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7150     regno = FIRST_MMX_REG;
7151
7152   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
7153      we prevent this case when sse is not available.  However some ABIs
7154      may require the result to be returned like integer TImode.  */
7155   else if (mode == TImode
7156            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7157     regno = FIRST_SSE_REG;
7158
7159   /* 32-byte vector modes in %ymm0.   */
7160   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
7161     regno = FIRST_SSE_REG;
7162
7163   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
7164   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
7165     regno = FIRST_FLOAT_REG;
7166   else
7167     /* Most things go in %eax.  */
7168     regno = AX_REG;
7169
7170   /* Override FP return register with %xmm0 for local functions when
7171      SSE math is enabled or for functions with sseregparm attribute.  */
7172   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
7173     {
7174       int sse_level = ix86_function_sseregparm (fntype, fn, false);
7175       if ((sse_level >= 1 && mode == SFmode)
7176           || (sse_level == 2 && mode == DFmode))
7177         regno = FIRST_SSE_REG;
7178     }
7179
7180   /* OImode shouldn't be used directly.  */
7181   gcc_assert (mode != OImode);
7182
7183   return gen_rtx_REG (orig_mode, regno);
7184 }
7185
7186 static rtx
7187 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
7188                    const_tree valtype)
7189 {
7190   rtx ret;
7191
7192   /* Handle libcalls, which don't provide a type node.  */
7193   if (valtype == NULL)
7194     {
7195       unsigned int regno;
7196
7197       switch (mode)
7198         {
7199         case SFmode:
7200         case SCmode:
7201         case DFmode:
7202         case DCmode:
7203         case TFmode:
7204         case SDmode:
7205         case DDmode:
7206         case TDmode:
7207           regno = FIRST_SSE_REG;
7208           break;
7209         case XFmode:
7210         case XCmode:
7211           regno = FIRST_FLOAT_REG;
7212           break;
7213         case TCmode:
7214           return NULL;
7215         default:
7216           regno = AX_REG;
7217         }
7218
7219       return gen_rtx_REG (mode, regno);
7220     }
7221   else if (POINTER_TYPE_P (valtype))
7222     {
7223       /* Pointers are always returned in Pmode. */
7224       mode = Pmode;
7225     }
7226
7227   ret = construct_container (mode, orig_mode, valtype, 1,
7228                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
7229                              x86_64_int_return_registers, 0);
7230
7231   /* For zero sized structures, construct_container returns NULL, but we
7232      need to keep rest of compiler happy by returning meaningful value.  */
7233   if (!ret)
7234     ret = gen_rtx_REG (orig_mode, AX_REG);
7235
7236   return ret;
7237 }
7238
7239 static rtx
7240 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
7241 {
7242   unsigned int regno = AX_REG;
7243
7244   if (TARGET_SSE)
7245     {
7246       switch (GET_MODE_SIZE (mode))
7247         {
7248         case 16:
7249           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7250              && !COMPLEX_MODE_P (mode))
7251             regno = FIRST_SSE_REG;
7252           break;
7253         case 8:
7254         case 4:
7255           if (mode == SFmode || mode == DFmode)
7256             regno = FIRST_SSE_REG;
7257           break;
7258         default:
7259           break;
7260         }
7261     }
7262   return gen_rtx_REG (orig_mode, regno);
7263 }
7264
7265 static rtx
7266 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
7267                        enum machine_mode orig_mode, enum machine_mode mode)
7268 {
7269   const_tree fn, fntype;
7270
7271   fn = NULL_TREE;
7272   if (fntype_or_decl && DECL_P (fntype_or_decl))
7273     fn = fntype_or_decl;
7274   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
7275
7276   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
7277     return function_value_ms_64 (orig_mode, mode);
7278   else if (TARGET_64BIT)
7279     return function_value_64 (orig_mode, mode, valtype);
7280   else
7281     return function_value_32 (orig_mode, mode, fntype, fn);
7282 }
7283
7284 static rtx
7285 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
7286                      bool outgoing ATTRIBUTE_UNUSED)
7287 {
7288   enum machine_mode mode, orig_mode;
7289
7290   orig_mode = TYPE_MODE (valtype);
7291   mode = type_natural_mode (valtype, NULL);
7292   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
7293 }
7294
7295 /* Pointer function arguments and return values are promoted to Pmode.  */
7296
7297 static enum machine_mode
7298 ix86_promote_function_mode (const_tree type, enum machine_mode mode,
7299                             int *punsignedp, const_tree fntype,
7300                             int for_return)
7301 {
7302   if (type != NULL_TREE && POINTER_TYPE_P (type))
7303     {
7304       *punsignedp = POINTERS_EXTEND_UNSIGNED;
7305       return Pmode;
7306     }
7307   return default_promote_function_mode (type, mode, punsignedp, fntype,
7308                                         for_return);
7309 }
7310
7311 rtx
7312 ix86_libcall_value (enum machine_mode mode)
7313 {
7314   return ix86_function_value_1 (NULL, NULL, mode, mode);
7315 }
7316
7317 /* Return true iff type is returned in memory.  */
7318
7319 static bool ATTRIBUTE_UNUSED
7320 return_in_memory_32 (const_tree type, enum machine_mode mode)
7321 {
7322   HOST_WIDE_INT size;
7323
7324   if (mode == BLKmode)
7325     return true;
7326
7327   size = int_size_in_bytes (type);
7328
7329   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
7330     return false;
7331
7332   if (VECTOR_MODE_P (mode) || mode == TImode)
7333     {
7334       /* User-created vectors small enough to fit in EAX.  */
7335       if (size < 8)
7336         return false;
7337
7338       /* MMX/3dNow values are returned in MM0,
7339          except when it doesn't exits or the ABI prescribes otherwise.  */
7340       if (size == 8)
7341         return !TARGET_MMX || TARGET_VECT8_RETURNS;
7342
7343       /* SSE values are returned in XMM0, except when it doesn't exist.  */
7344       if (size == 16)
7345         return !TARGET_SSE;
7346
7347       /* AVX values are returned in YMM0, except when it doesn't exist.  */
7348       if (size == 32)
7349         return !TARGET_AVX;
7350     }
7351
7352   if (mode == XFmode)
7353     return false;
7354
7355   if (size > 12)
7356     return true;
7357
7358   /* OImode shouldn't be used directly.  */
7359   gcc_assert (mode != OImode);
7360
7361   return false;
7362 }
7363
7364 static bool ATTRIBUTE_UNUSED
7365 return_in_memory_64 (const_tree type, enum machine_mode mode)
7366 {
7367   int needed_intregs, needed_sseregs;
7368   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
7369 }
7370
7371 static bool ATTRIBUTE_UNUSED
7372 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
7373 {
7374   HOST_WIDE_INT size = int_size_in_bytes (type);
7375
7376   /* __m128 is returned in xmm0.  */
7377   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7378       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
7379     return false;
7380
7381   /* Otherwise, the size must be exactly in [1248]. */
7382   return size != 1 && size != 2 && size != 4 && size != 8;
7383 }
7384
7385 static bool
7386 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7387 {
7388 #ifdef SUBTARGET_RETURN_IN_MEMORY
7389   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
7390 #else
7391   const enum machine_mode mode = type_natural_mode (type, NULL);
7392
7393   if (TARGET_64BIT)
7394     {
7395       if (ix86_function_type_abi (fntype) == MS_ABI)
7396         return return_in_memory_ms_64 (type, mode);
7397       else
7398         return return_in_memory_64 (type, mode);
7399     }
7400   else
7401     return return_in_memory_32 (type, mode);
7402 #endif
7403 }
7404
7405 /* When returning SSE vector types, we have a choice of either
7406      (1) being abi incompatible with a -march switch, or
7407      (2) generating an error.
7408    Given no good solution, I think the safest thing is one warning.
7409    The user won't be able to use -Werror, but....
7410
7411    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
7412    called in response to actually generating a caller or callee that
7413    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
7414    via aggregate_value_p for general type probing from tree-ssa.  */
7415
7416 static rtx
7417 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
7418 {
7419   static bool warnedsse, warnedmmx;
7420
7421   if (!TARGET_64BIT && type)
7422     {
7423       /* Look at the return type of the function, not the function type.  */
7424       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
7425
7426       if (!TARGET_SSE && !warnedsse)
7427         {
7428           if (mode == TImode
7429               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7430             {
7431               warnedsse = true;
7432               warning (0, "SSE vector return without SSE enabled "
7433                        "changes the ABI");
7434             }
7435         }
7436
7437       if (!TARGET_MMX && !warnedmmx)
7438         {
7439           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7440             {
7441               warnedmmx = true;
7442               warning (0, "MMX vector return without MMX enabled "
7443                        "changes the ABI");
7444             }
7445         }
7446     }
7447
7448   return NULL;
7449 }
7450
7451 \f
7452 /* Create the va_list data type.  */
7453
7454 /* Returns the calling convention specific va_list date type.
7455    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
7456
7457 static tree
7458 ix86_build_builtin_va_list_abi (enum calling_abi abi)
7459 {
7460   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
7461
7462   /* For i386 we use plain pointer to argument area.  */
7463   if (!TARGET_64BIT || abi == MS_ABI)
7464     return build_pointer_type (char_type_node);
7465
7466   record = lang_hooks.types.make_type (RECORD_TYPE);
7467   type_decl = build_decl (BUILTINS_LOCATION,
7468                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
7469
7470   f_gpr = build_decl (BUILTINS_LOCATION,
7471                       FIELD_DECL, get_identifier ("gp_offset"),
7472                       unsigned_type_node);
7473   f_fpr = build_decl (BUILTINS_LOCATION,
7474                       FIELD_DECL, get_identifier ("fp_offset"),
7475                       unsigned_type_node);
7476   f_ovf = build_decl (BUILTINS_LOCATION,
7477                       FIELD_DECL, get_identifier ("overflow_arg_area"),
7478                       ptr_type_node);
7479   f_sav = build_decl (BUILTINS_LOCATION,
7480                       FIELD_DECL, get_identifier ("reg_save_area"),
7481                       ptr_type_node);
7482
7483   va_list_gpr_counter_field = f_gpr;
7484   va_list_fpr_counter_field = f_fpr;
7485
7486   DECL_FIELD_CONTEXT (f_gpr) = record;
7487   DECL_FIELD_CONTEXT (f_fpr) = record;
7488   DECL_FIELD_CONTEXT (f_ovf) = record;
7489   DECL_FIELD_CONTEXT (f_sav) = record;
7490
7491   TYPE_STUB_DECL (record) = type_decl;
7492   TYPE_NAME (record) = type_decl;
7493   TYPE_FIELDS (record) = f_gpr;
7494   DECL_CHAIN (f_gpr) = f_fpr;
7495   DECL_CHAIN (f_fpr) = f_ovf;
7496   DECL_CHAIN (f_ovf) = f_sav;
7497
7498   layout_type (record);
7499
7500   /* The correct type is an array type of one element.  */
7501   return build_array_type (record, build_index_type (size_zero_node));
7502 }
7503
7504 /* Setup the builtin va_list data type and for 64-bit the additional
7505    calling convention specific va_list data types.  */
7506
7507 static tree
7508 ix86_build_builtin_va_list (void)
7509 {
7510   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
7511
7512   /* Initialize abi specific va_list builtin types.  */
7513   if (TARGET_64BIT)
7514     {
7515       tree t;
7516       if (ix86_abi == MS_ABI)
7517         {
7518           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
7519           if (TREE_CODE (t) != RECORD_TYPE)
7520             t = build_variant_type_copy (t);
7521           sysv_va_list_type_node = t;
7522         }
7523       else
7524         {
7525           t = ret;
7526           if (TREE_CODE (t) != RECORD_TYPE)
7527             t = build_variant_type_copy (t);
7528           sysv_va_list_type_node = t;
7529         }
7530       if (ix86_abi != MS_ABI)
7531         {
7532           t = ix86_build_builtin_va_list_abi (MS_ABI);
7533           if (TREE_CODE (t) != RECORD_TYPE)
7534             t = build_variant_type_copy (t);
7535           ms_va_list_type_node = t;
7536         }
7537       else
7538         {
7539           t = ret;
7540           if (TREE_CODE (t) != RECORD_TYPE)
7541             t = build_variant_type_copy (t);
7542           ms_va_list_type_node = t;
7543         }
7544     }
7545
7546   return ret;
7547 }
7548
7549 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
7550
7551 static void
7552 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
7553 {
7554   rtx save_area, mem;
7555   alias_set_type set;
7556   int i, max;
7557
7558   /* GPR size of varargs save area.  */
7559   if (cfun->va_list_gpr_size)
7560     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
7561   else
7562     ix86_varargs_gpr_size = 0;
7563
7564   /* FPR size of varargs save area.  We don't need it if we don't pass
7565      anything in SSE registers.  */
7566   if (TARGET_SSE && cfun->va_list_fpr_size)
7567     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
7568   else
7569     ix86_varargs_fpr_size = 0;
7570
7571   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
7572     return;
7573
7574   save_area = frame_pointer_rtx;
7575   set = get_varargs_alias_set ();
7576
7577   max = cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
7578   if (max > X86_64_REGPARM_MAX)
7579     max = X86_64_REGPARM_MAX;
7580
7581   for (i = cum->regno; i < max; i++)
7582     {
7583       mem = gen_rtx_MEM (Pmode,
7584                          plus_constant (save_area, i * UNITS_PER_WORD));
7585       MEM_NOTRAP_P (mem) = 1;
7586       set_mem_alias_set (mem, set);
7587       emit_move_insn (mem, gen_rtx_REG (Pmode,
7588                                         x86_64_int_parameter_registers[i]));
7589     }
7590
7591   if (ix86_varargs_fpr_size)
7592     {
7593       enum machine_mode smode;
7594       rtx label, test;
7595
7596       /* Now emit code to save SSE registers.  The AX parameter contains number
7597          of SSE parameter registers used to call this function, though all we
7598          actually check here is the zero/non-zero status.  */
7599
7600       label = gen_label_rtx ();
7601       test = gen_rtx_EQ (VOIDmode, gen_rtx_REG (QImode, AX_REG), const0_rtx);
7602       emit_jump_insn (gen_cbranchqi4 (test, XEXP (test, 0), XEXP (test, 1),
7603                                       label));
7604
7605       /* ??? If !TARGET_SSE_TYPELESS_STORES, would we perform better if
7606          we used movdqa (i.e. TImode) instead?  Perhaps even better would
7607          be if we could determine the real mode of the data, via a hook
7608          into pass_stdarg.  Ignore all that for now.  */
7609       smode = V4SFmode;
7610       if (crtl->stack_alignment_needed < GET_MODE_ALIGNMENT (smode))
7611         crtl->stack_alignment_needed = GET_MODE_ALIGNMENT (smode);
7612
7613       max = cum->sse_regno + cfun->va_list_fpr_size / 16;
7614       if (max > X86_64_SSE_REGPARM_MAX)
7615         max = X86_64_SSE_REGPARM_MAX;
7616
7617       for (i = cum->sse_regno; i < max; ++i)
7618         {
7619           mem = plus_constant (save_area, i * 16 + ix86_varargs_gpr_size);
7620           mem = gen_rtx_MEM (smode, mem);
7621           MEM_NOTRAP_P (mem) = 1;
7622           set_mem_alias_set (mem, set);
7623           set_mem_align (mem, GET_MODE_ALIGNMENT (smode));
7624
7625           emit_move_insn (mem, gen_rtx_REG (smode, SSE_REGNO (i)));
7626         }
7627
7628       emit_label (label);
7629     }
7630 }
7631
7632 static void
7633 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
7634 {
7635   alias_set_type set = get_varargs_alias_set ();
7636   int i;
7637
7638   /* Reset to zero, as there might be a sysv vaarg used
7639      before.  */
7640   ix86_varargs_gpr_size = 0;
7641   ix86_varargs_fpr_size = 0;
7642
7643   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
7644     {
7645       rtx reg, mem;
7646
7647       mem = gen_rtx_MEM (Pmode,
7648                          plus_constant (virtual_incoming_args_rtx,
7649                                         i * UNITS_PER_WORD));
7650       MEM_NOTRAP_P (mem) = 1;
7651       set_mem_alias_set (mem, set);
7652
7653       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
7654       emit_move_insn (mem, reg);
7655     }
7656 }
7657
7658 static void
7659 ix86_setup_incoming_varargs (cumulative_args_t cum_v, enum machine_mode mode,
7660                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
7661                              int no_rtl)
7662 {
7663   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
7664   CUMULATIVE_ARGS next_cum;
7665   tree fntype;
7666
7667   /* This argument doesn't appear to be used anymore.  Which is good,
7668      because the old code here didn't suppress rtl generation.  */
7669   gcc_assert (!no_rtl);
7670
7671   if (!TARGET_64BIT)
7672     return;
7673
7674   fntype = TREE_TYPE (current_function_decl);
7675
7676   /* For varargs, we do not want to skip the dummy va_dcl argument.
7677      For stdargs, we do want to skip the last named argument.  */
7678   next_cum = *cum;
7679   if (stdarg_p (fntype))
7680     ix86_function_arg_advance (pack_cumulative_args (&next_cum), mode, type,
7681                                true);
7682
7683   if (cum->call_abi == MS_ABI)
7684     setup_incoming_varargs_ms_64 (&next_cum);
7685   else
7686     setup_incoming_varargs_64 (&next_cum);
7687 }
7688
7689 /* Checks if TYPE is of kind va_list char *.  */
7690
7691 static bool
7692 is_va_list_char_pointer (tree type)
7693 {
7694   tree canonic;
7695
7696   /* For 32-bit it is always true.  */
7697   if (!TARGET_64BIT)
7698     return true;
7699   canonic = ix86_canonical_va_list_type (type);
7700   return (canonic == ms_va_list_type_node
7701           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
7702 }
7703
7704 /* Implement va_start.  */
7705
7706 static void
7707 ix86_va_start (tree valist, rtx nextarg)
7708 {
7709   HOST_WIDE_INT words, n_gpr, n_fpr;
7710   tree f_gpr, f_fpr, f_ovf, f_sav;
7711   tree gpr, fpr, ovf, sav, t;
7712   tree type;
7713   rtx ovf_rtx;
7714
7715   if (flag_split_stack
7716       && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7717     {
7718       unsigned int scratch_regno;
7719
7720       /* When we are splitting the stack, we can't refer to the stack
7721          arguments using internal_arg_pointer, because they may be on
7722          the old stack.  The split stack prologue will arrange to
7723          leave a pointer to the old stack arguments in a scratch
7724          register, which we here copy to a pseudo-register.  The split
7725          stack prologue can't set the pseudo-register directly because
7726          it (the prologue) runs before any registers have been saved.  */
7727
7728       scratch_regno = split_stack_prologue_scratch_regno ();
7729       if (scratch_regno != INVALID_REGNUM)
7730         {
7731           rtx reg, seq;
7732
7733           reg = gen_reg_rtx (Pmode);
7734           cfun->machine->split_stack_varargs_pointer = reg;
7735
7736           start_sequence ();
7737           emit_move_insn (reg, gen_rtx_REG (Pmode, scratch_regno));
7738           seq = get_insns ();
7739           end_sequence ();
7740
7741           push_topmost_sequence ();
7742           emit_insn_after (seq, entry_of_function ());
7743           pop_topmost_sequence ();
7744         }
7745     }
7746
7747   /* Only 64bit target needs something special.  */
7748   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7749     {
7750       if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7751         std_expand_builtin_va_start (valist, nextarg);
7752       else
7753         {
7754           rtx va_r, next;
7755
7756           va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
7757           next = expand_binop (ptr_mode, add_optab,
7758                                cfun->machine->split_stack_varargs_pointer,
7759                                crtl->args.arg_offset_rtx,
7760                                NULL_RTX, 0, OPTAB_LIB_WIDEN);
7761           convert_move (va_r, next, 0);
7762         }
7763       return;
7764     }
7765
7766   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7767   f_fpr = DECL_CHAIN (f_gpr);
7768   f_ovf = DECL_CHAIN (f_fpr);
7769   f_sav = DECL_CHAIN (f_ovf);
7770
7771   valist = build_simple_mem_ref (valist);
7772   TREE_TYPE (valist) = TREE_TYPE (sysv_va_list_type_node);
7773   /* The following should be folded into the MEM_REF offset.  */
7774   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), unshare_expr (valist),
7775                 f_gpr, NULL_TREE);
7776   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
7777                 f_fpr, NULL_TREE);
7778   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
7779                 f_ovf, NULL_TREE);
7780   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
7781                 f_sav, NULL_TREE);
7782
7783   /* Count number of gp and fp argument registers used.  */
7784   words = crtl->args.info.words;
7785   n_gpr = crtl->args.info.regno;
7786   n_fpr = crtl->args.info.sse_regno;
7787
7788   if (cfun->va_list_gpr_size)
7789     {
7790       type = TREE_TYPE (gpr);
7791       t = build2 (MODIFY_EXPR, type,
7792                   gpr, build_int_cst (type, n_gpr * 8));
7793       TREE_SIDE_EFFECTS (t) = 1;
7794       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7795     }
7796
7797   if (TARGET_SSE && cfun->va_list_fpr_size)
7798     {
7799       type = TREE_TYPE (fpr);
7800       t = build2 (MODIFY_EXPR, type, fpr,
7801                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
7802       TREE_SIDE_EFFECTS (t) = 1;
7803       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7804     }
7805
7806   /* Find the overflow area.  */
7807   type = TREE_TYPE (ovf);
7808   if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7809     ovf_rtx = crtl->args.internal_arg_pointer;
7810   else
7811     ovf_rtx = cfun->machine->split_stack_varargs_pointer;
7812   t = make_tree (type, ovf_rtx);
7813   if (words != 0)
7814     t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
7815   t = build2 (MODIFY_EXPR, type, ovf, t);
7816   TREE_SIDE_EFFECTS (t) = 1;
7817   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7818
7819   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
7820     {
7821       /* Find the register save area.
7822          Prologue of the function save it right above stack frame.  */
7823       type = TREE_TYPE (sav);
7824       t = make_tree (type, frame_pointer_rtx);
7825       if (!ix86_varargs_gpr_size)
7826         t = fold_build_pointer_plus_hwi (t, -8 * X86_64_REGPARM_MAX);
7827       t = build2 (MODIFY_EXPR, type, sav, t);
7828       TREE_SIDE_EFFECTS (t) = 1;
7829       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7830     }
7831 }
7832
7833 /* Implement va_arg.  */
7834
7835 static tree
7836 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7837                       gimple_seq *post_p)
7838 {
7839   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
7840   tree f_gpr, f_fpr, f_ovf, f_sav;
7841   tree gpr, fpr, ovf, sav, t;
7842   int size, rsize;
7843   tree lab_false, lab_over = NULL_TREE;
7844   tree addr, t2;
7845   rtx container;
7846   int indirect_p = 0;
7847   tree ptrtype;
7848   enum machine_mode nat_mode;
7849   unsigned int arg_boundary;
7850
7851   /* Only 64bit target needs something special.  */
7852   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7853     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7854
7855   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7856   f_fpr = DECL_CHAIN (f_gpr);
7857   f_ovf = DECL_CHAIN (f_fpr);
7858   f_sav = DECL_CHAIN (f_ovf);
7859
7860   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
7861                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
7862   valist = build_va_arg_indirect_ref (valist);
7863   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7864   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7865   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7866
7867   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
7868   if (indirect_p)
7869     type = build_pointer_type (type);
7870   size = int_size_in_bytes (type);
7871   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7872
7873   nat_mode = type_natural_mode (type, NULL);
7874   switch (nat_mode)
7875     {
7876     case V8SFmode:
7877     case V8SImode:
7878     case V32QImode:
7879     case V16HImode:
7880     case V4DFmode:
7881     case V4DImode:
7882       /* Unnamed 256bit vector mode parameters are passed on stack.  */
7883       if (!TARGET_64BIT_MS_ABI)
7884         {
7885           container = NULL;
7886           break;
7887         }
7888
7889     default:
7890       container = construct_container (nat_mode, TYPE_MODE (type),
7891                                        type, 0, X86_64_REGPARM_MAX,
7892                                        X86_64_SSE_REGPARM_MAX, intreg,
7893                                        0);
7894       break;
7895     }
7896
7897   /* Pull the value out of the saved registers.  */
7898
7899   addr = create_tmp_var (ptr_type_node, "addr");
7900
7901   if (container)
7902     {
7903       int needed_intregs, needed_sseregs;
7904       bool need_temp;
7905       tree int_addr, sse_addr;
7906
7907       lab_false = create_artificial_label (UNKNOWN_LOCATION);
7908       lab_over = create_artificial_label (UNKNOWN_LOCATION);
7909
7910       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
7911
7912       need_temp = (!REG_P (container)
7913                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
7914                        || TYPE_ALIGN (type) > 128));
7915
7916       /* In case we are passing structure, verify that it is consecutive block
7917          on the register save area.  If not we need to do moves.  */
7918       if (!need_temp && !REG_P (container))
7919         {
7920           /* Verify that all registers are strictly consecutive  */
7921           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
7922             {
7923               int i;
7924
7925               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7926                 {
7927                   rtx slot = XVECEXP (container, 0, i);
7928                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
7929                       || INTVAL (XEXP (slot, 1)) != i * 16)
7930                     need_temp = 1;
7931                 }
7932             }
7933           else
7934             {
7935               int i;
7936
7937               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7938                 {
7939                   rtx slot = XVECEXP (container, 0, i);
7940                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
7941                       || INTVAL (XEXP (slot, 1)) != i * 8)
7942                     need_temp = 1;
7943                 }
7944             }
7945         }
7946       if (!need_temp)
7947         {
7948           int_addr = addr;
7949           sse_addr = addr;
7950         }
7951       else
7952         {
7953           int_addr = create_tmp_var (ptr_type_node, "int_addr");
7954           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
7955         }
7956
7957       /* First ensure that we fit completely in registers.  */
7958       if (needed_intregs)
7959         {
7960           t = build_int_cst (TREE_TYPE (gpr),
7961                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
7962           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
7963           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7964           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7965           gimplify_and_add (t, pre_p);
7966         }
7967       if (needed_sseregs)
7968         {
7969           t = build_int_cst (TREE_TYPE (fpr),
7970                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
7971                              + X86_64_REGPARM_MAX * 8);
7972           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
7973           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7974           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7975           gimplify_and_add (t, pre_p);
7976         }
7977
7978       /* Compute index to start of area used for integer regs.  */
7979       if (needed_intregs)
7980         {
7981           /* int_addr = gpr + sav; */
7982           t = fold_build_pointer_plus (sav, gpr);
7983           gimplify_assign (int_addr, t, pre_p);
7984         }
7985       if (needed_sseregs)
7986         {
7987           /* sse_addr = fpr + sav; */
7988           t = fold_build_pointer_plus (sav, fpr);
7989           gimplify_assign (sse_addr, t, pre_p);
7990         }
7991       if (need_temp)
7992         {
7993           int i, prev_size = 0;
7994           tree temp = create_tmp_var (type, "va_arg_tmp");
7995
7996           /* addr = &temp; */
7997           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
7998           gimplify_assign (addr, t, pre_p);
7999
8000           for (i = 0; i < XVECLEN (container, 0); i++)
8001             {
8002               rtx slot = XVECEXP (container, 0, i);
8003               rtx reg = XEXP (slot, 0);
8004               enum machine_mode mode = GET_MODE (reg);
8005               tree piece_type;
8006               tree addr_type;
8007               tree daddr_type;
8008               tree src_addr, src;
8009               int src_offset;
8010               tree dest_addr, dest;
8011               int cur_size = GET_MODE_SIZE (mode);
8012
8013               gcc_assert (prev_size <= INTVAL (XEXP (slot, 1)));
8014               prev_size = INTVAL (XEXP (slot, 1));
8015               if (prev_size + cur_size > size)
8016                 {
8017                   cur_size = size - prev_size;
8018                   mode = mode_for_size (cur_size * BITS_PER_UNIT, MODE_INT, 1);
8019                   if (mode == BLKmode)
8020                     mode = QImode;
8021                 }
8022               piece_type = lang_hooks.types.type_for_mode (mode, 1);
8023               if (mode == GET_MODE (reg))
8024                 addr_type = build_pointer_type (piece_type);
8025               else
8026                 addr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8027                                                          true);
8028               daddr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8029                                                         true);
8030
8031               if (SSE_REGNO_P (REGNO (reg)))
8032                 {
8033                   src_addr = sse_addr;
8034                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
8035                 }
8036               else
8037                 {
8038                   src_addr = int_addr;
8039                   src_offset = REGNO (reg) * 8;
8040                 }
8041               src_addr = fold_convert (addr_type, src_addr);
8042               src_addr = fold_build_pointer_plus_hwi (src_addr, src_offset);
8043
8044               dest_addr = fold_convert (daddr_type, addr);
8045               dest_addr = fold_build_pointer_plus_hwi (dest_addr, prev_size);
8046               if (cur_size == GET_MODE_SIZE (mode))
8047                 {
8048                   src = build_va_arg_indirect_ref (src_addr);
8049                   dest = build_va_arg_indirect_ref (dest_addr);
8050
8051                   gimplify_assign (dest, src, pre_p);
8052                 }
8053               else
8054                 {
8055                   tree copy
8056                     = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
8057                                        3, dest_addr, src_addr,
8058                                        size_int (cur_size));
8059                   gimplify_and_add (copy, pre_p);
8060                 }
8061               prev_size += cur_size;
8062             }
8063         }
8064
8065       if (needed_intregs)
8066         {
8067           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
8068                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
8069           gimplify_assign (gpr, t, pre_p);
8070         }
8071
8072       if (needed_sseregs)
8073         {
8074           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
8075                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
8076           gimplify_assign (fpr, t, pre_p);
8077         }
8078
8079       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
8080
8081       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
8082     }
8083
8084   /* ... otherwise out of the overflow area.  */
8085
8086   /* When we align parameter on stack for caller, if the parameter
8087      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
8088      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
8089      here with caller.  */
8090   arg_boundary = ix86_function_arg_boundary (VOIDmode, type);
8091   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
8092     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
8093
8094   /* Care for on-stack alignment if needed.  */
8095   if (arg_boundary <= 64 || size == 0)
8096     t = ovf;
8097  else
8098     {
8099       HOST_WIDE_INT align = arg_boundary / 8;
8100       t = fold_build_pointer_plus_hwi (ovf, align - 1);
8101       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
8102                   build_int_cst (TREE_TYPE (t), -align));
8103     }
8104
8105   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
8106   gimplify_assign (addr, t, pre_p);
8107
8108   t = fold_build_pointer_plus_hwi (t, rsize * UNITS_PER_WORD);
8109   gimplify_assign (unshare_expr (ovf), t, pre_p);
8110
8111   if (container)
8112     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
8113
8114   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
8115   addr = fold_convert (ptrtype, addr);
8116
8117   if (indirect_p)
8118     addr = build_va_arg_indirect_ref (addr);
8119   return build_va_arg_indirect_ref (addr);
8120 }
8121 \f
8122 /* Return true if OPNUM's MEM should be matched
8123    in movabs* patterns.  */
8124
8125 bool
8126 ix86_check_movabs (rtx insn, int opnum)
8127 {
8128   rtx set, mem;
8129
8130   set = PATTERN (insn);
8131   if (GET_CODE (set) == PARALLEL)
8132     set = XVECEXP (set, 0, 0);
8133   gcc_assert (GET_CODE (set) == SET);
8134   mem = XEXP (set, opnum);
8135   while (GET_CODE (mem) == SUBREG)
8136     mem = SUBREG_REG (mem);
8137   gcc_assert (MEM_P (mem));
8138   return volatile_ok || !MEM_VOLATILE_P (mem);
8139 }
8140 \f
8141 /* Initialize the table of extra 80387 mathematical constants.  */
8142
8143 static void
8144 init_ext_80387_constants (void)
8145 {
8146   static const char * cst[5] =
8147   {
8148     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
8149     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
8150     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
8151     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
8152     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
8153   };
8154   int i;
8155
8156   for (i = 0; i < 5; i++)
8157     {
8158       real_from_string (&ext_80387_constants_table[i], cst[i]);
8159       /* Ensure each constant is rounded to XFmode precision.  */
8160       real_convert (&ext_80387_constants_table[i],
8161                     XFmode, &ext_80387_constants_table[i]);
8162     }
8163
8164   ext_80387_constants_init = 1;
8165 }
8166
8167 /* Return non-zero if the constant is something that
8168    can be loaded with a special instruction.  */
8169
8170 int
8171 standard_80387_constant_p (rtx x)
8172 {
8173   enum machine_mode mode = GET_MODE (x);
8174
8175   REAL_VALUE_TYPE r;
8176
8177   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
8178     return -1;
8179
8180   if (x == CONST0_RTX (mode))
8181     return 1;
8182   if (x == CONST1_RTX (mode))
8183     return 2;
8184
8185   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8186
8187   /* For XFmode constants, try to find a special 80387 instruction when
8188      optimizing for size or on those CPUs that benefit from them.  */
8189   if (mode == XFmode
8190       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
8191     {
8192       int i;
8193
8194       if (! ext_80387_constants_init)
8195         init_ext_80387_constants ();
8196
8197       for (i = 0; i < 5; i++)
8198         if (real_identical (&r, &ext_80387_constants_table[i]))
8199           return i + 3;
8200     }
8201
8202   /* Load of the constant -0.0 or -1.0 will be split as
8203      fldz;fchs or fld1;fchs sequence.  */
8204   if (real_isnegzero (&r))
8205     return 8;
8206   if (real_identical (&r, &dconstm1))
8207     return 9;
8208
8209   return 0;
8210 }
8211
8212 /* Return the opcode of the special instruction to be used to load
8213    the constant X.  */
8214
8215 const char *
8216 standard_80387_constant_opcode (rtx x)
8217 {
8218   switch (standard_80387_constant_p (x))
8219     {
8220     case 1:
8221       return "fldz";
8222     case 2:
8223       return "fld1";
8224     case 3:
8225       return "fldlg2";
8226     case 4:
8227       return "fldln2";
8228     case 5:
8229       return "fldl2e";
8230     case 6:
8231       return "fldl2t";
8232     case 7:
8233       return "fldpi";
8234     case 8:
8235     case 9:
8236       return "#";
8237     default:
8238       gcc_unreachable ();
8239     }
8240 }
8241
8242 /* Return the CONST_DOUBLE representing the 80387 constant that is
8243    loaded by the specified special instruction.  The argument IDX
8244    matches the return value from standard_80387_constant_p.  */
8245
8246 rtx
8247 standard_80387_constant_rtx (int idx)
8248 {
8249   int i;
8250
8251   if (! ext_80387_constants_init)
8252     init_ext_80387_constants ();
8253
8254   switch (idx)
8255     {
8256     case 3:
8257     case 4:
8258     case 5:
8259     case 6:
8260     case 7:
8261       i = idx - 3;
8262       break;
8263
8264     default:
8265       gcc_unreachable ();
8266     }
8267
8268   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
8269                                        XFmode);
8270 }
8271
8272 /* Return 1 if X is all 0s and 2 if x is all 1s
8273    in supported SSE/AVX vector mode.  */
8274
8275 int
8276 standard_sse_constant_p (rtx x)
8277 {
8278   enum machine_mode mode = GET_MODE (x);
8279
8280   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
8281     return 1;
8282   if (vector_all_ones_operand (x, mode))
8283     switch (mode)
8284       {
8285       case V16QImode:
8286       case V8HImode:
8287       case V4SImode:
8288       case V2DImode:
8289         if (TARGET_SSE2)
8290           return 2;
8291       case V32QImode:
8292       case V16HImode:
8293       case V8SImode:
8294       case V4DImode:
8295         if (TARGET_AVX2)
8296           return 2;
8297       default:
8298         break;
8299       }
8300
8301   return 0;
8302 }
8303
8304 /* Return the opcode of the special instruction to be used to load
8305    the constant X.  */
8306
8307 const char *
8308 standard_sse_constant_opcode (rtx insn, rtx x)
8309 {
8310   switch (standard_sse_constant_p (x))
8311     {
8312     case 1:
8313       switch (get_attr_mode (insn))
8314         {
8315         case MODE_TI:
8316           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8317             return "%vpxor\t%0, %d0";
8318         case MODE_V2DF:
8319           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8320             return "%vxorpd\t%0, %d0";
8321         case MODE_V4SF:
8322           return "%vxorps\t%0, %d0";
8323
8324         case MODE_OI:
8325           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8326             return "vpxor\t%x0, %x0, %x0";
8327         case MODE_V4DF:
8328           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8329             return "vxorpd\t%x0, %x0, %x0";
8330         case MODE_V8SF:
8331           return "vxorps\t%x0, %x0, %x0";
8332
8333         default:
8334           break;
8335         }
8336
8337     case 2:
8338       if (TARGET_AVX)
8339         return "vpcmpeqd\t%0, %0, %0";
8340       else
8341         return "pcmpeqd\t%0, %0";
8342
8343     default:
8344       break;
8345     }
8346   gcc_unreachable ();
8347 }
8348
8349 /* Returns true if OP contains a symbol reference */
8350
8351 bool
8352 symbolic_reference_mentioned_p (rtx op)
8353 {
8354   const char *fmt;
8355   int i;
8356
8357   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
8358     return true;
8359
8360   fmt = GET_RTX_FORMAT (GET_CODE (op));
8361   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
8362     {
8363       if (fmt[i] == 'E')
8364         {
8365           int j;
8366
8367           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
8368             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
8369               return true;
8370         }
8371
8372       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
8373         return true;
8374     }
8375
8376   return false;
8377 }
8378
8379 /* Return true if it is appropriate to emit `ret' instructions in the
8380    body of a function.  Do this only if the epilogue is simple, needing a
8381    couple of insns.  Prior to reloading, we can't tell how many registers
8382    must be saved, so return false then.  Return false if there is no frame
8383    marker to de-allocate.  */
8384
8385 bool
8386 ix86_can_use_return_insn_p (void)
8387 {
8388   struct ix86_frame frame;
8389
8390   if (! reload_completed || frame_pointer_needed)
8391     return 0;
8392
8393   /* Don't allow more than 32k pop, since that's all we can do
8394      with one instruction.  */
8395   if (crtl->args.pops_args && crtl->args.size >= 32768)
8396     return 0;
8397
8398   ix86_compute_frame_layout (&frame);
8399   return (frame.stack_pointer_offset == UNITS_PER_WORD
8400           && (frame.nregs + frame.nsseregs) == 0);
8401 }
8402 \f
8403 /* Value should be nonzero if functions must have frame pointers.
8404    Zero means the frame pointer need not be set up (and parms may
8405    be accessed via the stack pointer) in functions that seem suitable.  */
8406
8407 static bool
8408 ix86_frame_pointer_required (void)
8409 {
8410   /* If we accessed previous frames, then the generated code expects
8411      to be able to access the saved ebp value in our frame.  */
8412   if (cfun->machine->accesses_prev_frame)
8413     return true;
8414
8415   /* Several x86 os'es need a frame pointer for other reasons,
8416      usually pertaining to setjmp.  */
8417   if (SUBTARGET_FRAME_POINTER_REQUIRED)
8418     return true;
8419
8420   /* For older 32-bit runtimes setjmp requires valid frame-pointer.  */
8421   if (TARGET_32BIT_MS_ABI && cfun->calls_setjmp)
8422     return true;
8423
8424   /* Win64 SEH, very large frames need a frame-pointer as maximum stack
8425      allocation is 4GB.  */
8426   if (TARGET_64BIT_MS_ABI && get_frame_size () > SEH_MAX_FRAME_SIZE)
8427     return true;
8428
8429   /* In ix86_option_override_internal, TARGET_OMIT_LEAF_FRAME_POINTER
8430      turns off the frame pointer by default.  Turn it back on now if
8431      we've not got a leaf function.  */
8432   if (TARGET_OMIT_LEAF_FRAME_POINTER
8433       && (!current_function_is_leaf
8434           || ix86_current_function_calls_tls_descriptor))
8435     return true;
8436
8437   if (crtl->profile && !flag_fentry)
8438     return true;
8439
8440   return false;
8441 }
8442
8443 /* Record that the current function accesses previous call frames.  */
8444
8445 void
8446 ix86_setup_frame_addresses (void)
8447 {
8448   cfun->machine->accesses_prev_frame = 1;
8449 }
8450 \f
8451 #ifndef USE_HIDDEN_LINKONCE
8452 # if defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)
8453 #  define USE_HIDDEN_LINKONCE 1
8454 # else
8455 #  define USE_HIDDEN_LINKONCE 0
8456 # endif
8457 #endif
8458
8459 static int pic_labels_used;
8460
8461 /* Fills in the label name that should be used for a pc thunk for
8462    the given register.  */
8463
8464 static void
8465 get_pc_thunk_name (char name[32], unsigned int regno)
8466 {
8467   gcc_assert (!TARGET_64BIT);
8468
8469   if (USE_HIDDEN_LINKONCE)
8470     sprintf (name, "__x86.get_pc_thunk.%s", reg_names[regno]);
8471   else
8472     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
8473 }
8474
8475
8476 /* This function generates code for -fpic that loads %ebx with
8477    the return address of the caller and then returns.  */
8478
8479 static void
8480 ix86_code_end (void)
8481 {
8482   rtx xops[2];
8483   int regno;
8484
8485   for (regno = AX_REG; regno <= SP_REG; regno++)
8486     {
8487       char name[32];
8488       tree decl;
8489
8490       if (!(pic_labels_used & (1 << regno)))
8491         continue;
8492
8493       get_pc_thunk_name (name, regno);
8494
8495       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
8496                          get_identifier (name),
8497                          build_function_type_list (void_type_node, NULL_TREE));
8498       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
8499                                        NULL_TREE, void_type_node);
8500       TREE_PUBLIC (decl) = 1;
8501       TREE_STATIC (decl) = 1;
8502
8503 #if TARGET_MACHO
8504       if (TARGET_MACHO)
8505         {
8506           switch_to_section (darwin_sections[text_coal_section]);
8507           fputs ("\t.weak_definition\t", asm_out_file);
8508           assemble_name (asm_out_file, name);
8509           fputs ("\n\t.private_extern\t", asm_out_file);
8510           assemble_name (asm_out_file, name);
8511           putc ('\n', asm_out_file);
8512           ASM_OUTPUT_LABEL (asm_out_file, name);
8513           DECL_WEAK (decl) = 1;
8514         }
8515       else
8516 #endif
8517       if (USE_HIDDEN_LINKONCE)
8518         {
8519           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
8520
8521           targetm.asm_out.unique_section (decl, 0);
8522           switch_to_section (get_named_section (decl, NULL, 0));
8523
8524           targetm.asm_out.globalize_label (asm_out_file, name);
8525           fputs ("\t.hidden\t", asm_out_file);
8526           assemble_name (asm_out_file, name);
8527           putc ('\n', asm_out_file);
8528           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
8529         }
8530       else
8531         {
8532           switch_to_section (text_section);
8533           ASM_OUTPUT_LABEL (asm_out_file, name);
8534         }
8535
8536       DECL_INITIAL (decl) = make_node (BLOCK);
8537       current_function_decl = decl;
8538       init_function_start (decl);
8539       first_function_block_is_cold = false;
8540       /* Make sure unwind info is emitted for the thunk if needed.  */
8541       final_start_function (emit_barrier (), asm_out_file, 1);
8542
8543       /* Pad stack IP move with 4 instructions (two NOPs count
8544          as one instruction).  */
8545       if (TARGET_PAD_SHORT_FUNCTION)
8546         {
8547           int i = 8;
8548
8549           while (i--)
8550             fputs ("\tnop\n", asm_out_file);
8551         }
8552
8553       xops[0] = gen_rtx_REG (Pmode, regno);
8554       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8555       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
8556       fputs ("\tret\n", asm_out_file);
8557       final_end_function ();
8558       init_insn_lengths ();
8559       free_after_compilation (cfun);
8560       set_cfun (NULL);
8561       current_function_decl = NULL;
8562     }
8563
8564   if (flag_split_stack)
8565     file_end_indicate_split_stack ();
8566 }
8567
8568 /* Emit code for the SET_GOT patterns.  */
8569
8570 const char *
8571 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
8572 {
8573   rtx xops[3];
8574
8575   xops[0] = dest;
8576
8577   if (TARGET_VXWORKS_RTP && flag_pic)
8578     {
8579       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
8580       xops[2] = gen_rtx_MEM (Pmode,
8581                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
8582       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
8583
8584       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
8585          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
8586          an unadorned address.  */
8587       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
8588       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
8589       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
8590       return "";
8591     }
8592
8593   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
8594
8595   if (!flag_pic)
8596     {
8597       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
8598
8599       output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
8600
8601 #if TARGET_MACHO
8602       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8603          is what will be referenced by the Mach-O PIC subsystem.  */
8604       if (!label)
8605         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8606 #endif
8607
8608       targetm.asm_out.internal_label (asm_out_file, "L",
8609                                       CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
8610     }
8611   else
8612     {
8613       char name[32];
8614       get_pc_thunk_name (name, REGNO (dest));
8615       pic_labels_used |= 1 << REGNO (dest);
8616
8617       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
8618       xops[2] = gen_rtx_MEM (QImode, xops[2]);
8619       output_asm_insn ("call\t%X2", xops);
8620       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8621          is what will be referenced by the Mach-O PIC subsystem.  */
8622 #if TARGET_MACHO
8623       if (!label)
8624         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8625       else
8626         targetm.asm_out.internal_label (asm_out_file, "L",
8627                                            CODE_LABEL_NUMBER (label));
8628 #endif
8629     }
8630
8631   if (!TARGET_MACHO)
8632     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
8633
8634   return "";
8635 }
8636
8637 /* Generate an "push" pattern for input ARG.  */
8638
8639 static rtx
8640 gen_push (rtx arg)
8641 {
8642   struct machine_function *m = cfun->machine;
8643
8644   if (m->fs.cfa_reg == stack_pointer_rtx)
8645     m->fs.cfa_offset += UNITS_PER_WORD;
8646   m->fs.sp_offset += UNITS_PER_WORD;
8647
8648   return gen_rtx_SET (VOIDmode,
8649                       gen_rtx_MEM (Pmode,
8650                                    gen_rtx_PRE_DEC (Pmode,
8651                                                     stack_pointer_rtx)),
8652                       arg);
8653 }
8654
8655 /* Generate an "pop" pattern for input ARG.  */
8656
8657 static rtx
8658 gen_pop (rtx arg)
8659 {
8660   return gen_rtx_SET (VOIDmode,
8661                       arg,
8662                       gen_rtx_MEM (Pmode,
8663                                    gen_rtx_POST_INC (Pmode,
8664                                                      stack_pointer_rtx)));
8665 }
8666
8667 /* Return >= 0 if there is an unused call-clobbered register available
8668    for the entire function.  */
8669
8670 static unsigned int
8671 ix86_select_alt_pic_regnum (void)
8672 {
8673   if (current_function_is_leaf
8674       && !crtl->profile
8675       && !ix86_current_function_calls_tls_descriptor)
8676     {
8677       int i, drap;
8678       /* Can't use the same register for both PIC and DRAP.  */
8679       if (crtl->drap_reg)
8680         drap = REGNO (crtl->drap_reg);
8681       else
8682         drap = -1;
8683       for (i = 2; i >= 0; --i)
8684         if (i != drap && !df_regs_ever_live_p (i))
8685           return i;
8686     }
8687
8688   return INVALID_REGNUM;
8689 }
8690
8691 /* Return TRUE if we need to save REGNO.  */
8692
8693 static bool
8694 ix86_save_reg (unsigned int regno, bool maybe_eh_return)
8695 {
8696   if (pic_offset_table_rtx
8697       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
8698       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8699           || crtl->profile
8700           || crtl->calls_eh_return
8701           || crtl->uses_const_pool))
8702     return ix86_select_alt_pic_regnum () == INVALID_REGNUM;
8703
8704   if (crtl->calls_eh_return && maybe_eh_return)
8705     {
8706       unsigned i;
8707       for (i = 0; ; i++)
8708         {
8709           unsigned test = EH_RETURN_DATA_REGNO (i);
8710           if (test == INVALID_REGNUM)
8711             break;
8712           if (test == regno)
8713             return true;
8714         }
8715     }
8716
8717   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
8718     return true;
8719
8720   return (df_regs_ever_live_p (regno)
8721           && !call_used_regs[regno]
8722           && !fixed_regs[regno]
8723           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
8724 }
8725
8726 /* Return number of saved general prupose registers.  */
8727
8728 static int
8729 ix86_nsaved_regs (void)
8730 {
8731   int nregs = 0;
8732   int regno;
8733
8734   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8735     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8736       nregs ++;
8737   return nregs;
8738 }
8739
8740 /* Return number of saved SSE registrers.  */
8741
8742 static int
8743 ix86_nsaved_sseregs (void)
8744 {
8745   int nregs = 0;
8746   int regno;
8747
8748   if (!TARGET_64BIT_MS_ABI)
8749     return 0;
8750   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8751     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8752       nregs ++;
8753   return nregs;
8754 }
8755
8756 /* Given FROM and TO register numbers, say whether this elimination is
8757    allowed.  If stack alignment is needed, we can only replace argument
8758    pointer with hard frame pointer, or replace frame pointer with stack
8759    pointer.  Otherwise, frame pointer elimination is automatically
8760    handled and all other eliminations are valid.  */
8761
8762 static bool
8763 ix86_can_eliminate (const int from, const int to)
8764 {
8765   if (stack_realign_fp)
8766     return ((from == ARG_POINTER_REGNUM
8767              && to == HARD_FRAME_POINTER_REGNUM)
8768             || (from == FRAME_POINTER_REGNUM
8769                 && to == STACK_POINTER_REGNUM));
8770   else
8771     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
8772 }
8773
8774 /* Return the offset between two registers, one to be eliminated, and the other
8775    its replacement, at the start of a routine.  */
8776
8777 HOST_WIDE_INT
8778 ix86_initial_elimination_offset (int from, int to)
8779 {
8780   struct ix86_frame frame;
8781   ix86_compute_frame_layout (&frame);
8782
8783   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
8784     return frame.hard_frame_pointer_offset;
8785   else if (from == FRAME_POINTER_REGNUM
8786            && to == HARD_FRAME_POINTER_REGNUM)
8787     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
8788   else
8789     {
8790       gcc_assert (to == STACK_POINTER_REGNUM);
8791
8792       if (from == ARG_POINTER_REGNUM)
8793         return frame.stack_pointer_offset;
8794
8795       gcc_assert (from == FRAME_POINTER_REGNUM);
8796       return frame.stack_pointer_offset - frame.frame_pointer_offset;
8797     }
8798 }
8799
8800 /* In a dynamically-aligned function, we can't know the offset from
8801    stack pointer to frame pointer, so we must ensure that setjmp
8802    eliminates fp against the hard fp (%ebp) rather than trying to
8803    index from %esp up to the top of the frame across a gap that is
8804    of unknown (at compile-time) size.  */
8805 static rtx
8806 ix86_builtin_setjmp_frame_value (void)
8807 {
8808   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
8809 }
8810
8811 /* When using -fsplit-stack, the allocation routines set a field in
8812    the TCB to the bottom of the stack plus this much space, measured
8813    in bytes.  */
8814
8815 #define SPLIT_STACK_AVAILABLE 256
8816
8817 /* Fill structure ix86_frame about frame of currently computed function.  */
8818
8819 static void
8820 ix86_compute_frame_layout (struct ix86_frame *frame)
8821 {
8822   unsigned int stack_alignment_needed;
8823   HOST_WIDE_INT offset;
8824   unsigned int preferred_alignment;
8825   HOST_WIDE_INT size = get_frame_size ();
8826   HOST_WIDE_INT to_allocate;
8827
8828   frame->nregs = ix86_nsaved_regs ();
8829   frame->nsseregs = ix86_nsaved_sseregs ();
8830
8831   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
8832   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
8833
8834   /* 64-bit MS ABI seem to require stack alignment to be always 16 except for
8835      function prologues and leaf.  */
8836   if ((TARGET_64BIT_MS_ABI && preferred_alignment < 16)
8837       && (!current_function_is_leaf || cfun->calls_alloca != 0
8838           || ix86_current_function_calls_tls_descriptor))
8839     {
8840       preferred_alignment = 16;
8841       stack_alignment_needed = 16;
8842       crtl->preferred_stack_boundary = 128;
8843       crtl->stack_alignment_needed = 128;
8844     }
8845
8846   gcc_assert (!size || stack_alignment_needed);
8847   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
8848   gcc_assert (preferred_alignment <= stack_alignment_needed);
8849
8850   /* For SEH we have to limit the amount of code movement into the prologue.
8851      At present we do this via a BLOCKAGE, at which point there's very little
8852      scheduling that can be done, which means that there's very little point
8853      in doing anything except PUSHs.  */
8854   if (TARGET_SEH)
8855     cfun->machine->use_fast_prologue_epilogue = false;
8856
8857   /* During reload iteration the amount of registers saved can change.
8858      Recompute the value as needed.  Do not recompute when amount of registers
8859      didn't change as reload does multiple calls to the function and does not
8860      expect the decision to change within single iteration.  */
8861   else if (!optimize_function_for_size_p (cfun)
8862            && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
8863     {
8864       int count = frame->nregs;
8865       struct cgraph_node *node = cgraph_get_node (current_function_decl);
8866
8867       cfun->machine->use_fast_prologue_epilogue_nregs = count;
8868
8869       /* The fast prologue uses move instead of push to save registers.  This
8870          is significantly longer, but also executes faster as modern hardware
8871          can execute the moves in parallel, but can't do that for push/pop.
8872
8873          Be careful about choosing what prologue to emit:  When function takes
8874          many instructions to execute we may use slow version as well as in
8875          case function is known to be outside hot spot (this is known with
8876          feedback only).  Weight the size of function by number of registers
8877          to save as it is cheap to use one or two push instructions but very
8878          slow to use many of them.  */
8879       if (count)
8880         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
8881       if (node->frequency < NODE_FREQUENCY_NORMAL
8882           || (flag_branch_probabilities
8883               && node->frequency < NODE_FREQUENCY_HOT))
8884         cfun->machine->use_fast_prologue_epilogue = false;
8885       else
8886         cfun->machine->use_fast_prologue_epilogue
8887            = !expensive_function_p (count);
8888     }
8889
8890   frame->save_regs_using_mov
8891     = (TARGET_PROLOGUE_USING_MOVE && cfun->machine->use_fast_prologue_epilogue
8892        /* If static stack checking is enabled and done with probes,
8893           the registers need to be saved before allocating the frame.  */
8894        && flag_stack_check != STATIC_BUILTIN_STACK_CHECK);
8895
8896   /* Skip return address.  */
8897   offset = UNITS_PER_WORD;
8898
8899   /* Skip pushed static chain.  */
8900   if (ix86_static_chain_on_stack)
8901     offset += UNITS_PER_WORD;
8902
8903   /* Skip saved base pointer.  */
8904   if (frame_pointer_needed)
8905     offset += UNITS_PER_WORD;
8906   frame->hfp_save_offset = offset;
8907
8908   /* The traditional frame pointer location is at the top of the frame.  */
8909   frame->hard_frame_pointer_offset = offset;
8910
8911   /* Register save area */
8912   offset += frame->nregs * UNITS_PER_WORD;
8913   frame->reg_save_offset = offset;
8914
8915   /* On SEH target, registers are pushed just before the frame pointer
8916      location.  */
8917   if (TARGET_SEH)
8918     frame->hard_frame_pointer_offset = offset;
8919
8920   /* Align and set SSE register save area.  */
8921   if (frame->nsseregs)
8922     {
8923       /* The only ABI that has saved SSE registers (Win64) also has a
8924          16-byte aligned default stack, and thus we don't need to be
8925          within the re-aligned local stack frame to save them.  */
8926       gcc_assert (INCOMING_STACK_BOUNDARY >= 128);
8927       offset = (offset + 16 - 1) & -16;
8928       offset += frame->nsseregs * 16;
8929     }
8930   frame->sse_reg_save_offset = offset;
8931
8932   /* The re-aligned stack starts here.  Values before this point are not
8933      directly comparable with values below this point.  In order to make
8934      sure that no value happens to be the same before and after, force
8935      the alignment computation below to add a non-zero value.  */
8936   if (stack_realign_fp)
8937     offset = (offset + stack_alignment_needed) & -stack_alignment_needed;
8938
8939   /* Va-arg area */
8940   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
8941   offset += frame->va_arg_size;
8942
8943   /* Align start of frame for local function.  */
8944   if (stack_realign_fp
8945       || offset != frame->sse_reg_save_offset
8946       || size != 0
8947       || !current_function_is_leaf
8948       || cfun->calls_alloca
8949       || ix86_current_function_calls_tls_descriptor)
8950     offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed;
8951
8952   /* Frame pointer points here.  */
8953   frame->frame_pointer_offset = offset;
8954
8955   offset += size;
8956
8957   /* Add outgoing arguments area.  Can be skipped if we eliminated
8958      all the function calls as dead code.
8959      Skipping is however impossible when function calls alloca.  Alloca
8960      expander assumes that last crtl->outgoing_args_size
8961      of stack frame are unused.  */
8962   if (ACCUMULATE_OUTGOING_ARGS
8963       && (!current_function_is_leaf || cfun->calls_alloca
8964           || ix86_current_function_calls_tls_descriptor))
8965     {
8966       offset += crtl->outgoing_args_size;
8967       frame->outgoing_arguments_size = crtl->outgoing_args_size;
8968     }
8969   else
8970     frame->outgoing_arguments_size = 0;
8971
8972   /* Align stack boundary.  Only needed if we're calling another function
8973      or using alloca.  */
8974   if (!current_function_is_leaf || cfun->calls_alloca
8975       || ix86_current_function_calls_tls_descriptor)
8976     offset = (offset + preferred_alignment - 1) & -preferred_alignment;
8977
8978   /* We've reached end of stack frame.  */
8979   frame->stack_pointer_offset = offset;
8980
8981   /* Size prologue needs to allocate.  */
8982   to_allocate = offset - frame->sse_reg_save_offset;
8983
8984   if ((!to_allocate && frame->nregs <= 1)
8985       || (TARGET_64BIT && to_allocate >= (HOST_WIDE_INT) 0x80000000))
8986     frame->save_regs_using_mov = false;
8987
8988   if (ix86_using_red_zone ()
8989       && current_function_sp_is_unchanging
8990       && current_function_is_leaf
8991       && !ix86_current_function_calls_tls_descriptor)
8992     {
8993       frame->red_zone_size = to_allocate;
8994       if (frame->save_regs_using_mov)
8995         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
8996       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
8997         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
8998     }
8999   else
9000     frame->red_zone_size = 0;
9001   frame->stack_pointer_offset -= frame->red_zone_size;
9002
9003   /* The SEH frame pointer location is near the bottom of the frame.
9004      This is enforced by the fact that the difference between the
9005      stack pointer and the frame pointer is limited to 240 bytes in
9006      the unwind data structure.  */
9007   if (TARGET_SEH)
9008     {
9009       HOST_WIDE_INT diff;
9010
9011       /* If we can leave the frame pointer where it is, do so.  Also, returns
9012          the establisher frame for __builtin_frame_address (0).  */
9013       diff = frame->stack_pointer_offset - frame->hard_frame_pointer_offset;
9014       if (diff <= SEH_MAX_FRAME_SIZE
9015           && (diff > 240 || (diff & 15) != 0)
9016           && !crtl->accesses_prior_frames)
9017         {
9018           /* Ideally we'd determine what portion of the local stack frame
9019              (within the constraint of the lowest 240) is most heavily used.
9020              But without that complication, simply bias the frame pointer
9021              by 128 bytes so as to maximize the amount of the local stack
9022              frame that is addressable with 8-bit offsets.  */
9023           frame->hard_frame_pointer_offset = frame->stack_pointer_offset - 128;
9024         }
9025     }
9026 }
9027
9028 /* This is semi-inlined memory_address_length, but simplified
9029    since we know that we're always dealing with reg+offset, and
9030    to avoid having to create and discard all that rtl.  */
9031
9032 static inline int
9033 choose_baseaddr_len (unsigned int regno, HOST_WIDE_INT offset)
9034 {
9035   int len = 4;
9036
9037   if (offset == 0)
9038     {
9039       /* EBP and R13 cannot be encoded without an offset.  */
9040       len = (regno == BP_REG || regno == R13_REG);
9041     }
9042   else if (IN_RANGE (offset, -128, 127))
9043     len = 1;
9044
9045   /* ESP and R12 must be encoded with a SIB byte.  */
9046   if (regno == SP_REG || regno == R12_REG)
9047     len++;
9048
9049   return len;
9050 }
9051
9052 /* Return an RTX that points to CFA_OFFSET within the stack frame.
9053    The valid base registers are taken from CFUN->MACHINE->FS.  */
9054
9055 static rtx
9056 choose_baseaddr (HOST_WIDE_INT cfa_offset)
9057 {
9058   const struct machine_function *m = cfun->machine;
9059   rtx base_reg = NULL;
9060   HOST_WIDE_INT base_offset = 0;
9061
9062   if (m->use_fast_prologue_epilogue)
9063     {
9064       /* Choose the base register most likely to allow the most scheduling
9065          opportunities.  Generally FP is valid througout the function,
9066          while DRAP must be reloaded within the epilogue.  But choose either
9067          over the SP due to increased encoding size.  */
9068
9069       if (m->fs.fp_valid)
9070         {
9071           base_reg = hard_frame_pointer_rtx;
9072           base_offset = m->fs.fp_offset - cfa_offset;
9073         }
9074       else if (m->fs.drap_valid)
9075         {
9076           base_reg = crtl->drap_reg;
9077           base_offset = 0 - cfa_offset;
9078         }
9079       else if (m->fs.sp_valid)
9080         {
9081           base_reg = stack_pointer_rtx;
9082           base_offset = m->fs.sp_offset - cfa_offset;
9083         }
9084     }
9085   else
9086     {
9087       HOST_WIDE_INT toffset;
9088       int len = 16, tlen;
9089
9090       /* Choose the base register with the smallest address encoding.
9091          With a tie, choose FP > DRAP > SP.  */
9092       if (m->fs.sp_valid)
9093         {
9094           base_reg = stack_pointer_rtx;
9095           base_offset = m->fs.sp_offset - cfa_offset;
9096           len = choose_baseaddr_len (STACK_POINTER_REGNUM, base_offset);
9097         }
9098       if (m->fs.drap_valid)
9099         {
9100           toffset = 0 - cfa_offset;
9101           tlen = choose_baseaddr_len (REGNO (crtl->drap_reg), toffset);
9102           if (tlen <= len)
9103             {
9104               base_reg = crtl->drap_reg;
9105               base_offset = toffset;
9106               len = tlen;
9107             }
9108         }
9109       if (m->fs.fp_valid)
9110         {
9111           toffset = m->fs.fp_offset - cfa_offset;
9112           tlen = choose_baseaddr_len (HARD_FRAME_POINTER_REGNUM, toffset);
9113           if (tlen <= len)
9114             {
9115               base_reg = hard_frame_pointer_rtx;
9116               base_offset = toffset;
9117               len = tlen;
9118             }
9119         }
9120     }
9121   gcc_assert (base_reg != NULL);
9122
9123   return plus_constant (base_reg, base_offset);
9124 }
9125
9126 /* Emit code to save registers in the prologue.  */
9127
9128 static void
9129 ix86_emit_save_regs (void)
9130 {
9131   unsigned int regno;
9132   rtx insn;
9133
9134   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
9135     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9136       {
9137         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
9138         RTX_FRAME_RELATED_P (insn) = 1;
9139       }
9140 }
9141
9142 /* Emit a single register save at CFA - CFA_OFFSET.  */
9143
9144 static void
9145 ix86_emit_save_reg_using_mov (enum machine_mode mode, unsigned int regno,
9146                               HOST_WIDE_INT cfa_offset)
9147 {
9148   struct machine_function *m = cfun->machine;
9149   rtx reg = gen_rtx_REG (mode, regno);
9150   rtx mem, addr, base, insn;
9151
9152   addr = choose_baseaddr (cfa_offset);
9153   mem = gen_frame_mem (mode, addr);
9154
9155   /* For SSE saves, we need to indicate the 128-bit alignment.  */
9156   set_mem_align (mem, GET_MODE_ALIGNMENT (mode));
9157
9158   insn = emit_move_insn (mem, reg);
9159   RTX_FRAME_RELATED_P (insn) = 1;
9160
9161   base = addr;
9162   if (GET_CODE (base) == PLUS)
9163     base = XEXP (base, 0);
9164   gcc_checking_assert (REG_P (base));
9165
9166   /* When saving registers into a re-aligned local stack frame, avoid
9167      any tricky guessing by dwarf2out.  */
9168   if (m->fs.realigned)
9169     {
9170       gcc_checking_assert (stack_realign_drap);
9171
9172       if (regno == REGNO (crtl->drap_reg))
9173         {
9174           /* A bit of a hack.  We force the DRAP register to be saved in
9175              the re-aligned stack frame, which provides us with a copy
9176              of the CFA that will last past the prologue.  Install it.  */
9177           gcc_checking_assert (cfun->machine->fs.fp_valid);
9178           addr = plus_constant (hard_frame_pointer_rtx,
9179                                 cfun->machine->fs.fp_offset - cfa_offset);
9180           mem = gen_rtx_MEM (mode, addr);
9181           add_reg_note (insn, REG_CFA_DEF_CFA, mem);
9182         }
9183       else
9184         {
9185           /* The frame pointer is a stable reference within the
9186              aligned frame.  Use it.  */
9187           gcc_checking_assert (cfun->machine->fs.fp_valid);
9188           addr = plus_constant (hard_frame_pointer_rtx,
9189                                 cfun->machine->fs.fp_offset - cfa_offset);
9190           mem = gen_rtx_MEM (mode, addr);
9191           add_reg_note (insn, REG_CFA_EXPRESSION,
9192                         gen_rtx_SET (VOIDmode, mem, reg));
9193         }
9194     }
9195
9196   /* The memory may not be relative to the current CFA register,
9197      which means that we may need to generate a new pattern for
9198      use by the unwind info.  */
9199   else if (base != m->fs.cfa_reg)
9200     {
9201       addr = plus_constant (m->fs.cfa_reg, m->fs.cfa_offset - cfa_offset);
9202       mem = gen_rtx_MEM (mode, addr);
9203       add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (VOIDmode, mem, reg));
9204     }
9205 }
9206
9207 /* Emit code to save registers using MOV insns.
9208    First register is stored at CFA - CFA_OFFSET.  */
9209 static void
9210 ix86_emit_save_regs_using_mov (HOST_WIDE_INT cfa_offset)
9211 {
9212   unsigned int regno;
9213
9214   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9215     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9216       {
9217         ix86_emit_save_reg_using_mov (Pmode, regno, cfa_offset);
9218         cfa_offset -= UNITS_PER_WORD;
9219       }
9220 }
9221
9222 /* Emit code to save SSE registers using MOV insns.
9223    First register is stored at CFA - CFA_OFFSET.  */
9224 static void
9225 ix86_emit_save_sse_regs_using_mov (HOST_WIDE_INT cfa_offset)
9226 {
9227   unsigned int regno;
9228
9229   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9230     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9231       {
9232         ix86_emit_save_reg_using_mov (V4SFmode, regno, cfa_offset);
9233         cfa_offset -= 16;
9234       }
9235 }
9236
9237 static GTY(()) rtx queued_cfa_restores;
9238
9239 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
9240    manipulation insn.  The value is on the stack at CFA - CFA_OFFSET.
9241    Don't add the note if the previously saved value will be left untouched
9242    within stack red-zone till return, as unwinders can find the same value
9243    in the register and on the stack.  */
9244
9245 static void
9246 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT cfa_offset)
9247 {
9248   if (!crtl->shrink_wrapped
9249       && cfa_offset <= cfun->machine->fs.red_zone_offset)
9250     return;
9251
9252   if (insn)
9253     {
9254       add_reg_note (insn, REG_CFA_RESTORE, reg);
9255       RTX_FRAME_RELATED_P (insn) = 1;
9256     }
9257   else
9258     queued_cfa_restores
9259       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
9260 }
9261
9262 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
9263
9264 static void
9265 ix86_add_queued_cfa_restore_notes (rtx insn)
9266 {
9267   rtx last;
9268   if (!queued_cfa_restores)
9269     return;
9270   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
9271     ;
9272   XEXP (last, 1) = REG_NOTES (insn);
9273   REG_NOTES (insn) = queued_cfa_restores;
9274   queued_cfa_restores = NULL_RTX;
9275   RTX_FRAME_RELATED_P (insn) = 1;
9276 }
9277
9278 /* Expand prologue or epilogue stack adjustment.
9279    The pattern exist to put a dependency on all ebp-based memory accesses.
9280    STYLE should be negative if instructions should be marked as frame related,
9281    zero if %r11 register is live and cannot be freely used and positive
9282    otherwise.  */
9283
9284 static void
9285 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
9286                            int style, bool set_cfa)
9287 {
9288   struct machine_function *m = cfun->machine;
9289   rtx insn;
9290   bool add_frame_related_expr = false;
9291
9292   if (! TARGET_64BIT)
9293     insn = gen_pro_epilogue_adjust_stack_si_add (dest, src, offset);
9294   else if (x86_64_immediate_operand (offset, DImode))
9295     insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, offset);
9296   else
9297     {
9298       rtx tmp;
9299       /* r11 is used by indirect sibcall return as well, set before the
9300          epilogue and used after the epilogue.  */
9301       if (style)
9302         tmp = gen_rtx_REG (DImode, R11_REG);
9303       else
9304         {
9305           gcc_assert (src != hard_frame_pointer_rtx
9306                       && dest != hard_frame_pointer_rtx);
9307           tmp = hard_frame_pointer_rtx;
9308         }
9309       insn = emit_insn (gen_rtx_SET (DImode, tmp, offset));
9310       if (style < 0)
9311         add_frame_related_expr = true;
9312
9313       insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, tmp);
9314     }
9315
9316   insn = emit_insn (insn);
9317   if (style >= 0)
9318     ix86_add_queued_cfa_restore_notes (insn);
9319
9320   if (set_cfa)
9321     {
9322       rtx r;
9323
9324       gcc_assert (m->fs.cfa_reg == src);
9325       m->fs.cfa_offset += INTVAL (offset);
9326       m->fs.cfa_reg = dest;
9327
9328       r = gen_rtx_PLUS (Pmode, src, offset);
9329       r = gen_rtx_SET (VOIDmode, dest, r);
9330       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
9331       RTX_FRAME_RELATED_P (insn) = 1;
9332     }
9333   else if (style < 0)
9334     {
9335       RTX_FRAME_RELATED_P (insn) = 1;
9336       if (add_frame_related_expr)
9337         {
9338           rtx r = gen_rtx_PLUS (Pmode, src, offset);
9339           r = gen_rtx_SET (VOIDmode, dest, r);
9340           add_reg_note (insn, REG_FRAME_RELATED_EXPR, r);
9341         }
9342     }
9343
9344   if (dest == stack_pointer_rtx)
9345     {
9346       HOST_WIDE_INT ooffset = m->fs.sp_offset;
9347       bool valid = m->fs.sp_valid;
9348
9349       if (src == hard_frame_pointer_rtx)
9350         {
9351           valid = m->fs.fp_valid;
9352           ooffset = m->fs.fp_offset;
9353         }
9354       else if (src == crtl->drap_reg)
9355         {
9356           valid = m->fs.drap_valid;
9357           ooffset = 0;
9358         }
9359       else
9360         {
9361           /* Else there are two possibilities: SP itself, which we set
9362              up as the default above.  Or EH_RETURN_STACKADJ_RTX, which is
9363              taken care of this by hand along the eh_return path.  */
9364           gcc_checking_assert (src == stack_pointer_rtx
9365                                || offset == const0_rtx);
9366         }
9367
9368       m->fs.sp_offset = ooffset - INTVAL (offset);
9369       m->fs.sp_valid = valid;
9370     }
9371 }
9372
9373 /* Find an available register to be used as dynamic realign argument
9374    pointer regsiter.  Such a register will be written in prologue and
9375    used in begin of body, so it must not be
9376         1. parameter passing register.
9377         2. GOT pointer.
9378    We reuse static-chain register if it is available.  Otherwise, we
9379    use DI for i386 and R13 for x86-64.  We chose R13 since it has
9380    shorter encoding.
9381
9382    Return: the regno of chosen register.  */
9383
9384 static unsigned int
9385 find_drap_reg (void)
9386 {
9387   tree decl = cfun->decl;
9388
9389   if (TARGET_64BIT)
9390     {
9391       /* Use R13 for nested function or function need static chain.
9392          Since function with tail call may use any caller-saved
9393          registers in epilogue, DRAP must not use caller-saved
9394          register in such case.  */
9395       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9396         return R13_REG;
9397
9398       return R10_REG;
9399     }
9400   else
9401     {
9402       /* Use DI for nested function or function need static chain.
9403          Since function with tail call may use any caller-saved
9404          registers in epilogue, DRAP must not use caller-saved
9405          register in such case.  */
9406       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9407         return DI_REG;
9408
9409       /* Reuse static chain register if it isn't used for parameter
9410          passing.  */
9411       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2)
9412         {
9413           unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
9414           if ((ccvt & (IX86_CALLCVT_FASTCALL | IX86_CALLCVT_THISCALL)) == 0)
9415             return CX_REG;
9416         }
9417       return DI_REG;
9418     }
9419 }
9420
9421 /* Return minimum incoming stack alignment.  */
9422
9423 static unsigned int
9424 ix86_minimum_incoming_stack_boundary (bool sibcall)
9425 {
9426   unsigned int incoming_stack_boundary;
9427
9428   /* Prefer the one specified at command line. */
9429   if (ix86_user_incoming_stack_boundary)
9430     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
9431   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
9432      if -mstackrealign is used, it isn't used for sibcall check and
9433      estimated stack alignment is 128bit.  */
9434   else if (!sibcall
9435            && !TARGET_64BIT
9436            && ix86_force_align_arg_pointer
9437            && crtl->stack_alignment_estimated == 128)
9438     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9439   else
9440     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
9441
9442   /* Incoming stack alignment can be changed on individual functions
9443      via force_align_arg_pointer attribute.  We use the smallest
9444      incoming stack boundary.  */
9445   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
9446       && lookup_attribute (ix86_force_align_arg_pointer_string,
9447                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
9448     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9449
9450   /* The incoming stack frame has to be aligned at least at
9451      parm_stack_boundary.  */
9452   if (incoming_stack_boundary < crtl->parm_stack_boundary)
9453     incoming_stack_boundary = crtl->parm_stack_boundary;
9454
9455   /* Stack at entrance of main is aligned by runtime.  We use the
9456      smallest incoming stack boundary. */
9457   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
9458       && DECL_NAME (current_function_decl)
9459       && MAIN_NAME_P (DECL_NAME (current_function_decl))
9460       && DECL_FILE_SCOPE_P (current_function_decl))
9461     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
9462
9463   return incoming_stack_boundary;
9464 }
9465
9466 /* Update incoming stack boundary and estimated stack alignment.  */
9467
9468 static void
9469 ix86_update_stack_boundary (void)
9470 {
9471   ix86_incoming_stack_boundary
9472     = ix86_minimum_incoming_stack_boundary (false);
9473
9474   /* x86_64 vararg needs 16byte stack alignment for register save
9475      area.  */
9476   if (TARGET_64BIT
9477       && cfun->stdarg
9478       && crtl->stack_alignment_estimated < 128)
9479     crtl->stack_alignment_estimated = 128;
9480 }
9481
9482 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
9483    needed or an rtx for DRAP otherwise.  */
9484
9485 static rtx
9486 ix86_get_drap_rtx (void)
9487 {
9488   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
9489     crtl->need_drap = true;
9490
9491   if (stack_realign_drap)
9492     {
9493       /* Assign DRAP to vDRAP and returns vDRAP */
9494       unsigned int regno = find_drap_reg ();
9495       rtx drap_vreg;
9496       rtx arg_ptr;
9497       rtx seq, insn;
9498
9499       arg_ptr = gen_rtx_REG (Pmode, regno);
9500       crtl->drap_reg = arg_ptr;
9501
9502       start_sequence ();
9503       drap_vreg = copy_to_reg (arg_ptr);
9504       seq = get_insns ();
9505       end_sequence ();
9506
9507       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
9508       if (!optimize)
9509         {
9510           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
9511           RTX_FRAME_RELATED_P (insn) = 1;
9512         }
9513       return drap_vreg;
9514     }
9515   else
9516     return NULL;
9517 }
9518
9519 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
9520
9521 static rtx
9522 ix86_internal_arg_pointer (void)
9523 {
9524   return virtual_incoming_args_rtx;
9525 }
9526
9527 struct scratch_reg {
9528   rtx reg;
9529   bool saved;
9530 };
9531
9532 /* Return a short-lived scratch register for use on function entry.
9533    In 32-bit mode, it is valid only after the registers are saved
9534    in the prologue.  This register must be released by means of
9535    release_scratch_register_on_entry once it is dead.  */
9536
9537 static void
9538 get_scratch_register_on_entry (struct scratch_reg *sr)
9539 {
9540   int regno;
9541
9542   sr->saved = false;
9543
9544   if (TARGET_64BIT)
9545     {
9546       /* We always use R11 in 64-bit mode.  */
9547       regno = R11_REG;
9548     }
9549   else
9550     {
9551       tree decl = current_function_decl, fntype = TREE_TYPE (decl);
9552       bool fastcall_p
9553         = lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
9554       bool static_chain_p = DECL_STATIC_CHAIN (decl);
9555       int regparm = ix86_function_regparm (fntype, decl);
9556       int drap_regno
9557         = crtl->drap_reg ? REGNO (crtl->drap_reg) : INVALID_REGNUM;
9558
9559       /* 'fastcall' sets regparm to 2, uses ecx/edx for arguments and eax
9560           for the static chain register.  */
9561       if ((regparm < 1 || (fastcall_p && !static_chain_p))
9562           && drap_regno != AX_REG)
9563         regno = AX_REG;
9564       else if (regparm < 2 && drap_regno != DX_REG)
9565         regno = DX_REG;
9566       /* ecx is the static chain register.  */
9567       else if (regparm < 3 && !fastcall_p && !static_chain_p
9568                && drap_regno != CX_REG)
9569         regno = CX_REG;
9570       else if (ix86_save_reg (BX_REG, true))
9571         regno = BX_REG;
9572       /* esi is the static chain register.  */
9573       else if (!(regparm == 3 && static_chain_p)
9574                && ix86_save_reg (SI_REG, true))
9575         regno = SI_REG;
9576       else if (ix86_save_reg (DI_REG, true))
9577         regno = DI_REG;
9578       else
9579         {
9580           regno = (drap_regno == AX_REG ? DX_REG : AX_REG);
9581           sr->saved = true;
9582         }
9583     }
9584
9585   sr->reg = gen_rtx_REG (Pmode, regno);
9586   if (sr->saved)
9587     {
9588       rtx insn = emit_insn (gen_push (sr->reg));
9589       RTX_FRAME_RELATED_P (insn) = 1;
9590     }
9591 }
9592
9593 /* Release a scratch register obtained from the preceding function.  */
9594
9595 static void
9596 release_scratch_register_on_entry (struct scratch_reg *sr)
9597 {
9598   if (sr->saved)
9599     {
9600       rtx x, insn = emit_insn (gen_pop (sr->reg));
9601
9602       /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
9603       RTX_FRAME_RELATED_P (insn) = 1;
9604       x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
9605       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
9606       add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
9607     }
9608 }
9609
9610 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
9611
9612 /* Emit code to adjust the stack pointer by SIZE bytes while probing it.  */
9613
9614 static void
9615 ix86_adjust_stack_and_probe (const HOST_WIDE_INT size)
9616 {
9617   /* We skip the probe for the first interval + a small dope of 4 words and
9618      probe that many bytes past the specified size to maintain a protection
9619      area at the botton of the stack.  */
9620   const int dope = 4 * UNITS_PER_WORD;
9621   rtx size_rtx = GEN_INT (size), last;
9622
9623   /* See if we have a constant small number of probes to generate.  If so,
9624      that's the easy case.  The run-time loop is made up of 11 insns in the
9625      generic case while the compile-time loop is made up of 3+2*(n-1) insns
9626      for n # of intervals.  */
9627   if (size <= 5 * PROBE_INTERVAL)
9628     {
9629       HOST_WIDE_INT i, adjust;
9630       bool first_probe = true;
9631
9632       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
9633          values of N from 1 until it exceeds SIZE.  If only one probe is
9634          needed, this will not generate any code.  Then adjust and probe
9635          to PROBE_INTERVAL + SIZE.  */
9636       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9637         {
9638           if (first_probe)
9639             {
9640               adjust = 2 * PROBE_INTERVAL + dope;
9641               first_probe = false;
9642             }
9643           else
9644             adjust = PROBE_INTERVAL;
9645
9646           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9647                                   plus_constant (stack_pointer_rtx, -adjust)));
9648           emit_stack_probe (stack_pointer_rtx);
9649         }
9650
9651       if (first_probe)
9652         adjust = size + PROBE_INTERVAL + dope;
9653       else
9654         adjust = size + PROBE_INTERVAL - i;
9655
9656       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9657                               plus_constant (stack_pointer_rtx, -adjust)));
9658       emit_stack_probe (stack_pointer_rtx);
9659
9660       /* Adjust back to account for the additional first interval.  */
9661       last = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9662                                      plus_constant (stack_pointer_rtx,
9663                                                     PROBE_INTERVAL + dope)));
9664     }
9665
9666   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9667      extra careful with variables wrapping around because we might be at
9668      the very top (or the very bottom) of the address space and we have
9669      to be able to handle this case properly; in particular, we use an
9670      equality test for the loop condition.  */
9671   else
9672     {
9673       HOST_WIDE_INT rounded_size;
9674       struct scratch_reg sr;
9675
9676       get_scratch_register_on_entry (&sr);
9677
9678
9679       /* Step 1: round SIZE to the previous multiple of the interval.  */
9680
9681       rounded_size = size & -PROBE_INTERVAL;
9682
9683
9684       /* Step 2: compute initial and final value of the loop counter.  */
9685
9686       /* SP = SP_0 + PROBE_INTERVAL.  */
9687       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9688                               plus_constant (stack_pointer_rtx,
9689                                              - (PROBE_INTERVAL + dope))));
9690
9691       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
9692       emit_move_insn (sr.reg, GEN_INT (-rounded_size));
9693       emit_insn (gen_rtx_SET (VOIDmode, sr.reg,
9694                               gen_rtx_PLUS (Pmode, sr.reg,
9695                                             stack_pointer_rtx)));
9696
9697
9698       /* Step 3: the loop
9699
9700          while (SP != LAST_ADDR)
9701            {
9702              SP = SP + PROBE_INTERVAL
9703              probe at SP
9704            }
9705
9706          adjusts SP and probes to PROBE_INTERVAL + N * PROBE_INTERVAL for
9707          values of N from 1 until it is equal to ROUNDED_SIZE.  */
9708
9709       emit_insn (ix86_gen_adjust_stack_and_probe (sr.reg, sr.reg, size_rtx));
9710
9711
9712       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
9713          assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
9714
9715       if (size != rounded_size)
9716         {
9717           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9718                                   plus_constant (stack_pointer_rtx,
9719                                                  rounded_size - size)));
9720           emit_stack_probe (stack_pointer_rtx);
9721         }
9722
9723       /* Adjust back to account for the additional first interval.  */
9724       last = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9725                                      plus_constant (stack_pointer_rtx,
9726                                                     PROBE_INTERVAL + dope)));
9727
9728       release_scratch_register_on_entry (&sr);
9729     }
9730
9731   gcc_assert (cfun->machine->fs.cfa_reg != stack_pointer_rtx);
9732
9733   /* Even if the stack pointer isn't the CFA register, we need to correctly
9734      describe the adjustments made to it, in particular differentiate the
9735      frame-related ones from the frame-unrelated ones.  */
9736   if (size > 0)
9737     {
9738       rtx expr = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (2));
9739       XVECEXP (expr, 0, 0)
9740         = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9741                        plus_constant (stack_pointer_rtx, -size));
9742       XVECEXP (expr, 0, 1)
9743         = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9744                        plus_constant (stack_pointer_rtx,
9745                                       PROBE_INTERVAL + dope + size));
9746       add_reg_note (last, REG_FRAME_RELATED_EXPR, expr);
9747       RTX_FRAME_RELATED_P (last) = 1;
9748
9749       cfun->machine->fs.sp_offset += size;
9750     }
9751
9752   /* Make sure nothing is scheduled before we are done.  */
9753   emit_insn (gen_blockage ());
9754 }
9755
9756 /* Adjust the stack pointer up to REG while probing it.  */
9757
9758 const char *
9759 output_adjust_stack_and_probe (rtx reg)
9760 {
9761   static int labelno = 0;
9762   char loop_lab[32], end_lab[32];
9763   rtx xops[2];
9764
9765   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9766   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9767
9768   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9769
9770   /* Jump to END_LAB if SP == LAST_ADDR.  */
9771   xops[0] = stack_pointer_rtx;
9772   xops[1] = reg;
9773   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9774   fputs ("\tje\t", asm_out_file);
9775   assemble_name_raw (asm_out_file, end_lab);
9776   fputc ('\n', asm_out_file);
9777
9778   /* SP = SP + PROBE_INTERVAL.  */
9779   xops[1] = GEN_INT (PROBE_INTERVAL);
9780   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9781
9782   /* Probe at SP.  */
9783   xops[1] = const0_rtx;
9784   output_asm_insn ("or%z0\t{%1, (%0)|DWORD PTR [%0], %1}", xops);
9785
9786   fprintf (asm_out_file, "\tjmp\t");
9787   assemble_name_raw (asm_out_file, loop_lab);
9788   fputc ('\n', asm_out_file);
9789
9790   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9791
9792   return "";
9793 }
9794
9795 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
9796    inclusive.  These are offsets from the current stack pointer.  */
9797
9798 static void
9799 ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
9800 {
9801   /* See if we have a constant small number of probes to generate.  If so,
9802      that's the easy case.  The run-time loop is made up of 7 insns in the
9803      generic case while the compile-time loop is made up of n insns for n #
9804      of intervals.  */
9805   if (size <= 7 * PROBE_INTERVAL)
9806     {
9807       HOST_WIDE_INT i;
9808
9809       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
9810          it exceeds SIZE.  If only one probe is needed, this will not
9811          generate any code.  Then probe at FIRST + SIZE.  */
9812       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9813         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
9814
9815       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
9816     }
9817
9818   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9819      extra careful with variables wrapping around because we might be at
9820      the very top (or the very bottom) of the address space and we have
9821      to be able to handle this case properly; in particular, we use an
9822      equality test for the loop condition.  */
9823   else
9824     {
9825       HOST_WIDE_INT rounded_size, last;
9826       struct scratch_reg sr;
9827
9828       get_scratch_register_on_entry (&sr);
9829
9830
9831       /* Step 1: round SIZE to the previous multiple of the interval.  */
9832
9833       rounded_size = size & -PROBE_INTERVAL;
9834
9835
9836       /* Step 2: compute initial and final value of the loop counter.  */
9837
9838       /* TEST_OFFSET = FIRST.  */
9839       emit_move_insn (sr.reg, GEN_INT (-first));
9840
9841       /* LAST_OFFSET = FIRST + ROUNDED_SIZE.  */
9842       last = first + rounded_size;
9843
9844
9845       /* Step 3: the loop
9846
9847          while (TEST_ADDR != LAST_ADDR)
9848            {
9849              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
9850              probe at TEST_ADDR
9851            }
9852
9853          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
9854          until it is equal to ROUNDED_SIZE.  */
9855
9856       emit_insn (ix86_gen_probe_stack_range (sr.reg, sr.reg, GEN_INT (-last)));
9857
9858
9859       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
9860          that SIZE is equal to ROUNDED_SIZE.  */
9861
9862       if (size != rounded_size)
9863         emit_stack_probe (plus_constant (gen_rtx_PLUS (Pmode,
9864                                                        stack_pointer_rtx,
9865                                                        sr.reg),
9866                                          rounded_size - size));
9867
9868       release_scratch_register_on_entry (&sr);
9869     }
9870
9871   /* Make sure nothing is scheduled before we are done.  */
9872   emit_insn (gen_blockage ());
9873 }
9874
9875 /* Probe a range of stack addresses from REG to END, inclusive.  These are
9876    offsets from the current stack pointer.  */
9877
9878 const char *
9879 output_probe_stack_range (rtx reg, rtx end)
9880 {
9881   static int labelno = 0;
9882   char loop_lab[32], end_lab[32];
9883   rtx xops[3];
9884
9885   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9886   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9887
9888   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9889
9890   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
9891   xops[0] = reg;
9892   xops[1] = end;
9893   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9894   fputs ("\tje\t", asm_out_file);
9895   assemble_name_raw (asm_out_file, end_lab);
9896   fputc ('\n', asm_out_file);
9897
9898   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
9899   xops[1] = GEN_INT (PROBE_INTERVAL);
9900   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9901
9902   /* Probe at TEST_ADDR.  */
9903   xops[0] = stack_pointer_rtx;
9904   xops[1] = reg;
9905   xops[2] = const0_rtx;
9906   output_asm_insn ("or%z0\t{%2, (%0,%1)|DWORD PTR [%0+%1], %2}", xops);
9907
9908   fprintf (asm_out_file, "\tjmp\t");
9909   assemble_name_raw (asm_out_file, loop_lab);
9910   fputc ('\n', asm_out_file);
9911
9912   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9913
9914   return "";
9915 }
9916
9917 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
9918    to be generated in correct form.  */
9919 static void
9920 ix86_finalize_stack_realign_flags (void)
9921 {
9922   /* Check if stack realign is really needed after reload, and
9923      stores result in cfun */
9924   unsigned int incoming_stack_boundary
9925     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
9926        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
9927   unsigned int stack_realign = (incoming_stack_boundary
9928                                 < (current_function_is_leaf
9929                                    ? crtl->max_used_stack_slot_alignment
9930                                    : crtl->stack_alignment_needed));
9931
9932   if (crtl->stack_realign_finalized)
9933     {
9934       /* After stack_realign_needed is finalized, we can't no longer
9935          change it.  */
9936       gcc_assert (crtl->stack_realign_needed == stack_realign);
9937       return;
9938     }
9939
9940   /* If the only reason for frame_pointer_needed is that we conservatively
9941      assumed stack realignment might be needed, but in the end nothing that
9942      needed the stack alignment had been spilled, clear frame_pointer_needed
9943      and say we don't need stack realignment.  */
9944   if (stack_realign
9945       && !crtl->need_drap
9946       && frame_pointer_needed
9947       && current_function_is_leaf
9948       && flag_omit_frame_pointer
9949       && current_function_sp_is_unchanging
9950       && !ix86_current_function_calls_tls_descriptor
9951       && !crtl->accesses_prior_frames
9952       && !cfun->calls_alloca
9953       && !crtl->calls_eh_return
9954       && !(flag_stack_check && STACK_CHECK_MOVING_SP)
9955       && !ix86_frame_pointer_required ()
9956       && get_frame_size () == 0
9957       && ix86_nsaved_sseregs () == 0
9958       && ix86_varargs_gpr_size + ix86_varargs_fpr_size == 0)
9959     {
9960       HARD_REG_SET set_up_by_prologue, prologue_used;
9961       basic_block bb;
9962
9963       CLEAR_HARD_REG_SET (prologue_used);
9964       CLEAR_HARD_REG_SET (set_up_by_prologue);
9965       add_to_hard_reg_set (&set_up_by_prologue, Pmode, STACK_POINTER_REGNUM);
9966       add_to_hard_reg_set (&set_up_by_prologue, Pmode, ARG_POINTER_REGNUM);
9967       add_to_hard_reg_set (&set_up_by_prologue, Pmode,
9968                            HARD_FRAME_POINTER_REGNUM);
9969       FOR_EACH_BB (bb)
9970         {
9971           rtx insn;
9972           FOR_BB_INSNS (bb, insn)
9973             if (NONDEBUG_INSN_P (insn)
9974                 && requires_stack_frame_p (insn, prologue_used,
9975                                            set_up_by_prologue))
9976               {
9977                 crtl->stack_realign_needed = stack_realign;
9978                 crtl->stack_realign_finalized = true;
9979                 return;
9980               }
9981         }
9982
9983       frame_pointer_needed = false;
9984       stack_realign = false;
9985       crtl->max_used_stack_slot_alignment = incoming_stack_boundary;
9986       crtl->stack_alignment_needed = incoming_stack_boundary;
9987       crtl->stack_alignment_estimated = incoming_stack_boundary;
9988       if (crtl->preferred_stack_boundary > incoming_stack_boundary)
9989         crtl->preferred_stack_boundary = incoming_stack_boundary;
9990       df_finish_pass (true);
9991       df_scan_alloc (NULL);
9992       df_scan_blocks ();
9993       df_compute_regs_ever_live (true);
9994       df_analyze ();
9995     }
9996
9997   crtl->stack_realign_needed = stack_realign;
9998   crtl->stack_realign_finalized = true;
9999 }
10000
10001 /* Expand the prologue into a bunch of separate insns.  */
10002
10003 void
10004 ix86_expand_prologue (void)
10005 {
10006   struct machine_function *m = cfun->machine;
10007   rtx insn, t;
10008   bool pic_reg_used;
10009   struct ix86_frame frame;
10010   HOST_WIDE_INT allocate;
10011   bool int_registers_saved;
10012   bool sse_registers_saved;
10013
10014   ix86_finalize_stack_realign_flags ();
10015
10016   /* DRAP should not coexist with stack_realign_fp */
10017   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
10018
10019   memset (&m->fs, 0, sizeof (m->fs));
10020
10021   /* Initialize CFA state for before the prologue.  */
10022   m->fs.cfa_reg = stack_pointer_rtx;
10023   m->fs.cfa_offset = INCOMING_FRAME_SP_OFFSET;
10024
10025   /* Track SP offset to the CFA.  We continue tracking this after we've
10026      swapped the CFA register away from SP.  In the case of re-alignment
10027      this is fudged; we're interested to offsets within the local frame.  */
10028   m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10029   m->fs.sp_valid = true;
10030
10031   ix86_compute_frame_layout (&frame);
10032
10033   if (!TARGET_64BIT && ix86_function_ms_hook_prologue (current_function_decl))
10034     {
10035       /* We should have already generated an error for any use of
10036          ms_hook on a nested function.  */
10037       gcc_checking_assert (!ix86_static_chain_on_stack);
10038
10039       /* Check if profiling is active and we shall use profiling before
10040          prologue variant. If so sorry.  */
10041       if (crtl->profile && flag_fentry != 0)
10042         sorry ("ms_hook_prologue attribute isn%'t compatible "
10043                "with -mfentry for 32-bit");
10044
10045       /* In ix86_asm_output_function_label we emitted:
10046          8b ff     movl.s %edi,%edi
10047          55        push   %ebp
10048          8b ec     movl.s %esp,%ebp
10049
10050          This matches the hookable function prologue in Win32 API
10051          functions in Microsoft Windows XP Service Pack 2 and newer.
10052          Wine uses this to enable Windows apps to hook the Win32 API
10053          functions provided by Wine.
10054
10055          What that means is that we've already set up the frame pointer.  */
10056
10057       if (frame_pointer_needed
10058           && !(crtl->drap_reg && crtl->stack_realign_needed))
10059         {
10060           rtx push, mov;
10061
10062           /* We've decided to use the frame pointer already set up.
10063              Describe this to the unwinder by pretending that both
10064              push and mov insns happen right here.
10065
10066              Putting the unwind info here at the end of the ms_hook
10067              is done so that we can make absolutely certain we get
10068              the required byte sequence at the start of the function,
10069              rather than relying on an assembler that can produce
10070              the exact encoding required.
10071
10072              However it does mean (in the unpatched case) that we have
10073              a 1 insn window where the asynchronous unwind info is
10074              incorrect.  However, if we placed the unwind info at
10075              its correct location we would have incorrect unwind info
10076              in the patched case.  Which is probably all moot since
10077              I don't expect Wine generates dwarf2 unwind info for the
10078              system libraries that use this feature.  */
10079
10080           insn = emit_insn (gen_blockage ());
10081
10082           push = gen_push (hard_frame_pointer_rtx);
10083           mov = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10084                              stack_pointer_rtx);
10085           RTX_FRAME_RELATED_P (push) = 1;
10086           RTX_FRAME_RELATED_P (mov) = 1;
10087
10088           RTX_FRAME_RELATED_P (insn) = 1;
10089           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10090                         gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, push, mov)));
10091
10092           /* Note that gen_push incremented m->fs.cfa_offset, even
10093              though we didn't emit the push insn here.  */
10094           m->fs.cfa_reg = hard_frame_pointer_rtx;
10095           m->fs.fp_offset = m->fs.cfa_offset;
10096           m->fs.fp_valid = true;
10097         }
10098       else
10099         {
10100           /* The frame pointer is not needed so pop %ebp again.
10101              This leaves us with a pristine state.  */
10102           emit_insn (gen_pop (hard_frame_pointer_rtx));
10103         }
10104     }
10105
10106   /* The first insn of a function that accepts its static chain on the
10107      stack is to push the register that would be filled in by a direct
10108      call.  This insn will be skipped by the trampoline.  */
10109   else if (ix86_static_chain_on_stack)
10110     {
10111       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
10112       emit_insn (gen_blockage ());
10113
10114       /* We don't want to interpret this push insn as a register save,
10115          only as a stack adjustment.  The real copy of the register as
10116          a save will be done later, if needed.  */
10117       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
10118       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
10119       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
10120       RTX_FRAME_RELATED_P (insn) = 1;
10121     }
10122
10123   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
10124      of DRAP is needed and stack realignment is really needed after reload */
10125   if (stack_realign_drap)
10126     {
10127       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10128
10129       /* Only need to push parameter pointer reg if it is caller saved.  */
10130       if (!call_used_regs[REGNO (crtl->drap_reg)])
10131         {
10132           /* Push arg pointer reg */
10133           insn = emit_insn (gen_push (crtl->drap_reg));
10134           RTX_FRAME_RELATED_P (insn) = 1;
10135         }
10136
10137       /* Grab the argument pointer.  */
10138       t = plus_constant (stack_pointer_rtx, m->fs.sp_offset);
10139       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10140       RTX_FRAME_RELATED_P (insn) = 1;
10141       m->fs.cfa_reg = crtl->drap_reg;
10142       m->fs.cfa_offset = 0;
10143
10144       /* Align the stack.  */
10145       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10146                                         stack_pointer_rtx,
10147                                         GEN_INT (-align_bytes)));
10148       RTX_FRAME_RELATED_P (insn) = 1;
10149
10150       /* Replicate the return address on the stack so that return
10151          address can be reached via (argp - 1) slot.  This is needed
10152          to implement macro RETURN_ADDR_RTX and intrinsic function
10153          expand_builtin_return_addr etc.  */
10154       t = plus_constant (crtl->drap_reg, -UNITS_PER_WORD);
10155       t = gen_frame_mem (Pmode, t);
10156       insn = emit_insn (gen_push (t));
10157       RTX_FRAME_RELATED_P (insn) = 1;
10158
10159       /* For the purposes of frame and register save area addressing,
10160          we've started over with a new frame.  */
10161       m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10162       m->fs.realigned = true;
10163     }
10164
10165   int_registers_saved = (frame.nregs == 0);
10166   sse_registers_saved = (frame.nsseregs == 0);
10167
10168   if (frame_pointer_needed && !m->fs.fp_valid)
10169     {
10170       /* Note: AT&T enter does NOT have reversed args.  Enter is probably
10171          slower on all targets.  Also sdb doesn't like it.  */
10172       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
10173       RTX_FRAME_RELATED_P (insn) = 1;
10174
10175       /* Push registers now, before setting the frame pointer
10176          on SEH target.  */
10177       if (!int_registers_saved
10178           && TARGET_SEH
10179           && !frame.save_regs_using_mov)
10180         {
10181           ix86_emit_save_regs ();
10182           int_registers_saved = true;
10183           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
10184         }
10185
10186       if (m->fs.sp_offset == frame.hard_frame_pointer_offset)
10187         {
10188           insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
10189           RTX_FRAME_RELATED_P (insn) = 1;
10190
10191           if (m->fs.cfa_reg == stack_pointer_rtx)
10192             m->fs.cfa_reg = hard_frame_pointer_rtx;
10193           m->fs.fp_offset = m->fs.sp_offset;
10194           m->fs.fp_valid = true;
10195         }
10196     }
10197
10198   if (!int_registers_saved)
10199     {
10200       /* If saving registers via PUSH, do so now.  */
10201       if (!frame.save_regs_using_mov)
10202         {
10203           ix86_emit_save_regs ();
10204           int_registers_saved = true;
10205           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
10206         }
10207
10208       /* When using red zone we may start register saving before allocating
10209          the stack frame saving one cycle of the prologue.  However, avoid
10210          doing this if we have to probe the stack; at least on x86_64 the
10211          stack probe can turn into a call that clobbers a red zone location. */
10212       else if (ix86_using_red_zone ()
10213                && (! TARGET_STACK_PROBE
10214                    || frame.stack_pointer_offset < CHECK_STACK_LIMIT))
10215         {
10216           ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10217           int_registers_saved = true;
10218         }
10219     }
10220
10221   if (stack_realign_fp)
10222     {
10223       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10224       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
10225
10226       /* The computation of the size of the re-aligned stack frame means
10227          that we must allocate the size of the register save area before
10228          performing the actual alignment.  Otherwise we cannot guarantee
10229          that there's enough storage above the realignment point.  */
10230       if (m->fs.sp_offset != frame.sse_reg_save_offset)
10231         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10232                                    GEN_INT (m->fs.sp_offset
10233                                             - frame.sse_reg_save_offset),
10234                                    -1, false);
10235
10236       /* Align the stack.  */
10237       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10238                                         stack_pointer_rtx,
10239                                         GEN_INT (-align_bytes)));
10240
10241       /* For the purposes of register save area addressing, the stack
10242          pointer is no longer valid.  As for the value of sp_offset,
10243          see ix86_compute_frame_layout, which we need to match in order
10244          to pass verification of stack_pointer_offset at the end.  */
10245       m->fs.sp_offset = (m->fs.sp_offset + align_bytes) & -align_bytes;
10246       m->fs.sp_valid = false;
10247     }
10248
10249   allocate = frame.stack_pointer_offset - m->fs.sp_offset;
10250
10251   if (flag_stack_usage_info)
10252     {
10253       /* We start to count from ARG_POINTER.  */
10254       HOST_WIDE_INT stack_size = frame.stack_pointer_offset;
10255
10256       /* If it was realigned, take into account the fake frame.  */
10257       if (stack_realign_drap)
10258         {
10259           if (ix86_static_chain_on_stack)
10260             stack_size += UNITS_PER_WORD;
10261
10262           if (!call_used_regs[REGNO (crtl->drap_reg)])
10263             stack_size += UNITS_PER_WORD;
10264
10265           /* This over-estimates by 1 minimal-stack-alignment-unit but
10266              mitigates that by counting in the new return address slot.  */
10267           current_function_dynamic_stack_size
10268             += crtl->stack_alignment_needed / BITS_PER_UNIT;
10269         }
10270
10271       current_function_static_stack_size = stack_size;
10272     }
10273
10274   /* On SEH target with very large frame size, allocate an area to save
10275      SSE registers (as the very large allocation won't be described).  */
10276   if (TARGET_SEH
10277       && frame.stack_pointer_offset > SEH_MAX_FRAME_SIZE
10278       && !sse_registers_saved)
10279     {
10280       HOST_WIDE_INT sse_size =
10281         frame.sse_reg_save_offset - frame.reg_save_offset;
10282
10283       gcc_assert (int_registers_saved);
10284
10285       /* No need to do stack checking as the area will be immediately
10286          written.  */
10287       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10288                                  GEN_INT (-sse_size), -1,
10289                                  m->fs.cfa_reg == stack_pointer_rtx);
10290       allocate -= sse_size;
10291       ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
10292       sse_registers_saved = true;
10293     }
10294
10295   /* The stack has already been decremented by the instruction calling us
10296      so probe if the size is non-negative to preserve the protection area.  */
10297   if (allocate >= 0 && flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
10298     {
10299       /* We expect the registers to be saved when probes are used.  */
10300       gcc_assert (int_registers_saved);
10301
10302       if (STACK_CHECK_MOVING_SP)
10303         {
10304           ix86_adjust_stack_and_probe (allocate);
10305           allocate = 0;
10306         }
10307       else
10308         {
10309           HOST_WIDE_INT size = allocate;
10310
10311           if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
10312             size = 0x80000000 - STACK_CHECK_PROTECT - 1;
10313
10314           if (TARGET_STACK_PROBE)
10315             ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
10316           else
10317             ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
10318         }
10319     }
10320
10321   if (allocate == 0)
10322     ;
10323   else if (!ix86_target_stack_probe ()
10324            || frame.stack_pointer_offset < CHECK_STACK_LIMIT)
10325     {
10326       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10327                                  GEN_INT (-allocate), -1,
10328                                  m->fs.cfa_reg == stack_pointer_rtx);
10329     }
10330   else
10331     {
10332       rtx eax = gen_rtx_REG (Pmode, AX_REG);
10333       rtx r10 = NULL;
10334       rtx (*adjust_stack_insn)(rtx, rtx, rtx);
10335
10336       bool eax_live = false;
10337       bool r10_live = false;
10338
10339       if (TARGET_64BIT)
10340         r10_live = (DECL_STATIC_CHAIN (current_function_decl) != 0);
10341       if (!TARGET_64BIT_MS_ABI)
10342         eax_live = ix86_eax_live_at_start_p ();
10343
10344       if (eax_live)
10345         {
10346           emit_insn (gen_push (eax));
10347           allocate -= UNITS_PER_WORD;
10348         }
10349       if (r10_live)
10350         {
10351           r10 = gen_rtx_REG (Pmode, R10_REG);
10352           emit_insn (gen_push (r10));
10353           allocate -= UNITS_PER_WORD;
10354         }
10355
10356       emit_move_insn (eax, GEN_INT (allocate));
10357       emit_insn (ix86_gen_allocate_stack_worker (eax, eax));
10358
10359       /* Use the fact that AX still contains ALLOCATE.  */
10360       adjust_stack_insn = (TARGET_64BIT
10361                            ? gen_pro_epilogue_adjust_stack_di_sub
10362                            : gen_pro_epilogue_adjust_stack_si_sub);
10363
10364       insn = emit_insn (adjust_stack_insn (stack_pointer_rtx,
10365                                            stack_pointer_rtx, eax));
10366
10367       /* Note that SEH directives need to continue tracking the stack
10368          pointer even after the frame pointer has been set up.  */
10369       if (m->fs.cfa_reg == stack_pointer_rtx || TARGET_SEH)
10370         {
10371           if (m->fs.cfa_reg == stack_pointer_rtx)
10372             m->fs.cfa_offset += allocate;
10373
10374           RTX_FRAME_RELATED_P (insn) = 1;
10375           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10376                         gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10377                                      plus_constant (stack_pointer_rtx,
10378                                                     -allocate)));
10379         }
10380       m->fs.sp_offset += allocate;
10381
10382       if (r10_live && eax_live)
10383         {
10384           t = choose_baseaddr (m->fs.sp_offset - allocate);
10385           emit_move_insn (r10, gen_frame_mem (Pmode, t));
10386           t = choose_baseaddr (m->fs.sp_offset - allocate - UNITS_PER_WORD);
10387           emit_move_insn (eax, gen_frame_mem (Pmode, t));
10388         }
10389       else if (eax_live || r10_live)
10390         {
10391           t = choose_baseaddr (m->fs.sp_offset - allocate);
10392           emit_move_insn ((eax_live ? eax : r10), gen_frame_mem (Pmode, t));
10393         }
10394     }
10395   gcc_assert (m->fs.sp_offset == frame.stack_pointer_offset);
10396
10397   /* If we havn't already set up the frame pointer, do so now.  */
10398   if (frame_pointer_needed && !m->fs.fp_valid)
10399     {
10400       insn = ix86_gen_add3 (hard_frame_pointer_rtx, stack_pointer_rtx,
10401                             GEN_INT (frame.stack_pointer_offset
10402                                      - frame.hard_frame_pointer_offset));
10403       insn = emit_insn (insn);
10404       RTX_FRAME_RELATED_P (insn) = 1;
10405       add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
10406
10407       if (m->fs.cfa_reg == stack_pointer_rtx)
10408         m->fs.cfa_reg = hard_frame_pointer_rtx;
10409       m->fs.fp_offset = frame.hard_frame_pointer_offset;
10410       m->fs.fp_valid = true;
10411     }
10412
10413   if (!int_registers_saved)
10414     ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10415   if (!sse_registers_saved)
10416     ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
10417
10418   pic_reg_used = false;
10419   if (pic_offset_table_rtx
10420       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
10421           || crtl->profile))
10422     {
10423       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
10424
10425       if (alt_pic_reg_used != INVALID_REGNUM)
10426         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
10427
10428       pic_reg_used = true;
10429     }
10430
10431   if (pic_reg_used)
10432     {
10433       if (TARGET_64BIT)
10434         {
10435           if (ix86_cmodel == CM_LARGE_PIC)
10436             {
10437               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
10438               rtx label = gen_label_rtx ();
10439               emit_label (label);
10440               LABEL_PRESERVE_P (label) = 1;
10441               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
10442               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
10443               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
10444               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
10445                                             pic_offset_table_rtx, tmp_reg));
10446             }
10447           else
10448             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
10449         }
10450       else
10451         {
10452           insn = emit_insn (gen_set_got (pic_offset_table_rtx));
10453           RTX_FRAME_RELATED_P (insn) = 1;
10454           add_reg_note (insn, REG_CFA_FLUSH_QUEUE, NULL_RTX);
10455         }
10456     }
10457
10458   /* In the pic_reg_used case, make sure that the got load isn't deleted
10459      when mcount needs it.  Blockage to avoid call movement across mcount
10460      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
10461      note.  */
10462   if (crtl->profile && !flag_fentry && pic_reg_used)
10463     emit_insn (gen_prologue_use (pic_offset_table_rtx));
10464
10465   if (crtl->drap_reg && !crtl->stack_realign_needed)
10466     {
10467       /* vDRAP is setup but after reload it turns out stack realign
10468          isn't necessary, here we will emit prologue to setup DRAP
10469          without stack realign adjustment */
10470       t = choose_baseaddr (0);
10471       emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10472     }
10473
10474   /* Prevent instructions from being scheduled into register save push
10475      sequence when access to the redzone area is done through frame pointer.
10476      The offset between the frame pointer and the stack pointer is calculated
10477      relative to the value of the stack pointer at the end of the function
10478      prologue, and moving instructions that access redzone area via frame
10479      pointer inside push sequence violates this assumption.  */
10480   if (frame_pointer_needed && frame.red_zone_size)
10481     emit_insn (gen_memory_blockage ());
10482
10483   /* Emit cld instruction if stringops are used in the function.  */
10484   if (TARGET_CLD && ix86_current_function_needs_cld)
10485     emit_insn (gen_cld ());
10486
10487   /* SEH requires that the prologue end within 256 bytes of the start of
10488      the function.  Prevent instruction schedules that would extend that.
10489      Further, prevent alloca modifications to the stack pointer from being
10490      combined with prologue modifications.  */
10491   if (TARGET_SEH)
10492     emit_insn (gen_prologue_use (stack_pointer_rtx));
10493 }
10494
10495 /* Emit code to restore REG using a POP insn.  */
10496
10497 static void
10498 ix86_emit_restore_reg_using_pop (rtx reg)
10499 {
10500   struct machine_function *m = cfun->machine;
10501   rtx insn = emit_insn (gen_pop (reg));
10502
10503   ix86_add_cfa_restore_note (insn, reg, m->fs.sp_offset);
10504   m->fs.sp_offset -= UNITS_PER_WORD;
10505
10506   if (m->fs.cfa_reg == crtl->drap_reg
10507       && REGNO (reg) == REGNO (crtl->drap_reg))
10508     {
10509       /* Previously we'd represented the CFA as an expression
10510          like *(%ebp - 8).  We've just popped that value from
10511          the stack, which means we need to reset the CFA to
10512          the drap register.  This will remain until we restore
10513          the stack pointer.  */
10514       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10515       RTX_FRAME_RELATED_P (insn) = 1;
10516
10517       /* This means that the DRAP register is valid for addressing too.  */
10518       m->fs.drap_valid = true;
10519       return;
10520     }
10521
10522   if (m->fs.cfa_reg == stack_pointer_rtx)
10523     {
10524       rtx x = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
10525       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
10526       add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
10527       RTX_FRAME_RELATED_P (insn) = 1;
10528
10529       m->fs.cfa_offset -= UNITS_PER_WORD;
10530     }
10531
10532   /* When the frame pointer is the CFA, and we pop it, we are
10533      swapping back to the stack pointer as the CFA.  This happens
10534      for stack frames that don't allocate other data, so we assume
10535      the stack pointer is now pointing at the return address, i.e.
10536      the function entry state, which makes the offset be 1 word.  */
10537   if (reg == hard_frame_pointer_rtx)
10538     {
10539       m->fs.fp_valid = false;
10540       if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10541         {
10542           m->fs.cfa_reg = stack_pointer_rtx;
10543           m->fs.cfa_offset -= UNITS_PER_WORD;
10544
10545           add_reg_note (insn, REG_CFA_DEF_CFA,
10546                         gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10547                                       GEN_INT (m->fs.cfa_offset)));
10548           RTX_FRAME_RELATED_P (insn) = 1;
10549         }
10550     }
10551 }
10552
10553 /* Emit code to restore saved registers using POP insns.  */
10554
10555 static void
10556 ix86_emit_restore_regs_using_pop (void)
10557 {
10558   unsigned int regno;
10559
10560   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10561     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
10562       ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno));
10563 }
10564
10565 /* Emit code and notes for the LEAVE instruction.  */
10566
10567 static void
10568 ix86_emit_leave (void)
10569 {
10570   struct machine_function *m = cfun->machine;
10571   rtx insn = emit_insn (ix86_gen_leave ());
10572
10573   ix86_add_queued_cfa_restore_notes (insn);
10574
10575   gcc_assert (m->fs.fp_valid);
10576   m->fs.sp_valid = true;
10577   m->fs.sp_offset = m->fs.fp_offset - UNITS_PER_WORD;
10578   m->fs.fp_valid = false;
10579
10580   if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10581     {
10582       m->fs.cfa_reg = stack_pointer_rtx;
10583       m->fs.cfa_offset = m->fs.sp_offset;
10584
10585       add_reg_note (insn, REG_CFA_DEF_CFA,
10586                     plus_constant (stack_pointer_rtx, m->fs.sp_offset));
10587       RTX_FRAME_RELATED_P (insn) = 1;
10588     }
10589   ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx,
10590                              m->fs.fp_offset);
10591 }
10592
10593 /* Emit code to restore saved registers using MOV insns.
10594    First register is restored from CFA - CFA_OFFSET.  */
10595 static void
10596 ix86_emit_restore_regs_using_mov (HOST_WIDE_INT cfa_offset,
10597                                   bool maybe_eh_return)
10598 {
10599   struct machine_function *m = cfun->machine;
10600   unsigned int regno;
10601
10602   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10603     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10604       {
10605         rtx reg = gen_rtx_REG (Pmode, regno);
10606         rtx insn, mem;
10607
10608         mem = choose_baseaddr (cfa_offset);
10609         mem = gen_frame_mem (Pmode, mem);
10610         insn = emit_move_insn (reg, mem);
10611
10612         if (m->fs.cfa_reg == crtl->drap_reg && regno == REGNO (crtl->drap_reg))
10613           {
10614             /* Previously we'd represented the CFA as an expression
10615                like *(%ebp - 8).  We've just popped that value from
10616                the stack, which means we need to reset the CFA to
10617                the drap register.  This will remain until we restore
10618                the stack pointer.  */
10619             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10620             RTX_FRAME_RELATED_P (insn) = 1;
10621
10622             /* This means that the DRAP register is valid for addressing.  */
10623             m->fs.drap_valid = true;
10624           }
10625         else
10626           ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10627
10628         cfa_offset -= UNITS_PER_WORD;
10629       }
10630 }
10631
10632 /* Emit code to restore saved registers using MOV insns.
10633    First register is restored from CFA - CFA_OFFSET.  */
10634 static void
10635 ix86_emit_restore_sse_regs_using_mov (HOST_WIDE_INT cfa_offset,
10636                                       bool maybe_eh_return)
10637 {
10638   unsigned int regno;
10639
10640   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10641     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10642       {
10643         rtx reg = gen_rtx_REG (V4SFmode, regno);
10644         rtx mem;
10645
10646         mem = choose_baseaddr (cfa_offset);
10647         mem = gen_rtx_MEM (V4SFmode, mem);
10648         set_mem_align (mem, 128);
10649         emit_move_insn (reg, mem);
10650
10651         ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10652
10653         cfa_offset -= 16;
10654       }
10655 }
10656
10657 /* Emit vzeroupper if needed.  */
10658
10659 void
10660 ix86_maybe_emit_epilogue_vzeroupper (void)
10661 {
10662   if (TARGET_VZEROUPPER
10663       && !TREE_THIS_VOLATILE (cfun->decl)
10664       && !cfun->machine->caller_return_avx256_p)
10665     emit_insn (gen_avx_vzeroupper (GEN_INT (call_no_avx256)));
10666 }
10667
10668 /* Restore function stack, frame, and registers.  */
10669
10670 void
10671 ix86_expand_epilogue (int style)
10672 {
10673   struct machine_function *m = cfun->machine;
10674   struct machine_frame_state frame_state_save = m->fs;
10675   struct ix86_frame frame;
10676   bool restore_regs_via_mov;
10677   bool using_drap;
10678
10679   ix86_finalize_stack_realign_flags ();
10680   ix86_compute_frame_layout (&frame);
10681
10682   m->fs.sp_valid = (!frame_pointer_needed
10683                     || (current_function_sp_is_unchanging
10684                         && !stack_realign_fp));
10685   gcc_assert (!m->fs.sp_valid
10686               || m->fs.sp_offset == frame.stack_pointer_offset);
10687
10688   /* The FP must be valid if the frame pointer is present.  */
10689   gcc_assert (frame_pointer_needed == m->fs.fp_valid);
10690   gcc_assert (!m->fs.fp_valid
10691               || m->fs.fp_offset == frame.hard_frame_pointer_offset);
10692
10693   /* We must have *some* valid pointer to the stack frame.  */
10694   gcc_assert (m->fs.sp_valid || m->fs.fp_valid);
10695
10696   /* The DRAP is never valid at this point.  */
10697   gcc_assert (!m->fs.drap_valid);
10698
10699   /* See the comment about red zone and frame
10700      pointer usage in ix86_expand_prologue.  */
10701   if (frame_pointer_needed && frame.red_zone_size)
10702     emit_insn (gen_memory_blockage ());
10703
10704   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
10705   gcc_assert (!using_drap || m->fs.cfa_reg == crtl->drap_reg);
10706
10707   /* Determine the CFA offset of the end of the red-zone.  */
10708   m->fs.red_zone_offset = 0;
10709   if (ix86_using_red_zone () && crtl->args.pops_args < 65536)
10710     {
10711       /* The red-zone begins below the return address.  */
10712       m->fs.red_zone_offset = RED_ZONE_SIZE + UNITS_PER_WORD;
10713
10714       /* When the register save area is in the aligned portion of
10715          the stack, determine the maximum runtime displacement that
10716          matches up with the aligned frame.  */
10717       if (stack_realign_drap)
10718         m->fs.red_zone_offset -= (crtl->stack_alignment_needed / BITS_PER_UNIT
10719                                   + UNITS_PER_WORD);
10720     }
10721
10722   /* Special care must be taken for the normal return case of a function
10723      using eh_return: the eax and edx registers are marked as saved, but
10724      not restored along this path.  Adjust the save location to match.  */
10725   if (crtl->calls_eh_return && style != 2)
10726     frame.reg_save_offset -= 2 * UNITS_PER_WORD;
10727
10728   /* EH_RETURN requires the use of moves to function properly.  */
10729   if (crtl->calls_eh_return)
10730     restore_regs_via_mov = true;
10731   /* SEH requires the use of pops to identify the epilogue.  */
10732   else if (TARGET_SEH)
10733     restore_regs_via_mov = false;
10734   /* If we're only restoring one register and sp is not valid then
10735      using a move instruction to restore the register since it's
10736      less work than reloading sp and popping the register.  */
10737   else if (!m->fs.sp_valid && frame.nregs <= 1)
10738     restore_regs_via_mov = true;
10739   else if (TARGET_EPILOGUE_USING_MOVE
10740            && cfun->machine->use_fast_prologue_epilogue
10741            && (frame.nregs > 1
10742                || m->fs.sp_offset != frame.reg_save_offset))
10743     restore_regs_via_mov = true;
10744   else if (frame_pointer_needed
10745            && !frame.nregs
10746            && m->fs.sp_offset != frame.reg_save_offset)
10747     restore_regs_via_mov = true;
10748   else if (frame_pointer_needed
10749            && TARGET_USE_LEAVE
10750            && cfun->machine->use_fast_prologue_epilogue
10751            && frame.nregs == 1)
10752     restore_regs_via_mov = true;
10753   else
10754     restore_regs_via_mov = false;
10755
10756   if (restore_regs_via_mov || frame.nsseregs)
10757     {
10758       /* Ensure that the entire register save area is addressable via
10759          the stack pointer, if we will restore via sp.  */
10760       if (TARGET_64BIT
10761           && m->fs.sp_offset > 0x7fffffff
10762           && !(m->fs.fp_valid || m->fs.drap_valid)
10763           && (frame.nsseregs + frame.nregs) != 0)
10764         {
10765           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10766                                      GEN_INT (m->fs.sp_offset
10767                                               - frame.sse_reg_save_offset),
10768                                      style,
10769                                      m->fs.cfa_reg == stack_pointer_rtx);
10770         }
10771     }
10772
10773   /* If there are any SSE registers to restore, then we have to do it
10774      via moves, since there's obviously no pop for SSE regs.  */
10775   if (frame.nsseregs)
10776     ix86_emit_restore_sse_regs_using_mov (frame.sse_reg_save_offset,
10777                                           style == 2);
10778
10779   if (restore_regs_via_mov)
10780     {
10781       rtx t;
10782
10783       if (frame.nregs)
10784         ix86_emit_restore_regs_using_mov (frame.reg_save_offset, style == 2);
10785
10786       /* eh_return epilogues need %ecx added to the stack pointer.  */
10787       if (style == 2)
10788         {
10789           rtx insn, sa = EH_RETURN_STACKADJ_RTX;
10790
10791           /* Stack align doesn't work with eh_return.  */
10792           gcc_assert (!stack_realign_drap);
10793           /* Neither does regparm nested functions.  */
10794           gcc_assert (!ix86_static_chain_on_stack);
10795
10796           if (frame_pointer_needed)
10797             {
10798               t = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
10799               t = plus_constant (t, m->fs.fp_offset - UNITS_PER_WORD);
10800               emit_insn (gen_rtx_SET (VOIDmode, sa, t));
10801
10802               t = gen_frame_mem (Pmode, hard_frame_pointer_rtx);
10803               insn = emit_move_insn (hard_frame_pointer_rtx, t);
10804
10805               /* Note that we use SA as a temporary CFA, as the return
10806                  address is at the proper place relative to it.  We
10807                  pretend this happens at the FP restore insn because
10808                  prior to this insn the FP would be stored at the wrong
10809                  offset relative to SA, and after this insn we have no
10810                  other reasonable register to use for the CFA.  We don't
10811                  bother resetting the CFA to the SP for the duration of
10812                  the return insn.  */
10813               add_reg_note (insn, REG_CFA_DEF_CFA,
10814                             plus_constant (sa, UNITS_PER_WORD));
10815               ix86_add_queued_cfa_restore_notes (insn);
10816               add_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx);
10817               RTX_FRAME_RELATED_P (insn) = 1;
10818
10819               m->fs.cfa_reg = sa;
10820               m->fs.cfa_offset = UNITS_PER_WORD;
10821               m->fs.fp_valid = false;
10822
10823               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
10824                                          const0_rtx, style, false);
10825             }
10826           else
10827             {
10828               t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
10829               t = plus_constant (t, m->fs.sp_offset - UNITS_PER_WORD);
10830               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, t));
10831               ix86_add_queued_cfa_restore_notes (insn);
10832
10833               gcc_assert (m->fs.cfa_reg == stack_pointer_rtx);
10834               if (m->fs.cfa_offset != UNITS_PER_WORD)
10835                 {
10836                   m->fs.cfa_offset = UNITS_PER_WORD;
10837                   add_reg_note (insn, REG_CFA_DEF_CFA,
10838                                 plus_constant (stack_pointer_rtx,
10839                                                UNITS_PER_WORD));
10840                   RTX_FRAME_RELATED_P (insn) = 1;
10841                 }
10842             }
10843           m->fs.sp_offset = UNITS_PER_WORD;
10844           m->fs.sp_valid = true;
10845         }
10846     }
10847   else
10848     {
10849       /* SEH requires that the function end with (1) a stack adjustment
10850          if necessary, (2) a sequence of pops, and (3) a return or
10851          jump instruction.  Prevent insns from the function body from
10852          being scheduled into this sequence.  */
10853       if (TARGET_SEH)
10854         {
10855           /* Prevent a catch region from being adjacent to the standard
10856              epilogue sequence.  Unfortuantely crtl->uses_eh_lsda nor
10857              several other flags that would be interesting to test are
10858              not yet set up.  */
10859           if (flag_non_call_exceptions)
10860             emit_insn (gen_nops (const1_rtx));
10861           else
10862             emit_insn (gen_blockage ());
10863         }
10864
10865       /* First step is to deallocate the stack frame so that we can
10866          pop the registers.  Also do it on SEH target for very large
10867          frame as the emitted instructions aren't allowed by the ABI in
10868          epilogues.  */
10869       if (!m->fs.sp_valid
10870           || (TARGET_SEH
10871               && (m->fs.sp_offset - frame.reg_save_offset
10872                   >= SEH_MAX_FRAME_SIZE)))
10873         {
10874           pro_epilogue_adjust_stack (stack_pointer_rtx, hard_frame_pointer_rtx,
10875                                      GEN_INT (m->fs.fp_offset
10876                                               - frame.reg_save_offset),
10877                                      style, false);
10878         }
10879       else if (m->fs.sp_offset != frame.reg_save_offset)
10880         {
10881           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10882                                      GEN_INT (m->fs.sp_offset
10883                                               - frame.reg_save_offset),
10884                                      style,
10885                                      m->fs.cfa_reg == stack_pointer_rtx);
10886         }
10887
10888       ix86_emit_restore_regs_using_pop ();
10889     }
10890
10891   /* If we used a stack pointer and haven't already got rid of it,
10892      then do so now.  */
10893   if (m->fs.fp_valid)
10894     {
10895       /* If the stack pointer is valid and pointing at the frame
10896          pointer store address, then we only need a pop.  */
10897       if (m->fs.sp_valid && m->fs.sp_offset == frame.hfp_save_offset)
10898         ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10899       /* Leave results in shorter dependency chains on CPUs that are
10900          able to grok it fast.  */
10901       else if (TARGET_USE_LEAVE
10902                || optimize_function_for_size_p (cfun)
10903                || !cfun->machine->use_fast_prologue_epilogue)
10904         ix86_emit_leave ();
10905       else
10906         {
10907           pro_epilogue_adjust_stack (stack_pointer_rtx,
10908                                      hard_frame_pointer_rtx,
10909                                      const0_rtx, style, !using_drap);
10910           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10911         }
10912     }
10913
10914   if (using_drap)
10915     {
10916       int param_ptr_offset = UNITS_PER_WORD;
10917       rtx insn;
10918
10919       gcc_assert (stack_realign_drap);
10920
10921       if (ix86_static_chain_on_stack)
10922         param_ptr_offset += UNITS_PER_WORD;
10923       if (!call_used_regs[REGNO (crtl->drap_reg)])
10924         param_ptr_offset += UNITS_PER_WORD;
10925
10926       insn = emit_insn (gen_rtx_SET
10927                         (VOIDmode, stack_pointer_rtx,
10928                          gen_rtx_PLUS (Pmode,
10929                                        crtl->drap_reg,
10930                                        GEN_INT (-param_ptr_offset))));
10931       m->fs.cfa_reg = stack_pointer_rtx;
10932       m->fs.cfa_offset = param_ptr_offset;
10933       m->fs.sp_offset = param_ptr_offset;
10934       m->fs.realigned = false;
10935
10936       add_reg_note (insn, REG_CFA_DEF_CFA,
10937                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10938                                   GEN_INT (param_ptr_offset)));
10939       RTX_FRAME_RELATED_P (insn) = 1;
10940
10941       if (!call_used_regs[REGNO (crtl->drap_reg)])
10942         ix86_emit_restore_reg_using_pop (crtl->drap_reg);
10943     }
10944
10945   /* At this point the stack pointer must be valid, and we must have
10946      restored all of the registers.  We may not have deallocated the
10947      entire stack frame.  We've delayed this until now because it may
10948      be possible to merge the local stack deallocation with the
10949      deallocation forced by ix86_static_chain_on_stack.   */
10950   gcc_assert (m->fs.sp_valid);
10951   gcc_assert (!m->fs.fp_valid);
10952   gcc_assert (!m->fs.realigned);
10953   if (m->fs.sp_offset != UNITS_PER_WORD)
10954     {
10955       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10956                                  GEN_INT (m->fs.sp_offset - UNITS_PER_WORD),
10957                                  style, true);
10958     }
10959   else
10960     ix86_add_queued_cfa_restore_notes (get_last_insn ());
10961
10962   /* Sibcall epilogues don't want a return instruction.  */
10963   if (style == 0)
10964     {
10965       m->fs = frame_state_save;
10966       return;
10967     }
10968
10969   /* Emit vzeroupper if needed.  */
10970   ix86_maybe_emit_epilogue_vzeroupper ();
10971
10972   if (crtl->args.pops_args && crtl->args.size)
10973     {
10974       rtx popc = GEN_INT (crtl->args.pops_args);
10975
10976       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
10977          address, do explicit add, and jump indirectly to the caller.  */
10978
10979       if (crtl->args.pops_args >= 65536)
10980         {
10981           rtx ecx = gen_rtx_REG (SImode, CX_REG);
10982           rtx insn;
10983
10984           /* There is no "pascal" calling convention in any 64bit ABI.  */
10985           gcc_assert (!TARGET_64BIT);
10986
10987           insn = emit_insn (gen_pop (ecx));
10988           m->fs.cfa_offset -= UNITS_PER_WORD;
10989           m->fs.sp_offset -= UNITS_PER_WORD;
10990
10991           add_reg_note (insn, REG_CFA_ADJUST_CFA,
10992                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
10993           add_reg_note (insn, REG_CFA_REGISTER,
10994                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
10995           RTX_FRAME_RELATED_P (insn) = 1;
10996
10997           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10998                                      popc, -1, true);
10999           emit_jump_insn (gen_simple_return_indirect_internal (ecx));
11000         }
11001       else
11002         emit_jump_insn (gen_simple_return_pop_internal (popc));
11003     }
11004   else
11005     emit_jump_insn (gen_simple_return_internal ());
11006
11007   /* Restore the state back to the state from the prologue,
11008      so that it's correct for the next epilogue.  */
11009   m->fs = frame_state_save;
11010 }
11011
11012 /* Reset from the function's potential modifications.  */
11013
11014 static void
11015 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11016                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11017 {
11018   if (pic_offset_table_rtx)
11019     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
11020 #if TARGET_MACHO
11021   /* Mach-O doesn't support labels at the end of objects, so if
11022      it looks like we might want one, insert a NOP.  */
11023   {
11024     rtx insn = get_last_insn ();
11025     rtx deleted_debug_label = NULL_RTX;
11026     while (insn
11027            && NOTE_P (insn)
11028            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
11029       {
11030         /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
11031            notes only, instead set their CODE_LABEL_NUMBER to -1,
11032            otherwise there would be code generation differences
11033            in between -g and -g0.  */
11034         if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
11035           deleted_debug_label = insn;
11036         insn = PREV_INSN (insn);
11037       }
11038     if (insn
11039         && (LABEL_P (insn)
11040             || (NOTE_P (insn)
11041                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
11042       fputs ("\tnop\n", file);
11043     else if (deleted_debug_label)
11044       for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
11045         if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
11046           CODE_LABEL_NUMBER (insn) = -1;
11047   }
11048 #endif
11049
11050 }
11051
11052 /* Return a scratch register to use in the split stack prologue.  The
11053    split stack prologue is used for -fsplit-stack.  It is the first
11054    instructions in the function, even before the regular prologue.
11055    The scratch register can be any caller-saved register which is not
11056    used for parameters or for the static chain.  */
11057
11058 static unsigned int
11059 split_stack_prologue_scratch_regno (void)
11060 {
11061   if (TARGET_64BIT)
11062     return R11_REG;
11063   else
11064     {
11065       bool is_fastcall;
11066       int regparm;
11067
11068       is_fastcall = (lookup_attribute ("fastcall",
11069                                        TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
11070                      != NULL);
11071       regparm = ix86_function_regparm (TREE_TYPE (cfun->decl), cfun->decl);
11072
11073       if (is_fastcall)
11074         {
11075           if (DECL_STATIC_CHAIN (cfun->decl))
11076             {
11077               sorry ("-fsplit-stack does not support fastcall with "
11078                      "nested function");
11079               return INVALID_REGNUM;
11080             }
11081           return AX_REG;
11082         }
11083       else if (regparm < 3)
11084         {
11085           if (!DECL_STATIC_CHAIN (cfun->decl))
11086             return CX_REG;
11087           else
11088             {
11089               if (regparm >= 2)
11090                 {
11091                   sorry ("-fsplit-stack does not support 2 register "
11092                          " parameters for a nested function");
11093                   return INVALID_REGNUM;
11094                 }
11095               return DX_REG;
11096             }
11097         }
11098       else
11099         {
11100           /* FIXME: We could make this work by pushing a register
11101              around the addition and comparison.  */
11102           sorry ("-fsplit-stack does not support 3 register parameters");
11103           return INVALID_REGNUM;
11104         }
11105     }
11106 }
11107
11108 /* A SYMBOL_REF for the function which allocates new stackspace for
11109    -fsplit-stack.  */
11110
11111 static GTY(()) rtx split_stack_fn;
11112
11113 /* A SYMBOL_REF for the more stack function when using the large
11114    model.  */
11115
11116 static GTY(()) rtx split_stack_fn_large;
11117
11118 /* Handle -fsplit-stack.  These are the first instructions in the
11119    function, even before the regular prologue.  */
11120
11121 void
11122 ix86_expand_split_stack_prologue (void)
11123 {
11124   struct ix86_frame frame;
11125   HOST_WIDE_INT allocate;
11126   unsigned HOST_WIDE_INT args_size;
11127   rtx label, limit, current, jump_insn, allocate_rtx, call_insn, call_fusage;
11128   rtx scratch_reg = NULL_RTX;
11129   rtx varargs_label = NULL_RTX;
11130   rtx fn;
11131
11132   gcc_assert (flag_split_stack && reload_completed);
11133
11134   ix86_finalize_stack_realign_flags ();
11135   ix86_compute_frame_layout (&frame);
11136   allocate = frame.stack_pointer_offset - INCOMING_FRAME_SP_OFFSET;
11137
11138   /* This is the label we will branch to if we have enough stack
11139      space.  We expect the basic block reordering pass to reverse this
11140      branch if optimizing, so that we branch in the unlikely case.  */
11141   label = gen_label_rtx ();
11142
11143   /* We need to compare the stack pointer minus the frame size with
11144      the stack boundary in the TCB.  The stack boundary always gives
11145      us SPLIT_STACK_AVAILABLE bytes, so if we need less than that we
11146      can compare directly.  Otherwise we need to do an addition.  */
11147
11148   limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
11149                           UNSPEC_STACK_CHECK);
11150   limit = gen_rtx_CONST (Pmode, limit);
11151   limit = gen_rtx_MEM (Pmode, limit);
11152   if (allocate < SPLIT_STACK_AVAILABLE)
11153     current = stack_pointer_rtx;
11154   else
11155     {
11156       unsigned int scratch_regno;
11157       rtx offset;
11158
11159       /* We need a scratch register to hold the stack pointer minus
11160          the required frame size.  Since this is the very start of the
11161          function, the scratch register can be any caller-saved
11162          register which is not used for parameters.  */
11163       offset = GEN_INT (- allocate);
11164       scratch_regno = split_stack_prologue_scratch_regno ();
11165       if (scratch_regno == INVALID_REGNUM)
11166         return;
11167       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11168       if (!TARGET_64BIT || x86_64_immediate_operand (offset, Pmode))
11169         {
11170           /* We don't use ix86_gen_add3 in this case because it will
11171              want to split to lea, but when not optimizing the insn
11172              will not be split after this point.  */
11173           emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11174                                   gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11175                                                 offset)));
11176         }
11177       else
11178         {
11179           emit_move_insn (scratch_reg, offset);
11180           emit_insn (gen_adddi3 (scratch_reg, scratch_reg,
11181                                  stack_pointer_rtx));
11182         }
11183       current = scratch_reg;
11184     }
11185
11186   ix86_expand_branch (GEU, current, limit, label);
11187   jump_insn = get_last_insn ();
11188   JUMP_LABEL (jump_insn) = label;
11189
11190   /* Mark the jump as very likely to be taken.  */
11191   add_reg_note (jump_insn, REG_BR_PROB,
11192                 GEN_INT (REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100));
11193
11194   if (split_stack_fn == NULL_RTX)
11195     split_stack_fn = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
11196   fn = split_stack_fn;
11197
11198   /* Get more stack space.  We pass in the desired stack space and the
11199      size of the arguments to copy to the new stack.  In 32-bit mode
11200      we push the parameters; __morestack will return on a new stack
11201      anyhow.  In 64-bit mode we pass the parameters in r10 and
11202      r11.  */
11203   allocate_rtx = GEN_INT (allocate);
11204   args_size = crtl->args.size >= 0 ? crtl->args.size : 0;
11205   call_fusage = NULL_RTX;
11206   if (TARGET_64BIT)
11207     {
11208       rtx reg10, reg11;
11209
11210       reg10 = gen_rtx_REG (Pmode, R10_REG);
11211       reg11 = gen_rtx_REG (Pmode, R11_REG);
11212
11213       /* If this function uses a static chain, it will be in %r10.
11214          Preserve it across the call to __morestack.  */
11215       if (DECL_STATIC_CHAIN (cfun->decl))
11216         {
11217           rtx rax;
11218
11219           rax = gen_rtx_REG (Pmode, AX_REG);
11220           emit_move_insn (rax, reg10);
11221           use_reg (&call_fusage, rax);
11222         }
11223
11224       if (ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
11225         {
11226           HOST_WIDE_INT argval;
11227
11228           /* When using the large model we need to load the address
11229              into a register, and we've run out of registers.  So we
11230              switch to a different calling convention, and we call a
11231              different function: __morestack_large.  We pass the
11232              argument size in the upper 32 bits of r10 and pass the
11233              frame size in the lower 32 bits.  */
11234           gcc_assert ((allocate & (HOST_WIDE_INT) 0xffffffff) == allocate);
11235           gcc_assert ((args_size & 0xffffffff) == args_size);
11236
11237           if (split_stack_fn_large == NULL_RTX)
11238             split_stack_fn_large =
11239               gen_rtx_SYMBOL_REF (Pmode, "__morestack_large_model");
11240
11241           if (ix86_cmodel == CM_LARGE_PIC)
11242             {
11243               rtx label, x;
11244
11245               label = gen_label_rtx ();
11246               emit_label (label);
11247               LABEL_PRESERVE_P (label) = 1;
11248               emit_insn (gen_set_rip_rex64 (reg10, label));
11249               emit_insn (gen_set_got_offset_rex64 (reg11, label));
11250               emit_insn (gen_adddi3 (reg10, reg10, reg11));
11251               x = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, split_stack_fn_large),
11252                                   UNSPEC_GOT);
11253               x = gen_rtx_CONST (Pmode, x);
11254               emit_move_insn (reg11, x);
11255               x = gen_rtx_PLUS (Pmode, reg10, reg11);
11256               x = gen_const_mem (Pmode, x);
11257               emit_move_insn (reg11, x);
11258             }
11259           else
11260             emit_move_insn (reg11, split_stack_fn_large);
11261
11262           fn = reg11;
11263
11264           argval = ((args_size << 16) << 16) + allocate;
11265           emit_move_insn (reg10, GEN_INT (argval));
11266         }
11267       else
11268         {
11269           emit_move_insn (reg10, allocate_rtx);
11270           emit_move_insn (reg11, GEN_INT (args_size));
11271           use_reg (&call_fusage, reg11);
11272         }
11273
11274       use_reg (&call_fusage, reg10);
11275     }
11276   else
11277     {
11278       emit_insn (gen_push (GEN_INT (args_size)));
11279       emit_insn (gen_push (allocate_rtx));
11280     }
11281   call_insn = ix86_expand_call (NULL_RTX, gen_rtx_MEM (QImode, fn),
11282                                 GEN_INT (UNITS_PER_WORD), constm1_rtx,
11283                                 NULL_RTX, false);
11284   add_function_usage_to (call_insn, call_fusage);
11285
11286   /* In order to make call/return prediction work right, we now need
11287      to execute a return instruction.  See
11288      libgcc/config/i386/morestack.S for the details on how this works.
11289
11290      For flow purposes gcc must not see this as a return
11291      instruction--we need control flow to continue at the subsequent
11292      label.  Therefore, we use an unspec.  */
11293   gcc_assert (crtl->args.pops_args < 65536);
11294   emit_insn (gen_split_stack_return (GEN_INT (crtl->args.pops_args)));
11295
11296   /* If we are in 64-bit mode and this function uses a static chain,
11297      we saved %r10 in %rax before calling _morestack.  */
11298   if (TARGET_64BIT && DECL_STATIC_CHAIN (cfun->decl))
11299     emit_move_insn (gen_rtx_REG (Pmode, R10_REG),
11300                     gen_rtx_REG (Pmode, AX_REG));
11301
11302   /* If this function calls va_start, we need to store a pointer to
11303      the arguments on the old stack, because they may not have been
11304      all copied to the new stack.  At this point the old stack can be
11305      found at the frame pointer value used by __morestack, because
11306      __morestack has set that up before calling back to us.  Here we
11307      store that pointer in a scratch register, and in
11308      ix86_expand_prologue we store the scratch register in a stack
11309      slot.  */
11310   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11311     {
11312       unsigned int scratch_regno;
11313       rtx frame_reg;
11314       int words;
11315
11316       scratch_regno = split_stack_prologue_scratch_regno ();
11317       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11318       frame_reg = gen_rtx_REG (Pmode, BP_REG);
11319
11320       /* 64-bit:
11321          fp -> old fp value
11322                return address within this function
11323                return address of caller of this function
11324                stack arguments
11325          So we add three words to get to the stack arguments.
11326
11327          32-bit:
11328          fp -> old fp value
11329                return address within this function
11330                first argument to __morestack
11331                second argument to __morestack
11332                return address of caller of this function
11333                stack arguments
11334          So we add five words to get to the stack arguments.
11335       */
11336       words = TARGET_64BIT ? 3 : 5;
11337       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11338                               gen_rtx_PLUS (Pmode, frame_reg,
11339                                             GEN_INT (words * UNITS_PER_WORD))));
11340
11341       varargs_label = gen_label_rtx ();
11342       emit_jump_insn (gen_jump (varargs_label));
11343       JUMP_LABEL (get_last_insn ()) = varargs_label;
11344
11345       emit_barrier ();
11346     }
11347
11348   emit_label (label);
11349   LABEL_NUSES (label) = 1;
11350
11351   /* If this function calls va_start, we now have to set the scratch
11352      register for the case where we do not call __morestack.  In this
11353      case we need to set it based on the stack pointer.  */
11354   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11355     {
11356       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11357                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11358                                             GEN_INT (UNITS_PER_WORD))));
11359
11360       emit_label (varargs_label);
11361       LABEL_NUSES (varargs_label) = 1;
11362     }
11363 }
11364
11365 /* We may have to tell the dataflow pass that the split stack prologue
11366    is initializing a scratch register.  */
11367
11368 static void
11369 ix86_live_on_entry (bitmap regs)
11370 {
11371   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11372     {
11373       gcc_assert (flag_split_stack);
11374       bitmap_set_bit (regs, split_stack_prologue_scratch_regno ());
11375     }
11376 }
11377 \f
11378 /* Determine if op is suitable SUBREG RTX for address.  */
11379
11380 static bool
11381 ix86_address_subreg_operand (rtx op)
11382 {
11383   enum machine_mode mode;
11384
11385   if (!REG_P (op))
11386     return false;
11387
11388   mode = GET_MODE (op);
11389
11390   if (GET_MODE_CLASS (mode) != MODE_INT)
11391     return false;
11392
11393   /* Don't allow SUBREGs that span more than a word.  It can lead to spill
11394      failures when the register is one word out of a two word structure.  */
11395   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
11396     return false;
11397
11398   /* simplify_subreg does not handle stack pointer.  */
11399   if (REGNO (op) == STACK_POINTER_REGNUM)
11400     return false;
11401
11402   /* Allow only SUBREGs of non-eliminable hard registers.  */
11403   return register_no_elim_operand (op, mode);
11404 }
11405
11406 /* Extract the parts of an RTL expression that is a valid memory address
11407    for an instruction.  Return 0 if the structure of the address is
11408    grossly off.  Return -1 if the address contains ASHIFT, so it is not
11409    strictly valid, but still used for computing length of lea instruction.  */
11410
11411 int
11412 ix86_decompose_address (rtx addr, struct ix86_address *out)
11413 {
11414   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
11415   rtx base_reg, index_reg;
11416   HOST_WIDE_INT scale = 1;
11417   rtx scale_rtx = NULL_RTX;
11418   rtx tmp;
11419   int retval = 1;
11420   enum ix86_address_seg seg = SEG_DEFAULT;
11421
11422   /* Allow zero-extended SImode addresses,
11423      they will be emitted with addr32 prefix.  */
11424   if (TARGET_64BIT && GET_MODE (addr) == DImode)
11425     {
11426       if (GET_CODE (addr) == ZERO_EXTEND
11427           && GET_MODE (XEXP (addr, 0)) == SImode)
11428         addr = XEXP (addr, 0);
11429       else if (GET_CODE (addr) == AND
11430                && const_32bit_mask (XEXP (addr, 1), DImode))
11431         {
11432           addr = XEXP (addr, 0);
11433
11434           /* Strip subreg.  */
11435           if (GET_CODE (addr) == SUBREG
11436               && GET_MODE (SUBREG_REG (addr)) == SImode)
11437             addr = SUBREG_REG (addr);
11438         }
11439     }
11440
11441   if (REG_P (addr))
11442     base = addr;
11443   else if (GET_CODE (addr) == SUBREG)
11444     {
11445       if (ix86_address_subreg_operand (SUBREG_REG (addr)))
11446         base = addr;
11447       else
11448         return 0;
11449     }
11450   else if (GET_CODE (addr) == PLUS)
11451     {
11452       rtx addends[4], op;
11453       int n = 0, i;
11454
11455       op = addr;
11456       do
11457         {
11458           if (n >= 4)
11459             return 0;
11460           addends[n++] = XEXP (op, 1);
11461           op = XEXP (op, 0);
11462         }
11463       while (GET_CODE (op) == PLUS);
11464       if (n >= 4)
11465         return 0;
11466       addends[n] = op;
11467
11468       for (i = n; i >= 0; --i)
11469         {
11470           op = addends[i];
11471           switch (GET_CODE (op))
11472             {
11473             case MULT:
11474               if (index)
11475                 return 0;
11476               index = XEXP (op, 0);
11477               scale_rtx = XEXP (op, 1);
11478               break;
11479
11480             case ASHIFT:
11481               if (index)
11482                 return 0;
11483               index = XEXP (op, 0);
11484               tmp = XEXP (op, 1);
11485               if (!CONST_INT_P (tmp))
11486                 return 0;
11487               scale = INTVAL (tmp);
11488               if ((unsigned HOST_WIDE_INT) scale > 3)
11489                 return 0;
11490               scale = 1 << scale;
11491               break;
11492
11493             case UNSPEC:
11494               if (XINT (op, 1) == UNSPEC_TP
11495                   && TARGET_TLS_DIRECT_SEG_REFS
11496                   && seg == SEG_DEFAULT)
11497                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
11498               else
11499                 return 0;
11500               break;
11501
11502             case SUBREG:
11503               if (!ix86_address_subreg_operand (SUBREG_REG (op)))
11504                 return 0;
11505               /* FALLTHRU */
11506
11507             case REG:
11508               if (!base)
11509                 base = op;
11510               else if (!index)
11511                 index = op;
11512               else
11513                 return 0;
11514               break;
11515
11516             case CONST:
11517             case CONST_INT:
11518             case SYMBOL_REF:
11519             case LABEL_REF:
11520               if (disp)
11521                 return 0;
11522               disp = op;
11523               break;
11524
11525             default:
11526               return 0;
11527             }
11528         }
11529     }
11530   else if (GET_CODE (addr) == MULT)
11531     {
11532       index = XEXP (addr, 0);           /* index*scale */
11533       scale_rtx = XEXP (addr, 1);
11534     }
11535   else if (GET_CODE (addr) == ASHIFT)
11536     {
11537       /* We're called for lea too, which implements ashift on occasion.  */
11538       index = XEXP (addr, 0);
11539       tmp = XEXP (addr, 1);
11540       if (!CONST_INT_P (tmp))
11541         return 0;
11542       scale = INTVAL (tmp);
11543       if ((unsigned HOST_WIDE_INT) scale > 3)
11544         return 0;
11545       scale = 1 << scale;
11546       retval = -1;
11547     }
11548   else
11549     disp = addr;                        /* displacement */
11550
11551   if (index)
11552     {
11553       if (REG_P (index))
11554         ;
11555       else if (GET_CODE (index) == SUBREG
11556                && ix86_address_subreg_operand (SUBREG_REG (index)))
11557         ;
11558       else
11559         return 0;
11560     }
11561
11562   /* Extract the integral value of scale.  */
11563   if (scale_rtx)
11564     {
11565       if (!CONST_INT_P (scale_rtx))
11566         return 0;
11567       scale = INTVAL (scale_rtx);
11568     }
11569
11570   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
11571   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
11572
11573   /* Avoid useless 0 displacement.  */
11574   if (disp == const0_rtx && (base || index))
11575     disp = NULL_RTX;
11576
11577   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
11578   if (base_reg && index_reg && scale == 1
11579       && (index_reg == arg_pointer_rtx
11580           || index_reg == frame_pointer_rtx
11581           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
11582     {
11583       rtx tmp;
11584       tmp = base, base = index, index = tmp;
11585       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
11586     }
11587
11588   /* Special case: %ebp cannot be encoded as a base without a displacement.
11589      Similarly %r13.  */
11590   if (!disp
11591       && base_reg
11592       && (base_reg == hard_frame_pointer_rtx
11593           || base_reg == frame_pointer_rtx
11594           || base_reg == arg_pointer_rtx
11595           || (REG_P (base_reg)
11596               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
11597                   || REGNO (base_reg) == R13_REG))))
11598     disp = const0_rtx;
11599
11600   /* Special case: on K6, [%esi] makes the instruction vector decoded.
11601      Avoid this by transforming to [%esi+0].
11602      Reload calls address legitimization without cfun defined, so we need
11603      to test cfun for being non-NULL. */
11604   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
11605       && base_reg && !index_reg && !disp
11606       && REG_P (base_reg) && REGNO (base_reg) == SI_REG)
11607     disp = const0_rtx;
11608
11609   /* Special case: encode reg+reg instead of reg*2.  */
11610   if (!base && index && scale == 2)
11611     base = index, base_reg = index_reg, scale = 1;
11612
11613   /* Special case: scaling cannot be encoded without base or displacement.  */
11614   if (!base && !disp && index && scale != 1)
11615     disp = const0_rtx;
11616
11617   out->base = base;
11618   out->index = index;
11619   out->disp = disp;
11620   out->scale = scale;
11621   out->seg = seg;
11622
11623   return retval;
11624 }
11625 \f
11626 /* Return cost of the memory address x.
11627    For i386, it is better to use a complex address than let gcc copy
11628    the address into a reg and make a new pseudo.  But not if the address
11629    requires to two regs - that would mean more pseudos with longer
11630    lifetimes.  */
11631 static int
11632 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
11633 {
11634   struct ix86_address parts;
11635   int cost = 1;
11636   int ok = ix86_decompose_address (x, &parts);
11637
11638   gcc_assert (ok);
11639
11640   if (parts.base && GET_CODE (parts.base) == SUBREG)
11641     parts.base = SUBREG_REG (parts.base);
11642   if (parts.index && GET_CODE (parts.index) == SUBREG)
11643     parts.index = SUBREG_REG (parts.index);
11644
11645   /* Attempt to minimize number of registers in the address.  */
11646   if ((parts.base
11647        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
11648       || (parts.index
11649           && (!REG_P (parts.index)
11650               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
11651     cost++;
11652
11653   if (parts.base
11654       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
11655       && parts.index
11656       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
11657       && parts.base != parts.index)
11658     cost++;
11659
11660   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
11661      since it's predecode logic can't detect the length of instructions
11662      and it degenerates to vector decoded.  Increase cost of such
11663      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
11664      to split such addresses or even refuse such addresses at all.
11665
11666      Following addressing modes are affected:
11667       [base+scale*index]
11668       [scale*index+disp]
11669       [base+index]
11670
11671      The first and last case  may be avoidable by explicitly coding the zero in
11672      memory address, but I don't have AMD-K6 machine handy to check this
11673      theory.  */
11674
11675   if (TARGET_K6
11676       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
11677           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
11678           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
11679     cost += 10;
11680
11681   return cost;
11682 }
11683 \f
11684 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
11685    this is used for to form addresses to local data when -fPIC is in
11686    use.  */
11687
11688 static bool
11689 darwin_local_data_pic (rtx disp)
11690 {
11691   return (GET_CODE (disp) == UNSPEC
11692           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
11693 }
11694
11695 /* Determine if a given RTX is a valid constant.  We already know this
11696    satisfies CONSTANT_P.  */
11697
11698 static bool
11699 ix86_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
11700 {
11701   switch (GET_CODE (x))
11702     {
11703     case CONST:
11704       x = XEXP (x, 0);
11705
11706       if (GET_CODE (x) == PLUS)
11707         {
11708           if (!CONST_INT_P (XEXP (x, 1)))
11709             return false;
11710           x = XEXP (x, 0);
11711         }
11712
11713       if (TARGET_MACHO && darwin_local_data_pic (x))
11714         return true;
11715
11716       /* Only some unspecs are valid as "constants".  */
11717       if (GET_CODE (x) == UNSPEC)
11718         switch (XINT (x, 1))
11719           {
11720           case UNSPEC_GOT:
11721           case UNSPEC_GOTOFF:
11722           case UNSPEC_PLTOFF:
11723             return TARGET_64BIT;
11724           case UNSPEC_TPOFF:
11725           case UNSPEC_NTPOFF:
11726             x = XVECEXP (x, 0, 0);
11727             return (GET_CODE (x) == SYMBOL_REF
11728                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11729           case UNSPEC_DTPOFF:
11730             x = XVECEXP (x, 0, 0);
11731             return (GET_CODE (x) == SYMBOL_REF
11732                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
11733           default:
11734             return false;
11735           }
11736
11737       /* We must have drilled down to a symbol.  */
11738       if (GET_CODE (x) == LABEL_REF)
11739         return true;
11740       if (GET_CODE (x) != SYMBOL_REF)
11741         return false;
11742       /* FALLTHRU */
11743
11744     case SYMBOL_REF:
11745       /* TLS symbols are never valid.  */
11746       if (SYMBOL_REF_TLS_MODEL (x))
11747         return false;
11748
11749       /* DLLIMPORT symbols are never valid.  */
11750       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
11751           && SYMBOL_REF_DLLIMPORT_P (x))
11752         return false;
11753
11754 #if TARGET_MACHO
11755       /* mdynamic-no-pic */
11756       if (MACHO_DYNAMIC_NO_PIC_P)
11757         return machopic_symbol_defined_p (x);
11758 #endif
11759       break;
11760
11761     case CONST_DOUBLE:
11762       if (GET_MODE (x) == TImode
11763           && x != CONST0_RTX (TImode)
11764           && !TARGET_64BIT)
11765         return false;
11766       break;
11767
11768     case CONST_VECTOR:
11769       if (!standard_sse_constant_p (x))
11770         return false;
11771
11772     default:
11773       break;
11774     }
11775
11776   /* Otherwise we handle everything else in the move patterns.  */
11777   return true;
11778 }
11779
11780 /* Determine if it's legal to put X into the constant pool.  This
11781    is not possible for the address of thread-local symbols, which
11782    is checked above.  */
11783
11784 static bool
11785 ix86_cannot_force_const_mem (enum machine_mode mode, rtx x)
11786 {
11787   /* We can always put integral constants and vectors in memory.  */
11788   switch (GET_CODE (x))
11789     {
11790     case CONST_INT:
11791     case CONST_DOUBLE:
11792     case CONST_VECTOR:
11793       return false;
11794
11795     default:
11796       break;
11797     }
11798   return !ix86_legitimate_constant_p (mode, x);
11799 }
11800
11801
11802 /* Nonzero if the constant value X is a legitimate general operand
11803    when generating PIC code.  It is given that flag_pic is on and
11804    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
11805
11806 bool
11807 legitimate_pic_operand_p (rtx x)
11808 {
11809   rtx inner;
11810
11811   switch (GET_CODE (x))
11812     {
11813     case CONST:
11814       inner = XEXP (x, 0);
11815       if (GET_CODE (inner) == PLUS
11816           && CONST_INT_P (XEXP (inner, 1)))
11817         inner = XEXP (inner, 0);
11818
11819       /* Only some unspecs are valid as "constants".  */
11820       if (GET_CODE (inner) == UNSPEC)
11821         switch (XINT (inner, 1))
11822           {
11823           case UNSPEC_GOT:
11824           case UNSPEC_GOTOFF:
11825           case UNSPEC_PLTOFF:
11826             return TARGET_64BIT;
11827           case UNSPEC_TPOFF:
11828             x = XVECEXP (inner, 0, 0);
11829             return (GET_CODE (x) == SYMBOL_REF
11830                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11831           case UNSPEC_MACHOPIC_OFFSET:
11832             return legitimate_pic_address_disp_p (x);
11833           default:
11834             return false;
11835           }
11836       /* FALLTHRU */
11837
11838     case SYMBOL_REF:
11839     case LABEL_REF:
11840       return legitimate_pic_address_disp_p (x);
11841
11842     default:
11843       return true;
11844     }
11845 }
11846
11847 /* Determine if a given CONST RTX is a valid memory displacement
11848    in PIC mode.  */
11849
11850 bool
11851 legitimate_pic_address_disp_p (rtx disp)
11852 {
11853   bool saw_plus;
11854
11855   /* In 64bit mode we can allow direct addresses of symbols and labels
11856      when they are not dynamic symbols.  */
11857   if (TARGET_64BIT)
11858     {
11859       rtx op0 = disp, op1;
11860
11861       switch (GET_CODE (disp))
11862         {
11863         case LABEL_REF:
11864           return true;
11865
11866         case CONST:
11867           if (GET_CODE (XEXP (disp, 0)) != PLUS)
11868             break;
11869           op0 = XEXP (XEXP (disp, 0), 0);
11870           op1 = XEXP (XEXP (disp, 0), 1);
11871           if (!CONST_INT_P (op1)
11872               || INTVAL (op1) >= 16*1024*1024
11873               || INTVAL (op1) < -16*1024*1024)
11874             break;
11875           if (GET_CODE (op0) == LABEL_REF)
11876             return true;
11877           if (GET_CODE (op0) == CONST
11878               && GET_CODE (XEXP (op0, 0)) == UNSPEC
11879               && XINT (XEXP (op0, 0), 1) == UNSPEC_PCREL)
11880             return true;
11881           if (GET_CODE (op0) == UNSPEC
11882               && XINT (op0, 1) == UNSPEC_PCREL)
11883             return true;
11884           if (GET_CODE (op0) != SYMBOL_REF)
11885             break;
11886           /* FALLTHRU */
11887
11888         case SYMBOL_REF:
11889           /* TLS references should always be enclosed in UNSPEC.  */
11890           if (SYMBOL_REF_TLS_MODEL (op0))
11891             return false;
11892           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
11893               && ix86_cmodel != CM_LARGE_PIC)
11894             return true;
11895           break;
11896
11897         default:
11898           break;
11899         }
11900     }
11901   if (GET_CODE (disp) != CONST)
11902     return false;
11903   disp = XEXP (disp, 0);
11904
11905   if (TARGET_64BIT)
11906     {
11907       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
11908          of GOT tables.  We should not need these anyway.  */
11909       if (GET_CODE (disp) != UNSPEC
11910           || (XINT (disp, 1) != UNSPEC_GOTPCREL
11911               && XINT (disp, 1) != UNSPEC_GOTOFF
11912               && XINT (disp, 1) != UNSPEC_PCREL
11913               && XINT (disp, 1) != UNSPEC_PLTOFF))
11914         return false;
11915
11916       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
11917           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
11918         return false;
11919       return true;
11920     }
11921
11922   saw_plus = false;
11923   if (GET_CODE (disp) == PLUS)
11924     {
11925       if (!CONST_INT_P (XEXP (disp, 1)))
11926         return false;
11927       disp = XEXP (disp, 0);
11928       saw_plus = true;
11929     }
11930
11931   if (TARGET_MACHO && darwin_local_data_pic (disp))
11932     return true;
11933
11934   if (GET_CODE (disp) != UNSPEC)
11935     return false;
11936
11937   switch (XINT (disp, 1))
11938     {
11939     case UNSPEC_GOT:
11940       if (saw_plus)
11941         return false;
11942       /* We need to check for both symbols and labels because VxWorks loads
11943          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
11944          details.  */
11945       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11946               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
11947     case UNSPEC_GOTOFF:
11948       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
11949          While ABI specify also 32bit relocation but we don't produce it in
11950          small PIC model at all.  */
11951       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11952            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
11953           && !TARGET_64BIT)
11954         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
11955       return false;
11956     case UNSPEC_GOTTPOFF:
11957     case UNSPEC_GOTNTPOFF:
11958     case UNSPEC_INDNTPOFF:
11959       if (saw_plus)
11960         return false;
11961       disp = XVECEXP (disp, 0, 0);
11962       return (GET_CODE (disp) == SYMBOL_REF
11963               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
11964     case UNSPEC_NTPOFF:
11965       disp = XVECEXP (disp, 0, 0);
11966       return (GET_CODE (disp) == SYMBOL_REF
11967               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
11968     case UNSPEC_DTPOFF:
11969       disp = XVECEXP (disp, 0, 0);
11970       return (GET_CODE (disp) == SYMBOL_REF
11971               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
11972     }
11973
11974   return false;
11975 }
11976
11977 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
11978    replace the input X, or the original X if no replacement is called for.
11979    The output parameter *WIN is 1 if the calling macro should goto WIN,
11980    0 if it should not.  */
11981
11982 bool
11983 ix86_legitimize_reload_address (rtx x,
11984                                 enum machine_mode mode ATTRIBUTE_UNUSED,
11985                                 int opnum, int type,
11986                                 int ind_levels ATTRIBUTE_UNUSED)
11987 {
11988   /* Reload can generate:
11989
11990      (plus:DI (plus:DI (unspec:DI [(const_int 0 [0])] UNSPEC_TP)
11991                        (reg:DI 97))
11992               (reg:DI 2 cx))
11993
11994      This RTX is rejected from ix86_legitimate_address_p due to
11995      non-strictness of base register 97.  Following this rejection, 
11996      reload pushes all three components into separate registers,
11997      creating invalid memory address RTX.
11998
11999      Following code reloads only the invalid part of the
12000      memory address RTX.  */
12001
12002   if (GET_CODE (x) == PLUS
12003       && REG_P (XEXP (x, 1))
12004       && GET_CODE (XEXP (x, 0)) == PLUS
12005       && REG_P (XEXP (XEXP (x, 0), 1)))
12006     {
12007       rtx base, index;
12008       bool something_reloaded = false;
12009
12010       base = XEXP (XEXP (x, 0), 1);      
12011       if (!REG_OK_FOR_BASE_STRICT_P (base))
12012         {
12013           push_reload (base, NULL_RTX, &XEXP (XEXP (x, 0), 1), NULL,
12014                        BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
12015                        opnum, (enum reload_type)type);
12016           something_reloaded = true;
12017         }
12018
12019       index = XEXP (x, 1);
12020       if (!REG_OK_FOR_INDEX_STRICT_P (index))
12021         {
12022           push_reload (index, NULL_RTX, &XEXP (x, 1), NULL,
12023                        INDEX_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
12024                        opnum, (enum reload_type)type);
12025           something_reloaded = true;
12026         }
12027
12028       gcc_assert (something_reloaded);
12029       return true;
12030     }
12031
12032   return false;
12033 }
12034
12035 /* Recognizes RTL expressions that are valid memory addresses for an
12036    instruction.  The MODE argument is the machine mode for the MEM
12037    expression that wants to use this address.
12038
12039    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
12040    convert common non-canonical forms to canonical form so that they will
12041    be recognized.  */
12042
12043 static bool
12044 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
12045                            rtx addr, bool strict)
12046 {
12047   struct ix86_address parts;
12048   rtx base, index, disp;
12049   HOST_WIDE_INT scale;
12050
12051   /* Since constant address in x32 is signed extended to 64bit,
12052      we have to prevent addresses from 0x80000000 to 0xffffffff.  */
12053   if (TARGET_X32
12054       && CONST_INT_P (addr)
12055       && INTVAL (addr) < 0)
12056     return false;
12057
12058   if (ix86_decompose_address (addr, &parts) <= 0)
12059     /* Decomposition failed.  */
12060     return false;
12061
12062   base = parts.base;
12063   index = parts.index;
12064   disp = parts.disp;
12065   scale = parts.scale;
12066
12067   /* Validate base register.  */
12068   if (base)
12069     {
12070       rtx reg;
12071
12072       if (REG_P (base))
12073         reg = base;
12074       else if (GET_CODE (base) == SUBREG && REG_P (SUBREG_REG (base)))
12075         reg = SUBREG_REG (base);
12076       else
12077         /* Base is not a register.  */
12078         return false;
12079
12080       if (GET_MODE (base) != SImode && GET_MODE (base) != DImode)
12081         return false;
12082
12083       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
12084           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
12085         /* Base is not valid.  */
12086         return false;
12087     }
12088
12089   /* Validate index register.  */
12090   if (index)
12091     {
12092       rtx reg;
12093
12094       if (REG_P (index))
12095         reg = index;
12096       else if (GET_CODE (index) == SUBREG && REG_P (SUBREG_REG (index)))
12097         reg = SUBREG_REG (index);
12098       else
12099         /* Index is not a register.  */
12100         return false;
12101
12102       if (GET_MODE (index) != SImode && GET_MODE (index) != DImode)
12103         return false;
12104
12105       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
12106           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
12107         /* Index is not valid.  */
12108         return false;
12109     }
12110
12111   /* Index and base should have the same mode.  */
12112   if (base && index
12113       && GET_MODE (base) != GET_MODE (index))
12114     return false;
12115
12116   /* Validate scale factor.  */
12117   if (scale != 1)
12118     {
12119       if (!index)
12120         /* Scale without index.  */
12121         return false;
12122
12123       if (scale != 2 && scale != 4 && scale != 8)
12124         /* Scale is not a valid multiplier.  */
12125         return false;
12126     }
12127
12128   /* Validate displacement.  */
12129   if (disp)
12130     {
12131       if (GET_CODE (disp) == CONST
12132           && GET_CODE (XEXP (disp, 0)) == UNSPEC
12133           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
12134         switch (XINT (XEXP (disp, 0), 1))
12135           {
12136           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
12137              used.  While ABI specify also 32bit relocations, we don't produce
12138              them at all and use IP relative instead.  */
12139           case UNSPEC_GOT:
12140           case UNSPEC_GOTOFF:
12141             gcc_assert (flag_pic);
12142             if (!TARGET_64BIT)
12143               goto is_legitimate_pic;
12144
12145             /* 64bit address unspec.  */
12146             return false;
12147
12148           case UNSPEC_GOTPCREL:
12149           case UNSPEC_PCREL:
12150             gcc_assert (flag_pic);
12151             goto is_legitimate_pic;
12152
12153           case UNSPEC_GOTTPOFF:
12154           case UNSPEC_GOTNTPOFF:
12155           case UNSPEC_INDNTPOFF:
12156           case UNSPEC_NTPOFF:
12157           case UNSPEC_DTPOFF:
12158             break;
12159
12160           case UNSPEC_STACK_CHECK:
12161             gcc_assert (flag_split_stack);
12162             break;
12163
12164           default:
12165             /* Invalid address unspec.  */
12166             return false;
12167           }
12168
12169       else if (SYMBOLIC_CONST (disp)
12170                && (flag_pic
12171                    || (TARGET_MACHO
12172 #if TARGET_MACHO
12173                        && MACHOPIC_INDIRECT
12174                        && !machopic_operand_p (disp)
12175 #endif
12176                )))
12177         {
12178
12179         is_legitimate_pic:
12180           if (TARGET_64BIT && (index || base))
12181             {
12182               /* foo@dtpoff(%rX) is ok.  */
12183               if (GET_CODE (disp) != CONST
12184                   || GET_CODE (XEXP (disp, 0)) != PLUS
12185                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
12186                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
12187                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
12188                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
12189                 /* Non-constant pic memory reference.  */
12190                 return false;
12191             }
12192           else if ((!TARGET_MACHO || flag_pic)
12193                     && ! legitimate_pic_address_disp_p (disp))
12194             /* Displacement is an invalid pic construct.  */
12195             return false;
12196 #if TARGET_MACHO
12197           else if (MACHO_DYNAMIC_NO_PIC_P
12198                    && !ix86_legitimate_constant_p (Pmode, disp))
12199             /* displacment must be referenced via non_lazy_pointer */
12200             return false;
12201 #endif
12202
12203           /* This code used to verify that a symbolic pic displacement
12204              includes the pic_offset_table_rtx register.
12205
12206              While this is good idea, unfortunately these constructs may
12207              be created by "adds using lea" optimization for incorrect
12208              code like:
12209
12210              int a;
12211              int foo(int i)
12212                {
12213                  return *(&a+i);
12214                }
12215
12216              This code is nonsensical, but results in addressing
12217              GOT table with pic_offset_table_rtx base.  We can't
12218              just refuse it easily, since it gets matched by
12219              "addsi3" pattern, that later gets split to lea in the
12220              case output register differs from input.  While this
12221              can be handled by separate addsi pattern for this case
12222              that never results in lea, this seems to be easier and
12223              correct fix for crash to disable this test.  */
12224         }
12225       else if (GET_CODE (disp) != LABEL_REF
12226                && !CONST_INT_P (disp)
12227                && (GET_CODE (disp) != CONST
12228                    || !ix86_legitimate_constant_p (Pmode, disp))
12229                && (GET_CODE (disp) != SYMBOL_REF
12230                    || !ix86_legitimate_constant_p (Pmode, disp)))
12231         /* Displacement is not constant.  */
12232         return false;
12233       else if (TARGET_64BIT
12234                && !x86_64_immediate_operand (disp, VOIDmode))
12235         /* Displacement is out of range.  */
12236         return false;
12237     }
12238
12239   /* Everything looks valid.  */
12240   return true;
12241 }
12242
12243 /* Determine if a given RTX is a valid constant address.  */
12244
12245 bool
12246 constant_address_p (rtx x)
12247 {
12248   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
12249 }
12250 \f
12251 /* Return a unique alias set for the GOT.  */
12252
12253 static alias_set_type
12254 ix86_GOT_alias_set (void)
12255 {
12256   static alias_set_type set = -1;
12257   if (set == -1)
12258     set = new_alias_set ();
12259   return set;
12260 }
12261
12262 /* Return a legitimate reference for ORIG (an address) using the
12263    register REG.  If REG is 0, a new pseudo is generated.
12264
12265    There are two types of references that must be handled:
12266
12267    1. Global data references must load the address from the GOT, via
12268       the PIC reg.  An insn is emitted to do this load, and the reg is
12269       returned.
12270
12271    2. Static data references, constant pool addresses, and code labels
12272       compute the address as an offset from the GOT, whose base is in
12273       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
12274       differentiate them from global data objects.  The returned
12275       address is the PIC reg + an unspec constant.
12276
12277    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
12278    reg also appears in the address.  */
12279
12280 static rtx
12281 legitimize_pic_address (rtx orig, rtx reg)
12282 {
12283   rtx addr = orig;
12284   rtx new_rtx = orig;
12285   rtx base;
12286
12287 #if TARGET_MACHO
12288   if (TARGET_MACHO && !TARGET_64BIT)
12289     {
12290       if (reg == 0)
12291         reg = gen_reg_rtx (Pmode);
12292       /* Use the generic Mach-O PIC machinery.  */
12293       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
12294     }
12295 #endif
12296
12297   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
12298     new_rtx = addr;
12299   else if (TARGET_64BIT
12300            && ix86_cmodel != CM_SMALL_PIC
12301            && gotoff_operand (addr, Pmode))
12302     {
12303       rtx tmpreg;
12304       /* This symbol may be referenced via a displacement from the PIC
12305          base address (@GOTOFF).  */
12306
12307       if (reload_in_progress)
12308         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12309       if (GET_CODE (addr) == CONST)
12310         addr = XEXP (addr, 0);
12311       if (GET_CODE (addr) == PLUS)
12312           {
12313             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12314                                       UNSPEC_GOTOFF);
12315             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12316           }
12317         else
12318           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12319       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12320       if (!reg)
12321         tmpreg = gen_reg_rtx (Pmode);
12322       else
12323         tmpreg = reg;
12324       emit_move_insn (tmpreg, new_rtx);
12325
12326       if (reg != 0)
12327         {
12328           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
12329                                          tmpreg, 1, OPTAB_DIRECT);
12330           new_rtx = reg;
12331         }
12332       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
12333     }
12334   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
12335     {
12336       /* This symbol may be referenced via a displacement from the PIC
12337          base address (@GOTOFF).  */
12338
12339       if (reload_in_progress)
12340         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12341       if (GET_CODE (addr) == CONST)
12342         addr = XEXP (addr, 0);
12343       if (GET_CODE (addr) == PLUS)
12344           {
12345             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12346                                       UNSPEC_GOTOFF);
12347             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12348           }
12349         else
12350           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12351       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12352       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12353
12354       if (reg != 0)
12355         {
12356           emit_move_insn (reg, new_rtx);
12357           new_rtx = reg;
12358         }
12359     }
12360   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
12361            /* We can't use @GOTOFF for text labels on VxWorks;
12362               see gotoff_operand.  */
12363            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
12364     {
12365       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12366         {
12367           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
12368             return legitimize_dllimport_symbol (addr, true);
12369           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
12370               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
12371               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
12372             {
12373               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
12374               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
12375             }
12376         }
12377
12378       /* For x64 PE-COFF there is no GOT table.  So we use address
12379          directly.  */
12380       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
12381       {
12382           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_PCREL);
12383           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12384
12385           if (reg == 0)
12386             reg = gen_reg_rtx (Pmode);
12387           emit_move_insn (reg, new_rtx);
12388           new_rtx = reg;
12389       }
12390       else if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
12391         {
12392           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
12393           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12394           new_rtx = gen_const_mem (Pmode, new_rtx);
12395           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12396
12397           if (reg == 0)
12398             reg = gen_reg_rtx (Pmode);
12399           /* Use directly gen_movsi, otherwise the address is loaded
12400              into register for CSE.  We don't want to CSE this addresses,
12401              instead we CSE addresses from the GOT table, so skip this.  */
12402           emit_insn (gen_movsi (reg, new_rtx));
12403           new_rtx = reg;
12404         }
12405       else
12406         {
12407           /* This symbol must be referenced via a load from the
12408              Global Offset Table (@GOT).  */
12409
12410           if (reload_in_progress)
12411             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12412           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
12413           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12414           if (TARGET_64BIT)
12415             new_rtx = force_reg (Pmode, new_rtx);
12416           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12417           new_rtx = gen_const_mem (Pmode, new_rtx);
12418           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12419
12420           if (reg == 0)
12421             reg = gen_reg_rtx (Pmode);
12422           emit_move_insn (reg, new_rtx);
12423           new_rtx = reg;
12424         }
12425     }
12426   else
12427     {
12428       if (CONST_INT_P (addr)
12429           && !x86_64_immediate_operand (addr, VOIDmode))
12430         {
12431           if (reg)
12432             {
12433               emit_move_insn (reg, addr);
12434               new_rtx = reg;
12435             }
12436           else
12437             new_rtx = force_reg (Pmode, addr);
12438         }
12439       else if (GET_CODE (addr) == CONST)
12440         {
12441           addr = XEXP (addr, 0);
12442
12443           /* We must match stuff we generate before.  Assume the only
12444              unspecs that can get here are ours.  Not that we could do
12445              anything with them anyway....  */
12446           if (GET_CODE (addr) == UNSPEC
12447               || (GET_CODE (addr) == PLUS
12448                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
12449             return orig;
12450           gcc_assert (GET_CODE (addr) == PLUS);
12451         }
12452       if (GET_CODE (addr) == PLUS)
12453         {
12454           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
12455
12456           /* Check first to see if this is a constant offset from a @GOTOFF
12457              symbol reference.  */
12458           if (gotoff_operand (op0, Pmode)
12459               && CONST_INT_P (op1))
12460             {
12461               if (!TARGET_64BIT)
12462                 {
12463                   if (reload_in_progress)
12464                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12465                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
12466                                             UNSPEC_GOTOFF);
12467                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
12468                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12469                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12470
12471                   if (reg != 0)
12472                     {
12473                       emit_move_insn (reg, new_rtx);
12474                       new_rtx = reg;
12475                     }
12476                 }
12477               else
12478                 {
12479                   if (INTVAL (op1) < -16*1024*1024
12480                       || INTVAL (op1) >= 16*1024*1024)
12481                     {
12482                       if (!x86_64_immediate_operand (op1, Pmode))
12483                         op1 = force_reg (Pmode, op1);
12484                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
12485                     }
12486                 }
12487             }
12488           else
12489             {
12490               base = legitimize_pic_address (XEXP (addr, 0), reg);
12491               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
12492                                                  base == reg ? NULL_RTX : reg);
12493
12494               if (CONST_INT_P (new_rtx))
12495                 new_rtx = plus_constant (base, INTVAL (new_rtx));
12496               else
12497                 {
12498                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
12499                     {
12500                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
12501                       new_rtx = XEXP (new_rtx, 1);
12502                     }
12503                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
12504                 }
12505             }
12506         }
12507     }
12508   return new_rtx;
12509 }
12510 \f
12511 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
12512
12513 static rtx
12514 get_thread_pointer (bool to_reg)
12515 {
12516   rtx tp = gen_rtx_UNSPEC (ptr_mode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
12517
12518   if (GET_MODE (tp) != Pmode)
12519     tp = convert_to_mode (Pmode, tp, 1);
12520
12521   if (to_reg)
12522     tp = copy_addr_to_reg (tp);
12523
12524   return tp;
12525 }
12526
12527 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
12528
12529 static GTY(()) rtx ix86_tls_symbol;
12530
12531 static rtx
12532 ix86_tls_get_addr (void)
12533 {
12534   if (!ix86_tls_symbol)
12535     {
12536       const char *sym
12537         = ((TARGET_ANY_GNU_TLS && !TARGET_64BIT)
12538            ? "___tls_get_addr" : "__tls_get_addr");
12539
12540       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, sym);
12541     }
12542
12543   return ix86_tls_symbol;
12544 }
12545
12546 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
12547
12548 static GTY(()) rtx ix86_tls_module_base_symbol;
12549
12550 rtx
12551 ix86_tls_module_base (void)
12552 {
12553   if (!ix86_tls_module_base_symbol)
12554     {
12555       ix86_tls_module_base_symbol
12556         = gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
12557
12558       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
12559         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
12560     }
12561
12562   return ix86_tls_module_base_symbol;
12563 }
12564
12565 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
12566    false if we expect this to be used for a memory address and true if
12567    we expect to load the address into a register.  */
12568
12569 static rtx
12570 legitimize_tls_address (rtx x, enum tls_model model, bool for_mov)
12571 {
12572   rtx dest, base, off;
12573   rtx pic = NULL_RTX, tp = NULL_RTX;
12574   int type;
12575
12576   switch (model)
12577     {
12578     case TLS_MODEL_GLOBAL_DYNAMIC:
12579       dest = gen_reg_rtx (Pmode);
12580
12581       if (!TARGET_64BIT)
12582         {
12583           if (flag_pic)
12584             pic = pic_offset_table_rtx;
12585           else
12586             {
12587               pic = gen_reg_rtx (Pmode);
12588               emit_insn (gen_set_got (pic));
12589             }
12590         }
12591
12592       if (TARGET_GNU2_TLS)
12593         {
12594           if (TARGET_64BIT)
12595             emit_insn (gen_tls_dynamic_gnu2_64 (dest, x));
12596           else
12597             emit_insn (gen_tls_dynamic_gnu2_32 (dest, x, pic));
12598
12599           tp = get_thread_pointer (true);
12600           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
12601
12602           set_unique_reg_note (get_last_insn (), REG_EQUAL, x);
12603         }
12604       else
12605         {
12606           rtx caddr = ix86_tls_get_addr ();
12607
12608           if (TARGET_64BIT)
12609             {
12610               rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
12611
12612               start_sequence ();
12613               emit_call_insn (gen_tls_global_dynamic_64 (rax, x, caddr));
12614               insns = get_insns ();
12615               end_sequence ();
12616
12617               RTL_CONST_CALL_P (insns) = 1;
12618               emit_libcall_block (insns, dest, rax, x);
12619             }
12620           else
12621             emit_insn (gen_tls_global_dynamic_32 (dest, x, pic, caddr));
12622         }
12623       break;
12624
12625     case TLS_MODEL_LOCAL_DYNAMIC:
12626       base = gen_reg_rtx (Pmode);
12627
12628       if (!TARGET_64BIT)
12629         {
12630           if (flag_pic)
12631             pic = pic_offset_table_rtx;
12632           else
12633             {
12634               pic = gen_reg_rtx (Pmode);
12635               emit_insn (gen_set_got (pic));
12636             }
12637         }
12638
12639       if (TARGET_GNU2_TLS)
12640         {
12641           rtx tmp = ix86_tls_module_base ();
12642
12643           if (TARGET_64BIT)
12644             emit_insn (gen_tls_dynamic_gnu2_64 (base, tmp));
12645           else
12646             emit_insn (gen_tls_dynamic_gnu2_32 (base, tmp, pic));
12647
12648           tp = get_thread_pointer (true);
12649           set_unique_reg_note (get_last_insn (), REG_EQUAL,
12650                                gen_rtx_MINUS (Pmode, tmp, tp));
12651         }
12652       else
12653         {
12654           rtx caddr = ix86_tls_get_addr ();
12655
12656           if (TARGET_64BIT)
12657             {
12658               rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, eqv;
12659
12660               start_sequence ();
12661               emit_call_insn (gen_tls_local_dynamic_base_64 (rax, caddr));
12662               insns = get_insns ();
12663               end_sequence ();
12664
12665               /* Attach a unique REG_EQUAL, to allow the RTL optimizers to
12666                  share the LD_BASE result with other LD model accesses.  */
12667               eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
12668                                     UNSPEC_TLS_LD_BASE);
12669
12670               RTL_CONST_CALL_P (insns) = 1;
12671               emit_libcall_block (insns, base, rax, eqv);
12672             }
12673           else
12674             emit_insn (gen_tls_local_dynamic_base_32 (base, pic, caddr));
12675         }
12676
12677       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
12678       off = gen_rtx_CONST (Pmode, off);
12679
12680       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
12681
12682       if (TARGET_GNU2_TLS)
12683         {
12684           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
12685
12686           set_unique_reg_note (get_last_insn (), REG_EQUAL, x);
12687         }
12688       break;
12689
12690     case TLS_MODEL_INITIAL_EXEC:
12691       if (TARGET_64BIT)
12692         {
12693           if (TARGET_SUN_TLS)
12694             {
12695               /* The Sun linker took the AMD64 TLS spec literally
12696                  and can only handle %rax as destination of the
12697                  initial executable code sequence.  */
12698
12699               dest = gen_reg_rtx (Pmode);
12700               emit_insn (gen_tls_initial_exec_64_sun (dest, x));
12701               return dest;
12702             }
12703
12704           pic = NULL;
12705           type = UNSPEC_GOTNTPOFF;
12706         }
12707       else if (flag_pic)
12708         {
12709           if (reload_in_progress)
12710             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12711           pic = pic_offset_table_rtx;
12712           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
12713         }
12714       else if (!TARGET_ANY_GNU_TLS)
12715         {
12716           pic = gen_reg_rtx (Pmode);
12717           emit_insn (gen_set_got (pic));
12718           type = UNSPEC_GOTTPOFF;
12719         }
12720       else
12721         {
12722           pic = NULL;
12723           type = UNSPEC_INDNTPOFF;
12724         }
12725
12726       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
12727       off = gen_rtx_CONST (Pmode, off);
12728       if (pic)
12729         off = gen_rtx_PLUS (Pmode, pic, off);
12730       off = gen_const_mem (Pmode, off);
12731       set_mem_alias_set (off, ix86_GOT_alias_set ());
12732
12733       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12734         {
12735           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12736           off = force_reg (Pmode, off);
12737           return gen_rtx_PLUS (Pmode, base, off);
12738         }
12739       else
12740         {
12741           base = get_thread_pointer (true);
12742           dest = gen_reg_rtx (Pmode);
12743           emit_insn (gen_subsi3 (dest, base, off));
12744         }
12745       break;
12746
12747     case TLS_MODEL_LOCAL_EXEC:
12748       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
12749                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12750                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
12751       off = gen_rtx_CONST (Pmode, off);
12752
12753       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12754         {
12755           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12756           return gen_rtx_PLUS (Pmode, base, off);
12757         }
12758       else
12759         {
12760           base = get_thread_pointer (true);
12761           dest = gen_reg_rtx (Pmode);
12762           emit_insn (gen_subsi3 (dest, base, off));
12763         }
12764       break;
12765
12766     default:
12767       gcc_unreachable ();
12768     }
12769
12770   return dest;
12771 }
12772
12773 /* Create or return the unique __imp_DECL dllimport symbol corresponding
12774    to symbol DECL.  */
12775
12776 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
12777   htab_t dllimport_map;
12778
12779 static tree
12780 get_dllimport_decl (tree decl)
12781 {
12782   struct tree_map *h, in;
12783   void **loc;
12784   const char *name;
12785   const char *prefix;
12786   size_t namelen, prefixlen;
12787   char *imp_name;
12788   tree to;
12789   rtx rtl;
12790
12791   if (!dllimport_map)
12792     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
12793
12794   in.hash = htab_hash_pointer (decl);
12795   in.base.from = decl;
12796   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
12797   h = (struct tree_map *) *loc;
12798   if (h)
12799     return h->to;
12800
12801   *loc = h = ggc_alloc_tree_map ();
12802   h->hash = in.hash;
12803   h->base.from = decl;
12804   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
12805                            VAR_DECL, NULL, ptr_type_node);
12806   DECL_ARTIFICIAL (to) = 1;
12807   DECL_IGNORED_P (to) = 1;
12808   DECL_EXTERNAL (to) = 1;
12809   TREE_READONLY (to) = 1;
12810
12811   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
12812   name = targetm.strip_name_encoding (name);
12813   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
12814     ? "*__imp_" : "*__imp__";
12815   namelen = strlen (name);
12816   prefixlen = strlen (prefix);
12817   imp_name = (char *) alloca (namelen + prefixlen + 1);
12818   memcpy (imp_name, prefix, prefixlen);
12819   memcpy (imp_name + prefixlen, name, namelen + 1);
12820
12821   name = ggc_alloc_string (imp_name, namelen + prefixlen);
12822   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
12823   SET_SYMBOL_REF_DECL (rtl, to);
12824   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
12825
12826   rtl = gen_const_mem (Pmode, rtl);
12827   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
12828
12829   SET_DECL_RTL (to, rtl);
12830   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
12831
12832   return to;
12833 }
12834
12835 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
12836    true if we require the result be a register.  */
12837
12838 static rtx
12839 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
12840 {
12841   tree imp_decl;
12842   rtx x;
12843
12844   gcc_assert (SYMBOL_REF_DECL (symbol));
12845   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
12846
12847   x = DECL_RTL (imp_decl);
12848   if (want_reg)
12849     x = force_reg (Pmode, x);
12850   return x;
12851 }
12852
12853 /* Try machine-dependent ways of modifying an illegitimate address
12854    to be legitimate.  If we find one, return the new, valid address.
12855    This macro is used in only one place: `memory_address' in explow.c.
12856
12857    OLDX is the address as it was before break_out_memory_refs was called.
12858    In some cases it is useful to look at this to decide what needs to be done.
12859
12860    It is always safe for this macro to do nothing.  It exists to recognize
12861    opportunities to optimize the output.
12862
12863    For the 80386, we handle X+REG by loading X into a register R and
12864    using R+REG.  R will go in a general reg and indexing will be used.
12865    However, if REG is a broken-out memory address or multiplication,
12866    nothing needs to be done because REG can certainly go in a general reg.
12867
12868    When -fpic is used, special handling is needed for symbolic references.
12869    See comments by legitimize_pic_address in i386.c for details.  */
12870
12871 static rtx
12872 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
12873                          enum machine_mode mode)
12874 {
12875   int changed = 0;
12876   unsigned log;
12877
12878   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
12879   if (log)
12880     return legitimize_tls_address (x, (enum tls_model) log, false);
12881   if (GET_CODE (x) == CONST
12882       && GET_CODE (XEXP (x, 0)) == PLUS
12883       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12884       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
12885     {
12886       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
12887                                       (enum tls_model) log, false);
12888       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12889     }
12890
12891   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12892     {
12893       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
12894         return legitimize_dllimport_symbol (x, true);
12895       if (GET_CODE (x) == CONST
12896           && GET_CODE (XEXP (x, 0)) == PLUS
12897           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12898           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
12899         {
12900           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
12901           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12902         }
12903     }
12904
12905   if (flag_pic && SYMBOLIC_CONST (x))
12906     return legitimize_pic_address (x, 0);
12907
12908 #if TARGET_MACHO
12909   if (MACHO_DYNAMIC_NO_PIC_P && SYMBOLIC_CONST (x))
12910     return machopic_indirect_data_reference (x, 0);
12911 #endif
12912
12913   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
12914   if (GET_CODE (x) == ASHIFT
12915       && CONST_INT_P (XEXP (x, 1))
12916       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
12917     {
12918       changed = 1;
12919       log = INTVAL (XEXP (x, 1));
12920       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
12921                         GEN_INT (1 << log));
12922     }
12923
12924   if (GET_CODE (x) == PLUS)
12925     {
12926       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
12927
12928       if (GET_CODE (XEXP (x, 0)) == ASHIFT
12929           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
12930           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
12931         {
12932           changed = 1;
12933           log = INTVAL (XEXP (XEXP (x, 0), 1));
12934           XEXP (x, 0) = gen_rtx_MULT (Pmode,
12935                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
12936                                       GEN_INT (1 << log));
12937         }
12938
12939       if (GET_CODE (XEXP (x, 1)) == ASHIFT
12940           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
12941           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
12942         {
12943           changed = 1;
12944           log = INTVAL (XEXP (XEXP (x, 1), 1));
12945           XEXP (x, 1) = gen_rtx_MULT (Pmode,
12946                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
12947                                       GEN_INT (1 << log));
12948         }
12949
12950       /* Put multiply first if it isn't already.  */
12951       if (GET_CODE (XEXP (x, 1)) == MULT)
12952         {
12953           rtx tmp = XEXP (x, 0);
12954           XEXP (x, 0) = XEXP (x, 1);
12955           XEXP (x, 1) = tmp;
12956           changed = 1;
12957         }
12958
12959       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
12960          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
12961          created by virtual register instantiation, register elimination, and
12962          similar optimizations.  */
12963       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
12964         {
12965           changed = 1;
12966           x = gen_rtx_PLUS (Pmode,
12967                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
12968                                           XEXP (XEXP (x, 1), 0)),
12969                             XEXP (XEXP (x, 1), 1));
12970         }
12971
12972       /* Canonicalize
12973          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
12974          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
12975       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
12976                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
12977                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
12978                && CONSTANT_P (XEXP (x, 1)))
12979         {
12980           rtx constant;
12981           rtx other = NULL_RTX;
12982
12983           if (CONST_INT_P (XEXP (x, 1)))
12984             {
12985               constant = XEXP (x, 1);
12986               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
12987             }
12988           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
12989             {
12990               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
12991               other = XEXP (x, 1);
12992             }
12993           else
12994             constant = 0;
12995
12996           if (constant)
12997             {
12998               changed = 1;
12999               x = gen_rtx_PLUS (Pmode,
13000                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
13001                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
13002                                 plus_constant (other, INTVAL (constant)));
13003             }
13004         }
13005
13006       if (changed && ix86_legitimate_address_p (mode, x, false))
13007         return x;
13008
13009       if (GET_CODE (XEXP (x, 0)) == MULT)
13010         {
13011           changed = 1;
13012           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
13013         }
13014
13015       if (GET_CODE (XEXP (x, 1)) == MULT)
13016         {
13017           changed = 1;
13018           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
13019         }
13020
13021       if (changed
13022           && REG_P (XEXP (x, 1))
13023           && REG_P (XEXP (x, 0)))
13024         return x;
13025
13026       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
13027         {
13028           changed = 1;
13029           x = legitimize_pic_address (x, 0);
13030         }
13031
13032       if (changed && ix86_legitimate_address_p (mode, x, false))
13033         return x;
13034
13035       if (REG_P (XEXP (x, 0)))
13036         {
13037           rtx temp = gen_reg_rtx (Pmode);
13038           rtx val  = force_operand (XEXP (x, 1), temp);
13039           if (val != temp)
13040             {
13041               if (GET_MODE (val) != Pmode)
13042                 val = convert_to_mode (Pmode, val, 1);
13043               emit_move_insn (temp, val);
13044             }
13045
13046           XEXP (x, 1) = temp;
13047           return x;
13048         }
13049
13050       else if (REG_P (XEXP (x, 1)))
13051         {
13052           rtx temp = gen_reg_rtx (Pmode);
13053           rtx val  = force_operand (XEXP (x, 0), temp);
13054           if (val != temp)
13055             {
13056               if (GET_MODE (val) != Pmode)
13057                 val = convert_to_mode (Pmode, val, 1);
13058               emit_move_insn (temp, val);
13059             }
13060
13061           XEXP (x, 0) = temp;
13062           return x;
13063         }
13064     }
13065
13066   return x;
13067 }
13068 \f
13069 /* Print an integer constant expression in assembler syntax.  Addition
13070    and subtraction are the only arithmetic that may appear in these
13071    expressions.  FILE is the stdio stream to write to, X is the rtx, and
13072    CODE is the operand print code from the output string.  */
13073
13074 static void
13075 output_pic_addr_const (FILE *file, rtx x, int code)
13076 {
13077   char buf[256];
13078
13079   switch (GET_CODE (x))
13080     {
13081     case PC:
13082       gcc_assert (flag_pic);
13083       putc ('.', file);
13084       break;
13085
13086     case SYMBOL_REF:
13087       if (TARGET_64BIT || ! TARGET_MACHO_BRANCH_ISLANDS)
13088         output_addr_const (file, x);
13089       else
13090         {
13091           const char *name = XSTR (x, 0);
13092
13093           /* Mark the decl as referenced so that cgraph will
13094              output the function.  */
13095           if (SYMBOL_REF_DECL (x))
13096             mark_decl_referenced (SYMBOL_REF_DECL (x));
13097
13098 #if TARGET_MACHO
13099           if (MACHOPIC_INDIRECT
13100               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
13101             name = machopic_indirection_name (x, /*stub_p=*/true);
13102 #endif
13103           assemble_name (file, name);
13104         }
13105       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
13106           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
13107         fputs ("@PLT", file);
13108       break;
13109
13110     case LABEL_REF:
13111       x = XEXP (x, 0);
13112       /* FALLTHRU */
13113     case CODE_LABEL:
13114       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
13115       assemble_name (asm_out_file, buf);
13116       break;
13117
13118     case CONST_INT:
13119       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13120       break;
13121
13122     case CONST:
13123       /* This used to output parentheses around the expression,
13124          but that does not work on the 386 (either ATT or BSD assembler).  */
13125       output_pic_addr_const (file, XEXP (x, 0), code);
13126       break;
13127
13128     case CONST_DOUBLE:
13129       if (GET_MODE (x) == VOIDmode)
13130         {
13131           /* We can use %d if the number is <32 bits and positive.  */
13132           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
13133             fprintf (file, "0x%lx%08lx",
13134                      (unsigned long) CONST_DOUBLE_HIGH (x),
13135                      (unsigned long) CONST_DOUBLE_LOW (x));
13136           else
13137             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
13138         }
13139       else
13140         /* We can't handle floating point constants;
13141            TARGET_PRINT_OPERAND must handle them.  */
13142         output_operand_lossage ("floating constant misused");
13143       break;
13144
13145     case PLUS:
13146       /* Some assemblers need integer constants to appear first.  */
13147       if (CONST_INT_P (XEXP (x, 0)))
13148         {
13149           output_pic_addr_const (file, XEXP (x, 0), code);
13150           putc ('+', file);
13151           output_pic_addr_const (file, XEXP (x, 1), code);
13152         }
13153       else
13154         {
13155           gcc_assert (CONST_INT_P (XEXP (x, 1)));
13156           output_pic_addr_const (file, XEXP (x, 1), code);
13157           putc ('+', file);
13158           output_pic_addr_const (file, XEXP (x, 0), code);
13159         }
13160       break;
13161
13162     case MINUS:
13163       if (!TARGET_MACHO)
13164         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
13165       output_pic_addr_const (file, XEXP (x, 0), code);
13166       putc ('-', file);
13167       output_pic_addr_const (file, XEXP (x, 1), code);
13168       if (!TARGET_MACHO)
13169         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
13170       break;
13171
13172      case UNSPEC:
13173        if (XINT (x, 1) == UNSPEC_STACK_CHECK)
13174          {
13175            bool f = i386_asm_output_addr_const_extra (file, x);
13176            gcc_assert (f);
13177            break;
13178          }
13179
13180        gcc_assert (XVECLEN (x, 0) == 1);
13181        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
13182        switch (XINT (x, 1))
13183         {
13184         case UNSPEC_GOT:
13185           fputs ("@GOT", file);
13186           break;
13187         case UNSPEC_GOTOFF:
13188           fputs ("@GOTOFF", file);
13189           break;
13190         case UNSPEC_PLTOFF:
13191           fputs ("@PLTOFF", file);
13192           break;
13193         case UNSPEC_PCREL:
13194           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13195                  "(%rip)" : "[rip]", file);
13196           break;
13197         case UNSPEC_GOTPCREL:
13198           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13199                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
13200           break;
13201         case UNSPEC_GOTTPOFF:
13202           /* FIXME: This might be @TPOFF in Sun ld too.  */
13203           fputs ("@gottpoff", file);
13204           break;
13205         case UNSPEC_TPOFF:
13206           fputs ("@tpoff", file);
13207           break;
13208         case UNSPEC_NTPOFF:
13209           if (TARGET_64BIT)
13210             fputs ("@tpoff", file);
13211           else
13212             fputs ("@ntpoff", file);
13213           break;
13214         case UNSPEC_DTPOFF:
13215           fputs ("@dtpoff", file);
13216           break;
13217         case UNSPEC_GOTNTPOFF:
13218           if (TARGET_64BIT)
13219             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13220                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
13221           else
13222             fputs ("@gotntpoff", file);
13223           break;
13224         case UNSPEC_INDNTPOFF:
13225           fputs ("@indntpoff", file);
13226           break;
13227 #if TARGET_MACHO
13228         case UNSPEC_MACHOPIC_OFFSET:
13229           putc ('-', file);
13230           machopic_output_function_base_name (file);
13231           break;
13232 #endif
13233         default:
13234           output_operand_lossage ("invalid UNSPEC as operand");
13235           break;
13236         }
13237        break;
13238
13239     default:
13240       output_operand_lossage ("invalid expression as operand");
13241     }
13242 }
13243
13244 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
13245    We need to emit DTP-relative relocations.  */
13246
13247 static void ATTRIBUTE_UNUSED
13248 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
13249 {
13250   fputs (ASM_LONG, file);
13251   output_addr_const (file, x);
13252   fputs ("@dtpoff", file);
13253   switch (size)
13254     {
13255     case 4:
13256       break;
13257     case 8:
13258       fputs (", 0", file);
13259       break;
13260     default:
13261       gcc_unreachable ();
13262    }
13263 }
13264
13265 /* Return true if X is a representation of the PIC register.  This copes
13266    with calls from ix86_find_base_term, where the register might have
13267    been replaced by a cselib value.  */
13268
13269 static bool
13270 ix86_pic_register_p (rtx x)
13271 {
13272   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
13273     return (pic_offset_table_rtx
13274             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
13275   else
13276     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
13277 }
13278
13279 /* Helper function for ix86_delegitimize_address.
13280    Attempt to delegitimize TLS local-exec accesses.  */
13281
13282 static rtx
13283 ix86_delegitimize_tls_address (rtx orig_x)
13284 {
13285   rtx x = orig_x, unspec;
13286   struct ix86_address addr;
13287
13288   if (!TARGET_TLS_DIRECT_SEG_REFS)
13289     return orig_x;
13290   if (MEM_P (x))
13291     x = XEXP (x, 0);
13292   if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
13293     return orig_x;
13294   if (ix86_decompose_address (x, &addr) == 0
13295       || addr.seg != (TARGET_64BIT ? SEG_FS : SEG_GS)
13296       || addr.disp == NULL_RTX
13297       || GET_CODE (addr.disp) != CONST)
13298     return orig_x;
13299   unspec = XEXP (addr.disp, 0);
13300   if (GET_CODE (unspec) == PLUS && CONST_INT_P (XEXP (unspec, 1)))
13301     unspec = XEXP (unspec, 0);
13302   if (GET_CODE (unspec) != UNSPEC || XINT (unspec, 1) != UNSPEC_NTPOFF)
13303     return orig_x;
13304   x = XVECEXP (unspec, 0, 0);
13305   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13306   if (unspec != XEXP (addr.disp, 0))
13307     x = gen_rtx_PLUS (Pmode, x, XEXP (XEXP (addr.disp, 0), 1));
13308   if (addr.index)
13309     {
13310       rtx idx = addr.index;
13311       if (addr.scale != 1)
13312         idx = gen_rtx_MULT (Pmode, idx, GEN_INT (addr.scale));
13313       x = gen_rtx_PLUS (Pmode, idx, x);
13314     }
13315   if (addr.base)
13316     x = gen_rtx_PLUS (Pmode, addr.base, x);
13317   if (MEM_P (orig_x))
13318     x = replace_equiv_address_nv (orig_x, x);
13319   return x;
13320 }
13321
13322 /* In the name of slightly smaller debug output, and to cater to
13323    general assembler lossage, recognize PIC+GOTOFF and turn it back
13324    into a direct symbol reference.
13325
13326    On Darwin, this is necessary to avoid a crash, because Darwin
13327    has a different PIC label for each routine but the DWARF debugging
13328    information is not associated with any particular routine, so it's
13329    necessary to remove references to the PIC label from RTL stored by
13330    the DWARF output code.  */
13331
13332 static rtx
13333 ix86_delegitimize_address (rtx x)
13334 {
13335   rtx orig_x = delegitimize_mem_from_attrs (x);
13336   /* addend is NULL or some rtx if x is something+GOTOFF where
13337      something doesn't include the PIC register.  */
13338   rtx addend = NULL_RTX;
13339   /* reg_addend is NULL or a multiple of some register.  */
13340   rtx reg_addend = NULL_RTX;
13341   /* const_addend is NULL or a const_int.  */
13342   rtx const_addend = NULL_RTX;
13343   /* This is the result, or NULL.  */
13344   rtx result = NULL_RTX;
13345
13346   x = orig_x;
13347
13348   if (MEM_P (x))
13349     x = XEXP (x, 0);
13350
13351   if (TARGET_64BIT)
13352     {
13353       if (GET_CODE (x) == CONST
13354           && GET_CODE (XEXP (x, 0)) == PLUS
13355           && GET_MODE (XEXP (x, 0)) == Pmode
13356           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
13357           && GET_CODE (XEXP (XEXP (x, 0), 0)) == UNSPEC
13358           && XINT (XEXP (XEXP (x, 0), 0), 1) == UNSPEC_PCREL)
13359         {
13360           rtx x2 = XVECEXP (XEXP (XEXP (x, 0), 0), 0, 0);
13361           x = gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 1), x2);
13362           if (MEM_P (orig_x))
13363             x = replace_equiv_address_nv (orig_x, x);
13364           return x;
13365         }
13366       if (GET_CODE (x) != CONST
13367           || GET_CODE (XEXP (x, 0)) != UNSPEC
13368           || (XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
13369               && XINT (XEXP (x, 0), 1) != UNSPEC_PCREL)
13370           || (!MEM_P (orig_x) && XINT (XEXP (x, 0), 1) != UNSPEC_PCREL))
13371         return ix86_delegitimize_tls_address (orig_x);
13372       x = XVECEXP (XEXP (x, 0), 0, 0);
13373       if (GET_MODE (orig_x) != GET_MODE (x) && MEM_P (orig_x))
13374         {
13375           x = simplify_gen_subreg (GET_MODE (orig_x), x,
13376                                    GET_MODE (x), 0);
13377           if (x == NULL_RTX)
13378             return orig_x;
13379         }
13380       return x;
13381     }
13382
13383   if (GET_CODE (x) != PLUS
13384       || GET_CODE (XEXP (x, 1)) != CONST)
13385     return ix86_delegitimize_tls_address (orig_x);
13386
13387   if (ix86_pic_register_p (XEXP (x, 0)))
13388     /* %ebx + GOT/GOTOFF */
13389     ;
13390   else if (GET_CODE (XEXP (x, 0)) == PLUS)
13391     {
13392       /* %ebx + %reg * scale + GOT/GOTOFF */
13393       reg_addend = XEXP (x, 0);
13394       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
13395         reg_addend = XEXP (reg_addend, 1);
13396       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
13397         reg_addend = XEXP (reg_addend, 0);
13398       else
13399         {
13400           reg_addend = NULL_RTX;
13401           addend = XEXP (x, 0);
13402         }
13403     }
13404   else
13405     addend = XEXP (x, 0);
13406
13407   x = XEXP (XEXP (x, 1), 0);
13408   if (GET_CODE (x) == PLUS
13409       && CONST_INT_P (XEXP (x, 1)))
13410     {
13411       const_addend = XEXP (x, 1);
13412       x = XEXP (x, 0);
13413     }
13414
13415   if (GET_CODE (x) == UNSPEC
13416       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
13417           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
13418     result = XVECEXP (x, 0, 0);
13419
13420   if (TARGET_MACHO && darwin_local_data_pic (x)
13421       && !MEM_P (orig_x))
13422     result = XVECEXP (x, 0, 0);
13423
13424   if (! result)
13425     return ix86_delegitimize_tls_address (orig_x);
13426
13427   if (const_addend)
13428     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
13429   if (reg_addend)
13430     result = gen_rtx_PLUS (Pmode, reg_addend, result);
13431   if (addend)
13432     {
13433       /* If the rest of original X doesn't involve the PIC register, add
13434          addend and subtract pic_offset_table_rtx.  This can happen e.g.
13435          for code like:
13436          leal (%ebx, %ecx, 4), %ecx
13437          ...
13438          movl foo@GOTOFF(%ecx), %edx
13439          in which case we return (%ecx - %ebx) + foo.  */
13440       if (pic_offset_table_rtx)
13441         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
13442                                                      pic_offset_table_rtx),
13443                                result);
13444       else
13445         return orig_x;
13446     }
13447   if (GET_MODE (orig_x) != Pmode && MEM_P (orig_x))
13448     {
13449       result = simplify_gen_subreg (GET_MODE (orig_x), result, Pmode, 0);
13450       if (result == NULL_RTX)
13451         return orig_x;
13452     }
13453   return result;
13454 }
13455
13456 /* If X is a machine specific address (i.e. a symbol or label being
13457    referenced as a displacement from the GOT implemented using an
13458    UNSPEC), then return the base term.  Otherwise return X.  */
13459
13460 rtx
13461 ix86_find_base_term (rtx x)
13462 {
13463   rtx term;
13464
13465   if (TARGET_64BIT)
13466     {
13467       if (GET_CODE (x) != CONST)
13468         return x;
13469       term = XEXP (x, 0);
13470       if (GET_CODE (term) == PLUS
13471           && (CONST_INT_P (XEXP (term, 1))
13472               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
13473         term = XEXP (term, 0);
13474       if (GET_CODE (term) != UNSPEC
13475           || (XINT (term, 1) != UNSPEC_GOTPCREL
13476               && XINT (term, 1) != UNSPEC_PCREL))
13477         return x;
13478
13479       return XVECEXP (term, 0, 0);
13480     }
13481
13482   return ix86_delegitimize_address (x);
13483 }
13484 \f
13485 static void
13486 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
13487                     int fp, FILE *file)
13488 {
13489   const char *suffix;
13490
13491   if (mode == CCFPmode || mode == CCFPUmode)
13492     {
13493       code = ix86_fp_compare_code_to_integer (code);
13494       mode = CCmode;
13495     }
13496   if (reverse)
13497     code = reverse_condition (code);
13498
13499   switch (code)
13500     {
13501     case EQ:
13502       switch (mode)
13503         {
13504         case CCAmode:
13505           suffix = "a";
13506           break;
13507
13508         case CCCmode:
13509           suffix = "c";
13510           break;
13511
13512         case CCOmode:
13513           suffix = "o";
13514           break;
13515
13516         case CCSmode:
13517           suffix = "s";
13518           break;
13519
13520         default:
13521           suffix = "e";
13522         }
13523       break;
13524     case NE:
13525       switch (mode)
13526         {
13527         case CCAmode:
13528           suffix = "na";
13529           break;
13530
13531         case CCCmode:
13532           suffix = "nc";
13533           break;
13534
13535         case CCOmode:
13536           suffix = "no";
13537           break;
13538
13539         case CCSmode:
13540           suffix = "ns";
13541           break;
13542
13543         default:
13544           suffix = "ne";
13545         }
13546       break;
13547     case GT:
13548       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
13549       suffix = "g";
13550       break;
13551     case GTU:
13552       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
13553          Those same assemblers have the same but opposite lossage on cmov.  */
13554       if (mode == CCmode)
13555         suffix = fp ? "nbe" : "a";
13556       else if (mode == CCCmode)
13557         suffix = "b";
13558       else
13559         gcc_unreachable ();
13560       break;
13561     case LT:
13562       switch (mode)
13563         {
13564         case CCNOmode:
13565         case CCGOCmode:
13566           suffix = "s";
13567           break;
13568
13569         case CCmode:
13570         case CCGCmode:
13571           suffix = "l";
13572           break;
13573
13574         default:
13575           gcc_unreachable ();
13576         }
13577       break;
13578     case LTU:
13579       gcc_assert (mode == CCmode || mode == CCCmode);
13580       suffix = "b";
13581       break;
13582     case GE:
13583       switch (mode)
13584         {
13585         case CCNOmode:
13586         case CCGOCmode:
13587           suffix = "ns";
13588           break;
13589
13590         case CCmode:
13591         case CCGCmode:
13592           suffix = "ge";
13593           break;
13594
13595         default:
13596           gcc_unreachable ();
13597         }
13598       break;
13599     case GEU:
13600       /* ??? As above.  */
13601       gcc_assert (mode == CCmode || mode == CCCmode);
13602       suffix = fp ? "nb" : "ae";
13603       break;
13604     case LE:
13605       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
13606       suffix = "le";
13607       break;
13608     case LEU:
13609       /* ??? As above.  */
13610       if (mode == CCmode)
13611         suffix = "be";
13612       else if (mode == CCCmode)
13613         suffix = fp ? "nb" : "ae";
13614       else
13615         gcc_unreachable ();
13616       break;
13617     case UNORDERED:
13618       suffix = fp ? "u" : "p";
13619       break;
13620     case ORDERED:
13621       suffix = fp ? "nu" : "np";
13622       break;
13623     default:
13624       gcc_unreachable ();
13625     }
13626   fputs (suffix, file);
13627 }
13628
13629 /* Print the name of register X to FILE based on its machine mode and number.
13630    If CODE is 'w', pretend the mode is HImode.
13631    If CODE is 'b', pretend the mode is QImode.
13632    If CODE is 'k', pretend the mode is SImode.
13633    If CODE is 'q', pretend the mode is DImode.
13634    If CODE is 'x', pretend the mode is V4SFmode.
13635    If CODE is 't', pretend the mode is V8SFmode.
13636    If CODE is 'h', pretend the reg is the 'high' byte register.
13637    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
13638    If CODE is 'd', duplicate the operand for AVX instruction.
13639  */
13640
13641 void
13642 print_reg (rtx x, int code, FILE *file)
13643 {
13644   const char *reg;
13645   bool duplicated = code == 'd' && TARGET_AVX;
13646
13647   gcc_assert (x == pc_rtx
13648               || (REGNO (x) != ARG_POINTER_REGNUM
13649                   && REGNO (x) != FRAME_POINTER_REGNUM
13650                   && REGNO (x) != FLAGS_REG
13651                   && REGNO (x) != FPSR_REG
13652                   && REGNO (x) != FPCR_REG));
13653
13654   if (ASSEMBLER_DIALECT == ASM_ATT)
13655     putc ('%', file);
13656
13657   if (x == pc_rtx)
13658     {
13659       gcc_assert (TARGET_64BIT);
13660       fputs ("rip", file);
13661       return;
13662     }
13663
13664   if (code == 'w' || MMX_REG_P (x))
13665     code = 2;
13666   else if (code == 'b')
13667     code = 1;
13668   else if (code == 'k')
13669     code = 4;
13670   else if (code == 'q')
13671     code = 8;
13672   else if (code == 'y')
13673     code = 3;
13674   else if (code == 'h')
13675     code = 0;
13676   else if (code == 'x')
13677     code = 16;
13678   else if (code == 't')
13679     code = 32;
13680   else
13681     code = GET_MODE_SIZE (GET_MODE (x));
13682
13683   /* Irritatingly, AMD extended registers use different naming convention
13684      from the normal registers: "r%d[bwd]"  */
13685   if (REX_INT_REG_P (x))
13686     {
13687       gcc_assert (TARGET_64BIT);
13688       putc ('r', file);
13689       fprint_ul (file, REGNO (x) - FIRST_REX_INT_REG + 8);
13690       switch (code)
13691         {
13692           case 0:
13693             error ("extended registers have no high halves");
13694             break;
13695           case 1:
13696             putc ('b', file);
13697             break;
13698           case 2:
13699             putc ('w', file);
13700             break;
13701           case 4:
13702             putc ('d', file);
13703             break;
13704           case 8:
13705             /* no suffix */
13706             break;
13707           default:
13708             error ("unsupported operand size for extended register");
13709             break;
13710         }
13711       return;
13712     }
13713
13714   reg = NULL;
13715   switch (code)
13716     {
13717     case 3:
13718       if (STACK_TOP_P (x))
13719         {
13720           reg = "st(0)";
13721           break;
13722         }
13723       /* FALLTHRU */
13724     case 8:
13725     case 4:
13726     case 12:
13727       if (! ANY_FP_REG_P (x))
13728         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
13729       /* FALLTHRU */
13730     case 16:
13731     case 2:
13732     normal:
13733       reg = hi_reg_name[REGNO (x)];
13734       break;
13735     case 1:
13736       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
13737         goto normal;
13738       reg = qi_reg_name[REGNO (x)];
13739       break;
13740     case 0:
13741       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
13742         goto normal;
13743       reg = qi_high_reg_name[REGNO (x)];
13744       break;
13745     case 32:
13746       if (SSE_REG_P (x))
13747         {
13748           gcc_assert (!duplicated);
13749           putc ('y', file);
13750           fputs (hi_reg_name[REGNO (x)] + 1, file);
13751           return;
13752         }
13753       break;
13754     default:
13755       gcc_unreachable ();
13756     }
13757
13758   fputs (reg, file);
13759   if (duplicated)
13760     {
13761       if (ASSEMBLER_DIALECT == ASM_ATT)
13762         fprintf (file, ", %%%s", reg);
13763       else
13764         fprintf (file, ", %s", reg);
13765     }
13766 }
13767
13768 /* Locate some local-dynamic symbol still in use by this function
13769    so that we can print its name in some tls_local_dynamic_base
13770    pattern.  */
13771
13772 static int
13773 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
13774 {
13775   rtx x = *px;
13776
13777   if (GET_CODE (x) == SYMBOL_REF
13778       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
13779     {
13780       cfun->machine->some_ld_name = XSTR (x, 0);
13781       return 1;
13782     }
13783
13784   return 0;
13785 }
13786
13787 static const char *
13788 get_some_local_dynamic_name (void)
13789 {
13790   rtx insn;
13791
13792   if (cfun->machine->some_ld_name)
13793     return cfun->machine->some_ld_name;
13794
13795   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
13796     if (NONDEBUG_INSN_P (insn)
13797         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
13798       return cfun->machine->some_ld_name;
13799
13800   return NULL;
13801 }
13802
13803 /* Meaning of CODE:
13804    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
13805    C -- print opcode suffix for set/cmov insn.
13806    c -- like C, but print reversed condition
13807    F,f -- likewise, but for floating-point.
13808    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
13809         otherwise nothing
13810    R -- print the prefix for register names.
13811    z -- print the opcode suffix for the size of the current operand.
13812    Z -- likewise, with special suffixes for x87 instructions.
13813    * -- print a star (in certain assembler syntax)
13814    A -- print an absolute memory reference.
13815    E -- print address with DImode register names if TARGET_64BIT.
13816    w -- print the operand as if it's a "word" (HImode) even if it isn't.
13817    s -- print a shift double count, followed by the assemblers argument
13818         delimiter.
13819    b -- print the QImode name of the register for the indicated operand.
13820         %b0 would print %al if operands[0] is reg 0.
13821    w --  likewise, print the HImode name of the register.
13822    k --  likewise, print the SImode name of the register.
13823    q --  likewise, print the DImode name of the register.
13824    x --  likewise, print the V4SFmode name of the register.
13825    t --  likewise, print the V8SFmode name of the register.
13826    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
13827    y -- print "st(0)" instead of "st" as a register.
13828    d -- print duplicated register operand for AVX instruction.
13829    D -- print condition for SSE cmp instruction.
13830    P -- if PIC, print an @PLT suffix.
13831    p -- print raw symbol name.
13832    X -- don't print any sort of PIC '@' suffix for a symbol.
13833    & -- print some in-use local-dynamic symbol name.
13834    H -- print a memory address offset by 8; used for sse high-parts
13835    Y -- print condition for XOP pcom* instruction.
13836    + -- print a branch hint as 'cs' or 'ds' prefix
13837    ; -- print a semicolon (after prefixes due to bug in older gas).
13838    ~ -- print "i" if TARGET_AVX2, "f" otherwise.
13839    @ -- print a segment register of thread base pointer load
13840  */
13841
13842 void
13843 ix86_print_operand (FILE *file, rtx x, int code)
13844 {
13845   if (code)
13846     {
13847       switch (code)
13848         {
13849         case '*':
13850           if (ASSEMBLER_DIALECT == ASM_ATT)
13851             putc ('*', file);
13852           return;
13853
13854         case '&':
13855           {
13856             const char *name = get_some_local_dynamic_name ();
13857             if (name == NULL)
13858               output_operand_lossage ("'%%&' used without any "
13859                                       "local dynamic TLS references");
13860             else
13861               assemble_name (file, name);
13862             return;
13863           }
13864
13865         case 'A':
13866           switch (ASSEMBLER_DIALECT)
13867             {
13868             case ASM_ATT:
13869               putc ('*', file);
13870               break;
13871
13872             case ASM_INTEL:
13873               /* Intel syntax. For absolute addresses, registers should not
13874                  be surrounded by braces.  */
13875               if (!REG_P (x))
13876                 {
13877                   putc ('[', file);
13878                   ix86_print_operand (file, x, 0);
13879                   putc (']', file);
13880                   return;
13881                 }
13882               break;
13883
13884             default:
13885               gcc_unreachable ();
13886             }
13887
13888           ix86_print_operand (file, x, 0);
13889           return;
13890
13891         case 'E':
13892           /* Wrap address in an UNSPEC to declare special handling.  */
13893           if (TARGET_64BIT)
13894             x = gen_rtx_UNSPEC (DImode, gen_rtvec (1, x), UNSPEC_LEA_ADDR);
13895
13896           output_address (x);
13897           return;
13898             
13899         case 'L':
13900           if (ASSEMBLER_DIALECT == ASM_ATT)
13901             putc ('l', file);
13902           return;
13903
13904         case 'W':
13905           if (ASSEMBLER_DIALECT == ASM_ATT)
13906             putc ('w', file);
13907           return;
13908
13909         case 'B':
13910           if (ASSEMBLER_DIALECT == ASM_ATT)
13911             putc ('b', file);
13912           return;
13913
13914         case 'Q':
13915           if (ASSEMBLER_DIALECT == ASM_ATT)
13916             putc ('l', file);
13917           return;
13918
13919         case 'S':
13920           if (ASSEMBLER_DIALECT == ASM_ATT)
13921             putc ('s', file);
13922           return;
13923
13924         case 'T':
13925           if (ASSEMBLER_DIALECT == ASM_ATT)
13926             putc ('t', file);
13927           return;
13928
13929         case 'z':
13930           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
13931             {
13932               /* Opcodes don't get size suffixes if using Intel opcodes.  */
13933               if (ASSEMBLER_DIALECT == ASM_INTEL)
13934                 return;
13935
13936               switch (GET_MODE_SIZE (GET_MODE (x)))
13937                 {
13938                 case 1:
13939                   putc ('b', file);
13940                   return;
13941
13942                 case 2:
13943                   putc ('w', file);
13944                   return;
13945
13946                 case 4:
13947                   putc ('l', file);
13948                   return;
13949
13950                 case 8:
13951                   putc ('q', file);
13952                   return;
13953
13954                 default:
13955                   output_operand_lossage
13956                     ("invalid operand size for operand code '%c'", code);
13957                   return;
13958                 }
13959             }
13960
13961           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
13962             warning
13963               (0, "non-integer operand used with operand code '%c'", code);
13964           /* FALLTHRU */
13965
13966         case 'Z':
13967           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
13968           if (ASSEMBLER_DIALECT == ASM_INTEL)
13969             return;
13970
13971           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
13972             {
13973               switch (GET_MODE_SIZE (GET_MODE (x)))
13974                 {
13975                 case 2:
13976 #ifdef HAVE_AS_IX86_FILDS
13977                   putc ('s', file);
13978 #endif
13979                   return;
13980
13981                 case 4:
13982                   putc ('l', file);
13983                   return;
13984
13985                 case 8:
13986 #ifdef HAVE_AS_IX86_FILDQ
13987                   putc ('q', file);
13988 #else
13989                   fputs ("ll", file);
13990 #endif
13991                   return;
13992
13993                 default:
13994                   break;
13995                 }
13996             }
13997           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
13998             {
13999               /* 387 opcodes don't get size suffixes
14000                  if the operands are registers.  */
14001               if (STACK_REG_P (x))
14002                 return;
14003
14004               switch (GET_MODE_SIZE (GET_MODE (x)))
14005                 {
14006                 case 4:
14007                   putc ('s', file);
14008                   return;
14009
14010                 case 8:
14011                   putc ('l', file);
14012                   return;
14013
14014                 case 12:
14015                 case 16:
14016                   putc ('t', file);
14017                   return;
14018
14019                 default:
14020                   break;
14021                 }
14022             }
14023           else
14024             {
14025               output_operand_lossage
14026                 ("invalid operand type used with operand code '%c'", code);
14027               return;
14028             }
14029
14030           output_operand_lossage
14031             ("invalid operand size for operand code '%c'", code);
14032           return;
14033
14034         case 'd':
14035         case 'b':
14036         case 'w':
14037         case 'k':
14038         case 'q':
14039         case 'h':
14040         case 't':
14041         case 'y':
14042         case 'x':
14043         case 'X':
14044         case 'P':
14045         case 'p':
14046           break;
14047
14048         case 's':
14049           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
14050             {
14051               ix86_print_operand (file, x, 0);
14052               fputs (", ", file);
14053             }
14054           return;
14055
14056         case 'D':
14057           /* Little bit of braindamage here.  The SSE compare instructions
14058              does use completely different names for the comparisons that the
14059              fp conditional moves.  */
14060           if (TARGET_AVX)
14061             {
14062               switch (GET_CODE (x))
14063                 {
14064                 case EQ:
14065                   fputs ("eq", file);
14066                   break;
14067                 case UNEQ:
14068                   fputs ("eq_us", file);
14069                   break;
14070                 case LT:
14071                   fputs ("lt", file);
14072                   break;
14073                 case UNLT:
14074                   fputs ("nge", file);
14075                   break;
14076                 case LE:
14077                   fputs ("le", file);
14078                   break;
14079                 case UNLE:
14080                   fputs ("ngt", file);
14081                   break;
14082                 case UNORDERED:
14083                   fputs ("unord", file);
14084                   break;
14085                 case NE:
14086                   fputs ("neq", file);
14087                   break;
14088                 case LTGT:
14089                   fputs ("neq_oq", file);
14090                   break;
14091                 case GE:
14092                   fputs ("ge", file);
14093                   break;
14094                 case UNGE:
14095                   fputs ("nlt", file);
14096                   break;
14097                 case GT:
14098                   fputs ("gt", file);
14099                   break;
14100                 case UNGT:
14101                   fputs ("nle", file);
14102                   break;
14103                 case ORDERED:
14104                   fputs ("ord", file);
14105                   break;
14106                 default:
14107                   output_operand_lossage ("operand is not a condition code, "
14108                                           "invalid operand code 'D'");
14109                   return;
14110                 }
14111             }
14112           else
14113             {
14114               switch (GET_CODE (x))
14115                 {
14116                 case EQ:
14117                 case UNEQ:
14118                   fputs ("eq", file);
14119                   break;
14120                 case LT:
14121                 case UNLT:
14122                   fputs ("lt", file);
14123                   break;
14124                 case LE:
14125                 case UNLE:
14126                   fputs ("le", file);
14127                   break;
14128                 case UNORDERED:
14129                   fputs ("unord", file);
14130                   break;
14131                 case NE:
14132                 case LTGT:
14133                   fputs ("neq", file);
14134                   break;
14135                 case UNGE:
14136                 case GE:
14137                   fputs ("nlt", file);
14138                   break;
14139                 case UNGT:
14140                 case GT:
14141                   fputs ("nle", file);
14142                   break;
14143                 case ORDERED:
14144                   fputs ("ord", file);
14145                   break;
14146                 default:
14147                   output_operand_lossage ("operand is not a condition code, "
14148                                           "invalid operand code 'D'");
14149                   return;
14150                 }
14151             }
14152           return;
14153         case 'O':
14154 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14155           if (ASSEMBLER_DIALECT == ASM_ATT)
14156             {
14157               switch (GET_MODE (x))
14158                 {
14159                 case HImode: putc ('w', file); break;
14160                 case SImode:
14161                 case SFmode: putc ('l', file); break;
14162                 case DImode:
14163                 case DFmode: putc ('q', file); break;
14164                 default: gcc_unreachable ();
14165                 }
14166               putc ('.', file);
14167             }
14168 #endif
14169           return;
14170         case 'C':
14171           if (!COMPARISON_P (x))
14172             {
14173               output_operand_lossage ("operand is neither a constant nor a "
14174                                       "condition code, invalid operand code "
14175                                       "'C'");
14176               return;
14177             }
14178           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
14179           return;
14180         case 'F':
14181           if (!COMPARISON_P (x))
14182             {
14183               output_operand_lossage ("operand is neither a constant nor a "
14184                                       "condition code, invalid operand code "
14185                                       "'F'");
14186               return;
14187             }
14188 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14189           if (ASSEMBLER_DIALECT == ASM_ATT)
14190             putc ('.', file);
14191 #endif
14192           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
14193           return;
14194
14195           /* Like above, but reverse condition */
14196         case 'c':
14197           /* Check to see if argument to %c is really a constant
14198              and not a condition code which needs to be reversed.  */
14199           if (!COMPARISON_P (x))
14200             {
14201               output_operand_lossage ("operand is neither a constant nor a "
14202                                       "condition code, invalid operand "
14203                                       "code 'c'");
14204               return;
14205             }
14206           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
14207           return;
14208         case 'f':
14209           if (!COMPARISON_P (x))
14210             {
14211               output_operand_lossage ("operand is neither a constant nor a "
14212                                       "condition code, invalid operand "
14213                                       "code 'f'");
14214               return;
14215             }
14216 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14217           if (ASSEMBLER_DIALECT == ASM_ATT)
14218             putc ('.', file);
14219 #endif
14220           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
14221           return;
14222
14223         case 'H':
14224           if (!offsettable_memref_p (x))
14225             {
14226               output_operand_lossage ("operand is not an offsettable memory "
14227                                       "reference, invalid operand "
14228                                       "code 'H'");
14229               return;
14230             }
14231           /* It doesn't actually matter what mode we use here, as we're
14232              only going to use this for printing.  */
14233           x = adjust_address_nv (x, DImode, 8);
14234           break;
14235
14236         case '+':
14237           {
14238             rtx x;
14239
14240             if (!optimize
14241                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
14242               return;
14243
14244             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
14245             if (x)
14246               {
14247                 int pred_val = INTVAL (XEXP (x, 0));
14248
14249                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
14250                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
14251                   {
14252                     int taken = pred_val > REG_BR_PROB_BASE / 2;
14253                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
14254
14255                     /* Emit hints only in the case default branch prediction
14256                        heuristics would fail.  */
14257                     if (taken != cputaken)
14258                       {
14259                         /* We use 3e (DS) prefix for taken branches and
14260                            2e (CS) prefix for not taken branches.  */
14261                         if (taken)
14262                           fputs ("ds ; ", file);
14263                         else
14264                           fputs ("cs ; ", file);
14265                       }
14266                   }
14267               }
14268             return;
14269           }
14270
14271         case 'Y':
14272           switch (GET_CODE (x))
14273             {
14274             case NE:
14275               fputs ("neq", file);
14276               break;
14277             case EQ:
14278               fputs ("eq", file);
14279               break;
14280             case GE:
14281             case GEU:
14282               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
14283               break;
14284             case GT:
14285             case GTU:
14286               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
14287               break;
14288             case LE:
14289             case LEU:
14290               fputs ("le", file);
14291               break;
14292             case LT:
14293             case LTU:
14294               fputs ("lt", file);
14295               break;
14296             case UNORDERED:
14297               fputs ("unord", file);
14298               break;
14299             case ORDERED:
14300               fputs ("ord", file);
14301               break;
14302             case UNEQ:
14303               fputs ("ueq", file);
14304               break;
14305             case UNGE:
14306               fputs ("nlt", file);
14307               break;
14308             case UNGT:
14309               fputs ("nle", file);
14310               break;
14311             case UNLE:
14312               fputs ("ule", file);
14313               break;
14314             case UNLT:
14315               fputs ("ult", file);
14316               break;
14317             case LTGT:
14318               fputs ("une", file);
14319               break;
14320             default:
14321               output_operand_lossage ("operand is not a condition code, "
14322                                       "invalid operand code 'Y'");
14323               return;
14324             }
14325           return;
14326
14327         case ';':
14328 #ifndef HAVE_AS_IX86_REP_LOCK_PREFIX
14329           putc (';', file);
14330 #endif
14331           return;
14332
14333         case '@':
14334           if (ASSEMBLER_DIALECT == ASM_ATT)
14335             putc ('%', file);
14336
14337           /* The kernel uses a different segment register for performance
14338              reasons; a system call would not have to trash the userspace
14339              segment register, which would be expensive.  */
14340           if (TARGET_64BIT && ix86_cmodel != CM_KERNEL)
14341             fputs ("fs", file);
14342           else
14343             fputs ("gs", file);
14344           return;
14345
14346         case '~':
14347           putc (TARGET_AVX2 ? 'i' : 'f', file);
14348           return;
14349
14350         default:
14351             output_operand_lossage ("invalid operand code '%c'", code);
14352         }
14353     }
14354
14355   if (REG_P (x))
14356     print_reg (x, code, file);
14357
14358   else if (MEM_P (x))
14359     {
14360       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
14361       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
14362           && GET_MODE (x) != BLKmode)
14363         {
14364           const char * size;
14365           switch (GET_MODE_SIZE (GET_MODE (x)))
14366             {
14367             case 1: size = "BYTE"; break;
14368             case 2: size = "WORD"; break;
14369             case 4: size = "DWORD"; break;
14370             case 8: size = "QWORD"; break;
14371             case 12: size = "TBYTE"; break;
14372             case 16:
14373               if (GET_MODE (x) == XFmode)
14374                 size = "TBYTE";
14375               else
14376                 size = "XMMWORD";
14377               break;
14378             case 32: size = "YMMWORD"; break;
14379             default:
14380               gcc_unreachable ();
14381             }
14382
14383           /* Check for explicit size override (codes 'b', 'w', 'k',
14384              'q' and 'x')  */
14385           if (code == 'b')
14386             size = "BYTE";
14387           else if (code == 'w')
14388             size = "WORD";
14389           else if (code == 'k')
14390             size = "DWORD";
14391           else if (code == 'q')
14392             size = "QWORD";
14393           else if (code == 'x')
14394             size = "XMMWORD";
14395
14396           fputs (size, file);
14397           fputs (" PTR ", file);
14398         }
14399
14400       x = XEXP (x, 0);
14401       /* Avoid (%rip) for call operands.  */
14402       if (CONSTANT_ADDRESS_P (x) && code == 'P'
14403           && !CONST_INT_P (x))
14404         output_addr_const (file, x);
14405       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
14406         output_operand_lossage ("invalid constraints for operand");
14407       else
14408         output_address (x);
14409     }
14410
14411   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
14412     {
14413       REAL_VALUE_TYPE r;
14414       long l;
14415
14416       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14417       REAL_VALUE_TO_TARGET_SINGLE (r, l);
14418
14419       if (ASSEMBLER_DIALECT == ASM_ATT)
14420         putc ('$', file);
14421       /* Sign extend 32bit SFmode immediate to 8 bytes.  */
14422       if (code == 'q')
14423         fprintf (file, "0x%08llx", (unsigned long long) (int) l);
14424       else
14425         fprintf (file, "0x%08x", (unsigned int) l);
14426     }
14427
14428   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
14429     {
14430       REAL_VALUE_TYPE r;
14431       long l[2];
14432
14433       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14434       REAL_VALUE_TO_TARGET_DOUBLE (r, l);
14435
14436       if (ASSEMBLER_DIALECT == ASM_ATT)
14437         putc ('$', file);
14438       fprintf (file, "0x%lx%08lx", l[1] & 0xffffffff, l[0] & 0xffffffff);
14439     }
14440
14441   /* These float cases don't actually occur as immediate operands.  */
14442   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == XFmode)
14443     {
14444       char dstr[30];
14445
14446       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
14447       fputs (dstr, file);
14448     }
14449
14450   else
14451     {
14452       /* We have patterns that allow zero sets of memory, for instance.
14453          In 64-bit mode, we should probably support all 8-byte vectors,
14454          since we can in fact encode that into an immediate.  */
14455       if (GET_CODE (x) == CONST_VECTOR)
14456         {
14457           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
14458           x = const0_rtx;
14459         }
14460
14461       if (code != 'P' && code != 'p')
14462         {
14463           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
14464             {
14465               if (ASSEMBLER_DIALECT == ASM_ATT)
14466                 putc ('$', file);
14467             }
14468           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
14469                    || GET_CODE (x) == LABEL_REF)
14470             {
14471               if (ASSEMBLER_DIALECT == ASM_ATT)
14472                 putc ('$', file);
14473               else
14474                 fputs ("OFFSET FLAT:", file);
14475             }
14476         }
14477       if (CONST_INT_P (x))
14478         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
14479       else if (flag_pic || MACHOPIC_INDIRECT)
14480         output_pic_addr_const (file, x, code);
14481       else
14482         output_addr_const (file, x);
14483     }
14484 }
14485
14486 static bool
14487 ix86_print_operand_punct_valid_p (unsigned char code)
14488 {
14489   return (code == '@' || code == '*' || code == '+'
14490           || code == '&' || code == ';' || code == '~');
14491 }
14492 \f
14493 /* Print a memory operand whose address is ADDR.  */
14494
14495 static void
14496 ix86_print_operand_address (FILE *file, rtx addr)
14497 {
14498   struct ix86_address parts;
14499   rtx base, index, disp;
14500   int scale;
14501   int ok;
14502   bool vsib = false;
14503   int code = 0;
14504
14505   if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_VSIBADDR)
14506     {
14507       ok = ix86_decompose_address (XVECEXP (addr, 0, 0), &parts);
14508       gcc_assert (parts.index == NULL_RTX);
14509       parts.index = XVECEXP (addr, 0, 1);
14510       parts.scale = INTVAL (XVECEXP (addr, 0, 2));
14511       addr = XVECEXP (addr, 0, 0);
14512       vsib = true;
14513     }
14514   else if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_LEA_ADDR)
14515     {
14516       gcc_assert (TARGET_64BIT);
14517       ok = ix86_decompose_address (XVECEXP (addr, 0, 0), &parts);
14518       code = 'q';
14519     }
14520   else
14521     ok = ix86_decompose_address (addr, &parts);
14522
14523   gcc_assert (ok);
14524
14525   if (parts.base && GET_CODE (parts.base) == SUBREG)
14526     {
14527       rtx tmp = SUBREG_REG (parts.base);
14528       parts.base = simplify_subreg (GET_MODE (parts.base),
14529                                     tmp, GET_MODE (tmp), 0);
14530       gcc_assert (parts.base != NULL_RTX);
14531     }
14532
14533   if (parts.index && GET_CODE (parts.index) == SUBREG)
14534     {
14535       rtx tmp = SUBREG_REG (parts.index);
14536       parts.index = simplify_subreg (GET_MODE (parts.index),
14537                                      tmp, GET_MODE (tmp), 0);
14538       gcc_assert (parts.index != NULL_RTX);
14539     }
14540
14541   base = parts.base;
14542   index = parts.index;
14543   disp = parts.disp;
14544   scale = parts.scale;
14545
14546   switch (parts.seg)
14547     {
14548     case SEG_DEFAULT:
14549       break;
14550     case SEG_FS:
14551     case SEG_GS:
14552       if (ASSEMBLER_DIALECT == ASM_ATT)
14553         putc ('%', file);
14554       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
14555       break;
14556     default:
14557       gcc_unreachable ();
14558     }
14559
14560   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
14561   if (TARGET_64BIT && !base && !index)
14562     {
14563       rtx symbol = disp;
14564
14565       if (GET_CODE (disp) == CONST
14566           && GET_CODE (XEXP (disp, 0)) == PLUS
14567           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14568         symbol = XEXP (XEXP (disp, 0), 0);
14569
14570       if (GET_CODE (symbol) == LABEL_REF
14571           || (GET_CODE (symbol) == SYMBOL_REF
14572               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
14573         base = pc_rtx;
14574     }
14575   if (!base && !index)
14576     {
14577       /* Displacement only requires special attention.  */
14578
14579       if (CONST_INT_P (disp))
14580         {
14581           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
14582             fputs ("ds:", file);
14583           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
14584         }
14585       else if (flag_pic)
14586         output_pic_addr_const (file, disp, 0);
14587       else
14588         output_addr_const (file, disp);
14589     }
14590   else
14591     {
14592       /* Print SImode register names for zero-extended
14593          addresses to force addr32 prefix.  */
14594       if (TARGET_64BIT
14595           && (GET_CODE (addr) == ZERO_EXTEND
14596               || GET_CODE (addr) == AND))
14597         {
14598           gcc_assert (!code);
14599           code = 'l';
14600         }
14601
14602       if (ASSEMBLER_DIALECT == ASM_ATT)
14603         {
14604           if (disp)
14605             {
14606               if (flag_pic)
14607                 output_pic_addr_const (file, disp, 0);
14608               else if (GET_CODE (disp) == LABEL_REF)
14609                 output_asm_label (disp);
14610               else
14611                 output_addr_const (file, disp);
14612             }
14613
14614           putc ('(', file);
14615           if (base)
14616             print_reg (base, code, file);
14617           if (index)
14618             {
14619               putc (',', file);
14620               print_reg (index, vsib ? 0 : code, file);
14621               if (scale != 1 || vsib)
14622                 fprintf (file, ",%d", scale);
14623             }
14624           putc (')', file);
14625         }
14626       else
14627         {
14628           rtx offset = NULL_RTX;
14629
14630           if (disp)
14631             {
14632               /* Pull out the offset of a symbol; print any symbol itself.  */
14633               if (GET_CODE (disp) == CONST
14634                   && GET_CODE (XEXP (disp, 0)) == PLUS
14635                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14636                 {
14637                   offset = XEXP (XEXP (disp, 0), 1);
14638                   disp = gen_rtx_CONST (VOIDmode,
14639                                         XEXP (XEXP (disp, 0), 0));
14640                 }
14641
14642               if (flag_pic)
14643                 output_pic_addr_const (file, disp, 0);
14644               else if (GET_CODE (disp) == LABEL_REF)
14645                 output_asm_label (disp);
14646               else if (CONST_INT_P (disp))
14647                 offset = disp;
14648               else
14649                 output_addr_const (file, disp);
14650             }
14651
14652           putc ('[', file);
14653           if (base)
14654             {
14655               print_reg (base, code, file);
14656               if (offset)
14657                 {
14658                   if (INTVAL (offset) >= 0)
14659                     putc ('+', file);
14660                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14661                 }
14662             }
14663           else if (offset)
14664             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14665           else
14666             putc ('0', file);
14667
14668           if (index)
14669             {
14670               putc ('+', file);
14671               print_reg (index, vsib ? 0 : code, file);
14672               if (scale != 1 || vsib)
14673                 fprintf (file, "*%d", scale);
14674             }
14675           putc (']', file);
14676         }
14677     }
14678 }
14679
14680 /* Implementation of TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
14681
14682 static bool
14683 i386_asm_output_addr_const_extra (FILE *file, rtx x)
14684 {
14685   rtx op;
14686
14687   if (GET_CODE (x) != UNSPEC)
14688     return false;
14689
14690   op = XVECEXP (x, 0, 0);
14691   switch (XINT (x, 1))
14692     {
14693     case UNSPEC_GOTTPOFF:
14694       output_addr_const (file, op);
14695       /* FIXME: This might be @TPOFF in Sun ld.  */
14696       fputs ("@gottpoff", file);
14697       break;
14698     case UNSPEC_TPOFF:
14699       output_addr_const (file, op);
14700       fputs ("@tpoff", file);
14701       break;
14702     case UNSPEC_NTPOFF:
14703       output_addr_const (file, op);
14704       if (TARGET_64BIT)
14705         fputs ("@tpoff", file);
14706       else
14707         fputs ("@ntpoff", file);
14708       break;
14709     case UNSPEC_DTPOFF:
14710       output_addr_const (file, op);
14711       fputs ("@dtpoff", file);
14712       break;
14713     case UNSPEC_GOTNTPOFF:
14714       output_addr_const (file, op);
14715       if (TARGET_64BIT)
14716         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
14717                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
14718       else
14719         fputs ("@gotntpoff", file);
14720       break;
14721     case UNSPEC_INDNTPOFF:
14722       output_addr_const (file, op);
14723       fputs ("@indntpoff", file);
14724       break;
14725 #if TARGET_MACHO
14726     case UNSPEC_MACHOPIC_OFFSET:
14727       output_addr_const (file, op);
14728       putc ('-', file);
14729       machopic_output_function_base_name (file);
14730       break;
14731 #endif
14732
14733     case UNSPEC_STACK_CHECK:
14734       {
14735         int offset;
14736
14737         gcc_assert (flag_split_stack);
14738
14739 #ifdef TARGET_THREAD_SPLIT_STACK_OFFSET
14740         offset = TARGET_THREAD_SPLIT_STACK_OFFSET;
14741 #else
14742         gcc_unreachable ();
14743 #endif
14744
14745         fprintf (file, "%s:%d", TARGET_64BIT ? "%fs" : "%gs", offset);
14746       }
14747       break;
14748
14749     default:
14750       return false;
14751     }
14752
14753   return true;
14754 }
14755 \f
14756 /* Split one or more double-mode RTL references into pairs of half-mode
14757    references.  The RTL can be REG, offsettable MEM, integer constant, or
14758    CONST_DOUBLE.  "operands" is a pointer to an array of double-mode RTLs to
14759    split and "num" is its length.  lo_half and hi_half are output arrays
14760    that parallel "operands".  */
14761
14762 void
14763 split_double_mode (enum machine_mode mode, rtx operands[],
14764                    int num, rtx lo_half[], rtx hi_half[])
14765 {
14766   enum machine_mode half_mode;
14767   unsigned int byte;
14768
14769   switch (mode)
14770     {
14771     case TImode:
14772       half_mode = DImode;
14773       break;
14774     case DImode:
14775       half_mode = SImode;
14776       break;
14777     default:
14778       gcc_unreachable ();
14779     }
14780
14781   byte = GET_MODE_SIZE (half_mode);
14782
14783   while (num--)
14784     {
14785       rtx op = operands[num];
14786
14787       /* simplify_subreg refuse to split volatile memory addresses,
14788          but we still have to handle it.  */
14789       if (MEM_P (op))
14790         {
14791           lo_half[num] = adjust_address (op, half_mode, 0);
14792           hi_half[num] = adjust_address (op, half_mode, byte);
14793         }
14794       else
14795         {
14796           lo_half[num] = simplify_gen_subreg (half_mode, op,
14797                                               GET_MODE (op) == VOIDmode
14798                                               ? mode : GET_MODE (op), 0);
14799           hi_half[num] = simplify_gen_subreg (half_mode, op,
14800                                               GET_MODE (op) == VOIDmode
14801                                               ? mode : GET_MODE (op), byte);
14802         }
14803     }
14804 }
14805 \f
14806 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
14807    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
14808    is the expression of the binary operation.  The output may either be
14809    emitted here, or returned to the caller, like all output_* functions.
14810
14811    There is no guarantee that the operands are the same mode, as they
14812    might be within FLOAT or FLOAT_EXTEND expressions.  */
14813
14814 #ifndef SYSV386_COMPAT
14815 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
14816    wants to fix the assemblers because that causes incompatibility
14817    with gcc.  No-one wants to fix gcc because that causes
14818    incompatibility with assemblers...  You can use the option of
14819    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
14820 #define SYSV386_COMPAT 1
14821 #endif
14822
14823 const char *
14824 output_387_binary_op (rtx insn, rtx *operands)
14825 {
14826   static char buf[40];
14827   const char *p;
14828   const char *ssep;
14829   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
14830
14831 #ifdef ENABLE_CHECKING
14832   /* Even if we do not want to check the inputs, this documents input
14833      constraints.  Which helps in understanding the following code.  */
14834   if (STACK_REG_P (operands[0])
14835       && ((REG_P (operands[1])
14836            && REGNO (operands[0]) == REGNO (operands[1])
14837            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
14838           || (REG_P (operands[2])
14839               && REGNO (operands[0]) == REGNO (operands[2])
14840               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
14841       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
14842     ; /* ok */
14843   else
14844     gcc_assert (is_sse);
14845 #endif
14846
14847   switch (GET_CODE (operands[3]))
14848     {
14849     case PLUS:
14850       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14851           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14852         p = "fiadd";
14853       else
14854         p = "fadd";
14855       ssep = "vadd";
14856       break;
14857
14858     case MINUS:
14859       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14860           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14861         p = "fisub";
14862       else
14863         p = "fsub";
14864       ssep = "vsub";
14865       break;
14866
14867     case MULT:
14868       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14869           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14870         p = "fimul";
14871       else
14872         p = "fmul";
14873       ssep = "vmul";
14874       break;
14875
14876     case DIV:
14877       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14878           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14879         p = "fidiv";
14880       else
14881         p = "fdiv";
14882       ssep = "vdiv";
14883       break;
14884
14885     default:
14886       gcc_unreachable ();
14887     }
14888
14889   if (is_sse)
14890    {
14891      if (TARGET_AVX)
14892        {
14893          strcpy (buf, ssep);
14894          if (GET_MODE (operands[0]) == SFmode)
14895            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
14896          else
14897            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
14898        }
14899      else
14900        {
14901          strcpy (buf, ssep + 1);
14902          if (GET_MODE (operands[0]) == SFmode)
14903            strcat (buf, "ss\t{%2, %0|%0, %2}");
14904          else
14905            strcat (buf, "sd\t{%2, %0|%0, %2}");
14906        }
14907       return buf;
14908    }
14909   strcpy (buf, p);
14910
14911   switch (GET_CODE (operands[3]))
14912     {
14913     case MULT:
14914     case PLUS:
14915       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
14916         {
14917           rtx temp = operands[2];
14918           operands[2] = operands[1];
14919           operands[1] = temp;
14920         }
14921
14922       /* know operands[0] == operands[1].  */
14923
14924       if (MEM_P (operands[2]))
14925         {
14926           p = "%Z2\t%2";
14927           break;
14928         }
14929
14930       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
14931         {
14932           if (STACK_TOP_P (operands[0]))
14933             /* How is it that we are storing to a dead operand[2]?
14934                Well, presumably operands[1] is dead too.  We can't
14935                store the result to st(0) as st(0) gets popped on this
14936                instruction.  Instead store to operands[2] (which I
14937                think has to be st(1)).  st(1) will be popped later.
14938                gcc <= 2.8.1 didn't have this check and generated
14939                assembly code that the Unixware assembler rejected.  */
14940             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
14941           else
14942             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
14943           break;
14944         }
14945
14946       if (STACK_TOP_P (operands[0]))
14947         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
14948       else
14949         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
14950       break;
14951
14952     case MINUS:
14953     case DIV:
14954       if (MEM_P (operands[1]))
14955         {
14956           p = "r%Z1\t%1";
14957           break;
14958         }
14959
14960       if (MEM_P (operands[2]))
14961         {
14962           p = "%Z2\t%2";
14963           break;
14964         }
14965
14966       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
14967         {
14968 #if SYSV386_COMPAT
14969           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
14970              derived assemblers, confusingly reverse the direction of
14971              the operation for fsub{r} and fdiv{r} when the
14972              destination register is not st(0).  The Intel assembler
14973              doesn't have this brain damage.  Read !SYSV386_COMPAT to
14974              figure out what the hardware really does.  */
14975           if (STACK_TOP_P (operands[0]))
14976             p = "{p\t%0, %2|rp\t%2, %0}";
14977           else
14978             p = "{rp\t%2, %0|p\t%0, %2}";
14979 #else
14980           if (STACK_TOP_P (operands[0]))
14981             /* As above for fmul/fadd, we can't store to st(0).  */
14982             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
14983           else
14984             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
14985 #endif
14986           break;
14987         }
14988
14989       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
14990         {
14991 #if SYSV386_COMPAT
14992           if (STACK_TOP_P (operands[0]))
14993             p = "{rp\t%0, %1|p\t%1, %0}";
14994           else
14995             p = "{p\t%1, %0|rp\t%0, %1}";
14996 #else
14997           if (STACK_TOP_P (operands[0]))
14998             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
14999           else
15000             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
15001 #endif
15002           break;
15003         }
15004
15005       if (STACK_TOP_P (operands[0]))
15006         {
15007           if (STACK_TOP_P (operands[1]))
15008             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
15009           else
15010             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
15011           break;
15012         }
15013       else if (STACK_TOP_P (operands[1]))
15014         {
15015 #if SYSV386_COMPAT
15016           p = "{\t%1, %0|r\t%0, %1}";
15017 #else
15018           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
15019 #endif
15020         }
15021       else
15022         {
15023 #if SYSV386_COMPAT
15024           p = "{r\t%2, %0|\t%0, %2}";
15025 #else
15026           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
15027 #endif
15028         }
15029       break;
15030
15031     default:
15032       gcc_unreachable ();
15033     }
15034
15035   strcat (buf, p);
15036   return buf;
15037 }
15038
15039 /* Return needed mode for entity in optimize_mode_switching pass.  */
15040
15041 int
15042 ix86_mode_needed (int entity, rtx insn)
15043 {
15044   enum attr_i387_cw mode;
15045
15046   /* The mode UNINITIALIZED is used to store control word after a
15047      function call or ASM pattern.  The mode ANY specify that function
15048      has no requirements on the control word and make no changes in the
15049      bits we are interested in.  */
15050
15051   if (CALL_P (insn)
15052       || (NONJUMP_INSN_P (insn)
15053           && (asm_noperands (PATTERN (insn)) >= 0
15054               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
15055     return I387_CW_UNINITIALIZED;
15056
15057   if (recog_memoized (insn) < 0)
15058     return I387_CW_ANY;
15059
15060   mode = get_attr_i387_cw (insn);
15061
15062   switch (entity)
15063     {
15064     case I387_TRUNC:
15065       if (mode == I387_CW_TRUNC)
15066         return mode;
15067       break;
15068
15069     case I387_FLOOR:
15070       if (mode == I387_CW_FLOOR)
15071         return mode;
15072       break;
15073
15074     case I387_CEIL:
15075       if (mode == I387_CW_CEIL)
15076         return mode;
15077       break;
15078
15079     case I387_MASK_PM:
15080       if (mode == I387_CW_MASK_PM)
15081         return mode;
15082       break;
15083
15084     default:
15085       gcc_unreachable ();
15086     }
15087
15088   return I387_CW_ANY;
15089 }
15090
15091 /* Output code to initialize control word copies used by trunc?f?i and
15092    rounding patterns.  CURRENT_MODE is set to current control word,
15093    while NEW_MODE is set to new control word.  */
15094
15095 void
15096 emit_i387_cw_initialization (int mode)
15097 {
15098   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
15099   rtx new_mode;
15100
15101   enum ix86_stack_slot slot;
15102
15103   rtx reg = gen_reg_rtx (HImode);
15104
15105   emit_insn (gen_x86_fnstcw_1 (stored_mode));
15106   emit_move_insn (reg, copy_rtx (stored_mode));
15107
15108   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
15109       || optimize_function_for_size_p (cfun))
15110     {
15111       switch (mode)
15112         {
15113         case I387_CW_TRUNC:
15114           /* round toward zero (truncate) */
15115           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
15116           slot = SLOT_CW_TRUNC;
15117           break;
15118
15119         case I387_CW_FLOOR:
15120           /* round down toward -oo */
15121           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
15122           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
15123           slot = SLOT_CW_FLOOR;
15124           break;
15125
15126         case I387_CW_CEIL:
15127           /* round up toward +oo */
15128           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
15129           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
15130           slot = SLOT_CW_CEIL;
15131           break;
15132
15133         case I387_CW_MASK_PM:
15134           /* mask precision exception for nearbyint() */
15135           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
15136           slot = SLOT_CW_MASK_PM;
15137           break;
15138
15139         default:
15140           gcc_unreachable ();
15141         }
15142     }
15143   else
15144     {
15145       switch (mode)
15146         {
15147         case I387_CW_TRUNC:
15148           /* round toward zero (truncate) */
15149           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
15150           slot = SLOT_CW_TRUNC;
15151           break;
15152
15153         case I387_CW_FLOOR:
15154           /* round down toward -oo */
15155           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
15156           slot = SLOT_CW_FLOOR;
15157           break;
15158
15159         case I387_CW_CEIL:
15160           /* round up toward +oo */
15161           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
15162           slot = SLOT_CW_CEIL;
15163           break;
15164
15165         case I387_CW_MASK_PM:
15166           /* mask precision exception for nearbyint() */
15167           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
15168           slot = SLOT_CW_MASK_PM;
15169           break;
15170
15171         default:
15172           gcc_unreachable ();
15173         }
15174     }
15175
15176   gcc_assert (slot < MAX_386_STACK_LOCALS);
15177
15178   new_mode = assign_386_stack_local (HImode, slot);
15179   emit_move_insn (new_mode, reg);
15180 }
15181
15182 /* Output code for INSN to convert a float to a signed int.  OPERANDS
15183    are the insn operands.  The output may be [HSD]Imode and the input
15184    operand may be [SDX]Fmode.  */
15185
15186 const char *
15187 output_fix_trunc (rtx insn, rtx *operands, bool fisttp)
15188 {
15189   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
15190   int dimode_p = GET_MODE (operands[0]) == DImode;
15191   int round_mode = get_attr_i387_cw (insn);
15192
15193   /* Jump through a hoop or two for DImode, since the hardware has no
15194      non-popping instruction.  We used to do this a different way, but
15195      that was somewhat fragile and broke with post-reload splitters.  */
15196   if ((dimode_p || fisttp) && !stack_top_dies)
15197     output_asm_insn ("fld\t%y1", operands);
15198
15199   gcc_assert (STACK_TOP_P (operands[1]));
15200   gcc_assert (MEM_P (operands[0]));
15201   gcc_assert (GET_MODE (operands[1]) != TFmode);
15202
15203   if (fisttp)
15204       output_asm_insn ("fisttp%Z0\t%0", operands);
15205   else
15206     {
15207       if (round_mode != I387_CW_ANY)
15208         output_asm_insn ("fldcw\t%3", operands);
15209       if (stack_top_dies || dimode_p)
15210         output_asm_insn ("fistp%Z0\t%0", operands);
15211       else
15212         output_asm_insn ("fist%Z0\t%0", operands);
15213       if (round_mode != I387_CW_ANY)
15214         output_asm_insn ("fldcw\t%2", operands);
15215     }
15216
15217   return "";
15218 }
15219
15220 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
15221    have the values zero or one, indicates the ffreep insn's operand
15222    from the OPERANDS array.  */
15223
15224 static const char *
15225 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
15226 {
15227   if (TARGET_USE_FFREEP)
15228 #ifdef HAVE_AS_IX86_FFREEP
15229     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
15230 #else
15231     {
15232       static char retval[32];
15233       int regno = REGNO (operands[opno]);
15234
15235       gcc_assert (FP_REGNO_P (regno));
15236
15237       regno -= FIRST_STACK_REG;
15238
15239       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
15240       return retval;
15241     }
15242 #endif
15243
15244   return opno ? "fstp\t%y1" : "fstp\t%y0";
15245 }
15246
15247
15248 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
15249    should be used.  UNORDERED_P is true when fucom should be used.  */
15250
15251 const char *
15252 output_fp_compare (rtx insn, rtx *operands, bool eflags_p, bool unordered_p)
15253 {
15254   int stack_top_dies;
15255   rtx cmp_op0, cmp_op1;
15256   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
15257
15258   if (eflags_p)
15259     {
15260       cmp_op0 = operands[0];
15261       cmp_op1 = operands[1];
15262     }
15263   else
15264     {
15265       cmp_op0 = operands[1];
15266       cmp_op1 = operands[2];
15267     }
15268
15269   if (is_sse)
15270     {
15271       if (GET_MODE (operands[0]) == SFmode)
15272         if (unordered_p)
15273           return "%vucomiss\t{%1, %0|%0, %1}";
15274         else
15275           return "%vcomiss\t{%1, %0|%0, %1}";
15276       else
15277         if (unordered_p)
15278           return "%vucomisd\t{%1, %0|%0, %1}";
15279         else
15280           return "%vcomisd\t{%1, %0|%0, %1}";
15281     }
15282
15283   gcc_assert (STACK_TOP_P (cmp_op0));
15284
15285   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
15286
15287   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
15288     {
15289       if (stack_top_dies)
15290         {
15291           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
15292           return output_387_ffreep (operands, 1);
15293         }
15294       else
15295         return "ftst\n\tfnstsw\t%0";
15296     }
15297
15298   if (STACK_REG_P (cmp_op1)
15299       && stack_top_dies
15300       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
15301       && REGNO (cmp_op1) != FIRST_STACK_REG)
15302     {
15303       /* If both the top of the 387 stack dies, and the other operand
15304          is also a stack register that dies, then this must be a
15305          `fcompp' float compare */
15306
15307       if (eflags_p)
15308         {
15309           /* There is no double popping fcomi variant.  Fortunately,
15310              eflags is immune from the fstp's cc clobbering.  */
15311           if (unordered_p)
15312             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
15313           else
15314             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
15315           return output_387_ffreep (operands, 0);
15316         }
15317       else
15318         {
15319           if (unordered_p)
15320             return "fucompp\n\tfnstsw\t%0";
15321           else
15322             return "fcompp\n\tfnstsw\t%0";
15323         }
15324     }
15325   else
15326     {
15327       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
15328
15329       static const char * const alt[16] =
15330       {
15331         "fcom%Z2\t%y2\n\tfnstsw\t%0",
15332         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
15333         "fucom%Z2\t%y2\n\tfnstsw\t%0",
15334         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
15335
15336         "ficom%Z2\t%y2\n\tfnstsw\t%0",
15337         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
15338         NULL,
15339         NULL,
15340
15341         "fcomi\t{%y1, %0|%0, %y1}",
15342         "fcomip\t{%y1, %0|%0, %y1}",
15343         "fucomi\t{%y1, %0|%0, %y1}",
15344         "fucomip\t{%y1, %0|%0, %y1}",
15345
15346         NULL,
15347         NULL,
15348         NULL,
15349         NULL
15350       };
15351
15352       int mask;
15353       const char *ret;
15354
15355       mask  = eflags_p << 3;
15356       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
15357       mask |= unordered_p << 1;
15358       mask |= stack_top_dies;
15359
15360       gcc_assert (mask < 16);
15361       ret = alt[mask];
15362       gcc_assert (ret);
15363
15364       return ret;
15365     }
15366 }
15367
15368 void
15369 ix86_output_addr_vec_elt (FILE *file, int value)
15370 {
15371   const char *directive = ASM_LONG;
15372
15373 #ifdef ASM_QUAD
15374   if (TARGET_LP64)
15375     directive = ASM_QUAD;
15376 #else
15377   gcc_assert (!TARGET_64BIT);
15378 #endif
15379
15380   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
15381 }
15382
15383 void
15384 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
15385 {
15386   const char *directive = ASM_LONG;
15387
15388 #ifdef ASM_QUAD
15389   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
15390     directive = ASM_QUAD;
15391 #else
15392   gcc_assert (!TARGET_64BIT);
15393 #endif
15394   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
15395   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
15396     fprintf (file, "%s%s%d-%s%d\n",
15397              directive, LPREFIX, value, LPREFIX, rel);
15398   else if (HAVE_AS_GOTOFF_IN_DATA)
15399     fprintf (file, ASM_LONG "%s%d@GOTOFF\n", LPREFIX, value);
15400 #if TARGET_MACHO
15401   else if (TARGET_MACHO)
15402     {
15403       fprintf (file, ASM_LONG "%s%d-", LPREFIX, value);
15404       machopic_output_function_base_name (file);
15405       putc ('\n', file);
15406     }
15407 #endif
15408   else
15409     asm_fprintf (file, ASM_LONG "%U%s+[.-%s%d]\n",
15410                  GOT_SYMBOL_NAME, LPREFIX, value);
15411 }
15412 \f
15413 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
15414    for the target.  */
15415
15416 void
15417 ix86_expand_clear (rtx dest)
15418 {
15419   rtx tmp;
15420
15421   /* We play register width games, which are only valid after reload.  */
15422   gcc_assert (reload_completed);
15423
15424   /* Avoid HImode and its attendant prefix byte.  */
15425   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
15426     dest = gen_rtx_REG (SImode, REGNO (dest));
15427   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
15428
15429   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
15430   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
15431     {
15432       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15433       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
15434     }
15435
15436   emit_insn (tmp);
15437 }
15438
15439 /* X is an unchanging MEM.  If it is a constant pool reference, return
15440    the constant pool rtx, else NULL.  */
15441
15442 rtx
15443 maybe_get_pool_constant (rtx x)
15444 {
15445   x = ix86_delegitimize_address (XEXP (x, 0));
15446
15447   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
15448     return get_pool_constant (x);
15449
15450   return NULL_RTX;
15451 }
15452
15453 void
15454 ix86_expand_move (enum machine_mode mode, rtx operands[])
15455 {
15456   rtx op0, op1;
15457   enum tls_model model;
15458
15459   op0 = operands[0];
15460   op1 = operands[1];
15461
15462   if (GET_CODE (op1) == SYMBOL_REF)
15463     {
15464       model = SYMBOL_REF_TLS_MODEL (op1);
15465       if (model)
15466         {
15467           op1 = legitimize_tls_address (op1, model, true);
15468           op1 = force_operand (op1, op0);
15469           if (op1 == op0)
15470             return;
15471           if (GET_MODE (op1) != mode)
15472             op1 = convert_to_mode (mode, op1, 1);
15473         }
15474       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15475                && SYMBOL_REF_DLLIMPORT_P (op1))
15476         op1 = legitimize_dllimport_symbol (op1, false);
15477     }
15478   else if (GET_CODE (op1) == CONST
15479            && GET_CODE (XEXP (op1, 0)) == PLUS
15480            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
15481     {
15482       rtx addend = XEXP (XEXP (op1, 0), 1);
15483       rtx symbol = XEXP (XEXP (op1, 0), 0);
15484       rtx tmp = NULL;
15485
15486       model = SYMBOL_REF_TLS_MODEL (symbol);
15487       if (model)
15488         tmp = legitimize_tls_address (symbol, model, true);
15489       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15490                && SYMBOL_REF_DLLIMPORT_P (symbol))
15491         tmp = legitimize_dllimport_symbol (symbol, true);
15492
15493       if (tmp)
15494         {
15495           tmp = force_operand (tmp, NULL);
15496           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
15497                                      op0, 1, OPTAB_DIRECT);
15498           if (tmp == op0)
15499             return;
15500           if (GET_MODE (tmp) != mode)
15501             op1 = convert_to_mode (mode, tmp, 1);
15502         }
15503     }
15504
15505   if ((flag_pic || MACHOPIC_INDIRECT)
15506       && symbolic_operand (op1, mode))
15507     {
15508       if (TARGET_MACHO && !TARGET_64BIT)
15509         {
15510 #if TARGET_MACHO
15511           /* dynamic-no-pic */
15512           if (MACHOPIC_INDIRECT)
15513             {
15514               rtx temp = ((reload_in_progress
15515                            || ((op0 && REG_P (op0))
15516                                && mode == Pmode))
15517                           ? op0 : gen_reg_rtx (Pmode));
15518               op1 = machopic_indirect_data_reference (op1, temp);
15519               if (MACHOPIC_PURE)
15520                 op1 = machopic_legitimize_pic_address (op1, mode,
15521                                                        temp == op1 ? 0 : temp);
15522             }
15523           if (op0 != op1 && GET_CODE (op0) != MEM)
15524             {
15525               rtx insn = gen_rtx_SET (VOIDmode, op0, op1);
15526               emit_insn (insn);
15527               return;
15528             }
15529           if (GET_CODE (op0) == MEM)
15530             op1 = force_reg (Pmode, op1);
15531           else
15532             {
15533               rtx temp = op0;
15534               if (GET_CODE (temp) != REG)
15535                 temp = gen_reg_rtx (Pmode);
15536               temp = legitimize_pic_address (op1, temp);
15537               if (temp == op0)
15538             return;
15539               op1 = temp;
15540             }
15541       /* dynamic-no-pic */
15542 #endif
15543         }
15544       else
15545         {
15546           if (MEM_P (op0))
15547             op1 = force_reg (mode, op1);
15548           else if (!(TARGET_64BIT && x86_64_movabs_operand (op1, DImode)))
15549             {
15550               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
15551               op1 = legitimize_pic_address (op1, reg);
15552               if (op0 == op1)
15553                 return;
15554               if (GET_MODE (op1) != mode)
15555                 op1 = convert_to_mode (mode, op1, 1);
15556             }
15557         }
15558     }
15559   else
15560     {
15561       if (MEM_P (op0)
15562           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
15563               || !push_operand (op0, mode))
15564           && MEM_P (op1))
15565         op1 = force_reg (mode, op1);
15566
15567       if (push_operand (op0, mode)
15568           && ! general_no_elim_operand (op1, mode))
15569         op1 = copy_to_mode_reg (mode, op1);
15570
15571       /* Force large constants in 64bit compilation into register
15572          to get them CSEed.  */
15573       if (can_create_pseudo_p ()
15574           && (mode == DImode) && TARGET_64BIT
15575           && immediate_operand (op1, mode)
15576           && !x86_64_zext_immediate_operand (op1, VOIDmode)
15577           && !register_operand (op0, mode)
15578           && optimize)
15579         op1 = copy_to_mode_reg (mode, op1);
15580
15581       if (can_create_pseudo_p ()
15582           && FLOAT_MODE_P (mode)
15583           && GET_CODE (op1) == CONST_DOUBLE)
15584         {
15585           /* If we are loading a floating point constant to a register,
15586              force the value to memory now, since we'll get better code
15587              out the back end.  */
15588
15589           op1 = validize_mem (force_const_mem (mode, op1));
15590           if (!register_operand (op0, mode))
15591             {
15592               rtx temp = gen_reg_rtx (mode);
15593               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
15594               emit_move_insn (op0, temp);
15595               return;
15596             }
15597         }
15598     }
15599
15600   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15601 }
15602
15603 void
15604 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
15605 {
15606   rtx op0 = operands[0], op1 = operands[1];
15607   unsigned int align = GET_MODE_ALIGNMENT (mode);
15608
15609   /* Force constants other than zero into memory.  We do not know how
15610      the instructions used to build constants modify the upper 64 bits
15611      of the register, once we have that information we may be able
15612      to handle some of them more efficiently.  */
15613   if (can_create_pseudo_p ()
15614       && register_operand (op0, mode)
15615       && (CONSTANT_P (op1)
15616           || (GET_CODE (op1) == SUBREG
15617               && CONSTANT_P (SUBREG_REG (op1))))
15618       && !standard_sse_constant_p (op1))
15619     op1 = validize_mem (force_const_mem (mode, op1));
15620
15621   /* We need to check memory alignment for SSE mode since attribute
15622      can make operands unaligned.  */
15623   if (can_create_pseudo_p ()
15624       && SSE_REG_MODE_P (mode)
15625       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
15626           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
15627     {
15628       rtx tmp[2];
15629
15630       /* ix86_expand_vector_move_misalign() does not like constants ... */
15631       if (CONSTANT_P (op1)
15632           || (GET_CODE (op1) == SUBREG
15633               && CONSTANT_P (SUBREG_REG (op1))))
15634         op1 = validize_mem (force_const_mem (mode, op1));
15635
15636       /* ... nor both arguments in memory.  */
15637       if (!register_operand (op0, mode)
15638           && !register_operand (op1, mode))
15639         op1 = force_reg (mode, op1);
15640
15641       tmp[0] = op0; tmp[1] = op1;
15642       ix86_expand_vector_move_misalign (mode, tmp);
15643       return;
15644     }
15645
15646   /* Make operand1 a register if it isn't already.  */
15647   if (can_create_pseudo_p ()
15648       && !register_operand (op0, mode)
15649       && !register_operand (op1, mode))
15650     {
15651       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
15652       return;
15653     }
15654
15655   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15656 }
15657
15658 /* Split 32-byte AVX unaligned load and store if needed.  */
15659
15660 static void
15661 ix86_avx256_split_vector_move_misalign (rtx op0, rtx op1)
15662 {
15663   rtx m;
15664   rtx (*extract) (rtx, rtx, rtx);
15665   rtx (*move_unaligned) (rtx, rtx);
15666   enum machine_mode mode;
15667
15668   switch (GET_MODE (op0))
15669     {
15670     default:
15671       gcc_unreachable ();
15672     case V32QImode:
15673       extract = gen_avx_vextractf128v32qi;
15674       move_unaligned = gen_avx_movdqu256;
15675       mode = V16QImode;
15676       break;
15677     case V8SFmode:
15678       extract = gen_avx_vextractf128v8sf;
15679       move_unaligned = gen_avx_movups256;
15680       mode = V4SFmode;
15681       break;
15682     case V4DFmode:
15683       extract = gen_avx_vextractf128v4df;
15684       move_unaligned = gen_avx_movupd256;
15685       mode = V2DFmode;
15686       break;
15687     }
15688
15689   if (MEM_P (op1) && TARGET_AVX256_SPLIT_UNALIGNED_LOAD)
15690     {
15691       rtx r = gen_reg_rtx (mode);
15692       m = adjust_address (op1, mode, 0);
15693       emit_move_insn (r, m);
15694       m = adjust_address (op1, mode, 16);
15695       r = gen_rtx_VEC_CONCAT (GET_MODE (op0), r, m);
15696       emit_move_insn (op0, r);
15697     }
15698   else if (MEM_P (op0) && TARGET_AVX256_SPLIT_UNALIGNED_STORE)
15699     {
15700       m = adjust_address (op0, mode, 0);
15701       emit_insn (extract (m, op1, const0_rtx));
15702       m = adjust_address (op0, mode, 16);
15703       emit_insn (extract (m, op1, const1_rtx));
15704     }
15705   else
15706     emit_insn (move_unaligned (op0, op1));
15707 }
15708
15709 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
15710    straight to ix86_expand_vector_move.  */
15711 /* Code generation for scalar reg-reg moves of single and double precision data:
15712      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
15713        movaps reg, reg
15714      else
15715        movss reg, reg
15716      if (x86_sse_partial_reg_dependency == true)
15717        movapd reg, reg
15718      else
15719        movsd reg, reg
15720
15721    Code generation for scalar loads of double precision data:
15722      if (x86_sse_split_regs == true)
15723        movlpd mem, reg      (gas syntax)
15724      else
15725        movsd mem, reg
15726
15727    Code generation for unaligned packed loads of single precision data
15728    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
15729      if (x86_sse_unaligned_move_optimal)
15730        movups mem, reg
15731
15732      if (x86_sse_partial_reg_dependency == true)
15733        {
15734          xorps  reg, reg
15735          movlps mem, reg
15736          movhps mem+8, reg
15737        }
15738      else
15739        {
15740          movlps mem, reg
15741          movhps mem+8, reg
15742        }
15743
15744    Code generation for unaligned packed loads of double precision data
15745    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
15746      if (x86_sse_unaligned_move_optimal)
15747        movupd mem, reg
15748
15749      if (x86_sse_split_regs == true)
15750        {
15751          movlpd mem, reg
15752          movhpd mem+8, reg
15753        }
15754      else
15755        {
15756          movsd  mem, reg
15757          movhpd mem+8, reg
15758        }
15759  */
15760
15761 void
15762 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
15763 {
15764   rtx op0, op1, m;
15765
15766   op0 = operands[0];
15767   op1 = operands[1];
15768
15769   if (TARGET_AVX)
15770     {
15771       switch (GET_MODE_CLASS (mode))
15772         {
15773         case MODE_VECTOR_INT:
15774         case MODE_INT:
15775           switch (GET_MODE_SIZE (mode))
15776             {
15777             case 16:
15778               /*  If we're optimizing for size, movups is the smallest.  */
15779               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15780                 {
15781                   op0 = gen_lowpart (V4SFmode, op0);
15782                   op1 = gen_lowpart (V4SFmode, op1);
15783                   emit_insn (gen_sse_movups (op0, op1));
15784                   return;
15785                 }
15786               op0 = gen_lowpart (V16QImode, op0);
15787               op1 = gen_lowpart (V16QImode, op1);
15788               emit_insn (gen_sse2_movdqu (op0, op1));
15789               break;
15790             case 32:
15791               op0 = gen_lowpart (V32QImode, op0);
15792               op1 = gen_lowpart (V32QImode, op1);
15793               ix86_avx256_split_vector_move_misalign (op0, op1);
15794               break;
15795             default:
15796               gcc_unreachable ();
15797             }
15798           break;
15799         case MODE_VECTOR_FLOAT:
15800           op0 = gen_lowpart (mode, op0);
15801           op1 = gen_lowpart (mode, op1);
15802
15803           switch (mode)
15804             {
15805             case V4SFmode:
15806               emit_insn (gen_sse_movups (op0, op1));
15807               break;
15808             case V8SFmode:
15809               ix86_avx256_split_vector_move_misalign (op0, op1);
15810               break;
15811             case V2DFmode:
15812               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15813                 {
15814                   op0 = gen_lowpart (V4SFmode, op0);
15815                   op1 = gen_lowpart (V4SFmode, op1);
15816                   emit_insn (gen_sse_movups (op0, op1));
15817                   return;
15818                 }
15819               emit_insn (gen_sse2_movupd (op0, op1));
15820               break;
15821             case V4DFmode:
15822               ix86_avx256_split_vector_move_misalign (op0, op1);
15823               break;
15824             default:
15825               gcc_unreachable ();
15826             }
15827           break;
15828
15829         default:
15830           gcc_unreachable ();
15831         }
15832
15833       return;
15834     }
15835
15836   if (MEM_P (op1))
15837     {
15838       /* If we're optimizing for size, movups is the smallest.  */
15839       if (optimize_insn_for_size_p ()
15840           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15841         {
15842           op0 = gen_lowpart (V4SFmode, op0);
15843           op1 = gen_lowpart (V4SFmode, op1);
15844           emit_insn (gen_sse_movups (op0, op1));
15845           return;
15846         }
15847
15848       /* ??? If we have typed data, then it would appear that using
15849          movdqu is the only way to get unaligned data loaded with
15850          integer type.  */
15851       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15852         {
15853           op0 = gen_lowpart (V16QImode, op0);
15854           op1 = gen_lowpart (V16QImode, op1);
15855           emit_insn (gen_sse2_movdqu (op0, op1));
15856           return;
15857         }
15858
15859       if (TARGET_SSE2 && mode == V2DFmode)
15860         {
15861           rtx zero;
15862
15863           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
15864             {
15865               op0 = gen_lowpart (V2DFmode, op0);
15866               op1 = gen_lowpart (V2DFmode, op1);
15867               emit_insn (gen_sse2_movupd (op0, op1));
15868               return;
15869             }
15870
15871           /* When SSE registers are split into halves, we can avoid
15872              writing to the top half twice.  */
15873           if (TARGET_SSE_SPLIT_REGS)
15874             {
15875               emit_clobber (op0);
15876               zero = op0;
15877             }
15878           else
15879             {
15880               /* ??? Not sure about the best option for the Intel chips.
15881                  The following would seem to satisfy; the register is
15882                  entirely cleared, breaking the dependency chain.  We
15883                  then store to the upper half, with a dependency depth
15884                  of one.  A rumor has it that Intel recommends two movsd
15885                  followed by an unpacklpd, but this is unconfirmed.  And
15886                  given that the dependency depth of the unpacklpd would
15887                  still be one, I'm not sure why this would be better.  */
15888               zero = CONST0_RTX (V2DFmode);
15889             }
15890
15891           m = adjust_address (op1, DFmode, 0);
15892           emit_insn (gen_sse2_loadlpd (op0, zero, m));
15893           m = adjust_address (op1, DFmode, 8);
15894           emit_insn (gen_sse2_loadhpd (op0, op0, m));
15895         }
15896       else
15897         {
15898           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
15899             {
15900               op0 = gen_lowpart (V4SFmode, op0);
15901               op1 = gen_lowpart (V4SFmode, op1);
15902               emit_insn (gen_sse_movups (op0, op1));
15903               return;
15904             }
15905
15906           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
15907             emit_move_insn (op0, CONST0_RTX (mode));
15908           else
15909             emit_clobber (op0);
15910
15911           if (mode != V4SFmode)
15912             op0 = gen_lowpart (V4SFmode, op0);
15913           m = adjust_address (op1, V2SFmode, 0);
15914           emit_insn (gen_sse_loadlps (op0, op0, m));
15915           m = adjust_address (op1, V2SFmode, 8);
15916           emit_insn (gen_sse_loadhps (op0, op0, m));
15917         }
15918     }
15919   else if (MEM_P (op0))
15920     {
15921       /* If we're optimizing for size, movups is the smallest.  */
15922       if (optimize_insn_for_size_p ()
15923           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15924         {
15925           op0 = gen_lowpart (V4SFmode, op0);
15926           op1 = gen_lowpart (V4SFmode, op1);
15927           emit_insn (gen_sse_movups (op0, op1));
15928           return;
15929         }
15930
15931       /* ??? Similar to above, only less clear because of quote
15932          typeless stores unquote.  */
15933       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
15934           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15935         {
15936           op0 = gen_lowpart (V16QImode, op0);
15937           op1 = gen_lowpart (V16QImode, op1);
15938           emit_insn (gen_sse2_movdqu (op0, op1));
15939           return;
15940         }
15941
15942       if (TARGET_SSE2 && mode == V2DFmode)
15943         {
15944           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
15945             {
15946               op0 = gen_lowpart (V2DFmode, op0);
15947               op1 = gen_lowpart (V2DFmode, op1);
15948               emit_insn (gen_sse2_movupd (op0, op1));
15949             }
15950           else
15951             {
15952               m = adjust_address (op0, DFmode, 0);
15953               emit_insn (gen_sse2_storelpd (m, op1));
15954               m = adjust_address (op0, DFmode, 8);
15955               emit_insn (gen_sse2_storehpd (m, op1));
15956             }
15957         }
15958       else
15959         {
15960           if (mode != V4SFmode)
15961             op1 = gen_lowpart (V4SFmode, op1);
15962
15963           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
15964             {
15965               op0 = gen_lowpart (V4SFmode, op0);
15966               emit_insn (gen_sse_movups (op0, op1));
15967             }
15968           else
15969             {
15970               m = adjust_address (op0, V2SFmode, 0);
15971               emit_insn (gen_sse_storelps (m, op1));
15972               m = adjust_address (op0, V2SFmode, 8);
15973               emit_insn (gen_sse_storehps (m, op1));
15974             }
15975         }
15976     }
15977   else
15978     gcc_unreachable ();
15979 }
15980
15981 /* Expand a push in MODE.  This is some mode for which we do not support
15982    proper push instructions, at least from the registers that we expect
15983    the value to live in.  */
15984
15985 void
15986 ix86_expand_push (enum machine_mode mode, rtx x)
15987 {
15988   rtx tmp;
15989
15990   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
15991                              GEN_INT (-GET_MODE_SIZE (mode)),
15992                              stack_pointer_rtx, 1, OPTAB_DIRECT);
15993   if (tmp != stack_pointer_rtx)
15994     emit_move_insn (stack_pointer_rtx, tmp);
15995
15996   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
15997
15998   /* When we push an operand onto stack, it has to be aligned at least
15999      at the function argument boundary.  However since we don't have
16000      the argument type, we can't determine the actual argument
16001      boundary.  */
16002   emit_move_insn (tmp, x);
16003 }
16004
16005 /* Helper function of ix86_fixup_binary_operands to canonicalize
16006    operand order.  Returns true if the operands should be swapped.  */
16007
16008 static bool
16009 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
16010                              rtx operands[])
16011 {
16012   rtx dst = operands[0];
16013   rtx src1 = operands[1];
16014   rtx src2 = operands[2];
16015
16016   /* If the operation is not commutative, we can't do anything.  */
16017   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
16018     return false;
16019
16020   /* Highest priority is that src1 should match dst.  */
16021   if (rtx_equal_p (dst, src1))
16022     return false;
16023   if (rtx_equal_p (dst, src2))
16024     return true;
16025
16026   /* Next highest priority is that immediate constants come second.  */
16027   if (immediate_operand (src2, mode))
16028     return false;
16029   if (immediate_operand (src1, mode))
16030     return true;
16031
16032   /* Lowest priority is that memory references should come second.  */
16033   if (MEM_P (src2))
16034     return false;
16035   if (MEM_P (src1))
16036     return true;
16037
16038   return false;
16039 }
16040
16041
16042 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
16043    destination to use for the operation.  If different from the true
16044    destination in operands[0], a copy operation will be required.  */
16045
16046 rtx
16047 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
16048                             rtx operands[])
16049 {
16050   rtx dst = operands[0];
16051   rtx src1 = operands[1];
16052   rtx src2 = operands[2];
16053
16054   /* Canonicalize operand order.  */
16055   if (ix86_swap_binary_operands_p (code, mode, operands))
16056     {
16057       rtx temp;
16058
16059       /* It is invalid to swap operands of different modes.  */
16060       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
16061
16062       temp = src1;
16063       src1 = src2;
16064       src2 = temp;
16065     }
16066
16067   /* Both source operands cannot be in memory.  */
16068   if (MEM_P (src1) && MEM_P (src2))
16069     {
16070       /* Optimization: Only read from memory once.  */
16071       if (rtx_equal_p (src1, src2))
16072         {
16073           src2 = force_reg (mode, src2);
16074           src1 = src2;
16075         }
16076       else
16077         src2 = force_reg (mode, src2);
16078     }
16079
16080   /* If the destination is memory, and we do not have matching source
16081      operands, do things in registers.  */
16082   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
16083     dst = gen_reg_rtx (mode);
16084
16085   /* Source 1 cannot be a constant.  */
16086   if (CONSTANT_P (src1))
16087     src1 = force_reg (mode, src1);
16088
16089   /* Source 1 cannot be a non-matching memory.  */
16090   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
16091     src1 = force_reg (mode, src1);
16092
16093   /* Improve address combine.  */
16094   if (code == PLUS
16095       && GET_MODE_CLASS (mode) == MODE_INT
16096       && MEM_P (src2))
16097     src2 = force_reg (mode, src2);
16098
16099   operands[1] = src1;
16100   operands[2] = src2;
16101   return dst;
16102 }
16103
16104 /* Similarly, but assume that the destination has already been
16105    set up properly.  */
16106
16107 void
16108 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
16109                                     enum machine_mode mode, rtx operands[])
16110 {
16111   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
16112   gcc_assert (dst == operands[0]);
16113 }
16114
16115 /* Attempt to expand a binary operator.  Make the expansion closer to the
16116    actual machine, then just general_operand, which will allow 3 separate
16117    memory references (one output, two input) in a single insn.  */
16118
16119 void
16120 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
16121                              rtx operands[])
16122 {
16123   rtx src1, src2, dst, op, clob;
16124
16125   dst = ix86_fixup_binary_operands (code, mode, operands);
16126   src1 = operands[1];
16127   src2 = operands[2];
16128
16129  /* Emit the instruction.  */
16130
16131   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
16132   if (reload_in_progress)
16133     {
16134       /* Reload doesn't know about the flags register, and doesn't know that
16135          it doesn't want to clobber it.  We can only do this with PLUS.  */
16136       gcc_assert (code == PLUS);
16137       emit_insn (op);
16138     }
16139   else if (reload_completed
16140            && code == PLUS
16141            && !rtx_equal_p (dst, src1))
16142     {
16143       /* This is going to be an LEA; avoid splitting it later.  */
16144       emit_insn (op);
16145     }
16146   else
16147     {
16148       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16149       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
16150     }
16151
16152   /* Fix up the destination if needed.  */
16153   if (dst != operands[0])
16154     emit_move_insn (operands[0], dst);
16155 }
16156
16157 /* Return TRUE or FALSE depending on whether the binary operator meets the
16158    appropriate constraints.  */
16159
16160 bool
16161 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
16162                          rtx operands[3])
16163 {
16164   rtx dst = operands[0];
16165   rtx src1 = operands[1];
16166   rtx src2 = operands[2];
16167
16168   /* Both source operands cannot be in memory.  */
16169   if (MEM_P (src1) && MEM_P (src2))
16170     return false;
16171
16172   /* Canonicalize operand order for commutative operators.  */
16173   if (ix86_swap_binary_operands_p (code, mode, operands))
16174     {
16175       rtx temp = src1;
16176       src1 = src2;
16177       src2 = temp;
16178     }
16179
16180   /* If the destination is memory, we must have a matching source operand.  */
16181   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
16182       return false;
16183
16184   /* Source 1 cannot be a constant.  */
16185   if (CONSTANT_P (src1))
16186     return false;
16187
16188   /* Source 1 cannot be a non-matching memory.  */
16189   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
16190     /* Support "andhi/andsi/anddi" as a zero-extending move.  */
16191     return (code == AND
16192             && (mode == HImode
16193                 || mode == SImode
16194                 || (TARGET_64BIT && mode == DImode))
16195             && satisfies_constraint_L (src2));
16196
16197   return true;
16198 }
16199
16200 /* Attempt to expand a unary operator.  Make the expansion closer to the
16201    actual machine, then just general_operand, which will allow 2 separate
16202    memory references (one output, one input) in a single insn.  */
16203
16204 void
16205 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
16206                             rtx operands[])
16207 {
16208   int matching_memory;
16209   rtx src, dst, op, clob;
16210
16211   dst = operands[0];
16212   src = operands[1];
16213
16214   /* If the destination is memory, and we do not have matching source
16215      operands, do things in registers.  */
16216   matching_memory = 0;
16217   if (MEM_P (dst))
16218     {
16219       if (rtx_equal_p (dst, src))
16220         matching_memory = 1;
16221       else
16222         dst = gen_reg_rtx (mode);
16223     }
16224
16225   /* When source operand is memory, destination must match.  */
16226   if (MEM_P (src) && !matching_memory)
16227     src = force_reg (mode, src);
16228
16229   /* Emit the instruction.  */
16230
16231   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
16232   if (reload_in_progress || code == NOT)
16233     {
16234       /* Reload doesn't know about the flags register, and doesn't know that
16235          it doesn't want to clobber it.  */
16236       gcc_assert (code == NOT);
16237       emit_insn (op);
16238     }
16239   else
16240     {
16241       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16242       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
16243     }
16244
16245   /* Fix up the destination if needed.  */
16246   if (dst != operands[0])
16247     emit_move_insn (operands[0], dst);
16248 }
16249
16250 /* Split 32bit/64bit divmod with 8bit unsigned divmod if dividend and
16251    divisor are within the range [0-255].  */
16252
16253 void
16254 ix86_split_idivmod (enum machine_mode mode, rtx operands[],
16255                     bool signed_p)
16256 {
16257   rtx end_label, qimode_label;
16258   rtx insn, div, mod;
16259   rtx scratch, tmp0, tmp1, tmp2;
16260   rtx (*gen_divmod4_1) (rtx, rtx, rtx, rtx);
16261   rtx (*gen_zero_extend) (rtx, rtx);
16262   rtx (*gen_test_ccno_1) (rtx, rtx);
16263
16264   switch (mode)
16265     {
16266     case SImode:
16267       gen_divmod4_1 = signed_p ? gen_divmodsi4_1 : gen_udivmodsi4_1;
16268       gen_test_ccno_1 = gen_testsi_ccno_1;
16269       gen_zero_extend = gen_zero_extendqisi2;
16270       break;
16271     case DImode:
16272       gen_divmod4_1 = signed_p ? gen_divmoddi4_1 : gen_udivmoddi4_1;
16273       gen_test_ccno_1 = gen_testdi_ccno_1;
16274       gen_zero_extend = gen_zero_extendqidi2;
16275       break;
16276     default:
16277       gcc_unreachable ();
16278     }
16279
16280   end_label = gen_label_rtx ();
16281   qimode_label = gen_label_rtx ();
16282
16283   scratch = gen_reg_rtx (mode);
16284
16285   /* Use 8bit unsigned divimod if dividend and divisor are within
16286      the range [0-255].  */
16287   emit_move_insn (scratch, operands[2]);
16288   scratch = expand_simple_binop (mode, IOR, scratch, operands[3],
16289                                  scratch, 1, OPTAB_DIRECT);
16290   emit_insn (gen_test_ccno_1 (scratch, GEN_INT (-0x100)));
16291   tmp0 = gen_rtx_REG (CCNOmode, FLAGS_REG);
16292   tmp0 = gen_rtx_EQ (VOIDmode, tmp0, const0_rtx);
16293   tmp0 = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp0,
16294                                gen_rtx_LABEL_REF (VOIDmode, qimode_label),
16295                                pc_rtx);
16296   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp0));
16297   predict_jump (REG_BR_PROB_BASE * 50 / 100);
16298   JUMP_LABEL (insn) = qimode_label;
16299
16300   /* Generate original signed/unsigned divimod.  */
16301   div = gen_divmod4_1 (operands[0], operands[1],
16302                        operands[2], operands[3]);
16303   emit_insn (div);
16304
16305   /* Branch to the end.  */
16306   emit_jump_insn (gen_jump (end_label));
16307   emit_barrier ();
16308
16309   /* Generate 8bit unsigned divide.  */
16310   emit_label (qimode_label);
16311   /* Don't use operands[0] for result of 8bit divide since not all
16312      registers support QImode ZERO_EXTRACT.  */
16313   tmp0 = simplify_gen_subreg (HImode, scratch, mode, 0);
16314   tmp1 = simplify_gen_subreg (HImode, operands[2], mode, 0);
16315   tmp2 = simplify_gen_subreg (QImode, operands[3], mode, 0);
16316   emit_insn (gen_udivmodhiqi3 (tmp0, tmp1, tmp2));
16317
16318   if (signed_p)
16319     {
16320       div = gen_rtx_DIV (SImode, operands[2], operands[3]);
16321       mod = gen_rtx_MOD (SImode, operands[2], operands[3]);
16322     }
16323   else
16324     {
16325       div = gen_rtx_UDIV (SImode, operands[2], operands[3]);
16326       mod = gen_rtx_UMOD (SImode, operands[2], operands[3]);
16327     }
16328
16329   /* Extract remainder from AH.  */
16330   tmp1 = gen_rtx_ZERO_EXTRACT (mode, tmp0, GEN_INT (8), GEN_INT (8));
16331   if (REG_P (operands[1]))
16332     insn = emit_move_insn (operands[1], tmp1);
16333   else
16334     {
16335       /* Need a new scratch register since the old one has result
16336          of 8bit divide.  */
16337       scratch = gen_reg_rtx (mode);
16338       emit_move_insn (scratch, tmp1);
16339       insn = emit_move_insn (operands[1], scratch);
16340     }
16341   set_unique_reg_note (insn, REG_EQUAL, mod);
16342
16343   /* Zero extend quotient from AL.  */
16344   tmp1 = gen_lowpart (QImode, tmp0);
16345   insn = emit_insn (gen_zero_extend (operands[0], tmp1));
16346   set_unique_reg_note (insn, REG_EQUAL, div);
16347
16348   emit_label (end_label);
16349 }
16350
16351 #define LEA_MAX_STALL (3)
16352 #define LEA_SEARCH_THRESHOLD (LEA_MAX_STALL << 1)
16353
16354 /* Increase given DISTANCE in half-cycles according to
16355    dependencies between PREV and NEXT instructions.
16356    Add 1 half-cycle if there is no dependency and
16357    go to next cycle if there is some dependecy.  */
16358
16359 static unsigned int
16360 increase_distance (rtx prev, rtx next, unsigned int distance)
16361 {
16362   df_ref *use_rec;
16363   df_ref *def_rec;
16364
16365   if (!prev || !next)
16366     return distance + (distance & 1) + 2;
16367
16368   if (!DF_INSN_USES (next) || !DF_INSN_DEFS (prev))
16369     return distance + 1;
16370
16371   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
16372     for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
16373       if (!DF_REF_IS_ARTIFICIAL (*def_rec)
16374           && DF_REF_REGNO (*use_rec) == DF_REF_REGNO (*def_rec))
16375         return distance + (distance & 1) + 2;
16376
16377   return distance + 1;
16378 }
16379
16380 /* Function checks if instruction INSN defines register number
16381    REGNO1 or REGNO2.  */
16382
16383 static bool
16384 insn_defines_reg (unsigned int regno1, unsigned int regno2,
16385                   rtx insn)
16386 {
16387   df_ref *def_rec;
16388
16389   for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
16390     if (DF_REF_REG_DEF_P (*def_rec)
16391         && !DF_REF_IS_ARTIFICIAL (*def_rec)
16392         && (regno1 == DF_REF_REGNO (*def_rec)
16393             || regno2 == DF_REF_REGNO (*def_rec)))
16394       {
16395         return true;
16396       }
16397
16398   return false;
16399 }
16400
16401 /* Function checks if instruction INSN uses register number
16402    REGNO as a part of address expression.  */
16403
16404 static bool
16405 insn_uses_reg_mem (unsigned int regno, rtx insn)
16406 {
16407   df_ref *use_rec;
16408
16409   for (use_rec = DF_INSN_USES (insn); *use_rec; use_rec++)
16410     if (DF_REF_REG_MEM_P (*use_rec) && regno == DF_REF_REGNO (*use_rec))
16411       return true;
16412
16413   return false;
16414 }
16415
16416 /* Search backward for non-agu definition of register number REGNO1
16417    or register number REGNO2 in basic block starting from instruction
16418    START up to head of basic block or instruction INSN.
16419
16420    Function puts true value into *FOUND var if definition was found
16421    and false otherwise.
16422
16423    Distance in half-cycles between START and found instruction or head
16424    of BB is added to DISTANCE and returned.  */
16425
16426 static int
16427 distance_non_agu_define_in_bb (unsigned int regno1, unsigned int regno2,
16428                                rtx insn, int distance,
16429                                rtx start, bool *found)
16430 {
16431   basic_block bb = start ? BLOCK_FOR_INSN (start) : NULL;
16432   rtx prev = start;
16433   rtx next = NULL;
16434
16435   *found = false;
16436
16437   while (prev
16438          && prev != insn
16439          && distance < LEA_SEARCH_THRESHOLD)
16440     {
16441       if (NONDEBUG_INSN_P (prev) && NONJUMP_INSN_P (prev))
16442         {
16443           distance = increase_distance (prev, next, distance);
16444           if (insn_defines_reg (regno1, regno2, prev))
16445             {
16446               if (recog_memoized (prev) < 0
16447                   || get_attr_type (prev) != TYPE_LEA)
16448                 {
16449                   *found = true;
16450                   return distance;
16451                 }
16452             }
16453
16454           next = prev;
16455         }
16456       if (prev == BB_HEAD (bb))
16457         break;
16458
16459       prev = PREV_INSN (prev);
16460     }
16461
16462   return distance;
16463 }
16464
16465 /* Search backward for non-agu definition of register number REGNO1
16466    or register number REGNO2 in INSN's basic block until
16467    1. Pass LEA_SEARCH_THRESHOLD instructions, or
16468    2. Reach neighbour BBs boundary, or
16469    3. Reach agu definition.
16470    Returns the distance between the non-agu definition point and INSN.
16471    If no definition point, returns -1.  */
16472
16473 static int
16474 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
16475                          rtx insn)
16476 {
16477   basic_block bb = BLOCK_FOR_INSN (insn);
16478   int distance = 0;
16479   bool found = false;
16480
16481   if (insn != BB_HEAD (bb))
16482     distance = distance_non_agu_define_in_bb (regno1, regno2, insn,
16483                                               distance, PREV_INSN (insn),
16484                                               &found);
16485
16486   if (!found && distance < LEA_SEARCH_THRESHOLD)
16487     {
16488       edge e;
16489       edge_iterator ei;
16490       bool simple_loop = false;
16491
16492       FOR_EACH_EDGE (e, ei, bb->preds)
16493         if (e->src == bb)
16494           {
16495             simple_loop = true;
16496             break;
16497           }
16498
16499       if (simple_loop)
16500         distance = distance_non_agu_define_in_bb (regno1, regno2,
16501                                                   insn, distance,
16502                                                   BB_END (bb), &found);
16503       else
16504         {
16505           int shortest_dist = -1;
16506           bool found_in_bb = false;
16507
16508           FOR_EACH_EDGE (e, ei, bb->preds)
16509             {
16510               int bb_dist
16511                 = distance_non_agu_define_in_bb (regno1, regno2,
16512                                                  insn, distance,
16513                                                  BB_END (e->src),
16514                                                  &found_in_bb);
16515               if (found_in_bb)
16516                 {
16517                   if (shortest_dist < 0)
16518                     shortest_dist = bb_dist;
16519                   else if (bb_dist > 0)
16520                     shortest_dist = MIN (bb_dist, shortest_dist);
16521
16522                   found = true;
16523                 }
16524             }
16525
16526           distance = shortest_dist;
16527         }
16528     }
16529
16530   /* get_attr_type may modify recog data.  We want to make sure
16531      that recog data is valid for instruction INSN, on which
16532      distance_non_agu_define is called.  INSN is unchanged here.  */
16533   extract_insn_cached (insn);
16534
16535   if (!found)
16536     return -1;
16537
16538   return distance >> 1;
16539 }
16540
16541 /* Return the distance in half-cycles between INSN and the next
16542    insn that uses register number REGNO in memory address added
16543    to DISTANCE.  Return -1 if REGNO0 is set.
16544
16545    Put true value into *FOUND if register usage was found and
16546    false otherwise.
16547    Put true value into *REDEFINED if register redefinition was
16548    found and false otherwise.  */
16549
16550 static int
16551 distance_agu_use_in_bb (unsigned int regno,
16552                         rtx insn, int distance, rtx start,
16553                         bool *found, bool *redefined)
16554 {
16555   basic_block bb = start ? BLOCK_FOR_INSN (start) : NULL;
16556   rtx next = start;
16557   rtx prev = NULL;
16558
16559   *found = false;
16560   *redefined = false;
16561
16562   while (next
16563          && next != insn
16564          && distance < LEA_SEARCH_THRESHOLD)
16565     {
16566       if (NONDEBUG_INSN_P (next) && NONJUMP_INSN_P (next))
16567         {
16568           distance = increase_distance(prev, next, distance);
16569           if (insn_uses_reg_mem (regno, next))
16570             {
16571               /* Return DISTANCE if OP0 is used in memory
16572                  address in NEXT.  */
16573               *found = true;
16574               return distance;
16575             }
16576
16577           if (insn_defines_reg (regno, INVALID_REGNUM, next))
16578             {
16579               /* Return -1 if OP0 is set in NEXT.  */
16580               *redefined = true;
16581               return -1;
16582             }
16583
16584           prev = next;
16585         }
16586
16587       if (next == BB_END (bb))
16588         break;
16589
16590       next = NEXT_INSN (next);
16591     }
16592
16593   return distance;
16594 }
16595
16596 /* Return the distance between INSN and the next insn that uses
16597    register number REGNO0 in memory address.  Return -1 if no such
16598    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
16599
16600 static int
16601 distance_agu_use (unsigned int regno0, rtx insn)
16602 {
16603   basic_block bb = BLOCK_FOR_INSN (insn);
16604   int distance = 0;
16605   bool found = false;
16606   bool redefined = false;
16607
16608   if (insn != BB_END (bb))
16609     distance = distance_agu_use_in_bb (regno0, insn, distance,
16610                                        NEXT_INSN (insn),
16611                                        &found, &redefined);
16612
16613   if (!found && !redefined && distance < LEA_SEARCH_THRESHOLD)
16614     {
16615       edge e;
16616       edge_iterator ei;
16617       bool simple_loop = false;
16618
16619       FOR_EACH_EDGE (e, ei, bb->succs)
16620         if (e->dest == bb)
16621           {
16622             simple_loop = true;
16623             break;
16624           }
16625
16626       if (simple_loop)
16627         distance = distance_agu_use_in_bb (regno0, insn,
16628                                            distance, BB_HEAD (bb),
16629                                            &found, &redefined);
16630       else
16631         {
16632           int shortest_dist = -1;
16633           bool found_in_bb = false;
16634           bool redefined_in_bb = false;
16635
16636           FOR_EACH_EDGE (e, ei, bb->succs)
16637             {
16638               int bb_dist
16639                 = distance_agu_use_in_bb (regno0, insn,
16640                                           distance, BB_HEAD (e->dest),
16641                                           &found_in_bb, &redefined_in_bb);
16642               if (found_in_bb)
16643                 {
16644                   if (shortest_dist < 0)
16645                     shortest_dist = bb_dist;
16646                   else if (bb_dist > 0)
16647                     shortest_dist = MIN (bb_dist, shortest_dist);
16648
16649                   found = true;
16650                 }
16651             }
16652
16653           distance = shortest_dist;
16654         }
16655     }
16656
16657   if (!found || redefined)
16658     return -1;
16659
16660   return distance >> 1;
16661 }
16662
16663 /* Define this macro to tune LEA priority vs ADD, it take effect when
16664    there is a dilemma of choicing LEA or ADD
16665    Negative value: ADD is more preferred than LEA
16666    Zero: Netrual
16667    Positive value: LEA is more preferred than ADD*/
16668 #define IX86_LEA_PRIORITY 0
16669
16670 /* Return true if usage of lea INSN has performance advantage
16671    over a sequence of instructions.  Instructions sequence has
16672    SPLIT_COST cycles higher latency than lea latency.  */
16673
16674 bool
16675 ix86_lea_outperforms (rtx insn, unsigned int regno0, unsigned int regno1,
16676                       unsigned int regno2, unsigned int split_cost)
16677 {
16678   int dist_define, dist_use;
16679
16680   dist_define = distance_non_agu_define (regno1, regno2, insn);
16681   dist_use = distance_agu_use (regno0, insn);
16682
16683   if (dist_define < 0 || dist_define >= LEA_MAX_STALL)
16684     {
16685       /* If there is no non AGU operand definition, no AGU
16686          operand usage and split cost is 0 then both lea
16687          and non lea variants have same priority.  Currently
16688          we prefer lea for 64 bit code and non lea on 32 bit
16689          code.  */
16690       if (dist_use < 0 && split_cost == 0)
16691         return TARGET_64BIT || IX86_LEA_PRIORITY;
16692       else
16693         return true;
16694     }
16695
16696   /* With longer definitions distance lea is more preferable.
16697      Here we change it to take into account splitting cost and
16698      lea priority.  */
16699   dist_define += split_cost + IX86_LEA_PRIORITY;
16700
16701   /* If there is no use in memory addess then we just check
16702      that split cost does not exceed AGU stall.  */
16703   if (dist_use < 0)
16704     return dist_define >= LEA_MAX_STALL;
16705
16706   /* If this insn has both backward non-agu dependence and forward
16707      agu dependence, the one with short distance takes effect.  */
16708   return dist_define >= dist_use;
16709 }
16710
16711 /* Return true if it is legal to clobber flags by INSN and
16712    false otherwise.  */
16713
16714 static bool
16715 ix86_ok_to_clobber_flags (rtx insn)
16716 {
16717   basic_block bb = BLOCK_FOR_INSN (insn);
16718   df_ref *use;
16719   bitmap live;
16720
16721   while (insn)
16722     {
16723       if (NONDEBUG_INSN_P (insn))
16724         {
16725           for (use = DF_INSN_USES (insn); *use; use++)
16726             if (DF_REF_REG_USE_P (*use) && DF_REF_REGNO (*use) == FLAGS_REG)
16727               return false;
16728
16729           if (insn_defines_reg (FLAGS_REG, INVALID_REGNUM, insn))
16730             return true;
16731         }
16732
16733       if (insn == BB_END (bb))
16734         break;
16735
16736       insn = NEXT_INSN (insn);
16737     }
16738
16739   live = df_get_live_out(bb);
16740   return !REGNO_REG_SET_P (live, FLAGS_REG);
16741 }
16742
16743 /* Return true if we need to split op0 = op1 + op2 into a sequence of
16744    move and add to avoid AGU stalls.  */
16745
16746 bool
16747 ix86_avoid_lea_for_add (rtx insn, rtx operands[])
16748 {
16749   unsigned int regno0 = true_regnum (operands[0]);
16750   unsigned int regno1 = true_regnum (operands[1]);
16751   unsigned int regno2 = true_regnum (operands[2]);
16752
16753   /* Check if we need to optimize.  */
16754   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16755     return false;
16756
16757   /* Check it is correct to split here.  */
16758   if (!ix86_ok_to_clobber_flags(insn))
16759     return false;
16760
16761   /* We need to split only adds with non destructive
16762      destination operand.  */
16763   if (regno0 == regno1 || regno0 == regno2)
16764     return false;
16765   else
16766     return !ix86_lea_outperforms (insn, regno0, regno1, regno2, 1);
16767 }
16768
16769 /* Return true if we should emit lea instruction instead of mov
16770    instruction.  */
16771
16772 bool
16773 ix86_use_lea_for_mov (rtx insn, rtx operands[])
16774 {
16775   unsigned int regno0;
16776   unsigned int regno1;
16777
16778   /* Check if we need to optimize.  */
16779   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16780     return false;
16781
16782   /* Use lea for reg to reg moves only.  */
16783   if (!REG_P (operands[0]) || !REG_P (operands[1]))
16784     return false;
16785
16786   regno0 = true_regnum (operands[0]);
16787   regno1 = true_regnum (operands[1]);
16788
16789   return ix86_lea_outperforms (insn, regno0, regno1, -1, 0);
16790 }
16791
16792 /* Return true if we need to split lea into a sequence of
16793    instructions to avoid AGU stalls. */
16794
16795 bool
16796 ix86_avoid_lea_for_addr (rtx insn, rtx operands[])
16797 {
16798   unsigned int regno0 = true_regnum (operands[0]) ;
16799   unsigned int regno1 = -1;
16800   unsigned int regno2 = -1;
16801   unsigned int split_cost = 0;
16802   struct ix86_address parts;
16803   int ok;
16804
16805   /* Check we need to optimize.  */
16806   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16807     return false;
16808
16809   /* Check it is correct to split here.  */
16810   if (!ix86_ok_to_clobber_flags(insn))
16811     return false;
16812
16813   ok = ix86_decompose_address (operands[1], &parts);
16814   gcc_assert (ok);
16815
16816   /* We should not split into add if non legitimate pic
16817      operand is used as displacement. */
16818   if (parts.disp && flag_pic && !LEGITIMATE_PIC_OPERAND_P (parts.disp))
16819     return false;
16820
16821   if (parts.base)
16822     regno1 = true_regnum (parts.base);
16823   if (parts.index)
16824     regno2 = true_regnum (parts.index);
16825
16826   /* Compute how many cycles we will add to execution time
16827      if split lea into a sequence of instructions.  */
16828   if (parts.base || parts.index)
16829     {
16830       /* Have to use mov instruction if non desctructive
16831          destination form is used.  */
16832       if (regno1 != regno0 && regno2 != regno0)
16833         split_cost += 1;
16834
16835       /* Have to add index to base if both exist.  */
16836       if (parts.base && parts.index)
16837         split_cost += 1;
16838
16839       /* Have to use shift and adds if scale is 2 or greater.  */
16840       if (parts.scale > 1)
16841         {
16842           if (regno0 != regno1)
16843             split_cost += 1;
16844           else if (regno2 == regno0)
16845             split_cost += 4;
16846           else
16847             split_cost += parts.scale;
16848         }
16849
16850       /* Have to use add instruction with immediate if
16851          disp is non zero.  */
16852       if (parts.disp && parts.disp != const0_rtx)
16853         split_cost += 1;
16854
16855       /* Subtract the price of lea.  */
16856       split_cost -= 1;
16857     }
16858
16859   return !ix86_lea_outperforms (insn, regno0, regno1, regno2, split_cost);
16860 }
16861
16862 /* Emit x86 binary operand CODE in mode MODE, where the first operand
16863    matches destination.  RTX includes clobber of FLAGS_REG.  */
16864
16865 static void
16866 ix86_emit_binop (enum rtx_code code, enum machine_mode mode,
16867                  rtx dst, rtx src)
16868 {
16869   rtx op, clob;
16870
16871   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, dst, src));
16872   clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16873   
16874   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
16875 }
16876
16877 /* Split lea instructions into a sequence of instructions
16878    which are executed on ALU to avoid AGU stalls.
16879    It is assumed that it is allowed to clobber flags register
16880    at lea position.  */
16881
16882 extern void
16883 ix86_split_lea_for_addr (rtx operands[], enum machine_mode mode)
16884 {
16885   unsigned int regno0 = true_regnum (operands[0]) ;
16886   unsigned int regno1 = INVALID_REGNUM;
16887   unsigned int regno2 = INVALID_REGNUM;
16888   struct ix86_address parts;
16889   rtx tmp;
16890   int ok, adds;
16891
16892   ok = ix86_decompose_address (operands[1], &parts);
16893   gcc_assert (ok);
16894
16895   if (parts.base)
16896     {
16897       if (GET_MODE (parts.base) != mode)
16898         parts.base = gen_rtx_SUBREG (mode, parts.base, 0);
16899       regno1 = true_regnum (parts.base);
16900     }
16901
16902   if (parts.index)
16903     {
16904       if (GET_MODE (parts.index) != mode)
16905         parts.index = gen_rtx_SUBREG (mode, parts.index, 0);
16906       regno2 = true_regnum (parts.index);
16907     }
16908
16909   if (parts.scale > 1)
16910     {
16911       /* Case r1 = r1 + ...  */
16912       if (regno1 == regno0)
16913         {
16914           /* If we have a case r1 = r1 + C * r1 then we
16915              should use multiplication which is very
16916              expensive.  Assume cost model is wrong if we
16917              have such case here.  */
16918           gcc_assert (regno2 != regno0);
16919
16920           for (adds = parts.scale; adds > 0; adds--)
16921             ix86_emit_binop (PLUS, mode, operands[0], parts.index);
16922         }
16923       else
16924         {
16925           /* r1 = r2 + r3 * C case.  Need to move r3 into r1.  */
16926           if (regno0 != regno2)
16927             emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.index));
16928
16929           /* Use shift for scaling.  */
16930           ix86_emit_binop (ASHIFT, mode, operands[0],
16931                            GEN_INT (exact_log2 (parts.scale)));
16932
16933           if (parts.base)
16934             ix86_emit_binop (PLUS, mode, operands[0], parts.base);
16935
16936           if (parts.disp && parts.disp != const0_rtx)
16937             ix86_emit_binop (PLUS, mode, operands[0], parts.disp);
16938         }
16939     }
16940   else if (!parts.base && !parts.index)
16941     {
16942       gcc_assert(parts.disp);
16943       emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.disp));
16944     }
16945   else
16946     {
16947       if (!parts.base)
16948         {
16949           if (regno0 != regno2)
16950             emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.index));
16951         }
16952       else if (!parts.index)
16953         {
16954           if (regno0 != regno1)
16955             emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.base));
16956         }
16957       else
16958         {
16959           if (regno0 == regno1)
16960             tmp = parts.index;
16961           else if (regno0 == regno2)
16962             tmp = parts.base;
16963           else
16964             {
16965               emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.base));
16966               tmp = parts.index;
16967             }
16968
16969           ix86_emit_binop (PLUS, mode, operands[0], tmp);
16970         }
16971
16972       if (parts.disp && parts.disp != const0_rtx)
16973         ix86_emit_binop (PLUS, mode, operands[0], parts.disp);
16974     }
16975 }
16976
16977 /* Return true if it is ok to optimize an ADD operation to LEA
16978    operation to avoid flag register consumation.  For most processors,
16979    ADD is faster than LEA.  For the processors like ATOM, if the
16980    destination register of LEA holds an actual address which will be
16981    used soon, LEA is better and otherwise ADD is better.  */
16982
16983 bool
16984 ix86_lea_for_add_ok (rtx insn, rtx operands[])
16985 {
16986   unsigned int regno0 = true_regnum (operands[0]);
16987   unsigned int regno1 = true_regnum (operands[1]);
16988   unsigned int regno2 = true_regnum (operands[2]);
16989
16990   /* If a = b + c, (a!=b && a!=c), must use lea form. */
16991   if (regno0 != regno1 && regno0 != regno2)
16992     return true;
16993
16994   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16995     return false;
16996
16997   return ix86_lea_outperforms (insn, regno0, regno1, regno2, 0);
16998 }
16999
17000 /* Return true if destination reg of SET_BODY is shift count of
17001    USE_BODY.  */
17002
17003 static bool
17004 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
17005 {
17006   rtx set_dest;
17007   rtx shift_rtx;
17008   int i;
17009
17010   /* Retrieve destination of SET_BODY.  */
17011   switch (GET_CODE (set_body))
17012     {
17013     case SET:
17014       set_dest = SET_DEST (set_body);
17015       if (!set_dest || !REG_P (set_dest))
17016         return false;
17017       break;
17018     case PARALLEL:
17019       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
17020         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
17021                                           use_body))
17022           return true;
17023     default:
17024       return false;
17025       break;
17026     }
17027
17028   /* Retrieve shift count of USE_BODY.  */
17029   switch (GET_CODE (use_body))
17030     {
17031     case SET:
17032       shift_rtx = XEXP (use_body, 1);
17033       break;
17034     case PARALLEL:
17035       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
17036         if (ix86_dep_by_shift_count_body (set_body,
17037                                           XVECEXP (use_body, 0, i)))
17038           return true;
17039     default:
17040       return false;
17041       break;
17042     }
17043
17044   if (shift_rtx
17045       && (GET_CODE (shift_rtx) == ASHIFT
17046           || GET_CODE (shift_rtx) == LSHIFTRT
17047           || GET_CODE (shift_rtx) == ASHIFTRT
17048           || GET_CODE (shift_rtx) == ROTATE
17049           || GET_CODE (shift_rtx) == ROTATERT))
17050     {
17051       rtx shift_count = XEXP (shift_rtx, 1);
17052
17053       /* Return true if shift count is dest of SET_BODY.  */
17054       if (REG_P (shift_count)
17055           && true_regnum (set_dest) == true_regnum (shift_count))
17056         return true;
17057     }
17058
17059   return false;
17060 }
17061
17062 /* Return true if destination reg of SET_INSN is shift count of
17063    USE_INSN.  */
17064
17065 bool
17066 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
17067 {
17068   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
17069                                        PATTERN (use_insn));
17070 }
17071
17072 /* Return TRUE or FALSE depending on whether the unary operator meets the
17073    appropriate constraints.  */
17074
17075 bool
17076 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
17077                         enum machine_mode mode ATTRIBUTE_UNUSED,
17078                         rtx operands[2] ATTRIBUTE_UNUSED)
17079 {
17080   /* If one of operands is memory, source and destination must match.  */
17081   if ((MEM_P (operands[0])
17082        || MEM_P (operands[1]))
17083       && ! rtx_equal_p (operands[0], operands[1]))
17084     return false;
17085   return true;
17086 }
17087
17088 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
17089    are ok, keeping in mind the possible movddup alternative.  */
17090
17091 bool
17092 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
17093 {
17094   if (MEM_P (operands[0]))
17095     return rtx_equal_p (operands[0], operands[1 + high]);
17096   if (MEM_P (operands[1]) && MEM_P (operands[2]))
17097     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
17098   return true;
17099 }
17100
17101 /* Post-reload splitter for converting an SF or DFmode value in an
17102    SSE register into an unsigned SImode.  */
17103
17104 void
17105 ix86_split_convert_uns_si_sse (rtx operands[])
17106 {
17107   enum machine_mode vecmode;
17108   rtx value, large, zero_or_two31, input, two31, x;
17109
17110   large = operands[1];
17111   zero_or_two31 = operands[2];
17112   input = operands[3];
17113   two31 = operands[4];
17114   vecmode = GET_MODE (large);
17115   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
17116
17117   /* Load up the value into the low element.  We must ensure that the other
17118      elements are valid floats -- zero is the easiest such value.  */
17119   if (MEM_P (input))
17120     {
17121       if (vecmode == V4SFmode)
17122         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
17123       else
17124         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
17125     }
17126   else
17127     {
17128       input = gen_rtx_REG (vecmode, REGNO (input));
17129       emit_move_insn (value, CONST0_RTX (vecmode));
17130       if (vecmode == V4SFmode)
17131         emit_insn (gen_sse_movss (value, value, input));
17132       else
17133         emit_insn (gen_sse2_movsd (value, value, input));
17134     }
17135
17136   emit_move_insn (large, two31);
17137   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
17138
17139   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
17140   emit_insn (gen_rtx_SET (VOIDmode, large, x));
17141
17142   x = gen_rtx_AND (vecmode, zero_or_two31, large);
17143   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
17144
17145   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
17146   emit_insn (gen_rtx_SET (VOIDmode, value, x));
17147
17148   large = gen_rtx_REG (V4SImode, REGNO (large));
17149   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
17150
17151   x = gen_rtx_REG (V4SImode, REGNO (value));
17152   if (vecmode == V4SFmode)
17153     emit_insn (gen_fix_truncv4sfv4si2 (x, value));
17154   else
17155     emit_insn (gen_sse2_cvttpd2dq (x, value));
17156   value = x;
17157
17158   emit_insn (gen_xorv4si3 (value, value, large));
17159 }
17160
17161 /* Convert an unsigned DImode value into a DFmode, using only SSE.
17162    Expects the 64-bit DImode to be supplied in a pair of integral
17163    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
17164    -mfpmath=sse, !optimize_size only.  */
17165
17166 void
17167 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
17168 {
17169   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
17170   rtx int_xmm, fp_xmm;
17171   rtx biases, exponents;
17172   rtx x;
17173
17174   int_xmm = gen_reg_rtx (V4SImode);
17175   if (TARGET_INTER_UNIT_MOVES)
17176     emit_insn (gen_movdi_to_sse (int_xmm, input));
17177   else if (TARGET_SSE_SPLIT_REGS)
17178     {
17179       emit_clobber (int_xmm);
17180       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
17181     }
17182   else
17183     {
17184       x = gen_reg_rtx (V2DImode);
17185       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
17186       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
17187     }
17188
17189   x = gen_rtx_CONST_VECTOR (V4SImode,
17190                             gen_rtvec (4, GEN_INT (0x43300000UL),
17191                                        GEN_INT (0x45300000UL),
17192                                        const0_rtx, const0_rtx));
17193   exponents = validize_mem (force_const_mem (V4SImode, x));
17194
17195   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
17196   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
17197
17198   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
17199      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
17200      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
17201      (0x1.0p84 + double(fp_value_hi_xmm)).
17202      Note these exponents differ by 32.  */
17203
17204   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
17205
17206   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
17207      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
17208   real_ldexp (&bias_lo_rvt, &dconst1, 52);
17209   real_ldexp (&bias_hi_rvt, &dconst1, 84);
17210   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
17211   x = const_double_from_real_value (bias_hi_rvt, DFmode);
17212   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
17213   biases = validize_mem (force_const_mem (V2DFmode, biases));
17214   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
17215
17216   /* Add the upper and lower DFmode values together.  */
17217   if (TARGET_SSE3)
17218     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
17219   else
17220     {
17221       x = copy_to_mode_reg (V2DFmode, fp_xmm);
17222       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
17223       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
17224     }
17225
17226   ix86_expand_vector_extract (false, target, fp_xmm, 0);
17227 }
17228
17229 /* Not used, but eases macroization of patterns.  */
17230 void
17231 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
17232                                   rtx input ATTRIBUTE_UNUSED)
17233 {
17234   gcc_unreachable ();
17235 }
17236
17237 /* Convert an unsigned SImode value into a DFmode.  Only currently used
17238    for SSE, but applicable anywhere.  */
17239
17240 void
17241 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
17242 {
17243   REAL_VALUE_TYPE TWO31r;
17244   rtx x, fp;
17245
17246   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
17247                            NULL, 1, OPTAB_DIRECT);
17248
17249   fp = gen_reg_rtx (DFmode);
17250   emit_insn (gen_floatsidf2 (fp, x));
17251
17252   real_ldexp (&TWO31r, &dconst1, 31);
17253   x = const_double_from_real_value (TWO31r, DFmode);
17254
17255   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
17256   if (x != target)
17257     emit_move_insn (target, x);
17258 }
17259
17260 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
17261    32-bit mode; otherwise we have a direct convert instruction.  */
17262
17263 void
17264 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
17265 {
17266   REAL_VALUE_TYPE TWO32r;
17267   rtx fp_lo, fp_hi, x;
17268
17269   fp_lo = gen_reg_rtx (DFmode);
17270   fp_hi = gen_reg_rtx (DFmode);
17271
17272   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
17273
17274   real_ldexp (&TWO32r, &dconst1, 32);
17275   x = const_double_from_real_value (TWO32r, DFmode);
17276   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
17277
17278   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
17279
17280   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
17281                            0, OPTAB_DIRECT);
17282   if (x != target)
17283     emit_move_insn (target, x);
17284 }
17285
17286 /* Convert an unsigned SImode value into a SFmode, using only SSE.
17287    For x86_32, -mfpmath=sse, !optimize_size only.  */
17288 void
17289 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
17290 {
17291   REAL_VALUE_TYPE ONE16r;
17292   rtx fp_hi, fp_lo, int_hi, int_lo, x;
17293
17294   real_ldexp (&ONE16r, &dconst1, 16);
17295   x = const_double_from_real_value (ONE16r, SFmode);
17296   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
17297                                       NULL, 0, OPTAB_DIRECT);
17298   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
17299                                       NULL, 0, OPTAB_DIRECT);
17300   fp_hi = gen_reg_rtx (SFmode);
17301   fp_lo = gen_reg_rtx (SFmode);
17302   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
17303   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
17304   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
17305                                0, OPTAB_DIRECT);
17306   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
17307                                0, OPTAB_DIRECT);
17308   if (!rtx_equal_p (target, fp_hi))
17309     emit_move_insn (target, fp_hi);
17310 }
17311
17312 /* floatunsv{4,8}siv{4,8}sf2 expander.  Expand code to convert
17313    a vector of unsigned ints VAL to vector of floats TARGET.  */
17314
17315 void
17316 ix86_expand_vector_convert_uns_vsivsf (rtx target, rtx val)
17317 {
17318   rtx tmp[8];
17319   REAL_VALUE_TYPE TWO16r;
17320   enum machine_mode intmode = GET_MODE (val);
17321   enum machine_mode fltmode = GET_MODE (target);
17322   rtx (*cvt) (rtx, rtx);
17323
17324   if (intmode == V4SImode)
17325     cvt = gen_floatv4siv4sf2;
17326   else
17327     cvt = gen_floatv8siv8sf2;
17328   tmp[0] = ix86_build_const_vector (intmode, 1, GEN_INT (0xffff));
17329   tmp[0] = force_reg (intmode, tmp[0]);
17330   tmp[1] = expand_simple_binop (intmode, AND, val, tmp[0], NULL_RTX, 1,
17331                                 OPTAB_DIRECT);
17332   tmp[2] = expand_simple_binop (intmode, LSHIFTRT, val, GEN_INT (16),
17333                                 NULL_RTX, 1, OPTAB_DIRECT);
17334   tmp[3] = gen_reg_rtx (fltmode);
17335   emit_insn (cvt (tmp[3], tmp[1]));
17336   tmp[4] = gen_reg_rtx (fltmode);
17337   emit_insn (cvt (tmp[4], tmp[2]));
17338   real_ldexp (&TWO16r, &dconst1, 16);
17339   tmp[5] = const_double_from_real_value (TWO16r, SFmode);
17340   tmp[5] = force_reg (fltmode, ix86_build_const_vector (fltmode, 1, tmp[5]));
17341   tmp[6] = expand_simple_binop (fltmode, MULT, tmp[4], tmp[5], NULL_RTX, 1,
17342                                 OPTAB_DIRECT);
17343   tmp[7] = expand_simple_binop (fltmode, PLUS, tmp[3], tmp[6], target, 1,
17344                                 OPTAB_DIRECT);
17345   if (tmp[7] != target)
17346     emit_move_insn (target, tmp[7]);
17347 }
17348
17349 /* Adjust a V*SFmode/V*DFmode value VAL so that *sfix_trunc* resp. fix_trunc*
17350    pattern can be used on it instead of *ufix_trunc* resp. fixuns_trunc*.
17351    This is done by doing just signed conversion if < 0x1p31, and otherwise by
17352    subtracting 0x1p31 first and xoring in 0x80000000 from *XORP afterwards.  */
17353
17354 rtx
17355 ix86_expand_adjust_ufix_to_sfix_si (rtx val, rtx *xorp)
17356 {
17357   REAL_VALUE_TYPE TWO31r;
17358   rtx two31r, tmp[4];
17359   enum machine_mode mode = GET_MODE (val);
17360   enum machine_mode scalarmode = GET_MODE_INNER (mode);
17361   enum machine_mode intmode = GET_MODE_SIZE (mode) == 32 ? V8SImode : V4SImode;
17362   rtx (*cmp) (rtx, rtx, rtx, rtx);
17363   int i;
17364
17365   for (i = 0; i < 3; i++)
17366     tmp[i] = gen_reg_rtx (mode);
17367   real_ldexp (&TWO31r, &dconst1, 31);
17368   two31r = const_double_from_real_value (TWO31r, scalarmode);
17369   two31r = ix86_build_const_vector (mode, 1, two31r);
17370   two31r = force_reg (mode, two31r);
17371   switch (mode)
17372     {
17373     case V8SFmode: cmp = gen_avx_maskcmpv8sf3; break;
17374     case V4SFmode: cmp = gen_sse_maskcmpv4sf3; break;
17375     case V4DFmode: cmp = gen_avx_maskcmpv4df3; break;
17376     case V2DFmode: cmp = gen_sse2_maskcmpv2df3; break;
17377     default: gcc_unreachable ();
17378     }
17379   tmp[3] = gen_rtx_LE (mode, two31r, val);
17380   emit_insn (cmp (tmp[0], two31r, val, tmp[3]));
17381   tmp[1] = expand_simple_binop (mode, AND, tmp[0], two31r, tmp[1],
17382                                 0, OPTAB_DIRECT);
17383   if (intmode == V4SImode || TARGET_AVX2)
17384     *xorp = expand_simple_binop (intmode, ASHIFT,
17385                                  gen_lowpart (intmode, tmp[0]),
17386                                  GEN_INT (31), NULL_RTX, 0,
17387                                  OPTAB_DIRECT);
17388   else
17389     {
17390       rtx two31 = GEN_INT ((unsigned HOST_WIDE_INT) 1 << 31);
17391       two31 = ix86_build_const_vector (intmode, 1, two31);
17392       *xorp = expand_simple_binop (intmode, AND,
17393                                    gen_lowpart (intmode, tmp[0]),
17394                                    two31, NULL_RTX, 0,
17395                                    OPTAB_DIRECT);
17396     }
17397   return expand_simple_binop (mode, MINUS, val, tmp[1], tmp[2],
17398                               0, OPTAB_DIRECT);
17399 }
17400
17401 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
17402    then replicate the value for all elements of the vector
17403    register.  */
17404
17405 rtx
17406 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
17407 {
17408   int i, n_elt;
17409   rtvec v;
17410   enum machine_mode scalar_mode;
17411
17412   switch (mode)
17413     {
17414     case V32QImode:
17415     case V16QImode:
17416     case V16HImode:
17417     case V8HImode:
17418     case V8SImode:
17419     case V4SImode:
17420     case V4DImode:
17421     case V2DImode:
17422       gcc_assert (vect);
17423     case V8SFmode:
17424     case V4SFmode:
17425     case V4DFmode:
17426     case V2DFmode:
17427       n_elt = GET_MODE_NUNITS (mode);
17428       v = rtvec_alloc (n_elt);
17429       scalar_mode = GET_MODE_INNER (mode);
17430
17431       RTVEC_ELT (v, 0) = value;
17432
17433       for (i = 1; i < n_elt; ++i)
17434         RTVEC_ELT (v, i) = vect ? value : CONST0_RTX (scalar_mode);
17435
17436       return gen_rtx_CONST_VECTOR (mode, v);
17437
17438     default:
17439       gcc_unreachable ();
17440     }
17441 }
17442
17443 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
17444    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
17445    for an SSE register.  If VECT is true, then replicate the mask for
17446    all elements of the vector register.  If INVERT is true, then create
17447    a mask excluding the sign bit.  */
17448
17449 rtx
17450 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
17451 {
17452   enum machine_mode vec_mode, imode;
17453   HOST_WIDE_INT hi, lo;
17454   int shift = 63;
17455   rtx v;
17456   rtx mask;
17457
17458   /* Find the sign bit, sign extended to 2*HWI.  */
17459   switch (mode)
17460     {
17461     case V8SImode:
17462     case V4SImode:
17463     case V8SFmode:
17464     case V4SFmode:
17465       vec_mode = mode;
17466       mode = GET_MODE_INNER (mode);
17467       imode = SImode;
17468       lo = 0x80000000, hi = lo < 0;
17469       break;
17470
17471     case V4DImode:
17472     case V2DImode:
17473     case V4DFmode:
17474     case V2DFmode:
17475       vec_mode = mode;
17476       mode = GET_MODE_INNER (mode);
17477       imode = DImode;
17478       if (HOST_BITS_PER_WIDE_INT >= 64)
17479         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
17480       else
17481         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
17482       break;
17483
17484     case TImode:
17485     case TFmode:
17486       vec_mode = VOIDmode;
17487       if (HOST_BITS_PER_WIDE_INT >= 64)
17488         {
17489           imode = TImode;
17490           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
17491         }
17492       else
17493         {
17494           rtvec vec;
17495
17496           imode = DImode;
17497           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
17498
17499           if (invert)
17500             {
17501               lo = ~lo, hi = ~hi;
17502               v = constm1_rtx;
17503             }
17504           else
17505             v = const0_rtx;
17506
17507           mask = immed_double_const (lo, hi, imode);
17508
17509           vec = gen_rtvec (2, v, mask);
17510           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
17511           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
17512
17513           return v;
17514         }
17515      break;
17516
17517     default:
17518       gcc_unreachable ();
17519     }
17520
17521   if (invert)
17522     lo = ~lo, hi = ~hi;
17523
17524   /* Force this value into the low part of a fp vector constant.  */
17525   mask = immed_double_const (lo, hi, imode);
17526   mask = gen_lowpart (mode, mask);
17527
17528   if (vec_mode == VOIDmode)
17529     return force_reg (mode, mask);
17530
17531   v = ix86_build_const_vector (vec_mode, vect, mask);
17532   return force_reg (vec_mode, v);
17533 }
17534
17535 /* Generate code for floating point ABS or NEG.  */
17536
17537 void
17538 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
17539                                 rtx operands[])
17540 {
17541   rtx mask, set, dst, src;
17542   bool use_sse = false;
17543   bool vector_mode = VECTOR_MODE_P (mode);
17544   enum machine_mode vmode = mode;
17545
17546   if (vector_mode)
17547     use_sse = true;
17548   else if (mode == TFmode)
17549     use_sse = true;
17550   else if (TARGET_SSE_MATH)
17551     {
17552       use_sse = SSE_FLOAT_MODE_P (mode);
17553       if (mode == SFmode)
17554         vmode = V4SFmode;
17555       else if (mode == DFmode)
17556         vmode = V2DFmode;
17557     }
17558
17559   /* NEG and ABS performed with SSE use bitwise mask operations.
17560      Create the appropriate mask now.  */
17561   if (use_sse)
17562     mask = ix86_build_signbit_mask (vmode, vector_mode, code == ABS);
17563   else
17564     mask = NULL_RTX;
17565
17566   dst = operands[0];
17567   src = operands[1];
17568
17569   set = gen_rtx_fmt_e (code, mode, src);
17570   set = gen_rtx_SET (VOIDmode, dst, set);
17571
17572   if (mask)
17573     {
17574       rtx use, clob;
17575       rtvec par;
17576
17577       use = gen_rtx_USE (VOIDmode, mask);
17578       if (vector_mode)
17579         par = gen_rtvec (2, set, use);
17580       else
17581         {
17582           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
17583           par = gen_rtvec (3, set, use, clob);
17584         }
17585       emit_insn (gen_rtx_PARALLEL (VOIDmode, par));
17586     }
17587   else
17588     emit_insn (set);
17589 }
17590
17591 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
17592
17593 void
17594 ix86_expand_copysign (rtx operands[])
17595 {
17596   enum machine_mode mode, vmode;
17597   rtx dest, op0, op1, mask, nmask;
17598
17599   dest = operands[0];
17600   op0 = operands[1];
17601   op1 = operands[2];
17602
17603   mode = GET_MODE (dest);
17604
17605   if (mode == SFmode)
17606     vmode = V4SFmode;
17607   else if (mode == DFmode)
17608     vmode = V2DFmode;
17609   else
17610     vmode = mode;
17611
17612   if (GET_CODE (op0) == CONST_DOUBLE)
17613     {
17614       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
17615
17616       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
17617         op0 = simplify_unary_operation (ABS, mode, op0, mode);
17618
17619       if (mode == SFmode || mode == DFmode)
17620         {
17621           if (op0 == CONST0_RTX (mode))
17622             op0 = CONST0_RTX (vmode);
17623           else
17624             {
17625               rtx v = ix86_build_const_vector (vmode, false, op0);
17626
17627               op0 = force_reg (vmode, v);
17628             }
17629         }
17630       else if (op0 != CONST0_RTX (mode))
17631         op0 = force_reg (mode, op0);
17632
17633       mask = ix86_build_signbit_mask (vmode, 0, 0);
17634
17635       if (mode == SFmode)
17636         copysign_insn = gen_copysignsf3_const;
17637       else if (mode == DFmode)
17638         copysign_insn = gen_copysigndf3_const;
17639       else
17640         copysign_insn = gen_copysigntf3_const;
17641
17642         emit_insn (copysign_insn (dest, op0, op1, mask));
17643     }
17644   else
17645     {
17646       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
17647
17648       nmask = ix86_build_signbit_mask (vmode, 0, 1);
17649       mask = ix86_build_signbit_mask (vmode, 0, 0);
17650
17651       if (mode == SFmode)
17652         copysign_insn = gen_copysignsf3_var;
17653       else if (mode == DFmode)
17654         copysign_insn = gen_copysigndf3_var;
17655       else
17656         copysign_insn = gen_copysigntf3_var;
17657
17658       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
17659     }
17660 }
17661
17662 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
17663    be a constant, and so has already been expanded into a vector constant.  */
17664
17665 void
17666 ix86_split_copysign_const (rtx operands[])
17667 {
17668   enum machine_mode mode, vmode;
17669   rtx dest, op0, mask, x;
17670
17671   dest = operands[0];
17672   op0 = operands[1];
17673   mask = operands[3];
17674
17675   mode = GET_MODE (dest);
17676   vmode = GET_MODE (mask);
17677
17678   dest = simplify_gen_subreg (vmode, dest, mode, 0);
17679   x = gen_rtx_AND (vmode, dest, mask);
17680   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17681
17682   if (op0 != CONST0_RTX (vmode))
17683     {
17684       x = gen_rtx_IOR (vmode, dest, op0);
17685       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17686     }
17687 }
17688
17689 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
17690    so we have to do two masks.  */
17691
17692 void
17693 ix86_split_copysign_var (rtx operands[])
17694 {
17695   enum machine_mode mode, vmode;
17696   rtx dest, scratch, op0, op1, mask, nmask, x;
17697
17698   dest = operands[0];
17699   scratch = operands[1];
17700   op0 = operands[2];
17701   op1 = operands[3];
17702   nmask = operands[4];
17703   mask = operands[5];
17704
17705   mode = GET_MODE (dest);
17706   vmode = GET_MODE (mask);
17707
17708   if (rtx_equal_p (op0, op1))
17709     {
17710       /* Shouldn't happen often (it's useless, obviously), but when it does
17711          we'd generate incorrect code if we continue below.  */
17712       emit_move_insn (dest, op0);
17713       return;
17714     }
17715
17716   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
17717     {
17718       gcc_assert (REGNO (op1) == REGNO (scratch));
17719
17720       x = gen_rtx_AND (vmode, scratch, mask);
17721       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
17722
17723       dest = mask;
17724       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
17725       x = gen_rtx_NOT (vmode, dest);
17726       x = gen_rtx_AND (vmode, x, op0);
17727       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17728     }
17729   else
17730     {
17731       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
17732         {
17733           x = gen_rtx_AND (vmode, scratch, mask);
17734         }
17735       else                                              /* alternative 2,4 */
17736         {
17737           gcc_assert (REGNO (mask) == REGNO (scratch));
17738           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
17739           x = gen_rtx_AND (vmode, scratch, op1);
17740         }
17741       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
17742
17743       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
17744         {
17745           dest = simplify_gen_subreg (vmode, op0, mode, 0);
17746           x = gen_rtx_AND (vmode, dest, nmask);
17747         }
17748       else                                              /* alternative 3,4 */
17749         {
17750           gcc_assert (REGNO (nmask) == REGNO (dest));
17751           dest = nmask;
17752           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
17753           x = gen_rtx_AND (vmode, dest, op0);
17754         }
17755       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17756     }
17757
17758   x = gen_rtx_IOR (vmode, dest, scratch);
17759   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17760 }
17761
17762 /* Return TRUE or FALSE depending on whether the first SET in INSN
17763    has source and destination with matching CC modes, and that the
17764    CC mode is at least as constrained as REQ_MODE.  */
17765
17766 bool
17767 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
17768 {
17769   rtx set;
17770   enum machine_mode set_mode;
17771
17772   set = PATTERN (insn);
17773   if (GET_CODE (set) == PARALLEL)
17774     set = XVECEXP (set, 0, 0);
17775   gcc_assert (GET_CODE (set) == SET);
17776   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
17777
17778   set_mode = GET_MODE (SET_DEST (set));
17779   switch (set_mode)
17780     {
17781     case CCNOmode:
17782       if (req_mode != CCNOmode
17783           && (req_mode != CCmode
17784               || XEXP (SET_SRC (set), 1) != const0_rtx))
17785         return false;
17786       break;
17787     case CCmode:
17788       if (req_mode == CCGCmode)
17789         return false;
17790       /* FALLTHRU */
17791     case CCGCmode:
17792       if (req_mode == CCGOCmode || req_mode == CCNOmode)
17793         return false;
17794       /* FALLTHRU */
17795     case CCGOCmode:
17796       if (req_mode == CCZmode)
17797         return false;
17798       /* FALLTHRU */
17799     case CCZmode:
17800       break;
17801
17802     case CCAmode:
17803     case CCCmode:
17804     case CCOmode:
17805     case CCSmode:
17806       if (set_mode != req_mode)
17807         return false;
17808       break;
17809
17810     default:
17811       gcc_unreachable ();
17812     }
17813
17814   return GET_MODE (SET_SRC (set)) == set_mode;
17815 }
17816
17817 /* Generate insn patterns to do an integer compare of OPERANDS.  */
17818
17819 static rtx
17820 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
17821 {
17822   enum machine_mode cmpmode;
17823   rtx tmp, flags;
17824
17825   cmpmode = SELECT_CC_MODE (code, op0, op1);
17826   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
17827
17828   /* This is very simple, but making the interface the same as in the
17829      FP case makes the rest of the code easier.  */
17830   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
17831   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
17832
17833   /* Return the test that should be put into the flags user, i.e.
17834      the bcc, scc, or cmov instruction.  */
17835   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
17836 }
17837
17838 /* Figure out whether to use ordered or unordered fp comparisons.
17839    Return the appropriate mode to use.  */
17840
17841 enum machine_mode
17842 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
17843 {
17844   /* ??? In order to make all comparisons reversible, we do all comparisons
17845      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
17846      all forms trapping and nontrapping comparisons, we can make inequality
17847      comparisons trapping again, since it results in better code when using
17848      FCOM based compares.  */
17849   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
17850 }
17851
17852 enum machine_mode
17853 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
17854 {
17855   enum machine_mode mode = GET_MODE (op0);
17856
17857   if (SCALAR_FLOAT_MODE_P (mode))
17858     {
17859       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
17860       return ix86_fp_compare_mode (code);
17861     }
17862
17863   switch (code)
17864     {
17865       /* Only zero flag is needed.  */
17866     case EQ:                    /* ZF=0 */
17867     case NE:                    /* ZF!=0 */
17868       return CCZmode;
17869       /* Codes needing carry flag.  */
17870     case GEU:                   /* CF=0 */
17871     case LTU:                   /* CF=1 */
17872       /* Detect overflow checks.  They need just the carry flag.  */
17873       if (GET_CODE (op0) == PLUS
17874           && rtx_equal_p (op1, XEXP (op0, 0)))
17875         return CCCmode;
17876       else
17877         return CCmode;
17878     case GTU:                   /* CF=0 & ZF=0 */
17879     case LEU:                   /* CF=1 | ZF=1 */
17880       /* Detect overflow checks.  They need just the carry flag.  */
17881       if (GET_CODE (op0) == MINUS
17882           && rtx_equal_p (op1, XEXP (op0, 0)))
17883         return CCCmode;
17884       else
17885         return CCmode;
17886       /* Codes possibly doable only with sign flag when
17887          comparing against zero.  */
17888     case GE:                    /* SF=OF   or   SF=0 */
17889     case LT:                    /* SF<>OF  or   SF=1 */
17890       if (op1 == const0_rtx)
17891         return CCGOCmode;
17892       else
17893         /* For other cases Carry flag is not required.  */
17894         return CCGCmode;
17895       /* Codes doable only with sign flag when comparing
17896          against zero, but we miss jump instruction for it
17897          so we need to use relational tests against overflow
17898          that thus needs to be zero.  */
17899     case GT:                    /* ZF=0 & SF=OF */
17900     case LE:                    /* ZF=1 | SF<>OF */
17901       if (op1 == const0_rtx)
17902         return CCNOmode;
17903       else
17904         return CCGCmode;
17905       /* strcmp pattern do (use flags) and combine may ask us for proper
17906          mode.  */
17907     case USE:
17908       return CCmode;
17909     default:
17910       gcc_unreachable ();
17911     }
17912 }
17913
17914 /* Return the fixed registers used for condition codes.  */
17915
17916 static bool
17917 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
17918 {
17919   *p1 = FLAGS_REG;
17920   *p2 = FPSR_REG;
17921   return true;
17922 }
17923
17924 /* If two condition code modes are compatible, return a condition code
17925    mode which is compatible with both.  Otherwise, return
17926    VOIDmode.  */
17927
17928 static enum machine_mode
17929 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
17930 {
17931   if (m1 == m2)
17932     return m1;
17933
17934   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
17935     return VOIDmode;
17936
17937   if ((m1 == CCGCmode && m2 == CCGOCmode)
17938       || (m1 == CCGOCmode && m2 == CCGCmode))
17939     return CCGCmode;
17940
17941   switch (m1)
17942     {
17943     default:
17944       gcc_unreachable ();
17945
17946     case CCmode:
17947     case CCGCmode:
17948     case CCGOCmode:
17949     case CCNOmode:
17950     case CCAmode:
17951     case CCCmode:
17952     case CCOmode:
17953     case CCSmode:
17954     case CCZmode:
17955       switch (m2)
17956         {
17957         default:
17958           return VOIDmode;
17959
17960         case CCmode:
17961         case CCGCmode:
17962         case CCGOCmode:
17963         case CCNOmode:
17964         case CCAmode:
17965         case CCCmode:
17966         case CCOmode:
17967         case CCSmode:
17968         case CCZmode:
17969           return CCmode;
17970         }
17971
17972     case CCFPmode:
17973     case CCFPUmode:
17974       /* These are only compatible with themselves, which we already
17975          checked above.  */
17976       return VOIDmode;
17977     }
17978 }
17979
17980
17981 /* Return a comparison we can do and that it is equivalent to
17982    swap_condition (code) apart possibly from orderedness.
17983    But, never change orderedness if TARGET_IEEE_FP, returning
17984    UNKNOWN in that case if necessary.  */
17985
17986 static enum rtx_code
17987 ix86_fp_swap_condition (enum rtx_code code)
17988 {
17989   switch (code)
17990     {
17991     case GT:                   /* GTU - CF=0 & ZF=0 */
17992       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
17993     case GE:                   /* GEU - CF=0 */
17994       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
17995     case UNLT:                 /* LTU - CF=1 */
17996       return TARGET_IEEE_FP ? UNKNOWN : GT;
17997     case UNLE:                 /* LEU - CF=1 | ZF=1 */
17998       return TARGET_IEEE_FP ? UNKNOWN : GE;
17999     default:
18000       return swap_condition (code);
18001     }
18002 }
18003
18004 /* Return cost of comparison CODE using the best strategy for performance.
18005    All following functions do use number of instructions as a cost metrics.
18006    In future this should be tweaked to compute bytes for optimize_size and
18007    take into account performance of various instructions on various CPUs.  */
18008
18009 static int
18010 ix86_fp_comparison_cost (enum rtx_code code)
18011 {
18012   int arith_cost;
18013
18014   /* The cost of code using bit-twiddling on %ah.  */
18015   switch (code)
18016     {
18017     case UNLE:
18018     case UNLT:
18019     case LTGT:
18020     case GT:
18021     case GE:
18022     case UNORDERED:
18023     case ORDERED:
18024     case UNEQ:
18025       arith_cost = 4;
18026       break;
18027     case LT:
18028     case NE:
18029     case EQ:
18030     case UNGE:
18031       arith_cost = TARGET_IEEE_FP ? 5 : 4;
18032       break;
18033     case LE:
18034     case UNGT:
18035       arith_cost = TARGET_IEEE_FP ? 6 : 4;
18036       break;
18037     default:
18038       gcc_unreachable ();
18039     }
18040
18041   switch (ix86_fp_comparison_strategy (code))
18042     {
18043     case IX86_FPCMP_COMI:
18044       return arith_cost > 4 ? 3 : 2;
18045     case IX86_FPCMP_SAHF:
18046       return arith_cost > 4 ? 4 : 3;
18047     default:
18048       return arith_cost;
18049     }
18050 }
18051
18052 /* Return strategy to use for floating-point.  We assume that fcomi is always
18053    preferrable where available, since that is also true when looking at size
18054    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
18055
18056 enum ix86_fpcmp_strategy
18057 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
18058 {
18059   /* Do fcomi/sahf based test when profitable.  */
18060
18061   if (TARGET_CMOVE)
18062     return IX86_FPCMP_COMI;
18063
18064   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
18065     return IX86_FPCMP_SAHF;
18066
18067   return IX86_FPCMP_ARITH;
18068 }
18069
18070 /* Swap, force into registers, or otherwise massage the two operands
18071    to a fp comparison.  The operands are updated in place; the new
18072    comparison code is returned.  */
18073
18074 static enum rtx_code
18075 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
18076 {
18077   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
18078   rtx op0 = *pop0, op1 = *pop1;
18079   enum machine_mode op_mode = GET_MODE (op0);
18080   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
18081
18082   /* All of the unordered compare instructions only work on registers.
18083      The same is true of the fcomi compare instructions.  The XFmode
18084      compare instructions require registers except when comparing
18085      against zero or when converting operand 1 from fixed point to
18086      floating point.  */
18087
18088   if (!is_sse
18089       && (fpcmp_mode == CCFPUmode
18090           || (op_mode == XFmode
18091               && ! (standard_80387_constant_p (op0) == 1
18092                     || standard_80387_constant_p (op1) == 1)
18093               && GET_CODE (op1) != FLOAT)
18094           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
18095     {
18096       op0 = force_reg (op_mode, op0);
18097       op1 = force_reg (op_mode, op1);
18098     }
18099   else
18100     {
18101       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
18102          things around if they appear profitable, otherwise force op0
18103          into a register.  */
18104
18105       if (standard_80387_constant_p (op0) == 0
18106           || (MEM_P (op0)
18107               && ! (standard_80387_constant_p (op1) == 0
18108                     || MEM_P (op1))))
18109         {
18110           enum rtx_code new_code = ix86_fp_swap_condition (code);
18111           if (new_code != UNKNOWN)
18112             {
18113               rtx tmp;
18114               tmp = op0, op0 = op1, op1 = tmp;
18115               code = new_code;
18116             }
18117         }
18118
18119       if (!REG_P (op0))
18120         op0 = force_reg (op_mode, op0);
18121
18122       if (CONSTANT_P (op1))
18123         {
18124           int tmp = standard_80387_constant_p (op1);
18125           if (tmp == 0)
18126             op1 = validize_mem (force_const_mem (op_mode, op1));
18127           else if (tmp == 1)
18128             {
18129               if (TARGET_CMOVE)
18130                 op1 = force_reg (op_mode, op1);
18131             }
18132           else
18133             op1 = force_reg (op_mode, op1);
18134         }
18135     }
18136
18137   /* Try to rearrange the comparison to make it cheaper.  */
18138   if (ix86_fp_comparison_cost (code)
18139       > ix86_fp_comparison_cost (swap_condition (code))
18140       && (REG_P (op1) || can_create_pseudo_p ()))
18141     {
18142       rtx tmp;
18143       tmp = op0, op0 = op1, op1 = tmp;
18144       code = swap_condition (code);
18145       if (!REG_P (op0))
18146         op0 = force_reg (op_mode, op0);
18147     }
18148
18149   *pop0 = op0;
18150   *pop1 = op1;
18151   return code;
18152 }
18153
18154 /* Convert comparison codes we use to represent FP comparison to integer
18155    code that will result in proper branch.  Return UNKNOWN if no such code
18156    is available.  */
18157
18158 enum rtx_code
18159 ix86_fp_compare_code_to_integer (enum rtx_code code)
18160 {
18161   switch (code)
18162     {
18163     case GT:
18164       return GTU;
18165     case GE:
18166       return GEU;
18167     case ORDERED:
18168     case UNORDERED:
18169       return code;
18170       break;
18171     case UNEQ:
18172       return EQ;
18173       break;
18174     case UNLT:
18175       return LTU;
18176       break;
18177     case UNLE:
18178       return LEU;
18179       break;
18180     case LTGT:
18181       return NE;
18182       break;
18183     default:
18184       return UNKNOWN;
18185     }
18186 }
18187
18188 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
18189
18190 static rtx
18191 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
18192 {
18193   enum machine_mode fpcmp_mode, intcmp_mode;
18194   rtx tmp, tmp2;
18195
18196   fpcmp_mode = ix86_fp_compare_mode (code);
18197   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
18198
18199   /* Do fcomi/sahf based test when profitable.  */
18200   switch (ix86_fp_comparison_strategy (code))
18201     {
18202     case IX86_FPCMP_COMI:
18203       intcmp_mode = fpcmp_mode;
18204       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
18205       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
18206                          tmp);
18207       emit_insn (tmp);
18208       break;
18209
18210     case IX86_FPCMP_SAHF:
18211       intcmp_mode = fpcmp_mode;
18212       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
18213       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
18214                          tmp);
18215
18216       if (!scratch)
18217         scratch = gen_reg_rtx (HImode);
18218       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
18219       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
18220       break;
18221
18222     case IX86_FPCMP_ARITH:
18223       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
18224       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
18225       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
18226       if (!scratch)
18227         scratch = gen_reg_rtx (HImode);
18228       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
18229
18230       /* In the unordered case, we have to check C2 for NaN's, which
18231          doesn't happen to work out to anything nice combination-wise.
18232          So do some bit twiddling on the value we've got in AH to come
18233          up with an appropriate set of condition codes.  */
18234
18235       intcmp_mode = CCNOmode;
18236       switch (code)
18237         {
18238         case GT:
18239         case UNGT:
18240           if (code == GT || !TARGET_IEEE_FP)
18241             {
18242               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
18243               code = EQ;
18244             }
18245           else
18246             {
18247               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18248               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
18249               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
18250               intcmp_mode = CCmode;
18251               code = GEU;
18252             }
18253           break;
18254         case LT:
18255         case UNLT:
18256           if (code == LT && TARGET_IEEE_FP)
18257             {
18258               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18259               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
18260               intcmp_mode = CCmode;
18261               code = EQ;
18262             }
18263           else
18264             {
18265               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
18266               code = NE;
18267             }
18268           break;
18269         case GE:
18270         case UNGE:
18271           if (code == GE || !TARGET_IEEE_FP)
18272             {
18273               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
18274               code = EQ;
18275             }
18276           else
18277             {
18278               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18279               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
18280               code = NE;
18281             }
18282           break;
18283         case LE:
18284         case UNLE:
18285           if (code == LE && TARGET_IEEE_FP)
18286             {
18287               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18288               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
18289               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
18290               intcmp_mode = CCmode;
18291               code = LTU;
18292             }
18293           else
18294             {
18295               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
18296               code = NE;
18297             }
18298           break;
18299         case EQ:
18300         case UNEQ:
18301           if (code == EQ && TARGET_IEEE_FP)
18302             {
18303               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18304               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
18305               intcmp_mode = CCmode;
18306               code = EQ;
18307             }
18308           else
18309             {
18310               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
18311               code = NE;
18312             }
18313           break;
18314         case NE:
18315         case LTGT:
18316           if (code == NE && TARGET_IEEE_FP)
18317             {
18318               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18319               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
18320                                              GEN_INT (0x40)));
18321               code = NE;
18322             }
18323           else
18324             {
18325               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
18326               code = EQ;
18327             }
18328           break;
18329
18330         case UNORDERED:
18331           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
18332           code = NE;
18333           break;
18334         case ORDERED:
18335           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
18336           code = EQ;
18337           break;
18338
18339         default:
18340           gcc_unreachable ();
18341         }
18342         break;
18343
18344     default:
18345       gcc_unreachable();
18346     }
18347
18348   /* Return the test that should be put into the flags user, i.e.
18349      the bcc, scc, or cmov instruction.  */
18350   return gen_rtx_fmt_ee (code, VOIDmode,
18351                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
18352                          const0_rtx);
18353 }
18354
18355 static rtx
18356 ix86_expand_compare (enum rtx_code code, rtx op0, rtx op1)
18357 {
18358   rtx ret;
18359
18360   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
18361     ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
18362
18363   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
18364     {
18365       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
18366       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
18367     }
18368   else
18369     ret = ix86_expand_int_compare (code, op0, op1);
18370
18371   return ret;
18372 }
18373
18374 void
18375 ix86_expand_branch (enum rtx_code code, rtx op0, rtx op1, rtx label)
18376 {
18377   enum machine_mode mode = GET_MODE (op0);
18378   rtx tmp;
18379
18380   switch (mode)
18381     {
18382     case SFmode:
18383     case DFmode:
18384     case XFmode:
18385     case QImode:
18386     case HImode:
18387     case SImode:
18388       simple:
18389       tmp = ix86_expand_compare (code, op0, op1);
18390       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
18391                                   gen_rtx_LABEL_REF (VOIDmode, label),
18392                                   pc_rtx);
18393       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
18394       return;
18395
18396     case DImode:
18397       if (TARGET_64BIT)
18398         goto simple;
18399     case TImode:
18400       /* Expand DImode branch into multiple compare+branch.  */
18401       {
18402         rtx lo[2], hi[2], label2;
18403         enum rtx_code code1, code2, code3;
18404         enum machine_mode submode;
18405
18406         if (CONSTANT_P (op0) && !CONSTANT_P (op1))
18407           {
18408             tmp = op0, op0 = op1, op1 = tmp;
18409             code = swap_condition (code);
18410           }
18411
18412         split_double_mode (mode, &op0, 1, lo+0, hi+0);
18413         split_double_mode (mode, &op1, 1, lo+1, hi+1);
18414
18415         submode = mode == DImode ? SImode : DImode;
18416
18417         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
18418            avoid two branches.  This costs one extra insn, so disable when
18419            optimizing for size.  */
18420
18421         if ((code == EQ || code == NE)
18422             && (!optimize_insn_for_size_p ()
18423                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
18424           {
18425             rtx xor0, xor1;
18426
18427             xor1 = hi[0];
18428             if (hi[1] != const0_rtx)
18429               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
18430                                    NULL_RTX, 0, OPTAB_WIDEN);
18431
18432             xor0 = lo[0];
18433             if (lo[1] != const0_rtx)
18434               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
18435                                    NULL_RTX, 0, OPTAB_WIDEN);
18436
18437             tmp = expand_binop (submode, ior_optab, xor1, xor0,
18438                                 NULL_RTX, 0, OPTAB_WIDEN);
18439
18440             ix86_expand_branch (code, tmp, const0_rtx, label);
18441             return;
18442           }
18443
18444         /* Otherwise, if we are doing less-than or greater-or-equal-than,
18445            op1 is a constant and the low word is zero, then we can just
18446            examine the high word.  Similarly for low word -1 and
18447            less-or-equal-than or greater-than.  */
18448
18449         if (CONST_INT_P (hi[1]))
18450           switch (code)
18451             {
18452             case LT: case LTU: case GE: case GEU:
18453               if (lo[1] == const0_rtx)
18454                 {
18455                   ix86_expand_branch (code, hi[0], hi[1], label);
18456                   return;
18457                 }
18458               break;
18459             case LE: case LEU: case GT: case GTU:
18460               if (lo[1] == constm1_rtx)
18461                 {
18462                   ix86_expand_branch (code, hi[0], hi[1], label);
18463                   return;
18464                 }
18465               break;
18466             default:
18467               break;
18468             }
18469
18470         /* Otherwise, we need two or three jumps.  */
18471
18472         label2 = gen_label_rtx ();
18473
18474         code1 = code;
18475         code2 = swap_condition (code);
18476         code3 = unsigned_condition (code);
18477
18478         switch (code)
18479           {
18480           case LT: case GT: case LTU: case GTU:
18481             break;
18482
18483           case LE:   code1 = LT;  code2 = GT;  break;
18484           case GE:   code1 = GT;  code2 = LT;  break;
18485           case LEU:  code1 = LTU; code2 = GTU; break;
18486           case GEU:  code1 = GTU; code2 = LTU; break;
18487
18488           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
18489           case NE:   code2 = UNKNOWN; break;
18490
18491           default:
18492             gcc_unreachable ();
18493           }
18494
18495         /*
18496          * a < b =>
18497          *    if (hi(a) < hi(b)) goto true;
18498          *    if (hi(a) > hi(b)) goto false;
18499          *    if (lo(a) < lo(b)) goto true;
18500          *  false:
18501          */
18502
18503         if (code1 != UNKNOWN)
18504           ix86_expand_branch (code1, hi[0], hi[1], label);
18505         if (code2 != UNKNOWN)
18506           ix86_expand_branch (code2, hi[0], hi[1], label2);
18507
18508         ix86_expand_branch (code3, lo[0], lo[1], label);
18509
18510         if (code2 != UNKNOWN)
18511           emit_label (label2);
18512         return;
18513       }
18514
18515     default:
18516       gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC);
18517       goto simple;
18518     }
18519 }
18520
18521 /* Split branch based on floating point condition.  */
18522 void
18523 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
18524                       rtx target1, rtx target2, rtx tmp, rtx pushed)
18525 {
18526   rtx condition;
18527   rtx i;
18528
18529   if (target2 != pc_rtx)
18530     {
18531       rtx tmp = target2;
18532       code = reverse_condition_maybe_unordered (code);
18533       target2 = target1;
18534       target1 = tmp;
18535     }
18536
18537   condition = ix86_expand_fp_compare (code, op1, op2,
18538                                       tmp);
18539
18540   /* Remove pushed operand from stack.  */
18541   if (pushed)
18542     ix86_free_from_memory (GET_MODE (pushed));
18543
18544   i = emit_jump_insn (gen_rtx_SET
18545                       (VOIDmode, pc_rtx,
18546                        gen_rtx_IF_THEN_ELSE (VOIDmode,
18547                                              condition, target1, target2)));
18548   if (split_branch_probability >= 0)
18549     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
18550 }
18551
18552 void
18553 ix86_expand_setcc (rtx dest, enum rtx_code code, rtx op0, rtx op1)
18554 {
18555   rtx ret;
18556
18557   gcc_assert (GET_MODE (dest) == QImode);
18558
18559   ret = ix86_expand_compare (code, op0, op1);
18560   PUT_MODE (ret, QImode);
18561   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
18562 }
18563
18564 /* Expand comparison setting or clearing carry flag.  Return true when
18565    successful and set pop for the operation.  */
18566 static bool
18567 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
18568 {
18569   enum machine_mode mode =
18570     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
18571
18572   /* Do not handle double-mode compares that go through special path.  */
18573   if (mode == (TARGET_64BIT ? TImode : DImode))
18574     return false;
18575
18576   if (SCALAR_FLOAT_MODE_P (mode))
18577     {
18578       rtx compare_op, compare_seq;
18579
18580       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
18581
18582       /* Shortcut:  following common codes never translate
18583          into carry flag compares.  */
18584       if (code == EQ || code == NE || code == UNEQ || code == LTGT
18585           || code == ORDERED || code == UNORDERED)
18586         return false;
18587
18588       /* These comparisons require zero flag; swap operands so they won't.  */
18589       if ((code == GT || code == UNLE || code == LE || code == UNGT)
18590           && !TARGET_IEEE_FP)
18591         {
18592           rtx tmp = op0;
18593           op0 = op1;
18594           op1 = tmp;
18595           code = swap_condition (code);
18596         }
18597
18598       /* Try to expand the comparison and verify that we end up with
18599          carry flag based comparison.  This fails to be true only when
18600          we decide to expand comparison using arithmetic that is not
18601          too common scenario.  */
18602       start_sequence ();
18603       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
18604       compare_seq = get_insns ();
18605       end_sequence ();
18606
18607       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
18608           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
18609         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
18610       else
18611         code = GET_CODE (compare_op);
18612
18613       if (code != LTU && code != GEU)
18614         return false;
18615
18616       emit_insn (compare_seq);
18617       *pop = compare_op;
18618       return true;
18619     }
18620
18621   if (!INTEGRAL_MODE_P (mode))
18622     return false;
18623
18624   switch (code)
18625     {
18626     case LTU:
18627     case GEU:
18628       break;
18629
18630     /* Convert a==0 into (unsigned)a<1.  */
18631     case EQ:
18632     case NE:
18633       if (op1 != const0_rtx)
18634         return false;
18635       op1 = const1_rtx;
18636       code = (code == EQ ? LTU : GEU);
18637       break;
18638
18639     /* Convert a>b into b<a or a>=b-1.  */
18640     case GTU:
18641     case LEU:
18642       if (CONST_INT_P (op1))
18643         {
18644           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
18645           /* Bail out on overflow.  We still can swap operands but that
18646              would force loading of the constant into register.  */
18647           if (op1 == const0_rtx
18648               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
18649             return false;
18650           code = (code == GTU ? GEU : LTU);
18651         }
18652       else
18653         {
18654           rtx tmp = op1;
18655           op1 = op0;
18656           op0 = tmp;
18657           code = (code == GTU ? LTU : GEU);
18658         }
18659       break;
18660
18661     /* Convert a>=0 into (unsigned)a<0x80000000.  */
18662     case LT:
18663     case GE:
18664       if (mode == DImode || op1 != const0_rtx)
18665         return false;
18666       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
18667       code = (code == LT ? GEU : LTU);
18668       break;
18669     case LE:
18670     case GT:
18671       if (mode == DImode || op1 != constm1_rtx)
18672         return false;
18673       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
18674       code = (code == LE ? GEU : LTU);
18675       break;
18676
18677     default:
18678       return false;
18679     }
18680   /* Swapping operands may cause constant to appear as first operand.  */
18681   if (!nonimmediate_operand (op0, VOIDmode))
18682     {
18683       if (!can_create_pseudo_p ())
18684         return false;
18685       op0 = force_reg (mode, op0);
18686     }
18687   *pop = ix86_expand_compare (code, op0, op1);
18688   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
18689   return true;
18690 }
18691
18692 bool
18693 ix86_expand_int_movcc (rtx operands[])
18694 {
18695   enum rtx_code code = GET_CODE (operands[1]), compare_code;
18696   rtx compare_seq, compare_op;
18697   enum machine_mode mode = GET_MODE (operands[0]);
18698   bool sign_bit_compare_p = false;
18699   rtx op0 = XEXP (operands[1], 0);
18700   rtx op1 = XEXP (operands[1], 1);
18701
18702   start_sequence ();
18703   compare_op = ix86_expand_compare (code, op0, op1);
18704   compare_seq = get_insns ();
18705   end_sequence ();
18706
18707   compare_code = GET_CODE (compare_op);
18708
18709   if ((op1 == const0_rtx && (code == GE || code == LT))
18710       || (op1 == constm1_rtx && (code == GT || code == LE)))
18711     sign_bit_compare_p = true;
18712
18713   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
18714      HImode insns, we'd be swallowed in word prefix ops.  */
18715
18716   if ((mode != HImode || TARGET_FAST_PREFIX)
18717       && (mode != (TARGET_64BIT ? TImode : DImode))
18718       && CONST_INT_P (operands[2])
18719       && CONST_INT_P (operands[3]))
18720     {
18721       rtx out = operands[0];
18722       HOST_WIDE_INT ct = INTVAL (operands[2]);
18723       HOST_WIDE_INT cf = INTVAL (operands[3]);
18724       HOST_WIDE_INT diff;
18725
18726       diff = ct - cf;
18727       /*  Sign bit compares are better done using shifts than we do by using
18728           sbb.  */
18729       if (sign_bit_compare_p
18730           || ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
18731         {
18732           /* Detect overlap between destination and compare sources.  */
18733           rtx tmp = out;
18734
18735           if (!sign_bit_compare_p)
18736             {
18737               rtx flags;
18738               bool fpcmp = false;
18739
18740               compare_code = GET_CODE (compare_op);
18741
18742               flags = XEXP (compare_op, 0);
18743
18744               if (GET_MODE (flags) == CCFPmode
18745                   || GET_MODE (flags) == CCFPUmode)
18746                 {
18747                   fpcmp = true;
18748                   compare_code
18749                     = ix86_fp_compare_code_to_integer (compare_code);
18750                 }
18751
18752               /* To simplify rest of code, restrict to the GEU case.  */
18753               if (compare_code == LTU)
18754                 {
18755                   HOST_WIDE_INT tmp = ct;
18756                   ct = cf;
18757                   cf = tmp;
18758                   compare_code = reverse_condition (compare_code);
18759                   code = reverse_condition (code);
18760                 }
18761               else
18762                 {
18763                   if (fpcmp)
18764                     PUT_CODE (compare_op,
18765                               reverse_condition_maybe_unordered
18766                                 (GET_CODE (compare_op)));
18767                   else
18768                     PUT_CODE (compare_op,
18769                               reverse_condition (GET_CODE (compare_op)));
18770                 }
18771               diff = ct - cf;
18772
18773               if (reg_overlap_mentioned_p (out, op0)
18774                   || reg_overlap_mentioned_p (out, op1))
18775                 tmp = gen_reg_rtx (mode);
18776
18777               if (mode == DImode)
18778                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
18779               else
18780                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
18781                                                  flags, compare_op));
18782             }
18783           else
18784             {
18785               if (code == GT || code == GE)
18786                 code = reverse_condition (code);
18787               else
18788                 {
18789                   HOST_WIDE_INT tmp = ct;
18790                   ct = cf;
18791                   cf = tmp;
18792                   diff = ct - cf;
18793                 }
18794               tmp = emit_store_flag (tmp, code, op0, op1, VOIDmode, 0, -1);
18795             }
18796
18797           if (diff == 1)
18798             {
18799               /*
18800                * cmpl op0,op1
18801                * sbbl dest,dest
18802                * [addl dest, ct]
18803                *
18804                * Size 5 - 8.
18805                */
18806               if (ct)
18807                 tmp = expand_simple_binop (mode, PLUS,
18808                                            tmp, GEN_INT (ct),
18809                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18810             }
18811           else if (cf == -1)
18812             {
18813               /*
18814                * cmpl op0,op1
18815                * sbbl dest,dest
18816                * orl $ct, dest
18817                *
18818                * Size 8.
18819                */
18820               tmp = expand_simple_binop (mode, IOR,
18821                                          tmp, GEN_INT (ct),
18822                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
18823             }
18824           else if (diff == -1 && ct)
18825             {
18826               /*
18827                * cmpl op0,op1
18828                * sbbl dest,dest
18829                * notl dest
18830                * [addl dest, cf]
18831                *
18832                * Size 8 - 11.
18833                */
18834               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
18835               if (cf)
18836                 tmp = expand_simple_binop (mode, PLUS,
18837                                            copy_rtx (tmp), GEN_INT (cf),
18838                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18839             }
18840           else
18841             {
18842               /*
18843                * cmpl op0,op1
18844                * sbbl dest,dest
18845                * [notl dest]
18846                * andl cf - ct, dest
18847                * [addl dest, ct]
18848                *
18849                * Size 8 - 11.
18850                */
18851
18852               if (cf == 0)
18853                 {
18854                   cf = ct;
18855                   ct = 0;
18856                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
18857                 }
18858
18859               tmp = expand_simple_binop (mode, AND,
18860                                          copy_rtx (tmp),
18861                                          gen_int_mode (cf - ct, mode),
18862                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
18863               if (ct)
18864                 tmp = expand_simple_binop (mode, PLUS,
18865                                            copy_rtx (tmp), GEN_INT (ct),
18866                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18867             }
18868
18869           if (!rtx_equal_p (tmp, out))
18870             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
18871
18872           return true;
18873         }
18874
18875       if (diff < 0)
18876         {
18877           enum machine_mode cmp_mode = GET_MODE (op0);
18878
18879           HOST_WIDE_INT tmp;
18880           tmp = ct, ct = cf, cf = tmp;
18881           diff = -diff;
18882
18883           if (SCALAR_FLOAT_MODE_P (cmp_mode))
18884             {
18885               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
18886
18887               /* We may be reversing unordered compare to normal compare, that
18888                  is not valid in general (we may convert non-trapping condition
18889                  to trapping one), however on i386 we currently emit all
18890                  comparisons unordered.  */
18891               compare_code = reverse_condition_maybe_unordered (compare_code);
18892               code = reverse_condition_maybe_unordered (code);
18893             }
18894           else
18895             {
18896               compare_code = reverse_condition (compare_code);
18897               code = reverse_condition (code);
18898             }
18899         }
18900
18901       compare_code = UNKNOWN;
18902       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
18903           && CONST_INT_P (op1))
18904         {
18905           if (op1 == const0_rtx
18906               && (code == LT || code == GE))
18907             compare_code = code;
18908           else if (op1 == constm1_rtx)
18909             {
18910               if (code == LE)
18911                 compare_code = LT;
18912               else if (code == GT)
18913                 compare_code = GE;
18914             }
18915         }
18916
18917       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
18918       if (compare_code != UNKNOWN
18919           && GET_MODE (op0) == GET_MODE (out)
18920           && (cf == -1 || ct == -1))
18921         {
18922           /* If lea code below could be used, only optimize
18923              if it results in a 2 insn sequence.  */
18924
18925           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
18926                  || diff == 3 || diff == 5 || diff == 9)
18927               || (compare_code == LT && ct == -1)
18928               || (compare_code == GE && cf == -1))
18929             {
18930               /*
18931                * notl op1       (if necessary)
18932                * sarl $31, op1
18933                * orl cf, op1
18934                */
18935               if (ct != -1)
18936                 {
18937                   cf = ct;
18938                   ct = -1;
18939                   code = reverse_condition (code);
18940                 }
18941
18942               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
18943
18944               out = expand_simple_binop (mode, IOR,
18945                                          out, GEN_INT (cf),
18946                                          out, 1, OPTAB_DIRECT);
18947               if (out != operands[0])
18948                 emit_move_insn (operands[0], out);
18949
18950               return true;
18951             }
18952         }
18953
18954
18955       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
18956            || diff == 3 || diff == 5 || diff == 9)
18957           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
18958           && (mode != DImode
18959               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
18960         {
18961           /*
18962            * xorl dest,dest
18963            * cmpl op1,op2
18964            * setcc dest
18965            * lea cf(dest*(ct-cf)),dest
18966            *
18967            * Size 14.
18968            *
18969            * This also catches the degenerate setcc-only case.
18970            */
18971
18972           rtx tmp;
18973           int nops;
18974
18975           out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
18976
18977           nops = 0;
18978           /* On x86_64 the lea instruction operates on Pmode, so we need
18979              to get arithmetics done in proper mode to match.  */
18980           if (diff == 1)
18981             tmp = copy_rtx (out);
18982           else
18983             {
18984               rtx out1;
18985               out1 = copy_rtx (out);
18986               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
18987               nops++;
18988               if (diff & 1)
18989                 {
18990                   tmp = gen_rtx_PLUS (mode, tmp, out1);
18991                   nops++;
18992                 }
18993             }
18994           if (cf != 0)
18995             {
18996               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
18997               nops++;
18998             }
18999           if (!rtx_equal_p (tmp, out))
19000             {
19001               if (nops == 1)
19002                 out = force_operand (tmp, copy_rtx (out));
19003               else
19004                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
19005             }
19006           if (!rtx_equal_p (out, operands[0]))
19007             emit_move_insn (operands[0], copy_rtx (out));
19008
19009           return true;
19010         }
19011
19012       /*
19013        * General case:                  Jumpful:
19014        *   xorl dest,dest               cmpl op1, op2
19015        *   cmpl op1, op2                movl ct, dest
19016        *   setcc dest                   jcc 1f
19017        *   decl dest                    movl cf, dest
19018        *   andl (cf-ct),dest            1:
19019        *   addl ct,dest
19020        *
19021        * Size 20.                       Size 14.
19022        *
19023        * This is reasonably steep, but branch mispredict costs are
19024        * high on modern cpus, so consider failing only if optimizing
19025        * for space.
19026        */
19027
19028       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
19029           && BRANCH_COST (optimize_insn_for_speed_p (),
19030                           false) >= 2)
19031         {
19032           if (cf == 0)
19033             {
19034               enum machine_mode cmp_mode = GET_MODE (op0);
19035
19036               cf = ct;
19037               ct = 0;
19038
19039               if (SCALAR_FLOAT_MODE_P (cmp_mode))
19040                 {
19041                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
19042
19043                   /* We may be reversing unordered compare to normal compare,
19044                      that is not valid in general (we may convert non-trapping
19045                      condition to trapping one), however on i386 we currently
19046                      emit all comparisons unordered.  */
19047                   code = reverse_condition_maybe_unordered (code);
19048                 }
19049               else
19050                 {
19051                   code = reverse_condition (code);
19052                   if (compare_code != UNKNOWN)
19053                     compare_code = reverse_condition (compare_code);
19054                 }
19055             }
19056
19057           if (compare_code != UNKNOWN)
19058             {
19059               /* notl op1       (if needed)
19060                  sarl $31, op1
19061                  andl (cf-ct), op1
19062                  addl ct, op1
19063
19064                  For x < 0 (resp. x <= -1) there will be no notl,
19065                  so if possible swap the constants to get rid of the
19066                  complement.
19067                  True/false will be -1/0 while code below (store flag
19068                  followed by decrement) is 0/-1, so the constants need
19069                  to be exchanged once more.  */
19070
19071               if (compare_code == GE || !cf)
19072                 {
19073                   code = reverse_condition (code);
19074                   compare_code = LT;
19075                 }
19076               else
19077                 {
19078                   HOST_WIDE_INT tmp = cf;
19079                   cf = ct;
19080                   ct = tmp;
19081                 }
19082
19083               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
19084             }
19085           else
19086             {
19087               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
19088
19089               out = expand_simple_binop (mode, PLUS, copy_rtx (out),
19090                                          constm1_rtx,
19091                                          copy_rtx (out), 1, OPTAB_DIRECT);
19092             }
19093
19094           out = expand_simple_binop (mode, AND, copy_rtx (out),
19095                                      gen_int_mode (cf - ct, mode),
19096                                      copy_rtx (out), 1, OPTAB_DIRECT);
19097           if (ct)
19098             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
19099                                        copy_rtx (out), 1, OPTAB_DIRECT);
19100           if (!rtx_equal_p (out, operands[0]))
19101             emit_move_insn (operands[0], copy_rtx (out));
19102
19103           return true;
19104         }
19105     }
19106
19107   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
19108     {
19109       /* Try a few things more with specific constants and a variable.  */
19110
19111       optab op;
19112       rtx var, orig_out, out, tmp;
19113
19114       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
19115         return false;
19116
19117       /* If one of the two operands is an interesting constant, load a
19118          constant with the above and mask it in with a logical operation.  */
19119
19120       if (CONST_INT_P (operands[2]))
19121         {
19122           var = operands[3];
19123           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
19124             operands[3] = constm1_rtx, op = and_optab;
19125           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
19126             operands[3] = const0_rtx, op = ior_optab;
19127           else
19128             return false;
19129         }
19130       else if (CONST_INT_P (operands[3]))
19131         {
19132           var = operands[2];
19133           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
19134             operands[2] = constm1_rtx, op = and_optab;
19135           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
19136             operands[2] = const0_rtx, op = ior_optab;
19137           else
19138             return false;
19139         }
19140       else
19141         return false;
19142
19143       orig_out = operands[0];
19144       tmp = gen_reg_rtx (mode);
19145       operands[0] = tmp;
19146
19147       /* Recurse to get the constant loaded.  */
19148       if (ix86_expand_int_movcc (operands) == 0)
19149         return false;
19150
19151       /* Mask in the interesting variable.  */
19152       out = expand_binop (mode, op, var, tmp, orig_out, 0,
19153                           OPTAB_WIDEN);
19154       if (!rtx_equal_p (out, orig_out))
19155         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
19156
19157       return true;
19158     }
19159
19160   /*
19161    * For comparison with above,
19162    *
19163    * movl cf,dest
19164    * movl ct,tmp
19165    * cmpl op1,op2
19166    * cmovcc tmp,dest
19167    *
19168    * Size 15.
19169    */
19170
19171   if (! nonimmediate_operand (operands[2], mode))
19172     operands[2] = force_reg (mode, operands[2]);
19173   if (! nonimmediate_operand (operands[3], mode))
19174     operands[3] = force_reg (mode, operands[3]);
19175
19176   if (! register_operand (operands[2], VOIDmode)
19177       && (mode == QImode
19178           || ! register_operand (operands[3], VOIDmode)))
19179     operands[2] = force_reg (mode, operands[2]);
19180
19181   if (mode == QImode
19182       && ! register_operand (operands[3], VOIDmode))
19183     operands[3] = force_reg (mode, operands[3]);
19184
19185   emit_insn (compare_seq);
19186   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
19187                           gen_rtx_IF_THEN_ELSE (mode,
19188                                                 compare_op, operands[2],
19189                                                 operands[3])));
19190   return true;
19191 }
19192
19193 /* Swap, force into registers, or otherwise massage the two operands
19194    to an sse comparison with a mask result.  Thus we differ a bit from
19195    ix86_prepare_fp_compare_args which expects to produce a flags result.
19196
19197    The DEST operand exists to help determine whether to commute commutative
19198    operators.  The POP0/POP1 operands are updated in place.  The new
19199    comparison code is returned, or UNKNOWN if not implementable.  */
19200
19201 static enum rtx_code
19202 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
19203                                   rtx *pop0, rtx *pop1)
19204 {
19205   rtx tmp;
19206
19207   switch (code)
19208     {
19209     case LTGT:
19210     case UNEQ:
19211       /* AVX supports all the needed comparisons.  */
19212       if (TARGET_AVX)
19213         break;
19214       /* We have no LTGT as an operator.  We could implement it with
19215          NE & ORDERED, but this requires an extra temporary.  It's
19216          not clear that it's worth it.  */
19217       return UNKNOWN;
19218
19219     case LT:
19220     case LE:
19221     case UNGT:
19222     case UNGE:
19223       /* These are supported directly.  */
19224       break;
19225
19226     case EQ:
19227     case NE:
19228     case UNORDERED:
19229     case ORDERED:
19230       /* AVX has 3 operand comparisons, no need to swap anything.  */
19231       if (TARGET_AVX)
19232         break;
19233       /* For commutative operators, try to canonicalize the destination
19234          operand to be first in the comparison - this helps reload to
19235          avoid extra moves.  */
19236       if (!dest || !rtx_equal_p (dest, *pop1))
19237         break;
19238       /* FALLTHRU */
19239
19240     case GE:
19241     case GT:
19242     case UNLE:
19243     case UNLT:
19244       /* These are not supported directly before AVX, and furthermore
19245          ix86_expand_sse_fp_minmax only optimizes LT/UNGE.  Swap the
19246          comparison operands to transform into something that is
19247          supported.  */
19248       tmp = *pop0;
19249       *pop0 = *pop1;
19250       *pop1 = tmp;
19251       code = swap_condition (code);
19252       break;
19253
19254     default:
19255       gcc_unreachable ();
19256     }
19257
19258   return code;
19259 }
19260
19261 /* Detect conditional moves that exactly match min/max operational
19262    semantics.  Note that this is IEEE safe, as long as we don't
19263    interchange the operands.
19264
19265    Returns FALSE if this conditional move doesn't match a MIN/MAX,
19266    and TRUE if the operation is successful and instructions are emitted.  */
19267
19268 static bool
19269 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
19270                            rtx cmp_op1, rtx if_true, rtx if_false)
19271 {
19272   enum machine_mode mode;
19273   bool is_min;
19274   rtx tmp;
19275
19276   if (code == LT)
19277     ;
19278   else if (code == UNGE)
19279     {
19280       tmp = if_true;
19281       if_true = if_false;
19282       if_false = tmp;
19283     }
19284   else
19285     return false;
19286
19287   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
19288     is_min = true;
19289   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
19290     is_min = false;
19291   else
19292     return false;
19293
19294   mode = GET_MODE (dest);
19295
19296   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
19297      but MODE may be a vector mode and thus not appropriate.  */
19298   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
19299     {
19300       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
19301       rtvec v;
19302
19303       if_true = force_reg (mode, if_true);
19304       v = gen_rtvec (2, if_true, if_false);
19305       tmp = gen_rtx_UNSPEC (mode, v, u);
19306     }
19307   else
19308     {
19309       code = is_min ? SMIN : SMAX;
19310       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
19311     }
19312
19313   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
19314   return true;
19315 }
19316
19317 /* Expand an sse vector comparison.  Return the register with the result.  */
19318
19319 static rtx
19320 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
19321                      rtx op_true, rtx op_false)
19322 {
19323   enum machine_mode mode = GET_MODE (dest);
19324   enum machine_mode cmp_mode = GET_MODE (cmp_op0);
19325   rtx x;
19326
19327   cmp_op0 = force_reg (cmp_mode, cmp_op0);
19328   if (!nonimmediate_operand (cmp_op1, cmp_mode))
19329     cmp_op1 = force_reg (cmp_mode, cmp_op1);
19330
19331   if (optimize
19332       || reg_overlap_mentioned_p (dest, op_true)
19333       || reg_overlap_mentioned_p (dest, op_false))
19334     dest = gen_reg_rtx (mode);
19335
19336   x = gen_rtx_fmt_ee (code, cmp_mode, cmp_op0, cmp_op1);
19337   if (cmp_mode != mode)
19338     {
19339       x = force_reg (cmp_mode, x);
19340       convert_move (dest, x, false);
19341     }
19342   else
19343     emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19344
19345   return dest;
19346 }
19347
19348 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
19349    operations.  This is used for both scalar and vector conditional moves.  */
19350
19351 static void
19352 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
19353 {
19354   enum machine_mode mode = GET_MODE (dest);
19355   rtx t2, t3, x;
19356
19357   if (vector_all_ones_operand (op_true, mode)
19358       && rtx_equal_p (op_false, CONST0_RTX (mode)))
19359     {
19360       emit_insn (gen_rtx_SET (VOIDmode, dest, cmp));
19361     }
19362   else if (op_false == CONST0_RTX (mode))
19363     {
19364       op_true = force_reg (mode, op_true);
19365       x = gen_rtx_AND (mode, cmp, op_true);
19366       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19367     }
19368   else if (op_true == CONST0_RTX (mode))
19369     {
19370       op_false = force_reg (mode, op_false);
19371       x = gen_rtx_NOT (mode, cmp);
19372       x = gen_rtx_AND (mode, x, op_false);
19373       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19374     }
19375   else if (INTEGRAL_MODE_P (mode) && op_true == CONSTM1_RTX (mode))
19376     {
19377       op_false = force_reg (mode, op_false);
19378       x = gen_rtx_IOR (mode, cmp, op_false);
19379       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19380     }
19381   else if (TARGET_XOP)
19382     {
19383       op_true = force_reg (mode, op_true);
19384
19385       if (!nonimmediate_operand (op_false, mode))
19386         op_false = force_reg (mode, op_false);
19387
19388       emit_insn (gen_rtx_SET (mode, dest,
19389                               gen_rtx_IF_THEN_ELSE (mode, cmp,
19390                                                     op_true,
19391                                                     op_false)));
19392     }
19393   else
19394     {
19395       rtx (*gen) (rtx, rtx, rtx, rtx) = NULL;
19396
19397       if (!nonimmediate_operand (op_true, mode))
19398         op_true = force_reg (mode, op_true);
19399
19400       op_false = force_reg (mode, op_false);
19401
19402       switch (mode)
19403         {
19404         case V4SFmode:
19405           if (TARGET_SSE4_1)
19406             gen = gen_sse4_1_blendvps;
19407           break;
19408         case V2DFmode:
19409           if (TARGET_SSE4_1)
19410             gen = gen_sse4_1_blendvpd;
19411           break;
19412         case V16QImode:
19413         case V8HImode:
19414         case V4SImode:
19415         case V2DImode:
19416           if (TARGET_SSE4_1)
19417             {
19418               gen = gen_sse4_1_pblendvb;
19419               dest = gen_lowpart (V16QImode, dest);
19420               op_false = gen_lowpart (V16QImode, op_false);
19421               op_true = gen_lowpart (V16QImode, op_true);
19422               cmp = gen_lowpart (V16QImode, cmp);
19423             }
19424           break;
19425         case V8SFmode:
19426           if (TARGET_AVX)
19427             gen = gen_avx_blendvps256;
19428           break;
19429         case V4DFmode:
19430           if (TARGET_AVX)
19431             gen = gen_avx_blendvpd256;
19432           break;
19433         case V32QImode:
19434         case V16HImode:
19435         case V8SImode:
19436         case V4DImode:
19437           if (TARGET_AVX2)
19438             {
19439               gen = gen_avx2_pblendvb;
19440               dest = gen_lowpart (V32QImode, dest);
19441               op_false = gen_lowpart (V32QImode, op_false);
19442               op_true = gen_lowpart (V32QImode, op_true);
19443               cmp = gen_lowpart (V32QImode, cmp);
19444             }
19445           break;
19446         default:
19447           break;
19448         }
19449
19450       if (gen != NULL)
19451         emit_insn (gen (dest, op_false, op_true, cmp));
19452       else
19453         {
19454           op_true = force_reg (mode, op_true);
19455
19456           t2 = gen_reg_rtx (mode);
19457           if (optimize)
19458             t3 = gen_reg_rtx (mode);
19459           else
19460             t3 = dest;
19461
19462           x = gen_rtx_AND (mode, op_true, cmp);
19463           emit_insn (gen_rtx_SET (VOIDmode, t2, x));
19464
19465           x = gen_rtx_NOT (mode, cmp);
19466           x = gen_rtx_AND (mode, x, op_false);
19467           emit_insn (gen_rtx_SET (VOIDmode, t3, x));
19468
19469           x = gen_rtx_IOR (mode, t3, t2);
19470           emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19471         }
19472     }
19473 }
19474
19475 /* Expand a floating-point conditional move.  Return true if successful.  */
19476
19477 bool
19478 ix86_expand_fp_movcc (rtx operands[])
19479 {
19480   enum machine_mode mode = GET_MODE (operands[0]);
19481   enum rtx_code code = GET_CODE (operands[1]);
19482   rtx tmp, compare_op;
19483   rtx op0 = XEXP (operands[1], 0);
19484   rtx op1 = XEXP (operands[1], 1);
19485
19486   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
19487     {
19488       enum machine_mode cmode;
19489
19490       /* Since we've no cmove for sse registers, don't force bad register
19491          allocation just to gain access to it.  Deny movcc when the
19492          comparison mode doesn't match the move mode.  */
19493       cmode = GET_MODE (op0);
19494       if (cmode == VOIDmode)
19495         cmode = GET_MODE (op1);
19496       if (cmode != mode)
19497         return false;
19498
19499       code = ix86_prepare_sse_fp_compare_args (operands[0], code, &op0, &op1);
19500       if (code == UNKNOWN)
19501         return false;
19502
19503       if (ix86_expand_sse_fp_minmax (operands[0], code, op0, op1,
19504                                      operands[2], operands[3]))
19505         return true;
19506
19507       tmp = ix86_expand_sse_cmp (operands[0], code, op0, op1,
19508                                  operands[2], operands[3]);
19509       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
19510       return true;
19511     }
19512
19513   /* The floating point conditional move instructions don't directly
19514      support conditions resulting from a signed integer comparison.  */
19515
19516   compare_op = ix86_expand_compare (code, op0, op1);
19517   if (!fcmov_comparison_operator (compare_op, VOIDmode))
19518     {
19519       tmp = gen_reg_rtx (QImode);
19520       ix86_expand_setcc (tmp, code, op0, op1);
19521
19522       compare_op = ix86_expand_compare (NE, tmp, const0_rtx);
19523     }
19524
19525   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
19526                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
19527                                                 operands[2], operands[3])));
19528
19529   return true;
19530 }
19531
19532 /* Expand a floating-point vector conditional move; a vcond operation
19533    rather than a movcc operation.  */
19534
19535 bool
19536 ix86_expand_fp_vcond (rtx operands[])
19537 {
19538   enum rtx_code code = GET_CODE (operands[3]);
19539   rtx cmp;
19540
19541   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
19542                                            &operands[4], &operands[5]);
19543   if (code == UNKNOWN)
19544     {
19545       rtx temp;
19546       switch (GET_CODE (operands[3]))
19547         {
19548         case LTGT:
19549           temp = ix86_expand_sse_cmp (operands[0], ORDERED, operands[4],
19550                                       operands[5], operands[0], operands[0]);
19551           cmp = ix86_expand_sse_cmp (operands[0], NE, operands[4],
19552                                      operands[5], operands[1], operands[2]);
19553           code = AND;
19554           break;
19555         case UNEQ:
19556           temp = ix86_expand_sse_cmp (operands[0], UNORDERED, operands[4],
19557                                       operands[5], operands[0], operands[0]);
19558           cmp = ix86_expand_sse_cmp (operands[0], EQ, operands[4],
19559                                      operands[5], operands[1], operands[2]);
19560           code = IOR;
19561           break;
19562         default:
19563           gcc_unreachable ();
19564         }
19565       cmp = expand_simple_binop (GET_MODE (cmp), code, temp, cmp, cmp, 1,
19566                                  OPTAB_DIRECT);
19567       ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
19568       return true;
19569     }
19570
19571   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
19572                                  operands[5], operands[1], operands[2]))
19573     return true;
19574
19575   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
19576                              operands[1], operands[2]);
19577   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
19578   return true;
19579 }
19580
19581 /* Expand a signed/unsigned integral vector conditional move.  */
19582
19583 bool
19584 ix86_expand_int_vcond (rtx operands[])
19585 {
19586   enum machine_mode data_mode = GET_MODE (operands[0]);
19587   enum machine_mode mode = GET_MODE (operands[4]);
19588   enum rtx_code code = GET_CODE (operands[3]);
19589   bool negate = false;
19590   rtx x, cop0, cop1;
19591
19592   cop0 = operands[4];
19593   cop1 = operands[5];
19594
19595   /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
19596      and x < 0 ? 1 : 0 into (unsigned) x >> 31.  */
19597   if ((code == LT || code == GE)
19598       && data_mode == mode
19599       && cop1 == CONST0_RTX (mode)
19600       && operands[1 + (code == LT)] == CONST0_RTX (data_mode)
19601       && GET_MODE_SIZE (GET_MODE_INNER (data_mode)) > 1
19602       && GET_MODE_SIZE (GET_MODE_INNER (data_mode)) <= 8
19603       && (GET_MODE_SIZE (data_mode) == 16
19604           || (TARGET_AVX2 && GET_MODE_SIZE (data_mode) == 32)))
19605     {
19606       rtx negop = operands[2 - (code == LT)];
19607       int shift = GET_MODE_BITSIZE (GET_MODE_INNER (data_mode)) - 1;
19608       if (negop == CONST1_RTX (data_mode))
19609         {
19610           rtx res = expand_simple_binop (mode, LSHIFTRT, cop0, GEN_INT (shift),
19611                                          operands[0], 1, OPTAB_DIRECT);
19612           if (res != operands[0])
19613             emit_move_insn (operands[0], res);
19614           return true;
19615         }
19616       else if (GET_MODE_INNER (data_mode) != DImode
19617                && vector_all_ones_operand (negop, data_mode))
19618         {
19619           rtx res = expand_simple_binop (mode, ASHIFTRT, cop0, GEN_INT (shift),
19620                                          operands[0], 0, OPTAB_DIRECT);
19621           if (res != operands[0])
19622             emit_move_insn (operands[0], res);
19623           return true;
19624         }
19625     }
19626
19627   if (!nonimmediate_operand (cop1, mode))
19628     cop1 = force_reg (mode, cop1);
19629   if (!general_operand (operands[1], data_mode))
19630     operands[1] = force_reg (data_mode, operands[1]);
19631   if (!general_operand (operands[2], data_mode))
19632     operands[2] = force_reg (data_mode, operands[2]);
19633
19634   /* XOP supports all of the comparisons on all 128-bit vector int types.  */
19635   if (TARGET_XOP
19636       && (mode == V16QImode || mode == V8HImode
19637           || mode == V4SImode || mode == V2DImode))
19638     ;
19639   else
19640     {
19641       /* Canonicalize the comparison to EQ, GT, GTU.  */
19642       switch (code)
19643         {
19644         case EQ:
19645         case GT:
19646         case GTU:
19647           break;
19648
19649         case NE:
19650         case LE:
19651         case LEU:
19652           code = reverse_condition (code);
19653           negate = true;
19654           break;
19655
19656         case GE:
19657         case GEU:
19658           code = reverse_condition (code);
19659           negate = true;
19660           /* FALLTHRU */
19661
19662         case LT:
19663         case LTU:
19664           code = swap_condition (code);
19665           x = cop0, cop0 = cop1, cop1 = x;
19666           break;
19667
19668         default:
19669           gcc_unreachable ();
19670         }
19671
19672       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
19673       if (mode == V2DImode)
19674         {
19675           switch (code)
19676             {
19677             case EQ:
19678               /* SSE4.1 supports EQ.  */
19679               if (!TARGET_SSE4_1)
19680                 return false;
19681               break;
19682
19683             case GT:
19684             case GTU:
19685               /* SSE4.2 supports GT/GTU.  */
19686               if (!TARGET_SSE4_2)
19687                 return false;
19688               break;
19689
19690             default:
19691               gcc_unreachable ();
19692             }
19693         }
19694
19695       /* Unsigned parallel compare is not supported by the hardware.
19696          Play some tricks to turn this into a signed comparison
19697          against 0.  */
19698       if (code == GTU)
19699         {
19700           cop0 = force_reg (mode, cop0);
19701
19702           switch (mode)
19703             {
19704             case V8SImode:
19705             case V4DImode:
19706             case V4SImode:
19707             case V2DImode:
19708                 {
19709                   rtx t1, t2, mask;
19710                   rtx (*gen_sub3) (rtx, rtx, rtx);
19711
19712                   switch (mode)
19713                     {
19714                     case V8SImode: gen_sub3 = gen_subv8si3; break;
19715                     case V4DImode: gen_sub3 = gen_subv4di3; break;
19716                     case V4SImode: gen_sub3 = gen_subv4si3; break;
19717                     case V2DImode: gen_sub3 = gen_subv2di3; break;
19718                     default:
19719                       gcc_unreachable ();
19720                     }
19721                   /* Subtract (-(INT MAX) - 1) from both operands to make
19722                      them signed.  */
19723                   mask = ix86_build_signbit_mask (mode, true, false);
19724                   t1 = gen_reg_rtx (mode);
19725                   emit_insn (gen_sub3 (t1, cop0, mask));
19726
19727                   t2 = gen_reg_rtx (mode);
19728                   emit_insn (gen_sub3 (t2, cop1, mask));
19729
19730                   cop0 = t1;
19731                   cop1 = t2;
19732                   code = GT;
19733                 }
19734               break;
19735
19736             case V32QImode:
19737             case V16HImode:
19738             case V16QImode:
19739             case V8HImode:
19740               /* Perform a parallel unsigned saturating subtraction.  */
19741               x = gen_reg_rtx (mode);
19742               emit_insn (gen_rtx_SET (VOIDmode, x,
19743                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
19744
19745               cop0 = x;
19746               cop1 = CONST0_RTX (mode);
19747               code = EQ;
19748               negate = !negate;
19749               break;
19750
19751             default:
19752               gcc_unreachable ();
19753             }
19754         }
19755     }
19756
19757   /* Allow the comparison to be done in one mode, but the movcc to
19758      happen in another mode.  */
19759   if (data_mode == mode)
19760     {
19761       x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
19762                                operands[1+negate], operands[2-negate]);
19763     }
19764   else
19765     {
19766       gcc_assert (GET_MODE_SIZE (data_mode) == GET_MODE_SIZE (mode));
19767       x = ix86_expand_sse_cmp (gen_lowpart (mode, operands[0]),
19768                                code, cop0, cop1,
19769                                operands[1+negate], operands[2-negate]);
19770       x = gen_lowpart (data_mode, x);
19771     }
19772
19773   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
19774                          operands[2-negate]);
19775   return true;
19776 }
19777
19778 /* Expand a variable vector permutation.  */
19779
19780 void
19781 ix86_expand_vec_perm (rtx operands[])
19782 {
19783   rtx target = operands[0];
19784   rtx op0 = operands[1];
19785   rtx op1 = operands[2];
19786   rtx mask = operands[3];
19787   rtx t1, t2, t3, t4, vt, vt2, vec[32];
19788   enum machine_mode mode = GET_MODE (op0);
19789   enum machine_mode maskmode = GET_MODE (mask);
19790   int w, e, i;
19791   bool one_operand_shuffle = rtx_equal_p (op0, op1);
19792
19793   /* Number of elements in the vector.  */
19794   w = GET_MODE_NUNITS (mode);
19795   e = GET_MODE_UNIT_SIZE (mode);
19796   gcc_assert (w <= 32);
19797
19798   if (TARGET_AVX2)
19799     {
19800       if (mode == V4DImode || mode == V4DFmode || mode == V16HImode)
19801         {
19802           /* Unfortunately, the VPERMQ and VPERMPD instructions only support
19803              an constant shuffle operand.  With a tiny bit of effort we can
19804              use VPERMD instead.  A re-interpretation stall for V4DFmode is
19805              unfortunate but there's no avoiding it.
19806              Similarly for V16HImode we don't have instructions for variable
19807              shuffling, while for V32QImode we can use after preparing suitable
19808              masks vpshufb; vpshufb; vpermq; vpor.  */
19809
19810           if (mode == V16HImode)
19811             {
19812               maskmode = mode = V32QImode;
19813               w = 32;
19814               e = 1;
19815             }
19816           else
19817             {
19818               maskmode = mode = V8SImode;
19819               w = 8;
19820               e = 4;
19821             }
19822           t1 = gen_reg_rtx (maskmode);
19823
19824           /* Replicate the low bits of the V4DImode mask into V8SImode:
19825                mask = { A B C D }
19826                t1 = { A A B B C C D D }.  */
19827           for (i = 0; i < w / 2; ++i)
19828             vec[i*2 + 1] = vec[i*2] = GEN_INT (i * 2);
19829           vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
19830           vt = force_reg (maskmode, vt);
19831           mask = gen_lowpart (maskmode, mask);
19832           if (maskmode == V8SImode)
19833             emit_insn (gen_avx2_permvarv8si (t1, mask, vt));
19834           else
19835             emit_insn (gen_avx2_pshufbv32qi3 (t1, mask, vt));
19836
19837           /* Multiply the shuffle indicies by two.  */
19838           t1 = expand_simple_binop (maskmode, PLUS, t1, t1, t1, 1,
19839                                     OPTAB_DIRECT);
19840
19841           /* Add one to the odd shuffle indicies:
19842                 t1 = { A*2, A*2+1, B*2, B*2+1, ... }.  */
19843           for (i = 0; i < w / 2; ++i)
19844             {
19845               vec[i * 2] = const0_rtx;
19846               vec[i * 2 + 1] = const1_rtx;
19847             }
19848           vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
19849           vt = force_const_mem (maskmode, vt);
19850           t1 = expand_simple_binop (maskmode, PLUS, t1, vt, t1, 1,
19851                                     OPTAB_DIRECT);
19852
19853           /* Continue as if V8SImode (resp. V32QImode) was used initially.  */
19854           operands[3] = mask = t1;
19855           target = gen_lowpart (mode, target);
19856           op0 = gen_lowpart (mode, op0);
19857           op1 = gen_lowpart (mode, op1);
19858         }
19859
19860       switch (mode)
19861         {
19862         case V8SImode:
19863           /* The VPERMD and VPERMPS instructions already properly ignore
19864              the high bits of the shuffle elements.  No need for us to
19865              perform an AND ourselves.  */
19866           if (one_operand_shuffle)
19867             emit_insn (gen_avx2_permvarv8si (target, op0, mask));
19868           else
19869             {
19870               t1 = gen_reg_rtx (V8SImode);
19871               t2 = gen_reg_rtx (V8SImode);
19872               emit_insn (gen_avx2_permvarv8si (t1, op0, mask));
19873               emit_insn (gen_avx2_permvarv8si (t2, op1, mask));
19874               goto merge_two;
19875             }
19876           return;
19877
19878         case V8SFmode:
19879           mask = gen_lowpart (V8SFmode, mask);
19880           if (one_operand_shuffle)
19881             emit_insn (gen_avx2_permvarv8sf (target, op0, mask));
19882           else
19883             {
19884               t1 = gen_reg_rtx (V8SFmode);
19885               t2 = gen_reg_rtx (V8SFmode);
19886               emit_insn (gen_avx2_permvarv8sf (t1, op0, mask));
19887               emit_insn (gen_avx2_permvarv8sf (t2, op1, mask));
19888               goto merge_two;
19889             }
19890           return;
19891
19892         case V4SImode:
19893           /* By combining the two 128-bit input vectors into one 256-bit
19894              input vector, we can use VPERMD and VPERMPS for the full
19895              two-operand shuffle.  */
19896           t1 = gen_reg_rtx (V8SImode);
19897           t2 = gen_reg_rtx (V8SImode);
19898           emit_insn (gen_avx_vec_concatv8si (t1, op0, op1));
19899           emit_insn (gen_avx_vec_concatv8si (t2, mask, mask));
19900           emit_insn (gen_avx2_permvarv8si (t1, t1, t2));
19901           emit_insn (gen_avx_vextractf128v8si (target, t1, const0_rtx));
19902           return;
19903
19904         case V4SFmode:
19905           t1 = gen_reg_rtx (V8SFmode);
19906           t2 = gen_reg_rtx (V8SImode);
19907           mask = gen_lowpart (V4SImode, mask);
19908           emit_insn (gen_avx_vec_concatv8sf (t1, op0, op1));
19909           emit_insn (gen_avx_vec_concatv8si (t2, mask, mask));
19910           emit_insn (gen_avx2_permvarv8sf (t1, t1, t2));
19911           emit_insn (gen_avx_vextractf128v8sf (target, t1, const0_rtx));
19912           return;
19913
19914         case V32QImode:
19915           t1 = gen_reg_rtx (V32QImode);
19916           t2 = gen_reg_rtx (V32QImode);
19917           t3 = gen_reg_rtx (V32QImode);
19918           vt2 = GEN_INT (128);
19919           for (i = 0; i < 32; i++)
19920             vec[i] = vt2;
19921           vt = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, vec));
19922           vt = force_reg (V32QImode, vt);
19923           for (i = 0; i < 32; i++)
19924             vec[i] = i < 16 ? vt2 : const0_rtx;
19925           vt2 = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, vec));
19926           vt2 = force_reg (V32QImode, vt2);
19927           /* From mask create two adjusted masks, which contain the same
19928              bits as mask in the low 7 bits of each vector element.
19929              The first mask will have the most significant bit clear
19930              if it requests element from the same 128-bit lane
19931              and MSB set if it requests element from the other 128-bit lane.
19932              The second mask will have the opposite values of the MSB,
19933              and additionally will have its 128-bit lanes swapped.
19934              E.g. { 07 12 1e 09 ... | 17 19 05 1f ... } mask vector will have
19935              t1   { 07 92 9e 09 ... | 17 19 85 1f ... } and
19936              t3   { 97 99 05 9f ... | 87 12 1e 89 ... } where each ...
19937              stands for other 12 bytes.  */
19938           /* The bit whether element is from the same lane or the other
19939              lane is bit 4, so shift it up by 3 to the MSB position.  */
19940           emit_insn (gen_ashlv4di3 (gen_lowpart (V4DImode, t1),
19941                                     gen_lowpart (V4DImode, mask),
19942                                     GEN_INT (3)));
19943           /* Clear MSB bits from the mask just in case it had them set.  */
19944           emit_insn (gen_avx2_andnotv32qi3 (t2, vt, mask));
19945           /* After this t1 will have MSB set for elements from other lane.  */
19946           emit_insn (gen_xorv32qi3 (t1, t1, vt2));
19947           /* Clear bits other than MSB.  */
19948           emit_insn (gen_andv32qi3 (t1, t1, vt));
19949           /* Or in the lower bits from mask into t3.  */
19950           emit_insn (gen_iorv32qi3 (t3, t1, t2));
19951           /* And invert MSB bits in t1, so MSB is set for elements from the same
19952              lane.  */
19953           emit_insn (gen_xorv32qi3 (t1, t1, vt));
19954           /* Swap 128-bit lanes in t3.  */
19955           emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t3),
19956                                           gen_lowpart (V4DImode, t3),
19957                                           const2_rtx, GEN_INT (3),
19958                                           const0_rtx, const1_rtx));
19959           /* And or in the lower bits from mask into t1.  */
19960           emit_insn (gen_iorv32qi3 (t1, t1, t2));
19961           if (one_operand_shuffle)
19962             {
19963               /* Each of these shuffles will put 0s in places where
19964                  element from the other 128-bit lane is needed, otherwise
19965                  will shuffle in the requested value.  */
19966               emit_insn (gen_avx2_pshufbv32qi3 (t3, op0, t3));
19967               emit_insn (gen_avx2_pshufbv32qi3 (t1, op0, t1));
19968               /* For t3 the 128-bit lanes are swapped again.  */
19969               emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t3),
19970                                               gen_lowpart (V4DImode, t3),
19971                                               const2_rtx, GEN_INT (3),
19972                                               const0_rtx, const1_rtx));
19973               /* And oring both together leads to the result.  */
19974               emit_insn (gen_iorv32qi3 (target, t1, t3));
19975               return;
19976             }
19977
19978           t4 = gen_reg_rtx (V32QImode);
19979           /* Similarly to the above one_operand_shuffle code,
19980              just for repeated twice for each operand.  merge_two:
19981              code will merge the two results together.  */
19982           emit_insn (gen_avx2_pshufbv32qi3 (t4, op0, t3));
19983           emit_insn (gen_avx2_pshufbv32qi3 (t3, op1, t3));
19984           emit_insn (gen_avx2_pshufbv32qi3 (t2, op0, t1));
19985           emit_insn (gen_avx2_pshufbv32qi3 (t1, op1, t1));
19986           emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t4),
19987                                           gen_lowpart (V4DImode, t4),
19988                                           const2_rtx, GEN_INT (3),
19989                                           const0_rtx, const1_rtx));
19990           emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t3),
19991                                           gen_lowpart (V4DImode, t3),
19992                                           const2_rtx, GEN_INT (3),
19993                                           const0_rtx, const1_rtx));
19994           emit_insn (gen_iorv32qi3 (t4, t2, t4));
19995           emit_insn (gen_iorv32qi3 (t3, t1, t3));
19996           t1 = t4;
19997           t2 = t3;
19998           goto merge_two;
19999
20000         default:
20001           gcc_assert (GET_MODE_SIZE (mode) <= 16);
20002           break;
20003         }
20004     }
20005
20006   if (TARGET_XOP)
20007     {
20008       /* The XOP VPPERM insn supports three inputs.  By ignoring the 
20009          one_operand_shuffle special case, we avoid creating another
20010          set of constant vectors in memory.  */
20011       one_operand_shuffle = false;
20012
20013       /* mask = mask & {2*w-1, ...} */
20014       vt = GEN_INT (2*w - 1);
20015     }
20016   else
20017     {
20018       /* mask = mask & {w-1, ...} */
20019       vt = GEN_INT (w - 1);
20020     }
20021
20022   for (i = 0; i < w; i++)
20023     vec[i] = vt;
20024   vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
20025   mask = expand_simple_binop (maskmode, AND, mask, vt,
20026                               NULL_RTX, 0, OPTAB_DIRECT);
20027
20028   /* For non-QImode operations, convert the word permutation control
20029      into a byte permutation control.  */
20030   if (mode != V16QImode)
20031     {
20032       mask = expand_simple_binop (maskmode, ASHIFT, mask,
20033                                   GEN_INT (exact_log2 (e)),
20034                                   NULL_RTX, 0, OPTAB_DIRECT);
20035
20036       /* Convert mask to vector of chars.  */
20037       mask = force_reg (V16QImode, gen_lowpart (V16QImode, mask));
20038
20039       /* Replicate each of the input bytes into byte positions:
20040          (v2di) --> {0,0,0,0,0,0,0,0, 8,8,8,8,8,8,8,8}
20041          (v4si) --> {0,0,0,0, 4,4,4,4, 8,8,8,8, 12,12,12,12}
20042          (v8hi) --> {0,0, 2,2, 4,4, 6,6, ...}.  */
20043       for (i = 0; i < 16; ++i)
20044         vec[i] = GEN_INT (i/e * e);
20045       vt = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, vec));
20046       vt = force_const_mem (V16QImode, vt);
20047       if (TARGET_XOP)
20048         emit_insn (gen_xop_pperm (mask, mask, mask, vt));
20049       else
20050         emit_insn (gen_ssse3_pshufbv16qi3 (mask, mask, vt));
20051
20052       /* Convert it into the byte positions by doing
20053          mask = mask + {0,1,..,16/w, 0,1,..,16/w, ...}  */
20054       for (i = 0; i < 16; ++i)
20055         vec[i] = GEN_INT (i % e);
20056       vt = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, vec));
20057       vt = force_const_mem (V16QImode, vt);
20058       emit_insn (gen_addv16qi3 (mask, mask, vt));
20059     }
20060
20061   /* The actual shuffle operations all operate on V16QImode.  */
20062   op0 = gen_lowpart (V16QImode, op0);
20063   op1 = gen_lowpart (V16QImode, op1);
20064   target = gen_lowpart (V16QImode, target);
20065
20066   if (TARGET_XOP)
20067     {
20068       emit_insn (gen_xop_pperm (target, op0, op1, mask));
20069     }
20070   else if (one_operand_shuffle)
20071     {
20072       emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, mask));
20073     }
20074   else
20075     {
20076       rtx xops[6];
20077       bool ok;
20078
20079       /* Shuffle the two input vectors independently.  */
20080       t1 = gen_reg_rtx (V16QImode);
20081       t2 = gen_reg_rtx (V16QImode);
20082       emit_insn (gen_ssse3_pshufbv16qi3 (t1, op0, mask));
20083       emit_insn (gen_ssse3_pshufbv16qi3 (t2, op1, mask));
20084
20085  merge_two:
20086       /* Then merge them together.  The key is whether any given control
20087          element contained a bit set that indicates the second word.  */
20088       mask = operands[3];
20089       vt = GEN_INT (w);
20090       if (maskmode == V2DImode && !TARGET_SSE4_1)
20091         {
20092           /* Without SSE4.1, we don't have V2DImode EQ.  Perform one
20093              more shuffle to convert the V2DI input mask into a V4SI
20094              input mask.  At which point the masking that expand_int_vcond
20095              will work as desired.  */
20096           rtx t3 = gen_reg_rtx (V4SImode);
20097           emit_insn (gen_sse2_pshufd_1 (t3, gen_lowpart (V4SImode, mask),
20098                                         const0_rtx, const0_rtx,
20099                                         const2_rtx, const2_rtx));
20100           mask = t3;
20101           maskmode = V4SImode;
20102           e = w = 4;
20103         }
20104
20105       for (i = 0; i < w; i++)
20106         vec[i] = vt;
20107       vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
20108       vt = force_reg (maskmode, vt);
20109       mask = expand_simple_binop (maskmode, AND, mask, vt,
20110                                   NULL_RTX, 0, OPTAB_DIRECT);
20111
20112       xops[0] = gen_lowpart (mode, operands[0]);
20113       xops[1] = gen_lowpart (mode, t2);
20114       xops[2] = gen_lowpart (mode, t1);
20115       xops[3] = gen_rtx_EQ (maskmode, mask, vt);
20116       xops[4] = mask;
20117       xops[5] = vt;
20118       ok = ix86_expand_int_vcond (xops);
20119       gcc_assert (ok);
20120     }
20121 }
20122
20123 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
20124    true if we should do zero extension, else sign extension.  HIGH_P is
20125    true if we want the N/2 high elements, else the low elements.  */
20126
20127 void
20128 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
20129 {
20130   enum machine_mode imode = GET_MODE (operands[1]);
20131   rtx tmp, dest;
20132
20133   if (TARGET_SSE4_1)
20134     {
20135       rtx (*unpack)(rtx, rtx);
20136       rtx (*extract)(rtx, rtx) = NULL;
20137       enum machine_mode halfmode = BLKmode;
20138
20139       switch (imode)
20140         {
20141         case V32QImode:
20142           if (unsigned_p)
20143             unpack = gen_avx2_zero_extendv16qiv16hi2;
20144           else
20145             unpack = gen_avx2_sign_extendv16qiv16hi2;
20146           halfmode = V16QImode;
20147           extract
20148             = high_p ? gen_vec_extract_hi_v32qi : gen_vec_extract_lo_v32qi;
20149           break;
20150         case V16HImode:
20151           if (unsigned_p)
20152             unpack = gen_avx2_zero_extendv8hiv8si2;
20153           else
20154             unpack = gen_avx2_sign_extendv8hiv8si2;
20155           halfmode = V8HImode;
20156           extract
20157             = high_p ? gen_vec_extract_hi_v16hi : gen_vec_extract_lo_v16hi;
20158           break;
20159         case V8SImode:
20160           if (unsigned_p)
20161             unpack = gen_avx2_zero_extendv4siv4di2;
20162           else
20163             unpack = gen_avx2_sign_extendv4siv4di2;
20164           halfmode = V4SImode;
20165           extract
20166             = high_p ? gen_vec_extract_hi_v8si : gen_vec_extract_lo_v8si;
20167           break;
20168         case V16QImode:
20169           if (unsigned_p)
20170             unpack = gen_sse4_1_zero_extendv8qiv8hi2;
20171           else
20172             unpack = gen_sse4_1_sign_extendv8qiv8hi2;
20173           break;
20174         case V8HImode:
20175           if (unsigned_p)
20176             unpack = gen_sse4_1_zero_extendv4hiv4si2;
20177           else
20178             unpack = gen_sse4_1_sign_extendv4hiv4si2;
20179           break;
20180         case V4SImode:
20181           if (unsigned_p)
20182             unpack = gen_sse4_1_zero_extendv2siv2di2;
20183           else
20184             unpack = gen_sse4_1_sign_extendv2siv2di2;
20185           break;
20186         default:
20187           gcc_unreachable ();
20188         }
20189
20190       if (GET_MODE_SIZE (imode) == 32)
20191         {
20192           tmp = gen_reg_rtx (halfmode);
20193           emit_insn (extract (tmp, operands[1]));
20194         }
20195       else if (high_p)
20196         {
20197           /* Shift higher 8 bytes to lower 8 bytes.  */
20198           tmp = gen_reg_rtx (imode);
20199           emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, tmp),
20200                                          gen_lowpart (V1TImode, operands[1]),
20201                                          GEN_INT (64)));
20202         }
20203       else
20204         tmp = operands[1];
20205
20206       emit_insn (unpack (operands[0], tmp));
20207     }
20208   else
20209     {
20210       rtx (*unpack)(rtx, rtx, rtx);
20211
20212       switch (imode)
20213         {
20214         case V16QImode:
20215           if (high_p)
20216             unpack = gen_vec_interleave_highv16qi;
20217           else
20218             unpack = gen_vec_interleave_lowv16qi;
20219           break;
20220         case V8HImode:
20221           if (high_p)
20222             unpack = gen_vec_interleave_highv8hi;
20223           else
20224             unpack = gen_vec_interleave_lowv8hi;
20225           break;
20226         case V4SImode:
20227           if (high_p)
20228             unpack = gen_vec_interleave_highv4si;
20229           else
20230             unpack = gen_vec_interleave_lowv4si;
20231           break;
20232         default:
20233           gcc_unreachable ();
20234         }
20235
20236       dest = gen_lowpart (imode, operands[0]);
20237
20238       if (unsigned_p)
20239         tmp = force_reg (imode, CONST0_RTX (imode));
20240       else
20241         tmp = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
20242                                    operands[1], pc_rtx, pc_rtx);
20243
20244       emit_insn (unpack (dest, operands[1], tmp));
20245     }
20246 }
20247
20248 /* Expand conditional increment or decrement using adb/sbb instructions.
20249    The default case using setcc followed by the conditional move can be
20250    done by generic code.  */
20251 bool
20252 ix86_expand_int_addcc (rtx operands[])
20253 {
20254   enum rtx_code code = GET_CODE (operands[1]);
20255   rtx flags;
20256   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
20257   rtx compare_op;
20258   rtx val = const0_rtx;
20259   bool fpcmp = false;
20260   enum machine_mode mode;
20261   rtx op0 = XEXP (operands[1], 0);
20262   rtx op1 = XEXP (operands[1], 1);
20263
20264   if (operands[3] != const1_rtx
20265       && operands[3] != constm1_rtx)
20266     return false;
20267   if (!ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
20268      return false;
20269   code = GET_CODE (compare_op);
20270
20271   flags = XEXP (compare_op, 0);
20272
20273   if (GET_MODE (flags) == CCFPmode
20274       || GET_MODE (flags) == CCFPUmode)
20275     {
20276       fpcmp = true;
20277       code = ix86_fp_compare_code_to_integer (code);
20278     }
20279
20280   if (code != LTU)
20281     {
20282       val = constm1_rtx;
20283       if (fpcmp)
20284         PUT_CODE (compare_op,
20285                   reverse_condition_maybe_unordered
20286                     (GET_CODE (compare_op)));
20287       else
20288         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
20289     }
20290
20291   mode = GET_MODE (operands[0]);
20292
20293   /* Construct either adc or sbb insn.  */
20294   if ((code == LTU) == (operands[3] == constm1_rtx))
20295     {
20296       switch (mode)
20297         {
20298           case QImode:
20299             insn = gen_subqi3_carry;
20300             break;
20301           case HImode:
20302             insn = gen_subhi3_carry;
20303             break;
20304           case SImode:
20305             insn = gen_subsi3_carry;
20306             break;
20307           case DImode:
20308             insn = gen_subdi3_carry;
20309             break;
20310           default:
20311             gcc_unreachable ();
20312         }
20313     }
20314   else
20315     {
20316       switch (mode)
20317         {
20318           case QImode:
20319             insn = gen_addqi3_carry;
20320             break;
20321           case HImode:
20322             insn = gen_addhi3_carry;
20323             break;
20324           case SImode:
20325             insn = gen_addsi3_carry;
20326             break;
20327           case DImode:
20328             insn = gen_adddi3_carry;
20329             break;
20330           default:
20331             gcc_unreachable ();
20332         }
20333     }
20334   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
20335
20336   return true;
20337 }
20338
20339
20340 /* Split operands 0 and 1 into half-mode parts.  Similar to split_double_mode,
20341    but works for floating pointer parameters and nonoffsetable memories.
20342    For pushes, it returns just stack offsets; the values will be saved
20343    in the right order.  Maximally three parts are generated.  */
20344
20345 static int
20346 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
20347 {
20348   int size;
20349
20350   if (!TARGET_64BIT)
20351     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
20352   else
20353     size = (GET_MODE_SIZE (mode) + 4) / 8;
20354
20355   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
20356   gcc_assert (size >= 2 && size <= 4);
20357
20358   /* Optimize constant pool reference to immediates.  This is used by fp
20359      moves, that force all constants to memory to allow combining.  */
20360   if (MEM_P (operand) && MEM_READONLY_P (operand))
20361     {
20362       rtx tmp = maybe_get_pool_constant (operand);
20363       if (tmp)
20364         operand = tmp;
20365     }
20366
20367   if (MEM_P (operand) && !offsettable_memref_p (operand))
20368     {
20369       /* The only non-offsetable memories we handle are pushes.  */
20370       int ok = push_operand (operand, VOIDmode);
20371
20372       gcc_assert (ok);
20373
20374       operand = copy_rtx (operand);
20375       PUT_MODE (operand, Pmode);
20376       parts[0] = parts[1] = parts[2] = parts[3] = operand;
20377       return size;
20378     }
20379
20380   if (GET_CODE (operand) == CONST_VECTOR)
20381     {
20382       enum machine_mode imode = int_mode_for_mode (mode);
20383       /* Caution: if we looked through a constant pool memory above,
20384          the operand may actually have a different mode now.  That's
20385          ok, since we want to pun this all the way back to an integer.  */
20386       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
20387       gcc_assert (operand != NULL);
20388       mode = imode;
20389     }
20390
20391   if (!TARGET_64BIT)
20392     {
20393       if (mode == DImode)
20394         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
20395       else
20396         {
20397           int i;
20398
20399           if (REG_P (operand))
20400             {
20401               gcc_assert (reload_completed);
20402               for (i = 0; i < size; i++)
20403                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
20404             }
20405           else if (offsettable_memref_p (operand))
20406             {
20407               operand = adjust_address (operand, SImode, 0);
20408               parts[0] = operand;
20409               for (i = 1; i < size; i++)
20410                 parts[i] = adjust_address (operand, SImode, 4 * i);
20411             }
20412           else if (GET_CODE (operand) == CONST_DOUBLE)
20413             {
20414               REAL_VALUE_TYPE r;
20415               long l[4];
20416
20417               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
20418               switch (mode)
20419                 {
20420                 case TFmode:
20421                   real_to_target (l, &r, mode);
20422                   parts[3] = gen_int_mode (l[3], SImode);
20423                   parts[2] = gen_int_mode (l[2], SImode);
20424                   break;
20425                 case XFmode:
20426                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
20427                   parts[2] = gen_int_mode (l[2], SImode);
20428                   break;
20429                 case DFmode:
20430                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
20431                   break;
20432                 default:
20433                   gcc_unreachable ();
20434                 }
20435               parts[1] = gen_int_mode (l[1], SImode);
20436               parts[0] = gen_int_mode (l[0], SImode);
20437             }
20438           else
20439             gcc_unreachable ();
20440         }
20441     }
20442   else
20443     {
20444       if (mode == TImode)
20445         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
20446       if (mode == XFmode || mode == TFmode)
20447         {
20448           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
20449           if (REG_P (operand))
20450             {
20451               gcc_assert (reload_completed);
20452               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
20453               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
20454             }
20455           else if (offsettable_memref_p (operand))
20456             {
20457               operand = adjust_address (operand, DImode, 0);
20458               parts[0] = operand;
20459               parts[1] = adjust_address (operand, upper_mode, 8);
20460             }
20461           else if (GET_CODE (operand) == CONST_DOUBLE)
20462             {
20463               REAL_VALUE_TYPE r;
20464               long l[4];
20465
20466               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
20467               real_to_target (l, &r, mode);
20468
20469               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
20470               if (HOST_BITS_PER_WIDE_INT >= 64)
20471                 parts[0]
20472                   = gen_int_mode
20473                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
20474                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
20475                        DImode);
20476               else
20477                 parts[0] = immed_double_const (l[0], l[1], DImode);
20478
20479               if (upper_mode == SImode)
20480                 parts[1] = gen_int_mode (l[2], SImode);
20481               else if (HOST_BITS_PER_WIDE_INT >= 64)
20482                 parts[1]
20483                   = gen_int_mode
20484                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
20485                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
20486                        DImode);
20487               else
20488                 parts[1] = immed_double_const (l[2], l[3], DImode);
20489             }
20490           else
20491             gcc_unreachable ();
20492         }
20493     }
20494
20495   return size;
20496 }
20497
20498 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
20499    Return false when normal moves are needed; true when all required
20500    insns have been emitted.  Operands 2-4 contain the input values
20501    int the correct order; operands 5-7 contain the output values.  */
20502
20503 void
20504 ix86_split_long_move (rtx operands[])
20505 {
20506   rtx part[2][4];
20507   int nparts, i, j;
20508   int push = 0;
20509   int collisions = 0;
20510   enum machine_mode mode = GET_MODE (operands[0]);
20511   bool collisionparts[4];
20512
20513   /* The DFmode expanders may ask us to move double.
20514      For 64bit target this is single move.  By hiding the fact
20515      here we simplify i386.md splitters.  */
20516   if (TARGET_64BIT && GET_MODE_SIZE (GET_MODE (operands[0])) == 8)
20517     {
20518       /* Optimize constant pool reference to immediates.  This is used by
20519          fp moves, that force all constants to memory to allow combining.  */
20520
20521       if (MEM_P (operands[1])
20522           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
20523           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
20524         operands[1] = get_pool_constant (XEXP (operands[1], 0));
20525       if (push_operand (operands[0], VOIDmode))
20526         {
20527           operands[0] = copy_rtx (operands[0]);
20528           PUT_MODE (operands[0], Pmode);
20529         }
20530       else
20531         operands[0] = gen_lowpart (DImode, operands[0]);
20532       operands[1] = gen_lowpart (DImode, operands[1]);
20533       emit_move_insn (operands[0], operands[1]);
20534       return;
20535     }
20536
20537   /* The only non-offsettable memory we handle is push.  */
20538   if (push_operand (operands[0], VOIDmode))
20539     push = 1;
20540   else
20541     gcc_assert (!MEM_P (operands[0])
20542                 || offsettable_memref_p (operands[0]));
20543
20544   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
20545   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
20546
20547   /* When emitting push, take care for source operands on the stack.  */
20548   if (push && MEM_P (operands[1])
20549       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
20550     {
20551       rtx src_base = XEXP (part[1][nparts - 1], 0);
20552
20553       /* Compensate for the stack decrement by 4.  */
20554       if (!TARGET_64BIT && nparts == 3
20555           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
20556         src_base = plus_constant (src_base, 4);
20557
20558       /* src_base refers to the stack pointer and is
20559          automatically decreased by emitted push.  */
20560       for (i = 0; i < nparts; i++)
20561         part[1][i] = change_address (part[1][i],
20562                                      GET_MODE (part[1][i]), src_base);
20563     }
20564
20565   /* We need to do copy in the right order in case an address register
20566      of the source overlaps the destination.  */
20567   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
20568     {
20569       rtx tmp;
20570
20571       for (i = 0; i < nparts; i++)
20572         {
20573           collisionparts[i]
20574             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
20575           if (collisionparts[i])
20576             collisions++;
20577         }
20578
20579       /* Collision in the middle part can be handled by reordering.  */
20580       if (collisions == 1 && nparts == 3 && collisionparts [1])
20581         {
20582           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
20583           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
20584         }
20585       else if (collisions == 1
20586                && nparts == 4
20587                && (collisionparts [1] || collisionparts [2]))
20588         {
20589           if (collisionparts [1])
20590             {
20591               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
20592               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
20593             }
20594           else
20595             {
20596               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
20597               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
20598             }
20599         }
20600
20601       /* If there are more collisions, we can't handle it by reordering.
20602          Do an lea to the last part and use only one colliding move.  */
20603       else if (collisions > 1)
20604         {
20605           rtx base;
20606
20607           collisions = 1;
20608
20609           base = part[0][nparts - 1];
20610
20611           /* Handle the case when the last part isn't valid for lea.
20612              Happens in 64-bit mode storing the 12-byte XFmode.  */
20613           if (GET_MODE (base) != Pmode)
20614             base = gen_rtx_REG (Pmode, REGNO (base));
20615
20616           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
20617           part[1][0] = replace_equiv_address (part[1][0], base);
20618           for (i = 1; i < nparts; i++)
20619             {
20620               tmp = plus_constant (base, UNITS_PER_WORD * i);
20621               part[1][i] = replace_equiv_address (part[1][i], tmp);
20622             }
20623         }
20624     }
20625
20626   if (push)
20627     {
20628       if (!TARGET_64BIT)
20629         {
20630           if (nparts == 3)
20631             {
20632               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
20633                 emit_insn (gen_addsi3 (stack_pointer_rtx,
20634                                        stack_pointer_rtx, GEN_INT (-4)));
20635               emit_move_insn (part[0][2], part[1][2]);
20636             }
20637           else if (nparts == 4)
20638             {
20639               emit_move_insn (part[0][3], part[1][3]);
20640               emit_move_insn (part[0][2], part[1][2]);
20641             }
20642         }
20643       else
20644         {
20645           /* In 64bit mode we don't have 32bit push available.  In case this is
20646              register, it is OK - we will just use larger counterpart.  We also
20647              retype memory - these comes from attempt to avoid REX prefix on
20648              moving of second half of TFmode value.  */
20649           if (GET_MODE (part[1][1]) == SImode)
20650             {
20651               switch (GET_CODE (part[1][1]))
20652                 {
20653                 case MEM:
20654                   part[1][1] = adjust_address (part[1][1], DImode, 0);
20655                   break;
20656
20657                 case REG:
20658                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
20659                   break;
20660
20661                 default:
20662                   gcc_unreachable ();
20663                 }
20664
20665               if (GET_MODE (part[1][0]) == SImode)
20666                 part[1][0] = part[1][1];
20667             }
20668         }
20669       emit_move_insn (part[0][1], part[1][1]);
20670       emit_move_insn (part[0][0], part[1][0]);
20671       return;
20672     }
20673
20674   /* Choose correct order to not overwrite the source before it is copied.  */
20675   if ((REG_P (part[0][0])
20676        && REG_P (part[1][1])
20677        && (REGNO (part[0][0]) == REGNO (part[1][1])
20678            || (nparts == 3
20679                && REGNO (part[0][0]) == REGNO (part[1][2]))
20680            || (nparts == 4
20681                && REGNO (part[0][0]) == REGNO (part[1][3]))))
20682       || (collisions > 0
20683           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
20684     {
20685       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
20686         {
20687           operands[2 + i] = part[0][j];
20688           operands[6 + i] = part[1][j];
20689         }
20690     }
20691   else
20692     {
20693       for (i = 0; i < nparts; i++)
20694         {
20695           operands[2 + i] = part[0][i];
20696           operands[6 + i] = part[1][i];
20697         }
20698     }
20699
20700   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
20701   if (optimize_insn_for_size_p ())
20702     {
20703       for (j = 0; j < nparts - 1; j++)
20704         if (CONST_INT_P (operands[6 + j])
20705             && operands[6 + j] != const0_rtx
20706             && REG_P (operands[2 + j]))
20707           for (i = j; i < nparts - 1; i++)
20708             if (CONST_INT_P (operands[7 + i])
20709                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
20710               operands[7 + i] = operands[2 + j];
20711     }
20712
20713   for (i = 0; i < nparts; i++)
20714     emit_move_insn (operands[2 + i], operands[6 + i]);
20715
20716   return;
20717 }
20718
20719 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
20720    left shift by a constant, either using a single shift or
20721    a sequence of add instructions.  */
20722
20723 static void
20724 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
20725 {
20726   rtx (*insn)(rtx, rtx, rtx);
20727
20728   if (count == 1
20729       || (count * ix86_cost->add <= ix86_cost->shift_const
20730           && !optimize_insn_for_size_p ()))
20731     {
20732       insn = mode == DImode ? gen_addsi3 : gen_adddi3;
20733       while (count-- > 0)
20734         emit_insn (insn (operand, operand, operand));
20735     }
20736   else
20737     {
20738       insn = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
20739       emit_insn (insn (operand, operand, GEN_INT (count)));
20740     }
20741 }
20742
20743 void
20744 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
20745 {
20746   rtx (*gen_ashl3)(rtx, rtx, rtx);
20747   rtx (*gen_shld)(rtx, rtx, rtx);
20748   int half_width = GET_MODE_BITSIZE (mode) >> 1;
20749
20750   rtx low[2], high[2];
20751   int count;
20752
20753   if (CONST_INT_P (operands[2]))
20754     {
20755       split_double_mode (mode, operands, 2, low, high);
20756       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
20757
20758       if (count >= half_width)
20759         {
20760           emit_move_insn (high[0], low[1]);
20761           emit_move_insn (low[0], const0_rtx);
20762
20763           if (count > half_width)
20764             ix86_expand_ashl_const (high[0], count - half_width, mode);
20765         }
20766       else
20767         {
20768           gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
20769
20770           if (!rtx_equal_p (operands[0], operands[1]))
20771             emit_move_insn (operands[0], operands[1]);
20772
20773           emit_insn (gen_shld (high[0], low[0], GEN_INT (count)));
20774           ix86_expand_ashl_const (low[0], count, mode);
20775         }
20776       return;
20777     }
20778
20779   split_double_mode (mode, operands, 1, low, high);
20780
20781   gen_ashl3 = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
20782
20783   if (operands[1] == const1_rtx)
20784     {
20785       /* Assuming we've chosen a QImode capable registers, then 1 << N
20786          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
20787       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
20788         {
20789           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
20790
20791           ix86_expand_clear (low[0]);
20792           ix86_expand_clear (high[0]);
20793           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (half_width)));
20794
20795           d = gen_lowpart (QImode, low[0]);
20796           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
20797           s = gen_rtx_EQ (QImode, flags, const0_rtx);
20798           emit_insn (gen_rtx_SET (VOIDmode, d, s));
20799
20800           d = gen_lowpart (QImode, high[0]);
20801           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
20802           s = gen_rtx_NE (QImode, flags, const0_rtx);
20803           emit_insn (gen_rtx_SET (VOIDmode, d, s));
20804         }
20805
20806       /* Otherwise, we can get the same results by manually performing
20807          a bit extract operation on bit 5/6, and then performing the two
20808          shifts.  The two methods of getting 0/1 into low/high are exactly
20809          the same size.  Avoiding the shift in the bit extract case helps
20810          pentium4 a bit; no one else seems to care much either way.  */
20811       else
20812         {
20813           enum machine_mode half_mode;
20814           rtx (*gen_lshr3)(rtx, rtx, rtx);
20815           rtx (*gen_and3)(rtx, rtx, rtx);
20816           rtx (*gen_xor3)(rtx, rtx, rtx);
20817           HOST_WIDE_INT bits;
20818           rtx x;
20819
20820           if (mode == DImode)
20821             {
20822               half_mode = SImode;
20823               gen_lshr3 = gen_lshrsi3;
20824               gen_and3 = gen_andsi3;
20825               gen_xor3 = gen_xorsi3;
20826               bits = 5;
20827             }
20828           else
20829             {
20830               half_mode = DImode;
20831               gen_lshr3 = gen_lshrdi3;
20832               gen_and3 = gen_anddi3;
20833               gen_xor3 = gen_xordi3;
20834               bits = 6;
20835             }
20836
20837           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
20838             x = gen_rtx_ZERO_EXTEND (half_mode, operands[2]);
20839           else
20840             x = gen_lowpart (half_mode, operands[2]);
20841           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
20842
20843           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (bits)));
20844           emit_insn (gen_and3 (high[0], high[0], const1_rtx));
20845           emit_move_insn (low[0], high[0]);
20846           emit_insn (gen_xor3 (low[0], low[0], const1_rtx));
20847         }
20848
20849       emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
20850       emit_insn (gen_ashl3 (high[0], high[0], operands[2]));
20851       return;
20852     }
20853
20854   if (operands[1] == constm1_rtx)
20855     {
20856       /* For -1 << N, we can avoid the shld instruction, because we
20857          know that we're shifting 0...31/63 ones into a -1.  */
20858       emit_move_insn (low[0], constm1_rtx);
20859       if (optimize_insn_for_size_p ())
20860         emit_move_insn (high[0], low[0]);
20861       else
20862         emit_move_insn (high[0], constm1_rtx);
20863     }
20864   else
20865     {
20866       gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
20867
20868       if (!rtx_equal_p (operands[0], operands[1]))
20869         emit_move_insn (operands[0], operands[1]);
20870
20871       split_double_mode (mode, operands, 1, low, high);
20872       emit_insn (gen_shld (high[0], low[0], operands[2]));
20873     }
20874
20875   emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
20876
20877   if (TARGET_CMOVE && scratch)
20878     {
20879       rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
20880         = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
20881
20882       ix86_expand_clear (scratch);
20883       emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2], scratch));
20884     }
20885   else
20886     {
20887       rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
20888         = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
20889
20890       emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
20891     }
20892 }
20893
20894 void
20895 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
20896 {
20897   rtx (*gen_ashr3)(rtx, rtx, rtx)
20898     = mode == DImode ? gen_ashrsi3 : gen_ashrdi3;
20899   rtx (*gen_shrd)(rtx, rtx, rtx);
20900   int half_width = GET_MODE_BITSIZE (mode) >> 1;
20901
20902   rtx low[2], high[2];
20903   int count;
20904
20905   if (CONST_INT_P (operands[2]))
20906     {
20907       split_double_mode (mode, operands, 2, low, high);
20908       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
20909
20910       if (count == GET_MODE_BITSIZE (mode) - 1)
20911         {
20912           emit_move_insn (high[0], high[1]);
20913           emit_insn (gen_ashr3 (high[0], high[0],
20914                                 GEN_INT (half_width - 1)));
20915           emit_move_insn (low[0], high[0]);
20916
20917         }
20918       else if (count >= half_width)
20919         {
20920           emit_move_insn (low[0], high[1]);
20921           emit_move_insn (high[0], low[0]);
20922           emit_insn (gen_ashr3 (high[0], high[0],
20923                                 GEN_INT (half_width - 1)));
20924
20925           if (count > half_width)
20926             emit_insn (gen_ashr3 (low[0], low[0],
20927                                   GEN_INT (count - half_width)));
20928         }
20929       else
20930         {
20931           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
20932
20933           if (!rtx_equal_p (operands[0], operands[1]))
20934             emit_move_insn (operands[0], operands[1]);
20935
20936           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
20937           emit_insn (gen_ashr3 (high[0], high[0], GEN_INT (count)));
20938         }
20939     }
20940   else
20941     {
20942       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
20943
20944      if (!rtx_equal_p (operands[0], operands[1]))
20945         emit_move_insn (operands[0], operands[1]);
20946
20947       split_double_mode (mode, operands, 1, low, high);
20948
20949       emit_insn (gen_shrd (low[0], high[0], operands[2]));
20950       emit_insn (gen_ashr3 (high[0], high[0], operands[2]));
20951
20952       if (TARGET_CMOVE && scratch)
20953         {
20954           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
20955             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
20956
20957           emit_move_insn (scratch, high[0]);
20958           emit_insn (gen_ashr3 (scratch, scratch,
20959                                 GEN_INT (half_width - 1)));
20960           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
20961                                           scratch));
20962         }
20963       else
20964         {
20965           rtx (*gen_x86_shift_adj_3)(rtx, rtx, rtx)
20966             = mode == DImode ? gen_x86_shiftsi_adj_3 : gen_x86_shiftdi_adj_3;
20967
20968           emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
20969         }
20970     }
20971 }
20972
20973 void
20974 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
20975 {
20976   rtx (*gen_lshr3)(rtx, rtx, rtx)
20977     = mode == DImode ? gen_lshrsi3 : gen_lshrdi3;
20978   rtx (*gen_shrd)(rtx, rtx, rtx);
20979   int half_width = GET_MODE_BITSIZE (mode) >> 1;
20980
20981   rtx low[2], high[2];
20982   int count;
20983
20984   if (CONST_INT_P (operands[2]))
20985     {
20986       split_double_mode (mode, operands, 2, low, high);
20987       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
20988
20989       if (count >= half_width)
20990         {
20991           emit_move_insn (low[0], high[1]);
20992           ix86_expand_clear (high[0]);
20993
20994           if (count > half_width)
20995             emit_insn (gen_lshr3 (low[0], low[0],
20996                                   GEN_INT (count - half_width)));
20997         }
20998       else
20999         {
21000           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
21001
21002           if (!rtx_equal_p (operands[0], operands[1]))
21003             emit_move_insn (operands[0], operands[1]);
21004
21005           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
21006           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (count)));
21007         }
21008     }
21009   else
21010     {
21011       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
21012
21013       if (!rtx_equal_p (operands[0], operands[1]))
21014         emit_move_insn (operands[0], operands[1]);
21015
21016       split_double_mode (mode, operands, 1, low, high);
21017
21018       emit_insn (gen_shrd (low[0], high[0], operands[2]));
21019       emit_insn (gen_lshr3 (high[0], high[0], operands[2]));
21020
21021       if (TARGET_CMOVE && scratch)
21022         {
21023           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
21024             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
21025
21026           ix86_expand_clear (scratch);
21027           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
21028                                           scratch));
21029         }
21030       else
21031         {
21032           rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
21033             = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
21034
21035           emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
21036         }
21037     }
21038 }
21039
21040 /* Predict just emitted jump instruction to be taken with probability PROB.  */
21041 static void
21042 predict_jump (int prob)
21043 {
21044   rtx insn = get_last_insn ();
21045   gcc_assert (JUMP_P (insn));
21046   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
21047 }
21048
21049 /* Helper function for the string operations below.  Dest VARIABLE whether
21050    it is aligned to VALUE bytes.  If true, jump to the label.  */
21051 static rtx
21052 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
21053 {
21054   rtx label = gen_label_rtx ();
21055   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
21056   if (GET_MODE (variable) == DImode)
21057     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
21058   else
21059     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
21060   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
21061                            1, label);
21062   if (epilogue)
21063     predict_jump (REG_BR_PROB_BASE * 50 / 100);
21064   else
21065     predict_jump (REG_BR_PROB_BASE * 90 / 100);
21066   return label;
21067 }
21068
21069 /* Adjust COUNTER by the VALUE.  */
21070 static void
21071 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
21072 {
21073   rtx (*gen_add)(rtx, rtx, rtx)
21074     = GET_MODE (countreg) == DImode ? gen_adddi3 : gen_addsi3;
21075
21076   emit_insn (gen_add (countreg, countreg, GEN_INT (-value)));
21077 }
21078
21079 /* Zero extend possibly SImode EXP to Pmode register.  */
21080 rtx
21081 ix86_zero_extend_to_Pmode (rtx exp)
21082 {
21083   rtx r;
21084   if (GET_MODE (exp) == VOIDmode)
21085     return force_reg (Pmode, exp);
21086   if (GET_MODE (exp) == Pmode)
21087     return copy_to_mode_reg (Pmode, exp);
21088   r = gen_reg_rtx (Pmode);
21089   emit_insn (gen_zero_extendsidi2 (r, exp));
21090   return r;
21091 }
21092
21093 /* Divide COUNTREG by SCALE.  */
21094 static rtx
21095 scale_counter (rtx countreg, int scale)
21096 {
21097   rtx sc;
21098
21099   if (scale == 1)
21100     return countreg;
21101   if (CONST_INT_P (countreg))
21102     return GEN_INT (INTVAL (countreg) / scale);
21103   gcc_assert (REG_P (countreg));
21104
21105   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
21106                             GEN_INT (exact_log2 (scale)),
21107                             NULL, 1, OPTAB_DIRECT);
21108   return sc;
21109 }
21110
21111 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
21112    DImode for constant loop counts.  */
21113
21114 static enum machine_mode
21115 counter_mode (rtx count_exp)
21116 {
21117   if (GET_MODE (count_exp) != VOIDmode)
21118     return GET_MODE (count_exp);
21119   if (!CONST_INT_P (count_exp))
21120     return Pmode;
21121   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
21122     return DImode;
21123   return SImode;
21124 }
21125
21126 /* When SRCPTR is non-NULL, output simple loop to move memory
21127    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
21128    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
21129    equivalent loop to set memory by VALUE (supposed to be in MODE).
21130
21131    The size is rounded down to whole number of chunk size moved at once.
21132    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
21133
21134
21135 static void
21136 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
21137                                rtx destptr, rtx srcptr, rtx value,
21138                                rtx count, enum machine_mode mode, int unroll,
21139                                int expected_size)
21140 {
21141   rtx out_label, top_label, iter, tmp;
21142   enum machine_mode iter_mode = counter_mode (count);
21143   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
21144   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
21145   rtx size;
21146   rtx x_addr;
21147   rtx y_addr;
21148   int i;
21149
21150   top_label = gen_label_rtx ();
21151   out_label = gen_label_rtx ();
21152   iter = gen_reg_rtx (iter_mode);
21153
21154   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
21155                               NULL, 1, OPTAB_DIRECT);
21156   /* Those two should combine.  */
21157   if (piece_size == const1_rtx)
21158     {
21159       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
21160                                true, out_label);
21161       predict_jump (REG_BR_PROB_BASE * 10 / 100);
21162     }
21163   emit_move_insn (iter, const0_rtx);
21164
21165   emit_label (top_label);
21166
21167   tmp = convert_modes (Pmode, iter_mode, iter, true);
21168   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
21169   destmem = change_address (destmem, mode, x_addr);
21170
21171   if (srcmem)
21172     {
21173       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
21174       srcmem = change_address (srcmem, mode, y_addr);
21175
21176       /* When unrolling for chips that reorder memory reads and writes,
21177          we can save registers by using single temporary.
21178          Also using 4 temporaries is overkill in 32bit mode.  */
21179       if (!TARGET_64BIT && 0)
21180         {
21181           for (i = 0; i < unroll; i++)
21182             {
21183               if (i)
21184                 {
21185                   destmem =
21186                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
21187                   srcmem =
21188                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
21189                 }
21190               emit_move_insn (destmem, srcmem);
21191             }
21192         }
21193       else
21194         {
21195           rtx tmpreg[4];
21196           gcc_assert (unroll <= 4);
21197           for (i = 0; i < unroll; i++)
21198             {
21199               tmpreg[i] = gen_reg_rtx (mode);
21200               if (i)
21201                 {
21202                   srcmem =
21203                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
21204                 }
21205               emit_move_insn (tmpreg[i], srcmem);
21206             }
21207           for (i = 0; i < unroll; i++)
21208             {
21209               if (i)
21210                 {
21211                   destmem =
21212                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
21213                 }
21214               emit_move_insn (destmem, tmpreg[i]);
21215             }
21216         }
21217     }
21218   else
21219     for (i = 0; i < unroll; i++)
21220       {
21221         if (i)
21222           destmem =
21223             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
21224         emit_move_insn (destmem, value);
21225       }
21226
21227   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
21228                              true, OPTAB_LIB_WIDEN);
21229   if (tmp != iter)
21230     emit_move_insn (iter, tmp);
21231
21232   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
21233                            true, top_label);
21234   if (expected_size != -1)
21235     {
21236       expected_size /= GET_MODE_SIZE (mode) * unroll;
21237       if (expected_size == 0)
21238         predict_jump (0);
21239       else if (expected_size > REG_BR_PROB_BASE)
21240         predict_jump (REG_BR_PROB_BASE - 1);
21241       else
21242         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
21243     }
21244   else
21245     predict_jump (REG_BR_PROB_BASE * 80 / 100);
21246   iter = ix86_zero_extend_to_Pmode (iter);
21247   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
21248                              true, OPTAB_LIB_WIDEN);
21249   if (tmp != destptr)
21250     emit_move_insn (destptr, tmp);
21251   if (srcptr)
21252     {
21253       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
21254                                  true, OPTAB_LIB_WIDEN);
21255       if (tmp != srcptr)
21256         emit_move_insn (srcptr, tmp);
21257     }
21258   emit_label (out_label);
21259 }
21260
21261 /* Output "rep; mov" instruction.
21262    Arguments have same meaning as for previous function */
21263 static void
21264 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
21265                            rtx destptr, rtx srcptr,
21266                            rtx count,
21267                            enum machine_mode mode)
21268 {
21269   rtx destexp;
21270   rtx srcexp;
21271   rtx countreg;
21272   HOST_WIDE_INT rounded_count;
21273
21274   /* If the size is known, it is shorter to use rep movs.  */
21275   if (mode == QImode && CONST_INT_P (count)
21276       && !(INTVAL (count) & 3))
21277     mode = SImode;
21278
21279   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
21280     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
21281   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
21282     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
21283   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
21284   if (mode != QImode)
21285     {
21286       destexp = gen_rtx_ASHIFT (Pmode, countreg,
21287                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
21288       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
21289       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
21290                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
21291       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
21292     }
21293   else
21294     {
21295       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
21296       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
21297     }
21298   if (CONST_INT_P (count))
21299     {
21300       rounded_count = (INTVAL (count)
21301                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
21302       destmem = shallow_copy_rtx (destmem);
21303       srcmem = shallow_copy_rtx (srcmem);
21304       set_mem_size (destmem, rounded_count);
21305       set_mem_size (srcmem, rounded_count);
21306     }
21307   else
21308     {
21309       if (MEM_SIZE_KNOWN_P (destmem))
21310         clear_mem_size (destmem);
21311       if (MEM_SIZE_KNOWN_P (srcmem))
21312         clear_mem_size (srcmem);
21313     }
21314   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
21315                           destexp, srcexp));
21316 }
21317
21318 /* Output "rep; stos" instruction.
21319    Arguments have same meaning as for previous function */
21320 static void
21321 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
21322                             rtx count, enum machine_mode mode,
21323                             rtx orig_value)
21324 {
21325   rtx destexp;
21326   rtx countreg;
21327   HOST_WIDE_INT rounded_count;
21328
21329   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
21330     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
21331   value = force_reg (mode, gen_lowpart (mode, value));
21332   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
21333   if (mode != QImode)
21334     {
21335       destexp = gen_rtx_ASHIFT (Pmode, countreg,
21336                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
21337       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
21338     }
21339   else
21340     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
21341   if (orig_value == const0_rtx && CONST_INT_P (count))
21342     {
21343       rounded_count = (INTVAL (count)
21344                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
21345       destmem = shallow_copy_rtx (destmem);
21346       set_mem_size (destmem, rounded_count);
21347     }
21348   else if (MEM_SIZE_KNOWN_P (destmem))
21349     clear_mem_size (destmem);
21350   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
21351 }
21352
21353 static void
21354 emit_strmov (rtx destmem, rtx srcmem,
21355              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
21356 {
21357   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
21358   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
21359   emit_insn (gen_strmov (destptr, dest, srcptr, src));
21360 }
21361
21362 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
21363 static void
21364 expand_movmem_epilogue (rtx destmem, rtx srcmem,
21365                         rtx destptr, rtx srcptr, rtx count, int max_size)
21366 {
21367   rtx src, dest;
21368   if (CONST_INT_P (count))
21369     {
21370       HOST_WIDE_INT countval = INTVAL (count);
21371       int offset = 0;
21372
21373       if ((countval & 0x10) && max_size > 16)
21374         {
21375           if (TARGET_64BIT)
21376             {
21377               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
21378               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
21379             }
21380           else
21381             gcc_unreachable ();
21382           offset += 16;
21383         }
21384       if ((countval & 0x08) && max_size > 8)
21385         {
21386           if (TARGET_64BIT)
21387             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
21388           else
21389             {
21390               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
21391               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
21392             }
21393           offset += 8;
21394         }
21395       if ((countval & 0x04) && max_size > 4)
21396         {
21397           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
21398           offset += 4;
21399         }
21400       if ((countval & 0x02) && max_size > 2)
21401         {
21402           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
21403           offset += 2;
21404         }
21405       if ((countval & 0x01) && max_size > 1)
21406         {
21407           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
21408           offset += 1;
21409         }
21410       return;
21411     }
21412   if (max_size > 8)
21413     {
21414       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
21415                                     count, 1, OPTAB_DIRECT);
21416       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
21417                                      count, QImode, 1, 4);
21418       return;
21419     }
21420
21421   /* When there are stringops, we can cheaply increase dest and src pointers.
21422      Otherwise we save code size by maintaining offset (zero is readily
21423      available from preceding rep operation) and using x86 addressing modes.
21424    */
21425   if (TARGET_SINGLE_STRINGOP)
21426     {
21427       if (max_size > 4)
21428         {
21429           rtx label = ix86_expand_aligntest (count, 4, true);
21430           src = change_address (srcmem, SImode, srcptr);
21431           dest = change_address (destmem, SImode, destptr);
21432           emit_insn (gen_strmov (destptr, dest, srcptr, src));
21433           emit_label (label);
21434           LABEL_NUSES (label) = 1;
21435         }
21436       if (max_size > 2)
21437         {
21438           rtx label = ix86_expand_aligntest (count, 2, true);
21439           src = change_address (srcmem, HImode, srcptr);
21440           dest = change_address (destmem, HImode, destptr);
21441           emit_insn (gen_strmov (destptr, dest, srcptr, src));
21442           emit_label (label);
21443           LABEL_NUSES (label) = 1;
21444         }
21445       if (max_size > 1)
21446         {
21447           rtx label = ix86_expand_aligntest (count, 1, true);
21448           src = change_address (srcmem, QImode, srcptr);
21449           dest = change_address (destmem, QImode, destptr);
21450           emit_insn (gen_strmov (destptr, dest, srcptr, src));
21451           emit_label (label);
21452           LABEL_NUSES (label) = 1;
21453         }
21454     }
21455   else
21456     {
21457       rtx offset = force_reg (Pmode, const0_rtx);
21458       rtx tmp;
21459
21460       if (max_size > 4)
21461         {
21462           rtx label = ix86_expand_aligntest (count, 4, true);
21463           src = change_address (srcmem, SImode, srcptr);
21464           dest = change_address (destmem, SImode, destptr);
21465           emit_move_insn (dest, src);
21466           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
21467                                      true, OPTAB_LIB_WIDEN);
21468           if (tmp != offset)
21469             emit_move_insn (offset, tmp);
21470           emit_label (label);
21471           LABEL_NUSES (label) = 1;
21472         }
21473       if (max_size > 2)
21474         {
21475           rtx label = ix86_expand_aligntest (count, 2, true);
21476           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
21477           src = change_address (srcmem, HImode, tmp);
21478           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
21479           dest = change_address (destmem, HImode, tmp);
21480           emit_move_insn (dest, src);
21481           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
21482                                      true, OPTAB_LIB_WIDEN);
21483           if (tmp != offset)
21484             emit_move_insn (offset, tmp);
21485           emit_label (label);
21486           LABEL_NUSES (label) = 1;
21487         }
21488       if (max_size > 1)
21489         {
21490           rtx label = ix86_expand_aligntest (count, 1, true);
21491           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
21492           src = change_address (srcmem, QImode, tmp);
21493           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
21494           dest = change_address (destmem, QImode, tmp);
21495           emit_move_insn (dest, src);
21496           emit_label (label);
21497           LABEL_NUSES (label) = 1;
21498         }
21499     }
21500 }
21501
21502 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
21503 static void
21504 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
21505                                  rtx count, int max_size)
21506 {
21507   count =
21508     expand_simple_binop (counter_mode (count), AND, count,
21509                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
21510   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
21511                                  gen_lowpart (QImode, value), count, QImode,
21512                                  1, max_size / 2);
21513 }
21514
21515 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
21516 static void
21517 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
21518 {
21519   rtx dest;
21520
21521   if (CONST_INT_P (count))
21522     {
21523       HOST_WIDE_INT countval = INTVAL (count);
21524       int offset = 0;
21525
21526       if ((countval & 0x10) && max_size > 16)
21527         {
21528           if (TARGET_64BIT)
21529             {
21530               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
21531               emit_insn (gen_strset (destptr, dest, value));
21532               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
21533               emit_insn (gen_strset (destptr, dest, value));
21534             }
21535           else
21536             gcc_unreachable ();
21537           offset += 16;
21538         }
21539       if ((countval & 0x08) && max_size > 8)
21540         {
21541           if (TARGET_64BIT)
21542             {
21543               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
21544               emit_insn (gen_strset (destptr, dest, value));
21545             }
21546           else
21547             {
21548               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
21549               emit_insn (gen_strset (destptr, dest, value));
21550               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
21551               emit_insn (gen_strset (destptr, dest, value));
21552             }
21553           offset += 8;
21554         }
21555       if ((countval & 0x04) && max_size > 4)
21556         {
21557           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
21558           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
21559           offset += 4;
21560         }
21561       if ((countval & 0x02) && max_size > 2)
21562         {
21563           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
21564           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
21565           offset += 2;
21566         }
21567       if ((countval & 0x01) && max_size > 1)
21568         {
21569           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
21570           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
21571           offset += 1;
21572         }
21573       return;
21574     }
21575   if (max_size > 32)
21576     {
21577       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
21578       return;
21579     }
21580   if (max_size > 16)
21581     {
21582       rtx label = ix86_expand_aligntest (count, 16, true);
21583       if (TARGET_64BIT)
21584         {
21585           dest = change_address (destmem, DImode, destptr);
21586           emit_insn (gen_strset (destptr, dest, value));
21587           emit_insn (gen_strset (destptr, dest, value));
21588         }
21589       else
21590         {
21591           dest = change_address (destmem, SImode, destptr);
21592           emit_insn (gen_strset (destptr, dest, value));
21593           emit_insn (gen_strset (destptr, dest, value));
21594           emit_insn (gen_strset (destptr, dest, value));
21595           emit_insn (gen_strset (destptr, dest, value));
21596         }
21597       emit_label (label);
21598       LABEL_NUSES (label) = 1;
21599     }
21600   if (max_size > 8)
21601     {
21602       rtx label = ix86_expand_aligntest (count, 8, true);
21603       if (TARGET_64BIT)
21604         {
21605           dest = change_address (destmem, DImode, destptr);
21606           emit_insn (gen_strset (destptr, dest, value));
21607         }
21608       else
21609         {
21610           dest = change_address (destmem, SImode, destptr);
21611           emit_insn (gen_strset (destptr, dest, value));
21612           emit_insn (gen_strset (destptr, dest, value));
21613         }
21614       emit_label (label);
21615       LABEL_NUSES (label) = 1;
21616     }
21617   if (max_size > 4)
21618     {
21619       rtx label = ix86_expand_aligntest (count, 4, true);
21620       dest = change_address (destmem, SImode, destptr);
21621       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
21622       emit_label (label);
21623       LABEL_NUSES (label) = 1;
21624     }
21625   if (max_size > 2)
21626     {
21627       rtx label = ix86_expand_aligntest (count, 2, true);
21628       dest = change_address (destmem, HImode, destptr);
21629       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
21630       emit_label (label);
21631       LABEL_NUSES (label) = 1;
21632     }
21633   if (max_size > 1)
21634     {
21635       rtx label = ix86_expand_aligntest (count, 1, true);
21636       dest = change_address (destmem, QImode, destptr);
21637       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
21638       emit_label (label);
21639       LABEL_NUSES (label) = 1;
21640     }
21641 }
21642
21643 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
21644    DESIRED_ALIGNMENT.  */
21645 static void
21646 expand_movmem_prologue (rtx destmem, rtx srcmem,
21647                         rtx destptr, rtx srcptr, rtx count,
21648                         int align, int desired_alignment)
21649 {
21650   if (align <= 1 && desired_alignment > 1)
21651     {
21652       rtx label = ix86_expand_aligntest (destptr, 1, false);
21653       srcmem = change_address (srcmem, QImode, srcptr);
21654       destmem = change_address (destmem, QImode, destptr);
21655       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
21656       ix86_adjust_counter (count, 1);
21657       emit_label (label);
21658       LABEL_NUSES (label) = 1;
21659     }
21660   if (align <= 2 && desired_alignment > 2)
21661     {
21662       rtx label = ix86_expand_aligntest (destptr, 2, false);
21663       srcmem = change_address (srcmem, HImode, srcptr);
21664       destmem = change_address (destmem, HImode, destptr);
21665       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
21666       ix86_adjust_counter (count, 2);
21667       emit_label (label);
21668       LABEL_NUSES (label) = 1;
21669     }
21670   if (align <= 4 && desired_alignment > 4)
21671     {
21672       rtx label = ix86_expand_aligntest (destptr, 4, false);
21673       srcmem = change_address (srcmem, SImode, srcptr);
21674       destmem = change_address (destmem, SImode, destptr);
21675       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
21676       ix86_adjust_counter (count, 4);
21677       emit_label (label);
21678       LABEL_NUSES (label) = 1;
21679     }
21680   gcc_assert (desired_alignment <= 8);
21681 }
21682
21683 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
21684    ALIGN_BYTES is how many bytes need to be copied.  */
21685 static rtx
21686 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
21687                                  int desired_align, int align_bytes)
21688 {
21689   rtx src = *srcp;
21690   rtx orig_dst = dst;
21691   rtx orig_src = src;
21692   int off = 0;
21693   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
21694   if (src_align_bytes >= 0)
21695     src_align_bytes = desired_align - src_align_bytes;
21696   if (align_bytes & 1)
21697     {
21698       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
21699       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
21700       off = 1;
21701       emit_insn (gen_strmov (destreg, dst, srcreg, src));
21702     }
21703   if (align_bytes & 2)
21704     {
21705       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
21706       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
21707       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
21708         set_mem_align (dst, 2 * BITS_PER_UNIT);
21709       if (src_align_bytes >= 0
21710           && (src_align_bytes & 1) == (align_bytes & 1)
21711           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
21712         set_mem_align (src, 2 * BITS_PER_UNIT);
21713       off = 2;
21714       emit_insn (gen_strmov (destreg, dst, srcreg, src));
21715     }
21716   if (align_bytes & 4)
21717     {
21718       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
21719       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
21720       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
21721         set_mem_align (dst, 4 * BITS_PER_UNIT);
21722       if (src_align_bytes >= 0)
21723         {
21724           unsigned int src_align = 0;
21725           if ((src_align_bytes & 3) == (align_bytes & 3))
21726             src_align = 4;
21727           else if ((src_align_bytes & 1) == (align_bytes & 1))
21728             src_align = 2;
21729           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
21730             set_mem_align (src, src_align * BITS_PER_UNIT);
21731         }
21732       off = 4;
21733       emit_insn (gen_strmov (destreg, dst, srcreg, src));
21734     }
21735   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
21736   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
21737   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
21738     set_mem_align (dst, desired_align * BITS_PER_UNIT);
21739   if (src_align_bytes >= 0)
21740     {
21741       unsigned int src_align = 0;
21742       if ((src_align_bytes & 7) == (align_bytes & 7))
21743         src_align = 8;
21744       else if ((src_align_bytes & 3) == (align_bytes & 3))
21745         src_align = 4;
21746       else if ((src_align_bytes & 1) == (align_bytes & 1))
21747         src_align = 2;
21748       if (src_align > (unsigned int) desired_align)
21749         src_align = desired_align;
21750       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
21751         set_mem_align (src, src_align * BITS_PER_UNIT);
21752     }
21753   if (MEM_SIZE_KNOWN_P (orig_dst))
21754     set_mem_size (dst, MEM_SIZE (orig_dst) - align_bytes);
21755   if (MEM_SIZE_KNOWN_P (orig_src))
21756     set_mem_size (src, MEM_SIZE (orig_src) - align_bytes);
21757   *srcp = src;
21758   return dst;
21759 }
21760
21761 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
21762    DESIRED_ALIGNMENT.  */
21763 static void
21764 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
21765                         int align, int desired_alignment)
21766 {
21767   if (align <= 1 && desired_alignment > 1)
21768     {
21769       rtx label = ix86_expand_aligntest (destptr, 1, false);
21770       destmem = change_address (destmem, QImode, destptr);
21771       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
21772       ix86_adjust_counter (count, 1);
21773       emit_label (label);
21774       LABEL_NUSES (label) = 1;
21775     }
21776   if (align <= 2 && desired_alignment > 2)
21777     {
21778       rtx label = ix86_expand_aligntest (destptr, 2, false);
21779       destmem = change_address (destmem, HImode, destptr);
21780       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
21781       ix86_adjust_counter (count, 2);
21782       emit_label (label);
21783       LABEL_NUSES (label) = 1;
21784     }
21785   if (align <= 4 && desired_alignment > 4)
21786     {
21787       rtx label = ix86_expand_aligntest (destptr, 4, false);
21788       destmem = change_address (destmem, SImode, destptr);
21789       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
21790       ix86_adjust_counter (count, 4);
21791       emit_label (label);
21792       LABEL_NUSES (label) = 1;
21793     }
21794   gcc_assert (desired_alignment <= 8);
21795 }
21796
21797 /* Set enough from DST to align DST known to by aligned by ALIGN to
21798    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
21799 static rtx
21800 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
21801                                  int desired_align, int align_bytes)
21802 {
21803   int off = 0;
21804   rtx orig_dst = dst;
21805   if (align_bytes & 1)
21806     {
21807       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
21808       off = 1;
21809       emit_insn (gen_strset (destreg, dst,
21810                              gen_lowpart (QImode, value)));
21811     }
21812   if (align_bytes & 2)
21813     {
21814       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
21815       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
21816         set_mem_align (dst, 2 * BITS_PER_UNIT);
21817       off = 2;
21818       emit_insn (gen_strset (destreg, dst,
21819                              gen_lowpart (HImode, value)));
21820     }
21821   if (align_bytes & 4)
21822     {
21823       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
21824       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
21825         set_mem_align (dst, 4 * BITS_PER_UNIT);
21826       off = 4;
21827       emit_insn (gen_strset (destreg, dst,
21828                              gen_lowpart (SImode, value)));
21829     }
21830   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
21831   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
21832     set_mem_align (dst, desired_align * BITS_PER_UNIT);
21833   if (MEM_SIZE_KNOWN_P (orig_dst))
21834     set_mem_size (dst, MEM_SIZE (orig_dst) - align_bytes);
21835   return dst;
21836 }
21837
21838 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
21839 static enum stringop_alg
21840 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
21841             int *dynamic_check)
21842 {
21843   const struct stringop_algs * algs;
21844   bool optimize_for_speed;
21845   /* Algorithms using the rep prefix want at least edi and ecx;
21846      additionally, memset wants eax and memcpy wants esi.  Don't
21847      consider such algorithms if the user has appropriated those
21848      registers for their own purposes.  */
21849   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
21850                              || (memset
21851                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
21852
21853 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
21854                            || (alg != rep_prefix_1_byte         \
21855                                && alg != rep_prefix_4_byte      \
21856                                && alg != rep_prefix_8_byte))
21857   const struct processor_costs *cost;
21858
21859   /* Even if the string operation call is cold, we still might spend a lot
21860      of time processing large blocks.  */
21861   if (optimize_function_for_size_p (cfun)
21862       || (optimize_insn_for_size_p ()
21863           && expected_size != -1 && expected_size < 256))
21864     optimize_for_speed = false;
21865   else
21866     optimize_for_speed = true;
21867
21868   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
21869
21870   *dynamic_check = -1;
21871   if (memset)
21872     algs = &cost->memset[TARGET_64BIT != 0];
21873   else
21874     algs = &cost->memcpy[TARGET_64BIT != 0];
21875   if (ix86_stringop_alg != no_stringop && ALG_USABLE_P (ix86_stringop_alg))
21876     return ix86_stringop_alg;
21877   /* rep; movq or rep; movl is the smallest variant.  */
21878   else if (!optimize_for_speed)
21879     {
21880       if (!count || (count & 3))
21881         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
21882       else
21883         return rep_prefix_usable ? rep_prefix_4_byte : loop;
21884     }
21885   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
21886    */
21887   else if (expected_size != -1 && expected_size < 4)
21888     return loop_1_byte;
21889   else if (expected_size != -1)
21890     {
21891       unsigned int i;
21892       enum stringop_alg alg = libcall;
21893       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
21894         {
21895           /* We get here if the algorithms that were not libcall-based
21896              were rep-prefix based and we are unable to use rep prefixes
21897              based on global register usage.  Break out of the loop and
21898              use the heuristic below.  */
21899           if (algs->size[i].max == 0)
21900             break;
21901           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
21902             {
21903               enum stringop_alg candidate = algs->size[i].alg;
21904
21905               if (candidate != libcall && ALG_USABLE_P (candidate))
21906                 alg = candidate;
21907               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
21908                  last non-libcall inline algorithm.  */
21909               if (TARGET_INLINE_ALL_STRINGOPS)
21910                 {
21911                   /* When the current size is best to be copied by a libcall,
21912                      but we are still forced to inline, run the heuristic below
21913                      that will pick code for medium sized blocks.  */
21914                   if (alg != libcall)
21915                     return alg;
21916                   break;
21917                 }
21918               else if (ALG_USABLE_P (candidate))
21919                 return candidate;
21920             }
21921         }
21922       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
21923     }
21924   /* When asked to inline the call anyway, try to pick meaningful choice.
21925      We look for maximal size of block that is faster to copy by hand and
21926      take blocks of at most of that size guessing that average size will
21927      be roughly half of the block.
21928
21929      If this turns out to be bad, we might simply specify the preferred
21930      choice in ix86_costs.  */
21931   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
21932       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
21933     {
21934       int max = -1;
21935       enum stringop_alg alg;
21936       int i;
21937       bool any_alg_usable_p = true;
21938
21939       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
21940         {
21941           enum stringop_alg candidate = algs->size[i].alg;
21942           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
21943
21944           if (candidate != libcall && candidate
21945               && ALG_USABLE_P (candidate))
21946               max = algs->size[i].max;
21947         }
21948       /* If there aren't any usable algorithms, then recursing on
21949          smaller sizes isn't going to find anything.  Just return the
21950          simple byte-at-a-time copy loop.  */
21951       if (!any_alg_usable_p)
21952         {
21953           /* Pick something reasonable.  */
21954           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
21955             *dynamic_check = 128;
21956           return loop_1_byte;
21957         }
21958       if (max == -1)
21959         max = 4096;
21960       alg = decide_alg (count, max / 2, memset, dynamic_check);
21961       gcc_assert (*dynamic_check == -1);
21962       gcc_assert (alg != libcall);
21963       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
21964         *dynamic_check = max;
21965       return alg;
21966     }
21967   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
21968 #undef ALG_USABLE_P
21969 }
21970
21971 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
21972    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
21973 static int
21974 decide_alignment (int align,
21975                   enum stringop_alg alg,
21976                   int expected_size)
21977 {
21978   int desired_align = 0;
21979   switch (alg)
21980     {
21981       case no_stringop:
21982         gcc_unreachable ();
21983       case loop:
21984       case unrolled_loop:
21985         desired_align = GET_MODE_SIZE (Pmode);
21986         break;
21987       case rep_prefix_8_byte:
21988         desired_align = 8;
21989         break;
21990       case rep_prefix_4_byte:
21991         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
21992            copying whole cacheline at once.  */
21993         if (TARGET_PENTIUMPRO)
21994           desired_align = 8;
21995         else
21996           desired_align = 4;
21997         break;
21998       case rep_prefix_1_byte:
21999         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
22000            copying whole cacheline at once.  */
22001         if (TARGET_PENTIUMPRO)
22002           desired_align = 8;
22003         else
22004           desired_align = 1;
22005         break;
22006       case loop_1_byte:
22007         desired_align = 1;
22008         break;
22009       case libcall:
22010         return 0;
22011     }
22012
22013   if (optimize_size)
22014     desired_align = 1;
22015   if (desired_align < align)
22016     desired_align = align;
22017   if (expected_size != -1 && expected_size < 4)
22018     desired_align = align;
22019   return desired_align;
22020 }
22021
22022 /* Return the smallest power of 2 greater than VAL.  */
22023 static int
22024 smallest_pow2_greater_than (int val)
22025 {
22026   int ret = 1;
22027   while (ret <= val)
22028     ret <<= 1;
22029   return ret;
22030 }
22031
22032 /* Expand string move (memcpy) operation.  Use i386 string operations
22033    when profitable.  expand_setmem contains similar code.  The code
22034    depends upon architecture, block size and alignment, but always has
22035    the same overall structure:
22036
22037    1) Prologue guard: Conditional that jumps up to epilogues for small
22038       blocks that can be handled by epilogue alone.  This is faster
22039       but also needed for correctness, since prologue assume the block
22040       is larger than the desired alignment.
22041
22042       Optional dynamic check for size and libcall for large
22043       blocks is emitted here too, with -minline-stringops-dynamically.
22044
22045    2) Prologue: copy first few bytes in order to get destination
22046       aligned to DESIRED_ALIGN.  It is emitted only when ALIGN is less
22047       than DESIRED_ALIGN and up to DESIRED_ALIGN - ALIGN bytes can be
22048       copied.  We emit either a jump tree on power of two sized
22049       blocks, or a byte loop.
22050
22051    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
22052       with specified algorithm.
22053
22054    4) Epilogue: code copying tail of the block that is too small to be
22055       handled by main body (or up to size guarded by prologue guard).  */
22056
22057 bool
22058 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
22059                     rtx expected_align_exp, rtx expected_size_exp)
22060 {
22061   rtx destreg;
22062   rtx srcreg;
22063   rtx label = NULL;
22064   rtx tmp;
22065   rtx jump_around_label = NULL;
22066   HOST_WIDE_INT align = 1;
22067   unsigned HOST_WIDE_INT count = 0;
22068   HOST_WIDE_INT expected_size = -1;
22069   int size_needed = 0, epilogue_size_needed;
22070   int desired_align = 0, align_bytes = 0;
22071   enum stringop_alg alg;
22072   int dynamic_check;
22073   bool need_zero_guard = false;
22074
22075   if (CONST_INT_P (align_exp))
22076     align = INTVAL (align_exp);
22077   /* i386 can do misaligned access on reasonably increased cost.  */
22078   if (CONST_INT_P (expected_align_exp)
22079       && INTVAL (expected_align_exp) > align)
22080     align = INTVAL (expected_align_exp);
22081   /* ALIGN is the minimum of destination and source alignment, but we care here
22082      just about destination alignment.  */
22083   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
22084     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
22085
22086   if (CONST_INT_P (count_exp))
22087     count = expected_size = INTVAL (count_exp);
22088   if (CONST_INT_P (expected_size_exp) && count == 0)
22089     expected_size = INTVAL (expected_size_exp);
22090
22091   /* Make sure we don't need to care about overflow later on.  */
22092   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
22093     return false;
22094
22095   /* Step 0: Decide on preferred algorithm, desired alignment and
22096      size of chunks to be copied by main loop.  */
22097
22098   alg = decide_alg (count, expected_size, false, &dynamic_check);
22099   desired_align = decide_alignment (align, alg, expected_size);
22100
22101   if (!TARGET_ALIGN_STRINGOPS)
22102     align = desired_align;
22103
22104   if (alg == libcall)
22105     return false;
22106   gcc_assert (alg != no_stringop);
22107   if (!count)
22108     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
22109   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
22110   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
22111   switch (alg)
22112     {
22113     case libcall:
22114     case no_stringop:
22115       gcc_unreachable ();
22116     case loop:
22117       need_zero_guard = true;
22118       size_needed = GET_MODE_SIZE (Pmode);
22119       break;
22120     case unrolled_loop:
22121       need_zero_guard = true;
22122       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
22123       break;
22124     case rep_prefix_8_byte:
22125       size_needed = 8;
22126       break;
22127     case rep_prefix_4_byte:
22128       size_needed = 4;
22129       break;
22130     case rep_prefix_1_byte:
22131       size_needed = 1;
22132       break;
22133     case loop_1_byte:
22134       need_zero_guard = true;
22135       size_needed = 1;
22136       break;
22137     }
22138
22139   epilogue_size_needed = size_needed;
22140
22141   /* Step 1: Prologue guard.  */
22142
22143   /* Alignment code needs count to be in register.  */
22144   if (CONST_INT_P (count_exp) && desired_align > align)
22145     {
22146       if (INTVAL (count_exp) > desired_align
22147           && INTVAL (count_exp) > size_needed)
22148         {
22149           align_bytes
22150             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
22151           if (align_bytes <= 0)
22152             align_bytes = 0;
22153           else
22154             align_bytes = desired_align - align_bytes;
22155         }
22156       if (align_bytes == 0)
22157         count_exp = force_reg (counter_mode (count_exp), count_exp);
22158     }
22159   gcc_assert (desired_align >= 1 && align >= 1);
22160
22161   /* Ensure that alignment prologue won't copy past end of block.  */
22162   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
22163     {
22164       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
22165       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
22166          Make sure it is power of 2.  */
22167       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
22168
22169       if (count)
22170         {
22171           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
22172             {
22173               /* If main algorithm works on QImode, no epilogue is needed.
22174                  For small sizes just don't align anything.  */
22175               if (size_needed == 1)
22176                 desired_align = align;
22177               else
22178                 goto epilogue;
22179             }
22180         }
22181       else
22182         {
22183           label = gen_label_rtx ();
22184           emit_cmp_and_jump_insns (count_exp,
22185                                    GEN_INT (epilogue_size_needed),
22186                                    LTU, 0, counter_mode (count_exp), 1, label);
22187           if (expected_size == -1 || expected_size < epilogue_size_needed)
22188             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22189           else
22190             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22191         }
22192     }
22193
22194   /* Emit code to decide on runtime whether library call or inline should be
22195      used.  */
22196   if (dynamic_check != -1)
22197     {
22198       if (CONST_INT_P (count_exp))
22199         {
22200           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
22201             {
22202               emit_block_move_via_libcall (dst, src, count_exp, false);
22203               count_exp = const0_rtx;
22204               goto epilogue;
22205             }
22206         }
22207       else
22208         {
22209           rtx hot_label = gen_label_rtx ();
22210           jump_around_label = gen_label_rtx ();
22211           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
22212                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
22213           predict_jump (REG_BR_PROB_BASE * 90 / 100);
22214           emit_block_move_via_libcall (dst, src, count_exp, false);
22215           emit_jump (jump_around_label);
22216           emit_label (hot_label);
22217         }
22218     }
22219
22220   /* Step 2: Alignment prologue.  */
22221
22222   if (desired_align > align)
22223     {
22224       if (align_bytes == 0)
22225         {
22226           /* Except for the first move in epilogue, we no longer know
22227              constant offset in aliasing info.  It don't seems to worth
22228              the pain to maintain it for the first move, so throw away
22229              the info early.  */
22230           src = change_address (src, BLKmode, srcreg);
22231           dst = change_address (dst, BLKmode, destreg);
22232           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
22233                                   desired_align);
22234         }
22235       else
22236         {
22237           /* If we know how many bytes need to be stored before dst is
22238              sufficiently aligned, maintain aliasing info accurately.  */
22239           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
22240                                                  desired_align, align_bytes);
22241           count_exp = plus_constant (count_exp, -align_bytes);
22242           count -= align_bytes;
22243         }
22244       if (need_zero_guard
22245           && (count < (unsigned HOST_WIDE_INT) size_needed
22246               || (align_bytes == 0
22247                   && count < ((unsigned HOST_WIDE_INT) size_needed
22248                               + desired_align - align))))
22249         {
22250           /* It is possible that we copied enough so the main loop will not
22251              execute.  */
22252           gcc_assert (size_needed > 1);
22253           if (label == NULL_RTX)
22254             label = gen_label_rtx ();
22255           emit_cmp_and_jump_insns (count_exp,
22256                                    GEN_INT (size_needed),
22257                                    LTU, 0, counter_mode (count_exp), 1, label);
22258           if (expected_size == -1
22259               || expected_size < (desired_align - align) / 2 + size_needed)
22260             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22261           else
22262             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22263         }
22264     }
22265   if (label && size_needed == 1)
22266     {
22267       emit_label (label);
22268       LABEL_NUSES (label) = 1;
22269       label = NULL;
22270       epilogue_size_needed = 1;
22271     }
22272   else if (label == NULL_RTX)
22273     epilogue_size_needed = size_needed;
22274
22275   /* Step 3: Main loop.  */
22276
22277   switch (alg)
22278     {
22279     case libcall:
22280     case no_stringop:
22281       gcc_unreachable ();
22282     case loop_1_byte:
22283       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
22284                                      count_exp, QImode, 1, expected_size);
22285       break;
22286     case loop:
22287       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
22288                                      count_exp, Pmode, 1, expected_size);
22289       break;
22290     case unrolled_loop:
22291       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
22292          registers for 4 temporaries anyway.  */
22293       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
22294                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
22295                                      expected_size);
22296       break;
22297     case rep_prefix_8_byte:
22298       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
22299                                  DImode);
22300       break;
22301     case rep_prefix_4_byte:
22302       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
22303                                  SImode);
22304       break;
22305     case rep_prefix_1_byte:
22306       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
22307                                  QImode);
22308       break;
22309     }
22310   /* Adjust properly the offset of src and dest memory for aliasing.  */
22311   if (CONST_INT_P (count_exp))
22312     {
22313       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
22314                                           (count / size_needed) * size_needed);
22315       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
22316                                           (count / size_needed) * size_needed);
22317     }
22318   else
22319     {
22320       src = change_address (src, BLKmode, srcreg);
22321       dst = change_address (dst, BLKmode, destreg);
22322     }
22323
22324   /* Step 4: Epilogue to copy the remaining bytes.  */
22325  epilogue:
22326   if (label)
22327     {
22328       /* When the main loop is done, COUNT_EXP might hold original count,
22329          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
22330          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
22331          bytes. Compensate if needed.  */
22332
22333       if (size_needed < epilogue_size_needed)
22334         {
22335           tmp =
22336             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
22337                                  GEN_INT (size_needed - 1), count_exp, 1,
22338                                  OPTAB_DIRECT);
22339           if (tmp != count_exp)
22340             emit_move_insn (count_exp, tmp);
22341         }
22342       emit_label (label);
22343       LABEL_NUSES (label) = 1;
22344     }
22345
22346   if (count_exp != const0_rtx && epilogue_size_needed > 1)
22347     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
22348                             epilogue_size_needed);
22349   if (jump_around_label)
22350     emit_label (jump_around_label);
22351   return true;
22352 }
22353
22354 /* Helper function for memcpy.  For QImode value 0xXY produce
22355    0xXYXYXYXY of wide specified by MODE.  This is essentially
22356    a * 0x10101010, but we can do slightly better than
22357    synth_mult by unwinding the sequence by hand on CPUs with
22358    slow multiply.  */
22359 static rtx
22360 promote_duplicated_reg (enum machine_mode mode, rtx val)
22361 {
22362   enum machine_mode valmode = GET_MODE (val);
22363   rtx tmp;
22364   int nops = mode == DImode ? 3 : 2;
22365
22366   gcc_assert (mode == SImode || mode == DImode);
22367   if (val == const0_rtx)
22368     return copy_to_mode_reg (mode, const0_rtx);
22369   if (CONST_INT_P (val))
22370     {
22371       HOST_WIDE_INT v = INTVAL (val) & 255;
22372
22373       v |= v << 8;
22374       v |= v << 16;
22375       if (mode == DImode)
22376         v |= (v << 16) << 16;
22377       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
22378     }
22379
22380   if (valmode == VOIDmode)
22381     valmode = QImode;
22382   if (valmode != QImode)
22383     val = gen_lowpart (QImode, val);
22384   if (mode == QImode)
22385     return val;
22386   if (!TARGET_PARTIAL_REG_STALL)
22387     nops--;
22388   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
22389       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
22390       <= (ix86_cost->shift_const + ix86_cost->add) * nops
22391           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
22392     {
22393       rtx reg = convert_modes (mode, QImode, val, true);
22394       tmp = promote_duplicated_reg (mode, const1_rtx);
22395       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
22396                                   OPTAB_DIRECT);
22397     }
22398   else
22399     {
22400       rtx reg = convert_modes (mode, QImode, val, true);
22401
22402       if (!TARGET_PARTIAL_REG_STALL)
22403         if (mode == SImode)
22404           emit_insn (gen_movsi_insv_1 (reg, reg));
22405         else
22406           emit_insn (gen_movdi_insv_1 (reg, reg));
22407       else
22408         {
22409           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
22410                                      NULL, 1, OPTAB_DIRECT);
22411           reg =
22412             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
22413         }
22414       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
22415                                  NULL, 1, OPTAB_DIRECT);
22416       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
22417       if (mode == SImode)
22418         return reg;
22419       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
22420                                  NULL, 1, OPTAB_DIRECT);
22421       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
22422       return reg;
22423     }
22424 }
22425
22426 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
22427    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
22428    alignment from ALIGN to DESIRED_ALIGN.  */
22429 static rtx
22430 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
22431 {
22432   rtx promoted_val;
22433
22434   if (TARGET_64BIT
22435       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
22436     promoted_val = promote_duplicated_reg (DImode, val);
22437   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
22438     promoted_val = promote_duplicated_reg (SImode, val);
22439   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
22440     promoted_val = promote_duplicated_reg (HImode, val);
22441   else
22442     promoted_val = val;
22443
22444   return promoted_val;
22445 }
22446
22447 /* Expand string clear operation (bzero).  Use i386 string operations when
22448    profitable.  See expand_movmem comment for explanation of individual
22449    steps performed.  */
22450 bool
22451 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
22452                     rtx expected_align_exp, rtx expected_size_exp)
22453 {
22454   rtx destreg;
22455   rtx label = NULL;
22456   rtx tmp;
22457   rtx jump_around_label = NULL;
22458   HOST_WIDE_INT align = 1;
22459   unsigned HOST_WIDE_INT count = 0;
22460   HOST_WIDE_INT expected_size = -1;
22461   int size_needed = 0, epilogue_size_needed;
22462   int desired_align = 0, align_bytes = 0;
22463   enum stringop_alg alg;
22464   rtx promoted_val = NULL;
22465   bool force_loopy_epilogue = false;
22466   int dynamic_check;
22467   bool need_zero_guard = false;
22468
22469   if (CONST_INT_P (align_exp))
22470     align = INTVAL (align_exp);
22471   /* i386 can do misaligned access on reasonably increased cost.  */
22472   if (CONST_INT_P (expected_align_exp)
22473       && INTVAL (expected_align_exp) > align)
22474     align = INTVAL (expected_align_exp);
22475   if (CONST_INT_P (count_exp))
22476     count = expected_size = INTVAL (count_exp);
22477   if (CONST_INT_P (expected_size_exp) && count == 0)
22478     expected_size = INTVAL (expected_size_exp);
22479
22480   /* Make sure we don't need to care about overflow later on.  */
22481   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
22482     return false;
22483
22484   /* Step 0: Decide on preferred algorithm, desired alignment and
22485      size of chunks to be copied by main loop.  */
22486
22487   alg = decide_alg (count, expected_size, true, &dynamic_check);
22488   desired_align = decide_alignment (align, alg, expected_size);
22489
22490   if (!TARGET_ALIGN_STRINGOPS)
22491     align = desired_align;
22492
22493   if (alg == libcall)
22494     return false;
22495   gcc_assert (alg != no_stringop);
22496   if (!count)
22497     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
22498   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
22499   switch (alg)
22500     {
22501     case libcall:
22502     case no_stringop:
22503       gcc_unreachable ();
22504     case loop:
22505       need_zero_guard = true;
22506       size_needed = GET_MODE_SIZE (Pmode);
22507       break;
22508     case unrolled_loop:
22509       need_zero_guard = true;
22510       size_needed = GET_MODE_SIZE (Pmode) * 4;
22511       break;
22512     case rep_prefix_8_byte:
22513       size_needed = 8;
22514       break;
22515     case rep_prefix_4_byte:
22516       size_needed = 4;
22517       break;
22518     case rep_prefix_1_byte:
22519       size_needed = 1;
22520       break;
22521     case loop_1_byte:
22522       need_zero_guard = true;
22523       size_needed = 1;
22524       break;
22525     }
22526   epilogue_size_needed = size_needed;
22527
22528   /* Step 1: Prologue guard.  */
22529
22530   /* Alignment code needs count to be in register.  */
22531   if (CONST_INT_P (count_exp) && desired_align > align)
22532     {
22533       if (INTVAL (count_exp) > desired_align
22534           && INTVAL (count_exp) > size_needed)
22535         {
22536           align_bytes
22537             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
22538           if (align_bytes <= 0)
22539             align_bytes = 0;
22540           else
22541             align_bytes = desired_align - align_bytes;
22542         }
22543       if (align_bytes == 0)
22544         {
22545           enum machine_mode mode = SImode;
22546           if (TARGET_64BIT && (count & ~0xffffffff))
22547             mode = DImode;
22548           count_exp = force_reg (mode, count_exp);
22549         }
22550     }
22551   /* Do the cheap promotion to allow better CSE across the
22552      main loop and epilogue (ie one load of the big constant in the
22553      front of all code.  */
22554   if (CONST_INT_P (val_exp))
22555     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
22556                                                    desired_align, align);
22557   /* Ensure that alignment prologue won't copy past end of block.  */
22558   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
22559     {
22560       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
22561       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
22562          Make sure it is power of 2.  */
22563       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
22564
22565       /* To improve performance of small blocks, we jump around the VAL
22566          promoting mode.  This mean that if the promoted VAL is not constant,
22567          we might not use it in the epilogue and have to use byte
22568          loop variant.  */
22569       if (epilogue_size_needed > 2 && !promoted_val)
22570         force_loopy_epilogue = true;
22571       if (count)
22572         {
22573           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
22574             {
22575               /* If main algorithm works on QImode, no epilogue is needed.
22576                  For small sizes just don't align anything.  */
22577               if (size_needed == 1)
22578                 desired_align = align;
22579               else
22580                 goto epilogue;
22581             }
22582         }
22583       else
22584         {
22585           label = gen_label_rtx ();
22586           emit_cmp_and_jump_insns (count_exp,
22587                                    GEN_INT (epilogue_size_needed),
22588                                    LTU, 0, counter_mode (count_exp), 1, label);
22589           if (expected_size == -1 || expected_size <= epilogue_size_needed)
22590             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22591           else
22592             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22593         }
22594     }
22595   if (dynamic_check != -1)
22596     {
22597       rtx hot_label = gen_label_rtx ();
22598       jump_around_label = gen_label_rtx ();
22599       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
22600                                LEU, 0, counter_mode (count_exp), 1, hot_label);
22601       predict_jump (REG_BR_PROB_BASE * 90 / 100);
22602       set_storage_via_libcall (dst, count_exp, val_exp, false);
22603       emit_jump (jump_around_label);
22604       emit_label (hot_label);
22605     }
22606
22607   /* Step 2: Alignment prologue.  */
22608
22609   /* Do the expensive promotion once we branched off the small blocks.  */
22610   if (!promoted_val)
22611     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
22612                                                    desired_align, align);
22613   gcc_assert (desired_align >= 1 && align >= 1);
22614
22615   if (desired_align > align)
22616     {
22617       if (align_bytes == 0)
22618         {
22619           /* Except for the first move in epilogue, we no longer know
22620              constant offset in aliasing info.  It don't seems to worth
22621              the pain to maintain it for the first move, so throw away
22622              the info early.  */
22623           dst = change_address (dst, BLKmode, destreg);
22624           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
22625                                   desired_align);
22626         }
22627       else
22628         {
22629           /* If we know how many bytes need to be stored before dst is
22630              sufficiently aligned, maintain aliasing info accurately.  */
22631           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
22632                                                  desired_align, align_bytes);
22633           count_exp = plus_constant (count_exp, -align_bytes);
22634           count -= align_bytes;
22635         }
22636       if (need_zero_guard
22637           && (count < (unsigned HOST_WIDE_INT) size_needed
22638               || (align_bytes == 0
22639                   && count < ((unsigned HOST_WIDE_INT) size_needed
22640                               + desired_align - align))))
22641         {
22642           /* It is possible that we copied enough so the main loop will not
22643              execute.  */
22644           gcc_assert (size_needed > 1);
22645           if (label == NULL_RTX)
22646             label = gen_label_rtx ();
22647           emit_cmp_and_jump_insns (count_exp,
22648                                    GEN_INT (size_needed),
22649                                    LTU, 0, counter_mode (count_exp), 1, label);
22650           if (expected_size == -1
22651               || expected_size < (desired_align - align) / 2 + size_needed)
22652             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22653           else
22654             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22655         }
22656     }
22657   if (label && size_needed == 1)
22658     {
22659       emit_label (label);
22660       LABEL_NUSES (label) = 1;
22661       label = NULL;
22662       promoted_val = val_exp;
22663       epilogue_size_needed = 1;
22664     }
22665   else if (label == NULL_RTX)
22666     epilogue_size_needed = size_needed;
22667
22668   /* Step 3: Main loop.  */
22669
22670   switch (alg)
22671     {
22672     case libcall:
22673     case no_stringop:
22674       gcc_unreachable ();
22675     case loop_1_byte:
22676       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
22677                                      count_exp, QImode, 1, expected_size);
22678       break;
22679     case loop:
22680       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
22681                                      count_exp, Pmode, 1, expected_size);
22682       break;
22683     case unrolled_loop:
22684       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
22685                                      count_exp, Pmode, 4, expected_size);
22686       break;
22687     case rep_prefix_8_byte:
22688       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
22689                                   DImode, val_exp);
22690       break;
22691     case rep_prefix_4_byte:
22692       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
22693                                   SImode, val_exp);
22694       break;
22695     case rep_prefix_1_byte:
22696       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
22697                                   QImode, val_exp);
22698       break;
22699     }
22700   /* Adjust properly the offset of src and dest memory for aliasing.  */
22701   if (CONST_INT_P (count_exp))
22702     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
22703                                         (count / size_needed) * size_needed);
22704   else
22705     dst = change_address (dst, BLKmode, destreg);
22706
22707   /* Step 4: Epilogue to copy the remaining bytes.  */
22708
22709   if (label)
22710     {
22711       /* When the main loop is done, COUNT_EXP might hold original count,
22712          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
22713          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
22714          bytes. Compensate if needed.  */
22715
22716       if (size_needed < epilogue_size_needed)
22717         {
22718           tmp =
22719             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
22720                                  GEN_INT (size_needed - 1), count_exp, 1,
22721                                  OPTAB_DIRECT);
22722           if (tmp != count_exp)
22723             emit_move_insn (count_exp, tmp);
22724         }
22725       emit_label (label);
22726       LABEL_NUSES (label) = 1;
22727     }
22728  epilogue:
22729   if (count_exp != const0_rtx && epilogue_size_needed > 1)
22730     {
22731       if (force_loopy_epilogue)
22732         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
22733                                          epilogue_size_needed);
22734       else
22735         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
22736                                 epilogue_size_needed);
22737     }
22738   if (jump_around_label)
22739     emit_label (jump_around_label);
22740   return true;
22741 }
22742
22743 /* Expand the appropriate insns for doing strlen if not just doing
22744    repnz; scasb
22745
22746    out = result, initialized with the start address
22747    align_rtx = alignment of the address.
22748    scratch = scratch register, initialized with the startaddress when
22749         not aligned, otherwise undefined
22750
22751    This is just the body. It needs the initializations mentioned above and
22752    some address computing at the end.  These things are done in i386.md.  */
22753
22754 static void
22755 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
22756 {
22757   int align;
22758   rtx tmp;
22759   rtx align_2_label = NULL_RTX;
22760   rtx align_3_label = NULL_RTX;
22761   rtx align_4_label = gen_label_rtx ();
22762   rtx end_0_label = gen_label_rtx ();
22763   rtx mem;
22764   rtx tmpreg = gen_reg_rtx (SImode);
22765   rtx scratch = gen_reg_rtx (SImode);
22766   rtx cmp;
22767
22768   align = 0;
22769   if (CONST_INT_P (align_rtx))
22770     align = INTVAL (align_rtx);
22771
22772   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
22773
22774   /* Is there a known alignment and is it less than 4?  */
22775   if (align < 4)
22776     {
22777       rtx scratch1 = gen_reg_rtx (Pmode);
22778       emit_move_insn (scratch1, out);
22779       /* Is there a known alignment and is it not 2? */
22780       if (align != 2)
22781         {
22782           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
22783           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
22784
22785           /* Leave just the 3 lower bits.  */
22786           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
22787                                     NULL_RTX, 0, OPTAB_WIDEN);
22788
22789           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
22790                                    Pmode, 1, align_4_label);
22791           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
22792                                    Pmode, 1, align_2_label);
22793           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
22794                                    Pmode, 1, align_3_label);
22795         }
22796       else
22797         {
22798           /* Since the alignment is 2, we have to check 2 or 0 bytes;
22799              check if is aligned to 4 - byte.  */
22800
22801           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
22802                                     NULL_RTX, 0, OPTAB_WIDEN);
22803
22804           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
22805                                    Pmode, 1, align_4_label);
22806         }
22807
22808       mem = change_address (src, QImode, out);
22809
22810       /* Now compare the bytes.  */
22811
22812       /* Compare the first n unaligned byte on a byte per byte basis.  */
22813       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
22814                                QImode, 1, end_0_label);
22815
22816       /* Increment the address.  */
22817       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
22818
22819       /* Not needed with an alignment of 2 */
22820       if (align != 2)
22821         {
22822           emit_label (align_2_label);
22823
22824           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
22825                                    end_0_label);
22826
22827           emit_insn (ix86_gen_add3 (out, out, const1_rtx));
22828
22829           emit_label (align_3_label);
22830         }
22831
22832       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
22833                                end_0_label);
22834
22835       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
22836     }
22837
22838   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
22839      align this loop.  It gives only huge programs, but does not help to
22840      speed up.  */
22841   emit_label (align_4_label);
22842
22843   mem = change_address (src, SImode, out);
22844   emit_move_insn (scratch, mem);
22845   emit_insn (ix86_gen_add3 (out, out, GEN_INT (4)));
22846
22847   /* This formula yields a nonzero result iff one of the bytes is zero.
22848      This saves three branches inside loop and many cycles.  */
22849
22850   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
22851   emit_insn (gen_one_cmplsi2 (scratch, scratch));
22852   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
22853   emit_insn (gen_andsi3 (tmpreg, tmpreg,
22854                          gen_int_mode (0x80808080, SImode)));
22855   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
22856                            align_4_label);
22857
22858   if (TARGET_CMOVE)
22859     {
22860        rtx reg = gen_reg_rtx (SImode);
22861        rtx reg2 = gen_reg_rtx (Pmode);
22862        emit_move_insn (reg, tmpreg);
22863        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
22864
22865        /* If zero is not in the first two bytes, move two bytes forward.  */
22866        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
22867        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
22868        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
22869        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
22870                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
22871                                                      reg,
22872                                                      tmpreg)));
22873        /* Emit lea manually to avoid clobbering of flags.  */
22874        emit_insn (gen_rtx_SET (SImode, reg2,
22875                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
22876
22877        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
22878        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
22879        emit_insn (gen_rtx_SET (VOIDmode, out,
22880                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
22881                                                      reg2,
22882                                                      out)));
22883     }
22884   else
22885     {
22886        rtx end_2_label = gen_label_rtx ();
22887        /* Is zero in the first two bytes? */
22888
22889        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
22890        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
22891        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
22892        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
22893                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
22894                             pc_rtx);
22895        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
22896        JUMP_LABEL (tmp) = end_2_label;
22897
22898        /* Not in the first two.  Move two bytes forward.  */
22899        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
22900        emit_insn (ix86_gen_add3 (out, out, const2_rtx));
22901
22902        emit_label (end_2_label);
22903
22904     }
22905
22906   /* Avoid branch in fixing the byte.  */
22907   tmpreg = gen_lowpart (QImode, tmpreg);
22908   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
22909   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
22910   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
22911   emit_insn (ix86_gen_sub3_carry (out, out, GEN_INT (3), tmp, cmp));
22912
22913   emit_label (end_0_label);
22914 }
22915
22916 /* Expand strlen.  */
22917
22918 bool
22919 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
22920 {
22921   rtx addr, scratch1, scratch2, scratch3, scratch4;
22922
22923   /* The generic case of strlen expander is long.  Avoid it's
22924      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
22925
22926   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
22927       && !TARGET_INLINE_ALL_STRINGOPS
22928       && !optimize_insn_for_size_p ()
22929       && (!CONST_INT_P (align) || INTVAL (align) < 4))
22930     return false;
22931
22932   addr = force_reg (Pmode, XEXP (src, 0));
22933   scratch1 = gen_reg_rtx (Pmode);
22934
22935   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
22936       && !optimize_insn_for_size_p ())
22937     {
22938       /* Well it seems that some optimizer does not combine a call like
22939          foo(strlen(bar), strlen(bar));
22940          when the move and the subtraction is done here.  It does calculate
22941          the length just once when these instructions are done inside of
22942          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
22943          often used and I use one fewer register for the lifetime of
22944          output_strlen_unroll() this is better.  */
22945
22946       emit_move_insn (out, addr);
22947
22948       ix86_expand_strlensi_unroll_1 (out, src, align);
22949
22950       /* strlensi_unroll_1 returns the address of the zero at the end of
22951          the string, like memchr(), so compute the length by subtracting
22952          the start address.  */
22953       emit_insn (ix86_gen_sub3 (out, out, addr));
22954     }
22955   else
22956     {
22957       rtx unspec;
22958
22959       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
22960       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
22961         return false;
22962
22963       scratch2 = gen_reg_rtx (Pmode);
22964       scratch3 = gen_reg_rtx (Pmode);
22965       scratch4 = force_reg (Pmode, constm1_rtx);
22966
22967       emit_move_insn (scratch3, addr);
22968       eoschar = force_reg (QImode, eoschar);
22969
22970       src = replace_equiv_address_nv (src, scratch3);
22971
22972       /* If .md starts supporting :P, this can be done in .md.  */
22973       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
22974                                                  scratch4), UNSPEC_SCAS);
22975       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
22976       emit_insn (ix86_gen_one_cmpl2 (scratch2, scratch1));
22977       emit_insn (ix86_gen_add3 (out, scratch2, constm1_rtx));
22978     }
22979   return true;
22980 }
22981
22982 /* For given symbol (function) construct code to compute address of it's PLT
22983    entry in large x86-64 PIC model.  */
22984 rtx
22985 construct_plt_address (rtx symbol)
22986 {
22987   rtx tmp = gen_reg_rtx (Pmode);
22988   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
22989
22990   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
22991   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
22992
22993   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
22994   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
22995   return tmp;
22996 }
22997
22998 rtx
22999 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
23000                   rtx callarg2,
23001                   rtx pop, bool sibcall)
23002 {
23003   /* We need to represent that SI and DI registers are clobbered
23004      by SYSV calls.  */
23005   static int clobbered_registers[] = {
23006         XMM6_REG, XMM7_REG, XMM8_REG,
23007         XMM9_REG, XMM10_REG, XMM11_REG,
23008         XMM12_REG, XMM13_REG, XMM14_REG,
23009         XMM15_REG, SI_REG, DI_REG
23010   };
23011   rtx vec[ARRAY_SIZE (clobbered_registers) + 3];
23012   rtx use = NULL, call;
23013   unsigned int vec_len;
23014
23015   if (pop == const0_rtx)
23016     pop = NULL;
23017   gcc_assert (!TARGET_64BIT || !pop);
23018
23019   if (TARGET_MACHO && !TARGET_64BIT)
23020     {
23021 #if TARGET_MACHO
23022       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
23023         fnaddr = machopic_indirect_call_target (fnaddr);
23024 #endif
23025     }
23026   else
23027     {
23028       /* Static functions and indirect calls don't need the pic register.  */
23029       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
23030           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
23031           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
23032         use_reg (&use, pic_offset_table_rtx);
23033     }
23034
23035   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
23036     {
23037       rtx al = gen_rtx_REG (QImode, AX_REG);
23038       emit_move_insn (al, callarg2);
23039       use_reg (&use, al);
23040     }
23041
23042   if (ix86_cmodel == CM_LARGE_PIC
23043       && MEM_P (fnaddr)
23044       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
23045       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
23046     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
23047   else if (sibcall
23048            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
23049            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
23050     {
23051       fnaddr = XEXP (fnaddr, 0);
23052       if (GET_MODE (fnaddr) != Pmode)
23053         fnaddr = convert_to_mode (Pmode, fnaddr, 1);
23054       fnaddr = gen_rtx_MEM (QImode, copy_to_mode_reg (Pmode, fnaddr));
23055     }
23056
23057   vec_len = 0;
23058   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
23059   if (retval)
23060     call = gen_rtx_SET (VOIDmode, retval, call);
23061   vec[vec_len++] = call;
23062
23063   if (pop)
23064     {
23065       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
23066       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
23067       vec[vec_len++] = pop;
23068     }
23069
23070   if (TARGET_64BIT_MS_ABI
23071       && (!callarg2 || INTVAL (callarg2) != -2))
23072     {
23073       unsigned i;
23074
23075       vec[vec_len++] = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
23076                                        UNSPEC_MS_TO_SYSV_CALL);
23077
23078       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
23079         vec[vec_len++]
23080           = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
23081                              ? TImode : DImode,
23082                              gen_rtx_REG (SSE_REGNO_P (clobbered_registers[i])
23083                                           ? TImode : DImode,
23084                                           clobbered_registers[i]));
23085     }
23086
23087   /* Add UNSPEC_CALL_NEEDS_VZEROUPPER decoration.  */
23088   if (TARGET_VZEROUPPER)
23089     {
23090       int avx256;
23091       if (cfun->machine->callee_pass_avx256_p)
23092         {
23093           if (cfun->machine->callee_return_avx256_p)
23094             avx256 = callee_return_pass_avx256;
23095           else
23096             avx256 = callee_pass_avx256;
23097         }
23098       else if (cfun->machine->callee_return_avx256_p)
23099         avx256 = callee_return_avx256;
23100       else
23101         avx256 = call_no_avx256;
23102
23103       if (reload_completed)
23104         emit_insn (gen_avx_vzeroupper (GEN_INT (avx256)));
23105       else
23106         vec[vec_len++] = gen_rtx_UNSPEC (VOIDmode,
23107                                          gen_rtvec (1, GEN_INT (avx256)),
23108                                          UNSPEC_CALL_NEEDS_VZEROUPPER);
23109     }
23110
23111   if (vec_len > 1)
23112     call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (vec_len, vec));
23113   call = emit_call_insn (call);
23114   if (use)
23115     CALL_INSN_FUNCTION_USAGE (call) = use;
23116
23117   return call;
23118 }
23119
23120 void
23121 ix86_split_call_vzeroupper (rtx insn, rtx vzeroupper)
23122 {
23123   rtx pat = PATTERN (insn);
23124   rtvec vec = XVEC (pat, 0);
23125   int len = GET_NUM_ELEM (vec) - 1;
23126
23127   /* Strip off the last entry of the parallel.  */
23128   gcc_assert (GET_CODE (RTVEC_ELT (vec, len)) == UNSPEC);
23129   gcc_assert (XINT (RTVEC_ELT (vec, len), 1) == UNSPEC_CALL_NEEDS_VZEROUPPER);
23130   if (len == 1)
23131     pat = RTVEC_ELT (vec, 0);
23132   else
23133     pat = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (len, &RTVEC_ELT (vec, 0)));
23134
23135   emit_insn (gen_avx_vzeroupper (vzeroupper));
23136   emit_call_insn (pat);
23137 }
23138
23139 /* Output the assembly for a call instruction.  */
23140
23141 const char *
23142 ix86_output_call_insn (rtx insn, rtx call_op)
23143 {
23144   bool direct_p = constant_call_address_operand (call_op, Pmode);
23145   bool seh_nop_p = false;
23146   const char *xasm;
23147
23148   if (SIBLING_CALL_P (insn))
23149     {
23150       if (direct_p)
23151         xasm = "jmp\t%P0";
23152       /* SEH epilogue detection requires the indirect branch case
23153          to include REX.W.  */
23154       else if (TARGET_SEH)
23155         xasm = "rex.W jmp %A0";
23156       else
23157         xasm = "jmp\t%A0";
23158
23159       output_asm_insn (xasm, &call_op);
23160       return "";
23161     }
23162
23163   /* SEH unwinding can require an extra nop to be emitted in several
23164      circumstances.  Determine if we have one of those.  */
23165   if (TARGET_SEH)
23166     {
23167       rtx i;
23168
23169       for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
23170         {
23171           /* If we get to another real insn, we don't need the nop.  */
23172           if (INSN_P (i))
23173             break;
23174
23175           /* If we get to the epilogue note, prevent a catch region from
23176              being adjacent to the standard epilogue sequence.  If non-
23177              call-exceptions, we'll have done this during epilogue emission. */
23178           if (NOTE_P (i) && NOTE_KIND (i) == NOTE_INSN_EPILOGUE_BEG
23179               && !flag_non_call_exceptions
23180               && !can_throw_internal (insn))
23181             {
23182               seh_nop_p = true;
23183               break;
23184             }
23185         }
23186
23187       /* If we didn't find a real insn following the call, prevent the
23188          unwinder from looking into the next function.  */
23189       if (i == NULL)
23190         seh_nop_p = true;
23191     }
23192
23193   if (direct_p)
23194     xasm = "call\t%P0";
23195   else
23196     xasm = "call\t%A0";
23197
23198   output_asm_insn (xasm, &call_op);
23199
23200   if (seh_nop_p)
23201     return "nop";
23202
23203   return "";
23204 }
23205 \f
23206 /* Clear stack slot assignments remembered from previous functions.
23207    This is called from INIT_EXPANDERS once before RTL is emitted for each
23208    function.  */
23209
23210 static struct machine_function *
23211 ix86_init_machine_status (void)
23212 {
23213   struct machine_function *f;
23214
23215   f = ggc_alloc_cleared_machine_function ();
23216   f->use_fast_prologue_epilogue_nregs = -1;
23217   f->tls_descriptor_call_expanded_p = 0;
23218   f->call_abi = ix86_abi;
23219
23220   return f;
23221 }
23222
23223 /* Return a MEM corresponding to a stack slot with mode MODE.
23224    Allocate a new slot if necessary.
23225
23226    The RTL for a function can have several slots available: N is
23227    which slot to use.  */
23228
23229 rtx
23230 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
23231 {
23232   struct stack_local_entry *s;
23233
23234   gcc_assert (n < MAX_386_STACK_LOCALS);
23235
23236   /* Virtual slot is valid only before vregs are instantiated.  */
23237   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
23238
23239   for (s = ix86_stack_locals; s; s = s->next)
23240     if (s->mode == mode && s->n == n)
23241       return validize_mem (copy_rtx (s->rtl));
23242
23243   s = ggc_alloc_stack_local_entry ();
23244   s->n = n;
23245   s->mode = mode;
23246   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
23247
23248   s->next = ix86_stack_locals;
23249   ix86_stack_locals = s;
23250   return validize_mem (s->rtl);
23251 }
23252 \f
23253 /* Calculate the length of the memory address in the instruction encoding.
23254    Includes addr32 prefix, does not include the one-byte modrm, opcode,
23255    or other prefixes.  */
23256
23257 int
23258 memory_address_length (rtx addr)
23259 {
23260   struct ix86_address parts;
23261   rtx base, index, disp;
23262   int len;
23263   int ok;
23264
23265   if (GET_CODE (addr) == PRE_DEC
23266       || GET_CODE (addr) == POST_INC
23267       || GET_CODE (addr) == PRE_MODIFY
23268       || GET_CODE (addr) == POST_MODIFY)
23269     return 0;
23270
23271   ok = ix86_decompose_address (addr, &parts);
23272   gcc_assert (ok);
23273
23274   if (parts.base && GET_CODE (parts.base) == SUBREG)
23275     parts.base = SUBREG_REG (parts.base);
23276   if (parts.index && GET_CODE (parts.index) == SUBREG)
23277     parts.index = SUBREG_REG (parts.index);
23278
23279   base = parts.base;
23280   index = parts.index;
23281   disp = parts.disp;
23282
23283   /* Add length of addr32 prefix.  */
23284   len = (GET_CODE (addr) == ZERO_EXTEND
23285          || GET_CODE (addr) == AND);
23286
23287   /* Rule of thumb:
23288        - esp as the base always wants an index,
23289        - ebp as the base always wants a displacement,
23290        - r12 as the base always wants an index,
23291        - r13 as the base always wants a displacement.  */
23292
23293   /* Register Indirect.  */
23294   if (base && !index && !disp)
23295     {
23296       /* esp (for its index) and ebp (for its displacement) need
23297          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
23298          code.  */
23299       if (REG_P (addr)
23300           && (addr == arg_pointer_rtx
23301               || addr == frame_pointer_rtx
23302               || REGNO (addr) == SP_REG
23303               || REGNO (addr) == BP_REG
23304               || REGNO (addr) == R12_REG
23305               || REGNO (addr) == R13_REG))
23306         len = 1;
23307     }
23308
23309   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
23310      is not disp32, but disp32(%rip), so for disp32
23311      SIB byte is needed, unless print_operand_address
23312      optimizes it into disp32(%rip) or (%rip) is implied
23313      by UNSPEC.  */
23314   else if (disp && !base && !index)
23315     {
23316       len = 4;
23317       if (TARGET_64BIT)
23318         {
23319           rtx symbol = disp;
23320
23321           if (GET_CODE (disp) == CONST)
23322             symbol = XEXP (disp, 0);
23323           if (GET_CODE (symbol) == PLUS
23324               && CONST_INT_P (XEXP (symbol, 1)))
23325             symbol = XEXP (symbol, 0);
23326
23327           if (GET_CODE (symbol) != LABEL_REF
23328               && (GET_CODE (symbol) != SYMBOL_REF
23329                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
23330               && (GET_CODE (symbol) != UNSPEC
23331                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
23332                       && XINT (symbol, 1) != UNSPEC_PCREL
23333                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
23334             len += 1;
23335         }
23336     }
23337
23338   else
23339     {
23340       /* Find the length of the displacement constant.  */
23341       if (disp)
23342         {
23343           if (base && satisfies_constraint_K (disp))
23344             len = 1;
23345           else
23346             len = 4;
23347         }
23348       /* ebp always wants a displacement.  Similarly r13.  */
23349       else if (base && REG_P (base)
23350                && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
23351         len = 1;
23352
23353       /* An index requires the two-byte modrm form....  */
23354       if (index
23355           /* ...like esp (or r12), which always wants an index.  */
23356           || base == arg_pointer_rtx
23357           || base == frame_pointer_rtx
23358           || (base && REG_P (base)
23359               && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
23360         len += 1;
23361     }
23362
23363   switch (parts.seg)
23364     {
23365     case SEG_FS:
23366     case SEG_GS:
23367       len += 1;
23368       break;
23369     default:
23370       break;
23371     }
23372
23373   return len;
23374 }
23375
23376 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
23377    is set, expect that insn have 8bit immediate alternative.  */
23378 int
23379 ix86_attr_length_immediate_default (rtx insn, bool shortform)
23380 {
23381   int len = 0;
23382   int i;
23383   extract_insn_cached (insn);
23384   for (i = recog_data.n_operands - 1; i >= 0; --i)
23385     if (CONSTANT_P (recog_data.operand[i]))
23386       {
23387         enum attr_mode mode = get_attr_mode (insn);
23388
23389         gcc_assert (!len);
23390         if (shortform && CONST_INT_P (recog_data.operand[i]))
23391           {
23392             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
23393             switch (mode)
23394               {
23395               case MODE_QI:
23396                 len = 1;
23397                 continue;
23398               case MODE_HI:
23399                 ival = trunc_int_for_mode (ival, HImode);
23400                 break;
23401               case MODE_SI:
23402                 ival = trunc_int_for_mode (ival, SImode);
23403                 break;
23404               default:
23405                 break;
23406               }
23407             if (IN_RANGE (ival, -128, 127))
23408               {
23409                 len = 1;
23410                 continue;
23411               }
23412           }
23413         switch (mode)
23414           {
23415           case MODE_QI:
23416             len = 1;
23417             break;
23418           case MODE_HI:
23419             len = 2;
23420             break;
23421           case MODE_SI:
23422             len = 4;
23423             break;
23424           /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
23425           case MODE_DI:
23426             len = 4;
23427             break;
23428           default:
23429             fatal_insn ("unknown insn mode", insn);
23430         }
23431       }
23432   return len;
23433 }
23434 /* Compute default value for "length_address" attribute.  */
23435 int
23436 ix86_attr_length_address_default (rtx insn)
23437 {
23438   int i;
23439
23440   if (get_attr_type (insn) == TYPE_LEA)
23441     {
23442       rtx set = PATTERN (insn), addr;
23443
23444       if (GET_CODE (set) == PARALLEL)
23445         set = XVECEXP (set, 0, 0);
23446
23447       gcc_assert (GET_CODE (set) == SET);
23448
23449       addr = SET_SRC (set);
23450       if (TARGET_64BIT && get_attr_mode (insn) == MODE_SI)
23451         {
23452           if (GET_CODE (addr) == ZERO_EXTEND)
23453             addr = XEXP (addr, 0);
23454           if (GET_CODE (addr) == SUBREG)
23455             addr = SUBREG_REG (addr);
23456         }
23457
23458       return memory_address_length (addr);
23459     }
23460
23461   extract_insn_cached (insn);
23462   for (i = recog_data.n_operands - 1; i >= 0; --i)
23463     if (MEM_P (recog_data.operand[i]))
23464       {
23465         constrain_operands_cached (reload_completed);
23466         if (which_alternative != -1)
23467           {
23468             const char *constraints = recog_data.constraints[i];
23469             int alt = which_alternative;
23470
23471             while (*constraints == '=' || *constraints == '+')
23472               constraints++;
23473             while (alt-- > 0)
23474               while (*constraints++ != ',')
23475                 ;
23476             /* Skip ignored operands.  */
23477             if (*constraints == 'X')
23478               continue;
23479           }
23480         return memory_address_length (XEXP (recog_data.operand[i], 0));
23481       }
23482   return 0;
23483 }
23484
23485 /* Compute default value for "length_vex" attribute. It includes
23486    2 or 3 byte VEX prefix and 1 opcode byte.  */
23487
23488 int
23489 ix86_attr_length_vex_default (rtx insn, bool has_0f_opcode, bool has_vex_w)
23490 {
23491   int i;
23492
23493   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
23494      byte VEX prefix.  */
23495   if (!has_0f_opcode || has_vex_w)
23496     return 3 + 1;
23497
23498  /* We can always use 2 byte VEX prefix in 32bit.  */
23499   if (!TARGET_64BIT)
23500     return 2 + 1;
23501
23502   extract_insn_cached (insn);
23503
23504   for (i = recog_data.n_operands - 1; i >= 0; --i)
23505     if (REG_P (recog_data.operand[i]))
23506       {
23507         /* REX.W bit uses 3 byte VEX prefix.  */
23508         if (GET_MODE (recog_data.operand[i]) == DImode
23509             && GENERAL_REG_P (recog_data.operand[i]))
23510           return 3 + 1;
23511       }
23512     else
23513       {
23514         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
23515         if (MEM_P (recog_data.operand[i])
23516             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
23517           return 3 + 1;
23518       }
23519
23520   return 2 + 1;
23521 }
23522 \f
23523 /* Return the maximum number of instructions a cpu can issue.  */
23524
23525 static int
23526 ix86_issue_rate (void)
23527 {
23528   switch (ix86_tune)
23529     {
23530     case PROCESSOR_PENTIUM:
23531     case PROCESSOR_ATOM:
23532     case PROCESSOR_K6:
23533       return 2;
23534
23535     case PROCESSOR_PENTIUMPRO:
23536     case PROCESSOR_PENTIUM4:
23537     case PROCESSOR_CORE2_32:
23538     case PROCESSOR_CORE2_64:
23539     case PROCESSOR_COREI7_32:
23540     case PROCESSOR_COREI7_64:
23541     case PROCESSOR_ATHLON:
23542     case PROCESSOR_K8:
23543     case PROCESSOR_AMDFAM10:
23544     case PROCESSOR_NOCONA:
23545     case PROCESSOR_GENERIC32:
23546     case PROCESSOR_GENERIC64:
23547     case PROCESSOR_BDVER1:
23548     case PROCESSOR_BDVER2:
23549     case PROCESSOR_BTVER1:
23550       return 3;
23551
23552     default:
23553       return 1;
23554     }
23555 }
23556
23557 /* A subroutine of ix86_adjust_cost -- return TRUE iff INSN reads flags set
23558    by DEP_INSN and nothing set by DEP_INSN.  */
23559
23560 static bool
23561 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
23562 {
23563   rtx set, set2;
23564
23565   /* Simplify the test for uninteresting insns.  */
23566   if (insn_type != TYPE_SETCC
23567       && insn_type != TYPE_ICMOV
23568       && insn_type != TYPE_FCMOV
23569       && insn_type != TYPE_IBR)
23570     return false;
23571
23572   if ((set = single_set (dep_insn)) != 0)
23573     {
23574       set = SET_DEST (set);
23575       set2 = NULL_RTX;
23576     }
23577   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
23578            && XVECLEN (PATTERN (dep_insn), 0) == 2
23579            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
23580            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
23581     {
23582       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
23583       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
23584     }
23585   else
23586     return false;
23587
23588   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
23589     return false;
23590
23591   /* This test is true if the dependent insn reads the flags but
23592      not any other potentially set register.  */
23593   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
23594     return false;
23595
23596   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
23597     return false;
23598
23599   return true;
23600 }
23601
23602 /* Return true iff USE_INSN has a memory address with operands set by
23603    SET_INSN.  */
23604
23605 bool
23606 ix86_agi_dependent (rtx set_insn, rtx use_insn)
23607 {
23608   int i;
23609   extract_insn_cached (use_insn);
23610   for (i = recog_data.n_operands - 1; i >= 0; --i)
23611     if (MEM_P (recog_data.operand[i]))
23612       {
23613         rtx addr = XEXP (recog_data.operand[i], 0);
23614         return modified_in_p (addr, set_insn) != 0;
23615       }
23616   return false;
23617 }
23618
23619 static int
23620 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
23621 {
23622   enum attr_type insn_type, dep_insn_type;
23623   enum attr_memory memory;
23624   rtx set, set2;
23625   int dep_insn_code_number;
23626
23627   /* Anti and output dependencies have zero cost on all CPUs.  */
23628   if (REG_NOTE_KIND (link) != 0)
23629     return 0;
23630
23631   dep_insn_code_number = recog_memoized (dep_insn);
23632
23633   /* If we can't recognize the insns, we can't really do anything.  */
23634   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
23635     return cost;
23636
23637   insn_type = get_attr_type (insn);
23638   dep_insn_type = get_attr_type (dep_insn);
23639
23640   switch (ix86_tune)
23641     {
23642     case PROCESSOR_PENTIUM:
23643       /* Address Generation Interlock adds a cycle of latency.  */
23644       if (insn_type == TYPE_LEA)
23645         {
23646           rtx addr = PATTERN (insn);
23647
23648           if (GET_CODE (addr) == PARALLEL)
23649             addr = XVECEXP (addr, 0, 0);
23650
23651           gcc_assert (GET_CODE (addr) == SET);
23652
23653           addr = SET_SRC (addr);
23654           if (modified_in_p (addr, dep_insn))
23655             cost += 1;
23656         }
23657       else if (ix86_agi_dependent (dep_insn, insn))
23658         cost += 1;
23659
23660       /* ??? Compares pair with jump/setcc.  */
23661       if (ix86_flags_dependent (insn, dep_insn, insn_type))
23662         cost = 0;
23663
23664       /* Floating point stores require value to be ready one cycle earlier.  */
23665       if (insn_type == TYPE_FMOV
23666           && get_attr_memory (insn) == MEMORY_STORE
23667           && !ix86_agi_dependent (dep_insn, insn))
23668         cost += 1;
23669       break;
23670
23671     case PROCESSOR_PENTIUMPRO:
23672       memory = get_attr_memory (insn);
23673
23674       /* INT->FP conversion is expensive.  */
23675       if (get_attr_fp_int_src (dep_insn))
23676         cost += 5;
23677
23678       /* There is one cycle extra latency between an FP op and a store.  */
23679       if (insn_type == TYPE_FMOV
23680           && (set = single_set (dep_insn)) != NULL_RTX
23681           && (set2 = single_set (insn)) != NULL_RTX
23682           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
23683           && MEM_P (SET_DEST (set2)))
23684         cost += 1;
23685
23686       /* Show ability of reorder buffer to hide latency of load by executing
23687          in parallel with previous instruction in case
23688          previous instruction is not needed to compute the address.  */
23689       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
23690           && !ix86_agi_dependent (dep_insn, insn))
23691         {
23692           /* Claim moves to take one cycle, as core can issue one load
23693              at time and the next load can start cycle later.  */
23694           if (dep_insn_type == TYPE_IMOV
23695               || dep_insn_type == TYPE_FMOV)
23696             cost = 1;
23697           else if (cost > 1)
23698             cost--;
23699         }
23700       break;
23701
23702     case PROCESSOR_K6:
23703       memory = get_attr_memory (insn);
23704
23705       /* The esp dependency is resolved before the instruction is really
23706          finished.  */
23707       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
23708           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
23709         return 1;
23710
23711       /* INT->FP conversion is expensive.  */
23712       if (get_attr_fp_int_src (dep_insn))
23713         cost += 5;
23714
23715       /* Show ability of reorder buffer to hide latency of load by executing
23716          in parallel with previous instruction in case
23717          previous instruction is not needed to compute the address.  */
23718       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
23719           && !ix86_agi_dependent (dep_insn, insn))
23720         {
23721           /* Claim moves to take one cycle, as core can issue one load
23722              at time and the next load can start cycle later.  */
23723           if (dep_insn_type == TYPE_IMOV
23724               || dep_insn_type == TYPE_FMOV)
23725             cost = 1;
23726           else if (cost > 2)
23727             cost -= 2;
23728           else
23729             cost = 1;
23730         }
23731       break;
23732
23733     case PROCESSOR_ATHLON:
23734     case PROCESSOR_K8:
23735     case PROCESSOR_AMDFAM10:
23736     case PROCESSOR_BDVER1:
23737     case PROCESSOR_BDVER2:
23738     case PROCESSOR_BTVER1:
23739     case PROCESSOR_ATOM:
23740     case PROCESSOR_GENERIC32:
23741     case PROCESSOR_GENERIC64:
23742       memory = get_attr_memory (insn);
23743
23744       /* Show ability of reorder buffer to hide latency of load by executing
23745          in parallel with previous instruction in case
23746          previous instruction is not needed to compute the address.  */
23747       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
23748           && !ix86_agi_dependent (dep_insn, insn))
23749         {
23750           enum attr_unit unit = get_attr_unit (insn);
23751           int loadcost = 3;
23752
23753           /* Because of the difference between the length of integer and
23754              floating unit pipeline preparation stages, the memory operands
23755              for floating point are cheaper.
23756
23757              ??? For Athlon it the difference is most probably 2.  */
23758           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
23759             loadcost = 3;
23760           else
23761             loadcost = TARGET_ATHLON ? 2 : 0;
23762
23763           if (cost >= loadcost)
23764             cost -= loadcost;
23765           else
23766             cost = 0;
23767         }
23768
23769     default:
23770       break;
23771     }
23772
23773   return cost;
23774 }
23775
23776 /* How many alternative schedules to try.  This should be as wide as the
23777    scheduling freedom in the DFA, but no wider.  Making this value too
23778    large results extra work for the scheduler.  */
23779
23780 static int
23781 ia32_multipass_dfa_lookahead (void)
23782 {
23783   switch (ix86_tune)
23784     {
23785     case PROCESSOR_PENTIUM:
23786       return 2;
23787
23788     case PROCESSOR_PENTIUMPRO:
23789     case PROCESSOR_K6:
23790       return 1;
23791
23792     case PROCESSOR_CORE2_32:
23793     case PROCESSOR_CORE2_64:
23794     case PROCESSOR_COREI7_32:
23795     case PROCESSOR_COREI7_64:
23796       /* Generally, we want haifa-sched:max_issue() to look ahead as far
23797          as many instructions can be executed on a cycle, i.e.,
23798          issue_rate.  I wonder why tuning for many CPUs does not do this.  */
23799       return ix86_issue_rate ();
23800
23801     default:
23802       return 0;
23803     }
23804 }
23805
23806 \f
23807
23808 /* Model decoder of Core 2/i7.
23809    Below hooks for multipass scheduling (see haifa-sched.c:max_issue)
23810    track the instruction fetch block boundaries and make sure that long
23811    (9+ bytes) instructions are assigned to D0.  */
23812
23813 /* Maximum length of an insn that can be handled by
23814    a secondary decoder unit.  '8' for Core 2/i7.  */
23815 static int core2i7_secondary_decoder_max_insn_size;
23816
23817 /* Ifetch block size, i.e., number of bytes decoder reads per cycle.
23818    '16' for Core 2/i7.  */
23819 static int core2i7_ifetch_block_size;
23820
23821 /* Maximum number of instructions decoder can handle per cycle.
23822    '6' for Core 2/i7.  */
23823 static int core2i7_ifetch_block_max_insns;
23824
23825 typedef struct ix86_first_cycle_multipass_data_ *
23826   ix86_first_cycle_multipass_data_t;
23827 typedef const struct ix86_first_cycle_multipass_data_ *
23828   const_ix86_first_cycle_multipass_data_t;
23829
23830 /* A variable to store target state across calls to max_issue within
23831    one cycle.  */
23832 static struct ix86_first_cycle_multipass_data_ _ix86_first_cycle_multipass_data,
23833   *ix86_first_cycle_multipass_data = &_ix86_first_cycle_multipass_data;
23834
23835 /* Initialize DATA.  */
23836 static void
23837 core2i7_first_cycle_multipass_init (void *_data)
23838 {
23839   ix86_first_cycle_multipass_data_t data
23840     = (ix86_first_cycle_multipass_data_t) _data;
23841
23842   data->ifetch_block_len = 0;
23843   data->ifetch_block_n_insns = 0;
23844   data->ready_try_change = NULL;
23845   data->ready_try_change_size = 0;
23846 }
23847
23848 /* Advancing the cycle; reset ifetch block counts.  */
23849 static void
23850 core2i7_dfa_post_advance_cycle (void)
23851 {
23852   ix86_first_cycle_multipass_data_t data = ix86_first_cycle_multipass_data;
23853
23854   gcc_assert (data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
23855
23856   data->ifetch_block_len = 0;
23857   data->ifetch_block_n_insns = 0;
23858 }
23859
23860 static int min_insn_size (rtx);
23861
23862 /* Filter out insns from ready_try that the core will not be able to issue
23863    on current cycle due to decoder.  */
23864 static void
23865 core2i7_first_cycle_multipass_filter_ready_try
23866 (const_ix86_first_cycle_multipass_data_t data,
23867  char *ready_try, int n_ready, bool first_cycle_insn_p)
23868 {
23869   while (n_ready--)
23870     {
23871       rtx insn;
23872       int insn_size;
23873
23874       if (ready_try[n_ready])
23875         continue;
23876
23877       insn = get_ready_element (n_ready);
23878       insn_size = min_insn_size (insn);
23879
23880       if (/* If this is a too long an insn for a secondary decoder ...  */
23881           (!first_cycle_insn_p
23882            && insn_size > core2i7_secondary_decoder_max_insn_size)
23883           /* ... or it would not fit into the ifetch block ...  */
23884           || data->ifetch_block_len + insn_size > core2i7_ifetch_block_size
23885           /* ... or the decoder is full already ...  */
23886           || data->ifetch_block_n_insns + 1 > core2i7_ifetch_block_max_insns)
23887         /* ... mask the insn out.  */
23888         {
23889           ready_try[n_ready] = 1;
23890
23891           if (data->ready_try_change)
23892             SET_BIT (data->ready_try_change, n_ready);
23893         }
23894     }
23895 }
23896
23897 /* Prepare for a new round of multipass lookahead scheduling.  */
23898 static void
23899 core2i7_first_cycle_multipass_begin (void *_data, char *ready_try, int n_ready,
23900                                      bool first_cycle_insn_p)
23901 {
23902   ix86_first_cycle_multipass_data_t data
23903     = (ix86_first_cycle_multipass_data_t) _data;
23904   const_ix86_first_cycle_multipass_data_t prev_data
23905     = ix86_first_cycle_multipass_data;
23906
23907   /* Restore the state from the end of the previous round.  */
23908   data->ifetch_block_len = prev_data->ifetch_block_len;
23909   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns;
23910
23911   /* Filter instructions that cannot be issued on current cycle due to
23912      decoder restrictions.  */
23913   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
23914                                                   first_cycle_insn_p);
23915 }
23916
23917 /* INSN is being issued in current solution.  Account for its impact on
23918    the decoder model.  */
23919 static void
23920 core2i7_first_cycle_multipass_issue (void *_data, char *ready_try, int n_ready,
23921                                      rtx insn, const void *_prev_data)
23922 {
23923   ix86_first_cycle_multipass_data_t data
23924     = (ix86_first_cycle_multipass_data_t) _data;
23925   const_ix86_first_cycle_multipass_data_t prev_data
23926     = (const_ix86_first_cycle_multipass_data_t) _prev_data;
23927
23928   int insn_size = min_insn_size (insn);
23929
23930   data->ifetch_block_len = prev_data->ifetch_block_len + insn_size;
23931   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns + 1;
23932   gcc_assert (data->ifetch_block_len <= core2i7_ifetch_block_size
23933               && data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
23934
23935   /* Allocate or resize the bitmap for storing INSN's effect on ready_try.  */
23936   if (!data->ready_try_change)
23937     {
23938       data->ready_try_change = sbitmap_alloc (n_ready);
23939       data->ready_try_change_size = n_ready;
23940     }
23941   else if (data->ready_try_change_size < n_ready)
23942     {
23943       data->ready_try_change = sbitmap_resize (data->ready_try_change,
23944                                                n_ready, 0);
23945       data->ready_try_change_size = n_ready;
23946     }
23947   sbitmap_zero (data->ready_try_change);
23948
23949   /* Filter out insns from ready_try that the core will not be able to issue
23950      on current cycle due to decoder.  */
23951   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
23952                                                   false);
23953 }
23954
23955 /* Revert the effect on ready_try.  */
23956 static void
23957 core2i7_first_cycle_multipass_backtrack (const void *_data,
23958                                          char *ready_try,
23959                                          int n_ready ATTRIBUTE_UNUSED)
23960 {
23961   const_ix86_first_cycle_multipass_data_t data
23962     = (const_ix86_first_cycle_multipass_data_t) _data;
23963   unsigned int i = 0;
23964   sbitmap_iterator sbi;
23965
23966   gcc_assert (sbitmap_last_set_bit (data->ready_try_change) < n_ready);
23967   EXECUTE_IF_SET_IN_SBITMAP (data->ready_try_change, 0, i, sbi)
23968     {
23969       ready_try[i] = 0;
23970     }
23971 }
23972
23973 /* Save the result of multipass lookahead scheduling for the next round.  */
23974 static void
23975 core2i7_first_cycle_multipass_end (const void *_data)
23976 {
23977   const_ix86_first_cycle_multipass_data_t data
23978     = (const_ix86_first_cycle_multipass_data_t) _data;
23979   ix86_first_cycle_multipass_data_t next_data
23980     = ix86_first_cycle_multipass_data;
23981
23982   if (data != NULL)
23983     {
23984       next_data->ifetch_block_len = data->ifetch_block_len;
23985       next_data->ifetch_block_n_insns = data->ifetch_block_n_insns;
23986     }
23987 }
23988
23989 /* Deallocate target data.  */
23990 static void
23991 core2i7_first_cycle_multipass_fini (void *_data)
23992 {
23993   ix86_first_cycle_multipass_data_t data
23994     = (ix86_first_cycle_multipass_data_t) _data;
23995
23996   if (data->ready_try_change)
23997     {
23998       sbitmap_free (data->ready_try_change);
23999       data->ready_try_change = NULL;
24000       data->ready_try_change_size = 0;
24001     }
24002 }
24003
24004 /* Prepare for scheduling pass.  */
24005 static void
24006 ix86_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
24007                         int verbose ATTRIBUTE_UNUSED,
24008                         int max_uid ATTRIBUTE_UNUSED)
24009 {
24010   /* Install scheduling hooks for current CPU.  Some of these hooks are used
24011      in time-critical parts of the scheduler, so we only set them up when
24012      they are actually used.  */
24013   switch (ix86_tune)
24014     {
24015     case PROCESSOR_CORE2_32:
24016     case PROCESSOR_CORE2_64:
24017     case PROCESSOR_COREI7_32:
24018     case PROCESSOR_COREI7_64:
24019       targetm.sched.dfa_post_advance_cycle
24020         = core2i7_dfa_post_advance_cycle;
24021       targetm.sched.first_cycle_multipass_init
24022         = core2i7_first_cycle_multipass_init;
24023       targetm.sched.first_cycle_multipass_begin
24024         = core2i7_first_cycle_multipass_begin;
24025       targetm.sched.first_cycle_multipass_issue
24026         = core2i7_first_cycle_multipass_issue;
24027       targetm.sched.first_cycle_multipass_backtrack
24028         = core2i7_first_cycle_multipass_backtrack;
24029       targetm.sched.first_cycle_multipass_end
24030         = core2i7_first_cycle_multipass_end;
24031       targetm.sched.first_cycle_multipass_fini
24032         = core2i7_first_cycle_multipass_fini;
24033
24034       /* Set decoder parameters.  */
24035       core2i7_secondary_decoder_max_insn_size = 8;
24036       core2i7_ifetch_block_size = 16;
24037       core2i7_ifetch_block_max_insns = 6;
24038       break;
24039
24040     default:
24041       targetm.sched.dfa_post_advance_cycle = NULL;
24042       targetm.sched.first_cycle_multipass_init = NULL;
24043       targetm.sched.first_cycle_multipass_begin = NULL;
24044       targetm.sched.first_cycle_multipass_issue = NULL;
24045       targetm.sched.first_cycle_multipass_backtrack = NULL;
24046       targetm.sched.first_cycle_multipass_end = NULL;
24047       targetm.sched.first_cycle_multipass_fini = NULL;
24048       break;
24049     }
24050 }
24051
24052 \f
24053 /* Compute the alignment given to a constant that is being placed in memory.
24054    EXP is the constant and ALIGN is the alignment that the object would
24055    ordinarily have.
24056    The value of this function is used instead of that alignment to align
24057    the object.  */
24058
24059 int
24060 ix86_constant_alignment (tree exp, int align)
24061 {
24062   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
24063       || TREE_CODE (exp) == INTEGER_CST)
24064     {
24065       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
24066         return 64;
24067       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
24068         return 128;
24069     }
24070   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
24071            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
24072     return BITS_PER_WORD;
24073
24074   return align;
24075 }
24076
24077 /* Compute the alignment for a static variable.
24078    TYPE is the data type, and ALIGN is the alignment that
24079    the object would ordinarily have.  The value of this function is used
24080    instead of that alignment to align the object.  */
24081
24082 int
24083 ix86_data_alignment (tree type, int align)
24084 {
24085   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
24086
24087   if (AGGREGATE_TYPE_P (type)
24088       && TYPE_SIZE (type)
24089       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
24090       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
24091           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
24092       && align < max_align)
24093     align = max_align;
24094
24095   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
24096      to 16byte boundary.  */
24097   if (TARGET_64BIT)
24098     {
24099       if (AGGREGATE_TYPE_P (type)
24100            && TYPE_SIZE (type)
24101            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
24102            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
24103                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
24104         return 128;
24105     }
24106
24107   if (TREE_CODE (type) == ARRAY_TYPE)
24108     {
24109       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
24110         return 64;
24111       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
24112         return 128;
24113     }
24114   else if (TREE_CODE (type) == COMPLEX_TYPE)
24115     {
24116
24117       if (TYPE_MODE (type) == DCmode && align < 64)
24118         return 64;
24119       if ((TYPE_MODE (type) == XCmode
24120            || TYPE_MODE (type) == TCmode) && align < 128)
24121         return 128;
24122     }
24123   else if ((TREE_CODE (type) == RECORD_TYPE
24124             || TREE_CODE (type) == UNION_TYPE
24125             || TREE_CODE (type) == QUAL_UNION_TYPE)
24126            && TYPE_FIELDS (type))
24127     {
24128       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
24129         return 64;
24130       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
24131         return 128;
24132     }
24133   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
24134            || TREE_CODE (type) == INTEGER_TYPE)
24135     {
24136       if (TYPE_MODE (type) == DFmode && align < 64)
24137         return 64;
24138       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
24139         return 128;
24140     }
24141
24142   return align;
24143 }
24144
24145 /* Compute the alignment for a local variable or a stack slot.  EXP is
24146    the data type or decl itself, MODE is the widest mode available and
24147    ALIGN is the alignment that the object would ordinarily have.  The
24148    value of this macro is used instead of that alignment to align the
24149    object.  */
24150
24151 unsigned int
24152 ix86_local_alignment (tree exp, enum machine_mode mode,
24153                       unsigned int align)
24154 {
24155   tree type, decl;
24156
24157   if (exp && DECL_P (exp))
24158     {
24159       type = TREE_TYPE (exp);
24160       decl = exp;
24161     }
24162   else
24163     {
24164       type = exp;
24165       decl = NULL;
24166     }
24167
24168   /* Don't do dynamic stack realignment for long long objects with
24169      -mpreferred-stack-boundary=2.  */
24170   if (!TARGET_64BIT
24171       && align == 64
24172       && ix86_preferred_stack_boundary < 64
24173       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
24174       && (!type || !TYPE_USER_ALIGN (type))
24175       && (!decl || !DECL_USER_ALIGN (decl)))
24176     align = 32;
24177
24178   /* If TYPE is NULL, we are allocating a stack slot for caller-save
24179      register in MODE.  We will return the largest alignment of XF
24180      and DF.  */
24181   if (!type)
24182     {
24183       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
24184         align = GET_MODE_ALIGNMENT (DFmode);
24185       return align;
24186     }
24187
24188   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
24189      to 16byte boundary.  Exact wording is:
24190
24191      An array uses the same alignment as its elements, except that a local or
24192      global array variable of length at least 16 bytes or
24193      a C99 variable-length array variable always has alignment of at least 16 bytes.
24194
24195      This was added to allow use of aligned SSE instructions at arrays.  This
24196      rule is meant for static storage (where compiler can not do the analysis
24197      by itself).  We follow it for automatic variables only when convenient.
24198      We fully control everything in the function compiled and functions from
24199      other unit can not rely on the alignment.
24200
24201      Exclude va_list type.  It is the common case of local array where
24202      we can not benefit from the alignment.  */
24203   if (TARGET_64BIT && optimize_function_for_speed_p (cfun)
24204       && TARGET_SSE)
24205     {
24206       if (AGGREGATE_TYPE_P (type)
24207            && (va_list_type_node == NULL_TREE
24208                || (TYPE_MAIN_VARIANT (type)
24209                    != TYPE_MAIN_VARIANT (va_list_type_node)))
24210            && TYPE_SIZE (type)
24211            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
24212            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
24213                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
24214         return 128;
24215     }
24216   if (TREE_CODE (type) == ARRAY_TYPE)
24217     {
24218       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
24219         return 64;
24220       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
24221         return 128;
24222     }
24223   else if (TREE_CODE (type) == COMPLEX_TYPE)
24224     {
24225       if (TYPE_MODE (type) == DCmode && align < 64)
24226         return 64;
24227       if ((TYPE_MODE (type) == XCmode
24228            || TYPE_MODE (type) == TCmode) && align < 128)
24229         return 128;
24230     }
24231   else if ((TREE_CODE (type) == RECORD_TYPE
24232             || TREE_CODE (type) == UNION_TYPE
24233             || TREE_CODE (type) == QUAL_UNION_TYPE)
24234            && TYPE_FIELDS (type))
24235     {
24236       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
24237         return 64;
24238       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
24239         return 128;
24240     }
24241   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
24242            || TREE_CODE (type) == INTEGER_TYPE)
24243     {
24244
24245       if (TYPE_MODE (type) == DFmode && align < 64)
24246         return 64;
24247       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
24248         return 128;
24249     }
24250   return align;
24251 }
24252
24253 /* Compute the minimum required alignment for dynamic stack realignment
24254    purposes for a local variable, parameter or a stack slot.  EXP is
24255    the data type or decl itself, MODE is its mode and ALIGN is the
24256    alignment that the object would ordinarily have.  */
24257
24258 unsigned int
24259 ix86_minimum_alignment (tree exp, enum machine_mode mode,
24260                         unsigned int align)
24261 {
24262   tree type, decl;
24263
24264   if (exp && DECL_P (exp))
24265     {
24266       type = TREE_TYPE (exp);
24267       decl = exp;
24268     }
24269   else
24270     {
24271       type = exp;
24272       decl = NULL;
24273     }
24274
24275   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
24276     return align;
24277
24278   /* Don't do dynamic stack realignment for long long objects with
24279      -mpreferred-stack-boundary=2.  */
24280   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
24281       && (!type || !TYPE_USER_ALIGN (type))
24282       && (!decl || !DECL_USER_ALIGN (decl)))
24283     return 32;
24284
24285   return align;
24286 }
24287 \f
24288 /* Find a location for the static chain incoming to a nested function.
24289    This is a register, unless all free registers are used by arguments.  */
24290
24291 static rtx
24292 ix86_static_chain (const_tree fndecl, bool incoming_p)
24293 {
24294   unsigned regno;
24295
24296   if (!DECL_STATIC_CHAIN (fndecl))
24297     return NULL;
24298
24299   if (TARGET_64BIT)
24300     {
24301       /* We always use R10 in 64-bit mode.  */
24302       regno = R10_REG;
24303     }
24304   else
24305     {
24306       tree fntype;
24307       unsigned int ccvt;
24308
24309       /* By default in 32-bit mode we use ECX to pass the static chain.  */
24310       regno = CX_REG;
24311
24312       fntype = TREE_TYPE (fndecl);
24313       ccvt = ix86_get_callcvt (fntype);
24314       if ((ccvt & (IX86_CALLCVT_FASTCALL | IX86_CALLCVT_THISCALL)) != 0)
24315         {
24316           /* Fastcall functions use ecx/edx for arguments, which leaves
24317              us with EAX for the static chain.
24318              Thiscall functions use ecx for arguments, which also
24319              leaves us with EAX for the static chain.  */
24320           regno = AX_REG;
24321         }
24322       else if (ix86_function_regparm (fntype, fndecl) == 3)
24323         {
24324           /* For regparm 3, we have no free call-clobbered registers in
24325              which to store the static chain.  In order to implement this,
24326              we have the trampoline push the static chain to the stack.
24327              However, we can't push a value below the return address when
24328              we call the nested function directly, so we have to use an
24329              alternate entry point.  For this we use ESI, and have the
24330              alternate entry point push ESI, so that things appear the
24331              same once we're executing the nested function.  */
24332           if (incoming_p)
24333             {
24334               if (fndecl == current_function_decl)
24335                 ix86_static_chain_on_stack = true;
24336               return gen_frame_mem (SImode,
24337                                     plus_constant (arg_pointer_rtx, -8));
24338             }
24339           regno = SI_REG;
24340         }
24341     }
24342
24343   return gen_rtx_REG (Pmode, regno);
24344 }
24345
24346 /* Emit RTL insns to initialize the variable parts of a trampoline.
24347    FNDECL is the decl of the target address; M_TRAMP is a MEM for
24348    the trampoline, and CHAIN_VALUE is an RTX for the static chain
24349    to be passed to the target function.  */
24350
24351 static void
24352 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
24353 {
24354   rtx mem, fnaddr;
24355   int opcode;
24356   int offset = 0;
24357
24358   fnaddr = XEXP (DECL_RTL (fndecl), 0);
24359
24360   if (TARGET_64BIT)
24361     {
24362       int size;
24363
24364       /* Load the function address to r11.  Try to load address using
24365          the shorter movl instead of movabs.  We may want to support
24366          movq for kernel mode, but kernel does not use trampolines at
24367          the moment.  */
24368       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
24369         {
24370           fnaddr = copy_to_mode_reg (DImode, fnaddr);
24371
24372           mem = adjust_address (m_tramp, HImode, offset);
24373           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
24374
24375           mem = adjust_address (m_tramp, SImode, offset + 2);
24376           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
24377           offset += 6;
24378         }
24379       else
24380         {
24381           mem = adjust_address (m_tramp, HImode, offset);
24382           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
24383
24384           mem = adjust_address (m_tramp, DImode, offset + 2);
24385           emit_move_insn (mem, fnaddr);
24386           offset += 10;
24387         }
24388
24389       /* Load static chain using movabs to r10.  Use the
24390          shorter movl instead of movabs for x32.  */
24391       if (TARGET_X32)
24392         {
24393           opcode = 0xba41;
24394           size = 6;
24395         }
24396       else
24397         {
24398           opcode = 0xba49;
24399           size = 10;
24400         }
24401
24402       mem = adjust_address (m_tramp, HImode, offset);
24403       emit_move_insn (mem, gen_int_mode (opcode, HImode));
24404
24405       mem = adjust_address (m_tramp, ptr_mode, offset + 2);
24406       emit_move_insn (mem, chain_value);
24407       offset += size;
24408
24409       /* Jump to r11; the last (unused) byte is a nop, only there to
24410          pad the write out to a single 32-bit store.  */
24411       mem = adjust_address (m_tramp, SImode, offset);
24412       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
24413       offset += 4;
24414     }
24415   else
24416     {
24417       rtx disp, chain;
24418
24419       /* Depending on the static chain location, either load a register
24420          with a constant, or push the constant to the stack.  All of the
24421          instructions are the same size.  */
24422       chain = ix86_static_chain (fndecl, true);
24423       if (REG_P (chain))
24424         {
24425           switch (REGNO (chain))
24426             {
24427             case AX_REG:
24428               opcode = 0xb8; break;
24429             case CX_REG:
24430               opcode = 0xb9; break;
24431             default:
24432               gcc_unreachable ();
24433             }
24434         }
24435       else
24436         opcode = 0x68;
24437
24438       mem = adjust_address (m_tramp, QImode, offset);
24439       emit_move_insn (mem, gen_int_mode (opcode, QImode));
24440
24441       mem = adjust_address (m_tramp, SImode, offset + 1);
24442       emit_move_insn (mem, chain_value);
24443       offset += 5;
24444
24445       mem = adjust_address (m_tramp, QImode, offset);
24446       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
24447
24448       mem = adjust_address (m_tramp, SImode, offset + 1);
24449
24450       /* Compute offset from the end of the jmp to the target function.
24451          In the case in which the trampoline stores the static chain on
24452          the stack, we need to skip the first insn which pushes the
24453          (call-saved) register static chain; this push is 1 byte.  */
24454       offset += 5;
24455       disp = expand_binop (SImode, sub_optab, fnaddr,
24456                            plus_constant (XEXP (m_tramp, 0),
24457                                           offset - (MEM_P (chain) ? 1 : 0)),
24458                            NULL_RTX, 1, OPTAB_DIRECT);
24459       emit_move_insn (mem, disp);
24460     }
24461
24462   gcc_assert (offset <= TRAMPOLINE_SIZE);
24463
24464 #ifdef HAVE_ENABLE_EXECUTE_STACK
24465 #ifdef CHECK_EXECUTE_STACK_ENABLED
24466   if (CHECK_EXECUTE_STACK_ENABLED)
24467 #endif
24468   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
24469                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
24470 #endif
24471 }
24472 \f
24473 /* The following file contains several enumerations and data structures
24474    built from the definitions in i386-builtin-types.def.  */
24475
24476 #include "i386-builtin-types.inc"
24477
24478 /* Table for the ix86 builtin non-function types.  */
24479 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
24480
24481 /* Retrieve an element from the above table, building some of
24482    the types lazily.  */
24483
24484 static tree
24485 ix86_get_builtin_type (enum ix86_builtin_type tcode)
24486 {
24487   unsigned int index;
24488   tree type, itype;
24489
24490   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
24491
24492   type = ix86_builtin_type_tab[(int) tcode];
24493   if (type != NULL)
24494     return type;
24495
24496   gcc_assert (tcode > IX86_BT_LAST_PRIM);
24497   if (tcode <= IX86_BT_LAST_VECT)
24498     {
24499       enum machine_mode mode;
24500
24501       index = tcode - IX86_BT_LAST_PRIM - 1;
24502       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
24503       mode = ix86_builtin_type_vect_mode[index];
24504
24505       type = build_vector_type_for_mode (itype, mode);
24506     }
24507   else
24508     {
24509       int quals;
24510
24511       index = tcode - IX86_BT_LAST_VECT - 1;
24512       if (tcode <= IX86_BT_LAST_PTR)
24513         quals = TYPE_UNQUALIFIED;
24514       else
24515         quals = TYPE_QUAL_CONST;
24516
24517       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
24518       if (quals != TYPE_UNQUALIFIED)
24519         itype = build_qualified_type (itype, quals);
24520
24521       type = build_pointer_type (itype);
24522     }
24523
24524   ix86_builtin_type_tab[(int) tcode] = type;
24525   return type;
24526 }
24527
24528 /* Table for the ix86 builtin function types.  */
24529 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
24530
24531 /* Retrieve an element from the above table, building some of
24532    the types lazily.  */
24533
24534 static tree
24535 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
24536 {
24537   tree type;
24538
24539   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
24540
24541   type = ix86_builtin_func_type_tab[(int) tcode];
24542   if (type != NULL)
24543     return type;
24544
24545   if (tcode <= IX86_BT_LAST_FUNC)
24546     {
24547       unsigned start = ix86_builtin_func_start[(int) tcode];
24548       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
24549       tree rtype, atype, args = void_list_node;
24550       unsigned i;
24551
24552       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
24553       for (i = after - 1; i > start; --i)
24554         {
24555           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
24556           args = tree_cons (NULL, atype, args);
24557         }
24558
24559       type = build_function_type (rtype, args);
24560     }
24561   else
24562     {
24563       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
24564       enum ix86_builtin_func_type icode;
24565
24566       icode = ix86_builtin_func_alias_base[index];
24567       type = ix86_get_builtin_func_type (icode);
24568     }
24569
24570   ix86_builtin_func_type_tab[(int) tcode] = type;
24571   return type;
24572 }
24573
24574
24575 /* Codes for all the SSE/MMX builtins.  */
24576 enum ix86_builtins
24577 {
24578   IX86_BUILTIN_ADDPS,
24579   IX86_BUILTIN_ADDSS,
24580   IX86_BUILTIN_DIVPS,
24581   IX86_BUILTIN_DIVSS,
24582   IX86_BUILTIN_MULPS,
24583   IX86_BUILTIN_MULSS,
24584   IX86_BUILTIN_SUBPS,
24585   IX86_BUILTIN_SUBSS,
24586
24587   IX86_BUILTIN_CMPEQPS,
24588   IX86_BUILTIN_CMPLTPS,
24589   IX86_BUILTIN_CMPLEPS,
24590   IX86_BUILTIN_CMPGTPS,
24591   IX86_BUILTIN_CMPGEPS,
24592   IX86_BUILTIN_CMPNEQPS,
24593   IX86_BUILTIN_CMPNLTPS,
24594   IX86_BUILTIN_CMPNLEPS,
24595   IX86_BUILTIN_CMPNGTPS,
24596   IX86_BUILTIN_CMPNGEPS,
24597   IX86_BUILTIN_CMPORDPS,
24598   IX86_BUILTIN_CMPUNORDPS,
24599   IX86_BUILTIN_CMPEQSS,
24600   IX86_BUILTIN_CMPLTSS,
24601   IX86_BUILTIN_CMPLESS,
24602   IX86_BUILTIN_CMPNEQSS,
24603   IX86_BUILTIN_CMPNLTSS,
24604   IX86_BUILTIN_CMPNLESS,
24605   IX86_BUILTIN_CMPNGTSS,
24606   IX86_BUILTIN_CMPNGESS,
24607   IX86_BUILTIN_CMPORDSS,
24608   IX86_BUILTIN_CMPUNORDSS,
24609
24610   IX86_BUILTIN_COMIEQSS,
24611   IX86_BUILTIN_COMILTSS,
24612   IX86_BUILTIN_COMILESS,
24613   IX86_BUILTIN_COMIGTSS,
24614   IX86_BUILTIN_COMIGESS,
24615   IX86_BUILTIN_COMINEQSS,
24616   IX86_BUILTIN_UCOMIEQSS,
24617   IX86_BUILTIN_UCOMILTSS,
24618   IX86_BUILTIN_UCOMILESS,
24619   IX86_BUILTIN_UCOMIGTSS,
24620   IX86_BUILTIN_UCOMIGESS,
24621   IX86_BUILTIN_UCOMINEQSS,
24622
24623   IX86_BUILTIN_CVTPI2PS,
24624   IX86_BUILTIN_CVTPS2PI,
24625   IX86_BUILTIN_CVTSI2SS,
24626   IX86_BUILTIN_CVTSI642SS,
24627   IX86_BUILTIN_CVTSS2SI,
24628   IX86_BUILTIN_CVTSS2SI64,
24629   IX86_BUILTIN_CVTTPS2PI,
24630   IX86_BUILTIN_CVTTSS2SI,
24631   IX86_BUILTIN_CVTTSS2SI64,
24632
24633   IX86_BUILTIN_MAXPS,
24634   IX86_BUILTIN_MAXSS,
24635   IX86_BUILTIN_MINPS,
24636   IX86_BUILTIN_MINSS,
24637
24638   IX86_BUILTIN_LOADUPS,
24639   IX86_BUILTIN_STOREUPS,
24640   IX86_BUILTIN_MOVSS,
24641
24642   IX86_BUILTIN_MOVHLPS,
24643   IX86_BUILTIN_MOVLHPS,
24644   IX86_BUILTIN_LOADHPS,
24645   IX86_BUILTIN_LOADLPS,
24646   IX86_BUILTIN_STOREHPS,
24647   IX86_BUILTIN_STORELPS,
24648
24649   IX86_BUILTIN_MASKMOVQ,
24650   IX86_BUILTIN_MOVMSKPS,
24651   IX86_BUILTIN_PMOVMSKB,
24652
24653   IX86_BUILTIN_MOVNTPS,
24654   IX86_BUILTIN_MOVNTQ,
24655
24656   IX86_BUILTIN_LOADDQU,
24657   IX86_BUILTIN_STOREDQU,
24658
24659   IX86_BUILTIN_PACKSSWB,
24660   IX86_BUILTIN_PACKSSDW,
24661   IX86_BUILTIN_PACKUSWB,
24662
24663   IX86_BUILTIN_PADDB,
24664   IX86_BUILTIN_PADDW,
24665   IX86_BUILTIN_PADDD,
24666   IX86_BUILTIN_PADDQ,
24667   IX86_BUILTIN_PADDSB,
24668   IX86_BUILTIN_PADDSW,
24669   IX86_BUILTIN_PADDUSB,
24670   IX86_BUILTIN_PADDUSW,
24671   IX86_BUILTIN_PSUBB,
24672   IX86_BUILTIN_PSUBW,
24673   IX86_BUILTIN_PSUBD,
24674   IX86_BUILTIN_PSUBQ,
24675   IX86_BUILTIN_PSUBSB,
24676   IX86_BUILTIN_PSUBSW,
24677   IX86_BUILTIN_PSUBUSB,
24678   IX86_BUILTIN_PSUBUSW,
24679
24680   IX86_BUILTIN_PAND,
24681   IX86_BUILTIN_PANDN,
24682   IX86_BUILTIN_POR,
24683   IX86_BUILTIN_PXOR,
24684
24685   IX86_BUILTIN_PAVGB,
24686   IX86_BUILTIN_PAVGW,
24687
24688   IX86_BUILTIN_PCMPEQB,
24689   IX86_BUILTIN_PCMPEQW,
24690   IX86_BUILTIN_PCMPEQD,
24691   IX86_BUILTIN_PCMPGTB,
24692   IX86_BUILTIN_PCMPGTW,
24693   IX86_BUILTIN_PCMPGTD,
24694
24695   IX86_BUILTIN_PMADDWD,
24696
24697   IX86_BUILTIN_PMAXSW,
24698   IX86_BUILTIN_PMAXUB,
24699   IX86_BUILTIN_PMINSW,
24700   IX86_BUILTIN_PMINUB,
24701
24702   IX86_BUILTIN_PMULHUW,
24703   IX86_BUILTIN_PMULHW,
24704   IX86_BUILTIN_PMULLW,
24705
24706   IX86_BUILTIN_PSADBW,
24707   IX86_BUILTIN_PSHUFW,
24708
24709   IX86_BUILTIN_PSLLW,
24710   IX86_BUILTIN_PSLLD,
24711   IX86_BUILTIN_PSLLQ,
24712   IX86_BUILTIN_PSRAW,
24713   IX86_BUILTIN_PSRAD,
24714   IX86_BUILTIN_PSRLW,
24715   IX86_BUILTIN_PSRLD,
24716   IX86_BUILTIN_PSRLQ,
24717   IX86_BUILTIN_PSLLWI,
24718   IX86_BUILTIN_PSLLDI,
24719   IX86_BUILTIN_PSLLQI,
24720   IX86_BUILTIN_PSRAWI,
24721   IX86_BUILTIN_PSRADI,
24722   IX86_BUILTIN_PSRLWI,
24723   IX86_BUILTIN_PSRLDI,
24724   IX86_BUILTIN_PSRLQI,
24725
24726   IX86_BUILTIN_PUNPCKHBW,
24727   IX86_BUILTIN_PUNPCKHWD,
24728   IX86_BUILTIN_PUNPCKHDQ,
24729   IX86_BUILTIN_PUNPCKLBW,
24730   IX86_BUILTIN_PUNPCKLWD,
24731   IX86_BUILTIN_PUNPCKLDQ,
24732
24733   IX86_BUILTIN_SHUFPS,
24734
24735   IX86_BUILTIN_RCPPS,
24736   IX86_BUILTIN_RCPSS,
24737   IX86_BUILTIN_RSQRTPS,
24738   IX86_BUILTIN_RSQRTPS_NR,
24739   IX86_BUILTIN_RSQRTSS,
24740   IX86_BUILTIN_RSQRTF,
24741   IX86_BUILTIN_SQRTPS,
24742   IX86_BUILTIN_SQRTPS_NR,
24743   IX86_BUILTIN_SQRTSS,
24744
24745   IX86_BUILTIN_UNPCKHPS,
24746   IX86_BUILTIN_UNPCKLPS,
24747
24748   IX86_BUILTIN_ANDPS,
24749   IX86_BUILTIN_ANDNPS,
24750   IX86_BUILTIN_ORPS,
24751   IX86_BUILTIN_XORPS,
24752
24753   IX86_BUILTIN_EMMS,
24754   IX86_BUILTIN_LDMXCSR,
24755   IX86_BUILTIN_STMXCSR,
24756   IX86_BUILTIN_SFENCE,
24757
24758   /* 3DNow! Original */
24759   IX86_BUILTIN_FEMMS,
24760   IX86_BUILTIN_PAVGUSB,
24761   IX86_BUILTIN_PF2ID,
24762   IX86_BUILTIN_PFACC,
24763   IX86_BUILTIN_PFADD,
24764   IX86_BUILTIN_PFCMPEQ,
24765   IX86_BUILTIN_PFCMPGE,
24766   IX86_BUILTIN_PFCMPGT,
24767   IX86_BUILTIN_PFMAX,
24768   IX86_BUILTIN_PFMIN,
24769   IX86_BUILTIN_PFMUL,
24770   IX86_BUILTIN_PFRCP,
24771   IX86_BUILTIN_PFRCPIT1,
24772   IX86_BUILTIN_PFRCPIT2,
24773   IX86_BUILTIN_PFRSQIT1,
24774   IX86_BUILTIN_PFRSQRT,
24775   IX86_BUILTIN_PFSUB,
24776   IX86_BUILTIN_PFSUBR,
24777   IX86_BUILTIN_PI2FD,
24778   IX86_BUILTIN_PMULHRW,
24779
24780   /* 3DNow! Athlon Extensions */
24781   IX86_BUILTIN_PF2IW,
24782   IX86_BUILTIN_PFNACC,
24783   IX86_BUILTIN_PFPNACC,
24784   IX86_BUILTIN_PI2FW,
24785   IX86_BUILTIN_PSWAPDSI,
24786   IX86_BUILTIN_PSWAPDSF,
24787
24788   /* SSE2 */
24789   IX86_BUILTIN_ADDPD,
24790   IX86_BUILTIN_ADDSD,
24791   IX86_BUILTIN_DIVPD,
24792   IX86_BUILTIN_DIVSD,
24793   IX86_BUILTIN_MULPD,
24794   IX86_BUILTIN_MULSD,
24795   IX86_BUILTIN_SUBPD,
24796   IX86_BUILTIN_SUBSD,
24797
24798   IX86_BUILTIN_CMPEQPD,
24799   IX86_BUILTIN_CMPLTPD,
24800   IX86_BUILTIN_CMPLEPD,
24801   IX86_BUILTIN_CMPGTPD,
24802   IX86_BUILTIN_CMPGEPD,
24803   IX86_BUILTIN_CMPNEQPD,
24804   IX86_BUILTIN_CMPNLTPD,
24805   IX86_BUILTIN_CMPNLEPD,
24806   IX86_BUILTIN_CMPNGTPD,
24807   IX86_BUILTIN_CMPNGEPD,
24808   IX86_BUILTIN_CMPORDPD,
24809   IX86_BUILTIN_CMPUNORDPD,
24810   IX86_BUILTIN_CMPEQSD,
24811   IX86_BUILTIN_CMPLTSD,
24812   IX86_BUILTIN_CMPLESD,
24813   IX86_BUILTIN_CMPNEQSD,
24814   IX86_BUILTIN_CMPNLTSD,
24815   IX86_BUILTIN_CMPNLESD,
24816   IX86_BUILTIN_CMPORDSD,
24817   IX86_BUILTIN_CMPUNORDSD,
24818
24819   IX86_BUILTIN_COMIEQSD,
24820   IX86_BUILTIN_COMILTSD,
24821   IX86_BUILTIN_COMILESD,
24822   IX86_BUILTIN_COMIGTSD,
24823   IX86_BUILTIN_COMIGESD,
24824   IX86_BUILTIN_COMINEQSD,
24825   IX86_BUILTIN_UCOMIEQSD,
24826   IX86_BUILTIN_UCOMILTSD,
24827   IX86_BUILTIN_UCOMILESD,
24828   IX86_BUILTIN_UCOMIGTSD,
24829   IX86_BUILTIN_UCOMIGESD,
24830   IX86_BUILTIN_UCOMINEQSD,
24831
24832   IX86_BUILTIN_MAXPD,
24833   IX86_BUILTIN_MAXSD,
24834   IX86_BUILTIN_MINPD,
24835   IX86_BUILTIN_MINSD,
24836
24837   IX86_BUILTIN_ANDPD,
24838   IX86_BUILTIN_ANDNPD,
24839   IX86_BUILTIN_ORPD,
24840   IX86_BUILTIN_XORPD,
24841
24842   IX86_BUILTIN_SQRTPD,
24843   IX86_BUILTIN_SQRTSD,
24844
24845   IX86_BUILTIN_UNPCKHPD,
24846   IX86_BUILTIN_UNPCKLPD,
24847
24848   IX86_BUILTIN_SHUFPD,
24849
24850   IX86_BUILTIN_LOADUPD,
24851   IX86_BUILTIN_STOREUPD,
24852   IX86_BUILTIN_MOVSD,
24853
24854   IX86_BUILTIN_LOADHPD,
24855   IX86_BUILTIN_LOADLPD,
24856
24857   IX86_BUILTIN_CVTDQ2PD,
24858   IX86_BUILTIN_CVTDQ2PS,
24859
24860   IX86_BUILTIN_CVTPD2DQ,
24861   IX86_BUILTIN_CVTPD2PI,
24862   IX86_BUILTIN_CVTPD2PS,
24863   IX86_BUILTIN_CVTTPD2DQ,
24864   IX86_BUILTIN_CVTTPD2PI,
24865
24866   IX86_BUILTIN_CVTPI2PD,
24867   IX86_BUILTIN_CVTSI2SD,
24868   IX86_BUILTIN_CVTSI642SD,
24869
24870   IX86_BUILTIN_CVTSD2SI,
24871   IX86_BUILTIN_CVTSD2SI64,
24872   IX86_BUILTIN_CVTSD2SS,
24873   IX86_BUILTIN_CVTSS2SD,
24874   IX86_BUILTIN_CVTTSD2SI,
24875   IX86_BUILTIN_CVTTSD2SI64,
24876
24877   IX86_BUILTIN_CVTPS2DQ,
24878   IX86_BUILTIN_CVTPS2PD,
24879   IX86_BUILTIN_CVTTPS2DQ,
24880
24881   IX86_BUILTIN_MOVNTI,
24882   IX86_BUILTIN_MOVNTI64,
24883   IX86_BUILTIN_MOVNTPD,
24884   IX86_BUILTIN_MOVNTDQ,
24885
24886   IX86_BUILTIN_MOVQ128,
24887
24888   /* SSE2 MMX */
24889   IX86_BUILTIN_MASKMOVDQU,
24890   IX86_BUILTIN_MOVMSKPD,
24891   IX86_BUILTIN_PMOVMSKB128,
24892
24893   IX86_BUILTIN_PACKSSWB128,
24894   IX86_BUILTIN_PACKSSDW128,
24895   IX86_BUILTIN_PACKUSWB128,
24896
24897   IX86_BUILTIN_PADDB128,
24898   IX86_BUILTIN_PADDW128,
24899   IX86_BUILTIN_PADDD128,
24900   IX86_BUILTIN_PADDQ128,
24901   IX86_BUILTIN_PADDSB128,
24902   IX86_BUILTIN_PADDSW128,
24903   IX86_BUILTIN_PADDUSB128,
24904   IX86_BUILTIN_PADDUSW128,
24905   IX86_BUILTIN_PSUBB128,
24906   IX86_BUILTIN_PSUBW128,
24907   IX86_BUILTIN_PSUBD128,
24908   IX86_BUILTIN_PSUBQ128,
24909   IX86_BUILTIN_PSUBSB128,
24910   IX86_BUILTIN_PSUBSW128,
24911   IX86_BUILTIN_PSUBUSB128,
24912   IX86_BUILTIN_PSUBUSW128,
24913
24914   IX86_BUILTIN_PAND128,
24915   IX86_BUILTIN_PANDN128,
24916   IX86_BUILTIN_POR128,
24917   IX86_BUILTIN_PXOR128,
24918
24919   IX86_BUILTIN_PAVGB128,
24920   IX86_BUILTIN_PAVGW128,
24921
24922   IX86_BUILTIN_PCMPEQB128,
24923   IX86_BUILTIN_PCMPEQW128,
24924   IX86_BUILTIN_PCMPEQD128,
24925   IX86_BUILTIN_PCMPGTB128,
24926   IX86_BUILTIN_PCMPGTW128,
24927   IX86_BUILTIN_PCMPGTD128,
24928
24929   IX86_BUILTIN_PMADDWD128,
24930
24931   IX86_BUILTIN_PMAXSW128,
24932   IX86_BUILTIN_PMAXUB128,
24933   IX86_BUILTIN_PMINSW128,
24934   IX86_BUILTIN_PMINUB128,
24935
24936   IX86_BUILTIN_PMULUDQ,
24937   IX86_BUILTIN_PMULUDQ128,
24938   IX86_BUILTIN_PMULHUW128,
24939   IX86_BUILTIN_PMULHW128,
24940   IX86_BUILTIN_PMULLW128,
24941
24942   IX86_BUILTIN_PSADBW128,
24943   IX86_BUILTIN_PSHUFHW,
24944   IX86_BUILTIN_PSHUFLW,
24945   IX86_BUILTIN_PSHUFD,
24946
24947   IX86_BUILTIN_PSLLDQI128,
24948   IX86_BUILTIN_PSLLWI128,
24949   IX86_BUILTIN_PSLLDI128,
24950   IX86_BUILTIN_PSLLQI128,
24951   IX86_BUILTIN_PSRAWI128,
24952   IX86_BUILTIN_PSRADI128,
24953   IX86_BUILTIN_PSRLDQI128,
24954   IX86_BUILTIN_PSRLWI128,
24955   IX86_BUILTIN_PSRLDI128,
24956   IX86_BUILTIN_PSRLQI128,
24957
24958   IX86_BUILTIN_PSLLDQ128,
24959   IX86_BUILTIN_PSLLW128,
24960   IX86_BUILTIN_PSLLD128,
24961   IX86_BUILTIN_PSLLQ128,
24962   IX86_BUILTIN_PSRAW128,
24963   IX86_BUILTIN_PSRAD128,
24964   IX86_BUILTIN_PSRLW128,
24965   IX86_BUILTIN_PSRLD128,
24966   IX86_BUILTIN_PSRLQ128,
24967
24968   IX86_BUILTIN_PUNPCKHBW128,
24969   IX86_BUILTIN_PUNPCKHWD128,
24970   IX86_BUILTIN_PUNPCKHDQ128,
24971   IX86_BUILTIN_PUNPCKHQDQ128,
24972   IX86_BUILTIN_PUNPCKLBW128,
24973   IX86_BUILTIN_PUNPCKLWD128,
24974   IX86_BUILTIN_PUNPCKLDQ128,
24975   IX86_BUILTIN_PUNPCKLQDQ128,
24976
24977   IX86_BUILTIN_CLFLUSH,
24978   IX86_BUILTIN_MFENCE,
24979   IX86_BUILTIN_LFENCE,
24980   IX86_BUILTIN_PAUSE,
24981
24982   IX86_BUILTIN_BSRSI,
24983   IX86_BUILTIN_BSRDI,
24984   IX86_BUILTIN_RDPMC,
24985   IX86_BUILTIN_RDTSC,
24986   IX86_BUILTIN_RDTSCP,
24987   IX86_BUILTIN_ROLQI,
24988   IX86_BUILTIN_ROLHI,
24989   IX86_BUILTIN_RORQI,
24990   IX86_BUILTIN_RORHI,
24991
24992   /* SSE3.  */
24993   IX86_BUILTIN_ADDSUBPS,
24994   IX86_BUILTIN_HADDPS,
24995   IX86_BUILTIN_HSUBPS,
24996   IX86_BUILTIN_MOVSHDUP,
24997   IX86_BUILTIN_MOVSLDUP,
24998   IX86_BUILTIN_ADDSUBPD,
24999   IX86_BUILTIN_HADDPD,
25000   IX86_BUILTIN_HSUBPD,
25001   IX86_BUILTIN_LDDQU,
25002
25003   IX86_BUILTIN_MONITOR,
25004   IX86_BUILTIN_MWAIT,
25005
25006   /* SSSE3.  */
25007   IX86_BUILTIN_PHADDW,
25008   IX86_BUILTIN_PHADDD,
25009   IX86_BUILTIN_PHADDSW,
25010   IX86_BUILTIN_PHSUBW,
25011   IX86_BUILTIN_PHSUBD,
25012   IX86_BUILTIN_PHSUBSW,
25013   IX86_BUILTIN_PMADDUBSW,
25014   IX86_BUILTIN_PMULHRSW,
25015   IX86_BUILTIN_PSHUFB,
25016   IX86_BUILTIN_PSIGNB,
25017   IX86_BUILTIN_PSIGNW,
25018   IX86_BUILTIN_PSIGND,
25019   IX86_BUILTIN_PALIGNR,
25020   IX86_BUILTIN_PABSB,
25021   IX86_BUILTIN_PABSW,
25022   IX86_BUILTIN_PABSD,
25023
25024   IX86_BUILTIN_PHADDW128,
25025   IX86_BUILTIN_PHADDD128,
25026   IX86_BUILTIN_PHADDSW128,
25027   IX86_BUILTIN_PHSUBW128,
25028   IX86_BUILTIN_PHSUBD128,
25029   IX86_BUILTIN_PHSUBSW128,
25030   IX86_BUILTIN_PMADDUBSW128,
25031   IX86_BUILTIN_PMULHRSW128,
25032   IX86_BUILTIN_PSHUFB128,
25033   IX86_BUILTIN_PSIGNB128,
25034   IX86_BUILTIN_PSIGNW128,
25035   IX86_BUILTIN_PSIGND128,
25036   IX86_BUILTIN_PALIGNR128,
25037   IX86_BUILTIN_PABSB128,
25038   IX86_BUILTIN_PABSW128,
25039   IX86_BUILTIN_PABSD128,
25040
25041   /* AMDFAM10 - SSE4A New Instructions.  */
25042   IX86_BUILTIN_MOVNTSD,
25043   IX86_BUILTIN_MOVNTSS,
25044   IX86_BUILTIN_EXTRQI,
25045   IX86_BUILTIN_EXTRQ,
25046   IX86_BUILTIN_INSERTQI,
25047   IX86_BUILTIN_INSERTQ,
25048
25049   /* SSE4.1.  */
25050   IX86_BUILTIN_BLENDPD,
25051   IX86_BUILTIN_BLENDPS,
25052   IX86_BUILTIN_BLENDVPD,
25053   IX86_BUILTIN_BLENDVPS,
25054   IX86_BUILTIN_PBLENDVB128,
25055   IX86_BUILTIN_PBLENDW128,
25056
25057   IX86_BUILTIN_DPPD,
25058   IX86_BUILTIN_DPPS,
25059
25060   IX86_BUILTIN_INSERTPS128,
25061
25062   IX86_BUILTIN_MOVNTDQA,
25063   IX86_BUILTIN_MPSADBW128,
25064   IX86_BUILTIN_PACKUSDW128,
25065   IX86_BUILTIN_PCMPEQQ,
25066   IX86_BUILTIN_PHMINPOSUW128,
25067
25068   IX86_BUILTIN_PMAXSB128,
25069   IX86_BUILTIN_PMAXSD128,
25070   IX86_BUILTIN_PMAXUD128,
25071   IX86_BUILTIN_PMAXUW128,
25072
25073   IX86_BUILTIN_PMINSB128,
25074   IX86_BUILTIN_PMINSD128,
25075   IX86_BUILTIN_PMINUD128,
25076   IX86_BUILTIN_PMINUW128,
25077
25078   IX86_BUILTIN_PMOVSXBW128,
25079   IX86_BUILTIN_PMOVSXBD128,
25080   IX86_BUILTIN_PMOVSXBQ128,
25081   IX86_BUILTIN_PMOVSXWD128,
25082   IX86_BUILTIN_PMOVSXWQ128,
25083   IX86_BUILTIN_PMOVSXDQ128,
25084
25085   IX86_BUILTIN_PMOVZXBW128,
25086   IX86_BUILTIN_PMOVZXBD128,
25087   IX86_BUILTIN_PMOVZXBQ128,
25088   IX86_BUILTIN_PMOVZXWD128,
25089   IX86_BUILTIN_PMOVZXWQ128,
25090   IX86_BUILTIN_PMOVZXDQ128,
25091
25092   IX86_BUILTIN_PMULDQ128,
25093   IX86_BUILTIN_PMULLD128,
25094
25095   IX86_BUILTIN_ROUNDSD,
25096   IX86_BUILTIN_ROUNDSS,
25097
25098   IX86_BUILTIN_ROUNDPD,
25099   IX86_BUILTIN_ROUNDPS,
25100
25101   IX86_BUILTIN_FLOORPD,
25102   IX86_BUILTIN_CEILPD,
25103   IX86_BUILTIN_TRUNCPD,
25104   IX86_BUILTIN_RINTPD,
25105   IX86_BUILTIN_ROUNDPD_AZ,
25106
25107   IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX,
25108   IX86_BUILTIN_CEILPD_VEC_PACK_SFIX,
25109   IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX,
25110
25111   IX86_BUILTIN_FLOORPS,
25112   IX86_BUILTIN_CEILPS,
25113   IX86_BUILTIN_TRUNCPS,
25114   IX86_BUILTIN_RINTPS,
25115   IX86_BUILTIN_ROUNDPS_AZ,
25116
25117   IX86_BUILTIN_FLOORPS_SFIX,
25118   IX86_BUILTIN_CEILPS_SFIX,
25119   IX86_BUILTIN_ROUNDPS_AZ_SFIX,
25120
25121   IX86_BUILTIN_PTESTZ,
25122   IX86_BUILTIN_PTESTC,
25123   IX86_BUILTIN_PTESTNZC,
25124
25125   IX86_BUILTIN_VEC_INIT_V2SI,
25126   IX86_BUILTIN_VEC_INIT_V4HI,
25127   IX86_BUILTIN_VEC_INIT_V8QI,
25128   IX86_BUILTIN_VEC_EXT_V2DF,
25129   IX86_BUILTIN_VEC_EXT_V2DI,
25130   IX86_BUILTIN_VEC_EXT_V4SF,
25131   IX86_BUILTIN_VEC_EXT_V4SI,
25132   IX86_BUILTIN_VEC_EXT_V8HI,
25133   IX86_BUILTIN_VEC_EXT_V2SI,
25134   IX86_BUILTIN_VEC_EXT_V4HI,
25135   IX86_BUILTIN_VEC_EXT_V16QI,
25136   IX86_BUILTIN_VEC_SET_V2DI,
25137   IX86_BUILTIN_VEC_SET_V4SF,
25138   IX86_BUILTIN_VEC_SET_V4SI,
25139   IX86_BUILTIN_VEC_SET_V8HI,
25140   IX86_BUILTIN_VEC_SET_V4HI,
25141   IX86_BUILTIN_VEC_SET_V16QI,
25142
25143   IX86_BUILTIN_VEC_PACK_SFIX,
25144   IX86_BUILTIN_VEC_PACK_SFIX256,
25145
25146   /* SSE4.2.  */
25147   IX86_BUILTIN_CRC32QI,
25148   IX86_BUILTIN_CRC32HI,
25149   IX86_BUILTIN_CRC32SI,
25150   IX86_BUILTIN_CRC32DI,
25151
25152   IX86_BUILTIN_PCMPESTRI128,
25153   IX86_BUILTIN_PCMPESTRM128,
25154   IX86_BUILTIN_PCMPESTRA128,
25155   IX86_BUILTIN_PCMPESTRC128,
25156   IX86_BUILTIN_PCMPESTRO128,
25157   IX86_BUILTIN_PCMPESTRS128,
25158   IX86_BUILTIN_PCMPESTRZ128,
25159   IX86_BUILTIN_PCMPISTRI128,
25160   IX86_BUILTIN_PCMPISTRM128,
25161   IX86_BUILTIN_PCMPISTRA128,
25162   IX86_BUILTIN_PCMPISTRC128,
25163   IX86_BUILTIN_PCMPISTRO128,
25164   IX86_BUILTIN_PCMPISTRS128,
25165   IX86_BUILTIN_PCMPISTRZ128,
25166
25167   IX86_BUILTIN_PCMPGTQ,
25168
25169   /* AES instructions */
25170   IX86_BUILTIN_AESENC128,
25171   IX86_BUILTIN_AESENCLAST128,
25172   IX86_BUILTIN_AESDEC128,
25173   IX86_BUILTIN_AESDECLAST128,
25174   IX86_BUILTIN_AESIMC128,
25175   IX86_BUILTIN_AESKEYGENASSIST128,
25176
25177   /* PCLMUL instruction */
25178   IX86_BUILTIN_PCLMULQDQ128,
25179
25180   /* AVX */
25181   IX86_BUILTIN_ADDPD256,
25182   IX86_BUILTIN_ADDPS256,
25183   IX86_BUILTIN_ADDSUBPD256,
25184   IX86_BUILTIN_ADDSUBPS256,
25185   IX86_BUILTIN_ANDPD256,
25186   IX86_BUILTIN_ANDPS256,
25187   IX86_BUILTIN_ANDNPD256,
25188   IX86_BUILTIN_ANDNPS256,
25189   IX86_BUILTIN_BLENDPD256,
25190   IX86_BUILTIN_BLENDPS256,
25191   IX86_BUILTIN_BLENDVPD256,
25192   IX86_BUILTIN_BLENDVPS256,
25193   IX86_BUILTIN_DIVPD256,
25194   IX86_BUILTIN_DIVPS256,
25195   IX86_BUILTIN_DPPS256,
25196   IX86_BUILTIN_HADDPD256,
25197   IX86_BUILTIN_HADDPS256,
25198   IX86_BUILTIN_HSUBPD256,
25199   IX86_BUILTIN_HSUBPS256,
25200   IX86_BUILTIN_MAXPD256,
25201   IX86_BUILTIN_MAXPS256,
25202   IX86_BUILTIN_MINPD256,
25203   IX86_BUILTIN_MINPS256,
25204   IX86_BUILTIN_MULPD256,
25205   IX86_BUILTIN_MULPS256,
25206   IX86_BUILTIN_ORPD256,
25207   IX86_BUILTIN_ORPS256,
25208   IX86_BUILTIN_SHUFPD256,
25209   IX86_BUILTIN_SHUFPS256,
25210   IX86_BUILTIN_SUBPD256,
25211   IX86_BUILTIN_SUBPS256,
25212   IX86_BUILTIN_XORPD256,
25213   IX86_BUILTIN_XORPS256,
25214   IX86_BUILTIN_CMPSD,
25215   IX86_BUILTIN_CMPSS,
25216   IX86_BUILTIN_CMPPD,
25217   IX86_BUILTIN_CMPPS,
25218   IX86_BUILTIN_CMPPD256,
25219   IX86_BUILTIN_CMPPS256,
25220   IX86_BUILTIN_CVTDQ2PD256,
25221   IX86_BUILTIN_CVTDQ2PS256,
25222   IX86_BUILTIN_CVTPD2PS256,
25223   IX86_BUILTIN_CVTPS2DQ256,
25224   IX86_BUILTIN_CVTPS2PD256,
25225   IX86_BUILTIN_CVTTPD2DQ256,
25226   IX86_BUILTIN_CVTPD2DQ256,
25227   IX86_BUILTIN_CVTTPS2DQ256,
25228   IX86_BUILTIN_EXTRACTF128PD256,
25229   IX86_BUILTIN_EXTRACTF128PS256,
25230   IX86_BUILTIN_EXTRACTF128SI256,
25231   IX86_BUILTIN_VZEROALL,
25232   IX86_BUILTIN_VZEROUPPER,
25233   IX86_BUILTIN_VPERMILVARPD,
25234   IX86_BUILTIN_VPERMILVARPS,
25235   IX86_BUILTIN_VPERMILVARPD256,
25236   IX86_BUILTIN_VPERMILVARPS256,
25237   IX86_BUILTIN_VPERMILPD,
25238   IX86_BUILTIN_VPERMILPS,
25239   IX86_BUILTIN_VPERMILPD256,
25240   IX86_BUILTIN_VPERMILPS256,
25241   IX86_BUILTIN_VPERMIL2PD,
25242   IX86_BUILTIN_VPERMIL2PS,
25243   IX86_BUILTIN_VPERMIL2PD256,
25244   IX86_BUILTIN_VPERMIL2PS256,
25245   IX86_BUILTIN_VPERM2F128PD256,
25246   IX86_BUILTIN_VPERM2F128PS256,
25247   IX86_BUILTIN_VPERM2F128SI256,
25248   IX86_BUILTIN_VBROADCASTSS,
25249   IX86_BUILTIN_VBROADCASTSD256,
25250   IX86_BUILTIN_VBROADCASTSS256,
25251   IX86_BUILTIN_VBROADCASTPD256,
25252   IX86_BUILTIN_VBROADCASTPS256,
25253   IX86_BUILTIN_VINSERTF128PD256,
25254   IX86_BUILTIN_VINSERTF128PS256,
25255   IX86_BUILTIN_VINSERTF128SI256,
25256   IX86_BUILTIN_LOADUPD256,
25257   IX86_BUILTIN_LOADUPS256,
25258   IX86_BUILTIN_STOREUPD256,
25259   IX86_BUILTIN_STOREUPS256,
25260   IX86_BUILTIN_LDDQU256,
25261   IX86_BUILTIN_MOVNTDQ256,
25262   IX86_BUILTIN_MOVNTPD256,
25263   IX86_BUILTIN_MOVNTPS256,
25264   IX86_BUILTIN_LOADDQU256,
25265   IX86_BUILTIN_STOREDQU256,
25266   IX86_BUILTIN_MASKLOADPD,
25267   IX86_BUILTIN_MASKLOADPS,
25268   IX86_BUILTIN_MASKSTOREPD,
25269   IX86_BUILTIN_MASKSTOREPS,
25270   IX86_BUILTIN_MASKLOADPD256,
25271   IX86_BUILTIN_MASKLOADPS256,
25272   IX86_BUILTIN_MASKSTOREPD256,
25273   IX86_BUILTIN_MASKSTOREPS256,
25274   IX86_BUILTIN_MOVSHDUP256,
25275   IX86_BUILTIN_MOVSLDUP256,
25276   IX86_BUILTIN_MOVDDUP256,
25277
25278   IX86_BUILTIN_SQRTPD256,
25279   IX86_BUILTIN_SQRTPS256,
25280   IX86_BUILTIN_SQRTPS_NR256,
25281   IX86_BUILTIN_RSQRTPS256,
25282   IX86_BUILTIN_RSQRTPS_NR256,
25283
25284   IX86_BUILTIN_RCPPS256,
25285
25286   IX86_BUILTIN_ROUNDPD256,
25287   IX86_BUILTIN_ROUNDPS256,
25288
25289   IX86_BUILTIN_FLOORPD256,
25290   IX86_BUILTIN_CEILPD256,
25291   IX86_BUILTIN_TRUNCPD256,
25292   IX86_BUILTIN_RINTPD256,
25293   IX86_BUILTIN_ROUNDPD_AZ256,
25294
25295   IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX256,
25296   IX86_BUILTIN_CEILPD_VEC_PACK_SFIX256,
25297   IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX256,
25298
25299   IX86_BUILTIN_FLOORPS256,
25300   IX86_BUILTIN_CEILPS256,
25301   IX86_BUILTIN_TRUNCPS256,
25302   IX86_BUILTIN_RINTPS256,
25303   IX86_BUILTIN_ROUNDPS_AZ256,
25304
25305   IX86_BUILTIN_FLOORPS_SFIX256,
25306   IX86_BUILTIN_CEILPS_SFIX256,
25307   IX86_BUILTIN_ROUNDPS_AZ_SFIX256,
25308
25309   IX86_BUILTIN_UNPCKHPD256,
25310   IX86_BUILTIN_UNPCKLPD256,
25311   IX86_BUILTIN_UNPCKHPS256,
25312   IX86_BUILTIN_UNPCKLPS256,
25313
25314   IX86_BUILTIN_SI256_SI,
25315   IX86_BUILTIN_PS256_PS,
25316   IX86_BUILTIN_PD256_PD,
25317   IX86_BUILTIN_SI_SI256,
25318   IX86_BUILTIN_PS_PS256,
25319   IX86_BUILTIN_PD_PD256,
25320
25321   IX86_BUILTIN_VTESTZPD,
25322   IX86_BUILTIN_VTESTCPD,
25323   IX86_BUILTIN_VTESTNZCPD,
25324   IX86_BUILTIN_VTESTZPS,
25325   IX86_BUILTIN_VTESTCPS,
25326   IX86_BUILTIN_VTESTNZCPS,
25327   IX86_BUILTIN_VTESTZPD256,
25328   IX86_BUILTIN_VTESTCPD256,
25329   IX86_BUILTIN_VTESTNZCPD256,
25330   IX86_BUILTIN_VTESTZPS256,
25331   IX86_BUILTIN_VTESTCPS256,
25332   IX86_BUILTIN_VTESTNZCPS256,
25333   IX86_BUILTIN_PTESTZ256,
25334   IX86_BUILTIN_PTESTC256,
25335   IX86_BUILTIN_PTESTNZC256,
25336
25337   IX86_BUILTIN_MOVMSKPD256,
25338   IX86_BUILTIN_MOVMSKPS256,
25339
25340   /* AVX2 */
25341   IX86_BUILTIN_MPSADBW256,
25342   IX86_BUILTIN_PABSB256,
25343   IX86_BUILTIN_PABSW256,
25344   IX86_BUILTIN_PABSD256,
25345   IX86_BUILTIN_PACKSSDW256,
25346   IX86_BUILTIN_PACKSSWB256,
25347   IX86_BUILTIN_PACKUSDW256,
25348   IX86_BUILTIN_PACKUSWB256,
25349   IX86_BUILTIN_PADDB256,
25350   IX86_BUILTIN_PADDW256,
25351   IX86_BUILTIN_PADDD256,
25352   IX86_BUILTIN_PADDQ256,
25353   IX86_BUILTIN_PADDSB256,
25354   IX86_BUILTIN_PADDSW256,
25355   IX86_BUILTIN_PADDUSB256,
25356   IX86_BUILTIN_PADDUSW256,
25357   IX86_BUILTIN_PALIGNR256,
25358   IX86_BUILTIN_AND256I,
25359   IX86_BUILTIN_ANDNOT256I,
25360   IX86_BUILTIN_PAVGB256,
25361   IX86_BUILTIN_PAVGW256,
25362   IX86_BUILTIN_PBLENDVB256,
25363   IX86_BUILTIN_PBLENDVW256,
25364   IX86_BUILTIN_PCMPEQB256,
25365   IX86_BUILTIN_PCMPEQW256,
25366   IX86_BUILTIN_PCMPEQD256,
25367   IX86_BUILTIN_PCMPEQQ256,
25368   IX86_BUILTIN_PCMPGTB256,
25369   IX86_BUILTIN_PCMPGTW256,
25370   IX86_BUILTIN_PCMPGTD256,
25371   IX86_BUILTIN_PCMPGTQ256,
25372   IX86_BUILTIN_PHADDW256,
25373   IX86_BUILTIN_PHADDD256,
25374   IX86_BUILTIN_PHADDSW256,
25375   IX86_BUILTIN_PHSUBW256,
25376   IX86_BUILTIN_PHSUBD256,
25377   IX86_BUILTIN_PHSUBSW256,
25378   IX86_BUILTIN_PMADDUBSW256,
25379   IX86_BUILTIN_PMADDWD256,
25380   IX86_BUILTIN_PMAXSB256,
25381   IX86_BUILTIN_PMAXSW256,
25382   IX86_BUILTIN_PMAXSD256,
25383   IX86_BUILTIN_PMAXUB256,
25384   IX86_BUILTIN_PMAXUW256,
25385   IX86_BUILTIN_PMAXUD256,
25386   IX86_BUILTIN_PMINSB256,
25387   IX86_BUILTIN_PMINSW256,
25388   IX86_BUILTIN_PMINSD256,
25389   IX86_BUILTIN_PMINUB256,
25390   IX86_BUILTIN_PMINUW256,
25391   IX86_BUILTIN_PMINUD256,
25392   IX86_BUILTIN_PMOVMSKB256,
25393   IX86_BUILTIN_PMOVSXBW256,
25394   IX86_BUILTIN_PMOVSXBD256,
25395   IX86_BUILTIN_PMOVSXBQ256,
25396   IX86_BUILTIN_PMOVSXWD256,
25397   IX86_BUILTIN_PMOVSXWQ256,
25398   IX86_BUILTIN_PMOVSXDQ256,
25399   IX86_BUILTIN_PMOVZXBW256,
25400   IX86_BUILTIN_PMOVZXBD256,
25401   IX86_BUILTIN_PMOVZXBQ256,
25402   IX86_BUILTIN_PMOVZXWD256,
25403   IX86_BUILTIN_PMOVZXWQ256,
25404   IX86_BUILTIN_PMOVZXDQ256,
25405   IX86_BUILTIN_PMULDQ256,
25406   IX86_BUILTIN_PMULHRSW256,
25407   IX86_BUILTIN_PMULHUW256,
25408   IX86_BUILTIN_PMULHW256,
25409   IX86_BUILTIN_PMULLW256,
25410   IX86_BUILTIN_PMULLD256,
25411   IX86_BUILTIN_PMULUDQ256,
25412   IX86_BUILTIN_POR256,
25413   IX86_BUILTIN_PSADBW256,
25414   IX86_BUILTIN_PSHUFB256,
25415   IX86_BUILTIN_PSHUFD256,
25416   IX86_BUILTIN_PSHUFHW256,
25417   IX86_BUILTIN_PSHUFLW256,
25418   IX86_BUILTIN_PSIGNB256,
25419   IX86_BUILTIN_PSIGNW256,
25420   IX86_BUILTIN_PSIGND256,
25421   IX86_BUILTIN_PSLLDQI256,
25422   IX86_BUILTIN_PSLLWI256,
25423   IX86_BUILTIN_PSLLW256,
25424   IX86_BUILTIN_PSLLDI256,
25425   IX86_BUILTIN_PSLLD256,
25426   IX86_BUILTIN_PSLLQI256,
25427   IX86_BUILTIN_PSLLQ256,
25428   IX86_BUILTIN_PSRAWI256,
25429   IX86_BUILTIN_PSRAW256,
25430   IX86_BUILTIN_PSRADI256,
25431   IX86_BUILTIN_PSRAD256,
25432   IX86_BUILTIN_PSRLDQI256,
25433   IX86_BUILTIN_PSRLWI256,
25434   IX86_BUILTIN_PSRLW256,
25435   IX86_BUILTIN_PSRLDI256,
25436   IX86_BUILTIN_PSRLD256,
25437   IX86_BUILTIN_PSRLQI256,
25438   IX86_BUILTIN_PSRLQ256,
25439   IX86_BUILTIN_PSUBB256,
25440   IX86_BUILTIN_PSUBW256,
25441   IX86_BUILTIN_PSUBD256,
25442   IX86_BUILTIN_PSUBQ256,
25443   IX86_BUILTIN_PSUBSB256,
25444   IX86_BUILTIN_PSUBSW256,
25445   IX86_BUILTIN_PSUBUSB256,
25446   IX86_BUILTIN_PSUBUSW256,
25447   IX86_BUILTIN_PUNPCKHBW256,
25448   IX86_BUILTIN_PUNPCKHWD256,
25449   IX86_BUILTIN_PUNPCKHDQ256,
25450   IX86_BUILTIN_PUNPCKHQDQ256,
25451   IX86_BUILTIN_PUNPCKLBW256,
25452   IX86_BUILTIN_PUNPCKLWD256,
25453   IX86_BUILTIN_PUNPCKLDQ256,
25454   IX86_BUILTIN_PUNPCKLQDQ256,
25455   IX86_BUILTIN_PXOR256,
25456   IX86_BUILTIN_MOVNTDQA256,
25457   IX86_BUILTIN_VBROADCASTSS_PS,
25458   IX86_BUILTIN_VBROADCASTSS_PS256,
25459   IX86_BUILTIN_VBROADCASTSD_PD256,
25460   IX86_BUILTIN_VBROADCASTSI256,
25461   IX86_BUILTIN_PBLENDD256,
25462   IX86_BUILTIN_PBLENDD128,
25463   IX86_BUILTIN_PBROADCASTB256,
25464   IX86_BUILTIN_PBROADCASTW256,
25465   IX86_BUILTIN_PBROADCASTD256,
25466   IX86_BUILTIN_PBROADCASTQ256,
25467   IX86_BUILTIN_PBROADCASTB128,
25468   IX86_BUILTIN_PBROADCASTW128,
25469   IX86_BUILTIN_PBROADCASTD128,
25470   IX86_BUILTIN_PBROADCASTQ128,
25471   IX86_BUILTIN_VPERMVARSI256,
25472   IX86_BUILTIN_VPERMDF256,
25473   IX86_BUILTIN_VPERMVARSF256,
25474   IX86_BUILTIN_VPERMDI256,
25475   IX86_BUILTIN_VPERMTI256,
25476   IX86_BUILTIN_VEXTRACT128I256,
25477   IX86_BUILTIN_VINSERT128I256,
25478   IX86_BUILTIN_MASKLOADD,
25479   IX86_BUILTIN_MASKLOADQ,
25480   IX86_BUILTIN_MASKLOADD256,
25481   IX86_BUILTIN_MASKLOADQ256,
25482   IX86_BUILTIN_MASKSTORED,
25483   IX86_BUILTIN_MASKSTOREQ,
25484   IX86_BUILTIN_MASKSTORED256,
25485   IX86_BUILTIN_MASKSTOREQ256,
25486   IX86_BUILTIN_PSLLVV4DI,
25487   IX86_BUILTIN_PSLLVV2DI,
25488   IX86_BUILTIN_PSLLVV8SI,
25489   IX86_BUILTIN_PSLLVV4SI,
25490   IX86_BUILTIN_PSRAVV8SI,
25491   IX86_BUILTIN_PSRAVV4SI,
25492   IX86_BUILTIN_PSRLVV4DI,
25493   IX86_BUILTIN_PSRLVV2DI,
25494   IX86_BUILTIN_PSRLVV8SI,
25495   IX86_BUILTIN_PSRLVV4SI,
25496
25497   IX86_BUILTIN_GATHERSIV2DF,
25498   IX86_BUILTIN_GATHERSIV4DF,
25499   IX86_BUILTIN_GATHERDIV2DF,
25500   IX86_BUILTIN_GATHERDIV4DF,
25501   IX86_BUILTIN_GATHERSIV4SF,
25502   IX86_BUILTIN_GATHERSIV8SF,
25503   IX86_BUILTIN_GATHERDIV4SF,
25504   IX86_BUILTIN_GATHERDIV8SF,
25505   IX86_BUILTIN_GATHERSIV2DI,
25506   IX86_BUILTIN_GATHERSIV4DI,
25507   IX86_BUILTIN_GATHERDIV2DI,
25508   IX86_BUILTIN_GATHERDIV4DI,
25509   IX86_BUILTIN_GATHERSIV4SI,
25510   IX86_BUILTIN_GATHERSIV8SI,
25511   IX86_BUILTIN_GATHERDIV4SI,
25512   IX86_BUILTIN_GATHERDIV8SI,
25513
25514   /* Alternate 4 element gather for the vectorizer where
25515      all operands are 32-byte wide.  */
25516   IX86_BUILTIN_GATHERALTSIV4DF,
25517   IX86_BUILTIN_GATHERALTDIV8SF,
25518   IX86_BUILTIN_GATHERALTSIV4DI,
25519   IX86_BUILTIN_GATHERALTDIV8SI,
25520
25521   /* TFmode support builtins.  */
25522   IX86_BUILTIN_INFQ,
25523   IX86_BUILTIN_HUGE_VALQ,
25524   IX86_BUILTIN_FABSQ,
25525   IX86_BUILTIN_COPYSIGNQ,
25526
25527   /* Vectorizer support builtins.  */
25528   IX86_BUILTIN_CPYSGNPS,
25529   IX86_BUILTIN_CPYSGNPD,
25530   IX86_BUILTIN_CPYSGNPS256,
25531   IX86_BUILTIN_CPYSGNPD256,
25532
25533   /* FMA4 instructions.  */
25534   IX86_BUILTIN_VFMADDSS,
25535   IX86_BUILTIN_VFMADDSD,
25536   IX86_BUILTIN_VFMADDPS,
25537   IX86_BUILTIN_VFMADDPD,
25538   IX86_BUILTIN_VFMADDPS256,
25539   IX86_BUILTIN_VFMADDPD256,
25540   IX86_BUILTIN_VFMADDSUBPS,
25541   IX86_BUILTIN_VFMADDSUBPD,
25542   IX86_BUILTIN_VFMADDSUBPS256,
25543   IX86_BUILTIN_VFMADDSUBPD256,
25544
25545   /* FMA3 instructions.  */
25546   IX86_BUILTIN_VFMADDSS3,
25547   IX86_BUILTIN_VFMADDSD3,
25548
25549   /* XOP instructions.  */
25550   IX86_BUILTIN_VPCMOV,
25551   IX86_BUILTIN_VPCMOV_V2DI,
25552   IX86_BUILTIN_VPCMOV_V4SI,
25553   IX86_BUILTIN_VPCMOV_V8HI,
25554   IX86_BUILTIN_VPCMOV_V16QI,
25555   IX86_BUILTIN_VPCMOV_V4SF,
25556   IX86_BUILTIN_VPCMOV_V2DF,
25557   IX86_BUILTIN_VPCMOV256,
25558   IX86_BUILTIN_VPCMOV_V4DI256,
25559   IX86_BUILTIN_VPCMOV_V8SI256,
25560   IX86_BUILTIN_VPCMOV_V16HI256,
25561   IX86_BUILTIN_VPCMOV_V32QI256,
25562   IX86_BUILTIN_VPCMOV_V8SF256,
25563   IX86_BUILTIN_VPCMOV_V4DF256,
25564
25565   IX86_BUILTIN_VPPERM,
25566
25567   IX86_BUILTIN_VPMACSSWW,
25568   IX86_BUILTIN_VPMACSWW,
25569   IX86_BUILTIN_VPMACSSWD,
25570   IX86_BUILTIN_VPMACSWD,
25571   IX86_BUILTIN_VPMACSSDD,
25572   IX86_BUILTIN_VPMACSDD,
25573   IX86_BUILTIN_VPMACSSDQL,
25574   IX86_BUILTIN_VPMACSSDQH,
25575   IX86_BUILTIN_VPMACSDQL,
25576   IX86_BUILTIN_VPMACSDQH,
25577   IX86_BUILTIN_VPMADCSSWD,
25578   IX86_BUILTIN_VPMADCSWD,
25579
25580   IX86_BUILTIN_VPHADDBW,
25581   IX86_BUILTIN_VPHADDBD,
25582   IX86_BUILTIN_VPHADDBQ,
25583   IX86_BUILTIN_VPHADDWD,
25584   IX86_BUILTIN_VPHADDWQ,
25585   IX86_BUILTIN_VPHADDDQ,
25586   IX86_BUILTIN_VPHADDUBW,
25587   IX86_BUILTIN_VPHADDUBD,
25588   IX86_BUILTIN_VPHADDUBQ,
25589   IX86_BUILTIN_VPHADDUWD,
25590   IX86_BUILTIN_VPHADDUWQ,
25591   IX86_BUILTIN_VPHADDUDQ,
25592   IX86_BUILTIN_VPHSUBBW,
25593   IX86_BUILTIN_VPHSUBWD,
25594   IX86_BUILTIN_VPHSUBDQ,
25595
25596   IX86_BUILTIN_VPROTB,
25597   IX86_BUILTIN_VPROTW,
25598   IX86_BUILTIN_VPROTD,
25599   IX86_BUILTIN_VPROTQ,
25600   IX86_BUILTIN_VPROTB_IMM,
25601   IX86_BUILTIN_VPROTW_IMM,
25602   IX86_BUILTIN_VPROTD_IMM,
25603   IX86_BUILTIN_VPROTQ_IMM,
25604
25605   IX86_BUILTIN_VPSHLB,
25606   IX86_BUILTIN_VPSHLW,
25607   IX86_BUILTIN_VPSHLD,
25608   IX86_BUILTIN_VPSHLQ,
25609   IX86_BUILTIN_VPSHAB,
25610   IX86_BUILTIN_VPSHAW,
25611   IX86_BUILTIN_VPSHAD,
25612   IX86_BUILTIN_VPSHAQ,
25613
25614   IX86_BUILTIN_VFRCZSS,
25615   IX86_BUILTIN_VFRCZSD,
25616   IX86_BUILTIN_VFRCZPS,
25617   IX86_BUILTIN_VFRCZPD,
25618   IX86_BUILTIN_VFRCZPS256,
25619   IX86_BUILTIN_VFRCZPD256,
25620
25621   IX86_BUILTIN_VPCOMEQUB,
25622   IX86_BUILTIN_VPCOMNEUB,
25623   IX86_BUILTIN_VPCOMLTUB,
25624   IX86_BUILTIN_VPCOMLEUB,
25625   IX86_BUILTIN_VPCOMGTUB,
25626   IX86_BUILTIN_VPCOMGEUB,
25627   IX86_BUILTIN_VPCOMFALSEUB,
25628   IX86_BUILTIN_VPCOMTRUEUB,
25629
25630   IX86_BUILTIN_VPCOMEQUW,
25631   IX86_BUILTIN_VPCOMNEUW,
25632   IX86_BUILTIN_VPCOMLTUW,
25633   IX86_BUILTIN_VPCOMLEUW,
25634   IX86_BUILTIN_VPCOMGTUW,
25635   IX86_BUILTIN_VPCOMGEUW,
25636   IX86_BUILTIN_VPCOMFALSEUW,
25637   IX86_BUILTIN_VPCOMTRUEUW,
25638
25639   IX86_BUILTIN_VPCOMEQUD,
25640   IX86_BUILTIN_VPCOMNEUD,
25641   IX86_BUILTIN_VPCOMLTUD,
25642   IX86_BUILTIN_VPCOMLEUD,
25643   IX86_BUILTIN_VPCOMGTUD,
25644   IX86_BUILTIN_VPCOMGEUD,
25645   IX86_BUILTIN_VPCOMFALSEUD,
25646   IX86_BUILTIN_VPCOMTRUEUD,
25647
25648   IX86_BUILTIN_VPCOMEQUQ,
25649   IX86_BUILTIN_VPCOMNEUQ,
25650   IX86_BUILTIN_VPCOMLTUQ,
25651   IX86_BUILTIN_VPCOMLEUQ,
25652   IX86_BUILTIN_VPCOMGTUQ,
25653   IX86_BUILTIN_VPCOMGEUQ,
25654   IX86_BUILTIN_VPCOMFALSEUQ,
25655   IX86_BUILTIN_VPCOMTRUEUQ,
25656
25657   IX86_BUILTIN_VPCOMEQB,
25658   IX86_BUILTIN_VPCOMNEB,
25659   IX86_BUILTIN_VPCOMLTB,
25660   IX86_BUILTIN_VPCOMLEB,
25661   IX86_BUILTIN_VPCOMGTB,
25662   IX86_BUILTIN_VPCOMGEB,
25663   IX86_BUILTIN_VPCOMFALSEB,
25664   IX86_BUILTIN_VPCOMTRUEB,
25665
25666   IX86_BUILTIN_VPCOMEQW,
25667   IX86_BUILTIN_VPCOMNEW,
25668   IX86_BUILTIN_VPCOMLTW,
25669   IX86_BUILTIN_VPCOMLEW,
25670   IX86_BUILTIN_VPCOMGTW,
25671   IX86_BUILTIN_VPCOMGEW,
25672   IX86_BUILTIN_VPCOMFALSEW,
25673   IX86_BUILTIN_VPCOMTRUEW,
25674
25675   IX86_BUILTIN_VPCOMEQD,
25676   IX86_BUILTIN_VPCOMNED,
25677   IX86_BUILTIN_VPCOMLTD,
25678   IX86_BUILTIN_VPCOMLED,
25679   IX86_BUILTIN_VPCOMGTD,
25680   IX86_BUILTIN_VPCOMGED,
25681   IX86_BUILTIN_VPCOMFALSED,
25682   IX86_BUILTIN_VPCOMTRUED,
25683
25684   IX86_BUILTIN_VPCOMEQQ,
25685   IX86_BUILTIN_VPCOMNEQ,
25686   IX86_BUILTIN_VPCOMLTQ,
25687   IX86_BUILTIN_VPCOMLEQ,
25688   IX86_BUILTIN_VPCOMGTQ,
25689   IX86_BUILTIN_VPCOMGEQ,
25690   IX86_BUILTIN_VPCOMFALSEQ,
25691   IX86_BUILTIN_VPCOMTRUEQ,
25692
25693   /* LWP instructions.  */
25694   IX86_BUILTIN_LLWPCB,
25695   IX86_BUILTIN_SLWPCB,
25696   IX86_BUILTIN_LWPVAL32,
25697   IX86_BUILTIN_LWPVAL64,
25698   IX86_BUILTIN_LWPINS32,
25699   IX86_BUILTIN_LWPINS64,
25700
25701   IX86_BUILTIN_CLZS,
25702
25703   /* BMI instructions.  */
25704   IX86_BUILTIN_BEXTR32,
25705   IX86_BUILTIN_BEXTR64,
25706   IX86_BUILTIN_CTZS,
25707
25708   /* TBM instructions.  */
25709   IX86_BUILTIN_BEXTRI32,
25710   IX86_BUILTIN_BEXTRI64,
25711
25712   /* BMI2 instructions. */
25713   IX86_BUILTIN_BZHI32,
25714   IX86_BUILTIN_BZHI64,
25715   IX86_BUILTIN_PDEP32,
25716   IX86_BUILTIN_PDEP64,
25717   IX86_BUILTIN_PEXT32,
25718   IX86_BUILTIN_PEXT64,
25719
25720   /* FSGSBASE instructions.  */
25721   IX86_BUILTIN_RDFSBASE32,
25722   IX86_BUILTIN_RDFSBASE64,
25723   IX86_BUILTIN_RDGSBASE32,
25724   IX86_BUILTIN_RDGSBASE64,
25725   IX86_BUILTIN_WRFSBASE32,
25726   IX86_BUILTIN_WRFSBASE64,
25727   IX86_BUILTIN_WRGSBASE32,
25728   IX86_BUILTIN_WRGSBASE64,
25729
25730   /* RDRND instructions.  */
25731   IX86_BUILTIN_RDRAND16_STEP,
25732   IX86_BUILTIN_RDRAND32_STEP,
25733   IX86_BUILTIN_RDRAND64_STEP,
25734
25735   /* F16C instructions.  */
25736   IX86_BUILTIN_CVTPH2PS,
25737   IX86_BUILTIN_CVTPH2PS256,
25738   IX86_BUILTIN_CVTPS2PH,
25739   IX86_BUILTIN_CVTPS2PH256,
25740
25741   /* CFString built-in for darwin */
25742   IX86_BUILTIN_CFSTRING,
25743
25744   IX86_BUILTIN_MAX
25745 };
25746
25747 /* Table for the ix86 builtin decls.  */
25748 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
25749
25750 /* Table of all of the builtin functions that are possible with different ISA's
25751    but are waiting to be built until a function is declared to use that
25752    ISA.  */
25753 struct builtin_isa {
25754   const char *name;             /* function name */
25755   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
25756   HOST_WIDE_INT isa;            /* isa_flags this builtin is defined for */
25757   bool const_p;                 /* true if the declaration is constant */
25758   bool set_and_not_built_p;
25759 };
25760
25761 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
25762
25763
25764 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
25765    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
25766    function decl in the ix86_builtins array.  Returns the function decl or
25767    NULL_TREE, if the builtin was not added.
25768
25769    If the front end has a special hook for builtin functions, delay adding
25770    builtin functions that aren't in the current ISA until the ISA is changed
25771    with function specific optimization.  Doing so, can save about 300K for the
25772    default compiler.  When the builtin is expanded, check at that time whether
25773    it is valid.
25774
25775    If the front end doesn't have a special hook, record all builtins, even if
25776    it isn't an instruction set in the current ISA in case the user uses
25777    function specific options for a different ISA, so that we don't get scope
25778    errors if a builtin is added in the middle of a function scope.  */
25779
25780 static inline tree
25781 def_builtin (HOST_WIDE_INT mask, const char *name,
25782              enum ix86_builtin_func_type tcode,
25783              enum ix86_builtins code)
25784 {
25785   tree decl = NULL_TREE;
25786
25787   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
25788     {
25789       ix86_builtins_isa[(int) code].isa = mask;
25790
25791       mask &= ~OPTION_MASK_ISA_64BIT;
25792       if (mask == 0
25793           || (mask & ix86_isa_flags) != 0
25794           || (lang_hooks.builtin_function
25795               == lang_hooks.builtin_function_ext_scope))
25796
25797         {
25798           tree type = ix86_get_builtin_func_type (tcode);
25799           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
25800                                        NULL, NULL_TREE);
25801           ix86_builtins[(int) code] = decl;
25802           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
25803         }
25804       else
25805         {
25806           ix86_builtins[(int) code] = NULL_TREE;
25807           ix86_builtins_isa[(int) code].tcode = tcode;
25808           ix86_builtins_isa[(int) code].name = name;
25809           ix86_builtins_isa[(int) code].const_p = false;
25810           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
25811         }
25812     }
25813
25814   return decl;
25815 }
25816
25817 /* Like def_builtin, but also marks the function decl "const".  */
25818
25819 static inline tree
25820 def_builtin_const (HOST_WIDE_INT mask, const char *name,
25821                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
25822 {
25823   tree decl = def_builtin (mask, name, tcode, code);
25824   if (decl)
25825     TREE_READONLY (decl) = 1;
25826   else
25827     ix86_builtins_isa[(int) code].const_p = true;
25828
25829   return decl;
25830 }
25831
25832 /* Add any new builtin functions for a given ISA that may not have been
25833    declared.  This saves a bit of space compared to adding all of the
25834    declarations to the tree, even if we didn't use them.  */
25835
25836 static void
25837 ix86_add_new_builtins (HOST_WIDE_INT isa)
25838 {
25839   int i;
25840
25841   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
25842     {
25843       if ((ix86_builtins_isa[i].isa & isa) != 0
25844           && ix86_builtins_isa[i].set_and_not_built_p)
25845         {
25846           tree decl, type;
25847
25848           /* Don't define the builtin again.  */
25849           ix86_builtins_isa[i].set_and_not_built_p = false;
25850
25851           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
25852           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
25853                                                  type, i, BUILT_IN_MD, NULL,
25854                                                  NULL_TREE);
25855
25856           ix86_builtins[i] = decl;
25857           if (ix86_builtins_isa[i].const_p)
25858             TREE_READONLY (decl) = 1;
25859         }
25860     }
25861 }
25862
25863 /* Bits for builtin_description.flag.  */
25864
25865 /* Set when we don't support the comparison natively, and should
25866    swap_comparison in order to support it.  */
25867 #define BUILTIN_DESC_SWAP_OPERANDS      1
25868
25869 struct builtin_description
25870 {
25871   const HOST_WIDE_INT mask;
25872   const enum insn_code icode;
25873   const char *const name;
25874   const enum ix86_builtins code;
25875   const enum rtx_code comparison;
25876   const int flag;
25877 };
25878
25879 static const struct builtin_description bdesc_comi[] =
25880 {
25881   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
25882   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
25883   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
25884   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
25885   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
25886   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
25887   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
25888   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
25889   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
25890   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
25891   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
25892   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
25893   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
25894   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
25895   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
25896   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
25897   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
25898   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
25899   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
25900   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
25901   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
25902   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
25903   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
25904   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
25905 };
25906
25907 static const struct builtin_description bdesc_pcmpestr[] =
25908 {
25909   /* SSE4.2 */
25910   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
25911   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
25912   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
25913   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
25914   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
25915   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
25916   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
25917 };
25918
25919 static const struct builtin_description bdesc_pcmpistr[] =
25920 {
25921   /* SSE4.2 */
25922   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
25923   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
25924   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
25925   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
25926   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
25927   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
25928   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
25929 };
25930
25931 /* Special builtins with variable number of arguments.  */
25932 static const struct builtin_description bdesc_special_args[] =
25933 {
25934   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
25935   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
25936   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_pause, "__builtin_ia32_pause", IX86_BUILTIN_PAUSE, UNKNOWN, (int) VOID_FTYPE_VOID },
25937
25938   /* MMX */
25939   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
25940
25941   /* 3DNow! */
25942   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
25943
25944   /* SSE */
25945   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
25946   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
25947   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
25948
25949   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
25950   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
25951   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
25952   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
25953
25954   /* SSE or 3DNow!A  */
25955   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
25956   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_movntq, "__builtin_ia32_movntq", IX86_BUILTIN_MOVNTQ, UNKNOWN, (int) VOID_FTYPE_PULONGLONG_ULONGLONG },
25957
25958   /* SSE2 */
25959   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
25960   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
25961   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
25962   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
25963   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
25964   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
25965   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntisi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
25966   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_movntidi, "__builtin_ia32_movnti64", IX86_BUILTIN_MOVNTI64, UNKNOWN, (int) VOID_FTYPE_PLONGLONG_LONGLONG },
25967   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
25968   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
25969
25970   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
25971   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
25972
25973   /* SSE3 */
25974   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
25975
25976   /* SSE4.1 */
25977   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
25978
25979   /* SSE4A */
25980   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
25981   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
25982
25983   /* AVX */
25984   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
25985   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
25986
25987   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
25988   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
25989   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
25990   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
25991   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
25992
25993   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
25994   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
25995   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
25996   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
25997   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
25998   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
25999   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
26000
26001   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
26002   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
26003   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
26004
26005   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DI },
26006   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SI },
26007   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DI },
26008   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SI },
26009   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DI_V2DF },
26010   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SI_V4SF },
26011   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DI_V4DF },
26012   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SI_V8SF },
26013
26014   /* AVX2 */
26015   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_movntdqa, "__builtin_ia32_movntdqa256", IX86_BUILTIN_MOVNTDQA256, UNKNOWN, (int) V4DI_FTYPE_PV4DI },
26016   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadd, "__builtin_ia32_maskloadd", IX86_BUILTIN_MASKLOADD, UNKNOWN, (int) V4SI_FTYPE_PCV4SI_V4SI },
26017   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadq, "__builtin_ia32_maskloadq", IX86_BUILTIN_MASKLOADQ, UNKNOWN, (int) V2DI_FTYPE_PCV2DI_V2DI },
26018   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadd256, "__builtin_ia32_maskloadd256", IX86_BUILTIN_MASKLOADD256, UNKNOWN, (int) V8SI_FTYPE_PCV8SI_V8SI },
26019   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadq256, "__builtin_ia32_maskloadq256", IX86_BUILTIN_MASKLOADQ256, UNKNOWN, (int) V4DI_FTYPE_PCV4DI_V4DI },
26020   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstored, "__builtin_ia32_maskstored", IX86_BUILTIN_MASKSTORED, UNKNOWN, (int) VOID_FTYPE_PV4SI_V4SI_V4SI },
26021   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstoreq, "__builtin_ia32_maskstoreq", IX86_BUILTIN_MASKSTOREQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI_V2DI },
26022   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstored256, "__builtin_ia32_maskstored256", IX86_BUILTIN_MASKSTORED256, UNKNOWN, (int) VOID_FTYPE_PV8SI_V8SI_V8SI },
26023   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstoreq256, "__builtin_ia32_maskstoreq256", IX86_BUILTIN_MASKSTOREQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI_V4DI },
26024
26025   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
26026   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
26027   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
26028   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
26029   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
26030   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
26031
26032   /* FSGSBASE */
26033   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasesi, "__builtin_ia32_rdfsbase32", IX86_BUILTIN_RDFSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
26034   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasedi, "__builtin_ia32_rdfsbase64", IX86_BUILTIN_RDFSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
26035   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasesi, "__builtin_ia32_rdgsbase32", IX86_BUILTIN_RDGSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
26036   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasedi, "__builtin_ia32_rdgsbase64", IX86_BUILTIN_RDGSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
26037   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasesi, "__builtin_ia32_wrfsbase32", IX86_BUILTIN_WRFSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
26038   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasedi, "__builtin_ia32_wrfsbase64", IX86_BUILTIN_WRFSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
26039   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasesi, "__builtin_ia32_wrgsbase32", IX86_BUILTIN_WRGSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
26040   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasedi, "__builtin_ia32_wrgsbase64", IX86_BUILTIN_WRGSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
26041 };
26042
26043 /* Builtins with variable number of arguments.  */
26044 static const struct builtin_description bdesc_args[] =
26045 {
26046   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
26047   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
26048   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
26049   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
26050   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
26051   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
26052   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
26053
26054   /* MMX */
26055   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26056   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26057   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26058   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26059   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26060   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26061
26062   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26063   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26064   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26065   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26066   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26067   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26068   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26069   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26070
26071   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26072   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26073
26074   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26075   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26076   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26077   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26078
26079   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26080   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26081   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26082   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26083   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26084   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26085
26086   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26087   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26088   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26089   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26090   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
26091   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
26092
26093   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
26094   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
26095   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
26096
26097   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
26098
26099   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
26100   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
26101   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
26102   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
26103   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
26104   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
26105
26106   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
26107   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
26108   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
26109   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
26110   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
26111   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
26112
26113   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
26114   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
26115   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
26116   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
26117
26118   /* 3DNow! */
26119   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
26120   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
26121   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
26122   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
26123
26124   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26125   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26126   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26127   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
26128   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
26129   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
26130   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26131   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26132   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26133   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26134   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26135   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26136   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26137   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26138   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26139
26140   /* 3DNow!A */
26141   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
26142   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
26143   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
26144   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
26145   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26146   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26147
26148   /* SSE */
26149   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
26150   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26151   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26152   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26153   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26154   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26155   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
26156   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
26157   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
26158   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
26159   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
26160   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
26161
26162   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26163
26164   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26165   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26166   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26167   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26168   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26169   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26170   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26171   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26172
26173   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
26174   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
26175   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
26176   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26177   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26178   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26179   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
26180   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
26181   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
26182   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26183   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
26184   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26185   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
26186   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
26187   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
26188   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26189   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
26190   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
26191   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
26192   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26193   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26194   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26195
26196   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26197   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26198   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26199   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26200
26201   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26202   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26203   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26204   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26205
26206   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26207
26208   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26209   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26210   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26211   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26212   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26213
26214   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
26215   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
26216   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
26217
26218   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
26219
26220   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
26221   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
26222   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
26223
26224   /* SSE MMX or 3Dnow!A */
26225   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26226   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26227   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26228
26229   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26230   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26231   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26232   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26233
26234   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_psadbw, "__builtin_ia32_psadbw", IX86_BUILTIN_PSADBW, UNKNOWN, (int) V1DI_FTYPE_V8QI_V8QI },
26235   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
26236
26237   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pshufw, "__builtin_ia32_pshufw", IX86_BUILTIN_PSHUFW, UNKNOWN, (int) V4HI_FTYPE_V4HI_INT },
26238
26239   /* SSE2 */
26240   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26241
26242   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
26243   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
26244   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
26245   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
26246   { OPTION_MASK_ISA_SSE2, CODE_FOR_floatv4siv4sf2, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
26247
26248   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
26249   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
26250   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
26251   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
26252   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
26253
26254   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
26255
26256   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
26257   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
26258   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
26259   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
26260
26261   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
26262   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
26263   { OPTION_MASK_ISA_SSE2, CODE_FOR_fix_truncv4sfv4si2, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
26264
26265   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26266   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26267   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26268   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26269   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26270   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26271   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26272   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26273
26274   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
26275   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
26276   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
26277   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
26278   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
26279   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26280   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
26281   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
26282   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
26283   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
26284   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
26285   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26286   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
26287   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
26288   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
26289   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26290   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
26291   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
26292   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
26293   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26294
26295   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26296   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26297   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26298   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26299
26300   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26301   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26302   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26303   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26304
26305   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26306
26307   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26308   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26309   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26310
26311   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_pack_sfix_v2df, "__builtin_ia32_vec_pack_sfix", IX86_BUILTIN_VEC_PACK_SFIX, UNKNOWN, (int) V4SI_FTYPE_V2DF_V2DF },
26312
26313   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26314   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26315   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26316   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26317   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26318   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26319   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26320   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26321
26322   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26323   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26324   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26325   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26326   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26327   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26328   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26329   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26330
26331   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26332   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
26333
26334   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26335   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26336   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26337   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26338
26339   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26340   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26341
26342   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26343   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26344   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
26345   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26346   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26347   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
26348
26349   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26350   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26351   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26352   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26353
26354   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26355   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
26356   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
26357   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26358   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26359   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26360   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26361   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26362
26363   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
26364   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
26365   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
26366
26367   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26368   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
26369
26370   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
26371   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
26372
26373   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
26374
26375   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
26376   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
26377   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
26378   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
26379
26380   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
26381   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
26382   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
26383   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
26384   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
26385   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
26386   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
26387
26388   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
26389   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
26390   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
26391   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
26392   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
26393   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
26394   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
26395
26396   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
26397   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
26398   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
26399   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
26400
26401   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
26402   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
26403   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
26404
26405   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
26406
26407   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
26408   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
26409
26410   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
26411
26412   /* SSE2 MMX */
26413   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
26414   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
26415
26416   /* SSE3 */
26417   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
26418   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26419
26420   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26421   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26422   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26423   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26424   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26425   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26426
26427   /* SSSE3 */
26428   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
26429   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
26430   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
26431   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
26432   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
26433   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
26434
26435   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26436   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26437   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26438   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26439   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26440   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26441   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26442   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26443   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26444   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26445   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26446   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26447   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
26448   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
26449   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26450   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26451   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26452   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26453   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26454   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26455   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26456   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26457   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26458   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26459
26460   /* SSSE3.  */
26461   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
26462   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
26463
26464   /* SSE4.1 */
26465   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26466   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26467   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
26468   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
26469   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26470   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26471   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26472   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
26473   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
26474   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
26475
26476   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
26477   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
26478   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
26479   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
26480   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
26481   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
26482   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
26483   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
26484   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
26485   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
26486   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
26487   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
26488   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
26489
26490   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
26491   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26492   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26493   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26494   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26495   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26496   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26497   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26498   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26499   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26500   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
26501   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26502
26503   /* SSE4.1 */
26504   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
26505   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
26506   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26507   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26508
26509   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_floorpd", IX86_BUILTIN_FLOORPD, (enum rtx_code) ROUND_FLOOR, (int) V2DF_FTYPE_V2DF_ROUND },
26510   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_ceilpd", IX86_BUILTIN_CEILPD, (enum rtx_code) ROUND_CEIL, (int) V2DF_FTYPE_V2DF_ROUND },
26511   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_truncpd", IX86_BUILTIN_TRUNCPD, (enum rtx_code) ROUND_TRUNC, (int) V2DF_FTYPE_V2DF_ROUND },
26512   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_rintpd", IX86_BUILTIN_RINTPD, (enum rtx_code) ROUND_MXCSR, (int) V2DF_FTYPE_V2DF_ROUND },
26513
26514   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd_vec_pack_sfix, "__builtin_ia32_floorpd_vec_pack_sfix", IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX, (enum rtx_code) ROUND_FLOOR, (int) V4SI_FTYPE_V2DF_V2DF_ROUND },
26515   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd_vec_pack_sfix, "__builtin_ia32_ceilpd_vec_pack_sfix", IX86_BUILTIN_CEILPD_VEC_PACK_SFIX, (enum rtx_code) ROUND_CEIL, (int) V4SI_FTYPE_V2DF_V2DF_ROUND },
26516
26517   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv2df2, "__builtin_ia32_roundpd_az", IX86_BUILTIN_ROUNDPD_AZ, UNKNOWN, (int) V2DF_FTYPE_V2DF },
26518   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv2df2_vec_pack_sfix, "__builtin_ia32_roundpd_az_vec_pack_sfix", IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX, UNKNOWN, (int) V4SI_FTYPE_V2DF_V2DF },
26519
26520   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_floorps", IX86_BUILTIN_FLOORPS, (enum rtx_code) ROUND_FLOOR, (int) V4SF_FTYPE_V4SF_ROUND },
26521   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_ceilps", IX86_BUILTIN_CEILPS, (enum rtx_code) ROUND_CEIL, (int) V4SF_FTYPE_V4SF_ROUND },
26522   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_truncps", IX86_BUILTIN_TRUNCPS, (enum rtx_code) ROUND_TRUNC, (int) V4SF_FTYPE_V4SF_ROUND },
26523   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_rintps", IX86_BUILTIN_RINTPS, (enum rtx_code) ROUND_MXCSR, (int) V4SF_FTYPE_V4SF_ROUND },
26524
26525   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps_sfix, "__builtin_ia32_floorps_sfix", IX86_BUILTIN_FLOORPS_SFIX, (enum rtx_code) ROUND_FLOOR, (int) V4SI_FTYPE_V4SF_ROUND },
26526   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps_sfix, "__builtin_ia32_ceilps_sfix", IX86_BUILTIN_CEILPS_SFIX, (enum rtx_code) ROUND_CEIL, (int) V4SI_FTYPE_V4SF_ROUND },
26527
26528   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv4sf2, "__builtin_ia32_roundps_az", IX86_BUILTIN_ROUNDPS_AZ, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26529   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv4sf2_sfix, "__builtin_ia32_roundps_az_sfix", IX86_BUILTIN_ROUNDPS_AZ_SFIX, UNKNOWN, (int) V4SI_FTYPE_V4SF },
26530
26531   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
26532   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
26533   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
26534
26535   /* SSE4.2 */
26536   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26537   { OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_CRC32, CODE_FOR_sse4_2_crc32qi, "__builtin_ia32_crc32qi", IX86_BUILTIN_CRC32QI, UNKNOWN, (int) UINT_FTYPE_UINT_UCHAR },
26538   { OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_CRC32, CODE_FOR_sse4_2_crc32hi, "__builtin_ia32_crc32hi", IX86_BUILTIN_CRC32HI, UNKNOWN, (int) UINT_FTYPE_UINT_USHORT },
26539   { OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_CRC32, CODE_FOR_sse4_2_crc32si, "__builtin_ia32_crc32si", IX86_BUILTIN_CRC32SI, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
26540   { OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_CRC32 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse4_2_crc32di, "__builtin_ia32_crc32di", IX86_BUILTIN_CRC32DI, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
26541
26542   /* SSE4A */
26543   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
26544   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
26545   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
26546   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26547
26548   /* AES */
26549   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
26550   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
26551
26552   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26553   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26554   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26555   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26556
26557   /* PCLMUL */
26558   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
26559
26560   /* AVX */
26561   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26562   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26563   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26564   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26565   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26566   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26567   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26568   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26569   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26570   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26571   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26572   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26573   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26574   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26575   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26576   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26577   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26578   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26579   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26580   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26581   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26582   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26583   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26584   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26585   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26586   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26587
26588   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
26589   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
26590   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
26591   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
26592
26593   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26594   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26595   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
26596   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
26597   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26598   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26599   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26600   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vmcmpv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26601   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vmcmpv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26602   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26603   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26604   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26605   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26606   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
26607   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
26608   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
26609   { OPTION_MASK_ISA_AVX, CODE_FOR_floatv4siv4df2, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
26610   { OPTION_MASK_ISA_AVX, CODE_FOR_floatv8siv8sf2, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
26611   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
26612   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
26613   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
26614   { OPTION_MASK_ISA_AVX, CODE_FOR_fix_truncv4dfv4si2, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
26615   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
26616   { OPTION_MASK_ISA_AVX, CODE_FOR_fix_truncv8sfv8si2, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
26617   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26618   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26619   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
26620   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
26621   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
26622   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
26623   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
26624   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
26625   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
26626   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
26627
26628   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26629   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26630   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
26631
26632   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
26633   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26634   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26635   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26636   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26637
26638   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26639
26640   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
26641   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
26642
26643   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_floorpd256", IX86_BUILTIN_FLOORPD256, (enum rtx_code) ROUND_FLOOR, (int) V4DF_FTYPE_V4DF_ROUND },
26644   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_ceilpd256", IX86_BUILTIN_CEILPD256, (enum rtx_code) ROUND_CEIL, (int) V4DF_FTYPE_V4DF_ROUND },
26645   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_truncpd256", IX86_BUILTIN_TRUNCPD256, (enum rtx_code) ROUND_TRUNC, (int) V4DF_FTYPE_V4DF_ROUND },
26646   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_rintpd256", IX86_BUILTIN_RINTPD256, (enum rtx_code) ROUND_MXCSR, (int) V4DF_FTYPE_V4DF_ROUND },
26647
26648   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv4df2, "__builtin_ia32_roundpd_az256", IX86_BUILTIN_ROUNDPD_AZ256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
26649   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv4df2_vec_pack_sfix, "__builtin_ia32_roundpd_az_vec_pack_sfix256", IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX256, UNKNOWN, (int) V8SI_FTYPE_V4DF_V4DF },
26650
26651   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd_vec_pack_sfix256, "__builtin_ia32_floorpd_vec_pack_sfix256", IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX256, (enum rtx_code) ROUND_FLOOR, (int) V8SI_FTYPE_V4DF_V4DF_ROUND },
26652   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd_vec_pack_sfix256, "__builtin_ia32_ceilpd_vec_pack_sfix256", IX86_BUILTIN_CEILPD_VEC_PACK_SFIX256, (enum rtx_code) ROUND_CEIL, (int) V8SI_FTYPE_V4DF_V4DF_ROUND },
26653
26654   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_floorps256", IX86_BUILTIN_FLOORPS256, (enum rtx_code) ROUND_FLOOR, (int) V8SF_FTYPE_V8SF_ROUND },
26655   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_ceilps256", IX86_BUILTIN_CEILPS256, (enum rtx_code) ROUND_CEIL, (int) V8SF_FTYPE_V8SF_ROUND },
26656   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_truncps256", IX86_BUILTIN_TRUNCPS256, (enum rtx_code) ROUND_TRUNC, (int) V8SF_FTYPE_V8SF_ROUND },
26657   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_rintps256", IX86_BUILTIN_RINTPS256, (enum rtx_code) ROUND_MXCSR, (int) V8SF_FTYPE_V8SF_ROUND },
26658
26659   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps_sfix256, "__builtin_ia32_floorps_sfix256", IX86_BUILTIN_FLOORPS_SFIX256, (enum rtx_code) ROUND_FLOOR, (int) V8SI_FTYPE_V8SF_ROUND },
26660   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps_sfix256, "__builtin_ia32_ceilps_sfix256", IX86_BUILTIN_CEILPS_SFIX256, (enum rtx_code) ROUND_CEIL, (int) V8SI_FTYPE_V8SF_ROUND },
26661
26662   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv8sf2, "__builtin_ia32_roundps_az256", IX86_BUILTIN_ROUNDPS_AZ256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26663   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv8sf2_sfix, "__builtin_ia32_roundps_az_sfix256", IX86_BUILTIN_ROUNDPS_AZ_SFIX256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
26664
26665   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26666   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26667   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26668   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26669
26670   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
26671   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
26672   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
26673   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8si, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
26674   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8sf, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
26675   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v4df, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
26676
26677   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
26678   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
26679   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
26680   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
26681   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
26682   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
26683   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
26684   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
26685   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
26686   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
26687   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
26688   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
26689   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
26690   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
26691   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
26692
26693   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
26694   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
26695
26696   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv8sf3,  "__builtin_ia32_copysignps256", IX86_BUILTIN_CPYSGNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26697   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv4df3,  "__builtin_ia32_copysignpd256", IX86_BUILTIN_CPYSGNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26698
26699   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_pack_sfix_v4df, "__builtin_ia32_vec_pack_sfix256 ", IX86_BUILTIN_VEC_PACK_SFIX256, UNKNOWN, (int) V8SI_FTYPE_V4DF_V4DF },
26700
26701   /* AVX2 */
26702   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_mpsadbw, "__builtin_ia32_mpsadbw256", IX86_BUILTIN_MPSADBW256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI_INT },
26703   { OPTION_MASK_ISA_AVX2, CODE_FOR_absv32qi2, "__builtin_ia32_pabsb256", IX86_BUILTIN_PABSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI },
26704   { OPTION_MASK_ISA_AVX2, CODE_FOR_absv16hi2, "__builtin_ia32_pabsw256", IX86_BUILTIN_PABSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI },
26705   { OPTION_MASK_ISA_AVX2, CODE_FOR_absv8si2, "__builtin_ia32_pabsd256", IX86_BUILTIN_PABSD256, UNKNOWN, (int) V8SI_FTYPE_V8SI },
26706   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packssdw, "__builtin_ia32_packssdw256",  IX86_BUILTIN_PACKSSDW256, UNKNOWN, (int) V16HI_FTYPE_V8SI_V8SI },
26707   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packsswb, "__builtin_ia32_packsswb256",  IX86_BUILTIN_PACKSSWB256, UNKNOWN, (int) V32QI_FTYPE_V16HI_V16HI },
26708   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packusdw, "__builtin_ia32_packusdw256",  IX86_BUILTIN_PACKUSDW256, UNKNOWN, (int) V16HI_FTYPE_V8SI_V8SI },
26709   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packuswb, "__builtin_ia32_packuswb256",  IX86_BUILTIN_PACKUSWB256, UNKNOWN, (int) V32QI_FTYPE_V16HI_V16HI },
26710   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv32qi3, "__builtin_ia32_paddb256", IX86_BUILTIN_PADDB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26711   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv16hi3, "__builtin_ia32_paddw256", IX86_BUILTIN_PADDW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26712   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv8si3, "__builtin_ia32_paddd256", IX86_BUILTIN_PADDD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26713   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv4di3, "__builtin_ia32_paddq256", IX86_BUILTIN_PADDQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26714   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ssaddv32qi3, "__builtin_ia32_paddsb256", IX86_BUILTIN_PADDSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26715   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ssaddv16hi3, "__builtin_ia32_paddsw256", IX86_BUILTIN_PADDSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26716   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_usaddv32qi3, "__builtin_ia32_paddusb256", IX86_BUILTIN_PADDUSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26717   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_usaddv16hi3, "__builtin_ia32_paddusw256", IX86_BUILTIN_PADDUSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26718   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_palignrv2ti, "__builtin_ia32_palignr256", IX86_BUILTIN_PALIGNR256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI_INT_CONVERT },
26719   { OPTION_MASK_ISA_AVX2, CODE_FOR_andv4di3, "__builtin_ia32_andsi256", IX86_BUILTIN_AND256I, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26720   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_andnotv4di3, "__builtin_ia32_andnotsi256", IX86_BUILTIN_ANDNOT256I, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26721   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_uavgv32qi3, "__builtin_ia32_pavgb256",  IX86_BUILTIN_PAVGB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26722   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_uavgv16hi3, "__builtin_ia32_pavgw256",  IX86_BUILTIN_PAVGW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26723   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblendvb, "__builtin_ia32_pblendvb256", IX86_BUILTIN_PBLENDVB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI_V32QI },
26724   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblendw, "__builtin_ia32_pblendw256", IX86_BUILTIN_PBLENDVW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI_INT },
26725   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv32qi3, "__builtin_ia32_pcmpeqb256", IX86_BUILTIN_PCMPEQB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26726   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv16hi3, "__builtin_ia32_pcmpeqw256", IX86_BUILTIN_PCMPEQW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26727   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv8si3, "__builtin_ia32_pcmpeqd256", IX86_BUILTIN_PCMPEQD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI  },
26728   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv4di3, "__builtin_ia32_pcmpeqq256", IX86_BUILTIN_PCMPEQQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI  },
26729   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv32qi3, "__builtin_ia32_pcmpgtb256", IX86_BUILTIN_PCMPGTB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26730   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv16hi3, "__builtin_ia32_pcmpgtw256", IX86_BUILTIN_PCMPGTW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26731   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv8si3, "__builtin_ia32_pcmpgtd256", IX86_BUILTIN_PCMPGTD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI  },
26732   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv4di3, "__builtin_ia32_pcmpgtq256", IX86_BUILTIN_PCMPGTQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI  },
26733   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phaddwv16hi3, "__builtin_ia32_phaddw256", IX86_BUILTIN_PHADDW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26734   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phadddv8si3, "__builtin_ia32_phaddd256", IX86_BUILTIN_PHADDD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26735   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phaddswv16hi3, "__builtin_ia32_phaddsw256", IX86_BUILTIN_PHADDSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26736   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phsubwv16hi3, "__builtin_ia32_phsubw256", IX86_BUILTIN_PHSUBW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26737   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phsubdv8si3, "__builtin_ia32_phsubd256", IX86_BUILTIN_PHSUBD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26738   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phsubswv16hi3, "__builtin_ia32_phsubsw256", IX86_BUILTIN_PHSUBSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26739   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pmaddubsw256, "__builtin_ia32_pmaddubsw256", IX86_BUILTIN_PMADDUBSW256, UNKNOWN, (int) V16HI_FTYPE_V32QI_V32QI },
26740   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pmaddwd, "__builtin_ia32_pmaddwd256", IX86_BUILTIN_PMADDWD256, UNKNOWN, (int) V8SI_FTYPE_V16HI_V16HI },
26741   { OPTION_MASK_ISA_AVX2, CODE_FOR_smaxv32qi3, "__builtin_ia32_pmaxsb256", IX86_BUILTIN_PMAXSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26742   { OPTION_MASK_ISA_AVX2, CODE_FOR_smaxv16hi3, "__builtin_ia32_pmaxsw256", IX86_BUILTIN_PMAXSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26743   { OPTION_MASK_ISA_AVX2, CODE_FOR_smaxv8si3 , "__builtin_ia32_pmaxsd256", IX86_BUILTIN_PMAXSD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26744   { OPTION_MASK_ISA_AVX2, CODE_FOR_umaxv32qi3, "__builtin_ia32_pmaxub256", IX86_BUILTIN_PMAXUB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26745   { OPTION_MASK_ISA_AVX2, CODE_FOR_umaxv16hi3, "__builtin_ia32_pmaxuw256", IX86_BUILTIN_PMAXUW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26746   { OPTION_MASK_ISA_AVX2, CODE_FOR_umaxv8si3 , "__builtin_ia32_pmaxud256", IX86_BUILTIN_PMAXUD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26747   { OPTION_MASK_ISA_AVX2, CODE_FOR_sminv32qi3, "__builtin_ia32_pminsb256", IX86_BUILTIN_PMINSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26748   { OPTION_MASK_ISA_AVX2, CODE_FOR_sminv16hi3, "__builtin_ia32_pminsw256", IX86_BUILTIN_PMINSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26749   { OPTION_MASK_ISA_AVX2, CODE_FOR_sminv8si3 , "__builtin_ia32_pminsd256", IX86_BUILTIN_PMINSD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26750   { OPTION_MASK_ISA_AVX2, CODE_FOR_uminv32qi3, "__builtin_ia32_pminub256", IX86_BUILTIN_PMINUB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26751   { OPTION_MASK_ISA_AVX2, CODE_FOR_uminv16hi3, "__builtin_ia32_pminuw256", IX86_BUILTIN_PMINUW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26752   { OPTION_MASK_ISA_AVX2, CODE_FOR_uminv8si3 , "__builtin_ia32_pminud256", IX86_BUILTIN_PMINUD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26753   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pmovmskb, "__builtin_ia32_pmovmskb256", IX86_BUILTIN_PMOVMSKB256, UNKNOWN, (int) INT_FTYPE_V32QI },
26754   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv16qiv16hi2, "__builtin_ia32_pmovsxbw256", IX86_BUILTIN_PMOVSXBW256, UNKNOWN, (int) V16HI_FTYPE_V16QI },
26755   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv8qiv8si2  , "__builtin_ia32_pmovsxbd256", IX86_BUILTIN_PMOVSXBD256, UNKNOWN, (int) V8SI_FTYPE_V16QI },
26756   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv4qiv4di2  , "__builtin_ia32_pmovsxbq256", IX86_BUILTIN_PMOVSXBQ256, UNKNOWN, (int) V4DI_FTYPE_V16QI },
26757   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv8hiv8si2  , "__builtin_ia32_pmovsxwd256", IX86_BUILTIN_PMOVSXWD256, UNKNOWN, (int) V8SI_FTYPE_V8HI },
26758   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv4hiv4di2  , "__builtin_ia32_pmovsxwq256", IX86_BUILTIN_PMOVSXWQ256, UNKNOWN, (int) V4DI_FTYPE_V8HI },
26759   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv4siv4di2  , "__builtin_ia32_pmovsxdq256", IX86_BUILTIN_PMOVSXDQ256, UNKNOWN, (int) V4DI_FTYPE_V4SI },
26760   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv16qiv16hi2, "__builtin_ia32_pmovzxbw256", IX86_BUILTIN_PMOVZXBW256, UNKNOWN, (int) V16HI_FTYPE_V16QI },
26761   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv8qiv8si2  , "__builtin_ia32_pmovzxbd256", IX86_BUILTIN_PMOVZXBD256, UNKNOWN, (int) V8SI_FTYPE_V16QI },
26762   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv4qiv4di2  , "__builtin_ia32_pmovzxbq256", IX86_BUILTIN_PMOVZXBQ256, UNKNOWN, (int) V4DI_FTYPE_V16QI },
26763   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv8hiv8si2  , "__builtin_ia32_pmovzxwd256", IX86_BUILTIN_PMOVZXWD256, UNKNOWN, (int) V8SI_FTYPE_V8HI },
26764   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv4hiv4di2  , "__builtin_ia32_pmovzxwq256", IX86_BUILTIN_PMOVZXWQ256, UNKNOWN, (int) V4DI_FTYPE_V8HI },
26765   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv4siv4di2  , "__builtin_ia32_pmovzxdq256", IX86_BUILTIN_PMOVZXDQ256, UNKNOWN, (int) V4DI_FTYPE_V4SI },
26766   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_mulv4siv4di3  , "__builtin_ia32_pmuldq256"  , IX86_BUILTIN_PMULDQ256  , UNKNOWN, (int) V4DI_FTYPE_V8SI_V8SI },
26767   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_umulhrswv16hi3 , "__builtin_ia32_pmulhrsw256", IX86_BUILTIN_PMULHRSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26768   { OPTION_MASK_ISA_AVX2, CODE_FOR_umulv16hi3_highpart, "__builtin_ia32_pmulhuw256" , IX86_BUILTIN_PMULHUW256 , UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26769   { OPTION_MASK_ISA_AVX2, CODE_FOR_smulv16hi3_highpart, "__builtin_ia32_pmulhw256"  , IX86_BUILTIN_PMULHW256  , UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26770   { OPTION_MASK_ISA_AVX2, CODE_FOR_mulv16hi3, "__builtin_ia32_pmullw256"  , IX86_BUILTIN_PMULLW256  , UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26771   { OPTION_MASK_ISA_AVX2, CODE_FOR_mulv8si3, "__builtin_ia32_pmulld256"  , IX86_BUILTIN_PMULLD256  , UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26772   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_umulv4siv4di3  , "__builtin_ia32_pmuludq256" , IX86_BUILTIN_PMULUDQ256 , UNKNOWN, (int) V4DI_FTYPE_V8SI_V8SI },
26773   { OPTION_MASK_ISA_AVX2, CODE_FOR_iorv4di3, "__builtin_ia32_por256", IX86_BUILTIN_POR256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26774   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psadbw, "__builtin_ia32_psadbw256", IX86_BUILTIN_PSADBW256, UNKNOWN, (int) V16HI_FTYPE_V32QI_V32QI },
26775   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshufbv32qi3, "__builtin_ia32_pshufb256", IX86_BUILTIN_PSHUFB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26776   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshufdv3, "__builtin_ia32_pshufd256", IX86_BUILTIN_PSHUFD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_INT },
26777   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshufhwv3, "__builtin_ia32_pshufhw256", IX86_BUILTIN_PSHUFHW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_INT },
26778   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshuflwv3, "__builtin_ia32_pshuflw256", IX86_BUILTIN_PSHUFLW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_INT },
26779   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psignv32qi3, "__builtin_ia32_psignb256", IX86_BUILTIN_PSIGNB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26780   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psignv16hi3, "__builtin_ia32_psignw256", IX86_BUILTIN_PSIGNW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26781   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psignv8si3 , "__builtin_ia32_psignd256", IX86_BUILTIN_PSIGND256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26782   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlv2ti3, "__builtin_ia32_pslldqi256", IX86_BUILTIN_PSLLDQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_CONVERT },
26783   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv16hi3, "__builtin_ia32_psllwi256", IX86_BUILTIN_PSLLWI256 , UNKNOWN, (int) V16HI_FTYPE_V16HI_SI_COUNT },
26784   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv16hi3, "__builtin_ia32_psllw256", IX86_BUILTIN_PSLLW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V8HI_COUNT },
26785   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv8si3, "__builtin_ia32_pslldi256", IX86_BUILTIN_PSLLDI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_SI_COUNT },
26786   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv8si3, "__builtin_ia32_pslld256", IX86_BUILTIN_PSLLD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_COUNT },
26787   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv4di3, "__builtin_ia32_psllqi256", IX86_BUILTIN_PSLLQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_COUNT },
26788   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv4di3, "__builtin_ia32_psllq256", IX86_BUILTIN_PSLLQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V2DI_COUNT },
26789   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv16hi3, "__builtin_ia32_psrawi256", IX86_BUILTIN_PSRAWI256, UNKNOWN, (int) V16HI_FTYPE_V16HI_SI_COUNT },
26790   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv16hi3, "__builtin_ia32_psraw256", IX86_BUILTIN_PSRAW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V8HI_COUNT },
26791   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv8si3, "__builtin_ia32_psradi256", IX86_BUILTIN_PSRADI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_SI_COUNT },
26792   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv8si3, "__builtin_ia32_psrad256", IX86_BUILTIN_PSRAD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_COUNT },
26793   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrv2ti3, "__builtin_ia32_psrldqi256", IX86_BUILTIN_PSRLDQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_CONVERT },
26794   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv16hi3, "__builtin_ia32_psrlwi256", IX86_BUILTIN_PSRLWI256 , UNKNOWN, (int) V16HI_FTYPE_V16HI_SI_COUNT },
26795   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv16hi3, "__builtin_ia32_psrlw256", IX86_BUILTIN_PSRLW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V8HI_COUNT },
26796   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv8si3, "__builtin_ia32_psrldi256", IX86_BUILTIN_PSRLDI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_SI_COUNT },
26797   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv8si3, "__builtin_ia32_psrld256", IX86_BUILTIN_PSRLD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_COUNT },
26798   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv4di3, "__builtin_ia32_psrlqi256", IX86_BUILTIN_PSRLQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_COUNT },
26799   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv4di3, "__builtin_ia32_psrlq256", IX86_BUILTIN_PSRLQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V2DI_COUNT },
26800   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv32qi3, "__builtin_ia32_psubb256", IX86_BUILTIN_PSUBB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26801   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv16hi3, "__builtin_ia32_psubw256", IX86_BUILTIN_PSUBW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26802   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv8si3, "__builtin_ia32_psubd256", IX86_BUILTIN_PSUBD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26803   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv4di3, "__builtin_ia32_psubq256", IX86_BUILTIN_PSUBQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26804   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sssubv32qi3, "__builtin_ia32_psubsb256", IX86_BUILTIN_PSUBSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26805   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sssubv16hi3, "__builtin_ia32_psubsw256", IX86_BUILTIN_PSUBSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26806   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ussubv32qi3, "__builtin_ia32_psubusb256", IX86_BUILTIN_PSUBUSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26807   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ussubv16hi3, "__builtin_ia32_psubusw256", IX86_BUILTIN_PSUBUSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26808   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv32qi, "__builtin_ia32_punpckhbw256", IX86_BUILTIN_PUNPCKHBW256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26809   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv16hi, "__builtin_ia32_punpckhwd256", IX86_BUILTIN_PUNPCKHWD256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI  },
26810   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv8si, "__builtin_ia32_punpckhdq256", IX86_BUILTIN_PUNPCKHDQ256, UNKNOWN,  (int) V8SI_FTYPE_V8SI_V8SI },
26811   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv4di, "__builtin_ia32_punpckhqdq256", IX86_BUILTIN_PUNPCKHQDQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26812   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv32qi, "__builtin_ia32_punpcklbw256", IX86_BUILTIN_PUNPCKLBW256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26813   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv16hi, "__builtin_ia32_punpcklwd256", IX86_BUILTIN_PUNPCKLWD256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26814   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv8si, "__builtin_ia32_punpckldq256", IX86_BUILTIN_PUNPCKLDQ256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26815   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv4di, "__builtin_ia32_punpcklqdq256", IX86_BUILTIN_PUNPCKLQDQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26816   { OPTION_MASK_ISA_AVX2, CODE_FOR_xorv4di3, "__builtin_ia32_pxor256", IX86_BUILTIN_PXOR256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26817   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vec_dupv4sf, "__builtin_ia32_vbroadcastss_ps", IX86_BUILTIN_VBROADCASTSS_PS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26818   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vec_dupv8sf, "__builtin_ia32_vbroadcastss_ps256", IX86_BUILTIN_VBROADCASTSS_PS256, UNKNOWN, (int) V8SF_FTYPE_V4SF },
26819   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vec_dupv4df, "__builtin_ia32_vbroadcastsd_pd256", IX86_BUILTIN_VBROADCASTSD_PD256, UNKNOWN, (int) V4DF_FTYPE_V2DF },
26820   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vbroadcasti128_v4di, "__builtin_ia32_vbroadcastsi256", IX86_BUILTIN_VBROADCASTSI256, UNKNOWN, (int) V4DI_FTYPE_V2DI },
26821   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblenddv4si, "__builtin_ia32_pblendd128", IX86_BUILTIN_PBLENDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_INT },
26822   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblenddv8si, "__builtin_ia32_pblendd256", IX86_BUILTIN_PBLENDD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
26823   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv32qi, "__builtin_ia32_pbroadcastb256", IX86_BUILTIN_PBROADCASTB256, UNKNOWN, (int) V32QI_FTYPE_V16QI },
26824   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv16hi, "__builtin_ia32_pbroadcastw256", IX86_BUILTIN_PBROADCASTW256, UNKNOWN, (int) V16HI_FTYPE_V8HI },
26825   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv8si, "__builtin_ia32_pbroadcastd256", IX86_BUILTIN_PBROADCASTD256, UNKNOWN, (int) V8SI_FTYPE_V4SI },
26826   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv4di, "__builtin_ia32_pbroadcastq256", IX86_BUILTIN_PBROADCASTQ256, UNKNOWN, (int) V4DI_FTYPE_V2DI },
26827   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv16qi, "__builtin_ia32_pbroadcastb128", IX86_BUILTIN_PBROADCASTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
26828   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv8hi, "__builtin_ia32_pbroadcastw128", IX86_BUILTIN_PBROADCASTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
26829   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv4si, "__builtin_ia32_pbroadcastd128", IX86_BUILTIN_PBROADCASTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
26830   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv2di, "__builtin_ia32_pbroadcastq128", IX86_BUILTIN_PBROADCASTQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
26831   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permvarv8si, "__builtin_ia32_permvarsi256", IX86_BUILTIN_VPERMVARSI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26832   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permv4df, "__builtin_ia32_permdf256", IX86_BUILTIN_VPERMDF256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
26833   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permvarv8sf, "__builtin_ia32_permvarsf256", IX86_BUILTIN_VPERMVARSF256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
26834   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permv4di, "__builtin_ia32_permdi256", IX86_BUILTIN_VPERMDI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT },
26835   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permv2ti, "__builtin_ia32_permti256", IX86_BUILTIN_VPERMTI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI_INT },
26836   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_extracti128, "__builtin_ia32_extract128i256", IX86_BUILTIN_VEXTRACT128I256, UNKNOWN, (int) V2DI_FTYPE_V4DI_INT },
26837   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_inserti128, "__builtin_ia32_insert128i256", IX86_BUILTIN_VINSERT128I256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V2DI_INT },
26838   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv4di, "__builtin_ia32_psllv4di", IX86_BUILTIN_PSLLVV4DI, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26839   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv2di, "__builtin_ia32_psllv2di", IX86_BUILTIN_PSLLVV2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26840   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv8si, "__builtin_ia32_psllv8si", IX86_BUILTIN_PSLLVV8SI, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26841   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv4si, "__builtin_ia32_psllv4si", IX86_BUILTIN_PSLLVV4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26842   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashrvv8si, "__builtin_ia32_psrav8si", IX86_BUILTIN_PSRAVV8SI, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26843   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashrvv4si, "__builtin_ia32_psrav4si", IX86_BUILTIN_PSRAVV4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26844   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv4di, "__builtin_ia32_psrlv4di", IX86_BUILTIN_PSRLVV4DI, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26845   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv2di, "__builtin_ia32_psrlv2di", IX86_BUILTIN_PSRLVV2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26846   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv8si, "__builtin_ia32_psrlv8si", IX86_BUILTIN_PSRLVV8SI, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26847   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv4si, "__builtin_ia32_psrlv4si", IX86_BUILTIN_PSRLVV4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26848
26849   { OPTION_MASK_ISA_LZCNT, CODE_FOR_clzhi2_lzcnt,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
26850
26851   /* BMI */
26852   { OPTION_MASK_ISA_BMI, CODE_FOR_bmi_bextr_si, "__builtin_ia32_bextr_u32", IX86_BUILTIN_BEXTR32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
26853   { OPTION_MASK_ISA_BMI, CODE_FOR_bmi_bextr_di, "__builtin_ia32_bextr_u64", IX86_BUILTIN_BEXTR64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
26854   { OPTION_MASK_ISA_BMI, CODE_FOR_ctzhi2,       "__builtin_ctzs",           IX86_BUILTIN_CTZS,    UNKNOWN, (int) UINT16_FTYPE_UINT16 },
26855
26856   /* TBM */
26857   { OPTION_MASK_ISA_TBM, CODE_FOR_tbm_bextri_si, "__builtin_ia32_bextri_u32", IX86_BUILTIN_BEXTRI32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
26858   { OPTION_MASK_ISA_TBM, CODE_FOR_tbm_bextri_di, "__builtin_ia32_bextri_u64", IX86_BUILTIN_BEXTRI64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
26859
26860   /* F16C */
26861   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps, "__builtin_ia32_vcvtph2ps", IX86_BUILTIN_CVTPH2PS, UNKNOWN, (int) V4SF_FTYPE_V8HI },
26862   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps256, "__builtin_ia32_vcvtph2ps256", IX86_BUILTIN_CVTPH2PS256, UNKNOWN, (int) V8SF_FTYPE_V8HI },
26863   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph, "__builtin_ia32_vcvtps2ph", IX86_BUILTIN_CVTPS2PH, UNKNOWN, (int) V8HI_FTYPE_V4SF_INT },
26864   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph256, "__builtin_ia32_vcvtps2ph256", IX86_BUILTIN_CVTPS2PH256, UNKNOWN, (int) V8HI_FTYPE_V8SF_INT },
26865
26866   /* BMI2 */
26867   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_bzhi_si3, "__builtin_ia32_bzhi_si", IX86_BUILTIN_BZHI32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
26868   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_bzhi_di3, "__builtin_ia32_bzhi_di", IX86_BUILTIN_BZHI64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
26869   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pdep_si3, "__builtin_ia32_pdep_si", IX86_BUILTIN_PDEP32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
26870   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pdep_di3, "__builtin_ia32_pdep_di", IX86_BUILTIN_PDEP64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
26871   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pext_si3, "__builtin_ia32_pext_si", IX86_BUILTIN_PEXT32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
26872   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pext_di3, "__builtin_ia32_pext_di", IX86_BUILTIN_PEXT64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
26873 };
26874
26875 /* FMA4 and XOP.  */
26876 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
26877 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
26878 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
26879 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
26880 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
26881 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
26882 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
26883 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
26884 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
26885 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
26886 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
26887 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
26888 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
26889 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
26890 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
26891 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
26892 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
26893 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
26894 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
26895 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
26896 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
26897 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
26898 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
26899 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
26900 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
26901 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
26902 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
26903 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
26904 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
26905 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
26906 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
26907 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
26908 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
26909 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
26910 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
26911 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
26912 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
26913 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
26914 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
26915 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
26916 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
26917 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
26918 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
26919 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
26920 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
26921 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
26922 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
26923 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
26924 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
26925 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
26926 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
26927 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
26928
26929 static const struct builtin_description bdesc_multi_arg[] =
26930 {
26931   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v4sf,
26932     "__builtin_ia32_vfmaddss", IX86_BUILTIN_VFMADDSS,
26933     UNKNOWN, (int)MULTI_ARG_3_SF },
26934   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v2df,
26935     "__builtin_ia32_vfmaddsd", IX86_BUILTIN_VFMADDSD,
26936     UNKNOWN, (int)MULTI_ARG_3_DF },
26937
26938   { OPTION_MASK_ISA_FMA, CODE_FOR_fmai_vmfmadd_v4sf,
26939     "__builtin_ia32_vfmaddss3", IX86_BUILTIN_VFMADDSS3,
26940     UNKNOWN, (int)MULTI_ARG_3_SF },
26941   { OPTION_MASK_ISA_FMA, CODE_FOR_fmai_vmfmadd_v2df,
26942     "__builtin_ia32_vfmaddsd3", IX86_BUILTIN_VFMADDSD3,
26943     UNKNOWN, (int)MULTI_ARG_3_DF },
26944
26945   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4sf,
26946     "__builtin_ia32_vfmaddps", IX86_BUILTIN_VFMADDPS,
26947     UNKNOWN, (int)MULTI_ARG_3_SF },
26948   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v2df,
26949     "__builtin_ia32_vfmaddpd", IX86_BUILTIN_VFMADDPD,
26950     UNKNOWN, (int)MULTI_ARG_3_DF },
26951   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v8sf,
26952     "__builtin_ia32_vfmaddps256", IX86_BUILTIN_VFMADDPS256,
26953     UNKNOWN, (int)MULTI_ARG_3_SF2 },
26954   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4df,
26955     "__builtin_ia32_vfmaddpd256", IX86_BUILTIN_VFMADDPD256,
26956     UNKNOWN, (int)MULTI_ARG_3_DF2 },
26957
26958   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4sf,
26959     "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,
26960     UNKNOWN, (int)MULTI_ARG_3_SF },
26961   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v2df,
26962     "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,
26963     UNKNOWN, (int)MULTI_ARG_3_DF },
26964   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v8sf,
26965     "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,
26966     UNKNOWN, (int)MULTI_ARG_3_SF2 },
26967   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4df,
26968     "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,
26969     UNKNOWN, (int)MULTI_ARG_3_DF2 },
26970
26971   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
26972   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
26973   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
26974   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
26975   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
26976   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
26977   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
26978
26979   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
26980   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
26981   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
26982   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
26983   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
26984   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
26985   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
26986
26987   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
26988
26989   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
26990   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
26991   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
26992   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
26993   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
26994   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
26995   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
26996   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
26997   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
26998   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
26999   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
27000   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
27001
27002   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
27003   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
27004   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
27005   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
27006   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
27007   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
27008   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
27009   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
27010   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
27011   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
27012   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
27013   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
27014   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
27015   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
27016   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
27017   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
27018
27019   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_2_SF },
27020   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_2_DF },
27021   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
27022   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
27023   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
27024   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
27025
27026   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
27027   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
27028   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
27029   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
27030   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
27031   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
27032   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
27033   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
27034   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
27035   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
27036   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
27037   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
27038   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
27039   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
27040   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
27041
27042   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
27043   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
27044   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
27045   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
27046   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
27047   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
27048   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
27049
27050   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
27051   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
27052   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
27053   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
27054   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
27055   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
27056   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
27057
27058   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
27059   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
27060   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
27061   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
27062   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
27063   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
27064   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
27065
27066   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
27067   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
27068   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
27069   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
27070   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
27071   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
27072   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
27073
27074   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
27075   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
27076   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
27077   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
27078   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
27079   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
27080   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
27081
27082   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
27083   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
27084   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
27085   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
27086   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
27087   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
27088   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
27089
27090   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
27091   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
27092   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
27093   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
27094   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
27095   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
27096   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
27097
27098   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
27099   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
27100   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
27101   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
27102   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
27103   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
27104   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
27105
27106   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv16qi3,     "__builtin_ia32_vpcomfalseb", IX86_BUILTIN_VPCOMFALSEB, (enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_QI_TF },
27107   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv8hi3,      "__builtin_ia32_vpcomfalsew", IX86_BUILTIN_VPCOMFALSEW, (enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_HI_TF },
27108   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv4si3,      "__builtin_ia32_vpcomfalsed", IX86_BUILTIN_VPCOMFALSED, (enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_SI_TF },
27109   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv2di3,      "__builtin_ia32_vpcomfalseq", IX86_BUILTIN_VPCOMFALSEQ, (enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_DI_TF },
27110   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv16qi3,     "__builtin_ia32_vpcomfalseub",IX86_BUILTIN_VPCOMFALSEUB,(enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_QI_TF },
27111   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv8hi3,      "__builtin_ia32_vpcomfalseuw",IX86_BUILTIN_VPCOMFALSEUW,(enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_HI_TF },
27112   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv4si3,      "__builtin_ia32_vpcomfalseud",IX86_BUILTIN_VPCOMFALSEUD,(enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_SI_TF },
27113   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv2di3,      "__builtin_ia32_vpcomfalseuq",IX86_BUILTIN_VPCOMFALSEUQ,(enum rtx_code) PCOM_FALSE,   (int)MULTI_ARG_2_DI_TF },
27114
27115   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv16qi3,     "__builtin_ia32_vpcomtrueb",  IX86_BUILTIN_VPCOMTRUEB,  (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_QI_TF },
27116   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv8hi3,      "__builtin_ia32_vpcomtruew",  IX86_BUILTIN_VPCOMTRUEW,  (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_HI_TF },
27117   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv4si3,      "__builtin_ia32_vpcomtrued",  IX86_BUILTIN_VPCOMTRUED,  (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_SI_TF },
27118   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv2di3,      "__builtin_ia32_vpcomtrueq",  IX86_BUILTIN_VPCOMTRUEQ,  (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_DI_TF },
27119   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv16qi3,     "__builtin_ia32_vpcomtrueub", IX86_BUILTIN_VPCOMTRUEUB, (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_QI_TF },
27120   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv8hi3,      "__builtin_ia32_vpcomtrueuw", IX86_BUILTIN_VPCOMTRUEUW, (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_HI_TF },
27121   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv4si3,      "__builtin_ia32_vpcomtrueud", IX86_BUILTIN_VPCOMTRUEUD, (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_SI_TF },
27122   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcom_tfv2di3,      "__builtin_ia32_vpcomtrueuq", IX86_BUILTIN_VPCOMTRUEUQ, (enum rtx_code) PCOM_TRUE,    (int)MULTI_ARG_2_DI_TF },
27123
27124   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
27125   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
27126   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
27127   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
27128
27129 };
27130 \f
27131 /* TM vector builtins.  */
27132
27133 /* Reuse the existing x86-specific `struct builtin_description' cause
27134    we're lazy.  Add casts to make them fit.  */
27135 static const struct builtin_description bdesc_tm[] =
27136 {
27137   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_WM64", (enum ix86_builtins) BUILT_IN_TM_STORE_M64, UNKNOWN, VOID_FTYPE_PV2SI_V2SI },
27138   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_WaRM64", (enum ix86_builtins) BUILT_IN_TM_STORE_WAR_M64, UNKNOWN, VOID_FTYPE_PV2SI_V2SI },
27139   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_WaWM64", (enum ix86_builtins) BUILT_IN_TM_STORE_WAW_M64, UNKNOWN, VOID_FTYPE_PV2SI_V2SI },
27140   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27141   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RaRM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAR_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27142   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RaWM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAW_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27143   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RfWM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_RFW_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27144
27145   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_WM128", (enum ix86_builtins) BUILT_IN_TM_STORE_M128, UNKNOWN, VOID_FTYPE_PV4SF_V4SF },
27146   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_WaRM128", (enum ix86_builtins) BUILT_IN_TM_STORE_WAR_M128, UNKNOWN, VOID_FTYPE_PV4SF_V4SF },
27147   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_WaWM128", (enum ix86_builtins) BUILT_IN_TM_STORE_WAW_M128, UNKNOWN, VOID_FTYPE_PV4SF_V4SF },
27148   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27149   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RaRM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAR_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27150   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RaWM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAW_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27151   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RfWM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_RFW_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27152
27153   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_WM256", (enum ix86_builtins) BUILT_IN_TM_STORE_M256, UNKNOWN, VOID_FTYPE_PV8SF_V8SF },
27154   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_WaRM256", (enum ix86_builtins) BUILT_IN_TM_STORE_WAR_M256, UNKNOWN, VOID_FTYPE_PV8SF_V8SF },
27155   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_WaWM256", (enum ix86_builtins) BUILT_IN_TM_STORE_WAW_M256, UNKNOWN, VOID_FTYPE_PV8SF_V8SF },
27156   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27157   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RaRM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAR_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27158   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RaWM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAW_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27159   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RfWM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_RFW_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27160
27161   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_LM64", (enum ix86_builtins) BUILT_IN_TM_LOG_M64, UNKNOWN, VOID_FTYPE_PCVOID },
27162   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_LM128", (enum ix86_builtins) BUILT_IN_TM_LOG_M128, UNKNOWN, VOID_FTYPE_PCVOID },
27163   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_LM256", (enum ix86_builtins) BUILT_IN_TM_LOG_M256, UNKNOWN, VOID_FTYPE_PCVOID },
27164 };
27165
27166 /* TM callbacks.  */
27167
27168 /* Return the builtin decl needed to load a vector of TYPE.  */
27169
27170 static tree
27171 ix86_builtin_tm_load (tree type)
27172 {
27173   if (TREE_CODE (type) == VECTOR_TYPE)
27174     {
27175       switch (tree_low_cst (TYPE_SIZE (type), 1))
27176         {
27177         case 64:
27178           return builtin_decl_explicit (BUILT_IN_TM_LOAD_M64);
27179         case 128:
27180           return builtin_decl_explicit (BUILT_IN_TM_LOAD_M128);
27181         case 256:
27182           return builtin_decl_explicit (BUILT_IN_TM_LOAD_M256);
27183         }
27184     }
27185   return NULL_TREE;
27186 }
27187
27188 /* Return the builtin decl needed to store a vector of TYPE.  */
27189
27190 static tree
27191 ix86_builtin_tm_store (tree type)
27192 {
27193   if (TREE_CODE (type) == VECTOR_TYPE)
27194     {
27195       switch (tree_low_cst (TYPE_SIZE (type), 1))
27196         {
27197         case 64:
27198           return builtin_decl_explicit (BUILT_IN_TM_STORE_M64);
27199         case 128:
27200           return builtin_decl_explicit (BUILT_IN_TM_STORE_M128);
27201         case 256:
27202           return builtin_decl_explicit (BUILT_IN_TM_STORE_M256);
27203         }
27204     }
27205   return NULL_TREE;
27206 }
27207 \f
27208 /* Initialize the transactional memory vector load/store builtins.  */
27209
27210 static void
27211 ix86_init_tm_builtins (void)
27212 {
27213   enum ix86_builtin_func_type ftype;
27214   const struct builtin_description *d;
27215   size_t i;
27216   tree decl;
27217   tree attrs_load, attrs_type_load, attrs_store, attrs_type_store;
27218   tree attrs_log, attrs_type_log;
27219
27220   if (!flag_tm)
27221     return;
27222
27223   /* If there are no builtins defined, we must be compiling in a
27224      language without trans-mem support.  */
27225   if (!builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
27226     return;
27227
27228   /* Use whatever attributes a normal TM load has.  */
27229   decl = builtin_decl_explicit (BUILT_IN_TM_LOAD_1);
27230   attrs_load = DECL_ATTRIBUTES (decl);
27231   attrs_type_load = TYPE_ATTRIBUTES (TREE_TYPE (decl));
27232   /* Use whatever attributes a normal TM store has.  */
27233   decl = builtin_decl_explicit (BUILT_IN_TM_STORE_1);
27234   attrs_store = DECL_ATTRIBUTES (decl);
27235   attrs_type_store = TYPE_ATTRIBUTES (TREE_TYPE (decl));
27236   /* Use whatever attributes a normal TM log has.  */
27237   decl = builtin_decl_explicit (BUILT_IN_TM_LOG);
27238   attrs_log = DECL_ATTRIBUTES (decl);
27239   attrs_type_log = TYPE_ATTRIBUTES (TREE_TYPE (decl));
27240
27241   for (i = 0, d = bdesc_tm;
27242        i < ARRAY_SIZE (bdesc_tm);
27243        i++, d++)
27244     {
27245       if ((d->mask & ix86_isa_flags) != 0
27246           || (lang_hooks.builtin_function
27247               == lang_hooks.builtin_function_ext_scope))
27248         {
27249           tree type, attrs, attrs_type;
27250           enum built_in_function code = (enum built_in_function) d->code;
27251
27252           ftype = (enum ix86_builtin_func_type) d->flag;
27253           type = ix86_get_builtin_func_type (ftype);
27254
27255           if (BUILTIN_TM_LOAD_P (code))
27256             {
27257               attrs = attrs_load;
27258               attrs_type = attrs_type_load;
27259             }
27260           else if (BUILTIN_TM_STORE_P (code))
27261             {
27262               attrs = attrs_store;
27263               attrs_type = attrs_type_store;
27264             }
27265           else
27266             {
27267               attrs = attrs_log;
27268               attrs_type = attrs_type_log;
27269             }
27270           decl = add_builtin_function (d->name, type, code, BUILT_IN_NORMAL,
27271                                        /* The builtin without the prefix for
27272                                           calling it directly.  */
27273                                        d->name + strlen ("__builtin_"),
27274                                        attrs);
27275           /* add_builtin_function() will set the DECL_ATTRIBUTES, now
27276              set the TYPE_ATTRIBUTES.  */
27277           decl_attributes (&TREE_TYPE (decl), attrs_type, ATTR_FLAG_BUILT_IN);
27278
27279           set_builtin_decl (code, decl, false);
27280         }
27281     }
27282 }
27283
27284 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
27285    in the current target ISA to allow the user to compile particular modules
27286    with different target specific options that differ from the command line
27287    options.  */
27288 static void
27289 ix86_init_mmx_sse_builtins (void)
27290 {
27291   const struct builtin_description * d;
27292   enum ix86_builtin_func_type ftype;
27293   size_t i;
27294
27295   /* Add all special builtins with variable number of operands.  */
27296   for (i = 0, d = bdesc_special_args;
27297        i < ARRAY_SIZE (bdesc_special_args);
27298        i++, d++)
27299     {
27300       if (d->name == 0)
27301         continue;
27302
27303       ftype = (enum ix86_builtin_func_type) d->flag;
27304       def_builtin (d->mask, d->name, ftype, d->code);
27305     }
27306
27307   /* Add all builtins with variable number of operands.  */
27308   for (i = 0, d = bdesc_args;
27309        i < ARRAY_SIZE (bdesc_args);
27310        i++, d++)
27311     {
27312       if (d->name == 0)
27313         continue;
27314
27315       ftype = (enum ix86_builtin_func_type) d->flag;
27316       def_builtin_const (d->mask, d->name, ftype, d->code);
27317     }
27318
27319   /* pcmpestr[im] insns.  */
27320   for (i = 0, d = bdesc_pcmpestr;
27321        i < ARRAY_SIZE (bdesc_pcmpestr);
27322        i++, d++)
27323     {
27324       if (d->code == IX86_BUILTIN_PCMPESTRM128)
27325         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
27326       else
27327         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
27328       def_builtin_const (d->mask, d->name, ftype, d->code);
27329     }
27330
27331   /* pcmpistr[im] insns.  */
27332   for (i = 0, d = bdesc_pcmpistr;
27333        i < ARRAY_SIZE (bdesc_pcmpistr);
27334        i++, d++)
27335     {
27336       if (d->code == IX86_BUILTIN_PCMPISTRM128)
27337         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
27338       else
27339         ftype = INT_FTYPE_V16QI_V16QI_INT;
27340       def_builtin_const (d->mask, d->name, ftype, d->code);
27341     }
27342
27343   /* comi/ucomi insns.  */
27344   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
27345     {
27346       if (d->mask == OPTION_MASK_ISA_SSE2)
27347         ftype = INT_FTYPE_V2DF_V2DF;
27348       else
27349         ftype = INT_FTYPE_V4SF_V4SF;
27350       def_builtin_const (d->mask, d->name, ftype, d->code);
27351     }
27352
27353   /* SSE */
27354   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
27355                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
27356   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
27357                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
27358
27359   /* SSE or 3DNow!A */
27360   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
27361                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
27362                IX86_BUILTIN_MASKMOVQ);
27363
27364   /* SSE2 */
27365   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
27366                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
27367
27368   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
27369                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
27370   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
27371                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
27372
27373   /* SSE3.  */
27374   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
27375                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
27376   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
27377                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
27378
27379   /* AES */
27380   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
27381                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
27382   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
27383                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
27384   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
27385                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
27386   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
27387                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
27388   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
27389                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
27390   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
27391                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
27392
27393   /* PCLMUL */
27394   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
27395                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
27396
27397   /* RDRND */
27398   def_builtin (OPTION_MASK_ISA_RDRND, "__builtin_ia32_rdrand16_step",
27399                INT_FTYPE_PUSHORT, IX86_BUILTIN_RDRAND16_STEP);
27400   def_builtin (OPTION_MASK_ISA_RDRND, "__builtin_ia32_rdrand32_step",
27401                INT_FTYPE_PUNSIGNED, IX86_BUILTIN_RDRAND32_STEP);
27402   def_builtin (OPTION_MASK_ISA_RDRND | OPTION_MASK_ISA_64BIT,
27403                "__builtin_ia32_rdrand64_step", INT_FTYPE_PULONGLONG,
27404                IX86_BUILTIN_RDRAND64_STEP);
27405
27406   /* AVX2 */
27407   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv2df",
27408                V2DF_FTYPE_V2DF_PCDOUBLE_V4SI_V2DF_INT,
27409                IX86_BUILTIN_GATHERSIV2DF);
27410
27411   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4df",
27412                V4DF_FTYPE_V4DF_PCDOUBLE_V4SI_V4DF_INT,
27413                IX86_BUILTIN_GATHERSIV4DF);
27414
27415   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv2df",
27416                V2DF_FTYPE_V2DF_PCDOUBLE_V2DI_V2DF_INT,
27417                IX86_BUILTIN_GATHERDIV2DF);
27418
27419   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4df",
27420                V4DF_FTYPE_V4DF_PCDOUBLE_V4DI_V4DF_INT,
27421                IX86_BUILTIN_GATHERDIV4DF);
27422
27423   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4sf",
27424                V4SF_FTYPE_V4SF_PCFLOAT_V4SI_V4SF_INT,
27425                IX86_BUILTIN_GATHERSIV4SF);
27426
27427   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv8sf",
27428                V8SF_FTYPE_V8SF_PCFLOAT_V8SI_V8SF_INT,
27429                IX86_BUILTIN_GATHERSIV8SF);
27430
27431   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4sf",
27432                V4SF_FTYPE_V4SF_PCFLOAT_V2DI_V4SF_INT,
27433                IX86_BUILTIN_GATHERDIV4SF);
27434
27435   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4sf256",
27436                V4SF_FTYPE_V4SF_PCFLOAT_V4DI_V4SF_INT,
27437                IX86_BUILTIN_GATHERDIV8SF);
27438
27439   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv2di",
27440                V2DI_FTYPE_V2DI_PCINT64_V4SI_V2DI_INT,
27441                IX86_BUILTIN_GATHERSIV2DI);
27442
27443   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4di",
27444                V4DI_FTYPE_V4DI_PCINT64_V4SI_V4DI_INT,
27445                IX86_BUILTIN_GATHERSIV4DI);
27446
27447   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv2di",
27448                V2DI_FTYPE_V2DI_PCINT64_V2DI_V2DI_INT,
27449                IX86_BUILTIN_GATHERDIV2DI);
27450
27451   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4di",
27452                V4DI_FTYPE_V4DI_PCINT64_V4DI_V4DI_INT,
27453                IX86_BUILTIN_GATHERDIV4DI);
27454
27455   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4si",
27456                V4SI_FTYPE_V4SI_PCINT_V4SI_V4SI_INT,
27457                IX86_BUILTIN_GATHERSIV4SI);
27458
27459   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv8si",
27460                V8SI_FTYPE_V8SI_PCINT_V8SI_V8SI_INT,
27461                IX86_BUILTIN_GATHERSIV8SI);
27462
27463   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4si",
27464                V4SI_FTYPE_V4SI_PCINT_V2DI_V4SI_INT,
27465                IX86_BUILTIN_GATHERDIV4SI);
27466
27467   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4si256",
27468                V4SI_FTYPE_V4SI_PCINT_V4DI_V4SI_INT,
27469                IX86_BUILTIN_GATHERDIV8SI);
27470
27471   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltsiv4df ",
27472                V4DF_FTYPE_V4DF_PCDOUBLE_V8SI_V4DF_INT,
27473                IX86_BUILTIN_GATHERALTSIV4DF);
27474
27475   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltdiv4sf256 ",
27476                V8SF_FTYPE_V8SF_PCFLOAT_V4DI_V8SF_INT,
27477                IX86_BUILTIN_GATHERALTDIV8SF);
27478
27479   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltsiv4di ",
27480                V4DI_FTYPE_V4DI_PCINT64_V8SI_V4DI_INT,
27481                IX86_BUILTIN_GATHERALTSIV4DI);
27482
27483   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltdiv4si256 ",
27484                V8SI_FTYPE_V8SI_PCINT_V4DI_V8SI_INT,
27485                IX86_BUILTIN_GATHERALTDIV8SI);
27486
27487   /* MMX access to the vec_init patterns.  */
27488   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
27489                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
27490
27491   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
27492                      V4HI_FTYPE_HI_HI_HI_HI,
27493                      IX86_BUILTIN_VEC_INIT_V4HI);
27494
27495   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
27496                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
27497                      IX86_BUILTIN_VEC_INIT_V8QI);
27498
27499   /* Access to the vec_extract patterns.  */
27500   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
27501                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
27502   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
27503                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
27504   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
27505                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
27506   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
27507                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
27508   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
27509                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
27510
27511   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
27512                      "__builtin_ia32_vec_ext_v4hi",
27513                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
27514
27515   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
27516                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
27517
27518   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
27519                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
27520
27521   /* Access to the vec_set patterns.  */
27522   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
27523                      "__builtin_ia32_vec_set_v2di",
27524                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
27525
27526   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
27527                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
27528
27529   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
27530                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
27531
27532   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
27533                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
27534
27535   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
27536                      "__builtin_ia32_vec_set_v4hi",
27537                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
27538
27539   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
27540                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
27541
27542   /* Add FMA4 multi-arg argument instructions */
27543   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
27544     {
27545       if (d->name == 0)
27546         continue;
27547
27548       ftype = (enum ix86_builtin_func_type) d->flag;
27549       def_builtin_const (d->mask, d->name, ftype, d->code);
27550     }
27551 }
27552
27553 /* Internal method for ix86_init_builtins.  */
27554
27555 static void
27556 ix86_init_builtins_va_builtins_abi (void)
27557 {
27558   tree ms_va_ref, sysv_va_ref;
27559   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
27560   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
27561   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
27562   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
27563
27564   if (!TARGET_64BIT)
27565     return;
27566   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
27567   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
27568   ms_va_ref = build_reference_type (ms_va_list_type_node);
27569   sysv_va_ref =
27570     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
27571
27572   fnvoid_va_end_ms =
27573     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
27574   fnvoid_va_start_ms =
27575     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
27576   fnvoid_va_end_sysv =
27577     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
27578   fnvoid_va_start_sysv =
27579     build_varargs_function_type_list (void_type_node, sysv_va_ref,
27580                                        NULL_TREE);
27581   fnvoid_va_copy_ms =
27582     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
27583                               NULL_TREE);
27584   fnvoid_va_copy_sysv =
27585     build_function_type_list (void_type_node, sysv_va_ref,
27586                               sysv_va_ref, NULL_TREE);
27587
27588   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
27589                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
27590   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
27591                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
27592   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
27593                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
27594   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
27595                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
27596   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
27597                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
27598   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
27599                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
27600 }
27601
27602 static void
27603 ix86_init_builtin_types (void)
27604 {
27605   tree float128_type_node, float80_type_node;
27606
27607   /* The __float80 type.  */
27608   float80_type_node = long_double_type_node;
27609   if (TYPE_MODE (float80_type_node) != XFmode)
27610     {
27611       /* The __float80 type.  */
27612       float80_type_node = make_node (REAL_TYPE);
27613
27614       TYPE_PRECISION (float80_type_node) = 80;
27615       layout_type (float80_type_node);
27616     }
27617   lang_hooks.types.register_builtin_type (float80_type_node, "__float80");
27618
27619   /* The __float128 type.  */
27620   float128_type_node = make_node (REAL_TYPE);
27621   TYPE_PRECISION (float128_type_node) = 128;
27622   layout_type (float128_type_node);
27623   lang_hooks.types.register_builtin_type (float128_type_node, "__float128");
27624
27625   /* This macro is built by i386-builtin-types.awk.  */
27626   DEFINE_BUILTIN_PRIMITIVE_TYPES;
27627 }
27628
27629 static void
27630 ix86_init_builtins (void)
27631 {
27632   tree t;
27633
27634   ix86_init_builtin_types ();
27635
27636   /* TFmode support builtins.  */
27637   def_builtin_const (0, "__builtin_infq",
27638                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
27639   def_builtin_const (0, "__builtin_huge_valq",
27640                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
27641
27642   /* We will expand them to normal call if SSE2 isn't available since
27643      they are used by libgcc. */
27644   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
27645   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
27646                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
27647   TREE_READONLY (t) = 1;
27648   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
27649
27650   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
27651   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
27652                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
27653   TREE_READONLY (t) = 1;
27654   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
27655
27656   ix86_init_tm_builtins ();
27657   ix86_init_mmx_sse_builtins ();
27658
27659   if (TARGET_LP64)
27660     ix86_init_builtins_va_builtins_abi ();
27661
27662 #ifdef SUBTARGET_INIT_BUILTINS
27663   SUBTARGET_INIT_BUILTINS;
27664 #endif
27665 }
27666
27667 /* Return the ix86 builtin for CODE.  */
27668
27669 static tree
27670 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
27671 {
27672   if (code >= IX86_BUILTIN_MAX)
27673     return error_mark_node;
27674
27675   return ix86_builtins[code];
27676 }
27677
27678 /* Errors in the source file can cause expand_expr to return const0_rtx
27679    where we expect a vector.  To avoid crashing, use one of the vector
27680    clear instructions.  */
27681 static rtx
27682 safe_vector_operand (rtx x, enum machine_mode mode)
27683 {
27684   if (x == const0_rtx)
27685     x = CONST0_RTX (mode);
27686   return x;
27687 }
27688
27689 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
27690
27691 static rtx
27692 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
27693 {
27694   rtx pat;
27695   tree arg0 = CALL_EXPR_ARG (exp, 0);
27696   tree arg1 = CALL_EXPR_ARG (exp, 1);
27697   rtx op0 = expand_normal (arg0);
27698   rtx op1 = expand_normal (arg1);
27699   enum machine_mode tmode = insn_data[icode].operand[0].mode;
27700   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
27701   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
27702
27703   if (VECTOR_MODE_P (mode0))
27704     op0 = safe_vector_operand (op0, mode0);
27705   if (VECTOR_MODE_P (mode1))
27706     op1 = safe_vector_operand (op1, mode1);
27707
27708   if (optimize || !target
27709       || GET_MODE (target) != tmode
27710       || !insn_data[icode].operand[0].predicate (target, tmode))
27711     target = gen_reg_rtx (tmode);
27712
27713   if (GET_MODE (op1) == SImode && mode1 == TImode)
27714     {
27715       rtx x = gen_reg_rtx (V4SImode);
27716       emit_insn (gen_sse2_loadd (x, op1));
27717       op1 = gen_lowpart (TImode, x);
27718     }
27719
27720   if (!insn_data[icode].operand[1].predicate (op0, mode0))
27721     op0 = copy_to_mode_reg (mode0, op0);
27722   if (!insn_data[icode].operand[2].predicate (op1, mode1))
27723     op1 = copy_to_mode_reg (mode1, op1);
27724
27725   pat = GEN_FCN (icode) (target, op0, op1);
27726   if (! pat)
27727     return 0;
27728
27729   emit_insn (pat);
27730
27731   return target;
27732 }
27733
27734 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
27735
27736 static rtx
27737 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
27738                                enum ix86_builtin_func_type m_type,
27739                                enum rtx_code sub_code)
27740 {
27741   rtx pat;
27742   int i;
27743   int nargs;
27744   bool comparison_p = false;
27745   bool tf_p = false;
27746   bool last_arg_constant = false;
27747   int num_memory = 0;
27748   struct {
27749     rtx op;
27750     enum machine_mode mode;
27751   } args[4];
27752
27753   enum machine_mode tmode = insn_data[icode].operand[0].mode;
27754
27755   switch (m_type)
27756     {
27757     case MULTI_ARG_4_DF2_DI_I:
27758     case MULTI_ARG_4_DF2_DI_I1:
27759     case MULTI_ARG_4_SF2_SI_I:
27760     case MULTI_ARG_4_SF2_SI_I1:
27761       nargs = 4;
27762       last_arg_constant = true;
27763       break;
27764
27765     case MULTI_ARG_3_SF:
27766     case MULTI_ARG_3_DF:
27767     case MULTI_ARG_3_SF2:
27768     case MULTI_ARG_3_DF2:
27769     case MULTI_ARG_3_DI:
27770     case MULTI_ARG_3_SI:
27771     case MULTI_ARG_3_SI_DI:
27772     case MULTI_ARG_3_HI:
27773     case MULTI_ARG_3_HI_SI:
27774     case MULTI_ARG_3_QI:
27775     case MULTI_ARG_3_DI2:
27776     case MULTI_ARG_3_SI2:
27777     case MULTI_ARG_3_HI2:
27778     case MULTI_ARG_3_QI2:
27779       nargs = 3;
27780       break;
27781
27782     case MULTI_ARG_2_SF:
27783     case MULTI_ARG_2_DF:
27784     case MULTI_ARG_2_DI:
27785     case MULTI_ARG_2_SI:
27786     case MULTI_ARG_2_HI:
27787     case MULTI_ARG_2_QI:
27788       nargs = 2;
27789       break;
27790
27791     case MULTI_ARG_2_DI_IMM:
27792     case MULTI_ARG_2_SI_IMM:
27793     case MULTI_ARG_2_HI_IMM:
27794     case MULTI_ARG_2_QI_IMM:
27795       nargs = 2;
27796       last_arg_constant = true;
27797       break;
27798
27799     case MULTI_ARG_1_SF:
27800     case MULTI_ARG_1_DF:
27801     case MULTI_ARG_1_SF2:
27802     case MULTI_ARG_1_DF2:
27803     case MULTI_ARG_1_DI:
27804     case MULTI_ARG_1_SI:
27805     case MULTI_ARG_1_HI:
27806     case MULTI_ARG_1_QI:
27807     case MULTI_ARG_1_SI_DI:
27808     case MULTI_ARG_1_HI_DI:
27809     case MULTI_ARG_1_HI_SI:
27810     case MULTI_ARG_1_QI_DI:
27811     case MULTI_ARG_1_QI_SI:
27812     case MULTI_ARG_1_QI_HI:
27813       nargs = 1;
27814       break;
27815
27816     case MULTI_ARG_2_DI_CMP:
27817     case MULTI_ARG_2_SI_CMP:
27818     case MULTI_ARG_2_HI_CMP:
27819     case MULTI_ARG_2_QI_CMP:
27820       nargs = 2;
27821       comparison_p = true;
27822       break;
27823
27824     case MULTI_ARG_2_SF_TF:
27825     case MULTI_ARG_2_DF_TF:
27826     case MULTI_ARG_2_DI_TF:
27827     case MULTI_ARG_2_SI_TF:
27828     case MULTI_ARG_2_HI_TF:
27829     case MULTI_ARG_2_QI_TF:
27830       nargs = 2;
27831       tf_p = true;
27832       break;
27833
27834     default:
27835       gcc_unreachable ();
27836     }
27837
27838   if (optimize || !target
27839       || GET_MODE (target) != tmode
27840       || !insn_data[icode].operand[0].predicate (target, tmode))
27841     target = gen_reg_rtx (tmode);
27842
27843   gcc_assert (nargs <= 4);
27844
27845   for (i = 0; i < nargs; i++)
27846     {
27847       tree arg = CALL_EXPR_ARG (exp, i);
27848       rtx op = expand_normal (arg);
27849       int adjust = (comparison_p) ? 1 : 0;
27850       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
27851
27852       if (last_arg_constant && i == nargs - 1)
27853         {
27854           if (!insn_data[icode].operand[i + 1].predicate (op, mode))
27855             {
27856               enum insn_code new_icode = icode;
27857               switch (icode)
27858                 {
27859                 case CODE_FOR_xop_vpermil2v2df3:
27860                 case CODE_FOR_xop_vpermil2v4sf3:
27861                 case CODE_FOR_xop_vpermil2v4df3:
27862                 case CODE_FOR_xop_vpermil2v8sf3:
27863                   error ("the last argument must be a 2-bit immediate");
27864                   return gen_reg_rtx (tmode);
27865                 case CODE_FOR_xop_rotlv2di3:
27866                   new_icode = CODE_FOR_rotlv2di3;
27867                   goto xop_rotl;
27868                 case CODE_FOR_xop_rotlv4si3:
27869                   new_icode = CODE_FOR_rotlv4si3;
27870                   goto xop_rotl;
27871                 case CODE_FOR_xop_rotlv8hi3:
27872                   new_icode = CODE_FOR_rotlv8hi3;
27873                   goto xop_rotl;
27874                 case CODE_FOR_xop_rotlv16qi3:
27875                   new_icode = CODE_FOR_rotlv16qi3;
27876                 xop_rotl:
27877                   if (CONST_INT_P (op))
27878                     {
27879                       int mask = GET_MODE_BITSIZE (GET_MODE_INNER (tmode)) - 1;
27880                       op = GEN_INT (INTVAL (op) & mask);
27881                       gcc_checking_assert
27882                         (insn_data[icode].operand[i + 1].predicate (op, mode));
27883                     }
27884                   else
27885                     {
27886                       gcc_checking_assert
27887                         (nargs == 2
27888                          && insn_data[new_icode].operand[0].mode == tmode
27889                          && insn_data[new_icode].operand[1].mode == tmode
27890                          && insn_data[new_icode].operand[2].mode == mode
27891                          && insn_data[new_icode].operand[0].predicate
27892                             == insn_data[icode].operand[0].predicate
27893                          && insn_data[new_icode].operand[1].predicate
27894                             == insn_data[icode].operand[1].predicate);
27895                       icode = new_icode;
27896                       goto non_constant;
27897                     }
27898                   break;
27899                 default:
27900                   gcc_unreachable ();
27901                 }
27902             }
27903         }
27904       else
27905         {
27906         non_constant:
27907           if (VECTOR_MODE_P (mode))
27908             op = safe_vector_operand (op, mode);
27909
27910           /* If we aren't optimizing, only allow one memory operand to be
27911              generated.  */
27912           if (memory_operand (op, mode))
27913             num_memory++;
27914
27915           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
27916
27917           if (optimize
27918               || !insn_data[icode].operand[i+adjust+1].predicate (op, mode)
27919               || num_memory > 1)
27920             op = force_reg (mode, op);
27921         }
27922
27923       args[i].op = op;
27924       args[i].mode = mode;
27925     }
27926
27927   switch (nargs)
27928     {
27929     case 1:
27930       pat = GEN_FCN (icode) (target, args[0].op);
27931       break;
27932
27933     case 2:
27934       if (tf_p)
27935         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
27936                                GEN_INT ((int)sub_code));
27937       else if (! comparison_p)
27938         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
27939       else
27940         {
27941           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
27942                                        args[0].op,
27943                                        args[1].op);
27944
27945           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
27946         }
27947       break;
27948
27949     case 3:
27950       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
27951       break;
27952
27953     case 4:
27954       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
27955       break;
27956
27957     default:
27958       gcc_unreachable ();
27959     }
27960
27961   if (! pat)
27962     return 0;
27963
27964   emit_insn (pat);
27965   return target;
27966 }
27967
27968 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
27969    insns with vec_merge.  */
27970
27971 static rtx
27972 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
27973                                     rtx target)
27974 {
27975   rtx pat;
27976   tree arg0 = CALL_EXPR_ARG (exp, 0);
27977   rtx op1, op0 = expand_normal (arg0);
27978   enum machine_mode tmode = insn_data[icode].operand[0].mode;
27979   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
27980
27981   if (optimize || !target
27982       || GET_MODE (target) != tmode
27983       || !insn_data[icode].operand[0].predicate (target, tmode))
27984     target = gen_reg_rtx (tmode);
27985
27986   if (VECTOR_MODE_P (mode0))
27987     op0 = safe_vector_operand (op0, mode0);
27988
27989   if ((optimize && !register_operand (op0, mode0))
27990       || !insn_data[icode].operand[1].predicate (op0, mode0))
27991     op0 = copy_to_mode_reg (mode0, op0);
27992
27993   op1 = op0;
27994   if (!insn_data[icode].operand[2].predicate (op1, mode0))
27995     op1 = copy_to_mode_reg (mode0, op1);
27996
27997   pat = GEN_FCN (icode) (target, op0, op1);
27998   if (! pat)
27999     return 0;
28000   emit_insn (pat);
28001   return target;
28002 }
28003
28004 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
28005
28006 static rtx
28007 ix86_expand_sse_compare (const struct builtin_description *d,
28008                          tree exp, rtx target, bool swap)
28009 {
28010   rtx pat;
28011   tree arg0 = CALL_EXPR_ARG (exp, 0);
28012   tree arg1 = CALL_EXPR_ARG (exp, 1);
28013   rtx op0 = expand_normal (arg0);
28014   rtx op1 = expand_normal (arg1);
28015   rtx op2;
28016   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
28017   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
28018   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
28019   enum rtx_code comparison = d->comparison;
28020
28021   if (VECTOR_MODE_P (mode0))
28022     op0 = safe_vector_operand (op0, mode0);
28023   if (VECTOR_MODE_P (mode1))
28024     op1 = safe_vector_operand (op1, mode1);
28025
28026   /* Swap operands if we have a comparison that isn't available in
28027      hardware.  */
28028   if (swap)
28029     {
28030       rtx tmp = gen_reg_rtx (mode1);
28031       emit_move_insn (tmp, op1);
28032       op1 = op0;
28033       op0 = tmp;
28034     }
28035
28036   if (optimize || !target
28037       || GET_MODE (target) != tmode
28038       || !insn_data[d->icode].operand[0].predicate (target, tmode))
28039     target = gen_reg_rtx (tmode);
28040
28041   if ((optimize && !register_operand (op0, mode0))
28042       || !insn_data[d->icode].operand[1].predicate (op0, mode0))
28043     op0 = copy_to_mode_reg (mode0, op0);
28044   if ((optimize && !register_operand (op1, mode1))
28045       || !insn_data[d->icode].operand[2].predicate (op1, mode1))
28046     op1 = copy_to_mode_reg (mode1, op1);
28047
28048   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
28049   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
28050   if (! pat)
28051     return 0;
28052   emit_insn (pat);
28053   return target;
28054 }
28055
28056 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
28057
28058 static rtx
28059 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
28060                       rtx target)
28061 {
28062   rtx pat;
28063   tree arg0 = CALL_EXPR_ARG (exp, 0);
28064   tree arg1 = CALL_EXPR_ARG (exp, 1);
28065   rtx op0 = expand_normal (arg0);
28066   rtx op1 = expand_normal (arg1);
28067   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
28068   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
28069   enum rtx_code comparison = d->comparison;
28070
28071   if (VECTOR_MODE_P (mode0))
28072     op0 = safe_vector_operand (op0, mode0);
28073   if (VECTOR_MODE_P (mode1))
28074     op1 = safe_vector_operand (op1, mode1);
28075
28076   /* Swap operands if we have a comparison that isn't available in
28077      hardware.  */
28078   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
28079     {
28080       rtx tmp = op1;
28081       op1 = op0;
28082       op0 = tmp;
28083     }
28084
28085   target = gen_reg_rtx (SImode);
28086   emit_move_insn (target, const0_rtx);
28087   target = gen_rtx_SUBREG (QImode, target, 0);
28088
28089   if ((optimize && !register_operand (op0, mode0))
28090       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28091     op0 = copy_to_mode_reg (mode0, op0);
28092   if ((optimize && !register_operand (op1, mode1))
28093       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
28094     op1 = copy_to_mode_reg (mode1, op1);
28095
28096   pat = GEN_FCN (d->icode) (op0, op1);
28097   if (! pat)
28098     return 0;
28099   emit_insn (pat);
28100   emit_insn (gen_rtx_SET (VOIDmode,
28101                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28102                           gen_rtx_fmt_ee (comparison, QImode,
28103                                           SET_DEST (pat),
28104                                           const0_rtx)));
28105
28106   return SUBREG_REG (target);
28107 }
28108
28109 /* Subroutines of ix86_expand_args_builtin to take care of round insns.  */
28110
28111 static rtx
28112 ix86_expand_sse_round (const struct builtin_description *d, tree exp,
28113                        rtx target)
28114 {
28115   rtx pat;
28116   tree arg0 = CALL_EXPR_ARG (exp, 0);
28117   rtx op1, op0 = expand_normal (arg0);
28118   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
28119   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
28120
28121   if (optimize || target == 0
28122       || GET_MODE (target) != tmode
28123       || !insn_data[d->icode].operand[0].predicate (target, tmode))
28124     target = gen_reg_rtx (tmode);
28125
28126   if (VECTOR_MODE_P (mode0))
28127     op0 = safe_vector_operand (op0, mode0);
28128
28129   if ((optimize && !register_operand (op0, mode0))
28130       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28131     op0 = copy_to_mode_reg (mode0, op0);
28132
28133   op1 = GEN_INT (d->comparison);
28134
28135   pat = GEN_FCN (d->icode) (target, op0, op1);
28136   if (! pat)
28137     return 0;
28138   emit_insn (pat);
28139   return target;
28140 }
28141
28142 static rtx
28143 ix86_expand_sse_round_vec_pack_sfix (const struct builtin_description *d,
28144                                      tree exp, rtx target)
28145 {
28146   rtx pat;
28147   tree arg0 = CALL_EXPR_ARG (exp, 0);
28148   tree arg1 = CALL_EXPR_ARG (exp, 1);
28149   rtx op0 = expand_normal (arg0);
28150   rtx op1 = expand_normal (arg1);
28151   rtx op2;
28152   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
28153   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
28154   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
28155
28156   if (optimize || target == 0
28157       || GET_MODE (target) != tmode
28158       || !insn_data[d->icode].operand[0].predicate (target, tmode))
28159     target = gen_reg_rtx (tmode);
28160
28161   op0 = safe_vector_operand (op0, mode0);
28162   op1 = safe_vector_operand (op1, mode1);
28163
28164   if ((optimize && !register_operand (op0, mode0))
28165       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28166     op0 = copy_to_mode_reg (mode0, op0);
28167   if ((optimize && !register_operand (op1, mode1))
28168       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
28169     op1 = copy_to_mode_reg (mode1, op1);
28170
28171   op2 = GEN_INT (d->comparison);
28172
28173   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
28174   if (! pat)
28175     return 0;
28176   emit_insn (pat);
28177   return target;
28178 }
28179
28180 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
28181
28182 static rtx
28183 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
28184                        rtx target)
28185 {
28186   rtx pat;
28187   tree arg0 = CALL_EXPR_ARG (exp, 0);
28188   tree arg1 = CALL_EXPR_ARG (exp, 1);
28189   rtx op0 = expand_normal (arg0);
28190   rtx op1 = expand_normal (arg1);
28191   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
28192   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
28193   enum rtx_code comparison = d->comparison;
28194
28195   if (VECTOR_MODE_P (mode0))
28196     op0 = safe_vector_operand (op0, mode0);
28197   if (VECTOR_MODE_P (mode1))
28198     op1 = safe_vector_operand (op1, mode1);
28199
28200   target = gen_reg_rtx (SImode);
28201   emit_move_insn (target, const0_rtx);
28202   target = gen_rtx_SUBREG (QImode, target, 0);
28203
28204   if ((optimize && !register_operand (op0, mode0))
28205       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28206     op0 = copy_to_mode_reg (mode0, op0);
28207   if ((optimize && !register_operand (op1, mode1))
28208       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
28209     op1 = copy_to_mode_reg (mode1, op1);
28210
28211   pat = GEN_FCN (d->icode) (op0, op1);
28212   if (! pat)
28213     return 0;
28214   emit_insn (pat);
28215   emit_insn (gen_rtx_SET (VOIDmode,
28216                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28217                           gen_rtx_fmt_ee (comparison, QImode,
28218                                           SET_DEST (pat),
28219                                           const0_rtx)));
28220
28221   return SUBREG_REG (target);
28222 }
28223
28224 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
28225
28226 static rtx
28227 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
28228                           tree exp, rtx target)
28229 {
28230   rtx pat;
28231   tree arg0 = CALL_EXPR_ARG (exp, 0);
28232   tree arg1 = CALL_EXPR_ARG (exp, 1);
28233   tree arg2 = CALL_EXPR_ARG (exp, 2);
28234   tree arg3 = CALL_EXPR_ARG (exp, 3);
28235   tree arg4 = CALL_EXPR_ARG (exp, 4);
28236   rtx scratch0, scratch1;
28237   rtx op0 = expand_normal (arg0);
28238   rtx op1 = expand_normal (arg1);
28239   rtx op2 = expand_normal (arg2);
28240   rtx op3 = expand_normal (arg3);
28241   rtx op4 = expand_normal (arg4);
28242   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
28243
28244   tmode0 = insn_data[d->icode].operand[0].mode;
28245   tmode1 = insn_data[d->icode].operand[1].mode;
28246   modev2 = insn_data[d->icode].operand[2].mode;
28247   modei3 = insn_data[d->icode].operand[3].mode;
28248   modev4 = insn_data[d->icode].operand[4].mode;
28249   modei5 = insn_data[d->icode].operand[5].mode;
28250   modeimm = insn_data[d->icode].operand[6].mode;
28251
28252   if (VECTOR_MODE_P (modev2))
28253     op0 = safe_vector_operand (op0, modev2);
28254   if (VECTOR_MODE_P (modev4))
28255     op2 = safe_vector_operand (op2, modev4);
28256
28257   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
28258     op0 = copy_to_mode_reg (modev2, op0);
28259   if (!insn_data[d->icode].operand[3].predicate (op1, modei3))
28260     op1 = copy_to_mode_reg (modei3, op1);
28261   if ((optimize && !register_operand (op2, modev4))
28262       || !insn_data[d->icode].operand[4].predicate (op2, modev4))
28263     op2 = copy_to_mode_reg (modev4, op2);
28264   if (!insn_data[d->icode].operand[5].predicate (op3, modei5))
28265     op3 = copy_to_mode_reg (modei5, op3);
28266
28267   if (!insn_data[d->icode].operand[6].predicate (op4, modeimm))
28268     {
28269       error ("the fifth argument must be an 8-bit immediate");
28270       return const0_rtx;
28271     }
28272
28273   if (d->code == IX86_BUILTIN_PCMPESTRI128)
28274     {
28275       if (optimize || !target
28276           || GET_MODE (target) != tmode0
28277           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
28278         target = gen_reg_rtx (tmode0);
28279
28280       scratch1 = gen_reg_rtx (tmode1);
28281
28282       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
28283     }
28284   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
28285     {
28286       if (optimize || !target
28287           || GET_MODE (target) != tmode1
28288           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
28289         target = gen_reg_rtx (tmode1);
28290
28291       scratch0 = gen_reg_rtx (tmode0);
28292
28293       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
28294     }
28295   else
28296     {
28297       gcc_assert (d->flag);
28298
28299       scratch0 = gen_reg_rtx (tmode0);
28300       scratch1 = gen_reg_rtx (tmode1);
28301
28302       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
28303     }
28304
28305   if (! pat)
28306     return 0;
28307
28308   emit_insn (pat);
28309
28310   if (d->flag)
28311     {
28312       target = gen_reg_rtx (SImode);
28313       emit_move_insn (target, const0_rtx);
28314       target = gen_rtx_SUBREG (QImode, target, 0);
28315
28316       emit_insn
28317         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28318                       gen_rtx_fmt_ee (EQ, QImode,
28319                                       gen_rtx_REG ((enum machine_mode) d->flag,
28320                                                    FLAGS_REG),
28321                                       const0_rtx)));
28322       return SUBREG_REG (target);
28323     }
28324   else
28325     return target;
28326 }
28327
28328
28329 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
28330
28331 static rtx
28332 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
28333                           tree exp, rtx target)
28334 {
28335   rtx pat;
28336   tree arg0 = CALL_EXPR_ARG (exp, 0);
28337   tree arg1 = CALL_EXPR_ARG (exp, 1);
28338   tree arg2 = CALL_EXPR_ARG (exp, 2);
28339   rtx scratch0, scratch1;
28340   rtx op0 = expand_normal (arg0);
28341   rtx op1 = expand_normal (arg1);
28342   rtx op2 = expand_normal (arg2);
28343   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
28344
28345   tmode0 = insn_data[d->icode].operand[0].mode;
28346   tmode1 = insn_data[d->icode].operand[1].mode;
28347   modev2 = insn_data[d->icode].operand[2].mode;
28348   modev3 = insn_data[d->icode].operand[3].mode;
28349   modeimm = insn_data[d->icode].operand[4].mode;
28350
28351   if (VECTOR_MODE_P (modev2))
28352     op0 = safe_vector_operand (op0, modev2);
28353   if (VECTOR_MODE_P (modev3))
28354     op1 = safe_vector_operand (op1, modev3);
28355
28356   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
28357     op0 = copy_to_mode_reg (modev2, op0);
28358   if ((optimize && !register_operand (op1, modev3))
28359       || !insn_data[d->icode].operand[3].predicate (op1, modev3))
28360     op1 = copy_to_mode_reg (modev3, op1);
28361
28362   if (!insn_data[d->icode].operand[4].predicate (op2, modeimm))
28363     {
28364       error ("the third argument must be an 8-bit immediate");
28365       return const0_rtx;
28366     }
28367
28368   if (d->code == IX86_BUILTIN_PCMPISTRI128)
28369     {
28370       if (optimize || !target
28371           || GET_MODE (target) != tmode0
28372           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
28373         target = gen_reg_rtx (tmode0);
28374
28375       scratch1 = gen_reg_rtx (tmode1);
28376
28377       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
28378     }
28379   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
28380     {
28381       if (optimize || !target
28382           || GET_MODE (target) != tmode1
28383           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
28384         target = gen_reg_rtx (tmode1);
28385
28386       scratch0 = gen_reg_rtx (tmode0);
28387
28388       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
28389     }
28390   else
28391     {
28392       gcc_assert (d->flag);
28393
28394       scratch0 = gen_reg_rtx (tmode0);
28395       scratch1 = gen_reg_rtx (tmode1);
28396
28397       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
28398     }
28399
28400   if (! pat)
28401     return 0;
28402
28403   emit_insn (pat);
28404
28405   if (d->flag)
28406     {
28407       target = gen_reg_rtx (SImode);
28408       emit_move_insn (target, const0_rtx);
28409       target = gen_rtx_SUBREG (QImode, target, 0);
28410
28411       emit_insn
28412         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28413                       gen_rtx_fmt_ee (EQ, QImode,
28414                                       gen_rtx_REG ((enum machine_mode) d->flag,
28415                                                    FLAGS_REG),
28416                                       const0_rtx)));
28417       return SUBREG_REG (target);
28418     }
28419   else
28420     return target;
28421 }
28422
28423 /* Subroutine of ix86_expand_builtin to take care of insns with
28424    variable number of operands.  */
28425
28426 static rtx
28427 ix86_expand_args_builtin (const struct builtin_description *d,
28428                           tree exp, rtx target)
28429 {
28430   rtx pat, real_target;
28431   unsigned int i, nargs;
28432   unsigned int nargs_constant = 0;
28433   int num_memory = 0;
28434   struct
28435     {
28436       rtx op;
28437       enum machine_mode mode;
28438     } args[4];
28439   bool last_arg_count = false;
28440   enum insn_code icode = d->icode;
28441   const struct insn_data_d *insn_p = &insn_data[icode];
28442   enum machine_mode tmode = insn_p->operand[0].mode;
28443   enum machine_mode rmode = VOIDmode;
28444   bool swap = false;
28445   enum rtx_code comparison = d->comparison;
28446
28447   switch ((enum ix86_builtin_func_type) d->flag)
28448     {
28449     case V2DF_FTYPE_V2DF_ROUND:
28450     case V4DF_FTYPE_V4DF_ROUND:
28451     case V4SF_FTYPE_V4SF_ROUND:
28452     case V8SF_FTYPE_V8SF_ROUND:
28453     case V4SI_FTYPE_V4SF_ROUND:
28454     case V8SI_FTYPE_V8SF_ROUND:
28455       return ix86_expand_sse_round (d, exp, target);
28456     case V4SI_FTYPE_V2DF_V2DF_ROUND:
28457     case V8SI_FTYPE_V4DF_V4DF_ROUND:
28458       return ix86_expand_sse_round_vec_pack_sfix (d, exp, target);
28459     case INT_FTYPE_V8SF_V8SF_PTEST:
28460     case INT_FTYPE_V4DI_V4DI_PTEST:
28461     case INT_FTYPE_V4DF_V4DF_PTEST:
28462     case INT_FTYPE_V4SF_V4SF_PTEST:
28463     case INT_FTYPE_V2DI_V2DI_PTEST:
28464     case INT_FTYPE_V2DF_V2DF_PTEST:
28465       return ix86_expand_sse_ptest (d, exp, target);
28466     case FLOAT128_FTYPE_FLOAT128:
28467     case FLOAT_FTYPE_FLOAT:
28468     case INT_FTYPE_INT:
28469     case UINT64_FTYPE_INT:
28470     case UINT16_FTYPE_UINT16:
28471     case INT64_FTYPE_INT64:
28472     case INT64_FTYPE_V4SF:
28473     case INT64_FTYPE_V2DF:
28474     case INT_FTYPE_V16QI:
28475     case INT_FTYPE_V8QI:
28476     case INT_FTYPE_V8SF:
28477     case INT_FTYPE_V4DF:
28478     case INT_FTYPE_V4SF:
28479     case INT_FTYPE_V2DF:
28480     case INT_FTYPE_V32QI:
28481     case V16QI_FTYPE_V16QI:
28482     case V8SI_FTYPE_V8SF:
28483     case V8SI_FTYPE_V4SI:
28484     case V8HI_FTYPE_V8HI:
28485     case V8HI_FTYPE_V16QI:
28486     case V8QI_FTYPE_V8QI:
28487     case V8SF_FTYPE_V8SF:
28488     case V8SF_FTYPE_V8SI:
28489     case V8SF_FTYPE_V4SF:
28490     case V8SF_FTYPE_V8HI:
28491     case V4SI_FTYPE_V4SI:
28492     case V4SI_FTYPE_V16QI:
28493     case V4SI_FTYPE_V4SF:
28494     case V4SI_FTYPE_V8SI:
28495     case V4SI_FTYPE_V8HI:
28496     case V4SI_FTYPE_V4DF:
28497     case V4SI_FTYPE_V2DF:
28498     case V4HI_FTYPE_V4HI:
28499     case V4DF_FTYPE_V4DF:
28500     case V4DF_FTYPE_V4SI:
28501     case V4DF_FTYPE_V4SF:
28502     case V4DF_FTYPE_V2DF:
28503     case V4SF_FTYPE_V4SF:
28504     case V4SF_FTYPE_V4SI:
28505     case V4SF_FTYPE_V8SF:
28506     case V4SF_FTYPE_V4DF:
28507     case V4SF_FTYPE_V8HI:
28508     case V4SF_FTYPE_V2DF:
28509     case V2DI_FTYPE_V2DI:
28510     case V2DI_FTYPE_V16QI:
28511     case V2DI_FTYPE_V8HI:
28512     case V2DI_FTYPE_V4SI:
28513     case V2DF_FTYPE_V2DF:
28514     case V2DF_FTYPE_V4SI:
28515     case V2DF_FTYPE_V4DF:
28516     case V2DF_FTYPE_V4SF:
28517     case V2DF_FTYPE_V2SI:
28518     case V2SI_FTYPE_V2SI:
28519     case V2SI_FTYPE_V4SF:
28520     case V2SI_FTYPE_V2SF:
28521     case V2SI_FTYPE_V2DF:
28522     case V2SF_FTYPE_V2SF:
28523     case V2SF_FTYPE_V2SI:
28524     case V32QI_FTYPE_V32QI:
28525     case V32QI_FTYPE_V16QI:
28526     case V16HI_FTYPE_V16HI:
28527     case V16HI_FTYPE_V8HI:
28528     case V8SI_FTYPE_V8SI:
28529     case V16HI_FTYPE_V16QI:
28530     case V8SI_FTYPE_V16QI:
28531     case V4DI_FTYPE_V16QI:
28532     case V8SI_FTYPE_V8HI:
28533     case V4DI_FTYPE_V8HI:
28534     case V4DI_FTYPE_V4SI:
28535     case V4DI_FTYPE_V2DI:
28536       nargs = 1;
28537       break;
28538     case V4SF_FTYPE_V4SF_VEC_MERGE:
28539     case V2DF_FTYPE_V2DF_VEC_MERGE:
28540       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
28541     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
28542     case V16QI_FTYPE_V16QI_V16QI:
28543     case V16QI_FTYPE_V8HI_V8HI:
28544     case V8QI_FTYPE_V8QI_V8QI:
28545     case V8QI_FTYPE_V4HI_V4HI:
28546     case V8HI_FTYPE_V8HI_V8HI:
28547     case V8HI_FTYPE_V16QI_V16QI:
28548     case V8HI_FTYPE_V4SI_V4SI:
28549     case V8SF_FTYPE_V8SF_V8SF:
28550     case V8SF_FTYPE_V8SF_V8SI:
28551     case V4SI_FTYPE_V4SI_V4SI:
28552     case V4SI_FTYPE_V8HI_V8HI:
28553     case V4SI_FTYPE_V4SF_V4SF:
28554     case V4SI_FTYPE_V2DF_V2DF:
28555     case V4HI_FTYPE_V4HI_V4HI:
28556     case V4HI_FTYPE_V8QI_V8QI:
28557     case V4HI_FTYPE_V2SI_V2SI:
28558     case V4DF_FTYPE_V4DF_V4DF:
28559     case V4DF_FTYPE_V4DF_V4DI:
28560     case V4SF_FTYPE_V4SF_V4SF:
28561     case V4SF_FTYPE_V4SF_V4SI:
28562     case V4SF_FTYPE_V4SF_V2SI:
28563     case V4SF_FTYPE_V4SF_V2DF:
28564     case V4SF_FTYPE_V4SF_DI:
28565     case V4SF_FTYPE_V4SF_SI:
28566     case V2DI_FTYPE_V2DI_V2DI:
28567     case V2DI_FTYPE_V16QI_V16QI:
28568     case V2DI_FTYPE_V4SI_V4SI:
28569     case V2DI_FTYPE_V2DI_V16QI:
28570     case V2DI_FTYPE_V2DF_V2DF:
28571     case V2SI_FTYPE_V2SI_V2SI:
28572     case V2SI_FTYPE_V4HI_V4HI:
28573     case V2SI_FTYPE_V2SF_V2SF:
28574     case V2DF_FTYPE_V2DF_V2DF:
28575     case V2DF_FTYPE_V2DF_V4SF:
28576     case V2DF_FTYPE_V2DF_V2DI:
28577     case V2DF_FTYPE_V2DF_DI:
28578     case V2DF_FTYPE_V2DF_SI:
28579     case V2SF_FTYPE_V2SF_V2SF:
28580     case V1DI_FTYPE_V1DI_V1DI:
28581     case V1DI_FTYPE_V8QI_V8QI:
28582     case V1DI_FTYPE_V2SI_V2SI:
28583     case V32QI_FTYPE_V16HI_V16HI:
28584     case V16HI_FTYPE_V8SI_V8SI:
28585     case V32QI_FTYPE_V32QI_V32QI:
28586     case V16HI_FTYPE_V32QI_V32QI:
28587     case V16HI_FTYPE_V16HI_V16HI:
28588     case V8SI_FTYPE_V4DF_V4DF:
28589     case V8SI_FTYPE_V8SI_V8SI:
28590     case V8SI_FTYPE_V16HI_V16HI:
28591     case V4DI_FTYPE_V4DI_V4DI:
28592     case V4DI_FTYPE_V8SI_V8SI:
28593       if (comparison == UNKNOWN)
28594         return ix86_expand_binop_builtin (icode, exp, target);
28595       nargs = 2;
28596       break;
28597     case V4SF_FTYPE_V4SF_V4SF_SWAP:
28598     case V2DF_FTYPE_V2DF_V2DF_SWAP:
28599       gcc_assert (comparison != UNKNOWN);
28600       nargs = 2;
28601       swap = true;
28602       break;
28603     case V16HI_FTYPE_V16HI_V8HI_COUNT:
28604     case V16HI_FTYPE_V16HI_SI_COUNT:
28605     case V8SI_FTYPE_V8SI_V4SI_COUNT:
28606     case V8SI_FTYPE_V8SI_SI_COUNT:
28607     case V4DI_FTYPE_V4DI_V2DI_COUNT:
28608     case V4DI_FTYPE_V4DI_INT_COUNT:
28609     case V8HI_FTYPE_V8HI_V8HI_COUNT:
28610     case V8HI_FTYPE_V8HI_SI_COUNT:
28611     case V4SI_FTYPE_V4SI_V4SI_COUNT:
28612     case V4SI_FTYPE_V4SI_SI_COUNT:
28613     case V4HI_FTYPE_V4HI_V4HI_COUNT:
28614     case V4HI_FTYPE_V4HI_SI_COUNT:
28615     case V2DI_FTYPE_V2DI_V2DI_COUNT:
28616     case V2DI_FTYPE_V2DI_SI_COUNT:
28617     case V2SI_FTYPE_V2SI_V2SI_COUNT:
28618     case V2SI_FTYPE_V2SI_SI_COUNT:
28619     case V1DI_FTYPE_V1DI_V1DI_COUNT:
28620     case V1DI_FTYPE_V1DI_SI_COUNT:
28621       nargs = 2;
28622       last_arg_count = true;
28623       break;
28624     case UINT64_FTYPE_UINT64_UINT64:
28625     case UINT_FTYPE_UINT_UINT:
28626     case UINT_FTYPE_UINT_USHORT:
28627     case UINT_FTYPE_UINT_UCHAR:
28628     case UINT16_FTYPE_UINT16_INT:
28629     case UINT8_FTYPE_UINT8_INT:
28630       nargs = 2;
28631       break;
28632     case V2DI_FTYPE_V2DI_INT_CONVERT:
28633       nargs = 2;
28634       rmode = V1TImode;
28635       nargs_constant = 1;
28636       break;
28637     case V4DI_FTYPE_V4DI_INT_CONVERT:
28638       nargs = 2;
28639       rmode = V2TImode;
28640       nargs_constant = 1;
28641       break;
28642     case V8HI_FTYPE_V8HI_INT:
28643     case V8HI_FTYPE_V8SF_INT:
28644     case V8HI_FTYPE_V4SF_INT:
28645     case V8SF_FTYPE_V8SF_INT:
28646     case V4SI_FTYPE_V4SI_INT:
28647     case V4SI_FTYPE_V8SI_INT:
28648     case V4HI_FTYPE_V4HI_INT:
28649     case V4DF_FTYPE_V4DF_INT:
28650     case V4SF_FTYPE_V4SF_INT:
28651     case V4SF_FTYPE_V8SF_INT:
28652     case V2DI_FTYPE_V2DI_INT:
28653     case V2DF_FTYPE_V2DF_INT:
28654     case V2DF_FTYPE_V4DF_INT:
28655     case V16HI_FTYPE_V16HI_INT:
28656     case V8SI_FTYPE_V8SI_INT:
28657     case V4DI_FTYPE_V4DI_INT:
28658     case V2DI_FTYPE_V4DI_INT:
28659       nargs = 2;
28660       nargs_constant = 1;
28661       break;
28662     case V16QI_FTYPE_V16QI_V16QI_V16QI:
28663     case V8SF_FTYPE_V8SF_V8SF_V8SF:
28664     case V4DF_FTYPE_V4DF_V4DF_V4DF:
28665     case V4SF_FTYPE_V4SF_V4SF_V4SF:
28666     case V2DF_FTYPE_V2DF_V2DF_V2DF:
28667     case V32QI_FTYPE_V32QI_V32QI_V32QI:
28668       nargs = 3;
28669       break;
28670     case V32QI_FTYPE_V32QI_V32QI_INT:
28671     case V16HI_FTYPE_V16HI_V16HI_INT:
28672     case V16QI_FTYPE_V16QI_V16QI_INT:
28673     case V4DI_FTYPE_V4DI_V4DI_INT:
28674     case V8HI_FTYPE_V8HI_V8HI_INT:
28675     case V8SI_FTYPE_V8SI_V8SI_INT:
28676     case V8SI_FTYPE_V8SI_V4SI_INT:
28677     case V8SF_FTYPE_V8SF_V8SF_INT:
28678     case V8SF_FTYPE_V8SF_V4SF_INT:
28679     case V4SI_FTYPE_V4SI_V4SI_INT:
28680     case V4DF_FTYPE_V4DF_V4DF_INT:
28681     case V4DF_FTYPE_V4DF_V2DF_INT:
28682     case V4SF_FTYPE_V4SF_V4SF_INT:
28683     case V2DI_FTYPE_V2DI_V2DI_INT:
28684     case V4DI_FTYPE_V4DI_V2DI_INT:
28685     case V2DF_FTYPE_V2DF_V2DF_INT:
28686       nargs = 3;
28687       nargs_constant = 1;
28688       break;
28689     case V4DI_FTYPE_V4DI_V4DI_INT_CONVERT:
28690       nargs = 3;
28691       rmode = V4DImode;
28692       nargs_constant = 1;
28693       break;
28694     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
28695       nargs = 3;
28696       rmode = V2DImode;
28697       nargs_constant = 1;
28698       break;
28699     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
28700       nargs = 3;
28701       rmode = DImode;
28702       nargs_constant = 1;
28703       break;
28704     case V2DI_FTYPE_V2DI_UINT_UINT:
28705       nargs = 3;
28706       nargs_constant = 2;
28707       break;
28708     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
28709     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
28710     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
28711     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
28712       nargs = 4;
28713       nargs_constant = 1;
28714       break;
28715     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
28716       nargs = 4;
28717       nargs_constant = 2;
28718       break;
28719     default:
28720       gcc_unreachable ();
28721     }
28722
28723   gcc_assert (nargs <= ARRAY_SIZE (args));
28724
28725   if (comparison != UNKNOWN)
28726     {
28727       gcc_assert (nargs == 2);
28728       return ix86_expand_sse_compare (d, exp, target, swap);
28729     }
28730
28731   if (rmode == VOIDmode || rmode == tmode)
28732     {
28733       if (optimize
28734           || target == 0
28735           || GET_MODE (target) != tmode
28736           || !insn_p->operand[0].predicate (target, tmode))
28737         target = gen_reg_rtx (tmode);
28738       real_target = target;
28739     }
28740   else
28741     {
28742       target = gen_reg_rtx (rmode);
28743       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
28744     }
28745
28746   for (i = 0; i < nargs; i++)
28747     {
28748       tree arg = CALL_EXPR_ARG (exp, i);
28749       rtx op = expand_normal (arg);
28750       enum machine_mode mode = insn_p->operand[i + 1].mode;
28751       bool match = insn_p->operand[i + 1].predicate (op, mode);
28752
28753       if (last_arg_count && (i + 1) == nargs)
28754         {
28755           /* SIMD shift insns take either an 8-bit immediate or
28756              register as count.  But builtin functions take int as
28757              count.  If count doesn't match, we put it in register.  */
28758           if (!match)
28759             {
28760               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
28761               if (!insn_p->operand[i + 1].predicate (op, mode))
28762                 op = copy_to_reg (op);
28763             }
28764         }
28765       else if ((nargs - i) <= nargs_constant)
28766         {
28767           if (!match)
28768             switch (icode)
28769               {
28770               case CODE_FOR_avx2_inserti128:
28771               case CODE_FOR_avx2_extracti128:
28772                 error ("the last argument must be an 1-bit immediate");
28773                 return const0_rtx;
28774
28775               case CODE_FOR_sse4_1_roundsd:
28776               case CODE_FOR_sse4_1_roundss:
28777
28778               case CODE_FOR_sse4_1_roundpd:
28779               case CODE_FOR_sse4_1_roundps:
28780               case CODE_FOR_avx_roundpd256:
28781               case CODE_FOR_avx_roundps256:
28782
28783               case CODE_FOR_sse4_1_roundpd_vec_pack_sfix:
28784               case CODE_FOR_sse4_1_roundps_sfix:
28785               case CODE_FOR_avx_roundpd_vec_pack_sfix256:
28786               case CODE_FOR_avx_roundps_sfix256:
28787
28788               case CODE_FOR_sse4_1_blendps:
28789               case CODE_FOR_avx_blendpd256:
28790               case CODE_FOR_avx_vpermilv4df:
28791                 error ("the last argument must be a 4-bit immediate");
28792                 return const0_rtx;
28793
28794               case CODE_FOR_sse4_1_blendpd:
28795               case CODE_FOR_avx_vpermilv2df:
28796               case CODE_FOR_xop_vpermil2v2df3:
28797               case CODE_FOR_xop_vpermil2v4sf3:
28798               case CODE_FOR_xop_vpermil2v4df3:
28799               case CODE_FOR_xop_vpermil2v8sf3:
28800                 error ("the last argument must be a 2-bit immediate");
28801                 return const0_rtx;
28802
28803               case CODE_FOR_avx_vextractf128v4df:
28804               case CODE_FOR_avx_vextractf128v8sf:
28805               case CODE_FOR_avx_vextractf128v8si:
28806               case CODE_FOR_avx_vinsertf128v4df:
28807               case CODE_FOR_avx_vinsertf128v8sf:
28808               case CODE_FOR_avx_vinsertf128v8si:
28809                 error ("the last argument must be a 1-bit immediate");
28810                 return const0_rtx;
28811
28812               case CODE_FOR_avx_vmcmpv2df3:
28813               case CODE_FOR_avx_vmcmpv4sf3:
28814               case CODE_FOR_avx_cmpv2df3:
28815               case CODE_FOR_avx_cmpv4sf3:
28816               case CODE_FOR_avx_cmpv4df3:
28817               case CODE_FOR_avx_cmpv8sf3:
28818                 error ("the last argument must be a 5-bit immediate");
28819                 return const0_rtx;
28820
28821              default:
28822                 switch (nargs_constant)
28823                   {
28824                   case 2:
28825                     if ((nargs - i) == nargs_constant)
28826                       {
28827                         error ("the next to last argument must be an 8-bit immediate");
28828                         break;
28829                       }
28830                   case 1:
28831                     error ("the last argument must be an 8-bit immediate");
28832                     break;
28833                   default:
28834                     gcc_unreachable ();
28835                   }
28836                 return const0_rtx;
28837               }
28838         }
28839       else
28840         {
28841           if (VECTOR_MODE_P (mode))
28842             op = safe_vector_operand (op, mode);
28843
28844           /* If we aren't optimizing, only allow one memory operand to
28845              be generated.  */
28846           if (memory_operand (op, mode))
28847             num_memory++;
28848
28849           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
28850             {
28851               if (optimize || !match || num_memory > 1)
28852                 op = copy_to_mode_reg (mode, op);
28853             }
28854           else
28855             {
28856               op = copy_to_reg (op);
28857               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
28858             }
28859         }
28860
28861       args[i].op = op;
28862       args[i].mode = mode;
28863     }
28864
28865   switch (nargs)
28866     {
28867     case 1:
28868       pat = GEN_FCN (icode) (real_target, args[0].op);
28869       break;
28870     case 2:
28871       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
28872       break;
28873     case 3:
28874       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
28875                              args[2].op);
28876       break;
28877     case 4:
28878       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
28879                              args[2].op, args[3].op);
28880       break;
28881     default:
28882       gcc_unreachable ();
28883     }
28884
28885   if (! pat)
28886     return 0;
28887
28888   emit_insn (pat);
28889   return target;
28890 }
28891
28892 /* Subroutine of ix86_expand_builtin to take care of special insns
28893    with variable number of operands.  */
28894
28895 static rtx
28896 ix86_expand_special_args_builtin (const struct builtin_description *d,
28897                                     tree exp, rtx target)
28898 {
28899   tree arg;
28900   rtx pat, op;
28901   unsigned int i, nargs, arg_adjust, memory;
28902   struct
28903     {
28904       rtx op;
28905       enum machine_mode mode;
28906     } args[3];
28907   enum insn_code icode = d->icode;
28908   bool last_arg_constant = false;
28909   const struct insn_data_d *insn_p = &insn_data[icode];
28910   enum machine_mode tmode = insn_p->operand[0].mode;
28911   enum { load, store } klass;
28912
28913   switch ((enum ix86_builtin_func_type) d->flag)
28914     {
28915     case VOID_FTYPE_VOID:
28916       if (icode == CODE_FOR_avx_vzeroupper)
28917         target = GEN_INT (vzeroupper_intrinsic);
28918       emit_insn (GEN_FCN (icode) (target));
28919       return 0;
28920     case VOID_FTYPE_UINT64:
28921     case VOID_FTYPE_UNSIGNED:
28922       nargs = 0;
28923       klass = store;
28924       memory = 0;
28925       break;
28926     case UINT64_FTYPE_VOID:
28927     case UNSIGNED_FTYPE_VOID:
28928       nargs = 0;
28929       klass = load;
28930       memory = 0;
28931       break;
28932     case UINT64_FTYPE_PUNSIGNED:
28933     case V2DI_FTYPE_PV2DI:
28934     case V4DI_FTYPE_PV4DI:
28935     case V32QI_FTYPE_PCCHAR:
28936     case V16QI_FTYPE_PCCHAR:
28937     case V8SF_FTYPE_PCV4SF:
28938     case V8SF_FTYPE_PCFLOAT:
28939     case V4SF_FTYPE_PCFLOAT:
28940     case V4DF_FTYPE_PCV2DF:
28941     case V4DF_FTYPE_PCDOUBLE:
28942     case V2DF_FTYPE_PCDOUBLE:
28943     case VOID_FTYPE_PVOID:
28944       nargs = 1;
28945       klass = load;
28946       memory = 0;
28947       break;
28948     case VOID_FTYPE_PV2SF_V4SF:
28949     case VOID_FTYPE_PV4DI_V4DI:
28950     case VOID_FTYPE_PV2DI_V2DI:
28951     case VOID_FTYPE_PCHAR_V32QI:
28952     case VOID_FTYPE_PCHAR_V16QI:
28953     case VOID_FTYPE_PFLOAT_V8SF:
28954     case VOID_FTYPE_PFLOAT_V4SF:
28955     case VOID_FTYPE_PDOUBLE_V4DF:
28956     case VOID_FTYPE_PDOUBLE_V2DF:
28957     case VOID_FTYPE_PLONGLONG_LONGLONG:
28958     case VOID_FTYPE_PULONGLONG_ULONGLONG:
28959     case VOID_FTYPE_PINT_INT:
28960       nargs = 1;
28961       klass = store;
28962       /* Reserve memory operand for target.  */
28963       memory = ARRAY_SIZE (args);
28964       break;
28965     case V4SF_FTYPE_V4SF_PCV2SF:
28966     case V2DF_FTYPE_V2DF_PCDOUBLE:
28967       nargs = 2;
28968       klass = load;
28969       memory = 1;
28970       break;
28971     case V8SF_FTYPE_PCV8SF_V8SI:
28972     case V4DF_FTYPE_PCV4DF_V4DI:
28973     case V4SF_FTYPE_PCV4SF_V4SI:
28974     case V2DF_FTYPE_PCV2DF_V2DI:
28975     case V8SI_FTYPE_PCV8SI_V8SI:
28976     case V4DI_FTYPE_PCV4DI_V4DI:
28977     case V4SI_FTYPE_PCV4SI_V4SI:
28978     case V2DI_FTYPE_PCV2DI_V2DI:
28979       nargs = 2;
28980       klass = load;
28981       memory = 0;
28982       break;
28983     case VOID_FTYPE_PV8SF_V8SI_V8SF:
28984     case VOID_FTYPE_PV4DF_V4DI_V4DF:
28985     case VOID_FTYPE_PV4SF_V4SI_V4SF:
28986     case VOID_FTYPE_PV2DF_V2DI_V2DF:
28987     case VOID_FTYPE_PV8SI_V8SI_V8SI:
28988     case VOID_FTYPE_PV4DI_V4DI_V4DI:
28989     case VOID_FTYPE_PV4SI_V4SI_V4SI:
28990     case VOID_FTYPE_PV2DI_V2DI_V2DI:
28991       nargs = 2;
28992       klass = store;
28993       /* Reserve memory operand for target.  */
28994       memory = ARRAY_SIZE (args);
28995       break;
28996     case VOID_FTYPE_UINT_UINT_UINT:
28997     case VOID_FTYPE_UINT64_UINT_UINT:
28998     case UCHAR_FTYPE_UINT_UINT_UINT:
28999     case UCHAR_FTYPE_UINT64_UINT_UINT:
29000       nargs = 3;
29001       klass = load;
29002       memory = ARRAY_SIZE (args);
29003       last_arg_constant = true;
29004       break;
29005     default:
29006       gcc_unreachable ();
29007     }
29008
29009   gcc_assert (nargs <= ARRAY_SIZE (args));
29010
29011   if (klass == store)
29012     {
29013       arg = CALL_EXPR_ARG (exp, 0);
29014       op = expand_normal (arg);
29015       gcc_assert (target == 0);
29016       if (memory)
29017         {
29018           if (GET_MODE (op) != Pmode)
29019             op = convert_to_mode (Pmode, op, 1);
29020           target = gen_rtx_MEM (tmode, force_reg (Pmode, op));
29021         }
29022       else
29023         target = force_reg (tmode, op);
29024       arg_adjust = 1;
29025     }
29026   else
29027     {
29028       arg_adjust = 0;
29029       if (optimize
29030           || target == 0
29031           || !register_operand (target, tmode)
29032           || GET_MODE (target) != tmode)
29033         target = gen_reg_rtx (tmode);
29034     }
29035
29036   for (i = 0; i < nargs; i++)
29037     {
29038       enum machine_mode mode = insn_p->operand[i + 1].mode;
29039       bool match;
29040
29041       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
29042       op = expand_normal (arg);
29043       match = insn_p->operand[i + 1].predicate (op, mode);
29044
29045       if (last_arg_constant && (i + 1) == nargs)
29046         {
29047           if (!match)
29048             {
29049               if (icode == CODE_FOR_lwp_lwpvalsi3
29050                   || icode == CODE_FOR_lwp_lwpinssi3
29051                   || icode == CODE_FOR_lwp_lwpvaldi3
29052                   || icode == CODE_FOR_lwp_lwpinsdi3)
29053                 error ("the last argument must be a 32-bit immediate");
29054               else
29055                 error ("the last argument must be an 8-bit immediate");
29056               return const0_rtx;
29057             }
29058         }
29059       else
29060         {
29061           if (i == memory)
29062             {
29063               /* This must be the memory operand.  */
29064               if (GET_MODE (op) != Pmode)
29065                 op = convert_to_mode (Pmode, op, 1);
29066               op = gen_rtx_MEM (mode, force_reg (Pmode, op));
29067               gcc_assert (GET_MODE (op) == mode
29068                           || GET_MODE (op) == VOIDmode);
29069             }
29070           else
29071             {
29072               /* This must be register.  */
29073               if (VECTOR_MODE_P (mode))
29074                 op = safe_vector_operand (op, mode);
29075
29076               gcc_assert (GET_MODE (op) == mode
29077                           || GET_MODE (op) == VOIDmode);
29078               op = copy_to_mode_reg (mode, op);
29079             }
29080         }
29081
29082       args[i].op = op;
29083       args[i].mode = mode;
29084     }
29085
29086   switch (nargs)
29087     {
29088     case 0:
29089       pat = GEN_FCN (icode) (target);
29090       break;
29091     case 1:
29092       pat = GEN_FCN (icode) (target, args[0].op);
29093       break;
29094     case 2:
29095       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
29096       break;
29097     case 3:
29098       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
29099       break;
29100     default:
29101       gcc_unreachable ();
29102     }
29103
29104   if (! pat)
29105     return 0;
29106   emit_insn (pat);
29107   return klass == store ? 0 : target;
29108 }
29109
29110 /* Return the integer constant in ARG.  Constrain it to be in the range
29111    of the subparts of VEC_TYPE; issue an error if not.  */
29112
29113 static int
29114 get_element_number (tree vec_type, tree arg)
29115 {
29116   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
29117
29118   if (!host_integerp (arg, 1)
29119       || (elt = tree_low_cst (arg, 1), elt > max))
29120     {
29121       error ("selector must be an integer constant in the range 0..%wi", max);
29122       return 0;
29123     }
29124
29125   return elt;
29126 }
29127
29128 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
29129    ix86_expand_vector_init.  We DO have language-level syntax for this, in
29130    the form of  (type){ init-list }.  Except that since we can't place emms
29131    instructions from inside the compiler, we can't allow the use of MMX
29132    registers unless the user explicitly asks for it.  So we do *not* define
29133    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
29134    we have builtins invoked by mmintrin.h that gives us license to emit
29135    these sorts of instructions.  */
29136
29137 static rtx
29138 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
29139 {
29140   enum machine_mode tmode = TYPE_MODE (type);
29141   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
29142   int i, n_elt = GET_MODE_NUNITS (tmode);
29143   rtvec v = rtvec_alloc (n_elt);
29144
29145   gcc_assert (VECTOR_MODE_P (tmode));
29146   gcc_assert (call_expr_nargs (exp) == n_elt);
29147
29148   for (i = 0; i < n_elt; ++i)
29149     {
29150       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
29151       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
29152     }
29153
29154   if (!target || !register_operand (target, tmode))
29155     target = gen_reg_rtx (tmode);
29156
29157   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
29158   return target;
29159 }
29160
29161 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
29162    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
29163    had a language-level syntax for referencing vector elements.  */
29164
29165 static rtx
29166 ix86_expand_vec_ext_builtin (tree exp, rtx target)
29167 {
29168   enum machine_mode tmode, mode0;
29169   tree arg0, arg1;
29170   int elt;
29171   rtx op0;
29172
29173   arg0 = CALL_EXPR_ARG (exp, 0);
29174   arg1 = CALL_EXPR_ARG (exp, 1);
29175
29176   op0 = expand_normal (arg0);
29177   elt = get_element_number (TREE_TYPE (arg0), arg1);
29178
29179   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
29180   mode0 = TYPE_MODE (TREE_TYPE (arg0));
29181   gcc_assert (VECTOR_MODE_P (mode0));
29182
29183   op0 = force_reg (mode0, op0);
29184
29185   if (optimize || !target || !register_operand (target, tmode))
29186     target = gen_reg_rtx (tmode);
29187
29188   ix86_expand_vector_extract (true, target, op0, elt);
29189
29190   return target;
29191 }
29192
29193 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
29194    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
29195    a language-level syntax for referencing vector elements.  */
29196
29197 static rtx
29198 ix86_expand_vec_set_builtin (tree exp)
29199 {
29200   enum machine_mode tmode, mode1;
29201   tree arg0, arg1, arg2;
29202   int elt;
29203   rtx op0, op1, target;
29204
29205   arg0 = CALL_EXPR_ARG (exp, 0);
29206   arg1 = CALL_EXPR_ARG (exp, 1);
29207   arg2 = CALL_EXPR_ARG (exp, 2);
29208
29209   tmode = TYPE_MODE (TREE_TYPE (arg0));
29210   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
29211   gcc_assert (VECTOR_MODE_P (tmode));
29212
29213   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
29214   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
29215   elt = get_element_number (TREE_TYPE (arg0), arg2);
29216
29217   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
29218     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
29219
29220   op0 = force_reg (tmode, op0);
29221   op1 = force_reg (mode1, op1);
29222
29223   /* OP0 is the source of these builtin functions and shouldn't be
29224      modified.  Create a copy, use it and return it as target.  */
29225   target = gen_reg_rtx (tmode);
29226   emit_move_insn (target, op0);
29227   ix86_expand_vector_set (true, target, op1, elt);
29228
29229   return target;
29230 }
29231
29232 /* Expand an expression EXP that calls a built-in function,
29233    with result going to TARGET if that's convenient
29234    (and in mode MODE if that's convenient).
29235    SUBTARGET may be used as the target for computing one of EXP's operands.
29236    IGNORE is nonzero if the value is to be ignored.  */
29237
29238 static rtx
29239 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
29240                      enum machine_mode mode ATTRIBUTE_UNUSED,
29241                      int ignore ATTRIBUTE_UNUSED)
29242 {
29243   const struct builtin_description *d;
29244   size_t i;
29245   enum insn_code icode;
29246   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
29247   tree arg0, arg1, arg2, arg3, arg4;
29248   rtx op0, op1, op2, op3, op4, pat;
29249   enum machine_mode mode0, mode1, mode2, mode3, mode4;
29250   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
29251
29252   /* Determine whether the builtin function is available under the current ISA.
29253      Originally the builtin was not created if it wasn't applicable to the
29254      current ISA based on the command line switches.  With function specific
29255      options, we need to check in the context of the function making the call
29256      whether it is supported.  */
29257   if (ix86_builtins_isa[fcode].isa
29258       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
29259     {
29260       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
29261                                        NULL, (enum fpmath_unit) 0, false);
29262
29263       if (!opts)
29264         error ("%qE needs unknown isa option", fndecl);
29265       else
29266         {
29267           gcc_assert (opts != NULL);
29268           error ("%qE needs isa option %s", fndecl, opts);
29269           free (opts);
29270         }
29271       return const0_rtx;
29272     }
29273
29274   switch (fcode)
29275     {
29276     case IX86_BUILTIN_MASKMOVQ:
29277     case IX86_BUILTIN_MASKMOVDQU:
29278       icode = (fcode == IX86_BUILTIN_MASKMOVQ
29279                ? CODE_FOR_mmx_maskmovq
29280                : CODE_FOR_sse2_maskmovdqu);
29281       /* Note the arg order is different from the operand order.  */
29282       arg1 = CALL_EXPR_ARG (exp, 0);
29283       arg2 = CALL_EXPR_ARG (exp, 1);
29284       arg0 = CALL_EXPR_ARG (exp, 2);
29285       op0 = expand_normal (arg0);
29286       op1 = expand_normal (arg1);
29287       op2 = expand_normal (arg2);
29288       mode0 = insn_data[icode].operand[0].mode;
29289       mode1 = insn_data[icode].operand[1].mode;
29290       mode2 = insn_data[icode].operand[2].mode;
29291
29292       if (GET_MODE (op0) != Pmode)
29293         op0 = convert_to_mode (Pmode, op0, 1);
29294       op0 = gen_rtx_MEM (mode1, force_reg (Pmode, op0));
29295
29296       if (!insn_data[icode].operand[0].predicate (op0, mode0))
29297         op0 = copy_to_mode_reg (mode0, op0);
29298       if (!insn_data[icode].operand[1].predicate (op1, mode1))
29299         op1 = copy_to_mode_reg (mode1, op1);
29300       if (!insn_data[icode].operand[2].predicate (op2, mode2))
29301         op2 = copy_to_mode_reg (mode2, op2);
29302       pat = GEN_FCN (icode) (op0, op1, op2);
29303       if (! pat)
29304         return 0;
29305       emit_insn (pat);
29306       return 0;
29307
29308     case IX86_BUILTIN_LDMXCSR:
29309       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
29310       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
29311       emit_move_insn (target, op0);
29312       emit_insn (gen_sse_ldmxcsr (target));
29313       return 0;
29314
29315     case IX86_BUILTIN_STMXCSR:
29316       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
29317       emit_insn (gen_sse_stmxcsr (target));
29318       return copy_to_mode_reg (SImode, target);
29319
29320     case IX86_BUILTIN_CLFLUSH:
29321         arg0 = CALL_EXPR_ARG (exp, 0);
29322         op0 = expand_normal (arg0);
29323         icode = CODE_FOR_sse2_clflush;
29324         if (!insn_data[icode].operand[0].predicate (op0, Pmode))
29325           {
29326             if (GET_MODE (op0) != Pmode)
29327               op0 = convert_to_mode (Pmode, op0, 1);
29328             op0 = force_reg (Pmode, op0);
29329           }
29330
29331         emit_insn (gen_sse2_clflush (op0));
29332         return 0;
29333
29334     case IX86_BUILTIN_MONITOR:
29335       arg0 = CALL_EXPR_ARG (exp, 0);
29336       arg1 = CALL_EXPR_ARG (exp, 1);
29337       arg2 = CALL_EXPR_ARG (exp, 2);
29338       op0 = expand_normal (arg0);
29339       op1 = expand_normal (arg1);
29340       op2 = expand_normal (arg2);
29341       if (!REG_P (op0))
29342         {
29343           if (GET_MODE (op0) != Pmode)
29344             op0 = convert_to_mode (Pmode, op0, 1);
29345           op0 = force_reg (Pmode, op0);
29346         }
29347       if (!REG_P (op1))
29348         op1 = copy_to_mode_reg (SImode, op1);
29349       if (!REG_P (op2))
29350         op2 = copy_to_mode_reg (SImode, op2);
29351       emit_insn (ix86_gen_monitor (op0, op1, op2));
29352       return 0;
29353
29354     case IX86_BUILTIN_MWAIT:
29355       arg0 = CALL_EXPR_ARG (exp, 0);
29356       arg1 = CALL_EXPR_ARG (exp, 1);
29357       op0 = expand_normal (arg0);
29358       op1 = expand_normal (arg1);
29359       if (!REG_P (op0))
29360         op0 = copy_to_mode_reg (SImode, op0);
29361       if (!REG_P (op1))
29362         op1 = copy_to_mode_reg (SImode, op1);
29363       emit_insn (gen_sse3_mwait (op0, op1));
29364       return 0;
29365
29366     case IX86_BUILTIN_VEC_INIT_V2SI:
29367     case IX86_BUILTIN_VEC_INIT_V4HI:
29368     case IX86_BUILTIN_VEC_INIT_V8QI:
29369       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
29370
29371     case IX86_BUILTIN_VEC_EXT_V2DF:
29372     case IX86_BUILTIN_VEC_EXT_V2DI:
29373     case IX86_BUILTIN_VEC_EXT_V4SF:
29374     case IX86_BUILTIN_VEC_EXT_V4SI:
29375     case IX86_BUILTIN_VEC_EXT_V8HI:
29376     case IX86_BUILTIN_VEC_EXT_V2SI:
29377     case IX86_BUILTIN_VEC_EXT_V4HI:
29378     case IX86_BUILTIN_VEC_EXT_V16QI:
29379       return ix86_expand_vec_ext_builtin (exp, target);
29380
29381     case IX86_BUILTIN_VEC_SET_V2DI:
29382     case IX86_BUILTIN_VEC_SET_V4SF:
29383     case IX86_BUILTIN_VEC_SET_V4SI:
29384     case IX86_BUILTIN_VEC_SET_V8HI:
29385     case IX86_BUILTIN_VEC_SET_V4HI:
29386     case IX86_BUILTIN_VEC_SET_V16QI:
29387       return ix86_expand_vec_set_builtin (exp);
29388
29389     case IX86_BUILTIN_INFQ:
29390     case IX86_BUILTIN_HUGE_VALQ:
29391       {
29392         REAL_VALUE_TYPE inf;
29393         rtx tmp;
29394
29395         real_inf (&inf);
29396         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
29397
29398         tmp = validize_mem (force_const_mem (mode, tmp));
29399
29400         if (target == 0)
29401           target = gen_reg_rtx (mode);
29402
29403         emit_move_insn (target, tmp);
29404         return target;
29405       }
29406
29407     case IX86_BUILTIN_LLWPCB:
29408       arg0 = CALL_EXPR_ARG (exp, 0);
29409       op0 = expand_normal (arg0);
29410       icode = CODE_FOR_lwp_llwpcb;
29411       if (!insn_data[icode].operand[0].predicate (op0, Pmode))
29412         {
29413           if (GET_MODE (op0) != Pmode)
29414             op0 = convert_to_mode (Pmode, op0, 1);
29415           op0 = force_reg (Pmode, op0);
29416         }
29417       emit_insn (gen_lwp_llwpcb (op0));
29418       return 0;
29419
29420     case IX86_BUILTIN_SLWPCB:
29421       icode = CODE_FOR_lwp_slwpcb;
29422       if (!target
29423           || !insn_data[icode].operand[0].predicate (target, Pmode))
29424         target = gen_reg_rtx (Pmode);
29425       emit_insn (gen_lwp_slwpcb (target));
29426       return target;
29427
29428     case IX86_BUILTIN_BEXTRI32:
29429     case IX86_BUILTIN_BEXTRI64:
29430       arg0 = CALL_EXPR_ARG (exp, 0);
29431       arg1 = CALL_EXPR_ARG (exp, 1);
29432       op0 = expand_normal (arg0);
29433       op1 = expand_normal (arg1);
29434       icode = (fcode == IX86_BUILTIN_BEXTRI32
29435           ? CODE_FOR_tbm_bextri_si
29436           : CODE_FOR_tbm_bextri_di);
29437       if (!CONST_INT_P (op1))
29438         {
29439           error ("last argument must be an immediate");
29440           return const0_rtx;
29441         }
29442       else
29443         {
29444           unsigned char length = (INTVAL (op1) >> 8) & 0xFF;
29445           unsigned char lsb_index = INTVAL (op1) & 0xFF;
29446           op1 = GEN_INT (length);
29447           op2 = GEN_INT (lsb_index);
29448           pat = GEN_FCN (icode) (target, op0, op1, op2);
29449           if (pat)
29450             emit_insn (pat);
29451           return target;
29452         }
29453
29454     case IX86_BUILTIN_RDRAND16_STEP:
29455       icode = CODE_FOR_rdrandhi_1;
29456       mode0 = HImode;
29457       goto rdrand_step;
29458
29459     case IX86_BUILTIN_RDRAND32_STEP:
29460       icode = CODE_FOR_rdrandsi_1;
29461       mode0 = SImode;
29462       goto rdrand_step;
29463
29464     case IX86_BUILTIN_RDRAND64_STEP:
29465       icode = CODE_FOR_rdranddi_1;
29466       mode0 = DImode;
29467
29468 rdrand_step:
29469       op0 = gen_reg_rtx (mode0);
29470       emit_insn (GEN_FCN (icode) (op0));
29471
29472       arg0 = CALL_EXPR_ARG (exp, 0);
29473       op1 = expand_normal (arg0);
29474       if (!address_operand (op1, VOIDmode))
29475         {
29476           op1 = convert_memory_address (Pmode, op1);
29477           op1 = copy_addr_to_reg (op1);
29478         }
29479       emit_move_insn (gen_rtx_MEM (mode0, op1), op0);
29480
29481       op1 = gen_reg_rtx (SImode);
29482       emit_move_insn (op1, CONST1_RTX (SImode));
29483
29484       /* Emit SImode conditional move.  */
29485       if (mode0 == HImode)
29486         {
29487           op2 = gen_reg_rtx (SImode);
29488           emit_insn (gen_zero_extendhisi2 (op2, op0));
29489         }
29490       else if (mode0 == SImode)
29491         op2 = op0;
29492       else
29493         op2 = gen_rtx_SUBREG (SImode, op0, 0);
29494
29495       if (target == 0)
29496         target = gen_reg_rtx (SImode);
29497
29498       pat = gen_rtx_GEU (VOIDmode, gen_rtx_REG (CCCmode, FLAGS_REG),
29499                          const0_rtx);
29500       emit_insn (gen_rtx_SET (VOIDmode, target,
29501                               gen_rtx_IF_THEN_ELSE (SImode, pat, op2, op1)));
29502       return target;
29503
29504     case IX86_BUILTIN_GATHERSIV2DF:
29505       icode = CODE_FOR_avx2_gathersiv2df;
29506       goto gather_gen;
29507     case IX86_BUILTIN_GATHERSIV4DF:
29508       icode = CODE_FOR_avx2_gathersiv4df;
29509       goto gather_gen;
29510     case IX86_BUILTIN_GATHERDIV2DF:
29511       icode = CODE_FOR_avx2_gatherdiv2df;
29512       goto gather_gen;
29513     case IX86_BUILTIN_GATHERDIV4DF:
29514       icode = CODE_FOR_avx2_gatherdiv4df;
29515       goto gather_gen;
29516     case IX86_BUILTIN_GATHERSIV4SF:
29517       icode = CODE_FOR_avx2_gathersiv4sf;
29518       goto gather_gen;
29519     case IX86_BUILTIN_GATHERSIV8SF:
29520       icode = CODE_FOR_avx2_gathersiv8sf;
29521       goto gather_gen;
29522     case IX86_BUILTIN_GATHERDIV4SF:
29523       icode = CODE_FOR_avx2_gatherdiv4sf;
29524       goto gather_gen;
29525     case IX86_BUILTIN_GATHERDIV8SF:
29526       icode = CODE_FOR_avx2_gatherdiv8sf;
29527       goto gather_gen;
29528     case IX86_BUILTIN_GATHERSIV2DI:
29529       icode = CODE_FOR_avx2_gathersiv2di;
29530       goto gather_gen;
29531     case IX86_BUILTIN_GATHERSIV4DI:
29532       icode = CODE_FOR_avx2_gathersiv4di;
29533       goto gather_gen;
29534     case IX86_BUILTIN_GATHERDIV2DI:
29535       icode = CODE_FOR_avx2_gatherdiv2di;
29536       goto gather_gen;
29537     case IX86_BUILTIN_GATHERDIV4DI:
29538       icode = CODE_FOR_avx2_gatherdiv4di;
29539       goto gather_gen;
29540     case IX86_BUILTIN_GATHERSIV4SI:
29541       icode = CODE_FOR_avx2_gathersiv4si;
29542       goto gather_gen;
29543     case IX86_BUILTIN_GATHERSIV8SI:
29544       icode = CODE_FOR_avx2_gathersiv8si;
29545       goto gather_gen;
29546     case IX86_BUILTIN_GATHERDIV4SI:
29547       icode = CODE_FOR_avx2_gatherdiv4si;
29548       goto gather_gen;
29549     case IX86_BUILTIN_GATHERDIV8SI:
29550       icode = CODE_FOR_avx2_gatherdiv8si;
29551       goto gather_gen;
29552     case IX86_BUILTIN_GATHERALTSIV4DF:
29553       icode = CODE_FOR_avx2_gathersiv4df;
29554       goto gather_gen;
29555     case IX86_BUILTIN_GATHERALTDIV8SF:
29556       icode = CODE_FOR_avx2_gatherdiv8sf;
29557       goto gather_gen;
29558     case IX86_BUILTIN_GATHERALTSIV4DI:
29559       icode = CODE_FOR_avx2_gathersiv4di;
29560       goto gather_gen;
29561     case IX86_BUILTIN_GATHERALTDIV8SI:
29562       icode = CODE_FOR_avx2_gatherdiv8si;
29563       goto gather_gen;
29564
29565     gather_gen:
29566       arg0 = CALL_EXPR_ARG (exp, 0);
29567       arg1 = CALL_EXPR_ARG (exp, 1);
29568       arg2 = CALL_EXPR_ARG (exp, 2);
29569       arg3 = CALL_EXPR_ARG (exp, 3);
29570       arg4 = CALL_EXPR_ARG (exp, 4);
29571       op0 = expand_normal (arg0);
29572       op1 = expand_normal (arg1);
29573       op2 = expand_normal (arg2);
29574       op3 = expand_normal (arg3);
29575       op4 = expand_normal (arg4);
29576       /* Note the arg order is different from the operand order.  */
29577       mode0 = insn_data[icode].operand[1].mode;
29578       mode2 = insn_data[icode].operand[3].mode;
29579       mode3 = insn_data[icode].operand[4].mode;
29580       mode4 = insn_data[icode].operand[5].mode;
29581
29582       if (target == NULL_RTX
29583           || GET_MODE (target) != insn_data[icode].operand[0].mode)
29584         subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
29585       else
29586         subtarget = target;
29587
29588       if (fcode == IX86_BUILTIN_GATHERALTSIV4DF
29589           || fcode == IX86_BUILTIN_GATHERALTSIV4DI)
29590         {
29591           rtx half = gen_reg_rtx (V4SImode);
29592           if (!nonimmediate_operand (op2, V8SImode))
29593             op2 = copy_to_mode_reg (V8SImode, op2);
29594           emit_insn (gen_vec_extract_lo_v8si (half, op2));
29595           op2 = half;
29596         }
29597       else if (fcode == IX86_BUILTIN_GATHERALTDIV8SF
29598                || fcode == IX86_BUILTIN_GATHERALTDIV8SI)
29599         {
29600           rtx (*gen) (rtx, rtx);
29601           rtx half = gen_reg_rtx (mode0);
29602           if (mode0 == V4SFmode)
29603             gen = gen_vec_extract_lo_v8sf;
29604           else
29605             gen = gen_vec_extract_lo_v8si;
29606           if (!nonimmediate_operand (op0, GET_MODE (op0)))
29607             op0 = copy_to_mode_reg (GET_MODE (op0), op0);
29608           emit_insn (gen (half, op0));
29609           op0 = half;
29610           if (!nonimmediate_operand (op3, GET_MODE (op3)))
29611             op3 = copy_to_mode_reg (GET_MODE (op3), op3);
29612           emit_insn (gen (half, op3));
29613           op3 = half;
29614         }
29615
29616       /* Force memory operand only with base register here.  But we
29617          don't want to do it on memory operand for other builtin
29618          functions.  */
29619       if (GET_MODE (op1) != Pmode)
29620         op1 = convert_to_mode (Pmode, op1, 1);
29621       op1 = force_reg (Pmode, op1);
29622
29623       if (!insn_data[icode].operand[1].predicate (op0, mode0))
29624         op0 = copy_to_mode_reg (mode0, op0);
29625       if (!insn_data[icode].operand[2].predicate (op1, Pmode))
29626         op1 = copy_to_mode_reg (Pmode, op1);
29627       if (!insn_data[icode].operand[3].predicate (op2, mode2))
29628         op2 = copy_to_mode_reg (mode2, op2);
29629       if (!insn_data[icode].operand[4].predicate (op3, mode3))
29630         op3 = copy_to_mode_reg (mode3, op3);
29631       if (!insn_data[icode].operand[5].predicate (op4, mode4))
29632         {
29633           error ("last argument must be scale 1, 2, 4, 8");
29634           return const0_rtx;
29635         }
29636
29637       /* Optimize.  If mask is known to have all high bits set,
29638          replace op0 with pc_rtx to signal that the instruction
29639          overwrites the whole destination and doesn't use its
29640          previous contents.  */
29641       if (optimize)
29642         {
29643           if (TREE_CODE (arg3) == VECTOR_CST)
29644             {
29645               tree elt;
29646               unsigned int negative = 0;
29647               for (elt = TREE_VECTOR_CST_ELTS (arg3);
29648                    elt; elt = TREE_CHAIN (elt))
29649                 {
29650                   tree cst = TREE_VALUE (elt);
29651                   if (TREE_CODE (cst) == INTEGER_CST
29652                       && tree_int_cst_sign_bit (cst))
29653                     negative++;
29654                   else if (TREE_CODE (cst) == REAL_CST
29655                            && REAL_VALUE_NEGATIVE (TREE_REAL_CST (cst)))
29656                     negative++;
29657                 }
29658               if (negative == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg3)))
29659                 op0 = pc_rtx;
29660             }
29661           else if (TREE_CODE (arg3) == SSA_NAME)
29662             {
29663               /* Recognize also when mask is like:
29664                  __v2df src = _mm_setzero_pd ();
29665                  __v2df mask = _mm_cmpeq_pd (src, src);
29666                  or
29667                  __v8sf src = _mm256_setzero_ps ();
29668                  __v8sf mask = _mm256_cmp_ps (src, src, _CMP_EQ_OQ);
29669                  as that is a cheaper way to load all ones into
29670                  a register than having to load a constant from
29671                  memory.  */
29672               gimple def_stmt = SSA_NAME_DEF_STMT (arg3);
29673               if (is_gimple_call (def_stmt))
29674                 {
29675                   tree fndecl = gimple_call_fndecl (def_stmt);
29676                   if (fndecl
29677                       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
29678                     switch ((unsigned int) DECL_FUNCTION_CODE (fndecl))
29679                       {
29680                       case IX86_BUILTIN_CMPPD:
29681                       case IX86_BUILTIN_CMPPS:
29682                       case IX86_BUILTIN_CMPPD256:
29683                       case IX86_BUILTIN_CMPPS256:
29684                         if (!integer_zerop (gimple_call_arg (def_stmt, 2)))
29685                           break;
29686                         /* FALLTHRU */
29687                       case IX86_BUILTIN_CMPEQPD:
29688                       case IX86_BUILTIN_CMPEQPS:
29689                         if (initializer_zerop (gimple_call_arg (def_stmt, 0))
29690                             && initializer_zerop (gimple_call_arg (def_stmt,
29691                                                                    1)))
29692                           op0 = pc_rtx;
29693                         break;
29694                       default:
29695                         break;
29696                       }
29697                 }
29698             }
29699         }
29700
29701       pat = GEN_FCN (icode) (subtarget, op0, op1, op2, op3, op4);
29702       if (! pat)
29703         return const0_rtx;
29704       emit_insn (pat);
29705
29706       if (fcode == IX86_BUILTIN_GATHERDIV8SF
29707           || fcode == IX86_BUILTIN_GATHERDIV8SI)
29708         {
29709           enum machine_mode tmode = GET_MODE (subtarget) == V8SFmode
29710                                     ? V4SFmode : V4SImode;
29711           if (target == NULL_RTX)
29712             target = gen_reg_rtx (tmode);
29713           if (tmode == V4SFmode)
29714             emit_insn (gen_vec_extract_lo_v8sf (target, subtarget));
29715           else
29716             emit_insn (gen_vec_extract_lo_v8si (target, subtarget));
29717         }
29718       else
29719         target = subtarget;
29720
29721       return target;
29722
29723     default:
29724       break;
29725     }
29726
29727   for (i = 0, d = bdesc_special_args;
29728        i < ARRAY_SIZE (bdesc_special_args);
29729        i++, d++)
29730     if (d->code == fcode)
29731       return ix86_expand_special_args_builtin (d, exp, target);
29732
29733   for (i = 0, d = bdesc_args;
29734        i < ARRAY_SIZE (bdesc_args);
29735        i++, d++)
29736     if (d->code == fcode)
29737       switch (fcode)
29738         {
29739         case IX86_BUILTIN_FABSQ:
29740         case IX86_BUILTIN_COPYSIGNQ:
29741           if (!TARGET_SSE2)
29742             /* Emit a normal call if SSE2 isn't available.  */
29743             return expand_call (exp, target, ignore);
29744         default:
29745           return ix86_expand_args_builtin (d, exp, target);
29746         }
29747
29748   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
29749     if (d->code == fcode)
29750       return ix86_expand_sse_comi (d, exp, target);
29751
29752   for (i = 0, d = bdesc_pcmpestr;
29753        i < ARRAY_SIZE (bdesc_pcmpestr);
29754        i++, d++)
29755     if (d->code == fcode)
29756       return ix86_expand_sse_pcmpestr (d, exp, target);
29757
29758   for (i = 0, d = bdesc_pcmpistr;
29759        i < ARRAY_SIZE (bdesc_pcmpistr);
29760        i++, d++)
29761     if (d->code == fcode)
29762       return ix86_expand_sse_pcmpistr (d, exp, target);
29763
29764   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
29765     if (d->code == fcode)
29766       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
29767                                             (enum ix86_builtin_func_type)
29768                                             d->flag, d->comparison);
29769
29770   gcc_unreachable ();
29771 }
29772
29773 /* Returns a function decl for a vectorized version of the builtin function
29774    with builtin function code FN and the result vector type TYPE, or NULL_TREE
29775    if it is not available.  */
29776
29777 static tree
29778 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
29779                                   tree type_in)
29780 {
29781   enum machine_mode in_mode, out_mode;
29782   int in_n, out_n;
29783   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
29784
29785   if (TREE_CODE (type_out) != VECTOR_TYPE
29786       || TREE_CODE (type_in) != VECTOR_TYPE
29787       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
29788     return NULL_TREE;
29789
29790   out_mode = TYPE_MODE (TREE_TYPE (type_out));
29791   out_n = TYPE_VECTOR_SUBPARTS (type_out);
29792   in_mode = TYPE_MODE (TREE_TYPE (type_in));
29793   in_n = TYPE_VECTOR_SUBPARTS (type_in);
29794
29795   switch (fn)
29796     {
29797     case BUILT_IN_SQRT:
29798       if (out_mode == DFmode && in_mode == DFmode)
29799         {
29800           if (out_n == 2 && in_n == 2)
29801             return ix86_builtins[IX86_BUILTIN_SQRTPD];
29802           else if (out_n == 4 && in_n == 4)
29803             return ix86_builtins[IX86_BUILTIN_SQRTPD256];
29804         }
29805       break;
29806
29807     case BUILT_IN_SQRTF:
29808       if (out_mode == SFmode && in_mode == SFmode)
29809         {
29810           if (out_n == 4 && in_n == 4)
29811             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
29812           else if (out_n == 8 && in_n == 8)
29813             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR256];
29814         }
29815       break;
29816
29817     case BUILT_IN_IFLOOR:
29818     case BUILT_IN_LFLOOR:
29819     case BUILT_IN_LLFLOOR:
29820       /* The round insn does not trap on denormals.  */
29821       if (flag_trapping_math || !TARGET_ROUND)
29822         break;
29823
29824       if (out_mode == SImode && in_mode == DFmode)
29825         {
29826           if (out_n == 4 && in_n == 2)
29827             return ix86_builtins[IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX];
29828           else if (out_n == 8 && in_n == 4)
29829             return ix86_builtins[IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX256];
29830         }
29831       break;
29832
29833     case BUILT_IN_IFLOORF:
29834     case BUILT_IN_LFLOORF:
29835     case BUILT_IN_LLFLOORF:
29836       /* The round insn does not trap on denormals.  */
29837       if (flag_trapping_math || !TARGET_ROUND)
29838         break;
29839
29840       if (out_mode == SImode && in_mode == SFmode)
29841         {
29842           if (out_n == 4 && in_n == 4)
29843             return ix86_builtins[IX86_BUILTIN_FLOORPS_SFIX];
29844           else if (out_n == 8 && in_n == 8)
29845             return ix86_builtins[IX86_BUILTIN_FLOORPS_SFIX256];
29846         }
29847       break;
29848
29849     case BUILT_IN_ICEIL:
29850     case BUILT_IN_LCEIL:
29851     case BUILT_IN_LLCEIL:
29852       /* The round insn does not trap on denormals.  */
29853       if (flag_trapping_math || !TARGET_ROUND)
29854         break;
29855
29856       if (out_mode == SImode && in_mode == DFmode)
29857         {
29858           if (out_n == 4 && in_n == 2)
29859             return ix86_builtins[IX86_BUILTIN_CEILPD_VEC_PACK_SFIX];
29860           else if (out_n == 8 && in_n == 4)
29861             return ix86_builtins[IX86_BUILTIN_CEILPD_VEC_PACK_SFIX256];
29862         }
29863       break;
29864
29865     case BUILT_IN_ICEILF:
29866     case BUILT_IN_LCEILF:
29867     case BUILT_IN_LLCEILF:
29868       /* The round insn does not trap on denormals.  */
29869       if (flag_trapping_math || !TARGET_ROUND)
29870         break;
29871
29872       if (out_mode == SImode && in_mode == SFmode)
29873         {
29874           if (out_n == 4 && in_n == 4)
29875             return ix86_builtins[IX86_BUILTIN_CEILPS_SFIX];
29876           else if (out_n == 8 && in_n == 8)
29877             return ix86_builtins[IX86_BUILTIN_CEILPS_SFIX256];
29878         }
29879       break;
29880
29881     case BUILT_IN_IRINT:
29882     case BUILT_IN_LRINT:
29883     case BUILT_IN_LLRINT:
29884       if (out_mode == SImode && in_mode == DFmode)
29885         {
29886           if (out_n == 4 && in_n == 2)
29887             return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
29888           else if (out_n == 8 && in_n == 4)
29889             return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX256];
29890         }
29891       break;
29892
29893     case BUILT_IN_IRINTF:
29894     case BUILT_IN_LRINTF:
29895     case BUILT_IN_LLRINTF:
29896       if (out_mode == SImode && in_mode == SFmode)
29897         {
29898           if (out_n == 4 && in_n == 4)
29899             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
29900           else if (out_n == 8 && in_n == 8)
29901             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ256];
29902         }
29903       break;
29904
29905     case BUILT_IN_IROUND:
29906     case BUILT_IN_LROUND:
29907     case BUILT_IN_LLROUND:
29908       /* The round insn does not trap on denormals.  */
29909       if (flag_trapping_math || !TARGET_ROUND)
29910         break;
29911
29912       if (out_mode == SImode && in_mode == DFmode)
29913         {
29914           if (out_n == 4 && in_n == 2)
29915             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX];
29916           else if (out_n == 8 && in_n == 4)
29917             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX256];
29918         }
29919       break;
29920
29921     case BUILT_IN_IROUNDF:
29922     case BUILT_IN_LROUNDF:
29923     case BUILT_IN_LLROUNDF:
29924       /* The round insn does not trap on denormals.  */
29925       if (flag_trapping_math || !TARGET_ROUND)
29926         break;
29927
29928       if (out_mode == SImode && in_mode == SFmode)
29929         {
29930           if (out_n == 4 && in_n == 4)
29931             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ_SFIX];
29932           else if (out_n == 8 && in_n == 8)
29933             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ_SFIX256];
29934         }
29935       break;
29936
29937     case BUILT_IN_COPYSIGN:
29938       if (out_mode == DFmode && in_mode == DFmode)
29939         {
29940           if (out_n == 2 && in_n == 2)
29941             return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
29942           else if (out_n == 4 && in_n == 4)
29943             return ix86_builtins[IX86_BUILTIN_CPYSGNPD256];
29944         }
29945       break;
29946
29947     case BUILT_IN_COPYSIGNF:
29948       if (out_mode == SFmode && in_mode == SFmode)
29949         {
29950           if (out_n == 4 && in_n == 4)
29951             return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
29952           else if (out_n == 8 && in_n == 8)
29953             return ix86_builtins[IX86_BUILTIN_CPYSGNPS256];
29954         }
29955       break;
29956
29957     case BUILT_IN_FLOOR:
29958       /* The round insn does not trap on denormals.  */
29959       if (flag_trapping_math || !TARGET_ROUND)
29960         break;
29961
29962       if (out_mode == DFmode && in_mode == DFmode)
29963         {
29964           if (out_n == 2 && in_n == 2)
29965             return ix86_builtins[IX86_BUILTIN_FLOORPD];
29966           else if (out_n == 4 && in_n == 4)
29967             return ix86_builtins[IX86_BUILTIN_FLOORPD256];
29968         }
29969       break;
29970
29971     case BUILT_IN_FLOORF:
29972       /* The round insn does not trap on denormals.  */
29973       if (flag_trapping_math || !TARGET_ROUND)
29974         break;
29975
29976       if (out_mode == SFmode && in_mode == SFmode)
29977         {
29978           if (out_n == 4 && in_n == 4)
29979             return ix86_builtins[IX86_BUILTIN_FLOORPS];
29980           else if (out_n == 8 && in_n == 8)
29981             return ix86_builtins[IX86_BUILTIN_FLOORPS256];
29982         }
29983       break;
29984
29985     case BUILT_IN_CEIL:
29986       /* The round insn does not trap on denormals.  */
29987       if (flag_trapping_math || !TARGET_ROUND)
29988         break;
29989
29990       if (out_mode == DFmode && in_mode == DFmode)
29991         {
29992           if (out_n == 2 && in_n == 2)
29993             return ix86_builtins[IX86_BUILTIN_CEILPD];
29994           else if (out_n == 4 && in_n == 4)
29995             return ix86_builtins[IX86_BUILTIN_CEILPD256];
29996         }
29997       break;
29998
29999     case BUILT_IN_CEILF:
30000       /* The round insn does not trap on denormals.  */
30001       if (flag_trapping_math || !TARGET_ROUND)
30002         break;
30003
30004       if (out_mode == SFmode && in_mode == SFmode)
30005         {
30006           if (out_n == 4 && in_n == 4)
30007             return ix86_builtins[IX86_BUILTIN_CEILPS];
30008           else if (out_n == 8 && in_n == 8)
30009             return ix86_builtins[IX86_BUILTIN_CEILPS256];
30010         }
30011       break;
30012
30013     case BUILT_IN_TRUNC:
30014       /* The round insn does not trap on denormals.  */
30015       if (flag_trapping_math || !TARGET_ROUND)
30016         break;
30017
30018       if (out_mode == DFmode && in_mode == DFmode)
30019         {
30020           if (out_n == 2 && in_n == 2)
30021             return ix86_builtins[IX86_BUILTIN_TRUNCPD];
30022           else if (out_n == 4 && in_n == 4)
30023             return ix86_builtins[IX86_BUILTIN_TRUNCPD256];
30024         }
30025       break;
30026
30027     case BUILT_IN_TRUNCF:
30028       /* The round insn does not trap on denormals.  */
30029       if (flag_trapping_math || !TARGET_ROUND)
30030         break;
30031
30032       if (out_mode == SFmode && in_mode == SFmode)
30033         {
30034           if (out_n == 4 && in_n == 4)
30035             return ix86_builtins[IX86_BUILTIN_TRUNCPS];
30036           else if (out_n == 8 && in_n == 8)
30037             return ix86_builtins[IX86_BUILTIN_TRUNCPS256];
30038         }
30039       break;
30040
30041     case BUILT_IN_RINT:
30042       /* The round insn does not trap on denormals.  */
30043       if (flag_trapping_math || !TARGET_ROUND)
30044         break;
30045
30046       if (out_mode == DFmode && in_mode == DFmode)
30047         {
30048           if (out_n == 2 && in_n == 2)
30049             return ix86_builtins[IX86_BUILTIN_RINTPD];
30050           else if (out_n == 4 && in_n == 4)
30051             return ix86_builtins[IX86_BUILTIN_RINTPD256];
30052         }
30053       break;
30054
30055     case BUILT_IN_RINTF:
30056       /* The round insn does not trap on denormals.  */
30057       if (flag_trapping_math || !TARGET_ROUND)
30058         break;
30059
30060       if (out_mode == SFmode && in_mode == SFmode)
30061         {
30062           if (out_n == 4 && in_n == 4)
30063             return ix86_builtins[IX86_BUILTIN_RINTPS];
30064           else if (out_n == 8 && in_n == 8)
30065             return ix86_builtins[IX86_BUILTIN_RINTPS256];
30066         }
30067       break;
30068
30069     case BUILT_IN_ROUND:
30070       /* The round insn does not trap on denormals.  */
30071       if (flag_trapping_math || !TARGET_ROUND)
30072         break;
30073
30074       if (out_mode == DFmode && in_mode == DFmode)
30075         {
30076           if (out_n == 2 && in_n == 2)
30077             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ];
30078           else if (out_n == 4 && in_n == 4)
30079             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ256];
30080         }
30081       break;
30082
30083     case BUILT_IN_ROUNDF:
30084       /* The round insn does not trap on denormals.  */
30085       if (flag_trapping_math || !TARGET_ROUND)
30086         break;
30087
30088       if (out_mode == SFmode && in_mode == SFmode)
30089         {
30090           if (out_n == 4 && in_n == 4)
30091             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ];
30092           else if (out_n == 8 && in_n == 8)
30093             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ256];
30094         }
30095       break;
30096
30097     case BUILT_IN_FMA:
30098       if (out_mode == DFmode && in_mode == DFmode)
30099         {
30100           if (out_n == 2 && in_n == 2)
30101             return ix86_builtins[IX86_BUILTIN_VFMADDPD];
30102           if (out_n == 4 && in_n == 4)
30103             return ix86_builtins[IX86_BUILTIN_VFMADDPD256];
30104         }
30105       break;
30106
30107     case BUILT_IN_FMAF:
30108       if (out_mode == SFmode && in_mode == SFmode)
30109         {
30110           if (out_n == 4 && in_n == 4)
30111             return ix86_builtins[IX86_BUILTIN_VFMADDPS];
30112           if (out_n == 8 && in_n == 8)
30113             return ix86_builtins[IX86_BUILTIN_VFMADDPS256];
30114         }
30115       break;
30116
30117     default:
30118       break;
30119     }
30120
30121   /* Dispatch to a handler for a vectorization library.  */
30122   if (ix86_veclib_handler)
30123     return ix86_veclib_handler ((enum built_in_function) fn, type_out,
30124                                 type_in);
30125
30126   return NULL_TREE;
30127 }
30128
30129 /* Handler for an SVML-style interface to
30130    a library with vectorized intrinsics.  */
30131
30132 static tree
30133 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
30134 {
30135   char name[20];
30136   tree fntype, new_fndecl, args;
30137   unsigned arity;
30138   const char *bname;
30139   enum machine_mode el_mode, in_mode;
30140   int n, in_n;
30141
30142   /* The SVML is suitable for unsafe math only.  */
30143   if (!flag_unsafe_math_optimizations)
30144     return NULL_TREE;
30145
30146   el_mode = TYPE_MODE (TREE_TYPE (type_out));
30147   n = TYPE_VECTOR_SUBPARTS (type_out);
30148   in_mode = TYPE_MODE (TREE_TYPE (type_in));
30149   in_n = TYPE_VECTOR_SUBPARTS (type_in);
30150   if (el_mode != in_mode
30151       || n != in_n)
30152     return NULL_TREE;
30153
30154   switch (fn)
30155     {
30156     case BUILT_IN_EXP:
30157     case BUILT_IN_LOG:
30158     case BUILT_IN_LOG10:
30159     case BUILT_IN_POW:
30160     case BUILT_IN_TANH:
30161     case BUILT_IN_TAN:
30162     case BUILT_IN_ATAN:
30163     case BUILT_IN_ATAN2:
30164     case BUILT_IN_ATANH:
30165     case BUILT_IN_CBRT:
30166     case BUILT_IN_SINH:
30167     case BUILT_IN_SIN:
30168     case BUILT_IN_ASINH:
30169     case BUILT_IN_ASIN:
30170     case BUILT_IN_COSH:
30171     case BUILT_IN_COS:
30172     case BUILT_IN_ACOSH:
30173     case BUILT_IN_ACOS:
30174       if (el_mode != DFmode || n != 2)
30175         return NULL_TREE;
30176       break;
30177
30178     case BUILT_IN_EXPF:
30179     case BUILT_IN_LOGF:
30180     case BUILT_IN_LOG10F:
30181     case BUILT_IN_POWF:
30182     case BUILT_IN_TANHF:
30183     case BUILT_IN_TANF:
30184     case BUILT_IN_ATANF:
30185     case BUILT_IN_ATAN2F:
30186     case BUILT_IN_ATANHF:
30187     case BUILT_IN_CBRTF:
30188     case BUILT_IN_SINHF:
30189     case BUILT_IN_SINF:
30190     case BUILT_IN_ASINHF:
30191     case BUILT_IN_ASINF:
30192     case BUILT_IN_COSHF:
30193     case BUILT_IN_COSF:
30194     case BUILT_IN_ACOSHF:
30195     case BUILT_IN_ACOSF:
30196       if (el_mode != SFmode || n != 4)
30197         return NULL_TREE;
30198       break;
30199
30200     default:
30201       return NULL_TREE;
30202     }
30203
30204   bname = IDENTIFIER_POINTER (DECL_NAME (builtin_decl_implicit (fn)));
30205
30206   if (fn == BUILT_IN_LOGF)
30207     strcpy (name, "vmlsLn4");
30208   else if (fn == BUILT_IN_LOG)
30209     strcpy (name, "vmldLn2");
30210   else if (n == 4)
30211     {
30212       sprintf (name, "vmls%s", bname+10);
30213       name[strlen (name)-1] = '4';
30214     }
30215   else
30216     sprintf (name, "vmld%s2", bname+10);
30217
30218   /* Convert to uppercase. */
30219   name[4] &= ~0x20;
30220
30221   arity = 0;
30222   for (args = DECL_ARGUMENTS (builtin_decl_implicit (fn));
30223        args;
30224        args = TREE_CHAIN (args))
30225     arity++;
30226
30227   if (arity == 1)
30228     fntype = build_function_type_list (type_out, type_in, NULL);
30229   else
30230     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
30231
30232   /* Build a function declaration for the vectorized function.  */
30233   new_fndecl = build_decl (BUILTINS_LOCATION,
30234                            FUNCTION_DECL, get_identifier (name), fntype);
30235   TREE_PUBLIC (new_fndecl) = 1;
30236   DECL_EXTERNAL (new_fndecl) = 1;
30237   DECL_IS_NOVOPS (new_fndecl) = 1;
30238   TREE_READONLY (new_fndecl) = 1;
30239
30240   return new_fndecl;
30241 }
30242
30243 /* Handler for an ACML-style interface to
30244    a library with vectorized intrinsics.  */
30245
30246 static tree
30247 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
30248 {
30249   char name[20] = "__vr.._";
30250   tree fntype, new_fndecl, args;
30251   unsigned arity;
30252   const char *bname;
30253   enum machine_mode el_mode, in_mode;
30254   int n, in_n;
30255
30256   /* The ACML is 64bits only and suitable for unsafe math only as
30257      it does not correctly support parts of IEEE with the required
30258      precision such as denormals.  */
30259   if (!TARGET_64BIT
30260       || !flag_unsafe_math_optimizations)
30261     return NULL_TREE;
30262
30263   el_mode = TYPE_MODE (TREE_TYPE (type_out));
30264   n = TYPE_VECTOR_SUBPARTS (type_out);
30265   in_mode = TYPE_MODE (TREE_TYPE (type_in));
30266   in_n = TYPE_VECTOR_SUBPARTS (type_in);
30267   if (el_mode != in_mode
30268       || n != in_n)
30269     return NULL_TREE;
30270
30271   switch (fn)
30272     {
30273     case BUILT_IN_SIN:
30274     case BUILT_IN_COS:
30275     case BUILT_IN_EXP:
30276     case BUILT_IN_LOG:
30277     case BUILT_IN_LOG2:
30278     case BUILT_IN_LOG10:
30279       name[4] = 'd';
30280       name[5] = '2';
30281       if (el_mode != DFmode
30282           || n != 2)
30283         return NULL_TREE;
30284       break;
30285
30286     case BUILT_IN_SINF:
30287     case BUILT_IN_COSF:
30288     case BUILT_IN_EXPF:
30289     case BUILT_IN_POWF:
30290     case BUILT_IN_LOGF:
30291     case BUILT_IN_LOG2F:
30292     case BUILT_IN_LOG10F:
30293       name[4] = 's';
30294       name[5] = '4';
30295       if (el_mode != SFmode
30296           || n != 4)
30297         return NULL_TREE;
30298       break;
30299
30300     default:
30301       return NULL_TREE;
30302     }
30303
30304   bname = IDENTIFIER_POINTER (DECL_NAME (builtin_decl_implicit (fn)));
30305   sprintf (name + 7, "%s", bname+10);
30306
30307   arity = 0;
30308   for (args = DECL_ARGUMENTS (builtin_decl_implicit (fn));
30309        args;
30310        args = TREE_CHAIN (args))
30311     arity++;
30312
30313   if (arity == 1)
30314     fntype = build_function_type_list (type_out, type_in, NULL);
30315   else
30316     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
30317
30318   /* Build a function declaration for the vectorized function.  */
30319   new_fndecl = build_decl (BUILTINS_LOCATION,
30320                            FUNCTION_DECL, get_identifier (name), fntype);
30321   TREE_PUBLIC (new_fndecl) = 1;
30322   DECL_EXTERNAL (new_fndecl) = 1;
30323   DECL_IS_NOVOPS (new_fndecl) = 1;
30324   TREE_READONLY (new_fndecl) = 1;
30325
30326   return new_fndecl;
30327 }
30328
30329 /* Returns a decl of a function that implements gather load with
30330    memory type MEM_VECTYPE and index type INDEX_VECTYPE and SCALE.
30331    Return NULL_TREE if it is not available.  */
30332
30333 static tree
30334 ix86_vectorize_builtin_gather (const_tree mem_vectype,
30335                                const_tree index_type, int scale)
30336 {
30337   bool si;
30338   enum ix86_builtins code;
30339
30340   if (! TARGET_AVX2)
30341     return NULL_TREE;
30342
30343   if ((TREE_CODE (index_type) != INTEGER_TYPE
30344        && !POINTER_TYPE_P (index_type))
30345       || (TYPE_MODE (index_type) != SImode
30346           && TYPE_MODE (index_type) != DImode))
30347     return NULL_TREE;
30348
30349   if (TYPE_PRECISION (index_type) > POINTER_SIZE)
30350     return NULL_TREE;
30351
30352   /* v*gather* insn sign extends index to pointer mode.  */
30353   if (TYPE_PRECISION (index_type) < POINTER_SIZE
30354       && TYPE_UNSIGNED (index_type))
30355     return NULL_TREE;
30356
30357   if (scale <= 0
30358       || scale > 8
30359       || (scale & (scale - 1)) != 0)
30360     return NULL_TREE;
30361
30362   si = TYPE_MODE (index_type) == SImode;
30363   switch (TYPE_MODE (mem_vectype))
30364     {
30365     case V2DFmode:
30366       code = si ? IX86_BUILTIN_GATHERSIV2DF : IX86_BUILTIN_GATHERDIV2DF;
30367       break;
30368     case V4DFmode:
30369       code = si ? IX86_BUILTIN_GATHERALTSIV4DF : IX86_BUILTIN_GATHERDIV4DF;
30370       break;
30371     case V2DImode:
30372       code = si ? IX86_BUILTIN_GATHERSIV2DI : IX86_BUILTIN_GATHERDIV2DI;
30373       break;
30374     case V4DImode:
30375       code = si ? IX86_BUILTIN_GATHERALTSIV4DI : IX86_BUILTIN_GATHERDIV4DI;
30376       break;
30377     case V4SFmode:
30378       code = si ? IX86_BUILTIN_GATHERSIV4SF : IX86_BUILTIN_GATHERDIV4SF;
30379       break;
30380     case V8SFmode:
30381       code = si ? IX86_BUILTIN_GATHERSIV8SF : IX86_BUILTIN_GATHERALTDIV8SF;
30382       break;
30383     case V4SImode:
30384       code = si ? IX86_BUILTIN_GATHERSIV4SI : IX86_BUILTIN_GATHERDIV4SI;
30385       break;
30386     case V8SImode:
30387       code = si ? IX86_BUILTIN_GATHERSIV8SI : IX86_BUILTIN_GATHERALTDIV8SI;
30388       break;
30389     default:
30390       return NULL_TREE;
30391     }
30392
30393   return ix86_builtins[code];
30394 }
30395
30396 /* Returns a code for a target-specific builtin that implements
30397    reciprocal of the function, or NULL_TREE if not available.  */
30398
30399 static tree
30400 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
30401                          bool sqrt ATTRIBUTE_UNUSED)
30402 {
30403   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
30404          && flag_finite_math_only && !flag_trapping_math
30405          && flag_unsafe_math_optimizations))
30406     return NULL_TREE;
30407
30408   if (md_fn)
30409     /* Machine dependent builtins.  */
30410     switch (fn)
30411       {
30412         /* Vectorized version of sqrt to rsqrt conversion.  */
30413       case IX86_BUILTIN_SQRTPS_NR:
30414         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
30415
30416       case IX86_BUILTIN_SQRTPS_NR256:
30417         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR256];
30418
30419       default:
30420         return NULL_TREE;
30421       }
30422   else
30423     /* Normal builtins.  */
30424     switch (fn)
30425       {
30426         /* Sqrt to rsqrt conversion.  */
30427       case BUILT_IN_SQRTF:
30428         return ix86_builtins[IX86_BUILTIN_RSQRTF];
30429
30430       default:
30431         return NULL_TREE;
30432       }
30433 }
30434 \f
30435 /* Helper for avx_vpermilps256_operand et al.  This is also used by
30436    the expansion functions to turn the parallel back into a mask.
30437    The return value is 0 for no match and the imm8+1 for a match.  */
30438
30439 int
30440 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
30441 {
30442   unsigned i, nelt = GET_MODE_NUNITS (mode);
30443   unsigned mask = 0;
30444   unsigned char ipar[8];
30445
30446   if (XVECLEN (par, 0) != (int) nelt)
30447     return 0;
30448
30449   /* Validate that all of the elements are constants, and not totally
30450      out of range.  Copy the data into an integral array to make the
30451      subsequent checks easier.  */
30452   for (i = 0; i < nelt; ++i)
30453     {
30454       rtx er = XVECEXP (par, 0, i);
30455       unsigned HOST_WIDE_INT ei;
30456
30457       if (!CONST_INT_P (er))
30458         return 0;
30459       ei = INTVAL (er);
30460       if (ei >= nelt)
30461         return 0;
30462       ipar[i] = ei;
30463     }
30464
30465   switch (mode)
30466     {
30467     case V4DFmode:
30468       /* In the 256-bit DFmode case, we can only move elements within
30469          a 128-bit lane.  */
30470       for (i = 0; i < 2; ++i)
30471         {
30472           if (ipar[i] >= 2)
30473             return 0;
30474           mask |= ipar[i] << i;
30475         }
30476       for (i = 2; i < 4; ++i)
30477         {
30478           if (ipar[i] < 2)
30479             return 0;
30480           mask |= (ipar[i] - 2) << i;
30481         }
30482       break;
30483
30484     case V8SFmode:
30485       /* In the 256-bit SFmode case, we have full freedom of movement
30486          within the low 128-bit lane, but the high 128-bit lane must
30487          mirror the exact same pattern.  */
30488       for (i = 0; i < 4; ++i)
30489         if (ipar[i] + 4 != ipar[i + 4])
30490           return 0;
30491       nelt = 4;
30492       /* FALLTHRU */
30493
30494     case V2DFmode:
30495     case V4SFmode:
30496       /* In the 128-bit case, we've full freedom in the placement of
30497          the elements from the source operand.  */
30498       for (i = 0; i < nelt; ++i)
30499         mask |= ipar[i] << (i * (nelt / 2));
30500       break;
30501
30502     default:
30503       gcc_unreachable ();
30504     }
30505
30506   /* Make sure success has a non-zero value by adding one.  */
30507   return mask + 1;
30508 }
30509
30510 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
30511    the expansion functions to turn the parallel back into a mask.
30512    The return value is 0 for no match and the imm8+1 for a match.  */
30513
30514 int
30515 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
30516 {
30517   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
30518   unsigned mask = 0;
30519   unsigned char ipar[8];
30520
30521   if (XVECLEN (par, 0) != (int) nelt)
30522     return 0;
30523
30524   /* Validate that all of the elements are constants, and not totally
30525      out of range.  Copy the data into an integral array to make the
30526      subsequent checks easier.  */
30527   for (i = 0; i < nelt; ++i)
30528     {
30529       rtx er = XVECEXP (par, 0, i);
30530       unsigned HOST_WIDE_INT ei;
30531
30532       if (!CONST_INT_P (er))
30533         return 0;
30534       ei = INTVAL (er);
30535       if (ei >= 2 * nelt)
30536         return 0;
30537       ipar[i] = ei;
30538     }
30539
30540   /* Validate that the halves of the permute are halves.  */
30541   for (i = 0; i < nelt2 - 1; ++i)
30542     if (ipar[i] + 1 != ipar[i + 1])
30543       return 0;
30544   for (i = nelt2; i < nelt - 1; ++i)
30545     if (ipar[i] + 1 != ipar[i + 1])
30546       return 0;
30547
30548   /* Reconstruct the mask.  */
30549   for (i = 0; i < 2; ++i)
30550     {
30551       unsigned e = ipar[i * nelt2];
30552       if (e % nelt2)
30553         return 0;
30554       e /= nelt2;
30555       mask |= e << (i * 4);
30556     }
30557
30558   /* Make sure success has a non-zero value by adding one.  */
30559   return mask + 1;
30560 }
30561 \f
30562 /* Store OPERAND to the memory after reload is completed.  This means
30563    that we can't easily use assign_stack_local.  */
30564 rtx
30565 ix86_force_to_memory (enum machine_mode mode, rtx operand)
30566 {
30567   rtx result;
30568
30569   gcc_assert (reload_completed);
30570   if (ix86_using_red_zone ())
30571     {
30572       result = gen_rtx_MEM (mode,
30573                             gen_rtx_PLUS (Pmode,
30574                                           stack_pointer_rtx,
30575                                           GEN_INT (-RED_ZONE_SIZE)));
30576       emit_move_insn (result, operand);
30577     }
30578   else if (TARGET_64BIT)
30579     {
30580       switch (mode)
30581         {
30582         case HImode:
30583         case SImode:
30584           operand = gen_lowpart (DImode, operand);
30585           /* FALLTHRU */
30586         case DImode:
30587           emit_insn (
30588                       gen_rtx_SET (VOIDmode,
30589                                    gen_rtx_MEM (DImode,
30590                                                 gen_rtx_PRE_DEC (DImode,
30591                                                         stack_pointer_rtx)),
30592                                    operand));
30593           break;
30594         default:
30595           gcc_unreachable ();
30596         }
30597       result = gen_rtx_MEM (mode, stack_pointer_rtx);
30598     }
30599   else
30600     {
30601       switch (mode)
30602         {
30603         case DImode:
30604           {
30605             rtx operands[2];
30606             split_double_mode (mode, &operand, 1, operands, operands + 1);
30607             emit_insn (
30608                         gen_rtx_SET (VOIDmode,
30609                                      gen_rtx_MEM (SImode,
30610                                                   gen_rtx_PRE_DEC (Pmode,
30611                                                         stack_pointer_rtx)),
30612                                      operands[1]));
30613             emit_insn (
30614                         gen_rtx_SET (VOIDmode,
30615                                      gen_rtx_MEM (SImode,
30616                                                   gen_rtx_PRE_DEC (Pmode,
30617                                                         stack_pointer_rtx)),
30618                                      operands[0]));
30619           }
30620           break;
30621         case HImode:
30622           /* Store HImodes as SImodes.  */
30623           operand = gen_lowpart (SImode, operand);
30624           /* FALLTHRU */
30625         case SImode:
30626           emit_insn (
30627                       gen_rtx_SET (VOIDmode,
30628                                    gen_rtx_MEM (GET_MODE (operand),
30629                                                 gen_rtx_PRE_DEC (SImode,
30630                                                         stack_pointer_rtx)),
30631                                    operand));
30632           break;
30633         default:
30634           gcc_unreachable ();
30635         }
30636       result = gen_rtx_MEM (mode, stack_pointer_rtx);
30637     }
30638   return result;
30639 }
30640
30641 /* Free operand from the memory.  */
30642 void
30643 ix86_free_from_memory (enum machine_mode mode)
30644 {
30645   if (!ix86_using_red_zone ())
30646     {
30647       int size;
30648
30649       if (mode == DImode || TARGET_64BIT)
30650         size = 8;
30651       else
30652         size = 4;
30653       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
30654          to pop or add instruction if registers are available.  */
30655       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
30656                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
30657                                             GEN_INT (size))));
30658     }
30659 }
30660
30661 /* Implement TARGET_PREFERRED_RELOAD_CLASS.
30662
30663    Put float CONST_DOUBLE in the constant pool instead of fp regs.
30664    QImode must go into class Q_REGS.
30665    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
30666    movdf to do mem-to-mem moves through integer regs.  */
30667
30668 static reg_class_t
30669 ix86_preferred_reload_class (rtx x, reg_class_t regclass)
30670 {
30671   enum machine_mode mode = GET_MODE (x);
30672
30673   /* We're only allowed to return a subclass of CLASS.  Many of the
30674      following checks fail for NO_REGS, so eliminate that early.  */
30675   if (regclass == NO_REGS)
30676     return NO_REGS;
30677
30678   /* All classes can load zeros.  */
30679   if (x == CONST0_RTX (mode))
30680     return regclass;
30681
30682   /* Force constants into memory if we are loading a (nonzero) constant into
30683      an MMX or SSE register.  This is because there are no MMX/SSE instructions
30684      to load from a constant.  */
30685   if (CONSTANT_P (x)
30686       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
30687     return NO_REGS;
30688
30689   /* Prefer SSE regs only, if we can use them for math.  */
30690   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
30691     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
30692
30693   /* Floating-point constants need more complex checks.  */
30694   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
30695     {
30696       /* General regs can load everything.  */
30697       if (reg_class_subset_p (regclass, GENERAL_REGS))
30698         return regclass;
30699
30700       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
30701          zero above.  We only want to wind up preferring 80387 registers if
30702          we plan on doing computation with them.  */
30703       if (TARGET_80387
30704           && standard_80387_constant_p (x) > 0)
30705         {
30706           /* Limit class to non-sse.  */
30707           if (regclass == FLOAT_SSE_REGS)
30708             return FLOAT_REGS;
30709           if (regclass == FP_TOP_SSE_REGS)
30710             return FP_TOP_REG;
30711           if (regclass == FP_SECOND_SSE_REGS)
30712             return FP_SECOND_REG;
30713           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
30714             return regclass;
30715         }
30716
30717       return NO_REGS;
30718     }
30719
30720   /* Generally when we see PLUS here, it's the function invariant
30721      (plus soft-fp const_int).  Which can only be computed into general
30722      regs.  */
30723   if (GET_CODE (x) == PLUS)
30724     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
30725
30726   /* QImode constants are easy to load, but non-constant QImode data
30727      must go into Q_REGS.  */
30728   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
30729     {
30730       if (reg_class_subset_p (regclass, Q_REGS))
30731         return regclass;
30732       if (reg_class_subset_p (Q_REGS, regclass))
30733         return Q_REGS;
30734       return NO_REGS;
30735     }
30736
30737   return regclass;
30738 }
30739
30740 /* Discourage putting floating-point values in SSE registers unless
30741    SSE math is being used, and likewise for the 387 registers.  */
30742 static reg_class_t
30743 ix86_preferred_output_reload_class (rtx x, reg_class_t regclass)
30744 {
30745   enum machine_mode mode = GET_MODE (x);
30746
30747   /* Restrict the output reload class to the register bank that we are doing
30748      math on.  If we would like not to return a subset of CLASS, reject this
30749      alternative: if reload cannot do this, it will still use its choice.  */
30750   mode = GET_MODE (x);
30751   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
30752     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
30753
30754   if (X87_FLOAT_MODE_P (mode))
30755     {
30756       if (regclass == FP_TOP_SSE_REGS)
30757         return FP_TOP_REG;
30758       else if (regclass == FP_SECOND_SSE_REGS)
30759         return FP_SECOND_REG;
30760       else
30761         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
30762     }
30763
30764   return regclass;
30765 }
30766
30767 static reg_class_t
30768 ix86_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
30769                        enum machine_mode mode, secondary_reload_info *sri)
30770 {
30771   /* Double-word spills from general registers to non-offsettable memory
30772      references (zero-extended addresses) require special handling.  */
30773   if (TARGET_64BIT
30774       && MEM_P (x)
30775       && GET_MODE_SIZE (mode) > UNITS_PER_WORD
30776       && rclass == GENERAL_REGS
30777       && !offsettable_memref_p (x))
30778     {
30779       sri->icode = (in_p
30780                     ? CODE_FOR_reload_noff_load
30781                     : CODE_FOR_reload_noff_store);
30782       /* Add the cost of moving address to a temporary.  */
30783       sri->extra_cost = 1;
30784
30785       return NO_REGS;
30786     }
30787
30788   /* QImode spills from non-QI registers require
30789      intermediate register on 32bit targets.  */
30790   if (!TARGET_64BIT
30791       && !in_p && mode == QImode
30792       && (rclass == GENERAL_REGS
30793           || rclass == LEGACY_REGS
30794           || rclass == INDEX_REGS))
30795     {
30796       int regno;
30797
30798       if (REG_P (x))
30799         regno = REGNO (x);
30800       else
30801         regno = -1;
30802
30803       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
30804         regno = true_regnum (x);
30805
30806       /* Return Q_REGS if the operand is in memory.  */
30807       if (regno == -1)
30808         return Q_REGS;
30809     }
30810
30811   /* This condition handles corner case where an expression involving
30812      pointers gets vectorized.  We're trying to use the address of a
30813      stack slot as a vector initializer.
30814
30815      (set (reg:V2DI 74 [ vect_cst_.2 ])
30816           (vec_duplicate:V2DI (reg/f:DI 20 frame)))
30817
30818      Eventually frame gets turned into sp+offset like this:
30819
30820      (set (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
30821           (vec_duplicate:V2DI (plus:DI (reg/f:DI 7 sp)
30822                                        (const_int 392 [0x188]))))
30823
30824      That later gets turned into:
30825
30826      (set (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
30827           (vec_duplicate:V2DI (plus:DI (reg/f:DI 7 sp)
30828             (mem/u/c/i:DI (symbol_ref/u:DI ("*.LC0") [flags 0x2]) [0 S8 A64]))))
30829
30830      We'll have the following reload recorded:
30831
30832      Reload 0: reload_in (DI) =
30833            (plus:DI (reg/f:DI 7 sp)
30834             (mem/u/c/i:DI (symbol_ref/u:DI ("*.LC0") [flags 0x2]) [0 S8 A64]))
30835      reload_out (V2DI) = (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
30836      SSE_REGS, RELOAD_OTHER (opnum = 0), can't combine
30837      reload_in_reg: (plus:DI (reg/f:DI 7 sp) (const_int 392 [0x188]))
30838      reload_out_reg: (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
30839      reload_reg_rtx: (reg:V2DI 22 xmm1)
30840
30841      Which isn't going to work since SSE instructions can't handle scalar
30842      additions.  Returning GENERAL_REGS forces the addition into integer
30843      register and reload can handle subsequent reloads without problems.  */
30844
30845   if (in_p && GET_CODE (x) == PLUS
30846       && SSE_CLASS_P (rclass)
30847       && SCALAR_INT_MODE_P (mode))
30848     return GENERAL_REGS;
30849
30850   return NO_REGS;
30851 }
30852
30853 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.  */
30854
30855 static bool
30856 ix86_class_likely_spilled_p (reg_class_t rclass)
30857 {
30858   switch (rclass)
30859     {
30860       case AREG:
30861       case DREG:
30862       case CREG:
30863       case BREG:
30864       case AD_REGS:
30865       case SIREG:
30866       case DIREG:
30867       case SSE_FIRST_REG:
30868       case FP_TOP_REG:
30869       case FP_SECOND_REG:
30870         return true;
30871
30872       default:
30873         break;
30874     }
30875
30876   return false;
30877 }
30878
30879 /* If we are copying between general and FP registers, we need a memory
30880    location. The same is true for SSE and MMX registers.
30881
30882    To optimize register_move_cost performance, allow inline variant.
30883
30884    The macro can't work reliably when one of the CLASSES is class containing
30885    registers from multiple units (SSE, MMX, integer).  We avoid this by never
30886    combining those units in single alternative in the machine description.
30887    Ensure that this constraint holds to avoid unexpected surprises.
30888
30889    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
30890    enforce these sanity checks.  */
30891
30892 static inline bool
30893 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
30894                                 enum machine_mode mode, int strict)
30895 {
30896   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
30897       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
30898       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
30899       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
30900       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
30901       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
30902     {
30903       gcc_assert (!strict);
30904       return true;
30905     }
30906
30907   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
30908     return true;
30909
30910   /* ??? This is a lie.  We do have moves between mmx/general, and for
30911      mmx/sse2.  But by saying we need secondary memory we discourage the
30912      register allocator from using the mmx registers unless needed.  */
30913   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
30914     return true;
30915
30916   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
30917     {
30918       /* SSE1 doesn't have any direct moves from other classes.  */
30919       if (!TARGET_SSE2)
30920         return true;
30921
30922       /* If the target says that inter-unit moves are more expensive
30923          than moving through memory, then don't generate them.  */
30924       if (!TARGET_INTER_UNIT_MOVES)
30925         return true;
30926
30927       /* Between SSE and general, we have moves no larger than word size.  */
30928       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
30929         return true;
30930     }
30931
30932   return false;
30933 }
30934
30935 bool
30936 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
30937                               enum machine_mode mode, int strict)
30938 {
30939   return inline_secondary_memory_needed (class1, class2, mode, strict);
30940 }
30941
30942 /* Implement the TARGET_CLASS_MAX_NREGS hook.
30943
30944    On the 80386, this is the size of MODE in words,
30945    except in the FP regs, where a single reg is always enough.  */
30946
30947 static unsigned char
30948 ix86_class_max_nregs (reg_class_t rclass, enum machine_mode mode)
30949 {
30950   if (MAYBE_INTEGER_CLASS_P (rclass))
30951     {
30952       if (mode == XFmode)
30953         return (TARGET_64BIT ? 2 : 3);
30954       else if (mode == XCmode)
30955         return (TARGET_64BIT ? 4 : 6);
30956       else
30957         return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
30958     }
30959   else
30960     {
30961       if (COMPLEX_MODE_P (mode))
30962         return 2;
30963       else
30964         return 1;
30965     }
30966 }
30967
30968 /* Return true if the registers in CLASS cannot represent the change from
30969    modes FROM to TO.  */
30970
30971 bool
30972 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
30973                                enum reg_class regclass)
30974 {
30975   if (from == to)
30976     return false;
30977
30978   /* x87 registers can't do subreg at all, as all values are reformatted
30979      to extended precision.  */
30980   if (MAYBE_FLOAT_CLASS_P (regclass))
30981     return true;
30982
30983   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
30984     {
30985       /* Vector registers do not support QI or HImode loads.  If we don't
30986          disallow a change to these modes, reload will assume it's ok to
30987          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
30988          the vec_dupv4hi pattern.  */
30989       if (GET_MODE_SIZE (from) < 4)
30990         return true;
30991
30992       /* Vector registers do not support subreg with nonzero offsets, which
30993          are otherwise valid for integer registers.  Since we can't see
30994          whether we have a nonzero offset from here, prohibit all
30995          nonparadoxical subregs changing size.  */
30996       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
30997         return true;
30998     }
30999
31000   return false;
31001 }
31002
31003 /* Return the cost of moving data of mode M between a
31004    register and memory.  A value of 2 is the default; this cost is
31005    relative to those in `REGISTER_MOVE_COST'.
31006
31007    This function is used extensively by register_move_cost that is used to
31008    build tables at startup.  Make it inline in this case.
31009    When IN is 2, return maximum of in and out move cost.
31010
31011    If moving between registers and memory is more expensive than
31012    between two registers, you should define this macro to express the
31013    relative cost.
31014
31015    Model also increased moving costs of QImode registers in non
31016    Q_REGS classes.
31017  */
31018 static inline int
31019 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
31020                          int in)
31021 {
31022   int cost;
31023   if (FLOAT_CLASS_P (regclass))
31024     {
31025       int index;
31026       switch (mode)
31027         {
31028           case SFmode:
31029             index = 0;
31030             break;
31031           case DFmode:
31032             index = 1;
31033             break;
31034           case XFmode:
31035             index = 2;
31036             break;
31037           default:
31038             return 100;
31039         }
31040       if (in == 2)
31041         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
31042       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
31043     }
31044   if (SSE_CLASS_P (regclass))
31045     {
31046       int index;
31047       switch (GET_MODE_SIZE (mode))
31048         {
31049           case 4:
31050             index = 0;
31051             break;
31052           case 8:
31053             index = 1;
31054             break;
31055           case 16:
31056             index = 2;
31057             break;
31058           default:
31059             return 100;
31060         }
31061       if (in == 2)
31062         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
31063       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
31064     }
31065   if (MMX_CLASS_P (regclass))
31066     {
31067       int index;
31068       switch (GET_MODE_SIZE (mode))
31069         {
31070           case 4:
31071             index = 0;
31072             break;
31073           case 8:
31074             index = 1;
31075             break;
31076           default:
31077             return 100;
31078         }
31079       if (in)
31080         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
31081       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
31082     }
31083   switch (GET_MODE_SIZE (mode))
31084     {
31085       case 1:
31086         if (Q_CLASS_P (regclass) || TARGET_64BIT)
31087           {
31088             if (!in)
31089               return ix86_cost->int_store[0];
31090             if (TARGET_PARTIAL_REG_DEPENDENCY
31091                 && optimize_function_for_speed_p (cfun))
31092               cost = ix86_cost->movzbl_load;
31093             else
31094               cost = ix86_cost->int_load[0];
31095             if (in == 2)
31096               return MAX (cost, ix86_cost->int_store[0]);
31097             return cost;
31098           }
31099         else
31100           {
31101            if (in == 2)
31102              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
31103            if (in)
31104              return ix86_cost->movzbl_load;
31105            else
31106              return ix86_cost->int_store[0] + 4;
31107           }
31108         break;
31109       case 2:
31110         if (in == 2)
31111           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
31112         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
31113       default:
31114         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
31115         if (mode == TFmode)
31116           mode = XFmode;
31117         if (in == 2)
31118           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
31119         else if (in)
31120           cost = ix86_cost->int_load[2];
31121         else
31122           cost = ix86_cost->int_store[2];
31123         return (cost * (((int) GET_MODE_SIZE (mode)
31124                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
31125     }
31126 }
31127
31128 static int
31129 ix86_memory_move_cost (enum machine_mode mode, reg_class_t regclass,
31130                        bool in)
31131 {
31132   return inline_memory_move_cost (mode, (enum reg_class) regclass, in ? 1 : 0);
31133 }
31134
31135
31136 /* Return the cost of moving data from a register in class CLASS1 to
31137    one in class CLASS2.
31138
31139    It is not required that the cost always equal 2 when FROM is the same as TO;
31140    on some machines it is expensive to move between registers if they are not
31141    general registers.  */
31142
31143 static int
31144 ix86_register_move_cost (enum machine_mode mode, reg_class_t class1_i,
31145                          reg_class_t class2_i)
31146 {
31147   enum reg_class class1 = (enum reg_class) class1_i;
31148   enum reg_class class2 = (enum reg_class) class2_i;
31149
31150   /* In case we require secondary memory, compute cost of the store followed
31151      by load.  In order to avoid bad register allocation choices, we need
31152      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
31153
31154   if (inline_secondary_memory_needed (class1, class2, mode, 0))
31155     {
31156       int cost = 1;
31157
31158       cost += inline_memory_move_cost (mode, class1, 2);
31159       cost += inline_memory_move_cost (mode, class2, 2);
31160
31161       /* In case of copying from general_purpose_register we may emit multiple
31162          stores followed by single load causing memory size mismatch stall.
31163          Count this as arbitrarily high cost of 20.  */
31164       if (targetm.class_max_nregs (class1, mode)
31165           > targetm.class_max_nregs (class2, mode))
31166         cost += 20;
31167
31168       /* In the case of FP/MMX moves, the registers actually overlap, and we
31169          have to switch modes in order to treat them differently.  */
31170       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
31171           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
31172         cost += 20;
31173
31174       return cost;
31175     }
31176
31177   /* Moves between SSE/MMX and integer unit are expensive.  */
31178   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
31179       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
31180
31181     /* ??? By keeping returned value relatively high, we limit the number
31182        of moves between integer and MMX/SSE registers for all targets.
31183        Additionally, high value prevents problem with x86_modes_tieable_p(),
31184        where integer modes in MMX/SSE registers are not tieable
31185        because of missing QImode and HImode moves to, from or between
31186        MMX/SSE registers.  */
31187     return MAX (8, ix86_cost->mmxsse_to_integer);
31188
31189   if (MAYBE_FLOAT_CLASS_P (class1))
31190     return ix86_cost->fp_move;
31191   if (MAYBE_SSE_CLASS_P (class1))
31192     return ix86_cost->sse_move;
31193   if (MAYBE_MMX_CLASS_P (class1))
31194     return ix86_cost->mmx_move;
31195   return 2;
31196 }
31197
31198 /* Return TRUE if hard register REGNO can hold a value of machine-mode
31199    MODE.  */
31200
31201 bool
31202 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
31203 {
31204   /* Flags and only flags can only hold CCmode values.  */
31205   if (CC_REGNO_P (regno))
31206     return GET_MODE_CLASS (mode) == MODE_CC;
31207   if (GET_MODE_CLASS (mode) == MODE_CC
31208       || GET_MODE_CLASS (mode) == MODE_RANDOM
31209       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
31210     return false;
31211   if (FP_REGNO_P (regno))
31212     return VALID_FP_MODE_P (mode);
31213   if (SSE_REGNO_P (regno))
31214     {
31215       /* We implement the move patterns for all vector modes into and
31216          out of SSE registers, even when no operation instructions
31217          are available.  OImode move is available only when AVX is
31218          enabled.  */
31219       return ((TARGET_AVX && mode == OImode)
31220               || VALID_AVX256_REG_MODE (mode)
31221               || VALID_SSE_REG_MODE (mode)
31222               || VALID_SSE2_REG_MODE (mode)
31223               || VALID_MMX_REG_MODE (mode)
31224               || VALID_MMX_REG_MODE_3DNOW (mode));
31225     }
31226   if (MMX_REGNO_P (regno))
31227     {
31228       /* We implement the move patterns for 3DNOW modes even in MMX mode,
31229          so if the register is available at all, then we can move data of
31230          the given mode into or out of it.  */
31231       return (VALID_MMX_REG_MODE (mode)
31232               || VALID_MMX_REG_MODE_3DNOW (mode));
31233     }
31234
31235   if (mode == QImode)
31236     {
31237       /* Take care for QImode values - they can be in non-QI regs,
31238          but then they do cause partial register stalls.  */
31239       if (regno <= BX_REG || TARGET_64BIT)
31240         return true;
31241       if (!TARGET_PARTIAL_REG_STALL)
31242         return true;
31243       return !can_create_pseudo_p ();
31244     }
31245   /* We handle both integer and floats in the general purpose registers.  */
31246   else if (VALID_INT_MODE_P (mode))
31247     return true;
31248   else if (VALID_FP_MODE_P (mode))
31249     return true;
31250   else if (VALID_DFP_MODE_P (mode))
31251     return true;
31252   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
31253      on to use that value in smaller contexts, this can easily force a
31254      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
31255      supporting DImode, allow it.  */
31256   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
31257     return true;
31258
31259   return false;
31260 }
31261
31262 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
31263    tieable integer mode.  */
31264
31265 static bool
31266 ix86_tieable_integer_mode_p (enum machine_mode mode)
31267 {
31268   switch (mode)
31269     {
31270     case HImode:
31271     case SImode:
31272       return true;
31273
31274     case QImode:
31275       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
31276
31277     case DImode:
31278       return TARGET_64BIT;
31279
31280     default:
31281       return false;
31282     }
31283 }
31284
31285 /* Return true if MODE1 is accessible in a register that can hold MODE2
31286    without copying.  That is, all register classes that can hold MODE2
31287    can also hold MODE1.  */
31288
31289 bool
31290 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
31291 {
31292   if (mode1 == mode2)
31293     return true;
31294
31295   if (ix86_tieable_integer_mode_p (mode1)
31296       && ix86_tieable_integer_mode_p (mode2))
31297     return true;
31298
31299   /* MODE2 being XFmode implies fp stack or general regs, which means we
31300      can tie any smaller floating point modes to it.  Note that we do not
31301      tie this with TFmode.  */
31302   if (mode2 == XFmode)
31303     return mode1 == SFmode || mode1 == DFmode;
31304
31305   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
31306      that we can tie it with SFmode.  */
31307   if (mode2 == DFmode)
31308     return mode1 == SFmode;
31309
31310   /* If MODE2 is only appropriate for an SSE register, then tie with
31311      any other mode acceptable to SSE registers.  */
31312   if (GET_MODE_SIZE (mode2) == 16
31313       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
31314     return (GET_MODE_SIZE (mode1) == 16
31315             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
31316
31317   /* If MODE2 is appropriate for an MMX register, then tie
31318      with any other mode acceptable to MMX registers.  */
31319   if (GET_MODE_SIZE (mode2) == 8
31320       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
31321     return (GET_MODE_SIZE (mode1) == 8
31322             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
31323
31324   return false;
31325 }
31326
31327 /* Compute a (partial) cost for rtx X.  Return true if the complete
31328    cost has been computed, and false if subexpressions should be
31329    scanned.  In either case, *TOTAL contains the cost result.  */
31330
31331 static bool
31332 ix86_rtx_costs (rtx x, int code, int outer_code_i, int opno, int *total,
31333                 bool speed)
31334 {
31335   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
31336   enum machine_mode mode = GET_MODE (x);
31337   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
31338
31339   switch (code)
31340     {
31341     case CONST_INT:
31342     case CONST:
31343     case LABEL_REF:
31344     case SYMBOL_REF:
31345       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
31346         *total = 3;
31347       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
31348         *total = 2;
31349       else if (flag_pic && SYMBOLIC_CONST (x)
31350                && (!TARGET_64BIT
31351                    || (!GET_CODE (x) != LABEL_REF
31352                        && (GET_CODE (x) != SYMBOL_REF
31353                            || !SYMBOL_REF_LOCAL_P (x)))))
31354         *total = 1;
31355       else
31356         *total = 0;
31357       return true;
31358
31359     case CONST_DOUBLE:
31360       if (mode == VOIDmode)
31361         *total = 0;
31362       else
31363         switch (standard_80387_constant_p (x))
31364           {
31365           case 1: /* 0.0 */
31366             *total = 1;
31367             break;
31368           default: /* Other constants */
31369             *total = 2;
31370             break;
31371           case 0:
31372           case -1:
31373             /* Start with (MEM (SYMBOL_REF)), since that's where
31374                it'll probably end up.  Add a penalty for size.  */
31375             *total = (COSTS_N_INSNS (1)
31376                       + (flag_pic != 0 && !TARGET_64BIT)
31377                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
31378             break;
31379           }
31380       return true;
31381
31382     case ZERO_EXTEND:
31383       /* The zero extensions is often completely free on x86_64, so make
31384          it as cheap as possible.  */
31385       if (TARGET_64BIT && mode == DImode
31386           && GET_MODE (XEXP (x, 0)) == SImode)
31387         *total = 1;
31388       else if (TARGET_ZERO_EXTEND_WITH_AND)
31389         *total = cost->add;
31390       else
31391         *total = cost->movzx;
31392       return false;
31393
31394     case SIGN_EXTEND:
31395       *total = cost->movsx;
31396       return false;
31397
31398     case ASHIFT:
31399       if (CONST_INT_P (XEXP (x, 1))
31400           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
31401         {
31402           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
31403           if (value == 1)
31404             {
31405               *total = cost->add;
31406               return false;
31407             }
31408           if ((value == 2 || value == 3)
31409               && cost->lea <= cost->shift_const)
31410             {
31411               *total = cost->lea;
31412               return false;
31413             }
31414         }
31415       /* FALLTHRU */
31416
31417     case ROTATE:
31418     case ASHIFTRT:
31419     case LSHIFTRT:
31420     case ROTATERT:
31421       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
31422         {
31423           if (CONST_INT_P (XEXP (x, 1)))
31424             {
31425               if (INTVAL (XEXP (x, 1)) > 32)
31426                 *total = cost->shift_const + COSTS_N_INSNS (2);
31427               else
31428                 *total = cost->shift_const * 2;
31429             }
31430           else
31431             {
31432               if (GET_CODE (XEXP (x, 1)) == AND)
31433                 *total = cost->shift_var * 2;
31434               else
31435                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
31436             }
31437         }
31438       else
31439         {
31440           if (CONST_INT_P (XEXP (x, 1)))
31441             *total = cost->shift_const;
31442           else
31443             *total = cost->shift_var;
31444         }
31445       return false;
31446
31447     case FMA:
31448       {
31449         rtx sub;
31450
31451         gcc_assert (FLOAT_MODE_P (mode));
31452         gcc_assert (TARGET_FMA || TARGET_FMA4);
31453
31454         /* ??? SSE scalar/vector cost should be used here.  */
31455         /* ??? Bald assumption that fma has the same cost as fmul.  */
31456         *total = cost->fmul;
31457         *total += rtx_cost (XEXP (x, 1), FMA, 1, speed);
31458
31459         /* Negate in op0 or op2 is free: FMS, FNMA, FNMS.  */
31460         sub = XEXP (x, 0);
31461         if (GET_CODE (sub) == NEG)
31462           sub = XEXP (sub, 0);
31463         *total += rtx_cost (sub, FMA, 0, speed);
31464
31465         sub = XEXP (x, 2);
31466         if (GET_CODE (sub) == NEG)
31467           sub = XEXP (sub, 0);
31468         *total += rtx_cost (sub, FMA, 2, speed);
31469         return true;
31470       }
31471
31472     case MULT:
31473       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31474         {
31475           /* ??? SSE scalar cost should be used here.  */
31476           *total = cost->fmul;
31477           return false;
31478         }
31479       else if (X87_FLOAT_MODE_P (mode))
31480         {
31481           *total = cost->fmul;
31482           return false;
31483         }
31484       else if (FLOAT_MODE_P (mode))
31485         {
31486           /* ??? SSE vector cost should be used here.  */
31487           *total = cost->fmul;
31488           return false;
31489         }
31490       else
31491         {
31492           rtx op0 = XEXP (x, 0);
31493           rtx op1 = XEXP (x, 1);
31494           int nbits;
31495           if (CONST_INT_P (XEXP (x, 1)))
31496             {
31497               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
31498               for (nbits = 0; value != 0; value &= value - 1)
31499                 nbits++;
31500             }
31501           else
31502             /* This is arbitrary.  */
31503             nbits = 7;
31504
31505           /* Compute costs correctly for widening multiplication.  */
31506           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
31507               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
31508                  == GET_MODE_SIZE (mode))
31509             {
31510               int is_mulwiden = 0;
31511               enum machine_mode inner_mode = GET_MODE (op0);
31512
31513               if (GET_CODE (op0) == GET_CODE (op1))
31514                 is_mulwiden = 1, op1 = XEXP (op1, 0);
31515               else if (CONST_INT_P (op1))
31516                 {
31517                   if (GET_CODE (op0) == SIGN_EXTEND)
31518                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
31519                                   == INTVAL (op1);
31520                   else
31521                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
31522                 }
31523
31524               if (is_mulwiden)
31525                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
31526             }
31527
31528           *total = (cost->mult_init[MODE_INDEX (mode)]
31529                     + nbits * cost->mult_bit
31530                     + rtx_cost (op0, outer_code, opno, speed)
31531                     + rtx_cost (op1, outer_code, opno, speed));
31532
31533           return true;
31534         }
31535
31536     case DIV:
31537     case UDIV:
31538     case MOD:
31539     case UMOD:
31540       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31541         /* ??? SSE cost should be used here.  */
31542         *total = cost->fdiv;
31543       else if (X87_FLOAT_MODE_P (mode))
31544         *total = cost->fdiv;
31545       else if (FLOAT_MODE_P (mode))
31546         /* ??? SSE vector cost should be used here.  */
31547         *total = cost->fdiv;
31548       else
31549         *total = cost->divide[MODE_INDEX (mode)];
31550       return false;
31551
31552     case PLUS:
31553       if (GET_MODE_CLASS (mode) == MODE_INT
31554                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
31555         {
31556           if (GET_CODE (XEXP (x, 0)) == PLUS
31557               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
31558               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
31559               && CONSTANT_P (XEXP (x, 1)))
31560             {
31561               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
31562               if (val == 2 || val == 4 || val == 8)
31563                 {
31564                   *total = cost->lea;
31565                   *total += rtx_cost (XEXP (XEXP (x, 0), 1),
31566                                       outer_code, opno, speed);
31567                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
31568                                       outer_code, opno, speed);
31569                   *total += rtx_cost (XEXP (x, 1), outer_code, opno, speed);
31570                   return true;
31571                 }
31572             }
31573           else if (GET_CODE (XEXP (x, 0)) == MULT
31574                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
31575             {
31576               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
31577               if (val == 2 || val == 4 || val == 8)
31578                 {
31579                   *total = cost->lea;
31580                   *total += rtx_cost (XEXP (XEXP (x, 0), 0),
31581                                       outer_code, opno, speed);
31582                   *total += rtx_cost (XEXP (x, 1), outer_code, opno, speed);
31583                   return true;
31584                 }
31585             }
31586           else if (GET_CODE (XEXP (x, 0)) == PLUS)
31587             {
31588               *total = cost->lea;
31589               *total += rtx_cost (XEXP (XEXP (x, 0), 0),
31590                                   outer_code, opno, speed);
31591               *total += rtx_cost (XEXP (XEXP (x, 0), 1),
31592                                   outer_code, opno, speed);
31593               *total += rtx_cost (XEXP (x, 1), outer_code, opno, speed);
31594               return true;
31595             }
31596         }
31597       /* FALLTHRU */
31598
31599     case MINUS:
31600       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31601         {
31602           /* ??? SSE cost should be used here.  */
31603           *total = cost->fadd;
31604           return false;
31605         }
31606       else if (X87_FLOAT_MODE_P (mode))
31607         {
31608           *total = cost->fadd;
31609           return false;
31610         }
31611       else if (FLOAT_MODE_P (mode))
31612         {
31613           /* ??? SSE vector cost should be used here.  */
31614           *total = cost->fadd;
31615           return false;
31616         }
31617       /* FALLTHRU */
31618
31619     case AND:
31620     case IOR:
31621     case XOR:
31622       if (!TARGET_64BIT && mode == DImode)
31623         {
31624           *total = (cost->add * 2
31625                     + (rtx_cost (XEXP (x, 0), outer_code, opno, speed)
31626                        << (GET_MODE (XEXP (x, 0)) != DImode))
31627                     + (rtx_cost (XEXP (x, 1), outer_code, opno, speed)
31628                        << (GET_MODE (XEXP (x, 1)) != DImode)));
31629           return true;
31630         }
31631       /* FALLTHRU */
31632
31633     case NEG:
31634       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31635         {
31636           /* ??? SSE cost should be used here.  */
31637           *total = cost->fchs;
31638           return false;
31639         }
31640       else if (X87_FLOAT_MODE_P (mode))
31641         {
31642           *total = cost->fchs;
31643           return false;
31644         }
31645       else if (FLOAT_MODE_P (mode))
31646         {
31647           /* ??? SSE vector cost should be used here.  */
31648           *total = cost->fchs;
31649           return false;
31650         }
31651       /* FALLTHRU */
31652
31653     case NOT:
31654       if (!TARGET_64BIT && mode == DImode)
31655         *total = cost->add * 2;
31656       else
31657         *total = cost->add;
31658       return false;
31659
31660     case COMPARE:
31661       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
31662           && XEXP (XEXP (x, 0), 1) == const1_rtx
31663           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
31664           && XEXP (x, 1) == const0_rtx)
31665         {
31666           /* This kind of construct is implemented using test[bwl].
31667              Treat it as if we had an AND.  */
31668           *total = (cost->add
31669                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, opno, speed)
31670                     + rtx_cost (const1_rtx, outer_code, opno, speed));
31671           return true;
31672         }
31673       return false;
31674
31675     case FLOAT_EXTEND:
31676       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
31677         *total = 0;
31678       return false;
31679
31680     case ABS:
31681       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31682         /* ??? SSE cost should be used here.  */
31683         *total = cost->fabs;
31684       else if (X87_FLOAT_MODE_P (mode))
31685         *total = cost->fabs;
31686       else if (FLOAT_MODE_P (mode))
31687         /* ??? SSE vector cost should be used here.  */
31688         *total = cost->fabs;
31689       return false;
31690
31691     case SQRT:
31692       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31693         /* ??? SSE cost should be used here.  */
31694         *total = cost->fsqrt;
31695       else if (X87_FLOAT_MODE_P (mode))
31696         *total = cost->fsqrt;
31697       else if (FLOAT_MODE_P (mode))
31698         /* ??? SSE vector cost should be used here.  */
31699         *total = cost->fsqrt;
31700       return false;
31701
31702     case UNSPEC:
31703       if (XINT (x, 1) == UNSPEC_TP)
31704         *total = 0;
31705       return false;
31706
31707     case VEC_SELECT:
31708     case VEC_CONCAT:
31709     case VEC_MERGE:
31710     case VEC_DUPLICATE:
31711       /* ??? Assume all of these vector manipulation patterns are
31712          recognizable.  In which case they all pretty much have the
31713          same cost.  */
31714      *total = COSTS_N_INSNS (1);
31715      return true;
31716
31717     default:
31718       return false;
31719     }
31720 }
31721
31722 #if TARGET_MACHO
31723
31724 static int current_machopic_label_num;
31725
31726 /* Given a symbol name and its associated stub, write out the
31727    definition of the stub.  */
31728
31729 void
31730 machopic_output_stub (FILE *file, const char *symb, const char *stub)
31731 {
31732   unsigned int length;
31733   char *binder_name, *symbol_name, lazy_ptr_name[32];
31734   int label = ++current_machopic_label_num;
31735
31736   /* For 64-bit we shouldn't get here.  */
31737   gcc_assert (!TARGET_64BIT);
31738
31739   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
31740   symb = targetm.strip_name_encoding (symb);
31741
31742   length = strlen (stub);
31743   binder_name = XALLOCAVEC (char, length + 32);
31744   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
31745
31746   length = strlen (symb);
31747   symbol_name = XALLOCAVEC (char, length + 32);
31748   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
31749
31750   sprintf (lazy_ptr_name, "L%d$lz", label);
31751
31752   if (MACHOPIC_ATT_STUB)
31753     switch_to_section (darwin_sections[machopic_picsymbol_stub3_section]);
31754   else if (MACHOPIC_PURE)
31755     switch_to_section (darwin_sections[machopic_picsymbol_stub2_section]);
31756   else
31757     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
31758
31759   fprintf (file, "%s:\n", stub);
31760   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
31761
31762   if (MACHOPIC_ATT_STUB)
31763     {
31764       fprintf (file, "\thlt ; hlt ; hlt ; hlt ; hlt\n");
31765     }
31766   else if (MACHOPIC_PURE)
31767     {
31768       /* PIC stub.  */
31769       /* 25-byte PIC stub using "CALL get_pc_thunk".  */
31770       rtx tmp = gen_rtx_REG (SImode, 2 /* ECX */);
31771       output_set_got (tmp, NULL_RTX);   /* "CALL ___<cpu>.get_pc_thunk.cx".  */
31772       fprintf (file, "LPC$%d:\tmovl\t%s-LPC$%d(%%ecx),%%ecx\n",
31773                label, lazy_ptr_name, label);
31774       fprintf (file, "\tjmp\t*%%ecx\n");
31775     }
31776   else
31777     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
31778
31779   /* The AT&T-style ("self-modifying") stub is not lazily bound, thus
31780      it needs no stub-binding-helper.  */
31781   if (MACHOPIC_ATT_STUB)
31782     return;
31783
31784   fprintf (file, "%s:\n", binder_name);
31785
31786   if (MACHOPIC_PURE)
31787     {
31788       fprintf (file, "\tlea\t%s-%s(%%ecx),%%ecx\n", lazy_ptr_name, binder_name);
31789       fprintf (file, "\tpushl\t%%ecx\n");
31790     }
31791   else
31792     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
31793
31794   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
31795
31796   /* N.B. Keep the correspondence of these
31797      'symbol_ptr/symbol_ptr2/symbol_ptr3' sections consistent with the
31798      old-pic/new-pic/non-pic stubs; altering this will break
31799      compatibility with existing dylibs.  */
31800   if (MACHOPIC_PURE)
31801     {
31802       /* 25-byte PIC stub using "CALL get_pc_thunk".  */
31803       switch_to_section (darwin_sections[machopic_lazy_symbol_ptr2_section]);
31804     }
31805   else
31806     /* 16-byte -mdynamic-no-pic stub.  */
31807     switch_to_section(darwin_sections[machopic_lazy_symbol_ptr3_section]);
31808
31809   fprintf (file, "%s:\n", lazy_ptr_name);
31810   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
31811   fprintf (file, ASM_LONG "%s\n", binder_name);
31812 }
31813 #endif /* TARGET_MACHO */
31814
31815 /* Order the registers for register allocator.  */
31816
31817 void
31818 x86_order_regs_for_local_alloc (void)
31819 {
31820    int pos = 0;
31821    int i;
31822
31823    /* First allocate the local general purpose registers.  */
31824    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
31825      if (GENERAL_REGNO_P (i) && call_used_regs[i])
31826         reg_alloc_order [pos++] = i;
31827
31828    /* Global general purpose registers.  */
31829    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
31830      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
31831         reg_alloc_order [pos++] = i;
31832
31833    /* x87 registers come first in case we are doing FP math
31834       using them.  */
31835    if (!TARGET_SSE_MATH)
31836      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
31837        reg_alloc_order [pos++] = i;
31838
31839    /* SSE registers.  */
31840    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
31841      reg_alloc_order [pos++] = i;
31842    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
31843      reg_alloc_order [pos++] = i;
31844
31845    /* x87 registers.  */
31846    if (TARGET_SSE_MATH)
31847      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
31848        reg_alloc_order [pos++] = i;
31849
31850    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
31851      reg_alloc_order [pos++] = i;
31852
31853    /* Initialize the rest of array as we do not allocate some registers
31854       at all.  */
31855    while (pos < FIRST_PSEUDO_REGISTER)
31856      reg_alloc_order [pos++] = 0;
31857 }
31858
31859 /* Handle a "callee_pop_aggregate_return" attribute; arguments as
31860    in struct attribute_spec handler.  */
31861 static tree
31862 ix86_handle_callee_pop_aggregate_return (tree *node, tree name,
31863                                               tree args,
31864                                               int flags ATTRIBUTE_UNUSED,
31865                                               bool *no_add_attrs)
31866 {
31867   if (TREE_CODE (*node) != FUNCTION_TYPE
31868       && TREE_CODE (*node) != METHOD_TYPE
31869       && TREE_CODE (*node) != FIELD_DECL
31870       && TREE_CODE (*node) != TYPE_DECL)
31871     {
31872       warning (OPT_Wattributes, "%qE attribute only applies to functions",
31873                name);
31874       *no_add_attrs = true;
31875       return NULL_TREE;
31876     }
31877   if (TARGET_64BIT)
31878     {
31879       warning (OPT_Wattributes, "%qE attribute only available for 32-bit",
31880                name);
31881       *no_add_attrs = true;
31882       return NULL_TREE;
31883     }
31884   if (is_attribute_p ("callee_pop_aggregate_return", name))
31885     {
31886       tree cst;
31887
31888       cst = TREE_VALUE (args);
31889       if (TREE_CODE (cst) != INTEGER_CST)
31890         {
31891           warning (OPT_Wattributes,
31892                    "%qE attribute requires an integer constant argument",
31893                    name);
31894           *no_add_attrs = true;
31895         }
31896       else if (compare_tree_int (cst, 0) != 0
31897                && compare_tree_int (cst, 1) != 0)
31898         {
31899           warning (OPT_Wattributes,
31900                    "argument to %qE attribute is neither zero, nor one",
31901                    name);
31902           *no_add_attrs = true;
31903         }
31904
31905       return NULL_TREE;
31906     }
31907
31908   return NULL_TREE;
31909 }
31910
31911 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
31912    struct attribute_spec.handler.  */
31913 static tree
31914 ix86_handle_abi_attribute (tree *node, tree name,
31915                               tree args ATTRIBUTE_UNUSED,
31916                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
31917 {
31918   if (TREE_CODE (*node) != FUNCTION_TYPE
31919       && TREE_CODE (*node) != METHOD_TYPE
31920       && TREE_CODE (*node) != FIELD_DECL
31921       && TREE_CODE (*node) != TYPE_DECL)
31922     {
31923       warning (OPT_Wattributes, "%qE attribute only applies to functions",
31924                name);
31925       *no_add_attrs = true;
31926       return NULL_TREE;
31927     }
31928
31929   /* Can combine regparm with all attributes but fastcall.  */
31930   if (is_attribute_p ("ms_abi", name))
31931     {
31932       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
31933         {
31934           error ("ms_abi and sysv_abi attributes are not compatible");
31935         }
31936
31937       return NULL_TREE;
31938     }
31939   else if (is_attribute_p ("sysv_abi", name))
31940     {
31941       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
31942         {
31943           error ("ms_abi and sysv_abi attributes are not compatible");
31944         }
31945
31946       return NULL_TREE;
31947     }
31948
31949   return NULL_TREE;
31950 }
31951
31952 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
31953    struct attribute_spec.handler.  */
31954 static tree
31955 ix86_handle_struct_attribute (tree *node, tree name,
31956                               tree args ATTRIBUTE_UNUSED,
31957                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
31958 {
31959   tree *type = NULL;
31960   if (DECL_P (*node))
31961     {
31962       if (TREE_CODE (*node) == TYPE_DECL)
31963         type = &TREE_TYPE (*node);
31964     }
31965   else
31966     type = node;
31967
31968   if (!(type && RECORD_OR_UNION_TYPE_P (*type)))
31969     {
31970       warning (OPT_Wattributes, "%qE attribute ignored",
31971                name);
31972       *no_add_attrs = true;
31973     }
31974
31975   else if ((is_attribute_p ("ms_struct", name)
31976             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
31977            || ((is_attribute_p ("gcc_struct", name)
31978                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
31979     {
31980       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
31981                name);
31982       *no_add_attrs = true;
31983     }
31984
31985   return NULL_TREE;
31986 }
31987
31988 static tree
31989 ix86_handle_fndecl_attribute (tree *node, tree name,
31990                               tree args ATTRIBUTE_UNUSED,
31991                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
31992 {
31993   if (TREE_CODE (*node) != FUNCTION_DECL)
31994     {
31995       warning (OPT_Wattributes, "%qE attribute only applies to functions",
31996                name);
31997       *no_add_attrs = true;
31998     }
31999   return NULL_TREE;
32000 }
32001
32002 static bool
32003 ix86_ms_bitfield_layout_p (const_tree record_type)
32004 {
32005   return ((TARGET_MS_BITFIELD_LAYOUT
32006            && !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
32007           || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type)));
32008 }
32009
32010 /* Returns an expression indicating where the this parameter is
32011    located on entry to the FUNCTION.  */
32012
32013 static rtx
32014 x86_this_parameter (tree function)
32015 {
32016   tree type = TREE_TYPE (function);
32017   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
32018   int nregs;
32019
32020   if (TARGET_64BIT)
32021     {
32022       const int *parm_regs;
32023
32024       if (ix86_function_type_abi (type) == MS_ABI)
32025         parm_regs = x86_64_ms_abi_int_parameter_registers;
32026       else
32027         parm_regs = x86_64_int_parameter_registers;
32028       return gen_rtx_REG (DImode, parm_regs[aggr]);
32029     }
32030
32031   nregs = ix86_function_regparm (type, function);
32032
32033   if (nregs > 0 && !stdarg_p (type))
32034     {
32035       int regno;
32036       unsigned int ccvt = ix86_get_callcvt (type);
32037
32038       if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
32039         regno = aggr ? DX_REG : CX_REG;
32040       else if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
32041         {
32042           regno = CX_REG;
32043           if (aggr)
32044             return gen_rtx_MEM (SImode,
32045                                 plus_constant (stack_pointer_rtx, 4));
32046         }
32047       else
32048         {
32049           regno = AX_REG;
32050           if (aggr)
32051             {
32052               regno = DX_REG;
32053               if (nregs == 1)
32054                 return gen_rtx_MEM (SImode,
32055                                     plus_constant (stack_pointer_rtx, 4));
32056             }
32057         }
32058       return gen_rtx_REG (SImode, regno);
32059     }
32060
32061   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
32062 }
32063
32064 /* Determine whether x86_output_mi_thunk can succeed.  */
32065
32066 static bool
32067 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
32068                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
32069                          HOST_WIDE_INT vcall_offset, const_tree function)
32070 {
32071   /* 64-bit can handle anything.  */
32072   if (TARGET_64BIT)
32073     return true;
32074
32075   /* For 32-bit, everything's fine if we have one free register.  */
32076   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
32077     return true;
32078
32079   /* Need a free register for vcall_offset.  */
32080   if (vcall_offset)
32081     return false;
32082
32083   /* Need a free register for GOT references.  */
32084   if (flag_pic && !targetm.binds_local_p (function))
32085     return false;
32086
32087   /* Otherwise ok.  */
32088   return true;
32089 }
32090
32091 /* Output the assembler code for a thunk function.  THUNK_DECL is the
32092    declaration for the thunk function itself, FUNCTION is the decl for
32093    the target function.  DELTA is an immediate constant offset to be
32094    added to THIS.  If VCALL_OFFSET is nonzero, the word at
32095    *(*this + vcall_offset) should be added to THIS.  */
32096
32097 static void
32098 x86_output_mi_thunk (FILE *file,
32099                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
32100                      HOST_WIDE_INT vcall_offset, tree function)
32101 {
32102   rtx this_param = x86_this_parameter (function);
32103   rtx this_reg, tmp, fnaddr;
32104   unsigned int tmp_regno;
32105
32106   if (TARGET_64BIT)
32107     tmp_regno = R10_REG;
32108   else
32109     {
32110       unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (function));
32111       if ((ccvt & (IX86_CALLCVT_FASTCALL | IX86_CALLCVT_THISCALL)) != 0)
32112         tmp_regno = AX_REG;
32113       else
32114         tmp_regno = CX_REG;
32115     }
32116
32117   emit_note (NOTE_INSN_PROLOGUE_END);
32118
32119   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
32120      pull it in now and let DELTA benefit.  */
32121   if (REG_P (this_param))
32122     this_reg = this_param;
32123   else if (vcall_offset)
32124     {
32125       /* Put the this parameter into %eax.  */
32126       this_reg = gen_rtx_REG (Pmode, AX_REG);
32127       emit_move_insn (this_reg, this_param);
32128     }
32129   else
32130     this_reg = NULL_RTX;
32131
32132   /* Adjust the this parameter by a fixed constant.  */
32133   if (delta)
32134     {
32135       rtx delta_rtx = GEN_INT (delta);
32136       rtx delta_dst = this_reg ? this_reg : this_param;
32137
32138       if (TARGET_64BIT)
32139         {
32140           if (!x86_64_general_operand (delta_rtx, Pmode))
32141             {
32142               tmp = gen_rtx_REG (Pmode, tmp_regno);
32143               emit_move_insn (tmp, delta_rtx);
32144               delta_rtx = tmp;
32145             }
32146         }
32147
32148       ix86_emit_binop (PLUS, Pmode, delta_dst, delta_rtx);
32149     }
32150
32151   /* Adjust the this parameter by a value stored in the vtable.  */
32152   if (vcall_offset)
32153     {
32154       rtx vcall_addr, vcall_mem, this_mem;
32155
32156       tmp = gen_rtx_REG (Pmode, tmp_regno);
32157
32158       this_mem = gen_rtx_MEM (ptr_mode, this_reg);
32159       if (Pmode != ptr_mode)
32160         this_mem = gen_rtx_ZERO_EXTEND (Pmode, this_mem);
32161       emit_move_insn (tmp, this_mem);
32162
32163       /* Adjust the this parameter.  */
32164       vcall_addr = plus_constant (tmp, vcall_offset);
32165       if (TARGET_64BIT
32166           && !ix86_legitimate_address_p (ptr_mode, vcall_addr, true))
32167         {
32168           rtx tmp2 = gen_rtx_REG (Pmode, R11_REG);
32169           emit_move_insn (tmp2, GEN_INT (vcall_offset));
32170           vcall_addr = gen_rtx_PLUS (Pmode, tmp, tmp2);
32171         }
32172
32173       vcall_mem = gen_rtx_MEM (ptr_mode, vcall_addr);
32174       if (Pmode != ptr_mode)
32175         emit_insn (gen_addsi_1_zext (this_reg,
32176                                      gen_rtx_REG (ptr_mode,
32177                                                   REGNO (this_reg)),
32178                                      vcall_mem));
32179       else
32180         ix86_emit_binop (PLUS, Pmode, this_reg, vcall_mem);
32181     }
32182
32183   /* If necessary, drop THIS back to its stack slot.  */
32184   if (this_reg && this_reg != this_param)
32185     emit_move_insn (this_param, this_reg);
32186
32187   fnaddr = XEXP (DECL_RTL (function), 0);
32188   if (TARGET_64BIT)
32189     {
32190       if (!flag_pic || targetm.binds_local_p (function)
32191           || cfun->machine->call_abi == MS_ABI)
32192         ;
32193       else
32194         {
32195           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, fnaddr), UNSPEC_GOTPCREL);
32196           tmp = gen_rtx_CONST (Pmode, tmp);
32197           fnaddr = gen_rtx_MEM (Pmode, tmp);
32198         }
32199     }
32200   else
32201     {
32202       if (!flag_pic || targetm.binds_local_p (function))
32203         ;
32204 #if TARGET_MACHO
32205       else if (TARGET_MACHO)
32206         {
32207           fnaddr = machopic_indirect_call_target (DECL_RTL (function));
32208           fnaddr = XEXP (fnaddr, 0);
32209         }
32210 #endif /* TARGET_MACHO */
32211       else
32212         {
32213           tmp = gen_rtx_REG (Pmode, CX_REG);
32214           output_set_got (tmp, NULL_RTX);
32215
32216           fnaddr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, fnaddr), UNSPEC_GOT);
32217           fnaddr = gen_rtx_PLUS (Pmode, fnaddr, tmp);
32218           fnaddr = gen_rtx_MEM (Pmode, fnaddr);
32219         }
32220     }
32221
32222   /* Our sibling call patterns do not allow memories, because we have no
32223      predicate that can distinguish between frame and non-frame memory.
32224      For our purposes here, we can get away with (ab)using a jump pattern,
32225      because we're going to do no optimization.  */
32226   if (MEM_P (fnaddr))
32227     emit_jump_insn (gen_indirect_jump (fnaddr));
32228   else
32229     {
32230       if (ix86_cmodel == CM_LARGE_PIC && SYMBOLIC_CONST (fnaddr))
32231         fnaddr = legitimize_pic_address (fnaddr,
32232                                          gen_rtx_REG (Pmode, tmp_regno));
32233
32234       if (!sibcall_insn_operand (fnaddr, Pmode))
32235         {
32236           tmp = gen_rtx_REG (Pmode, tmp_regno);
32237           if (GET_MODE (fnaddr) != Pmode)
32238             fnaddr = gen_rtx_ZERO_EXTEND (Pmode, fnaddr);
32239           emit_move_insn (tmp, fnaddr);
32240           fnaddr = tmp;
32241         }
32242
32243       tmp = gen_rtx_MEM (QImode, fnaddr);
32244       tmp = gen_rtx_CALL (VOIDmode, tmp, const0_rtx);
32245       tmp = emit_call_insn (tmp);
32246       SIBLING_CALL_P (tmp) = 1;
32247     }
32248   emit_barrier ();
32249
32250   /* Emit just enough of rest_of_compilation to get the insns emitted.
32251      Note that use_thunk calls assemble_start_function et al.  */
32252   tmp = get_insns ();
32253   insn_locators_alloc ();
32254   shorten_branches (tmp);
32255   final_start_function (tmp, file, 1);
32256   final (tmp, file, 1);
32257   final_end_function ();
32258 }
32259
32260 static void
32261 x86_file_start (void)
32262 {
32263   default_file_start ();
32264 #if TARGET_MACHO
32265   darwin_file_start ();
32266 #endif
32267   if (X86_FILE_START_VERSION_DIRECTIVE)
32268     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
32269   if (X86_FILE_START_FLTUSED)
32270     fputs ("\t.global\t__fltused\n", asm_out_file);
32271   if (ix86_asm_dialect == ASM_INTEL)
32272     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
32273 }
32274
32275 int
32276 x86_field_alignment (tree field, int computed)
32277 {
32278   enum machine_mode mode;
32279   tree type = TREE_TYPE (field);
32280
32281   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
32282     return computed;
32283   mode = TYPE_MODE (strip_array_types (type));
32284   if (mode == DFmode || mode == DCmode
32285       || GET_MODE_CLASS (mode) == MODE_INT
32286       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
32287     return MIN (32, computed);
32288   return computed;
32289 }
32290
32291 /* Output assembler code to FILE to increment profiler label # LABELNO
32292    for profiling a function entry.  */
32293 void
32294 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
32295 {
32296   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
32297                                          : MCOUNT_NAME);
32298
32299   if (TARGET_64BIT)
32300     {
32301 #ifndef NO_PROFILE_COUNTERS
32302       fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
32303 #endif
32304
32305       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
32306         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name);
32307       else
32308         fprintf (file, "\tcall\t%s\n", mcount_name);
32309     }
32310   else if (flag_pic)
32311     {
32312 #ifndef NO_PROFILE_COUNTERS
32313       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
32314                LPREFIX, labelno);
32315 #endif
32316       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
32317     }
32318   else
32319     {
32320 #ifndef NO_PROFILE_COUNTERS
32321       fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
32322                LPREFIX, labelno);
32323 #endif
32324       fprintf (file, "\tcall\t%s\n", mcount_name);
32325     }
32326 }
32327
32328 /* We don't have exact information about the insn sizes, but we may assume
32329    quite safely that we are informed about all 1 byte insns and memory
32330    address sizes.  This is enough to eliminate unnecessary padding in
32331    99% of cases.  */
32332
32333 static int
32334 min_insn_size (rtx insn)
32335 {
32336   int l = 0, len;
32337
32338   if (!INSN_P (insn) || !active_insn_p (insn))
32339     return 0;
32340
32341   /* Discard alignments we've emit and jump instructions.  */
32342   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
32343       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
32344     return 0;
32345   if (JUMP_TABLE_DATA_P (insn))
32346     return 0;
32347
32348   /* Important case - calls are always 5 bytes.
32349      It is common to have many calls in the row.  */
32350   if (CALL_P (insn)
32351       && symbolic_reference_mentioned_p (PATTERN (insn))
32352       && !SIBLING_CALL_P (insn))
32353     return 5;
32354   len = get_attr_length (insn);
32355   if (len <= 1)
32356     return 1;
32357
32358   /* For normal instructions we rely on get_attr_length being exact,
32359      with a few exceptions.  */
32360   if (!JUMP_P (insn))
32361     {
32362       enum attr_type type = get_attr_type (insn);
32363
32364       switch (type)
32365         {
32366         case TYPE_MULTI:
32367           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
32368               || asm_noperands (PATTERN (insn)) >= 0)
32369             return 0;
32370           break;
32371         case TYPE_OTHER:
32372         case TYPE_FCMP:
32373           break;
32374         default:
32375           /* Otherwise trust get_attr_length.  */
32376           return len;
32377         }
32378
32379       l = get_attr_length_address (insn);
32380       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
32381         l = 4;
32382     }
32383   if (l)
32384     return 1+l;
32385   else
32386     return 2;
32387 }
32388
32389 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
32390
32391 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
32392    window.  */
32393
32394 static void
32395 ix86_avoid_jump_mispredicts (void)
32396 {
32397   rtx insn, start = get_insns ();
32398   int nbytes = 0, njumps = 0;
32399   int isjump = 0;
32400
32401   /* Look for all minimal intervals of instructions containing 4 jumps.
32402      The intervals are bounded by START and INSN.  NBYTES is the total
32403      size of instructions in the interval including INSN and not including
32404      START.  When the NBYTES is smaller than 16 bytes, it is possible
32405      that the end of START and INSN ends up in the same 16byte page.
32406
32407      The smallest offset in the page INSN can start is the case where START
32408      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
32409      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
32410      */
32411   for (insn = start; insn; insn = NEXT_INSN (insn))
32412     {
32413       int min_size;
32414
32415       if (LABEL_P (insn))
32416         {
32417           int align = label_to_alignment (insn);
32418           int max_skip = label_to_max_skip (insn);
32419
32420           if (max_skip > 15)
32421             max_skip = 15;
32422           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
32423              already in the current 16 byte page, because otherwise
32424              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
32425              bytes to reach 16 byte boundary.  */
32426           if (align <= 0
32427               || (align <= 3 && max_skip != (1 << align) - 1))
32428             max_skip = 0;
32429           if (dump_file)
32430             fprintf (dump_file, "Label %i with max_skip %i\n",
32431                      INSN_UID (insn), max_skip);
32432           if (max_skip)
32433             {
32434               while (nbytes + max_skip >= 16)
32435                 {
32436                   start = NEXT_INSN (start);
32437                   if ((JUMP_P (start)
32438                        && GET_CODE (PATTERN (start)) != ADDR_VEC
32439                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
32440                       || CALL_P (start))
32441                     njumps--, isjump = 1;
32442                   else
32443                     isjump = 0;
32444                   nbytes -= min_insn_size (start);
32445                 }
32446             }
32447           continue;
32448         }
32449
32450       min_size = min_insn_size (insn);
32451       nbytes += min_size;
32452       if (dump_file)
32453         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
32454                  INSN_UID (insn), min_size);
32455       if ((JUMP_P (insn)
32456            && GET_CODE (PATTERN (insn)) != ADDR_VEC
32457            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
32458           || CALL_P (insn))
32459         njumps++;
32460       else
32461         continue;
32462
32463       while (njumps > 3)
32464         {
32465           start = NEXT_INSN (start);
32466           if ((JUMP_P (start)
32467                && GET_CODE (PATTERN (start)) != ADDR_VEC
32468                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
32469               || CALL_P (start))
32470             njumps--, isjump = 1;
32471           else
32472             isjump = 0;
32473           nbytes -= min_insn_size (start);
32474         }
32475       gcc_assert (njumps >= 0);
32476       if (dump_file)
32477         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
32478                  INSN_UID (start), INSN_UID (insn), nbytes);
32479
32480       if (njumps == 3 && isjump && nbytes < 16)
32481         {
32482           int padsize = 15 - nbytes + min_insn_size (insn);
32483
32484           if (dump_file)
32485             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
32486                      INSN_UID (insn), padsize);
32487           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
32488         }
32489     }
32490 }
32491 #endif
32492
32493 /* AMD Athlon works faster
32494    when RET is not destination of conditional jump or directly preceded
32495    by other jump instruction.  We avoid the penalty by inserting NOP just
32496    before the RET instructions in such cases.  */
32497 static void
32498 ix86_pad_returns (void)
32499 {
32500   edge e;
32501   edge_iterator ei;
32502
32503   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
32504     {
32505       basic_block bb = e->src;
32506       rtx ret = BB_END (bb);
32507       rtx prev;
32508       bool replace = false;
32509
32510       if (!JUMP_P (ret) || !ANY_RETURN_P (PATTERN (ret))
32511           || optimize_bb_for_size_p (bb))
32512         continue;
32513       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
32514         if (active_insn_p (prev) || LABEL_P (prev))
32515           break;
32516       if (prev && LABEL_P (prev))
32517         {
32518           edge e;
32519           edge_iterator ei;
32520
32521           FOR_EACH_EDGE (e, ei, bb->preds)
32522             if (EDGE_FREQUENCY (e) && e->src->index >= 0
32523                 && !(e->flags & EDGE_FALLTHRU))
32524               replace = true;
32525         }
32526       if (!replace)
32527         {
32528           prev = prev_active_insn (ret);
32529           if (prev
32530               && ((JUMP_P (prev) && any_condjump_p (prev))
32531                   || CALL_P (prev)))
32532             replace = true;
32533           /* Empty functions get branch mispredict even when
32534              the jump destination is not visible to us.  */
32535           if (!prev && !optimize_function_for_size_p (cfun))
32536             replace = true;
32537         }
32538       if (replace)
32539         {
32540           emit_jump_insn_before (gen_simple_return_internal_long (), ret);
32541           delete_insn (ret);
32542         }
32543     }
32544 }
32545
32546 /* Count the minimum number of instructions in BB.  Return 4 if the
32547    number of instructions >= 4.  */
32548
32549 static int
32550 ix86_count_insn_bb (basic_block bb)
32551 {
32552   rtx insn;
32553   int insn_count = 0;
32554
32555   /* Count number of instructions in this block.  Return 4 if the number
32556      of instructions >= 4.  */
32557   FOR_BB_INSNS (bb, insn)
32558     {
32559       /* Only happen in exit blocks.  */
32560       if (JUMP_P (insn)
32561           && ANY_RETURN_P (PATTERN (insn)))
32562         break;
32563
32564       if (NONDEBUG_INSN_P (insn)
32565           && GET_CODE (PATTERN (insn)) != USE
32566           && GET_CODE (PATTERN (insn)) != CLOBBER)
32567         {
32568           insn_count++;
32569           if (insn_count >= 4)
32570             return insn_count;
32571         }
32572     }
32573
32574   return insn_count;
32575 }
32576
32577
32578 /* Count the minimum number of instructions in code path in BB.
32579    Return 4 if the number of instructions >= 4.  */
32580
32581 static int
32582 ix86_count_insn (basic_block bb)
32583 {
32584   edge e;
32585   edge_iterator ei;
32586   int min_prev_count;
32587
32588   /* Only bother counting instructions along paths with no
32589      more than 2 basic blocks between entry and exit.  Given
32590      that BB has an edge to exit, determine if a predecessor
32591      of BB has an edge from entry.  If so, compute the number
32592      of instructions in the predecessor block.  If there
32593      happen to be multiple such blocks, compute the minimum.  */
32594   min_prev_count = 4;
32595   FOR_EACH_EDGE (e, ei, bb->preds)
32596     {
32597       edge prev_e;
32598       edge_iterator prev_ei;
32599
32600       if (e->src == ENTRY_BLOCK_PTR)
32601         {
32602           min_prev_count = 0;
32603           break;
32604         }
32605       FOR_EACH_EDGE (prev_e, prev_ei, e->src->preds)
32606         {
32607           if (prev_e->src == ENTRY_BLOCK_PTR)
32608             {
32609               int count = ix86_count_insn_bb (e->src);
32610               if (count < min_prev_count)
32611                 min_prev_count = count;
32612               break;
32613             }
32614         }
32615     }
32616
32617   if (min_prev_count < 4)
32618     min_prev_count += ix86_count_insn_bb (bb);
32619
32620   return min_prev_count;
32621 }
32622
32623 /* Pad short funtion to 4 instructions.   */
32624
32625 static void
32626 ix86_pad_short_function (void)
32627 {
32628   edge e;
32629   edge_iterator ei;
32630
32631   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
32632     {
32633       rtx ret = BB_END (e->src);
32634       if (JUMP_P (ret) && ANY_RETURN_P (PATTERN (ret)))
32635         {
32636           int insn_count = ix86_count_insn (e->src);
32637
32638           /* Pad short function.  */
32639           if (insn_count < 4)
32640             {
32641               rtx insn = ret;
32642
32643               /* Find epilogue.  */
32644               while (insn
32645                      && (!NOTE_P (insn)
32646                          || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG))
32647                 insn = PREV_INSN (insn);
32648
32649               if (!insn)
32650                 insn = ret;
32651
32652               /* Two NOPs count as one instruction.  */
32653               insn_count = 2 * (4 - insn_count);
32654               emit_insn_before (gen_nops (GEN_INT (insn_count)), insn);
32655             }
32656         }
32657     }
32658 }
32659
32660 /* Implement machine specific optimizations.  We implement padding of returns
32661    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
32662 static void
32663 ix86_reorg (void)
32664 {
32665   /* We are freeing block_for_insn in the toplev to keep compatibility
32666      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
32667   compute_bb_for_insn ();
32668
32669   /* Run the vzeroupper optimization if needed.  */
32670   if (TARGET_VZEROUPPER)
32671     move_or_delete_vzeroupper ();
32672
32673   if (optimize && optimize_function_for_speed_p (cfun))
32674     {
32675       if (TARGET_PAD_SHORT_FUNCTION)
32676         ix86_pad_short_function ();
32677       else if (TARGET_PAD_RETURNS)
32678         ix86_pad_returns ();
32679 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
32680       if (TARGET_FOUR_JUMP_LIMIT)
32681         ix86_avoid_jump_mispredicts ();
32682 #endif
32683     }
32684 }
32685
32686 /* Return nonzero when QImode register that must be represented via REX prefix
32687    is used.  */
32688 bool
32689 x86_extended_QIreg_mentioned_p (rtx insn)
32690 {
32691   int i;
32692   extract_insn_cached (insn);
32693   for (i = 0; i < recog_data.n_operands; i++)
32694     if (REG_P (recog_data.operand[i])
32695         && REGNO (recog_data.operand[i]) > BX_REG)
32696        return true;
32697   return false;
32698 }
32699
32700 /* Return nonzero when P points to register encoded via REX prefix.
32701    Called via for_each_rtx.  */
32702 static int
32703 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
32704 {
32705    unsigned int regno;
32706    if (!REG_P (*p))
32707      return 0;
32708    regno = REGNO (*p);
32709    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
32710 }
32711
32712 /* Return true when INSN mentions register that must be encoded using REX
32713    prefix.  */
32714 bool
32715 x86_extended_reg_mentioned_p (rtx insn)
32716 {
32717   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
32718                        extended_reg_mentioned_1, NULL);
32719 }
32720
32721 /* If profitable, negate (without causing overflow) integer constant
32722    of mode MODE at location LOC.  Return true in this case.  */
32723 bool
32724 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
32725 {
32726   HOST_WIDE_INT val;
32727
32728   if (!CONST_INT_P (*loc))
32729     return false;
32730
32731   switch (mode)
32732     {
32733     case DImode:
32734       /* DImode x86_64 constants must fit in 32 bits.  */
32735       gcc_assert (x86_64_immediate_operand (*loc, mode));
32736
32737       mode = SImode;
32738       break;
32739
32740     case SImode:
32741     case HImode:
32742     case QImode:
32743       break;
32744
32745     default:
32746       gcc_unreachable ();
32747     }
32748
32749   /* Avoid overflows.  */
32750   if (mode_signbit_p (mode, *loc))
32751     return false;
32752
32753   val = INTVAL (*loc);
32754
32755   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
32756      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
32757   if ((val < 0 && val != -128)
32758       || val == 128)
32759     {
32760       *loc = GEN_INT (-val);
32761       return true;
32762     }
32763
32764   return false;
32765 }
32766
32767 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
32768    optabs would emit if we didn't have TFmode patterns.  */
32769
32770 void
32771 x86_emit_floatuns (rtx operands[2])
32772 {
32773   rtx neglab, donelab, i0, i1, f0, in, out;
32774   enum machine_mode mode, inmode;
32775
32776   inmode = GET_MODE (operands[1]);
32777   gcc_assert (inmode == SImode || inmode == DImode);
32778
32779   out = operands[0];
32780   in = force_reg (inmode, operands[1]);
32781   mode = GET_MODE (out);
32782   neglab = gen_label_rtx ();
32783   donelab = gen_label_rtx ();
32784   f0 = gen_reg_rtx (mode);
32785
32786   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
32787
32788   expand_float (out, in, 0);
32789
32790   emit_jump_insn (gen_jump (donelab));
32791   emit_barrier ();
32792
32793   emit_label (neglab);
32794
32795   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
32796                             1, OPTAB_DIRECT);
32797   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
32798                             1, OPTAB_DIRECT);
32799   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
32800
32801   expand_float (f0, i0, 0);
32802
32803   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
32804
32805   emit_label (donelab);
32806 }
32807 \f
32808 /* AVX2 does support 32-byte integer vector operations,
32809    thus the longest vector we are faced with is V32QImode.  */
32810 #define MAX_VECT_LEN    32
32811
32812 struct expand_vec_perm_d
32813 {
32814   rtx target, op0, op1;
32815   unsigned char perm[MAX_VECT_LEN];
32816   enum machine_mode vmode;
32817   unsigned char nelt;
32818   bool testing_p;
32819 };
32820
32821 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
32822 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
32823
32824 /* Get a vector mode of the same size as the original but with elements
32825    twice as wide.  This is only guaranteed to apply to integral vectors.  */
32826
32827 static inline enum machine_mode
32828 get_mode_wider_vector (enum machine_mode o)
32829 {
32830   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
32831   enum machine_mode n = GET_MODE_WIDER_MODE (o);
32832   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
32833   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
32834   return n;
32835 }
32836
32837 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
32838    with all elements equal to VAR.  Return true if successful.  */
32839
32840 static bool
32841 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
32842                                    rtx target, rtx val)
32843 {
32844   bool ok;
32845
32846   switch (mode)
32847     {
32848     case V2SImode:
32849     case V2SFmode:
32850       if (!mmx_ok)
32851         return false;
32852       /* FALLTHRU */
32853
32854     case V4DFmode:
32855     case V4DImode:
32856     case V8SFmode:
32857     case V8SImode:
32858     case V2DFmode:
32859     case V2DImode:
32860     case V4SFmode:
32861     case V4SImode:
32862       {
32863         rtx insn, dup;
32864
32865         /* First attempt to recognize VAL as-is.  */
32866         dup = gen_rtx_VEC_DUPLICATE (mode, val);
32867         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
32868         if (recog_memoized (insn) < 0)
32869           {
32870             rtx seq;
32871             /* If that fails, force VAL into a register.  */
32872
32873             start_sequence ();
32874             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
32875             seq = get_insns ();
32876             end_sequence ();
32877             if (seq)
32878               emit_insn_before (seq, insn);
32879
32880             ok = recog_memoized (insn) >= 0;
32881             gcc_assert (ok);
32882           }
32883       }
32884       return true;
32885
32886     case V4HImode:
32887       if (!mmx_ok)
32888         return false;
32889       if (TARGET_SSE || TARGET_3DNOW_A)
32890         {
32891           rtx x;
32892
32893           val = gen_lowpart (SImode, val);
32894           x = gen_rtx_TRUNCATE (HImode, val);
32895           x = gen_rtx_VEC_DUPLICATE (mode, x);
32896           emit_insn (gen_rtx_SET (VOIDmode, target, x));
32897           return true;
32898         }
32899       goto widen;
32900
32901     case V8QImode:
32902       if (!mmx_ok)
32903         return false;
32904       goto widen;
32905
32906     case V8HImode:
32907       if (TARGET_SSE2)
32908         {
32909           struct expand_vec_perm_d dperm;
32910           rtx tmp1, tmp2;
32911
32912         permute:
32913           memset (&dperm, 0, sizeof (dperm));
32914           dperm.target = target;
32915           dperm.vmode = mode;
32916           dperm.nelt = GET_MODE_NUNITS (mode);
32917           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
32918
32919           /* Extend to SImode using a paradoxical SUBREG.  */
32920           tmp1 = gen_reg_rtx (SImode);
32921           emit_move_insn (tmp1, gen_lowpart (SImode, val));
32922
32923           /* Insert the SImode value as low element of a V4SImode vector. */
32924           tmp2 = gen_lowpart (V4SImode, dperm.op0);
32925           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
32926
32927           ok = (expand_vec_perm_1 (&dperm)
32928                 || expand_vec_perm_broadcast_1 (&dperm));
32929           gcc_assert (ok);
32930           return ok;
32931         }
32932       goto widen;
32933
32934     case V16QImode:
32935       if (TARGET_SSE2)
32936         goto permute;
32937       goto widen;
32938
32939     widen:
32940       /* Replicate the value once into the next wider mode and recurse.  */
32941       {
32942         enum machine_mode smode, wsmode, wvmode;
32943         rtx x;
32944
32945         smode = GET_MODE_INNER (mode);
32946         wvmode = get_mode_wider_vector (mode);
32947         wsmode = GET_MODE_INNER (wvmode);
32948
32949         val = convert_modes (wsmode, smode, val, true);
32950         x = expand_simple_binop (wsmode, ASHIFT, val,
32951                                  GEN_INT (GET_MODE_BITSIZE (smode)),
32952                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
32953         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
32954
32955         x = gen_lowpart (wvmode, target);
32956         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
32957         gcc_assert (ok);
32958         return ok;
32959       }
32960
32961     case V16HImode:
32962     case V32QImode:
32963       {
32964         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
32965         rtx x = gen_reg_rtx (hvmode);
32966
32967         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
32968         gcc_assert (ok);
32969
32970         x = gen_rtx_VEC_CONCAT (mode, x, x);
32971         emit_insn (gen_rtx_SET (VOIDmode, target, x));
32972       }
32973       return true;
32974
32975     default:
32976       return false;
32977     }
32978 }
32979
32980 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
32981    whose ONE_VAR element is VAR, and other elements are zero.  Return true
32982    if successful.  */
32983
32984 static bool
32985 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
32986                                      rtx target, rtx var, int one_var)
32987 {
32988   enum machine_mode vsimode;
32989   rtx new_target;
32990   rtx x, tmp;
32991   bool use_vector_set = false;
32992
32993   switch (mode)
32994     {
32995     case V2DImode:
32996       /* For SSE4.1, we normally use vector set.  But if the second
32997          element is zero and inter-unit moves are OK, we use movq
32998          instead.  */
32999       use_vector_set = (TARGET_64BIT
33000                         && TARGET_SSE4_1
33001                         && !(TARGET_INTER_UNIT_MOVES
33002                              && one_var == 0));
33003       break;
33004     case V16QImode:
33005     case V4SImode:
33006     case V4SFmode:
33007       use_vector_set = TARGET_SSE4_1;
33008       break;
33009     case V8HImode:
33010       use_vector_set = TARGET_SSE2;
33011       break;
33012     case V4HImode:
33013       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
33014       break;
33015     case V32QImode:
33016     case V16HImode:
33017     case V8SImode:
33018     case V8SFmode:
33019     case V4DFmode:
33020       use_vector_set = TARGET_AVX;
33021       break;
33022     case V4DImode:
33023       /* Use ix86_expand_vector_set in 64bit mode only.  */
33024       use_vector_set = TARGET_AVX && TARGET_64BIT;
33025       break;
33026     default:
33027       break;
33028     }
33029
33030   if (use_vector_set)
33031     {
33032       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
33033       var = force_reg (GET_MODE_INNER (mode), var);
33034       ix86_expand_vector_set (mmx_ok, target, var, one_var);
33035       return true;
33036     }
33037
33038   switch (mode)
33039     {
33040     case V2SFmode:
33041     case V2SImode:
33042       if (!mmx_ok)
33043         return false;
33044       /* FALLTHRU */
33045
33046     case V2DFmode:
33047     case V2DImode:
33048       if (one_var != 0)
33049         return false;
33050       var = force_reg (GET_MODE_INNER (mode), var);
33051       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
33052       emit_insn (gen_rtx_SET (VOIDmode, target, x));
33053       return true;
33054
33055     case V4SFmode:
33056     case V4SImode:
33057       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
33058         new_target = gen_reg_rtx (mode);
33059       else
33060         new_target = target;
33061       var = force_reg (GET_MODE_INNER (mode), var);
33062       x = gen_rtx_VEC_DUPLICATE (mode, var);
33063       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
33064       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
33065       if (one_var != 0)
33066         {
33067           /* We need to shuffle the value to the correct position, so
33068              create a new pseudo to store the intermediate result.  */
33069
33070           /* With SSE2, we can use the integer shuffle insns.  */
33071           if (mode != V4SFmode && TARGET_SSE2)
33072             {
33073               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
33074                                             const1_rtx,
33075                                             GEN_INT (one_var == 1 ? 0 : 1),
33076                                             GEN_INT (one_var == 2 ? 0 : 1),
33077                                             GEN_INT (one_var == 3 ? 0 : 1)));
33078               if (target != new_target)
33079                 emit_move_insn (target, new_target);
33080               return true;
33081             }
33082
33083           /* Otherwise convert the intermediate result to V4SFmode and
33084              use the SSE1 shuffle instructions.  */
33085           if (mode != V4SFmode)
33086             {
33087               tmp = gen_reg_rtx (V4SFmode);
33088               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
33089             }
33090           else
33091             tmp = new_target;
33092
33093           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
33094                                        const1_rtx,
33095                                        GEN_INT (one_var == 1 ? 0 : 1),
33096                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
33097                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
33098
33099           if (mode != V4SFmode)
33100             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
33101           else if (tmp != target)
33102             emit_move_insn (target, tmp);
33103         }
33104       else if (target != new_target)
33105         emit_move_insn (target, new_target);
33106       return true;
33107
33108     case V8HImode:
33109     case V16QImode:
33110       vsimode = V4SImode;
33111       goto widen;
33112     case V4HImode:
33113     case V8QImode:
33114       if (!mmx_ok)
33115         return false;
33116       vsimode = V2SImode;
33117       goto widen;
33118     widen:
33119       if (one_var != 0)
33120         return false;
33121
33122       /* Zero extend the variable element to SImode and recurse.  */
33123       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
33124
33125       x = gen_reg_rtx (vsimode);
33126       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
33127                                                 var, one_var))
33128         gcc_unreachable ();
33129
33130       emit_move_insn (target, gen_lowpart (mode, x));
33131       return true;
33132
33133     default:
33134       return false;
33135     }
33136 }
33137
33138 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
33139    consisting of the values in VALS.  It is known that all elements
33140    except ONE_VAR are constants.  Return true if successful.  */
33141
33142 static bool
33143 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
33144                                  rtx target, rtx vals, int one_var)
33145 {
33146   rtx var = XVECEXP (vals, 0, one_var);
33147   enum machine_mode wmode;
33148   rtx const_vec, x;
33149
33150   const_vec = copy_rtx (vals);
33151   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
33152   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
33153
33154   switch (mode)
33155     {
33156     case V2DFmode:
33157     case V2DImode:
33158     case V2SFmode:
33159     case V2SImode:
33160       /* For the two element vectors, it's just as easy to use
33161          the general case.  */
33162       return false;
33163
33164     case V4DImode:
33165       /* Use ix86_expand_vector_set in 64bit mode only.  */
33166       if (!TARGET_64BIT)
33167         return false;
33168     case V4DFmode:
33169     case V8SFmode:
33170     case V8SImode:
33171     case V16HImode:
33172     case V32QImode:
33173     case V4SFmode:
33174     case V4SImode:
33175     case V8HImode:
33176     case V4HImode:
33177       break;
33178
33179     case V16QImode:
33180       if (TARGET_SSE4_1)
33181         break;
33182       wmode = V8HImode;
33183       goto widen;
33184     case V8QImode:
33185       wmode = V4HImode;
33186       goto widen;
33187     widen:
33188       /* There's no way to set one QImode entry easily.  Combine
33189          the variable value with its adjacent constant value, and
33190          promote to an HImode set.  */
33191       x = XVECEXP (vals, 0, one_var ^ 1);
33192       if (one_var & 1)
33193         {
33194           var = convert_modes (HImode, QImode, var, true);
33195           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
33196                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
33197           x = GEN_INT (INTVAL (x) & 0xff);
33198         }
33199       else
33200         {
33201           var = convert_modes (HImode, QImode, var, true);
33202           x = gen_int_mode (INTVAL (x) << 8, HImode);
33203         }
33204       if (x != const0_rtx)
33205         var = expand_simple_binop (HImode, IOR, var, x, var,
33206                                    1, OPTAB_LIB_WIDEN);
33207
33208       x = gen_reg_rtx (wmode);
33209       emit_move_insn (x, gen_lowpart (wmode, const_vec));
33210       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
33211
33212       emit_move_insn (target, gen_lowpart (mode, x));
33213       return true;
33214
33215     default:
33216       return false;
33217     }
33218
33219   emit_move_insn (target, const_vec);
33220   ix86_expand_vector_set (mmx_ok, target, var, one_var);
33221   return true;
33222 }
33223
33224 /* A subroutine of ix86_expand_vector_init_general.  Use vector
33225    concatenate to handle the most general case: all values variable,
33226    and none identical.  */
33227
33228 static void
33229 ix86_expand_vector_init_concat (enum machine_mode mode,
33230                                 rtx target, rtx *ops, int n)
33231 {
33232   enum machine_mode cmode, hmode = VOIDmode;
33233   rtx first[8], second[4];
33234   rtvec v;
33235   int i, j;
33236
33237   switch (n)
33238     {
33239     case 2:
33240       switch (mode)
33241         {
33242         case V8SImode:
33243           cmode = V4SImode;
33244           break;
33245         case V8SFmode:
33246           cmode = V4SFmode;
33247           break;
33248         case V4DImode:
33249           cmode = V2DImode;
33250           break;
33251         case V4DFmode:
33252           cmode = V2DFmode;
33253           break;
33254         case V4SImode:
33255           cmode = V2SImode;
33256           break;
33257         case V4SFmode:
33258           cmode = V2SFmode;
33259           break;
33260         case V2DImode:
33261           cmode = DImode;
33262           break;
33263         case V2SImode:
33264           cmode = SImode;
33265           break;
33266         case V2DFmode:
33267           cmode = DFmode;
33268           break;
33269         case V2SFmode:
33270           cmode = SFmode;
33271           break;
33272         default:
33273           gcc_unreachable ();
33274         }
33275
33276       if (!register_operand (ops[1], cmode))
33277         ops[1] = force_reg (cmode, ops[1]);
33278       if (!register_operand (ops[0], cmode))
33279         ops[0] = force_reg (cmode, ops[0]);
33280       emit_insn (gen_rtx_SET (VOIDmode, target,
33281                               gen_rtx_VEC_CONCAT (mode, ops[0],
33282                                                   ops[1])));
33283       break;
33284
33285     case 4:
33286       switch (mode)
33287         {
33288         case V4DImode:
33289           cmode = V2DImode;
33290           break;
33291         case V4DFmode:
33292           cmode = V2DFmode;
33293           break;
33294         case V4SImode:
33295           cmode = V2SImode;
33296           break;
33297         case V4SFmode:
33298           cmode = V2SFmode;
33299           break;
33300         default:
33301           gcc_unreachable ();
33302         }
33303       goto half;
33304
33305     case 8:
33306       switch (mode)
33307         {
33308         case V8SImode:
33309           cmode = V2SImode;
33310           hmode = V4SImode;
33311           break;
33312         case V8SFmode:
33313           cmode = V2SFmode;
33314           hmode = V4SFmode;
33315           break;
33316         default:
33317           gcc_unreachable ();
33318         }
33319       goto half;
33320
33321 half:
33322       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
33323       i = n - 1;
33324       j = (n >> 1) - 1;
33325       for (; i > 0; i -= 2, j--)
33326         {
33327           first[j] = gen_reg_rtx (cmode);
33328           v = gen_rtvec (2, ops[i - 1], ops[i]);
33329           ix86_expand_vector_init (false, first[j],
33330                                    gen_rtx_PARALLEL (cmode, v));
33331         }
33332
33333       n >>= 1;
33334       if (n > 2)
33335         {
33336           gcc_assert (hmode != VOIDmode);
33337           for (i = j = 0; i < n; i += 2, j++)
33338             {
33339               second[j] = gen_reg_rtx (hmode);
33340               ix86_expand_vector_init_concat (hmode, second [j],
33341                                               &first [i], 2);
33342             }
33343           n >>= 1;
33344           ix86_expand_vector_init_concat (mode, target, second, n);
33345         }
33346       else
33347         ix86_expand_vector_init_concat (mode, target, first, n);
33348       break;
33349
33350     default:
33351       gcc_unreachable ();
33352     }
33353 }
33354
33355 /* A subroutine of ix86_expand_vector_init_general.  Use vector
33356    interleave to handle the most general case: all values variable,
33357    and none identical.  */
33358
33359 static void
33360 ix86_expand_vector_init_interleave (enum machine_mode mode,
33361                                     rtx target, rtx *ops, int n)
33362 {
33363   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
33364   int i, j;
33365   rtx op0, op1;
33366   rtx (*gen_load_even) (rtx, rtx, rtx);
33367   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
33368   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
33369
33370   switch (mode)
33371     {
33372     case V8HImode:
33373       gen_load_even = gen_vec_setv8hi;
33374       gen_interleave_first_low = gen_vec_interleave_lowv4si;
33375       gen_interleave_second_low = gen_vec_interleave_lowv2di;
33376       inner_mode = HImode;
33377       first_imode = V4SImode;
33378       second_imode = V2DImode;
33379       third_imode = VOIDmode;
33380       break;
33381     case V16QImode:
33382       gen_load_even = gen_vec_setv16qi;
33383       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
33384       gen_interleave_second_low = gen_vec_interleave_lowv4si;
33385       inner_mode = QImode;
33386       first_imode = V8HImode;
33387       second_imode = V4SImode;
33388       third_imode = V2DImode;
33389       break;
33390     default:
33391       gcc_unreachable ();
33392     }
33393
33394   for (i = 0; i < n; i++)
33395     {
33396       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
33397       op0 = gen_reg_rtx (SImode);
33398       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
33399
33400       /* Insert the SImode value as low element of V4SImode vector. */
33401       op1 = gen_reg_rtx (V4SImode);
33402       op0 = gen_rtx_VEC_MERGE (V4SImode,
33403                                gen_rtx_VEC_DUPLICATE (V4SImode,
33404                                                       op0),
33405                                CONST0_RTX (V4SImode),
33406                                const1_rtx);
33407       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
33408
33409       /* Cast the V4SImode vector back to a vector in orignal mode.  */
33410       op0 = gen_reg_rtx (mode);
33411       emit_move_insn (op0, gen_lowpart (mode, op1));
33412
33413       /* Load even elements into the second positon.  */
33414       emit_insn (gen_load_even (op0,
33415                                 force_reg (inner_mode,
33416                                            ops [i + i + 1]),
33417                                 const1_rtx));
33418
33419       /* Cast vector to FIRST_IMODE vector.  */
33420       ops[i] = gen_reg_rtx (first_imode);
33421       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
33422     }
33423
33424   /* Interleave low FIRST_IMODE vectors.  */
33425   for (i = j = 0; i < n; i += 2, j++)
33426     {
33427       op0 = gen_reg_rtx (first_imode);
33428       emit_insn (gen_interleave_first_low (op0, ops[i], ops[i + 1]));
33429
33430       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
33431       ops[j] = gen_reg_rtx (second_imode);
33432       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
33433     }
33434
33435   /* Interleave low SECOND_IMODE vectors.  */
33436   switch (second_imode)
33437     {
33438     case V4SImode:
33439       for (i = j = 0; i < n / 2; i += 2, j++)
33440         {
33441           op0 = gen_reg_rtx (second_imode);
33442           emit_insn (gen_interleave_second_low (op0, ops[i],
33443                                                 ops[i + 1]));
33444
33445           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
33446              vector.  */
33447           ops[j] = gen_reg_rtx (third_imode);
33448           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
33449         }
33450       second_imode = V2DImode;
33451       gen_interleave_second_low = gen_vec_interleave_lowv2di;
33452       /* FALLTHRU */
33453
33454     case V2DImode:
33455       op0 = gen_reg_rtx (second_imode);
33456       emit_insn (gen_interleave_second_low (op0, ops[0],
33457                                             ops[1]));
33458
33459       /* Cast the SECOND_IMODE vector back to a vector on original
33460          mode.  */
33461       emit_insn (gen_rtx_SET (VOIDmode, target,
33462                               gen_lowpart (mode, op0)));
33463       break;
33464
33465     default:
33466       gcc_unreachable ();
33467     }
33468 }
33469
33470 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
33471    all values variable, and none identical.  */
33472
33473 static void
33474 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
33475                                  rtx target, rtx vals)
33476 {
33477   rtx ops[32], op0, op1;
33478   enum machine_mode half_mode = VOIDmode;
33479   int n, i;
33480
33481   switch (mode)
33482     {
33483     case V2SFmode:
33484     case V2SImode:
33485       if (!mmx_ok && !TARGET_SSE)
33486         break;
33487       /* FALLTHRU */
33488
33489     case V8SFmode:
33490     case V8SImode:
33491     case V4DFmode:
33492     case V4DImode:
33493     case V4SFmode:
33494     case V4SImode:
33495     case V2DFmode:
33496     case V2DImode:
33497       n = GET_MODE_NUNITS (mode);
33498       for (i = 0; i < n; i++)
33499         ops[i] = XVECEXP (vals, 0, i);
33500       ix86_expand_vector_init_concat (mode, target, ops, n);
33501       return;
33502
33503     case V32QImode:
33504       half_mode = V16QImode;
33505       goto half;
33506
33507     case V16HImode:
33508       half_mode = V8HImode;
33509       goto half;
33510
33511 half:
33512       n = GET_MODE_NUNITS (mode);
33513       for (i = 0; i < n; i++)
33514         ops[i] = XVECEXP (vals, 0, i);
33515       op0 = gen_reg_rtx (half_mode);
33516       op1 = gen_reg_rtx (half_mode);
33517       ix86_expand_vector_init_interleave (half_mode, op0, ops,
33518                                           n >> 2);
33519       ix86_expand_vector_init_interleave (half_mode, op1,
33520                                           &ops [n >> 1], n >> 2);
33521       emit_insn (gen_rtx_SET (VOIDmode, target,
33522                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
33523       return;
33524
33525     case V16QImode:
33526       if (!TARGET_SSE4_1)
33527         break;
33528       /* FALLTHRU */
33529
33530     case V8HImode:
33531       if (!TARGET_SSE2)
33532         break;
33533
33534       /* Don't use ix86_expand_vector_init_interleave if we can't
33535          move from GPR to SSE register directly.  */
33536       if (!TARGET_INTER_UNIT_MOVES)
33537         break;
33538
33539       n = GET_MODE_NUNITS (mode);
33540       for (i = 0; i < n; i++)
33541         ops[i] = XVECEXP (vals, 0, i);
33542       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
33543       return;
33544
33545     case V4HImode:
33546     case V8QImode:
33547       break;
33548
33549     default:
33550       gcc_unreachable ();
33551     }
33552
33553     {
33554       int i, j, n_elts, n_words, n_elt_per_word;
33555       enum machine_mode inner_mode;
33556       rtx words[4], shift;
33557
33558       inner_mode = GET_MODE_INNER (mode);
33559       n_elts = GET_MODE_NUNITS (mode);
33560       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
33561       n_elt_per_word = n_elts / n_words;
33562       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
33563
33564       for (i = 0; i < n_words; ++i)
33565         {
33566           rtx word = NULL_RTX;
33567
33568           for (j = 0; j < n_elt_per_word; ++j)
33569             {
33570               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
33571               elt = convert_modes (word_mode, inner_mode, elt, true);
33572
33573               if (j == 0)
33574                 word = elt;
33575               else
33576                 {
33577                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
33578                                               word, 1, OPTAB_LIB_WIDEN);
33579                   word = expand_simple_binop (word_mode, IOR, word, elt,
33580                                               word, 1, OPTAB_LIB_WIDEN);
33581                 }
33582             }
33583
33584           words[i] = word;
33585         }
33586
33587       if (n_words == 1)
33588         emit_move_insn (target, gen_lowpart (mode, words[0]));
33589       else if (n_words == 2)
33590         {
33591           rtx tmp = gen_reg_rtx (mode);
33592           emit_clobber (tmp);
33593           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
33594           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
33595           emit_move_insn (target, tmp);
33596         }
33597       else if (n_words == 4)
33598         {
33599           rtx tmp = gen_reg_rtx (V4SImode);
33600           gcc_assert (word_mode == SImode);
33601           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
33602           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
33603           emit_move_insn (target, gen_lowpart (mode, tmp));
33604         }
33605       else
33606         gcc_unreachable ();
33607     }
33608 }
33609
33610 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
33611    instructions unless MMX_OK is true.  */
33612
33613 void
33614 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
33615 {
33616   enum machine_mode mode = GET_MODE (target);
33617   enum machine_mode inner_mode = GET_MODE_INNER (mode);
33618   int n_elts = GET_MODE_NUNITS (mode);
33619   int n_var = 0, one_var = -1;
33620   bool all_same = true, all_const_zero = true;
33621   int i;
33622   rtx x;
33623
33624   for (i = 0; i < n_elts; ++i)
33625     {
33626       x = XVECEXP (vals, 0, i);
33627       if (!(CONST_INT_P (x)
33628             || GET_CODE (x) == CONST_DOUBLE
33629             || GET_CODE (x) == CONST_FIXED))
33630         n_var++, one_var = i;
33631       else if (x != CONST0_RTX (inner_mode))
33632         all_const_zero = false;
33633       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
33634         all_same = false;
33635     }
33636
33637   /* Constants are best loaded from the constant pool.  */
33638   if (n_var == 0)
33639     {
33640       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
33641       return;
33642     }
33643
33644   /* If all values are identical, broadcast the value.  */
33645   if (all_same
33646       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
33647                                             XVECEXP (vals, 0, 0)))
33648     return;
33649
33650   /* Values where only one field is non-constant are best loaded from
33651      the pool and overwritten via move later.  */
33652   if (n_var == 1)
33653     {
33654       if (all_const_zero
33655           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
33656                                                   XVECEXP (vals, 0, one_var),
33657                                                   one_var))
33658         return;
33659
33660       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
33661         return;
33662     }
33663
33664   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
33665 }
33666
33667 void
33668 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
33669 {
33670   enum machine_mode mode = GET_MODE (target);
33671   enum machine_mode inner_mode = GET_MODE_INNER (mode);
33672   enum machine_mode half_mode;
33673   bool use_vec_merge = false;
33674   rtx tmp;
33675   static rtx (*gen_extract[6][2]) (rtx, rtx)
33676     = {
33677         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
33678         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
33679         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
33680         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
33681         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
33682         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
33683       };
33684   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
33685     = {
33686         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
33687         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
33688         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
33689         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
33690         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
33691         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
33692       };
33693   int i, j, n;
33694
33695   switch (mode)
33696     {
33697     case V2SFmode:
33698     case V2SImode:
33699       if (mmx_ok)
33700         {
33701           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
33702           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
33703           if (elt == 0)
33704             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
33705           else
33706             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
33707           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33708           return;
33709         }
33710       break;
33711
33712     case V2DImode:
33713       use_vec_merge = TARGET_SSE4_1 && TARGET_64BIT;
33714       if (use_vec_merge)
33715         break;
33716
33717       tmp = gen_reg_rtx (GET_MODE_INNER (mode));
33718       ix86_expand_vector_extract (false, tmp, target, 1 - elt);
33719       if (elt == 0)
33720         tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
33721       else
33722         tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
33723       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33724       return;
33725
33726     case V2DFmode:
33727       {
33728         rtx op0, op1;
33729
33730         /* For the two element vectors, we implement a VEC_CONCAT with
33731            the extraction of the other element.  */
33732
33733         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
33734         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
33735
33736         if (elt == 0)
33737           op0 = val, op1 = tmp;
33738         else
33739           op0 = tmp, op1 = val;
33740
33741         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
33742         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33743       }
33744       return;
33745
33746     case V4SFmode:
33747       use_vec_merge = TARGET_SSE4_1;
33748       if (use_vec_merge)
33749         break;
33750
33751       switch (elt)
33752         {
33753         case 0:
33754           use_vec_merge = true;
33755           break;
33756
33757         case 1:
33758           /* tmp = target = A B C D */
33759           tmp = copy_to_reg (target);
33760           /* target = A A B B */
33761           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
33762           /* target = X A B B */
33763           ix86_expand_vector_set (false, target, val, 0);
33764           /* target = A X C D  */
33765           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
33766                                           const1_rtx, const0_rtx,
33767                                           GEN_INT (2+4), GEN_INT (3+4)));
33768           return;
33769
33770         case 2:
33771           /* tmp = target = A B C D */
33772           tmp = copy_to_reg (target);
33773           /* tmp = X B C D */
33774           ix86_expand_vector_set (false, tmp, val, 0);
33775           /* target = A B X D */
33776           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
33777                                           const0_rtx, const1_rtx,
33778                                           GEN_INT (0+4), GEN_INT (3+4)));
33779           return;
33780
33781         case 3:
33782           /* tmp = target = A B C D */
33783           tmp = copy_to_reg (target);
33784           /* tmp = X B C D */
33785           ix86_expand_vector_set (false, tmp, val, 0);
33786           /* target = A B X D */
33787           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
33788                                           const0_rtx, const1_rtx,
33789                                           GEN_INT (2+4), GEN_INT (0+4)));
33790           return;
33791
33792         default:
33793           gcc_unreachable ();
33794         }
33795       break;
33796
33797     case V4SImode:
33798       use_vec_merge = TARGET_SSE4_1;
33799       if (use_vec_merge)
33800         break;
33801
33802       /* Element 0 handled by vec_merge below.  */
33803       if (elt == 0)
33804         {
33805           use_vec_merge = true;
33806           break;
33807         }
33808
33809       if (TARGET_SSE2)
33810         {
33811           /* With SSE2, use integer shuffles to swap element 0 and ELT,
33812              store into element 0, then shuffle them back.  */
33813
33814           rtx order[4];
33815
33816           order[0] = GEN_INT (elt);
33817           order[1] = const1_rtx;
33818           order[2] = const2_rtx;
33819           order[3] = GEN_INT (3);
33820           order[elt] = const0_rtx;
33821
33822           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
33823                                         order[1], order[2], order[3]));
33824
33825           ix86_expand_vector_set (false, target, val, 0);
33826
33827           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
33828                                         order[1], order[2], order[3]));
33829         }
33830       else
33831         {
33832           /* For SSE1, we have to reuse the V4SF code.  */
33833           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
33834                                   gen_lowpart (SFmode, val), elt);
33835         }
33836       return;
33837
33838     case V8HImode:
33839       use_vec_merge = TARGET_SSE2;
33840       break;
33841     case V4HImode:
33842       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
33843       break;
33844
33845     case V16QImode:
33846       use_vec_merge = TARGET_SSE4_1;
33847       break;
33848
33849     case V8QImode:
33850       break;
33851
33852     case V32QImode:
33853       half_mode = V16QImode;
33854       j = 0;
33855       n = 16;
33856       goto half;
33857
33858     case V16HImode:
33859       half_mode = V8HImode;
33860       j = 1;
33861       n = 8;
33862       goto half;
33863
33864     case V8SImode:
33865       half_mode = V4SImode;
33866       j = 2;
33867       n = 4;
33868       goto half;
33869
33870     case V4DImode:
33871       half_mode = V2DImode;
33872       j = 3;
33873       n = 2;
33874       goto half;
33875
33876     case V8SFmode:
33877       half_mode = V4SFmode;
33878       j = 4;
33879       n = 4;
33880       goto half;
33881
33882     case V4DFmode:
33883       half_mode = V2DFmode;
33884       j = 5;
33885       n = 2;
33886       goto half;
33887
33888 half:
33889       /* Compute offset.  */
33890       i = elt / n;
33891       elt %= n;
33892
33893       gcc_assert (i <= 1);
33894
33895       /* Extract the half.  */
33896       tmp = gen_reg_rtx (half_mode);
33897       emit_insn (gen_extract[j][i] (tmp, target));
33898
33899       /* Put val in tmp at elt.  */
33900       ix86_expand_vector_set (false, tmp, val, elt);
33901
33902       /* Put it back.  */
33903       emit_insn (gen_insert[j][i] (target, target, tmp));
33904       return;
33905
33906     default:
33907       break;
33908     }
33909
33910   if (use_vec_merge)
33911     {
33912       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
33913       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
33914       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33915     }
33916   else
33917     {
33918       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
33919
33920       emit_move_insn (mem, target);
33921
33922       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
33923       emit_move_insn (tmp, val);
33924
33925       emit_move_insn (target, mem);
33926     }
33927 }
33928
33929 void
33930 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
33931 {
33932   enum machine_mode mode = GET_MODE (vec);
33933   enum machine_mode inner_mode = GET_MODE_INNER (mode);
33934   bool use_vec_extr = false;
33935   rtx tmp;
33936
33937   switch (mode)
33938     {
33939     case V2SImode:
33940     case V2SFmode:
33941       if (!mmx_ok)
33942         break;
33943       /* FALLTHRU */
33944
33945     case V2DFmode:
33946     case V2DImode:
33947       use_vec_extr = true;
33948       break;
33949
33950     case V4SFmode:
33951       use_vec_extr = TARGET_SSE4_1;
33952       if (use_vec_extr)
33953         break;
33954
33955       switch (elt)
33956         {
33957         case 0:
33958           tmp = vec;
33959           break;
33960
33961         case 1:
33962         case 3:
33963           tmp = gen_reg_rtx (mode);
33964           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
33965                                        GEN_INT (elt), GEN_INT (elt),
33966                                        GEN_INT (elt+4), GEN_INT (elt+4)));
33967           break;
33968
33969         case 2:
33970           tmp = gen_reg_rtx (mode);
33971           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
33972           break;
33973
33974         default:
33975           gcc_unreachable ();
33976         }
33977       vec = tmp;
33978       use_vec_extr = true;
33979       elt = 0;
33980       break;
33981
33982     case V4SImode:
33983       use_vec_extr = TARGET_SSE4_1;
33984       if (use_vec_extr)
33985         break;
33986
33987       if (TARGET_SSE2)
33988         {
33989           switch (elt)
33990             {
33991             case 0:
33992               tmp = vec;
33993               break;
33994
33995             case 1:
33996             case 3:
33997               tmp = gen_reg_rtx (mode);
33998               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
33999                                             GEN_INT (elt), GEN_INT (elt),
34000                                             GEN_INT (elt), GEN_INT (elt)));
34001               break;
34002
34003             case 2:
34004               tmp = gen_reg_rtx (mode);
34005               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
34006               break;
34007
34008             default:
34009               gcc_unreachable ();
34010             }
34011           vec = tmp;
34012           use_vec_extr = true;
34013           elt = 0;
34014         }
34015       else
34016         {
34017           /* For SSE1, we have to reuse the V4SF code.  */
34018           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
34019                                       gen_lowpart (V4SFmode, vec), elt);
34020           return;
34021         }
34022       break;
34023
34024     case V8HImode:
34025       use_vec_extr = TARGET_SSE2;
34026       break;
34027     case V4HImode:
34028       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
34029       break;
34030
34031     case V16QImode:
34032       use_vec_extr = TARGET_SSE4_1;
34033       break;
34034
34035     case V8SFmode:
34036       if (TARGET_AVX)
34037         {
34038           tmp = gen_reg_rtx (V4SFmode);
34039           if (elt < 4)
34040             emit_insn (gen_vec_extract_lo_v8sf (tmp, vec));
34041           else
34042             emit_insn (gen_vec_extract_hi_v8sf (tmp, vec));
34043           ix86_expand_vector_extract (false, target, tmp, elt & 3);
34044           return;
34045         }
34046       break;
34047
34048     case V4DFmode:
34049       if (TARGET_AVX)
34050         {
34051           tmp = gen_reg_rtx (V2DFmode);
34052           if (elt < 2)
34053             emit_insn (gen_vec_extract_lo_v4df (tmp, vec));
34054           else
34055             emit_insn (gen_vec_extract_hi_v4df (tmp, vec));
34056           ix86_expand_vector_extract (false, target, tmp, elt & 1);
34057           return;
34058         }
34059       break;
34060
34061     case V32QImode:
34062       if (TARGET_AVX)
34063         {
34064           tmp = gen_reg_rtx (V16QImode);
34065           if (elt < 16)
34066             emit_insn (gen_vec_extract_lo_v32qi (tmp, vec));
34067           else
34068             emit_insn (gen_vec_extract_hi_v32qi (tmp, vec));
34069           ix86_expand_vector_extract (false, target, tmp, elt & 15);
34070           return;
34071         }
34072       break;
34073
34074     case V16HImode:
34075       if (TARGET_AVX)
34076         {
34077           tmp = gen_reg_rtx (V8HImode);
34078           if (elt < 8)
34079             emit_insn (gen_vec_extract_lo_v16hi (tmp, vec));
34080           else
34081             emit_insn (gen_vec_extract_hi_v16hi (tmp, vec));
34082           ix86_expand_vector_extract (false, target, tmp, elt & 7);
34083           return;
34084         }
34085       break;
34086
34087     case V8SImode:
34088       if (TARGET_AVX)
34089         {
34090           tmp = gen_reg_rtx (V4SImode);
34091           if (elt < 4)
34092             emit_insn (gen_vec_extract_lo_v8si (tmp, vec));
34093           else
34094             emit_insn (gen_vec_extract_hi_v8si (tmp, vec));
34095           ix86_expand_vector_extract (false, target, tmp, elt & 3);
34096           return;
34097         }
34098       break;
34099
34100     case V4DImode:
34101       if (TARGET_AVX)
34102         {
34103           tmp = gen_reg_rtx (V2DImode);
34104           if (elt < 2)
34105             emit_insn (gen_vec_extract_lo_v4di (tmp, vec));
34106           else
34107             emit_insn (gen_vec_extract_hi_v4di (tmp, vec));
34108           ix86_expand_vector_extract (false, target, tmp, elt & 1);
34109           return;
34110         }
34111       break;
34112
34113     case V8QImode:
34114       /* ??? Could extract the appropriate HImode element and shift.  */
34115     default:
34116       break;
34117     }
34118
34119   if (use_vec_extr)
34120     {
34121       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
34122       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
34123
34124       /* Let the rtl optimizers know about the zero extension performed.  */
34125       if (inner_mode == QImode || inner_mode == HImode)
34126         {
34127           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
34128           target = gen_lowpart (SImode, target);
34129         }
34130
34131       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
34132     }
34133   else
34134     {
34135       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
34136
34137       emit_move_insn (mem, vec);
34138
34139       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
34140       emit_move_insn (target, tmp);
34141     }
34142 }
34143
34144 /* Generate code to copy vector bits i / 2 ... i - 1 from vector SRC
34145    to bits 0 ... i / 2 - 1 of vector DEST, which has the same mode.
34146    The upper bits of DEST are undefined, though they shouldn't cause
34147    exceptions (some bits from src or all zeros are ok).  */
34148
34149 static void
34150 emit_reduc_half (rtx dest, rtx src, int i)
34151 {
34152   rtx tem;
34153   switch (GET_MODE (src))
34154     {
34155     case V4SFmode:
34156       if (i == 128)
34157         tem = gen_sse_movhlps (dest, src, src);
34158       else
34159         tem = gen_sse_shufps_v4sf (dest, src, src, const1_rtx, const1_rtx,
34160                                    GEN_INT (1 + 4), GEN_INT (1 + 4));
34161       break;
34162     case V2DFmode:
34163       tem = gen_vec_interleave_highv2df (dest, src, src);
34164       break;
34165     case V16QImode:
34166     case V8HImode:
34167     case V4SImode:
34168     case V2DImode:
34169       tem = gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, dest),
34170                                 gen_lowpart (V1TImode, src),
34171                                 GEN_INT (i / 2));
34172       break;
34173     case V8SFmode:
34174       if (i == 256)
34175         tem = gen_avx_vperm2f128v8sf3 (dest, src, src, const1_rtx);
34176       else
34177         tem = gen_avx_shufps256 (dest, src, src,
34178                                  GEN_INT (i == 128 ? 2 + (3 << 2) : 1));
34179       break;
34180     case V4DFmode:
34181       if (i == 256)
34182         tem = gen_avx_vperm2f128v4df3 (dest, src, src, const1_rtx);
34183       else
34184         tem = gen_avx_shufpd256 (dest, src, src, const1_rtx);
34185       break;
34186     case V32QImode:
34187     case V16HImode:
34188     case V8SImode:
34189     case V4DImode:
34190       if (i == 256)
34191         tem = gen_avx2_permv2ti (gen_lowpart (V4DImode, dest),
34192                                  gen_lowpart (V4DImode, src),
34193                                  gen_lowpart (V4DImode, src),
34194                                  const1_rtx);
34195       else
34196         tem = gen_avx2_lshrv2ti3 (gen_lowpart (V2TImode, dest),
34197                                   gen_lowpart (V2TImode, src),
34198                                   GEN_INT (i / 2));
34199       break;
34200     default:
34201       gcc_unreachable ();
34202     }
34203   emit_insn (tem);
34204 }
34205
34206 /* Expand a vector reduction.  FN is the binary pattern to reduce;
34207    DEST is the destination; IN is the input vector.  */
34208
34209 void
34210 ix86_expand_reduc (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
34211 {
34212   rtx half, dst, vec = in;
34213   enum machine_mode mode = GET_MODE (in);
34214   int i;
34215
34216   /* SSE4 has a special instruction for V8HImode UMIN reduction.  */
34217   if (TARGET_SSE4_1
34218       && mode == V8HImode
34219       && fn == gen_uminv8hi3)
34220     {
34221       emit_insn (gen_sse4_1_phminposuw (dest, in));
34222       return;
34223     }
34224
34225   for (i = GET_MODE_BITSIZE (mode);
34226        i > GET_MODE_BITSIZE (GET_MODE_INNER (mode));
34227        i >>= 1)
34228     {
34229       half = gen_reg_rtx (mode);
34230       emit_reduc_half (half, vec, i);
34231       if (i == GET_MODE_BITSIZE (GET_MODE_INNER (mode)) * 2)
34232         dst = dest;
34233       else
34234         dst = gen_reg_rtx (mode);
34235       emit_insn (fn (dst, half, vec));
34236       vec = dst;
34237     }
34238 }
34239 \f
34240 /* Target hook for scalar_mode_supported_p.  */
34241 static bool
34242 ix86_scalar_mode_supported_p (enum machine_mode mode)
34243 {
34244   if (DECIMAL_FLOAT_MODE_P (mode))
34245     return default_decimal_float_supported_p ();
34246   else if (mode == TFmode)
34247     return true;
34248   else
34249     return default_scalar_mode_supported_p (mode);
34250 }
34251
34252 /* Implements target hook vector_mode_supported_p.  */
34253 static bool
34254 ix86_vector_mode_supported_p (enum machine_mode mode)
34255 {
34256   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
34257     return true;
34258   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
34259     return true;
34260   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
34261     return true;
34262   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
34263     return true;
34264   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
34265     return true;
34266   return false;
34267 }
34268
34269 /* Target hook for c_mode_for_suffix.  */
34270 static enum machine_mode
34271 ix86_c_mode_for_suffix (char suffix)
34272 {
34273   if (suffix == 'q')
34274     return TFmode;
34275   if (suffix == 'w')
34276     return XFmode;
34277
34278   return VOIDmode;
34279 }
34280
34281 /* Worker function for TARGET_MD_ASM_CLOBBERS.
34282
34283    We do this in the new i386 backend to maintain source compatibility
34284    with the old cc0-based compiler.  */
34285
34286 static tree
34287 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
34288                       tree inputs ATTRIBUTE_UNUSED,
34289                       tree clobbers)
34290 {
34291   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
34292                         clobbers);
34293   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
34294                         clobbers);
34295   return clobbers;
34296 }
34297
34298 /* Implements target vector targetm.asm.encode_section_info.  */
34299
34300 static void ATTRIBUTE_UNUSED
34301 ix86_encode_section_info (tree decl, rtx rtl, int first)
34302 {
34303   default_encode_section_info (decl, rtl, first);
34304
34305   if (TREE_CODE (decl) == VAR_DECL
34306       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
34307       && ix86_in_large_data_p (decl))
34308     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
34309 }
34310
34311 /* Worker function for REVERSE_CONDITION.  */
34312
34313 enum rtx_code
34314 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
34315 {
34316   return (mode != CCFPmode && mode != CCFPUmode
34317           ? reverse_condition (code)
34318           : reverse_condition_maybe_unordered (code));
34319 }
34320
34321 /* Output code to perform an x87 FP register move, from OPERANDS[1]
34322    to OPERANDS[0].  */
34323
34324 const char *
34325 output_387_reg_move (rtx insn, rtx *operands)
34326 {
34327   if (REG_P (operands[0]))
34328     {
34329       if (REG_P (operands[1])
34330           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
34331         {
34332           if (REGNO (operands[0]) == FIRST_STACK_REG)
34333             return output_387_ffreep (operands, 0);
34334           return "fstp\t%y0";
34335         }
34336       if (STACK_TOP_P (operands[0]))
34337         return "fld%Z1\t%y1";
34338       return "fst\t%y0";
34339     }
34340   else if (MEM_P (operands[0]))
34341     {
34342       gcc_assert (REG_P (operands[1]));
34343       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
34344         return "fstp%Z0\t%y0";
34345       else
34346         {
34347           /* There is no non-popping store to memory for XFmode.
34348              So if we need one, follow the store with a load.  */
34349           if (GET_MODE (operands[0]) == XFmode)
34350             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
34351           else
34352             return "fst%Z0\t%y0";
34353         }
34354     }
34355   else
34356     gcc_unreachable();
34357 }
34358
34359 /* Output code to perform a conditional jump to LABEL, if C2 flag in
34360    FP status register is set.  */
34361
34362 void
34363 ix86_emit_fp_unordered_jump (rtx label)
34364 {
34365   rtx reg = gen_reg_rtx (HImode);
34366   rtx temp;
34367
34368   emit_insn (gen_x86_fnstsw_1 (reg));
34369
34370   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
34371     {
34372       emit_insn (gen_x86_sahf_1 (reg));
34373
34374       temp = gen_rtx_REG (CCmode, FLAGS_REG);
34375       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
34376     }
34377   else
34378     {
34379       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
34380
34381       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
34382       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
34383     }
34384
34385   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
34386                               gen_rtx_LABEL_REF (VOIDmode, label),
34387                               pc_rtx);
34388   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
34389
34390   emit_jump_insn (temp);
34391   predict_jump (REG_BR_PROB_BASE * 10 / 100);
34392 }
34393
34394 /* Output code to perform a log1p XFmode calculation.  */
34395
34396 void ix86_emit_i387_log1p (rtx op0, rtx op1)
34397 {
34398   rtx label1 = gen_label_rtx ();
34399   rtx label2 = gen_label_rtx ();
34400
34401   rtx tmp = gen_reg_rtx (XFmode);
34402   rtx tmp2 = gen_reg_rtx (XFmode);
34403   rtx test;
34404
34405   emit_insn (gen_absxf2 (tmp, op1));
34406   test = gen_rtx_GE (VOIDmode, tmp,
34407     CONST_DOUBLE_FROM_REAL_VALUE (
34408        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
34409        XFmode));
34410   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
34411
34412   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
34413   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
34414   emit_jump (label2);
34415
34416   emit_label (label1);
34417   emit_move_insn (tmp, CONST1_RTX (XFmode));
34418   emit_insn (gen_addxf3 (tmp, op1, tmp));
34419   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
34420   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
34421
34422   emit_label (label2);
34423 }
34424
34425 /* Emit code for round calculation.  */
34426 void ix86_emit_i387_round (rtx op0, rtx op1)
34427 {
34428   enum machine_mode inmode = GET_MODE (op1);
34429   enum machine_mode outmode = GET_MODE (op0);
34430   rtx e1, e2, res, tmp, tmp1, half;
34431   rtx scratch = gen_reg_rtx (HImode);
34432   rtx flags = gen_rtx_REG (CCNOmode, FLAGS_REG);
34433   rtx jump_label = gen_label_rtx ();
34434   rtx insn;
34435   rtx (*gen_abs) (rtx, rtx);
34436   rtx (*gen_neg) (rtx, rtx);
34437
34438   switch (inmode)
34439     {
34440     case SFmode:
34441       gen_abs = gen_abssf2;
34442       break;
34443     case DFmode:
34444       gen_abs = gen_absdf2;
34445       break;
34446     case XFmode:
34447       gen_abs = gen_absxf2;
34448       break;
34449     default:
34450       gcc_unreachable ();
34451     }
34452
34453   switch (outmode)
34454     {
34455     case SFmode:
34456       gen_neg = gen_negsf2;
34457       break;
34458     case DFmode:
34459       gen_neg = gen_negdf2;
34460       break;
34461     case XFmode:
34462       gen_neg = gen_negxf2;
34463       break;
34464     case HImode:
34465       gen_neg = gen_neghi2;
34466       break;
34467     case SImode:
34468       gen_neg = gen_negsi2;
34469       break;
34470     case DImode:
34471       gen_neg = gen_negdi2;
34472       break;
34473     default:
34474       gcc_unreachable ();
34475     }
34476
34477   e1 = gen_reg_rtx (inmode);
34478   e2 = gen_reg_rtx (inmode);
34479   res = gen_reg_rtx (outmode);
34480
34481   half = CONST_DOUBLE_FROM_REAL_VALUE (dconsthalf, inmode);
34482
34483   /* round(a) = sgn(a) * floor(fabs(a) + 0.5) */
34484
34485   /* scratch = fxam(op1) */
34486   emit_insn (gen_rtx_SET (VOIDmode, scratch,
34487                           gen_rtx_UNSPEC (HImode, gen_rtvec (1, op1),
34488                                           UNSPEC_FXAM)));
34489   /* e1 = fabs(op1) */
34490   emit_insn (gen_abs (e1, op1));
34491
34492   /* e2 = e1 + 0.5 */
34493   half = force_reg (inmode, half);
34494   emit_insn (gen_rtx_SET (VOIDmode, e2,
34495                           gen_rtx_PLUS (inmode, e1, half)));
34496
34497   /* res = floor(e2) */
34498   if (inmode != XFmode)
34499     {
34500       tmp1 = gen_reg_rtx (XFmode);
34501
34502       emit_insn (gen_rtx_SET (VOIDmode, tmp1,
34503                               gen_rtx_FLOAT_EXTEND (XFmode, e2)));
34504     }
34505   else
34506     tmp1 = e2;
34507
34508   switch (outmode)
34509     {
34510     case SFmode:
34511     case DFmode:
34512       {
34513         rtx tmp0 = gen_reg_rtx (XFmode);
34514
34515         emit_insn (gen_frndintxf2_floor (tmp0, tmp1));
34516
34517         emit_insn (gen_rtx_SET (VOIDmode, res,
34518                                 gen_rtx_UNSPEC (outmode, gen_rtvec (1, tmp0),
34519                                                 UNSPEC_TRUNC_NOOP)));
34520       }
34521       break;
34522     case XFmode:
34523       emit_insn (gen_frndintxf2_floor (res, tmp1));
34524       break;
34525     case HImode:
34526       emit_insn (gen_lfloorxfhi2 (res, tmp1));
34527       break;
34528     case SImode:
34529       emit_insn (gen_lfloorxfsi2 (res, tmp1));
34530       break;
34531     case DImode:
34532       emit_insn (gen_lfloorxfdi2 (res, tmp1));
34533         break;
34534     default:
34535       gcc_unreachable ();
34536     }
34537
34538   /* flags = signbit(a) */
34539   emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x02)));
34540
34541   /* if (flags) then res = -res */
34542   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode,
34543                               gen_rtx_EQ (VOIDmode, flags, const0_rtx),
34544                               gen_rtx_LABEL_REF (VOIDmode, jump_label),
34545                               pc_rtx);
34546   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
34547   predict_jump (REG_BR_PROB_BASE * 50 / 100);
34548   JUMP_LABEL (insn) = jump_label;
34549
34550   emit_insn (gen_neg (res, res));
34551
34552   emit_label (jump_label);
34553   LABEL_NUSES (jump_label) = 1;
34554
34555   emit_move_insn (op0, res);
34556 }
34557
34558 /* Output code to perform a Newton-Rhapson approximation of a single precision
34559    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
34560
34561 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
34562 {
34563   rtx x0, x1, e0, e1;
34564
34565   x0 = gen_reg_rtx (mode);
34566   e0 = gen_reg_rtx (mode);
34567   e1 = gen_reg_rtx (mode);
34568   x1 = gen_reg_rtx (mode);
34569
34570   /* a / b = a * ((rcp(b) + rcp(b)) - (b * rcp(b) * rcp (b))) */
34571
34572   b = force_reg (mode, b);
34573
34574   /* x0 = rcp(b) estimate */
34575   emit_insn (gen_rtx_SET (VOIDmode, x0,
34576                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
34577                                           UNSPEC_RCP)));
34578   /* e0 = x0 * b */
34579   emit_insn (gen_rtx_SET (VOIDmode, e0,
34580                           gen_rtx_MULT (mode, x0, b)));
34581
34582   /* e0 = x0 * e0 */
34583   emit_insn (gen_rtx_SET (VOIDmode, e0,
34584                           gen_rtx_MULT (mode, x0, e0)));
34585
34586   /* e1 = x0 + x0 */
34587   emit_insn (gen_rtx_SET (VOIDmode, e1,
34588                           gen_rtx_PLUS (mode, x0, x0)));
34589
34590   /* x1 = e1 - e0 */
34591   emit_insn (gen_rtx_SET (VOIDmode, x1,
34592                           gen_rtx_MINUS (mode, e1, e0)));
34593
34594   /* res = a * x1 */
34595   emit_insn (gen_rtx_SET (VOIDmode, res,
34596                           gen_rtx_MULT (mode, a, x1)));
34597 }
34598
34599 /* Output code to perform a Newton-Rhapson approximation of a
34600    single precision floating point [reciprocal] square root.  */
34601
34602 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
34603                          bool recip)
34604 {
34605   rtx x0, e0, e1, e2, e3, mthree, mhalf;
34606   REAL_VALUE_TYPE r;
34607
34608   x0 = gen_reg_rtx (mode);
34609   e0 = gen_reg_rtx (mode);
34610   e1 = gen_reg_rtx (mode);
34611   e2 = gen_reg_rtx (mode);
34612   e3 = gen_reg_rtx (mode);
34613
34614   real_from_integer (&r, VOIDmode, -3, -1, 0);
34615   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
34616
34617   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
34618   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
34619
34620   if (VECTOR_MODE_P (mode))
34621     {
34622       mthree = ix86_build_const_vector (mode, true, mthree);
34623       mhalf = ix86_build_const_vector (mode, true, mhalf);
34624     }
34625
34626   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
34627      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
34628
34629   a = force_reg (mode, a);
34630
34631   /* x0 = rsqrt(a) estimate */
34632   emit_insn (gen_rtx_SET (VOIDmode, x0,
34633                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
34634                                           UNSPEC_RSQRT)));
34635
34636   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
34637   if (!recip)
34638     {
34639       rtx zero, mask;
34640
34641       zero = gen_reg_rtx (mode);
34642       mask = gen_reg_rtx (mode);
34643
34644       zero = force_reg (mode, CONST0_RTX(mode));
34645       emit_insn (gen_rtx_SET (VOIDmode, mask,
34646                               gen_rtx_NE (mode, zero, a)));
34647
34648       emit_insn (gen_rtx_SET (VOIDmode, x0,
34649                               gen_rtx_AND (mode, x0, mask)));
34650     }
34651
34652   /* e0 = x0 * a */
34653   emit_insn (gen_rtx_SET (VOIDmode, e0,
34654                           gen_rtx_MULT (mode, x0, a)));
34655   /* e1 = e0 * x0 */
34656   emit_insn (gen_rtx_SET (VOIDmode, e1,
34657                           gen_rtx_MULT (mode, e0, x0)));
34658
34659   /* e2 = e1 - 3. */
34660   mthree = force_reg (mode, mthree);
34661   emit_insn (gen_rtx_SET (VOIDmode, e2,
34662                           gen_rtx_PLUS (mode, e1, mthree)));
34663
34664   mhalf = force_reg (mode, mhalf);
34665   if (recip)
34666     /* e3 = -.5 * x0 */
34667     emit_insn (gen_rtx_SET (VOIDmode, e3,
34668                             gen_rtx_MULT (mode, x0, mhalf)));
34669   else
34670     /* e3 = -.5 * e0 */
34671     emit_insn (gen_rtx_SET (VOIDmode, e3,
34672                             gen_rtx_MULT (mode, e0, mhalf)));
34673   /* ret = e2 * e3 */
34674   emit_insn (gen_rtx_SET (VOIDmode, res,
34675                           gen_rtx_MULT (mode, e2, e3)));
34676 }
34677
34678 #ifdef TARGET_SOLARIS
34679 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
34680
34681 static void
34682 i386_solaris_elf_named_section (const char *name, unsigned int flags,
34683                                 tree decl)
34684 {
34685   /* With Binutils 2.15, the "@unwind" marker must be specified on
34686      every occurrence of the ".eh_frame" section, not just the first
34687      one.  */
34688   if (TARGET_64BIT
34689       && strcmp (name, ".eh_frame") == 0)
34690     {
34691       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
34692                flags & SECTION_WRITE ? "aw" : "a");
34693       return;
34694     }
34695
34696 #ifndef USE_GAS
34697   if (HAVE_COMDAT_GROUP && flags & SECTION_LINKONCE)
34698     {
34699       solaris_elf_asm_comdat_section (name, flags, decl);
34700       return;
34701     }
34702 #endif
34703
34704   default_elf_asm_named_section (name, flags, decl);
34705 }
34706 #endif /* TARGET_SOLARIS */
34707
34708 /* Return the mangling of TYPE if it is an extended fundamental type.  */
34709
34710 static const char *
34711 ix86_mangle_type (const_tree type)
34712 {
34713   type = TYPE_MAIN_VARIANT (type);
34714
34715   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
34716       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
34717     return NULL;
34718
34719   switch (TYPE_MODE (type))
34720     {
34721     case TFmode:
34722       /* __float128 is "g".  */
34723       return "g";
34724     case XFmode:
34725       /* "long double" or __float80 is "e".  */
34726       return "e";
34727     default:
34728       return NULL;
34729     }
34730 }
34731
34732 /* For 32-bit code we can save PIC register setup by using
34733    __stack_chk_fail_local hidden function instead of calling
34734    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
34735    register, so it is better to call __stack_chk_fail directly.  */
34736
34737 static tree ATTRIBUTE_UNUSED
34738 ix86_stack_protect_fail (void)
34739 {
34740   return TARGET_64BIT
34741          ? default_external_stack_protect_fail ()
34742          : default_hidden_stack_protect_fail ();
34743 }
34744
34745 /* Select a format to encode pointers in exception handling data.  CODE
34746    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
34747    true if the symbol may be affected by dynamic relocations.
34748
34749    ??? All x86 object file formats are capable of representing this.
34750    After all, the relocation needed is the same as for the call insn.
34751    Whether or not a particular assembler allows us to enter such, I
34752    guess we'll have to see.  */
34753 int
34754 asm_preferred_eh_data_format (int code, int global)
34755 {
34756   if (flag_pic)
34757     {
34758       int type = DW_EH_PE_sdata8;
34759       if (!TARGET_64BIT
34760           || ix86_cmodel == CM_SMALL_PIC
34761           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
34762         type = DW_EH_PE_sdata4;
34763       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
34764     }
34765   if (ix86_cmodel == CM_SMALL
34766       || (ix86_cmodel == CM_MEDIUM && code))
34767     return DW_EH_PE_udata4;
34768   return DW_EH_PE_absptr;
34769 }
34770 \f
34771 /* Expand copysign from SIGN to the positive value ABS_VALUE
34772    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
34773    the sign-bit.  */
34774 static void
34775 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
34776 {
34777   enum machine_mode mode = GET_MODE (sign);
34778   rtx sgn = gen_reg_rtx (mode);
34779   if (mask == NULL_RTX)
34780     {
34781       enum machine_mode vmode;
34782
34783       if (mode == SFmode)
34784         vmode = V4SFmode;
34785       else if (mode == DFmode)
34786         vmode = V2DFmode;
34787       else
34788         vmode = mode;
34789
34790       mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), false);
34791       if (!VECTOR_MODE_P (mode))
34792         {
34793           /* We need to generate a scalar mode mask in this case.  */
34794           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
34795           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
34796           mask = gen_reg_rtx (mode);
34797           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
34798         }
34799     }
34800   else
34801     mask = gen_rtx_NOT (mode, mask);
34802   emit_insn (gen_rtx_SET (VOIDmode, sgn,
34803                           gen_rtx_AND (mode, mask, sign)));
34804   emit_insn (gen_rtx_SET (VOIDmode, result,
34805                           gen_rtx_IOR (mode, abs_value, sgn)));
34806 }
34807
34808 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
34809    mask for masking out the sign-bit is stored in *SMASK, if that is
34810    non-null.  */
34811 static rtx
34812 ix86_expand_sse_fabs (rtx op0, rtx *smask)
34813 {
34814   enum machine_mode vmode, mode = GET_MODE (op0);
34815   rtx xa, mask;
34816
34817   xa = gen_reg_rtx (mode);
34818   if (mode == SFmode)
34819     vmode = V4SFmode;
34820   else if (mode == DFmode)
34821     vmode = V2DFmode;
34822   else
34823     vmode = mode;
34824   mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), true);
34825   if (!VECTOR_MODE_P (mode))
34826     {
34827       /* We need to generate a scalar mode mask in this case.  */
34828       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
34829       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
34830       mask = gen_reg_rtx (mode);
34831       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
34832     }
34833   emit_insn (gen_rtx_SET (VOIDmode, xa,
34834                           gen_rtx_AND (mode, op0, mask)));
34835
34836   if (smask)
34837     *smask = mask;
34838
34839   return xa;
34840 }
34841
34842 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
34843    swapping the operands if SWAP_OPERANDS is true.  The expanded
34844    code is a forward jump to a newly created label in case the
34845    comparison is true.  The generated label rtx is returned.  */
34846 static rtx
34847 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
34848                                   bool swap_operands)
34849 {
34850   rtx label, tmp;
34851
34852   if (swap_operands)
34853     {
34854       tmp = op0;
34855       op0 = op1;
34856       op1 = tmp;
34857     }
34858
34859   label = gen_label_rtx ();
34860   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
34861   emit_insn (gen_rtx_SET (VOIDmode, tmp,
34862                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
34863   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
34864   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
34865                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
34866   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
34867   JUMP_LABEL (tmp) = label;
34868
34869   return label;
34870 }
34871
34872 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
34873    using comparison code CODE.  Operands are swapped for the comparison if
34874    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
34875 static rtx
34876 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
34877                               bool swap_operands)
34878 {
34879   rtx (*insn)(rtx, rtx, rtx, rtx);
34880   enum machine_mode mode = GET_MODE (op0);
34881   rtx mask = gen_reg_rtx (mode);
34882
34883   if (swap_operands)
34884     {
34885       rtx tmp = op0;
34886       op0 = op1;
34887       op1 = tmp;
34888     }
34889
34890   insn = mode == DFmode ? gen_setcc_df_sse : gen_setcc_sf_sse;
34891
34892   emit_insn (insn (mask, op0, op1,
34893                    gen_rtx_fmt_ee (code, mode, op0, op1)));
34894   return mask;
34895 }
34896
34897 /* Generate and return a rtx of mode MODE for 2**n where n is the number
34898    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
34899 static rtx
34900 ix86_gen_TWO52 (enum machine_mode mode)
34901 {
34902   REAL_VALUE_TYPE TWO52r;
34903   rtx TWO52;
34904
34905   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
34906   TWO52 = const_double_from_real_value (TWO52r, mode);
34907   TWO52 = force_reg (mode, TWO52);
34908
34909   return TWO52;
34910 }
34911
34912 /* Expand SSE sequence for computing lround from OP1 storing
34913    into OP0.  */
34914 void
34915 ix86_expand_lround (rtx op0, rtx op1)
34916 {
34917   /* C code for the stuff we're doing below:
34918        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
34919        return (long)tmp;
34920    */
34921   enum machine_mode mode = GET_MODE (op1);
34922   const struct real_format *fmt;
34923   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
34924   rtx adj;
34925
34926   /* load nextafter (0.5, 0.0) */
34927   fmt = REAL_MODE_FORMAT (mode);
34928   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
34929   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
34930
34931   /* adj = copysign (0.5, op1) */
34932   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
34933   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
34934
34935   /* adj = op1 + adj */
34936   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
34937
34938   /* op0 = (imode)adj */
34939   expand_fix (op0, adj, 0);
34940 }
34941
34942 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
34943    into OPERAND0.  */
34944 void
34945 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
34946 {
34947   /* C code for the stuff we're doing below (for do_floor):
34948         xi = (long)op1;
34949         xi -= (double)xi > op1 ? 1 : 0;
34950         return xi;
34951    */
34952   enum machine_mode fmode = GET_MODE (op1);
34953   enum machine_mode imode = GET_MODE (op0);
34954   rtx ireg, freg, label, tmp;
34955
34956   /* reg = (long)op1 */
34957   ireg = gen_reg_rtx (imode);
34958   expand_fix (ireg, op1, 0);
34959
34960   /* freg = (double)reg */
34961   freg = gen_reg_rtx (fmode);
34962   expand_float (freg, ireg, 0);
34963
34964   /* ireg = (freg > op1) ? ireg - 1 : ireg */
34965   label = ix86_expand_sse_compare_and_jump (UNLE,
34966                                             freg, op1, !do_floor);
34967   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
34968                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
34969   emit_move_insn (ireg, tmp);
34970
34971   emit_label (label);
34972   LABEL_NUSES (label) = 1;
34973
34974   emit_move_insn (op0, ireg);
34975 }
34976
34977 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
34978    result in OPERAND0.  */
34979 void
34980 ix86_expand_rint (rtx operand0, rtx operand1)
34981 {
34982   /* C code for the stuff we're doing below:
34983         xa = fabs (operand1);
34984         if (!isless (xa, 2**52))
34985           return operand1;
34986         xa = xa + 2**52 - 2**52;
34987         return copysign (xa, operand1);
34988    */
34989   enum machine_mode mode = GET_MODE (operand0);
34990   rtx res, xa, label, TWO52, mask;
34991
34992   res = gen_reg_rtx (mode);
34993   emit_move_insn (res, operand1);
34994
34995   /* xa = abs (operand1) */
34996   xa = ix86_expand_sse_fabs (res, &mask);
34997
34998   /* if (!isless (xa, TWO52)) goto label; */
34999   TWO52 = ix86_gen_TWO52 (mode);
35000   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35001
35002   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35003   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
35004
35005   ix86_sse_copysign_to_positive (res, xa, res, mask);
35006
35007   emit_label (label);
35008   LABEL_NUSES (label) = 1;
35009
35010   emit_move_insn (operand0, res);
35011 }
35012
35013 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
35014    into OPERAND0.  */
35015 void
35016 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
35017 {
35018   /* C code for the stuff we expand below.
35019         double xa = fabs (x), x2;
35020         if (!isless (xa, TWO52))
35021           return x;
35022         xa = xa + TWO52 - TWO52;
35023         x2 = copysign (xa, x);
35024      Compensate.  Floor:
35025         if (x2 > x)
35026           x2 -= 1;
35027      Compensate.  Ceil:
35028         if (x2 < x)
35029           x2 -= -1;
35030         return x2;
35031    */
35032   enum machine_mode mode = GET_MODE (operand0);
35033   rtx xa, TWO52, tmp, label, one, res, mask;
35034
35035   TWO52 = ix86_gen_TWO52 (mode);
35036
35037   /* Temporary for holding the result, initialized to the input
35038      operand to ease control flow.  */
35039   res = gen_reg_rtx (mode);
35040   emit_move_insn (res, operand1);
35041
35042   /* xa = abs (operand1) */
35043   xa = ix86_expand_sse_fabs (res, &mask);
35044
35045   /* if (!isless (xa, TWO52)) goto label; */
35046   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35047
35048   /* xa = xa + TWO52 - TWO52; */
35049   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35050   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
35051
35052   /* xa = copysign (xa, operand1) */
35053   ix86_sse_copysign_to_positive (xa, xa, res, mask);
35054
35055   /* generate 1.0 or -1.0 */
35056   one = force_reg (mode,
35057                    const_double_from_real_value (do_floor
35058                                                  ? dconst1 : dconstm1, mode));
35059
35060   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
35061   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
35062   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35063                           gen_rtx_AND (mode, one, tmp)));
35064   /* We always need to subtract here to preserve signed zero.  */
35065   tmp = expand_simple_binop (mode, MINUS,
35066                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35067   emit_move_insn (res, tmp);
35068
35069   emit_label (label);
35070   LABEL_NUSES (label) = 1;
35071
35072   emit_move_insn (operand0, res);
35073 }
35074
35075 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
35076    into OPERAND0.  */
35077 void
35078 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
35079 {
35080   /* C code for the stuff we expand below.
35081         double xa = fabs (x), x2;
35082         if (!isless (xa, TWO52))
35083           return x;
35084         x2 = (double)(long)x;
35085      Compensate.  Floor:
35086         if (x2 > x)
35087           x2 -= 1;
35088      Compensate.  Ceil:
35089         if (x2 < x)
35090           x2 += 1;
35091         if (HONOR_SIGNED_ZEROS (mode))
35092           return copysign (x2, x);
35093         return x2;
35094    */
35095   enum machine_mode mode = GET_MODE (operand0);
35096   rtx xa, xi, TWO52, tmp, label, one, res, mask;
35097
35098   TWO52 = ix86_gen_TWO52 (mode);
35099
35100   /* Temporary for holding the result, initialized to the input
35101      operand to ease control flow.  */
35102   res = gen_reg_rtx (mode);
35103   emit_move_insn (res, operand1);
35104
35105   /* xa = abs (operand1) */
35106   xa = ix86_expand_sse_fabs (res, &mask);
35107
35108   /* if (!isless (xa, TWO52)) goto label; */
35109   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35110
35111   /* xa = (double)(long)x */
35112   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
35113   expand_fix (xi, res, 0);
35114   expand_float (xa, xi, 0);
35115
35116   /* generate 1.0 */
35117   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
35118
35119   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
35120   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
35121   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35122                           gen_rtx_AND (mode, one, tmp)));
35123   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
35124                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35125   emit_move_insn (res, tmp);
35126
35127   if (HONOR_SIGNED_ZEROS (mode))
35128     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
35129
35130   emit_label (label);
35131   LABEL_NUSES (label) = 1;
35132
35133   emit_move_insn (operand0, res);
35134 }
35135
35136 /* Expand SSE sequence for computing round from OPERAND1 storing
35137    into OPERAND0.  Sequence that works without relying on DImode truncation
35138    via cvttsd2siq that is only available on 64bit targets.  */
35139 void
35140 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
35141 {
35142   /* C code for the stuff we expand below.
35143         double xa = fabs (x), xa2, x2;
35144         if (!isless (xa, TWO52))
35145           return x;
35146      Using the absolute value and copying back sign makes
35147      -0.0 -> -0.0 correct.
35148         xa2 = xa + TWO52 - TWO52;
35149      Compensate.
35150         dxa = xa2 - xa;
35151         if (dxa <= -0.5)
35152           xa2 += 1;
35153         else if (dxa > 0.5)
35154           xa2 -= 1;
35155         x2 = copysign (xa2, x);
35156         return x2;
35157    */
35158   enum machine_mode mode = GET_MODE (operand0);
35159   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
35160
35161   TWO52 = ix86_gen_TWO52 (mode);
35162
35163   /* Temporary for holding the result, initialized to the input
35164      operand to ease control flow.  */
35165   res = gen_reg_rtx (mode);
35166   emit_move_insn (res, operand1);
35167
35168   /* xa = abs (operand1) */
35169   xa = ix86_expand_sse_fabs (res, &mask);
35170
35171   /* if (!isless (xa, TWO52)) goto label; */
35172   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35173
35174   /* xa2 = xa + TWO52 - TWO52; */
35175   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35176   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
35177
35178   /* dxa = xa2 - xa; */
35179   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
35180
35181   /* generate 0.5, 1.0 and -0.5 */
35182   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
35183   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
35184   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
35185                                0, OPTAB_DIRECT);
35186
35187   /* Compensate.  */
35188   tmp = gen_reg_rtx (mode);
35189   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
35190   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
35191   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35192                           gen_rtx_AND (mode, one, tmp)));
35193   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35194   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
35195   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
35196   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35197                           gen_rtx_AND (mode, one, tmp)));
35198   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35199
35200   /* res = copysign (xa2, operand1) */
35201   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
35202
35203   emit_label (label);
35204   LABEL_NUSES (label) = 1;
35205
35206   emit_move_insn (operand0, res);
35207 }
35208
35209 /* Expand SSE sequence for computing trunc from OPERAND1 storing
35210    into OPERAND0.  */
35211 void
35212 ix86_expand_trunc (rtx operand0, rtx operand1)
35213 {
35214   /* C code for SSE variant we expand below.
35215         double xa = fabs (x), x2;
35216         if (!isless (xa, TWO52))
35217           return x;
35218         x2 = (double)(long)x;
35219         if (HONOR_SIGNED_ZEROS (mode))
35220           return copysign (x2, x);
35221         return x2;
35222    */
35223   enum machine_mode mode = GET_MODE (operand0);
35224   rtx xa, xi, TWO52, label, res, mask;
35225
35226   TWO52 = ix86_gen_TWO52 (mode);
35227
35228   /* Temporary for holding the result, initialized to the input
35229      operand to ease control flow.  */
35230   res = gen_reg_rtx (mode);
35231   emit_move_insn (res, operand1);
35232
35233   /* xa = abs (operand1) */
35234   xa = ix86_expand_sse_fabs (res, &mask);
35235
35236   /* if (!isless (xa, TWO52)) goto label; */
35237   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35238
35239   /* x = (double)(long)x */
35240   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
35241   expand_fix (xi, res, 0);
35242   expand_float (res, xi, 0);
35243
35244   if (HONOR_SIGNED_ZEROS (mode))
35245     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
35246
35247   emit_label (label);
35248   LABEL_NUSES (label) = 1;
35249
35250   emit_move_insn (operand0, res);
35251 }
35252
35253 /* Expand SSE sequence for computing trunc from OPERAND1 storing
35254    into OPERAND0.  */
35255 void
35256 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
35257 {
35258   enum machine_mode mode = GET_MODE (operand0);
35259   rtx xa, mask, TWO52, label, one, res, smask, tmp;
35260
35261   /* C code for SSE variant we expand below.
35262         double xa = fabs (x), x2;
35263         if (!isless (xa, TWO52))
35264           return x;
35265         xa2 = xa + TWO52 - TWO52;
35266      Compensate:
35267         if (xa2 > xa)
35268           xa2 -= 1.0;
35269         x2 = copysign (xa2, x);
35270         return x2;
35271    */
35272
35273   TWO52 = ix86_gen_TWO52 (mode);
35274
35275   /* Temporary for holding the result, initialized to the input
35276      operand to ease control flow.  */
35277   res = gen_reg_rtx (mode);
35278   emit_move_insn (res, operand1);
35279
35280   /* xa = abs (operand1) */
35281   xa = ix86_expand_sse_fabs (res, &smask);
35282
35283   /* if (!isless (xa, TWO52)) goto label; */
35284   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35285
35286   /* res = xa + TWO52 - TWO52; */
35287   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35288   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
35289   emit_move_insn (res, tmp);
35290
35291   /* generate 1.0 */
35292   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
35293
35294   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
35295   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
35296   emit_insn (gen_rtx_SET (VOIDmode, mask,
35297                           gen_rtx_AND (mode, mask, one)));
35298   tmp = expand_simple_binop (mode, MINUS,
35299                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
35300   emit_move_insn (res, tmp);
35301
35302   /* res = copysign (res, operand1) */
35303   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
35304
35305   emit_label (label);
35306   LABEL_NUSES (label) = 1;
35307
35308   emit_move_insn (operand0, res);
35309 }
35310
35311 /* Expand SSE sequence for computing round from OPERAND1 storing
35312    into OPERAND0.  */
35313 void
35314 ix86_expand_round (rtx operand0, rtx operand1)
35315 {
35316   /* C code for the stuff we're doing below:
35317         double xa = fabs (x);
35318         if (!isless (xa, TWO52))
35319           return x;
35320         xa = (double)(long)(xa + nextafter (0.5, 0.0));
35321         return copysign (xa, x);
35322    */
35323   enum machine_mode mode = GET_MODE (operand0);
35324   rtx res, TWO52, xa, label, xi, half, mask;
35325   const struct real_format *fmt;
35326   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
35327
35328   /* Temporary for holding the result, initialized to the input
35329      operand to ease control flow.  */
35330   res = gen_reg_rtx (mode);
35331   emit_move_insn (res, operand1);
35332
35333   TWO52 = ix86_gen_TWO52 (mode);
35334   xa = ix86_expand_sse_fabs (res, &mask);
35335   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35336
35337   /* load nextafter (0.5, 0.0) */
35338   fmt = REAL_MODE_FORMAT (mode);
35339   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
35340   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
35341
35342   /* xa = xa + 0.5 */
35343   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
35344   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
35345
35346   /* xa = (double)(int64_t)xa */
35347   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
35348   expand_fix (xi, xa, 0);
35349   expand_float (xa, xi, 0);
35350
35351   /* res = copysign (xa, operand1) */
35352   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
35353
35354   emit_label (label);
35355   LABEL_NUSES (label) = 1;
35356
35357   emit_move_insn (operand0, res);
35358 }
35359
35360 /* Expand SSE sequence for computing round
35361    from OP1 storing into OP0 using sse4 round insn.  */
35362 void
35363 ix86_expand_round_sse4 (rtx op0, rtx op1)
35364 {
35365   enum machine_mode mode = GET_MODE (op0);
35366   rtx e1, e2, res, half;
35367   const struct real_format *fmt;
35368   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
35369   rtx (*gen_copysign) (rtx, rtx, rtx);
35370   rtx (*gen_round) (rtx, rtx, rtx);
35371
35372   switch (mode)
35373     {
35374     case SFmode:
35375       gen_copysign = gen_copysignsf3;
35376       gen_round = gen_sse4_1_roundsf2;
35377       break;
35378     case DFmode:
35379       gen_copysign = gen_copysigndf3;
35380       gen_round = gen_sse4_1_rounddf2;
35381       break;
35382     default:
35383       gcc_unreachable ();
35384     }
35385
35386   /* round (a) = trunc (a + copysign (0.5, a)) */
35387
35388   /* load nextafter (0.5, 0.0) */
35389   fmt = REAL_MODE_FORMAT (mode);
35390   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
35391   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
35392   half = const_double_from_real_value (pred_half, mode);
35393
35394   /* e1 = copysign (0.5, op1) */
35395   e1 = gen_reg_rtx (mode);
35396   emit_insn (gen_copysign (e1, half, op1));
35397
35398   /* e2 = op1 + e1 */
35399   e2 = expand_simple_binop (mode, PLUS, op1, e1, NULL_RTX, 0, OPTAB_DIRECT);
35400
35401   /* res = trunc (e2) */
35402   res = gen_reg_rtx (mode);
35403   emit_insn (gen_round (res, e2, GEN_INT (ROUND_TRUNC)));
35404
35405   emit_move_insn (op0, res);
35406 }
35407 \f
35408
35409 /* Table of valid machine attributes.  */
35410 static const struct attribute_spec ix86_attribute_table[] =
35411 {
35412   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
35413        affects_type_identity } */
35414   /* Stdcall attribute says callee is responsible for popping arguments
35415      if they are not variable.  */
35416   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35417     true },
35418   /* Fastcall attribute says callee is responsible for popping arguments
35419      if they are not variable.  */
35420   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35421     true },
35422   /* Thiscall attribute says callee is responsible for popping arguments
35423      if they are not variable.  */
35424   { "thiscall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35425     true },
35426   /* Cdecl attribute says the callee is a normal C declaration */
35427   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35428     true },
35429   /* Regparm attribute specifies how many integer arguments are to be
35430      passed in registers.  */
35431   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute,
35432     true },
35433   /* Sseregparm attribute says we are using x86_64 calling conventions
35434      for FP arguments.  */
35435   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute,
35436     true },
35437   /* The transactional memory builtins are implicitly regparm or fastcall
35438      depending on the ABI.  Override the generic do-nothing attribute that
35439      these builtins were declared with.  */
35440   { "*tm regparm", 0, 0, false, true, true, ix86_handle_tm_regparm_attribute,
35441     true },
35442   /* force_align_arg_pointer says this function realigns the stack at entry.  */
35443   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
35444     false, true,  true, ix86_handle_cconv_attribute, false },
35445 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
35446   { "dllimport", 0, 0, false, false, false, handle_dll_attribute, false },
35447   { "dllexport", 0, 0, false, false, false, handle_dll_attribute, false },
35448   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute,
35449     false },
35450 #endif
35451   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute,
35452     false },
35453   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute,
35454     false },
35455 #ifdef SUBTARGET_ATTRIBUTE_TABLE
35456   SUBTARGET_ATTRIBUTE_TABLE,
35457 #endif
35458   /* ms_abi and sysv_abi calling convention function attributes.  */
35459   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute, true },
35460   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute, true },
35461   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute,
35462     false },
35463   { "callee_pop_aggregate_return", 1, 1, false, true, true,
35464     ix86_handle_callee_pop_aggregate_return, true },
35465   /* End element.  */
35466   { NULL,        0, 0, false, false, false, NULL, false }
35467 };
35468
35469 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
35470 static int
35471 ix86_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
35472                                  tree vectype ATTRIBUTE_UNUSED,
35473                                  int misalign ATTRIBUTE_UNUSED)
35474 {
35475   switch (type_of_cost)
35476     {
35477       case scalar_stmt:
35478         return ix86_cost->scalar_stmt_cost;
35479
35480       case scalar_load:
35481         return ix86_cost->scalar_load_cost;
35482
35483       case scalar_store:
35484         return ix86_cost->scalar_store_cost;
35485
35486       case vector_stmt:
35487         return ix86_cost->vec_stmt_cost;
35488
35489       case vector_load:
35490         return ix86_cost->vec_align_load_cost;
35491
35492       case vector_store:
35493         return ix86_cost->vec_store_cost;
35494
35495       case vec_to_scalar:
35496         return ix86_cost->vec_to_scalar_cost;
35497
35498       case scalar_to_vec:
35499         return ix86_cost->scalar_to_vec_cost;
35500
35501       case unaligned_load:
35502       case unaligned_store:
35503         return ix86_cost->vec_unalign_load_cost;
35504
35505       case cond_branch_taken:
35506         return ix86_cost->cond_taken_branch_cost;
35507
35508       case cond_branch_not_taken:
35509         return ix86_cost->cond_not_taken_branch_cost;
35510
35511       case vec_perm:
35512       case vec_promote_demote:
35513         return ix86_cost->vec_stmt_cost;
35514
35515       default:
35516         gcc_unreachable ();
35517     }
35518 }
35519
35520 /* Construct (set target (vec_select op0 (parallel perm))) and
35521    return true if that's a valid instruction in the active ISA.  */
35522
35523 static bool
35524 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
35525 {
35526   rtx rperm[MAX_VECT_LEN], x;
35527   unsigned i;
35528
35529   for (i = 0; i < nelt; ++i)
35530     rperm[i] = GEN_INT (perm[i]);
35531
35532   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
35533   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
35534   x = gen_rtx_SET (VOIDmode, target, x);
35535
35536   x = emit_insn (x);
35537   if (recog_memoized (x) < 0)
35538     {
35539       remove_insn (x);
35540       return false;
35541     }
35542   return true;
35543 }
35544
35545 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
35546
35547 static bool
35548 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
35549                         const unsigned char *perm, unsigned nelt)
35550 {
35551   enum machine_mode v2mode;
35552   rtx x;
35553
35554   v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
35555   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
35556   return expand_vselect (target, x, perm, nelt);
35557 }
35558
35559 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
35560    in terms of blendp[sd] / pblendw / pblendvb / vpblendd.  */
35561
35562 static bool
35563 expand_vec_perm_blend (struct expand_vec_perm_d *d)
35564 {
35565   enum machine_mode vmode = d->vmode;
35566   unsigned i, mask, nelt = d->nelt;
35567   rtx target, op0, op1, x;
35568   rtx rperm[32], vperm;
35569
35570   if (d->op0 == d->op1)
35571     return false;
35572   if (TARGET_AVX2 && GET_MODE_SIZE (vmode) == 32)
35573     ;
35574   else if (TARGET_AVX && (vmode == V4DFmode || vmode == V8SFmode))
35575     ;
35576   else if (TARGET_SSE4_1 && GET_MODE_SIZE (vmode) == 16)
35577     ;
35578   else
35579     return false;
35580
35581   /* This is a blend, not a permute.  Elements must stay in their
35582      respective lanes.  */
35583   for (i = 0; i < nelt; ++i)
35584     {
35585       unsigned e = d->perm[i];
35586       if (!(e == i || e == i + nelt))
35587         return false;
35588     }
35589
35590   if (d->testing_p)
35591     return true;
35592
35593   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
35594      decision should be extracted elsewhere, so that we only try that
35595      sequence once all budget==3 options have been tried.  */
35596   target = d->target;
35597   op0 = d->op0;
35598   op1 = d->op1;
35599   mask = 0;
35600
35601   switch (vmode)
35602     {
35603     case V4DFmode:
35604     case V8SFmode:
35605     case V2DFmode:
35606     case V4SFmode:
35607     case V8HImode:
35608     case V8SImode:
35609       for (i = 0; i < nelt; ++i)
35610         mask |= (d->perm[i] >= nelt) << i;
35611       break;
35612
35613     case V2DImode:
35614       for (i = 0; i < 2; ++i)
35615         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
35616       vmode = V8HImode;
35617       goto do_subreg;
35618
35619     case V4SImode:
35620       for (i = 0; i < 4; ++i)
35621         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
35622       vmode = V8HImode;
35623       goto do_subreg;
35624
35625     case V16QImode:
35626       /* See if bytes move in pairs so we can use pblendw with
35627          an immediate argument, rather than pblendvb with a vector
35628          argument.  */
35629       for (i = 0; i < 16; i += 2)
35630         if (d->perm[i] + 1 != d->perm[i + 1])
35631           {
35632           use_pblendvb:
35633             for (i = 0; i < nelt; ++i)
35634               rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
35635
35636           finish_pblendvb:
35637             vperm = gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (nelt, rperm));
35638             vperm = force_reg (vmode, vperm);
35639
35640             if (GET_MODE_SIZE (vmode) == 16)
35641               emit_insn (gen_sse4_1_pblendvb (target, op0, op1, vperm));
35642             else
35643               emit_insn (gen_avx2_pblendvb (target, op0, op1, vperm));
35644             return true;
35645           }
35646
35647       for (i = 0; i < 8; ++i)
35648         mask |= (d->perm[i * 2] >= 16) << i;
35649       vmode = V8HImode;
35650       /* FALLTHRU */
35651
35652     do_subreg:
35653       target = gen_lowpart (vmode, target);
35654       op0 = gen_lowpart (vmode, op0);
35655       op1 = gen_lowpart (vmode, op1);
35656       break;
35657
35658     case V32QImode:
35659       /* See if bytes move in pairs.  If not, vpblendvb must be used.  */
35660       for (i = 0; i < 32; i += 2)
35661         if (d->perm[i] + 1 != d->perm[i + 1])
35662           goto use_pblendvb;
35663       /* See if bytes move in quadruplets.  If yes, vpblendd
35664          with immediate can be used.  */
35665       for (i = 0; i < 32; i += 4)
35666         if (d->perm[i] + 2 != d->perm[i + 2])
35667           break;
35668       if (i < 32)
35669         {
35670           /* See if bytes move the same in both lanes.  If yes,
35671              vpblendw with immediate can be used.  */
35672           for (i = 0; i < 16; i += 2)
35673             if (d->perm[i] + 16 != d->perm[i + 16])
35674               goto use_pblendvb;
35675
35676           /* Use vpblendw.  */
35677           for (i = 0; i < 16; ++i)
35678             mask |= (d->perm[i * 2] >= 32) << i;
35679           vmode = V16HImode;
35680           goto do_subreg;
35681         }
35682
35683       /* Use vpblendd.  */
35684       for (i = 0; i < 8; ++i)
35685         mask |= (d->perm[i * 4] >= 32) << i;
35686       vmode = V8SImode;
35687       goto do_subreg;
35688
35689     case V16HImode:
35690       /* See if words move in pairs.  If yes, vpblendd can be used.  */
35691       for (i = 0; i < 16; i += 2)
35692         if (d->perm[i] + 1 != d->perm[i + 1])
35693           break;
35694       if (i < 16)
35695         {
35696           /* See if words move the same in both lanes.  If not,
35697              vpblendvb must be used.  */
35698           for (i = 0; i < 8; i++)
35699             if (d->perm[i] + 8 != d->perm[i + 8])
35700               {
35701                 /* Use vpblendvb.  */
35702                 for (i = 0; i < 32; ++i)
35703                   rperm[i] = (d->perm[i / 2] < 16 ? const0_rtx : constm1_rtx);
35704
35705                 vmode = V32QImode;
35706                 nelt = 32;
35707                 target = gen_lowpart (vmode, target);
35708                 op0 = gen_lowpart (vmode, op0);
35709                 op1 = gen_lowpart (vmode, op1);
35710                 goto finish_pblendvb;
35711               }
35712
35713           /* Use vpblendw.  */
35714           for (i = 0; i < 16; ++i)
35715             mask |= (d->perm[i] >= 16) << i;
35716           break;
35717         }
35718
35719       /* Use vpblendd.  */
35720       for (i = 0; i < 8; ++i)
35721         mask |= (d->perm[i * 2] >= 16) << i;
35722       vmode = V8SImode;
35723       goto do_subreg;
35724
35725     case V4DImode:
35726       /* Use vpblendd.  */
35727       for (i = 0; i < 4; ++i)
35728         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
35729       vmode = V8SImode;
35730       goto do_subreg;
35731
35732     default:
35733       gcc_unreachable ();
35734     }
35735
35736   /* This matches five different patterns with the different modes.  */
35737   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
35738   x = gen_rtx_SET (VOIDmode, target, x);
35739   emit_insn (x);
35740
35741   return true;
35742 }
35743
35744 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
35745    in terms of the variable form of vpermilps.
35746
35747    Note that we will have already failed the immediate input vpermilps,
35748    which requires that the high and low part shuffle be identical; the
35749    variable form doesn't require that.  */
35750
35751 static bool
35752 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
35753 {
35754   rtx rperm[8], vperm;
35755   unsigned i;
35756
35757   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
35758     return false;
35759
35760   /* We can only permute within the 128-bit lane.  */
35761   for (i = 0; i < 8; ++i)
35762     {
35763       unsigned e = d->perm[i];
35764       if (i < 4 ? e >= 4 : e < 4)
35765         return false;
35766     }
35767
35768   if (d->testing_p)
35769     return true;
35770
35771   for (i = 0; i < 8; ++i)
35772     {
35773       unsigned e = d->perm[i];
35774
35775       /* Within each 128-bit lane, the elements of op0 are numbered
35776          from 0 and the elements of op1 are numbered from 4.  */
35777       if (e >= 8 + 4)
35778         e -= 8;
35779       else if (e >= 4)
35780         e -= 4;
35781
35782       rperm[i] = GEN_INT (e);
35783     }
35784
35785   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
35786   vperm = force_reg (V8SImode, vperm);
35787   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
35788
35789   return true;
35790 }
35791
35792 /* Return true if permutation D can be performed as VMODE permutation
35793    instead.  */
35794
35795 static bool
35796 valid_perm_using_mode_p (enum machine_mode vmode, struct expand_vec_perm_d *d)
35797 {
35798   unsigned int i, j, chunk;
35799
35800   if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT
35801       || GET_MODE_CLASS (d->vmode) != MODE_VECTOR_INT
35802       || GET_MODE_SIZE (vmode) != GET_MODE_SIZE (d->vmode))
35803     return false;
35804
35805   if (GET_MODE_NUNITS (vmode) >= d->nelt)
35806     return true;
35807
35808   chunk = d->nelt / GET_MODE_NUNITS (vmode);
35809   for (i = 0; i < d->nelt; i += chunk)
35810     if (d->perm[i] & (chunk - 1))
35811       return false;
35812     else
35813       for (j = 1; j < chunk; ++j)
35814         if (d->perm[i] + j != d->perm[i + j])
35815           return false;
35816
35817   return true;
35818 }
35819
35820 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
35821    in terms of pshufb, vpperm, vpermq, vpermd or vperm2i128.  */
35822
35823 static bool
35824 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
35825 {
35826   unsigned i, nelt, eltsz, mask;
35827   unsigned char perm[32];
35828   enum machine_mode vmode = V16QImode;
35829   rtx rperm[32], vperm, target, op0, op1;
35830
35831   nelt = d->nelt;
35832
35833   if (d->op0 != d->op1)
35834     {
35835       if (!TARGET_XOP || GET_MODE_SIZE (d->vmode) != 16)
35836         {
35837           if (TARGET_AVX2
35838               && valid_perm_using_mode_p (V2TImode, d))
35839             {
35840               if (d->testing_p)
35841                 return true;
35842
35843               /* Use vperm2i128 insn.  The pattern uses
35844                  V4DImode instead of V2TImode.  */
35845               target = gen_lowpart (V4DImode, d->target);
35846               op0 = gen_lowpart (V4DImode, d->op0);
35847               op1 = gen_lowpart (V4DImode, d->op1);
35848               rperm[0]
35849                 = GEN_INT (((d->perm[0] & (nelt / 2)) ? 1 : 0)
35850                            || ((d->perm[nelt / 2] & (nelt / 2)) ? 2 : 0));
35851               emit_insn (gen_avx2_permv2ti (target, op0, op1, rperm[0]));
35852               return true;
35853             }
35854           return false;
35855         }
35856     }
35857   else
35858     {
35859       if (GET_MODE_SIZE (d->vmode) == 16)
35860         {
35861           if (!TARGET_SSSE3)
35862             return false;
35863         }
35864       else if (GET_MODE_SIZE (d->vmode) == 32)
35865         {
35866           if (!TARGET_AVX2)
35867             return false;
35868
35869           /* V4DImode should be already handled through
35870              expand_vselect by vpermq instruction.  */
35871           gcc_assert (d->vmode != V4DImode);
35872
35873           vmode = V32QImode;
35874           if (d->vmode == V8SImode
35875               || d->vmode == V16HImode
35876               || d->vmode == V32QImode)
35877             {
35878               /* First see if vpermq can be used for
35879                  V8SImode/V16HImode/V32QImode.  */
35880               if (valid_perm_using_mode_p (V4DImode, d))
35881                 {
35882                   for (i = 0; i < 4; i++)
35883                     perm[i] = (d->perm[i * nelt / 4] * 4 / nelt) & 3;
35884                   if (d->testing_p)
35885                     return true;
35886                   return expand_vselect (gen_lowpart (V4DImode, d->target),
35887                                          gen_lowpart (V4DImode, d->op0),
35888                                          perm, 4);
35889                 }
35890
35891               /* Next see if vpermd can be used.  */
35892               if (valid_perm_using_mode_p (V8SImode, d))
35893                 vmode = V8SImode;
35894             }
35895
35896           if (vmode == V32QImode)
35897             {
35898               /* vpshufb only works intra lanes, it is not
35899                  possible to shuffle bytes in between the lanes.  */
35900               for (i = 0; i < nelt; ++i)
35901                 if ((d->perm[i] ^ i) & (nelt / 2))
35902                   return false;
35903             }
35904         }
35905       else
35906         return false;
35907     }
35908
35909   if (d->testing_p)
35910     return true;
35911
35912   if (vmode == V8SImode)
35913     for (i = 0; i < 8; ++i)
35914       rperm[i] = GEN_INT ((d->perm[i * nelt / 8] * 8 / nelt) & 7);
35915   else
35916     {
35917       eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
35918       if (d->op0 != d->op1)
35919         mask = 2 * nelt - 1;
35920       else if (vmode == V16QImode)
35921         mask = nelt - 1;
35922       else
35923         mask = nelt / 2 - 1;
35924
35925       for (i = 0; i < nelt; ++i)
35926         {
35927           unsigned j, e = d->perm[i] & mask;
35928           for (j = 0; j < eltsz; ++j)
35929             rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
35930         }
35931     }
35932
35933   vperm = gen_rtx_CONST_VECTOR (vmode,
35934                                 gen_rtvec_v (GET_MODE_NUNITS (vmode), rperm));
35935   vperm = force_reg (vmode, vperm);
35936
35937   target = gen_lowpart (vmode, d->target);
35938   op0 = gen_lowpart (vmode, d->op0);
35939   if (d->op0 == d->op1)
35940     {
35941       if (vmode == V16QImode)
35942         emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
35943       else if (vmode == V32QImode)
35944         emit_insn (gen_avx2_pshufbv32qi3 (target, op0, vperm));
35945       else
35946         emit_insn (gen_avx2_permvarv8si (target, op0, vperm));
35947     }
35948   else
35949     {
35950       op1 = gen_lowpart (vmode, d->op1);
35951       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
35952     }
35953
35954   return true;
35955 }
35956
35957 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
35958    in a single instruction.  */
35959
35960 static bool
35961 expand_vec_perm_1 (struct expand_vec_perm_d *d)
35962 {
35963   unsigned i, nelt = d->nelt;
35964   unsigned char perm2[MAX_VECT_LEN];
35965
35966   /* Check plain VEC_SELECT first, because AVX has instructions that could
35967      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
35968      input where SEL+CONCAT may not.  */
35969   if (d->op0 == d->op1)
35970     {
35971       int mask = nelt - 1;
35972       bool identity_perm = true;
35973       bool broadcast_perm = true;
35974
35975       for (i = 0; i < nelt; i++)
35976         {
35977           perm2[i] = d->perm[i] & mask;
35978           if (perm2[i] != i)
35979             identity_perm = false;
35980           if (perm2[i])
35981             broadcast_perm = false;
35982         }
35983
35984       if (identity_perm)
35985         {
35986           if (!d->testing_p)
35987             emit_move_insn (d->target, d->op0);
35988           return true;
35989         }
35990       else if (broadcast_perm && TARGET_AVX2)
35991         {
35992           /* Use vpbroadcast{b,w,d}.  */
35993           rtx op = d->op0, (*gen) (rtx, rtx) = NULL;
35994           switch (d->vmode)
35995             {
35996             case V32QImode:
35997               op = gen_lowpart (V16QImode, op);
35998               gen = gen_avx2_pbroadcastv32qi;
35999               break;
36000             case V16HImode:
36001               op = gen_lowpart (V8HImode, op);
36002               gen = gen_avx2_pbroadcastv16hi;
36003               break;
36004             case V8SImode:
36005               op = gen_lowpart (V4SImode, op);
36006               gen = gen_avx2_pbroadcastv8si;
36007               break;
36008             case V16QImode:
36009               gen = gen_avx2_pbroadcastv16qi;
36010               break;
36011             case V8HImode:
36012               gen = gen_avx2_pbroadcastv8hi;
36013               break;
36014             /* For other modes prefer other shuffles this function creates.  */
36015             default: break;
36016             }
36017           if (gen != NULL)
36018             {
36019               if (!d->testing_p)
36020                 emit_insn (gen (d->target, op));
36021               return true;
36022             }
36023         }
36024
36025       if (expand_vselect (d->target, d->op0, perm2, nelt))
36026         return true;
36027
36028       /* There are plenty of patterns in sse.md that are written for
36029          SEL+CONCAT and are not replicated for a single op.  Perhaps
36030          that should be changed, to avoid the nastiness here.  */
36031
36032       /* Recognize interleave style patterns, which means incrementing
36033          every other permutation operand.  */
36034       for (i = 0; i < nelt; i += 2)
36035         {
36036           perm2[i] = d->perm[i] & mask;
36037           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
36038         }
36039       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
36040         return true;
36041
36042       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
36043       if (nelt >= 4)
36044         {
36045           for (i = 0; i < nelt; i += 4)
36046             {
36047               perm2[i + 0] = d->perm[i + 0] & mask;
36048               perm2[i + 1] = d->perm[i + 1] & mask;
36049               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
36050               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
36051             }
36052
36053           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
36054             return true;
36055         }
36056     }
36057
36058   /* Finally, try the fully general two operand permute.  */
36059   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
36060     return true;
36061
36062   /* Recognize interleave style patterns with reversed operands.  */
36063   if (d->op0 != d->op1)
36064     {
36065       for (i = 0; i < nelt; ++i)
36066         {
36067           unsigned e = d->perm[i];
36068           if (e >= nelt)
36069             e -= nelt;
36070           else
36071             e += nelt;
36072           perm2[i] = e;
36073         }
36074
36075       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
36076         return true;
36077     }
36078
36079   /* Try the SSE4.1 blend variable merge instructions.  */
36080   if (expand_vec_perm_blend (d))
36081     return true;
36082
36083   /* Try one of the AVX vpermil variable permutations.  */
36084   if (expand_vec_perm_vpermil (d))
36085     return true;
36086
36087   /* Try the SSSE3 pshufb or XOP vpperm or AVX2 vperm2i128,
36088      vpshufb, vpermd or vpermq variable permutation.  */
36089   if (expand_vec_perm_pshufb (d))
36090     return true;
36091
36092   return false;
36093 }
36094
36095 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
36096    in terms of a pair of pshuflw + pshufhw instructions.  */
36097
36098 static bool
36099 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
36100 {
36101   unsigned char perm2[MAX_VECT_LEN];
36102   unsigned i;
36103   bool ok;
36104
36105   if (d->vmode != V8HImode || d->op0 != d->op1)
36106     return false;
36107
36108   /* The two permutations only operate in 64-bit lanes.  */
36109   for (i = 0; i < 4; ++i)
36110     if (d->perm[i] >= 4)
36111       return false;
36112   for (i = 4; i < 8; ++i)
36113     if (d->perm[i] < 4)
36114       return false;
36115
36116   if (d->testing_p)
36117     return true;
36118
36119   /* Emit the pshuflw.  */
36120   memcpy (perm2, d->perm, 4);
36121   for (i = 4; i < 8; ++i)
36122     perm2[i] = i;
36123   ok = expand_vselect (d->target, d->op0, perm2, 8);
36124   gcc_assert (ok);
36125
36126   /* Emit the pshufhw.  */
36127   memcpy (perm2 + 4, d->perm + 4, 4);
36128   for (i = 0; i < 4; ++i)
36129     perm2[i] = i;
36130   ok = expand_vselect (d->target, d->target, perm2, 8);
36131   gcc_assert (ok);
36132
36133   return true;
36134 }
36135
36136 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36137    the permutation using the SSSE3 palignr instruction.  This succeeds
36138    when all of the elements in PERM fit within one vector and we merely
36139    need to shift them down so that a single vector permutation has a
36140    chance to succeed.  */
36141
36142 static bool
36143 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
36144 {
36145   unsigned i, nelt = d->nelt;
36146   unsigned min, max;
36147   bool in_order, ok;
36148   rtx shift;
36149
36150   /* Even with AVX, palignr only operates on 128-bit vectors.  */
36151   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
36152     return false;
36153
36154   min = nelt, max = 0;
36155   for (i = 0; i < nelt; ++i)
36156     {
36157       unsigned e = d->perm[i];
36158       if (e < min)
36159         min = e;
36160       if (e > max)
36161         max = e;
36162     }
36163   if (min == 0 || max - min >= nelt)
36164     return false;
36165
36166   /* Given that we have SSSE3, we know we'll be able to implement the
36167      single operand permutation after the palignr with pshufb.  */
36168   if (d->testing_p)
36169     return true;
36170
36171   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
36172   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
36173                                   gen_lowpart (TImode, d->op1),
36174                                   gen_lowpart (TImode, d->op0), shift));
36175
36176   d->op0 = d->op1 = d->target;
36177
36178   in_order = true;
36179   for (i = 0; i < nelt; ++i)
36180     {
36181       unsigned e = d->perm[i] - min;
36182       if (e != i)
36183         in_order = false;
36184       d->perm[i] = e;
36185     }
36186
36187   /* Test for the degenerate case where the alignment by itself
36188      produces the desired permutation.  */
36189   if (in_order)
36190     return true;
36191
36192   ok = expand_vec_perm_1 (d);
36193   gcc_assert (ok);
36194
36195   return ok;
36196 }
36197
36198 static bool expand_vec_perm_interleave3 (struct expand_vec_perm_d *d);
36199
36200 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36201    a two vector permutation into a single vector permutation by using
36202    an interleave operation to merge the vectors.  */
36203
36204 static bool
36205 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
36206 {
36207   struct expand_vec_perm_d dremap, dfinal;
36208   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
36209   unsigned HOST_WIDE_INT contents;
36210   unsigned char remap[2 * MAX_VECT_LEN];
36211   rtx seq;
36212   bool ok, same_halves = false;
36213
36214   if (GET_MODE_SIZE (d->vmode) == 16)
36215     {
36216       if (d->op0 == d->op1)
36217         return false;
36218     }
36219   else if (GET_MODE_SIZE (d->vmode) == 32)
36220     {
36221       if (!TARGET_AVX)
36222         return false;
36223       /* For 32-byte modes allow even d->op0 == d->op1.
36224          The lack of cross-lane shuffling in some instructions
36225          might prevent a single insn shuffle.  */
36226       dfinal = *d;
36227       dfinal.testing_p = true;
36228       /* If expand_vec_perm_interleave3 can expand this into
36229          a 3 insn sequence, give up and let it be expanded as
36230          3 insn sequence.  While that is one insn longer,
36231          it doesn't need a memory operand and in the common
36232          case that both interleave low and high permutations
36233          with the same operands are adjacent needs 4 insns
36234          for both after CSE.  */
36235       if (expand_vec_perm_interleave3 (&dfinal))
36236         return false;
36237     }
36238   else
36239     return false;
36240
36241   /* Examine from whence the elements come.  */
36242   contents = 0;
36243   for (i = 0; i < nelt; ++i)
36244     contents |= ((unsigned HOST_WIDE_INT) 1) << d->perm[i];
36245
36246   memset (remap, 0xff, sizeof (remap));
36247   dremap = *d;
36248
36249   if (GET_MODE_SIZE (d->vmode) == 16)
36250     {
36251       unsigned HOST_WIDE_INT h1, h2, h3, h4;
36252
36253       /* Split the two input vectors into 4 halves.  */
36254       h1 = (((unsigned HOST_WIDE_INT) 1) << nelt2) - 1;
36255       h2 = h1 << nelt2;
36256       h3 = h2 << nelt2;
36257       h4 = h3 << nelt2;
36258
36259       /* If the elements from the low halves use interleave low, and similarly
36260          for interleave high.  If the elements are from mis-matched halves, we
36261          can use shufps for V4SF/V4SI or do a DImode shuffle.  */
36262       if ((contents & (h1 | h3)) == contents)
36263         {
36264           /* punpckl* */
36265           for (i = 0; i < nelt2; ++i)
36266             {
36267               remap[i] = i * 2;
36268               remap[i + nelt] = i * 2 + 1;
36269               dremap.perm[i * 2] = i;
36270               dremap.perm[i * 2 + 1] = i + nelt;
36271             }
36272           if (!TARGET_SSE2 && d->vmode == V4SImode)
36273             dremap.vmode = V4SFmode;
36274         }
36275       else if ((contents & (h2 | h4)) == contents)
36276         {
36277           /* punpckh* */
36278           for (i = 0; i < nelt2; ++i)
36279             {
36280               remap[i + nelt2] = i * 2;
36281               remap[i + nelt + nelt2] = i * 2 + 1;
36282               dremap.perm[i * 2] = i + nelt2;
36283               dremap.perm[i * 2 + 1] = i + nelt + nelt2;
36284             }
36285           if (!TARGET_SSE2 && d->vmode == V4SImode)
36286             dremap.vmode = V4SFmode;
36287         }
36288       else if ((contents & (h1 | h4)) == contents)
36289         {
36290           /* shufps */
36291           for (i = 0; i < nelt2; ++i)
36292             {
36293               remap[i] = i;
36294               remap[i + nelt + nelt2] = i + nelt2;
36295               dremap.perm[i] = i;
36296               dremap.perm[i + nelt2] = i + nelt + nelt2;
36297             }
36298           if (nelt != 4)
36299             {
36300               /* shufpd */
36301               dremap.vmode = V2DImode;
36302               dremap.nelt = 2;
36303               dremap.perm[0] = 0;
36304               dremap.perm[1] = 3;
36305             }
36306         }
36307       else if ((contents & (h2 | h3)) == contents)
36308         {
36309           /* shufps */
36310           for (i = 0; i < nelt2; ++i)
36311             {
36312               remap[i + nelt2] = i;
36313               remap[i + nelt] = i + nelt2;
36314               dremap.perm[i] = i + nelt2;
36315               dremap.perm[i + nelt2] = i + nelt;
36316             }
36317           if (nelt != 4)
36318             {
36319               /* shufpd */
36320               dremap.vmode = V2DImode;
36321               dremap.nelt = 2;
36322               dremap.perm[0] = 1;
36323               dremap.perm[1] = 2;
36324             }
36325         }
36326       else
36327         return false;
36328     }
36329   else
36330     {
36331       unsigned int nelt4 = nelt / 4, nzcnt = 0;
36332       unsigned HOST_WIDE_INT q[8];
36333       unsigned int nonzero_halves[4];
36334
36335       /* Split the two input vectors into 8 quarters.  */
36336       q[0] = (((unsigned HOST_WIDE_INT) 1) << nelt4) - 1;
36337       for (i = 1; i < 8; ++i)
36338         q[i] = q[0] << (nelt4 * i);
36339       for (i = 0; i < 4; ++i)
36340         if (((q[2 * i] | q[2 * i + 1]) & contents) != 0)
36341           {
36342             nonzero_halves[nzcnt] = i;
36343             ++nzcnt;
36344           }
36345
36346       if (nzcnt == 1)
36347         {
36348           gcc_assert (d->op0 == d->op1);
36349           nonzero_halves[1] = nonzero_halves[0];
36350           same_halves = true;
36351         }
36352       else if (d->op0 == d->op1)
36353         {
36354           gcc_assert (nonzero_halves[0] == 0);
36355           gcc_assert (nonzero_halves[1] == 1);
36356         }
36357
36358       if (nzcnt <= 2)
36359         {
36360           if (d->perm[0] / nelt2 == nonzero_halves[1])
36361             {
36362               /* Attempt to increase the likelyhood that dfinal
36363                  shuffle will be intra-lane.  */
36364               char tmph = nonzero_halves[0];
36365               nonzero_halves[0] = nonzero_halves[1];
36366               nonzero_halves[1] = tmph;
36367             }
36368
36369           /* vperm2f128 or vperm2i128.  */
36370           for (i = 0; i < nelt2; ++i)
36371             {
36372               remap[i + nonzero_halves[1] * nelt2] = i + nelt2;
36373               remap[i + nonzero_halves[0] * nelt2] = i;
36374               dremap.perm[i + nelt2] = i + nonzero_halves[1] * nelt2;
36375               dremap.perm[i] = i + nonzero_halves[0] * nelt2;
36376             }
36377
36378           if (d->vmode != V8SFmode
36379               && d->vmode != V4DFmode
36380               && d->vmode != V8SImode)
36381             {
36382               dremap.vmode = V8SImode;
36383               dremap.nelt = 8;
36384               for (i = 0; i < 4; ++i)
36385                 {
36386                   dremap.perm[i] = i + nonzero_halves[0] * 4;
36387                   dremap.perm[i + 4] = i + nonzero_halves[1] * 4;
36388                 }
36389             }
36390         }
36391       else if (d->op0 == d->op1)
36392         return false;
36393       else if (TARGET_AVX2
36394                && (contents & (q[0] | q[2] | q[4] | q[6])) == contents)
36395         {
36396           /* vpunpckl* */
36397           for (i = 0; i < nelt4; ++i)
36398             {
36399               remap[i] = i * 2;
36400               remap[i + nelt] = i * 2 + 1;
36401               remap[i + nelt2] = i * 2 + nelt2;
36402               remap[i + nelt + nelt2] = i * 2 + nelt2 + 1;
36403               dremap.perm[i * 2] = i;
36404               dremap.perm[i * 2 + 1] = i + nelt;
36405               dremap.perm[i * 2 + nelt2] = i + nelt2;
36406               dremap.perm[i * 2 + nelt2 + 1] = i + nelt + nelt2;
36407             }
36408         }
36409       else if (TARGET_AVX2
36410                && (contents & (q[1] | q[3] | q[5] | q[7])) == contents)
36411         {
36412           /* vpunpckh* */
36413           for (i = 0; i < nelt4; ++i)
36414             {
36415               remap[i + nelt4] = i * 2;
36416               remap[i + nelt + nelt4] = i * 2 + 1;
36417               remap[i + nelt2 + nelt4] = i * 2 + nelt2;
36418               remap[i + nelt + nelt2 + nelt4] = i * 2 + nelt2 + 1;
36419               dremap.perm[i * 2] = i + nelt4;
36420               dremap.perm[i * 2 + 1] = i + nelt + nelt4;
36421               dremap.perm[i * 2 + nelt2] = i + nelt2 + nelt4;
36422               dremap.perm[i * 2 + nelt2 + 1] = i + nelt + nelt2 + nelt4;
36423             }
36424         }
36425       else
36426         return false;
36427     }
36428
36429   /* Use the remapping array set up above to move the elements from their
36430      swizzled locations into their final destinations.  */
36431   dfinal = *d;
36432   for (i = 0; i < nelt; ++i)
36433     {
36434       unsigned e = remap[d->perm[i]];
36435       gcc_assert (e < nelt);
36436       /* If same_halves is true, both halves of the remapped vector are the
36437          same.  Avoid cross-lane accesses if possible.  */
36438       if (same_halves && i >= nelt2)
36439         {
36440           gcc_assert (e < nelt2);
36441           dfinal.perm[i] = e + nelt2;
36442         }
36443       else
36444         dfinal.perm[i] = e;
36445     }
36446   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
36447   dfinal.op1 = dfinal.op0;
36448   dremap.target = dfinal.op0;
36449
36450   /* Test if the final remap can be done with a single insn.  For V4SFmode or
36451      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
36452   start_sequence ();
36453   ok = expand_vec_perm_1 (&dfinal);
36454   seq = get_insns ();
36455   end_sequence ();
36456
36457   if (!ok)
36458     return false;
36459
36460   if (d->testing_p)
36461     return true;
36462
36463   if (dremap.vmode != dfinal.vmode)
36464     {
36465       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
36466       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
36467       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
36468     }
36469
36470   ok = expand_vec_perm_1 (&dremap);
36471   gcc_assert (ok);
36472
36473   emit_insn (seq);
36474   return true;
36475 }
36476
36477 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36478    a single vector cross-lane permutation into vpermq followed
36479    by any of the single insn permutations.  */
36480
36481 static bool
36482 expand_vec_perm_vpermq_perm_1 (struct expand_vec_perm_d *d)
36483 {
36484   struct expand_vec_perm_d dremap, dfinal;
36485   unsigned i, j, nelt = d->nelt, nelt2 = nelt / 2, nelt4 = nelt / 4;
36486   unsigned contents[2];
36487   bool ok;
36488
36489   if (!(TARGET_AVX2
36490         && (d->vmode == V32QImode || d->vmode == V16HImode)
36491         && d->op0 == d->op1))
36492     return false;
36493
36494   contents[0] = 0;
36495   contents[1] = 0;
36496   for (i = 0; i < nelt2; ++i)
36497     {
36498       contents[0] |= 1u << (d->perm[i] / nelt4);
36499       contents[1] |= 1u << (d->perm[i + nelt2] / nelt4);
36500     }
36501
36502   for (i = 0; i < 2; ++i)
36503     {
36504       unsigned int cnt = 0;
36505       for (j = 0; j < 4; ++j)
36506         if ((contents[i] & (1u << j)) != 0 && ++cnt > 2)
36507           return false;
36508     }
36509
36510   if (d->testing_p)
36511     return true;
36512
36513   dremap = *d;
36514   dremap.vmode = V4DImode;
36515   dremap.nelt = 4;
36516   dremap.target = gen_reg_rtx (V4DImode);
36517   dremap.op0 = gen_lowpart (V4DImode, d->op0);
36518   dremap.op1 = dremap.op0;
36519   for (i = 0; i < 2; ++i)
36520     {
36521       unsigned int cnt = 0;
36522       for (j = 0; j < 4; ++j)
36523         if ((contents[i] & (1u << j)) != 0)
36524           dremap.perm[2 * i + cnt++] = j;
36525       for (; cnt < 2; ++cnt)
36526         dremap.perm[2 * i + cnt] = 0;
36527     }
36528
36529   dfinal = *d;
36530   dfinal.op0 = gen_lowpart (dfinal.vmode, dremap.target);
36531   dfinal.op1 = dfinal.op0;
36532   for (i = 0, j = 0; i < nelt; ++i)
36533     {
36534       if (i == nelt2)
36535         j = 2;
36536       dfinal.perm[i] = (d->perm[i] & (nelt4 - 1)) | (j ? nelt2 : 0);
36537       if ((d->perm[i] / nelt4) == dremap.perm[j])
36538         ;
36539       else if ((d->perm[i] / nelt4) == dremap.perm[j + 1])
36540         dfinal.perm[i] |= nelt4;
36541       else
36542         gcc_unreachable ();
36543     }
36544
36545   ok = expand_vec_perm_1 (&dremap);
36546   gcc_assert (ok);
36547
36548   ok = expand_vec_perm_1 (&dfinal);
36549   gcc_assert (ok);
36550
36551   return true;
36552 }
36553
36554 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36555    a two vector permutation using 2 intra-lane interleave insns
36556    and cross-lane shuffle for 32-byte vectors.  */
36557
36558 static bool
36559 expand_vec_perm_interleave3 (struct expand_vec_perm_d *d)
36560 {
36561   unsigned i, nelt;
36562   rtx (*gen) (rtx, rtx, rtx);
36563
36564   if (d->op0 == d->op1)
36565     return false;
36566   if (TARGET_AVX2 && GET_MODE_SIZE (d->vmode) == 32)
36567     ;
36568   else if (TARGET_AVX && (d->vmode == V8SFmode || d->vmode == V4DFmode))
36569     ;
36570   else
36571     return false;
36572
36573   nelt = d->nelt;
36574   if (d->perm[0] != 0 && d->perm[0] != nelt / 2)
36575     return false;
36576   for (i = 0; i < nelt; i += 2)
36577     if (d->perm[i] != d->perm[0] + i / 2
36578         || d->perm[i + 1] != d->perm[0] + i / 2 + nelt)
36579       return false;
36580
36581   if (d->testing_p)
36582     return true;
36583
36584   switch (d->vmode)
36585     {
36586     case V32QImode:
36587       if (d->perm[0])
36588         gen = gen_vec_interleave_highv32qi;
36589       else
36590         gen = gen_vec_interleave_lowv32qi;
36591       break;
36592     case V16HImode:
36593       if (d->perm[0])
36594         gen = gen_vec_interleave_highv16hi;
36595       else
36596         gen = gen_vec_interleave_lowv16hi;
36597       break;
36598     case V8SImode:
36599       if (d->perm[0])
36600         gen = gen_vec_interleave_highv8si;
36601       else
36602         gen = gen_vec_interleave_lowv8si;
36603       break;
36604     case V4DImode:
36605       if (d->perm[0])
36606         gen = gen_vec_interleave_highv4di;
36607       else
36608         gen = gen_vec_interleave_lowv4di;
36609       break;
36610     case V8SFmode:
36611       if (d->perm[0])
36612         gen = gen_vec_interleave_highv8sf;
36613       else
36614         gen = gen_vec_interleave_lowv8sf;
36615       break;
36616     case V4DFmode:
36617       if (d->perm[0])
36618         gen = gen_vec_interleave_highv4df;
36619       else
36620         gen = gen_vec_interleave_lowv4df;
36621       break;
36622     default:
36623       gcc_unreachable ();
36624     }
36625
36626   emit_insn (gen (d->target, d->op0, d->op1));
36627   return true;
36628 }
36629
36630 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
36631    permutation with two pshufb insns and an ior.  We should have already
36632    failed all two instruction sequences.  */
36633
36634 static bool
36635 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
36636 {
36637   rtx rperm[2][16], vperm, l, h, op, m128;
36638   unsigned int i, nelt, eltsz;
36639
36640   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
36641     return false;
36642   gcc_assert (d->op0 != d->op1);
36643
36644   nelt = d->nelt;
36645   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36646
36647   /* Generate two permutation masks.  If the required element is within
36648      the given vector it is shuffled into the proper lane.  If the required
36649      element is in the other vector, force a zero into the lane by setting
36650      bit 7 in the permutation mask.  */
36651   m128 = GEN_INT (-128);
36652   for (i = 0; i < nelt; ++i)
36653     {
36654       unsigned j, e = d->perm[i];
36655       unsigned which = (e >= nelt);
36656       if (e >= nelt)
36657         e -= nelt;
36658
36659       for (j = 0; j < eltsz; ++j)
36660         {
36661           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
36662           rperm[1-which][i*eltsz + j] = m128;
36663         }
36664     }
36665
36666   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
36667   vperm = force_reg (V16QImode, vperm);
36668
36669   l = gen_reg_rtx (V16QImode);
36670   op = gen_lowpart (V16QImode, d->op0);
36671   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
36672
36673   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
36674   vperm = force_reg (V16QImode, vperm);
36675
36676   h = gen_reg_rtx (V16QImode);
36677   op = gen_lowpart (V16QImode, d->op1);
36678   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
36679
36680   op = gen_lowpart (V16QImode, d->target);
36681   emit_insn (gen_iorv16qi3 (op, l, h));
36682
36683   return true;
36684 }
36685
36686 /* Implement arbitrary permutation of one V32QImode and V16QImode operand
36687    with two vpshufb insns, vpermq and vpor.  We should have already failed
36688    all two or three instruction sequences.  */
36689
36690 static bool
36691 expand_vec_perm_vpshufb2_vpermq (struct expand_vec_perm_d *d)
36692 {
36693   rtx rperm[2][32], vperm, l, h, hp, op, m128;
36694   unsigned int i, nelt, eltsz;
36695
36696   if (!TARGET_AVX2
36697       || d->op0 != d->op1
36698       || (d->vmode != V32QImode && d->vmode != V16HImode))
36699     return false;
36700
36701   if (d->testing_p)
36702     return true;
36703
36704   nelt = d->nelt;
36705   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36706
36707   /* Generate two permutation masks.  If the required element is within
36708      the same lane, it is shuffled in.  If the required element from the
36709      other lane, force a zero by setting bit 7 in the permutation mask.
36710      In the other mask the mask has non-negative elements if element
36711      is requested from the other lane, but also moved to the other lane,
36712      so that the result of vpshufb can have the two V2TImode halves
36713      swapped.  */
36714   m128 = GEN_INT (-128);
36715   for (i = 0; i < nelt; ++i)
36716     {
36717       unsigned j, e = d->perm[i] & (nelt / 2 - 1);
36718       unsigned which = ((d->perm[i] ^ i) & (nelt / 2)) * eltsz;
36719
36720       for (j = 0; j < eltsz; ++j)
36721         {
36722           rperm[!!which][(i * eltsz + j) ^ which] = GEN_INT (e * eltsz + j);
36723           rperm[!which][(i * eltsz + j) ^ (which ^ 16)] = m128;
36724         }
36725     }
36726
36727   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[1]));
36728   vperm = force_reg (V32QImode, vperm);
36729
36730   h = gen_reg_rtx (V32QImode);
36731   op = gen_lowpart (V32QImode, d->op0);
36732   emit_insn (gen_avx2_pshufbv32qi3 (h, op, vperm));
36733
36734   /* Swap the 128-byte lanes of h into hp.  */
36735   hp = gen_reg_rtx (V4DImode);
36736   op = gen_lowpart (V4DImode, h);
36737   emit_insn (gen_avx2_permv4di_1 (hp, op, const2_rtx, GEN_INT (3), const0_rtx,
36738                                   const1_rtx));
36739
36740   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[0]));
36741   vperm = force_reg (V32QImode, vperm);
36742
36743   l = gen_reg_rtx (V32QImode);
36744   op = gen_lowpart (V32QImode, d->op0);
36745   emit_insn (gen_avx2_pshufbv32qi3 (l, op, vperm));
36746
36747   op = gen_lowpart (V32QImode, d->target);
36748   emit_insn (gen_iorv32qi3 (op, l, gen_lowpart (V32QImode, hp)));
36749
36750   return true;
36751 }
36752
36753 /* A subroutine of expand_vec_perm_even_odd_1.  Implement extract-even
36754    and extract-odd permutations of two V32QImode and V16QImode operand
36755    with two vpshufb insns, vpor and vpermq.  We should have already
36756    failed all two or three instruction sequences.  */
36757
36758 static bool
36759 expand_vec_perm_vpshufb2_vpermq_even_odd (struct expand_vec_perm_d *d)
36760 {
36761   rtx rperm[2][32], vperm, l, h, ior, op, m128;
36762   unsigned int i, nelt, eltsz;
36763
36764   if (!TARGET_AVX2
36765       || d->op0 == d->op1
36766       || (d->vmode != V32QImode && d->vmode != V16HImode))
36767     return false;
36768
36769   for (i = 0; i < d->nelt; ++i)
36770     if ((d->perm[i] ^ (i * 2)) & (3 * d->nelt / 2))
36771       return false;
36772
36773   if (d->testing_p)
36774     return true;
36775
36776   nelt = d->nelt;
36777   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36778
36779   /* Generate two permutation masks.  In the first permutation mask
36780      the first quarter will contain indexes for the first half
36781      of the op0, the second quarter will contain bit 7 set, third quarter
36782      will contain indexes for the second half of the op0 and the
36783      last quarter bit 7 set.  In the second permutation mask
36784      the first quarter will contain bit 7 set, the second quarter
36785      indexes for the first half of the op1, the third quarter bit 7 set
36786      and last quarter indexes for the second half of the op1.
36787      I.e. the first mask e.g. for V32QImode extract even will be:
36788      0, 2, ..., 0xe, -128, ..., -128, 0, 2, ..., 0xe, -128, ..., -128
36789      (all values masked with 0xf except for -128) and second mask
36790      for extract even will be
36791      -128, ..., -128, 0, 2, ..., 0xe, -128, ..., -128, 0, 2, ..., 0xe.  */
36792   m128 = GEN_INT (-128);
36793   for (i = 0; i < nelt; ++i)
36794     {
36795       unsigned j, e = d->perm[i] & (nelt / 2 - 1);
36796       unsigned which = d->perm[i] >= nelt;
36797       unsigned xorv = (i >= nelt / 4 && i < 3 * nelt / 4) ? 24 : 0;
36798
36799       for (j = 0; j < eltsz; ++j)
36800         {
36801           rperm[which][(i * eltsz + j) ^ xorv] = GEN_INT (e * eltsz + j);
36802           rperm[1 - which][(i * eltsz + j) ^ xorv] = m128;
36803         }
36804     }
36805
36806   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[0]));
36807   vperm = force_reg (V32QImode, vperm);
36808
36809   l = gen_reg_rtx (V32QImode);
36810   op = gen_lowpart (V32QImode, d->op0);
36811   emit_insn (gen_avx2_pshufbv32qi3 (l, op, vperm));
36812
36813   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[1]));
36814   vperm = force_reg (V32QImode, vperm);
36815
36816   h = gen_reg_rtx (V32QImode);
36817   op = gen_lowpart (V32QImode, d->op1);
36818   emit_insn (gen_avx2_pshufbv32qi3 (h, op, vperm));
36819
36820   ior = gen_reg_rtx (V32QImode);
36821   emit_insn (gen_iorv32qi3 (ior, l, h));
36822
36823   /* Permute the V4DImode quarters using { 0, 2, 1, 3 } permutation.  */
36824   op = gen_lowpart (V4DImode, d->target);
36825   ior = gen_lowpart (V4DImode, ior);
36826   emit_insn (gen_avx2_permv4di_1 (op, ior, const0_rtx, const2_rtx,
36827                                   const1_rtx, GEN_INT (3)));
36828
36829   return true;
36830 }
36831
36832 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
36833    and extract-odd permutations.  */
36834
36835 static bool
36836 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
36837 {
36838   rtx t1, t2, t3;
36839
36840   switch (d->vmode)
36841     {
36842     case V4DFmode:
36843       t1 = gen_reg_rtx (V4DFmode);
36844       t2 = gen_reg_rtx (V4DFmode);
36845
36846       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
36847       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
36848       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
36849
36850       /* Now an unpck[lh]pd will produce the result required.  */
36851       if (odd)
36852         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
36853       else
36854         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
36855       emit_insn (t3);
36856       break;
36857
36858     case V8SFmode:
36859       {
36860         int mask = odd ? 0xdd : 0x88;
36861
36862         t1 = gen_reg_rtx (V8SFmode);
36863         t2 = gen_reg_rtx (V8SFmode);
36864         t3 = gen_reg_rtx (V8SFmode);
36865
36866         /* Shuffle within the 128-bit lanes to produce:
36867            { 0 2 8 a 4 6 c e } | { 1 3 9 b 5 7 d f }.  */
36868         emit_insn (gen_avx_shufps256 (t1, d->op0, d->op1,
36869                                       GEN_INT (mask)));
36870
36871         /* Shuffle the lanes around to produce:
36872            { 4 6 c e 0 2 8 a } and { 5 7 d f 1 3 9 b }.  */
36873         emit_insn (gen_avx_vperm2f128v8sf3 (t2, t1, t1,
36874                                             GEN_INT (0x3)));
36875
36876         /* Shuffle within the 128-bit lanes to produce:
36877            { 0 2 4 6 4 6 0 2 } | { 1 3 5 7 5 7 1 3 }.  */
36878         emit_insn (gen_avx_shufps256 (t3, t1, t2, GEN_INT (0x44)));
36879
36880         /* Shuffle within the 128-bit lanes to produce:
36881            { 8 a c e c e 8 a } | { 9 b d f d f 9 b }.  */
36882         emit_insn (gen_avx_shufps256 (t2, t1, t2, GEN_INT (0xee)));
36883
36884         /* Shuffle the lanes around to produce:
36885            { 0 2 4 6 8 a c e } | { 1 3 5 7 9 b d f }.  */
36886         emit_insn (gen_avx_vperm2f128v8sf3 (d->target, t3, t2,
36887                                             GEN_INT (0x20)));
36888       }
36889       break;
36890
36891     case V2DFmode:
36892     case V4SFmode:
36893     case V2DImode:
36894     case V4SImode:
36895       /* These are always directly implementable by expand_vec_perm_1.  */
36896       gcc_unreachable ();
36897
36898     case V8HImode:
36899       if (TARGET_SSSE3)
36900         return expand_vec_perm_pshufb2 (d);
36901       else
36902         {
36903           /* We need 2*log2(N)-1 operations to achieve odd/even
36904              with interleave. */
36905           t1 = gen_reg_rtx (V8HImode);
36906           t2 = gen_reg_rtx (V8HImode);
36907           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
36908           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
36909           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
36910           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
36911           if (odd)
36912             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
36913           else
36914             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
36915           emit_insn (t3);
36916         }
36917       break;
36918
36919     case V16QImode:
36920       if (TARGET_SSSE3)
36921         return expand_vec_perm_pshufb2 (d);
36922       else
36923         {
36924           t1 = gen_reg_rtx (V16QImode);
36925           t2 = gen_reg_rtx (V16QImode);
36926           t3 = gen_reg_rtx (V16QImode);
36927           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
36928           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
36929           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
36930           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
36931           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
36932           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
36933           if (odd)
36934             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
36935           else
36936             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
36937           emit_insn (t3);
36938         }
36939       break;
36940
36941     case V16HImode:
36942     case V32QImode:
36943       return expand_vec_perm_vpshufb2_vpermq_even_odd (d);
36944
36945     case V4DImode:
36946       if (!TARGET_AVX2)
36947         {
36948           struct expand_vec_perm_d d_copy = *d;
36949           d_copy.vmode = V4DFmode;
36950           d_copy.target = gen_lowpart (V4DFmode, d->target);
36951           d_copy.op0 = gen_lowpart (V4DFmode, d->op0);
36952           d_copy.op1 = gen_lowpart (V4DFmode, d->op1);
36953           return expand_vec_perm_even_odd_1 (&d_copy, odd);
36954         }
36955
36956       t1 = gen_reg_rtx (V4DImode);
36957       t2 = gen_reg_rtx (V4DImode);
36958
36959       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
36960       emit_insn (gen_avx2_permv2ti (t1, d->op0, d->op1, GEN_INT (0x20)));
36961       emit_insn (gen_avx2_permv2ti (t2, d->op0, d->op1, GEN_INT (0x31)));
36962
36963       /* Now an vpunpck[lh]qdq will produce the result required.  */
36964       if (odd)
36965         t3 = gen_avx2_interleave_highv4di (d->target, t1, t2);
36966       else
36967         t3 = gen_avx2_interleave_lowv4di (d->target, t1, t2);
36968       emit_insn (t3);
36969       break;
36970
36971     case V8SImode:
36972       if (!TARGET_AVX2)
36973         {
36974           struct expand_vec_perm_d d_copy = *d;
36975           d_copy.vmode = V8SFmode;
36976           d_copy.target = gen_lowpart (V8SFmode, d->target);
36977           d_copy.op0 = gen_lowpart (V8SFmode, d->op0);
36978           d_copy.op1 = gen_lowpart (V8SFmode, d->op1);
36979           return expand_vec_perm_even_odd_1 (&d_copy, odd);
36980         }
36981
36982       t1 = gen_reg_rtx (V8SImode);
36983       t2 = gen_reg_rtx (V8SImode);
36984
36985       /* Shuffle the lanes around into
36986          { 0 1 2 3 8 9 a b } and { 4 5 6 7 c d e f }.  */
36987       emit_insn (gen_avx2_permv2ti (gen_lowpart (V4DImode, t1),
36988                                     gen_lowpart (V4DImode, d->op0),
36989                                     gen_lowpart (V4DImode, d->op1),
36990                                     GEN_INT (0x20)));
36991       emit_insn (gen_avx2_permv2ti (gen_lowpart (V4DImode, t2),
36992                                     gen_lowpart (V4DImode, d->op0),
36993                                     gen_lowpart (V4DImode, d->op1),
36994                                     GEN_INT (0x31)));
36995
36996       /* Swap the 2nd and 3rd position in each lane into
36997          { 0 2 1 3 8 a 9 b } and { 4 6 5 7 c e d f }.  */
36998       emit_insn (gen_avx2_pshufdv3 (t1, t1,
36999                                     GEN_INT (2 * 4 + 1 * 16 + 3 * 64)));
37000       emit_insn (gen_avx2_pshufdv3 (t2, t2,
37001                                     GEN_INT (2 * 4 + 1 * 16 + 3 * 64)));
37002
37003       /* Now an vpunpck[lh]qdq will produce
37004          { 0 2 4 6 8 a c e } resp. { 1 3 5 7 9 b d f }.  */
37005       if (odd)
37006         t3 = gen_avx2_interleave_highv4di (gen_lowpart (V4DImode, d->target),
37007                                            gen_lowpart (V4DImode, t1),
37008                                            gen_lowpart (V4DImode, t2));
37009       else
37010         t3 = gen_avx2_interleave_lowv4di (gen_lowpart (V4DImode, d->target),
37011                                           gen_lowpart (V4DImode, t1),
37012                                           gen_lowpart (V4DImode, t2));
37013       emit_insn (t3);
37014       break;
37015
37016     default:
37017       gcc_unreachable ();
37018     }
37019
37020   return true;
37021 }
37022
37023 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
37024    extract-even and extract-odd permutations.  */
37025
37026 static bool
37027 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
37028 {
37029   unsigned i, odd, nelt = d->nelt;
37030
37031   odd = d->perm[0];
37032   if (odd != 0 && odd != 1)
37033     return false;
37034
37035   for (i = 1; i < nelt; ++i)
37036     if (d->perm[i] != 2 * i + odd)
37037       return false;
37038
37039   return expand_vec_perm_even_odd_1 (d, odd);
37040 }
37041
37042 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
37043    permutations.  We assume that expand_vec_perm_1 has already failed.  */
37044
37045 static bool
37046 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
37047 {
37048   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
37049   enum machine_mode vmode = d->vmode;
37050   unsigned char perm2[4];
37051   rtx op0 = d->op0;
37052   bool ok;
37053
37054   switch (vmode)
37055     {
37056     case V4DFmode:
37057     case V8SFmode:
37058       /* These are special-cased in sse.md so that we can optionally
37059          use the vbroadcast instruction.  They expand to two insns
37060          if the input happens to be in a register.  */
37061       gcc_unreachable ();
37062
37063     case V2DFmode:
37064     case V2DImode:
37065     case V4SFmode:
37066     case V4SImode:
37067       /* These are always implementable using standard shuffle patterns.  */
37068       gcc_unreachable ();
37069
37070     case V8HImode:
37071     case V16QImode:
37072       /* These can be implemented via interleave.  We save one insn by
37073          stopping once we have promoted to V4SImode and then use pshufd.  */
37074       do
37075         {
37076           rtx dest;
37077           rtx (*gen) (rtx, rtx, rtx)
37078             = vmode == V16QImode ? gen_vec_interleave_lowv16qi
37079                                  : gen_vec_interleave_lowv8hi;
37080
37081           if (elt >= nelt2)
37082             {
37083               gen = vmode == V16QImode ? gen_vec_interleave_highv16qi
37084                                        : gen_vec_interleave_highv8hi;
37085               elt -= nelt2;
37086             }
37087           nelt2 /= 2;
37088
37089           dest = gen_reg_rtx (vmode);
37090           emit_insn (gen (dest, op0, op0));
37091           vmode = get_mode_wider_vector (vmode);
37092           op0 = gen_lowpart (vmode, dest);
37093         }
37094       while (vmode != V4SImode);
37095
37096       memset (perm2, elt, 4);
37097       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
37098       gcc_assert (ok);
37099       return true;
37100
37101     case V32QImode:
37102     case V16HImode:
37103     case V8SImode:
37104     case V4DImode:
37105       /* For AVX2 broadcasts of the first element vpbroadcast* or
37106          vpermq should be used by expand_vec_perm_1.  */
37107       gcc_assert (!TARGET_AVX2 || d->perm[0]);
37108       return false;
37109
37110     default:
37111       gcc_unreachable ();
37112     }
37113 }
37114
37115 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
37116    broadcast permutations.  */
37117
37118 static bool
37119 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
37120 {
37121   unsigned i, elt, nelt = d->nelt;
37122
37123   if (d->op0 != d->op1)
37124     return false;
37125
37126   elt = d->perm[0];
37127   for (i = 1; i < nelt; ++i)
37128     if (d->perm[i] != elt)
37129       return false;
37130
37131   return expand_vec_perm_broadcast_1 (d);
37132 }
37133
37134 /* Implement arbitrary permutation of two V32QImode and V16QImode operands
37135    with 4 vpshufb insns, 2 vpermq and 3 vpor.  We should have already failed
37136    all the shorter instruction sequences.  */
37137
37138 static bool
37139 expand_vec_perm_vpshufb4_vpermq2 (struct expand_vec_perm_d *d)
37140 {
37141   rtx rperm[4][32], vperm, l[2], h[2], op, m128;
37142   unsigned int i, nelt, eltsz;
37143   bool used[4];
37144
37145   if (!TARGET_AVX2
37146       || d->op0 == d->op1
37147       || (d->vmode != V32QImode && d->vmode != V16HImode))
37148     return false;
37149
37150   if (d->testing_p)
37151     return true;
37152
37153   nelt = d->nelt;
37154   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
37155
37156   /* Generate 4 permutation masks.  If the required element is within
37157      the same lane, it is shuffled in.  If the required element from the
37158      other lane, force a zero by setting bit 7 in the permutation mask.
37159      In the other mask the mask has non-negative elements if element
37160      is requested from the other lane, but also moved to the other lane,
37161      so that the result of vpshufb can have the two V2TImode halves
37162      swapped.  */
37163   m128 = GEN_INT (-128);
37164   for (i = 0; i < 32; ++i)
37165     {
37166       rperm[0][i] = m128;
37167       rperm[1][i] = m128;
37168       rperm[2][i] = m128;
37169       rperm[3][i] = m128;
37170     }
37171   used[0] = false;
37172   used[1] = false;
37173   used[2] = false;
37174   used[3] = false;
37175   for (i = 0; i < nelt; ++i)
37176     {
37177       unsigned j, e = d->perm[i] & (nelt / 2 - 1);
37178       unsigned xlane = ((d->perm[i] ^ i) & (nelt / 2)) * eltsz;
37179       unsigned int which = ((d->perm[i] & nelt) ? 2 : 0) + (xlane ? 1 : 0);
37180
37181       for (j = 0; j < eltsz; ++j)
37182         rperm[which][(i * eltsz + j) ^ xlane] = GEN_INT (e * eltsz + j);
37183       used[which] = true;
37184     }
37185
37186   for (i = 0; i < 2; ++i)
37187     {
37188       if (!used[2 * i + 1])
37189         {
37190           h[i] = NULL_RTX;
37191           continue;
37192         }
37193       vperm = gen_rtx_CONST_VECTOR (V32QImode,
37194                                     gen_rtvec_v (32, rperm[2 * i + 1]));
37195       vperm = force_reg (V32QImode, vperm);
37196       h[i] = gen_reg_rtx (V32QImode);
37197       op = gen_lowpart (V32QImode, i ? d->op1 : d->op0);
37198       emit_insn (gen_avx2_pshufbv32qi3 (h[i], op, vperm));
37199     }
37200
37201   /* Swap the 128-byte lanes of h[X].  */
37202   for (i = 0; i < 2; ++i)
37203    {
37204      if (h[i] == NULL_RTX)
37205        continue;
37206      op = gen_reg_rtx (V4DImode);
37207      emit_insn (gen_avx2_permv4di_1 (op, gen_lowpart (V4DImode, h[i]),
37208                                      const2_rtx, GEN_INT (3), const0_rtx,
37209                                      const1_rtx));
37210      h[i] = gen_lowpart (V32QImode, op);
37211    }
37212
37213   for (i = 0; i < 2; ++i)
37214     {
37215       if (!used[2 * i])
37216         {
37217           l[i] = NULL_RTX;
37218           continue;
37219         }
37220       vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[2 * i]));
37221       vperm = force_reg (V32QImode, vperm);
37222       l[i] = gen_reg_rtx (V32QImode);
37223       op = gen_lowpart (V32QImode, i ? d->op1 : d->op0);
37224       emit_insn (gen_avx2_pshufbv32qi3 (l[i], op, vperm));
37225     }
37226
37227   for (i = 0; i < 2; ++i)
37228     {
37229       if (h[i] && l[i])
37230         {
37231           op = gen_reg_rtx (V32QImode);
37232           emit_insn (gen_iorv32qi3 (op, l[i], h[i]));
37233           l[i] = op;
37234         }
37235       else if (h[i])
37236         l[i] = h[i];
37237     }
37238
37239   gcc_assert (l[0] && l[1]);
37240   op = gen_lowpart (V32QImode, d->target);
37241   emit_insn (gen_iorv32qi3 (op, l[0], l[1]));
37242   return true;
37243 }
37244
37245 /* The guts of ix86_expand_vec_perm_const, also used by the ok hook.
37246    With all of the interface bits taken care of, perform the expansion
37247    in D and return true on success.  */
37248
37249 static bool
37250 ix86_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
37251 {
37252   /* Try a single instruction expansion.  */
37253   if (expand_vec_perm_1 (d))
37254     return true;
37255
37256   /* Try sequences of two instructions.  */
37257
37258   if (expand_vec_perm_pshuflw_pshufhw (d))
37259     return true;
37260
37261   if (expand_vec_perm_palignr (d))
37262     return true;
37263
37264   if (expand_vec_perm_interleave2 (d))
37265     return true;
37266
37267   if (expand_vec_perm_broadcast (d))
37268     return true;
37269
37270   if (expand_vec_perm_vpermq_perm_1 (d))
37271     return true;
37272
37273   /* Try sequences of three instructions.  */
37274
37275   if (expand_vec_perm_pshufb2 (d))
37276     return true;
37277
37278   if (expand_vec_perm_interleave3 (d))
37279     return true;
37280
37281   /* Try sequences of four instructions.  */
37282
37283   if (expand_vec_perm_vpshufb2_vpermq (d))
37284     return true;
37285
37286   if (expand_vec_perm_vpshufb2_vpermq_even_odd (d))
37287     return true;
37288
37289   /* ??? Look for narrow permutations whose element orderings would
37290      allow the promotion to a wider mode.  */
37291
37292   /* ??? Look for sequences of interleave or a wider permute that place
37293      the data into the correct lanes for a half-vector shuffle like
37294      pshuf[lh]w or vpermilps.  */
37295
37296   /* ??? Look for sequences of interleave that produce the desired results.
37297      The combinatorics of punpck[lh] get pretty ugly... */
37298
37299   if (expand_vec_perm_even_odd (d))
37300     return true;
37301
37302   /* Even longer sequences.  */
37303   if (expand_vec_perm_vpshufb4_vpermq2 (d))
37304     return true;
37305
37306   return false;
37307 }
37308
37309 bool
37310 ix86_expand_vec_perm_const (rtx operands[4])
37311 {
37312   struct expand_vec_perm_d d;
37313   unsigned char perm[MAX_VECT_LEN];
37314   int i, nelt, which;
37315   rtx sel;
37316
37317   d.target = operands[0];
37318   d.op0 = operands[1];
37319   d.op1 = operands[2];
37320   sel = operands[3];
37321
37322   d.vmode = GET_MODE (d.target);
37323   gcc_assert (VECTOR_MODE_P (d.vmode));
37324   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
37325   d.testing_p = false;
37326
37327   gcc_assert (GET_CODE (sel) == CONST_VECTOR);
37328   gcc_assert (XVECLEN (sel, 0) == nelt);
37329   gcc_checking_assert (sizeof (d.perm) == sizeof (perm));
37330
37331   for (i = which = 0; i < nelt; ++i)
37332     {
37333       rtx e = XVECEXP (sel, 0, i);
37334       int ei = INTVAL (e) & (2 * nelt - 1);
37335
37336       which |= (ei < nelt ? 1 : 2);
37337       d.perm[i] = ei;
37338       perm[i] = ei;
37339     }
37340
37341   switch (which)
37342     {
37343     default:
37344       gcc_unreachable();
37345
37346     case 3:
37347       if (!rtx_equal_p (d.op0, d.op1))
37348         break;
37349
37350       /* The elements of PERM do not suggest that only the first operand
37351          is used, but both operands are identical.  Allow easier matching
37352          of the permutation by folding the permutation into the single
37353          input vector.  */
37354       for (i = 0; i < nelt; ++i)
37355         if (d.perm[i] >= nelt)
37356           d.perm[i] -= nelt;
37357       /* FALLTHRU */
37358
37359     case 1:
37360       d.op1 = d.op0;
37361       break;
37362
37363     case 2:
37364       for (i = 0; i < nelt; ++i)
37365         d.perm[i] -= nelt;
37366       d.op0 = d.op1;
37367       break;
37368     }
37369
37370   if (ix86_expand_vec_perm_const_1 (&d))
37371     return true;
37372
37373   /* If the mask says both arguments are needed, but they are the same,
37374      the above tried to expand with d.op0 == d.op1.  If that didn't work,
37375      retry with d.op0 != d.op1 as that is what testing has been done with.  */
37376   if (which == 3 && d.op0 == d.op1)
37377     {
37378       rtx seq;
37379       bool ok;
37380
37381       memcpy (d.perm, perm, sizeof (perm));
37382       d.op1 = gen_reg_rtx (d.vmode);
37383       start_sequence ();
37384       ok = ix86_expand_vec_perm_const_1 (&d);
37385       seq = get_insns ();
37386       end_sequence ();
37387       if (ok)
37388         {
37389           emit_move_insn (d.op1, d.op0);
37390           emit_insn (seq);
37391           return true;
37392         }
37393     }
37394
37395   return false;
37396 }
37397
37398 /* Implement targetm.vectorize.vec_perm_const_ok.  */
37399
37400 static bool
37401 ix86_vectorize_vec_perm_const_ok (enum machine_mode vmode,
37402                                   const unsigned char *sel)
37403 {
37404   struct expand_vec_perm_d d;
37405   unsigned int i, nelt, which;
37406   bool ret, one_vec;
37407
37408   d.vmode = vmode;
37409   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
37410   d.testing_p = true;
37411
37412   /* Given sufficient ISA support we can just return true here
37413      for selected vector modes.  */
37414   if (GET_MODE_SIZE (d.vmode) == 16)
37415     {
37416       /* All implementable with a single vpperm insn.  */
37417       if (TARGET_XOP)
37418         return true;
37419       /* All implementable with 2 pshufb + 1 ior.  */
37420       if (TARGET_SSSE3)
37421         return true;
37422       /* All implementable with shufpd or unpck[lh]pd.  */
37423       if (d.nelt == 2)
37424         return true;
37425     }
37426
37427   /* Extract the values from the vector CST into the permutation
37428      array in D.  */
37429   memcpy (d.perm, sel, nelt);
37430   for (i = which = 0; i < nelt; ++i)
37431     {
37432       unsigned char e = d.perm[i];
37433       gcc_assert (e < 2 * nelt);
37434       which |= (e < nelt ? 1 : 2);
37435     }
37436
37437   /* For all elements from second vector, fold the elements to first.  */
37438   if (which == 2)
37439     for (i = 0; i < nelt; ++i)
37440       d.perm[i] -= nelt;
37441
37442   /* Check whether the mask can be applied to the vector type.  */
37443   one_vec = (which != 3);
37444
37445   /* Implementable with shufps or pshufd.  */
37446   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
37447     return true;
37448
37449   /* Otherwise we have to go through the motions and see if we can
37450      figure out how to generate the requested permutation.  */
37451   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
37452   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
37453   if (!one_vec)
37454     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
37455
37456   start_sequence ();
37457   ret = ix86_expand_vec_perm_const_1 (&d);
37458   end_sequence ();
37459
37460   return ret;
37461 }
37462
37463 void
37464 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
37465 {
37466   struct expand_vec_perm_d d;
37467   unsigned i, nelt;
37468
37469   d.target = targ;
37470   d.op0 = op0;
37471   d.op1 = op1;
37472   d.vmode = GET_MODE (targ);
37473   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
37474   d.testing_p = false;
37475
37476   for (i = 0; i < nelt; ++i)
37477     d.perm[i] = i * 2 + odd;
37478
37479   /* We'll either be able to implement the permutation directly...  */
37480   if (expand_vec_perm_1 (&d))
37481     return;
37482
37483   /* ... or we use the special-case patterns.  */
37484   expand_vec_perm_even_odd_1 (&d, odd);
37485 }
37486
37487 /* Expand an insert into a vector register through pinsr insn.
37488    Return true if successful.  */
37489
37490 bool
37491 ix86_expand_pinsr (rtx *operands)
37492 {
37493   rtx dst = operands[0];
37494   rtx src = operands[3];
37495
37496   unsigned int size = INTVAL (operands[1]);
37497   unsigned int pos = INTVAL (operands[2]);
37498
37499   if (GET_CODE (dst) == SUBREG)
37500     {
37501       pos += SUBREG_BYTE (dst) * BITS_PER_UNIT;
37502       dst = SUBREG_REG (dst);
37503     }
37504
37505   if (GET_CODE (src) == SUBREG)
37506     src = SUBREG_REG (src);
37507
37508   switch (GET_MODE (dst))
37509     {
37510     case V16QImode:
37511     case V8HImode:
37512     case V4SImode:
37513     case V2DImode:
37514       {
37515         enum machine_mode srcmode, dstmode;
37516         rtx (*pinsr)(rtx, rtx, rtx, rtx);
37517
37518         srcmode = mode_for_size (size, MODE_INT, 0);
37519
37520         switch (srcmode)
37521           {
37522           case QImode:
37523             if (!TARGET_SSE4_1)
37524               return false;
37525             dstmode = V16QImode;
37526             pinsr = gen_sse4_1_pinsrb;
37527             break;
37528
37529           case HImode:
37530             if (!TARGET_SSE2)
37531               return false;
37532             dstmode = V8HImode;
37533             pinsr = gen_sse2_pinsrw;
37534             break;
37535
37536           case SImode:
37537             if (!TARGET_SSE4_1)
37538               return false;
37539             dstmode = V4SImode;
37540             pinsr = gen_sse4_1_pinsrd;
37541             break;
37542
37543           case DImode:
37544             gcc_assert (TARGET_64BIT);
37545             if (!TARGET_SSE4_1)
37546               return false;
37547             dstmode = V2DImode;
37548             pinsr = gen_sse4_1_pinsrq;
37549             break;
37550
37551           default:
37552             return false;
37553           }
37554
37555         dst = gen_lowpart (dstmode, dst);
37556         src = gen_lowpart (srcmode, src);
37557
37558         pos /= size;
37559
37560         emit_insn (pinsr (dst, dst, src, GEN_INT (1 << pos)));
37561         return true;
37562       }
37563
37564     default:
37565       return false;
37566     }
37567 }
37568 \f
37569 /* This function returns the calling abi specific va_list type node.
37570    It returns  the FNDECL specific va_list type.  */
37571
37572 static tree
37573 ix86_fn_abi_va_list (tree fndecl)
37574 {
37575   if (!TARGET_64BIT)
37576     return va_list_type_node;
37577   gcc_assert (fndecl != NULL_TREE);
37578
37579   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
37580     return ms_va_list_type_node;
37581   else
37582     return sysv_va_list_type_node;
37583 }
37584
37585 /* Returns the canonical va_list type specified by TYPE. If there
37586    is no valid TYPE provided, it return NULL_TREE.  */
37587
37588 static tree
37589 ix86_canonical_va_list_type (tree type)
37590 {
37591   tree wtype, htype;
37592
37593   /* Resolve references and pointers to va_list type.  */
37594   if (TREE_CODE (type) == MEM_REF)
37595     type = TREE_TYPE (type);
37596   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
37597     type = TREE_TYPE (type);
37598   else if (POINTER_TYPE_P (type) && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
37599     type = TREE_TYPE (type);
37600
37601   if (TARGET_64BIT && va_list_type_node != NULL_TREE)
37602     {
37603       wtype = va_list_type_node;
37604           gcc_assert (wtype != NULL_TREE);
37605       htype = type;
37606       if (TREE_CODE (wtype) == ARRAY_TYPE)
37607         {
37608           /* If va_list is an array type, the argument may have decayed
37609              to a pointer type, e.g. by being passed to another function.
37610              In that case, unwrap both types so that we can compare the
37611              underlying records.  */
37612           if (TREE_CODE (htype) == ARRAY_TYPE
37613               || POINTER_TYPE_P (htype))
37614             {
37615               wtype = TREE_TYPE (wtype);
37616               htype = TREE_TYPE (htype);
37617             }
37618         }
37619       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
37620         return va_list_type_node;
37621       wtype = sysv_va_list_type_node;
37622           gcc_assert (wtype != NULL_TREE);
37623       htype = type;
37624       if (TREE_CODE (wtype) == ARRAY_TYPE)
37625         {
37626           /* If va_list is an array type, the argument may have decayed
37627              to a pointer type, e.g. by being passed to another function.
37628              In that case, unwrap both types so that we can compare the
37629              underlying records.  */
37630           if (TREE_CODE (htype) == ARRAY_TYPE
37631               || POINTER_TYPE_P (htype))
37632             {
37633               wtype = TREE_TYPE (wtype);
37634               htype = TREE_TYPE (htype);
37635             }
37636         }
37637       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
37638         return sysv_va_list_type_node;
37639       wtype = ms_va_list_type_node;
37640           gcc_assert (wtype != NULL_TREE);
37641       htype = type;
37642       if (TREE_CODE (wtype) == ARRAY_TYPE)
37643         {
37644           /* If va_list is an array type, the argument may have decayed
37645              to a pointer type, e.g. by being passed to another function.
37646              In that case, unwrap both types so that we can compare the
37647              underlying records.  */
37648           if (TREE_CODE (htype) == ARRAY_TYPE
37649               || POINTER_TYPE_P (htype))
37650             {
37651               wtype = TREE_TYPE (wtype);
37652               htype = TREE_TYPE (htype);
37653             }
37654         }
37655       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
37656         return ms_va_list_type_node;
37657       return NULL_TREE;
37658     }
37659   return std_canonical_va_list_type (type);
37660 }
37661
37662 /* Iterate through the target-specific builtin types for va_list.
37663    IDX denotes the iterator, *PTREE is set to the result type of
37664    the va_list builtin, and *PNAME to its internal type.
37665    Returns zero if there is no element for this index, otherwise
37666    IDX should be increased upon the next call.
37667    Note, do not iterate a base builtin's name like __builtin_va_list.
37668    Used from c_common_nodes_and_builtins.  */
37669
37670 static int
37671 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
37672 {
37673   if (TARGET_64BIT)
37674     {
37675       switch (idx)
37676         {
37677         default:
37678           break;
37679
37680         case 0:
37681           *ptree = ms_va_list_type_node;
37682           *pname = "__builtin_ms_va_list";
37683           return 1;
37684
37685         case 1:
37686           *ptree = sysv_va_list_type_node;
37687           *pname = "__builtin_sysv_va_list";
37688           return 1;
37689         }
37690     }
37691
37692   return 0;
37693 }
37694
37695 #undef TARGET_SCHED_DISPATCH
37696 #define TARGET_SCHED_DISPATCH has_dispatch
37697 #undef TARGET_SCHED_DISPATCH_DO
37698 #define TARGET_SCHED_DISPATCH_DO do_dispatch
37699 #undef TARGET_SCHED_REASSOCIATION_WIDTH
37700 #define TARGET_SCHED_REASSOCIATION_WIDTH ix86_reassociation_width
37701
37702 /* The size of the dispatch window is the total number of bytes of
37703    object code allowed in a window.  */
37704 #define DISPATCH_WINDOW_SIZE 16
37705
37706 /* Number of dispatch windows considered for scheduling.  */
37707 #define MAX_DISPATCH_WINDOWS 3
37708
37709 /* Maximum number of instructions in a window.  */
37710 #define MAX_INSN 4
37711
37712 /* Maximum number of immediate operands in a window.  */
37713 #define MAX_IMM 4
37714
37715 /* Maximum number of immediate bits allowed in a window.  */
37716 #define MAX_IMM_SIZE 128
37717
37718 /* Maximum number of 32 bit immediates allowed in a window.  */
37719 #define MAX_IMM_32 4
37720
37721 /* Maximum number of 64 bit immediates allowed in a window.  */
37722 #define MAX_IMM_64 2
37723
37724 /* Maximum total of loads or prefetches allowed in a window.  */
37725 #define MAX_LOAD 2
37726
37727 /* Maximum total of stores allowed in a window.  */
37728 #define MAX_STORE 1
37729
37730 #undef BIG
37731 #define BIG 100
37732
37733
37734 /* Dispatch groups.  Istructions that affect the mix in a dispatch window.  */
37735 enum dispatch_group {
37736   disp_no_group = 0,
37737   disp_load,
37738   disp_store,
37739   disp_load_store,
37740   disp_prefetch,
37741   disp_imm,
37742   disp_imm_32,
37743   disp_imm_64,
37744   disp_branch,
37745   disp_cmp,
37746   disp_jcc,
37747   disp_last
37748 };
37749
37750 /* Number of allowable groups in a dispatch window.  It is an array
37751    indexed by dispatch_group enum.  100 is used as a big number,
37752    because the number of these kind of operations does not have any
37753    effect in dispatch window, but we need them for other reasons in
37754    the table.  */
37755 static unsigned int num_allowable_groups[disp_last] = {
37756   0, 2, 1, 1, 2, 4, 4, 2, 1, BIG, BIG
37757 };
37758
37759 char group_name[disp_last + 1][16] = {
37760   "disp_no_group", "disp_load", "disp_store", "disp_load_store",
37761   "disp_prefetch", "disp_imm", "disp_imm_32", "disp_imm_64",
37762   "disp_branch", "disp_cmp", "disp_jcc", "disp_last"
37763 };
37764
37765 /* Instruction path.  */
37766 enum insn_path {
37767   no_path = 0,
37768   path_single, /* Single micro op.  */
37769   path_double, /* Double micro op.  */
37770   path_multi,  /* Instructions with more than 2 micro op..  */
37771   last_path
37772 };
37773
37774 /* sched_insn_info defines a window to the instructions scheduled in
37775    the basic block.  It contains a pointer to the insn_info table and
37776    the instruction scheduled.
37777
37778    Windows are allocated for each basic block and are linked
37779    together.  */
37780 typedef struct sched_insn_info_s {
37781   rtx insn;
37782   enum dispatch_group group;
37783   enum insn_path path;
37784   int byte_len;
37785   int imm_bytes;
37786 } sched_insn_info;
37787
37788 /* Linked list of dispatch windows.  This is a two way list of
37789    dispatch windows of a basic block.  It contains information about
37790    the number of uops in the window and the total number of
37791    instructions and of bytes in the object code for this dispatch
37792    window.  */
37793 typedef struct dispatch_windows_s {
37794   int num_insn;            /* Number of insn in the window.  */
37795   int num_uops;            /* Number of uops in the window.  */
37796   int window_size;         /* Number of bytes in the window.  */
37797   int window_num;          /* Window number between 0 or 1.  */
37798   int num_imm;             /* Number of immediates in an insn.  */
37799   int num_imm_32;          /* Number of 32 bit immediates in an insn.  */
37800   int num_imm_64;          /* Number of 64 bit immediates in an insn.  */
37801   int imm_size;            /* Total immediates in the window.  */
37802   int num_loads;           /* Total memory loads in the window.  */
37803   int num_stores;          /* Total memory stores in the window.  */
37804   int violation;          /* Violation exists in window.  */
37805   sched_insn_info *window; /* Pointer to the window.  */
37806   struct dispatch_windows_s *next;
37807   struct dispatch_windows_s *prev;
37808 } dispatch_windows;
37809
37810 /* Immediate valuse used in an insn.  */
37811 typedef struct imm_info_s
37812   {
37813     int imm;
37814     int imm32;
37815     int imm64;
37816   } imm_info;
37817
37818 static dispatch_windows *dispatch_window_list;
37819 static dispatch_windows *dispatch_window_list1;
37820
37821 /* Get dispatch group of insn.  */
37822
37823 static enum dispatch_group
37824 get_mem_group (rtx insn)
37825 {
37826   enum attr_memory memory;
37827
37828   if (INSN_CODE (insn) < 0)
37829     return disp_no_group;
37830   memory = get_attr_memory (insn);
37831   if (memory == MEMORY_STORE)
37832     return disp_store;
37833
37834   if (memory == MEMORY_LOAD)
37835     return disp_load;
37836
37837   if (memory == MEMORY_BOTH)
37838     return disp_load_store;
37839
37840   return disp_no_group;
37841 }
37842
37843 /* Return true if insn is a compare instruction.  */
37844
37845 static bool
37846 is_cmp (rtx insn)
37847 {
37848   enum attr_type type;
37849
37850   type = get_attr_type (insn);
37851   return (type == TYPE_TEST
37852           || type == TYPE_ICMP
37853           || type == TYPE_FCMP
37854           || GET_CODE (PATTERN (insn)) == COMPARE);
37855 }
37856
37857 /* Return true if a dispatch violation encountered.  */
37858
37859 static bool
37860 dispatch_violation (void)
37861 {
37862   if (dispatch_window_list->next)
37863     return dispatch_window_list->next->violation;
37864   return dispatch_window_list->violation;
37865 }
37866
37867 /* Return true if insn is a branch instruction.  */
37868
37869 static bool
37870 is_branch (rtx insn)
37871 {
37872   return (CALL_P (insn) || JUMP_P (insn));
37873 }
37874
37875 /* Return true if insn is a prefetch instruction.  */
37876
37877 static bool
37878 is_prefetch (rtx insn)
37879 {
37880   return NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == PREFETCH;
37881 }
37882
37883 /* This function initializes a dispatch window and the list container holding a
37884    pointer to the window.  */
37885
37886 static void
37887 init_window (int window_num)
37888 {
37889   int i;
37890   dispatch_windows *new_list;
37891
37892   if (window_num == 0)
37893     new_list = dispatch_window_list;
37894   else
37895     new_list = dispatch_window_list1;
37896
37897   new_list->num_insn = 0;
37898   new_list->num_uops = 0;
37899   new_list->window_size = 0;
37900   new_list->next = NULL;
37901   new_list->prev = NULL;
37902   new_list->window_num = window_num;
37903   new_list->num_imm = 0;
37904   new_list->num_imm_32 = 0;
37905   new_list->num_imm_64 = 0;
37906   new_list->imm_size = 0;
37907   new_list->num_loads = 0;
37908   new_list->num_stores = 0;
37909   new_list->violation = false;
37910
37911   for (i = 0; i < MAX_INSN; i++)
37912     {
37913       new_list->window[i].insn = NULL;
37914       new_list->window[i].group = disp_no_group;
37915       new_list->window[i].path = no_path;
37916       new_list->window[i].byte_len = 0;
37917       new_list->window[i].imm_bytes = 0;
37918     }
37919   return;
37920 }
37921
37922 /* This function allocates and initializes a dispatch window and the
37923    list container holding a pointer to the window.  */
37924
37925 static dispatch_windows *
37926 allocate_window (void)
37927 {
37928   dispatch_windows *new_list = XNEW (struct dispatch_windows_s);
37929   new_list->window = XNEWVEC (struct sched_insn_info_s, MAX_INSN + 1);
37930
37931   return new_list;
37932 }
37933
37934 /* This routine initializes the dispatch scheduling information.  It
37935    initiates building dispatch scheduler tables and constructs the
37936    first dispatch window.  */
37937
37938 static void
37939 init_dispatch_sched (void)
37940 {
37941   /* Allocate a dispatch list and a window.  */
37942   dispatch_window_list = allocate_window ();
37943   dispatch_window_list1 = allocate_window ();
37944   init_window (0);
37945   init_window (1);
37946 }
37947
37948 /* This function returns true if a branch is detected.  End of a basic block
37949    does not have to be a branch, but here we assume only branches end a
37950    window.  */
37951
37952 static bool
37953 is_end_basic_block (enum dispatch_group group)
37954 {
37955   return group == disp_branch;
37956 }
37957
37958 /* This function is called when the end of a window processing is reached.  */
37959
37960 static void
37961 process_end_window (void)
37962 {
37963   gcc_assert (dispatch_window_list->num_insn <= MAX_INSN);
37964   if (dispatch_window_list->next)
37965     {
37966       gcc_assert (dispatch_window_list1->num_insn <= MAX_INSN);
37967       gcc_assert (dispatch_window_list->window_size
37968                   + dispatch_window_list1->window_size <= 48);
37969       init_window (1);
37970     }
37971   init_window (0);
37972 }
37973
37974 /* Allocates a new dispatch window and adds it to WINDOW_LIST.
37975    WINDOW_NUM is either 0 or 1.  A maximum of two windows are generated
37976    for 48 bytes of instructions.  Note that these windows are not dispatch
37977    windows that their sizes are DISPATCH_WINDOW_SIZE.  */
37978
37979 static dispatch_windows *
37980 allocate_next_window (int window_num)
37981 {
37982   if (window_num == 0)
37983     {
37984       if (dispatch_window_list->next)
37985           init_window (1);
37986       init_window (0);
37987       return dispatch_window_list;
37988     }
37989
37990   dispatch_window_list->next = dispatch_window_list1;
37991   dispatch_window_list1->prev = dispatch_window_list;
37992
37993   return dispatch_window_list1;
37994 }
37995
37996 /* Increment the number of immediate operands of an instruction.  */
37997
37998 static int
37999 find_constant_1 (rtx *in_rtx, imm_info *imm_values)
38000 {
38001   if (*in_rtx == 0)
38002     return 0;
38003
38004     switch ( GET_CODE (*in_rtx))
38005     {
38006     case CONST:
38007     case SYMBOL_REF:
38008     case CONST_INT:
38009       (imm_values->imm)++;
38010       if (x86_64_immediate_operand (*in_rtx, SImode))
38011         (imm_values->imm32)++;
38012       else
38013         (imm_values->imm64)++;
38014       break;
38015
38016     case CONST_DOUBLE:
38017       (imm_values->imm)++;
38018       (imm_values->imm64)++;
38019       break;
38020
38021     case CODE_LABEL:
38022       if (LABEL_KIND (*in_rtx) == LABEL_NORMAL)
38023         {
38024           (imm_values->imm)++;
38025           (imm_values->imm32)++;
38026         }
38027       break;
38028
38029     default:
38030       break;
38031     }
38032
38033   return 0;
38034 }
38035
38036 /* Compute number of immediate operands of an instruction.  */
38037
38038 static void
38039 find_constant (rtx in_rtx, imm_info *imm_values)
38040 {
38041   for_each_rtx (INSN_P (in_rtx) ? &PATTERN (in_rtx) : &in_rtx,
38042                 (rtx_function) find_constant_1, (void *) imm_values);
38043 }
38044
38045 /* Return total size of immediate operands of an instruction along with number
38046    of corresponding immediate-operands.  It initializes its parameters to zero
38047    befor calling FIND_CONSTANT.
38048    INSN is the input instruction.  IMM is the total of immediates.
38049    IMM32 is the number of 32 bit immediates.  IMM64 is the number of 64
38050    bit immediates.  */
38051
38052 static int
38053 get_num_immediates (rtx insn, int *imm, int *imm32, int *imm64)
38054 {
38055   imm_info imm_values = {0, 0, 0};
38056
38057   find_constant (insn, &imm_values);
38058   *imm = imm_values.imm;
38059   *imm32 = imm_values.imm32;
38060   *imm64 = imm_values.imm64;
38061   return imm_values.imm32 * 4 + imm_values.imm64 * 8;
38062 }
38063
38064 /* This function indicates if an operand of an instruction is an
38065    immediate.  */
38066
38067 static bool
38068 has_immediate (rtx insn)
38069 {
38070   int num_imm_operand;
38071   int num_imm32_operand;
38072   int num_imm64_operand;
38073
38074   if (insn)
38075     return get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38076                                &num_imm64_operand);
38077   return false;
38078 }
38079
38080 /* Return single or double path for instructions.  */
38081
38082 static enum insn_path
38083 get_insn_path (rtx insn)
38084 {
38085   enum attr_amdfam10_decode path = get_attr_amdfam10_decode (insn);
38086
38087   if ((int)path == 0)
38088     return path_single;
38089
38090   if ((int)path == 1)
38091     return path_double;
38092
38093   return path_multi;
38094 }
38095
38096 /* Return insn dispatch group.  */
38097
38098 static enum dispatch_group
38099 get_insn_group (rtx insn)
38100 {
38101   enum dispatch_group group = get_mem_group (insn);
38102   if (group)
38103     return group;
38104
38105   if (is_branch (insn))
38106     return disp_branch;
38107
38108   if (is_cmp (insn))
38109     return disp_cmp;
38110
38111   if (has_immediate (insn))
38112     return disp_imm;
38113
38114   if (is_prefetch (insn))
38115     return disp_prefetch;
38116
38117   return disp_no_group;
38118 }
38119
38120 /* Count number of GROUP restricted instructions in a dispatch
38121    window WINDOW_LIST.  */
38122
38123 static int
38124 count_num_restricted (rtx insn, dispatch_windows *window_list)
38125 {
38126   enum dispatch_group group = get_insn_group (insn);
38127   int imm_size;
38128   int num_imm_operand;
38129   int num_imm32_operand;
38130   int num_imm64_operand;
38131
38132   if (group == disp_no_group)
38133     return 0;
38134
38135   if (group == disp_imm)
38136     {
38137       imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38138                               &num_imm64_operand);
38139       if (window_list->imm_size + imm_size > MAX_IMM_SIZE
38140           || num_imm_operand + window_list->num_imm > MAX_IMM
38141           || (num_imm32_operand > 0
38142               && (window_list->num_imm_32 + num_imm32_operand > MAX_IMM_32
38143                   || window_list->num_imm_64 * 2 + num_imm32_operand > MAX_IMM_32))
38144           || (num_imm64_operand > 0
38145               && (window_list->num_imm_64 + num_imm64_operand > MAX_IMM_64
38146                   || window_list->num_imm_32 + num_imm64_operand * 2 > MAX_IMM_32))
38147           || (window_list->imm_size + imm_size == MAX_IMM_SIZE
38148               && num_imm64_operand > 0
38149               && ((window_list->num_imm_64 > 0
38150                    && window_list->num_insn >= 2)
38151                   || window_list->num_insn >= 3)))
38152         return BIG;
38153
38154       return 1;
38155     }
38156
38157   if ((group == disp_load_store
38158        && (window_list->num_loads >= MAX_LOAD
38159            || window_list->num_stores >= MAX_STORE))
38160       || ((group == disp_load
38161            || group == disp_prefetch)
38162           && window_list->num_loads >= MAX_LOAD)
38163       || (group == disp_store
38164           && window_list->num_stores >= MAX_STORE))
38165     return BIG;
38166
38167   return 1;
38168 }
38169
38170 /* This function returns true if insn satisfies dispatch rules on the
38171    last window scheduled.  */
38172
38173 static bool
38174 fits_dispatch_window (rtx insn)
38175 {
38176   dispatch_windows *window_list = dispatch_window_list;
38177   dispatch_windows *window_list_next = dispatch_window_list->next;
38178   unsigned int num_restrict;
38179   enum dispatch_group group = get_insn_group (insn);
38180   enum insn_path path = get_insn_path (insn);
38181   int sum;
38182
38183   /* Make disp_cmp and disp_jcc get scheduled at the latest.  These
38184      instructions should be given the lowest priority in the
38185      scheduling process in Haifa scheduler to make sure they will be
38186      scheduled in the same dispatch window as the refrence to them.  */
38187   if (group == disp_jcc || group == disp_cmp)
38188     return false;
38189
38190   /* Check nonrestricted.  */
38191   if (group == disp_no_group || group == disp_branch)
38192     return true;
38193
38194   /* Get last dispatch window.  */
38195   if (window_list_next)
38196     window_list = window_list_next;
38197
38198   if (window_list->window_num == 1)
38199     {
38200       sum = window_list->prev->window_size + window_list->window_size;
38201
38202       if (sum == 32
38203           || (min_insn_size (insn) + sum) >= 48)
38204         /* Window 1 is full.  Go for next window.  */
38205         return true;
38206     }
38207
38208   num_restrict = count_num_restricted (insn, window_list);
38209
38210   if (num_restrict > num_allowable_groups[group])
38211     return false;
38212
38213   /* See if it fits in the first window.  */
38214   if (window_list->window_num == 0)
38215     {
38216       /* The first widow should have only single and double path
38217          uops.  */
38218       if (path == path_double
38219           && (window_list->num_uops + 2) > MAX_INSN)
38220         return false;
38221       else if (path != path_single)
38222         return false;
38223     }
38224   return true;
38225 }
38226
38227 /* Add an instruction INSN with NUM_UOPS micro-operations to the
38228    dispatch window WINDOW_LIST.  */
38229
38230 static void
38231 add_insn_window (rtx insn, dispatch_windows *window_list, int num_uops)
38232 {
38233   int byte_len = min_insn_size (insn);
38234   int num_insn = window_list->num_insn;
38235   int imm_size;
38236   sched_insn_info *window = window_list->window;
38237   enum dispatch_group group = get_insn_group (insn);
38238   enum insn_path path = get_insn_path (insn);
38239   int num_imm_operand;
38240   int num_imm32_operand;
38241   int num_imm64_operand;
38242
38243   if (!window_list->violation && group != disp_cmp
38244       && !fits_dispatch_window (insn))
38245     window_list->violation = true;
38246
38247   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38248                                  &num_imm64_operand);
38249
38250   /* Initialize window with new instruction.  */
38251   window[num_insn].insn = insn;
38252   window[num_insn].byte_len = byte_len;
38253   window[num_insn].group = group;
38254   window[num_insn].path = path;
38255   window[num_insn].imm_bytes = imm_size;
38256
38257   window_list->window_size += byte_len;
38258   window_list->num_insn = num_insn + 1;
38259   window_list->num_uops = window_list->num_uops + num_uops;
38260   window_list->imm_size += imm_size;
38261   window_list->num_imm += num_imm_operand;
38262   window_list->num_imm_32 += num_imm32_operand;
38263   window_list->num_imm_64 += num_imm64_operand;
38264
38265   if (group == disp_store)
38266     window_list->num_stores += 1;
38267   else if (group == disp_load
38268            || group == disp_prefetch)
38269     window_list->num_loads += 1;
38270   else if (group == disp_load_store)
38271     {
38272       window_list->num_stores += 1;
38273       window_list->num_loads += 1;
38274     }
38275 }
38276
38277 /* Adds a scheduled instruction, INSN, to the current dispatch window.
38278    If the total bytes of instructions or the number of instructions in
38279    the window exceed allowable, it allocates a new window.  */
38280
38281 static void
38282 add_to_dispatch_window (rtx insn)
38283 {
38284   int byte_len;
38285   dispatch_windows *window_list;
38286   dispatch_windows *next_list;
38287   dispatch_windows *window0_list;
38288   enum insn_path path;
38289   enum dispatch_group insn_group;
38290   bool insn_fits;
38291   int num_insn;
38292   int num_uops;
38293   int window_num;
38294   int insn_num_uops;
38295   int sum;
38296
38297   if (INSN_CODE (insn) < 0)
38298     return;
38299
38300   byte_len = min_insn_size (insn);
38301   window_list = dispatch_window_list;
38302   next_list = window_list->next;
38303   path = get_insn_path (insn);
38304   insn_group = get_insn_group (insn);
38305
38306   /* Get the last dispatch window.  */
38307   if (next_list)
38308       window_list = dispatch_window_list->next;
38309
38310   if (path == path_single)
38311     insn_num_uops = 1;
38312   else if (path == path_double)
38313     insn_num_uops = 2;
38314   else
38315     insn_num_uops = (int) path;
38316
38317   /* If current window is full, get a new window.
38318      Window number zero is full, if MAX_INSN uops are scheduled in it.
38319      Window number one is full, if window zero's bytes plus window
38320      one's bytes is 32, or if the bytes of the new instruction added
38321      to the total makes it greater than 48, or it has already MAX_INSN
38322      instructions in it.  */
38323   num_insn = window_list->num_insn;
38324   num_uops = window_list->num_uops;
38325   window_num = window_list->window_num;
38326   insn_fits = fits_dispatch_window (insn);
38327
38328   if (num_insn >= MAX_INSN
38329       || num_uops + insn_num_uops > MAX_INSN
38330       || !(insn_fits))
38331     {
38332       window_num = ~window_num & 1;
38333       window_list = allocate_next_window (window_num);
38334     }
38335
38336   if (window_num == 0)
38337     {
38338       add_insn_window (insn, window_list, insn_num_uops);
38339       if (window_list->num_insn >= MAX_INSN
38340           && insn_group == disp_branch)
38341         {
38342           process_end_window ();
38343           return;
38344         }
38345     }
38346   else if (window_num == 1)
38347     {
38348       window0_list = window_list->prev;
38349       sum = window0_list->window_size + window_list->window_size;
38350       if (sum == 32
38351           || (byte_len + sum) >= 48)
38352         {
38353           process_end_window ();
38354           window_list = dispatch_window_list;
38355         }
38356
38357       add_insn_window (insn, window_list, insn_num_uops);
38358     }
38359   else
38360     gcc_unreachable ();
38361
38362   if (is_end_basic_block (insn_group))
38363     {
38364       /* End of basic block is reached do end-basic-block process.  */
38365       process_end_window ();
38366       return;
38367     }
38368 }
38369
38370 /* Print the dispatch window, WINDOW_NUM, to FILE.  */
38371
38372 DEBUG_FUNCTION static void
38373 debug_dispatch_window_file (FILE *file, int window_num)
38374 {
38375   dispatch_windows *list;
38376   int i;
38377
38378   if (window_num == 0)
38379     list = dispatch_window_list;
38380   else
38381     list = dispatch_window_list1;
38382
38383   fprintf (file, "Window #%d:\n", list->window_num);
38384   fprintf (file, "  num_insn = %d, num_uops = %d, window_size = %d\n",
38385           list->num_insn, list->num_uops, list->window_size);
38386   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
38387            list->num_imm, list->num_imm_32, list->num_imm_64, list->imm_size);
38388
38389   fprintf (file, "  num_loads = %d, num_stores = %d\n", list->num_loads,
38390           list->num_stores);
38391   fprintf (file, " insn info:\n");
38392
38393   for (i = 0; i < MAX_INSN; i++)
38394     {
38395       if (!list->window[i].insn)
38396         break;
38397       fprintf (file, "    group[%d] = %s, insn[%d] = %p, path[%d] = %d byte_len[%d] = %d, imm_bytes[%d] = %d\n",
38398               i, group_name[list->window[i].group],
38399               i, (void *)list->window[i].insn,
38400               i, list->window[i].path,
38401               i, list->window[i].byte_len,
38402               i, list->window[i].imm_bytes);
38403     }
38404 }
38405
38406 /* Print to stdout a dispatch window.  */
38407
38408 DEBUG_FUNCTION void
38409 debug_dispatch_window (int window_num)
38410 {
38411   debug_dispatch_window_file (stdout, window_num);
38412 }
38413
38414 /* Print INSN dispatch information to FILE.  */
38415
38416 DEBUG_FUNCTION static void
38417 debug_insn_dispatch_info_file (FILE *file, rtx insn)
38418 {
38419   int byte_len;
38420   enum insn_path path;
38421   enum dispatch_group group;
38422   int imm_size;
38423   int num_imm_operand;
38424   int num_imm32_operand;
38425   int num_imm64_operand;
38426
38427   if (INSN_CODE (insn) < 0)
38428     return;
38429
38430   byte_len = min_insn_size (insn);
38431   path = get_insn_path (insn);
38432   group = get_insn_group (insn);
38433   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38434                                  &num_imm64_operand);
38435
38436   fprintf (file, " insn info:\n");
38437   fprintf (file, "  group = %s, path = %d, byte_len = %d\n",
38438            group_name[group], path, byte_len);
38439   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
38440            num_imm_operand, num_imm32_operand, num_imm64_operand, imm_size);
38441 }
38442
38443 /* Print to STDERR the status of the ready list with respect to
38444    dispatch windows.  */
38445
38446 DEBUG_FUNCTION void
38447 debug_ready_dispatch (void)
38448 {
38449   int i;
38450   int no_ready = number_in_ready ();
38451
38452   fprintf (stdout, "Number of ready: %d\n", no_ready);
38453
38454   for (i = 0; i < no_ready; i++)
38455     debug_insn_dispatch_info_file (stdout, get_ready_element (i));
38456 }
38457
38458 /* This routine is the driver of the dispatch scheduler.  */
38459
38460 static void
38461 do_dispatch (rtx insn, int mode)
38462 {
38463   if (mode == DISPATCH_INIT)
38464     init_dispatch_sched ();
38465   else if (mode == ADD_TO_DISPATCH_WINDOW)
38466     add_to_dispatch_window (insn);
38467 }
38468
38469 /* Return TRUE if Dispatch Scheduling is supported.  */
38470
38471 static bool
38472 has_dispatch (rtx insn, int action)
38473 {
38474   if ((ix86_tune == PROCESSOR_BDVER1 || ix86_tune == PROCESSOR_BDVER2)
38475       && flag_dispatch_scheduler)
38476     switch (action)
38477       {
38478       default:
38479         return false;
38480
38481       case IS_DISPATCH_ON:
38482         return true;
38483         break;
38484
38485       case IS_CMP:
38486         return is_cmp (insn);
38487
38488       case DISPATCH_VIOLATION:
38489         return dispatch_violation ();
38490
38491       case FITS_DISPATCH_WINDOW:
38492         return fits_dispatch_window (insn);
38493       }
38494
38495   return false;
38496 }
38497
38498 /* Implementation of reassociation_width target hook used by
38499    reassoc phase to identify parallelism level in reassociated
38500    tree.  Statements tree_code is passed in OPC.  Arguments type
38501    is passed in MODE.
38502
38503    Currently parallel reassociation is enabled for Atom
38504    processors only and we set reassociation width to be 2
38505    because Atom may issue up to 2 instructions per cycle.
38506
38507    Return value should be fixed if parallel reassociation is
38508    enabled for other processors.  */
38509
38510 static int
38511 ix86_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
38512                           enum machine_mode mode)
38513 {
38514   int res = 1;
38515
38516   if (INTEGRAL_MODE_P (mode) && TARGET_REASSOC_INT_TO_PARALLEL)
38517     res = 2;
38518   else if (FLOAT_MODE_P (mode) && TARGET_REASSOC_FP_TO_PARALLEL)
38519     res = 2;
38520
38521   return res;
38522 }
38523
38524 /* ??? No autovectorization into MMX or 3DNOW until we can reliably
38525    place emms and femms instructions.  */
38526
38527 static enum machine_mode
38528 ix86_preferred_simd_mode (enum machine_mode mode)
38529 {
38530   if (!TARGET_SSE)
38531     return word_mode;
38532
38533   switch (mode)
38534     {
38535     case QImode:
38536       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V32QImode : V16QImode;
38537     case HImode:
38538       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V16HImode : V8HImode;
38539     case SImode:
38540       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V8SImode : V4SImode;
38541     case DImode:
38542       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V4DImode : V2DImode;
38543
38544     case SFmode:
38545       if (TARGET_AVX && !TARGET_PREFER_AVX128)
38546         return V8SFmode;
38547       else
38548         return V4SFmode;
38549
38550     case DFmode:
38551       if (!TARGET_VECTORIZE_DOUBLE)
38552         return word_mode;
38553       else if (TARGET_AVX && !TARGET_PREFER_AVX128)
38554         return V4DFmode;
38555       else if (TARGET_SSE2)
38556         return V2DFmode;
38557       /* FALLTHRU */
38558
38559     default:
38560       return word_mode;
38561     }
38562 }
38563
38564 /* If AVX is enabled then try vectorizing with both 256bit and 128bit
38565    vectors.  */
38566
38567 static unsigned int
38568 ix86_autovectorize_vector_sizes (void)
38569 {
38570   return (TARGET_AVX && !TARGET_PREFER_AVX128) ? 32 | 16 : 0;
38571 }
38572
38573 /* Initialize the GCC target structure.  */
38574 #undef TARGET_RETURN_IN_MEMORY
38575 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
38576
38577 #undef TARGET_LEGITIMIZE_ADDRESS
38578 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
38579
38580 #undef TARGET_ATTRIBUTE_TABLE
38581 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
38582 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
38583 #  undef TARGET_MERGE_DECL_ATTRIBUTES
38584 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
38585 #endif
38586
38587 #undef TARGET_COMP_TYPE_ATTRIBUTES
38588 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
38589
38590 #undef TARGET_INIT_BUILTINS
38591 #define TARGET_INIT_BUILTINS ix86_init_builtins
38592 #undef TARGET_BUILTIN_DECL
38593 #define TARGET_BUILTIN_DECL ix86_builtin_decl
38594 #undef TARGET_EXPAND_BUILTIN
38595 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
38596
38597 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
38598 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
38599   ix86_builtin_vectorized_function
38600
38601 #undef TARGET_VECTORIZE_BUILTIN_TM_LOAD
38602 #define TARGET_VECTORIZE_BUILTIN_TM_LOAD ix86_builtin_tm_load
38603
38604 #undef TARGET_VECTORIZE_BUILTIN_TM_STORE
38605 #define TARGET_VECTORIZE_BUILTIN_TM_STORE ix86_builtin_tm_store
38606
38607 #undef TARGET_VECTORIZE_BUILTIN_GATHER
38608 #define TARGET_VECTORIZE_BUILTIN_GATHER ix86_vectorize_builtin_gather
38609
38610 #undef TARGET_BUILTIN_RECIPROCAL
38611 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
38612
38613 #undef TARGET_ASM_FUNCTION_EPILOGUE
38614 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
38615
38616 #undef TARGET_ENCODE_SECTION_INFO
38617 #ifndef SUBTARGET_ENCODE_SECTION_INFO
38618 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
38619 #else
38620 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
38621 #endif
38622
38623 #undef TARGET_ASM_OPEN_PAREN
38624 #define TARGET_ASM_OPEN_PAREN ""
38625 #undef TARGET_ASM_CLOSE_PAREN
38626 #define TARGET_ASM_CLOSE_PAREN ""
38627
38628 #undef TARGET_ASM_BYTE_OP
38629 #define TARGET_ASM_BYTE_OP ASM_BYTE
38630
38631 #undef TARGET_ASM_ALIGNED_HI_OP
38632 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
38633 #undef TARGET_ASM_ALIGNED_SI_OP
38634 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
38635 #ifdef ASM_QUAD
38636 #undef TARGET_ASM_ALIGNED_DI_OP
38637 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
38638 #endif
38639
38640 #undef TARGET_PROFILE_BEFORE_PROLOGUE
38641 #define TARGET_PROFILE_BEFORE_PROLOGUE ix86_profile_before_prologue
38642
38643 #undef TARGET_ASM_UNALIGNED_HI_OP
38644 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
38645 #undef TARGET_ASM_UNALIGNED_SI_OP
38646 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
38647 #undef TARGET_ASM_UNALIGNED_DI_OP
38648 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
38649
38650 #undef TARGET_PRINT_OPERAND
38651 #define TARGET_PRINT_OPERAND ix86_print_operand
38652 #undef TARGET_PRINT_OPERAND_ADDRESS
38653 #define TARGET_PRINT_OPERAND_ADDRESS ix86_print_operand_address
38654 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
38655 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ix86_print_operand_punct_valid_p
38656 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
38657 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA i386_asm_output_addr_const_extra
38658
38659 #undef TARGET_SCHED_INIT_GLOBAL
38660 #define TARGET_SCHED_INIT_GLOBAL ix86_sched_init_global
38661 #undef TARGET_SCHED_ADJUST_COST
38662 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
38663 #undef TARGET_SCHED_ISSUE_RATE
38664 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
38665 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
38666 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
38667   ia32_multipass_dfa_lookahead
38668
38669 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
38670 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
38671
38672 #ifdef HAVE_AS_TLS
38673 #undef TARGET_HAVE_TLS
38674 #define TARGET_HAVE_TLS true
38675 #endif
38676 #undef TARGET_CANNOT_FORCE_CONST_MEM
38677 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
38678 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
38679 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
38680
38681 #undef TARGET_DELEGITIMIZE_ADDRESS
38682 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
38683
38684 #undef TARGET_MS_BITFIELD_LAYOUT_P
38685 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
38686
38687 #if TARGET_MACHO
38688 #undef TARGET_BINDS_LOCAL_P
38689 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
38690 #endif
38691 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
38692 #undef TARGET_BINDS_LOCAL_P
38693 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
38694 #endif
38695
38696 #undef TARGET_ASM_OUTPUT_MI_THUNK
38697 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
38698 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
38699 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
38700
38701 #undef TARGET_ASM_FILE_START
38702 #define TARGET_ASM_FILE_START x86_file_start
38703
38704 #undef TARGET_OPTION_OVERRIDE
38705 #define TARGET_OPTION_OVERRIDE ix86_option_override
38706
38707 #undef TARGET_REGISTER_MOVE_COST
38708 #define TARGET_REGISTER_MOVE_COST ix86_register_move_cost
38709 #undef TARGET_MEMORY_MOVE_COST
38710 #define TARGET_MEMORY_MOVE_COST ix86_memory_move_cost
38711 #undef TARGET_RTX_COSTS
38712 #define TARGET_RTX_COSTS ix86_rtx_costs
38713 #undef TARGET_ADDRESS_COST
38714 #define TARGET_ADDRESS_COST ix86_address_cost
38715
38716 #undef TARGET_FIXED_CONDITION_CODE_REGS
38717 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
38718 #undef TARGET_CC_MODES_COMPATIBLE
38719 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
38720
38721 #undef TARGET_MACHINE_DEPENDENT_REORG
38722 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
38723
38724 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
38725 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
38726
38727 #undef TARGET_BUILD_BUILTIN_VA_LIST
38728 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
38729
38730 #undef TARGET_ENUM_VA_LIST_P
38731 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
38732
38733 #undef TARGET_FN_ABI_VA_LIST
38734 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
38735
38736 #undef TARGET_CANONICAL_VA_LIST_TYPE
38737 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
38738
38739 #undef TARGET_EXPAND_BUILTIN_VA_START
38740 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
38741
38742 #undef TARGET_MD_ASM_CLOBBERS
38743 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
38744
38745 #undef TARGET_PROMOTE_PROTOTYPES
38746 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
38747 #undef TARGET_STRUCT_VALUE_RTX
38748 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
38749 #undef TARGET_SETUP_INCOMING_VARARGS
38750 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
38751 #undef TARGET_MUST_PASS_IN_STACK
38752 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
38753 #undef TARGET_FUNCTION_ARG_ADVANCE
38754 #define TARGET_FUNCTION_ARG_ADVANCE ix86_function_arg_advance
38755 #undef TARGET_FUNCTION_ARG
38756 #define TARGET_FUNCTION_ARG ix86_function_arg
38757 #undef TARGET_FUNCTION_ARG_BOUNDARY
38758 #define TARGET_FUNCTION_ARG_BOUNDARY ix86_function_arg_boundary
38759 #undef TARGET_PASS_BY_REFERENCE
38760 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
38761 #undef TARGET_INTERNAL_ARG_POINTER
38762 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
38763 #undef TARGET_UPDATE_STACK_BOUNDARY
38764 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
38765 #undef TARGET_GET_DRAP_RTX
38766 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
38767 #undef TARGET_STRICT_ARGUMENT_NAMING
38768 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
38769 #undef TARGET_STATIC_CHAIN
38770 #define TARGET_STATIC_CHAIN ix86_static_chain
38771 #undef TARGET_TRAMPOLINE_INIT
38772 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
38773 #undef TARGET_RETURN_POPS_ARGS
38774 #define TARGET_RETURN_POPS_ARGS ix86_return_pops_args
38775
38776 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
38777 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
38778
38779 #undef TARGET_SCALAR_MODE_SUPPORTED_P
38780 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
38781
38782 #undef TARGET_VECTOR_MODE_SUPPORTED_P
38783 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
38784
38785 #undef TARGET_C_MODE_FOR_SUFFIX
38786 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
38787
38788 #ifdef HAVE_AS_TLS
38789 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
38790 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
38791 #endif
38792
38793 #ifdef SUBTARGET_INSERT_ATTRIBUTES
38794 #undef TARGET_INSERT_ATTRIBUTES
38795 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
38796 #endif
38797
38798 #undef TARGET_MANGLE_TYPE
38799 #define TARGET_MANGLE_TYPE ix86_mangle_type
38800
38801 #if !TARGET_MACHO
38802 #undef TARGET_STACK_PROTECT_FAIL
38803 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
38804 #endif
38805
38806 #undef TARGET_FUNCTION_VALUE
38807 #define TARGET_FUNCTION_VALUE ix86_function_value
38808
38809 #undef TARGET_FUNCTION_VALUE_REGNO_P
38810 #define TARGET_FUNCTION_VALUE_REGNO_P ix86_function_value_regno_p
38811
38812 #undef TARGET_PROMOTE_FUNCTION_MODE
38813 #define TARGET_PROMOTE_FUNCTION_MODE ix86_promote_function_mode
38814
38815 #undef TARGET_SECONDARY_RELOAD
38816 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
38817
38818 #undef TARGET_CLASS_MAX_NREGS
38819 #define TARGET_CLASS_MAX_NREGS ix86_class_max_nregs
38820
38821 #undef TARGET_PREFERRED_RELOAD_CLASS
38822 #define TARGET_PREFERRED_RELOAD_CLASS ix86_preferred_reload_class
38823 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
38824 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS ix86_preferred_output_reload_class
38825 #undef TARGET_CLASS_LIKELY_SPILLED_P
38826 #define TARGET_CLASS_LIKELY_SPILLED_P ix86_class_likely_spilled_p
38827
38828 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
38829 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
38830   ix86_builtin_vectorization_cost
38831 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
38832 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK \
38833   ix86_vectorize_vec_perm_const_ok
38834 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
38835 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
38836   ix86_preferred_simd_mode
38837 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
38838 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
38839   ix86_autovectorize_vector_sizes
38840
38841 #undef TARGET_SET_CURRENT_FUNCTION
38842 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
38843
38844 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
38845 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
38846
38847 #undef TARGET_OPTION_SAVE
38848 #define TARGET_OPTION_SAVE ix86_function_specific_save
38849
38850 #undef TARGET_OPTION_RESTORE
38851 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
38852
38853 #undef TARGET_OPTION_PRINT
38854 #define TARGET_OPTION_PRINT ix86_function_specific_print
38855
38856 #undef TARGET_CAN_INLINE_P
38857 #define TARGET_CAN_INLINE_P ix86_can_inline_p
38858
38859 #undef TARGET_EXPAND_TO_RTL_HOOK
38860 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
38861
38862 #undef TARGET_LEGITIMATE_ADDRESS_P
38863 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
38864
38865 #undef TARGET_LEGITIMATE_CONSTANT_P
38866 #define TARGET_LEGITIMATE_CONSTANT_P ix86_legitimate_constant_p
38867
38868 #undef TARGET_FRAME_POINTER_REQUIRED
38869 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
38870
38871 #undef TARGET_CAN_ELIMINATE
38872 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
38873
38874 #undef TARGET_EXTRA_LIVE_ON_ENTRY
38875 #define TARGET_EXTRA_LIVE_ON_ENTRY ix86_live_on_entry
38876
38877 #undef TARGET_ASM_CODE_END
38878 #define TARGET_ASM_CODE_END ix86_code_end
38879
38880 #undef TARGET_CONDITIONAL_REGISTER_USAGE
38881 #define TARGET_CONDITIONAL_REGISTER_USAGE ix86_conditional_register_usage
38882
38883 #if TARGET_MACHO
38884 #undef TARGET_INIT_LIBFUNCS
38885 #define TARGET_INIT_LIBFUNCS darwin_rename_builtins
38886 #endif
38887
38888 struct gcc_target targetm = TARGET_INITIALIZER;
38889 \f
38890 #include "gt-i386.h"