OSDN Git Service

cc53a6262d15485706f42e129104982799729bd5
[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, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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   64,                                   /* 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 processor has SSE prefetch instruction.  */
2432 int x86_prefetch_sse;
2433
2434 /* True if processor has prefetchw instruction.  */
2435 int x86_prefetchw;
2436  
2437 /* -mstackrealign option */
2438 static const char ix86_force_align_arg_pointer_string[]
2439   = "force_align_arg_pointer";
2440
2441 static rtx (*ix86_gen_leave) (void);
2442 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
2443 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
2444 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
2445 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
2446 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
2447 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
2448 static rtx (*ix86_gen_allocate_stack_worker) (rtx, rtx);
2449 static rtx (*ix86_gen_adjust_stack_and_probe) (rtx, rtx, rtx);
2450 static rtx (*ix86_gen_probe_stack_range) (rtx, rtx, rtx);
2451
2452 /* Preferred alignment for stack boundary in bits.  */
2453 unsigned int ix86_preferred_stack_boundary;
2454
2455 /* Alignment for incoming stack boundary in bits specified at
2456    command line.  */
2457 static unsigned int ix86_user_incoming_stack_boundary;
2458
2459 /* Default alignment for incoming stack boundary in bits.  */
2460 static unsigned int ix86_default_incoming_stack_boundary;
2461
2462 /* Alignment for incoming stack boundary in bits.  */
2463 unsigned int ix86_incoming_stack_boundary;
2464
2465 /* Calling abi specific va_list type nodes.  */
2466 static GTY(()) tree sysv_va_list_type_node;
2467 static GTY(()) tree ms_va_list_type_node;
2468
2469 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
2470 char internal_label_prefix[16];
2471 int internal_label_prefix_len;
2472
2473 /* Fence to use after loop using movnt.  */
2474 tree x86_mfence;
2475
2476 /* Register class used for passing given 64bit part of the argument.
2477    These represent classes as documented by the PS ABI, with the exception
2478    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
2479    use SF or DFmode move instead of DImode to avoid reformatting penalties.
2480
2481    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
2482    whenever possible (upper half does contain padding).  */
2483 enum x86_64_reg_class
2484   {
2485     X86_64_NO_CLASS,
2486     X86_64_INTEGER_CLASS,
2487     X86_64_INTEGERSI_CLASS,
2488     X86_64_SSE_CLASS,
2489     X86_64_SSESF_CLASS,
2490     X86_64_SSEDF_CLASS,
2491     X86_64_SSEUP_CLASS,
2492     X86_64_X87_CLASS,
2493     X86_64_X87UP_CLASS,
2494     X86_64_COMPLEX_X87_CLASS,
2495     X86_64_MEMORY_CLASS
2496   };
2497
2498 #define MAX_CLASSES 4
2499
2500 /* Table of constants used by fldpi, fldln2, etc....  */
2501 static REAL_VALUE_TYPE ext_80387_constants_table [5];
2502 static bool ext_80387_constants_init = 0;
2503
2504 \f
2505 static struct machine_function * ix86_init_machine_status (void);
2506 static rtx ix86_function_value (const_tree, const_tree, bool);
2507 static bool ix86_function_value_regno_p (const unsigned int);
2508 static unsigned int ix86_function_arg_boundary (enum machine_mode,
2509                                                 const_tree);
2510 static rtx ix86_static_chain (const_tree, bool);
2511 static int ix86_function_regparm (const_tree, const_tree);
2512 static void ix86_compute_frame_layout (struct ix86_frame *);
2513 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
2514                                                  rtx, rtx, int);
2515 static void ix86_add_new_builtins (HOST_WIDE_INT);
2516 static tree ix86_canonical_va_list_type (tree);
2517 static void predict_jump (int);
2518 static unsigned int split_stack_prologue_scratch_regno (void);
2519 static bool i386_asm_output_addr_const_extra (FILE *, rtx);
2520
2521 enum ix86_function_specific_strings
2522 {
2523   IX86_FUNCTION_SPECIFIC_ARCH,
2524   IX86_FUNCTION_SPECIFIC_TUNE,
2525   IX86_FUNCTION_SPECIFIC_MAX
2526 };
2527
2528 static char *ix86_target_string (HOST_WIDE_INT, int, const char *,
2529                                  const char *, enum fpmath_unit, bool);
2530 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
2531 static void ix86_function_specific_save (struct cl_target_option *);
2532 static void ix86_function_specific_restore (struct cl_target_option *);
2533 static void ix86_function_specific_print (FILE *, int,
2534                                           struct cl_target_option *);
2535 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
2536 static bool ix86_valid_target_attribute_inner_p (tree, char *[],
2537                                                  struct gcc_options *);
2538 static bool ix86_can_inline_p (tree, tree);
2539 static void ix86_set_current_function (tree);
2540 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
2541
2542 static enum calling_abi ix86_function_abi (const_tree);
2543
2544 \f
2545 #ifndef SUBTARGET32_DEFAULT_CPU
2546 #define SUBTARGET32_DEFAULT_CPU "i386"
2547 #endif
2548
2549 /* The svr4 ABI for the i386 says that records and unions are returned
2550    in memory.  */
2551 #ifndef DEFAULT_PCC_STRUCT_RETURN
2552 #define DEFAULT_PCC_STRUCT_RETURN 1
2553 #endif
2554
2555 /* Whether -mtune= or -march= were specified */
2556 static int ix86_tune_defaulted;
2557 static int ix86_arch_specified;
2558
2559 /* Vectorization library interface and handlers.  */
2560 static tree (*ix86_veclib_handler) (enum built_in_function, tree, tree);
2561
2562 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2563 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2564
2565 /* Processor target table, indexed by processor number */
2566 struct ptt
2567 {
2568   const struct processor_costs *cost;           /* Processor costs */
2569   const int align_loop;                         /* Default alignments.  */
2570   const int align_loop_max_skip;
2571   const int align_jump;
2572   const int align_jump_max_skip;
2573   const int align_func;
2574 };
2575
2576 static const struct ptt processor_target_table[PROCESSOR_max] =
2577 {
2578   {&i386_cost, 4, 3, 4, 3, 4},
2579   {&i486_cost, 16, 15, 16, 15, 16},
2580   {&pentium_cost, 16, 7, 16, 7, 16},
2581   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2582   {&geode_cost, 0, 0, 0, 0, 0},
2583   {&k6_cost, 32, 7, 32, 7, 32},
2584   {&athlon_cost, 16, 7, 16, 7, 16},
2585   {&pentium4_cost, 0, 0, 0, 0, 0},
2586   {&k8_cost, 16, 7, 16, 7, 16},
2587   {&nocona_cost, 0, 0, 0, 0, 0},
2588   /* Core 2 32-bit.  */
2589   {&generic32_cost, 16, 10, 16, 10, 16},
2590   /* Core 2 64-bit.  */
2591   {&generic64_cost, 16, 10, 16, 10, 16},
2592   /* Core i7 32-bit.  */
2593   {&generic32_cost, 16, 10, 16, 10, 16},
2594   /* Core i7 64-bit.  */
2595   {&generic64_cost, 16, 10, 16, 10, 16},
2596   {&generic32_cost, 16, 7, 16, 7, 16},
2597   {&generic64_cost, 16, 10, 16, 10, 16},
2598   {&amdfam10_cost, 32, 24, 32, 7, 32},
2599   {&bdver1_cost, 32, 24, 32, 7, 32},
2600   {&bdver2_cost, 32, 24, 32, 7, 32},
2601   {&btver1_cost, 32, 24, 32, 7, 32},
2602   {&atom_cost, 16, 15, 16, 7, 16}
2603 };
2604
2605 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2606 {
2607   "generic",
2608   "i386",
2609   "i486",
2610   "pentium",
2611   "pentium-mmx",
2612   "pentiumpro",
2613   "pentium2",
2614   "pentium3",
2615   "pentium4",
2616   "pentium-m",
2617   "prescott",
2618   "nocona",
2619   "core2",
2620   "corei7",
2621   "atom",
2622   "geode",
2623   "k6",
2624   "k6-2",
2625   "k6-3",
2626   "athlon",
2627   "athlon-4",
2628   "k8",
2629   "amdfam10",
2630   "bdver1",
2631   "bdver2",
2632   "btver1"
2633 };
2634 \f
2635 /* Return true if a red-zone is in use.  */
2636
2637 static inline bool
2638 ix86_using_red_zone (void)
2639 {
2640   return TARGET_RED_ZONE && !TARGET_64BIT_MS_ABI;
2641 }
2642 \f
2643 /* Return a string that documents the current -m options.  The caller is
2644    responsible for freeing the string.  */
2645
2646 static char *
2647 ix86_target_string (HOST_WIDE_INT isa, int flags, const char *arch,
2648                     const char *tune, enum fpmath_unit fpmath,
2649                     bool add_nl_p)
2650 {
2651   struct ix86_target_opts
2652   {
2653     const char *option;         /* option string */
2654     HOST_WIDE_INT mask;         /* isa mask options */
2655   };
2656
2657   /* This table is ordered so that options like -msse4.2 that imply
2658      preceding options while match those first.  */
2659   static struct ix86_target_opts isa_opts[] =
2660   {
2661     { "-m64",           OPTION_MASK_ISA_64BIT },
2662     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2663     { "-mfma",          OPTION_MASK_ISA_FMA },
2664     { "-mxop",          OPTION_MASK_ISA_XOP },
2665     { "-mlwp",          OPTION_MASK_ISA_LWP },
2666     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2667     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2668     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2669     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2670     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2671     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2672     { "-msse",          OPTION_MASK_ISA_SSE },
2673     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2674     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2675     { "-mmmx",          OPTION_MASK_ISA_MMX },
2676     { "-mabm",          OPTION_MASK_ISA_ABM },
2677     { "-mbmi",          OPTION_MASK_ISA_BMI },
2678     { "-mbmi2",         OPTION_MASK_ISA_BMI2 },
2679     { "-mlzcnt",        OPTION_MASK_ISA_LZCNT },
2680     { "-mtbm",          OPTION_MASK_ISA_TBM },
2681     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2682     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2683     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2684     { "-maes",          OPTION_MASK_ISA_AES },
2685     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2686     { "-mfsgsbase",     OPTION_MASK_ISA_FSGSBASE },
2687     { "-mrdrnd",        OPTION_MASK_ISA_RDRND },
2688     { "-mf16c",         OPTION_MASK_ISA_F16C },
2689   };
2690
2691   /* Flag options.  */
2692   static struct ix86_target_opts flag_opts[] =
2693   {
2694     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
2695     { "-m80387",                        MASK_80387 },
2696     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
2697     { "-malign-double",                 MASK_ALIGN_DOUBLE },
2698     { "-mcld",                          MASK_CLD },
2699     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
2700     { "-mieee-fp",                      MASK_IEEE_FP },
2701     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
2702     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
2703     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
2704     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
2705     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
2706     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
2707     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
2708     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
2709     { "-mrecip",                        MASK_RECIP },
2710     { "-mrtd",                          MASK_RTD },
2711     { "-msseregparm",                   MASK_SSEREGPARM },
2712     { "-mstack-arg-probe",              MASK_STACK_PROBE },
2713     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
2714     { "-mvect8-ret-in-mem",             MASK_VECT8_RETURNS },
2715     { "-m8bit-idiv",                    MASK_USE_8BIT_IDIV },
2716     { "-mvzeroupper",                   MASK_VZEROUPPER },
2717     { "-mavx256-split-unaligned-load",  MASK_AVX256_SPLIT_UNALIGNED_LOAD},
2718     { "-mavx256-split-unaligned-store", MASK_AVX256_SPLIT_UNALIGNED_STORE},
2719     { "-mprefer-avx128",                MASK_PREFER_AVX128},
2720   };
2721
2722   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
2723
2724   char isa_other[40];
2725   char target_other[40];
2726   unsigned num = 0;
2727   unsigned i, j;
2728   char *ret;
2729   char *ptr;
2730   size_t len;
2731   size_t line_len;
2732   size_t sep_len;
2733
2734   memset (opts, '\0', sizeof (opts));
2735
2736   /* Add -march= option.  */
2737   if (arch)
2738     {
2739       opts[num][0] = "-march=";
2740       opts[num++][1] = arch;
2741     }
2742
2743   /* Add -mtune= option.  */
2744   if (tune)
2745     {
2746       opts[num][0] = "-mtune=";
2747       opts[num++][1] = tune;
2748     }
2749
2750   /* Pick out the options in isa options.  */
2751   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
2752     {
2753       if ((isa & isa_opts[i].mask) != 0)
2754         {
2755           opts[num++][0] = isa_opts[i].option;
2756           isa &= ~ isa_opts[i].mask;
2757         }
2758     }
2759
2760   if (isa && add_nl_p)
2761     {
2762       opts[num++][0] = isa_other;
2763       sprintf (isa_other, "(other isa: %#" HOST_WIDE_INT_PRINT "x)",
2764                isa);
2765     }
2766
2767   /* Add flag options.  */
2768   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
2769     {
2770       if ((flags & flag_opts[i].mask) != 0)
2771         {
2772           opts[num++][0] = flag_opts[i].option;
2773           flags &= ~ flag_opts[i].mask;
2774         }
2775     }
2776
2777   if (flags && add_nl_p)
2778     {
2779       opts[num++][0] = target_other;
2780       sprintf (target_other, "(other flags: %#x)", flags);
2781     }
2782
2783   /* Add -fpmath= option.  */
2784   if (fpmath)
2785     {
2786       opts[num][0] = "-mfpmath=";
2787       switch ((int) fpmath)
2788         {
2789         case FPMATH_387:
2790           opts[num++][1] = "387";
2791           break;
2792
2793         case FPMATH_SSE:
2794           opts[num++][1] = "sse";
2795           break;
2796
2797         case FPMATH_387 | FPMATH_SSE:
2798           opts[num++][1] = "sse+387";
2799           break;
2800
2801         default:
2802           gcc_unreachable ();
2803         }
2804     }
2805
2806   /* Any options?  */
2807   if (num == 0)
2808     return NULL;
2809
2810   gcc_assert (num < ARRAY_SIZE (opts));
2811
2812   /* Size the string.  */
2813   len = 0;
2814   sep_len = (add_nl_p) ? 3 : 1;
2815   for (i = 0; i < num; i++)
2816     {
2817       len += sep_len;
2818       for (j = 0; j < 2; j++)
2819         if (opts[i][j])
2820           len += strlen (opts[i][j]);
2821     }
2822
2823   /* Build the string.  */
2824   ret = ptr = (char *) xmalloc (len);
2825   line_len = 0;
2826
2827   for (i = 0; i < num; i++)
2828     {
2829       size_t len2[2];
2830
2831       for (j = 0; j < 2; j++)
2832         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
2833
2834       if (i != 0)
2835         {
2836           *ptr++ = ' ';
2837           line_len++;
2838
2839           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
2840             {
2841               *ptr++ = '\\';
2842               *ptr++ = '\n';
2843               line_len = 0;
2844             }
2845         }
2846
2847       for (j = 0; j < 2; j++)
2848         if (opts[i][j])
2849           {
2850             memcpy (ptr, opts[i][j], len2[j]);
2851             ptr += len2[j];
2852             line_len += len2[j];
2853           }
2854     }
2855
2856   *ptr = '\0';
2857   gcc_assert (ret + len >= ptr);
2858
2859   return ret;
2860 }
2861
2862 /* Return true, if profiling code should be emitted before
2863    prologue. Otherwise it returns false.
2864    Note: For x86 with "hotfix" it is sorried.  */
2865 static bool
2866 ix86_profile_before_prologue (void)
2867 {
2868   return flag_fentry != 0;
2869 }
2870
2871 /* Function that is callable from the debugger to print the current
2872    options.  */
2873 void
2874 ix86_debug_options (void)
2875 {
2876   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
2877                                    ix86_arch_string, ix86_tune_string,
2878                                    ix86_fpmath, true);
2879
2880   if (opts)
2881     {
2882       fprintf (stderr, "%s\n\n", opts);
2883       free (opts);
2884     }
2885   else
2886     fputs ("<no options>\n\n", stderr);
2887
2888   return;
2889 }
2890 \f
2891 /* Override various settings based on options.  If MAIN_ARGS_P, the
2892    options are from the command line, otherwise they are from
2893    attributes.  */
2894
2895 static void
2896 ix86_option_override_internal (bool main_args_p)
2897 {
2898   int i;
2899   unsigned int ix86_arch_mask, ix86_tune_mask;
2900   const bool ix86_tune_specified = (ix86_tune_string != NULL);
2901   const char *prefix;
2902   const char *suffix;
2903   const char *sw;
2904
2905 #define PTA_3DNOW               (HOST_WIDE_INT_1 << 0)
2906 #define PTA_3DNOW_A             (HOST_WIDE_INT_1 << 1)
2907 #define PTA_64BIT               (HOST_WIDE_INT_1 << 2)
2908 #define PTA_ABM                 (HOST_WIDE_INT_1 << 3)
2909 #define PTA_AES                 (HOST_WIDE_INT_1 << 4)
2910 #define PTA_AVX                 (HOST_WIDE_INT_1 << 5)
2911 #define PTA_BMI                 (HOST_WIDE_INT_1 << 6)
2912 #define PTA_CX16                (HOST_WIDE_INT_1 << 7)
2913 #define PTA_F16C                (HOST_WIDE_INT_1 << 8)
2914 #define PTA_FMA                 (HOST_WIDE_INT_1 << 9)
2915 #define PTA_FMA4                (HOST_WIDE_INT_1 << 10)
2916 #define PTA_FSGSBASE            (HOST_WIDE_INT_1 << 11)
2917 #define PTA_LWP                 (HOST_WIDE_INT_1 << 12)
2918 #define PTA_LZCNT               (HOST_WIDE_INT_1 << 13)
2919 #define PTA_MMX                 (HOST_WIDE_INT_1 << 14)
2920 #define PTA_MOVBE               (HOST_WIDE_INT_1 << 15)
2921 #define PTA_NO_SAHF             (HOST_WIDE_INT_1 << 16)
2922 #define PTA_PCLMUL              (HOST_WIDE_INT_1 << 17)
2923 #define PTA_POPCNT              (HOST_WIDE_INT_1 << 18)
2924 #define PTA_PREFETCH_SSE        (HOST_WIDE_INT_1 << 19)
2925 #define PTA_RDRND               (HOST_WIDE_INT_1 << 20)
2926 #define PTA_SSE                 (HOST_WIDE_INT_1 << 21)
2927 #define PTA_SSE2                (HOST_WIDE_INT_1 << 22)
2928 #define PTA_SSE3                (HOST_WIDE_INT_1 << 23)
2929 #define PTA_SSE4_1              (HOST_WIDE_INT_1 << 24)
2930 #define PTA_SSE4_2              (HOST_WIDE_INT_1 << 25)
2931 #define PTA_SSE4A               (HOST_WIDE_INT_1 << 26)
2932 #define PTA_SSSE3               (HOST_WIDE_INT_1 << 27)
2933 #define PTA_TBM                 (HOST_WIDE_INT_1 << 28)
2934 #define PTA_XOP                 (HOST_WIDE_INT_1 << 29)
2935 #define PTA_AVX2                (HOST_WIDE_INT_1 << 30)
2936 #define PTA_BMI2                (HOST_WIDE_INT_1 << 31)
2937 #define PTA_PREFETCHW           (HOST_WIDE_INT_1 << 32)
2938
2939 /* if this reaches 64, need to widen struct pta flags below */
2940
2941   static struct pta
2942     {
2943       const char *const name;           /* processor name or nickname.  */
2944       const enum processor_type processor;
2945       const enum attr_cpu schedule;
2946       const unsigned HOST_WIDE_INT flags;
2947     }
2948   const processor_alias_table[] =
2949     {
2950       {"i386", PROCESSOR_I386, CPU_NONE, 0},
2951       {"i486", PROCESSOR_I486, CPU_NONE, 0},
2952       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2953       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2954       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
2955       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
2956       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2957       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2958       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
2959       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2960       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2961       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
2962       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2963         PTA_MMX | PTA_SSE},
2964       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2965         PTA_MMX | PTA_SSE},
2966       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2967         PTA_MMX | PTA_SSE | PTA_SSE2},
2968       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
2969         PTA_MMX |PTA_SSE | PTA_SSE2},
2970       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
2971         PTA_MMX | PTA_SSE | PTA_SSE2},
2972       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
2973         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
2974       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
2975         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2976         | PTA_CX16 | PTA_NO_SAHF},
2977       {"core2", PROCESSOR_CORE2_64, CPU_CORE2,
2978         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2979         | PTA_SSSE3 | PTA_CX16},
2980       {"corei7", PROCESSOR_COREI7_64, CPU_COREI7,
2981         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2982         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_CX16 | PTA_POPCNT},
2983       {"corei7-avx", PROCESSOR_COREI7_64, CPU_COREI7,
2984         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2985         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AVX
2986         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL},
2987       {"core-avx-i", 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
2990         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL | PTA_FSGSBASE
2991         | PTA_RDRND | PTA_F16C},
2992       {"core-avx2", PROCESSOR_COREI7_64, CPU_COREI7,
2993         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2994         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AVX | PTA_AVX2
2995         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL | PTA_FSGSBASE
2996         | PTA_RDRND | PTA_F16C | PTA_BMI | PTA_BMI2 | PTA_LZCNT
2997         | PTA_FMA | PTA_MOVBE},
2998       {"atom", PROCESSOR_ATOM, CPU_ATOM,
2999         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3000         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
3001       {"geode", PROCESSOR_GEODE, CPU_GEODE,
3002         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3003       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
3004       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3005       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3006       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
3007         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3008       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
3009         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3010       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
3011         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3012       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
3013         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3014       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
3015         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3016       {"x86-64", PROCESSOR_K8, CPU_K8,
3017         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
3018       {"k8", PROCESSOR_K8, CPU_K8,
3019         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3020         | PTA_SSE2 | PTA_NO_SAHF},
3021       {"k8-sse3", PROCESSOR_K8, CPU_K8,
3022         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3023         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3024       {"opteron", PROCESSOR_K8, CPU_K8,
3025         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3026         | PTA_SSE2 | PTA_NO_SAHF},
3027       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
3028         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3029         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3030       {"athlon64", PROCESSOR_K8, CPU_K8,
3031         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3032         | PTA_SSE2 | PTA_NO_SAHF},
3033       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
3034         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3035         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3036       {"athlon-fx", PROCESSOR_K8, CPU_K8,
3037         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3038         | PTA_SSE2 | PTA_NO_SAHF},
3039       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3040         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3041         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3042       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3043         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3044         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3045       {"bdver1", PROCESSOR_BDVER1, CPU_BDVER1,
3046         PTA_64BIT | PTA_MMX | PTA_PREFETCHW | PTA_SSE | PTA_SSE2
3047         | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM | PTA_SSSE3
3048         | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AES | PTA_PCLMUL | PTA_AVX
3049         | PTA_FMA4 | PTA_XOP | PTA_LWP},
3050       {"bdver2", PROCESSOR_BDVER2, CPU_BDVER2,
3051         PTA_64BIT | PTA_MMX | PTA_PREFETCHW | PTA_SSE | PTA_SSE2
3052         | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM | PTA_SSSE3
3053         | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AES | PTA_PCLMUL | PTA_AVX
3054         | PTA_FMA4 | PTA_XOP | PTA_LWP | PTA_BMI | PTA_TBM | PTA_F16C
3055         | PTA_FMA},
3056       {"btver1", PROCESSOR_BTVER1, CPU_GENERIC64,
3057         PTA_64BIT | PTA_MMX | PTA_PREFETCHW | PTA_SSE | PTA_SSE2
3058         | PTA_SSE3 | PTA_SSSE3 | PTA_SSE4A | PTA_ABM | PTA_CX16},
3059       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
3060         0 /* flags are only used for -march switch.  */ },
3061       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
3062         PTA_64BIT /* flags are only used for -march switch.  */ },
3063     };
3064
3065   /* -mrecip options.  */
3066   static struct
3067     {
3068       const char *string;           /* option name */
3069       unsigned int mask;            /* mask bits to set */
3070     }
3071   const recip_options[] =
3072     {
3073       { "all",       RECIP_MASK_ALL },
3074       { "none",      RECIP_MASK_NONE },
3075       { "div",       RECIP_MASK_DIV },
3076       { "sqrt",      RECIP_MASK_SQRT },
3077       { "vec-div",   RECIP_MASK_VEC_DIV },
3078       { "vec-sqrt",  RECIP_MASK_VEC_SQRT },
3079     };
3080
3081   int const pta_size = ARRAY_SIZE (processor_alias_table);
3082
3083   /* Set up prefix/suffix so the error messages refer to either the command
3084      line argument, or the attribute(target).  */
3085   if (main_args_p)
3086     {
3087       prefix = "-m";
3088       suffix = "";
3089       sw = "switch";
3090     }
3091   else
3092     {
3093       prefix = "option(\"";
3094       suffix = "\")";
3095       sw = "attribute";
3096     }
3097
3098 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3099   SUBTARGET_OVERRIDE_OPTIONS;
3100 #endif
3101
3102 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3103   SUBSUBTARGET_OVERRIDE_OPTIONS;
3104 #endif
3105
3106   if (TARGET_X32)
3107     ix86_isa_flags |= OPTION_MASK_ISA_64BIT;
3108
3109   /* -fPIC is the default for x86_64.  */
3110   if (TARGET_MACHO && TARGET_64BIT)
3111     flag_pic = 2;
3112
3113   /* Need to check -mtune=generic first.  */
3114   if (ix86_tune_string)
3115     {
3116       if (!strcmp (ix86_tune_string, "generic")
3117           || !strcmp (ix86_tune_string, "i686")
3118           /* As special support for cross compilers we read -mtune=native
3119              as -mtune=generic.  With native compilers we won't see the
3120              -mtune=native, as it was changed by the driver.  */
3121           || !strcmp (ix86_tune_string, "native"))
3122         {
3123           if (TARGET_64BIT)
3124             ix86_tune_string = "generic64";
3125           else
3126             ix86_tune_string = "generic32";
3127         }
3128       /* If this call is for setting the option attribute, allow the
3129          generic32/generic64 that was previously set.  */
3130       else if (!main_args_p
3131                && (!strcmp (ix86_tune_string, "generic32")
3132                    || !strcmp (ix86_tune_string, "generic64")))
3133         ;
3134       else if (!strncmp (ix86_tune_string, "generic", 7))
3135         error ("bad value (%s) for %stune=%s %s",
3136                ix86_tune_string, prefix, suffix, sw);
3137       else if (!strcmp (ix86_tune_string, "x86-64"))
3138         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated; use "
3139                  "%stune=k8%s or %stune=generic%s instead as appropriate",
3140                  prefix, suffix, prefix, suffix, prefix, suffix);
3141     }
3142   else
3143     {
3144       if (ix86_arch_string)
3145         ix86_tune_string = ix86_arch_string;
3146       if (!ix86_tune_string)
3147         {
3148           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
3149           ix86_tune_defaulted = 1;
3150         }
3151
3152       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
3153          need to use a sensible tune option.  */
3154       if (!strcmp (ix86_tune_string, "generic")
3155           || !strcmp (ix86_tune_string, "x86-64")
3156           || !strcmp (ix86_tune_string, "i686"))
3157         {
3158           if (TARGET_64BIT)
3159             ix86_tune_string = "generic64";
3160           else
3161             ix86_tune_string = "generic32";
3162         }
3163     }
3164
3165   if (ix86_stringop_alg == rep_prefix_8_byte && !TARGET_64BIT)
3166     {
3167       /* rep; movq isn't available in 32-bit code.  */
3168       error ("-mstringop-strategy=rep_8byte not supported for 32-bit code");
3169       ix86_stringop_alg = no_stringop;
3170     }
3171
3172   if (!ix86_arch_string)
3173     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
3174   else
3175     ix86_arch_specified = 1;
3176
3177   if (!global_options_set.x_ix86_abi)
3178     ix86_abi = DEFAULT_ABI;
3179
3180   if (global_options_set.x_ix86_cmodel)
3181     {
3182       switch (ix86_cmodel)
3183         {
3184         case CM_SMALL:
3185         case CM_SMALL_PIC:
3186           if (flag_pic)
3187             ix86_cmodel = CM_SMALL_PIC;
3188           if (!TARGET_64BIT)
3189             error ("code model %qs not supported in the %s bit mode",
3190                    "small", "32");
3191           break;
3192
3193         case CM_MEDIUM:
3194         case CM_MEDIUM_PIC:
3195           if (flag_pic)
3196             ix86_cmodel = CM_MEDIUM_PIC;
3197           if (!TARGET_64BIT)
3198             error ("code model %qs not supported in the %s bit mode",
3199                    "medium", "32");
3200           else if (TARGET_X32)
3201             error ("code model %qs not supported in x32 mode",
3202                    "medium");
3203           break;
3204
3205         case CM_LARGE:
3206         case CM_LARGE_PIC:
3207           if (flag_pic)
3208             ix86_cmodel = CM_LARGE_PIC;
3209           if (!TARGET_64BIT)
3210             error ("code model %qs not supported in the %s bit mode",
3211                    "large", "32");
3212           else if (TARGET_X32)
3213             error ("code model %qs not supported in x32 mode",
3214                    "large");
3215           break;
3216
3217         case CM_32:
3218           if (flag_pic)
3219             error ("code model %s does not support PIC mode", "32");
3220           if (TARGET_64BIT)
3221             error ("code model %qs not supported in the %s bit mode",
3222                    "32", "64");
3223           break;
3224
3225         case CM_KERNEL:
3226           if (flag_pic)
3227             {
3228               error ("code model %s does not support PIC mode", "kernel");
3229               ix86_cmodel = CM_32;
3230             }
3231           if (!TARGET_64BIT)
3232             error ("code model %qs not supported in the %s bit mode",
3233                    "kernel", "32");
3234           break;
3235
3236         default:
3237           gcc_unreachable ();
3238         }
3239     }
3240   else
3241     {
3242       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
3243          use of rip-relative addressing.  This eliminates fixups that
3244          would otherwise be needed if this object is to be placed in a
3245          DLL, and is essentially just as efficient as direct addressing.  */
3246       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
3247         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
3248       else if (TARGET_64BIT)
3249         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3250       else
3251         ix86_cmodel = CM_32;
3252     }
3253   if (TARGET_MACHO && ix86_asm_dialect == ASM_INTEL)
3254     {
3255       error ("-masm=intel not supported in this configuration");
3256       ix86_asm_dialect = ASM_ATT;
3257     }
3258   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
3259     sorry ("%i-bit mode not compiled in",
3260            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
3261
3262   for (i = 0; i < pta_size; i++)
3263     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
3264       {
3265         ix86_schedule = processor_alias_table[i].schedule;
3266         ix86_arch = processor_alias_table[i].processor;
3267         /* Default cpu tuning to the architecture.  */
3268         ix86_tune = ix86_arch;
3269
3270         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3271           error ("CPU you selected does not support x86-64 "
3272                  "instruction set");
3273
3274         if (processor_alias_table[i].flags & PTA_MMX
3275             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
3276           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
3277         if (processor_alias_table[i].flags & PTA_3DNOW
3278             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
3279           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
3280         if (processor_alias_table[i].flags & PTA_3DNOW_A
3281             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
3282           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
3283         if (processor_alias_table[i].flags & PTA_SSE
3284             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
3285           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
3286         if (processor_alias_table[i].flags & PTA_SSE2
3287             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
3288           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
3289         if (processor_alias_table[i].flags & PTA_SSE3
3290             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
3291           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
3292         if (processor_alias_table[i].flags & PTA_SSSE3
3293             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
3294           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
3295         if (processor_alias_table[i].flags & PTA_SSE4_1
3296             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
3297           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
3298         if (processor_alias_table[i].flags & PTA_SSE4_2
3299             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
3300           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
3301         if (processor_alias_table[i].flags & PTA_AVX
3302             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
3303           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
3304         if (processor_alias_table[i].flags & PTA_AVX2
3305             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX2))
3306           ix86_isa_flags |= OPTION_MASK_ISA_AVX2;
3307         if (processor_alias_table[i].flags & PTA_FMA
3308             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
3309           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
3310         if (processor_alias_table[i].flags & PTA_SSE4A
3311             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3312           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3313         if (processor_alias_table[i].flags & PTA_FMA4
3314             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3315           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3316         if (processor_alias_table[i].flags & PTA_XOP
3317             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3318           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3319         if (processor_alias_table[i].flags & PTA_LWP
3320             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3321           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3322         if (processor_alias_table[i].flags & PTA_ABM
3323             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3324           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3325         if (processor_alias_table[i].flags & PTA_BMI
3326             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_BMI))
3327           ix86_isa_flags |= OPTION_MASK_ISA_BMI;
3328         if (processor_alias_table[i].flags & (PTA_LZCNT | PTA_ABM)
3329             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LZCNT))
3330           ix86_isa_flags |= OPTION_MASK_ISA_LZCNT;
3331         if (processor_alias_table[i].flags & PTA_TBM
3332             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_TBM))
3333           ix86_isa_flags |= OPTION_MASK_ISA_TBM;
3334         if (processor_alias_table[i].flags & PTA_BMI2
3335             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_BMI2))
3336           ix86_isa_flags |= OPTION_MASK_ISA_BMI2;
3337         if (processor_alias_table[i].flags & PTA_CX16
3338             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3339           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3340         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3341             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3342           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3343         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3344             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3345           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3346         if (processor_alias_table[i].flags & PTA_MOVBE
3347             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3348           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3349         if (processor_alias_table[i].flags & PTA_AES
3350             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3351           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3352         if (processor_alias_table[i].flags & PTA_PCLMUL
3353             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3354           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3355         if (processor_alias_table[i].flags & PTA_FSGSBASE
3356             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FSGSBASE))
3357           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE;
3358         if (processor_alias_table[i].flags & PTA_RDRND
3359             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_RDRND))
3360           ix86_isa_flags |= OPTION_MASK_ISA_RDRND;
3361         if (processor_alias_table[i].flags & PTA_F16C
3362             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_F16C))
3363           ix86_isa_flags |= OPTION_MASK_ISA_F16C;
3364         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3365           x86_prefetch_sse = true;
3366         if (processor_alias_table[i].flags & PTA_PREFETCHW)
3367           x86_prefetchw = true;
3368
3369         break;
3370       }
3371
3372   if (!strcmp (ix86_arch_string, "generic"))
3373     error ("generic CPU can be used only for %stune=%s %s",
3374            prefix, suffix, sw);
3375   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3376     error ("bad value (%s) for %sarch=%s %s",
3377            ix86_arch_string, prefix, suffix, sw);
3378
3379   ix86_arch_mask = 1u << ix86_arch;
3380   for (i = 0; i < X86_ARCH_LAST; ++i)
3381     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3382
3383   for (i = 0; i < pta_size; i++)
3384     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3385       {
3386         ix86_schedule = processor_alias_table[i].schedule;
3387         ix86_tune = processor_alias_table[i].processor;
3388         if (TARGET_64BIT)
3389           {
3390             if (!(processor_alias_table[i].flags & PTA_64BIT))
3391               {
3392                 if (ix86_tune_defaulted)
3393                   {
3394                     ix86_tune_string = "x86-64";
3395                     for (i = 0; i < pta_size; i++)
3396                       if (! strcmp (ix86_tune_string,
3397                                     processor_alias_table[i].name))
3398                         break;
3399                     ix86_schedule = processor_alias_table[i].schedule;
3400                     ix86_tune = processor_alias_table[i].processor;
3401                   }
3402                 else
3403                   error ("CPU you selected does not support x86-64 "
3404                          "instruction set");
3405               }
3406           }
3407         else
3408           {
3409             /* Adjust tuning when compiling for 32-bit ABI.  */
3410             switch (ix86_tune)
3411               {
3412               case PROCESSOR_GENERIC64:
3413                 ix86_tune = PROCESSOR_GENERIC32;
3414                 ix86_schedule = CPU_PENTIUMPRO;
3415                 break;
3416
3417               case PROCESSOR_CORE2_64:
3418                 ix86_tune = PROCESSOR_CORE2_32;
3419                 break;
3420
3421               case PROCESSOR_COREI7_64:
3422                 ix86_tune = PROCESSOR_COREI7_32;
3423                 break;
3424
3425               default:
3426                 break;
3427               }
3428           }
3429         /* Intel CPUs have always interpreted SSE prefetch instructions as
3430            NOPs; so, we can enable SSE prefetch instructions even when
3431            -mtune (rather than -march) points us to a processor that has them.
3432            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3433            higher processors.  */
3434         if (TARGET_CMOV
3435             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3436           x86_prefetch_sse = true;
3437         break;
3438       }
3439
3440   if (ix86_tune_specified && i == pta_size)
3441     error ("bad value (%s) for %stune=%s %s",
3442            ix86_tune_string, prefix, suffix, sw);
3443
3444   ix86_tune_mask = 1u << ix86_tune;
3445   for (i = 0; i < X86_TUNE_LAST; ++i)
3446     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3447
3448 #ifndef USE_IX86_FRAME_POINTER
3449 #define USE_IX86_FRAME_POINTER 0
3450 #endif
3451
3452 #ifndef USE_X86_64_FRAME_POINTER
3453 #define USE_X86_64_FRAME_POINTER 0
3454 #endif
3455
3456   /* Set the default values for switches whose default depends on TARGET_64BIT
3457      in case they weren't overwritten by command line options.  */
3458   if (TARGET_64BIT)
3459     {
3460       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3461         flag_omit_frame_pointer = !USE_X86_64_FRAME_POINTER;
3462       if (flag_asynchronous_unwind_tables == 2)
3463         flag_unwind_tables = flag_asynchronous_unwind_tables = 1;
3464       if (flag_pcc_struct_return == 2)
3465         flag_pcc_struct_return = 0;
3466     }
3467   else
3468     {
3469       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3470         flag_omit_frame_pointer = !(USE_IX86_FRAME_POINTER || optimize_size);
3471       if (flag_asynchronous_unwind_tables == 2)
3472         flag_asynchronous_unwind_tables = !USE_IX86_FRAME_POINTER;
3473       if (flag_pcc_struct_return == 2)
3474         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
3475     }
3476
3477   if (optimize_size)
3478     ix86_cost = &ix86_size_cost;
3479   else
3480     ix86_cost = processor_target_table[ix86_tune].cost;
3481
3482   /* Arrange to set up i386_stack_locals for all functions.  */
3483   init_machine_status = ix86_init_machine_status;
3484
3485   /* Validate -mregparm= value.  */
3486   if (global_options_set.x_ix86_regparm)
3487     {
3488       if (TARGET_64BIT)
3489         warning (0, "-mregparm is ignored in 64-bit mode");
3490       if (ix86_regparm > REGPARM_MAX)
3491         {
3492           error ("-mregparm=%d is not between 0 and %d",
3493                  ix86_regparm, REGPARM_MAX);
3494           ix86_regparm = 0;
3495         }
3496     }
3497   if (TARGET_64BIT)
3498     ix86_regparm = REGPARM_MAX;
3499
3500   /* Default align_* from the processor table.  */
3501   if (align_loops == 0)
3502     {
3503       align_loops = processor_target_table[ix86_tune].align_loop;
3504       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3505     }
3506   if (align_jumps == 0)
3507     {
3508       align_jumps = processor_target_table[ix86_tune].align_jump;
3509       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3510     }
3511   if (align_functions == 0)
3512     {
3513       align_functions = processor_target_table[ix86_tune].align_func;
3514     }
3515
3516   /* Provide default for -mbranch-cost= value.  */
3517   if (!global_options_set.x_ix86_branch_cost)
3518     ix86_branch_cost = ix86_cost->branch_cost;
3519
3520   if (TARGET_64BIT)
3521     {
3522       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3523
3524       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3525          explicitly disable any of these.  In particular, disabling SSE and
3526          MMX for kernel code is extremely useful.  */
3527       if (!ix86_arch_specified)
3528       ix86_isa_flags
3529         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3530              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3531
3532       if (TARGET_RTD)
3533         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3534     }
3535   else
3536     {
3537       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3538
3539       if (!ix86_arch_specified)
3540       ix86_isa_flags
3541         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3542
3543       /* i386 ABI does not specify red zone.  It still makes sense to use it
3544          when programmer takes care to stack from being destroyed.  */
3545       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3546         target_flags |= MASK_NO_RED_ZONE;
3547     }
3548
3549   /* Keep nonleaf frame pointers.  */
3550   if (flag_omit_frame_pointer)
3551     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3552   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3553     flag_omit_frame_pointer = 1;
3554
3555   /* If we're doing fast math, we don't care about comparison order
3556      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3557   if (flag_finite_math_only)
3558     target_flags &= ~MASK_IEEE_FP;
3559
3560   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3561      since the insns won't need emulation.  */
3562   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3563     target_flags &= ~MASK_NO_FANCY_MATH_387;
3564
3565   /* Likewise, if the target doesn't have a 387, or we've specified
3566      software floating point, don't use 387 inline intrinsics.  */
3567   if (!TARGET_80387)
3568     target_flags |= MASK_NO_FANCY_MATH_387;
3569
3570   /* Turn on MMX builtins for -msse.  */
3571   if (TARGET_SSE)
3572     {
3573       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3574       x86_prefetch_sse = true;
3575     }
3576
3577   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3578   if (TARGET_SSE4_2 || TARGET_ABM)
3579     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3580
3581   /* Turn on lzcnt instruction for -mabm.  */
3582   if (TARGET_ABM)
3583     ix86_isa_flags |= OPTION_MASK_ISA_LZCNT & ~ix86_isa_flags_explicit;
3584
3585   /* Validate -mpreferred-stack-boundary= value or default it to
3586      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3587   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3588   if (global_options_set.x_ix86_preferred_stack_boundary_arg)
3589     {
3590       int min = (TARGET_64BIT ? 4 : 2);
3591       int max = (TARGET_SEH ? 4 : 12);
3592
3593       if (ix86_preferred_stack_boundary_arg < min
3594           || ix86_preferred_stack_boundary_arg > max)
3595         {
3596           if (min == max)
3597             error ("-mpreferred-stack-boundary is not supported "
3598                    "for this target");
3599           else
3600             error ("-mpreferred-stack-boundary=%d is not between %d and %d",
3601                    ix86_preferred_stack_boundary_arg, min, max);
3602         }
3603       else
3604         ix86_preferred_stack_boundary
3605           = (1 << ix86_preferred_stack_boundary_arg) * BITS_PER_UNIT;
3606     }
3607
3608   /* Set the default value for -mstackrealign.  */
3609   if (ix86_force_align_arg_pointer == -1)
3610     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3611
3612   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3613
3614   /* Validate -mincoming-stack-boundary= value or default it to
3615      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3616   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3617   if (global_options_set.x_ix86_incoming_stack_boundary_arg)
3618     {
3619       if (ix86_incoming_stack_boundary_arg < (TARGET_64BIT ? 4 : 2)
3620           || ix86_incoming_stack_boundary_arg > 12)
3621         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3622                ix86_incoming_stack_boundary_arg, TARGET_64BIT ? 4 : 2);
3623       else
3624         {
3625           ix86_user_incoming_stack_boundary
3626             = (1 << ix86_incoming_stack_boundary_arg) * BITS_PER_UNIT;
3627           ix86_incoming_stack_boundary
3628             = ix86_user_incoming_stack_boundary;
3629         }
3630     }
3631
3632   /* Accept -msseregparm only if at least SSE support is enabled.  */
3633   if (TARGET_SSEREGPARM
3634       && ! TARGET_SSE)
3635     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3636
3637   if (global_options_set.x_ix86_fpmath)
3638     {
3639       if (ix86_fpmath & FPMATH_SSE)
3640         {
3641           if (!TARGET_SSE)
3642             {
3643               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3644               ix86_fpmath = FPMATH_387;
3645             }
3646           else if ((ix86_fpmath & FPMATH_387) && !TARGET_80387)
3647             {
3648               warning (0, "387 instruction set disabled, using SSE arithmetics");
3649               ix86_fpmath = FPMATH_SSE;
3650             }
3651         }
3652     }
3653   else
3654     ix86_fpmath = TARGET_FPMATH_DEFAULT;
3655
3656   /* If the i387 is disabled, then do not return values in it. */
3657   if (!TARGET_80387)
3658     target_flags &= ~MASK_FLOAT_RETURNS;
3659
3660   /* Use external vectorized library in vectorizing intrinsics.  */
3661   if (global_options_set.x_ix86_veclibabi_type)
3662     switch (ix86_veclibabi_type)
3663       {
3664       case ix86_veclibabi_type_svml:
3665         ix86_veclib_handler = ix86_veclibabi_svml;
3666         break;
3667
3668       case ix86_veclibabi_type_acml:
3669         ix86_veclib_handler = ix86_veclibabi_acml;
3670         break;
3671
3672       default:
3673         gcc_unreachable ();
3674       }
3675
3676   if ((!USE_IX86_FRAME_POINTER
3677        || (x86_accumulate_outgoing_args & ix86_tune_mask))
3678       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3679       && !optimize_size)
3680     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3681
3682   /* ??? Unwind info is not correct around the CFG unless either a frame
3683      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
3684      unwind info generation to be aware of the CFG and propagating states
3685      around edges.  */
3686   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
3687        || flag_exceptions || flag_non_call_exceptions)
3688       && flag_omit_frame_pointer
3689       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3690     {
3691       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3692         warning (0, "unwind tables currently require either a frame pointer "
3693                  "or %saccumulate-outgoing-args%s for correctness",
3694                  prefix, suffix);
3695       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3696     }
3697
3698   /* If stack probes are required, the space used for large function
3699      arguments on the stack must also be probed, so enable
3700      -maccumulate-outgoing-args so this happens in the prologue.  */
3701   if (TARGET_STACK_PROBE
3702       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3703     {
3704       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3705         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
3706                  "for correctness", prefix, suffix);
3707       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3708     }
3709
3710   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
3711   {
3712     char *p;
3713     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
3714     p = strchr (internal_label_prefix, 'X');
3715     internal_label_prefix_len = p - internal_label_prefix;
3716     *p = '\0';
3717   }
3718
3719   /* When scheduling description is not available, disable scheduler pass
3720      so it won't slow down the compilation and make x87 code slower.  */
3721   if (!TARGET_SCHEDULE)
3722     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
3723
3724   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3725                          ix86_cost->simultaneous_prefetches,
3726                          global_options.x_param_values,
3727                          global_options_set.x_param_values);
3728   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, ix86_cost->prefetch_block,
3729                          global_options.x_param_values,
3730                          global_options_set.x_param_values);
3731   maybe_set_param_value (PARAM_L1_CACHE_SIZE, ix86_cost->l1_cache_size,
3732                          global_options.x_param_values,
3733                          global_options_set.x_param_values);
3734   maybe_set_param_value (PARAM_L2_CACHE_SIZE, ix86_cost->l2_cache_size,
3735                          global_options.x_param_values,
3736                          global_options_set.x_param_values);
3737
3738   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
3739   if (flag_prefetch_loop_arrays < 0
3740       && HAVE_prefetch
3741       && optimize >= 3
3742       && TARGET_SOFTWARE_PREFETCHING_BENEFICIAL)
3743     flag_prefetch_loop_arrays = 1;
3744
3745   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3746      can be optimized to ap = __builtin_next_arg (0).  */
3747   if (!TARGET_64BIT && !flag_split_stack)
3748     targetm.expand_builtin_va_start = NULL;
3749
3750   if (TARGET_64BIT)
3751     {
3752       ix86_gen_leave = gen_leave_rex64;
3753       ix86_gen_add3 = gen_adddi3;
3754       ix86_gen_sub3 = gen_subdi3;
3755       ix86_gen_sub3_carry = gen_subdi3_carry;
3756       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
3757       ix86_gen_monitor = gen_sse3_monitor64;
3758       ix86_gen_andsp = gen_anddi3;
3759       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_di;
3760       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probedi;
3761       ix86_gen_probe_stack_range = gen_probe_stack_rangedi;
3762     }
3763   else
3764     {
3765       ix86_gen_leave = gen_leave;
3766       ix86_gen_add3 = gen_addsi3;
3767       ix86_gen_sub3 = gen_subsi3;
3768       ix86_gen_sub3_carry = gen_subsi3_carry;
3769       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
3770       ix86_gen_monitor = gen_sse3_monitor;
3771       ix86_gen_andsp = gen_andsi3;
3772       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_si;
3773       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probesi;
3774       ix86_gen_probe_stack_range = gen_probe_stack_rangesi;
3775     }
3776
3777 #ifdef USE_IX86_CLD
3778   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
3779   if (!TARGET_64BIT)
3780     target_flags |= MASK_CLD & ~target_flags_explicit;
3781 #endif
3782
3783   if (!TARGET_64BIT && flag_pic)
3784     {
3785       if (flag_fentry > 0)
3786         sorry ("-mfentry isn%'t supported for 32-bit in combination "
3787                "with -fpic");
3788       flag_fentry = 0;
3789     }
3790   else if (TARGET_SEH)
3791     {
3792       if (flag_fentry == 0)
3793         sorry ("-mno-fentry isn%'t compatible with SEH");
3794       flag_fentry = 1;
3795     }
3796   else if (flag_fentry < 0)
3797    {
3798 #if defined(PROFILE_BEFORE_PROLOGUE)
3799      flag_fentry = 1;
3800 #else
3801      flag_fentry = 0;
3802 #endif
3803    }
3804
3805   if (TARGET_AVX)
3806     {
3807       /* When not optimize for size, enable vzeroupper optimization for
3808          TARGET_AVX with -fexpensive-optimizations and split 32-byte
3809          AVX unaligned load/store.  */
3810       if (!optimize_size)
3811         {
3812           if (flag_expensive_optimizations
3813               && !(target_flags_explicit & MASK_VZEROUPPER))
3814             target_flags |= MASK_VZEROUPPER;
3815           if ((x86_avx256_split_unaligned_load & ix86_tune_mask)
3816               && !(target_flags_explicit & MASK_AVX256_SPLIT_UNALIGNED_LOAD))
3817             target_flags |= MASK_AVX256_SPLIT_UNALIGNED_LOAD;
3818           if ((x86_avx256_split_unaligned_store & ix86_tune_mask)
3819               && !(target_flags_explicit & MASK_AVX256_SPLIT_UNALIGNED_STORE))
3820             target_flags |= MASK_AVX256_SPLIT_UNALIGNED_STORE;
3821           /* Enable 128-bit AVX instruction generation for the auto-vectorizer.  */
3822           if (TARGET_AVX128_OPTIMAL && !(target_flags_explicit & MASK_PREFER_AVX128))
3823             target_flags |= MASK_PREFER_AVX128;
3824         }
3825     }
3826   else
3827     {
3828       /* Disable vzeroupper pass if TARGET_AVX is disabled.  */
3829       target_flags &= ~MASK_VZEROUPPER;
3830     }
3831
3832   if (ix86_recip_name)
3833     {
3834       char *p = ASTRDUP (ix86_recip_name);
3835       char *q;
3836       unsigned int mask, i;
3837       bool invert;
3838
3839       while ((q = strtok (p, ",")) != NULL)
3840         {
3841           p = NULL;
3842           if (*q == '!')
3843             {
3844               invert = true;
3845               q++;
3846             }
3847           else
3848             invert = false;
3849
3850           if (!strcmp (q, "default"))
3851             mask = RECIP_MASK_ALL;
3852           else
3853             {
3854               for (i = 0; i < ARRAY_SIZE (recip_options); i++)
3855                 if (!strcmp (q, recip_options[i].string))
3856                   {
3857                     mask = recip_options[i].mask;
3858                     break;
3859                   }
3860
3861               if (i == ARRAY_SIZE (recip_options))
3862                 {
3863                   error ("unknown option for -mrecip=%s", q);
3864                   invert = false;
3865                   mask = RECIP_MASK_NONE;
3866                 }
3867             }
3868
3869           recip_mask_explicit |= mask;
3870           if (invert)
3871             recip_mask &= ~mask;
3872           else
3873             recip_mask |= mask;
3874         }
3875     }
3876
3877   if (TARGET_RECIP)
3878     recip_mask |= RECIP_MASK_ALL & ~recip_mask_explicit;
3879   else if (target_flags_explicit & MASK_RECIP)
3880     recip_mask &= ~(RECIP_MASK_ALL & ~recip_mask_explicit);
3881
3882   /* Save the initial options in case the user does function specific
3883      options.  */
3884   if (main_args_p)
3885     target_option_default_node = target_option_current_node
3886       = build_target_option_node ();
3887 }
3888
3889 /* Return TRUE if VAL is passed in register with 256bit AVX modes.  */
3890
3891 static bool
3892 function_pass_avx256_p (const_rtx val)
3893 {
3894   if (!val)
3895     return false;
3896
3897   if (REG_P (val) && VALID_AVX256_REG_MODE (GET_MODE (val)))
3898     return true;
3899
3900   if (GET_CODE (val) == PARALLEL)
3901     {
3902       int i;
3903       rtx r;
3904
3905       for (i = XVECLEN (val, 0) - 1; i >= 0; i--)
3906         {
3907           r = XVECEXP (val, 0, i);
3908           if (GET_CODE (r) == EXPR_LIST
3909               && XEXP (r, 0)
3910               && REG_P (XEXP (r, 0))
3911               && (GET_MODE (XEXP (r, 0)) == OImode
3912                   || VALID_AVX256_REG_MODE (GET_MODE (XEXP (r, 0)))))
3913             return true;
3914         }
3915     }
3916
3917   return false;
3918 }
3919
3920 /* Implement the TARGET_OPTION_OVERRIDE hook.  */
3921
3922 static void
3923 ix86_option_override (void)
3924 {
3925   ix86_option_override_internal (true);
3926 }
3927
3928 /* Update register usage after having seen the compiler flags.  */
3929
3930 static void
3931 ix86_conditional_register_usage (void)
3932 {
3933   int i;
3934   unsigned int j;
3935
3936   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3937     {
3938       if (fixed_regs[i] > 1)
3939         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
3940       if (call_used_regs[i] > 1)
3941         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
3942     }
3943
3944   /* The PIC register, if it exists, is fixed.  */
3945   j = PIC_OFFSET_TABLE_REGNUM;
3946   if (j != INVALID_REGNUM)
3947     fixed_regs[j] = call_used_regs[j] = 1;
3948
3949   /* The 64-bit MS_ABI changes the set of call-used registers.  */
3950   if (TARGET_64BIT_MS_ABI)
3951     {
3952       call_used_regs[SI_REG] = 0;
3953       call_used_regs[DI_REG] = 0;
3954       call_used_regs[XMM6_REG] = 0;
3955       call_used_regs[XMM7_REG] = 0;
3956       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3957         call_used_regs[i] = 0;
3958     }
3959
3960   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
3961      other call-clobbered regs for 64-bit.  */
3962   if (TARGET_64BIT)
3963     {
3964       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
3965
3966       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3967         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
3968             && call_used_regs[i])
3969           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
3970     }
3971
3972   /* If MMX is disabled, squash the registers.  */
3973   if (! TARGET_MMX)
3974     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3975       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
3976         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3977
3978   /* If SSE is disabled, squash the registers.  */
3979   if (! TARGET_SSE)
3980     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3981       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
3982         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3983
3984   /* If the FPU is disabled, squash the registers.  */
3985   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
3986     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3987       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
3988         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3989
3990   /* If 32-bit, squash the 64-bit registers.  */
3991   if (! TARGET_64BIT)
3992     {
3993       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
3994         reg_names[i] = "";
3995       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3996         reg_names[i] = "";
3997     }
3998 }
3999
4000 \f
4001 /* Save the current options */
4002
4003 static void
4004 ix86_function_specific_save (struct cl_target_option *ptr)
4005 {
4006   ptr->arch = ix86_arch;
4007   ptr->schedule = ix86_schedule;
4008   ptr->tune = ix86_tune;
4009   ptr->branch_cost = ix86_branch_cost;
4010   ptr->tune_defaulted = ix86_tune_defaulted;
4011   ptr->arch_specified = ix86_arch_specified;
4012   ptr->x_ix86_isa_flags_explicit = ix86_isa_flags_explicit;
4013   ptr->ix86_target_flags_explicit = target_flags_explicit;
4014   ptr->x_recip_mask_explicit = recip_mask_explicit;
4015
4016   /* The fields are char but the variables are not; make sure the
4017      values fit in the fields.  */
4018   gcc_assert (ptr->arch == ix86_arch);
4019   gcc_assert (ptr->schedule == ix86_schedule);
4020   gcc_assert (ptr->tune == ix86_tune);
4021   gcc_assert (ptr->branch_cost == ix86_branch_cost);
4022 }
4023
4024 /* Restore the current options */
4025
4026 static void
4027 ix86_function_specific_restore (struct cl_target_option *ptr)
4028 {
4029   enum processor_type old_tune = ix86_tune;
4030   enum processor_type old_arch = ix86_arch;
4031   unsigned int ix86_arch_mask, ix86_tune_mask;
4032   int i;
4033
4034   ix86_arch = (enum processor_type) ptr->arch;
4035   ix86_schedule = (enum attr_cpu) ptr->schedule;
4036   ix86_tune = (enum processor_type) ptr->tune;
4037   ix86_branch_cost = ptr->branch_cost;
4038   ix86_tune_defaulted = ptr->tune_defaulted;
4039   ix86_arch_specified = ptr->arch_specified;
4040   ix86_isa_flags_explicit = ptr->x_ix86_isa_flags_explicit;
4041   target_flags_explicit = ptr->ix86_target_flags_explicit;
4042   recip_mask_explicit = ptr->x_recip_mask_explicit;
4043
4044   /* Recreate the arch feature tests if the arch changed */
4045   if (old_arch != ix86_arch)
4046     {
4047       ix86_arch_mask = 1u << ix86_arch;
4048       for (i = 0; i < X86_ARCH_LAST; ++i)
4049         ix86_arch_features[i]
4050           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
4051     }
4052
4053   /* Recreate the tune optimization tests */
4054   if (old_tune != ix86_tune)
4055     {
4056       ix86_tune_mask = 1u << ix86_tune;
4057       for (i = 0; i < X86_TUNE_LAST; ++i)
4058         ix86_tune_features[i]
4059           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
4060     }
4061 }
4062
4063 /* Print the current options */
4064
4065 static void
4066 ix86_function_specific_print (FILE *file, int indent,
4067                               struct cl_target_option *ptr)
4068 {
4069   char *target_string
4070     = ix86_target_string (ptr->x_ix86_isa_flags, ptr->x_target_flags,
4071                           NULL, NULL, ptr->x_ix86_fpmath, false);
4072
4073   fprintf (file, "%*sarch = %d (%s)\n",
4074            indent, "",
4075            ptr->arch,
4076            ((ptr->arch < TARGET_CPU_DEFAULT_max)
4077             ? cpu_names[ptr->arch]
4078             : "<unknown>"));
4079
4080   fprintf (file, "%*stune = %d (%s)\n",
4081            indent, "",
4082            ptr->tune,
4083            ((ptr->tune < TARGET_CPU_DEFAULT_max)
4084             ? cpu_names[ptr->tune]
4085             : "<unknown>"));
4086
4087   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
4088
4089   if (target_string)
4090     {
4091       fprintf (file, "%*s%s\n", indent, "", target_string);
4092       free (target_string);
4093     }
4094 }
4095
4096 \f
4097 /* Inner function to process the attribute((target(...))), take an argument and
4098    set the current options from the argument. If we have a list, recursively go
4099    over the list.  */
4100
4101 static bool
4102 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[],
4103                                      struct gcc_options *enum_opts_set)
4104 {
4105   char *next_optstr;
4106   bool ret = true;
4107
4108 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
4109 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
4110 #define IX86_ATTR_ENUM(S,O)  { S, sizeof (S)-1, ix86_opt_enum, O, 0 }
4111 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
4112 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
4113
4114   enum ix86_opt_type
4115   {
4116     ix86_opt_unknown,
4117     ix86_opt_yes,
4118     ix86_opt_no,
4119     ix86_opt_str,
4120     ix86_opt_enum,
4121     ix86_opt_isa
4122   };
4123
4124   static const struct
4125   {
4126     const char *string;
4127     size_t len;
4128     enum ix86_opt_type type;
4129     int opt;
4130     int mask;
4131   } attrs[] = {
4132     /* isa options */
4133     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
4134     IX86_ATTR_ISA ("abm",       OPT_mabm),
4135     IX86_ATTR_ISA ("bmi",       OPT_mbmi),
4136     IX86_ATTR_ISA ("bmi2",      OPT_mbmi2),
4137     IX86_ATTR_ISA ("lzcnt",     OPT_mlzcnt),
4138     IX86_ATTR_ISA ("tbm",       OPT_mtbm),
4139     IX86_ATTR_ISA ("aes",       OPT_maes),
4140     IX86_ATTR_ISA ("avx",       OPT_mavx),
4141     IX86_ATTR_ISA ("avx2",      OPT_mavx2),
4142     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
4143     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
4144     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
4145     IX86_ATTR_ISA ("sse",       OPT_msse),
4146     IX86_ATTR_ISA ("sse2",      OPT_msse2),
4147     IX86_ATTR_ISA ("sse3",      OPT_msse3),
4148     IX86_ATTR_ISA ("sse4",      OPT_msse4),
4149     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
4150     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
4151     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
4152     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
4153     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
4154     IX86_ATTR_ISA ("fma",       OPT_mfma),
4155     IX86_ATTR_ISA ("xop",       OPT_mxop),
4156     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
4157     IX86_ATTR_ISA ("fsgsbase",  OPT_mfsgsbase),
4158     IX86_ATTR_ISA ("rdrnd",     OPT_mrdrnd),
4159     IX86_ATTR_ISA ("f16c",      OPT_mf16c),
4160
4161     /* enum options */
4162     IX86_ATTR_ENUM ("fpmath=",  OPT_mfpmath_),
4163
4164     /* string options */
4165     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
4166     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
4167
4168     /* flag options */
4169     IX86_ATTR_YES ("cld",
4170                    OPT_mcld,
4171                    MASK_CLD),
4172
4173     IX86_ATTR_NO ("fancy-math-387",
4174                   OPT_mfancy_math_387,
4175                   MASK_NO_FANCY_MATH_387),
4176
4177     IX86_ATTR_YES ("ieee-fp",
4178                    OPT_mieee_fp,
4179                    MASK_IEEE_FP),
4180
4181     IX86_ATTR_YES ("inline-all-stringops",
4182                    OPT_minline_all_stringops,
4183                    MASK_INLINE_ALL_STRINGOPS),
4184
4185     IX86_ATTR_YES ("inline-stringops-dynamically",
4186                    OPT_minline_stringops_dynamically,
4187                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
4188
4189     IX86_ATTR_NO ("align-stringops",
4190                   OPT_mno_align_stringops,
4191                   MASK_NO_ALIGN_STRINGOPS),
4192
4193     IX86_ATTR_YES ("recip",
4194                    OPT_mrecip,
4195                    MASK_RECIP),
4196
4197   };
4198
4199   /* If this is a list, recurse to get the options.  */
4200   if (TREE_CODE (args) == TREE_LIST)
4201     {
4202       bool ret = true;
4203
4204       for (; args; args = TREE_CHAIN (args))
4205         if (TREE_VALUE (args)
4206             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args),
4207                                                      p_strings, enum_opts_set))
4208           ret = false;
4209
4210       return ret;
4211     }
4212
4213   else if (TREE_CODE (args) != STRING_CST)
4214     gcc_unreachable ();
4215
4216   /* Handle multiple arguments separated by commas.  */
4217   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
4218
4219   while (next_optstr && *next_optstr != '\0')
4220     {
4221       char *p = next_optstr;
4222       char *orig_p = p;
4223       char *comma = strchr (next_optstr, ',');
4224       const char *opt_string;
4225       size_t len, opt_len;
4226       int opt;
4227       bool opt_set_p;
4228       char ch;
4229       unsigned i;
4230       enum ix86_opt_type type = ix86_opt_unknown;
4231       int mask = 0;
4232
4233       if (comma)
4234         {
4235           *comma = '\0';
4236           len = comma - next_optstr;
4237           next_optstr = comma + 1;
4238         }
4239       else
4240         {
4241           len = strlen (p);
4242           next_optstr = NULL;
4243         }
4244
4245       /* Recognize no-xxx.  */
4246       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
4247         {
4248           opt_set_p = false;
4249           p += 3;
4250           len -= 3;
4251         }
4252       else
4253         opt_set_p = true;
4254
4255       /* Find the option.  */
4256       ch = *p;
4257       opt = N_OPTS;
4258       for (i = 0; i < ARRAY_SIZE (attrs); i++)
4259         {
4260           type = attrs[i].type;
4261           opt_len = attrs[i].len;
4262           if (ch == attrs[i].string[0]
4263               && ((type != ix86_opt_str && type != ix86_opt_enum)
4264                   ? len == opt_len
4265                   : len > opt_len)
4266               && memcmp (p, attrs[i].string, opt_len) == 0)
4267             {
4268               opt = attrs[i].opt;
4269               mask = attrs[i].mask;
4270               opt_string = attrs[i].string;
4271               break;
4272             }
4273         }
4274
4275       /* Process the option.  */
4276       if (opt == N_OPTS)
4277         {
4278           error ("attribute(target(\"%s\")) is unknown", orig_p);
4279           ret = false;
4280         }
4281
4282       else if (type == ix86_opt_isa)
4283         {
4284           struct cl_decoded_option decoded;
4285
4286           generate_option (opt, NULL, opt_set_p, CL_TARGET, &decoded);
4287           ix86_handle_option (&global_options, &global_options_set,
4288                               &decoded, input_location);
4289         }
4290
4291       else if (type == ix86_opt_yes || type == ix86_opt_no)
4292         {
4293           if (type == ix86_opt_no)
4294             opt_set_p = !opt_set_p;
4295
4296           if (opt_set_p)
4297             target_flags |= mask;
4298           else
4299             target_flags &= ~mask;
4300         }
4301
4302       else if (type == ix86_opt_str)
4303         {
4304           if (p_strings[opt])
4305             {
4306               error ("option(\"%s\") was already specified", opt_string);
4307               ret = false;
4308             }
4309           else
4310             p_strings[opt] = xstrdup (p + opt_len);
4311         }
4312
4313       else if (type == ix86_opt_enum)
4314         {
4315           bool arg_ok;
4316           int value;
4317
4318           arg_ok = opt_enum_arg_to_value (opt, p + opt_len, &value, CL_TARGET);
4319           if (arg_ok)
4320             set_option (&global_options, enum_opts_set, opt, value,
4321                         p + opt_len, DK_UNSPECIFIED, input_location,
4322                         global_dc);
4323           else
4324             {
4325               error ("attribute(target(\"%s\")) is unknown", orig_p);
4326               ret = false;
4327             }
4328         }
4329
4330       else
4331         gcc_unreachable ();
4332     }
4333
4334   return ret;
4335 }
4336
4337 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
4338
4339 tree
4340 ix86_valid_target_attribute_tree (tree args)
4341 {
4342   const char *orig_arch_string = ix86_arch_string;
4343   const char *orig_tune_string = ix86_tune_string;
4344   enum fpmath_unit orig_fpmath_set = global_options_set.x_ix86_fpmath;
4345   int orig_tune_defaulted = ix86_tune_defaulted;
4346   int orig_arch_specified = ix86_arch_specified;
4347   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL };
4348   tree t = NULL_TREE;
4349   int i;
4350   struct cl_target_option *def
4351     = TREE_TARGET_OPTION (target_option_default_node);
4352   struct gcc_options enum_opts_set;
4353
4354   memset (&enum_opts_set, 0, sizeof (enum_opts_set));
4355
4356   /* Process each of the options on the chain.  */
4357   if (! ix86_valid_target_attribute_inner_p (args, option_strings,
4358                                              &enum_opts_set))
4359     return NULL_TREE;
4360
4361   /* If the changed options are different from the default, rerun
4362      ix86_option_override_internal, and then save the options away.
4363      The string options are are attribute options, and will be undone
4364      when we copy the save structure.  */
4365   if (ix86_isa_flags != def->x_ix86_isa_flags
4366       || target_flags != def->x_target_flags
4367       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
4368       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
4369       || enum_opts_set.x_ix86_fpmath)
4370     {
4371       /* If we are using the default tune= or arch=, undo the string assigned,
4372          and use the default.  */
4373       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
4374         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
4375       else if (!orig_arch_specified)
4376         ix86_arch_string = NULL;
4377
4378       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
4379         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
4380       else if (orig_tune_defaulted)
4381         ix86_tune_string = NULL;
4382
4383       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
4384       if (enum_opts_set.x_ix86_fpmath)
4385         global_options_set.x_ix86_fpmath = (enum fpmath_unit) 1;
4386       else if (!TARGET_64BIT && TARGET_SSE)
4387         {
4388           ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
4389           global_options_set.x_ix86_fpmath = (enum fpmath_unit) 1;
4390         }
4391
4392       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
4393       ix86_option_override_internal (false);
4394
4395       /* Add any builtin functions with the new isa if any.  */
4396       ix86_add_new_builtins (ix86_isa_flags);
4397
4398       /* Save the current options unless we are validating options for
4399          #pragma.  */
4400       t = build_target_option_node ();
4401
4402       ix86_arch_string = orig_arch_string;
4403       ix86_tune_string = orig_tune_string;
4404       global_options_set.x_ix86_fpmath = orig_fpmath_set;
4405
4406       /* Free up memory allocated to hold the strings */
4407       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
4408         free (option_strings[i]);
4409     }
4410
4411   return t;
4412 }
4413
4414 /* Hook to validate attribute((target("string"))).  */
4415
4416 static bool
4417 ix86_valid_target_attribute_p (tree fndecl,
4418                                tree ARG_UNUSED (name),
4419                                tree args,
4420                                int ARG_UNUSED (flags))
4421 {
4422   struct cl_target_option cur_target;
4423   bool ret = true;
4424   tree old_optimize = build_optimization_node ();
4425   tree new_target, new_optimize;
4426   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
4427
4428   /* If the function changed the optimization levels as well as setting target
4429      options, start with the optimizations specified.  */
4430   if (func_optimize && func_optimize != old_optimize)
4431     cl_optimization_restore (&global_options,
4432                              TREE_OPTIMIZATION (func_optimize));
4433
4434   /* The target attributes may also change some optimization flags, so update
4435      the optimization options if necessary.  */
4436   cl_target_option_save (&cur_target, &global_options);
4437   new_target = ix86_valid_target_attribute_tree (args);
4438   new_optimize = build_optimization_node ();
4439
4440   if (!new_target)
4441     ret = false;
4442
4443   else if (fndecl)
4444     {
4445       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
4446
4447       if (old_optimize != new_optimize)
4448         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
4449     }
4450
4451   cl_target_option_restore (&global_options, &cur_target);
4452
4453   if (old_optimize != new_optimize)
4454     cl_optimization_restore (&global_options,
4455                              TREE_OPTIMIZATION (old_optimize));
4456
4457   return ret;
4458 }
4459
4460 \f
4461 /* Hook to determine if one function can safely inline another.  */
4462
4463 static bool
4464 ix86_can_inline_p (tree caller, tree callee)
4465 {
4466   bool ret = false;
4467   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
4468   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
4469
4470   /* If callee has no option attributes, then it is ok to inline.  */
4471   if (!callee_tree)
4472     ret = true;
4473
4474   /* If caller has no option attributes, but callee does then it is not ok to
4475      inline.  */
4476   else if (!caller_tree)
4477     ret = false;
4478
4479   else
4480     {
4481       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
4482       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
4483
4484       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
4485          can inline a SSE2 function but a SSE2 function can't inline a SSE4
4486          function.  */
4487       if ((caller_opts->x_ix86_isa_flags & callee_opts->x_ix86_isa_flags)
4488           != callee_opts->x_ix86_isa_flags)
4489         ret = false;
4490
4491       /* See if we have the same non-isa options.  */
4492       else if (caller_opts->x_target_flags != callee_opts->x_target_flags)
4493         ret = false;
4494
4495       /* See if arch, tune, etc. are the same.  */
4496       else if (caller_opts->arch != callee_opts->arch)
4497         ret = false;
4498
4499       else if (caller_opts->tune != callee_opts->tune)
4500         ret = false;
4501
4502       else if (caller_opts->x_ix86_fpmath != callee_opts->x_ix86_fpmath)
4503         ret = false;
4504
4505       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4506         ret = false;
4507
4508       else
4509         ret = true;
4510     }
4511
4512   return ret;
4513 }
4514
4515 \f
4516 /* Remember the last target of ix86_set_current_function.  */
4517 static GTY(()) tree ix86_previous_fndecl;
4518
4519 /* Establish appropriate back-end context for processing the function
4520    FNDECL.  The argument might be NULL to indicate processing at top
4521    level, outside of any function scope.  */
4522 static void
4523 ix86_set_current_function (tree fndecl)
4524 {
4525   /* Only change the context if the function changes.  This hook is called
4526      several times in the course of compiling a function, and we don't want to
4527      slow things down too much or call target_reinit when it isn't safe.  */
4528   if (fndecl && fndecl != ix86_previous_fndecl)
4529     {
4530       tree old_tree = (ix86_previous_fndecl
4531                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4532                        : NULL_TREE);
4533
4534       tree new_tree = (fndecl
4535                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4536                        : NULL_TREE);
4537
4538       ix86_previous_fndecl = fndecl;
4539       if (old_tree == new_tree)
4540         ;
4541
4542       else if (new_tree)
4543         {
4544           cl_target_option_restore (&global_options,
4545                                     TREE_TARGET_OPTION (new_tree));
4546           target_reinit ();
4547         }
4548
4549       else if (old_tree)
4550         {
4551           struct cl_target_option *def
4552             = TREE_TARGET_OPTION (target_option_current_node);
4553
4554           cl_target_option_restore (&global_options, def);
4555           target_reinit ();
4556         }
4557     }
4558 }
4559
4560 \f
4561 /* Return true if this goes in large data/bss.  */
4562
4563 static bool
4564 ix86_in_large_data_p (tree exp)
4565 {
4566   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4567     return false;
4568
4569   /* Functions are never large data.  */
4570   if (TREE_CODE (exp) == FUNCTION_DECL)
4571     return false;
4572
4573   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4574     {
4575       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4576       if (strcmp (section, ".ldata") == 0
4577           || strcmp (section, ".lbss") == 0)
4578         return true;
4579       return false;
4580     }
4581   else
4582     {
4583       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4584
4585       /* If this is an incomplete type with size 0, then we can't put it
4586          in data because it might be too big when completed.  */
4587       if (!size || size > ix86_section_threshold)
4588         return true;
4589     }
4590
4591   return false;
4592 }
4593
4594 /* Switch to the appropriate section for output of DECL.
4595    DECL is either a `VAR_DECL' node or a constant of some sort.
4596    RELOC indicates whether forming the initial value of DECL requires
4597    link-time relocations.  */
4598
4599 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4600         ATTRIBUTE_UNUSED;
4601
4602 static section *
4603 x86_64_elf_select_section (tree decl, int reloc,
4604                            unsigned HOST_WIDE_INT align)
4605 {
4606   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4607       && ix86_in_large_data_p (decl))
4608     {
4609       const char *sname = NULL;
4610       unsigned int flags = SECTION_WRITE;
4611       switch (categorize_decl_for_section (decl, reloc))
4612         {
4613         case SECCAT_DATA:
4614           sname = ".ldata";
4615           break;
4616         case SECCAT_DATA_REL:
4617           sname = ".ldata.rel";
4618           break;
4619         case SECCAT_DATA_REL_LOCAL:
4620           sname = ".ldata.rel.local";
4621           break;
4622         case SECCAT_DATA_REL_RO:
4623           sname = ".ldata.rel.ro";
4624           break;
4625         case SECCAT_DATA_REL_RO_LOCAL:
4626           sname = ".ldata.rel.ro.local";
4627           break;
4628         case SECCAT_BSS:
4629           sname = ".lbss";
4630           flags |= SECTION_BSS;
4631           break;
4632         case SECCAT_RODATA:
4633         case SECCAT_RODATA_MERGE_STR:
4634         case SECCAT_RODATA_MERGE_STR_INIT:
4635         case SECCAT_RODATA_MERGE_CONST:
4636           sname = ".lrodata";
4637           flags = 0;
4638           break;
4639         case SECCAT_SRODATA:
4640         case SECCAT_SDATA:
4641         case SECCAT_SBSS:
4642           gcc_unreachable ();
4643         case SECCAT_TEXT:
4644         case SECCAT_TDATA:
4645         case SECCAT_TBSS:
4646           /* We don't split these for medium model.  Place them into
4647              default sections and hope for best.  */
4648           break;
4649         }
4650       if (sname)
4651         {
4652           /* We might get called with string constants, but get_named_section
4653              doesn't like them as they are not DECLs.  Also, we need to set
4654              flags in that case.  */
4655           if (!DECL_P (decl))
4656             return get_section (sname, flags, NULL);
4657           return get_named_section (decl, sname, reloc);
4658         }
4659     }
4660   return default_elf_select_section (decl, reloc, align);
4661 }
4662
4663 /* Build up a unique section name, expressed as a
4664    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4665    RELOC indicates whether the initial value of EXP requires
4666    link-time relocations.  */
4667
4668 static void ATTRIBUTE_UNUSED
4669 x86_64_elf_unique_section (tree decl, int reloc)
4670 {
4671   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4672       && ix86_in_large_data_p (decl))
4673     {
4674       const char *prefix = NULL;
4675       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4676       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4677
4678       switch (categorize_decl_for_section (decl, reloc))
4679         {
4680         case SECCAT_DATA:
4681         case SECCAT_DATA_REL:
4682         case SECCAT_DATA_REL_LOCAL:
4683         case SECCAT_DATA_REL_RO:
4684         case SECCAT_DATA_REL_RO_LOCAL:
4685           prefix = one_only ? ".ld" : ".ldata";
4686           break;
4687         case SECCAT_BSS:
4688           prefix = one_only ? ".lb" : ".lbss";
4689           break;
4690         case SECCAT_RODATA:
4691         case SECCAT_RODATA_MERGE_STR:
4692         case SECCAT_RODATA_MERGE_STR_INIT:
4693         case SECCAT_RODATA_MERGE_CONST:
4694           prefix = one_only ? ".lr" : ".lrodata";
4695           break;
4696         case SECCAT_SRODATA:
4697         case SECCAT_SDATA:
4698         case SECCAT_SBSS:
4699           gcc_unreachable ();
4700         case SECCAT_TEXT:
4701         case SECCAT_TDATA:
4702         case SECCAT_TBSS:
4703           /* We don't split these for medium model.  Place them into
4704              default sections and hope for best.  */
4705           break;
4706         }
4707       if (prefix)
4708         {
4709           const char *name, *linkonce;
4710           char *string;
4711
4712           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4713           name = targetm.strip_name_encoding (name);
4714
4715           /* If we're using one_only, then there needs to be a .gnu.linkonce
4716              prefix to the section name.  */
4717           linkonce = one_only ? ".gnu.linkonce" : "";
4718
4719           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4720
4721           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4722           return;
4723         }
4724     }
4725   default_unique_section (decl, reloc);
4726 }
4727
4728 #ifdef COMMON_ASM_OP
4729 /* This says how to output assembler code to declare an
4730    uninitialized external linkage data object.
4731
4732    For medium model x86-64 we need to use .largecomm opcode for
4733    large objects.  */
4734 void
4735 x86_elf_aligned_common (FILE *file,
4736                         const char *name, unsigned HOST_WIDE_INT size,
4737                         int align)
4738 {
4739   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4740       && size > (unsigned int)ix86_section_threshold)
4741     fputs (".largecomm\t", file);
4742   else
4743     fputs (COMMON_ASM_OP, file);
4744   assemble_name (file, name);
4745   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
4746            size, align / BITS_PER_UNIT);
4747 }
4748 #endif
4749
4750 /* Utility function for targets to use in implementing
4751    ASM_OUTPUT_ALIGNED_BSS.  */
4752
4753 void
4754 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
4755                         const char *name, unsigned HOST_WIDE_INT size,
4756                         int align)
4757 {
4758   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4759       && size > (unsigned int)ix86_section_threshold)
4760     switch_to_section (get_named_section (decl, ".lbss", 0));
4761   else
4762     switch_to_section (bss_section);
4763   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
4764 #ifdef ASM_DECLARE_OBJECT_NAME
4765   last_assemble_variable_decl = decl;
4766   ASM_DECLARE_OBJECT_NAME (file, name, decl);
4767 #else
4768   /* Standard thing is just output label for the object.  */
4769   ASM_OUTPUT_LABEL (file, name);
4770 #endif /* ASM_DECLARE_OBJECT_NAME */
4771   ASM_OUTPUT_SKIP (file, size ? size : 1);
4772 }
4773 \f
4774 /* Decide whether we must probe the stack before any space allocation
4775    on this target.  It's essentially TARGET_STACK_PROBE except when
4776    -fstack-check causes the stack to be already probed differently.  */
4777
4778 bool
4779 ix86_target_stack_probe (void)
4780 {
4781   /* Do not probe the stack twice if static stack checking is enabled.  */
4782   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
4783     return false;
4784
4785   return TARGET_STACK_PROBE;
4786 }
4787 \f
4788 /* Decide whether we can make a sibling call to a function.  DECL is the
4789    declaration of the function being targeted by the call and EXP is the
4790    CALL_EXPR representing the call.  */
4791
4792 static bool
4793 ix86_function_ok_for_sibcall (tree decl, tree exp)
4794 {
4795   tree type, decl_or_type;
4796   rtx a, b;
4797
4798   /* If we are generating position-independent code, we cannot sibcall
4799      optimize any indirect call, or a direct call to a global function,
4800      as the PLT requires %ebx be live. (Darwin does not have a PLT.)  */
4801   if (!TARGET_MACHO
4802       && !TARGET_64BIT
4803       && flag_pic
4804       && (!decl || !targetm.binds_local_p (decl)))
4805     return false;
4806
4807   /* If we need to align the outgoing stack, then sibcalling would
4808      unalign the stack, which may break the called function.  */
4809   if (ix86_minimum_incoming_stack_boundary (true)
4810       < PREFERRED_STACK_BOUNDARY)
4811     return false;
4812
4813   if (decl)
4814     {
4815       decl_or_type = decl;
4816       type = TREE_TYPE (decl);
4817     }
4818   else
4819     {
4820       /* We're looking at the CALL_EXPR, we need the type of the function.  */
4821       type = CALL_EXPR_FN (exp);                /* pointer expression */
4822       type = TREE_TYPE (type);                  /* pointer type */
4823       type = TREE_TYPE (type);                  /* function type */
4824       decl_or_type = type;
4825     }
4826
4827   /* Check that the return value locations are the same.  Like
4828      if we are returning floats on the 80387 register stack, we cannot
4829      make a sibcall from a function that doesn't return a float to a
4830      function that does or, conversely, from a function that does return
4831      a float to a function that doesn't; the necessary stack adjustment
4832      would not be executed.  This is also the place we notice
4833      differences in the return value ABI.  Note that it is ok for one
4834      of the functions to have void return type as long as the return
4835      value of the other is passed in a register.  */
4836   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
4837   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4838                            cfun->decl, false);
4839   if (STACK_REG_P (a) || STACK_REG_P (b))
4840     {
4841       if (!rtx_equal_p (a, b))
4842         return false;
4843     }
4844   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4845     {
4846       /* Disable sibcall if we need to generate vzeroupper after
4847          callee returns.  */
4848       if (TARGET_VZEROUPPER
4849           && cfun->machine->callee_return_avx256_p
4850           && !cfun->machine->caller_return_avx256_p)
4851         return false;
4852     }
4853   else if (!rtx_equal_p (a, b))
4854     return false;
4855
4856   if (TARGET_64BIT)
4857     {
4858       /* The SYSV ABI has more call-clobbered registers;
4859          disallow sibcalls from MS to SYSV.  */
4860       if (cfun->machine->call_abi == MS_ABI
4861           && ix86_function_type_abi (type) == SYSV_ABI)
4862         return false;
4863     }
4864   else
4865     {
4866       /* If this call is indirect, we'll need to be able to use a
4867          call-clobbered register for the address of the target function.
4868          Make sure that all such registers are not used for passing
4869          parameters.  Note that DLLIMPORT functions are indirect.  */
4870       if (!decl
4871           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
4872         {
4873           if (ix86_function_regparm (type, NULL) >= 3)
4874             {
4875               /* ??? Need to count the actual number of registers to be used,
4876                  not the possible number of registers.  Fix later.  */
4877               return false;
4878             }
4879         }
4880     }
4881
4882   /* Otherwise okay.  That also includes certain types of indirect calls.  */
4883   return true;
4884 }
4885
4886 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
4887    and "sseregparm" calling convention attributes;
4888    arguments as in struct attribute_spec.handler.  */
4889
4890 static tree
4891 ix86_handle_cconv_attribute (tree *node, tree name,
4892                                    tree args,
4893                                    int flags ATTRIBUTE_UNUSED,
4894                                    bool *no_add_attrs)
4895 {
4896   if (TREE_CODE (*node) != FUNCTION_TYPE
4897       && TREE_CODE (*node) != METHOD_TYPE
4898       && TREE_CODE (*node) != FIELD_DECL
4899       && TREE_CODE (*node) != TYPE_DECL)
4900     {
4901       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4902                name);
4903       *no_add_attrs = true;
4904       return NULL_TREE;
4905     }
4906
4907   /* Can combine regparm with all attributes but fastcall, and thiscall.  */
4908   if (is_attribute_p ("regparm", name))
4909     {
4910       tree cst;
4911
4912       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4913         {
4914           error ("fastcall and regparm attributes are not compatible");
4915         }
4916
4917       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4918         {
4919           error ("regparam and thiscall attributes are not compatible");
4920         }
4921
4922       cst = TREE_VALUE (args);
4923       if (TREE_CODE (cst) != INTEGER_CST)
4924         {
4925           warning (OPT_Wattributes,
4926                    "%qE attribute requires an integer constant argument",
4927                    name);
4928           *no_add_attrs = true;
4929         }
4930       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
4931         {
4932           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
4933                    name, REGPARM_MAX);
4934           *no_add_attrs = true;
4935         }
4936
4937       return NULL_TREE;
4938     }
4939
4940   if (TARGET_64BIT)
4941     {
4942       /* Do not warn when emulating the MS ABI.  */
4943       if ((TREE_CODE (*node) != FUNCTION_TYPE
4944            && TREE_CODE (*node) != METHOD_TYPE)
4945           || ix86_function_type_abi (*node) != MS_ABI)
4946         warning (OPT_Wattributes, "%qE attribute ignored",
4947                  name);
4948       *no_add_attrs = true;
4949       return NULL_TREE;
4950     }
4951
4952   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
4953   if (is_attribute_p ("fastcall", name))
4954     {
4955       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4956         {
4957           error ("fastcall and cdecl attributes are not compatible");
4958         }
4959       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4960         {
4961           error ("fastcall and stdcall attributes are not compatible");
4962         }
4963       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
4964         {
4965           error ("fastcall and regparm attributes are not compatible");
4966         }
4967       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4968         {
4969           error ("fastcall and thiscall attributes are not compatible");
4970         }
4971     }
4972
4973   /* Can combine stdcall with fastcall (redundant), regparm and
4974      sseregparm.  */
4975   else if (is_attribute_p ("stdcall", name))
4976     {
4977       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4978         {
4979           error ("stdcall and cdecl attributes are not compatible");
4980         }
4981       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4982         {
4983           error ("stdcall and fastcall attributes are not compatible");
4984         }
4985       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4986         {
4987           error ("stdcall and thiscall attributes are not compatible");
4988         }
4989     }
4990
4991   /* Can combine cdecl with regparm and sseregparm.  */
4992   else if (is_attribute_p ("cdecl", name))
4993     {
4994       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4995         {
4996           error ("stdcall and cdecl attributes are not compatible");
4997         }
4998       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4999         {
5000           error ("fastcall and cdecl attributes are not compatible");
5001         }
5002       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5003         {
5004           error ("cdecl and thiscall attributes are not compatible");
5005         }
5006     }
5007   else if (is_attribute_p ("thiscall", name))
5008     {
5009       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
5010         warning (OPT_Wattributes, "%qE attribute is used for none class-method",
5011                  name);
5012       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5013         {
5014           error ("stdcall and thiscall attributes are not compatible");
5015         }
5016       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5017         {
5018           error ("fastcall and thiscall attributes are not compatible");
5019         }
5020       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5021         {
5022           error ("cdecl and thiscall attributes are not compatible");
5023         }
5024     }
5025
5026   /* Can combine sseregparm with all attributes.  */
5027
5028   return NULL_TREE;
5029 }
5030
5031 /* The transactional memory builtins are implicitly regparm or fastcall
5032    depending on the ABI.  Override the generic do-nothing attribute that
5033    these builtins were declared with, and replace it with one of the two
5034    attributes that we expect elsewhere.  */
5035
5036 static tree
5037 ix86_handle_tm_regparm_attribute (tree *node, tree name ATTRIBUTE_UNUSED,
5038                                   tree args ATTRIBUTE_UNUSED,
5039                                   int flags ATTRIBUTE_UNUSED,
5040                                   bool *no_add_attrs)
5041 {
5042   tree alt;
5043
5044   /* In no case do we want to add the placeholder attribute.  */
5045   *no_add_attrs = true;
5046
5047   /* The 64-bit ABI is unchanged for transactional memory.  */
5048   if (TARGET_64BIT)
5049     return NULL_TREE;
5050
5051   /* ??? Is there a better way to validate 32-bit windows?  We have
5052      cfun->machine->call_abi, but that seems to be set only for 64-bit.  */
5053   if (CHECK_STACK_LIMIT > 0)
5054     alt = tree_cons (get_identifier ("fastcall"), NULL, NULL);
5055   else
5056     {
5057       alt = tree_cons (NULL, build_int_cst (NULL, 2), NULL);
5058       alt = tree_cons (get_identifier ("regparm"), alt, NULL);
5059     }
5060   decl_attributes (node, alt, flags);
5061
5062   return NULL_TREE;
5063 }
5064
5065 /* This function determines from TYPE the calling-convention.  */
5066
5067 unsigned int
5068 ix86_get_callcvt (const_tree type)
5069 {
5070   unsigned int ret = 0;
5071   bool is_stdarg;
5072   tree attrs;
5073
5074   if (TARGET_64BIT)
5075     return IX86_CALLCVT_CDECL;
5076
5077   attrs = TYPE_ATTRIBUTES (type);
5078   if (attrs != NULL_TREE)
5079     {
5080       if (lookup_attribute ("cdecl", attrs))
5081         ret |= IX86_CALLCVT_CDECL;
5082       else if (lookup_attribute ("stdcall", attrs))
5083         ret |= IX86_CALLCVT_STDCALL;
5084       else if (lookup_attribute ("fastcall", attrs))
5085         ret |= IX86_CALLCVT_FASTCALL;
5086       else if (lookup_attribute ("thiscall", attrs))
5087         ret |= IX86_CALLCVT_THISCALL;
5088
5089       /* Regparam isn't allowed for thiscall and fastcall.  */
5090       if ((ret & (IX86_CALLCVT_THISCALL | IX86_CALLCVT_FASTCALL)) == 0)
5091         {
5092           if (lookup_attribute ("regparm", attrs))
5093             ret |= IX86_CALLCVT_REGPARM;
5094           if (lookup_attribute ("sseregparm", attrs))
5095             ret |= IX86_CALLCVT_SSEREGPARM;
5096         }
5097
5098       if (IX86_BASE_CALLCVT(ret) != 0)
5099         return ret;
5100     }
5101
5102   is_stdarg = stdarg_p (type);
5103   if (TARGET_RTD && !is_stdarg)
5104     return IX86_CALLCVT_STDCALL | ret;
5105
5106   if (ret != 0
5107       || is_stdarg
5108       || TREE_CODE (type) != METHOD_TYPE
5109       || ix86_function_type_abi (type) != MS_ABI)
5110     return IX86_CALLCVT_CDECL | ret;
5111
5112   return IX86_CALLCVT_THISCALL;
5113 }
5114
5115 /* Return 0 if the attributes for two types are incompatible, 1 if they
5116    are compatible, and 2 if they are nearly compatible (which causes a
5117    warning to be generated).  */
5118
5119 static int
5120 ix86_comp_type_attributes (const_tree type1, const_tree type2)
5121 {
5122   unsigned int ccvt1, ccvt2;
5123
5124   if (TREE_CODE (type1) != FUNCTION_TYPE
5125       && TREE_CODE (type1) != METHOD_TYPE)
5126     return 1;
5127
5128   ccvt1 = ix86_get_callcvt (type1);
5129   ccvt2 = ix86_get_callcvt (type2);
5130   if (ccvt1 != ccvt2)
5131     return 0;
5132   if (ix86_function_regparm (type1, NULL)
5133       != ix86_function_regparm (type2, NULL))
5134     return 0;
5135
5136   return 1;
5137 }
5138 \f
5139 /* Return the regparm value for a function with the indicated TYPE and DECL.
5140    DECL may be NULL when calling function indirectly
5141    or considering a libcall.  */
5142
5143 static int
5144 ix86_function_regparm (const_tree type, const_tree decl)
5145 {
5146   tree attr;
5147   int regparm;
5148   unsigned int ccvt;
5149
5150   if (TARGET_64BIT)
5151     return (ix86_function_type_abi (type) == SYSV_ABI
5152             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
5153   ccvt = ix86_get_callcvt (type);
5154   regparm = ix86_regparm;
5155
5156   if ((ccvt & IX86_CALLCVT_REGPARM) != 0)
5157     {
5158       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
5159       if (attr)
5160         {
5161           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
5162           return regparm;
5163         }
5164     }
5165   else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
5166     return 2;
5167   else if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
5168     return 1;
5169
5170   /* Use register calling convention for local functions when possible.  */
5171   if (decl
5172       && TREE_CODE (decl) == FUNCTION_DECL
5173       && optimize
5174       && !(profile_flag && !flag_fentry))
5175     {
5176       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5177       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
5178       if (i && i->local && i->can_change_signature)
5179         {
5180           int local_regparm, globals = 0, regno;
5181
5182           /* Make sure no regparm register is taken by a
5183              fixed register variable.  */
5184           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
5185             if (fixed_regs[local_regparm])
5186               break;
5187
5188           /* We don't want to use regparm(3) for nested functions as
5189              these use a static chain pointer in the third argument.  */
5190           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
5191             local_regparm = 2;
5192
5193           /* In 32-bit mode save a register for the split stack.  */
5194           if (!TARGET_64BIT && local_regparm == 3 && flag_split_stack)
5195             local_regparm = 2;
5196
5197           /* Each fixed register usage increases register pressure,
5198              so less registers should be used for argument passing.
5199              This functionality can be overriden by an explicit
5200              regparm value.  */
5201           for (regno = 0; regno <= DI_REG; regno++)
5202             if (fixed_regs[regno])
5203               globals++;
5204
5205           local_regparm
5206             = globals < local_regparm ? local_regparm - globals : 0;
5207
5208           if (local_regparm > regparm)
5209             regparm = local_regparm;
5210         }
5211     }
5212
5213   return regparm;
5214 }
5215
5216 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
5217    DFmode (2) arguments in SSE registers for a function with the
5218    indicated TYPE and DECL.  DECL may be NULL when calling function
5219    indirectly or considering a libcall.  Otherwise return 0.  */
5220
5221 static int
5222 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
5223 {
5224   gcc_assert (!TARGET_64BIT);
5225
5226   /* Use SSE registers to pass SFmode and DFmode arguments if requested
5227      by the sseregparm attribute.  */
5228   if (TARGET_SSEREGPARM
5229       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
5230     {
5231       if (!TARGET_SSE)
5232         {
5233           if (warn)
5234             {
5235               if (decl)
5236                 error ("calling %qD with attribute sseregparm without "
5237                        "SSE/SSE2 enabled", decl);
5238               else
5239                 error ("calling %qT with attribute sseregparm without "
5240                        "SSE/SSE2 enabled", type);
5241             }
5242           return 0;
5243         }
5244
5245       return 2;
5246     }
5247
5248   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
5249      (and DFmode for SSE2) arguments in SSE registers.  */
5250   if (decl && TARGET_SSE_MATH && optimize
5251       && !(profile_flag && !flag_fentry))
5252     {
5253       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5254       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
5255       if (i && i->local && i->can_change_signature)
5256         return TARGET_SSE2 ? 2 : 1;
5257     }
5258
5259   return 0;
5260 }
5261
5262 /* Return true if EAX is live at the start of the function.  Used by
5263    ix86_expand_prologue to determine if we need special help before
5264    calling allocate_stack_worker.  */
5265
5266 static bool
5267 ix86_eax_live_at_start_p (void)
5268 {
5269   /* Cheat.  Don't bother working forward from ix86_function_regparm
5270      to the function type to whether an actual argument is located in
5271      eax.  Instead just look at cfg info, which is still close enough
5272      to correct at this point.  This gives false positives for broken
5273      functions that might use uninitialized data that happens to be
5274      allocated in eax, but who cares?  */
5275   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
5276 }
5277
5278 static bool
5279 ix86_keep_aggregate_return_pointer (tree fntype)
5280 {
5281   tree attr;
5282
5283   if (!TARGET_64BIT)
5284     {
5285       attr = lookup_attribute ("callee_pop_aggregate_return",
5286                                TYPE_ATTRIBUTES (fntype));
5287       if (attr)
5288         return (TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr))) == 0);
5289
5290       /* For 32-bit MS-ABI the default is to keep aggregate
5291          return pointer.  */
5292       if (ix86_function_type_abi (fntype) == MS_ABI)
5293         return true;
5294     }
5295   return KEEP_AGGREGATE_RETURN_POINTER != 0;
5296 }
5297
5298 /* Value is the number of bytes of arguments automatically
5299    popped when returning from a subroutine call.
5300    FUNDECL is the declaration node of the function (as a tree),
5301    FUNTYPE is the data type of the function (as a tree),
5302    or for a library call it is an identifier node for the subroutine name.
5303    SIZE is the number of bytes of arguments passed on the stack.
5304
5305    On the 80386, the RTD insn may be used to pop them if the number
5306      of args is fixed, but if the number is variable then the caller
5307      must pop them all.  RTD can't be used for library calls now
5308      because the library is compiled with the Unix compiler.
5309    Use of RTD is a selectable option, since it is incompatible with
5310    standard Unix calling sequences.  If the option is not selected,
5311    the caller must always pop the args.
5312
5313    The attribute stdcall is equivalent to RTD on a per module basis.  */
5314
5315 static int
5316 ix86_return_pops_args (tree fundecl, tree funtype, int size)
5317 {
5318   unsigned int ccvt;
5319
5320   /* None of the 64-bit ABIs pop arguments.  */
5321   if (TARGET_64BIT)
5322     return 0;
5323
5324   ccvt = ix86_get_callcvt (funtype);
5325
5326   if ((ccvt & (IX86_CALLCVT_STDCALL | IX86_CALLCVT_FASTCALL
5327                | IX86_CALLCVT_THISCALL)) != 0
5328       && ! stdarg_p (funtype))
5329     return size;
5330
5331   /* Lose any fake structure return argument if it is passed on the stack.  */
5332   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
5333       && !ix86_keep_aggregate_return_pointer (funtype))
5334     {
5335       int nregs = ix86_function_regparm (funtype, fundecl);
5336       if (nregs == 0)
5337         return GET_MODE_SIZE (Pmode);
5338     }
5339
5340   return 0;
5341 }
5342 \f
5343 /* Argument support functions.  */
5344
5345 /* Return true when register may be used to pass function parameters.  */
5346 bool
5347 ix86_function_arg_regno_p (int regno)
5348 {
5349   int i;
5350   const int *parm_regs;
5351
5352   if (!TARGET_64BIT)
5353     {
5354       if (TARGET_MACHO)
5355         return (regno < REGPARM_MAX
5356                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
5357       else
5358         return (regno < REGPARM_MAX
5359                 || (TARGET_MMX && MMX_REGNO_P (regno)
5360                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
5361                 || (TARGET_SSE && SSE_REGNO_P (regno)
5362                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
5363     }
5364
5365   if (TARGET_MACHO)
5366     {
5367       if (SSE_REGNO_P (regno) && TARGET_SSE)
5368         return true;
5369     }
5370   else
5371     {
5372       if (TARGET_SSE && SSE_REGNO_P (regno)
5373           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
5374         return true;
5375     }
5376
5377   /* TODO: The function should depend on current function ABI but
5378      builtins.c would need updating then. Therefore we use the
5379      default ABI.  */
5380
5381   /* RAX is used as hidden argument to va_arg functions.  */
5382   if (ix86_abi == SYSV_ABI && regno == AX_REG)
5383     return true;
5384
5385   if (ix86_abi == MS_ABI)
5386     parm_regs = x86_64_ms_abi_int_parameter_registers;
5387   else
5388     parm_regs = x86_64_int_parameter_registers;
5389   for (i = 0; i < (ix86_abi == MS_ABI
5390                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
5391     if (regno == parm_regs[i])
5392       return true;
5393   return false;
5394 }
5395
5396 /* Return if we do not know how to pass TYPE solely in registers.  */
5397
5398 static bool
5399 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
5400 {
5401   if (must_pass_in_stack_var_size_or_pad (mode, type))
5402     return true;
5403
5404   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
5405      The layout_type routine is crafty and tries to trick us into passing
5406      currently unsupported vector types on the stack by using TImode.  */
5407   return (!TARGET_64BIT && mode == TImode
5408           && type && TREE_CODE (type) != VECTOR_TYPE);
5409 }
5410
5411 /* It returns the size, in bytes, of the area reserved for arguments passed
5412    in registers for the function represented by fndecl dependent to the used
5413    abi format.  */
5414 int
5415 ix86_reg_parm_stack_space (const_tree fndecl)
5416 {
5417   enum calling_abi call_abi = SYSV_ABI;
5418   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
5419     call_abi = ix86_function_abi (fndecl);
5420   else
5421     call_abi = ix86_function_type_abi (fndecl);
5422   if (TARGET_64BIT && call_abi == MS_ABI)
5423     return 32;
5424   return 0;
5425 }
5426
5427 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
5428    call abi used.  */
5429 enum calling_abi
5430 ix86_function_type_abi (const_tree fntype)
5431 {
5432   if (fntype != NULL_TREE && TYPE_ATTRIBUTES (fntype) != NULL_TREE)
5433     {
5434       enum calling_abi abi = ix86_abi;
5435       if (abi == SYSV_ABI)
5436         {
5437           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
5438             abi = MS_ABI;
5439         }
5440       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
5441         abi = SYSV_ABI;
5442       return abi;
5443     }
5444   return ix86_abi;
5445 }
5446
5447 static bool
5448 ix86_function_ms_hook_prologue (const_tree fn)
5449 {
5450   if (fn && lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fn)))
5451     {
5452       if (decl_function_context (fn) != NULL_TREE)
5453         error_at (DECL_SOURCE_LOCATION (fn),
5454                   "ms_hook_prologue is not compatible with nested function");
5455       else
5456         return true;
5457     }
5458   return false;
5459 }
5460
5461 static enum calling_abi
5462 ix86_function_abi (const_tree fndecl)
5463 {
5464   if (! fndecl)
5465     return ix86_abi;
5466   return ix86_function_type_abi (TREE_TYPE (fndecl));
5467 }
5468
5469 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
5470    call abi used.  */
5471 enum calling_abi
5472 ix86_cfun_abi (void)
5473 {
5474   if (! cfun)
5475     return ix86_abi;
5476   return cfun->machine->call_abi;
5477 }
5478
5479 /* Write the extra assembler code needed to declare a function properly.  */
5480
5481 void
5482 ix86_asm_output_function_label (FILE *asm_out_file, const char *fname,
5483                                 tree decl)
5484 {
5485   bool is_ms_hook = ix86_function_ms_hook_prologue (decl);
5486
5487   if (is_ms_hook)
5488     {
5489       int i, filler_count = (TARGET_64BIT ? 32 : 16);
5490       unsigned int filler_cc = 0xcccccccc;
5491
5492       for (i = 0; i < filler_count; i += 4)
5493         fprintf (asm_out_file, ASM_LONG " %#x\n", filler_cc);
5494     }
5495
5496 #ifdef SUBTARGET_ASM_UNWIND_INIT
5497   SUBTARGET_ASM_UNWIND_INIT (asm_out_file);
5498 #endif
5499
5500   ASM_OUTPUT_LABEL (asm_out_file, fname);
5501
5502   /* Output magic byte marker, if hot-patch attribute is set.  */
5503   if (is_ms_hook)
5504     {
5505       if (TARGET_64BIT)
5506         {
5507           /* leaq [%rsp + 0], %rsp  */
5508           asm_fprintf (asm_out_file, ASM_BYTE
5509                        "0x48, 0x8d, 0xa4, 0x24, 0x00, 0x00, 0x00, 0x00\n");
5510         }
5511       else
5512         {
5513           /* movl.s %edi, %edi
5514              push   %ebp
5515              movl.s %esp, %ebp */
5516           asm_fprintf (asm_out_file, ASM_BYTE
5517                        "0x8b, 0xff, 0x55, 0x8b, 0xec\n");
5518         }
5519     }
5520 }
5521
5522 /* regclass.c  */
5523 extern void init_regs (void);
5524
5525 /* Implementation of call abi switching target hook. Specific to FNDECL
5526    the specific call register sets are set.  See also
5527    ix86_conditional_register_usage for more details.  */
5528 void
5529 ix86_call_abi_override (const_tree fndecl)
5530 {
5531   if (fndecl == NULL_TREE)
5532     cfun->machine->call_abi = ix86_abi;
5533   else
5534     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
5535 }
5536
5537 /* 64-bit MS and SYSV ABI have different set of call used registers.  Avoid
5538    expensive re-initialization of init_regs each time we switch function context
5539    since this is needed only during RTL expansion.  */
5540 static void
5541 ix86_maybe_switch_abi (void)
5542 {
5543   if (TARGET_64BIT &&
5544       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
5545     reinit_regs ();
5546 }
5547
5548 /* Initialize a variable CUM of type CUMULATIVE_ARGS
5549    for a call to a function whose data type is FNTYPE.
5550    For a library call, FNTYPE is 0.  */
5551
5552 void
5553 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
5554                       tree fntype,      /* tree ptr for function decl */
5555                       rtx libname,      /* SYMBOL_REF of library name or 0 */
5556                       tree fndecl,
5557                       int caller)
5558 {
5559   struct cgraph_local_info *i;
5560   tree fnret_type;
5561
5562   memset (cum, 0, sizeof (*cum));
5563
5564   /* Initialize for the current callee.  */
5565   if (caller)
5566     {
5567       cfun->machine->callee_pass_avx256_p = false;
5568       cfun->machine->callee_return_avx256_p = false;
5569     }
5570
5571   if (fndecl)
5572     {
5573       i = cgraph_local_info (fndecl);
5574       cum->call_abi = ix86_function_abi (fndecl);
5575       fnret_type = TREE_TYPE (TREE_TYPE (fndecl));
5576     }
5577   else
5578     {
5579       i = NULL;
5580       cum->call_abi = ix86_function_type_abi (fntype);
5581       if (fntype)
5582         fnret_type = TREE_TYPE (fntype);
5583       else
5584         fnret_type = NULL;
5585     }
5586
5587   if (TARGET_VZEROUPPER && fnret_type)
5588     {
5589       rtx fnret_value = ix86_function_value (fnret_type, fntype,
5590                                              false);
5591       if (function_pass_avx256_p (fnret_value))
5592         {
5593           /* The return value of this function uses 256bit AVX modes.  */
5594           if (caller)
5595             {
5596               cfun->machine->callee_return_avx256_p = true;
5597               cum->callee_return_avx256_p = true;
5598             }
5599           else
5600             cfun->machine->caller_return_avx256_p = true;
5601         }
5602     }
5603
5604   cum->caller = caller;
5605
5606   /* Set up the number of registers to use for passing arguments.  */
5607
5608   if (TARGET_64BIT && cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
5609     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
5610            "or subtarget optimization implying it");
5611   cum->nregs = ix86_regparm;
5612   if (TARGET_64BIT)
5613     {
5614       cum->nregs = (cum->call_abi == SYSV_ABI
5615                    ? X86_64_REGPARM_MAX
5616                    : X86_64_MS_REGPARM_MAX);
5617     }
5618   if (TARGET_SSE)
5619     {
5620       cum->sse_nregs = SSE_REGPARM_MAX;
5621       if (TARGET_64BIT)
5622         {
5623           cum->sse_nregs = (cum->call_abi == SYSV_ABI
5624                            ? X86_64_SSE_REGPARM_MAX
5625                            : X86_64_MS_SSE_REGPARM_MAX);
5626         }
5627     }
5628   if (TARGET_MMX)
5629     cum->mmx_nregs = MMX_REGPARM_MAX;
5630   cum->warn_avx = true;
5631   cum->warn_sse = true;
5632   cum->warn_mmx = true;
5633
5634   /* Because type might mismatch in between caller and callee, we need to
5635      use actual type of function for local calls.
5636      FIXME: cgraph_analyze can be told to actually record if function uses
5637      va_start so for local functions maybe_vaarg can be made aggressive
5638      helping K&R code.
5639      FIXME: once typesytem is fixed, we won't need this code anymore.  */
5640   if (i && i->local && i->can_change_signature)
5641     fntype = TREE_TYPE (fndecl);
5642   cum->maybe_vaarg = (fntype
5643                       ? (!prototype_p (fntype) || stdarg_p (fntype))
5644                       : !libname);
5645
5646   if (!TARGET_64BIT)
5647     {
5648       /* If there are variable arguments, then we won't pass anything
5649          in registers in 32-bit mode. */
5650       if (stdarg_p (fntype))
5651         {
5652           cum->nregs = 0;
5653           cum->sse_nregs = 0;
5654           cum->mmx_nregs = 0;
5655           cum->warn_avx = 0;
5656           cum->warn_sse = 0;
5657           cum->warn_mmx = 0;
5658           return;
5659         }
5660
5661       /* Use ecx and edx registers if function has fastcall attribute,
5662          else look for regparm information.  */
5663       if (fntype)
5664         {
5665           unsigned int ccvt = ix86_get_callcvt (fntype);
5666           if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
5667             {
5668               cum->nregs = 1;
5669               cum->fastcall = 1; /* Same first register as in fastcall.  */
5670             }
5671           else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
5672             {
5673               cum->nregs = 2;
5674               cum->fastcall = 1;
5675             }
5676           else
5677             cum->nregs = ix86_function_regparm (fntype, fndecl);
5678         }
5679
5680       /* Set up the number of SSE registers used for passing SFmode
5681          and DFmode arguments.  Warn for mismatching ABI.  */
5682       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
5683     }
5684 }
5685
5686 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
5687    But in the case of vector types, it is some vector mode.
5688
5689    When we have only some of our vector isa extensions enabled, then there
5690    are some modes for which vector_mode_supported_p is false.  For these
5691    modes, the generic vector support in gcc will choose some non-vector mode
5692    in order to implement the type.  By computing the natural mode, we'll
5693    select the proper ABI location for the operand and not depend on whatever
5694    the middle-end decides to do with these vector types.
5695
5696    The midde-end can't deal with the vector types > 16 bytes.  In this
5697    case, we return the original mode and warn ABI change if CUM isn't
5698    NULL.  */
5699
5700 static enum machine_mode
5701 type_natural_mode (const_tree type, const CUMULATIVE_ARGS *cum)
5702 {
5703   enum machine_mode mode = TYPE_MODE (type);
5704
5705   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5706     {
5707       HOST_WIDE_INT size = int_size_in_bytes (type);
5708       if ((size == 8 || size == 16 || size == 32)
5709           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5710           && TYPE_VECTOR_SUBPARTS (type) > 1)
5711         {
5712           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5713
5714           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5715             mode = MIN_MODE_VECTOR_FLOAT;
5716           else
5717             mode = MIN_MODE_VECTOR_INT;
5718
5719           /* Get the mode which has this inner mode and number of units.  */
5720           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5721             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5722                 && GET_MODE_INNER (mode) == innermode)
5723               {
5724                 if (size == 32 && !TARGET_AVX)
5725                   {
5726                     static bool warnedavx;
5727
5728                     if (cum
5729                         && !warnedavx
5730                         && cum->warn_avx)
5731                       {
5732                         warnedavx = true;
5733                         warning (0, "AVX vector argument without AVX "
5734                                  "enabled changes the ABI");
5735                       }
5736                     return TYPE_MODE (type);
5737                   }
5738                 else
5739                   return mode;
5740               }
5741
5742           gcc_unreachable ();
5743         }
5744     }
5745
5746   return mode;
5747 }
5748
5749 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5750    this may not agree with the mode that the type system has chosen for the
5751    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5752    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5753
5754 static rtx
5755 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5756                      unsigned int regno)
5757 {
5758   rtx tmp;
5759
5760   if (orig_mode != BLKmode)
5761     tmp = gen_rtx_REG (orig_mode, regno);
5762   else
5763     {
5764       tmp = gen_rtx_REG (mode, regno);
5765       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5766       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5767     }
5768
5769   return tmp;
5770 }
5771
5772 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5773    of this code is to classify each 8bytes of incoming argument by the register
5774    class and assign registers accordingly.  */
5775
5776 /* Return the union class of CLASS1 and CLASS2.
5777    See the x86-64 PS ABI for details.  */
5778
5779 static enum x86_64_reg_class
5780 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5781 {
5782   /* Rule #1: If both classes are equal, this is the resulting class.  */
5783   if (class1 == class2)
5784     return class1;
5785
5786   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5787      the other class.  */
5788   if (class1 == X86_64_NO_CLASS)
5789     return class2;
5790   if (class2 == X86_64_NO_CLASS)
5791     return class1;
5792
5793   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
5794   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
5795     return X86_64_MEMORY_CLASS;
5796
5797   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
5798   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
5799       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
5800     return X86_64_INTEGERSI_CLASS;
5801   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
5802       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
5803     return X86_64_INTEGER_CLASS;
5804
5805   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
5806      MEMORY is used.  */
5807   if (class1 == X86_64_X87_CLASS
5808       || class1 == X86_64_X87UP_CLASS
5809       || class1 == X86_64_COMPLEX_X87_CLASS
5810       || class2 == X86_64_X87_CLASS
5811       || class2 == X86_64_X87UP_CLASS
5812       || class2 == X86_64_COMPLEX_X87_CLASS)
5813     return X86_64_MEMORY_CLASS;
5814
5815   /* Rule #6: Otherwise class SSE is used.  */
5816   return X86_64_SSE_CLASS;
5817 }
5818
5819 /* Classify the argument of type TYPE and mode MODE.
5820    CLASSES will be filled by the register class used to pass each word
5821    of the operand.  The number of words is returned.  In case the parameter
5822    should be passed in memory, 0 is returned. As a special case for zero
5823    sized containers, classes[0] will be NO_CLASS and 1 is returned.
5824
5825    BIT_OFFSET is used internally for handling records and specifies offset
5826    of the offset in bits modulo 256 to avoid overflow cases.
5827
5828    See the x86-64 PS ABI for details.
5829 */
5830
5831 static int
5832 classify_argument (enum machine_mode mode, const_tree type,
5833                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
5834 {
5835   HOST_WIDE_INT bytes =
5836     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5837   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5838
5839   /* Variable sized entities are always passed/returned in memory.  */
5840   if (bytes < 0)
5841     return 0;
5842
5843   if (mode != VOIDmode
5844       && targetm.calls.must_pass_in_stack (mode, type))
5845     return 0;
5846
5847   if (type && AGGREGATE_TYPE_P (type))
5848     {
5849       int i;
5850       tree field;
5851       enum x86_64_reg_class subclasses[MAX_CLASSES];
5852
5853       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
5854       if (bytes > 32)
5855         return 0;
5856
5857       for (i = 0; i < words; i++)
5858         classes[i] = X86_64_NO_CLASS;
5859
5860       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
5861          signalize memory class, so handle it as special case.  */
5862       if (!words)
5863         {
5864           classes[0] = X86_64_NO_CLASS;
5865           return 1;
5866         }
5867
5868       /* Classify each field of record and merge classes.  */
5869       switch (TREE_CODE (type))
5870         {
5871         case RECORD_TYPE:
5872           /* And now merge the fields of structure.  */
5873           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5874             {
5875               if (TREE_CODE (field) == FIELD_DECL)
5876                 {
5877                   int num;
5878
5879                   if (TREE_TYPE (field) == error_mark_node)
5880                     continue;
5881
5882                   /* Bitfields are always classified as integer.  Handle them
5883                      early, since later code would consider them to be
5884                      misaligned integers.  */
5885                   if (DECL_BIT_FIELD (field))
5886                     {
5887                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5888                            i < ((int_bit_position (field) + (bit_offset % 64))
5889                                 + tree_low_cst (DECL_SIZE (field), 0)
5890                                 + 63) / 8 / 8; i++)
5891                         classes[i] =
5892                           merge_classes (X86_64_INTEGER_CLASS,
5893                                          classes[i]);
5894                     }
5895                   else
5896                     {
5897                       int pos;
5898
5899                       type = TREE_TYPE (field);
5900
5901                       /* Flexible array member is ignored.  */
5902                       if (TYPE_MODE (type) == BLKmode
5903                           && TREE_CODE (type) == ARRAY_TYPE
5904                           && TYPE_SIZE (type) == NULL_TREE
5905                           && TYPE_DOMAIN (type) != NULL_TREE
5906                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5907                               == NULL_TREE))
5908                         {
5909                           static bool warned;
5910
5911                           if (!warned && warn_psabi)
5912                             {
5913                               warned = true;
5914                               inform (input_location,
5915                                       "the ABI of passing struct with"
5916                                       " a flexible array member has"
5917                                       " changed in GCC 4.4");
5918                             }
5919                           continue;
5920                         }
5921                       num = classify_argument (TYPE_MODE (type), type,
5922                                                subclasses,
5923                                                (int_bit_position (field)
5924                                                 + bit_offset) % 256);
5925                       if (!num)
5926                         return 0;
5927                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5928                       for (i = 0; i < num && (i + pos) < words; i++)
5929                         classes[i + pos] =
5930                           merge_classes (subclasses[i], classes[i + pos]);
5931                     }
5932                 }
5933             }
5934           break;
5935
5936         case ARRAY_TYPE:
5937           /* Arrays are handled as small records.  */
5938           {
5939             int num;
5940             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
5941                                      TREE_TYPE (type), subclasses, bit_offset);
5942             if (!num)
5943               return 0;
5944
5945             /* The partial classes are now full classes.  */
5946             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
5947               subclasses[0] = X86_64_SSE_CLASS;
5948             if (subclasses[0] == X86_64_INTEGERSI_CLASS
5949                 && !((bit_offset % 64) == 0 && bytes == 4))
5950               subclasses[0] = X86_64_INTEGER_CLASS;
5951
5952             for (i = 0; i < words; i++)
5953               classes[i] = subclasses[i % num];
5954
5955             break;
5956           }
5957         case UNION_TYPE:
5958         case QUAL_UNION_TYPE:
5959           /* Unions are similar to RECORD_TYPE but offset is always 0.
5960              */
5961           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5962             {
5963               if (TREE_CODE (field) == FIELD_DECL)
5964                 {
5965                   int num;
5966
5967                   if (TREE_TYPE (field) == error_mark_node)
5968                     continue;
5969
5970                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
5971                                            TREE_TYPE (field), subclasses,
5972                                            bit_offset);
5973                   if (!num)
5974                     return 0;
5975                   for (i = 0; i < num; i++)
5976                     classes[i] = merge_classes (subclasses[i], classes[i]);
5977                 }
5978             }
5979           break;
5980
5981         default:
5982           gcc_unreachable ();
5983         }
5984
5985       if (words > 2)
5986         {
5987           /* When size > 16 bytes, if the first one isn't
5988              X86_64_SSE_CLASS or any other ones aren't
5989              X86_64_SSEUP_CLASS, everything should be passed in
5990              memory.  */
5991           if (classes[0] != X86_64_SSE_CLASS)
5992               return 0;
5993
5994           for (i = 1; i < words; i++)
5995             if (classes[i] != X86_64_SSEUP_CLASS)
5996               return 0;
5997         }
5998
5999       /* Final merger cleanup.  */
6000       for (i = 0; i < words; i++)
6001         {
6002           /* If one class is MEMORY, everything should be passed in
6003              memory.  */
6004           if (classes[i] == X86_64_MEMORY_CLASS)
6005             return 0;
6006
6007           /* The X86_64_SSEUP_CLASS should be always preceded by
6008              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
6009           if (classes[i] == X86_64_SSEUP_CLASS
6010               && classes[i - 1] != X86_64_SSE_CLASS
6011               && classes[i - 1] != X86_64_SSEUP_CLASS)
6012             {
6013               /* The first one should never be X86_64_SSEUP_CLASS.  */
6014               gcc_assert (i != 0);
6015               classes[i] = X86_64_SSE_CLASS;
6016             }
6017
6018           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
6019                everything should be passed in memory.  */
6020           if (classes[i] == X86_64_X87UP_CLASS
6021               && (classes[i - 1] != X86_64_X87_CLASS))
6022             {
6023               static bool warned;
6024
6025               /* The first one should never be X86_64_X87UP_CLASS.  */
6026               gcc_assert (i != 0);
6027               if (!warned && warn_psabi)
6028                 {
6029                   warned = true;
6030                   inform (input_location,
6031                           "the ABI of passing union with long double"
6032                           " has changed in GCC 4.4");
6033                 }
6034               return 0;
6035             }
6036         }
6037       return words;
6038     }
6039
6040   /* Compute alignment needed.  We align all types to natural boundaries with
6041      exception of XFmode that is aligned to 64bits.  */
6042   if (mode != VOIDmode && mode != BLKmode)
6043     {
6044       int mode_alignment = GET_MODE_BITSIZE (mode);
6045
6046       if (mode == XFmode)
6047         mode_alignment = 128;
6048       else if (mode == XCmode)
6049         mode_alignment = 256;
6050       if (COMPLEX_MODE_P (mode))
6051         mode_alignment /= 2;
6052       /* Misaligned fields are always returned in memory.  */
6053       if (bit_offset % mode_alignment)
6054         return 0;
6055     }
6056
6057   /* for V1xx modes, just use the base mode */
6058   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
6059       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
6060     mode = GET_MODE_INNER (mode);
6061
6062   /* Classification of atomic types.  */
6063   switch (mode)
6064     {
6065     case SDmode:
6066     case DDmode:
6067       classes[0] = X86_64_SSE_CLASS;
6068       return 1;
6069     case TDmode:
6070       classes[0] = X86_64_SSE_CLASS;
6071       classes[1] = X86_64_SSEUP_CLASS;
6072       return 2;
6073     case DImode:
6074     case SImode:
6075     case HImode:
6076     case QImode:
6077     case CSImode:
6078     case CHImode:
6079     case CQImode:
6080       {
6081         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
6082
6083         if (size <= 32)
6084           {
6085             classes[0] = X86_64_INTEGERSI_CLASS;
6086             return 1;
6087           }
6088         else if (size <= 64)
6089           {
6090             classes[0] = X86_64_INTEGER_CLASS;
6091             return 1;
6092           }
6093         else if (size <= 64+32)
6094           {
6095             classes[0] = X86_64_INTEGER_CLASS;
6096             classes[1] = X86_64_INTEGERSI_CLASS;
6097             return 2;
6098           }
6099         else if (size <= 64+64)
6100           {
6101             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6102             return 2;
6103           }
6104         else
6105           gcc_unreachable ();
6106       }
6107     case CDImode:
6108     case TImode:
6109       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6110       return 2;
6111     case COImode:
6112     case OImode:
6113       /* OImode shouldn't be used directly.  */
6114       gcc_unreachable ();
6115     case CTImode:
6116       return 0;
6117     case SFmode:
6118       if (!(bit_offset % 64))
6119         classes[0] = X86_64_SSESF_CLASS;
6120       else
6121         classes[0] = X86_64_SSE_CLASS;
6122       return 1;
6123     case DFmode:
6124       classes[0] = X86_64_SSEDF_CLASS;
6125       return 1;
6126     case XFmode:
6127       classes[0] = X86_64_X87_CLASS;
6128       classes[1] = X86_64_X87UP_CLASS;
6129       return 2;
6130     case TFmode:
6131       classes[0] = X86_64_SSE_CLASS;
6132       classes[1] = X86_64_SSEUP_CLASS;
6133       return 2;
6134     case SCmode:
6135       classes[0] = X86_64_SSE_CLASS;
6136       if (!(bit_offset % 64))
6137         return 1;
6138       else
6139         {
6140           static bool warned;
6141
6142           if (!warned && warn_psabi)
6143             {
6144               warned = true;
6145               inform (input_location,
6146                       "the ABI of passing structure with complex float"
6147                       " member has changed in GCC 4.4");
6148             }
6149           classes[1] = X86_64_SSESF_CLASS;
6150           return 2;
6151         }
6152     case DCmode:
6153       classes[0] = X86_64_SSEDF_CLASS;
6154       classes[1] = X86_64_SSEDF_CLASS;
6155       return 2;
6156     case XCmode:
6157       classes[0] = X86_64_COMPLEX_X87_CLASS;
6158       return 1;
6159     case TCmode:
6160       /* This modes is larger than 16 bytes.  */
6161       return 0;
6162     case V8SFmode:
6163     case V8SImode:
6164     case V32QImode:
6165     case V16HImode:
6166     case V4DFmode:
6167     case V4DImode:
6168       classes[0] = X86_64_SSE_CLASS;
6169       classes[1] = X86_64_SSEUP_CLASS;
6170       classes[2] = X86_64_SSEUP_CLASS;
6171       classes[3] = X86_64_SSEUP_CLASS;
6172       return 4;
6173     case V4SFmode:
6174     case V4SImode:
6175     case V16QImode:
6176     case V8HImode:
6177     case V2DFmode:
6178     case V2DImode:
6179       classes[0] = X86_64_SSE_CLASS;
6180       classes[1] = X86_64_SSEUP_CLASS;
6181       return 2;
6182     case V1TImode:
6183     case V1DImode:
6184     case V2SFmode:
6185     case V2SImode:
6186     case V4HImode:
6187     case V8QImode:
6188       classes[0] = X86_64_SSE_CLASS;
6189       return 1;
6190     case BLKmode:
6191     case VOIDmode:
6192       return 0;
6193     default:
6194       gcc_assert (VECTOR_MODE_P (mode));
6195
6196       if (bytes > 16)
6197         return 0;
6198
6199       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
6200
6201       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
6202         classes[0] = X86_64_INTEGERSI_CLASS;
6203       else
6204         classes[0] = X86_64_INTEGER_CLASS;
6205       classes[1] = X86_64_INTEGER_CLASS;
6206       return 1 + (bytes > 8);
6207     }
6208 }
6209
6210 /* Examine the argument and return set number of register required in each
6211    class.  Return 0 iff parameter should be passed in memory.  */
6212 static int
6213 examine_argument (enum machine_mode mode, const_tree type, int in_return,
6214                   int *int_nregs, int *sse_nregs)
6215 {
6216   enum x86_64_reg_class regclass[MAX_CLASSES];
6217   int n = classify_argument (mode, type, regclass, 0);
6218
6219   *int_nregs = 0;
6220   *sse_nregs = 0;
6221   if (!n)
6222     return 0;
6223   for (n--; n >= 0; n--)
6224     switch (regclass[n])
6225       {
6226       case X86_64_INTEGER_CLASS:
6227       case X86_64_INTEGERSI_CLASS:
6228         (*int_nregs)++;
6229         break;
6230       case X86_64_SSE_CLASS:
6231       case X86_64_SSESF_CLASS:
6232       case X86_64_SSEDF_CLASS:
6233         (*sse_nregs)++;
6234         break;
6235       case X86_64_NO_CLASS:
6236       case X86_64_SSEUP_CLASS:
6237         break;
6238       case X86_64_X87_CLASS:
6239       case X86_64_X87UP_CLASS:
6240         if (!in_return)
6241           return 0;
6242         break;
6243       case X86_64_COMPLEX_X87_CLASS:
6244         return in_return ? 2 : 0;
6245       case X86_64_MEMORY_CLASS:
6246         gcc_unreachable ();
6247       }
6248   return 1;
6249 }
6250
6251 /* Construct container for the argument used by GCC interface.  See
6252    FUNCTION_ARG for the detailed description.  */
6253
6254 static rtx
6255 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
6256                      const_tree type, int in_return, int nintregs, int nsseregs,
6257                      const int *intreg, int sse_regno)
6258 {
6259   /* The following variables hold the static issued_error state.  */
6260   static bool issued_sse_arg_error;
6261   static bool issued_sse_ret_error;
6262   static bool issued_x87_ret_error;
6263
6264   enum machine_mode tmpmode;
6265   int bytes =
6266     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
6267   enum x86_64_reg_class regclass[MAX_CLASSES];
6268   int n;
6269   int i;
6270   int nexps = 0;
6271   int needed_sseregs, needed_intregs;
6272   rtx exp[MAX_CLASSES];
6273   rtx ret;
6274
6275   n = classify_argument (mode, type, regclass, 0);
6276   if (!n)
6277     return NULL;
6278   if (!examine_argument (mode, type, in_return, &needed_intregs,
6279                          &needed_sseregs))
6280     return NULL;
6281   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
6282     return NULL;
6283
6284   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
6285      some less clueful developer tries to use floating-point anyway.  */
6286   if (needed_sseregs && !TARGET_SSE)
6287     {
6288       if (in_return)
6289         {
6290           if (!issued_sse_ret_error)
6291             {
6292               error ("SSE register return with SSE disabled");
6293               issued_sse_ret_error = true;
6294             }
6295         }
6296       else if (!issued_sse_arg_error)
6297         {
6298           error ("SSE register argument with SSE disabled");
6299           issued_sse_arg_error = true;
6300         }
6301       return NULL;
6302     }
6303
6304   /* Likewise, error if the ABI requires us to return values in the
6305      x87 registers and the user specified -mno-80387.  */
6306   if (!TARGET_FLOAT_RETURNS_IN_80387 && in_return)
6307     for (i = 0; i < n; i++)
6308       if (regclass[i] == X86_64_X87_CLASS
6309           || regclass[i] == X86_64_X87UP_CLASS
6310           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
6311         {
6312           if (!issued_x87_ret_error)
6313             {
6314               error ("x87 register return with x87 disabled");
6315               issued_x87_ret_error = true;
6316             }
6317           return NULL;
6318         }
6319
6320   /* First construct simple cases.  Avoid SCmode, since we want to use
6321      single register to pass this type.  */
6322   if (n == 1 && mode != SCmode)
6323     switch (regclass[0])
6324       {
6325       case X86_64_INTEGER_CLASS:
6326       case X86_64_INTEGERSI_CLASS:
6327         return gen_rtx_REG (mode, intreg[0]);
6328       case X86_64_SSE_CLASS:
6329       case X86_64_SSESF_CLASS:
6330       case X86_64_SSEDF_CLASS:
6331         if (mode != BLKmode)
6332           return gen_reg_or_parallel (mode, orig_mode,
6333                                       SSE_REGNO (sse_regno));
6334         break;
6335       case X86_64_X87_CLASS:
6336       case X86_64_COMPLEX_X87_CLASS:
6337         return gen_rtx_REG (mode, FIRST_STACK_REG);
6338       case X86_64_NO_CLASS:
6339         /* Zero sized array, struct or class.  */
6340         return NULL;
6341       default:
6342         gcc_unreachable ();
6343       }
6344   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
6345       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
6346     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6347   if (n == 4
6348       && regclass[0] == X86_64_SSE_CLASS
6349       && regclass[1] == X86_64_SSEUP_CLASS
6350       && regclass[2] == X86_64_SSEUP_CLASS
6351       && regclass[3] == X86_64_SSEUP_CLASS
6352       && mode != BLKmode)
6353     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6354
6355   if (n == 2
6356       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
6357     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
6358   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
6359       && regclass[1] == X86_64_INTEGER_CLASS
6360       && (mode == CDImode || mode == TImode || mode == TFmode)
6361       && intreg[0] + 1 == intreg[1])
6362     return gen_rtx_REG (mode, intreg[0]);
6363
6364   /* Otherwise figure out the entries of the PARALLEL.  */
6365   for (i = 0; i < n; i++)
6366     {
6367       int pos;
6368
6369       switch (regclass[i])
6370         {
6371           case X86_64_NO_CLASS:
6372             break;
6373           case X86_64_INTEGER_CLASS:
6374           case X86_64_INTEGERSI_CLASS:
6375             /* Merge TImodes on aligned occasions here too.  */
6376             if (i * 8 + 8 > bytes)
6377               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
6378             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
6379               tmpmode = SImode;
6380             else
6381               tmpmode = DImode;
6382             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
6383             if (tmpmode == BLKmode)
6384               tmpmode = DImode;
6385             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6386                                                gen_rtx_REG (tmpmode, *intreg),
6387                                                GEN_INT (i*8));
6388             intreg++;
6389             break;
6390           case X86_64_SSESF_CLASS:
6391             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6392                                                gen_rtx_REG (SFmode,
6393                                                             SSE_REGNO (sse_regno)),
6394                                                GEN_INT (i*8));
6395             sse_regno++;
6396             break;
6397           case X86_64_SSEDF_CLASS:
6398             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6399                                                gen_rtx_REG (DFmode,
6400                                                             SSE_REGNO (sse_regno)),
6401                                                GEN_INT (i*8));
6402             sse_regno++;
6403             break;
6404           case X86_64_SSE_CLASS:
6405             pos = i;
6406             switch (n)
6407               {
6408               case 1:
6409                 tmpmode = DImode;
6410                 break;
6411               case 2:
6412                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
6413                   {
6414                     tmpmode = TImode;
6415                     i++;
6416                   }
6417                 else
6418                   tmpmode = DImode;
6419                 break;
6420               case 4:
6421                 gcc_assert (i == 0
6422                             && regclass[1] == X86_64_SSEUP_CLASS
6423                             && regclass[2] == X86_64_SSEUP_CLASS
6424                             && regclass[3] == X86_64_SSEUP_CLASS);
6425                 tmpmode = OImode;
6426                 i += 3;
6427                 break;
6428               default:
6429                 gcc_unreachable ();
6430               }
6431             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6432                                                gen_rtx_REG (tmpmode,
6433                                                             SSE_REGNO (sse_regno)),
6434                                                GEN_INT (pos*8));
6435             sse_regno++;
6436             break;
6437           default:
6438             gcc_unreachable ();
6439         }
6440     }
6441
6442   /* Empty aligned struct, union or class.  */
6443   if (nexps == 0)
6444     return NULL;
6445
6446   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
6447   for (i = 0; i < nexps; i++)
6448     XVECEXP (ret, 0, i) = exp [i];
6449   return ret;
6450 }
6451
6452 /* Update the data in CUM to advance over an argument of mode MODE
6453    and data type TYPE.  (TYPE is null for libcalls where that information
6454    may not be available.)  */
6455
6456 static void
6457 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6458                          const_tree type, HOST_WIDE_INT bytes,
6459                          HOST_WIDE_INT words)
6460 {
6461   switch (mode)
6462     {
6463     default:
6464       break;
6465
6466     case BLKmode:
6467       if (bytes < 0)
6468         break;
6469       /* FALLTHRU */
6470
6471     case DImode:
6472     case SImode:
6473     case HImode:
6474     case QImode:
6475       cum->words += words;
6476       cum->nregs -= words;
6477       cum->regno += words;
6478
6479       if (cum->nregs <= 0)
6480         {
6481           cum->nregs = 0;
6482           cum->regno = 0;
6483         }
6484       break;
6485
6486     case OImode:
6487       /* OImode shouldn't be used directly.  */
6488       gcc_unreachable ();
6489
6490     case DFmode:
6491       if (cum->float_in_sse < 2)
6492         break;
6493     case SFmode:
6494       if (cum->float_in_sse < 1)
6495         break;
6496       /* FALLTHRU */
6497
6498     case V8SFmode:
6499     case V8SImode:
6500     case V32QImode:
6501     case V16HImode:
6502     case V4DFmode:
6503     case V4DImode:
6504     case TImode:
6505     case V16QImode:
6506     case V8HImode:
6507     case V4SImode:
6508     case V2DImode:
6509     case V4SFmode:
6510     case V2DFmode:
6511       if (!type || !AGGREGATE_TYPE_P (type))
6512         {
6513           cum->sse_words += words;
6514           cum->sse_nregs -= 1;
6515           cum->sse_regno += 1;
6516           if (cum->sse_nregs <= 0)
6517             {
6518               cum->sse_nregs = 0;
6519               cum->sse_regno = 0;
6520             }
6521         }
6522       break;
6523
6524     case V8QImode:
6525     case V4HImode:
6526     case V2SImode:
6527     case V2SFmode:
6528     case V1TImode:
6529     case V1DImode:
6530       if (!type || !AGGREGATE_TYPE_P (type))
6531         {
6532           cum->mmx_words += words;
6533           cum->mmx_nregs -= 1;
6534           cum->mmx_regno += 1;
6535           if (cum->mmx_nregs <= 0)
6536             {
6537               cum->mmx_nregs = 0;
6538               cum->mmx_regno = 0;
6539             }
6540         }
6541       break;
6542     }
6543 }
6544
6545 static void
6546 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6547                          const_tree type, HOST_WIDE_INT words, bool named)
6548 {
6549   int int_nregs, sse_nregs;
6550
6551   /* Unnamed 256bit vector mode parameters are passed on stack.  */
6552   if (!named && VALID_AVX256_REG_MODE (mode))
6553     return;
6554
6555   if (examine_argument (mode, type, 0, &int_nregs, &sse_nregs)
6556       && sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
6557     {
6558       cum->nregs -= int_nregs;
6559       cum->sse_nregs -= sse_nregs;
6560       cum->regno += int_nregs;
6561       cum->sse_regno += sse_nregs;
6562     }
6563   else
6564     {
6565       int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD;
6566       cum->words = (cum->words + align - 1) & ~(align - 1);
6567       cum->words += words;
6568     }
6569 }
6570
6571 static void
6572 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
6573                             HOST_WIDE_INT words)
6574 {
6575   /* Otherwise, this should be passed indirect.  */
6576   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
6577
6578   cum->words += words;
6579   if (cum->nregs > 0)
6580     {
6581       cum->nregs -= 1;
6582       cum->regno += 1;
6583     }
6584 }
6585
6586 /* Update the data in CUM to advance over an argument of mode MODE and
6587    data type TYPE.  (TYPE is null for libcalls where that information
6588    may not be available.)  */
6589
6590 static void
6591 ix86_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
6592                            const_tree type, bool named)
6593 {
6594   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6595   HOST_WIDE_INT bytes, words;
6596
6597   if (mode == BLKmode)
6598     bytes = int_size_in_bytes (type);
6599   else
6600     bytes = GET_MODE_SIZE (mode);
6601   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6602
6603   if (type)
6604     mode = type_natural_mode (type, NULL);
6605
6606   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6607     function_arg_advance_ms_64 (cum, bytes, words);
6608   else if (TARGET_64BIT)
6609     function_arg_advance_64 (cum, mode, type, words, named);
6610   else
6611     function_arg_advance_32 (cum, mode, type, bytes, words);
6612 }
6613
6614 /* Define where to put the arguments to a function.
6615    Value is zero to push the argument on the stack,
6616    or a hard register in which to store the argument.
6617
6618    MODE is the argument's machine mode.
6619    TYPE is the data type of the argument (as a tree).
6620     This is null for libcalls where that information may
6621     not be available.
6622    CUM is a variable of type CUMULATIVE_ARGS which gives info about
6623     the preceding args and about the function being called.
6624    NAMED is nonzero if this argument is a named parameter
6625     (otherwise it is an extra parameter matching an ellipsis).  */
6626
6627 static rtx
6628 function_arg_32 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6629                  enum machine_mode orig_mode, const_tree type,
6630                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
6631 {
6632   static bool warnedsse, warnedmmx;
6633
6634   /* Avoid the AL settings for the Unix64 ABI.  */
6635   if (mode == VOIDmode)
6636     return constm1_rtx;
6637
6638   switch (mode)
6639     {
6640     default:
6641       break;
6642
6643     case BLKmode:
6644       if (bytes < 0)
6645         break;
6646       /* FALLTHRU */
6647     case DImode:
6648     case SImode:
6649     case HImode:
6650     case QImode:
6651       if (words <= cum->nregs)
6652         {
6653           int regno = cum->regno;
6654
6655           /* Fastcall allocates the first two DWORD (SImode) or
6656             smaller arguments to ECX and EDX if it isn't an
6657             aggregate type .  */
6658           if (cum->fastcall)
6659             {
6660               if (mode == BLKmode
6661                   || mode == DImode
6662                   || (type && AGGREGATE_TYPE_P (type)))
6663                 break;
6664
6665               /* ECX not EAX is the first allocated register.  */
6666               if (regno == AX_REG)
6667                 regno = CX_REG;
6668             }
6669           return gen_rtx_REG (mode, regno);
6670         }
6671       break;
6672
6673     case DFmode:
6674       if (cum->float_in_sse < 2)
6675         break;
6676     case SFmode:
6677       if (cum->float_in_sse < 1)
6678         break;
6679       /* FALLTHRU */
6680     case TImode:
6681       /* In 32bit, we pass TImode in xmm registers.  */
6682     case V16QImode:
6683     case V8HImode:
6684     case V4SImode:
6685     case V2DImode:
6686     case V4SFmode:
6687     case V2DFmode:
6688       if (!type || !AGGREGATE_TYPE_P (type))
6689         {
6690           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
6691             {
6692               warnedsse = true;
6693               warning (0, "SSE vector argument without SSE enabled "
6694                        "changes the ABI");
6695             }
6696           if (cum->sse_nregs)
6697             return gen_reg_or_parallel (mode, orig_mode,
6698                                         cum->sse_regno + FIRST_SSE_REG);
6699         }
6700       break;
6701
6702     case OImode:
6703       /* OImode shouldn't be used directly.  */
6704       gcc_unreachable ();
6705
6706     case V8SFmode:
6707     case V8SImode:
6708     case V32QImode:
6709     case V16HImode:
6710     case V4DFmode:
6711     case V4DImode:
6712       if (!type || !AGGREGATE_TYPE_P (type))
6713         {
6714           if (cum->sse_nregs)
6715             return gen_reg_or_parallel (mode, orig_mode,
6716                                         cum->sse_regno + FIRST_SSE_REG);
6717         }
6718       break;
6719
6720     case V8QImode:
6721     case V4HImode:
6722     case V2SImode:
6723     case V2SFmode:
6724     case V1TImode:
6725     case V1DImode:
6726       if (!type || !AGGREGATE_TYPE_P (type))
6727         {
6728           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6729             {
6730               warnedmmx = true;
6731               warning (0, "MMX vector argument without MMX enabled "
6732                        "changes the ABI");
6733             }
6734           if (cum->mmx_nregs)
6735             return gen_reg_or_parallel (mode, orig_mode,
6736                                         cum->mmx_regno + FIRST_MMX_REG);
6737         }
6738       break;
6739     }
6740
6741   return NULL_RTX;
6742 }
6743
6744 static rtx
6745 function_arg_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6746                  enum machine_mode orig_mode, const_tree type, bool named)
6747 {
6748   /* Handle a hidden AL argument containing number of registers
6749      for varargs x86-64 functions.  */
6750   if (mode == VOIDmode)
6751     return GEN_INT (cum->maybe_vaarg
6752                     ? (cum->sse_nregs < 0
6753                        ? X86_64_SSE_REGPARM_MAX
6754                        : cum->sse_regno)
6755                     : -1);
6756
6757   switch (mode)
6758     {
6759     default:
6760       break;
6761
6762     case V8SFmode:
6763     case V8SImode:
6764     case V32QImode:
6765     case V16HImode:
6766     case V4DFmode:
6767     case V4DImode:
6768       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6769       if (!named)
6770         return NULL;
6771       break;
6772     }
6773
6774   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6775                               cum->sse_nregs,
6776                               &x86_64_int_parameter_registers [cum->regno],
6777                               cum->sse_regno);
6778 }
6779
6780 static rtx
6781 function_arg_ms_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6782                     enum machine_mode orig_mode, bool named,
6783                     HOST_WIDE_INT bytes)
6784 {
6785   unsigned int regno;
6786
6787   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6788      We use value of -2 to specify that current function call is MSABI.  */
6789   if (mode == VOIDmode)
6790     return GEN_INT (-2);
6791
6792   /* If we've run out of registers, it goes on the stack.  */
6793   if (cum->nregs == 0)
6794     return NULL_RTX;
6795
6796   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
6797
6798   /* Only floating point modes are passed in anything but integer regs.  */
6799   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
6800     {
6801       if (named)
6802         regno = cum->regno + FIRST_SSE_REG;
6803       else
6804         {
6805           rtx t1, t2;
6806
6807           /* Unnamed floating parameters are passed in both the
6808              SSE and integer registers.  */
6809           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
6810           t2 = gen_rtx_REG (mode, regno);
6811           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
6812           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
6813           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
6814         }
6815     }
6816   /* Handle aggregated types passed in register.  */
6817   if (orig_mode == BLKmode)
6818     {
6819       if (bytes > 0 && bytes <= 8)
6820         mode = (bytes > 4 ? DImode : SImode);
6821       if (mode == BLKmode)
6822         mode = DImode;
6823     }
6824
6825   return gen_reg_or_parallel (mode, orig_mode, regno);
6826 }
6827
6828 /* Return where to put the arguments to a function.
6829    Return zero to push the argument on the stack, or a hard register in which to store the argument.
6830
6831    MODE is the argument's machine mode.  TYPE is the data type of the
6832    argument.  It is null for libcalls where that information may not be
6833    available.  CUM gives information about the preceding args and about
6834    the function being called.  NAMED is nonzero if this argument is a
6835    named parameter (otherwise it is an extra parameter matching an
6836    ellipsis).  */
6837
6838 static rtx
6839 ix86_function_arg (cumulative_args_t cum_v, enum machine_mode omode,
6840                    const_tree type, bool named)
6841 {
6842   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6843   enum machine_mode mode = omode;
6844   HOST_WIDE_INT bytes, words;
6845   rtx arg;
6846
6847   if (mode == BLKmode)
6848     bytes = int_size_in_bytes (type);
6849   else
6850     bytes = GET_MODE_SIZE (mode);
6851   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6852
6853   /* To simplify the code below, represent vector types with a vector mode
6854      even if MMX/SSE are not active.  */
6855   if (type && TREE_CODE (type) == VECTOR_TYPE)
6856     mode = type_natural_mode (type, cum);
6857
6858   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6859     arg = function_arg_ms_64 (cum, mode, omode, named, bytes);
6860   else if (TARGET_64BIT)
6861     arg = function_arg_64 (cum, mode, omode, type, named);
6862   else
6863     arg = function_arg_32 (cum, mode, omode, type, bytes, words);
6864
6865   if (TARGET_VZEROUPPER && function_pass_avx256_p (arg))
6866     {
6867       /* This argument uses 256bit AVX modes.  */
6868       if (cum->caller)
6869         cum->callee_pass_avx256_p = true;
6870       else
6871         cfun->machine->caller_pass_avx256_p = true;
6872     }
6873
6874   if (cum->caller && mode == VOIDmode)
6875     {
6876       /* This function is called with MODE == VOIDmode immediately
6877          before the call instruction is emitted.  We copy callee 256bit
6878          AVX info from the current CUM here.  */
6879       cfun->machine->callee_return_avx256_p = cum->callee_return_avx256_p;
6880       cfun->machine->callee_pass_avx256_p = cum->callee_pass_avx256_p;
6881     }
6882
6883   return arg;
6884 }
6885
6886 /* A C expression that indicates when an argument must be passed by
6887    reference.  If nonzero for an argument, a copy of that argument is
6888    made in memory and a pointer to the argument is passed instead of
6889    the argument itself.  The pointer is passed in whatever way is
6890    appropriate for passing a pointer to that type.  */
6891
6892 static bool
6893 ix86_pass_by_reference (cumulative_args_t cum_v ATTRIBUTE_UNUSED,
6894                         enum machine_mode mode ATTRIBUTE_UNUSED,
6895                         const_tree type, bool named ATTRIBUTE_UNUSED)
6896 {
6897   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6898
6899   /* See Windows x64 Software Convention.  */
6900   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6901     {
6902       int msize = (int) GET_MODE_SIZE (mode);
6903       if (type)
6904         {
6905           /* Arrays are passed by reference.  */
6906           if (TREE_CODE (type) == ARRAY_TYPE)
6907             return true;
6908
6909           if (AGGREGATE_TYPE_P (type))
6910             {
6911               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
6912                  are passed by reference.  */
6913               msize = int_size_in_bytes (type);
6914             }
6915         }
6916
6917       /* __m128 is passed by reference.  */
6918       switch (msize) {
6919       case 1: case 2: case 4: case 8:
6920         break;
6921       default:
6922         return true;
6923       }
6924     }
6925   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
6926     return 1;
6927
6928   return 0;
6929 }
6930
6931 /* Return true when TYPE should be 128bit aligned for 32bit argument
6932    passing ABI.  XXX: This function is obsolete and is only used for
6933    checking psABI compatibility with previous versions of GCC.  */
6934
6935 static bool
6936 ix86_compat_aligned_value_p (const_tree type)
6937 {
6938   enum machine_mode mode = TYPE_MODE (type);
6939   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
6940        || mode == TDmode
6941        || mode == TFmode
6942        || mode == TCmode)
6943       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
6944     return true;
6945   if (TYPE_ALIGN (type) < 128)
6946     return false;
6947
6948   if (AGGREGATE_TYPE_P (type))
6949     {
6950       /* Walk the aggregates recursively.  */
6951       switch (TREE_CODE (type))
6952         {
6953         case RECORD_TYPE:
6954         case UNION_TYPE:
6955         case QUAL_UNION_TYPE:
6956           {
6957             tree field;
6958
6959             /* Walk all the structure fields.  */
6960             for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6961               {
6962                 if (TREE_CODE (field) == FIELD_DECL
6963                     && ix86_compat_aligned_value_p (TREE_TYPE (field)))
6964                   return true;
6965               }
6966             break;
6967           }
6968
6969         case ARRAY_TYPE:
6970           /* Just for use if some languages passes arrays by value.  */
6971           if (ix86_compat_aligned_value_p (TREE_TYPE (type)))
6972             return true;
6973           break;
6974
6975         default:
6976           gcc_unreachable ();
6977         }
6978     }
6979   return false;
6980 }
6981
6982 /* Return the alignment boundary for MODE and TYPE with alignment ALIGN.
6983    XXX: This function is obsolete and is only used for checking psABI
6984    compatibility with previous versions of GCC.  */
6985
6986 static unsigned int
6987 ix86_compat_function_arg_boundary (enum machine_mode mode,
6988                                    const_tree type, unsigned int align)
6989 {
6990   /* In 32bit, only _Decimal128 and __float128 are aligned to their
6991      natural boundaries.  */
6992   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
6993     {
6994       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
6995          make an exception for SSE modes since these require 128bit
6996          alignment.
6997
6998          The handling here differs from field_alignment.  ICC aligns MMX
6999          arguments to 4 byte boundaries, while structure fields are aligned
7000          to 8 byte boundaries.  */
7001       if (!type)
7002         {
7003           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
7004             align = PARM_BOUNDARY;
7005         }
7006       else
7007         {
7008           if (!ix86_compat_aligned_value_p (type))
7009             align = PARM_BOUNDARY;
7010         }
7011     }
7012   if (align > BIGGEST_ALIGNMENT)
7013     align = BIGGEST_ALIGNMENT;
7014   return align;
7015 }
7016
7017 /* Return true when TYPE should be 128bit aligned for 32bit argument
7018    passing ABI.  */
7019
7020 static bool
7021 ix86_contains_aligned_value_p (const_tree type)
7022 {
7023   enum machine_mode mode = TYPE_MODE (type);
7024
7025   if (mode == XFmode || mode == XCmode)
7026     return false;
7027
7028   if (TYPE_ALIGN (type) < 128)
7029     return false;
7030
7031   if (AGGREGATE_TYPE_P (type))
7032     {
7033       /* Walk the aggregates recursively.  */
7034       switch (TREE_CODE (type))
7035         {
7036         case RECORD_TYPE:
7037         case UNION_TYPE:
7038         case QUAL_UNION_TYPE:
7039           {
7040             tree field;
7041
7042             /* Walk all the structure fields.  */
7043             for (field = TYPE_FIELDS (type);
7044                  field;
7045                  field = DECL_CHAIN (field))
7046               {
7047                 if (TREE_CODE (field) == FIELD_DECL
7048                     && ix86_contains_aligned_value_p (TREE_TYPE (field)))
7049                   return true;
7050               }
7051             break;
7052           }
7053
7054         case ARRAY_TYPE:
7055           /* Just for use if some languages passes arrays by value.  */
7056           if (ix86_contains_aligned_value_p (TREE_TYPE (type)))
7057             return true;
7058           break;
7059
7060         default:
7061           gcc_unreachable ();
7062         }
7063     }
7064   else
7065     return TYPE_ALIGN (type) >= 128;
7066
7067   return false;
7068 }
7069
7070 /* Gives the alignment boundary, in bits, of an argument with the
7071    specified mode and type.  */
7072
7073 static unsigned int
7074 ix86_function_arg_boundary (enum machine_mode mode, const_tree type)
7075 {
7076   unsigned int align;
7077   if (type)
7078     {
7079       /* Since the main variant type is used for call, we convert it to
7080          the main variant type.  */
7081       type = TYPE_MAIN_VARIANT (type);
7082       align = TYPE_ALIGN (type);
7083     }
7084   else
7085     align = GET_MODE_ALIGNMENT (mode);
7086   if (align < PARM_BOUNDARY)
7087     align = PARM_BOUNDARY;
7088   else
7089     {
7090       static bool warned;
7091       unsigned int saved_align = align;
7092
7093       if (!TARGET_64BIT)
7094         {
7095           /* i386 ABI defines XFmode arguments to be 4 byte aligned.  */
7096           if (!type)
7097             {
7098               if (mode == XFmode || mode == XCmode)
7099                 align = PARM_BOUNDARY;
7100             }
7101           else if (!ix86_contains_aligned_value_p (type))
7102             align = PARM_BOUNDARY;
7103
7104           if (align < 128)
7105             align = PARM_BOUNDARY;
7106         }
7107
7108       if (warn_psabi
7109           && !warned
7110           && align != ix86_compat_function_arg_boundary (mode, type,
7111                                                          saved_align))
7112         {
7113           warned = true;
7114           inform (input_location,
7115                   "The ABI for passing parameters with %d-byte"
7116                   " alignment has changed in GCC 4.6",
7117                   align / BITS_PER_UNIT);
7118         }
7119     }
7120
7121   return align;
7122 }
7123
7124 /* Return true if N is a possible register number of function value.  */
7125
7126 static bool
7127 ix86_function_value_regno_p (const unsigned int regno)
7128 {
7129   switch (regno)
7130     {
7131     case AX_REG:
7132     case DX_REG:
7133       return true;
7134     case DI_REG:
7135     case SI_REG:
7136       return TARGET_64BIT && ix86_abi != MS_ABI;
7137
7138       /* Complex values are returned in %st(0)/%st(1) pair.  */
7139     case ST0_REG:
7140     case ST1_REG:
7141       /* TODO: The function should depend on current function ABI but
7142        builtins.c would need updating then. Therefore we use the
7143        default ABI.  */
7144       if (TARGET_64BIT && ix86_abi == MS_ABI)
7145         return false;
7146       return TARGET_FLOAT_RETURNS_IN_80387;
7147
7148       /* Complex values are returned in %xmm0/%xmm1 pair.  */
7149     case XMM0_REG:
7150     case XMM1_REG:
7151       return TARGET_SSE;
7152
7153     case MM0_REG:
7154       if (TARGET_MACHO || TARGET_64BIT)
7155         return false;
7156       return TARGET_MMX;
7157     }
7158
7159   return false;
7160 }
7161
7162 /* Define how to find the value returned by a function.
7163    VALTYPE is the data type of the value (as a tree).
7164    If the precise function being called is known, FUNC is its FUNCTION_DECL;
7165    otherwise, FUNC is 0.  */
7166
7167 static rtx
7168 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
7169                    const_tree fntype, const_tree fn)
7170 {
7171   unsigned int regno;
7172
7173   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
7174      we normally prevent this case when mmx is not available.  However
7175      some ABIs may require the result to be returned like DImode.  */
7176   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7177     regno = FIRST_MMX_REG;
7178
7179   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
7180      we prevent this case when sse is not available.  However some ABIs
7181      may require the result to be returned like integer TImode.  */
7182   else if (mode == TImode
7183            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7184     regno = FIRST_SSE_REG;
7185
7186   /* 32-byte vector modes in %ymm0.   */
7187   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
7188     regno = FIRST_SSE_REG;
7189
7190   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
7191   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
7192     regno = FIRST_FLOAT_REG;
7193   else
7194     /* Most things go in %eax.  */
7195     regno = AX_REG;
7196
7197   /* Override FP return register with %xmm0 for local functions when
7198      SSE math is enabled or for functions with sseregparm attribute.  */
7199   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
7200     {
7201       int sse_level = ix86_function_sseregparm (fntype, fn, false);
7202       if ((sse_level >= 1 && mode == SFmode)
7203           || (sse_level == 2 && mode == DFmode))
7204         regno = FIRST_SSE_REG;
7205     }
7206
7207   /* OImode shouldn't be used directly.  */
7208   gcc_assert (mode != OImode);
7209
7210   return gen_rtx_REG (orig_mode, regno);
7211 }
7212
7213 static rtx
7214 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
7215                    const_tree valtype)
7216 {
7217   rtx ret;
7218
7219   /* Handle libcalls, which don't provide a type node.  */
7220   if (valtype == NULL)
7221     {
7222       unsigned int regno;
7223
7224       switch (mode)
7225         {
7226         case SFmode:
7227         case SCmode:
7228         case DFmode:
7229         case DCmode:
7230         case TFmode:
7231         case SDmode:
7232         case DDmode:
7233         case TDmode:
7234           regno = FIRST_SSE_REG;
7235           break;
7236         case XFmode:
7237         case XCmode:
7238           regno = FIRST_FLOAT_REG;
7239           break;
7240         case TCmode:
7241           return NULL;
7242         default:
7243           regno = AX_REG;
7244         }
7245
7246       return gen_rtx_REG (mode, regno);
7247     }
7248   else if (POINTER_TYPE_P (valtype))
7249     {
7250       /* Pointers are always returned in Pmode. */
7251       mode = Pmode;
7252     }
7253
7254   ret = construct_container (mode, orig_mode, valtype, 1,
7255                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
7256                              x86_64_int_return_registers, 0);
7257
7258   /* For zero sized structures, construct_container returns NULL, but we
7259      need to keep rest of compiler happy by returning meaningful value.  */
7260   if (!ret)
7261     ret = gen_rtx_REG (orig_mode, AX_REG);
7262
7263   return ret;
7264 }
7265
7266 static rtx
7267 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
7268 {
7269   unsigned int regno = AX_REG;
7270
7271   if (TARGET_SSE)
7272     {
7273       switch (GET_MODE_SIZE (mode))
7274         {
7275         case 16:
7276           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7277              && !COMPLEX_MODE_P (mode))
7278             regno = FIRST_SSE_REG;
7279           break;
7280         case 8:
7281         case 4:
7282           if (mode == SFmode || mode == DFmode)
7283             regno = FIRST_SSE_REG;
7284           break;
7285         default:
7286           break;
7287         }
7288     }
7289   return gen_rtx_REG (orig_mode, regno);
7290 }
7291
7292 static rtx
7293 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
7294                        enum machine_mode orig_mode, enum machine_mode mode)
7295 {
7296   const_tree fn, fntype;
7297
7298   fn = NULL_TREE;
7299   if (fntype_or_decl && DECL_P (fntype_or_decl))
7300     fn = fntype_or_decl;
7301   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
7302
7303   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
7304     return function_value_ms_64 (orig_mode, mode);
7305   else if (TARGET_64BIT)
7306     return function_value_64 (orig_mode, mode, valtype);
7307   else
7308     return function_value_32 (orig_mode, mode, fntype, fn);
7309 }
7310
7311 static rtx
7312 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
7313                      bool outgoing ATTRIBUTE_UNUSED)
7314 {
7315   enum machine_mode mode, orig_mode;
7316
7317   orig_mode = TYPE_MODE (valtype);
7318   mode = type_natural_mode (valtype, NULL);
7319   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
7320 }
7321
7322 /* Pointer function arguments and return values are promoted to Pmode.  */
7323
7324 static enum machine_mode
7325 ix86_promote_function_mode (const_tree type, enum machine_mode mode,
7326                             int *punsignedp, const_tree fntype,
7327                             int for_return)
7328 {
7329   if (type != NULL_TREE && POINTER_TYPE_P (type))
7330     {
7331       *punsignedp = POINTERS_EXTEND_UNSIGNED;
7332       return Pmode;
7333     }
7334   return default_promote_function_mode (type, mode, punsignedp, fntype,
7335                                         for_return);
7336 }
7337
7338 rtx
7339 ix86_libcall_value (enum machine_mode mode)
7340 {
7341   return ix86_function_value_1 (NULL, NULL, mode, mode);
7342 }
7343
7344 /* Return true iff type is returned in memory.  */
7345
7346 static bool ATTRIBUTE_UNUSED
7347 return_in_memory_32 (const_tree type, enum machine_mode mode)
7348 {
7349   HOST_WIDE_INT size;
7350
7351   if (mode == BLKmode)
7352     return true;
7353
7354   size = int_size_in_bytes (type);
7355
7356   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
7357     return false;
7358
7359   if (VECTOR_MODE_P (mode) || mode == TImode)
7360     {
7361       /* User-created vectors small enough to fit in EAX.  */
7362       if (size < 8)
7363         return false;
7364
7365       /* MMX/3dNow values are returned in MM0,
7366          except when it doesn't exits or the ABI prescribes otherwise.  */
7367       if (size == 8)
7368         return !TARGET_MMX || TARGET_VECT8_RETURNS;
7369
7370       /* SSE values are returned in XMM0, except when it doesn't exist.  */
7371       if (size == 16)
7372         return !TARGET_SSE;
7373
7374       /* AVX values are returned in YMM0, except when it doesn't exist.  */
7375       if (size == 32)
7376         return !TARGET_AVX;
7377     }
7378
7379   if (mode == XFmode)
7380     return false;
7381
7382   if (size > 12)
7383     return true;
7384
7385   /* OImode shouldn't be used directly.  */
7386   gcc_assert (mode != OImode);
7387
7388   return false;
7389 }
7390
7391 static bool ATTRIBUTE_UNUSED
7392 return_in_memory_64 (const_tree type, enum machine_mode mode)
7393 {
7394   int needed_intregs, needed_sseregs;
7395   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
7396 }
7397
7398 static bool ATTRIBUTE_UNUSED
7399 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
7400 {
7401   HOST_WIDE_INT size = int_size_in_bytes (type);
7402
7403   /* __m128 is returned in xmm0.  */
7404   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7405       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
7406     return false;
7407
7408   /* Otherwise, the size must be exactly in [1248]. */
7409   return size != 1 && size != 2 && size != 4 && size != 8;
7410 }
7411
7412 static bool
7413 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7414 {
7415 #ifdef SUBTARGET_RETURN_IN_MEMORY
7416   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
7417 #else
7418   const enum machine_mode mode = type_natural_mode (type, NULL);
7419
7420   if (TARGET_64BIT)
7421     {
7422       if (ix86_function_type_abi (fntype) == MS_ABI)
7423         return return_in_memory_ms_64 (type, mode);
7424       else
7425         return return_in_memory_64 (type, mode);
7426     }
7427   else
7428     return return_in_memory_32 (type, mode);
7429 #endif
7430 }
7431
7432 /* When returning SSE vector types, we have a choice of either
7433      (1) being abi incompatible with a -march switch, or
7434      (2) generating an error.
7435    Given no good solution, I think the safest thing is one warning.
7436    The user won't be able to use -Werror, but....
7437
7438    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
7439    called in response to actually generating a caller or callee that
7440    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
7441    via aggregate_value_p for general type probing from tree-ssa.  */
7442
7443 static rtx
7444 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
7445 {
7446   static bool warnedsse, warnedmmx;
7447
7448   if (!TARGET_64BIT && type)
7449     {
7450       /* Look at the return type of the function, not the function type.  */
7451       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
7452
7453       if (!TARGET_SSE && !warnedsse)
7454         {
7455           if (mode == TImode
7456               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7457             {
7458               warnedsse = true;
7459               warning (0, "SSE vector return without SSE enabled "
7460                        "changes the ABI");
7461             }
7462         }
7463
7464       if (!TARGET_MMX && !warnedmmx)
7465         {
7466           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7467             {
7468               warnedmmx = true;
7469               warning (0, "MMX vector return without MMX enabled "
7470                        "changes the ABI");
7471             }
7472         }
7473     }
7474
7475   return NULL;
7476 }
7477
7478 \f
7479 /* Create the va_list data type.  */
7480
7481 /* Returns the calling convention specific va_list date type.
7482    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
7483
7484 static tree
7485 ix86_build_builtin_va_list_abi (enum calling_abi abi)
7486 {
7487   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
7488
7489   /* For i386 we use plain pointer to argument area.  */
7490   if (!TARGET_64BIT || abi == MS_ABI)
7491     return build_pointer_type (char_type_node);
7492
7493   record = lang_hooks.types.make_type (RECORD_TYPE);
7494   type_decl = build_decl (BUILTINS_LOCATION,
7495                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
7496
7497   f_gpr = build_decl (BUILTINS_LOCATION,
7498                       FIELD_DECL, get_identifier ("gp_offset"),
7499                       unsigned_type_node);
7500   f_fpr = build_decl (BUILTINS_LOCATION,
7501                       FIELD_DECL, get_identifier ("fp_offset"),
7502                       unsigned_type_node);
7503   f_ovf = build_decl (BUILTINS_LOCATION,
7504                       FIELD_DECL, get_identifier ("overflow_arg_area"),
7505                       ptr_type_node);
7506   f_sav = build_decl (BUILTINS_LOCATION,
7507                       FIELD_DECL, get_identifier ("reg_save_area"),
7508                       ptr_type_node);
7509
7510   va_list_gpr_counter_field = f_gpr;
7511   va_list_fpr_counter_field = f_fpr;
7512
7513   DECL_FIELD_CONTEXT (f_gpr) = record;
7514   DECL_FIELD_CONTEXT (f_fpr) = record;
7515   DECL_FIELD_CONTEXT (f_ovf) = record;
7516   DECL_FIELD_CONTEXT (f_sav) = record;
7517
7518   TYPE_STUB_DECL (record) = type_decl;
7519   TYPE_NAME (record) = type_decl;
7520   TYPE_FIELDS (record) = f_gpr;
7521   DECL_CHAIN (f_gpr) = f_fpr;
7522   DECL_CHAIN (f_fpr) = f_ovf;
7523   DECL_CHAIN (f_ovf) = f_sav;
7524
7525   layout_type (record);
7526
7527   /* The correct type is an array type of one element.  */
7528   return build_array_type (record, build_index_type (size_zero_node));
7529 }
7530
7531 /* Setup the builtin va_list data type and for 64-bit the additional
7532    calling convention specific va_list data types.  */
7533
7534 static tree
7535 ix86_build_builtin_va_list (void)
7536 {
7537   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
7538
7539   /* Initialize abi specific va_list builtin types.  */
7540   if (TARGET_64BIT)
7541     {
7542       tree t;
7543       if (ix86_abi == MS_ABI)
7544         {
7545           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
7546           if (TREE_CODE (t) != RECORD_TYPE)
7547             t = build_variant_type_copy (t);
7548           sysv_va_list_type_node = t;
7549         }
7550       else
7551         {
7552           t = ret;
7553           if (TREE_CODE (t) != RECORD_TYPE)
7554             t = build_variant_type_copy (t);
7555           sysv_va_list_type_node = t;
7556         }
7557       if (ix86_abi != MS_ABI)
7558         {
7559           t = ix86_build_builtin_va_list_abi (MS_ABI);
7560           if (TREE_CODE (t) != RECORD_TYPE)
7561             t = build_variant_type_copy (t);
7562           ms_va_list_type_node = t;
7563         }
7564       else
7565         {
7566           t = ret;
7567           if (TREE_CODE (t) != RECORD_TYPE)
7568             t = build_variant_type_copy (t);
7569           ms_va_list_type_node = t;
7570         }
7571     }
7572
7573   return ret;
7574 }
7575
7576 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
7577
7578 static void
7579 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
7580 {
7581   rtx save_area, mem;
7582   alias_set_type set;
7583   int i, max;
7584
7585   /* GPR size of varargs save area.  */
7586   if (cfun->va_list_gpr_size)
7587     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
7588   else
7589     ix86_varargs_gpr_size = 0;
7590
7591   /* FPR size of varargs save area.  We don't need it if we don't pass
7592      anything in SSE registers.  */
7593   if (TARGET_SSE && cfun->va_list_fpr_size)
7594     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
7595   else
7596     ix86_varargs_fpr_size = 0;
7597
7598   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
7599     return;
7600
7601   save_area = frame_pointer_rtx;
7602   set = get_varargs_alias_set ();
7603
7604   max = cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
7605   if (max > X86_64_REGPARM_MAX)
7606     max = X86_64_REGPARM_MAX;
7607
7608   for (i = cum->regno; i < max; i++)
7609     {
7610       mem = gen_rtx_MEM (Pmode,
7611                          plus_constant (save_area, i * UNITS_PER_WORD));
7612       MEM_NOTRAP_P (mem) = 1;
7613       set_mem_alias_set (mem, set);
7614       emit_move_insn (mem, gen_rtx_REG (Pmode,
7615                                         x86_64_int_parameter_registers[i]));
7616     }
7617
7618   if (ix86_varargs_fpr_size)
7619     {
7620       enum machine_mode smode;
7621       rtx label, test;
7622
7623       /* Now emit code to save SSE registers.  The AX parameter contains number
7624          of SSE parameter registers used to call this function, though all we
7625          actually check here is the zero/non-zero status.  */
7626
7627       label = gen_label_rtx ();
7628       test = gen_rtx_EQ (VOIDmode, gen_rtx_REG (QImode, AX_REG), const0_rtx);
7629       emit_jump_insn (gen_cbranchqi4 (test, XEXP (test, 0), XEXP (test, 1),
7630                                       label));
7631
7632       /* ??? If !TARGET_SSE_TYPELESS_STORES, would we perform better if
7633          we used movdqa (i.e. TImode) instead?  Perhaps even better would
7634          be if we could determine the real mode of the data, via a hook
7635          into pass_stdarg.  Ignore all that for now.  */
7636       smode = V4SFmode;
7637       if (crtl->stack_alignment_needed < GET_MODE_ALIGNMENT (smode))
7638         crtl->stack_alignment_needed = GET_MODE_ALIGNMENT (smode);
7639
7640       max = cum->sse_regno + cfun->va_list_fpr_size / 16;
7641       if (max > X86_64_SSE_REGPARM_MAX)
7642         max = X86_64_SSE_REGPARM_MAX;
7643
7644       for (i = cum->sse_regno; i < max; ++i)
7645         {
7646           mem = plus_constant (save_area, i * 16 + ix86_varargs_gpr_size);
7647           mem = gen_rtx_MEM (smode, mem);
7648           MEM_NOTRAP_P (mem) = 1;
7649           set_mem_alias_set (mem, set);
7650           set_mem_align (mem, GET_MODE_ALIGNMENT (smode));
7651
7652           emit_move_insn (mem, gen_rtx_REG (smode, SSE_REGNO (i)));
7653         }
7654
7655       emit_label (label);
7656     }
7657 }
7658
7659 static void
7660 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
7661 {
7662   alias_set_type set = get_varargs_alias_set ();
7663   int i;
7664
7665   /* Reset to zero, as there might be a sysv vaarg used
7666      before.  */
7667   ix86_varargs_gpr_size = 0;
7668   ix86_varargs_fpr_size = 0;
7669
7670   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
7671     {
7672       rtx reg, mem;
7673
7674       mem = gen_rtx_MEM (Pmode,
7675                          plus_constant (virtual_incoming_args_rtx,
7676                                         i * UNITS_PER_WORD));
7677       MEM_NOTRAP_P (mem) = 1;
7678       set_mem_alias_set (mem, set);
7679
7680       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
7681       emit_move_insn (mem, reg);
7682     }
7683 }
7684
7685 static void
7686 ix86_setup_incoming_varargs (cumulative_args_t cum_v, enum machine_mode mode,
7687                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
7688                              int no_rtl)
7689 {
7690   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
7691   CUMULATIVE_ARGS next_cum;
7692   tree fntype;
7693
7694   /* This argument doesn't appear to be used anymore.  Which is good,
7695      because the old code here didn't suppress rtl generation.  */
7696   gcc_assert (!no_rtl);
7697
7698   if (!TARGET_64BIT)
7699     return;
7700
7701   fntype = TREE_TYPE (current_function_decl);
7702
7703   /* For varargs, we do not want to skip the dummy va_dcl argument.
7704      For stdargs, we do want to skip the last named argument.  */
7705   next_cum = *cum;
7706   if (stdarg_p (fntype))
7707     ix86_function_arg_advance (pack_cumulative_args (&next_cum), mode, type,
7708                                true);
7709
7710   if (cum->call_abi == MS_ABI)
7711     setup_incoming_varargs_ms_64 (&next_cum);
7712   else
7713     setup_incoming_varargs_64 (&next_cum);
7714 }
7715
7716 /* Checks if TYPE is of kind va_list char *.  */
7717
7718 static bool
7719 is_va_list_char_pointer (tree type)
7720 {
7721   tree canonic;
7722
7723   /* For 32-bit it is always true.  */
7724   if (!TARGET_64BIT)
7725     return true;
7726   canonic = ix86_canonical_va_list_type (type);
7727   return (canonic == ms_va_list_type_node
7728           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
7729 }
7730
7731 /* Implement va_start.  */
7732
7733 static void
7734 ix86_va_start (tree valist, rtx nextarg)
7735 {
7736   HOST_WIDE_INT words, n_gpr, n_fpr;
7737   tree f_gpr, f_fpr, f_ovf, f_sav;
7738   tree gpr, fpr, ovf, sav, t;
7739   tree type;
7740   rtx ovf_rtx;
7741
7742   if (flag_split_stack
7743       && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7744     {
7745       unsigned int scratch_regno;
7746
7747       /* When we are splitting the stack, we can't refer to the stack
7748          arguments using internal_arg_pointer, because they may be on
7749          the old stack.  The split stack prologue will arrange to
7750          leave a pointer to the old stack arguments in a scratch
7751          register, which we here copy to a pseudo-register.  The split
7752          stack prologue can't set the pseudo-register directly because
7753          it (the prologue) runs before any registers have been saved.  */
7754
7755       scratch_regno = split_stack_prologue_scratch_regno ();
7756       if (scratch_regno != INVALID_REGNUM)
7757         {
7758           rtx reg, seq;
7759
7760           reg = gen_reg_rtx (Pmode);
7761           cfun->machine->split_stack_varargs_pointer = reg;
7762
7763           start_sequence ();
7764           emit_move_insn (reg, gen_rtx_REG (Pmode, scratch_regno));
7765           seq = get_insns ();
7766           end_sequence ();
7767
7768           push_topmost_sequence ();
7769           emit_insn_after (seq, entry_of_function ());
7770           pop_topmost_sequence ();
7771         }
7772     }
7773
7774   /* Only 64bit target needs something special.  */
7775   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7776     {
7777       if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7778         std_expand_builtin_va_start (valist, nextarg);
7779       else
7780         {
7781           rtx va_r, next;
7782
7783           va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
7784           next = expand_binop (ptr_mode, add_optab,
7785                                cfun->machine->split_stack_varargs_pointer,
7786                                crtl->args.arg_offset_rtx,
7787                                NULL_RTX, 0, OPTAB_LIB_WIDEN);
7788           convert_move (va_r, next, 0);
7789         }
7790       return;
7791     }
7792
7793   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7794   f_fpr = DECL_CHAIN (f_gpr);
7795   f_ovf = DECL_CHAIN (f_fpr);
7796   f_sav = DECL_CHAIN (f_ovf);
7797
7798   valist = build_simple_mem_ref (valist);
7799   TREE_TYPE (valist) = TREE_TYPE (sysv_va_list_type_node);
7800   /* The following should be folded into the MEM_REF offset.  */
7801   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), unshare_expr (valist),
7802                 f_gpr, NULL_TREE);
7803   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
7804                 f_fpr, NULL_TREE);
7805   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
7806                 f_ovf, NULL_TREE);
7807   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
7808                 f_sav, NULL_TREE);
7809
7810   /* Count number of gp and fp argument registers used.  */
7811   words = crtl->args.info.words;
7812   n_gpr = crtl->args.info.regno;
7813   n_fpr = crtl->args.info.sse_regno;
7814
7815   if (cfun->va_list_gpr_size)
7816     {
7817       type = TREE_TYPE (gpr);
7818       t = build2 (MODIFY_EXPR, type,
7819                   gpr, build_int_cst (type, n_gpr * 8));
7820       TREE_SIDE_EFFECTS (t) = 1;
7821       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7822     }
7823
7824   if (TARGET_SSE && cfun->va_list_fpr_size)
7825     {
7826       type = TREE_TYPE (fpr);
7827       t = build2 (MODIFY_EXPR, type, fpr,
7828                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
7829       TREE_SIDE_EFFECTS (t) = 1;
7830       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7831     }
7832
7833   /* Find the overflow area.  */
7834   type = TREE_TYPE (ovf);
7835   if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7836     ovf_rtx = crtl->args.internal_arg_pointer;
7837   else
7838     ovf_rtx = cfun->machine->split_stack_varargs_pointer;
7839   t = make_tree (type, ovf_rtx);
7840   if (words != 0)
7841     t = fold_build_pointer_plus_hwi (t, words * UNITS_PER_WORD);
7842   t = build2 (MODIFY_EXPR, type, ovf, t);
7843   TREE_SIDE_EFFECTS (t) = 1;
7844   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7845
7846   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
7847     {
7848       /* Find the register save area.
7849          Prologue of the function save it right above stack frame.  */
7850       type = TREE_TYPE (sav);
7851       t = make_tree (type, frame_pointer_rtx);
7852       if (!ix86_varargs_gpr_size)
7853         t = fold_build_pointer_plus_hwi (t, -8 * X86_64_REGPARM_MAX);
7854       t = build2 (MODIFY_EXPR, type, sav, t);
7855       TREE_SIDE_EFFECTS (t) = 1;
7856       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7857     }
7858 }
7859
7860 /* Implement va_arg.  */
7861
7862 static tree
7863 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7864                       gimple_seq *post_p)
7865 {
7866   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
7867   tree f_gpr, f_fpr, f_ovf, f_sav;
7868   tree gpr, fpr, ovf, sav, t;
7869   int size, rsize;
7870   tree lab_false, lab_over = NULL_TREE;
7871   tree addr, t2;
7872   rtx container;
7873   int indirect_p = 0;
7874   tree ptrtype;
7875   enum machine_mode nat_mode;
7876   unsigned int arg_boundary;
7877
7878   /* Only 64bit target needs something special.  */
7879   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7880     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7881
7882   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7883   f_fpr = DECL_CHAIN (f_gpr);
7884   f_ovf = DECL_CHAIN (f_fpr);
7885   f_sav = DECL_CHAIN (f_ovf);
7886
7887   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
7888                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
7889   valist = build_va_arg_indirect_ref (valist);
7890   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7891   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7892   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7893
7894   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
7895   if (indirect_p)
7896     type = build_pointer_type (type);
7897   size = int_size_in_bytes (type);
7898   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7899
7900   nat_mode = type_natural_mode (type, NULL);
7901   switch (nat_mode)
7902     {
7903     case V8SFmode:
7904     case V8SImode:
7905     case V32QImode:
7906     case V16HImode:
7907     case V4DFmode:
7908     case V4DImode:
7909       /* Unnamed 256bit vector mode parameters are passed on stack.  */
7910       if (!TARGET_64BIT_MS_ABI)
7911         {
7912           container = NULL;
7913           break;
7914         }
7915
7916     default:
7917       container = construct_container (nat_mode, TYPE_MODE (type),
7918                                        type, 0, X86_64_REGPARM_MAX,
7919                                        X86_64_SSE_REGPARM_MAX, intreg,
7920                                        0);
7921       break;
7922     }
7923
7924   /* Pull the value out of the saved registers.  */
7925
7926   addr = create_tmp_var (ptr_type_node, "addr");
7927
7928   if (container)
7929     {
7930       int needed_intregs, needed_sseregs;
7931       bool need_temp;
7932       tree int_addr, sse_addr;
7933
7934       lab_false = create_artificial_label (UNKNOWN_LOCATION);
7935       lab_over = create_artificial_label (UNKNOWN_LOCATION);
7936
7937       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
7938
7939       need_temp = (!REG_P (container)
7940                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
7941                        || TYPE_ALIGN (type) > 128));
7942
7943       /* In case we are passing structure, verify that it is consecutive block
7944          on the register save area.  If not we need to do moves.  */
7945       if (!need_temp && !REG_P (container))
7946         {
7947           /* Verify that all registers are strictly consecutive  */
7948           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
7949             {
7950               int i;
7951
7952               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7953                 {
7954                   rtx slot = XVECEXP (container, 0, i);
7955                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
7956                       || INTVAL (XEXP (slot, 1)) != i * 16)
7957                     need_temp = 1;
7958                 }
7959             }
7960           else
7961             {
7962               int i;
7963
7964               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7965                 {
7966                   rtx slot = XVECEXP (container, 0, i);
7967                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
7968                       || INTVAL (XEXP (slot, 1)) != i * 8)
7969                     need_temp = 1;
7970                 }
7971             }
7972         }
7973       if (!need_temp)
7974         {
7975           int_addr = addr;
7976           sse_addr = addr;
7977         }
7978       else
7979         {
7980           int_addr = create_tmp_var (ptr_type_node, "int_addr");
7981           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
7982         }
7983
7984       /* First ensure that we fit completely in registers.  */
7985       if (needed_intregs)
7986         {
7987           t = build_int_cst (TREE_TYPE (gpr),
7988                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
7989           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
7990           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7991           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7992           gimplify_and_add (t, pre_p);
7993         }
7994       if (needed_sseregs)
7995         {
7996           t = build_int_cst (TREE_TYPE (fpr),
7997                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
7998                              + X86_64_REGPARM_MAX * 8);
7999           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
8000           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
8001           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
8002           gimplify_and_add (t, pre_p);
8003         }
8004
8005       /* Compute index to start of area used for integer regs.  */
8006       if (needed_intregs)
8007         {
8008           /* int_addr = gpr + sav; */
8009           t = fold_build_pointer_plus (sav, gpr);
8010           gimplify_assign (int_addr, t, pre_p);
8011         }
8012       if (needed_sseregs)
8013         {
8014           /* sse_addr = fpr + sav; */
8015           t = fold_build_pointer_plus (sav, fpr);
8016           gimplify_assign (sse_addr, t, pre_p);
8017         }
8018       if (need_temp)
8019         {
8020           int i, prev_size = 0;
8021           tree temp = create_tmp_var (type, "va_arg_tmp");
8022
8023           /* addr = &temp; */
8024           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
8025           gimplify_assign (addr, t, pre_p);
8026
8027           for (i = 0; i < XVECLEN (container, 0); i++)
8028             {
8029               rtx slot = XVECEXP (container, 0, i);
8030               rtx reg = XEXP (slot, 0);
8031               enum machine_mode mode = GET_MODE (reg);
8032               tree piece_type;
8033               tree addr_type;
8034               tree daddr_type;
8035               tree src_addr, src;
8036               int src_offset;
8037               tree dest_addr, dest;
8038               int cur_size = GET_MODE_SIZE (mode);
8039
8040               gcc_assert (prev_size <= INTVAL (XEXP (slot, 1)));
8041               prev_size = INTVAL (XEXP (slot, 1));
8042               if (prev_size + cur_size > size)
8043                 {
8044                   cur_size = size - prev_size;
8045                   mode = mode_for_size (cur_size * BITS_PER_UNIT, MODE_INT, 1);
8046                   if (mode == BLKmode)
8047                     mode = QImode;
8048                 }
8049               piece_type = lang_hooks.types.type_for_mode (mode, 1);
8050               if (mode == GET_MODE (reg))
8051                 addr_type = build_pointer_type (piece_type);
8052               else
8053                 addr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8054                                                          true);
8055               daddr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8056                                                         true);
8057
8058               if (SSE_REGNO_P (REGNO (reg)))
8059                 {
8060                   src_addr = sse_addr;
8061                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
8062                 }
8063               else
8064                 {
8065                   src_addr = int_addr;
8066                   src_offset = REGNO (reg) * 8;
8067                 }
8068               src_addr = fold_convert (addr_type, src_addr);
8069               src_addr = fold_build_pointer_plus_hwi (src_addr, src_offset);
8070
8071               dest_addr = fold_convert (daddr_type, addr);
8072               dest_addr = fold_build_pointer_plus_hwi (dest_addr, prev_size);
8073               if (cur_size == GET_MODE_SIZE (mode))
8074                 {
8075                   src = build_va_arg_indirect_ref (src_addr);
8076                   dest = build_va_arg_indirect_ref (dest_addr);
8077
8078                   gimplify_assign (dest, src, pre_p);
8079                 }
8080               else
8081                 {
8082                   tree copy
8083                     = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
8084                                        3, dest_addr, src_addr,
8085                                        size_int (cur_size));
8086                   gimplify_and_add (copy, pre_p);
8087                 }
8088               prev_size += cur_size;
8089             }
8090         }
8091
8092       if (needed_intregs)
8093         {
8094           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
8095                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
8096           gimplify_assign (gpr, t, pre_p);
8097         }
8098
8099       if (needed_sseregs)
8100         {
8101           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
8102                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
8103           gimplify_assign (fpr, t, pre_p);
8104         }
8105
8106       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
8107
8108       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
8109     }
8110
8111   /* ... otherwise out of the overflow area.  */
8112
8113   /* When we align parameter on stack for caller, if the parameter
8114      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
8115      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
8116      here with caller.  */
8117   arg_boundary = ix86_function_arg_boundary (VOIDmode, type);
8118   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
8119     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
8120
8121   /* Care for on-stack alignment if needed.  */
8122   if (arg_boundary <= 64 || size == 0)
8123     t = ovf;
8124  else
8125     {
8126       HOST_WIDE_INT align = arg_boundary / 8;
8127       t = fold_build_pointer_plus_hwi (ovf, align - 1);
8128       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
8129                   build_int_cst (TREE_TYPE (t), -align));
8130     }
8131
8132   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
8133   gimplify_assign (addr, t, pre_p);
8134
8135   t = fold_build_pointer_plus_hwi (t, rsize * UNITS_PER_WORD);
8136   gimplify_assign (unshare_expr (ovf), t, pre_p);
8137
8138   if (container)
8139     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
8140
8141   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
8142   addr = fold_convert (ptrtype, addr);
8143
8144   if (indirect_p)
8145     addr = build_va_arg_indirect_ref (addr);
8146   return build_va_arg_indirect_ref (addr);
8147 }
8148 \f
8149 /* Return true if OPNUM's MEM should be matched
8150    in movabs* patterns.  */
8151
8152 bool
8153 ix86_check_movabs (rtx insn, int opnum)
8154 {
8155   rtx set, mem;
8156
8157   set = PATTERN (insn);
8158   if (GET_CODE (set) == PARALLEL)
8159     set = XVECEXP (set, 0, 0);
8160   gcc_assert (GET_CODE (set) == SET);
8161   mem = XEXP (set, opnum);
8162   while (GET_CODE (mem) == SUBREG)
8163     mem = SUBREG_REG (mem);
8164   gcc_assert (MEM_P (mem));
8165   return volatile_ok || !MEM_VOLATILE_P (mem);
8166 }
8167 \f
8168 /* Initialize the table of extra 80387 mathematical constants.  */
8169
8170 static void
8171 init_ext_80387_constants (void)
8172 {
8173   static const char * cst[5] =
8174   {
8175     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
8176     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
8177     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
8178     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
8179     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
8180   };
8181   int i;
8182
8183   for (i = 0; i < 5; i++)
8184     {
8185       real_from_string (&ext_80387_constants_table[i], cst[i]);
8186       /* Ensure each constant is rounded to XFmode precision.  */
8187       real_convert (&ext_80387_constants_table[i],
8188                     XFmode, &ext_80387_constants_table[i]);
8189     }
8190
8191   ext_80387_constants_init = 1;
8192 }
8193
8194 /* Return non-zero if the constant is something that
8195    can be loaded with a special instruction.  */
8196
8197 int
8198 standard_80387_constant_p (rtx x)
8199 {
8200   enum machine_mode mode = GET_MODE (x);
8201
8202   REAL_VALUE_TYPE r;
8203
8204   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
8205     return -1;
8206
8207   if (x == CONST0_RTX (mode))
8208     return 1;
8209   if (x == CONST1_RTX (mode))
8210     return 2;
8211
8212   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8213
8214   /* For XFmode constants, try to find a special 80387 instruction when
8215      optimizing for size or on those CPUs that benefit from them.  */
8216   if (mode == XFmode
8217       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
8218     {
8219       int i;
8220
8221       if (! ext_80387_constants_init)
8222         init_ext_80387_constants ();
8223
8224       for (i = 0; i < 5; i++)
8225         if (real_identical (&r, &ext_80387_constants_table[i]))
8226           return i + 3;
8227     }
8228
8229   /* Load of the constant -0.0 or -1.0 will be split as
8230      fldz;fchs or fld1;fchs sequence.  */
8231   if (real_isnegzero (&r))
8232     return 8;
8233   if (real_identical (&r, &dconstm1))
8234     return 9;
8235
8236   return 0;
8237 }
8238
8239 /* Return the opcode of the special instruction to be used to load
8240    the constant X.  */
8241
8242 const char *
8243 standard_80387_constant_opcode (rtx x)
8244 {
8245   switch (standard_80387_constant_p (x))
8246     {
8247     case 1:
8248       return "fldz";
8249     case 2:
8250       return "fld1";
8251     case 3:
8252       return "fldlg2";
8253     case 4:
8254       return "fldln2";
8255     case 5:
8256       return "fldl2e";
8257     case 6:
8258       return "fldl2t";
8259     case 7:
8260       return "fldpi";
8261     case 8:
8262     case 9:
8263       return "#";
8264     default:
8265       gcc_unreachable ();
8266     }
8267 }
8268
8269 /* Return the CONST_DOUBLE representing the 80387 constant that is
8270    loaded by the specified special instruction.  The argument IDX
8271    matches the return value from standard_80387_constant_p.  */
8272
8273 rtx
8274 standard_80387_constant_rtx (int idx)
8275 {
8276   int i;
8277
8278   if (! ext_80387_constants_init)
8279     init_ext_80387_constants ();
8280
8281   switch (idx)
8282     {
8283     case 3:
8284     case 4:
8285     case 5:
8286     case 6:
8287     case 7:
8288       i = idx - 3;
8289       break;
8290
8291     default:
8292       gcc_unreachable ();
8293     }
8294
8295   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
8296                                        XFmode);
8297 }
8298
8299 /* Return 1 if X is all 0s and 2 if x is all 1s
8300    in supported SSE/AVX vector mode.  */
8301
8302 int
8303 standard_sse_constant_p (rtx x)
8304 {
8305   enum machine_mode mode = GET_MODE (x);
8306
8307   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
8308     return 1;
8309   if (vector_all_ones_operand (x, mode))
8310     switch (mode)
8311       {
8312       case V16QImode:
8313       case V8HImode:
8314       case V4SImode:
8315       case V2DImode:
8316         if (TARGET_SSE2)
8317           return 2;
8318       case V32QImode:
8319       case V16HImode:
8320       case V8SImode:
8321       case V4DImode:
8322         if (TARGET_AVX2)
8323           return 2;
8324       default:
8325         break;
8326       }
8327
8328   return 0;
8329 }
8330
8331 /* Return the opcode of the special instruction to be used to load
8332    the constant X.  */
8333
8334 const char *
8335 standard_sse_constant_opcode (rtx insn, rtx x)
8336 {
8337   switch (standard_sse_constant_p (x))
8338     {
8339     case 1:
8340       switch (get_attr_mode (insn))
8341         {
8342         case MODE_TI:
8343           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8344             return "%vpxor\t%0, %d0";
8345         case MODE_V2DF:
8346           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8347             return "%vxorpd\t%0, %d0";
8348         case MODE_V4SF:
8349           return "%vxorps\t%0, %d0";
8350
8351         case MODE_OI:
8352           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8353             return "vpxor\t%x0, %x0, %x0";
8354         case MODE_V4DF:
8355           if (!TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8356             return "vxorpd\t%x0, %x0, %x0";
8357         case MODE_V8SF:
8358           return "vxorps\t%x0, %x0, %x0";
8359
8360         default:
8361           break;
8362         }
8363
8364     case 2:
8365       if (TARGET_AVX)
8366         return "vpcmpeqd\t%0, %0, %0";
8367       else
8368         return "pcmpeqd\t%0, %0";
8369
8370     default:
8371       break;
8372     }
8373   gcc_unreachable ();
8374 }
8375
8376 /* Returns true if OP contains a symbol reference */
8377
8378 bool
8379 symbolic_reference_mentioned_p (rtx op)
8380 {
8381   const char *fmt;
8382   int i;
8383
8384   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
8385     return true;
8386
8387   fmt = GET_RTX_FORMAT (GET_CODE (op));
8388   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
8389     {
8390       if (fmt[i] == 'E')
8391         {
8392           int j;
8393
8394           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
8395             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
8396               return true;
8397         }
8398
8399       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
8400         return true;
8401     }
8402
8403   return false;
8404 }
8405
8406 /* Return true if it is appropriate to emit `ret' instructions in the
8407    body of a function.  Do this only if the epilogue is simple, needing a
8408    couple of insns.  Prior to reloading, we can't tell how many registers
8409    must be saved, so return false then.  Return false if there is no frame
8410    marker to de-allocate.  */
8411
8412 bool
8413 ix86_can_use_return_insn_p (void)
8414 {
8415   struct ix86_frame frame;
8416
8417   if (! reload_completed || frame_pointer_needed)
8418     return 0;
8419
8420   /* Don't allow more than 32k pop, since that's all we can do
8421      with one instruction.  */
8422   if (crtl->args.pops_args && crtl->args.size >= 32768)
8423     return 0;
8424
8425   ix86_compute_frame_layout (&frame);
8426   return (frame.stack_pointer_offset == UNITS_PER_WORD
8427           && (frame.nregs + frame.nsseregs) == 0);
8428 }
8429 \f
8430 /* Value should be nonzero if functions must have frame pointers.
8431    Zero means the frame pointer need not be set up (and parms may
8432    be accessed via the stack pointer) in functions that seem suitable.  */
8433
8434 static bool
8435 ix86_frame_pointer_required (void)
8436 {
8437   /* If we accessed previous frames, then the generated code expects
8438      to be able to access the saved ebp value in our frame.  */
8439   if (cfun->machine->accesses_prev_frame)
8440     return true;
8441
8442   /* Several x86 os'es need a frame pointer for other reasons,
8443      usually pertaining to setjmp.  */
8444   if (SUBTARGET_FRAME_POINTER_REQUIRED)
8445     return true;
8446
8447   /* For older 32-bit runtimes setjmp requires valid frame-pointer.  */
8448   if (TARGET_32BIT_MS_ABI && cfun->calls_setjmp)
8449     return true;
8450
8451   /* Win64 SEH, very large frames need a frame-pointer as maximum stack
8452      allocation is 4GB.  */
8453   if (TARGET_64BIT_MS_ABI && get_frame_size () > SEH_MAX_FRAME_SIZE)
8454     return true;
8455
8456   /* In ix86_option_override_internal, TARGET_OMIT_LEAF_FRAME_POINTER
8457      turns off the frame pointer by default.  Turn it back on now if
8458      we've not got a leaf function.  */
8459   if (TARGET_OMIT_LEAF_FRAME_POINTER
8460       && (!current_function_is_leaf
8461           || ix86_current_function_calls_tls_descriptor))
8462     return true;
8463
8464   if (crtl->profile && !flag_fentry)
8465     return true;
8466
8467   return false;
8468 }
8469
8470 /* Record that the current function accesses previous call frames.  */
8471
8472 void
8473 ix86_setup_frame_addresses (void)
8474 {
8475   cfun->machine->accesses_prev_frame = 1;
8476 }
8477 \f
8478 #ifndef USE_HIDDEN_LINKONCE
8479 # if defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)
8480 #  define USE_HIDDEN_LINKONCE 1
8481 # else
8482 #  define USE_HIDDEN_LINKONCE 0
8483 # endif
8484 #endif
8485
8486 static int pic_labels_used;
8487
8488 /* Fills in the label name that should be used for a pc thunk for
8489    the given register.  */
8490
8491 static void
8492 get_pc_thunk_name (char name[32], unsigned int regno)
8493 {
8494   gcc_assert (!TARGET_64BIT);
8495
8496   if (USE_HIDDEN_LINKONCE)
8497     sprintf (name, "__x86.get_pc_thunk.%s", reg_names[regno]);
8498   else
8499     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
8500 }
8501
8502
8503 /* This function generates code for -fpic that loads %ebx with
8504    the return address of the caller and then returns.  */
8505
8506 static void
8507 ix86_code_end (void)
8508 {
8509   rtx xops[2];
8510   int regno;
8511
8512   for (regno = AX_REG; regno <= SP_REG; regno++)
8513     {
8514       char name[32];
8515       tree decl;
8516
8517       if (!(pic_labels_used & (1 << regno)))
8518         continue;
8519
8520       get_pc_thunk_name (name, regno);
8521
8522       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
8523                          get_identifier (name),
8524                          build_function_type_list (void_type_node, NULL_TREE));
8525       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
8526                                        NULL_TREE, void_type_node);
8527       TREE_PUBLIC (decl) = 1;
8528       TREE_STATIC (decl) = 1;
8529
8530 #if TARGET_MACHO
8531       if (TARGET_MACHO)
8532         {
8533           switch_to_section (darwin_sections[text_coal_section]);
8534           fputs ("\t.weak_definition\t", asm_out_file);
8535           assemble_name (asm_out_file, name);
8536           fputs ("\n\t.private_extern\t", asm_out_file);
8537           assemble_name (asm_out_file, name);
8538           putc ('\n', asm_out_file);
8539           ASM_OUTPUT_LABEL (asm_out_file, name);
8540           DECL_WEAK (decl) = 1;
8541         }
8542       else
8543 #endif
8544       if (USE_HIDDEN_LINKONCE)
8545         {
8546           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
8547
8548           targetm.asm_out.unique_section (decl, 0);
8549           switch_to_section (get_named_section (decl, NULL, 0));
8550
8551           targetm.asm_out.globalize_label (asm_out_file, name);
8552           fputs ("\t.hidden\t", asm_out_file);
8553           assemble_name (asm_out_file, name);
8554           putc ('\n', asm_out_file);
8555           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
8556         }
8557       else
8558         {
8559           switch_to_section (text_section);
8560           ASM_OUTPUT_LABEL (asm_out_file, name);
8561         }
8562
8563       DECL_INITIAL (decl) = make_node (BLOCK);
8564       current_function_decl = decl;
8565       init_function_start (decl);
8566       first_function_block_is_cold = false;
8567       /* Make sure unwind info is emitted for the thunk if needed.  */
8568       final_start_function (emit_barrier (), asm_out_file, 1);
8569
8570       /* Pad stack IP move with 4 instructions (two NOPs count
8571          as one instruction).  */
8572       if (TARGET_PAD_SHORT_FUNCTION)
8573         {
8574           int i = 8;
8575
8576           while (i--)
8577             fputs ("\tnop\n", asm_out_file);
8578         }
8579
8580       xops[0] = gen_rtx_REG (Pmode, regno);
8581       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8582       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
8583       fputs ("\tret\n", asm_out_file);
8584       final_end_function ();
8585       init_insn_lengths ();
8586       free_after_compilation (cfun);
8587       set_cfun (NULL);
8588       current_function_decl = NULL;
8589     }
8590
8591   if (flag_split_stack)
8592     file_end_indicate_split_stack ();
8593 }
8594
8595 /* Emit code for the SET_GOT patterns.  */
8596
8597 const char *
8598 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
8599 {
8600   rtx xops[3];
8601
8602   xops[0] = dest;
8603
8604   if (TARGET_VXWORKS_RTP && flag_pic)
8605     {
8606       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
8607       xops[2] = gen_rtx_MEM (Pmode,
8608                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
8609       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
8610
8611       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
8612          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
8613          an unadorned address.  */
8614       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
8615       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
8616       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
8617       return "";
8618     }
8619
8620   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
8621
8622   if (!flag_pic)
8623     {
8624       if (TARGET_MACHO)
8625         /* We don't need a pic base, we're not producing pic.  */
8626         gcc_unreachable ();
8627
8628       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
8629       output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
8630       targetm.asm_out.internal_label (asm_out_file, "L",
8631                                       CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
8632     }
8633   else
8634     {
8635       char name[32];
8636       get_pc_thunk_name (name, REGNO (dest));
8637       pic_labels_used |= 1 << REGNO (dest);
8638
8639       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
8640       xops[2] = gen_rtx_MEM (QImode, xops[2]);
8641       output_asm_insn ("call\t%X2", xops);
8642
8643 #if TARGET_MACHO
8644       /* Output the Mach-O "canonical" pic base label name ("Lxx$pb") here.
8645          This is what will be referenced by the Mach-O PIC subsystem.  */
8646       if (machopic_should_output_picbase_label () || !label)
8647         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8648
8649       /* When we are restoring the pic base at the site of a nonlocal label,
8650          and we decided to emit the pic base above, we will still output a
8651          local label used for calculating the correction offset (even though
8652          the offset will be 0 in that case).  */
8653       if (label)
8654         targetm.asm_out.internal_label (asm_out_file, "L",
8655                                            CODE_LABEL_NUMBER (label));
8656 #endif
8657     }
8658
8659   if (!TARGET_MACHO)
8660     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
8661
8662   return "";
8663 }
8664
8665 /* Generate an "push" pattern for input ARG.  */
8666
8667 static rtx
8668 gen_push (rtx arg)
8669 {
8670   struct machine_function *m = cfun->machine;
8671
8672   if (m->fs.cfa_reg == stack_pointer_rtx)
8673     m->fs.cfa_offset += UNITS_PER_WORD;
8674   m->fs.sp_offset += UNITS_PER_WORD;
8675
8676   return gen_rtx_SET (VOIDmode,
8677                       gen_rtx_MEM (Pmode,
8678                                    gen_rtx_PRE_DEC (Pmode,
8679                                                     stack_pointer_rtx)),
8680                       arg);
8681 }
8682
8683 /* Generate an "pop" pattern for input ARG.  */
8684
8685 static rtx
8686 gen_pop (rtx arg)
8687 {
8688   return gen_rtx_SET (VOIDmode,
8689                       arg,
8690                       gen_rtx_MEM (Pmode,
8691                                    gen_rtx_POST_INC (Pmode,
8692                                                      stack_pointer_rtx)));
8693 }
8694
8695 /* Return >= 0 if there is an unused call-clobbered register available
8696    for the entire function.  */
8697
8698 static unsigned int
8699 ix86_select_alt_pic_regnum (void)
8700 {
8701   if (current_function_is_leaf
8702       && !crtl->profile
8703       && !ix86_current_function_calls_tls_descriptor)
8704     {
8705       int i, drap;
8706       /* Can't use the same register for both PIC and DRAP.  */
8707       if (crtl->drap_reg)
8708         drap = REGNO (crtl->drap_reg);
8709       else
8710         drap = -1;
8711       for (i = 2; i >= 0; --i)
8712         if (i != drap && !df_regs_ever_live_p (i))
8713           return i;
8714     }
8715
8716   return INVALID_REGNUM;
8717 }
8718
8719 /* Return TRUE if we need to save REGNO.  */
8720
8721 static bool
8722 ix86_save_reg (unsigned int regno, bool maybe_eh_return)
8723 {
8724   if (pic_offset_table_rtx
8725       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
8726       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8727           || crtl->profile
8728           || crtl->calls_eh_return
8729           || crtl->uses_const_pool
8730           || cfun->has_nonlocal_label))
8731     return ix86_select_alt_pic_regnum () == INVALID_REGNUM;
8732
8733   if (crtl->calls_eh_return && maybe_eh_return)
8734     {
8735       unsigned i;
8736       for (i = 0; ; i++)
8737         {
8738           unsigned test = EH_RETURN_DATA_REGNO (i);
8739           if (test == INVALID_REGNUM)
8740             break;
8741           if (test == regno)
8742             return true;
8743         }
8744     }
8745
8746   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
8747     return true;
8748
8749   return (df_regs_ever_live_p (regno)
8750           && !call_used_regs[regno]
8751           && !fixed_regs[regno]
8752           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
8753 }
8754
8755 /* Return number of saved general prupose registers.  */
8756
8757 static int
8758 ix86_nsaved_regs (void)
8759 {
8760   int nregs = 0;
8761   int regno;
8762
8763   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8764     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8765       nregs ++;
8766   return nregs;
8767 }
8768
8769 /* Return number of saved SSE registrers.  */
8770
8771 static int
8772 ix86_nsaved_sseregs (void)
8773 {
8774   int nregs = 0;
8775   int regno;
8776
8777   if (!TARGET_64BIT_MS_ABI)
8778     return 0;
8779   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8780     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8781       nregs ++;
8782   return nregs;
8783 }
8784
8785 /* Given FROM and TO register numbers, say whether this elimination is
8786    allowed.  If stack alignment is needed, we can only replace argument
8787    pointer with hard frame pointer, or replace frame pointer with stack
8788    pointer.  Otherwise, frame pointer elimination is automatically
8789    handled and all other eliminations are valid.  */
8790
8791 static bool
8792 ix86_can_eliminate (const int from, const int to)
8793 {
8794   if (stack_realign_fp)
8795     return ((from == ARG_POINTER_REGNUM
8796              && to == HARD_FRAME_POINTER_REGNUM)
8797             || (from == FRAME_POINTER_REGNUM
8798                 && to == STACK_POINTER_REGNUM));
8799   else
8800     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
8801 }
8802
8803 /* Return the offset between two registers, one to be eliminated, and the other
8804    its replacement, at the start of a routine.  */
8805
8806 HOST_WIDE_INT
8807 ix86_initial_elimination_offset (int from, int to)
8808 {
8809   struct ix86_frame frame;
8810   ix86_compute_frame_layout (&frame);
8811
8812   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
8813     return frame.hard_frame_pointer_offset;
8814   else if (from == FRAME_POINTER_REGNUM
8815            && to == HARD_FRAME_POINTER_REGNUM)
8816     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
8817   else
8818     {
8819       gcc_assert (to == STACK_POINTER_REGNUM);
8820
8821       if (from == ARG_POINTER_REGNUM)
8822         return frame.stack_pointer_offset;
8823
8824       gcc_assert (from == FRAME_POINTER_REGNUM);
8825       return frame.stack_pointer_offset - frame.frame_pointer_offset;
8826     }
8827 }
8828
8829 /* In a dynamically-aligned function, we can't know the offset from
8830    stack pointer to frame pointer, so we must ensure that setjmp
8831    eliminates fp against the hard fp (%ebp) rather than trying to
8832    index from %esp up to the top of the frame across a gap that is
8833    of unknown (at compile-time) size.  */
8834 static rtx
8835 ix86_builtin_setjmp_frame_value (void)
8836 {
8837   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
8838 }
8839
8840 /* When using -fsplit-stack, the allocation routines set a field in
8841    the TCB to the bottom of the stack plus this much space, measured
8842    in bytes.  */
8843
8844 #define SPLIT_STACK_AVAILABLE 256
8845
8846 /* Fill structure ix86_frame about frame of currently computed function.  */
8847
8848 static void
8849 ix86_compute_frame_layout (struct ix86_frame *frame)
8850 {
8851   unsigned int stack_alignment_needed;
8852   HOST_WIDE_INT offset;
8853   unsigned int preferred_alignment;
8854   HOST_WIDE_INT size = get_frame_size ();
8855   HOST_WIDE_INT to_allocate;
8856
8857   frame->nregs = ix86_nsaved_regs ();
8858   frame->nsseregs = ix86_nsaved_sseregs ();
8859
8860   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
8861   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
8862
8863   /* 64-bit MS ABI seem to require stack alignment to be always 16 except for
8864      function prologues and leaf.  */
8865   if ((TARGET_64BIT_MS_ABI && preferred_alignment < 16)
8866       && (!current_function_is_leaf || cfun->calls_alloca != 0
8867           || ix86_current_function_calls_tls_descriptor))
8868     {
8869       preferred_alignment = 16;
8870       stack_alignment_needed = 16;
8871       crtl->preferred_stack_boundary = 128;
8872       crtl->stack_alignment_needed = 128;
8873     }
8874
8875   gcc_assert (!size || stack_alignment_needed);
8876   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
8877   gcc_assert (preferred_alignment <= stack_alignment_needed);
8878
8879   /* For SEH we have to limit the amount of code movement into the prologue.
8880      At present we do this via a BLOCKAGE, at which point there's very little
8881      scheduling that can be done, which means that there's very little point
8882      in doing anything except PUSHs.  */
8883   if (TARGET_SEH)
8884     cfun->machine->use_fast_prologue_epilogue = false;
8885
8886   /* During reload iteration the amount of registers saved can change.
8887      Recompute the value as needed.  Do not recompute when amount of registers
8888      didn't change as reload does multiple calls to the function and does not
8889      expect the decision to change within single iteration.  */
8890   else if (!optimize_function_for_size_p (cfun)
8891            && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
8892     {
8893       int count = frame->nregs;
8894       struct cgraph_node *node = cgraph_get_node (current_function_decl);
8895
8896       cfun->machine->use_fast_prologue_epilogue_nregs = count;
8897
8898       /* The fast prologue uses move instead of push to save registers.  This
8899          is significantly longer, but also executes faster as modern hardware
8900          can execute the moves in parallel, but can't do that for push/pop.
8901
8902          Be careful about choosing what prologue to emit:  When function takes
8903          many instructions to execute we may use slow version as well as in
8904          case function is known to be outside hot spot (this is known with
8905          feedback only).  Weight the size of function by number of registers
8906          to save as it is cheap to use one or two push instructions but very
8907          slow to use many of them.  */
8908       if (count)
8909         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
8910       if (node->frequency < NODE_FREQUENCY_NORMAL
8911           || (flag_branch_probabilities
8912               && node->frequency < NODE_FREQUENCY_HOT))
8913         cfun->machine->use_fast_prologue_epilogue = false;
8914       else
8915         cfun->machine->use_fast_prologue_epilogue
8916            = !expensive_function_p (count);
8917     }
8918
8919   frame->save_regs_using_mov
8920     = (TARGET_PROLOGUE_USING_MOVE && cfun->machine->use_fast_prologue_epilogue
8921        /* If static stack checking is enabled and done with probes,
8922           the registers need to be saved before allocating the frame.  */
8923        && flag_stack_check != STATIC_BUILTIN_STACK_CHECK);
8924
8925   /* Skip return address.  */
8926   offset = UNITS_PER_WORD;
8927
8928   /* Skip pushed static chain.  */
8929   if (ix86_static_chain_on_stack)
8930     offset += UNITS_PER_WORD;
8931
8932   /* Skip saved base pointer.  */
8933   if (frame_pointer_needed)
8934     offset += UNITS_PER_WORD;
8935   frame->hfp_save_offset = offset;
8936
8937   /* The traditional frame pointer location is at the top of the frame.  */
8938   frame->hard_frame_pointer_offset = offset;
8939
8940   /* Register save area */
8941   offset += frame->nregs * UNITS_PER_WORD;
8942   frame->reg_save_offset = offset;
8943
8944   /* On SEH target, registers are pushed just before the frame pointer
8945      location.  */
8946   if (TARGET_SEH)
8947     frame->hard_frame_pointer_offset = offset;
8948
8949   /* Align and set SSE register save area.  */
8950   if (frame->nsseregs)
8951     {
8952       /* The only ABI that has saved SSE registers (Win64) also has a
8953          16-byte aligned default stack, and thus we don't need to be
8954          within the re-aligned local stack frame to save them.  */
8955       gcc_assert (INCOMING_STACK_BOUNDARY >= 128);
8956       offset = (offset + 16 - 1) & -16;
8957       offset += frame->nsseregs * 16;
8958     }
8959   frame->sse_reg_save_offset = offset;
8960
8961   /* The re-aligned stack starts here.  Values before this point are not
8962      directly comparable with values below this point.  In order to make
8963      sure that no value happens to be the same before and after, force
8964      the alignment computation below to add a non-zero value.  */
8965   if (stack_realign_fp)
8966     offset = (offset + stack_alignment_needed) & -stack_alignment_needed;
8967
8968   /* Va-arg area */
8969   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
8970   offset += frame->va_arg_size;
8971
8972   /* Align start of frame for local function.  */
8973   if (stack_realign_fp
8974       || offset != frame->sse_reg_save_offset
8975       || size != 0
8976       || !current_function_is_leaf
8977       || cfun->calls_alloca
8978       || ix86_current_function_calls_tls_descriptor)
8979     offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed;
8980
8981   /* Frame pointer points here.  */
8982   frame->frame_pointer_offset = offset;
8983
8984   offset += size;
8985
8986   /* Add outgoing arguments area.  Can be skipped if we eliminated
8987      all the function calls as dead code.
8988      Skipping is however impossible when function calls alloca.  Alloca
8989      expander assumes that last crtl->outgoing_args_size
8990      of stack frame are unused.  */
8991   if (ACCUMULATE_OUTGOING_ARGS
8992       && (!current_function_is_leaf || cfun->calls_alloca
8993           || ix86_current_function_calls_tls_descriptor))
8994     {
8995       offset += crtl->outgoing_args_size;
8996       frame->outgoing_arguments_size = crtl->outgoing_args_size;
8997     }
8998   else
8999     frame->outgoing_arguments_size = 0;
9000
9001   /* Align stack boundary.  Only needed if we're calling another function
9002      or using alloca.  */
9003   if (!current_function_is_leaf || cfun->calls_alloca
9004       || ix86_current_function_calls_tls_descriptor)
9005     offset = (offset + preferred_alignment - 1) & -preferred_alignment;
9006
9007   /* We've reached end of stack frame.  */
9008   frame->stack_pointer_offset = offset;
9009
9010   /* Size prologue needs to allocate.  */
9011   to_allocate = offset - frame->sse_reg_save_offset;
9012
9013   if ((!to_allocate && frame->nregs <= 1)
9014       || (TARGET_64BIT && to_allocate >= (HOST_WIDE_INT) 0x80000000))
9015     frame->save_regs_using_mov = false;
9016
9017   if (ix86_using_red_zone ()
9018       && current_function_sp_is_unchanging
9019       && current_function_is_leaf
9020       && !ix86_current_function_calls_tls_descriptor)
9021     {
9022       frame->red_zone_size = to_allocate;
9023       if (frame->save_regs_using_mov)
9024         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
9025       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
9026         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
9027     }
9028   else
9029     frame->red_zone_size = 0;
9030   frame->stack_pointer_offset -= frame->red_zone_size;
9031
9032   /* The SEH frame pointer location is near the bottom of the frame.
9033      This is enforced by the fact that the difference between the
9034      stack pointer and the frame pointer is limited to 240 bytes in
9035      the unwind data structure.  */
9036   if (TARGET_SEH)
9037     {
9038       HOST_WIDE_INT diff;
9039
9040       /* If we can leave the frame pointer where it is, do so.  Also, returns
9041          the establisher frame for __builtin_frame_address (0).  */
9042       diff = frame->stack_pointer_offset - frame->hard_frame_pointer_offset;
9043       if (diff <= SEH_MAX_FRAME_SIZE
9044           && (diff > 240 || (diff & 15) != 0)
9045           && !crtl->accesses_prior_frames)
9046         {
9047           /* Ideally we'd determine what portion of the local stack frame
9048              (within the constraint of the lowest 240) is most heavily used.
9049              But without that complication, simply bias the frame pointer
9050              by 128 bytes so as to maximize the amount of the local stack
9051              frame that is addressable with 8-bit offsets.  */
9052           frame->hard_frame_pointer_offset = frame->stack_pointer_offset - 128;
9053         }
9054     }
9055 }
9056
9057 /* This is semi-inlined memory_address_length, but simplified
9058    since we know that we're always dealing with reg+offset, and
9059    to avoid having to create and discard all that rtl.  */
9060
9061 static inline int
9062 choose_baseaddr_len (unsigned int regno, HOST_WIDE_INT offset)
9063 {
9064   int len = 4;
9065
9066   if (offset == 0)
9067     {
9068       /* EBP and R13 cannot be encoded without an offset.  */
9069       len = (regno == BP_REG || regno == R13_REG);
9070     }
9071   else if (IN_RANGE (offset, -128, 127))
9072     len = 1;
9073
9074   /* ESP and R12 must be encoded with a SIB byte.  */
9075   if (regno == SP_REG || regno == R12_REG)
9076     len++;
9077
9078   return len;
9079 }
9080
9081 /* Return an RTX that points to CFA_OFFSET within the stack frame.
9082    The valid base registers are taken from CFUN->MACHINE->FS.  */
9083
9084 static rtx
9085 choose_baseaddr (HOST_WIDE_INT cfa_offset)
9086 {
9087   const struct machine_function *m = cfun->machine;
9088   rtx base_reg = NULL;
9089   HOST_WIDE_INT base_offset = 0;
9090
9091   if (m->use_fast_prologue_epilogue)
9092     {
9093       /* Choose the base register most likely to allow the most scheduling
9094          opportunities.  Generally FP is valid througout the function,
9095          while DRAP must be reloaded within the epilogue.  But choose either
9096          over the SP due to increased encoding size.  */
9097
9098       if (m->fs.fp_valid)
9099         {
9100           base_reg = hard_frame_pointer_rtx;
9101           base_offset = m->fs.fp_offset - cfa_offset;
9102         }
9103       else if (m->fs.drap_valid)
9104         {
9105           base_reg = crtl->drap_reg;
9106           base_offset = 0 - cfa_offset;
9107         }
9108       else if (m->fs.sp_valid)
9109         {
9110           base_reg = stack_pointer_rtx;
9111           base_offset = m->fs.sp_offset - cfa_offset;
9112         }
9113     }
9114   else
9115     {
9116       HOST_WIDE_INT toffset;
9117       int len = 16, tlen;
9118
9119       /* Choose the base register with the smallest address encoding.
9120          With a tie, choose FP > DRAP > SP.  */
9121       if (m->fs.sp_valid)
9122         {
9123           base_reg = stack_pointer_rtx;
9124           base_offset = m->fs.sp_offset - cfa_offset;
9125           len = choose_baseaddr_len (STACK_POINTER_REGNUM, base_offset);
9126         }
9127       if (m->fs.drap_valid)
9128         {
9129           toffset = 0 - cfa_offset;
9130           tlen = choose_baseaddr_len (REGNO (crtl->drap_reg), toffset);
9131           if (tlen <= len)
9132             {
9133               base_reg = crtl->drap_reg;
9134               base_offset = toffset;
9135               len = tlen;
9136             }
9137         }
9138       if (m->fs.fp_valid)
9139         {
9140           toffset = m->fs.fp_offset - cfa_offset;
9141           tlen = choose_baseaddr_len (HARD_FRAME_POINTER_REGNUM, toffset);
9142           if (tlen <= len)
9143             {
9144               base_reg = hard_frame_pointer_rtx;
9145               base_offset = toffset;
9146               len = tlen;
9147             }
9148         }
9149     }
9150   gcc_assert (base_reg != NULL);
9151
9152   return plus_constant (base_reg, base_offset);
9153 }
9154
9155 /* Emit code to save registers in the prologue.  */
9156
9157 static void
9158 ix86_emit_save_regs (void)
9159 {
9160   unsigned int regno;
9161   rtx insn;
9162
9163   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
9164     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9165       {
9166         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
9167         RTX_FRAME_RELATED_P (insn) = 1;
9168       }
9169 }
9170
9171 /* Emit a single register save at CFA - CFA_OFFSET.  */
9172
9173 static void
9174 ix86_emit_save_reg_using_mov (enum machine_mode mode, unsigned int regno,
9175                               HOST_WIDE_INT cfa_offset)
9176 {
9177   struct machine_function *m = cfun->machine;
9178   rtx reg = gen_rtx_REG (mode, regno);
9179   rtx mem, addr, base, insn;
9180
9181   addr = choose_baseaddr (cfa_offset);
9182   mem = gen_frame_mem (mode, addr);
9183
9184   /* For SSE saves, we need to indicate the 128-bit alignment.  */
9185   set_mem_align (mem, GET_MODE_ALIGNMENT (mode));
9186
9187   insn = emit_move_insn (mem, reg);
9188   RTX_FRAME_RELATED_P (insn) = 1;
9189
9190   base = addr;
9191   if (GET_CODE (base) == PLUS)
9192     base = XEXP (base, 0);
9193   gcc_checking_assert (REG_P (base));
9194
9195   /* When saving registers into a re-aligned local stack frame, avoid
9196      any tricky guessing by dwarf2out.  */
9197   if (m->fs.realigned)
9198     {
9199       gcc_checking_assert (stack_realign_drap);
9200
9201       if (regno == REGNO (crtl->drap_reg))
9202         {
9203           /* A bit of a hack.  We force the DRAP register to be saved in
9204              the re-aligned stack frame, which provides us with a copy
9205              of the CFA that will last past the prologue.  Install it.  */
9206           gcc_checking_assert (cfun->machine->fs.fp_valid);
9207           addr = plus_constant (hard_frame_pointer_rtx,
9208                                 cfun->machine->fs.fp_offset - cfa_offset);
9209           mem = gen_rtx_MEM (mode, addr);
9210           add_reg_note (insn, REG_CFA_DEF_CFA, mem);
9211         }
9212       else
9213         {
9214           /* The frame pointer is a stable reference within the
9215              aligned frame.  Use it.  */
9216           gcc_checking_assert (cfun->machine->fs.fp_valid);
9217           addr = plus_constant (hard_frame_pointer_rtx,
9218                                 cfun->machine->fs.fp_offset - cfa_offset);
9219           mem = gen_rtx_MEM (mode, addr);
9220           add_reg_note (insn, REG_CFA_EXPRESSION,
9221                         gen_rtx_SET (VOIDmode, mem, reg));
9222         }
9223     }
9224
9225   /* The memory may not be relative to the current CFA register,
9226      which means that we may need to generate a new pattern for
9227      use by the unwind info.  */
9228   else if (base != m->fs.cfa_reg)
9229     {
9230       addr = plus_constant (m->fs.cfa_reg, m->fs.cfa_offset - cfa_offset);
9231       mem = gen_rtx_MEM (mode, addr);
9232       add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (VOIDmode, mem, reg));
9233     }
9234 }
9235
9236 /* Emit code to save registers using MOV insns.
9237    First register is stored at CFA - CFA_OFFSET.  */
9238 static void
9239 ix86_emit_save_regs_using_mov (HOST_WIDE_INT cfa_offset)
9240 {
9241   unsigned int regno;
9242
9243   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9244     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9245       {
9246         ix86_emit_save_reg_using_mov (Pmode, regno, cfa_offset);
9247         cfa_offset -= UNITS_PER_WORD;
9248       }
9249 }
9250
9251 /* Emit code to save SSE registers using MOV insns.
9252    First register is stored at CFA - CFA_OFFSET.  */
9253 static void
9254 ix86_emit_save_sse_regs_using_mov (HOST_WIDE_INT cfa_offset)
9255 {
9256   unsigned int regno;
9257
9258   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9259     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9260       {
9261         ix86_emit_save_reg_using_mov (V4SFmode, regno, cfa_offset);
9262         cfa_offset -= 16;
9263       }
9264 }
9265
9266 static GTY(()) rtx queued_cfa_restores;
9267
9268 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
9269    manipulation insn.  The value is on the stack at CFA - CFA_OFFSET.
9270    Don't add the note if the previously saved value will be left untouched
9271    within stack red-zone till return, as unwinders can find the same value
9272    in the register and on the stack.  */
9273
9274 static void
9275 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT cfa_offset)
9276 {
9277   if (!crtl->shrink_wrapped
9278       && cfa_offset <= cfun->machine->fs.red_zone_offset)
9279     return;
9280
9281   if (insn)
9282     {
9283       add_reg_note (insn, REG_CFA_RESTORE, reg);
9284       RTX_FRAME_RELATED_P (insn) = 1;
9285     }
9286   else
9287     queued_cfa_restores
9288       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
9289 }
9290
9291 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
9292
9293 static void
9294 ix86_add_queued_cfa_restore_notes (rtx insn)
9295 {
9296   rtx last;
9297   if (!queued_cfa_restores)
9298     return;
9299   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
9300     ;
9301   XEXP (last, 1) = REG_NOTES (insn);
9302   REG_NOTES (insn) = queued_cfa_restores;
9303   queued_cfa_restores = NULL_RTX;
9304   RTX_FRAME_RELATED_P (insn) = 1;
9305 }
9306
9307 /* Expand prologue or epilogue stack adjustment.
9308    The pattern exist to put a dependency on all ebp-based memory accesses.
9309    STYLE should be negative if instructions should be marked as frame related,
9310    zero if %r11 register is live and cannot be freely used and positive
9311    otherwise.  */
9312
9313 static void
9314 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
9315                            int style, bool set_cfa)
9316 {
9317   struct machine_function *m = cfun->machine;
9318   rtx insn;
9319   bool add_frame_related_expr = false;
9320
9321   if (! TARGET_64BIT)
9322     insn = gen_pro_epilogue_adjust_stack_si_add (dest, src, offset);
9323   else if (x86_64_immediate_operand (offset, DImode))
9324     insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, offset);
9325   else
9326     {
9327       rtx tmp;
9328       /* r11 is used by indirect sibcall return as well, set before the
9329          epilogue and used after the epilogue.  */
9330       if (style)
9331         tmp = gen_rtx_REG (DImode, R11_REG);
9332       else
9333         {
9334           gcc_assert (src != hard_frame_pointer_rtx
9335                       && dest != hard_frame_pointer_rtx);
9336           tmp = hard_frame_pointer_rtx;
9337         }
9338       insn = emit_insn (gen_rtx_SET (DImode, tmp, offset));
9339       if (style < 0)
9340         add_frame_related_expr = true;
9341
9342       insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, tmp);
9343     }
9344
9345   insn = emit_insn (insn);
9346   if (style >= 0)
9347     ix86_add_queued_cfa_restore_notes (insn);
9348
9349   if (set_cfa)
9350     {
9351       rtx r;
9352
9353       gcc_assert (m->fs.cfa_reg == src);
9354       m->fs.cfa_offset += INTVAL (offset);
9355       m->fs.cfa_reg = dest;
9356
9357       r = gen_rtx_PLUS (Pmode, src, offset);
9358       r = gen_rtx_SET (VOIDmode, dest, r);
9359       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
9360       RTX_FRAME_RELATED_P (insn) = 1;
9361     }
9362   else if (style < 0)
9363     {
9364       RTX_FRAME_RELATED_P (insn) = 1;
9365       if (add_frame_related_expr)
9366         {
9367           rtx r = gen_rtx_PLUS (Pmode, src, offset);
9368           r = gen_rtx_SET (VOIDmode, dest, r);
9369           add_reg_note (insn, REG_FRAME_RELATED_EXPR, r);
9370         }
9371     }
9372
9373   if (dest == stack_pointer_rtx)
9374     {
9375       HOST_WIDE_INT ooffset = m->fs.sp_offset;
9376       bool valid = m->fs.sp_valid;
9377
9378       if (src == hard_frame_pointer_rtx)
9379         {
9380           valid = m->fs.fp_valid;
9381           ooffset = m->fs.fp_offset;
9382         }
9383       else if (src == crtl->drap_reg)
9384         {
9385           valid = m->fs.drap_valid;
9386           ooffset = 0;
9387         }
9388       else
9389         {
9390           /* Else there are two possibilities: SP itself, which we set
9391              up as the default above.  Or EH_RETURN_STACKADJ_RTX, which is
9392              taken care of this by hand along the eh_return path.  */
9393           gcc_checking_assert (src == stack_pointer_rtx
9394                                || offset == const0_rtx);
9395         }
9396
9397       m->fs.sp_offset = ooffset - INTVAL (offset);
9398       m->fs.sp_valid = valid;
9399     }
9400 }
9401
9402 /* Find an available register to be used as dynamic realign argument
9403    pointer regsiter.  Such a register will be written in prologue and
9404    used in begin of body, so it must not be
9405         1. parameter passing register.
9406         2. GOT pointer.
9407    We reuse static-chain register if it is available.  Otherwise, we
9408    use DI for i386 and R13 for x86-64.  We chose R13 since it has
9409    shorter encoding.
9410
9411    Return: the regno of chosen register.  */
9412
9413 static unsigned int
9414 find_drap_reg (void)
9415 {
9416   tree decl = cfun->decl;
9417
9418   if (TARGET_64BIT)
9419     {
9420       /* Use R13 for nested function or function need static chain.
9421          Since function with tail call may use any caller-saved
9422          registers in epilogue, DRAP must not use caller-saved
9423          register in such case.  */
9424       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9425         return R13_REG;
9426
9427       return R10_REG;
9428     }
9429   else
9430     {
9431       /* Use DI for nested function or function need static chain.
9432          Since function with tail call may use any caller-saved
9433          registers in epilogue, DRAP must not use caller-saved
9434          register in such case.  */
9435       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9436         return DI_REG;
9437
9438       /* Reuse static chain register if it isn't used for parameter
9439          passing.  */
9440       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2)
9441         {
9442           unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
9443           if ((ccvt & (IX86_CALLCVT_FASTCALL | IX86_CALLCVT_THISCALL)) == 0)
9444             return CX_REG;
9445         }
9446       return DI_REG;
9447     }
9448 }
9449
9450 /* Return minimum incoming stack alignment.  */
9451
9452 static unsigned int
9453 ix86_minimum_incoming_stack_boundary (bool sibcall)
9454 {
9455   unsigned int incoming_stack_boundary;
9456
9457   /* Prefer the one specified at command line. */
9458   if (ix86_user_incoming_stack_boundary)
9459     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
9460   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
9461      if -mstackrealign is used, it isn't used for sibcall check and
9462      estimated stack alignment is 128bit.  */
9463   else if (!sibcall
9464            && !TARGET_64BIT
9465            && ix86_force_align_arg_pointer
9466            && crtl->stack_alignment_estimated == 128)
9467     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9468   else
9469     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
9470
9471   /* Incoming stack alignment can be changed on individual functions
9472      via force_align_arg_pointer attribute.  We use the smallest
9473      incoming stack boundary.  */
9474   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
9475       && lookup_attribute (ix86_force_align_arg_pointer_string,
9476                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
9477     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9478
9479   /* The incoming stack frame has to be aligned at least at
9480      parm_stack_boundary.  */
9481   if (incoming_stack_boundary < crtl->parm_stack_boundary)
9482     incoming_stack_boundary = crtl->parm_stack_boundary;
9483
9484   /* Stack at entrance of main is aligned by runtime.  We use the
9485      smallest incoming stack boundary. */
9486   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
9487       && DECL_NAME (current_function_decl)
9488       && MAIN_NAME_P (DECL_NAME (current_function_decl))
9489       && DECL_FILE_SCOPE_P (current_function_decl))
9490     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
9491
9492   return incoming_stack_boundary;
9493 }
9494
9495 /* Update incoming stack boundary and estimated stack alignment.  */
9496
9497 static void
9498 ix86_update_stack_boundary (void)
9499 {
9500   ix86_incoming_stack_boundary
9501     = ix86_minimum_incoming_stack_boundary (false);
9502
9503   /* x86_64 vararg needs 16byte stack alignment for register save
9504      area.  */
9505   if (TARGET_64BIT
9506       && cfun->stdarg
9507       && crtl->stack_alignment_estimated < 128)
9508     crtl->stack_alignment_estimated = 128;
9509 }
9510
9511 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
9512    needed or an rtx for DRAP otherwise.  */
9513
9514 static rtx
9515 ix86_get_drap_rtx (void)
9516 {
9517   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
9518     crtl->need_drap = true;
9519
9520   if (stack_realign_drap)
9521     {
9522       /* Assign DRAP to vDRAP and returns vDRAP */
9523       unsigned int regno = find_drap_reg ();
9524       rtx drap_vreg;
9525       rtx arg_ptr;
9526       rtx seq, insn;
9527
9528       arg_ptr = gen_rtx_REG (Pmode, regno);
9529       crtl->drap_reg = arg_ptr;
9530
9531       start_sequence ();
9532       drap_vreg = copy_to_reg (arg_ptr);
9533       seq = get_insns ();
9534       end_sequence ();
9535
9536       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
9537       if (!optimize)
9538         {
9539           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
9540           RTX_FRAME_RELATED_P (insn) = 1;
9541         }
9542       return drap_vreg;
9543     }
9544   else
9545     return NULL;
9546 }
9547
9548 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
9549
9550 static rtx
9551 ix86_internal_arg_pointer (void)
9552 {
9553   return virtual_incoming_args_rtx;
9554 }
9555
9556 struct scratch_reg {
9557   rtx reg;
9558   bool saved;
9559 };
9560
9561 /* Return a short-lived scratch register for use on function entry.
9562    In 32-bit mode, it is valid only after the registers are saved
9563    in the prologue.  This register must be released by means of
9564    release_scratch_register_on_entry once it is dead.  */
9565
9566 static void
9567 get_scratch_register_on_entry (struct scratch_reg *sr)
9568 {
9569   int regno;
9570
9571   sr->saved = false;
9572
9573   if (TARGET_64BIT)
9574     {
9575       /* We always use R11 in 64-bit mode.  */
9576       regno = R11_REG;
9577     }
9578   else
9579     {
9580       tree decl = current_function_decl, fntype = TREE_TYPE (decl);
9581       bool fastcall_p
9582         = lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
9583       bool thiscall_p
9584         = lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
9585       bool static_chain_p = DECL_STATIC_CHAIN (decl);
9586       int regparm = ix86_function_regparm (fntype, decl);
9587       int drap_regno
9588         = crtl->drap_reg ? REGNO (crtl->drap_reg) : INVALID_REGNUM;
9589
9590       /* 'fastcall' sets regparm to 2, uses ecx/edx for arguments and eax
9591           for the static chain register.  */
9592       if ((regparm < 1 || (fastcall_p && !static_chain_p))
9593           && drap_regno != AX_REG)
9594         regno = AX_REG;
9595       /* 'thiscall' sets regparm to 1, uses ecx for arguments and edx
9596           for the static chain register.  */
9597       else if (thiscall_p && !static_chain_p && drap_regno != AX_REG)
9598         regno = AX_REG;
9599       else if (regparm < 2 && !thiscall_p && drap_regno != DX_REG)
9600         regno = DX_REG;
9601       /* ecx is the static chain register.  */
9602       else if (regparm < 3 && !fastcall_p && !thiscall_p
9603                && !static_chain_p
9604                && drap_regno != CX_REG)
9605         regno = CX_REG;
9606       else if (ix86_save_reg (BX_REG, true))
9607         regno = BX_REG;
9608       /* esi is the static chain register.  */
9609       else if (!(regparm == 3 && static_chain_p)
9610                && ix86_save_reg (SI_REG, true))
9611         regno = SI_REG;
9612       else if (ix86_save_reg (DI_REG, true))
9613         regno = DI_REG;
9614       else
9615         {
9616           regno = (drap_regno == AX_REG ? DX_REG : AX_REG);
9617           sr->saved = true;
9618         }
9619     }
9620
9621   sr->reg = gen_rtx_REG (Pmode, regno);
9622   if (sr->saved)
9623     {
9624       rtx insn = emit_insn (gen_push (sr->reg));
9625       RTX_FRAME_RELATED_P (insn) = 1;
9626     }
9627 }
9628
9629 /* Release a scratch register obtained from the preceding function.  */
9630
9631 static void
9632 release_scratch_register_on_entry (struct scratch_reg *sr)
9633 {
9634   if (sr->saved)
9635     {
9636       struct machine_function *m = cfun->machine;
9637       rtx x, insn = emit_insn (gen_pop (sr->reg));
9638
9639       /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
9640       RTX_FRAME_RELATED_P (insn) = 1;
9641       x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
9642       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
9643       add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
9644       m->fs.sp_offset -= UNITS_PER_WORD;
9645     }
9646 }
9647
9648 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
9649
9650 /* Emit code to adjust the stack pointer by SIZE bytes while probing it.  */
9651
9652 static void
9653 ix86_adjust_stack_and_probe (const HOST_WIDE_INT size)
9654 {
9655   /* We skip the probe for the first interval + a small dope of 4 words and
9656      probe that many bytes past the specified size to maintain a protection
9657      area at the botton of the stack.  */
9658   const int dope = 4 * UNITS_PER_WORD;
9659   rtx size_rtx = GEN_INT (size), last;
9660
9661   /* See if we have a constant small number of probes to generate.  If so,
9662      that's the easy case.  The run-time loop is made up of 11 insns in the
9663      generic case while the compile-time loop is made up of 3+2*(n-1) insns
9664      for n # of intervals.  */
9665   if (size <= 5 * PROBE_INTERVAL)
9666     {
9667       HOST_WIDE_INT i, adjust;
9668       bool first_probe = true;
9669
9670       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
9671          values of N from 1 until it exceeds SIZE.  If only one probe is
9672          needed, this will not generate any code.  Then adjust and probe
9673          to PROBE_INTERVAL + SIZE.  */
9674       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9675         {
9676           if (first_probe)
9677             {
9678               adjust = 2 * PROBE_INTERVAL + dope;
9679               first_probe = false;
9680             }
9681           else
9682             adjust = PROBE_INTERVAL;
9683
9684           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9685                                   plus_constant (stack_pointer_rtx, -adjust)));
9686           emit_stack_probe (stack_pointer_rtx);
9687         }
9688
9689       if (first_probe)
9690         adjust = size + PROBE_INTERVAL + dope;
9691       else
9692         adjust = size + PROBE_INTERVAL - i;
9693
9694       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9695                               plus_constant (stack_pointer_rtx, -adjust)));
9696       emit_stack_probe (stack_pointer_rtx);
9697
9698       /* Adjust back to account for the additional first interval.  */
9699       last = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9700                                      plus_constant (stack_pointer_rtx,
9701                                                     PROBE_INTERVAL + dope)));
9702     }
9703
9704   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9705      extra careful with variables wrapping around because we might be at
9706      the very top (or the very bottom) of the address space and we have
9707      to be able to handle this case properly; in particular, we use an
9708      equality test for the loop condition.  */
9709   else
9710     {
9711       HOST_WIDE_INT rounded_size;
9712       struct scratch_reg sr;
9713
9714       get_scratch_register_on_entry (&sr);
9715
9716
9717       /* Step 1: round SIZE to the previous multiple of the interval.  */
9718
9719       rounded_size = size & -PROBE_INTERVAL;
9720
9721
9722       /* Step 2: compute initial and final value of the loop counter.  */
9723
9724       /* SP = SP_0 + PROBE_INTERVAL.  */
9725       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9726                               plus_constant (stack_pointer_rtx,
9727                                              - (PROBE_INTERVAL + dope))));
9728
9729       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
9730       emit_move_insn (sr.reg, GEN_INT (-rounded_size));
9731       emit_insn (gen_rtx_SET (VOIDmode, sr.reg,
9732                               gen_rtx_PLUS (Pmode, sr.reg,
9733                                             stack_pointer_rtx)));
9734
9735
9736       /* Step 3: the loop
9737
9738          while (SP != LAST_ADDR)
9739            {
9740              SP = SP + PROBE_INTERVAL
9741              probe at SP
9742            }
9743
9744          adjusts SP and probes to PROBE_INTERVAL + N * PROBE_INTERVAL for
9745          values of N from 1 until it is equal to ROUNDED_SIZE.  */
9746
9747       emit_insn (ix86_gen_adjust_stack_and_probe (sr.reg, sr.reg, size_rtx));
9748
9749
9750       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
9751          assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
9752
9753       if (size != rounded_size)
9754         {
9755           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9756                                   plus_constant (stack_pointer_rtx,
9757                                                  rounded_size - size)));
9758           emit_stack_probe (stack_pointer_rtx);
9759         }
9760
9761       /* Adjust back to account for the additional first interval.  */
9762       last = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9763                                      plus_constant (stack_pointer_rtx,
9764                                                     PROBE_INTERVAL + dope)));
9765
9766       release_scratch_register_on_entry (&sr);
9767     }
9768
9769   gcc_assert (cfun->machine->fs.cfa_reg != stack_pointer_rtx);
9770
9771   /* Even if the stack pointer isn't the CFA register, we need to correctly
9772      describe the adjustments made to it, in particular differentiate the
9773      frame-related ones from the frame-unrelated ones.  */
9774   if (size > 0)
9775     {
9776       rtx expr = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (2));
9777       XVECEXP (expr, 0, 0)
9778         = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9779                        plus_constant (stack_pointer_rtx, -size));
9780       XVECEXP (expr, 0, 1)
9781         = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9782                        plus_constant (stack_pointer_rtx,
9783                                       PROBE_INTERVAL + dope + size));
9784       add_reg_note (last, REG_FRAME_RELATED_EXPR, expr);
9785       RTX_FRAME_RELATED_P (last) = 1;
9786
9787       cfun->machine->fs.sp_offset += size;
9788     }
9789
9790   /* Make sure nothing is scheduled before we are done.  */
9791   emit_insn (gen_blockage ());
9792 }
9793
9794 /* Adjust the stack pointer up to REG while probing it.  */
9795
9796 const char *
9797 output_adjust_stack_and_probe (rtx reg)
9798 {
9799   static int labelno = 0;
9800   char loop_lab[32], end_lab[32];
9801   rtx xops[2];
9802
9803   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9804   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9805
9806   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9807
9808   /* Jump to END_LAB if SP == LAST_ADDR.  */
9809   xops[0] = stack_pointer_rtx;
9810   xops[1] = reg;
9811   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9812   fputs ("\tje\t", asm_out_file);
9813   assemble_name_raw (asm_out_file, end_lab);
9814   fputc ('\n', asm_out_file);
9815
9816   /* SP = SP + PROBE_INTERVAL.  */
9817   xops[1] = GEN_INT (PROBE_INTERVAL);
9818   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9819
9820   /* Probe at SP.  */
9821   xops[1] = const0_rtx;
9822   output_asm_insn ("or%z0\t{%1, (%0)|DWORD PTR [%0], %1}", xops);
9823
9824   fprintf (asm_out_file, "\tjmp\t");
9825   assemble_name_raw (asm_out_file, loop_lab);
9826   fputc ('\n', asm_out_file);
9827
9828   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9829
9830   return "";
9831 }
9832
9833 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
9834    inclusive.  These are offsets from the current stack pointer.  */
9835
9836 static void
9837 ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
9838 {
9839   /* See if we have a constant small number of probes to generate.  If so,
9840      that's the easy case.  The run-time loop is made up of 7 insns in the
9841      generic case while the compile-time loop is made up of n insns for n #
9842      of intervals.  */
9843   if (size <= 7 * PROBE_INTERVAL)
9844     {
9845       HOST_WIDE_INT i;
9846
9847       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
9848          it exceeds SIZE.  If only one probe is needed, this will not
9849          generate any code.  Then probe at FIRST + SIZE.  */
9850       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9851         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
9852
9853       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
9854     }
9855
9856   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9857      extra careful with variables wrapping around because we might be at
9858      the very top (or the very bottom) of the address space and we have
9859      to be able to handle this case properly; in particular, we use an
9860      equality test for the loop condition.  */
9861   else
9862     {
9863       HOST_WIDE_INT rounded_size, last;
9864       struct scratch_reg sr;
9865
9866       get_scratch_register_on_entry (&sr);
9867
9868
9869       /* Step 1: round SIZE to the previous multiple of the interval.  */
9870
9871       rounded_size = size & -PROBE_INTERVAL;
9872
9873
9874       /* Step 2: compute initial and final value of the loop counter.  */
9875
9876       /* TEST_OFFSET = FIRST.  */
9877       emit_move_insn (sr.reg, GEN_INT (-first));
9878
9879       /* LAST_OFFSET = FIRST + ROUNDED_SIZE.  */
9880       last = first + rounded_size;
9881
9882
9883       /* Step 3: the loop
9884
9885          while (TEST_ADDR != LAST_ADDR)
9886            {
9887              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
9888              probe at TEST_ADDR
9889            }
9890
9891          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
9892          until it is equal to ROUNDED_SIZE.  */
9893
9894       emit_insn (ix86_gen_probe_stack_range (sr.reg, sr.reg, GEN_INT (-last)));
9895
9896
9897       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
9898          that SIZE is equal to ROUNDED_SIZE.  */
9899
9900       if (size != rounded_size)
9901         emit_stack_probe (plus_constant (gen_rtx_PLUS (Pmode,
9902                                                        stack_pointer_rtx,
9903                                                        sr.reg),
9904                                          rounded_size - size));
9905
9906       release_scratch_register_on_entry (&sr);
9907     }
9908
9909   /* Make sure nothing is scheduled before we are done.  */
9910   emit_insn (gen_blockage ());
9911 }
9912
9913 /* Probe a range of stack addresses from REG to END, inclusive.  These are
9914    offsets from the current stack pointer.  */
9915
9916 const char *
9917 output_probe_stack_range (rtx reg, rtx end)
9918 {
9919   static int labelno = 0;
9920   char loop_lab[32], end_lab[32];
9921   rtx xops[3];
9922
9923   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9924   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9925
9926   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9927
9928   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
9929   xops[0] = reg;
9930   xops[1] = end;
9931   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9932   fputs ("\tje\t", asm_out_file);
9933   assemble_name_raw (asm_out_file, end_lab);
9934   fputc ('\n', asm_out_file);
9935
9936   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
9937   xops[1] = GEN_INT (PROBE_INTERVAL);
9938   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9939
9940   /* Probe at TEST_ADDR.  */
9941   xops[0] = stack_pointer_rtx;
9942   xops[1] = reg;
9943   xops[2] = const0_rtx;
9944   output_asm_insn ("or%z0\t{%2, (%0,%1)|DWORD PTR [%0+%1], %2}", xops);
9945
9946   fprintf (asm_out_file, "\tjmp\t");
9947   assemble_name_raw (asm_out_file, loop_lab);
9948   fputc ('\n', asm_out_file);
9949
9950   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9951
9952   return "";
9953 }
9954
9955 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
9956    to be generated in correct form.  */
9957 static void
9958 ix86_finalize_stack_realign_flags (void)
9959 {
9960   /* Check if stack realign is really needed after reload, and
9961      stores result in cfun */
9962   unsigned int incoming_stack_boundary
9963     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
9964        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
9965   unsigned int stack_realign = (incoming_stack_boundary
9966                                 < (current_function_is_leaf
9967                                    ? crtl->max_used_stack_slot_alignment
9968                                    : crtl->stack_alignment_needed));
9969
9970   if (crtl->stack_realign_finalized)
9971     {
9972       /* After stack_realign_needed is finalized, we can't no longer
9973          change it.  */
9974       gcc_assert (crtl->stack_realign_needed == stack_realign);
9975       return;
9976     }
9977
9978   /* If the only reason for frame_pointer_needed is that we conservatively
9979      assumed stack realignment might be needed, but in the end nothing that
9980      needed the stack alignment had been spilled, clear frame_pointer_needed
9981      and say we don't need stack realignment.  */
9982   if (stack_realign
9983       && !crtl->need_drap
9984       && frame_pointer_needed
9985       && current_function_is_leaf
9986       && flag_omit_frame_pointer
9987       && current_function_sp_is_unchanging
9988       && !ix86_current_function_calls_tls_descriptor
9989       && !crtl->accesses_prior_frames
9990       && !cfun->calls_alloca
9991       && !crtl->calls_eh_return
9992       && !(flag_stack_check && STACK_CHECK_MOVING_SP)
9993       && !ix86_frame_pointer_required ()
9994       && get_frame_size () == 0
9995       && ix86_nsaved_sseregs () == 0
9996       && ix86_varargs_gpr_size + ix86_varargs_fpr_size == 0)
9997     {
9998       HARD_REG_SET set_up_by_prologue, prologue_used;
9999       basic_block bb;
10000
10001       CLEAR_HARD_REG_SET (prologue_used);
10002       CLEAR_HARD_REG_SET (set_up_by_prologue);
10003       add_to_hard_reg_set (&set_up_by_prologue, Pmode, STACK_POINTER_REGNUM);
10004       add_to_hard_reg_set (&set_up_by_prologue, Pmode, ARG_POINTER_REGNUM);
10005       add_to_hard_reg_set (&set_up_by_prologue, Pmode,
10006                            HARD_FRAME_POINTER_REGNUM);
10007       FOR_EACH_BB (bb)
10008         {
10009           rtx insn;
10010           FOR_BB_INSNS (bb, insn)
10011             if (NONDEBUG_INSN_P (insn)
10012                 && requires_stack_frame_p (insn, prologue_used,
10013                                            set_up_by_prologue))
10014               {
10015                 crtl->stack_realign_needed = stack_realign;
10016                 crtl->stack_realign_finalized = true;
10017                 return;
10018               }
10019         }
10020
10021       frame_pointer_needed = false;
10022       stack_realign = false;
10023       crtl->max_used_stack_slot_alignment = incoming_stack_boundary;
10024       crtl->stack_alignment_needed = incoming_stack_boundary;
10025       crtl->stack_alignment_estimated = incoming_stack_boundary;
10026       if (crtl->preferred_stack_boundary > incoming_stack_boundary)
10027         crtl->preferred_stack_boundary = incoming_stack_boundary;
10028       df_finish_pass (true);
10029       df_scan_alloc (NULL);
10030       df_scan_blocks ();
10031       df_compute_regs_ever_live (true);
10032       df_analyze ();
10033     }
10034
10035   crtl->stack_realign_needed = stack_realign;
10036   crtl->stack_realign_finalized = true;
10037 }
10038
10039 /* Expand the prologue into a bunch of separate insns.  */
10040
10041 void
10042 ix86_expand_prologue (void)
10043 {
10044   struct machine_function *m = cfun->machine;
10045   rtx insn, t;
10046   bool pic_reg_used;
10047   struct ix86_frame frame;
10048   HOST_WIDE_INT allocate;
10049   bool int_registers_saved;
10050   bool sse_registers_saved;
10051
10052   ix86_finalize_stack_realign_flags ();
10053
10054   /* DRAP should not coexist with stack_realign_fp */
10055   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
10056
10057   memset (&m->fs, 0, sizeof (m->fs));
10058
10059   /* Initialize CFA state for before the prologue.  */
10060   m->fs.cfa_reg = stack_pointer_rtx;
10061   m->fs.cfa_offset = INCOMING_FRAME_SP_OFFSET;
10062
10063   /* Track SP offset to the CFA.  We continue tracking this after we've
10064      swapped the CFA register away from SP.  In the case of re-alignment
10065      this is fudged; we're interested to offsets within the local frame.  */
10066   m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10067   m->fs.sp_valid = true;
10068
10069   ix86_compute_frame_layout (&frame);
10070
10071   if (!TARGET_64BIT && ix86_function_ms_hook_prologue (current_function_decl))
10072     {
10073       /* We should have already generated an error for any use of
10074          ms_hook on a nested function.  */
10075       gcc_checking_assert (!ix86_static_chain_on_stack);
10076
10077       /* Check if profiling is active and we shall use profiling before
10078          prologue variant. If so sorry.  */
10079       if (crtl->profile && flag_fentry != 0)
10080         sorry ("ms_hook_prologue attribute isn%'t compatible "
10081                "with -mfentry for 32-bit");
10082
10083       /* In ix86_asm_output_function_label we emitted:
10084          8b ff     movl.s %edi,%edi
10085          55        push   %ebp
10086          8b ec     movl.s %esp,%ebp
10087
10088          This matches the hookable function prologue in Win32 API
10089          functions in Microsoft Windows XP Service Pack 2 and newer.
10090          Wine uses this to enable Windows apps to hook the Win32 API
10091          functions provided by Wine.
10092
10093          What that means is that we've already set up the frame pointer.  */
10094
10095       if (frame_pointer_needed
10096           && !(crtl->drap_reg && crtl->stack_realign_needed))
10097         {
10098           rtx push, mov;
10099
10100           /* We've decided to use the frame pointer already set up.
10101              Describe this to the unwinder by pretending that both
10102              push and mov insns happen right here.
10103
10104              Putting the unwind info here at the end of the ms_hook
10105              is done so that we can make absolutely certain we get
10106              the required byte sequence at the start of the function,
10107              rather than relying on an assembler that can produce
10108              the exact encoding required.
10109
10110              However it does mean (in the unpatched case) that we have
10111              a 1 insn window where the asynchronous unwind info is
10112              incorrect.  However, if we placed the unwind info at
10113              its correct location we would have incorrect unwind info
10114              in the patched case.  Which is probably all moot since
10115              I don't expect Wine generates dwarf2 unwind info for the
10116              system libraries that use this feature.  */
10117
10118           insn = emit_insn (gen_blockage ());
10119
10120           push = gen_push (hard_frame_pointer_rtx);
10121           mov = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10122                              stack_pointer_rtx);
10123           RTX_FRAME_RELATED_P (push) = 1;
10124           RTX_FRAME_RELATED_P (mov) = 1;
10125
10126           RTX_FRAME_RELATED_P (insn) = 1;
10127           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10128                         gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, push, mov)));
10129
10130           /* Note that gen_push incremented m->fs.cfa_offset, even
10131              though we didn't emit the push insn here.  */
10132           m->fs.cfa_reg = hard_frame_pointer_rtx;
10133           m->fs.fp_offset = m->fs.cfa_offset;
10134           m->fs.fp_valid = true;
10135         }
10136       else
10137         {
10138           /* The frame pointer is not needed so pop %ebp again.
10139              This leaves us with a pristine state.  */
10140           emit_insn (gen_pop (hard_frame_pointer_rtx));
10141         }
10142     }
10143
10144   /* The first insn of a function that accepts its static chain on the
10145      stack is to push the register that would be filled in by a direct
10146      call.  This insn will be skipped by the trampoline.  */
10147   else if (ix86_static_chain_on_stack)
10148     {
10149       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
10150       emit_insn (gen_blockage ());
10151
10152       /* We don't want to interpret this push insn as a register save,
10153          only as a stack adjustment.  The real copy of the register as
10154          a save will be done later, if needed.  */
10155       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
10156       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
10157       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
10158       RTX_FRAME_RELATED_P (insn) = 1;
10159     }
10160
10161   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
10162      of DRAP is needed and stack realignment is really needed after reload */
10163   if (stack_realign_drap)
10164     {
10165       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10166
10167       /* Only need to push parameter pointer reg if it is caller saved.  */
10168       if (!call_used_regs[REGNO (crtl->drap_reg)])
10169         {
10170           /* Push arg pointer reg */
10171           insn = emit_insn (gen_push (crtl->drap_reg));
10172           RTX_FRAME_RELATED_P (insn) = 1;
10173         }
10174
10175       /* Grab the argument pointer.  */
10176       t = plus_constant (stack_pointer_rtx, m->fs.sp_offset);
10177       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10178       RTX_FRAME_RELATED_P (insn) = 1;
10179       m->fs.cfa_reg = crtl->drap_reg;
10180       m->fs.cfa_offset = 0;
10181
10182       /* Align the stack.  */
10183       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10184                                         stack_pointer_rtx,
10185                                         GEN_INT (-align_bytes)));
10186       RTX_FRAME_RELATED_P (insn) = 1;
10187
10188       /* Replicate the return address on the stack so that return
10189          address can be reached via (argp - 1) slot.  This is needed
10190          to implement macro RETURN_ADDR_RTX and intrinsic function
10191          expand_builtin_return_addr etc.  */
10192       t = plus_constant (crtl->drap_reg, -UNITS_PER_WORD);
10193       t = gen_frame_mem (Pmode, t);
10194       insn = emit_insn (gen_push (t));
10195       RTX_FRAME_RELATED_P (insn) = 1;
10196
10197       /* For the purposes of frame and register save area addressing,
10198          we've started over with a new frame.  */
10199       m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10200       m->fs.realigned = true;
10201     }
10202
10203   int_registers_saved = (frame.nregs == 0);
10204   sse_registers_saved = (frame.nsseregs == 0);
10205
10206   if (frame_pointer_needed && !m->fs.fp_valid)
10207     {
10208       /* Note: AT&T enter does NOT have reversed args.  Enter is probably
10209          slower on all targets.  Also sdb doesn't like it.  */
10210       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
10211       RTX_FRAME_RELATED_P (insn) = 1;
10212
10213       /* Push registers now, before setting the frame pointer
10214          on SEH target.  */
10215       if (!int_registers_saved
10216           && TARGET_SEH
10217           && !frame.save_regs_using_mov)
10218         {
10219           ix86_emit_save_regs ();
10220           int_registers_saved = true;
10221           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
10222         }
10223
10224       if (m->fs.sp_offset == frame.hard_frame_pointer_offset)
10225         {
10226           insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
10227           RTX_FRAME_RELATED_P (insn) = 1;
10228
10229           if (m->fs.cfa_reg == stack_pointer_rtx)
10230             m->fs.cfa_reg = hard_frame_pointer_rtx;
10231           m->fs.fp_offset = m->fs.sp_offset;
10232           m->fs.fp_valid = true;
10233         }
10234     }
10235
10236   if (!int_registers_saved)
10237     {
10238       /* If saving registers via PUSH, do so now.  */
10239       if (!frame.save_regs_using_mov)
10240         {
10241           ix86_emit_save_regs ();
10242           int_registers_saved = true;
10243           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
10244         }
10245
10246       /* When using red zone we may start register saving before allocating
10247          the stack frame saving one cycle of the prologue.  However, avoid
10248          doing this if we have to probe the stack; at least on x86_64 the
10249          stack probe can turn into a call that clobbers a red zone location. */
10250       else if (ix86_using_red_zone ()
10251                && (! TARGET_STACK_PROBE
10252                    || frame.stack_pointer_offset < CHECK_STACK_LIMIT))
10253         {
10254           ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10255           int_registers_saved = true;
10256         }
10257     }
10258
10259   if (stack_realign_fp)
10260     {
10261       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10262       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
10263
10264       /* The computation of the size of the re-aligned stack frame means
10265          that we must allocate the size of the register save area before
10266          performing the actual alignment.  Otherwise we cannot guarantee
10267          that there's enough storage above the realignment point.  */
10268       if (m->fs.sp_offset != frame.sse_reg_save_offset)
10269         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10270                                    GEN_INT (m->fs.sp_offset
10271                                             - frame.sse_reg_save_offset),
10272                                    -1, false);
10273
10274       /* Align the stack.  */
10275       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10276                                         stack_pointer_rtx,
10277                                         GEN_INT (-align_bytes)));
10278
10279       /* For the purposes of register save area addressing, the stack
10280          pointer is no longer valid.  As for the value of sp_offset,
10281          see ix86_compute_frame_layout, which we need to match in order
10282          to pass verification of stack_pointer_offset at the end.  */
10283       m->fs.sp_offset = (m->fs.sp_offset + align_bytes) & -align_bytes;
10284       m->fs.sp_valid = false;
10285     }
10286
10287   allocate = frame.stack_pointer_offset - m->fs.sp_offset;
10288
10289   if (flag_stack_usage_info)
10290     {
10291       /* We start to count from ARG_POINTER.  */
10292       HOST_WIDE_INT stack_size = frame.stack_pointer_offset;
10293
10294       /* If it was realigned, take into account the fake frame.  */
10295       if (stack_realign_drap)
10296         {
10297           if (ix86_static_chain_on_stack)
10298             stack_size += UNITS_PER_WORD;
10299
10300           if (!call_used_regs[REGNO (crtl->drap_reg)])
10301             stack_size += UNITS_PER_WORD;
10302
10303           /* This over-estimates by 1 minimal-stack-alignment-unit but
10304              mitigates that by counting in the new return address slot.  */
10305           current_function_dynamic_stack_size
10306             += crtl->stack_alignment_needed / BITS_PER_UNIT;
10307         }
10308
10309       current_function_static_stack_size = stack_size;
10310     }
10311
10312   /* On SEH target with very large frame size, allocate an area to save
10313      SSE registers (as the very large allocation won't be described).  */
10314   if (TARGET_SEH
10315       && frame.stack_pointer_offset > SEH_MAX_FRAME_SIZE
10316       && !sse_registers_saved)
10317     {
10318       HOST_WIDE_INT sse_size =
10319         frame.sse_reg_save_offset - frame.reg_save_offset;
10320
10321       gcc_assert (int_registers_saved);
10322
10323       /* No need to do stack checking as the area will be immediately
10324          written.  */
10325       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10326                                  GEN_INT (-sse_size), -1,
10327                                  m->fs.cfa_reg == stack_pointer_rtx);
10328       allocate -= sse_size;
10329       ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
10330       sse_registers_saved = true;
10331     }
10332
10333   /* The stack has already been decremented by the instruction calling us
10334      so probe if the size is non-negative to preserve the protection area.  */
10335   if (allocate >= 0 && flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
10336     {
10337       /* We expect the registers to be saved when probes are used.  */
10338       gcc_assert (int_registers_saved);
10339
10340       if (STACK_CHECK_MOVING_SP)
10341         {
10342           ix86_adjust_stack_and_probe (allocate);
10343           allocate = 0;
10344         }
10345       else
10346         {
10347           HOST_WIDE_INT size = allocate;
10348
10349           if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
10350             size = 0x80000000 - STACK_CHECK_PROTECT - 1;
10351
10352           if (TARGET_STACK_PROBE)
10353             ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
10354           else
10355             ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
10356         }
10357     }
10358
10359   if (allocate == 0)
10360     ;
10361   else if (!ix86_target_stack_probe ()
10362            || frame.stack_pointer_offset < CHECK_STACK_LIMIT)
10363     {
10364       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10365                                  GEN_INT (-allocate), -1,
10366                                  m->fs.cfa_reg == stack_pointer_rtx);
10367     }
10368   else
10369     {
10370       rtx eax = gen_rtx_REG (Pmode, AX_REG);
10371       rtx r10 = NULL;
10372       rtx (*adjust_stack_insn)(rtx, rtx, rtx);
10373       const bool sp_is_cfa_reg = (m->fs.cfa_reg == stack_pointer_rtx);
10374       bool eax_live = false;
10375       bool r10_live = false;
10376
10377       if (TARGET_64BIT)
10378         r10_live = (DECL_STATIC_CHAIN (current_function_decl) != 0);
10379       if (!TARGET_64BIT_MS_ABI)
10380         eax_live = ix86_eax_live_at_start_p ();
10381
10382       /* Note that SEH directives need to continue tracking the stack
10383          pointer even after the frame pointer has been set up.  */
10384       if (eax_live)
10385         {
10386           insn = emit_insn (gen_push (eax));
10387           allocate -= UNITS_PER_WORD;
10388           if (sp_is_cfa_reg || TARGET_SEH)
10389             {
10390               if (sp_is_cfa_reg)
10391                 m->fs.cfa_offset += UNITS_PER_WORD;
10392               RTX_FRAME_RELATED_P (insn) = 1;
10393             }
10394         }
10395
10396       if (r10_live)
10397         {
10398           r10 = gen_rtx_REG (Pmode, R10_REG);
10399           insn = emit_insn (gen_push (r10));
10400           allocate -= UNITS_PER_WORD;
10401           if (sp_is_cfa_reg || TARGET_SEH)
10402             {
10403               if (sp_is_cfa_reg)
10404                 m->fs.cfa_offset += UNITS_PER_WORD;
10405               RTX_FRAME_RELATED_P (insn) = 1;
10406             }
10407         }
10408
10409       emit_move_insn (eax, GEN_INT (allocate));
10410       emit_insn (ix86_gen_allocate_stack_worker (eax, eax));
10411
10412       /* Use the fact that AX still contains ALLOCATE.  */
10413       adjust_stack_insn = (TARGET_64BIT
10414                            ? gen_pro_epilogue_adjust_stack_di_sub
10415                            : gen_pro_epilogue_adjust_stack_si_sub);
10416
10417       insn = emit_insn (adjust_stack_insn (stack_pointer_rtx,
10418                                            stack_pointer_rtx, eax));
10419
10420       if (sp_is_cfa_reg || TARGET_SEH)
10421         {
10422           if (sp_is_cfa_reg)
10423             m->fs.cfa_offset += allocate;
10424           RTX_FRAME_RELATED_P (insn) = 1;
10425           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10426                         gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10427                                      plus_constant (stack_pointer_rtx,
10428                                                     -allocate)));
10429         }
10430       m->fs.sp_offset += allocate;
10431
10432       if (r10_live && eax_live)
10433         {
10434           t = plus_constant (stack_pointer_rtx, allocate);
10435           emit_move_insn (r10, gen_frame_mem (Pmode, t));
10436           t = plus_constant (stack_pointer_rtx,
10437                              allocate - UNITS_PER_WORD);
10438           emit_move_insn (eax, gen_frame_mem (Pmode, t));
10439         }
10440       else if (eax_live || r10_live)
10441         {
10442           t = plus_constant (stack_pointer_rtx, allocate);
10443           emit_move_insn ((eax_live ? eax : r10), gen_frame_mem (Pmode, t));
10444         }
10445     }
10446   gcc_assert (m->fs.sp_offset == frame.stack_pointer_offset);
10447
10448   /* If we havn't already set up the frame pointer, do so now.  */
10449   if (frame_pointer_needed && !m->fs.fp_valid)
10450     {
10451       insn = ix86_gen_add3 (hard_frame_pointer_rtx, stack_pointer_rtx,
10452                             GEN_INT (frame.stack_pointer_offset
10453                                      - frame.hard_frame_pointer_offset));
10454       insn = emit_insn (insn);
10455       RTX_FRAME_RELATED_P (insn) = 1;
10456       add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
10457
10458       if (m->fs.cfa_reg == stack_pointer_rtx)
10459         m->fs.cfa_reg = hard_frame_pointer_rtx;
10460       m->fs.fp_offset = frame.hard_frame_pointer_offset;
10461       m->fs.fp_valid = true;
10462     }
10463
10464   if (!int_registers_saved)
10465     ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10466   if (!sse_registers_saved)
10467     ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
10468
10469   pic_reg_used = false;
10470   if (pic_offset_table_rtx
10471       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
10472           || crtl->profile))
10473     {
10474       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
10475
10476       if (alt_pic_reg_used != INVALID_REGNUM)
10477         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
10478
10479       pic_reg_used = true;
10480     }
10481
10482   if (pic_reg_used)
10483     {
10484       if (TARGET_64BIT)
10485         {
10486           if (ix86_cmodel == CM_LARGE_PIC)
10487             {
10488               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
10489               rtx label = gen_label_rtx ();
10490               emit_label (label);
10491               LABEL_PRESERVE_P (label) = 1;
10492               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
10493               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
10494               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
10495               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
10496                                             pic_offset_table_rtx, tmp_reg));
10497             }
10498           else
10499             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
10500         }
10501       else
10502         {
10503           insn = emit_insn (gen_set_got (pic_offset_table_rtx));
10504           RTX_FRAME_RELATED_P (insn) = 1;
10505           add_reg_note (insn, REG_CFA_FLUSH_QUEUE, NULL_RTX);
10506         }
10507     }
10508
10509   /* In the pic_reg_used case, make sure that the got load isn't deleted
10510      when mcount needs it.  Blockage to avoid call movement across mcount
10511      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
10512      note.  */
10513   if (crtl->profile && !flag_fentry && pic_reg_used)
10514     emit_insn (gen_prologue_use (pic_offset_table_rtx));
10515
10516   if (crtl->drap_reg && !crtl->stack_realign_needed)
10517     {
10518       /* vDRAP is setup but after reload it turns out stack realign
10519          isn't necessary, here we will emit prologue to setup DRAP
10520          without stack realign adjustment */
10521       t = choose_baseaddr (0);
10522       emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10523     }
10524
10525   /* Prevent instructions from being scheduled into register save push
10526      sequence when access to the redzone area is done through frame pointer.
10527      The offset between the frame pointer and the stack pointer is calculated
10528      relative to the value of the stack pointer at the end of the function
10529      prologue, and moving instructions that access redzone area via frame
10530      pointer inside push sequence violates this assumption.  */
10531   if (frame_pointer_needed && frame.red_zone_size)
10532     emit_insn (gen_memory_blockage ());
10533
10534   /* Emit cld instruction if stringops are used in the function.  */
10535   if (TARGET_CLD && ix86_current_function_needs_cld)
10536     emit_insn (gen_cld ());
10537
10538   /* SEH requires that the prologue end within 256 bytes of the start of
10539      the function.  Prevent instruction schedules that would extend that.
10540      Further, prevent alloca modifications to the stack pointer from being
10541      combined with prologue modifications.  */
10542   if (TARGET_SEH)
10543     emit_insn (gen_prologue_use (stack_pointer_rtx));
10544 }
10545
10546 /* Emit code to restore REG using a POP insn.  */
10547
10548 static void
10549 ix86_emit_restore_reg_using_pop (rtx reg)
10550 {
10551   struct machine_function *m = cfun->machine;
10552   rtx insn = emit_insn (gen_pop (reg));
10553
10554   ix86_add_cfa_restore_note (insn, reg, m->fs.sp_offset);
10555   m->fs.sp_offset -= UNITS_PER_WORD;
10556
10557   if (m->fs.cfa_reg == crtl->drap_reg
10558       && REGNO (reg) == REGNO (crtl->drap_reg))
10559     {
10560       /* Previously we'd represented the CFA as an expression
10561          like *(%ebp - 8).  We've just popped that value from
10562          the stack, which means we need to reset the CFA to
10563          the drap register.  This will remain until we restore
10564          the stack pointer.  */
10565       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10566       RTX_FRAME_RELATED_P (insn) = 1;
10567
10568       /* This means that the DRAP register is valid for addressing too.  */
10569       m->fs.drap_valid = true;
10570       return;
10571     }
10572
10573   if (m->fs.cfa_reg == stack_pointer_rtx)
10574     {
10575       rtx x = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
10576       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
10577       add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
10578       RTX_FRAME_RELATED_P (insn) = 1;
10579
10580       m->fs.cfa_offset -= UNITS_PER_WORD;
10581     }
10582
10583   /* When the frame pointer is the CFA, and we pop it, we are
10584      swapping back to the stack pointer as the CFA.  This happens
10585      for stack frames that don't allocate other data, so we assume
10586      the stack pointer is now pointing at the return address, i.e.
10587      the function entry state, which makes the offset be 1 word.  */
10588   if (reg == hard_frame_pointer_rtx)
10589     {
10590       m->fs.fp_valid = false;
10591       if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10592         {
10593           m->fs.cfa_reg = stack_pointer_rtx;
10594           m->fs.cfa_offset -= UNITS_PER_WORD;
10595
10596           add_reg_note (insn, REG_CFA_DEF_CFA,
10597                         gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10598                                       GEN_INT (m->fs.cfa_offset)));
10599           RTX_FRAME_RELATED_P (insn) = 1;
10600         }
10601     }
10602 }
10603
10604 /* Emit code to restore saved registers using POP insns.  */
10605
10606 static void
10607 ix86_emit_restore_regs_using_pop (void)
10608 {
10609   unsigned int regno;
10610
10611   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10612     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
10613       ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno));
10614 }
10615
10616 /* Emit code and notes for the LEAVE instruction.  */
10617
10618 static void
10619 ix86_emit_leave (void)
10620 {
10621   struct machine_function *m = cfun->machine;
10622   rtx insn = emit_insn (ix86_gen_leave ());
10623
10624   ix86_add_queued_cfa_restore_notes (insn);
10625
10626   gcc_assert (m->fs.fp_valid);
10627   m->fs.sp_valid = true;
10628   m->fs.sp_offset = m->fs.fp_offset - UNITS_PER_WORD;
10629   m->fs.fp_valid = false;
10630
10631   if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10632     {
10633       m->fs.cfa_reg = stack_pointer_rtx;
10634       m->fs.cfa_offset = m->fs.sp_offset;
10635
10636       add_reg_note (insn, REG_CFA_DEF_CFA,
10637                     plus_constant (stack_pointer_rtx, m->fs.sp_offset));
10638       RTX_FRAME_RELATED_P (insn) = 1;
10639     }
10640   ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx,
10641                              m->fs.fp_offset);
10642 }
10643
10644 /* Emit code to restore saved registers using MOV insns.
10645    First register is restored from CFA - CFA_OFFSET.  */
10646 static void
10647 ix86_emit_restore_regs_using_mov (HOST_WIDE_INT cfa_offset,
10648                                   bool maybe_eh_return)
10649 {
10650   struct machine_function *m = cfun->machine;
10651   unsigned int regno;
10652
10653   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10654     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10655       {
10656         rtx reg = gen_rtx_REG (Pmode, regno);
10657         rtx insn, mem;
10658
10659         mem = choose_baseaddr (cfa_offset);
10660         mem = gen_frame_mem (Pmode, mem);
10661         insn = emit_move_insn (reg, mem);
10662
10663         if (m->fs.cfa_reg == crtl->drap_reg && regno == REGNO (crtl->drap_reg))
10664           {
10665             /* Previously we'd represented the CFA as an expression
10666                like *(%ebp - 8).  We've just popped that value from
10667                the stack, which means we need to reset the CFA to
10668                the drap register.  This will remain until we restore
10669                the stack pointer.  */
10670             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10671             RTX_FRAME_RELATED_P (insn) = 1;
10672
10673             /* This means that the DRAP register is valid for addressing.  */
10674             m->fs.drap_valid = true;
10675           }
10676         else
10677           ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10678
10679         cfa_offset -= UNITS_PER_WORD;
10680       }
10681 }
10682
10683 /* Emit code to restore saved registers using MOV insns.
10684    First register is restored from CFA - CFA_OFFSET.  */
10685 static void
10686 ix86_emit_restore_sse_regs_using_mov (HOST_WIDE_INT cfa_offset,
10687                                       bool maybe_eh_return)
10688 {
10689   unsigned int regno;
10690
10691   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10692     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10693       {
10694         rtx reg = gen_rtx_REG (V4SFmode, regno);
10695         rtx mem;
10696
10697         mem = choose_baseaddr (cfa_offset);
10698         mem = gen_rtx_MEM (V4SFmode, mem);
10699         set_mem_align (mem, 128);
10700         emit_move_insn (reg, mem);
10701
10702         ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10703
10704         cfa_offset -= 16;
10705       }
10706 }
10707
10708 /* Emit vzeroupper if needed.  */
10709
10710 void
10711 ix86_maybe_emit_epilogue_vzeroupper (void)
10712 {
10713   if (TARGET_VZEROUPPER
10714       && !TREE_THIS_VOLATILE (cfun->decl)
10715       && !cfun->machine->caller_return_avx256_p)
10716     emit_insn (gen_avx_vzeroupper (GEN_INT (call_no_avx256)));
10717 }
10718
10719 /* Restore function stack, frame, and registers.  */
10720
10721 void
10722 ix86_expand_epilogue (int style)
10723 {
10724   struct machine_function *m = cfun->machine;
10725   struct machine_frame_state frame_state_save = m->fs;
10726   struct ix86_frame frame;
10727   bool restore_regs_via_mov;
10728   bool using_drap;
10729
10730   ix86_finalize_stack_realign_flags ();
10731   ix86_compute_frame_layout (&frame);
10732
10733   m->fs.sp_valid = (!frame_pointer_needed
10734                     || (current_function_sp_is_unchanging
10735                         && !stack_realign_fp));
10736   gcc_assert (!m->fs.sp_valid
10737               || m->fs.sp_offset == frame.stack_pointer_offset);
10738
10739   /* The FP must be valid if the frame pointer is present.  */
10740   gcc_assert (frame_pointer_needed == m->fs.fp_valid);
10741   gcc_assert (!m->fs.fp_valid
10742               || m->fs.fp_offset == frame.hard_frame_pointer_offset);
10743
10744   /* We must have *some* valid pointer to the stack frame.  */
10745   gcc_assert (m->fs.sp_valid || m->fs.fp_valid);
10746
10747   /* The DRAP is never valid at this point.  */
10748   gcc_assert (!m->fs.drap_valid);
10749
10750   /* See the comment about red zone and frame
10751      pointer usage in ix86_expand_prologue.  */
10752   if (frame_pointer_needed && frame.red_zone_size)
10753     emit_insn (gen_memory_blockage ());
10754
10755   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
10756   gcc_assert (!using_drap || m->fs.cfa_reg == crtl->drap_reg);
10757
10758   /* Determine the CFA offset of the end of the red-zone.  */
10759   m->fs.red_zone_offset = 0;
10760   if (ix86_using_red_zone () && crtl->args.pops_args < 65536)
10761     {
10762       /* The red-zone begins below the return address.  */
10763       m->fs.red_zone_offset = RED_ZONE_SIZE + UNITS_PER_WORD;
10764
10765       /* When the register save area is in the aligned portion of
10766          the stack, determine the maximum runtime displacement that
10767          matches up with the aligned frame.  */
10768       if (stack_realign_drap)
10769         m->fs.red_zone_offset -= (crtl->stack_alignment_needed / BITS_PER_UNIT
10770                                   + UNITS_PER_WORD);
10771     }
10772
10773   /* Special care must be taken for the normal return case of a function
10774      using eh_return: the eax and edx registers are marked as saved, but
10775      not restored along this path.  Adjust the save location to match.  */
10776   if (crtl->calls_eh_return && style != 2)
10777     frame.reg_save_offset -= 2 * UNITS_PER_WORD;
10778
10779   /* EH_RETURN requires the use of moves to function properly.  */
10780   if (crtl->calls_eh_return)
10781     restore_regs_via_mov = true;
10782   /* SEH requires the use of pops to identify the epilogue.  */
10783   else if (TARGET_SEH)
10784     restore_regs_via_mov = false;
10785   /* If we're only restoring one register and sp is not valid then
10786      using a move instruction to restore the register since it's
10787      less work than reloading sp and popping the register.  */
10788   else if (!m->fs.sp_valid && frame.nregs <= 1)
10789     restore_regs_via_mov = true;
10790   else if (TARGET_EPILOGUE_USING_MOVE
10791            && cfun->machine->use_fast_prologue_epilogue
10792            && (frame.nregs > 1
10793                || m->fs.sp_offset != frame.reg_save_offset))
10794     restore_regs_via_mov = true;
10795   else if (frame_pointer_needed
10796            && !frame.nregs
10797            && m->fs.sp_offset != frame.reg_save_offset)
10798     restore_regs_via_mov = true;
10799   else if (frame_pointer_needed
10800            && TARGET_USE_LEAVE
10801            && cfun->machine->use_fast_prologue_epilogue
10802            && frame.nregs == 1)
10803     restore_regs_via_mov = true;
10804   else
10805     restore_regs_via_mov = false;
10806
10807   if (restore_regs_via_mov || frame.nsseregs)
10808     {
10809       /* Ensure that the entire register save area is addressable via
10810          the stack pointer, if we will restore via sp.  */
10811       if (TARGET_64BIT
10812           && m->fs.sp_offset > 0x7fffffff
10813           && !(m->fs.fp_valid || m->fs.drap_valid)
10814           && (frame.nsseregs + frame.nregs) != 0)
10815         {
10816           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10817                                      GEN_INT (m->fs.sp_offset
10818                                               - frame.sse_reg_save_offset),
10819                                      style,
10820                                      m->fs.cfa_reg == stack_pointer_rtx);
10821         }
10822     }
10823
10824   /* If there are any SSE registers to restore, then we have to do it
10825      via moves, since there's obviously no pop for SSE regs.  */
10826   if (frame.nsseregs)
10827     ix86_emit_restore_sse_regs_using_mov (frame.sse_reg_save_offset,
10828                                           style == 2);
10829
10830   if (restore_regs_via_mov)
10831     {
10832       rtx t;
10833
10834       if (frame.nregs)
10835         ix86_emit_restore_regs_using_mov (frame.reg_save_offset, style == 2);
10836
10837       /* eh_return epilogues need %ecx added to the stack pointer.  */
10838       if (style == 2)
10839         {
10840           rtx insn, sa = EH_RETURN_STACKADJ_RTX;
10841
10842           /* Stack align doesn't work with eh_return.  */
10843           gcc_assert (!stack_realign_drap);
10844           /* Neither does regparm nested functions.  */
10845           gcc_assert (!ix86_static_chain_on_stack);
10846
10847           if (frame_pointer_needed)
10848             {
10849               t = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
10850               t = plus_constant (t, m->fs.fp_offset - UNITS_PER_WORD);
10851               emit_insn (gen_rtx_SET (VOIDmode, sa, t));
10852
10853               t = gen_frame_mem (Pmode, hard_frame_pointer_rtx);
10854               insn = emit_move_insn (hard_frame_pointer_rtx, t);
10855
10856               /* Note that we use SA as a temporary CFA, as the return
10857                  address is at the proper place relative to it.  We
10858                  pretend this happens at the FP restore insn because
10859                  prior to this insn the FP would be stored at the wrong
10860                  offset relative to SA, and after this insn we have no
10861                  other reasonable register to use for the CFA.  We don't
10862                  bother resetting the CFA to the SP for the duration of
10863                  the return insn.  */
10864               add_reg_note (insn, REG_CFA_DEF_CFA,
10865                             plus_constant (sa, UNITS_PER_WORD));
10866               ix86_add_queued_cfa_restore_notes (insn);
10867               add_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx);
10868               RTX_FRAME_RELATED_P (insn) = 1;
10869
10870               m->fs.cfa_reg = sa;
10871               m->fs.cfa_offset = UNITS_PER_WORD;
10872               m->fs.fp_valid = false;
10873
10874               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
10875                                          const0_rtx, style, false);
10876             }
10877           else
10878             {
10879               t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
10880               t = plus_constant (t, m->fs.sp_offset - UNITS_PER_WORD);
10881               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, t));
10882               ix86_add_queued_cfa_restore_notes (insn);
10883
10884               gcc_assert (m->fs.cfa_reg == stack_pointer_rtx);
10885               if (m->fs.cfa_offset != UNITS_PER_WORD)
10886                 {
10887                   m->fs.cfa_offset = UNITS_PER_WORD;
10888                   add_reg_note (insn, REG_CFA_DEF_CFA,
10889                                 plus_constant (stack_pointer_rtx,
10890                                                UNITS_PER_WORD));
10891                   RTX_FRAME_RELATED_P (insn) = 1;
10892                 }
10893             }
10894           m->fs.sp_offset = UNITS_PER_WORD;
10895           m->fs.sp_valid = true;
10896         }
10897     }
10898   else
10899     {
10900       /* SEH requires that the function end with (1) a stack adjustment
10901          if necessary, (2) a sequence of pops, and (3) a return or
10902          jump instruction.  Prevent insns from the function body from
10903          being scheduled into this sequence.  */
10904       if (TARGET_SEH)
10905         {
10906           /* Prevent a catch region from being adjacent to the standard
10907              epilogue sequence.  Unfortuantely crtl->uses_eh_lsda nor
10908              several other flags that would be interesting to test are
10909              not yet set up.  */
10910           if (flag_non_call_exceptions)
10911             emit_insn (gen_nops (const1_rtx));
10912           else
10913             emit_insn (gen_blockage ());
10914         }
10915
10916       /* First step is to deallocate the stack frame so that we can
10917          pop the registers.  Also do it on SEH target for very large
10918          frame as the emitted instructions aren't allowed by the ABI in
10919          epilogues.  */
10920       if (!m->fs.sp_valid
10921           || (TARGET_SEH
10922               && (m->fs.sp_offset - frame.reg_save_offset
10923                   >= SEH_MAX_FRAME_SIZE)))
10924         {
10925           pro_epilogue_adjust_stack (stack_pointer_rtx, hard_frame_pointer_rtx,
10926                                      GEN_INT (m->fs.fp_offset
10927                                               - frame.reg_save_offset),
10928                                      style, false);
10929         }
10930       else if (m->fs.sp_offset != frame.reg_save_offset)
10931         {
10932           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10933                                      GEN_INT (m->fs.sp_offset
10934                                               - frame.reg_save_offset),
10935                                      style,
10936                                      m->fs.cfa_reg == stack_pointer_rtx);
10937         }
10938
10939       ix86_emit_restore_regs_using_pop ();
10940     }
10941
10942   /* If we used a stack pointer and haven't already got rid of it,
10943      then do so now.  */
10944   if (m->fs.fp_valid)
10945     {
10946       /* If the stack pointer is valid and pointing at the frame
10947          pointer store address, then we only need a pop.  */
10948       if (m->fs.sp_valid && m->fs.sp_offset == frame.hfp_save_offset)
10949         ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10950       /* Leave results in shorter dependency chains on CPUs that are
10951          able to grok it fast.  */
10952       else if (TARGET_USE_LEAVE
10953                || optimize_function_for_size_p (cfun)
10954                || !cfun->machine->use_fast_prologue_epilogue)
10955         ix86_emit_leave ();
10956       else
10957         {
10958           pro_epilogue_adjust_stack (stack_pointer_rtx,
10959                                      hard_frame_pointer_rtx,
10960                                      const0_rtx, style, !using_drap);
10961           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10962         }
10963     }
10964
10965   if (using_drap)
10966     {
10967       int param_ptr_offset = UNITS_PER_WORD;
10968       rtx insn;
10969
10970       gcc_assert (stack_realign_drap);
10971
10972       if (ix86_static_chain_on_stack)
10973         param_ptr_offset += UNITS_PER_WORD;
10974       if (!call_used_regs[REGNO (crtl->drap_reg)])
10975         param_ptr_offset += UNITS_PER_WORD;
10976
10977       insn = emit_insn (gen_rtx_SET
10978                         (VOIDmode, stack_pointer_rtx,
10979                          gen_rtx_PLUS (Pmode,
10980                                        crtl->drap_reg,
10981                                        GEN_INT (-param_ptr_offset))));
10982       m->fs.cfa_reg = stack_pointer_rtx;
10983       m->fs.cfa_offset = param_ptr_offset;
10984       m->fs.sp_offset = param_ptr_offset;
10985       m->fs.realigned = false;
10986
10987       add_reg_note (insn, REG_CFA_DEF_CFA,
10988                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10989                                   GEN_INT (param_ptr_offset)));
10990       RTX_FRAME_RELATED_P (insn) = 1;
10991
10992       if (!call_used_regs[REGNO (crtl->drap_reg)])
10993         ix86_emit_restore_reg_using_pop (crtl->drap_reg);
10994     }
10995
10996   /* At this point the stack pointer must be valid, and we must have
10997      restored all of the registers.  We may not have deallocated the
10998      entire stack frame.  We've delayed this until now because it may
10999      be possible to merge the local stack deallocation with the
11000      deallocation forced by ix86_static_chain_on_stack.   */
11001   gcc_assert (m->fs.sp_valid);
11002   gcc_assert (!m->fs.fp_valid);
11003   gcc_assert (!m->fs.realigned);
11004   if (m->fs.sp_offset != UNITS_PER_WORD)
11005     {
11006       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
11007                                  GEN_INT (m->fs.sp_offset - UNITS_PER_WORD),
11008                                  style, true);
11009     }
11010   else
11011     ix86_add_queued_cfa_restore_notes (get_last_insn ());
11012
11013   /* Sibcall epilogues don't want a return instruction.  */
11014   if (style == 0)
11015     {
11016       m->fs = frame_state_save;
11017       return;
11018     }
11019
11020   /* Emit vzeroupper if needed.  */
11021   ix86_maybe_emit_epilogue_vzeroupper ();
11022
11023   if (crtl->args.pops_args && crtl->args.size)
11024     {
11025       rtx popc = GEN_INT (crtl->args.pops_args);
11026
11027       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
11028          address, do explicit add, and jump indirectly to the caller.  */
11029
11030       if (crtl->args.pops_args >= 65536)
11031         {
11032           rtx ecx = gen_rtx_REG (SImode, CX_REG);
11033           rtx insn;
11034
11035           /* There is no "pascal" calling convention in any 64bit ABI.  */
11036           gcc_assert (!TARGET_64BIT);
11037
11038           insn = emit_insn (gen_pop (ecx));
11039           m->fs.cfa_offset -= UNITS_PER_WORD;
11040           m->fs.sp_offset -= UNITS_PER_WORD;
11041
11042           add_reg_note (insn, REG_CFA_ADJUST_CFA,
11043                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
11044           add_reg_note (insn, REG_CFA_REGISTER,
11045                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
11046           RTX_FRAME_RELATED_P (insn) = 1;
11047
11048           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
11049                                      popc, -1, true);
11050           emit_jump_insn (gen_simple_return_indirect_internal (ecx));
11051         }
11052       else
11053         emit_jump_insn (gen_simple_return_pop_internal (popc));
11054     }
11055   else
11056     emit_jump_insn (gen_simple_return_internal ());
11057
11058   /* Restore the state back to the state from the prologue,
11059      so that it's correct for the next epilogue.  */
11060   m->fs = frame_state_save;
11061 }
11062
11063 /* Reset from the function's potential modifications.  */
11064
11065 static void
11066 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11067                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11068 {
11069   if (pic_offset_table_rtx)
11070     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
11071 #if TARGET_MACHO
11072   /* Mach-O doesn't support labels at the end of objects, so if
11073      it looks like we might want one, insert a NOP.  */
11074   {
11075     rtx insn = get_last_insn ();
11076     rtx deleted_debug_label = NULL_RTX;
11077     while (insn
11078            && NOTE_P (insn)
11079            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
11080       {
11081         /* Don't insert a nop for NOTE_INSN_DELETED_DEBUG_LABEL
11082            notes only, instead set their CODE_LABEL_NUMBER to -1,
11083            otherwise there would be code generation differences
11084            in between -g and -g0.  */
11085         if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
11086           deleted_debug_label = insn;
11087         insn = PREV_INSN (insn);
11088       }
11089     if (insn
11090         && (LABEL_P (insn)
11091             || (NOTE_P (insn)
11092                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
11093       fputs ("\tnop\n", file);
11094     else if (deleted_debug_label)
11095       for (insn = deleted_debug_label; insn; insn = NEXT_INSN (insn))
11096         if (NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL)
11097           CODE_LABEL_NUMBER (insn) = -1;
11098   }
11099 #endif
11100
11101 }
11102
11103 /* Return a scratch register to use in the split stack prologue.  The
11104    split stack prologue is used for -fsplit-stack.  It is the first
11105    instructions in the function, even before the regular prologue.
11106    The scratch register can be any caller-saved register which is not
11107    used for parameters or for the static chain.  */
11108
11109 static unsigned int
11110 split_stack_prologue_scratch_regno (void)
11111 {
11112   if (TARGET_64BIT)
11113     return R11_REG;
11114   else
11115     {
11116       bool is_fastcall, is_thiscall;
11117       int regparm;
11118
11119       is_fastcall = (lookup_attribute ("fastcall",
11120                                        TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
11121                      != NULL);
11122       is_thiscall = (lookup_attribute ("thiscall",
11123                                        TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
11124                      != NULL);
11125       regparm = ix86_function_regparm (TREE_TYPE (cfun->decl), cfun->decl);
11126
11127       if (is_fastcall)
11128         {
11129           if (DECL_STATIC_CHAIN (cfun->decl))
11130             {
11131               sorry ("-fsplit-stack does not support fastcall with "
11132                      "nested function");
11133               return INVALID_REGNUM;
11134             }
11135           return AX_REG;
11136         }
11137       else if (is_thiscall)
11138         {
11139           if (!DECL_STATIC_CHAIN (cfun->decl))
11140             return DX_REG;
11141           return AX_REG;
11142         }
11143       else if (regparm < 3)
11144         {
11145           if (!DECL_STATIC_CHAIN (cfun->decl))
11146             return CX_REG;
11147           else
11148             {
11149               if (regparm >= 2)
11150                 {
11151                   sorry ("-fsplit-stack does not support 2 register "
11152                          " parameters for a nested function");
11153                   return INVALID_REGNUM;
11154                 }
11155               return DX_REG;
11156             }
11157         }
11158       else
11159         {
11160           /* FIXME: We could make this work by pushing a register
11161              around the addition and comparison.  */
11162           sorry ("-fsplit-stack does not support 3 register parameters");
11163           return INVALID_REGNUM;
11164         }
11165     }
11166 }
11167
11168 /* A SYMBOL_REF for the function which allocates new stackspace for
11169    -fsplit-stack.  */
11170
11171 static GTY(()) rtx split_stack_fn;
11172
11173 /* A SYMBOL_REF for the more stack function when using the large
11174    model.  */
11175
11176 static GTY(()) rtx split_stack_fn_large;
11177
11178 /* Handle -fsplit-stack.  These are the first instructions in the
11179    function, even before the regular prologue.  */
11180
11181 void
11182 ix86_expand_split_stack_prologue (void)
11183 {
11184   struct ix86_frame frame;
11185   HOST_WIDE_INT allocate;
11186   unsigned HOST_WIDE_INT args_size;
11187   rtx label, limit, current, jump_insn, allocate_rtx, call_insn, call_fusage;
11188   rtx scratch_reg = NULL_RTX;
11189   rtx varargs_label = NULL_RTX;
11190   rtx fn;
11191
11192   gcc_assert (flag_split_stack && reload_completed);
11193
11194   ix86_finalize_stack_realign_flags ();
11195   ix86_compute_frame_layout (&frame);
11196   allocate = frame.stack_pointer_offset - INCOMING_FRAME_SP_OFFSET;
11197
11198   /* This is the label we will branch to if we have enough stack
11199      space.  We expect the basic block reordering pass to reverse this
11200      branch if optimizing, so that we branch in the unlikely case.  */
11201   label = gen_label_rtx ();
11202
11203   /* We need to compare the stack pointer minus the frame size with
11204      the stack boundary in the TCB.  The stack boundary always gives
11205      us SPLIT_STACK_AVAILABLE bytes, so if we need less than that we
11206      can compare directly.  Otherwise we need to do an addition.  */
11207
11208   limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
11209                           UNSPEC_STACK_CHECK);
11210   limit = gen_rtx_CONST (Pmode, limit);
11211   limit = gen_rtx_MEM (Pmode, limit);
11212   if (allocate < SPLIT_STACK_AVAILABLE)
11213     current = stack_pointer_rtx;
11214   else
11215     {
11216       unsigned int scratch_regno;
11217       rtx offset;
11218
11219       /* We need a scratch register to hold the stack pointer minus
11220          the required frame size.  Since this is the very start of the
11221          function, the scratch register can be any caller-saved
11222          register which is not used for parameters.  */
11223       offset = GEN_INT (- allocate);
11224       scratch_regno = split_stack_prologue_scratch_regno ();
11225       if (scratch_regno == INVALID_REGNUM)
11226         return;
11227       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11228       if (!TARGET_64BIT || x86_64_immediate_operand (offset, Pmode))
11229         {
11230           /* We don't use ix86_gen_add3 in this case because it will
11231              want to split to lea, but when not optimizing the insn
11232              will not be split after this point.  */
11233           emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11234                                   gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11235                                                 offset)));
11236         }
11237       else
11238         {
11239           emit_move_insn (scratch_reg, offset);
11240           emit_insn (gen_adddi3 (scratch_reg, scratch_reg,
11241                                  stack_pointer_rtx));
11242         }
11243       current = scratch_reg;
11244     }
11245
11246   ix86_expand_branch (GEU, current, limit, label);
11247   jump_insn = get_last_insn ();
11248   JUMP_LABEL (jump_insn) = label;
11249
11250   /* Mark the jump as very likely to be taken.  */
11251   add_reg_note (jump_insn, REG_BR_PROB,
11252                 GEN_INT (REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100));
11253
11254   if (split_stack_fn == NULL_RTX)
11255     split_stack_fn = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
11256   fn = split_stack_fn;
11257
11258   /* Get more stack space.  We pass in the desired stack space and the
11259      size of the arguments to copy to the new stack.  In 32-bit mode
11260      we push the parameters; __morestack will return on a new stack
11261      anyhow.  In 64-bit mode we pass the parameters in r10 and
11262      r11.  */
11263   allocate_rtx = GEN_INT (allocate);
11264   args_size = crtl->args.size >= 0 ? crtl->args.size : 0;
11265   call_fusage = NULL_RTX;
11266   if (TARGET_64BIT)
11267     {
11268       rtx reg10, reg11;
11269
11270       reg10 = gen_rtx_REG (Pmode, R10_REG);
11271       reg11 = gen_rtx_REG (Pmode, R11_REG);
11272
11273       /* If this function uses a static chain, it will be in %r10.
11274          Preserve it across the call to __morestack.  */
11275       if (DECL_STATIC_CHAIN (cfun->decl))
11276         {
11277           rtx rax;
11278
11279           rax = gen_rtx_REG (Pmode, AX_REG);
11280           emit_move_insn (rax, reg10);
11281           use_reg (&call_fusage, rax);
11282         }
11283
11284       if (ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
11285         {
11286           HOST_WIDE_INT argval;
11287
11288           /* When using the large model we need to load the address
11289              into a register, and we've run out of registers.  So we
11290              switch to a different calling convention, and we call a
11291              different function: __morestack_large.  We pass the
11292              argument size in the upper 32 bits of r10 and pass the
11293              frame size in the lower 32 bits.  */
11294           gcc_assert ((allocate & (HOST_WIDE_INT) 0xffffffff) == allocate);
11295           gcc_assert ((args_size & 0xffffffff) == args_size);
11296
11297           if (split_stack_fn_large == NULL_RTX)
11298             split_stack_fn_large =
11299               gen_rtx_SYMBOL_REF (Pmode, "__morestack_large_model");
11300
11301           if (ix86_cmodel == CM_LARGE_PIC)
11302             {
11303               rtx label, x;
11304
11305               label = gen_label_rtx ();
11306               emit_label (label);
11307               LABEL_PRESERVE_P (label) = 1;
11308               emit_insn (gen_set_rip_rex64 (reg10, label));
11309               emit_insn (gen_set_got_offset_rex64 (reg11, label));
11310               emit_insn (gen_adddi3 (reg10, reg10, reg11));
11311               x = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, split_stack_fn_large),
11312                                   UNSPEC_GOT);
11313               x = gen_rtx_CONST (Pmode, x);
11314               emit_move_insn (reg11, x);
11315               x = gen_rtx_PLUS (Pmode, reg10, reg11);
11316               x = gen_const_mem (Pmode, x);
11317               emit_move_insn (reg11, x);
11318             }
11319           else
11320             emit_move_insn (reg11, split_stack_fn_large);
11321
11322           fn = reg11;
11323
11324           argval = ((args_size << 16) << 16) + allocate;
11325           emit_move_insn (reg10, GEN_INT (argval));
11326         }
11327       else
11328         {
11329           emit_move_insn (reg10, allocate_rtx);
11330           emit_move_insn (reg11, GEN_INT (args_size));
11331           use_reg (&call_fusage, reg11);
11332         }
11333
11334       use_reg (&call_fusage, reg10);
11335     }
11336   else
11337     {
11338       emit_insn (gen_push (GEN_INT (args_size)));
11339       emit_insn (gen_push (allocate_rtx));
11340     }
11341   call_insn = ix86_expand_call (NULL_RTX, gen_rtx_MEM (QImode, fn),
11342                                 GEN_INT (UNITS_PER_WORD), constm1_rtx,
11343                                 NULL_RTX, false);
11344   add_function_usage_to (call_insn, call_fusage);
11345
11346   /* In order to make call/return prediction work right, we now need
11347      to execute a return instruction.  See
11348      libgcc/config/i386/morestack.S for the details on how this works.
11349
11350      For flow purposes gcc must not see this as a return
11351      instruction--we need control flow to continue at the subsequent
11352      label.  Therefore, we use an unspec.  */
11353   gcc_assert (crtl->args.pops_args < 65536);
11354   emit_insn (gen_split_stack_return (GEN_INT (crtl->args.pops_args)));
11355
11356   /* If we are in 64-bit mode and this function uses a static chain,
11357      we saved %r10 in %rax before calling _morestack.  */
11358   if (TARGET_64BIT && DECL_STATIC_CHAIN (cfun->decl))
11359     emit_move_insn (gen_rtx_REG (Pmode, R10_REG),
11360                     gen_rtx_REG (Pmode, AX_REG));
11361
11362   /* If this function calls va_start, we need to store a pointer to
11363      the arguments on the old stack, because they may not have been
11364      all copied to the new stack.  At this point the old stack can be
11365      found at the frame pointer value used by __morestack, because
11366      __morestack has set that up before calling back to us.  Here we
11367      store that pointer in a scratch register, and in
11368      ix86_expand_prologue we store the scratch register in a stack
11369      slot.  */
11370   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11371     {
11372       unsigned int scratch_regno;
11373       rtx frame_reg;
11374       int words;
11375
11376       scratch_regno = split_stack_prologue_scratch_regno ();
11377       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11378       frame_reg = gen_rtx_REG (Pmode, BP_REG);
11379
11380       /* 64-bit:
11381          fp -> old fp value
11382                return address within this function
11383                return address of caller of this function
11384                stack arguments
11385          So we add three words to get to the stack arguments.
11386
11387          32-bit:
11388          fp -> old fp value
11389                return address within this function
11390                first argument to __morestack
11391                second argument to __morestack
11392                return address of caller of this function
11393                stack arguments
11394          So we add five words to get to the stack arguments.
11395       */
11396       words = TARGET_64BIT ? 3 : 5;
11397       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11398                               gen_rtx_PLUS (Pmode, frame_reg,
11399                                             GEN_INT (words * UNITS_PER_WORD))));
11400
11401       varargs_label = gen_label_rtx ();
11402       emit_jump_insn (gen_jump (varargs_label));
11403       JUMP_LABEL (get_last_insn ()) = varargs_label;
11404
11405       emit_barrier ();
11406     }
11407
11408   emit_label (label);
11409   LABEL_NUSES (label) = 1;
11410
11411   /* If this function calls va_start, we now have to set the scratch
11412      register for the case where we do not call __morestack.  In this
11413      case we need to set it based on the stack pointer.  */
11414   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11415     {
11416       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11417                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11418                                             GEN_INT (UNITS_PER_WORD))));
11419
11420       emit_label (varargs_label);
11421       LABEL_NUSES (varargs_label) = 1;
11422     }
11423 }
11424
11425 /* We may have to tell the dataflow pass that the split stack prologue
11426    is initializing a scratch register.  */
11427
11428 static void
11429 ix86_live_on_entry (bitmap regs)
11430 {
11431   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11432     {
11433       gcc_assert (flag_split_stack);
11434       bitmap_set_bit (regs, split_stack_prologue_scratch_regno ());
11435     }
11436 }
11437 \f
11438 /* Extract the parts of an RTL expression that is a valid memory address
11439    for an instruction.  Return 0 if the structure of the address is
11440    grossly off.  Return -1 if the address contains ASHIFT, so it is not
11441    strictly valid, but still used for computing length of lea instruction.  */
11442
11443 int
11444 ix86_decompose_address (rtx addr, struct ix86_address *out)
11445 {
11446   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
11447   rtx base_reg, index_reg;
11448   HOST_WIDE_INT scale = 1;
11449   rtx scale_rtx = NULL_RTX;
11450   rtx tmp;
11451   int retval = 1;
11452   enum ix86_address_seg seg = SEG_DEFAULT;
11453
11454   /* Allow zero-extended SImode addresses,
11455      they will be emitted with addr32 prefix.  */
11456   if (TARGET_64BIT && GET_MODE (addr) == DImode)
11457     {
11458       if (GET_CODE (addr) == ZERO_EXTEND
11459           && GET_MODE (XEXP (addr, 0)) == SImode)
11460         {
11461           addr = XEXP (addr, 0);
11462           if (CONST_INT_P (addr))
11463             return 0;
11464         }             
11465       else if (GET_CODE (addr) == AND
11466                && const_32bit_mask (XEXP (addr, 1), DImode))
11467         {
11468           addr = XEXP (addr, 0);
11469
11470           /* Adjust SUBREGs.  */
11471           if (GET_CODE (addr) == SUBREG
11472               && GET_MODE (SUBREG_REG (addr)) == SImode)
11473             {
11474               addr = SUBREG_REG (addr);
11475               if (CONST_INT_P (addr))
11476                 return 0;
11477             }
11478           else if (GET_MODE (addr) == DImode)
11479             addr = gen_rtx_SUBREG (SImode, addr, 0);
11480           else if (GET_MODE (addr) != VOIDmode)
11481             return 0;
11482         }
11483     }
11484
11485   /* Allow SImode subregs of DImode addresses,
11486      they will be emitted with addr32 prefix.  */
11487   if (TARGET_64BIT && GET_MODE (addr) == SImode)
11488     {
11489       if (GET_CODE (addr) == SUBREG
11490           && GET_MODE (SUBREG_REG (addr)) == DImode)
11491         {
11492           addr = SUBREG_REG (addr);
11493           if (CONST_INT_P (addr))
11494             return 0;
11495         }
11496     }
11497
11498   if (REG_P (addr))
11499     base = addr;
11500   else if (GET_CODE (addr) == SUBREG)
11501     {
11502       if (REG_P (SUBREG_REG (addr)))
11503         base = addr;
11504       else
11505         return 0;
11506     }
11507   else if (GET_CODE (addr) == PLUS)
11508     {
11509       rtx addends[4], op;
11510       int n = 0, i;
11511
11512       op = addr;
11513       do
11514         {
11515           if (n >= 4)
11516             return 0;
11517           addends[n++] = XEXP (op, 1);
11518           op = XEXP (op, 0);
11519         }
11520       while (GET_CODE (op) == PLUS);
11521       if (n >= 4)
11522         return 0;
11523       addends[n] = op;
11524
11525       for (i = n; i >= 0; --i)
11526         {
11527           op = addends[i];
11528           switch (GET_CODE (op))
11529             {
11530             case MULT:
11531               if (index)
11532                 return 0;
11533               index = XEXP (op, 0);
11534               scale_rtx = XEXP (op, 1);
11535               break;
11536
11537             case ASHIFT:
11538               if (index)
11539                 return 0;
11540               index = XEXP (op, 0);
11541               tmp = XEXP (op, 1);
11542               if (!CONST_INT_P (tmp))
11543                 return 0;
11544               scale = INTVAL (tmp);
11545               if ((unsigned HOST_WIDE_INT) scale > 3)
11546                 return 0;
11547               scale = 1 << scale;
11548               break;
11549
11550             case UNSPEC:
11551               if (XINT (op, 1) == UNSPEC_TP
11552                   && TARGET_TLS_DIRECT_SEG_REFS
11553                   && seg == SEG_DEFAULT)
11554                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
11555               else
11556                 return 0;
11557               break;
11558
11559             case SUBREG:
11560               if (!REG_P (SUBREG_REG (op)))
11561                 return 0;
11562               /* FALLTHRU */
11563
11564             case REG:
11565               if (!base)
11566                 base = op;
11567               else if (!index)
11568                 index = op;
11569               else
11570                 return 0;
11571               break;
11572
11573             case CONST:
11574             case CONST_INT:
11575             case SYMBOL_REF:
11576             case LABEL_REF:
11577               if (disp)
11578                 return 0;
11579               disp = op;
11580               break;
11581
11582             default:
11583               return 0;
11584             }
11585         }
11586     }
11587   else if (GET_CODE (addr) == MULT)
11588     {
11589       index = XEXP (addr, 0);           /* index*scale */
11590       scale_rtx = XEXP (addr, 1);
11591     }
11592   else if (GET_CODE (addr) == ASHIFT)
11593     {
11594       /* We're called for lea too, which implements ashift on occasion.  */
11595       index = XEXP (addr, 0);
11596       tmp = XEXP (addr, 1);
11597       if (!CONST_INT_P (tmp))
11598         return 0;
11599       scale = INTVAL (tmp);
11600       if ((unsigned HOST_WIDE_INT) scale > 3)
11601         return 0;
11602       scale = 1 << scale;
11603       retval = -1;
11604     }
11605   else
11606     disp = addr;                        /* displacement */
11607
11608   if (index)
11609     {
11610       if (REG_P (index))
11611         ;
11612       else if (GET_CODE (index) == SUBREG
11613                && REG_P (SUBREG_REG (index)))
11614         ;
11615       else
11616         return 0;
11617     }
11618
11619   /* Extract the integral value of scale.  */
11620   if (scale_rtx)
11621     {
11622       if (!CONST_INT_P (scale_rtx))
11623         return 0;
11624       scale = INTVAL (scale_rtx);
11625     }
11626
11627   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
11628   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
11629
11630   /* Avoid useless 0 displacement.  */
11631   if (disp == const0_rtx && (base || index))
11632     disp = NULL_RTX;
11633
11634   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
11635   if (base_reg && index_reg && scale == 1
11636       && (index_reg == arg_pointer_rtx
11637           || index_reg == frame_pointer_rtx
11638           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
11639     {
11640       rtx tmp;
11641       tmp = base, base = index, index = tmp;
11642       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
11643     }
11644
11645   /* Special case: %ebp cannot be encoded as a base without a displacement.
11646      Similarly %r13.  */
11647   if (!disp
11648       && base_reg
11649       && (base_reg == hard_frame_pointer_rtx
11650           || base_reg == frame_pointer_rtx
11651           || base_reg == arg_pointer_rtx
11652           || (REG_P (base_reg)
11653               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
11654                   || REGNO (base_reg) == R13_REG))))
11655     disp = const0_rtx;
11656
11657   /* Special case: on K6, [%esi] makes the instruction vector decoded.
11658      Avoid this by transforming to [%esi+0].
11659      Reload calls address legitimization without cfun defined, so we need
11660      to test cfun for being non-NULL. */
11661   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
11662       && base_reg && !index_reg && !disp
11663       && REG_P (base_reg) && REGNO (base_reg) == SI_REG)
11664     disp = const0_rtx;
11665
11666   /* Special case: encode reg+reg instead of reg*2.  */
11667   if (!base && index && scale == 2)
11668     base = index, base_reg = index_reg, scale = 1;
11669
11670   /* Special case: scaling cannot be encoded without base or displacement.  */
11671   if (!base && !disp && index && scale != 1)
11672     disp = const0_rtx;
11673
11674   out->base = base;
11675   out->index = index;
11676   out->disp = disp;
11677   out->scale = scale;
11678   out->seg = seg;
11679
11680   return retval;
11681 }
11682 \f
11683 /* Return cost of the memory address x.
11684    For i386, it is better to use a complex address than let gcc copy
11685    the address into a reg and make a new pseudo.  But not if the address
11686    requires to two regs - that would mean more pseudos with longer
11687    lifetimes.  */
11688 static int
11689 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
11690 {
11691   struct ix86_address parts;
11692   int cost = 1;
11693   int ok = ix86_decompose_address (x, &parts);
11694
11695   gcc_assert (ok);
11696
11697   if (parts.base && GET_CODE (parts.base) == SUBREG)
11698     parts.base = SUBREG_REG (parts.base);
11699   if (parts.index && GET_CODE (parts.index) == SUBREG)
11700     parts.index = SUBREG_REG (parts.index);
11701
11702   /* Attempt to minimize number of registers in the address.  */
11703   if ((parts.base
11704        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
11705       || (parts.index
11706           && (!REG_P (parts.index)
11707               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
11708     cost++;
11709
11710   if (parts.base
11711       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
11712       && parts.index
11713       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
11714       && parts.base != parts.index)
11715     cost++;
11716
11717   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
11718      since it's predecode logic can't detect the length of instructions
11719      and it degenerates to vector decoded.  Increase cost of such
11720      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
11721      to split such addresses or even refuse such addresses at all.
11722
11723      Following addressing modes are affected:
11724       [base+scale*index]
11725       [scale*index+disp]
11726       [base+index]
11727
11728      The first and last case  may be avoidable by explicitly coding the zero in
11729      memory address, but I don't have AMD-K6 machine handy to check this
11730      theory.  */
11731
11732   if (TARGET_K6
11733       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
11734           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
11735           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
11736     cost += 10;
11737
11738   return cost;
11739 }
11740 \f
11741 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
11742    this is used for to form addresses to local data when -fPIC is in
11743    use.  */
11744
11745 static bool
11746 darwin_local_data_pic (rtx disp)
11747 {
11748   return (GET_CODE (disp) == UNSPEC
11749           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
11750 }
11751
11752 /* Determine if a given RTX is a valid constant.  We already know this
11753    satisfies CONSTANT_P.  */
11754
11755 static bool
11756 ix86_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
11757 {
11758   switch (GET_CODE (x))
11759     {
11760     case CONST:
11761       x = XEXP (x, 0);
11762
11763       if (GET_CODE (x) == PLUS)
11764         {
11765           if (!CONST_INT_P (XEXP (x, 1)))
11766             return false;
11767           x = XEXP (x, 0);
11768         }
11769
11770       if (TARGET_MACHO && darwin_local_data_pic (x))
11771         return true;
11772
11773       /* Only some unspecs are valid as "constants".  */
11774       if (GET_CODE (x) == UNSPEC)
11775         switch (XINT (x, 1))
11776           {
11777           case UNSPEC_GOT:
11778           case UNSPEC_GOTOFF:
11779           case UNSPEC_PLTOFF:
11780             return TARGET_64BIT;
11781           case UNSPEC_TPOFF:
11782           case UNSPEC_NTPOFF:
11783             x = XVECEXP (x, 0, 0);
11784             return (GET_CODE (x) == SYMBOL_REF
11785                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11786           case UNSPEC_DTPOFF:
11787             x = XVECEXP (x, 0, 0);
11788             return (GET_CODE (x) == SYMBOL_REF
11789                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
11790           default:
11791             return false;
11792           }
11793
11794       /* We must have drilled down to a symbol.  */
11795       if (GET_CODE (x) == LABEL_REF)
11796         return true;
11797       if (GET_CODE (x) != SYMBOL_REF)
11798         return false;
11799       /* FALLTHRU */
11800
11801     case SYMBOL_REF:
11802       /* TLS symbols are never valid.  */
11803       if (SYMBOL_REF_TLS_MODEL (x))
11804         return false;
11805
11806       /* DLLIMPORT symbols are never valid.  */
11807       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
11808           && SYMBOL_REF_DLLIMPORT_P (x))
11809         return false;
11810
11811 #if TARGET_MACHO
11812       /* mdynamic-no-pic */
11813       if (MACHO_DYNAMIC_NO_PIC_P)
11814         return machopic_symbol_defined_p (x);
11815 #endif
11816       break;
11817
11818     case CONST_DOUBLE:
11819       if (GET_MODE (x) == TImode
11820           && x != CONST0_RTX (TImode)
11821           && !TARGET_64BIT)
11822         return false;
11823       break;
11824
11825     case CONST_VECTOR:
11826       if (!standard_sse_constant_p (x))
11827         return false;
11828
11829     default:
11830       break;
11831     }
11832
11833   /* Otherwise we handle everything else in the move patterns.  */
11834   return true;
11835 }
11836
11837 /* Determine if it's legal to put X into the constant pool.  This
11838    is not possible for the address of thread-local symbols, which
11839    is checked above.  */
11840
11841 static bool
11842 ix86_cannot_force_const_mem (enum machine_mode mode, rtx x)
11843 {
11844   /* We can always put integral constants and vectors in memory.  */
11845   switch (GET_CODE (x))
11846     {
11847     case CONST_INT:
11848     case CONST_DOUBLE:
11849     case CONST_VECTOR:
11850       return false;
11851
11852     default:
11853       break;
11854     }
11855   return !ix86_legitimate_constant_p (mode, x);
11856 }
11857
11858
11859 /* Nonzero if the constant value X is a legitimate general operand
11860    when generating PIC code.  It is given that flag_pic is on and
11861    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
11862
11863 bool
11864 legitimate_pic_operand_p (rtx x)
11865 {
11866   rtx inner;
11867
11868   switch (GET_CODE (x))
11869     {
11870     case CONST:
11871       inner = XEXP (x, 0);
11872       if (GET_CODE (inner) == PLUS
11873           && CONST_INT_P (XEXP (inner, 1)))
11874         inner = XEXP (inner, 0);
11875
11876       /* Only some unspecs are valid as "constants".  */
11877       if (GET_CODE (inner) == UNSPEC)
11878         switch (XINT (inner, 1))
11879           {
11880           case UNSPEC_GOT:
11881           case UNSPEC_GOTOFF:
11882           case UNSPEC_PLTOFF:
11883             return TARGET_64BIT;
11884           case UNSPEC_TPOFF:
11885             x = XVECEXP (inner, 0, 0);
11886             return (GET_CODE (x) == SYMBOL_REF
11887                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11888           case UNSPEC_MACHOPIC_OFFSET:
11889             return legitimate_pic_address_disp_p (x);
11890           default:
11891             return false;
11892           }
11893       /* FALLTHRU */
11894
11895     case SYMBOL_REF:
11896     case LABEL_REF:
11897       return legitimate_pic_address_disp_p (x);
11898
11899     default:
11900       return true;
11901     }
11902 }
11903
11904 /* Determine if a given CONST RTX is a valid memory displacement
11905    in PIC mode.  */
11906
11907 bool
11908 legitimate_pic_address_disp_p (rtx disp)
11909 {
11910   bool saw_plus;
11911
11912   /* In 64bit mode we can allow direct addresses of symbols and labels
11913      when they are not dynamic symbols.  */
11914   if (TARGET_64BIT)
11915     {
11916       rtx op0 = disp, op1;
11917
11918       switch (GET_CODE (disp))
11919         {
11920         case LABEL_REF:
11921           return true;
11922
11923         case CONST:
11924           if (GET_CODE (XEXP (disp, 0)) != PLUS)
11925             break;
11926           op0 = XEXP (XEXP (disp, 0), 0);
11927           op1 = XEXP (XEXP (disp, 0), 1);
11928           if (!CONST_INT_P (op1)
11929               || INTVAL (op1) >= 16*1024*1024
11930               || INTVAL (op1) < -16*1024*1024)
11931             break;
11932           if (GET_CODE (op0) == LABEL_REF)
11933             return true;
11934           if (GET_CODE (op0) == CONST
11935               && GET_CODE (XEXP (op0, 0)) == UNSPEC
11936               && XINT (XEXP (op0, 0), 1) == UNSPEC_PCREL)
11937             return true;
11938           if (GET_CODE (op0) == UNSPEC
11939               && XINT (op0, 1) == UNSPEC_PCREL)
11940             return true;
11941           if (GET_CODE (op0) != SYMBOL_REF)
11942             break;
11943           /* FALLTHRU */
11944
11945         case SYMBOL_REF:
11946           /* TLS references should always be enclosed in UNSPEC.  */
11947           if (SYMBOL_REF_TLS_MODEL (op0))
11948             return false;
11949           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
11950               && ix86_cmodel != CM_LARGE_PIC)
11951             return true;
11952           break;
11953
11954         default:
11955           break;
11956         }
11957     }
11958   if (GET_CODE (disp) != CONST)
11959     return false;
11960   disp = XEXP (disp, 0);
11961
11962   if (TARGET_64BIT)
11963     {
11964       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
11965          of GOT tables.  We should not need these anyway.  */
11966       if (GET_CODE (disp) != UNSPEC
11967           || (XINT (disp, 1) != UNSPEC_GOTPCREL
11968               && XINT (disp, 1) != UNSPEC_GOTOFF
11969               && XINT (disp, 1) != UNSPEC_PCREL
11970               && XINT (disp, 1) != UNSPEC_PLTOFF))
11971         return false;
11972
11973       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
11974           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
11975         return false;
11976       return true;
11977     }
11978
11979   saw_plus = false;
11980   if (GET_CODE (disp) == PLUS)
11981     {
11982       if (!CONST_INT_P (XEXP (disp, 1)))
11983         return false;
11984       disp = XEXP (disp, 0);
11985       saw_plus = true;
11986     }
11987
11988   if (TARGET_MACHO && darwin_local_data_pic (disp))
11989     return true;
11990
11991   if (GET_CODE (disp) != UNSPEC)
11992     return false;
11993
11994   switch (XINT (disp, 1))
11995     {
11996     case UNSPEC_GOT:
11997       if (saw_plus)
11998         return false;
11999       /* We need to check for both symbols and labels because VxWorks loads
12000          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
12001          details.  */
12002       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
12003               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
12004     case UNSPEC_GOTOFF:
12005       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
12006          While ABI specify also 32bit relocation but we don't produce it in
12007          small PIC model at all.  */
12008       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
12009            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
12010           && !TARGET_64BIT)
12011         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
12012       return false;
12013     case UNSPEC_GOTTPOFF:
12014     case UNSPEC_GOTNTPOFF:
12015     case UNSPEC_INDNTPOFF:
12016       if (saw_plus)
12017         return false;
12018       disp = XVECEXP (disp, 0, 0);
12019       return (GET_CODE (disp) == SYMBOL_REF
12020               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
12021     case UNSPEC_NTPOFF:
12022       disp = XVECEXP (disp, 0, 0);
12023       return (GET_CODE (disp) == SYMBOL_REF
12024               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
12025     case UNSPEC_DTPOFF:
12026       disp = XVECEXP (disp, 0, 0);
12027       return (GET_CODE (disp) == SYMBOL_REF
12028               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
12029     }
12030
12031   return false;
12032 }
12033
12034 /* Our implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
12035    replace the input X, or the original X if no replacement is called for.
12036    The output parameter *WIN is 1 if the calling macro should goto WIN,
12037    0 if it should not.  */
12038
12039 bool
12040 ix86_legitimize_reload_address (rtx x,
12041                                 enum machine_mode mode ATTRIBUTE_UNUSED,
12042                                 int opnum, int type,
12043                                 int ind_levels ATTRIBUTE_UNUSED)
12044 {
12045   /* Reload can generate:
12046
12047      (plus:DI (plus:DI (unspec:DI [(const_int 0 [0])] UNSPEC_TP)
12048                        (reg:DI 97))
12049               (reg:DI 2 cx))
12050
12051      This RTX is rejected from ix86_legitimate_address_p due to
12052      non-strictness of base register 97.  Following this rejection, 
12053      reload pushes all three components into separate registers,
12054      creating invalid memory address RTX.
12055
12056      Following code reloads only the invalid part of the
12057      memory address RTX.  */
12058
12059   if (GET_CODE (x) == PLUS
12060       && REG_P (XEXP (x, 1))
12061       && GET_CODE (XEXP (x, 0)) == PLUS
12062       && REG_P (XEXP (XEXP (x, 0), 1)))
12063     {
12064       rtx base, index;
12065       bool something_reloaded = false;
12066
12067       base = XEXP (XEXP (x, 0), 1);      
12068       if (!REG_OK_FOR_BASE_STRICT_P (base))
12069         {
12070           push_reload (base, NULL_RTX, &XEXP (XEXP (x, 0), 1), NULL,
12071                        BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
12072                        opnum, (enum reload_type)type);
12073           something_reloaded = true;
12074         }
12075
12076       index = XEXP (x, 1);
12077       if (!REG_OK_FOR_INDEX_STRICT_P (index))
12078         {
12079           push_reload (index, NULL_RTX, &XEXP (x, 1), NULL,
12080                        INDEX_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
12081                        opnum, (enum reload_type)type);
12082           something_reloaded = true;
12083         }
12084
12085       gcc_assert (something_reloaded);
12086       return true;
12087     }
12088
12089   return false;
12090 }
12091
12092 /* Determine if op is suitable RTX for an address register.
12093    Return naked register if a register or a register subreg is
12094    found, otherwise return NULL_RTX.  */
12095
12096 static rtx
12097 ix86_validate_address_register (rtx op)
12098 {
12099   enum machine_mode mode = GET_MODE (op);
12100
12101   /* Only SImode or DImode registers can form the address.  */
12102   if (mode != SImode && mode != DImode)
12103     return NULL_RTX;
12104
12105   if (REG_P (op))
12106     return op;
12107   else if (GET_CODE (op) == SUBREG)
12108     {
12109       rtx reg = SUBREG_REG (op);
12110
12111       if (!REG_P (reg))
12112         return NULL_RTX;
12113
12114       mode = GET_MODE (reg);
12115
12116       /* Don't allow SUBREGs that span more than a word.  It can
12117          lead to spill failures when the register is one word out
12118          of a two word structure.  */
12119       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
12120         return NULL_RTX;
12121
12122       /* Allow only SUBREGs of non-eliminable hard registers.  */
12123       if (register_no_elim_operand (reg, mode))
12124         return reg;
12125     }
12126
12127   /* Op is not a register.  */
12128   return NULL_RTX;
12129 }
12130
12131 /* Recognizes RTL expressions that are valid memory addresses for an
12132    instruction.  The MODE argument is the machine mode for the MEM
12133    expression that wants to use this address.
12134
12135    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
12136    convert common non-canonical forms to canonical form so that they will
12137    be recognized.  */
12138
12139 static bool
12140 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
12141                            rtx addr, bool strict)
12142 {
12143   struct ix86_address parts;
12144   rtx base, index, disp;
12145   HOST_WIDE_INT scale;
12146   enum ix86_address_seg seg;
12147
12148   if (ix86_decompose_address (addr, &parts) <= 0)
12149     /* Decomposition failed.  */
12150     return false;
12151
12152   base = parts.base;
12153   index = parts.index;
12154   disp = parts.disp;
12155   scale = parts.scale;
12156   seg = parts.seg;
12157
12158   /* Validate base register.  */
12159   if (base)
12160     {
12161       rtx reg = ix86_validate_address_register (base);
12162
12163       if (reg == NULL_RTX)
12164         return false;
12165
12166       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
12167           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
12168         /* Base is not valid.  */
12169         return false;
12170     }
12171
12172   /* Validate index register.  */
12173   if (index)
12174     {
12175       rtx reg = ix86_validate_address_register (index);
12176
12177       if (reg == NULL_RTX)
12178         return false;
12179
12180       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
12181           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
12182         /* Index is not valid.  */
12183         return false;
12184     }
12185
12186   /* Index and base should have the same mode.  */
12187   if (base && index
12188       && GET_MODE (base) != GET_MODE (index))
12189     return false;
12190
12191   /* Address override works only on the (%reg) part of %fs:(%reg).  */
12192   if (seg != SEG_DEFAULT
12193       && ((base && GET_MODE (base) != word_mode)
12194           || (index && GET_MODE (index) != word_mode)))
12195     return false;
12196
12197   /* Validate scale factor.  */
12198   if (scale != 1)
12199     {
12200       if (!index)
12201         /* Scale without index.  */
12202         return false;
12203
12204       if (scale != 2 && scale != 4 && scale != 8)
12205         /* Scale is not a valid multiplier.  */
12206         return false;
12207     }
12208
12209   /* Validate displacement.  */
12210   if (disp)
12211     {
12212       if (GET_CODE (disp) == CONST
12213           && GET_CODE (XEXP (disp, 0)) == UNSPEC
12214           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
12215         switch (XINT (XEXP (disp, 0), 1))
12216           {
12217           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
12218              used.  While ABI specify also 32bit relocations, we don't produce
12219              them at all and use IP relative instead.  */
12220           case UNSPEC_GOT:
12221           case UNSPEC_GOTOFF:
12222             gcc_assert (flag_pic);
12223             if (!TARGET_64BIT)
12224               goto is_legitimate_pic;
12225
12226             /* 64bit address unspec.  */
12227             return false;
12228
12229           case UNSPEC_GOTPCREL:
12230           case UNSPEC_PCREL:
12231             gcc_assert (flag_pic);
12232             goto is_legitimate_pic;
12233
12234           case UNSPEC_GOTTPOFF:
12235           case UNSPEC_GOTNTPOFF:
12236           case UNSPEC_INDNTPOFF:
12237           case UNSPEC_NTPOFF:
12238           case UNSPEC_DTPOFF:
12239             break;
12240
12241           case UNSPEC_STACK_CHECK:
12242             gcc_assert (flag_split_stack);
12243             break;
12244
12245           default:
12246             /* Invalid address unspec.  */
12247             return false;
12248           }
12249
12250       else if (SYMBOLIC_CONST (disp)
12251                && (flag_pic
12252                    || (TARGET_MACHO
12253 #if TARGET_MACHO
12254                        && MACHOPIC_INDIRECT
12255                        && !machopic_operand_p (disp)
12256 #endif
12257                )))
12258         {
12259
12260         is_legitimate_pic:
12261           if (TARGET_64BIT && (index || base))
12262             {
12263               /* foo@dtpoff(%rX) is ok.  */
12264               if (GET_CODE (disp) != CONST
12265                   || GET_CODE (XEXP (disp, 0)) != PLUS
12266                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
12267                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
12268                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
12269                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
12270                 /* Non-constant pic memory reference.  */
12271                 return false;
12272             }
12273           else if ((!TARGET_MACHO || flag_pic)
12274                     && ! legitimate_pic_address_disp_p (disp))
12275             /* Displacement is an invalid pic construct.  */
12276             return false;
12277 #if TARGET_MACHO
12278           else if (MACHO_DYNAMIC_NO_PIC_P
12279                    && !ix86_legitimate_constant_p (Pmode, disp))
12280             /* displacment must be referenced via non_lazy_pointer */
12281             return false;
12282 #endif
12283
12284           /* This code used to verify that a symbolic pic displacement
12285              includes the pic_offset_table_rtx register.
12286
12287              While this is good idea, unfortunately these constructs may
12288              be created by "adds using lea" optimization for incorrect
12289              code like:
12290
12291              int a;
12292              int foo(int i)
12293                {
12294                  return *(&a+i);
12295                }
12296
12297              This code is nonsensical, but results in addressing
12298              GOT table with pic_offset_table_rtx base.  We can't
12299              just refuse it easily, since it gets matched by
12300              "addsi3" pattern, that later gets split to lea in the
12301              case output register differs from input.  While this
12302              can be handled by separate addsi pattern for this case
12303              that never results in lea, this seems to be easier and
12304              correct fix for crash to disable this test.  */
12305         }
12306       else if (GET_CODE (disp) != LABEL_REF
12307                && !CONST_INT_P (disp)
12308                && (GET_CODE (disp) != CONST
12309                    || !ix86_legitimate_constant_p (Pmode, disp))
12310                && (GET_CODE (disp) != SYMBOL_REF
12311                    || !ix86_legitimate_constant_p (Pmode, disp)))
12312         /* Displacement is not constant.  */
12313         return false;
12314       else if (TARGET_64BIT
12315                && !x86_64_immediate_operand (disp, VOIDmode))
12316         /* Displacement is out of range.  */
12317         return false;
12318       /* In x32 mode, constant addresses are sign extended to 64bit, so
12319          we have to prevent addresses from 0x80000000 to 0xffffffff.  */
12320       else if (TARGET_X32 && !(index || base)
12321                && CONST_INT_P (disp)
12322                && val_signbit_known_set_p (SImode, INTVAL (disp)))
12323         return false;
12324     }
12325
12326   /* Everything looks valid.  */
12327   return true;
12328 }
12329
12330 /* Determine if a given RTX is a valid constant address.  */
12331
12332 bool
12333 constant_address_p (rtx x)
12334 {
12335   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
12336 }
12337 \f
12338 /* Return a unique alias set for the GOT.  */
12339
12340 static alias_set_type
12341 ix86_GOT_alias_set (void)
12342 {
12343   static alias_set_type set = -1;
12344   if (set == -1)
12345     set = new_alias_set ();
12346   return set;
12347 }
12348
12349 /* Return a legitimate reference for ORIG (an address) using the
12350    register REG.  If REG is 0, a new pseudo is generated.
12351
12352    There are two types of references that must be handled:
12353
12354    1. Global data references must load the address from the GOT, via
12355       the PIC reg.  An insn is emitted to do this load, and the reg is
12356       returned.
12357
12358    2. Static data references, constant pool addresses, and code labels
12359       compute the address as an offset from the GOT, whose base is in
12360       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
12361       differentiate them from global data objects.  The returned
12362       address is the PIC reg + an unspec constant.
12363
12364    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
12365    reg also appears in the address.  */
12366
12367 static rtx
12368 legitimize_pic_address (rtx orig, rtx reg)
12369 {
12370   rtx addr = orig;
12371   rtx new_rtx = orig;
12372
12373 #if TARGET_MACHO
12374   if (TARGET_MACHO && !TARGET_64BIT)
12375     {
12376       if (reg == 0)
12377         reg = gen_reg_rtx (Pmode);
12378       /* Use the generic Mach-O PIC machinery.  */
12379       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
12380     }
12381 #endif
12382
12383   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
12384     new_rtx = addr;
12385   else if (TARGET_64BIT
12386            && ix86_cmodel != CM_SMALL_PIC
12387            && gotoff_operand (addr, Pmode))
12388     {
12389       rtx tmpreg;
12390       /* This symbol may be referenced via a displacement from the PIC
12391          base address (@GOTOFF).  */
12392
12393       if (reload_in_progress)
12394         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12395       if (GET_CODE (addr) == CONST)
12396         addr = XEXP (addr, 0);
12397       if (GET_CODE (addr) == PLUS)
12398           {
12399             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12400                                       UNSPEC_GOTOFF);
12401             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12402           }
12403         else
12404           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12405       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12406       if (!reg)
12407         tmpreg = gen_reg_rtx (Pmode);
12408       else
12409         tmpreg = reg;
12410       emit_move_insn (tmpreg, new_rtx);
12411
12412       if (reg != 0)
12413         {
12414           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
12415                                          tmpreg, 1, OPTAB_DIRECT);
12416           new_rtx = reg;
12417         }
12418       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
12419     }
12420   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
12421     {
12422       /* This symbol may be referenced via a displacement from the PIC
12423          base address (@GOTOFF).  */
12424
12425       if (reload_in_progress)
12426         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12427       if (GET_CODE (addr) == CONST)
12428         addr = XEXP (addr, 0);
12429       if (GET_CODE (addr) == PLUS)
12430           {
12431             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12432                                       UNSPEC_GOTOFF);
12433             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12434           }
12435         else
12436           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12437       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12438       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12439
12440       if (reg != 0)
12441         {
12442           emit_move_insn (reg, new_rtx);
12443           new_rtx = reg;
12444         }
12445     }
12446   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
12447            /* We can't use @GOTOFF for text labels on VxWorks;
12448               see gotoff_operand.  */
12449            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
12450     {
12451       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12452         {
12453           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
12454             return legitimize_dllimport_symbol (addr, true);
12455           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
12456               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
12457               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
12458             {
12459               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
12460               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
12461             }
12462         }
12463
12464       /* For x64 PE-COFF there is no GOT table.  So we use address
12465          directly.  */
12466       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
12467       {
12468           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_PCREL);
12469           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12470
12471           if (reg == 0)
12472             reg = gen_reg_rtx (Pmode);
12473           emit_move_insn (reg, new_rtx);
12474           new_rtx = reg;
12475       }
12476       else if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
12477         {
12478           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
12479           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12480           new_rtx = gen_const_mem (Pmode, new_rtx);
12481           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12482
12483           if (reg == 0)
12484             reg = gen_reg_rtx (Pmode);
12485           /* Use directly gen_movsi, otherwise the address is loaded
12486              into register for CSE.  We don't want to CSE this addresses,
12487              instead we CSE addresses from the GOT table, so skip this.  */
12488           emit_insn (gen_movsi (reg, new_rtx));
12489           new_rtx = reg;
12490         }
12491       else
12492         {
12493           /* This symbol must be referenced via a load from the
12494              Global Offset Table (@GOT).  */
12495
12496           if (reload_in_progress)
12497             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12498           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
12499           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12500           if (TARGET_64BIT)
12501             new_rtx = force_reg (Pmode, new_rtx);
12502           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12503           new_rtx = gen_const_mem (Pmode, new_rtx);
12504           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12505
12506           if (reg == 0)
12507             reg = gen_reg_rtx (Pmode);
12508           emit_move_insn (reg, new_rtx);
12509           new_rtx = reg;
12510         }
12511     }
12512   else
12513     {
12514       if (CONST_INT_P (addr)
12515           && !x86_64_immediate_operand (addr, VOIDmode))
12516         {
12517           if (reg)
12518             {
12519               emit_move_insn (reg, addr);
12520               new_rtx = reg;
12521             }
12522           else
12523             new_rtx = force_reg (Pmode, addr);
12524         }
12525       else if (GET_CODE (addr) == CONST)
12526         {
12527           addr = XEXP (addr, 0);
12528
12529           /* We must match stuff we generate before.  Assume the only
12530              unspecs that can get here are ours.  Not that we could do
12531              anything with them anyway....  */
12532           if (GET_CODE (addr) == UNSPEC
12533               || (GET_CODE (addr) == PLUS
12534                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
12535             return orig;
12536           gcc_assert (GET_CODE (addr) == PLUS);
12537         }
12538       if (GET_CODE (addr) == PLUS)
12539         {
12540           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
12541
12542           /* Check first to see if this is a constant offset from a @GOTOFF
12543              symbol reference.  */
12544           if (gotoff_operand (op0, Pmode)
12545               && CONST_INT_P (op1))
12546             {
12547               if (!TARGET_64BIT)
12548                 {
12549                   if (reload_in_progress)
12550                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12551                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
12552                                             UNSPEC_GOTOFF);
12553                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
12554                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12555                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12556
12557                   if (reg != 0)
12558                     {
12559                       emit_move_insn (reg, new_rtx);
12560                       new_rtx = reg;
12561                     }
12562                 }
12563               else
12564                 {
12565                   if (INTVAL (op1) < -16*1024*1024
12566                       || INTVAL (op1) >= 16*1024*1024)
12567                     {
12568                       if (!x86_64_immediate_operand (op1, Pmode))
12569                         op1 = force_reg (Pmode, op1);
12570                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
12571                     }
12572                 }
12573             }
12574           else
12575             {
12576               rtx base = legitimize_pic_address (op0, reg);
12577               enum machine_mode mode = GET_MODE (base);
12578               new_rtx
12579                 = legitimize_pic_address (op1, base == reg ? NULL_RTX : reg);
12580
12581               if (CONST_INT_P (new_rtx))
12582                 {
12583                   if (INTVAL (new_rtx) < -16*1024*1024
12584                       || INTVAL (new_rtx) >= 16*1024*1024)
12585                     {
12586                       if (!x86_64_immediate_operand (new_rtx, mode))
12587                         new_rtx = force_reg (mode, new_rtx);
12588                       new_rtx
12589                         = gen_rtx_PLUS (mode, force_reg (mode, base), new_rtx);
12590                     }
12591                   else
12592                     new_rtx = plus_constant (base, INTVAL (new_rtx));
12593                 }
12594               else
12595                 {
12596                   if (GET_CODE (new_rtx) == PLUS
12597                       && CONSTANT_P (XEXP (new_rtx, 1)))
12598                     {
12599                       base = gen_rtx_PLUS (mode, base, XEXP (new_rtx, 0));
12600                       new_rtx = XEXP (new_rtx, 1);
12601                     }
12602                   new_rtx = gen_rtx_PLUS (mode, base, new_rtx);
12603                 }
12604             }
12605         }
12606     }
12607   return new_rtx;
12608 }
12609 \f
12610 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
12611
12612 static rtx
12613 get_thread_pointer (bool to_reg)
12614 {
12615   rtx tp = gen_rtx_UNSPEC (ptr_mode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
12616
12617   if (GET_MODE (tp) != Pmode)
12618     tp = convert_to_mode (Pmode, tp, 1);
12619
12620   if (to_reg)
12621     tp = copy_addr_to_reg (tp);
12622
12623   return tp;
12624 }
12625
12626 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
12627
12628 static GTY(()) rtx ix86_tls_symbol;
12629
12630 static rtx
12631 ix86_tls_get_addr (void)
12632 {
12633   if (!ix86_tls_symbol)
12634     {
12635       const char *sym
12636         = ((TARGET_ANY_GNU_TLS && !TARGET_64BIT)
12637            ? "___tls_get_addr" : "__tls_get_addr");
12638
12639       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, sym);
12640     }
12641
12642   return ix86_tls_symbol;
12643 }
12644
12645 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
12646
12647 static GTY(()) rtx ix86_tls_module_base_symbol;
12648
12649 rtx
12650 ix86_tls_module_base (void)
12651 {
12652   if (!ix86_tls_module_base_symbol)
12653     {
12654       ix86_tls_module_base_symbol
12655         = gen_rtx_SYMBOL_REF (Pmode, "_TLS_MODULE_BASE_");
12656
12657       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
12658         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
12659     }
12660
12661   return ix86_tls_module_base_symbol;
12662 }
12663
12664 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
12665    false if we expect this to be used for a memory address and true if
12666    we expect to load the address into a register.  */
12667
12668 static rtx
12669 legitimize_tls_address (rtx x, enum tls_model model, bool for_mov)
12670 {
12671   rtx dest, base, off;
12672   rtx pic = NULL_RTX, tp = NULL_RTX;
12673   int type;
12674
12675   switch (model)
12676     {
12677     case TLS_MODEL_GLOBAL_DYNAMIC:
12678       dest = gen_reg_rtx (Pmode);
12679
12680       if (!TARGET_64BIT)
12681         {
12682           if (flag_pic)
12683             pic = pic_offset_table_rtx;
12684           else
12685             {
12686               pic = gen_reg_rtx (Pmode);
12687               emit_insn (gen_set_got (pic));
12688             }
12689         }
12690
12691       if (TARGET_GNU2_TLS)
12692         {
12693           if (TARGET_64BIT)
12694             emit_insn (gen_tls_dynamic_gnu2_64 (dest, x));
12695           else
12696             emit_insn (gen_tls_dynamic_gnu2_32 (dest, x, pic));
12697
12698           tp = get_thread_pointer (true);
12699           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
12700
12701           if (GET_MODE (x) != Pmode)
12702             x = gen_rtx_ZERO_EXTEND (Pmode, x);
12703
12704           set_unique_reg_note (get_last_insn (), REG_EQUAL, x);
12705         }
12706       else
12707         {
12708           rtx caddr = ix86_tls_get_addr ();
12709
12710           if (TARGET_64BIT)
12711             {
12712               rtx rax = gen_rtx_REG (Pmode, AX_REG);
12713               rtx insns;
12714
12715               start_sequence ();
12716               emit_call_insn (gen_tls_global_dynamic_64 (rax, x, caddr));
12717               insns = get_insns ();
12718               end_sequence ();
12719
12720               if (GET_MODE (x) != Pmode)
12721                 x = gen_rtx_ZERO_EXTEND (Pmode, x);
12722
12723               RTL_CONST_CALL_P (insns) = 1;
12724               emit_libcall_block (insns, dest, rax, x);
12725             }
12726           else
12727             emit_insn (gen_tls_global_dynamic_32 (dest, x, pic, caddr));
12728         }
12729       break;
12730
12731     case TLS_MODEL_LOCAL_DYNAMIC:
12732       base = gen_reg_rtx (Pmode);
12733
12734       if (!TARGET_64BIT)
12735         {
12736           if (flag_pic)
12737             pic = pic_offset_table_rtx;
12738           else
12739             {
12740               pic = gen_reg_rtx (Pmode);
12741               emit_insn (gen_set_got (pic));
12742             }
12743         }
12744
12745       if (TARGET_GNU2_TLS)
12746         {
12747           rtx tmp = ix86_tls_module_base ();
12748
12749           if (TARGET_64BIT)
12750             emit_insn (gen_tls_dynamic_gnu2_64 (base, tmp));
12751           else
12752             emit_insn (gen_tls_dynamic_gnu2_32 (base, tmp, pic));
12753
12754           tp = get_thread_pointer (true);
12755           set_unique_reg_note (get_last_insn (), REG_EQUAL,
12756                                gen_rtx_MINUS (Pmode, tmp, tp));
12757         }
12758       else
12759         {
12760           rtx caddr = ix86_tls_get_addr ();
12761
12762           if (TARGET_64BIT)
12763             {
12764               rtx rax = gen_rtx_REG (Pmode, AX_REG);
12765               rtx insns, eqv;
12766
12767               start_sequence ();
12768               emit_call_insn (gen_tls_local_dynamic_base_64 (rax, caddr));
12769               insns = get_insns ();
12770               end_sequence ();
12771
12772               /* Attach a unique REG_EQUAL, to allow the RTL optimizers to
12773                  share the LD_BASE result with other LD model accesses.  */
12774               eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
12775                                     UNSPEC_TLS_LD_BASE);
12776
12777               RTL_CONST_CALL_P (insns) = 1;
12778               emit_libcall_block (insns, base, rax, eqv);
12779             }
12780           else
12781             emit_insn (gen_tls_local_dynamic_base_32 (base, pic, caddr));
12782         }
12783
12784       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
12785       off = gen_rtx_CONST (Pmode, off);
12786
12787       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
12788
12789       if (TARGET_GNU2_TLS)
12790         {
12791           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
12792
12793           if (GET_MODE (x) != Pmode)
12794             x = gen_rtx_ZERO_EXTEND (Pmode, x);
12795
12796           set_unique_reg_note (get_last_insn (), REG_EQUAL, x);
12797         }
12798       break;
12799
12800     case TLS_MODEL_INITIAL_EXEC:
12801       if (TARGET_64BIT)
12802         {
12803           if (TARGET_SUN_TLS)
12804             {
12805               /* The Sun linker took the AMD64 TLS spec literally
12806                  and can only handle %rax as destination of the
12807                  initial executable code sequence.  */
12808
12809               dest = gen_reg_rtx (Pmode);
12810               emit_insn (gen_tls_initial_exec_64_sun (dest, x));
12811               return dest;
12812             }
12813
12814           pic = NULL;
12815           type = UNSPEC_GOTNTPOFF;
12816         }
12817       else if (flag_pic)
12818         {
12819           if (reload_in_progress)
12820             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12821           pic = pic_offset_table_rtx;
12822           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
12823         }
12824       else if (!TARGET_ANY_GNU_TLS)
12825         {
12826           pic = gen_reg_rtx (Pmode);
12827           emit_insn (gen_set_got (pic));
12828           type = UNSPEC_GOTTPOFF;
12829         }
12830       else
12831         {
12832           pic = NULL;
12833           type = UNSPEC_INDNTPOFF;
12834         }
12835
12836       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
12837       off = gen_rtx_CONST (Pmode, off);
12838       if (pic)
12839         off = gen_rtx_PLUS (Pmode, pic, off);
12840       off = gen_const_mem (Pmode, off);
12841       set_mem_alias_set (off, ix86_GOT_alias_set ());
12842
12843       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12844         {
12845           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12846           off = force_reg (Pmode, off);
12847           return gen_rtx_PLUS (Pmode, base, off);
12848         }
12849       else
12850         {
12851           base = get_thread_pointer (true);
12852           dest = gen_reg_rtx (Pmode);
12853           emit_insn (gen_subsi3 (dest, base, off));
12854         }
12855       break;
12856
12857     case TLS_MODEL_LOCAL_EXEC:
12858       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
12859                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12860                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
12861       off = gen_rtx_CONST (Pmode, off);
12862
12863       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12864         {
12865           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12866           return gen_rtx_PLUS (Pmode, base, off);
12867         }
12868       else
12869         {
12870           base = get_thread_pointer (true);
12871           dest = gen_reg_rtx (Pmode);
12872           emit_insn (gen_subsi3 (dest, base, off));
12873         }
12874       break;
12875
12876     default:
12877       gcc_unreachable ();
12878     }
12879
12880   return dest;
12881 }
12882
12883 /* Create or return the unique __imp_DECL dllimport symbol corresponding
12884    to symbol DECL.  */
12885
12886 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
12887   htab_t dllimport_map;
12888
12889 static tree
12890 get_dllimport_decl (tree decl)
12891 {
12892   struct tree_map *h, in;
12893   void **loc;
12894   const char *name;
12895   const char *prefix;
12896   size_t namelen, prefixlen;
12897   char *imp_name;
12898   tree to;
12899   rtx rtl;
12900
12901   if (!dllimport_map)
12902     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
12903
12904   in.hash = htab_hash_pointer (decl);
12905   in.base.from = decl;
12906   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
12907   h = (struct tree_map *) *loc;
12908   if (h)
12909     return h->to;
12910
12911   *loc = h = ggc_alloc_tree_map ();
12912   h->hash = in.hash;
12913   h->base.from = decl;
12914   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
12915                            VAR_DECL, NULL, ptr_type_node);
12916   DECL_ARTIFICIAL (to) = 1;
12917   DECL_IGNORED_P (to) = 1;
12918   DECL_EXTERNAL (to) = 1;
12919   TREE_READONLY (to) = 1;
12920
12921   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
12922   name = targetm.strip_name_encoding (name);
12923   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
12924     ? "*__imp_" : "*__imp__";
12925   namelen = strlen (name);
12926   prefixlen = strlen (prefix);
12927   imp_name = (char *) alloca (namelen + prefixlen + 1);
12928   memcpy (imp_name, prefix, prefixlen);
12929   memcpy (imp_name + prefixlen, name, namelen + 1);
12930
12931   name = ggc_alloc_string (imp_name, namelen + prefixlen);
12932   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
12933   SET_SYMBOL_REF_DECL (rtl, to);
12934   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
12935
12936   rtl = gen_const_mem (Pmode, rtl);
12937   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
12938
12939   SET_DECL_RTL (to, rtl);
12940   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
12941
12942   return to;
12943 }
12944
12945 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
12946    true if we require the result be a register.  */
12947
12948 static rtx
12949 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
12950 {
12951   tree imp_decl;
12952   rtx x;
12953
12954   gcc_assert (SYMBOL_REF_DECL (symbol));
12955   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
12956
12957   x = DECL_RTL (imp_decl);
12958   if (want_reg)
12959     x = force_reg (Pmode, x);
12960   return x;
12961 }
12962
12963 /* Try machine-dependent ways of modifying an illegitimate address
12964    to be legitimate.  If we find one, return the new, valid address.
12965    This macro is used in only one place: `memory_address' in explow.c.
12966
12967    OLDX is the address as it was before break_out_memory_refs was called.
12968    In some cases it is useful to look at this to decide what needs to be done.
12969
12970    It is always safe for this macro to do nothing.  It exists to recognize
12971    opportunities to optimize the output.
12972
12973    For the 80386, we handle X+REG by loading X into a register R and
12974    using R+REG.  R will go in a general reg and indexing will be used.
12975    However, if REG is a broken-out memory address or multiplication,
12976    nothing needs to be done because REG can certainly go in a general reg.
12977
12978    When -fpic is used, special handling is needed for symbolic references.
12979    See comments by legitimize_pic_address in i386.c for details.  */
12980
12981 static rtx
12982 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
12983                          enum machine_mode mode)
12984 {
12985   int changed = 0;
12986   unsigned log;
12987
12988   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
12989   if (log)
12990     return legitimize_tls_address (x, (enum tls_model) log, false);
12991   if (GET_CODE (x) == CONST
12992       && GET_CODE (XEXP (x, 0)) == PLUS
12993       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12994       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
12995     {
12996       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
12997                                       (enum tls_model) log, false);
12998       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12999     }
13000
13001   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
13002     {
13003       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
13004         return legitimize_dllimport_symbol (x, true);
13005       if (GET_CODE (x) == CONST
13006           && GET_CODE (XEXP (x, 0)) == PLUS
13007           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
13008           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
13009         {
13010           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
13011           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
13012         }
13013     }
13014
13015   if (flag_pic && SYMBOLIC_CONST (x))
13016     return legitimize_pic_address (x, 0);
13017
13018 #if TARGET_MACHO
13019   if (MACHO_DYNAMIC_NO_PIC_P && SYMBOLIC_CONST (x))
13020     return machopic_indirect_data_reference (x, 0);
13021 #endif
13022
13023   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
13024   if (GET_CODE (x) == ASHIFT
13025       && CONST_INT_P (XEXP (x, 1))
13026       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
13027     {
13028       changed = 1;
13029       log = INTVAL (XEXP (x, 1));
13030       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
13031                         GEN_INT (1 << log));
13032     }
13033
13034   if (GET_CODE (x) == PLUS)
13035     {
13036       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
13037
13038       if (GET_CODE (XEXP (x, 0)) == ASHIFT
13039           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
13040           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
13041         {
13042           changed = 1;
13043           log = INTVAL (XEXP (XEXP (x, 0), 1));
13044           XEXP (x, 0) = gen_rtx_MULT (Pmode,
13045                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
13046                                       GEN_INT (1 << log));
13047         }
13048
13049       if (GET_CODE (XEXP (x, 1)) == ASHIFT
13050           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
13051           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
13052         {
13053           changed = 1;
13054           log = INTVAL (XEXP (XEXP (x, 1), 1));
13055           XEXP (x, 1) = gen_rtx_MULT (Pmode,
13056                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
13057                                       GEN_INT (1 << log));
13058         }
13059
13060       /* Put multiply first if it isn't already.  */
13061       if (GET_CODE (XEXP (x, 1)) == MULT)
13062         {
13063           rtx tmp = XEXP (x, 0);
13064           XEXP (x, 0) = XEXP (x, 1);
13065           XEXP (x, 1) = tmp;
13066           changed = 1;
13067         }
13068
13069       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
13070          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
13071          created by virtual register instantiation, register elimination, and
13072          similar optimizations.  */
13073       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
13074         {
13075           changed = 1;
13076           x = gen_rtx_PLUS (Pmode,
13077                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
13078                                           XEXP (XEXP (x, 1), 0)),
13079                             XEXP (XEXP (x, 1), 1));
13080         }
13081
13082       /* Canonicalize
13083          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
13084          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
13085       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
13086                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
13087                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
13088                && CONSTANT_P (XEXP (x, 1)))
13089         {
13090           rtx constant;
13091           rtx other = NULL_RTX;
13092
13093           if (CONST_INT_P (XEXP (x, 1)))
13094             {
13095               constant = XEXP (x, 1);
13096               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
13097             }
13098           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
13099             {
13100               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
13101               other = XEXP (x, 1);
13102             }
13103           else
13104             constant = 0;
13105
13106           if (constant)
13107             {
13108               changed = 1;
13109               x = gen_rtx_PLUS (Pmode,
13110                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
13111                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
13112                                 plus_constant (other, INTVAL (constant)));
13113             }
13114         }
13115
13116       if (changed && ix86_legitimate_address_p (mode, x, false))
13117         return x;
13118
13119       if (GET_CODE (XEXP (x, 0)) == MULT)
13120         {
13121           changed = 1;
13122           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
13123         }
13124
13125       if (GET_CODE (XEXP (x, 1)) == MULT)
13126         {
13127           changed = 1;
13128           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
13129         }
13130
13131       if (changed
13132           && REG_P (XEXP (x, 1))
13133           && REG_P (XEXP (x, 0)))
13134         return x;
13135
13136       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
13137         {
13138           changed = 1;
13139           x = legitimize_pic_address (x, 0);
13140         }
13141
13142       if (changed && ix86_legitimate_address_p (mode, x, false))
13143         return x;
13144
13145       if (REG_P (XEXP (x, 0)))
13146         {
13147           rtx temp = gen_reg_rtx (Pmode);
13148           rtx val  = force_operand (XEXP (x, 1), temp);
13149           if (val != temp)
13150             {
13151               if (GET_MODE (val) != Pmode)
13152                 val = convert_to_mode (Pmode, val, 1);
13153               emit_move_insn (temp, val);
13154             }
13155
13156           XEXP (x, 1) = temp;
13157           return x;
13158         }
13159
13160       else if (REG_P (XEXP (x, 1)))
13161         {
13162           rtx temp = gen_reg_rtx (Pmode);
13163           rtx val  = force_operand (XEXP (x, 0), temp);
13164           if (val != temp)
13165             {
13166               if (GET_MODE (val) != Pmode)
13167                 val = convert_to_mode (Pmode, val, 1);
13168               emit_move_insn (temp, val);
13169             }
13170
13171           XEXP (x, 0) = temp;
13172           return x;
13173         }
13174     }
13175
13176   return x;
13177 }
13178 \f
13179 /* Print an integer constant expression in assembler syntax.  Addition
13180    and subtraction are the only arithmetic that may appear in these
13181    expressions.  FILE is the stdio stream to write to, X is the rtx, and
13182    CODE is the operand print code from the output string.  */
13183
13184 static void
13185 output_pic_addr_const (FILE *file, rtx x, int code)
13186 {
13187   char buf[256];
13188
13189   switch (GET_CODE (x))
13190     {
13191     case PC:
13192       gcc_assert (flag_pic);
13193       putc ('.', file);
13194       break;
13195
13196     case SYMBOL_REF:
13197       if (TARGET_64BIT || ! TARGET_MACHO_BRANCH_ISLANDS)
13198         output_addr_const (file, x);
13199       else
13200         {
13201           const char *name = XSTR (x, 0);
13202
13203           /* Mark the decl as referenced so that cgraph will
13204              output the function.  */
13205           if (SYMBOL_REF_DECL (x))
13206             mark_decl_referenced (SYMBOL_REF_DECL (x));
13207
13208 #if TARGET_MACHO
13209           if (MACHOPIC_INDIRECT
13210               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
13211             name = machopic_indirection_name (x, /*stub_p=*/true);
13212 #endif
13213           assemble_name (file, name);
13214         }
13215       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
13216           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
13217         fputs ("@PLT", file);
13218       break;
13219
13220     case LABEL_REF:
13221       x = XEXP (x, 0);
13222       /* FALLTHRU */
13223     case CODE_LABEL:
13224       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
13225       assemble_name (asm_out_file, buf);
13226       break;
13227
13228     case CONST_INT:
13229       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13230       break;
13231
13232     case CONST:
13233       /* This used to output parentheses around the expression,
13234          but that does not work on the 386 (either ATT or BSD assembler).  */
13235       output_pic_addr_const (file, XEXP (x, 0), code);
13236       break;
13237
13238     case CONST_DOUBLE:
13239       if (GET_MODE (x) == VOIDmode)
13240         {
13241           /* We can use %d if the number is <32 bits and positive.  */
13242           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
13243             fprintf (file, "0x%lx%08lx",
13244                      (unsigned long) CONST_DOUBLE_HIGH (x),
13245                      (unsigned long) CONST_DOUBLE_LOW (x));
13246           else
13247             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
13248         }
13249       else
13250         /* We can't handle floating point constants;
13251            TARGET_PRINT_OPERAND must handle them.  */
13252         output_operand_lossage ("floating constant misused");
13253       break;
13254
13255     case PLUS:
13256       /* Some assemblers need integer constants to appear first.  */
13257       if (CONST_INT_P (XEXP (x, 0)))
13258         {
13259           output_pic_addr_const (file, XEXP (x, 0), code);
13260           putc ('+', file);
13261           output_pic_addr_const (file, XEXP (x, 1), code);
13262         }
13263       else
13264         {
13265           gcc_assert (CONST_INT_P (XEXP (x, 1)));
13266           output_pic_addr_const (file, XEXP (x, 1), code);
13267           putc ('+', file);
13268           output_pic_addr_const (file, XEXP (x, 0), code);
13269         }
13270       break;
13271
13272     case MINUS:
13273       if (!TARGET_MACHO)
13274         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
13275       output_pic_addr_const (file, XEXP (x, 0), code);
13276       putc ('-', file);
13277       output_pic_addr_const (file, XEXP (x, 1), code);
13278       if (!TARGET_MACHO)
13279         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
13280       break;
13281
13282      case UNSPEC:
13283        if (XINT (x, 1) == UNSPEC_STACK_CHECK)
13284          {
13285            bool f = i386_asm_output_addr_const_extra (file, x);
13286            gcc_assert (f);
13287            break;
13288          }
13289
13290        gcc_assert (XVECLEN (x, 0) == 1);
13291        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
13292        switch (XINT (x, 1))
13293         {
13294         case UNSPEC_GOT:
13295           fputs ("@GOT", file);
13296           break;
13297         case UNSPEC_GOTOFF:
13298           fputs ("@GOTOFF", file);
13299           break;
13300         case UNSPEC_PLTOFF:
13301           fputs ("@PLTOFF", file);
13302           break;
13303         case UNSPEC_PCREL:
13304           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13305                  "(%rip)" : "[rip]", file);
13306           break;
13307         case UNSPEC_GOTPCREL:
13308           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13309                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
13310           break;
13311         case UNSPEC_GOTTPOFF:
13312           /* FIXME: This might be @TPOFF in Sun ld too.  */
13313           fputs ("@gottpoff", file);
13314           break;
13315         case UNSPEC_TPOFF:
13316           fputs ("@tpoff", file);
13317           break;
13318         case UNSPEC_NTPOFF:
13319           if (TARGET_64BIT)
13320             fputs ("@tpoff", file);
13321           else
13322             fputs ("@ntpoff", file);
13323           break;
13324         case UNSPEC_DTPOFF:
13325           fputs ("@dtpoff", file);
13326           break;
13327         case UNSPEC_GOTNTPOFF:
13328           if (TARGET_64BIT)
13329             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13330                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
13331           else
13332             fputs ("@gotntpoff", file);
13333           break;
13334         case UNSPEC_INDNTPOFF:
13335           fputs ("@indntpoff", file);
13336           break;
13337 #if TARGET_MACHO
13338         case UNSPEC_MACHOPIC_OFFSET:
13339           putc ('-', file);
13340           machopic_output_function_base_name (file);
13341           break;
13342 #endif
13343         default:
13344           output_operand_lossage ("invalid UNSPEC as operand");
13345           break;
13346         }
13347        break;
13348
13349     default:
13350       output_operand_lossage ("invalid expression as operand");
13351     }
13352 }
13353
13354 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
13355    We need to emit DTP-relative relocations.  */
13356
13357 static void ATTRIBUTE_UNUSED
13358 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
13359 {
13360   fputs (ASM_LONG, file);
13361   output_addr_const (file, x);
13362   fputs ("@dtpoff", file);
13363   switch (size)
13364     {
13365     case 4:
13366       break;
13367     case 8:
13368       fputs (", 0", file);
13369       break;
13370     default:
13371       gcc_unreachable ();
13372    }
13373 }
13374
13375 /* Return true if X is a representation of the PIC register.  This copes
13376    with calls from ix86_find_base_term, where the register might have
13377    been replaced by a cselib value.  */
13378
13379 static bool
13380 ix86_pic_register_p (rtx x)
13381 {
13382   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
13383     return (pic_offset_table_rtx
13384             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
13385   else
13386     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
13387 }
13388
13389 /* Helper function for ix86_delegitimize_address.
13390    Attempt to delegitimize TLS local-exec accesses.  */
13391
13392 static rtx
13393 ix86_delegitimize_tls_address (rtx orig_x)
13394 {
13395   rtx x = orig_x, unspec;
13396   struct ix86_address addr;
13397
13398   if (!TARGET_TLS_DIRECT_SEG_REFS)
13399     return orig_x;
13400   if (MEM_P (x))
13401     x = XEXP (x, 0);
13402   if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
13403     return orig_x;
13404   if (ix86_decompose_address (x, &addr) == 0
13405       || addr.seg != (TARGET_64BIT ? SEG_FS : SEG_GS)
13406       || addr.disp == NULL_RTX
13407       || GET_CODE (addr.disp) != CONST)
13408     return orig_x;
13409   unspec = XEXP (addr.disp, 0);
13410   if (GET_CODE (unspec) == PLUS && CONST_INT_P (XEXP (unspec, 1)))
13411     unspec = XEXP (unspec, 0);
13412   if (GET_CODE (unspec) != UNSPEC || XINT (unspec, 1) != UNSPEC_NTPOFF)
13413     return orig_x;
13414   x = XVECEXP (unspec, 0, 0);
13415   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13416   if (unspec != XEXP (addr.disp, 0))
13417     x = gen_rtx_PLUS (Pmode, x, XEXP (XEXP (addr.disp, 0), 1));
13418   if (addr.index)
13419     {
13420       rtx idx = addr.index;
13421       if (addr.scale != 1)
13422         idx = gen_rtx_MULT (Pmode, idx, GEN_INT (addr.scale));
13423       x = gen_rtx_PLUS (Pmode, idx, x);
13424     }
13425   if (addr.base)
13426     x = gen_rtx_PLUS (Pmode, addr.base, x);
13427   if (MEM_P (orig_x))
13428     x = replace_equiv_address_nv (orig_x, x);
13429   return x;
13430 }
13431
13432 /* In the name of slightly smaller debug output, and to cater to
13433    general assembler lossage, recognize PIC+GOTOFF and turn it back
13434    into a direct symbol reference.
13435
13436    On Darwin, this is necessary to avoid a crash, because Darwin
13437    has a different PIC label for each routine but the DWARF debugging
13438    information is not associated with any particular routine, so it's
13439    necessary to remove references to the PIC label from RTL stored by
13440    the DWARF output code.  */
13441
13442 static rtx
13443 ix86_delegitimize_address (rtx x)
13444 {
13445   rtx orig_x = delegitimize_mem_from_attrs (x);
13446   /* addend is NULL or some rtx if x is something+GOTOFF where
13447      something doesn't include the PIC register.  */
13448   rtx addend = NULL_RTX;
13449   /* reg_addend is NULL or a multiple of some register.  */
13450   rtx reg_addend = NULL_RTX;
13451   /* const_addend is NULL or a const_int.  */
13452   rtx const_addend = NULL_RTX;
13453   /* This is the result, or NULL.  */
13454   rtx result = NULL_RTX;
13455
13456   x = orig_x;
13457
13458   if (MEM_P (x))
13459     x = XEXP (x, 0);
13460
13461   if (TARGET_64BIT)
13462     {
13463       if (GET_CODE (x) == CONST
13464           && GET_CODE (XEXP (x, 0)) == PLUS
13465           && GET_MODE (XEXP (x, 0)) == Pmode
13466           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
13467           && GET_CODE (XEXP (XEXP (x, 0), 0)) == UNSPEC
13468           && XINT (XEXP (XEXP (x, 0), 0), 1) == UNSPEC_PCREL)
13469         {
13470           rtx x2 = XVECEXP (XEXP (XEXP (x, 0), 0), 0, 0);
13471           x = gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 1), x2);
13472           if (MEM_P (orig_x))
13473             x = replace_equiv_address_nv (orig_x, x);
13474           return x;
13475         }
13476       if (GET_CODE (x) != CONST
13477           || GET_CODE (XEXP (x, 0)) != UNSPEC
13478           || (XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
13479               && XINT (XEXP (x, 0), 1) != UNSPEC_PCREL)
13480           || (!MEM_P (orig_x) && XINT (XEXP (x, 0), 1) != UNSPEC_PCREL))
13481         return ix86_delegitimize_tls_address (orig_x);
13482       x = XVECEXP (XEXP (x, 0), 0, 0);
13483       if (GET_MODE (orig_x) != GET_MODE (x) && MEM_P (orig_x))
13484         {
13485           x = simplify_gen_subreg (GET_MODE (orig_x), x,
13486                                    GET_MODE (x), 0);
13487           if (x == NULL_RTX)
13488             return orig_x;
13489         }
13490       return x;
13491     }
13492
13493   if (GET_CODE (x) != PLUS
13494       || GET_CODE (XEXP (x, 1)) != CONST)
13495     return ix86_delegitimize_tls_address (orig_x);
13496
13497   if (ix86_pic_register_p (XEXP (x, 0)))
13498     /* %ebx + GOT/GOTOFF */
13499     ;
13500   else if (GET_CODE (XEXP (x, 0)) == PLUS)
13501     {
13502       /* %ebx + %reg * scale + GOT/GOTOFF */
13503       reg_addend = XEXP (x, 0);
13504       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
13505         reg_addend = XEXP (reg_addend, 1);
13506       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
13507         reg_addend = XEXP (reg_addend, 0);
13508       else
13509         {
13510           reg_addend = NULL_RTX;
13511           addend = XEXP (x, 0);
13512         }
13513     }
13514   else
13515     addend = XEXP (x, 0);
13516
13517   x = XEXP (XEXP (x, 1), 0);
13518   if (GET_CODE (x) == PLUS
13519       && CONST_INT_P (XEXP (x, 1)))
13520     {
13521       const_addend = XEXP (x, 1);
13522       x = XEXP (x, 0);
13523     }
13524
13525   if (GET_CODE (x) == UNSPEC
13526       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
13527           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
13528     result = XVECEXP (x, 0, 0);
13529
13530   if (TARGET_MACHO && darwin_local_data_pic (x)
13531       && !MEM_P (orig_x))
13532     result = XVECEXP (x, 0, 0);
13533
13534   if (! result)
13535     return ix86_delegitimize_tls_address (orig_x);
13536
13537   if (const_addend)
13538     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
13539   if (reg_addend)
13540     result = gen_rtx_PLUS (Pmode, reg_addend, result);
13541   if (addend)
13542     {
13543       /* If the rest of original X doesn't involve the PIC register, add
13544          addend and subtract pic_offset_table_rtx.  This can happen e.g.
13545          for code like:
13546          leal (%ebx, %ecx, 4), %ecx
13547          ...
13548          movl foo@GOTOFF(%ecx), %edx
13549          in which case we return (%ecx - %ebx) + foo.  */
13550       if (pic_offset_table_rtx)
13551         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
13552                                                      pic_offset_table_rtx),
13553                                result);
13554       else
13555         return orig_x;
13556     }
13557   if (GET_MODE (orig_x) != Pmode && MEM_P (orig_x))
13558     {
13559       result = simplify_gen_subreg (GET_MODE (orig_x), result, Pmode, 0);
13560       if (result == NULL_RTX)
13561         return orig_x;
13562     }
13563   return result;
13564 }
13565
13566 /* If X is a machine specific address (i.e. a symbol or label being
13567    referenced as a displacement from the GOT implemented using an
13568    UNSPEC), then return the base term.  Otherwise return X.  */
13569
13570 rtx
13571 ix86_find_base_term (rtx x)
13572 {
13573   rtx term;
13574
13575   if (TARGET_64BIT)
13576     {
13577       if (GET_CODE (x) != CONST)
13578         return x;
13579       term = XEXP (x, 0);
13580       if (GET_CODE (term) == PLUS
13581           && (CONST_INT_P (XEXP (term, 1))
13582               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
13583         term = XEXP (term, 0);
13584       if (GET_CODE (term) != UNSPEC
13585           || (XINT (term, 1) != UNSPEC_GOTPCREL
13586               && XINT (term, 1) != UNSPEC_PCREL))
13587         return x;
13588
13589       return XVECEXP (term, 0, 0);
13590     }
13591
13592   return ix86_delegitimize_address (x);
13593 }
13594 \f
13595 static void
13596 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
13597                     int fp, FILE *file)
13598 {
13599   const char *suffix;
13600
13601   if (mode == CCFPmode || mode == CCFPUmode)
13602     {
13603       code = ix86_fp_compare_code_to_integer (code);
13604       mode = CCmode;
13605     }
13606   if (reverse)
13607     code = reverse_condition (code);
13608
13609   switch (code)
13610     {
13611     case EQ:
13612       switch (mode)
13613         {
13614         case CCAmode:
13615           suffix = "a";
13616           break;
13617
13618         case CCCmode:
13619           suffix = "c";
13620           break;
13621
13622         case CCOmode:
13623           suffix = "o";
13624           break;
13625
13626         case CCSmode:
13627           suffix = "s";
13628           break;
13629
13630         default:
13631           suffix = "e";
13632         }
13633       break;
13634     case NE:
13635       switch (mode)
13636         {
13637         case CCAmode:
13638           suffix = "na";
13639           break;
13640
13641         case CCCmode:
13642           suffix = "nc";
13643           break;
13644
13645         case CCOmode:
13646           suffix = "no";
13647           break;
13648
13649         case CCSmode:
13650           suffix = "ns";
13651           break;
13652
13653         default:
13654           suffix = "ne";
13655         }
13656       break;
13657     case GT:
13658       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
13659       suffix = "g";
13660       break;
13661     case GTU:
13662       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
13663          Those same assemblers have the same but opposite lossage on cmov.  */
13664       if (mode == CCmode)
13665         suffix = fp ? "nbe" : "a";
13666       else
13667         gcc_unreachable ();
13668       break;
13669     case LT:
13670       switch (mode)
13671         {
13672         case CCNOmode:
13673         case CCGOCmode:
13674           suffix = "s";
13675           break;
13676
13677         case CCmode:
13678         case CCGCmode:
13679           suffix = "l";
13680           break;
13681
13682         default:
13683           gcc_unreachable ();
13684         }
13685       break;
13686     case LTU:
13687       if (mode == CCmode)
13688         suffix = "b";
13689       else if (mode == CCCmode)
13690         suffix = "c";
13691       else
13692         gcc_unreachable ();
13693       break;
13694     case GE:
13695       switch (mode)
13696         {
13697         case CCNOmode:
13698         case CCGOCmode:
13699           suffix = "ns";
13700           break;
13701
13702         case CCmode:
13703         case CCGCmode:
13704           suffix = "ge";
13705           break;
13706
13707         default:
13708           gcc_unreachable ();
13709         }
13710       break;
13711     case GEU:
13712       if (mode == CCmode)
13713         suffix = fp ? "nb" : "ae";
13714       else if (mode == CCCmode)
13715         suffix = "nc";
13716       else
13717         gcc_unreachable ();
13718       break;
13719     case LE:
13720       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
13721       suffix = "le";
13722       break;
13723     case LEU:
13724       if (mode == CCmode)
13725         suffix = "be";
13726       else
13727         gcc_unreachable ();
13728       break;
13729     case UNORDERED:
13730       suffix = fp ? "u" : "p";
13731       break;
13732     case ORDERED:
13733       suffix = fp ? "nu" : "np";
13734       break;
13735     default:
13736       gcc_unreachable ();
13737     }
13738   fputs (suffix, file);
13739 }
13740
13741 /* Print the name of register X to FILE based on its machine mode and number.
13742    If CODE is 'w', pretend the mode is HImode.
13743    If CODE is 'b', pretend the mode is QImode.
13744    If CODE is 'k', pretend the mode is SImode.
13745    If CODE is 'q', pretend the mode is DImode.
13746    If CODE is 'x', pretend the mode is V4SFmode.
13747    If CODE is 't', pretend the mode is V8SFmode.
13748    If CODE is 'h', pretend the reg is the 'high' byte register.
13749    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
13750    If CODE is 'd', duplicate the operand for AVX instruction.
13751  */
13752
13753 void
13754 print_reg (rtx x, int code, FILE *file)
13755 {
13756   const char *reg;
13757   unsigned int regno;
13758   bool duplicated = code == 'd' && TARGET_AVX;
13759
13760   if (ASSEMBLER_DIALECT == ASM_ATT)
13761     putc ('%', file);
13762
13763   if (x == pc_rtx)
13764     {
13765       gcc_assert (TARGET_64BIT);
13766       fputs ("rip", file);
13767       return;
13768     }
13769
13770   regno = true_regnum (x);
13771   gcc_assert (regno != ARG_POINTER_REGNUM
13772               && regno != FRAME_POINTER_REGNUM
13773               && regno != FLAGS_REG
13774               && regno != FPSR_REG
13775               && regno != FPCR_REG);
13776
13777   if (code == 'w' || MMX_REG_P (x))
13778     code = 2;
13779   else if (code == 'b')
13780     code = 1;
13781   else if (code == 'k')
13782     code = 4;
13783   else if (code == 'q')
13784     code = 8;
13785   else if (code == 'y')
13786     code = 3;
13787   else if (code == 'h')
13788     code = 0;
13789   else if (code == 'x')
13790     code = 16;
13791   else if (code == 't')
13792     code = 32;
13793   else
13794     code = GET_MODE_SIZE (GET_MODE (x));
13795
13796   /* Irritatingly, AMD extended registers use different naming convention
13797      from the normal registers: "r%d[bwd]"  */
13798   if (REX_INT_REGNO_P (regno))
13799     {
13800       gcc_assert (TARGET_64BIT);
13801       putc ('r', file);
13802       fprint_ul (file, regno - FIRST_REX_INT_REG + 8);
13803       switch (code)
13804         {
13805           case 0:
13806             error ("extended registers have no high halves");
13807             break;
13808           case 1:
13809             putc ('b', file);
13810             break;
13811           case 2:
13812             putc ('w', file);
13813             break;
13814           case 4:
13815             putc ('d', file);
13816             break;
13817           case 8:
13818             /* no suffix */
13819             break;
13820           default:
13821             error ("unsupported operand size for extended register");
13822             break;
13823         }
13824       return;
13825     }
13826
13827   reg = NULL;
13828   switch (code)
13829     {
13830     case 3:
13831       if (STACK_TOP_P (x))
13832         {
13833           reg = "st(0)";
13834           break;
13835         }
13836       /* FALLTHRU */
13837     case 8:
13838     case 4:
13839     case 12:
13840       if (! ANY_FP_REG_P (x))
13841         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
13842       /* FALLTHRU */
13843     case 16:
13844     case 2:
13845     normal:
13846       reg = hi_reg_name[regno];
13847       break;
13848     case 1:
13849       if (regno >= ARRAY_SIZE (qi_reg_name))
13850         goto normal;
13851       reg = qi_reg_name[regno];
13852       break;
13853     case 0:
13854       if (regno >= ARRAY_SIZE (qi_high_reg_name))
13855         goto normal;
13856       reg = qi_high_reg_name[regno];
13857       break;
13858     case 32:
13859       if (SSE_REG_P (x))
13860         {
13861           gcc_assert (!duplicated);
13862           putc ('y', file);
13863           fputs (hi_reg_name[regno] + 1, file);
13864           return;
13865         }
13866       break;
13867     default:
13868       gcc_unreachable ();
13869     }
13870
13871   fputs (reg, file);
13872   if (duplicated)
13873     {
13874       if (ASSEMBLER_DIALECT == ASM_ATT)
13875         fprintf (file, ", %%%s", reg);
13876       else
13877         fprintf (file, ", %s", reg);
13878     }
13879 }
13880
13881 /* Locate some local-dynamic symbol still in use by this function
13882    so that we can print its name in some tls_local_dynamic_base
13883    pattern.  */
13884
13885 static int
13886 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
13887 {
13888   rtx x = *px;
13889
13890   if (GET_CODE (x) == SYMBOL_REF
13891       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
13892     {
13893       cfun->machine->some_ld_name = XSTR (x, 0);
13894       return 1;
13895     }
13896
13897   return 0;
13898 }
13899
13900 static const char *
13901 get_some_local_dynamic_name (void)
13902 {
13903   rtx insn;
13904
13905   if (cfun->machine->some_ld_name)
13906     return cfun->machine->some_ld_name;
13907
13908   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
13909     if (NONDEBUG_INSN_P (insn)
13910         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
13911       return cfun->machine->some_ld_name;
13912
13913   return NULL;
13914 }
13915
13916 /* Meaning of CODE:
13917    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
13918    C -- print opcode suffix for set/cmov insn.
13919    c -- like C, but print reversed condition
13920    F,f -- likewise, but for floating-point.
13921    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
13922         otherwise nothing
13923    R -- print the prefix for register names.
13924    z -- print the opcode suffix for the size of the current operand.
13925    Z -- likewise, with special suffixes for x87 instructions.
13926    * -- print a star (in certain assembler syntax)
13927    A -- print an absolute memory reference.
13928    E -- print address with DImode register names if TARGET_64BIT.
13929    w -- print the operand as if it's a "word" (HImode) even if it isn't.
13930    s -- print a shift double count, followed by the assemblers argument
13931         delimiter.
13932    b -- print the QImode name of the register for the indicated operand.
13933         %b0 would print %al if operands[0] is reg 0.
13934    w --  likewise, print the HImode name of the register.
13935    k --  likewise, print the SImode name of the register.
13936    q --  likewise, print the DImode name of the register.
13937    x --  likewise, print the V4SFmode name of the register.
13938    t --  likewise, print the V8SFmode name of the register.
13939    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
13940    y -- print "st(0)" instead of "st" as a register.
13941    d -- print duplicated register operand for AVX instruction.
13942    D -- print condition for SSE cmp instruction.
13943    P -- if PIC, print an @PLT suffix.
13944    p -- print raw symbol name.
13945    X -- don't print any sort of PIC '@' suffix for a symbol.
13946    & -- print some in-use local-dynamic symbol name.
13947    H -- print a memory address offset by 8; used for sse high-parts
13948    Y -- print condition for XOP pcom* instruction.
13949    + -- print a branch hint as 'cs' or 'ds' prefix
13950    ; -- print a semicolon (after prefixes due to bug in older gas).
13951    ~ -- print "i" if TARGET_AVX2, "f" otherwise.
13952    @ -- print a segment register of thread base pointer load
13953  */
13954
13955 void
13956 ix86_print_operand (FILE *file, rtx x, int code)
13957 {
13958   if (code)
13959     {
13960       switch (code)
13961         {
13962         case '*':
13963           if (ASSEMBLER_DIALECT == ASM_ATT)
13964             putc ('*', file);
13965           return;
13966
13967         case '&':
13968           {
13969             const char *name = get_some_local_dynamic_name ();
13970             if (name == NULL)
13971               output_operand_lossage ("'%%&' used without any "
13972                                       "local dynamic TLS references");
13973             else
13974               assemble_name (file, name);
13975             return;
13976           }
13977
13978         case 'A':
13979           switch (ASSEMBLER_DIALECT)
13980             {
13981             case ASM_ATT:
13982               putc ('*', file);
13983               break;
13984
13985             case ASM_INTEL:
13986               /* Intel syntax. For absolute addresses, registers should not
13987                  be surrounded by braces.  */
13988               if (!REG_P (x))
13989                 {
13990                   putc ('[', file);
13991                   ix86_print_operand (file, x, 0);
13992                   putc (']', file);
13993                   return;
13994                 }
13995               break;
13996
13997             default:
13998               gcc_unreachable ();
13999             }
14000
14001           ix86_print_operand (file, x, 0);
14002           return;
14003
14004         case 'E':
14005           /* Wrap address in an UNSPEC to declare special handling.  */
14006           if (TARGET_64BIT)
14007             x = gen_rtx_UNSPEC (DImode, gen_rtvec (1, x), UNSPEC_LEA_ADDR);
14008
14009           output_address (x);
14010           return;
14011             
14012         case 'L':
14013           if (ASSEMBLER_DIALECT == ASM_ATT)
14014             putc ('l', file);
14015           return;
14016
14017         case 'W':
14018           if (ASSEMBLER_DIALECT == ASM_ATT)
14019             putc ('w', file);
14020           return;
14021
14022         case 'B':
14023           if (ASSEMBLER_DIALECT == ASM_ATT)
14024             putc ('b', file);
14025           return;
14026
14027         case 'Q':
14028           if (ASSEMBLER_DIALECT == ASM_ATT)
14029             putc ('l', file);
14030           return;
14031
14032         case 'S':
14033           if (ASSEMBLER_DIALECT == ASM_ATT)
14034             putc ('s', file);
14035           return;
14036
14037         case 'T':
14038           if (ASSEMBLER_DIALECT == ASM_ATT)
14039             putc ('t', file);
14040           return;
14041
14042         case 'z':
14043           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
14044             {
14045               /* Opcodes don't get size suffixes if using Intel opcodes.  */
14046               if (ASSEMBLER_DIALECT == ASM_INTEL)
14047                 return;
14048
14049               switch (GET_MODE_SIZE (GET_MODE (x)))
14050                 {
14051                 case 1:
14052                   putc ('b', file);
14053                   return;
14054
14055                 case 2:
14056                   putc ('w', file);
14057                   return;
14058
14059                 case 4:
14060                   putc ('l', file);
14061                   return;
14062
14063                 case 8:
14064                   putc ('q', file);
14065                   return;
14066
14067                 default:
14068                   output_operand_lossage
14069                     ("invalid operand size for operand code '%c'", code);
14070                   return;
14071                 }
14072             }
14073
14074           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
14075             warning
14076               (0, "non-integer operand used with operand code '%c'", code);
14077           /* FALLTHRU */
14078
14079         case 'Z':
14080           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
14081           if (ASSEMBLER_DIALECT == ASM_INTEL)
14082             return;
14083
14084           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
14085             {
14086               switch (GET_MODE_SIZE (GET_MODE (x)))
14087                 {
14088                 case 2:
14089 #ifdef HAVE_AS_IX86_FILDS
14090                   putc ('s', file);
14091 #endif
14092                   return;
14093
14094                 case 4:
14095                   putc ('l', file);
14096                   return;
14097
14098                 case 8:
14099 #ifdef HAVE_AS_IX86_FILDQ
14100                   putc ('q', file);
14101 #else
14102                   fputs ("ll", file);
14103 #endif
14104                   return;
14105
14106                 default:
14107                   break;
14108                 }
14109             }
14110           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
14111             {
14112               /* 387 opcodes don't get size suffixes
14113                  if the operands are registers.  */
14114               if (STACK_REG_P (x))
14115                 return;
14116
14117               switch (GET_MODE_SIZE (GET_MODE (x)))
14118                 {
14119                 case 4:
14120                   putc ('s', file);
14121                   return;
14122
14123                 case 8:
14124                   putc ('l', file);
14125                   return;
14126
14127                 case 12:
14128                 case 16:
14129                   putc ('t', file);
14130                   return;
14131
14132                 default:
14133                   break;
14134                 }
14135             }
14136           else
14137             {
14138               output_operand_lossage
14139                 ("invalid operand type used with operand code '%c'", code);
14140               return;
14141             }
14142
14143           output_operand_lossage
14144             ("invalid operand size for operand code '%c'", code);
14145           return;
14146
14147         case 'd':
14148         case 'b':
14149         case 'w':
14150         case 'k':
14151         case 'q':
14152         case 'h':
14153         case 't':
14154         case 'y':
14155         case 'x':
14156         case 'X':
14157         case 'P':
14158         case 'p':
14159           break;
14160
14161         case 's':
14162           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
14163             {
14164               ix86_print_operand (file, x, 0);
14165               fputs (", ", file);
14166             }
14167           return;
14168
14169         case 'D':
14170           /* Little bit of braindamage here.  The SSE compare instructions
14171              does use completely different names for the comparisons that the
14172              fp conditional moves.  */
14173           if (TARGET_AVX)
14174             {
14175               switch (GET_CODE (x))
14176                 {
14177                 case EQ:
14178                   fputs ("eq", file);
14179                   break;
14180                 case UNEQ:
14181                   fputs ("eq_us", file);
14182                   break;
14183                 case LT:
14184                   fputs ("lt", file);
14185                   break;
14186                 case UNLT:
14187                   fputs ("nge", file);
14188                   break;
14189                 case LE:
14190                   fputs ("le", file);
14191                   break;
14192                 case UNLE:
14193                   fputs ("ngt", file);
14194                   break;
14195                 case UNORDERED:
14196                   fputs ("unord", file);
14197                   break;
14198                 case NE:
14199                   fputs ("neq", file);
14200                   break;
14201                 case LTGT:
14202                   fputs ("neq_oq", file);
14203                   break;
14204                 case GE:
14205                   fputs ("ge", file);
14206                   break;
14207                 case UNGE:
14208                   fputs ("nlt", file);
14209                   break;
14210                 case GT:
14211                   fputs ("gt", file);
14212                   break;
14213                 case UNGT:
14214                   fputs ("nle", file);
14215                   break;
14216                 case ORDERED:
14217                   fputs ("ord", file);
14218                   break;
14219                 default:
14220                   output_operand_lossage ("operand is not a condition code, "
14221                                           "invalid operand code 'D'");
14222                   return;
14223                 }
14224             }
14225           else
14226             {
14227               switch (GET_CODE (x))
14228                 {
14229                 case EQ:
14230                 case UNEQ:
14231                   fputs ("eq", file);
14232                   break;
14233                 case LT:
14234                 case UNLT:
14235                   fputs ("lt", file);
14236                   break;
14237                 case LE:
14238                 case UNLE:
14239                   fputs ("le", file);
14240                   break;
14241                 case UNORDERED:
14242                   fputs ("unord", file);
14243                   break;
14244                 case NE:
14245                 case LTGT:
14246                   fputs ("neq", file);
14247                   break;
14248                 case UNGE:
14249                 case GE:
14250                   fputs ("nlt", file);
14251                   break;
14252                 case UNGT:
14253                 case GT:
14254                   fputs ("nle", file);
14255                   break;
14256                 case ORDERED:
14257                   fputs ("ord", file);
14258                   break;
14259                 default:
14260                   output_operand_lossage ("operand is not a condition code, "
14261                                           "invalid operand code 'D'");
14262                   return;
14263                 }
14264             }
14265           return;
14266         case 'O':
14267 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14268           if (ASSEMBLER_DIALECT == ASM_ATT)
14269             {
14270               switch (GET_MODE (x))
14271                 {
14272                 case HImode: putc ('w', file); break;
14273                 case SImode:
14274                 case SFmode: putc ('l', file); break;
14275                 case DImode:
14276                 case DFmode: putc ('q', file); break;
14277                 default: gcc_unreachable ();
14278                 }
14279               putc ('.', file);
14280             }
14281 #endif
14282           return;
14283         case 'C':
14284           if (!COMPARISON_P (x))
14285             {
14286               output_operand_lossage ("operand is neither a constant nor a "
14287                                       "condition code, invalid operand code "
14288                                       "'C'");
14289               return;
14290             }
14291           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
14292           return;
14293         case 'F':
14294           if (!COMPARISON_P (x))
14295             {
14296               output_operand_lossage ("operand is neither a constant nor a "
14297                                       "condition code, invalid operand code "
14298                                       "'F'");
14299               return;
14300             }
14301 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14302           if (ASSEMBLER_DIALECT == ASM_ATT)
14303             putc ('.', file);
14304 #endif
14305           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
14306           return;
14307
14308           /* Like above, but reverse condition */
14309         case 'c':
14310           /* Check to see if argument to %c is really a constant
14311              and not a condition code which needs to be reversed.  */
14312           if (!COMPARISON_P (x))
14313             {
14314               output_operand_lossage ("operand is neither a constant nor a "
14315                                       "condition code, invalid operand "
14316                                       "code 'c'");
14317               return;
14318             }
14319           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
14320           return;
14321         case 'f':
14322           if (!COMPARISON_P (x))
14323             {
14324               output_operand_lossage ("operand is neither a constant nor a "
14325                                       "condition code, invalid operand "
14326                                       "code 'f'");
14327               return;
14328             }
14329 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14330           if (ASSEMBLER_DIALECT == ASM_ATT)
14331             putc ('.', file);
14332 #endif
14333           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
14334           return;
14335
14336         case 'H':
14337           if (!offsettable_memref_p (x))
14338             {
14339               output_operand_lossage ("operand is not an offsettable memory "
14340                                       "reference, invalid operand "
14341                                       "code 'H'");
14342               return;
14343             }
14344           /* It doesn't actually matter what mode we use here, as we're
14345              only going to use this for printing.  */
14346           x = adjust_address_nv (x, DImode, 8);
14347           break;
14348
14349         case '+':
14350           {
14351             rtx x;
14352
14353             if (!optimize
14354                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
14355               return;
14356
14357             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
14358             if (x)
14359               {
14360                 int pred_val = INTVAL (XEXP (x, 0));
14361
14362                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
14363                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
14364                   {
14365                     int taken = pred_val > REG_BR_PROB_BASE / 2;
14366                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
14367
14368                     /* Emit hints only in the case default branch prediction
14369                        heuristics would fail.  */
14370                     if (taken != cputaken)
14371                       {
14372                         /* We use 3e (DS) prefix for taken branches and
14373                            2e (CS) prefix for not taken branches.  */
14374                         if (taken)
14375                           fputs ("ds ; ", file);
14376                         else
14377                           fputs ("cs ; ", file);
14378                       }
14379                   }
14380               }
14381             return;
14382           }
14383
14384         case 'Y':
14385           switch (GET_CODE (x))
14386             {
14387             case NE:
14388               fputs ("neq", file);
14389               break;
14390             case EQ:
14391               fputs ("eq", file);
14392               break;
14393             case GE:
14394             case GEU:
14395               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
14396               break;
14397             case GT:
14398             case GTU:
14399               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
14400               break;
14401             case LE:
14402             case LEU:
14403               fputs ("le", file);
14404               break;
14405             case LT:
14406             case LTU:
14407               fputs ("lt", file);
14408               break;
14409             case UNORDERED:
14410               fputs ("unord", file);
14411               break;
14412             case ORDERED:
14413               fputs ("ord", file);
14414               break;
14415             case UNEQ:
14416               fputs ("ueq", file);
14417               break;
14418             case UNGE:
14419               fputs ("nlt", file);
14420               break;
14421             case UNGT:
14422               fputs ("nle", file);
14423               break;
14424             case UNLE:
14425               fputs ("ule", file);
14426               break;
14427             case UNLT:
14428               fputs ("ult", file);
14429               break;
14430             case LTGT:
14431               fputs ("une", file);
14432               break;
14433             default:
14434               output_operand_lossage ("operand is not a condition code, "
14435                                       "invalid operand code 'Y'");
14436               return;
14437             }
14438           return;
14439
14440         case ';':
14441 #ifndef HAVE_AS_IX86_REP_LOCK_PREFIX
14442           putc (';', file);
14443 #endif
14444           return;
14445
14446         case '@':
14447           if (ASSEMBLER_DIALECT == ASM_ATT)
14448             putc ('%', file);
14449
14450           /* The kernel uses a different segment register for performance
14451              reasons; a system call would not have to trash the userspace
14452              segment register, which would be expensive.  */
14453           if (TARGET_64BIT && ix86_cmodel != CM_KERNEL)
14454             fputs ("fs", file);
14455           else
14456             fputs ("gs", file);
14457           return;
14458
14459         case '~':
14460           putc (TARGET_AVX2 ? 'i' : 'f', file);
14461           return;
14462
14463         default:
14464             output_operand_lossage ("invalid operand code '%c'", code);
14465         }
14466     }
14467
14468   if (REG_P (x))
14469     print_reg (x, code, file);
14470
14471   else if (MEM_P (x))
14472     {
14473       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
14474       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
14475           && GET_MODE (x) != BLKmode)
14476         {
14477           const char * size;
14478           switch (GET_MODE_SIZE (GET_MODE (x)))
14479             {
14480             case 1: size = "BYTE"; break;
14481             case 2: size = "WORD"; break;
14482             case 4: size = "DWORD"; break;
14483             case 8: size = "QWORD"; break;
14484             case 12: size = "TBYTE"; break;
14485             case 16:
14486               if (GET_MODE (x) == XFmode)
14487                 size = "TBYTE";
14488               else
14489                 size = "XMMWORD";
14490               break;
14491             case 32: size = "YMMWORD"; break;
14492             default:
14493               gcc_unreachable ();
14494             }
14495
14496           /* Check for explicit size override (codes 'b', 'w', 'k',
14497              'q' and 'x')  */
14498           if (code == 'b')
14499             size = "BYTE";
14500           else if (code == 'w')
14501             size = "WORD";
14502           else if (code == 'k')
14503             size = "DWORD";
14504           else if (code == 'q')
14505             size = "QWORD";
14506           else if (code == 'x')
14507             size = "XMMWORD";
14508
14509           fputs (size, file);
14510           fputs (" PTR ", file);
14511         }
14512
14513       x = XEXP (x, 0);
14514       /* Avoid (%rip) for call operands.  */
14515       if (CONSTANT_ADDRESS_P (x) && code == 'P'
14516           && !CONST_INT_P (x))
14517         output_addr_const (file, x);
14518       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
14519         output_operand_lossage ("invalid constraints for operand");
14520       else
14521         output_address (x);
14522     }
14523
14524   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
14525     {
14526       REAL_VALUE_TYPE r;
14527       long l;
14528
14529       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14530       REAL_VALUE_TO_TARGET_SINGLE (r, l);
14531
14532       if (ASSEMBLER_DIALECT == ASM_ATT)
14533         putc ('$', file);
14534       /* Sign extend 32bit SFmode immediate to 8 bytes.  */
14535       if (code == 'q')
14536         fprintf (file, "0x%08" HOST_LONG_LONG_FORMAT "x",
14537                  (unsigned long long) (int) l);
14538       else
14539         fprintf (file, "0x%08x", (unsigned int) l);
14540     }
14541
14542   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
14543     {
14544       REAL_VALUE_TYPE r;
14545       long l[2];
14546
14547       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14548       REAL_VALUE_TO_TARGET_DOUBLE (r, l);
14549
14550       if (ASSEMBLER_DIALECT == ASM_ATT)
14551         putc ('$', file);
14552       fprintf (file, "0x%lx%08lx", l[1] & 0xffffffff, l[0] & 0xffffffff);
14553     }
14554
14555   /* These float cases don't actually occur as immediate operands.  */
14556   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == XFmode)
14557     {
14558       char dstr[30];
14559
14560       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
14561       fputs (dstr, file);
14562     }
14563
14564   else
14565     {
14566       /* We have patterns that allow zero sets of memory, for instance.
14567          In 64-bit mode, we should probably support all 8-byte vectors,
14568          since we can in fact encode that into an immediate.  */
14569       if (GET_CODE (x) == CONST_VECTOR)
14570         {
14571           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
14572           x = const0_rtx;
14573         }
14574
14575       if (code != 'P' && code != 'p')
14576         {
14577           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
14578             {
14579               if (ASSEMBLER_DIALECT == ASM_ATT)
14580                 putc ('$', file);
14581             }
14582           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
14583                    || GET_CODE (x) == LABEL_REF)
14584             {
14585               if (ASSEMBLER_DIALECT == ASM_ATT)
14586                 putc ('$', file);
14587               else
14588                 fputs ("OFFSET FLAT:", file);
14589             }
14590         }
14591       if (CONST_INT_P (x))
14592         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
14593       else if (flag_pic || MACHOPIC_INDIRECT)
14594         output_pic_addr_const (file, x, code);
14595       else
14596         output_addr_const (file, x);
14597     }
14598 }
14599
14600 static bool
14601 ix86_print_operand_punct_valid_p (unsigned char code)
14602 {
14603   return (code == '@' || code == '*' || code == '+'
14604           || code == '&' || code == ';' || code == '~');
14605 }
14606 \f
14607 /* Print a memory operand whose address is ADDR.  */
14608
14609 static void
14610 ix86_print_operand_address (FILE *file, rtx addr)
14611 {
14612   struct ix86_address parts;
14613   rtx base, index, disp;
14614   int scale;
14615   int ok;
14616   bool vsib = false;
14617   int code = 0;
14618
14619   if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_VSIBADDR)
14620     {
14621       ok = ix86_decompose_address (XVECEXP (addr, 0, 0), &parts);
14622       gcc_assert (parts.index == NULL_RTX);
14623       parts.index = XVECEXP (addr, 0, 1);
14624       parts.scale = INTVAL (XVECEXP (addr, 0, 2));
14625       addr = XVECEXP (addr, 0, 0);
14626       vsib = true;
14627     }
14628   else if (GET_CODE (addr) == UNSPEC && XINT (addr, 1) == UNSPEC_LEA_ADDR)
14629     {
14630       gcc_assert (TARGET_64BIT);
14631       ok = ix86_decompose_address (XVECEXP (addr, 0, 0), &parts);
14632       code = 'q';
14633     }
14634   else
14635     ok = ix86_decompose_address (addr, &parts);
14636
14637   gcc_assert (ok);
14638
14639   base = parts.base;
14640   index = parts.index;
14641   disp = parts.disp;
14642   scale = parts.scale;
14643
14644   switch (parts.seg)
14645     {
14646     case SEG_DEFAULT:
14647       break;
14648     case SEG_FS:
14649     case SEG_GS:
14650       if (ASSEMBLER_DIALECT == ASM_ATT)
14651         putc ('%', file);
14652       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
14653       break;
14654     default:
14655       gcc_unreachable ();
14656     }
14657
14658   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
14659   if (TARGET_64BIT && !base && !index)
14660     {
14661       rtx symbol = disp;
14662
14663       if (GET_CODE (disp) == CONST
14664           && GET_CODE (XEXP (disp, 0)) == PLUS
14665           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14666         symbol = XEXP (XEXP (disp, 0), 0);
14667
14668       if (GET_CODE (symbol) == LABEL_REF
14669           || (GET_CODE (symbol) == SYMBOL_REF
14670               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
14671         base = pc_rtx;
14672     }
14673   if (!base && !index)
14674     {
14675       /* Displacement only requires special attention.  */
14676
14677       if (CONST_INT_P (disp))
14678         {
14679           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
14680             fputs ("ds:", file);
14681           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
14682         }
14683       else if (flag_pic)
14684         output_pic_addr_const (file, disp, 0);
14685       else
14686         output_addr_const (file, disp);
14687     }
14688   else
14689     {
14690       /* Print SImode register names to force addr32 prefix.  */
14691       if (SImode_address_operand (addr, VOIDmode))
14692         {
14693 #ifdef ENABLE_CHECKING
14694           gcc_assert (TARGET_64BIT);
14695           switch (GET_CODE (addr))
14696             {
14697             case SUBREG:
14698               gcc_assert (GET_MODE (addr) == SImode);
14699               gcc_assert (GET_MODE (SUBREG_REG (addr)) == DImode);
14700               break;
14701             case ZERO_EXTEND:
14702             case AND:
14703               gcc_assert (GET_MODE (addr) == DImode);
14704               break;
14705             default:
14706               gcc_unreachable ();
14707             }
14708 #endif
14709           gcc_assert (!code);
14710           code = 'k';
14711         }
14712       else if (code == 0
14713                && TARGET_X32
14714                && disp
14715                && CONST_INT_P (disp)
14716                && INTVAL (disp) < -16*1024*1024)
14717         {
14718           /* X32 runs in 64-bit mode, where displacement, DISP, in
14719              address DISP(%r64), is encoded as 32-bit immediate sign-
14720              extended from 32-bit to 64-bit.  For -0x40000300(%r64),
14721              address is %r64 + 0xffffffffbffffd00.  When %r64 <
14722              0x40000300, like 0x37ffe064, address is 0xfffffffff7ffdd64,
14723              which is invalid for x32.  The correct address is %r64
14724              - 0x40000300 == 0xf7ffdd64.  To properly encode
14725              -0x40000300(%r64) for x32, we zero-extend negative
14726              displacement by forcing addr32 prefix which truncates
14727              0xfffffffff7ffdd64 to 0xf7ffdd64.  In theory, we should
14728              zero-extend all negative displacements, including -1(%rsp).
14729              However, for small negative displacements, sign-extension
14730              won't cause overflow.  We only zero-extend negative
14731              displacements if they < -16*1024*1024, which is also used
14732              to check legitimate address displacements for PIC.  */
14733           code = 'k';
14734         }
14735
14736       if (ASSEMBLER_DIALECT == ASM_ATT)
14737         {
14738           if (disp)
14739             {
14740               if (flag_pic)
14741                 output_pic_addr_const (file, disp, 0);
14742               else if (GET_CODE (disp) == LABEL_REF)
14743                 output_asm_label (disp);
14744               else
14745                 output_addr_const (file, disp);
14746             }
14747
14748           putc ('(', file);
14749           if (base)
14750             print_reg (base, code, file);
14751           if (index)
14752             {
14753               putc (',', file);
14754               print_reg (index, vsib ? 0 : code, file);
14755               if (scale != 1 || vsib)
14756                 fprintf (file, ",%d", scale);
14757             }
14758           putc (')', file);
14759         }
14760       else
14761         {
14762           rtx offset = NULL_RTX;
14763
14764           if (disp)
14765             {
14766               /* Pull out the offset of a symbol; print any symbol itself.  */
14767               if (GET_CODE (disp) == CONST
14768                   && GET_CODE (XEXP (disp, 0)) == PLUS
14769                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14770                 {
14771                   offset = XEXP (XEXP (disp, 0), 1);
14772                   disp = gen_rtx_CONST (VOIDmode,
14773                                         XEXP (XEXP (disp, 0), 0));
14774                 }
14775
14776               if (flag_pic)
14777                 output_pic_addr_const (file, disp, 0);
14778               else if (GET_CODE (disp) == LABEL_REF)
14779                 output_asm_label (disp);
14780               else if (CONST_INT_P (disp))
14781                 offset = disp;
14782               else
14783                 output_addr_const (file, disp);
14784             }
14785
14786           putc ('[', file);
14787           if (base)
14788             {
14789               print_reg (base, code, file);
14790               if (offset)
14791                 {
14792                   if (INTVAL (offset) >= 0)
14793                     putc ('+', file);
14794                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14795                 }
14796             }
14797           else if (offset)
14798             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14799           else
14800             putc ('0', file);
14801
14802           if (index)
14803             {
14804               putc ('+', file);
14805               print_reg (index, vsib ? 0 : code, file);
14806               if (scale != 1 || vsib)
14807                 fprintf (file, "*%d", scale);
14808             }
14809           putc (']', file);
14810         }
14811     }
14812 }
14813
14814 /* Implementation of TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
14815
14816 static bool
14817 i386_asm_output_addr_const_extra (FILE *file, rtx x)
14818 {
14819   rtx op;
14820
14821   if (GET_CODE (x) != UNSPEC)
14822     return false;
14823
14824   op = XVECEXP (x, 0, 0);
14825   switch (XINT (x, 1))
14826     {
14827     case UNSPEC_GOTTPOFF:
14828       output_addr_const (file, op);
14829       /* FIXME: This might be @TPOFF in Sun ld.  */
14830       fputs ("@gottpoff", file);
14831       break;
14832     case UNSPEC_TPOFF:
14833       output_addr_const (file, op);
14834       fputs ("@tpoff", file);
14835       break;
14836     case UNSPEC_NTPOFF:
14837       output_addr_const (file, op);
14838       if (TARGET_64BIT)
14839         fputs ("@tpoff", file);
14840       else
14841         fputs ("@ntpoff", file);
14842       break;
14843     case UNSPEC_DTPOFF:
14844       output_addr_const (file, op);
14845       fputs ("@dtpoff", file);
14846       break;
14847     case UNSPEC_GOTNTPOFF:
14848       output_addr_const (file, op);
14849       if (TARGET_64BIT)
14850         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
14851                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
14852       else
14853         fputs ("@gotntpoff", file);
14854       break;
14855     case UNSPEC_INDNTPOFF:
14856       output_addr_const (file, op);
14857       fputs ("@indntpoff", file);
14858       break;
14859 #if TARGET_MACHO
14860     case UNSPEC_MACHOPIC_OFFSET:
14861       output_addr_const (file, op);
14862       putc ('-', file);
14863       machopic_output_function_base_name (file);
14864       break;
14865 #endif
14866
14867     case UNSPEC_STACK_CHECK:
14868       {
14869         int offset;
14870
14871         gcc_assert (flag_split_stack);
14872
14873 #ifdef TARGET_THREAD_SPLIT_STACK_OFFSET
14874         offset = TARGET_THREAD_SPLIT_STACK_OFFSET;
14875 #else
14876         gcc_unreachable ();
14877 #endif
14878
14879         fprintf (file, "%s:%d", TARGET_64BIT ? "%fs" : "%gs", offset);
14880       }
14881       break;
14882
14883     default:
14884       return false;
14885     }
14886
14887   return true;
14888 }
14889 \f
14890 /* Split one or more double-mode RTL references into pairs of half-mode
14891    references.  The RTL can be REG, offsettable MEM, integer constant, or
14892    CONST_DOUBLE.  "operands" is a pointer to an array of double-mode RTLs to
14893    split and "num" is its length.  lo_half and hi_half are output arrays
14894    that parallel "operands".  */
14895
14896 void
14897 split_double_mode (enum machine_mode mode, rtx operands[],
14898                    int num, rtx lo_half[], rtx hi_half[])
14899 {
14900   enum machine_mode half_mode;
14901   unsigned int byte;
14902
14903   switch (mode)
14904     {
14905     case TImode:
14906       half_mode = DImode;
14907       break;
14908     case DImode:
14909       half_mode = SImode;
14910       break;
14911     default:
14912       gcc_unreachable ();
14913     }
14914
14915   byte = GET_MODE_SIZE (half_mode);
14916
14917   while (num--)
14918     {
14919       rtx op = operands[num];
14920
14921       /* simplify_subreg refuse to split volatile memory addresses,
14922          but we still have to handle it.  */
14923       if (MEM_P (op))
14924         {
14925           lo_half[num] = adjust_address (op, half_mode, 0);
14926           hi_half[num] = adjust_address (op, half_mode, byte);
14927         }
14928       else
14929         {
14930           lo_half[num] = simplify_gen_subreg (half_mode, op,
14931                                               GET_MODE (op) == VOIDmode
14932                                               ? mode : GET_MODE (op), 0);
14933           hi_half[num] = simplify_gen_subreg (half_mode, op,
14934                                               GET_MODE (op) == VOIDmode
14935                                               ? mode : GET_MODE (op), byte);
14936         }
14937     }
14938 }
14939 \f
14940 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
14941    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
14942    is the expression of the binary operation.  The output may either be
14943    emitted here, or returned to the caller, like all output_* functions.
14944
14945    There is no guarantee that the operands are the same mode, as they
14946    might be within FLOAT or FLOAT_EXTEND expressions.  */
14947
14948 #ifndef SYSV386_COMPAT
14949 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
14950    wants to fix the assemblers because that causes incompatibility
14951    with gcc.  No-one wants to fix gcc because that causes
14952    incompatibility with assemblers...  You can use the option of
14953    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
14954 #define SYSV386_COMPAT 1
14955 #endif
14956
14957 const char *
14958 output_387_binary_op (rtx insn, rtx *operands)
14959 {
14960   static char buf[40];
14961   const char *p;
14962   const char *ssep;
14963   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
14964
14965 #ifdef ENABLE_CHECKING
14966   /* Even if we do not want to check the inputs, this documents input
14967      constraints.  Which helps in understanding the following code.  */
14968   if (STACK_REG_P (operands[0])
14969       && ((REG_P (operands[1])
14970            && REGNO (operands[0]) == REGNO (operands[1])
14971            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
14972           || (REG_P (operands[2])
14973               && REGNO (operands[0]) == REGNO (operands[2])
14974               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
14975       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
14976     ; /* ok */
14977   else
14978     gcc_assert (is_sse);
14979 #endif
14980
14981   switch (GET_CODE (operands[3]))
14982     {
14983     case PLUS:
14984       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14985           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14986         p = "fiadd";
14987       else
14988         p = "fadd";
14989       ssep = "vadd";
14990       break;
14991
14992     case MINUS:
14993       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14994           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14995         p = "fisub";
14996       else
14997         p = "fsub";
14998       ssep = "vsub";
14999       break;
15000
15001     case MULT:
15002       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
15003           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
15004         p = "fimul";
15005       else
15006         p = "fmul";
15007       ssep = "vmul";
15008       break;
15009
15010     case DIV:
15011       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
15012           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
15013         p = "fidiv";
15014       else
15015         p = "fdiv";
15016       ssep = "vdiv";
15017       break;
15018
15019     default:
15020       gcc_unreachable ();
15021     }
15022
15023   if (is_sse)
15024    {
15025      if (TARGET_AVX)
15026        {
15027          strcpy (buf, ssep);
15028          if (GET_MODE (operands[0]) == SFmode)
15029            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
15030          else
15031            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
15032        }
15033      else
15034        {
15035          strcpy (buf, ssep + 1);
15036          if (GET_MODE (operands[0]) == SFmode)
15037            strcat (buf, "ss\t{%2, %0|%0, %2}");
15038          else
15039            strcat (buf, "sd\t{%2, %0|%0, %2}");
15040        }
15041       return buf;
15042    }
15043   strcpy (buf, p);
15044
15045   switch (GET_CODE (operands[3]))
15046     {
15047     case MULT:
15048     case PLUS:
15049       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
15050         {
15051           rtx temp = operands[2];
15052           operands[2] = operands[1];
15053           operands[1] = temp;
15054         }
15055
15056       /* know operands[0] == operands[1].  */
15057
15058       if (MEM_P (operands[2]))
15059         {
15060           p = "%Z2\t%2";
15061           break;
15062         }
15063
15064       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
15065         {
15066           if (STACK_TOP_P (operands[0]))
15067             /* How is it that we are storing to a dead operand[2]?
15068                Well, presumably operands[1] is dead too.  We can't
15069                store the result to st(0) as st(0) gets popped on this
15070                instruction.  Instead store to operands[2] (which I
15071                think has to be st(1)).  st(1) will be popped later.
15072                gcc <= 2.8.1 didn't have this check and generated
15073                assembly code that the Unixware assembler rejected.  */
15074             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
15075           else
15076             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
15077           break;
15078         }
15079
15080       if (STACK_TOP_P (operands[0]))
15081         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
15082       else
15083         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
15084       break;
15085
15086     case MINUS:
15087     case DIV:
15088       if (MEM_P (operands[1]))
15089         {
15090           p = "r%Z1\t%1";
15091           break;
15092         }
15093
15094       if (MEM_P (operands[2]))
15095         {
15096           p = "%Z2\t%2";
15097           break;
15098         }
15099
15100       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
15101         {
15102 #if SYSV386_COMPAT
15103           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
15104              derived assemblers, confusingly reverse the direction of
15105              the operation for fsub{r} and fdiv{r} when the
15106              destination register is not st(0).  The Intel assembler
15107              doesn't have this brain damage.  Read !SYSV386_COMPAT to
15108              figure out what the hardware really does.  */
15109           if (STACK_TOP_P (operands[0]))
15110             p = "{p\t%0, %2|rp\t%2, %0}";
15111           else
15112             p = "{rp\t%2, %0|p\t%0, %2}";
15113 #else
15114           if (STACK_TOP_P (operands[0]))
15115             /* As above for fmul/fadd, we can't store to st(0).  */
15116             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
15117           else
15118             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
15119 #endif
15120           break;
15121         }
15122
15123       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
15124         {
15125 #if SYSV386_COMPAT
15126           if (STACK_TOP_P (operands[0]))
15127             p = "{rp\t%0, %1|p\t%1, %0}";
15128           else
15129             p = "{p\t%1, %0|rp\t%0, %1}";
15130 #else
15131           if (STACK_TOP_P (operands[0]))
15132             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
15133           else
15134             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
15135 #endif
15136           break;
15137         }
15138
15139       if (STACK_TOP_P (operands[0]))
15140         {
15141           if (STACK_TOP_P (operands[1]))
15142             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
15143           else
15144             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
15145           break;
15146         }
15147       else if (STACK_TOP_P (operands[1]))
15148         {
15149 #if SYSV386_COMPAT
15150           p = "{\t%1, %0|r\t%0, %1}";
15151 #else
15152           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
15153 #endif
15154         }
15155       else
15156         {
15157 #if SYSV386_COMPAT
15158           p = "{r\t%2, %0|\t%0, %2}";
15159 #else
15160           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
15161 #endif
15162         }
15163       break;
15164
15165     default:
15166       gcc_unreachable ();
15167     }
15168
15169   strcat (buf, p);
15170   return buf;
15171 }
15172
15173 /* Return needed mode for entity in optimize_mode_switching pass.  */
15174
15175 int
15176 ix86_mode_needed (int entity, rtx insn)
15177 {
15178   enum attr_i387_cw mode;
15179
15180   /* The mode UNINITIALIZED is used to store control word after a
15181      function call or ASM pattern.  The mode ANY specify that function
15182      has no requirements on the control word and make no changes in the
15183      bits we are interested in.  */
15184
15185   if (CALL_P (insn)
15186       || (NONJUMP_INSN_P (insn)
15187           && (asm_noperands (PATTERN (insn)) >= 0
15188               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
15189     return I387_CW_UNINITIALIZED;
15190
15191   if (recog_memoized (insn) < 0)
15192     return I387_CW_ANY;
15193
15194   mode = get_attr_i387_cw (insn);
15195
15196   switch (entity)
15197     {
15198     case I387_TRUNC:
15199       if (mode == I387_CW_TRUNC)
15200         return mode;
15201       break;
15202
15203     case I387_FLOOR:
15204       if (mode == I387_CW_FLOOR)
15205         return mode;
15206       break;
15207
15208     case I387_CEIL:
15209       if (mode == I387_CW_CEIL)
15210         return mode;
15211       break;
15212
15213     case I387_MASK_PM:
15214       if (mode == I387_CW_MASK_PM)
15215         return mode;
15216       break;
15217
15218     default:
15219       gcc_unreachable ();
15220     }
15221
15222   return I387_CW_ANY;
15223 }
15224
15225 /* Output code to initialize control word copies used by trunc?f?i and
15226    rounding patterns.  CURRENT_MODE is set to current control word,
15227    while NEW_MODE is set to new control word.  */
15228
15229 void
15230 emit_i387_cw_initialization (int mode)
15231 {
15232   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
15233   rtx new_mode;
15234
15235   enum ix86_stack_slot slot;
15236
15237   rtx reg = gen_reg_rtx (HImode);
15238
15239   emit_insn (gen_x86_fnstcw_1 (stored_mode));
15240   emit_move_insn (reg, copy_rtx (stored_mode));
15241
15242   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
15243       || optimize_function_for_size_p (cfun))
15244     {
15245       switch (mode)
15246         {
15247         case I387_CW_TRUNC:
15248           /* round toward zero (truncate) */
15249           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
15250           slot = SLOT_CW_TRUNC;
15251           break;
15252
15253         case I387_CW_FLOOR:
15254           /* round down toward -oo */
15255           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
15256           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
15257           slot = SLOT_CW_FLOOR;
15258           break;
15259
15260         case I387_CW_CEIL:
15261           /* round up toward +oo */
15262           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
15263           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
15264           slot = SLOT_CW_CEIL;
15265           break;
15266
15267         case I387_CW_MASK_PM:
15268           /* mask precision exception for nearbyint() */
15269           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
15270           slot = SLOT_CW_MASK_PM;
15271           break;
15272
15273         default:
15274           gcc_unreachable ();
15275         }
15276     }
15277   else
15278     {
15279       switch (mode)
15280         {
15281         case I387_CW_TRUNC:
15282           /* round toward zero (truncate) */
15283           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
15284           slot = SLOT_CW_TRUNC;
15285           break;
15286
15287         case I387_CW_FLOOR:
15288           /* round down toward -oo */
15289           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
15290           slot = SLOT_CW_FLOOR;
15291           break;
15292
15293         case I387_CW_CEIL:
15294           /* round up toward +oo */
15295           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
15296           slot = SLOT_CW_CEIL;
15297           break;
15298
15299         case I387_CW_MASK_PM:
15300           /* mask precision exception for nearbyint() */
15301           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
15302           slot = SLOT_CW_MASK_PM;
15303           break;
15304
15305         default:
15306           gcc_unreachable ();
15307         }
15308     }
15309
15310   gcc_assert (slot < MAX_386_STACK_LOCALS);
15311
15312   new_mode = assign_386_stack_local (HImode, slot);
15313   emit_move_insn (new_mode, reg);
15314 }
15315
15316 /* Output code for INSN to convert a float to a signed int.  OPERANDS
15317    are the insn operands.  The output may be [HSD]Imode and the input
15318    operand may be [SDX]Fmode.  */
15319
15320 const char *
15321 output_fix_trunc (rtx insn, rtx *operands, bool fisttp)
15322 {
15323   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
15324   int dimode_p = GET_MODE (operands[0]) == DImode;
15325   int round_mode = get_attr_i387_cw (insn);
15326
15327   /* Jump through a hoop or two for DImode, since the hardware has no
15328      non-popping instruction.  We used to do this a different way, but
15329      that was somewhat fragile and broke with post-reload splitters.  */
15330   if ((dimode_p || fisttp) && !stack_top_dies)
15331     output_asm_insn ("fld\t%y1", operands);
15332
15333   gcc_assert (STACK_TOP_P (operands[1]));
15334   gcc_assert (MEM_P (operands[0]));
15335   gcc_assert (GET_MODE (operands[1]) != TFmode);
15336
15337   if (fisttp)
15338       output_asm_insn ("fisttp%Z0\t%0", operands);
15339   else
15340     {
15341       if (round_mode != I387_CW_ANY)
15342         output_asm_insn ("fldcw\t%3", operands);
15343       if (stack_top_dies || dimode_p)
15344         output_asm_insn ("fistp%Z0\t%0", operands);
15345       else
15346         output_asm_insn ("fist%Z0\t%0", operands);
15347       if (round_mode != I387_CW_ANY)
15348         output_asm_insn ("fldcw\t%2", operands);
15349     }
15350
15351   return "";
15352 }
15353
15354 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
15355    have the values zero or one, indicates the ffreep insn's operand
15356    from the OPERANDS array.  */
15357
15358 static const char *
15359 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
15360 {
15361   if (TARGET_USE_FFREEP)
15362 #ifdef HAVE_AS_IX86_FFREEP
15363     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
15364 #else
15365     {
15366       static char retval[32];
15367       int regno = REGNO (operands[opno]);
15368
15369       gcc_assert (FP_REGNO_P (regno));
15370
15371       regno -= FIRST_STACK_REG;
15372
15373       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
15374       return retval;
15375     }
15376 #endif
15377
15378   return opno ? "fstp\t%y1" : "fstp\t%y0";
15379 }
15380
15381
15382 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
15383    should be used.  UNORDERED_P is true when fucom should be used.  */
15384
15385 const char *
15386 output_fp_compare (rtx insn, rtx *operands, bool eflags_p, bool unordered_p)
15387 {
15388   int stack_top_dies;
15389   rtx cmp_op0, cmp_op1;
15390   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
15391
15392   if (eflags_p)
15393     {
15394       cmp_op0 = operands[0];
15395       cmp_op1 = operands[1];
15396     }
15397   else
15398     {
15399       cmp_op0 = operands[1];
15400       cmp_op1 = operands[2];
15401     }
15402
15403   if (is_sse)
15404     {
15405       if (GET_MODE (operands[0]) == SFmode)
15406         if (unordered_p)
15407           return "%vucomiss\t{%1, %0|%0, %1}";
15408         else
15409           return "%vcomiss\t{%1, %0|%0, %1}";
15410       else
15411         if (unordered_p)
15412           return "%vucomisd\t{%1, %0|%0, %1}";
15413         else
15414           return "%vcomisd\t{%1, %0|%0, %1}";
15415     }
15416
15417   gcc_assert (STACK_TOP_P (cmp_op0));
15418
15419   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
15420
15421   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
15422     {
15423       if (stack_top_dies)
15424         {
15425           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
15426           return output_387_ffreep (operands, 1);
15427         }
15428       else
15429         return "ftst\n\tfnstsw\t%0";
15430     }
15431
15432   if (STACK_REG_P (cmp_op1)
15433       && stack_top_dies
15434       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
15435       && REGNO (cmp_op1) != FIRST_STACK_REG)
15436     {
15437       /* If both the top of the 387 stack dies, and the other operand
15438          is also a stack register that dies, then this must be a
15439          `fcompp' float compare */
15440
15441       if (eflags_p)
15442         {
15443           /* There is no double popping fcomi variant.  Fortunately,
15444              eflags is immune from the fstp's cc clobbering.  */
15445           if (unordered_p)
15446             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
15447           else
15448             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
15449           return output_387_ffreep (operands, 0);
15450         }
15451       else
15452         {
15453           if (unordered_p)
15454             return "fucompp\n\tfnstsw\t%0";
15455           else
15456             return "fcompp\n\tfnstsw\t%0";
15457         }
15458     }
15459   else
15460     {
15461       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
15462
15463       static const char * const alt[16] =
15464       {
15465         "fcom%Z2\t%y2\n\tfnstsw\t%0",
15466         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
15467         "fucom%Z2\t%y2\n\tfnstsw\t%0",
15468         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
15469
15470         "ficom%Z2\t%y2\n\tfnstsw\t%0",
15471         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
15472         NULL,
15473         NULL,
15474
15475         "fcomi\t{%y1, %0|%0, %y1}",
15476         "fcomip\t{%y1, %0|%0, %y1}",
15477         "fucomi\t{%y1, %0|%0, %y1}",
15478         "fucomip\t{%y1, %0|%0, %y1}",
15479
15480         NULL,
15481         NULL,
15482         NULL,
15483         NULL
15484       };
15485
15486       int mask;
15487       const char *ret;
15488
15489       mask  = eflags_p << 3;
15490       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
15491       mask |= unordered_p << 1;
15492       mask |= stack_top_dies;
15493
15494       gcc_assert (mask < 16);
15495       ret = alt[mask];
15496       gcc_assert (ret);
15497
15498       return ret;
15499     }
15500 }
15501
15502 void
15503 ix86_output_addr_vec_elt (FILE *file, int value)
15504 {
15505   const char *directive = ASM_LONG;
15506
15507 #ifdef ASM_QUAD
15508   if (TARGET_LP64)
15509     directive = ASM_QUAD;
15510 #else
15511   gcc_assert (!TARGET_64BIT);
15512 #endif
15513
15514   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
15515 }
15516
15517 void
15518 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
15519 {
15520   const char *directive = ASM_LONG;
15521
15522 #ifdef ASM_QUAD
15523   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
15524     directive = ASM_QUAD;
15525 #else
15526   gcc_assert (!TARGET_64BIT);
15527 #endif
15528   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
15529   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
15530     fprintf (file, "%s%s%d-%s%d\n",
15531              directive, LPREFIX, value, LPREFIX, rel);
15532   else if (HAVE_AS_GOTOFF_IN_DATA)
15533     fprintf (file, ASM_LONG "%s%d@GOTOFF\n", LPREFIX, value);
15534 #if TARGET_MACHO
15535   else if (TARGET_MACHO)
15536     {
15537       fprintf (file, ASM_LONG "%s%d-", LPREFIX, value);
15538       machopic_output_function_base_name (file);
15539       putc ('\n', file);
15540     }
15541 #endif
15542   else
15543     asm_fprintf (file, ASM_LONG "%U%s+[.-%s%d]\n",
15544                  GOT_SYMBOL_NAME, LPREFIX, value);
15545 }
15546 \f
15547 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
15548    for the target.  */
15549
15550 void
15551 ix86_expand_clear (rtx dest)
15552 {
15553   rtx tmp;
15554
15555   /* We play register width games, which are only valid after reload.  */
15556   gcc_assert (reload_completed);
15557
15558   /* Avoid HImode and its attendant prefix byte.  */
15559   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
15560     dest = gen_rtx_REG (SImode, REGNO (dest));
15561   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
15562
15563   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
15564   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
15565     {
15566       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15567       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
15568     }
15569
15570   emit_insn (tmp);
15571 }
15572
15573 /* X is an unchanging MEM.  If it is a constant pool reference, return
15574    the constant pool rtx, else NULL.  */
15575
15576 rtx
15577 maybe_get_pool_constant (rtx x)
15578 {
15579   x = ix86_delegitimize_address (XEXP (x, 0));
15580
15581   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
15582     return get_pool_constant (x);
15583
15584   return NULL_RTX;
15585 }
15586
15587 void
15588 ix86_expand_move (enum machine_mode mode, rtx operands[])
15589 {
15590   rtx op0, op1;
15591   enum tls_model model;
15592
15593   op0 = operands[0];
15594   op1 = operands[1];
15595
15596   if (GET_CODE (op1) == SYMBOL_REF)
15597     {
15598       model = SYMBOL_REF_TLS_MODEL (op1);
15599       if (model)
15600         {
15601           op1 = legitimize_tls_address (op1, model, true);
15602           op1 = force_operand (op1, op0);
15603           if (op1 == op0)
15604             return;
15605           if (GET_MODE (op1) != mode)
15606             op1 = convert_to_mode (mode, op1, 1);
15607         }
15608       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15609                && SYMBOL_REF_DLLIMPORT_P (op1))
15610         op1 = legitimize_dllimport_symbol (op1, false);
15611     }
15612   else if (GET_CODE (op1) == CONST
15613            && GET_CODE (XEXP (op1, 0)) == PLUS
15614            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
15615     {
15616       rtx addend = XEXP (XEXP (op1, 0), 1);
15617       rtx symbol = XEXP (XEXP (op1, 0), 0);
15618       rtx tmp = NULL;
15619
15620       model = SYMBOL_REF_TLS_MODEL (symbol);
15621       if (model)
15622         tmp = legitimize_tls_address (symbol, model, true);
15623       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15624                && SYMBOL_REF_DLLIMPORT_P (symbol))
15625         tmp = legitimize_dllimport_symbol (symbol, true);
15626
15627       if (tmp)
15628         {
15629           tmp = force_operand (tmp, NULL);
15630           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
15631                                      op0, 1, OPTAB_DIRECT);
15632           if (tmp == op0)
15633             return;
15634           op1 = convert_to_mode (mode, tmp, 1);
15635         }
15636     }
15637
15638   if ((flag_pic || MACHOPIC_INDIRECT)
15639       && symbolic_operand (op1, mode))
15640     {
15641       if (TARGET_MACHO && !TARGET_64BIT)
15642         {
15643 #if TARGET_MACHO
15644           /* dynamic-no-pic */
15645           if (MACHOPIC_INDIRECT)
15646             {
15647               rtx temp = ((reload_in_progress
15648                            || ((op0 && REG_P (op0))
15649                                && mode == Pmode))
15650                           ? op0 : gen_reg_rtx (Pmode));
15651               op1 = machopic_indirect_data_reference (op1, temp);
15652               if (MACHOPIC_PURE)
15653                 op1 = machopic_legitimize_pic_address (op1, mode,
15654                                                        temp == op1 ? 0 : temp);
15655             }
15656           if (op0 != op1 && GET_CODE (op0) != MEM)
15657             {
15658               rtx insn = gen_rtx_SET (VOIDmode, op0, op1);
15659               emit_insn (insn);
15660               return;
15661             }
15662           if (GET_CODE (op0) == MEM)
15663             op1 = force_reg (Pmode, op1);
15664           else
15665             {
15666               rtx temp = op0;
15667               if (GET_CODE (temp) != REG)
15668                 temp = gen_reg_rtx (Pmode);
15669               temp = legitimize_pic_address (op1, temp);
15670               if (temp == op0)
15671             return;
15672               op1 = temp;
15673             }
15674       /* dynamic-no-pic */
15675 #endif
15676         }
15677       else
15678         {
15679           if (MEM_P (op0))
15680             op1 = force_reg (mode, op1);
15681           else if (!(TARGET_64BIT && x86_64_movabs_operand (op1, DImode)))
15682             {
15683               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
15684               op1 = legitimize_pic_address (op1, reg);
15685               if (op0 == op1)
15686                 return;
15687               if (GET_MODE (op1) != mode)
15688                 op1 = convert_to_mode (mode, op1, 1);
15689             }
15690         }
15691     }
15692   else
15693     {
15694       if (MEM_P (op0)
15695           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
15696               || !push_operand (op0, mode))
15697           && MEM_P (op1))
15698         op1 = force_reg (mode, op1);
15699
15700       if (push_operand (op0, mode)
15701           && ! general_no_elim_operand (op1, mode))
15702         op1 = copy_to_mode_reg (mode, op1);
15703
15704       /* Force large constants in 64bit compilation into register
15705          to get them CSEed.  */
15706       if (can_create_pseudo_p ()
15707           && (mode == DImode) && TARGET_64BIT
15708           && immediate_operand (op1, mode)
15709           && !x86_64_zext_immediate_operand (op1, VOIDmode)
15710           && !register_operand (op0, mode)
15711           && optimize)
15712         op1 = copy_to_mode_reg (mode, op1);
15713
15714       if (can_create_pseudo_p ()
15715           && FLOAT_MODE_P (mode)
15716           && GET_CODE (op1) == CONST_DOUBLE)
15717         {
15718           /* If we are loading a floating point constant to a register,
15719              force the value to memory now, since we'll get better code
15720              out the back end.  */
15721
15722           op1 = validize_mem (force_const_mem (mode, op1));
15723           if (!register_operand (op0, mode))
15724             {
15725               rtx temp = gen_reg_rtx (mode);
15726               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
15727               emit_move_insn (op0, temp);
15728               return;
15729             }
15730         }
15731     }
15732
15733   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15734 }
15735
15736 void
15737 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
15738 {
15739   rtx op0 = operands[0], op1 = operands[1];
15740   unsigned int align = GET_MODE_ALIGNMENT (mode);
15741
15742   /* Force constants other than zero into memory.  We do not know how
15743      the instructions used to build constants modify the upper 64 bits
15744      of the register, once we have that information we may be able
15745      to handle some of them more efficiently.  */
15746   if (can_create_pseudo_p ()
15747       && register_operand (op0, mode)
15748       && (CONSTANT_P (op1)
15749           || (GET_CODE (op1) == SUBREG
15750               && CONSTANT_P (SUBREG_REG (op1))))
15751       && !standard_sse_constant_p (op1))
15752     op1 = validize_mem (force_const_mem (mode, op1));
15753
15754   /* We need to check memory alignment for SSE mode since attribute
15755      can make operands unaligned.  */
15756   if (can_create_pseudo_p ()
15757       && SSE_REG_MODE_P (mode)
15758       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
15759           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
15760     {
15761       rtx tmp[2];
15762
15763       /* ix86_expand_vector_move_misalign() does not like constants ... */
15764       if (CONSTANT_P (op1)
15765           || (GET_CODE (op1) == SUBREG
15766               && CONSTANT_P (SUBREG_REG (op1))))
15767         op1 = validize_mem (force_const_mem (mode, op1));
15768
15769       /* ... nor both arguments in memory.  */
15770       if (!register_operand (op0, mode)
15771           && !register_operand (op1, mode))
15772         op1 = force_reg (mode, op1);
15773
15774       tmp[0] = op0; tmp[1] = op1;
15775       ix86_expand_vector_move_misalign (mode, tmp);
15776       return;
15777     }
15778
15779   /* Make operand1 a register if it isn't already.  */
15780   if (can_create_pseudo_p ()
15781       && !register_operand (op0, mode)
15782       && !register_operand (op1, mode))
15783     {
15784       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
15785       return;
15786     }
15787
15788   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15789 }
15790
15791 /* Split 32-byte AVX unaligned load and store if needed.  */
15792
15793 static void
15794 ix86_avx256_split_vector_move_misalign (rtx op0, rtx op1)
15795 {
15796   rtx m;
15797   rtx (*extract) (rtx, rtx, rtx);
15798   rtx (*load_unaligned) (rtx, rtx);
15799   rtx (*store_unaligned) (rtx, rtx);
15800   enum machine_mode mode;
15801
15802   switch (GET_MODE (op0))
15803     {
15804     default:
15805       gcc_unreachable ();
15806     case V32QImode:
15807       extract = gen_avx_vextractf128v32qi;
15808       load_unaligned = gen_avx_loaddqu256;
15809       store_unaligned = gen_avx_storedqu256;
15810       mode = V16QImode;
15811       break;
15812     case V8SFmode:
15813       extract = gen_avx_vextractf128v8sf;
15814       load_unaligned = gen_avx_loadups256;
15815       store_unaligned = gen_avx_storeups256;
15816       mode = V4SFmode;
15817       break;
15818     case V4DFmode:
15819       extract = gen_avx_vextractf128v4df;
15820       load_unaligned = gen_avx_loadupd256;
15821       store_unaligned = gen_avx_storeupd256;
15822       mode = V2DFmode;
15823       break;
15824     }
15825
15826   if (MEM_P (op1))
15827     {
15828       if (TARGET_AVX256_SPLIT_UNALIGNED_LOAD)
15829         {
15830           rtx r = gen_reg_rtx (mode);
15831           m = adjust_address (op1, mode, 0);
15832           emit_move_insn (r, m);
15833           m = adjust_address (op1, mode, 16);
15834           r = gen_rtx_VEC_CONCAT (GET_MODE (op0), r, m);
15835           emit_move_insn (op0, r);
15836         }
15837       else
15838         emit_insn (load_unaligned (op0, op1));
15839     }
15840   else if (MEM_P (op0))
15841     {
15842       if (TARGET_AVX256_SPLIT_UNALIGNED_STORE)
15843         {
15844           m = adjust_address (op0, mode, 0);
15845           emit_insn (extract (m, op1, const0_rtx));
15846           m = adjust_address (op0, mode, 16);
15847           emit_insn (extract (m, op1, const1_rtx));
15848         }
15849       else
15850         emit_insn (store_unaligned (op0, op1));
15851     }
15852   else
15853     gcc_unreachable ();
15854 }
15855
15856 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
15857    straight to ix86_expand_vector_move.  */
15858 /* Code generation for scalar reg-reg moves of single and double precision data:
15859      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
15860        movaps reg, reg
15861      else
15862        movss reg, reg
15863      if (x86_sse_partial_reg_dependency == true)
15864        movapd reg, reg
15865      else
15866        movsd reg, reg
15867
15868    Code generation for scalar loads of double precision data:
15869      if (x86_sse_split_regs == true)
15870        movlpd mem, reg      (gas syntax)
15871      else
15872        movsd mem, reg
15873
15874    Code generation for unaligned packed loads of single precision data
15875    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
15876      if (x86_sse_unaligned_move_optimal)
15877        movups mem, reg
15878
15879      if (x86_sse_partial_reg_dependency == true)
15880        {
15881          xorps  reg, reg
15882          movlps mem, reg
15883          movhps mem+8, reg
15884        }
15885      else
15886        {
15887          movlps mem, reg
15888          movhps mem+8, reg
15889        }
15890
15891    Code generation for unaligned packed loads of double precision data
15892    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
15893      if (x86_sse_unaligned_move_optimal)
15894        movupd mem, reg
15895
15896      if (x86_sse_split_regs == true)
15897        {
15898          movlpd mem, reg
15899          movhpd mem+8, reg
15900        }
15901      else
15902        {
15903          movsd  mem, reg
15904          movhpd mem+8, reg
15905        }
15906  */
15907
15908 void
15909 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
15910 {
15911   rtx op0, op1, m;
15912   rtx (*move_unaligned) (rtx, rtx);
15913
15914   op0 = operands[0];
15915   op1 = operands[1];
15916
15917   if (TARGET_AVX)
15918     {
15919       switch (GET_MODE_CLASS (mode))
15920         {
15921         case MODE_VECTOR_INT:
15922         case MODE_INT:
15923           switch (GET_MODE_SIZE (mode))
15924             {
15925             case 16:
15926               /*  If we're optimizing for size, movups is the smallest.  */
15927               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15928                 {
15929                   if (MEM_P (op1))
15930                     move_unaligned = gen_sse_loadups;
15931                   else if (MEM_P (op0))
15932                     move_unaligned = gen_sse_storeups;
15933                   else
15934                     gcc_unreachable ();
15935
15936                   op0 = gen_lowpart (V4SFmode, op0);
15937                   op1 = gen_lowpart (V4SFmode, op1);
15938                   emit_insn (move_unaligned (op0, op1));
15939                   return;
15940                 }
15941               if (MEM_P (op1))
15942                 move_unaligned = gen_sse2_loaddqu;
15943               else if (MEM_P (op0))
15944                 move_unaligned = gen_sse2_storedqu;
15945               else
15946                 gcc_unreachable ();
15947
15948               op0 = gen_lowpart (V16QImode, op0);
15949               op1 = gen_lowpart (V16QImode, op1);
15950               emit_insn (move_unaligned (op0, op1));
15951               break;
15952             case 32:
15953               op0 = gen_lowpart (V32QImode, op0);
15954               op1 = gen_lowpart (V32QImode, op1);
15955               ix86_avx256_split_vector_move_misalign (op0, op1);
15956               break;
15957             default:
15958               gcc_unreachable ();
15959             }
15960           break;
15961         case MODE_VECTOR_FLOAT:
15962           op0 = gen_lowpart (mode, op0);
15963           op1 = gen_lowpart (mode, op1);
15964
15965           switch (mode)
15966             {
15967             case V4SFmode:
15968               if (MEM_P (op1))
15969                 move_unaligned = gen_sse_loadups;
15970               else if (MEM_P (op0))
15971                 move_unaligned = gen_sse_storeups;
15972               else
15973                 gcc_unreachable ();
15974
15975               emit_insn (move_unaligned (op0, op1));
15976               break;
15977             case V8SFmode:
15978               ix86_avx256_split_vector_move_misalign (op0, op1);
15979               break;
15980             case V2DFmode:
15981               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15982                 {
15983                   if (MEM_P (op1))
15984                     move_unaligned = gen_sse_loadups;
15985                   else if (MEM_P (op0))
15986                     move_unaligned = gen_sse_storeups;
15987                   else
15988                     gcc_unreachable ();
15989
15990                   op0 = gen_lowpart (V4SFmode, op0);
15991                   op1 = gen_lowpart (V4SFmode, op1);
15992                   emit_insn (move_unaligned (op0, op1));
15993                   return;
15994                 }
15995               if (MEM_P (op1))
15996                 move_unaligned = gen_sse2_loadupd;
15997               else if (MEM_P (op0))
15998                 move_unaligned = gen_sse2_storeupd;
15999               else
16000                 gcc_unreachable ();
16001
16002               emit_insn (move_unaligned (op0, op1));
16003               break;
16004             case V4DFmode:
16005               ix86_avx256_split_vector_move_misalign (op0, op1);
16006               break;
16007             default:
16008               gcc_unreachable ();
16009             }
16010           break;
16011
16012         default:
16013           gcc_unreachable ();
16014         }
16015
16016       return;
16017     }
16018
16019   if (MEM_P (op1))
16020     {
16021       /* If we're optimizing for size, movups is the smallest.  */
16022       if (optimize_insn_for_size_p ()
16023           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
16024         {
16025           op0 = gen_lowpart (V4SFmode, op0);
16026           op1 = gen_lowpart (V4SFmode, op1);
16027           emit_insn (gen_sse_loadups (op0, op1));
16028           return;
16029         }
16030
16031       /* ??? If we have typed data, then it would appear that using
16032          movdqu is the only way to get unaligned data loaded with
16033          integer type.  */
16034       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16035         {
16036           op0 = gen_lowpart (V16QImode, op0);
16037           op1 = gen_lowpart (V16QImode, op1);
16038           emit_insn (gen_sse2_loaddqu (op0, op1));
16039           return;
16040         }
16041
16042       if (TARGET_SSE2 && mode == V2DFmode)
16043         {
16044           rtx zero;
16045
16046           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
16047             {
16048               op0 = gen_lowpart (V2DFmode, op0);
16049               op1 = gen_lowpart (V2DFmode, op1);
16050               emit_insn (gen_sse2_loadupd (op0, op1));
16051               return;
16052             }
16053
16054           /* When SSE registers are split into halves, we can avoid
16055              writing to the top half twice.  */
16056           if (TARGET_SSE_SPLIT_REGS)
16057             {
16058               emit_clobber (op0);
16059               zero = op0;
16060             }
16061           else
16062             {
16063               /* ??? Not sure about the best option for the Intel chips.
16064                  The following would seem to satisfy; the register is
16065                  entirely cleared, breaking the dependency chain.  We
16066                  then store to the upper half, with a dependency depth
16067                  of one.  A rumor has it that Intel recommends two movsd
16068                  followed by an unpacklpd, but this is unconfirmed.  And
16069                  given that the dependency depth of the unpacklpd would
16070                  still be one, I'm not sure why this would be better.  */
16071               zero = CONST0_RTX (V2DFmode);
16072             }
16073
16074           m = adjust_address (op1, DFmode, 0);
16075           emit_insn (gen_sse2_loadlpd (op0, zero, m));
16076           m = adjust_address (op1, DFmode, 8);
16077           emit_insn (gen_sse2_loadhpd (op0, op0, m));
16078         }
16079       else
16080         {
16081           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
16082             {
16083               op0 = gen_lowpart (V4SFmode, op0);
16084               op1 = gen_lowpart (V4SFmode, op1);
16085               emit_insn (gen_sse_loadups (op0, op1));
16086               return;
16087             }
16088
16089           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
16090             emit_move_insn (op0, CONST0_RTX (mode));
16091           else
16092             emit_clobber (op0);
16093
16094           if (mode != V4SFmode)
16095             op0 = gen_lowpart (V4SFmode, op0);
16096           m = adjust_address (op1, V2SFmode, 0);
16097           emit_insn (gen_sse_loadlps (op0, op0, m));
16098           m = adjust_address (op1, V2SFmode, 8);
16099           emit_insn (gen_sse_loadhps (op0, op0, m));
16100         }
16101     }
16102   else if (MEM_P (op0))
16103     {
16104       /* If we're optimizing for size, movups is the smallest.  */
16105       if (optimize_insn_for_size_p ()
16106           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
16107         {
16108           op0 = gen_lowpart (V4SFmode, op0);
16109           op1 = gen_lowpart (V4SFmode, op1);
16110           emit_insn (gen_sse_storeups (op0, op1));
16111           return;
16112         }
16113
16114       /* ??? Similar to above, only less clear because of quote
16115          typeless stores unquote.  */
16116       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
16117           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
16118         {
16119           op0 = gen_lowpart (V16QImode, op0);
16120           op1 = gen_lowpart (V16QImode, op1);
16121           emit_insn (gen_sse2_storedqu (op0, op1));
16122           return;
16123         }
16124
16125       if (TARGET_SSE2 && mode == V2DFmode)
16126         {
16127           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
16128             {
16129               op0 = gen_lowpart (V2DFmode, op0);
16130               op1 = gen_lowpart (V2DFmode, op1);
16131               emit_insn (gen_sse2_storeupd (op0, op1));
16132             }
16133           else
16134             {
16135               m = adjust_address (op0, DFmode, 0);
16136               emit_insn (gen_sse2_storelpd (m, op1));
16137               m = adjust_address (op0, DFmode, 8);
16138               emit_insn (gen_sse2_storehpd (m, op1));
16139             }
16140         }
16141       else
16142         {
16143           if (mode != V4SFmode)
16144             op1 = gen_lowpart (V4SFmode, op1);
16145
16146           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
16147             {
16148               op0 = gen_lowpart (V4SFmode, op0);
16149               emit_insn (gen_sse_storeups (op0, op1));
16150             }
16151           else
16152             {
16153               m = adjust_address (op0, V2SFmode, 0);
16154               emit_insn (gen_sse_storelps (m, op1));
16155               m = adjust_address (op0, V2SFmode, 8);
16156               emit_insn (gen_sse_storehps (m, op1));
16157             }
16158         }
16159     }
16160   else
16161     gcc_unreachable ();
16162 }
16163
16164 /* Expand a push in MODE.  This is some mode for which we do not support
16165    proper push instructions, at least from the registers that we expect
16166    the value to live in.  */
16167
16168 void
16169 ix86_expand_push (enum machine_mode mode, rtx x)
16170 {
16171   rtx tmp;
16172
16173   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
16174                              GEN_INT (-GET_MODE_SIZE (mode)),
16175                              stack_pointer_rtx, 1, OPTAB_DIRECT);
16176   if (tmp != stack_pointer_rtx)
16177     emit_move_insn (stack_pointer_rtx, tmp);
16178
16179   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
16180
16181   /* When we push an operand onto stack, it has to be aligned at least
16182      at the function argument boundary.  However since we don't have
16183      the argument type, we can't determine the actual argument
16184      boundary.  */
16185   emit_move_insn (tmp, x);
16186 }
16187
16188 /* Helper function of ix86_fixup_binary_operands to canonicalize
16189    operand order.  Returns true if the operands should be swapped.  */
16190
16191 static bool
16192 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
16193                              rtx operands[])
16194 {
16195   rtx dst = operands[0];
16196   rtx src1 = operands[1];
16197   rtx src2 = operands[2];
16198
16199   /* If the operation is not commutative, we can't do anything.  */
16200   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
16201     return false;
16202
16203   /* Highest priority is that src1 should match dst.  */
16204   if (rtx_equal_p (dst, src1))
16205     return false;
16206   if (rtx_equal_p (dst, src2))
16207     return true;
16208
16209   /* Next highest priority is that immediate constants come second.  */
16210   if (immediate_operand (src2, mode))
16211     return false;
16212   if (immediate_operand (src1, mode))
16213     return true;
16214
16215   /* Lowest priority is that memory references should come second.  */
16216   if (MEM_P (src2))
16217     return false;
16218   if (MEM_P (src1))
16219     return true;
16220
16221   return false;
16222 }
16223
16224
16225 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
16226    destination to use for the operation.  If different from the true
16227    destination in operands[0], a copy operation will be required.  */
16228
16229 rtx
16230 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
16231                             rtx operands[])
16232 {
16233   rtx dst = operands[0];
16234   rtx src1 = operands[1];
16235   rtx src2 = operands[2];
16236
16237   /* Canonicalize operand order.  */
16238   if (ix86_swap_binary_operands_p (code, mode, operands))
16239     {
16240       rtx temp;
16241
16242       /* It is invalid to swap operands of different modes.  */
16243       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
16244
16245       temp = src1;
16246       src1 = src2;
16247       src2 = temp;
16248     }
16249
16250   /* Both source operands cannot be in memory.  */
16251   if (MEM_P (src1) && MEM_P (src2))
16252     {
16253       /* Optimization: Only read from memory once.  */
16254       if (rtx_equal_p (src1, src2))
16255         {
16256           src2 = force_reg (mode, src2);
16257           src1 = src2;
16258         }
16259       else
16260         src2 = force_reg (mode, src2);
16261     }
16262
16263   /* If the destination is memory, and we do not have matching source
16264      operands, do things in registers.  */
16265   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
16266     dst = gen_reg_rtx (mode);
16267
16268   /* Source 1 cannot be a constant.  */
16269   if (CONSTANT_P (src1))
16270     src1 = force_reg (mode, src1);
16271
16272   /* Source 1 cannot be a non-matching memory.  */
16273   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
16274     src1 = force_reg (mode, src1);
16275
16276   /* Improve address combine.  */
16277   if (code == PLUS
16278       && GET_MODE_CLASS (mode) == MODE_INT
16279       && MEM_P (src2))
16280     src2 = force_reg (mode, src2);
16281
16282   operands[1] = src1;
16283   operands[2] = src2;
16284   return dst;
16285 }
16286
16287 /* Similarly, but assume that the destination has already been
16288    set up properly.  */
16289
16290 void
16291 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
16292                                     enum machine_mode mode, rtx operands[])
16293 {
16294   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
16295   gcc_assert (dst == operands[0]);
16296 }
16297
16298 /* Attempt to expand a binary operator.  Make the expansion closer to the
16299    actual machine, then just general_operand, which will allow 3 separate
16300    memory references (one output, two input) in a single insn.  */
16301
16302 void
16303 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
16304                              rtx operands[])
16305 {
16306   rtx src1, src2, dst, op, clob;
16307
16308   dst = ix86_fixup_binary_operands (code, mode, operands);
16309   src1 = operands[1];
16310   src2 = operands[2];
16311
16312  /* Emit the instruction.  */
16313
16314   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
16315   if (reload_in_progress)
16316     {
16317       /* Reload doesn't know about the flags register, and doesn't know that
16318          it doesn't want to clobber it.  We can only do this with PLUS.  */
16319       gcc_assert (code == PLUS);
16320       emit_insn (op);
16321     }
16322   else if (reload_completed
16323            && code == PLUS
16324            && !rtx_equal_p (dst, src1))
16325     {
16326       /* This is going to be an LEA; avoid splitting it later.  */
16327       emit_insn (op);
16328     }
16329   else
16330     {
16331       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16332       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
16333     }
16334
16335   /* Fix up the destination if needed.  */
16336   if (dst != operands[0])
16337     emit_move_insn (operands[0], dst);
16338 }
16339
16340 /* Return TRUE or FALSE depending on whether the binary operator meets the
16341    appropriate constraints.  */
16342
16343 bool
16344 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
16345                          rtx operands[3])
16346 {
16347   rtx dst = operands[0];
16348   rtx src1 = operands[1];
16349   rtx src2 = operands[2];
16350
16351   /* Both source operands cannot be in memory.  */
16352   if (MEM_P (src1) && MEM_P (src2))
16353     return false;
16354
16355   /* Canonicalize operand order for commutative operators.  */
16356   if (ix86_swap_binary_operands_p (code, mode, operands))
16357     {
16358       rtx temp = src1;
16359       src1 = src2;
16360       src2 = temp;
16361     }
16362
16363   /* If the destination is memory, we must have a matching source operand.  */
16364   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
16365       return false;
16366
16367   /* Source 1 cannot be a constant.  */
16368   if (CONSTANT_P (src1))
16369     return false;
16370
16371   /* Source 1 cannot be a non-matching memory.  */
16372   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
16373     /* Support "andhi/andsi/anddi" as a zero-extending move.  */
16374     return (code == AND
16375             && (mode == HImode
16376                 || mode == SImode
16377                 || (TARGET_64BIT && mode == DImode))
16378             && satisfies_constraint_L (src2));
16379
16380   return true;
16381 }
16382
16383 /* Attempt to expand a unary operator.  Make the expansion closer to the
16384    actual machine, then just general_operand, which will allow 2 separate
16385    memory references (one output, one input) in a single insn.  */
16386
16387 void
16388 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
16389                             rtx operands[])
16390 {
16391   int matching_memory;
16392   rtx src, dst, op, clob;
16393
16394   dst = operands[0];
16395   src = operands[1];
16396
16397   /* If the destination is memory, and we do not have matching source
16398      operands, do things in registers.  */
16399   matching_memory = 0;
16400   if (MEM_P (dst))
16401     {
16402       if (rtx_equal_p (dst, src))
16403         matching_memory = 1;
16404       else
16405         dst = gen_reg_rtx (mode);
16406     }
16407
16408   /* When source operand is memory, destination must match.  */
16409   if (MEM_P (src) && !matching_memory)
16410     src = force_reg (mode, src);
16411
16412   /* Emit the instruction.  */
16413
16414   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
16415   if (reload_in_progress || code == NOT)
16416     {
16417       /* Reload doesn't know about the flags register, and doesn't know that
16418          it doesn't want to clobber it.  */
16419       gcc_assert (code == NOT);
16420       emit_insn (op);
16421     }
16422   else
16423     {
16424       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16425       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
16426     }
16427
16428   /* Fix up the destination if needed.  */
16429   if (dst != operands[0])
16430     emit_move_insn (operands[0], dst);
16431 }
16432
16433 /* Split 32bit/64bit divmod with 8bit unsigned divmod if dividend and
16434    divisor are within the range [0-255].  */
16435
16436 void
16437 ix86_split_idivmod (enum machine_mode mode, rtx operands[],
16438                     bool signed_p)
16439 {
16440   rtx end_label, qimode_label;
16441   rtx insn, div, mod;
16442   rtx scratch, tmp0, tmp1, tmp2;
16443   rtx (*gen_divmod4_1) (rtx, rtx, rtx, rtx);
16444   rtx (*gen_zero_extend) (rtx, rtx);
16445   rtx (*gen_test_ccno_1) (rtx, rtx);
16446
16447   switch (mode)
16448     {
16449     case SImode:
16450       gen_divmod4_1 = signed_p ? gen_divmodsi4_1 : gen_udivmodsi4_1;
16451       gen_test_ccno_1 = gen_testsi_ccno_1;
16452       gen_zero_extend = gen_zero_extendqisi2;
16453       break;
16454     case DImode:
16455       gen_divmod4_1 = signed_p ? gen_divmoddi4_1 : gen_udivmoddi4_1;
16456       gen_test_ccno_1 = gen_testdi_ccno_1;
16457       gen_zero_extend = gen_zero_extendqidi2;
16458       break;
16459     default:
16460       gcc_unreachable ();
16461     }
16462
16463   end_label = gen_label_rtx ();
16464   qimode_label = gen_label_rtx ();
16465
16466   scratch = gen_reg_rtx (mode);
16467
16468   /* Use 8bit unsigned divimod if dividend and divisor are within
16469      the range [0-255].  */
16470   emit_move_insn (scratch, operands[2]);
16471   scratch = expand_simple_binop (mode, IOR, scratch, operands[3],
16472                                  scratch, 1, OPTAB_DIRECT);
16473   emit_insn (gen_test_ccno_1 (scratch, GEN_INT (-0x100)));
16474   tmp0 = gen_rtx_REG (CCNOmode, FLAGS_REG);
16475   tmp0 = gen_rtx_EQ (VOIDmode, tmp0, const0_rtx);
16476   tmp0 = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp0,
16477                                gen_rtx_LABEL_REF (VOIDmode, qimode_label),
16478                                pc_rtx);
16479   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp0));
16480   predict_jump (REG_BR_PROB_BASE * 50 / 100);
16481   JUMP_LABEL (insn) = qimode_label;
16482
16483   /* Generate original signed/unsigned divimod.  */
16484   div = gen_divmod4_1 (operands[0], operands[1],
16485                        operands[2], operands[3]);
16486   emit_insn (div);
16487
16488   /* Branch to the end.  */
16489   emit_jump_insn (gen_jump (end_label));
16490   emit_barrier ();
16491
16492   /* Generate 8bit unsigned divide.  */
16493   emit_label (qimode_label);
16494   /* Don't use operands[0] for result of 8bit divide since not all
16495      registers support QImode ZERO_EXTRACT.  */
16496   tmp0 = simplify_gen_subreg (HImode, scratch, mode, 0);
16497   tmp1 = simplify_gen_subreg (HImode, operands[2], mode, 0);
16498   tmp2 = simplify_gen_subreg (QImode, operands[3], mode, 0);
16499   emit_insn (gen_udivmodhiqi3 (tmp0, tmp1, tmp2));
16500
16501   if (signed_p)
16502     {
16503       div = gen_rtx_DIV (SImode, operands[2], operands[3]);
16504       mod = gen_rtx_MOD (SImode, operands[2], operands[3]);
16505     }
16506   else
16507     {
16508       div = gen_rtx_UDIV (SImode, operands[2], operands[3]);
16509       mod = gen_rtx_UMOD (SImode, operands[2], operands[3]);
16510     }
16511
16512   /* Extract remainder from AH.  */
16513   tmp1 = gen_rtx_ZERO_EXTRACT (mode, tmp0, GEN_INT (8), GEN_INT (8));
16514   if (REG_P (operands[1]))
16515     insn = emit_move_insn (operands[1], tmp1);
16516   else
16517     {
16518       /* Need a new scratch register since the old one has result
16519          of 8bit divide.  */
16520       scratch = gen_reg_rtx (mode);
16521       emit_move_insn (scratch, tmp1);
16522       insn = emit_move_insn (operands[1], scratch);
16523     }
16524   set_unique_reg_note (insn, REG_EQUAL, mod);
16525
16526   /* Zero extend quotient from AL.  */
16527   tmp1 = gen_lowpart (QImode, tmp0);
16528   insn = emit_insn (gen_zero_extend (operands[0], tmp1));
16529   set_unique_reg_note (insn, REG_EQUAL, div);
16530
16531   emit_label (end_label);
16532 }
16533
16534 #define LEA_MAX_STALL (3)
16535 #define LEA_SEARCH_THRESHOLD (LEA_MAX_STALL << 1)
16536
16537 /* Increase given DISTANCE in half-cycles according to
16538    dependencies between PREV and NEXT instructions.
16539    Add 1 half-cycle if there is no dependency and
16540    go to next cycle if there is some dependecy.  */
16541
16542 static unsigned int
16543 increase_distance (rtx prev, rtx next, unsigned int distance)
16544 {
16545   df_ref *use_rec;
16546   df_ref *def_rec;
16547
16548   if (!prev || !next)
16549     return distance + (distance & 1) + 2;
16550
16551   if (!DF_INSN_USES (next) || !DF_INSN_DEFS (prev))
16552     return distance + 1;
16553
16554   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
16555     for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
16556       if (!DF_REF_IS_ARTIFICIAL (*def_rec)
16557           && DF_REF_REGNO (*use_rec) == DF_REF_REGNO (*def_rec))
16558         return distance + (distance & 1) + 2;
16559
16560   return distance + 1;
16561 }
16562
16563 /* Function checks if instruction INSN defines register number
16564    REGNO1 or REGNO2.  */
16565
16566 static bool
16567 insn_defines_reg (unsigned int regno1, unsigned int regno2,
16568                   rtx insn)
16569 {
16570   df_ref *def_rec;
16571
16572   for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
16573     if (DF_REF_REG_DEF_P (*def_rec)
16574         && !DF_REF_IS_ARTIFICIAL (*def_rec)
16575         && (regno1 == DF_REF_REGNO (*def_rec)
16576             || regno2 == DF_REF_REGNO (*def_rec)))
16577       {
16578         return true;
16579       }
16580
16581   return false;
16582 }
16583
16584 /* Function checks if instruction INSN uses register number
16585    REGNO as a part of address expression.  */
16586
16587 static bool
16588 insn_uses_reg_mem (unsigned int regno, rtx insn)
16589 {
16590   df_ref *use_rec;
16591
16592   for (use_rec = DF_INSN_USES (insn); *use_rec; use_rec++)
16593     if (DF_REF_REG_MEM_P (*use_rec) && regno == DF_REF_REGNO (*use_rec))
16594       return true;
16595
16596   return false;
16597 }
16598
16599 /* Search backward for non-agu definition of register number REGNO1
16600    or register number REGNO2 in basic block starting from instruction
16601    START up to head of basic block or instruction INSN.
16602
16603    Function puts true value into *FOUND var if definition was found
16604    and false otherwise.
16605
16606    Distance in half-cycles between START and found instruction or head
16607    of BB is added to DISTANCE and returned.  */
16608
16609 static int
16610 distance_non_agu_define_in_bb (unsigned int regno1, unsigned int regno2,
16611                                rtx insn, int distance,
16612                                rtx start, bool *found)
16613 {
16614   basic_block bb = start ? BLOCK_FOR_INSN (start) : NULL;
16615   rtx prev = start;
16616   rtx next = NULL;
16617
16618   *found = false;
16619
16620   while (prev
16621          && prev != insn
16622          && distance < LEA_SEARCH_THRESHOLD)
16623     {
16624       if (NONDEBUG_INSN_P (prev) && NONJUMP_INSN_P (prev))
16625         {
16626           distance = increase_distance (prev, next, distance);
16627           if (insn_defines_reg (regno1, regno2, prev))
16628             {
16629               if (recog_memoized (prev) < 0
16630                   || get_attr_type (prev) != TYPE_LEA)
16631                 {
16632                   *found = true;
16633                   return distance;
16634                 }
16635             }
16636
16637           next = prev;
16638         }
16639       if (prev == BB_HEAD (bb))
16640         break;
16641
16642       prev = PREV_INSN (prev);
16643     }
16644
16645   return distance;
16646 }
16647
16648 /* Search backward for non-agu definition of register number REGNO1
16649    or register number REGNO2 in INSN's basic block until
16650    1. Pass LEA_SEARCH_THRESHOLD instructions, or
16651    2. Reach neighbour BBs boundary, or
16652    3. Reach agu definition.
16653    Returns the distance between the non-agu definition point and INSN.
16654    If no definition point, returns -1.  */
16655
16656 static int
16657 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
16658                          rtx insn)
16659 {
16660   basic_block bb = BLOCK_FOR_INSN (insn);
16661   int distance = 0;
16662   bool found = false;
16663
16664   if (insn != BB_HEAD (bb))
16665     distance = distance_non_agu_define_in_bb (regno1, regno2, insn,
16666                                               distance, PREV_INSN (insn),
16667                                               &found);
16668
16669   if (!found && distance < LEA_SEARCH_THRESHOLD)
16670     {
16671       edge e;
16672       edge_iterator ei;
16673       bool simple_loop = false;
16674
16675       FOR_EACH_EDGE (e, ei, bb->preds)
16676         if (e->src == bb)
16677           {
16678             simple_loop = true;
16679             break;
16680           }
16681
16682       if (simple_loop)
16683         distance = distance_non_agu_define_in_bb (regno1, regno2,
16684                                                   insn, distance,
16685                                                   BB_END (bb), &found);
16686       else
16687         {
16688           int shortest_dist = -1;
16689           bool found_in_bb = false;
16690
16691           FOR_EACH_EDGE (e, ei, bb->preds)
16692             {
16693               int bb_dist
16694                 = distance_non_agu_define_in_bb (regno1, regno2,
16695                                                  insn, distance,
16696                                                  BB_END (e->src),
16697                                                  &found_in_bb);
16698               if (found_in_bb)
16699                 {
16700                   if (shortest_dist < 0)
16701                     shortest_dist = bb_dist;
16702                   else if (bb_dist > 0)
16703                     shortest_dist = MIN (bb_dist, shortest_dist);
16704
16705                   found = true;
16706                 }
16707             }
16708
16709           distance = shortest_dist;
16710         }
16711     }
16712
16713   /* get_attr_type may modify recog data.  We want to make sure
16714      that recog data is valid for instruction INSN, on which
16715      distance_non_agu_define is called.  INSN is unchanged here.  */
16716   extract_insn_cached (insn);
16717
16718   if (!found)
16719     return -1;
16720
16721   return distance >> 1;
16722 }
16723
16724 /* Return the distance in half-cycles between INSN and the next
16725    insn that uses register number REGNO in memory address added
16726    to DISTANCE.  Return -1 if REGNO0 is set.
16727
16728    Put true value into *FOUND if register usage was found and
16729    false otherwise.
16730    Put true value into *REDEFINED if register redefinition was
16731    found and false otherwise.  */
16732
16733 static int
16734 distance_agu_use_in_bb (unsigned int regno,
16735                         rtx insn, int distance, rtx start,
16736                         bool *found, bool *redefined)
16737 {
16738   basic_block bb = start ? BLOCK_FOR_INSN (start) : NULL;
16739   rtx next = start;
16740   rtx prev = NULL;
16741
16742   *found = false;
16743   *redefined = false;
16744
16745   while (next
16746          && next != insn
16747          && distance < LEA_SEARCH_THRESHOLD)
16748     {
16749       if (NONDEBUG_INSN_P (next) && NONJUMP_INSN_P (next))
16750         {
16751           distance = increase_distance(prev, next, distance);
16752           if (insn_uses_reg_mem (regno, next))
16753             {
16754               /* Return DISTANCE if OP0 is used in memory
16755                  address in NEXT.  */
16756               *found = true;
16757               return distance;
16758             }
16759
16760           if (insn_defines_reg (regno, INVALID_REGNUM, next))
16761             {
16762               /* Return -1 if OP0 is set in NEXT.  */
16763               *redefined = true;
16764               return -1;
16765             }
16766
16767           prev = next;
16768         }
16769
16770       if (next == BB_END (bb))
16771         break;
16772
16773       next = NEXT_INSN (next);
16774     }
16775
16776   return distance;
16777 }
16778
16779 /* Return the distance between INSN and the next insn that uses
16780    register number REGNO0 in memory address.  Return -1 if no such
16781    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
16782
16783 static int
16784 distance_agu_use (unsigned int regno0, rtx insn)
16785 {
16786   basic_block bb = BLOCK_FOR_INSN (insn);
16787   int distance = 0;
16788   bool found = false;
16789   bool redefined = false;
16790
16791   if (insn != BB_END (bb))
16792     distance = distance_agu_use_in_bb (regno0, insn, distance,
16793                                        NEXT_INSN (insn),
16794                                        &found, &redefined);
16795
16796   if (!found && !redefined && distance < LEA_SEARCH_THRESHOLD)
16797     {
16798       edge e;
16799       edge_iterator ei;
16800       bool simple_loop = false;
16801
16802       FOR_EACH_EDGE (e, ei, bb->succs)
16803         if (e->dest == bb)
16804           {
16805             simple_loop = true;
16806             break;
16807           }
16808
16809       if (simple_loop)
16810         distance = distance_agu_use_in_bb (regno0, insn,
16811                                            distance, BB_HEAD (bb),
16812                                            &found, &redefined);
16813       else
16814         {
16815           int shortest_dist = -1;
16816           bool found_in_bb = false;
16817           bool redefined_in_bb = false;
16818
16819           FOR_EACH_EDGE (e, ei, bb->succs)
16820             {
16821               int bb_dist
16822                 = distance_agu_use_in_bb (regno0, insn,
16823                                           distance, BB_HEAD (e->dest),
16824                                           &found_in_bb, &redefined_in_bb);
16825               if (found_in_bb)
16826                 {
16827                   if (shortest_dist < 0)
16828                     shortest_dist = bb_dist;
16829                   else if (bb_dist > 0)
16830                     shortest_dist = MIN (bb_dist, shortest_dist);
16831
16832                   found = true;
16833                 }
16834             }
16835
16836           distance = shortest_dist;
16837         }
16838     }
16839
16840   if (!found || redefined)
16841     return -1;
16842
16843   return distance >> 1;
16844 }
16845
16846 /* Define this macro to tune LEA priority vs ADD, it take effect when
16847    there is a dilemma of choicing LEA or ADD
16848    Negative value: ADD is more preferred than LEA
16849    Zero: Netrual
16850    Positive value: LEA is more preferred than ADD*/
16851 #define IX86_LEA_PRIORITY 0
16852
16853 /* Return true if usage of lea INSN has performance advantage
16854    over a sequence of instructions.  Instructions sequence has
16855    SPLIT_COST cycles higher latency than lea latency.  */
16856
16857 static bool
16858 ix86_lea_outperforms (rtx insn, unsigned int regno0, unsigned int regno1,
16859                       unsigned int regno2, int split_cost)
16860 {
16861   int dist_define, dist_use;
16862
16863   dist_define = distance_non_agu_define (regno1, regno2, insn);
16864   dist_use = distance_agu_use (regno0, insn);
16865
16866   if (dist_define < 0 || dist_define >= LEA_MAX_STALL)
16867     {
16868       /* If there is no non AGU operand definition, no AGU
16869          operand usage and split cost is 0 then both lea
16870          and non lea variants have same priority.  Currently
16871          we prefer lea for 64 bit code and non lea on 32 bit
16872          code.  */
16873       if (dist_use < 0 && split_cost == 0)
16874         return TARGET_64BIT || IX86_LEA_PRIORITY;
16875       else
16876         return true;
16877     }
16878
16879   /* With longer definitions distance lea is more preferable.
16880      Here we change it to take into account splitting cost and
16881      lea priority.  */
16882   dist_define += split_cost + IX86_LEA_PRIORITY;
16883
16884   /* If there is no use in memory addess then we just check
16885      that split cost does not exceed AGU stall.  */
16886   if (dist_use < 0)
16887     return dist_define >= LEA_MAX_STALL;
16888
16889   /* If this insn has both backward non-agu dependence and forward
16890      agu dependence, the one with short distance takes effect.  */
16891   return dist_define >= dist_use;
16892 }
16893
16894 /* Return true if it is legal to clobber flags by INSN and
16895    false otherwise.  */
16896
16897 static bool
16898 ix86_ok_to_clobber_flags (rtx insn)
16899 {
16900   basic_block bb = BLOCK_FOR_INSN (insn);
16901   df_ref *use;
16902   bitmap live;
16903
16904   while (insn)
16905     {
16906       if (NONDEBUG_INSN_P (insn))
16907         {
16908           for (use = DF_INSN_USES (insn); *use; use++)
16909             if (DF_REF_REG_USE_P (*use) && DF_REF_REGNO (*use) == FLAGS_REG)
16910               return false;
16911
16912           if (insn_defines_reg (FLAGS_REG, INVALID_REGNUM, insn))
16913             return true;
16914         }
16915
16916       if (insn == BB_END (bb))
16917         break;
16918
16919       insn = NEXT_INSN (insn);
16920     }
16921
16922   live = df_get_live_out(bb);
16923   return !REGNO_REG_SET_P (live, FLAGS_REG);
16924 }
16925
16926 /* Return true if we need to split op0 = op1 + op2 into a sequence of
16927    move and add to avoid AGU stalls.  */
16928
16929 bool
16930 ix86_avoid_lea_for_add (rtx insn, rtx operands[])
16931 {
16932   unsigned int regno0 = true_regnum (operands[0]);
16933   unsigned int regno1 = true_regnum (operands[1]);
16934   unsigned int regno2 = true_regnum (operands[2]);
16935
16936   /* Check if we need to optimize.  */
16937   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16938     return false;
16939
16940   /* Check it is correct to split here.  */
16941   if (!ix86_ok_to_clobber_flags(insn))
16942     return false;
16943
16944   /* We need to split only adds with non destructive
16945      destination operand.  */
16946   if (regno0 == regno1 || regno0 == regno2)
16947     return false;
16948   else
16949     return !ix86_lea_outperforms (insn, regno0, regno1, regno2, 1);
16950 }
16951
16952 /* Return true if we should emit lea instruction instead of mov
16953    instruction.  */
16954
16955 bool
16956 ix86_use_lea_for_mov (rtx insn, rtx operands[])
16957 {
16958   unsigned int regno0;
16959   unsigned int regno1;
16960
16961   /* Check if we need to optimize.  */
16962   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16963     return false;
16964
16965   /* Use lea for reg to reg moves only.  */
16966   if (!REG_P (operands[0]) || !REG_P (operands[1]))
16967     return false;
16968
16969   regno0 = true_regnum (operands[0]);
16970   regno1 = true_regnum (operands[1]);
16971
16972   return ix86_lea_outperforms (insn, regno0, regno1, INVALID_REGNUM, 0);
16973 }
16974
16975 /* Return true if we need to split lea into a sequence of
16976    instructions to avoid AGU stalls. */
16977
16978 bool
16979 ix86_avoid_lea_for_addr (rtx insn, rtx operands[])
16980 {
16981   unsigned int regno0 = true_regnum (operands[0]) ;
16982   unsigned int regno1 = INVALID_REGNUM;
16983   unsigned int regno2 = INVALID_REGNUM;
16984   int split_cost = 0;
16985   struct ix86_address parts;
16986   int ok;
16987
16988   /* FIXME: Handle zero-extended addresses.  */
16989   if (GET_CODE (operands[1]) == ZERO_EXTEND
16990       || GET_CODE (operands[1]) == AND)
16991     return false;
16992
16993   /* Check we need to optimize.  */
16994   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16995     return false;
16996
16997   /* Check it is correct to split here.  */
16998   if (!ix86_ok_to_clobber_flags(insn))
16999     return false;
17000
17001   ok = ix86_decompose_address (operands[1], &parts);
17002   gcc_assert (ok);
17003
17004   /* There should be at least two components in the address.  */
17005   if ((parts.base != NULL_RTX) + (parts.index != NULL_RTX)
17006       + (parts.disp != NULL_RTX) + (parts.scale > 1) < 2)
17007     return false;
17008
17009   /* We should not split into add if non legitimate pic
17010      operand is used as displacement. */
17011   if (parts.disp && flag_pic && !LEGITIMATE_PIC_OPERAND_P (parts.disp))
17012     return false;
17013
17014   if (parts.base)
17015     regno1 = true_regnum (parts.base);
17016   if (parts.index)
17017     regno2 = true_regnum (parts.index);
17018
17019   /* Compute how many cycles we will add to execution time
17020      if split lea into a sequence of instructions.  */
17021   if (parts.base || parts.index)
17022     {
17023       /* Have to use mov instruction if non desctructive
17024          destination form is used.  */
17025       if (regno1 != regno0 && regno2 != regno0)
17026         split_cost += 1;
17027
17028       /* Have to add index to base if both exist.  */
17029       if (parts.base && parts.index)
17030         split_cost += 1;
17031
17032       /* Have to use shift and adds if scale is 2 or greater.  */
17033       if (parts.scale > 1)
17034         {
17035           if (regno0 != regno1)
17036             split_cost += 1;
17037           else if (regno2 == regno0)
17038             split_cost += 4;
17039           else
17040             split_cost += parts.scale;
17041         }
17042
17043       /* Have to use add instruction with immediate if
17044          disp is non zero.  */
17045       if (parts.disp && parts.disp != const0_rtx)
17046         split_cost += 1;
17047
17048       /* Subtract the price of lea.  */
17049       split_cost -= 1;
17050     }
17051
17052   return !ix86_lea_outperforms (insn, regno0, regno1, regno2, split_cost);
17053 }
17054
17055 /* Emit x86 binary operand CODE in mode MODE, where the first operand
17056    matches destination.  RTX includes clobber of FLAGS_REG.  */
17057
17058 static void
17059 ix86_emit_binop (enum rtx_code code, enum machine_mode mode,
17060                  rtx dst, rtx src)
17061 {
17062   rtx op, clob;
17063
17064   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, dst, src));
17065   clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
17066   
17067   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
17068 }
17069
17070 /* Split lea instructions into a sequence of instructions
17071    which are executed on ALU to avoid AGU stalls.
17072    It is assumed that it is allowed to clobber flags register
17073    at lea position.  */
17074
17075 extern void
17076 ix86_split_lea_for_addr (rtx operands[], enum machine_mode mode)
17077 {
17078   unsigned int regno0 = true_regnum (operands[0]) ;
17079   unsigned int regno1 = INVALID_REGNUM;
17080   unsigned int regno2 = INVALID_REGNUM;
17081   struct ix86_address parts;
17082   rtx tmp;
17083   int ok, adds;
17084
17085   ok = ix86_decompose_address (operands[1], &parts);
17086   gcc_assert (ok);
17087
17088   if (parts.base)
17089     {
17090       if (GET_MODE (parts.base) != mode)
17091         parts.base = gen_rtx_SUBREG (mode, parts.base, 0);
17092       regno1 = true_regnum (parts.base);
17093     }
17094
17095   if (parts.index)
17096     {
17097       if (GET_MODE (parts.index) != mode)
17098         parts.index = gen_rtx_SUBREG (mode, parts.index, 0);
17099       regno2 = true_regnum (parts.index);
17100     }
17101
17102   if (parts.scale > 1)
17103     {
17104       /* Case r1 = r1 + ...  */
17105       if (regno1 == regno0)
17106         {
17107           /* If we have a case r1 = r1 + C * r1 then we
17108              should use multiplication which is very
17109              expensive.  Assume cost model is wrong if we
17110              have such case here.  */
17111           gcc_assert (regno2 != regno0);
17112
17113           for (adds = parts.scale; adds > 0; adds--)
17114             ix86_emit_binop (PLUS, mode, operands[0], parts.index);
17115         }
17116       else
17117         {
17118           /* r1 = r2 + r3 * C case.  Need to move r3 into r1.  */
17119           if (regno0 != regno2)
17120             emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.index));
17121
17122           /* Use shift for scaling.  */
17123           ix86_emit_binop (ASHIFT, mode, operands[0],
17124                            GEN_INT (exact_log2 (parts.scale)));
17125
17126           if (parts.base)
17127             ix86_emit_binop (PLUS, mode, operands[0], parts.base);
17128
17129           if (parts.disp && parts.disp != const0_rtx)
17130             ix86_emit_binop (PLUS, mode, operands[0], parts.disp);
17131         }
17132     }
17133   else if (!parts.base && !parts.index)
17134     {
17135       gcc_assert(parts.disp);
17136       emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.disp));
17137     }
17138   else
17139     {
17140       if (!parts.base)
17141         {
17142           if (regno0 != regno2)
17143             emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.index));
17144         }
17145       else if (!parts.index)
17146         {
17147           if (regno0 != regno1)
17148             emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.base));
17149         }
17150       else
17151         {
17152           if (regno0 == regno1)
17153             tmp = parts.index;
17154           else if (regno0 == regno2)
17155             tmp = parts.base;
17156           else
17157             {
17158               emit_insn (gen_rtx_SET (VOIDmode, operands[0], parts.base));
17159               tmp = parts.index;
17160             }
17161
17162           ix86_emit_binop (PLUS, mode, operands[0], tmp);
17163         }
17164
17165       if (parts.disp && parts.disp != const0_rtx)
17166         ix86_emit_binop (PLUS, mode, operands[0], parts.disp);
17167     }
17168 }
17169
17170 /* Return true if it is ok to optimize an ADD operation to LEA
17171    operation to avoid flag register consumation.  For most processors,
17172    ADD is faster than LEA.  For the processors like ATOM, if the
17173    destination register of LEA holds an actual address which will be
17174    used soon, LEA is better and otherwise ADD is better.  */
17175
17176 bool
17177 ix86_lea_for_add_ok (rtx insn, rtx operands[])
17178 {
17179   unsigned int regno0 = true_regnum (operands[0]);
17180   unsigned int regno1 = true_regnum (operands[1]);
17181   unsigned int regno2 = true_regnum (operands[2]);
17182
17183   /* If a = b + c, (a!=b && a!=c), must use lea form. */
17184   if (regno0 != regno1 && regno0 != regno2)
17185     return true;
17186
17187   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
17188     return false;
17189
17190   return ix86_lea_outperforms (insn, regno0, regno1, regno2, 0);
17191 }
17192
17193 /* Return true if destination reg of SET_BODY is shift count of
17194    USE_BODY.  */
17195
17196 static bool
17197 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
17198 {
17199   rtx set_dest;
17200   rtx shift_rtx;
17201   int i;
17202
17203   /* Retrieve destination of SET_BODY.  */
17204   switch (GET_CODE (set_body))
17205     {
17206     case SET:
17207       set_dest = SET_DEST (set_body);
17208       if (!set_dest || !REG_P (set_dest))
17209         return false;
17210       break;
17211     case PARALLEL:
17212       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
17213         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
17214                                           use_body))
17215           return true;
17216     default:
17217       return false;
17218       break;
17219     }
17220
17221   /* Retrieve shift count of USE_BODY.  */
17222   switch (GET_CODE (use_body))
17223     {
17224     case SET:
17225       shift_rtx = XEXP (use_body, 1);
17226       break;
17227     case PARALLEL:
17228       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
17229         if (ix86_dep_by_shift_count_body (set_body,
17230                                           XVECEXP (use_body, 0, i)))
17231           return true;
17232     default:
17233       return false;
17234       break;
17235     }
17236
17237   if (shift_rtx
17238       && (GET_CODE (shift_rtx) == ASHIFT
17239           || GET_CODE (shift_rtx) == LSHIFTRT
17240           || GET_CODE (shift_rtx) == ASHIFTRT
17241           || GET_CODE (shift_rtx) == ROTATE
17242           || GET_CODE (shift_rtx) == ROTATERT))
17243     {
17244       rtx shift_count = XEXP (shift_rtx, 1);
17245
17246       /* Return true if shift count is dest of SET_BODY.  */
17247       if (REG_P (shift_count)
17248           && true_regnum (set_dest) == true_regnum (shift_count))
17249         return true;
17250     }
17251
17252   return false;
17253 }
17254
17255 /* Return true if destination reg of SET_INSN is shift count of
17256    USE_INSN.  */
17257
17258 bool
17259 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
17260 {
17261   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
17262                                        PATTERN (use_insn));
17263 }
17264
17265 /* Return TRUE or FALSE depending on whether the unary operator meets the
17266    appropriate constraints.  */
17267
17268 bool
17269 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
17270                         enum machine_mode mode ATTRIBUTE_UNUSED,
17271                         rtx operands[2] ATTRIBUTE_UNUSED)
17272 {
17273   /* If one of operands is memory, source and destination must match.  */
17274   if ((MEM_P (operands[0])
17275        || MEM_P (operands[1]))
17276       && ! rtx_equal_p (operands[0], operands[1]))
17277     return false;
17278   return true;
17279 }
17280
17281 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
17282    are ok, keeping in mind the possible movddup alternative.  */
17283
17284 bool
17285 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
17286 {
17287   if (MEM_P (operands[0]))
17288     return rtx_equal_p (operands[0], operands[1 + high]);
17289   if (MEM_P (operands[1]) && MEM_P (operands[2]))
17290     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
17291   return true;
17292 }
17293
17294 /* Post-reload splitter for converting an SF or DFmode value in an
17295    SSE register into an unsigned SImode.  */
17296
17297 void
17298 ix86_split_convert_uns_si_sse (rtx operands[])
17299 {
17300   enum machine_mode vecmode;
17301   rtx value, large, zero_or_two31, input, two31, x;
17302
17303   large = operands[1];
17304   zero_or_two31 = operands[2];
17305   input = operands[3];
17306   two31 = operands[4];
17307   vecmode = GET_MODE (large);
17308   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
17309
17310   /* Load up the value into the low element.  We must ensure that the other
17311      elements are valid floats -- zero is the easiest such value.  */
17312   if (MEM_P (input))
17313     {
17314       if (vecmode == V4SFmode)
17315         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
17316       else
17317         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
17318     }
17319   else
17320     {
17321       input = gen_rtx_REG (vecmode, REGNO (input));
17322       emit_move_insn (value, CONST0_RTX (vecmode));
17323       if (vecmode == V4SFmode)
17324         emit_insn (gen_sse_movss (value, value, input));
17325       else
17326         emit_insn (gen_sse2_movsd (value, value, input));
17327     }
17328
17329   emit_move_insn (large, two31);
17330   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
17331
17332   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
17333   emit_insn (gen_rtx_SET (VOIDmode, large, x));
17334
17335   x = gen_rtx_AND (vecmode, zero_or_two31, large);
17336   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
17337
17338   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
17339   emit_insn (gen_rtx_SET (VOIDmode, value, x));
17340
17341   large = gen_rtx_REG (V4SImode, REGNO (large));
17342   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
17343
17344   x = gen_rtx_REG (V4SImode, REGNO (value));
17345   if (vecmode == V4SFmode)
17346     emit_insn (gen_fix_truncv4sfv4si2 (x, value));
17347   else
17348     emit_insn (gen_sse2_cvttpd2dq (x, value));
17349   value = x;
17350
17351   emit_insn (gen_xorv4si3 (value, value, large));
17352 }
17353
17354 /* Convert an unsigned DImode value into a DFmode, using only SSE.
17355    Expects the 64-bit DImode to be supplied in a pair of integral
17356    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
17357    -mfpmath=sse, !optimize_size only.  */
17358
17359 void
17360 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
17361 {
17362   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
17363   rtx int_xmm, fp_xmm;
17364   rtx biases, exponents;
17365   rtx x;
17366
17367   int_xmm = gen_reg_rtx (V4SImode);
17368   if (TARGET_INTER_UNIT_MOVES)
17369     emit_insn (gen_movdi_to_sse (int_xmm, input));
17370   else if (TARGET_SSE_SPLIT_REGS)
17371     {
17372       emit_clobber (int_xmm);
17373       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
17374     }
17375   else
17376     {
17377       x = gen_reg_rtx (V2DImode);
17378       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
17379       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
17380     }
17381
17382   x = gen_rtx_CONST_VECTOR (V4SImode,
17383                             gen_rtvec (4, GEN_INT (0x43300000UL),
17384                                        GEN_INT (0x45300000UL),
17385                                        const0_rtx, const0_rtx));
17386   exponents = validize_mem (force_const_mem (V4SImode, x));
17387
17388   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
17389   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
17390
17391   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
17392      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
17393      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
17394      (0x1.0p84 + double(fp_value_hi_xmm)).
17395      Note these exponents differ by 32.  */
17396
17397   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
17398
17399   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
17400      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
17401   real_ldexp (&bias_lo_rvt, &dconst1, 52);
17402   real_ldexp (&bias_hi_rvt, &dconst1, 84);
17403   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
17404   x = const_double_from_real_value (bias_hi_rvt, DFmode);
17405   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
17406   biases = validize_mem (force_const_mem (V2DFmode, biases));
17407   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
17408
17409   /* Add the upper and lower DFmode values together.  */
17410   if (TARGET_SSE3)
17411     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
17412   else
17413     {
17414       x = copy_to_mode_reg (V2DFmode, fp_xmm);
17415       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
17416       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
17417     }
17418
17419   ix86_expand_vector_extract (false, target, fp_xmm, 0);
17420 }
17421
17422 /* Not used, but eases macroization of patterns.  */
17423 void
17424 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
17425                                   rtx input ATTRIBUTE_UNUSED)
17426 {
17427   gcc_unreachable ();
17428 }
17429
17430 /* Convert an unsigned SImode value into a DFmode.  Only currently used
17431    for SSE, but applicable anywhere.  */
17432
17433 void
17434 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
17435 {
17436   REAL_VALUE_TYPE TWO31r;
17437   rtx x, fp;
17438
17439   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
17440                            NULL, 1, OPTAB_DIRECT);
17441
17442   fp = gen_reg_rtx (DFmode);
17443   emit_insn (gen_floatsidf2 (fp, x));
17444
17445   real_ldexp (&TWO31r, &dconst1, 31);
17446   x = const_double_from_real_value (TWO31r, DFmode);
17447
17448   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
17449   if (x != target)
17450     emit_move_insn (target, x);
17451 }
17452
17453 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
17454    32-bit mode; otherwise we have a direct convert instruction.  */
17455
17456 void
17457 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
17458 {
17459   REAL_VALUE_TYPE TWO32r;
17460   rtx fp_lo, fp_hi, x;
17461
17462   fp_lo = gen_reg_rtx (DFmode);
17463   fp_hi = gen_reg_rtx (DFmode);
17464
17465   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
17466
17467   real_ldexp (&TWO32r, &dconst1, 32);
17468   x = const_double_from_real_value (TWO32r, DFmode);
17469   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
17470
17471   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
17472
17473   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
17474                            0, OPTAB_DIRECT);
17475   if (x != target)
17476     emit_move_insn (target, x);
17477 }
17478
17479 /* Convert an unsigned SImode value into a SFmode, using only SSE.
17480    For x86_32, -mfpmath=sse, !optimize_size only.  */
17481 void
17482 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
17483 {
17484   REAL_VALUE_TYPE ONE16r;
17485   rtx fp_hi, fp_lo, int_hi, int_lo, x;
17486
17487   real_ldexp (&ONE16r, &dconst1, 16);
17488   x = const_double_from_real_value (ONE16r, SFmode);
17489   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
17490                                       NULL, 0, OPTAB_DIRECT);
17491   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
17492                                       NULL, 0, OPTAB_DIRECT);
17493   fp_hi = gen_reg_rtx (SFmode);
17494   fp_lo = gen_reg_rtx (SFmode);
17495   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
17496   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
17497   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
17498                                0, OPTAB_DIRECT);
17499   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
17500                                0, OPTAB_DIRECT);
17501   if (!rtx_equal_p (target, fp_hi))
17502     emit_move_insn (target, fp_hi);
17503 }
17504
17505 /* floatunsv{4,8}siv{4,8}sf2 expander.  Expand code to convert
17506    a vector of unsigned ints VAL to vector of floats TARGET.  */
17507
17508 void
17509 ix86_expand_vector_convert_uns_vsivsf (rtx target, rtx val)
17510 {
17511   rtx tmp[8];
17512   REAL_VALUE_TYPE TWO16r;
17513   enum machine_mode intmode = GET_MODE (val);
17514   enum machine_mode fltmode = GET_MODE (target);
17515   rtx (*cvt) (rtx, rtx);
17516
17517   if (intmode == V4SImode)
17518     cvt = gen_floatv4siv4sf2;
17519   else
17520     cvt = gen_floatv8siv8sf2;
17521   tmp[0] = ix86_build_const_vector (intmode, 1, GEN_INT (0xffff));
17522   tmp[0] = force_reg (intmode, tmp[0]);
17523   tmp[1] = expand_simple_binop (intmode, AND, val, tmp[0], NULL_RTX, 1,
17524                                 OPTAB_DIRECT);
17525   tmp[2] = expand_simple_binop (intmode, LSHIFTRT, val, GEN_INT (16),
17526                                 NULL_RTX, 1, OPTAB_DIRECT);
17527   tmp[3] = gen_reg_rtx (fltmode);
17528   emit_insn (cvt (tmp[3], tmp[1]));
17529   tmp[4] = gen_reg_rtx (fltmode);
17530   emit_insn (cvt (tmp[4], tmp[2]));
17531   real_ldexp (&TWO16r, &dconst1, 16);
17532   tmp[5] = const_double_from_real_value (TWO16r, SFmode);
17533   tmp[5] = force_reg (fltmode, ix86_build_const_vector (fltmode, 1, tmp[5]));
17534   tmp[6] = expand_simple_binop (fltmode, MULT, tmp[4], tmp[5], NULL_RTX, 1,
17535                                 OPTAB_DIRECT);
17536   tmp[7] = expand_simple_binop (fltmode, PLUS, tmp[3], tmp[6], target, 1,
17537                                 OPTAB_DIRECT);
17538   if (tmp[7] != target)
17539     emit_move_insn (target, tmp[7]);
17540 }
17541
17542 /* Adjust a V*SFmode/V*DFmode value VAL so that *sfix_trunc* resp. fix_trunc*
17543    pattern can be used on it instead of *ufix_trunc* resp. fixuns_trunc*.
17544    This is done by doing just signed conversion if < 0x1p31, and otherwise by
17545    subtracting 0x1p31 first and xoring in 0x80000000 from *XORP afterwards.  */
17546
17547 rtx
17548 ix86_expand_adjust_ufix_to_sfix_si (rtx val, rtx *xorp)
17549 {
17550   REAL_VALUE_TYPE TWO31r;
17551   rtx two31r, tmp[4];
17552   enum machine_mode mode = GET_MODE (val);
17553   enum machine_mode scalarmode = GET_MODE_INNER (mode);
17554   enum machine_mode intmode = GET_MODE_SIZE (mode) == 32 ? V8SImode : V4SImode;
17555   rtx (*cmp) (rtx, rtx, rtx, rtx);
17556   int i;
17557
17558   for (i = 0; i < 3; i++)
17559     tmp[i] = gen_reg_rtx (mode);
17560   real_ldexp (&TWO31r, &dconst1, 31);
17561   two31r = const_double_from_real_value (TWO31r, scalarmode);
17562   two31r = ix86_build_const_vector (mode, 1, two31r);
17563   two31r = force_reg (mode, two31r);
17564   switch (mode)
17565     {
17566     case V8SFmode: cmp = gen_avx_maskcmpv8sf3; break;
17567     case V4SFmode: cmp = gen_sse_maskcmpv4sf3; break;
17568     case V4DFmode: cmp = gen_avx_maskcmpv4df3; break;
17569     case V2DFmode: cmp = gen_sse2_maskcmpv2df3; break;
17570     default: gcc_unreachable ();
17571     }
17572   tmp[3] = gen_rtx_LE (mode, two31r, val);
17573   emit_insn (cmp (tmp[0], two31r, val, tmp[3]));
17574   tmp[1] = expand_simple_binop (mode, AND, tmp[0], two31r, tmp[1],
17575                                 0, OPTAB_DIRECT);
17576   if (intmode == V4SImode || TARGET_AVX2)
17577     *xorp = expand_simple_binop (intmode, ASHIFT,
17578                                  gen_lowpart (intmode, tmp[0]),
17579                                  GEN_INT (31), NULL_RTX, 0,
17580                                  OPTAB_DIRECT);
17581   else
17582     {
17583       rtx two31 = GEN_INT ((unsigned HOST_WIDE_INT) 1 << 31);
17584       two31 = ix86_build_const_vector (intmode, 1, two31);
17585       *xorp = expand_simple_binop (intmode, AND,
17586                                    gen_lowpart (intmode, tmp[0]),
17587                                    two31, NULL_RTX, 0,
17588                                    OPTAB_DIRECT);
17589     }
17590   return expand_simple_binop (mode, MINUS, val, tmp[1], tmp[2],
17591                               0, OPTAB_DIRECT);
17592 }
17593
17594 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
17595    then replicate the value for all elements of the vector
17596    register.  */
17597
17598 rtx
17599 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
17600 {
17601   int i, n_elt;
17602   rtvec v;
17603   enum machine_mode scalar_mode;
17604
17605   switch (mode)
17606     {
17607     case V32QImode:
17608     case V16QImode:
17609     case V16HImode:
17610     case V8HImode:
17611     case V8SImode:
17612     case V4SImode:
17613     case V4DImode:
17614     case V2DImode:
17615       gcc_assert (vect);
17616     case V8SFmode:
17617     case V4SFmode:
17618     case V4DFmode:
17619     case V2DFmode:
17620       n_elt = GET_MODE_NUNITS (mode);
17621       v = rtvec_alloc (n_elt);
17622       scalar_mode = GET_MODE_INNER (mode);
17623
17624       RTVEC_ELT (v, 0) = value;
17625
17626       for (i = 1; i < n_elt; ++i)
17627         RTVEC_ELT (v, i) = vect ? value : CONST0_RTX (scalar_mode);
17628
17629       return gen_rtx_CONST_VECTOR (mode, v);
17630
17631     default:
17632       gcc_unreachable ();
17633     }
17634 }
17635
17636 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
17637    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
17638    for an SSE register.  If VECT is true, then replicate the mask for
17639    all elements of the vector register.  If INVERT is true, then create
17640    a mask excluding the sign bit.  */
17641
17642 rtx
17643 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
17644 {
17645   enum machine_mode vec_mode, imode;
17646   HOST_WIDE_INT hi, lo;
17647   int shift = 63;
17648   rtx v;
17649   rtx mask;
17650
17651   /* Find the sign bit, sign extended to 2*HWI.  */
17652   switch (mode)
17653     {
17654     case V8SImode:
17655     case V4SImode:
17656     case V8SFmode:
17657     case V4SFmode:
17658       vec_mode = mode;
17659       mode = GET_MODE_INNER (mode);
17660       imode = SImode;
17661       lo = 0x80000000, hi = lo < 0;
17662       break;
17663
17664     case V4DImode:
17665     case V2DImode:
17666     case V4DFmode:
17667     case V2DFmode:
17668       vec_mode = mode;
17669       mode = GET_MODE_INNER (mode);
17670       imode = DImode;
17671       if (HOST_BITS_PER_WIDE_INT >= 64)
17672         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
17673       else
17674         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
17675       break;
17676
17677     case TImode:
17678     case TFmode:
17679       vec_mode = VOIDmode;
17680       if (HOST_BITS_PER_WIDE_INT >= 64)
17681         {
17682           imode = TImode;
17683           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
17684         }
17685       else
17686         {
17687           rtvec vec;
17688
17689           imode = DImode;
17690           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
17691
17692           if (invert)
17693             {
17694               lo = ~lo, hi = ~hi;
17695               v = constm1_rtx;
17696             }
17697           else
17698             v = const0_rtx;
17699
17700           mask = immed_double_const (lo, hi, imode);
17701
17702           vec = gen_rtvec (2, v, mask);
17703           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
17704           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
17705
17706           return v;
17707         }
17708      break;
17709
17710     default:
17711       gcc_unreachable ();
17712     }
17713
17714   if (invert)
17715     lo = ~lo, hi = ~hi;
17716
17717   /* Force this value into the low part of a fp vector constant.  */
17718   mask = immed_double_const (lo, hi, imode);
17719   mask = gen_lowpart (mode, mask);
17720
17721   if (vec_mode == VOIDmode)
17722     return force_reg (mode, mask);
17723
17724   v = ix86_build_const_vector (vec_mode, vect, mask);
17725   return force_reg (vec_mode, v);
17726 }
17727
17728 /* Generate code for floating point ABS or NEG.  */
17729
17730 void
17731 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
17732                                 rtx operands[])
17733 {
17734   rtx mask, set, dst, src;
17735   bool use_sse = false;
17736   bool vector_mode = VECTOR_MODE_P (mode);
17737   enum machine_mode vmode = mode;
17738
17739   if (vector_mode)
17740     use_sse = true;
17741   else if (mode == TFmode)
17742     use_sse = true;
17743   else if (TARGET_SSE_MATH)
17744     {
17745       use_sse = SSE_FLOAT_MODE_P (mode);
17746       if (mode == SFmode)
17747         vmode = V4SFmode;
17748       else if (mode == DFmode)
17749         vmode = V2DFmode;
17750     }
17751
17752   /* NEG and ABS performed with SSE use bitwise mask operations.
17753      Create the appropriate mask now.  */
17754   if (use_sse)
17755     mask = ix86_build_signbit_mask (vmode, vector_mode, code == ABS);
17756   else
17757     mask = NULL_RTX;
17758
17759   dst = operands[0];
17760   src = operands[1];
17761
17762   set = gen_rtx_fmt_e (code, mode, src);
17763   set = gen_rtx_SET (VOIDmode, dst, set);
17764
17765   if (mask)
17766     {
17767       rtx use, clob;
17768       rtvec par;
17769
17770       use = gen_rtx_USE (VOIDmode, mask);
17771       if (vector_mode)
17772         par = gen_rtvec (2, set, use);
17773       else
17774         {
17775           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
17776           par = gen_rtvec (3, set, use, clob);
17777         }
17778       emit_insn (gen_rtx_PARALLEL (VOIDmode, par));
17779     }
17780   else
17781     emit_insn (set);
17782 }
17783
17784 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
17785
17786 void
17787 ix86_expand_copysign (rtx operands[])
17788 {
17789   enum machine_mode mode, vmode;
17790   rtx dest, op0, op1, mask, nmask;
17791
17792   dest = operands[0];
17793   op0 = operands[1];
17794   op1 = operands[2];
17795
17796   mode = GET_MODE (dest);
17797
17798   if (mode == SFmode)
17799     vmode = V4SFmode;
17800   else if (mode == DFmode)
17801     vmode = V2DFmode;
17802   else
17803     vmode = mode;
17804
17805   if (GET_CODE (op0) == CONST_DOUBLE)
17806     {
17807       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
17808
17809       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
17810         op0 = simplify_unary_operation (ABS, mode, op0, mode);
17811
17812       if (mode == SFmode || mode == DFmode)
17813         {
17814           if (op0 == CONST0_RTX (mode))
17815             op0 = CONST0_RTX (vmode);
17816           else
17817             {
17818               rtx v = ix86_build_const_vector (vmode, false, op0);
17819
17820               op0 = force_reg (vmode, v);
17821             }
17822         }
17823       else if (op0 != CONST0_RTX (mode))
17824         op0 = force_reg (mode, op0);
17825
17826       mask = ix86_build_signbit_mask (vmode, 0, 0);
17827
17828       if (mode == SFmode)
17829         copysign_insn = gen_copysignsf3_const;
17830       else if (mode == DFmode)
17831         copysign_insn = gen_copysigndf3_const;
17832       else
17833         copysign_insn = gen_copysigntf3_const;
17834
17835         emit_insn (copysign_insn (dest, op0, op1, mask));
17836     }
17837   else
17838     {
17839       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
17840
17841       nmask = ix86_build_signbit_mask (vmode, 0, 1);
17842       mask = ix86_build_signbit_mask (vmode, 0, 0);
17843
17844       if (mode == SFmode)
17845         copysign_insn = gen_copysignsf3_var;
17846       else if (mode == DFmode)
17847         copysign_insn = gen_copysigndf3_var;
17848       else
17849         copysign_insn = gen_copysigntf3_var;
17850
17851       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
17852     }
17853 }
17854
17855 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
17856    be a constant, and so has already been expanded into a vector constant.  */
17857
17858 void
17859 ix86_split_copysign_const (rtx operands[])
17860 {
17861   enum machine_mode mode, vmode;
17862   rtx dest, op0, mask, x;
17863
17864   dest = operands[0];
17865   op0 = operands[1];
17866   mask = operands[3];
17867
17868   mode = GET_MODE (dest);
17869   vmode = GET_MODE (mask);
17870
17871   dest = simplify_gen_subreg (vmode, dest, mode, 0);
17872   x = gen_rtx_AND (vmode, dest, mask);
17873   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17874
17875   if (op0 != CONST0_RTX (vmode))
17876     {
17877       x = gen_rtx_IOR (vmode, dest, op0);
17878       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17879     }
17880 }
17881
17882 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
17883    so we have to do two masks.  */
17884
17885 void
17886 ix86_split_copysign_var (rtx operands[])
17887 {
17888   enum machine_mode mode, vmode;
17889   rtx dest, scratch, op0, op1, mask, nmask, x;
17890
17891   dest = operands[0];
17892   scratch = operands[1];
17893   op0 = operands[2];
17894   op1 = operands[3];
17895   nmask = operands[4];
17896   mask = operands[5];
17897
17898   mode = GET_MODE (dest);
17899   vmode = GET_MODE (mask);
17900
17901   if (rtx_equal_p (op0, op1))
17902     {
17903       /* Shouldn't happen often (it's useless, obviously), but when it does
17904          we'd generate incorrect code if we continue below.  */
17905       emit_move_insn (dest, op0);
17906       return;
17907     }
17908
17909   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
17910     {
17911       gcc_assert (REGNO (op1) == REGNO (scratch));
17912
17913       x = gen_rtx_AND (vmode, scratch, mask);
17914       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
17915
17916       dest = mask;
17917       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
17918       x = gen_rtx_NOT (vmode, dest);
17919       x = gen_rtx_AND (vmode, x, op0);
17920       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17921     }
17922   else
17923     {
17924       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
17925         {
17926           x = gen_rtx_AND (vmode, scratch, mask);
17927         }
17928       else                                              /* alternative 2,4 */
17929         {
17930           gcc_assert (REGNO (mask) == REGNO (scratch));
17931           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
17932           x = gen_rtx_AND (vmode, scratch, op1);
17933         }
17934       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
17935
17936       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
17937         {
17938           dest = simplify_gen_subreg (vmode, op0, mode, 0);
17939           x = gen_rtx_AND (vmode, dest, nmask);
17940         }
17941       else                                              /* alternative 3,4 */
17942         {
17943           gcc_assert (REGNO (nmask) == REGNO (dest));
17944           dest = nmask;
17945           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
17946           x = gen_rtx_AND (vmode, dest, op0);
17947         }
17948       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17949     }
17950
17951   x = gen_rtx_IOR (vmode, dest, scratch);
17952   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17953 }
17954
17955 /* Return TRUE or FALSE depending on whether the first SET in INSN
17956    has source and destination with matching CC modes, and that the
17957    CC mode is at least as constrained as REQ_MODE.  */
17958
17959 bool
17960 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
17961 {
17962   rtx set;
17963   enum machine_mode set_mode;
17964
17965   set = PATTERN (insn);
17966   if (GET_CODE (set) == PARALLEL)
17967     set = XVECEXP (set, 0, 0);
17968   gcc_assert (GET_CODE (set) == SET);
17969   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
17970
17971   set_mode = GET_MODE (SET_DEST (set));
17972   switch (set_mode)
17973     {
17974     case CCNOmode:
17975       if (req_mode != CCNOmode
17976           && (req_mode != CCmode
17977               || XEXP (SET_SRC (set), 1) != const0_rtx))
17978         return false;
17979       break;
17980     case CCmode:
17981       if (req_mode == CCGCmode)
17982         return false;
17983       /* FALLTHRU */
17984     case CCGCmode:
17985       if (req_mode == CCGOCmode || req_mode == CCNOmode)
17986         return false;
17987       /* FALLTHRU */
17988     case CCGOCmode:
17989       if (req_mode == CCZmode)
17990         return false;
17991       /* FALLTHRU */
17992     case CCZmode:
17993       break;
17994
17995     case CCAmode:
17996     case CCCmode:
17997     case CCOmode:
17998     case CCSmode:
17999       if (set_mode != req_mode)
18000         return false;
18001       break;
18002
18003     default:
18004       gcc_unreachable ();
18005     }
18006
18007   return GET_MODE (SET_SRC (set)) == set_mode;
18008 }
18009
18010 /* Generate insn patterns to do an integer compare of OPERANDS.  */
18011
18012 static rtx
18013 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
18014 {
18015   enum machine_mode cmpmode;
18016   rtx tmp, flags;
18017
18018   cmpmode = SELECT_CC_MODE (code, op0, op1);
18019   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
18020
18021   /* This is very simple, but making the interface the same as in the
18022      FP case makes the rest of the code easier.  */
18023   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
18024   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
18025
18026   /* Return the test that should be put into the flags user, i.e.
18027      the bcc, scc, or cmov instruction.  */
18028   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
18029 }
18030
18031 /* Figure out whether to use ordered or unordered fp comparisons.
18032    Return the appropriate mode to use.  */
18033
18034 enum machine_mode
18035 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
18036 {
18037   /* ??? In order to make all comparisons reversible, we do all comparisons
18038      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
18039      all forms trapping and nontrapping comparisons, we can make inequality
18040      comparisons trapping again, since it results in better code when using
18041      FCOM based compares.  */
18042   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
18043 }
18044
18045 enum machine_mode
18046 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
18047 {
18048   enum machine_mode mode = GET_MODE (op0);
18049
18050   if (SCALAR_FLOAT_MODE_P (mode))
18051     {
18052       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
18053       return ix86_fp_compare_mode (code);
18054     }
18055
18056   switch (code)
18057     {
18058       /* Only zero flag is needed.  */
18059     case EQ:                    /* ZF=0 */
18060     case NE:                    /* ZF!=0 */
18061       return CCZmode;
18062       /* Codes needing carry flag.  */
18063     case GEU:                   /* CF=0 */
18064     case LTU:                   /* CF=1 */
18065       /* Detect overflow checks.  They need just the carry flag.  */
18066       if (GET_CODE (op0) == PLUS
18067           && rtx_equal_p (op1, XEXP (op0, 0)))
18068         return CCCmode;
18069       else
18070         return CCmode;
18071     case GTU:                   /* CF=0 & ZF=0 */
18072     case LEU:                   /* CF=1 | ZF=1 */
18073       return CCmode;
18074       /* Codes possibly doable only with sign flag when
18075          comparing against zero.  */
18076     case GE:                    /* SF=OF   or   SF=0 */
18077     case LT:                    /* SF<>OF  or   SF=1 */
18078       if (op1 == const0_rtx)
18079         return CCGOCmode;
18080       else
18081         /* For other cases Carry flag is not required.  */
18082         return CCGCmode;
18083       /* Codes doable only with sign flag when comparing
18084          against zero, but we miss jump instruction for it
18085          so we need to use relational tests against overflow
18086          that thus needs to be zero.  */
18087     case GT:                    /* ZF=0 & SF=OF */
18088     case LE:                    /* ZF=1 | SF<>OF */
18089       if (op1 == const0_rtx)
18090         return CCNOmode;
18091       else
18092         return CCGCmode;
18093       /* strcmp pattern do (use flags) and combine may ask us for proper
18094          mode.  */
18095     case USE:
18096       return CCmode;
18097     default:
18098       gcc_unreachable ();
18099     }
18100 }
18101
18102 /* Return the fixed registers used for condition codes.  */
18103
18104 static bool
18105 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
18106 {
18107   *p1 = FLAGS_REG;
18108   *p2 = FPSR_REG;
18109   return true;
18110 }
18111
18112 /* If two condition code modes are compatible, return a condition code
18113    mode which is compatible with both.  Otherwise, return
18114    VOIDmode.  */
18115
18116 static enum machine_mode
18117 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
18118 {
18119   if (m1 == m2)
18120     return m1;
18121
18122   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
18123     return VOIDmode;
18124
18125   if ((m1 == CCGCmode && m2 == CCGOCmode)
18126       || (m1 == CCGOCmode && m2 == CCGCmode))
18127     return CCGCmode;
18128
18129   switch (m1)
18130     {
18131     default:
18132       gcc_unreachable ();
18133
18134     case CCmode:
18135     case CCGCmode:
18136     case CCGOCmode:
18137     case CCNOmode:
18138     case CCAmode:
18139     case CCCmode:
18140     case CCOmode:
18141     case CCSmode:
18142     case CCZmode:
18143       switch (m2)
18144         {
18145         default:
18146           return VOIDmode;
18147
18148         case CCmode:
18149         case CCGCmode:
18150         case CCGOCmode:
18151         case CCNOmode:
18152         case CCAmode:
18153         case CCCmode:
18154         case CCOmode:
18155         case CCSmode:
18156         case CCZmode:
18157           return CCmode;
18158         }
18159
18160     case CCFPmode:
18161     case CCFPUmode:
18162       /* These are only compatible with themselves, which we already
18163          checked above.  */
18164       return VOIDmode;
18165     }
18166 }
18167
18168
18169 /* Return a comparison we can do and that it is equivalent to
18170    swap_condition (code) apart possibly from orderedness.
18171    But, never change orderedness if TARGET_IEEE_FP, returning
18172    UNKNOWN in that case if necessary.  */
18173
18174 static enum rtx_code
18175 ix86_fp_swap_condition (enum rtx_code code)
18176 {
18177   switch (code)
18178     {
18179     case GT:                   /* GTU - CF=0 & ZF=0 */
18180       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
18181     case GE:                   /* GEU - CF=0 */
18182       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
18183     case UNLT:                 /* LTU - CF=1 */
18184       return TARGET_IEEE_FP ? UNKNOWN : GT;
18185     case UNLE:                 /* LEU - CF=1 | ZF=1 */
18186       return TARGET_IEEE_FP ? UNKNOWN : GE;
18187     default:
18188       return swap_condition (code);
18189     }
18190 }
18191
18192 /* Return cost of comparison CODE using the best strategy for performance.
18193    All following functions do use number of instructions as a cost metrics.
18194    In future this should be tweaked to compute bytes for optimize_size and
18195    take into account performance of various instructions on various CPUs.  */
18196
18197 static int
18198 ix86_fp_comparison_cost (enum rtx_code code)
18199 {
18200   int arith_cost;
18201
18202   /* The cost of code using bit-twiddling on %ah.  */
18203   switch (code)
18204     {
18205     case UNLE:
18206     case UNLT:
18207     case LTGT:
18208     case GT:
18209     case GE:
18210     case UNORDERED:
18211     case ORDERED:
18212     case UNEQ:
18213       arith_cost = 4;
18214       break;
18215     case LT:
18216     case NE:
18217     case EQ:
18218     case UNGE:
18219       arith_cost = TARGET_IEEE_FP ? 5 : 4;
18220       break;
18221     case LE:
18222     case UNGT:
18223       arith_cost = TARGET_IEEE_FP ? 6 : 4;
18224       break;
18225     default:
18226       gcc_unreachable ();
18227     }
18228
18229   switch (ix86_fp_comparison_strategy (code))
18230     {
18231     case IX86_FPCMP_COMI:
18232       return arith_cost > 4 ? 3 : 2;
18233     case IX86_FPCMP_SAHF:
18234       return arith_cost > 4 ? 4 : 3;
18235     default:
18236       return arith_cost;
18237     }
18238 }
18239
18240 /* Return strategy to use for floating-point.  We assume that fcomi is always
18241    preferrable where available, since that is also true when looking at size
18242    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
18243
18244 enum ix86_fpcmp_strategy
18245 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
18246 {
18247   /* Do fcomi/sahf based test when profitable.  */
18248
18249   if (TARGET_CMOVE)
18250     return IX86_FPCMP_COMI;
18251
18252   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
18253     return IX86_FPCMP_SAHF;
18254
18255   return IX86_FPCMP_ARITH;
18256 }
18257
18258 /* Swap, force into registers, or otherwise massage the two operands
18259    to a fp comparison.  The operands are updated in place; the new
18260    comparison code is returned.  */
18261
18262 static enum rtx_code
18263 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
18264 {
18265   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
18266   rtx op0 = *pop0, op1 = *pop1;
18267   enum machine_mode op_mode = GET_MODE (op0);
18268   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
18269
18270   /* All of the unordered compare instructions only work on registers.
18271      The same is true of the fcomi compare instructions.  The XFmode
18272      compare instructions require registers except when comparing
18273      against zero or when converting operand 1 from fixed point to
18274      floating point.  */
18275
18276   if (!is_sse
18277       && (fpcmp_mode == CCFPUmode
18278           || (op_mode == XFmode
18279               && ! (standard_80387_constant_p (op0) == 1
18280                     || standard_80387_constant_p (op1) == 1)
18281               && GET_CODE (op1) != FLOAT)
18282           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
18283     {
18284       op0 = force_reg (op_mode, op0);
18285       op1 = force_reg (op_mode, op1);
18286     }
18287   else
18288     {
18289       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
18290          things around if they appear profitable, otherwise force op0
18291          into a register.  */
18292
18293       if (standard_80387_constant_p (op0) == 0
18294           || (MEM_P (op0)
18295               && ! (standard_80387_constant_p (op1) == 0
18296                     || MEM_P (op1))))
18297         {
18298           enum rtx_code new_code = ix86_fp_swap_condition (code);
18299           if (new_code != UNKNOWN)
18300             {
18301               rtx tmp;
18302               tmp = op0, op0 = op1, op1 = tmp;
18303               code = new_code;
18304             }
18305         }
18306
18307       if (!REG_P (op0))
18308         op0 = force_reg (op_mode, op0);
18309
18310       if (CONSTANT_P (op1))
18311         {
18312           int tmp = standard_80387_constant_p (op1);
18313           if (tmp == 0)
18314             op1 = validize_mem (force_const_mem (op_mode, op1));
18315           else if (tmp == 1)
18316             {
18317               if (TARGET_CMOVE)
18318                 op1 = force_reg (op_mode, op1);
18319             }
18320           else
18321             op1 = force_reg (op_mode, op1);
18322         }
18323     }
18324
18325   /* Try to rearrange the comparison to make it cheaper.  */
18326   if (ix86_fp_comparison_cost (code)
18327       > ix86_fp_comparison_cost (swap_condition (code))
18328       && (REG_P (op1) || can_create_pseudo_p ()))
18329     {
18330       rtx tmp;
18331       tmp = op0, op0 = op1, op1 = tmp;
18332       code = swap_condition (code);
18333       if (!REG_P (op0))
18334         op0 = force_reg (op_mode, op0);
18335     }
18336
18337   *pop0 = op0;
18338   *pop1 = op1;
18339   return code;
18340 }
18341
18342 /* Convert comparison codes we use to represent FP comparison to integer
18343    code that will result in proper branch.  Return UNKNOWN if no such code
18344    is available.  */
18345
18346 enum rtx_code
18347 ix86_fp_compare_code_to_integer (enum rtx_code code)
18348 {
18349   switch (code)
18350     {
18351     case GT:
18352       return GTU;
18353     case GE:
18354       return GEU;
18355     case ORDERED:
18356     case UNORDERED:
18357       return code;
18358       break;
18359     case UNEQ:
18360       return EQ;
18361       break;
18362     case UNLT:
18363       return LTU;
18364       break;
18365     case UNLE:
18366       return LEU;
18367       break;
18368     case LTGT:
18369       return NE;
18370       break;
18371     default:
18372       return UNKNOWN;
18373     }
18374 }
18375
18376 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
18377
18378 static rtx
18379 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
18380 {
18381   enum machine_mode fpcmp_mode, intcmp_mode;
18382   rtx tmp, tmp2;
18383
18384   fpcmp_mode = ix86_fp_compare_mode (code);
18385   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
18386
18387   /* Do fcomi/sahf based test when profitable.  */
18388   switch (ix86_fp_comparison_strategy (code))
18389     {
18390     case IX86_FPCMP_COMI:
18391       intcmp_mode = fpcmp_mode;
18392       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
18393       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
18394                          tmp);
18395       emit_insn (tmp);
18396       break;
18397
18398     case IX86_FPCMP_SAHF:
18399       intcmp_mode = fpcmp_mode;
18400       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
18401       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
18402                          tmp);
18403
18404       if (!scratch)
18405         scratch = gen_reg_rtx (HImode);
18406       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
18407       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
18408       break;
18409
18410     case IX86_FPCMP_ARITH:
18411       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
18412       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
18413       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
18414       if (!scratch)
18415         scratch = gen_reg_rtx (HImode);
18416       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
18417
18418       /* In the unordered case, we have to check C2 for NaN's, which
18419          doesn't happen to work out to anything nice combination-wise.
18420          So do some bit twiddling on the value we've got in AH to come
18421          up with an appropriate set of condition codes.  */
18422
18423       intcmp_mode = CCNOmode;
18424       switch (code)
18425         {
18426         case GT:
18427         case UNGT:
18428           if (code == GT || !TARGET_IEEE_FP)
18429             {
18430               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
18431               code = EQ;
18432             }
18433           else
18434             {
18435               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18436               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
18437               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
18438               intcmp_mode = CCmode;
18439               code = GEU;
18440             }
18441           break;
18442         case LT:
18443         case UNLT:
18444           if (code == LT && TARGET_IEEE_FP)
18445             {
18446               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18447               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
18448               intcmp_mode = CCmode;
18449               code = EQ;
18450             }
18451           else
18452             {
18453               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
18454               code = NE;
18455             }
18456           break;
18457         case GE:
18458         case UNGE:
18459           if (code == GE || !TARGET_IEEE_FP)
18460             {
18461               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
18462               code = EQ;
18463             }
18464           else
18465             {
18466               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18467               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
18468               code = NE;
18469             }
18470           break;
18471         case LE:
18472         case UNLE:
18473           if (code == LE && TARGET_IEEE_FP)
18474             {
18475               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18476               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
18477               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
18478               intcmp_mode = CCmode;
18479               code = LTU;
18480             }
18481           else
18482             {
18483               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
18484               code = NE;
18485             }
18486           break;
18487         case EQ:
18488         case UNEQ:
18489           if (code == EQ && TARGET_IEEE_FP)
18490             {
18491               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18492               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
18493               intcmp_mode = CCmode;
18494               code = EQ;
18495             }
18496           else
18497             {
18498               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
18499               code = NE;
18500             }
18501           break;
18502         case NE:
18503         case LTGT:
18504           if (code == NE && TARGET_IEEE_FP)
18505             {
18506               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
18507               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
18508                                              GEN_INT (0x40)));
18509               code = NE;
18510             }
18511           else
18512             {
18513               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
18514               code = EQ;
18515             }
18516           break;
18517
18518         case UNORDERED:
18519           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
18520           code = NE;
18521           break;
18522         case ORDERED:
18523           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
18524           code = EQ;
18525           break;
18526
18527         default:
18528           gcc_unreachable ();
18529         }
18530         break;
18531
18532     default:
18533       gcc_unreachable();
18534     }
18535
18536   /* Return the test that should be put into the flags user, i.e.
18537      the bcc, scc, or cmov instruction.  */
18538   return gen_rtx_fmt_ee (code, VOIDmode,
18539                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
18540                          const0_rtx);
18541 }
18542
18543 static rtx
18544 ix86_expand_compare (enum rtx_code code, rtx op0, rtx op1)
18545 {
18546   rtx ret;
18547
18548   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
18549     ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
18550
18551   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
18552     {
18553       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
18554       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
18555     }
18556   else
18557     ret = ix86_expand_int_compare (code, op0, op1);
18558
18559   return ret;
18560 }
18561
18562 void
18563 ix86_expand_branch (enum rtx_code code, rtx op0, rtx op1, rtx label)
18564 {
18565   enum machine_mode mode = GET_MODE (op0);
18566   rtx tmp;
18567
18568   switch (mode)
18569     {
18570     case SFmode:
18571     case DFmode:
18572     case XFmode:
18573     case QImode:
18574     case HImode:
18575     case SImode:
18576       simple:
18577       tmp = ix86_expand_compare (code, op0, op1);
18578       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
18579                                   gen_rtx_LABEL_REF (VOIDmode, label),
18580                                   pc_rtx);
18581       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
18582       return;
18583
18584     case DImode:
18585       if (TARGET_64BIT)
18586         goto simple;
18587     case TImode:
18588       /* Expand DImode branch into multiple compare+branch.  */
18589       {
18590         rtx lo[2], hi[2], label2;
18591         enum rtx_code code1, code2, code3;
18592         enum machine_mode submode;
18593
18594         if (CONSTANT_P (op0) && !CONSTANT_P (op1))
18595           {
18596             tmp = op0, op0 = op1, op1 = tmp;
18597             code = swap_condition (code);
18598           }
18599
18600         split_double_mode (mode, &op0, 1, lo+0, hi+0);
18601         split_double_mode (mode, &op1, 1, lo+1, hi+1);
18602
18603         submode = mode == DImode ? SImode : DImode;
18604
18605         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
18606            avoid two branches.  This costs one extra insn, so disable when
18607            optimizing for size.  */
18608
18609         if ((code == EQ || code == NE)
18610             && (!optimize_insn_for_size_p ()
18611                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
18612           {
18613             rtx xor0, xor1;
18614
18615             xor1 = hi[0];
18616             if (hi[1] != const0_rtx)
18617               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
18618                                    NULL_RTX, 0, OPTAB_WIDEN);
18619
18620             xor0 = lo[0];
18621             if (lo[1] != const0_rtx)
18622               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
18623                                    NULL_RTX, 0, OPTAB_WIDEN);
18624
18625             tmp = expand_binop (submode, ior_optab, xor1, xor0,
18626                                 NULL_RTX, 0, OPTAB_WIDEN);
18627
18628             ix86_expand_branch (code, tmp, const0_rtx, label);
18629             return;
18630           }
18631
18632         /* Otherwise, if we are doing less-than or greater-or-equal-than,
18633            op1 is a constant and the low word is zero, then we can just
18634            examine the high word.  Similarly for low word -1 and
18635            less-or-equal-than or greater-than.  */
18636
18637         if (CONST_INT_P (hi[1]))
18638           switch (code)
18639             {
18640             case LT: case LTU: case GE: case GEU:
18641               if (lo[1] == const0_rtx)
18642                 {
18643                   ix86_expand_branch (code, hi[0], hi[1], label);
18644                   return;
18645                 }
18646               break;
18647             case LE: case LEU: case GT: case GTU:
18648               if (lo[1] == constm1_rtx)
18649                 {
18650                   ix86_expand_branch (code, hi[0], hi[1], label);
18651                   return;
18652                 }
18653               break;
18654             default:
18655               break;
18656             }
18657
18658         /* Otherwise, we need two or three jumps.  */
18659
18660         label2 = gen_label_rtx ();
18661
18662         code1 = code;
18663         code2 = swap_condition (code);
18664         code3 = unsigned_condition (code);
18665
18666         switch (code)
18667           {
18668           case LT: case GT: case LTU: case GTU:
18669             break;
18670
18671           case LE:   code1 = LT;  code2 = GT;  break;
18672           case GE:   code1 = GT;  code2 = LT;  break;
18673           case LEU:  code1 = LTU; code2 = GTU; break;
18674           case GEU:  code1 = GTU; code2 = LTU; break;
18675
18676           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
18677           case NE:   code2 = UNKNOWN; break;
18678
18679           default:
18680             gcc_unreachable ();
18681           }
18682
18683         /*
18684          * a < b =>
18685          *    if (hi(a) < hi(b)) goto true;
18686          *    if (hi(a) > hi(b)) goto false;
18687          *    if (lo(a) < lo(b)) goto true;
18688          *  false:
18689          */
18690
18691         if (code1 != UNKNOWN)
18692           ix86_expand_branch (code1, hi[0], hi[1], label);
18693         if (code2 != UNKNOWN)
18694           ix86_expand_branch (code2, hi[0], hi[1], label2);
18695
18696         ix86_expand_branch (code3, lo[0], lo[1], label);
18697
18698         if (code2 != UNKNOWN)
18699           emit_label (label2);
18700         return;
18701       }
18702
18703     default:
18704       gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC);
18705       goto simple;
18706     }
18707 }
18708
18709 /* Split branch based on floating point condition.  */
18710 void
18711 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
18712                       rtx target1, rtx target2, rtx tmp, rtx pushed)
18713 {
18714   rtx condition;
18715   rtx i;
18716
18717   if (target2 != pc_rtx)
18718     {
18719       rtx tmp = target2;
18720       code = reverse_condition_maybe_unordered (code);
18721       target2 = target1;
18722       target1 = tmp;
18723     }
18724
18725   condition = ix86_expand_fp_compare (code, op1, op2,
18726                                       tmp);
18727
18728   /* Remove pushed operand from stack.  */
18729   if (pushed)
18730     ix86_free_from_memory (GET_MODE (pushed));
18731
18732   i = emit_jump_insn (gen_rtx_SET
18733                       (VOIDmode, pc_rtx,
18734                        gen_rtx_IF_THEN_ELSE (VOIDmode,
18735                                              condition, target1, target2)));
18736   if (split_branch_probability >= 0)
18737     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
18738 }
18739
18740 void
18741 ix86_expand_setcc (rtx dest, enum rtx_code code, rtx op0, rtx op1)
18742 {
18743   rtx ret;
18744
18745   gcc_assert (GET_MODE (dest) == QImode);
18746
18747   ret = ix86_expand_compare (code, op0, op1);
18748   PUT_MODE (ret, QImode);
18749   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
18750 }
18751
18752 /* Expand comparison setting or clearing carry flag.  Return true when
18753    successful and set pop for the operation.  */
18754 static bool
18755 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
18756 {
18757   enum machine_mode mode =
18758     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
18759
18760   /* Do not handle double-mode compares that go through special path.  */
18761   if (mode == (TARGET_64BIT ? TImode : DImode))
18762     return false;
18763
18764   if (SCALAR_FLOAT_MODE_P (mode))
18765     {
18766       rtx compare_op, compare_seq;
18767
18768       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
18769
18770       /* Shortcut:  following common codes never translate
18771          into carry flag compares.  */
18772       if (code == EQ || code == NE || code == UNEQ || code == LTGT
18773           || code == ORDERED || code == UNORDERED)
18774         return false;
18775
18776       /* These comparisons require zero flag; swap operands so they won't.  */
18777       if ((code == GT || code == UNLE || code == LE || code == UNGT)
18778           && !TARGET_IEEE_FP)
18779         {
18780           rtx tmp = op0;
18781           op0 = op1;
18782           op1 = tmp;
18783           code = swap_condition (code);
18784         }
18785
18786       /* Try to expand the comparison and verify that we end up with
18787          carry flag based comparison.  This fails to be true only when
18788          we decide to expand comparison using arithmetic that is not
18789          too common scenario.  */
18790       start_sequence ();
18791       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
18792       compare_seq = get_insns ();
18793       end_sequence ();
18794
18795       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
18796           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
18797         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
18798       else
18799         code = GET_CODE (compare_op);
18800
18801       if (code != LTU && code != GEU)
18802         return false;
18803
18804       emit_insn (compare_seq);
18805       *pop = compare_op;
18806       return true;
18807     }
18808
18809   if (!INTEGRAL_MODE_P (mode))
18810     return false;
18811
18812   switch (code)
18813     {
18814     case LTU:
18815     case GEU:
18816       break;
18817
18818     /* Convert a==0 into (unsigned)a<1.  */
18819     case EQ:
18820     case NE:
18821       if (op1 != const0_rtx)
18822         return false;
18823       op1 = const1_rtx;
18824       code = (code == EQ ? LTU : GEU);
18825       break;
18826
18827     /* Convert a>b into b<a or a>=b-1.  */
18828     case GTU:
18829     case LEU:
18830       if (CONST_INT_P (op1))
18831         {
18832           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
18833           /* Bail out on overflow.  We still can swap operands but that
18834              would force loading of the constant into register.  */
18835           if (op1 == const0_rtx
18836               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
18837             return false;
18838           code = (code == GTU ? GEU : LTU);
18839         }
18840       else
18841         {
18842           rtx tmp = op1;
18843           op1 = op0;
18844           op0 = tmp;
18845           code = (code == GTU ? LTU : GEU);
18846         }
18847       break;
18848
18849     /* Convert a>=0 into (unsigned)a<0x80000000.  */
18850     case LT:
18851     case GE:
18852       if (mode == DImode || op1 != const0_rtx)
18853         return false;
18854       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
18855       code = (code == LT ? GEU : LTU);
18856       break;
18857     case LE:
18858     case GT:
18859       if (mode == DImode || op1 != constm1_rtx)
18860         return false;
18861       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
18862       code = (code == LE ? GEU : LTU);
18863       break;
18864
18865     default:
18866       return false;
18867     }
18868   /* Swapping operands may cause constant to appear as first operand.  */
18869   if (!nonimmediate_operand (op0, VOIDmode))
18870     {
18871       if (!can_create_pseudo_p ())
18872         return false;
18873       op0 = force_reg (mode, op0);
18874     }
18875   *pop = ix86_expand_compare (code, op0, op1);
18876   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
18877   return true;
18878 }
18879
18880 bool
18881 ix86_expand_int_movcc (rtx operands[])
18882 {
18883   enum rtx_code code = GET_CODE (operands[1]), compare_code;
18884   rtx compare_seq, compare_op;
18885   enum machine_mode mode = GET_MODE (operands[0]);
18886   bool sign_bit_compare_p = false;
18887   rtx op0 = XEXP (operands[1], 0);
18888   rtx op1 = XEXP (operands[1], 1);
18889
18890   start_sequence ();
18891   compare_op = ix86_expand_compare (code, op0, op1);
18892   compare_seq = get_insns ();
18893   end_sequence ();
18894
18895   compare_code = GET_CODE (compare_op);
18896
18897   if ((op1 == const0_rtx && (code == GE || code == LT))
18898       || (op1 == constm1_rtx && (code == GT || code == LE)))
18899     sign_bit_compare_p = true;
18900
18901   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
18902      HImode insns, we'd be swallowed in word prefix ops.  */
18903
18904   if ((mode != HImode || TARGET_FAST_PREFIX)
18905       && (mode != (TARGET_64BIT ? TImode : DImode))
18906       && CONST_INT_P (operands[2])
18907       && CONST_INT_P (operands[3]))
18908     {
18909       rtx out = operands[0];
18910       HOST_WIDE_INT ct = INTVAL (operands[2]);
18911       HOST_WIDE_INT cf = INTVAL (operands[3]);
18912       HOST_WIDE_INT diff;
18913
18914       diff = ct - cf;
18915       /*  Sign bit compares are better done using shifts than we do by using
18916           sbb.  */
18917       if (sign_bit_compare_p
18918           || ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
18919         {
18920           /* Detect overlap between destination and compare sources.  */
18921           rtx tmp = out;
18922
18923           if (!sign_bit_compare_p)
18924             {
18925               rtx flags;
18926               bool fpcmp = false;
18927
18928               compare_code = GET_CODE (compare_op);
18929
18930               flags = XEXP (compare_op, 0);
18931
18932               if (GET_MODE (flags) == CCFPmode
18933                   || GET_MODE (flags) == CCFPUmode)
18934                 {
18935                   fpcmp = true;
18936                   compare_code
18937                     = ix86_fp_compare_code_to_integer (compare_code);
18938                 }
18939
18940               /* To simplify rest of code, restrict to the GEU case.  */
18941               if (compare_code == LTU)
18942                 {
18943                   HOST_WIDE_INT tmp = ct;
18944                   ct = cf;
18945                   cf = tmp;
18946                   compare_code = reverse_condition (compare_code);
18947                   code = reverse_condition (code);
18948                 }
18949               else
18950                 {
18951                   if (fpcmp)
18952                     PUT_CODE (compare_op,
18953                               reverse_condition_maybe_unordered
18954                                 (GET_CODE (compare_op)));
18955                   else
18956                     PUT_CODE (compare_op,
18957                               reverse_condition (GET_CODE (compare_op)));
18958                 }
18959               diff = ct - cf;
18960
18961               if (reg_overlap_mentioned_p (out, op0)
18962                   || reg_overlap_mentioned_p (out, op1))
18963                 tmp = gen_reg_rtx (mode);
18964
18965               if (mode == DImode)
18966                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
18967               else
18968                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
18969                                                  flags, compare_op));
18970             }
18971           else
18972             {
18973               if (code == GT || code == GE)
18974                 code = reverse_condition (code);
18975               else
18976                 {
18977                   HOST_WIDE_INT tmp = ct;
18978                   ct = cf;
18979                   cf = tmp;
18980                   diff = ct - cf;
18981                 }
18982               tmp = emit_store_flag (tmp, code, op0, op1, VOIDmode, 0, -1);
18983             }
18984
18985           if (diff == 1)
18986             {
18987               /*
18988                * cmpl op0,op1
18989                * sbbl dest,dest
18990                * [addl dest, ct]
18991                *
18992                * Size 5 - 8.
18993                */
18994               if (ct)
18995                 tmp = expand_simple_binop (mode, PLUS,
18996                                            tmp, GEN_INT (ct),
18997                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18998             }
18999           else if (cf == -1)
19000             {
19001               /*
19002                * cmpl op0,op1
19003                * sbbl dest,dest
19004                * orl $ct, dest
19005                *
19006                * Size 8.
19007                */
19008               tmp = expand_simple_binop (mode, IOR,
19009                                          tmp, GEN_INT (ct),
19010                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
19011             }
19012           else if (diff == -1 && ct)
19013             {
19014               /*
19015                * cmpl op0,op1
19016                * sbbl dest,dest
19017                * notl dest
19018                * [addl dest, cf]
19019                *
19020                * Size 8 - 11.
19021                */
19022               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
19023               if (cf)
19024                 tmp = expand_simple_binop (mode, PLUS,
19025                                            copy_rtx (tmp), GEN_INT (cf),
19026                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
19027             }
19028           else
19029             {
19030               /*
19031                * cmpl op0,op1
19032                * sbbl dest,dest
19033                * [notl dest]
19034                * andl cf - ct, dest
19035                * [addl dest, ct]
19036                *
19037                * Size 8 - 11.
19038                */
19039
19040               if (cf == 0)
19041                 {
19042                   cf = ct;
19043                   ct = 0;
19044                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
19045                 }
19046
19047               tmp = expand_simple_binop (mode, AND,
19048                                          copy_rtx (tmp),
19049                                          gen_int_mode (cf - ct, mode),
19050                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
19051               if (ct)
19052                 tmp = expand_simple_binop (mode, PLUS,
19053                                            copy_rtx (tmp), GEN_INT (ct),
19054                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
19055             }
19056
19057           if (!rtx_equal_p (tmp, out))
19058             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
19059
19060           return true;
19061         }
19062
19063       if (diff < 0)
19064         {
19065           enum machine_mode cmp_mode = GET_MODE (op0);
19066
19067           HOST_WIDE_INT tmp;
19068           tmp = ct, ct = cf, cf = tmp;
19069           diff = -diff;
19070
19071           if (SCALAR_FLOAT_MODE_P (cmp_mode))
19072             {
19073               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
19074
19075               /* We may be reversing unordered compare to normal compare, that
19076                  is not valid in general (we may convert non-trapping condition
19077                  to trapping one), however on i386 we currently emit all
19078                  comparisons unordered.  */
19079               compare_code = reverse_condition_maybe_unordered (compare_code);
19080               code = reverse_condition_maybe_unordered (code);
19081             }
19082           else
19083             {
19084               compare_code = reverse_condition (compare_code);
19085               code = reverse_condition (code);
19086             }
19087         }
19088
19089       compare_code = UNKNOWN;
19090       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
19091           && CONST_INT_P (op1))
19092         {
19093           if (op1 == const0_rtx
19094               && (code == LT || code == GE))
19095             compare_code = code;
19096           else if (op1 == constm1_rtx)
19097             {
19098               if (code == LE)
19099                 compare_code = LT;
19100               else if (code == GT)
19101                 compare_code = GE;
19102             }
19103         }
19104
19105       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
19106       if (compare_code != UNKNOWN
19107           && GET_MODE (op0) == GET_MODE (out)
19108           && (cf == -1 || ct == -1))
19109         {
19110           /* If lea code below could be used, only optimize
19111              if it results in a 2 insn sequence.  */
19112
19113           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
19114                  || diff == 3 || diff == 5 || diff == 9)
19115               || (compare_code == LT && ct == -1)
19116               || (compare_code == GE && cf == -1))
19117             {
19118               /*
19119                * notl op1       (if necessary)
19120                * sarl $31, op1
19121                * orl cf, op1
19122                */
19123               if (ct != -1)
19124                 {
19125                   cf = ct;
19126                   ct = -1;
19127                   code = reverse_condition (code);
19128                 }
19129
19130               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
19131
19132               out = expand_simple_binop (mode, IOR,
19133                                          out, GEN_INT (cf),
19134                                          out, 1, OPTAB_DIRECT);
19135               if (out != operands[0])
19136                 emit_move_insn (operands[0], out);
19137
19138               return true;
19139             }
19140         }
19141
19142
19143       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
19144            || diff == 3 || diff == 5 || diff == 9)
19145           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
19146           && (mode != DImode
19147               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
19148         {
19149           /*
19150            * xorl dest,dest
19151            * cmpl op1,op2
19152            * setcc dest
19153            * lea cf(dest*(ct-cf)),dest
19154            *
19155            * Size 14.
19156            *
19157            * This also catches the degenerate setcc-only case.
19158            */
19159
19160           rtx tmp;
19161           int nops;
19162
19163           out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
19164
19165           nops = 0;
19166           /* On x86_64 the lea instruction operates on Pmode, so we need
19167              to get arithmetics done in proper mode to match.  */
19168           if (diff == 1)
19169             tmp = copy_rtx (out);
19170           else
19171             {
19172               rtx out1;
19173               out1 = copy_rtx (out);
19174               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
19175               nops++;
19176               if (diff & 1)
19177                 {
19178                   tmp = gen_rtx_PLUS (mode, tmp, out1);
19179                   nops++;
19180                 }
19181             }
19182           if (cf != 0)
19183             {
19184               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
19185               nops++;
19186             }
19187           if (!rtx_equal_p (tmp, out))
19188             {
19189               if (nops == 1)
19190                 out = force_operand (tmp, copy_rtx (out));
19191               else
19192                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
19193             }
19194           if (!rtx_equal_p (out, operands[0]))
19195             emit_move_insn (operands[0], copy_rtx (out));
19196
19197           return true;
19198         }
19199
19200       /*
19201        * General case:                  Jumpful:
19202        *   xorl dest,dest               cmpl op1, op2
19203        *   cmpl op1, op2                movl ct, dest
19204        *   setcc dest                   jcc 1f
19205        *   decl dest                    movl cf, dest
19206        *   andl (cf-ct),dest            1:
19207        *   addl ct,dest
19208        *
19209        * Size 20.                       Size 14.
19210        *
19211        * This is reasonably steep, but branch mispredict costs are
19212        * high on modern cpus, so consider failing only if optimizing
19213        * for space.
19214        */
19215
19216       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
19217           && BRANCH_COST (optimize_insn_for_speed_p (),
19218                           false) >= 2)
19219         {
19220           if (cf == 0)
19221             {
19222               enum machine_mode cmp_mode = GET_MODE (op0);
19223
19224               cf = ct;
19225               ct = 0;
19226
19227               if (SCALAR_FLOAT_MODE_P (cmp_mode))
19228                 {
19229                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
19230
19231                   /* We may be reversing unordered compare to normal compare,
19232                      that is not valid in general (we may convert non-trapping
19233                      condition to trapping one), however on i386 we currently
19234                      emit all comparisons unordered.  */
19235                   code = reverse_condition_maybe_unordered (code);
19236                 }
19237               else
19238                 {
19239                   code = reverse_condition (code);
19240                   if (compare_code != UNKNOWN)
19241                     compare_code = reverse_condition (compare_code);
19242                 }
19243             }
19244
19245           if (compare_code != UNKNOWN)
19246             {
19247               /* notl op1       (if needed)
19248                  sarl $31, op1
19249                  andl (cf-ct), op1
19250                  addl ct, op1
19251
19252                  For x < 0 (resp. x <= -1) there will be no notl,
19253                  so if possible swap the constants to get rid of the
19254                  complement.
19255                  True/false will be -1/0 while code below (store flag
19256                  followed by decrement) is 0/-1, so the constants need
19257                  to be exchanged once more.  */
19258
19259               if (compare_code == GE || !cf)
19260                 {
19261                   code = reverse_condition (code);
19262                   compare_code = LT;
19263                 }
19264               else
19265                 {
19266                   HOST_WIDE_INT tmp = cf;
19267                   cf = ct;
19268                   ct = tmp;
19269                 }
19270
19271               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
19272             }
19273           else
19274             {
19275               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
19276
19277               out = expand_simple_binop (mode, PLUS, copy_rtx (out),
19278                                          constm1_rtx,
19279                                          copy_rtx (out), 1, OPTAB_DIRECT);
19280             }
19281
19282           out = expand_simple_binop (mode, AND, copy_rtx (out),
19283                                      gen_int_mode (cf - ct, mode),
19284                                      copy_rtx (out), 1, OPTAB_DIRECT);
19285           if (ct)
19286             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
19287                                        copy_rtx (out), 1, OPTAB_DIRECT);
19288           if (!rtx_equal_p (out, operands[0]))
19289             emit_move_insn (operands[0], copy_rtx (out));
19290
19291           return true;
19292         }
19293     }
19294
19295   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
19296     {
19297       /* Try a few things more with specific constants and a variable.  */
19298
19299       optab op;
19300       rtx var, orig_out, out, tmp;
19301
19302       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
19303         return false;
19304
19305       /* If one of the two operands is an interesting constant, load a
19306          constant with the above and mask it in with a logical operation.  */
19307
19308       if (CONST_INT_P (operands[2]))
19309         {
19310           var = operands[3];
19311           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
19312             operands[3] = constm1_rtx, op = and_optab;
19313           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
19314             operands[3] = const0_rtx, op = ior_optab;
19315           else
19316             return false;
19317         }
19318       else if (CONST_INT_P (operands[3]))
19319         {
19320           var = operands[2];
19321           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
19322             operands[2] = constm1_rtx, op = and_optab;
19323           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
19324             operands[2] = const0_rtx, op = ior_optab;
19325           else
19326             return false;
19327         }
19328       else
19329         return false;
19330
19331       orig_out = operands[0];
19332       tmp = gen_reg_rtx (mode);
19333       operands[0] = tmp;
19334
19335       /* Recurse to get the constant loaded.  */
19336       if (ix86_expand_int_movcc (operands) == 0)
19337         return false;
19338
19339       /* Mask in the interesting variable.  */
19340       out = expand_binop (mode, op, var, tmp, orig_out, 0,
19341                           OPTAB_WIDEN);
19342       if (!rtx_equal_p (out, orig_out))
19343         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
19344
19345       return true;
19346     }
19347
19348   /*
19349    * For comparison with above,
19350    *
19351    * movl cf,dest
19352    * movl ct,tmp
19353    * cmpl op1,op2
19354    * cmovcc tmp,dest
19355    *
19356    * Size 15.
19357    */
19358
19359   if (! nonimmediate_operand (operands[2], mode))
19360     operands[2] = force_reg (mode, operands[2]);
19361   if (! nonimmediate_operand (operands[3], mode))
19362     operands[3] = force_reg (mode, operands[3]);
19363
19364   if (! register_operand (operands[2], VOIDmode)
19365       && (mode == QImode
19366           || ! register_operand (operands[3], VOIDmode)))
19367     operands[2] = force_reg (mode, operands[2]);
19368
19369   if (mode == QImode
19370       && ! register_operand (operands[3], VOIDmode))
19371     operands[3] = force_reg (mode, operands[3]);
19372
19373   emit_insn (compare_seq);
19374   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
19375                           gen_rtx_IF_THEN_ELSE (mode,
19376                                                 compare_op, operands[2],
19377                                                 operands[3])));
19378   return true;
19379 }
19380
19381 /* Swap, force into registers, or otherwise massage the two operands
19382    to an sse comparison with a mask result.  Thus we differ a bit from
19383    ix86_prepare_fp_compare_args which expects to produce a flags result.
19384
19385    The DEST operand exists to help determine whether to commute commutative
19386    operators.  The POP0/POP1 operands are updated in place.  The new
19387    comparison code is returned, or UNKNOWN if not implementable.  */
19388
19389 static enum rtx_code
19390 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
19391                                   rtx *pop0, rtx *pop1)
19392 {
19393   rtx tmp;
19394
19395   switch (code)
19396     {
19397     case LTGT:
19398     case UNEQ:
19399       /* AVX supports all the needed comparisons.  */
19400       if (TARGET_AVX)
19401         break;
19402       /* We have no LTGT as an operator.  We could implement it with
19403          NE & ORDERED, but this requires an extra temporary.  It's
19404          not clear that it's worth it.  */
19405       return UNKNOWN;
19406
19407     case LT:
19408     case LE:
19409     case UNGT:
19410     case UNGE:
19411       /* These are supported directly.  */
19412       break;
19413
19414     case EQ:
19415     case NE:
19416     case UNORDERED:
19417     case ORDERED:
19418       /* AVX has 3 operand comparisons, no need to swap anything.  */
19419       if (TARGET_AVX)
19420         break;
19421       /* For commutative operators, try to canonicalize the destination
19422          operand to be first in the comparison - this helps reload to
19423          avoid extra moves.  */
19424       if (!dest || !rtx_equal_p (dest, *pop1))
19425         break;
19426       /* FALLTHRU */
19427
19428     case GE:
19429     case GT:
19430     case UNLE:
19431     case UNLT:
19432       /* These are not supported directly before AVX, and furthermore
19433          ix86_expand_sse_fp_minmax only optimizes LT/UNGE.  Swap the
19434          comparison operands to transform into something that is
19435          supported.  */
19436       tmp = *pop0;
19437       *pop0 = *pop1;
19438       *pop1 = tmp;
19439       code = swap_condition (code);
19440       break;
19441
19442     default:
19443       gcc_unreachable ();
19444     }
19445
19446   return code;
19447 }
19448
19449 /* Detect conditional moves that exactly match min/max operational
19450    semantics.  Note that this is IEEE safe, as long as we don't
19451    interchange the operands.
19452
19453    Returns FALSE if this conditional move doesn't match a MIN/MAX,
19454    and TRUE if the operation is successful and instructions are emitted.  */
19455
19456 static bool
19457 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
19458                            rtx cmp_op1, rtx if_true, rtx if_false)
19459 {
19460   enum machine_mode mode;
19461   bool is_min;
19462   rtx tmp;
19463
19464   if (code == LT)
19465     ;
19466   else if (code == UNGE)
19467     {
19468       tmp = if_true;
19469       if_true = if_false;
19470       if_false = tmp;
19471     }
19472   else
19473     return false;
19474
19475   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
19476     is_min = true;
19477   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
19478     is_min = false;
19479   else
19480     return false;
19481
19482   mode = GET_MODE (dest);
19483
19484   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
19485      but MODE may be a vector mode and thus not appropriate.  */
19486   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
19487     {
19488       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
19489       rtvec v;
19490
19491       if_true = force_reg (mode, if_true);
19492       v = gen_rtvec (2, if_true, if_false);
19493       tmp = gen_rtx_UNSPEC (mode, v, u);
19494     }
19495   else
19496     {
19497       code = is_min ? SMIN : SMAX;
19498       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
19499     }
19500
19501   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
19502   return true;
19503 }
19504
19505 /* Expand an sse vector comparison.  Return the register with the result.  */
19506
19507 static rtx
19508 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
19509                      rtx op_true, rtx op_false)
19510 {
19511   enum machine_mode mode = GET_MODE (dest);
19512   enum machine_mode cmp_mode = GET_MODE (cmp_op0);
19513   rtx x;
19514
19515   cmp_op0 = force_reg (cmp_mode, cmp_op0);
19516   if (!nonimmediate_operand (cmp_op1, cmp_mode))
19517     cmp_op1 = force_reg (cmp_mode, cmp_op1);
19518
19519   if (optimize
19520       || reg_overlap_mentioned_p (dest, op_true)
19521       || reg_overlap_mentioned_p (dest, op_false))
19522     dest = gen_reg_rtx (mode);
19523
19524   x = gen_rtx_fmt_ee (code, cmp_mode, cmp_op0, cmp_op1);
19525   if (cmp_mode != mode)
19526     {
19527       x = force_reg (cmp_mode, x);
19528       convert_move (dest, x, false);
19529     }
19530   else
19531     emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19532
19533   return dest;
19534 }
19535
19536 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
19537    operations.  This is used for both scalar and vector conditional moves.  */
19538
19539 static void
19540 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
19541 {
19542   enum machine_mode mode = GET_MODE (dest);
19543   rtx t2, t3, x;
19544
19545   if (vector_all_ones_operand (op_true, mode)
19546       && rtx_equal_p (op_false, CONST0_RTX (mode)))
19547     {
19548       emit_insn (gen_rtx_SET (VOIDmode, dest, cmp));
19549     }
19550   else if (op_false == CONST0_RTX (mode))
19551     {
19552       op_true = force_reg (mode, op_true);
19553       x = gen_rtx_AND (mode, cmp, op_true);
19554       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19555     }
19556   else if (op_true == CONST0_RTX (mode))
19557     {
19558       op_false = force_reg (mode, op_false);
19559       x = gen_rtx_NOT (mode, cmp);
19560       x = gen_rtx_AND (mode, x, op_false);
19561       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19562     }
19563   else if (INTEGRAL_MODE_P (mode) && op_true == CONSTM1_RTX (mode))
19564     {
19565       op_false = force_reg (mode, op_false);
19566       x = gen_rtx_IOR (mode, cmp, op_false);
19567       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19568     }
19569   else if (TARGET_XOP)
19570     {
19571       op_true = force_reg (mode, op_true);
19572
19573       if (!nonimmediate_operand (op_false, mode))
19574         op_false = force_reg (mode, op_false);
19575
19576       emit_insn (gen_rtx_SET (mode, dest,
19577                               gen_rtx_IF_THEN_ELSE (mode, cmp,
19578                                                     op_true,
19579                                                     op_false)));
19580     }
19581   else
19582     {
19583       rtx (*gen) (rtx, rtx, rtx, rtx) = NULL;
19584
19585       if (!nonimmediate_operand (op_true, mode))
19586         op_true = force_reg (mode, op_true);
19587
19588       op_false = force_reg (mode, op_false);
19589
19590       switch (mode)
19591         {
19592         case V4SFmode:
19593           if (TARGET_SSE4_1)
19594             gen = gen_sse4_1_blendvps;
19595           break;
19596         case V2DFmode:
19597           if (TARGET_SSE4_1)
19598             gen = gen_sse4_1_blendvpd;
19599           break;
19600         case V16QImode:
19601         case V8HImode:
19602         case V4SImode:
19603         case V2DImode:
19604           if (TARGET_SSE4_1)
19605             {
19606               gen = gen_sse4_1_pblendvb;
19607               dest = gen_lowpart (V16QImode, dest);
19608               op_false = gen_lowpart (V16QImode, op_false);
19609               op_true = gen_lowpart (V16QImode, op_true);
19610               cmp = gen_lowpart (V16QImode, cmp);
19611             }
19612           break;
19613         case V8SFmode:
19614           if (TARGET_AVX)
19615             gen = gen_avx_blendvps256;
19616           break;
19617         case V4DFmode:
19618           if (TARGET_AVX)
19619             gen = gen_avx_blendvpd256;
19620           break;
19621         case V32QImode:
19622         case V16HImode:
19623         case V8SImode:
19624         case V4DImode:
19625           if (TARGET_AVX2)
19626             {
19627               gen = gen_avx2_pblendvb;
19628               dest = gen_lowpart (V32QImode, dest);
19629               op_false = gen_lowpart (V32QImode, op_false);
19630               op_true = gen_lowpart (V32QImode, op_true);
19631               cmp = gen_lowpart (V32QImode, cmp);
19632             }
19633           break;
19634         default:
19635           break;
19636         }
19637
19638       if (gen != NULL)
19639         emit_insn (gen (dest, op_false, op_true, cmp));
19640       else
19641         {
19642           op_true = force_reg (mode, op_true);
19643
19644           t2 = gen_reg_rtx (mode);
19645           if (optimize)
19646             t3 = gen_reg_rtx (mode);
19647           else
19648             t3 = dest;
19649
19650           x = gen_rtx_AND (mode, op_true, cmp);
19651           emit_insn (gen_rtx_SET (VOIDmode, t2, x));
19652
19653           x = gen_rtx_NOT (mode, cmp);
19654           x = gen_rtx_AND (mode, x, op_false);
19655           emit_insn (gen_rtx_SET (VOIDmode, t3, x));
19656
19657           x = gen_rtx_IOR (mode, t3, t2);
19658           emit_insn (gen_rtx_SET (VOIDmode, dest, x));
19659         }
19660     }
19661 }
19662
19663 /* Expand a floating-point conditional move.  Return true if successful.  */
19664
19665 bool
19666 ix86_expand_fp_movcc (rtx operands[])
19667 {
19668   enum machine_mode mode = GET_MODE (operands[0]);
19669   enum rtx_code code = GET_CODE (operands[1]);
19670   rtx tmp, compare_op;
19671   rtx op0 = XEXP (operands[1], 0);
19672   rtx op1 = XEXP (operands[1], 1);
19673
19674   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
19675     {
19676       enum machine_mode cmode;
19677
19678       /* Since we've no cmove for sse registers, don't force bad register
19679          allocation just to gain access to it.  Deny movcc when the
19680          comparison mode doesn't match the move mode.  */
19681       cmode = GET_MODE (op0);
19682       if (cmode == VOIDmode)
19683         cmode = GET_MODE (op1);
19684       if (cmode != mode)
19685         return false;
19686
19687       code = ix86_prepare_sse_fp_compare_args (operands[0], code, &op0, &op1);
19688       if (code == UNKNOWN)
19689         return false;
19690
19691       if (ix86_expand_sse_fp_minmax (operands[0], code, op0, op1,
19692                                      operands[2], operands[3]))
19693         return true;
19694
19695       tmp = ix86_expand_sse_cmp (operands[0], code, op0, op1,
19696                                  operands[2], operands[3]);
19697       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
19698       return true;
19699     }
19700
19701   /* The floating point conditional move instructions don't directly
19702      support conditions resulting from a signed integer comparison.  */
19703
19704   compare_op = ix86_expand_compare (code, op0, op1);
19705   if (!fcmov_comparison_operator (compare_op, VOIDmode))
19706     {
19707       tmp = gen_reg_rtx (QImode);
19708       ix86_expand_setcc (tmp, code, op0, op1);
19709
19710       compare_op = ix86_expand_compare (NE, tmp, const0_rtx);
19711     }
19712
19713   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
19714                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
19715                                                 operands[2], operands[3])));
19716
19717   return true;
19718 }
19719
19720 /* Expand a floating-point vector conditional move; a vcond operation
19721    rather than a movcc operation.  */
19722
19723 bool
19724 ix86_expand_fp_vcond (rtx operands[])
19725 {
19726   enum rtx_code code = GET_CODE (operands[3]);
19727   rtx cmp;
19728
19729   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
19730                                            &operands[4], &operands[5]);
19731   if (code == UNKNOWN)
19732     {
19733       rtx temp;
19734       switch (GET_CODE (operands[3]))
19735         {
19736         case LTGT:
19737           temp = ix86_expand_sse_cmp (operands[0], ORDERED, operands[4],
19738                                       operands[5], operands[0], operands[0]);
19739           cmp = ix86_expand_sse_cmp (operands[0], NE, operands[4],
19740                                      operands[5], operands[1], operands[2]);
19741           code = AND;
19742           break;
19743         case UNEQ:
19744           temp = ix86_expand_sse_cmp (operands[0], UNORDERED, operands[4],
19745                                       operands[5], operands[0], operands[0]);
19746           cmp = ix86_expand_sse_cmp (operands[0], EQ, operands[4],
19747                                      operands[5], operands[1], operands[2]);
19748           code = IOR;
19749           break;
19750         default:
19751           gcc_unreachable ();
19752         }
19753       cmp = expand_simple_binop (GET_MODE (cmp), code, temp, cmp, cmp, 1,
19754                                  OPTAB_DIRECT);
19755       ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
19756       return true;
19757     }
19758
19759   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
19760                                  operands[5], operands[1], operands[2]))
19761     return true;
19762
19763   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
19764                              operands[1], operands[2]);
19765   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
19766   return true;
19767 }
19768
19769 /* Expand a signed/unsigned integral vector conditional move.  */
19770
19771 bool
19772 ix86_expand_int_vcond (rtx operands[])
19773 {
19774   enum machine_mode data_mode = GET_MODE (operands[0]);
19775   enum machine_mode mode = GET_MODE (operands[4]);
19776   enum rtx_code code = GET_CODE (operands[3]);
19777   bool negate = false;
19778   rtx x, cop0, cop1;
19779
19780   cop0 = operands[4];
19781   cop1 = operands[5];
19782
19783   /* Try to optimize x < 0 ? -1 : 0 into (signed) x >> 31
19784      and x < 0 ? 1 : 0 into (unsigned) x >> 31.  */
19785   if ((code == LT || code == GE)
19786       && data_mode == mode
19787       && cop1 == CONST0_RTX (mode)
19788       && operands[1 + (code == LT)] == CONST0_RTX (data_mode)
19789       && GET_MODE_SIZE (GET_MODE_INNER (data_mode)) > 1
19790       && GET_MODE_SIZE (GET_MODE_INNER (data_mode)) <= 8
19791       && (GET_MODE_SIZE (data_mode) == 16
19792           || (TARGET_AVX2 && GET_MODE_SIZE (data_mode) == 32)))
19793     {
19794       rtx negop = operands[2 - (code == LT)];
19795       int shift = GET_MODE_BITSIZE (GET_MODE_INNER (data_mode)) - 1;
19796       if (negop == CONST1_RTX (data_mode))
19797         {
19798           rtx res = expand_simple_binop (mode, LSHIFTRT, cop0, GEN_INT (shift),
19799                                          operands[0], 1, OPTAB_DIRECT);
19800           if (res != operands[0])
19801             emit_move_insn (operands[0], res);
19802           return true;
19803         }
19804       else if (GET_MODE_INNER (data_mode) != DImode
19805                && vector_all_ones_operand (negop, data_mode))
19806         {
19807           rtx res = expand_simple_binop (mode, ASHIFTRT, cop0, GEN_INT (shift),
19808                                          operands[0], 0, OPTAB_DIRECT);
19809           if (res != operands[0])
19810             emit_move_insn (operands[0], res);
19811           return true;
19812         }
19813     }
19814
19815   if (!nonimmediate_operand (cop1, mode))
19816     cop1 = force_reg (mode, cop1);
19817   if (!general_operand (operands[1], data_mode))
19818     operands[1] = force_reg (data_mode, operands[1]);
19819   if (!general_operand (operands[2], data_mode))
19820     operands[2] = force_reg (data_mode, operands[2]);
19821
19822   /* XOP supports all of the comparisons on all 128-bit vector int types.  */
19823   if (TARGET_XOP
19824       && (mode == V16QImode || mode == V8HImode
19825           || mode == V4SImode || mode == V2DImode))
19826     ;
19827   else
19828     {
19829       /* Canonicalize the comparison to EQ, GT, GTU.  */
19830       switch (code)
19831         {
19832         case EQ:
19833         case GT:
19834         case GTU:
19835           break;
19836
19837         case NE:
19838         case LE:
19839         case LEU:
19840           code = reverse_condition (code);
19841           negate = true;
19842           break;
19843
19844         case GE:
19845         case GEU:
19846           code = reverse_condition (code);
19847           negate = true;
19848           /* FALLTHRU */
19849
19850         case LT:
19851         case LTU:
19852           code = swap_condition (code);
19853           x = cop0, cop0 = cop1, cop1 = x;
19854           break;
19855
19856         default:
19857           gcc_unreachable ();
19858         }
19859
19860       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
19861       if (mode == V2DImode)
19862         {
19863           switch (code)
19864             {
19865             case EQ:
19866               /* SSE4.1 supports EQ.  */
19867               if (!TARGET_SSE4_1)
19868                 return false;
19869               break;
19870
19871             case GT:
19872             case GTU:
19873               /* SSE4.2 supports GT/GTU.  */
19874               if (!TARGET_SSE4_2)
19875                 return false;
19876               break;
19877
19878             default:
19879               gcc_unreachable ();
19880             }
19881         }
19882
19883       /* Unsigned parallel compare is not supported by the hardware.
19884          Play some tricks to turn this into a signed comparison
19885          against 0.  */
19886       if (code == GTU)
19887         {
19888           cop0 = force_reg (mode, cop0);
19889
19890           switch (mode)
19891             {
19892             case V8SImode:
19893             case V4DImode:
19894             case V4SImode:
19895             case V2DImode:
19896                 {
19897                   rtx t1, t2, mask;
19898                   rtx (*gen_sub3) (rtx, rtx, rtx);
19899
19900                   switch (mode)
19901                     {
19902                     case V8SImode: gen_sub3 = gen_subv8si3; break;
19903                     case V4DImode: gen_sub3 = gen_subv4di3; break;
19904                     case V4SImode: gen_sub3 = gen_subv4si3; break;
19905                     case V2DImode: gen_sub3 = gen_subv2di3; break;
19906                     default:
19907                       gcc_unreachable ();
19908                     }
19909                   /* Subtract (-(INT MAX) - 1) from both operands to make
19910                      them signed.  */
19911                   mask = ix86_build_signbit_mask (mode, true, false);
19912                   t1 = gen_reg_rtx (mode);
19913                   emit_insn (gen_sub3 (t1, cop0, mask));
19914
19915                   t2 = gen_reg_rtx (mode);
19916                   emit_insn (gen_sub3 (t2, cop1, mask));
19917
19918                   cop0 = t1;
19919                   cop1 = t2;
19920                   code = GT;
19921                 }
19922               break;
19923
19924             case V32QImode:
19925             case V16HImode:
19926             case V16QImode:
19927             case V8HImode:
19928               /* Perform a parallel unsigned saturating subtraction.  */
19929               x = gen_reg_rtx (mode);
19930               emit_insn (gen_rtx_SET (VOIDmode, x,
19931                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
19932
19933               cop0 = x;
19934               cop1 = CONST0_RTX (mode);
19935               code = EQ;
19936               negate = !negate;
19937               break;
19938
19939             default:
19940               gcc_unreachable ();
19941             }
19942         }
19943     }
19944
19945   /* Allow the comparison to be done in one mode, but the movcc to
19946      happen in another mode.  */
19947   if (data_mode == mode)
19948     {
19949       x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
19950                                operands[1+negate], operands[2-negate]);
19951     }
19952   else
19953     {
19954       gcc_assert (GET_MODE_SIZE (data_mode) == GET_MODE_SIZE (mode));
19955       x = ix86_expand_sse_cmp (gen_lowpart (mode, operands[0]),
19956                                code, cop0, cop1,
19957                                operands[1+negate], operands[2-negate]);
19958       x = gen_lowpart (data_mode, x);
19959     }
19960
19961   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
19962                          operands[2-negate]);
19963   return true;
19964 }
19965
19966 /* Expand a variable vector permutation.  */
19967
19968 void
19969 ix86_expand_vec_perm (rtx operands[])
19970 {
19971   rtx target = operands[0];
19972   rtx op0 = operands[1];
19973   rtx op1 = operands[2];
19974   rtx mask = operands[3];
19975   rtx t1, t2, t3, t4, vt, vt2, vec[32];
19976   enum machine_mode mode = GET_MODE (op0);
19977   enum machine_mode maskmode = GET_MODE (mask);
19978   int w, e, i;
19979   bool one_operand_shuffle = rtx_equal_p (op0, op1);
19980
19981   /* Number of elements in the vector.  */
19982   w = GET_MODE_NUNITS (mode);
19983   e = GET_MODE_UNIT_SIZE (mode);
19984   gcc_assert (w <= 32);
19985
19986   if (TARGET_AVX2)
19987     {
19988       if (mode == V4DImode || mode == V4DFmode || mode == V16HImode)
19989         {
19990           /* Unfortunately, the VPERMQ and VPERMPD instructions only support
19991              an constant shuffle operand.  With a tiny bit of effort we can
19992              use VPERMD instead.  A re-interpretation stall for V4DFmode is
19993              unfortunate but there's no avoiding it.
19994              Similarly for V16HImode we don't have instructions for variable
19995              shuffling, while for V32QImode we can use after preparing suitable
19996              masks vpshufb; vpshufb; vpermq; vpor.  */
19997
19998           if (mode == V16HImode)
19999             {
20000               maskmode = mode = V32QImode;
20001               w = 32;
20002               e = 1;
20003             }
20004           else
20005             {
20006               maskmode = mode = V8SImode;
20007               w = 8;
20008               e = 4;
20009             }
20010           t1 = gen_reg_rtx (maskmode);
20011
20012           /* Replicate the low bits of the V4DImode mask into V8SImode:
20013                mask = { A B C D }
20014                t1 = { A A B B C C D D }.  */
20015           for (i = 0; i < w / 2; ++i)
20016             vec[i*2 + 1] = vec[i*2] = GEN_INT (i * 2);
20017           vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
20018           vt = force_reg (maskmode, vt);
20019           mask = gen_lowpart (maskmode, mask);
20020           if (maskmode == V8SImode)
20021             emit_insn (gen_avx2_permvarv8si (t1, mask, vt));
20022           else
20023             emit_insn (gen_avx2_pshufbv32qi3 (t1, mask, vt));
20024
20025           /* Multiply the shuffle indicies by two.  */
20026           t1 = expand_simple_binop (maskmode, PLUS, t1, t1, t1, 1,
20027                                     OPTAB_DIRECT);
20028
20029           /* Add one to the odd shuffle indicies:
20030                 t1 = { A*2, A*2+1, B*2, B*2+1, ... }.  */
20031           for (i = 0; i < w / 2; ++i)
20032             {
20033               vec[i * 2] = const0_rtx;
20034               vec[i * 2 + 1] = const1_rtx;
20035             }
20036           vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
20037           vt = validize_mem (force_const_mem (maskmode, vt));
20038           t1 = expand_simple_binop (maskmode, PLUS, t1, vt, t1, 1,
20039                                     OPTAB_DIRECT);
20040
20041           /* Continue as if V8SImode (resp. V32QImode) was used initially.  */
20042           operands[3] = mask = t1;
20043           target = gen_lowpart (mode, target);
20044           op0 = gen_lowpart (mode, op0);
20045           op1 = gen_lowpart (mode, op1);
20046         }
20047
20048       switch (mode)
20049         {
20050         case V8SImode:
20051           /* The VPERMD and VPERMPS instructions already properly ignore
20052              the high bits of the shuffle elements.  No need for us to
20053              perform an AND ourselves.  */
20054           if (one_operand_shuffle)
20055             emit_insn (gen_avx2_permvarv8si (target, op0, mask));
20056           else
20057             {
20058               t1 = gen_reg_rtx (V8SImode);
20059               t2 = gen_reg_rtx (V8SImode);
20060               emit_insn (gen_avx2_permvarv8si (t1, op0, mask));
20061               emit_insn (gen_avx2_permvarv8si (t2, op1, mask));
20062               goto merge_two;
20063             }
20064           return;
20065
20066         case V8SFmode:
20067           mask = gen_lowpart (V8SFmode, mask);
20068           if (one_operand_shuffle)
20069             emit_insn (gen_avx2_permvarv8sf (target, op0, mask));
20070           else
20071             {
20072               t1 = gen_reg_rtx (V8SFmode);
20073               t2 = gen_reg_rtx (V8SFmode);
20074               emit_insn (gen_avx2_permvarv8sf (t1, op0, mask));
20075               emit_insn (gen_avx2_permvarv8sf (t2, op1, mask));
20076               goto merge_two;
20077             }
20078           return;
20079
20080         case V4SImode:
20081           /* By combining the two 128-bit input vectors into one 256-bit
20082              input vector, we can use VPERMD and VPERMPS for the full
20083              two-operand shuffle.  */
20084           t1 = gen_reg_rtx (V8SImode);
20085           t2 = gen_reg_rtx (V8SImode);
20086           emit_insn (gen_avx_vec_concatv8si (t1, op0, op1));
20087           emit_insn (gen_avx_vec_concatv8si (t2, mask, mask));
20088           emit_insn (gen_avx2_permvarv8si (t1, t1, t2));
20089           emit_insn (gen_avx_vextractf128v8si (target, t1, const0_rtx));
20090           return;
20091
20092         case V4SFmode:
20093           t1 = gen_reg_rtx (V8SFmode);
20094           t2 = gen_reg_rtx (V8SImode);
20095           mask = gen_lowpart (V4SImode, mask);
20096           emit_insn (gen_avx_vec_concatv8sf (t1, op0, op1));
20097           emit_insn (gen_avx_vec_concatv8si (t2, mask, mask));
20098           emit_insn (gen_avx2_permvarv8sf (t1, t1, t2));
20099           emit_insn (gen_avx_vextractf128v8sf (target, t1, const0_rtx));
20100           return;
20101
20102         case V32QImode:
20103           t1 = gen_reg_rtx (V32QImode);
20104           t2 = gen_reg_rtx (V32QImode);
20105           t3 = gen_reg_rtx (V32QImode);
20106           vt2 = GEN_INT (128);
20107           for (i = 0; i < 32; i++)
20108             vec[i] = vt2;
20109           vt = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, vec));
20110           vt = force_reg (V32QImode, vt);
20111           for (i = 0; i < 32; i++)
20112             vec[i] = i < 16 ? vt2 : const0_rtx;
20113           vt2 = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, vec));
20114           vt2 = force_reg (V32QImode, vt2);
20115           /* From mask create two adjusted masks, which contain the same
20116              bits as mask in the low 7 bits of each vector element.
20117              The first mask will have the most significant bit clear
20118              if it requests element from the same 128-bit lane
20119              and MSB set if it requests element from the other 128-bit lane.
20120              The second mask will have the opposite values of the MSB,
20121              and additionally will have its 128-bit lanes swapped.
20122              E.g. { 07 12 1e 09 ... | 17 19 05 1f ... } mask vector will have
20123              t1   { 07 92 9e 09 ... | 17 19 85 1f ... } and
20124              t3   { 97 99 05 9f ... | 87 12 1e 89 ... } where each ...
20125              stands for other 12 bytes.  */
20126           /* The bit whether element is from the same lane or the other
20127              lane is bit 4, so shift it up by 3 to the MSB position.  */
20128           emit_insn (gen_ashlv4di3 (gen_lowpart (V4DImode, t1),
20129                                     gen_lowpart (V4DImode, mask),
20130                                     GEN_INT (3)));
20131           /* Clear MSB bits from the mask just in case it had them set.  */
20132           emit_insn (gen_avx2_andnotv32qi3 (t2, vt, mask));
20133           /* After this t1 will have MSB set for elements from other lane.  */
20134           emit_insn (gen_xorv32qi3 (t1, t1, vt2));
20135           /* Clear bits other than MSB.  */
20136           emit_insn (gen_andv32qi3 (t1, t1, vt));
20137           /* Or in the lower bits from mask into t3.  */
20138           emit_insn (gen_iorv32qi3 (t3, t1, t2));
20139           /* And invert MSB bits in t1, so MSB is set for elements from the same
20140              lane.  */
20141           emit_insn (gen_xorv32qi3 (t1, t1, vt));
20142           /* Swap 128-bit lanes in t3.  */
20143           emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t3),
20144                                           gen_lowpart (V4DImode, t3),
20145                                           const2_rtx, GEN_INT (3),
20146                                           const0_rtx, const1_rtx));
20147           /* And or in the lower bits from mask into t1.  */
20148           emit_insn (gen_iorv32qi3 (t1, t1, t2));
20149           if (one_operand_shuffle)
20150             {
20151               /* Each of these shuffles will put 0s in places where
20152                  element from the other 128-bit lane is needed, otherwise
20153                  will shuffle in the requested value.  */
20154               emit_insn (gen_avx2_pshufbv32qi3 (t3, op0, t3));
20155               emit_insn (gen_avx2_pshufbv32qi3 (t1, op0, t1));
20156               /* For t3 the 128-bit lanes are swapped again.  */
20157               emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t3),
20158                                               gen_lowpart (V4DImode, t3),
20159                                               const2_rtx, GEN_INT (3),
20160                                               const0_rtx, const1_rtx));
20161               /* And oring both together leads to the result.  */
20162               emit_insn (gen_iorv32qi3 (target, t1, t3));
20163               return;
20164             }
20165
20166           t4 = gen_reg_rtx (V32QImode);
20167           /* Similarly to the above one_operand_shuffle code,
20168              just for repeated twice for each operand.  merge_two:
20169              code will merge the two results together.  */
20170           emit_insn (gen_avx2_pshufbv32qi3 (t4, op0, t3));
20171           emit_insn (gen_avx2_pshufbv32qi3 (t3, op1, t3));
20172           emit_insn (gen_avx2_pshufbv32qi3 (t2, op0, t1));
20173           emit_insn (gen_avx2_pshufbv32qi3 (t1, op1, t1));
20174           emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t4),
20175                                           gen_lowpart (V4DImode, t4),
20176                                           const2_rtx, GEN_INT (3),
20177                                           const0_rtx, const1_rtx));
20178           emit_insn (gen_avx2_permv4di_1 (gen_lowpart (V4DImode, t3),
20179                                           gen_lowpart (V4DImode, t3),
20180                                           const2_rtx, GEN_INT (3),
20181                                           const0_rtx, const1_rtx));
20182           emit_insn (gen_iorv32qi3 (t4, t2, t4));
20183           emit_insn (gen_iorv32qi3 (t3, t1, t3));
20184           t1 = t4;
20185           t2 = t3;
20186           goto merge_two;
20187
20188         default:
20189           gcc_assert (GET_MODE_SIZE (mode) <= 16);
20190           break;
20191         }
20192     }
20193
20194   if (TARGET_XOP)
20195     {
20196       /* The XOP VPPERM insn supports three inputs.  By ignoring the 
20197          one_operand_shuffle special case, we avoid creating another
20198          set of constant vectors in memory.  */
20199       one_operand_shuffle = false;
20200
20201       /* mask = mask & {2*w-1, ...} */
20202       vt = GEN_INT (2*w - 1);
20203     }
20204   else
20205     {
20206       /* mask = mask & {w-1, ...} */
20207       vt = GEN_INT (w - 1);
20208     }
20209
20210   for (i = 0; i < w; i++)
20211     vec[i] = vt;
20212   vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
20213   mask = expand_simple_binop (maskmode, AND, mask, vt,
20214                               NULL_RTX, 0, OPTAB_DIRECT);
20215
20216   /* For non-QImode operations, convert the word permutation control
20217      into a byte permutation control.  */
20218   if (mode != V16QImode)
20219     {
20220       mask = expand_simple_binop (maskmode, ASHIFT, mask,
20221                                   GEN_INT (exact_log2 (e)),
20222                                   NULL_RTX, 0, OPTAB_DIRECT);
20223
20224       /* Convert mask to vector of chars.  */
20225       mask = force_reg (V16QImode, gen_lowpart (V16QImode, mask));
20226
20227       /* Replicate each of the input bytes into byte positions:
20228          (v2di) --> {0,0,0,0,0,0,0,0, 8,8,8,8,8,8,8,8}
20229          (v4si) --> {0,0,0,0, 4,4,4,4, 8,8,8,8, 12,12,12,12}
20230          (v8hi) --> {0,0, 2,2, 4,4, 6,6, ...}.  */
20231       for (i = 0; i < 16; ++i)
20232         vec[i] = GEN_INT (i/e * e);
20233       vt = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, vec));
20234       vt = validize_mem (force_const_mem (V16QImode, vt));
20235       if (TARGET_XOP)
20236         emit_insn (gen_xop_pperm (mask, mask, mask, vt));
20237       else
20238         emit_insn (gen_ssse3_pshufbv16qi3 (mask, mask, vt));
20239
20240       /* Convert it into the byte positions by doing
20241          mask = mask + {0,1,..,16/w, 0,1,..,16/w, ...}  */
20242       for (i = 0; i < 16; ++i)
20243         vec[i] = GEN_INT (i % e);
20244       vt = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, vec));
20245       vt = validize_mem (force_const_mem (V16QImode, vt));
20246       emit_insn (gen_addv16qi3 (mask, mask, vt));
20247     }
20248
20249   /* The actual shuffle operations all operate on V16QImode.  */
20250   op0 = gen_lowpart (V16QImode, op0);
20251   op1 = gen_lowpart (V16QImode, op1);
20252   target = gen_lowpart (V16QImode, target);
20253
20254   if (TARGET_XOP)
20255     {
20256       emit_insn (gen_xop_pperm (target, op0, op1, mask));
20257     }
20258   else if (one_operand_shuffle)
20259     {
20260       emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, mask));
20261     }
20262   else
20263     {
20264       rtx xops[6];
20265       bool ok;
20266
20267       /* Shuffle the two input vectors independently.  */
20268       t1 = gen_reg_rtx (V16QImode);
20269       t2 = gen_reg_rtx (V16QImode);
20270       emit_insn (gen_ssse3_pshufbv16qi3 (t1, op0, mask));
20271       emit_insn (gen_ssse3_pshufbv16qi3 (t2, op1, mask));
20272
20273  merge_two:
20274       /* Then merge them together.  The key is whether any given control
20275          element contained a bit set that indicates the second word.  */
20276       mask = operands[3];
20277       vt = GEN_INT (w);
20278       if (maskmode == V2DImode && !TARGET_SSE4_1)
20279         {
20280           /* Without SSE4.1, we don't have V2DImode EQ.  Perform one
20281              more shuffle to convert the V2DI input mask into a V4SI
20282              input mask.  At which point the masking that expand_int_vcond
20283              will work as desired.  */
20284           rtx t3 = gen_reg_rtx (V4SImode);
20285           emit_insn (gen_sse2_pshufd_1 (t3, gen_lowpart (V4SImode, mask),
20286                                         const0_rtx, const0_rtx,
20287                                         const2_rtx, const2_rtx));
20288           mask = t3;
20289           maskmode = V4SImode;
20290           e = w = 4;
20291         }
20292
20293       for (i = 0; i < w; i++)
20294         vec[i] = vt;
20295       vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
20296       vt = force_reg (maskmode, vt);
20297       mask = expand_simple_binop (maskmode, AND, mask, vt,
20298                                   NULL_RTX, 0, OPTAB_DIRECT);
20299
20300       xops[0] = gen_lowpart (mode, operands[0]);
20301       xops[1] = gen_lowpart (mode, t2);
20302       xops[2] = gen_lowpart (mode, t1);
20303       xops[3] = gen_rtx_EQ (maskmode, mask, vt);
20304       xops[4] = mask;
20305       xops[5] = vt;
20306       ok = ix86_expand_int_vcond (xops);
20307       gcc_assert (ok);
20308     }
20309 }
20310
20311 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
20312    true if we should do zero extension, else sign extension.  HIGH_P is
20313    true if we want the N/2 high elements, else the low elements.  */
20314
20315 void
20316 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
20317 {
20318   enum machine_mode imode = GET_MODE (operands[1]);
20319   rtx tmp, dest;
20320
20321   if (TARGET_SSE4_1)
20322     {
20323       rtx (*unpack)(rtx, rtx);
20324       rtx (*extract)(rtx, rtx) = NULL;
20325       enum machine_mode halfmode = BLKmode;
20326
20327       switch (imode)
20328         {
20329         case V32QImode:
20330           if (unsigned_p)
20331             unpack = gen_avx2_zero_extendv16qiv16hi2;
20332           else
20333             unpack = gen_avx2_sign_extendv16qiv16hi2;
20334           halfmode = V16QImode;
20335           extract
20336             = high_p ? gen_vec_extract_hi_v32qi : gen_vec_extract_lo_v32qi;
20337           break;
20338         case V16HImode:
20339           if (unsigned_p)
20340             unpack = gen_avx2_zero_extendv8hiv8si2;
20341           else
20342             unpack = gen_avx2_sign_extendv8hiv8si2;
20343           halfmode = V8HImode;
20344           extract
20345             = high_p ? gen_vec_extract_hi_v16hi : gen_vec_extract_lo_v16hi;
20346           break;
20347         case V8SImode:
20348           if (unsigned_p)
20349             unpack = gen_avx2_zero_extendv4siv4di2;
20350           else
20351             unpack = gen_avx2_sign_extendv4siv4di2;
20352           halfmode = V4SImode;
20353           extract
20354             = high_p ? gen_vec_extract_hi_v8si : gen_vec_extract_lo_v8si;
20355           break;
20356         case V16QImode:
20357           if (unsigned_p)
20358             unpack = gen_sse4_1_zero_extendv8qiv8hi2;
20359           else
20360             unpack = gen_sse4_1_sign_extendv8qiv8hi2;
20361           break;
20362         case V8HImode:
20363           if (unsigned_p)
20364             unpack = gen_sse4_1_zero_extendv4hiv4si2;
20365           else
20366             unpack = gen_sse4_1_sign_extendv4hiv4si2;
20367           break;
20368         case V4SImode:
20369           if (unsigned_p)
20370             unpack = gen_sse4_1_zero_extendv2siv2di2;
20371           else
20372             unpack = gen_sse4_1_sign_extendv2siv2di2;
20373           break;
20374         default:
20375           gcc_unreachable ();
20376         }
20377
20378       if (GET_MODE_SIZE (imode) == 32)
20379         {
20380           tmp = gen_reg_rtx (halfmode);
20381           emit_insn (extract (tmp, operands[1]));
20382         }
20383       else if (high_p)
20384         {
20385           /* Shift higher 8 bytes to lower 8 bytes.  */
20386           tmp = gen_reg_rtx (imode);
20387           emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, tmp),
20388                                          gen_lowpart (V1TImode, operands[1]),
20389                                          GEN_INT (64)));
20390         }
20391       else
20392         tmp = operands[1];
20393
20394       emit_insn (unpack (operands[0], tmp));
20395     }
20396   else
20397     {
20398       rtx (*unpack)(rtx, rtx, rtx);
20399
20400       switch (imode)
20401         {
20402         case V16QImode:
20403           if (high_p)
20404             unpack = gen_vec_interleave_highv16qi;
20405           else
20406             unpack = gen_vec_interleave_lowv16qi;
20407           break;
20408         case V8HImode:
20409           if (high_p)
20410             unpack = gen_vec_interleave_highv8hi;
20411           else
20412             unpack = gen_vec_interleave_lowv8hi;
20413           break;
20414         case V4SImode:
20415           if (high_p)
20416             unpack = gen_vec_interleave_highv4si;
20417           else
20418             unpack = gen_vec_interleave_lowv4si;
20419           break;
20420         default:
20421           gcc_unreachable ();
20422         }
20423
20424       dest = gen_lowpart (imode, operands[0]);
20425
20426       if (unsigned_p)
20427         tmp = force_reg (imode, CONST0_RTX (imode));
20428       else
20429         tmp = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
20430                                    operands[1], pc_rtx, pc_rtx);
20431
20432       emit_insn (unpack (dest, operands[1], tmp));
20433     }
20434 }
20435
20436 /* Expand conditional increment or decrement using adb/sbb instructions.
20437    The default case using setcc followed by the conditional move can be
20438    done by generic code.  */
20439 bool
20440 ix86_expand_int_addcc (rtx operands[])
20441 {
20442   enum rtx_code code = GET_CODE (operands[1]);
20443   rtx flags;
20444   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
20445   rtx compare_op;
20446   rtx val = const0_rtx;
20447   bool fpcmp = false;
20448   enum machine_mode mode;
20449   rtx op0 = XEXP (operands[1], 0);
20450   rtx op1 = XEXP (operands[1], 1);
20451
20452   if (operands[3] != const1_rtx
20453       && operands[3] != constm1_rtx)
20454     return false;
20455   if (!ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
20456      return false;
20457   code = GET_CODE (compare_op);
20458
20459   flags = XEXP (compare_op, 0);
20460
20461   if (GET_MODE (flags) == CCFPmode
20462       || GET_MODE (flags) == CCFPUmode)
20463     {
20464       fpcmp = true;
20465       code = ix86_fp_compare_code_to_integer (code);
20466     }
20467
20468   if (code != LTU)
20469     {
20470       val = constm1_rtx;
20471       if (fpcmp)
20472         PUT_CODE (compare_op,
20473                   reverse_condition_maybe_unordered
20474                     (GET_CODE (compare_op)));
20475       else
20476         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
20477     }
20478
20479   mode = GET_MODE (operands[0]);
20480
20481   /* Construct either adc or sbb insn.  */
20482   if ((code == LTU) == (operands[3] == constm1_rtx))
20483     {
20484       switch (mode)
20485         {
20486           case QImode:
20487             insn = gen_subqi3_carry;
20488             break;
20489           case HImode:
20490             insn = gen_subhi3_carry;
20491             break;
20492           case SImode:
20493             insn = gen_subsi3_carry;
20494             break;
20495           case DImode:
20496             insn = gen_subdi3_carry;
20497             break;
20498           default:
20499             gcc_unreachable ();
20500         }
20501     }
20502   else
20503     {
20504       switch (mode)
20505         {
20506           case QImode:
20507             insn = gen_addqi3_carry;
20508             break;
20509           case HImode:
20510             insn = gen_addhi3_carry;
20511             break;
20512           case SImode:
20513             insn = gen_addsi3_carry;
20514             break;
20515           case DImode:
20516             insn = gen_adddi3_carry;
20517             break;
20518           default:
20519             gcc_unreachable ();
20520         }
20521     }
20522   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
20523
20524   return true;
20525 }
20526
20527
20528 /* Split operands 0 and 1 into half-mode parts.  Similar to split_double_mode,
20529    but works for floating pointer parameters and nonoffsetable memories.
20530    For pushes, it returns just stack offsets; the values will be saved
20531    in the right order.  Maximally three parts are generated.  */
20532
20533 static int
20534 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
20535 {
20536   int size;
20537
20538   if (!TARGET_64BIT)
20539     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
20540   else
20541     size = (GET_MODE_SIZE (mode) + 4) / 8;
20542
20543   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
20544   gcc_assert (size >= 2 && size <= 4);
20545
20546   /* Optimize constant pool reference to immediates.  This is used by fp
20547      moves, that force all constants to memory to allow combining.  */
20548   if (MEM_P (operand) && MEM_READONLY_P (operand))
20549     {
20550       rtx tmp = maybe_get_pool_constant (operand);
20551       if (tmp)
20552         operand = tmp;
20553     }
20554
20555   if (MEM_P (operand) && !offsettable_memref_p (operand))
20556     {
20557       /* The only non-offsetable memories we handle are pushes.  */
20558       int ok = push_operand (operand, VOIDmode);
20559
20560       gcc_assert (ok);
20561
20562       operand = copy_rtx (operand);
20563       PUT_MODE (operand, Pmode);
20564       parts[0] = parts[1] = parts[2] = parts[3] = operand;
20565       return size;
20566     }
20567
20568   if (GET_CODE (operand) == CONST_VECTOR)
20569     {
20570       enum machine_mode imode = int_mode_for_mode (mode);
20571       /* Caution: if we looked through a constant pool memory above,
20572          the operand may actually have a different mode now.  That's
20573          ok, since we want to pun this all the way back to an integer.  */
20574       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
20575       gcc_assert (operand != NULL);
20576       mode = imode;
20577     }
20578
20579   if (!TARGET_64BIT)
20580     {
20581       if (mode == DImode)
20582         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
20583       else
20584         {
20585           int i;
20586
20587           if (REG_P (operand))
20588             {
20589               gcc_assert (reload_completed);
20590               for (i = 0; i < size; i++)
20591                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
20592             }
20593           else if (offsettable_memref_p (operand))
20594             {
20595               operand = adjust_address (operand, SImode, 0);
20596               parts[0] = operand;
20597               for (i = 1; i < size; i++)
20598                 parts[i] = adjust_address (operand, SImode, 4 * i);
20599             }
20600           else if (GET_CODE (operand) == CONST_DOUBLE)
20601             {
20602               REAL_VALUE_TYPE r;
20603               long l[4];
20604
20605               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
20606               switch (mode)
20607                 {
20608                 case TFmode:
20609                   real_to_target (l, &r, mode);
20610                   parts[3] = gen_int_mode (l[3], SImode);
20611                   parts[2] = gen_int_mode (l[2], SImode);
20612                   break;
20613                 case XFmode:
20614                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
20615                   parts[2] = gen_int_mode (l[2], SImode);
20616                   break;
20617                 case DFmode:
20618                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
20619                   break;
20620                 default:
20621                   gcc_unreachable ();
20622                 }
20623               parts[1] = gen_int_mode (l[1], SImode);
20624               parts[0] = gen_int_mode (l[0], SImode);
20625             }
20626           else
20627             gcc_unreachable ();
20628         }
20629     }
20630   else
20631     {
20632       if (mode == TImode)
20633         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
20634       if (mode == XFmode || mode == TFmode)
20635         {
20636           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
20637           if (REG_P (operand))
20638             {
20639               gcc_assert (reload_completed);
20640               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
20641               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
20642             }
20643           else if (offsettable_memref_p (operand))
20644             {
20645               operand = adjust_address (operand, DImode, 0);
20646               parts[0] = operand;
20647               parts[1] = adjust_address (operand, upper_mode, 8);
20648             }
20649           else if (GET_CODE (operand) == CONST_DOUBLE)
20650             {
20651               REAL_VALUE_TYPE r;
20652               long l[4];
20653
20654               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
20655               real_to_target (l, &r, mode);
20656
20657               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
20658               if (HOST_BITS_PER_WIDE_INT >= 64)
20659                 parts[0]
20660                   = gen_int_mode
20661                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
20662                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
20663                        DImode);
20664               else
20665                 parts[0] = immed_double_const (l[0], l[1], DImode);
20666
20667               if (upper_mode == SImode)
20668                 parts[1] = gen_int_mode (l[2], SImode);
20669               else if (HOST_BITS_PER_WIDE_INT >= 64)
20670                 parts[1]
20671                   = gen_int_mode
20672                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
20673                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
20674                        DImode);
20675               else
20676                 parts[1] = immed_double_const (l[2], l[3], DImode);
20677             }
20678           else
20679             gcc_unreachable ();
20680         }
20681     }
20682
20683   return size;
20684 }
20685
20686 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
20687    Return false when normal moves are needed; true when all required
20688    insns have been emitted.  Operands 2-4 contain the input values
20689    int the correct order; operands 5-7 contain the output values.  */
20690
20691 void
20692 ix86_split_long_move (rtx operands[])
20693 {
20694   rtx part[2][4];
20695   int nparts, i, j;
20696   int push = 0;
20697   int collisions = 0;
20698   enum machine_mode mode = GET_MODE (operands[0]);
20699   bool collisionparts[4];
20700
20701   /* The DFmode expanders may ask us to move double.
20702      For 64bit target this is single move.  By hiding the fact
20703      here we simplify i386.md splitters.  */
20704   if (TARGET_64BIT && GET_MODE_SIZE (GET_MODE (operands[0])) == 8)
20705     {
20706       /* Optimize constant pool reference to immediates.  This is used by
20707          fp moves, that force all constants to memory to allow combining.  */
20708
20709       if (MEM_P (operands[1])
20710           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
20711           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
20712         operands[1] = get_pool_constant (XEXP (operands[1], 0));
20713       if (push_operand (operands[0], VOIDmode))
20714         {
20715           operands[0] = copy_rtx (operands[0]);
20716           PUT_MODE (operands[0], Pmode);
20717         }
20718       else
20719         operands[0] = gen_lowpart (DImode, operands[0]);
20720       operands[1] = gen_lowpart (DImode, operands[1]);
20721       emit_move_insn (operands[0], operands[1]);
20722       return;
20723     }
20724
20725   /* The only non-offsettable memory we handle is push.  */
20726   if (push_operand (operands[0], VOIDmode))
20727     push = 1;
20728   else
20729     gcc_assert (!MEM_P (operands[0])
20730                 || offsettable_memref_p (operands[0]));
20731
20732   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
20733   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
20734
20735   /* When emitting push, take care for source operands on the stack.  */
20736   if (push && MEM_P (operands[1])
20737       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
20738     {
20739       rtx src_base = XEXP (part[1][nparts - 1], 0);
20740
20741       /* Compensate for the stack decrement by 4.  */
20742       if (!TARGET_64BIT && nparts == 3
20743           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
20744         src_base = plus_constant (src_base, 4);
20745
20746       /* src_base refers to the stack pointer and is
20747          automatically decreased by emitted push.  */
20748       for (i = 0; i < nparts; i++)
20749         part[1][i] = change_address (part[1][i],
20750                                      GET_MODE (part[1][i]), src_base);
20751     }
20752
20753   /* We need to do copy in the right order in case an address register
20754      of the source overlaps the destination.  */
20755   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
20756     {
20757       rtx tmp;
20758
20759       for (i = 0; i < nparts; i++)
20760         {
20761           collisionparts[i]
20762             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
20763           if (collisionparts[i])
20764             collisions++;
20765         }
20766
20767       /* Collision in the middle part can be handled by reordering.  */
20768       if (collisions == 1 && nparts == 3 && collisionparts [1])
20769         {
20770           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
20771           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
20772         }
20773       else if (collisions == 1
20774                && nparts == 4
20775                && (collisionparts [1] || collisionparts [2]))
20776         {
20777           if (collisionparts [1])
20778             {
20779               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
20780               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
20781             }
20782           else
20783             {
20784               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
20785               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
20786             }
20787         }
20788
20789       /* If there are more collisions, we can't handle it by reordering.
20790          Do an lea to the last part and use only one colliding move.  */
20791       else if (collisions > 1)
20792         {
20793           rtx base;
20794
20795           collisions = 1;
20796
20797           base = part[0][nparts - 1];
20798
20799           /* Handle the case when the last part isn't valid for lea.
20800              Happens in 64-bit mode storing the 12-byte XFmode.  */
20801           if (GET_MODE (base) != Pmode)
20802             base = gen_rtx_REG (Pmode, REGNO (base));
20803
20804           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
20805           part[1][0] = replace_equiv_address (part[1][0], base);
20806           for (i = 1; i < nparts; i++)
20807             {
20808               tmp = plus_constant (base, UNITS_PER_WORD * i);
20809               part[1][i] = replace_equiv_address (part[1][i], tmp);
20810             }
20811         }
20812     }
20813
20814   if (push)
20815     {
20816       if (!TARGET_64BIT)
20817         {
20818           if (nparts == 3)
20819             {
20820               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
20821                 emit_insn (gen_addsi3 (stack_pointer_rtx,
20822                                        stack_pointer_rtx, GEN_INT (-4)));
20823               emit_move_insn (part[0][2], part[1][2]);
20824             }
20825           else if (nparts == 4)
20826             {
20827               emit_move_insn (part[0][3], part[1][3]);
20828               emit_move_insn (part[0][2], part[1][2]);
20829             }
20830         }
20831       else
20832         {
20833           /* In 64bit mode we don't have 32bit push available.  In case this is
20834              register, it is OK - we will just use larger counterpart.  We also
20835              retype memory - these comes from attempt to avoid REX prefix on
20836              moving of second half of TFmode value.  */
20837           if (GET_MODE (part[1][1]) == SImode)
20838             {
20839               switch (GET_CODE (part[1][1]))
20840                 {
20841                 case MEM:
20842                   part[1][1] = adjust_address (part[1][1], DImode, 0);
20843                   break;
20844
20845                 case REG:
20846                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
20847                   break;
20848
20849                 default:
20850                   gcc_unreachable ();
20851                 }
20852
20853               if (GET_MODE (part[1][0]) == SImode)
20854                 part[1][0] = part[1][1];
20855             }
20856         }
20857       emit_move_insn (part[0][1], part[1][1]);
20858       emit_move_insn (part[0][0], part[1][0]);
20859       return;
20860     }
20861
20862   /* Choose correct order to not overwrite the source before it is copied.  */
20863   if ((REG_P (part[0][0])
20864        && REG_P (part[1][1])
20865        && (REGNO (part[0][0]) == REGNO (part[1][1])
20866            || (nparts == 3
20867                && REGNO (part[0][0]) == REGNO (part[1][2]))
20868            || (nparts == 4
20869                && REGNO (part[0][0]) == REGNO (part[1][3]))))
20870       || (collisions > 0
20871           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
20872     {
20873       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
20874         {
20875           operands[2 + i] = part[0][j];
20876           operands[6 + i] = part[1][j];
20877         }
20878     }
20879   else
20880     {
20881       for (i = 0; i < nparts; i++)
20882         {
20883           operands[2 + i] = part[0][i];
20884           operands[6 + i] = part[1][i];
20885         }
20886     }
20887
20888   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
20889   if (optimize_insn_for_size_p ())
20890     {
20891       for (j = 0; j < nparts - 1; j++)
20892         if (CONST_INT_P (operands[6 + j])
20893             && operands[6 + j] != const0_rtx
20894             && REG_P (operands[2 + j]))
20895           for (i = j; i < nparts - 1; i++)
20896             if (CONST_INT_P (operands[7 + i])
20897                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
20898               operands[7 + i] = operands[2 + j];
20899     }
20900
20901   for (i = 0; i < nparts; i++)
20902     emit_move_insn (operands[2 + i], operands[6 + i]);
20903
20904   return;
20905 }
20906
20907 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
20908    left shift by a constant, either using a single shift or
20909    a sequence of add instructions.  */
20910
20911 static void
20912 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
20913 {
20914   rtx (*insn)(rtx, rtx, rtx);
20915
20916   if (count == 1
20917       || (count * ix86_cost->add <= ix86_cost->shift_const
20918           && !optimize_insn_for_size_p ()))
20919     {
20920       insn = mode == DImode ? gen_addsi3 : gen_adddi3;
20921       while (count-- > 0)
20922         emit_insn (insn (operand, operand, operand));
20923     }
20924   else
20925     {
20926       insn = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
20927       emit_insn (insn (operand, operand, GEN_INT (count)));
20928     }
20929 }
20930
20931 void
20932 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
20933 {
20934   rtx (*gen_ashl3)(rtx, rtx, rtx);
20935   rtx (*gen_shld)(rtx, rtx, rtx);
20936   int half_width = GET_MODE_BITSIZE (mode) >> 1;
20937
20938   rtx low[2], high[2];
20939   int count;
20940
20941   if (CONST_INT_P (operands[2]))
20942     {
20943       split_double_mode (mode, operands, 2, low, high);
20944       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
20945
20946       if (count >= half_width)
20947         {
20948           emit_move_insn (high[0], low[1]);
20949           emit_move_insn (low[0], const0_rtx);
20950
20951           if (count > half_width)
20952             ix86_expand_ashl_const (high[0], count - half_width, mode);
20953         }
20954       else
20955         {
20956           gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
20957
20958           if (!rtx_equal_p (operands[0], operands[1]))
20959             emit_move_insn (operands[0], operands[1]);
20960
20961           emit_insn (gen_shld (high[0], low[0], GEN_INT (count)));
20962           ix86_expand_ashl_const (low[0], count, mode);
20963         }
20964       return;
20965     }
20966
20967   split_double_mode (mode, operands, 1, low, high);
20968
20969   gen_ashl3 = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
20970
20971   if (operands[1] == const1_rtx)
20972     {
20973       /* Assuming we've chosen a QImode capable registers, then 1 << N
20974          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
20975       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
20976         {
20977           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
20978
20979           ix86_expand_clear (low[0]);
20980           ix86_expand_clear (high[0]);
20981           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (half_width)));
20982
20983           d = gen_lowpart (QImode, low[0]);
20984           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
20985           s = gen_rtx_EQ (QImode, flags, const0_rtx);
20986           emit_insn (gen_rtx_SET (VOIDmode, d, s));
20987
20988           d = gen_lowpart (QImode, high[0]);
20989           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
20990           s = gen_rtx_NE (QImode, flags, const0_rtx);
20991           emit_insn (gen_rtx_SET (VOIDmode, d, s));
20992         }
20993
20994       /* Otherwise, we can get the same results by manually performing
20995          a bit extract operation on bit 5/6, and then performing the two
20996          shifts.  The two methods of getting 0/1 into low/high are exactly
20997          the same size.  Avoiding the shift in the bit extract case helps
20998          pentium4 a bit; no one else seems to care much either way.  */
20999       else
21000         {
21001           enum machine_mode half_mode;
21002           rtx (*gen_lshr3)(rtx, rtx, rtx);
21003           rtx (*gen_and3)(rtx, rtx, rtx);
21004           rtx (*gen_xor3)(rtx, rtx, rtx);
21005           HOST_WIDE_INT bits;
21006           rtx x;
21007
21008           if (mode == DImode)
21009             {
21010               half_mode = SImode;
21011               gen_lshr3 = gen_lshrsi3;
21012               gen_and3 = gen_andsi3;
21013               gen_xor3 = gen_xorsi3;
21014               bits = 5;
21015             }
21016           else
21017             {
21018               half_mode = DImode;
21019               gen_lshr3 = gen_lshrdi3;
21020               gen_and3 = gen_anddi3;
21021               gen_xor3 = gen_xordi3;
21022               bits = 6;
21023             }
21024
21025           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
21026             x = gen_rtx_ZERO_EXTEND (half_mode, operands[2]);
21027           else
21028             x = gen_lowpart (half_mode, operands[2]);
21029           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
21030
21031           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (bits)));
21032           emit_insn (gen_and3 (high[0], high[0], const1_rtx));
21033           emit_move_insn (low[0], high[0]);
21034           emit_insn (gen_xor3 (low[0], low[0], const1_rtx));
21035         }
21036
21037       emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
21038       emit_insn (gen_ashl3 (high[0], high[0], operands[2]));
21039       return;
21040     }
21041
21042   if (operands[1] == constm1_rtx)
21043     {
21044       /* For -1 << N, we can avoid the shld instruction, because we
21045          know that we're shifting 0...31/63 ones into a -1.  */
21046       emit_move_insn (low[0], constm1_rtx);
21047       if (optimize_insn_for_size_p ())
21048         emit_move_insn (high[0], low[0]);
21049       else
21050         emit_move_insn (high[0], constm1_rtx);
21051     }
21052   else
21053     {
21054       gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
21055
21056       if (!rtx_equal_p (operands[0], operands[1]))
21057         emit_move_insn (operands[0], operands[1]);
21058
21059       split_double_mode (mode, operands, 1, low, high);
21060       emit_insn (gen_shld (high[0], low[0], operands[2]));
21061     }
21062
21063   emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
21064
21065   if (TARGET_CMOVE && scratch)
21066     {
21067       rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
21068         = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
21069
21070       ix86_expand_clear (scratch);
21071       emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2], scratch));
21072     }
21073   else
21074     {
21075       rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
21076         = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
21077
21078       emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
21079     }
21080 }
21081
21082 void
21083 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
21084 {
21085   rtx (*gen_ashr3)(rtx, rtx, rtx)
21086     = mode == DImode ? gen_ashrsi3 : gen_ashrdi3;
21087   rtx (*gen_shrd)(rtx, rtx, rtx);
21088   int half_width = GET_MODE_BITSIZE (mode) >> 1;
21089
21090   rtx low[2], high[2];
21091   int count;
21092
21093   if (CONST_INT_P (operands[2]))
21094     {
21095       split_double_mode (mode, operands, 2, low, high);
21096       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
21097
21098       if (count == GET_MODE_BITSIZE (mode) - 1)
21099         {
21100           emit_move_insn (high[0], high[1]);
21101           emit_insn (gen_ashr3 (high[0], high[0],
21102                                 GEN_INT (half_width - 1)));
21103           emit_move_insn (low[0], high[0]);
21104
21105         }
21106       else if (count >= half_width)
21107         {
21108           emit_move_insn (low[0], high[1]);
21109           emit_move_insn (high[0], low[0]);
21110           emit_insn (gen_ashr3 (high[0], high[0],
21111                                 GEN_INT (half_width - 1)));
21112
21113           if (count > half_width)
21114             emit_insn (gen_ashr3 (low[0], low[0],
21115                                   GEN_INT (count - half_width)));
21116         }
21117       else
21118         {
21119           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
21120
21121           if (!rtx_equal_p (operands[0], operands[1]))
21122             emit_move_insn (operands[0], operands[1]);
21123
21124           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
21125           emit_insn (gen_ashr3 (high[0], high[0], GEN_INT (count)));
21126         }
21127     }
21128   else
21129     {
21130       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
21131
21132      if (!rtx_equal_p (operands[0], operands[1]))
21133         emit_move_insn (operands[0], operands[1]);
21134
21135       split_double_mode (mode, operands, 1, low, high);
21136
21137       emit_insn (gen_shrd (low[0], high[0], operands[2]));
21138       emit_insn (gen_ashr3 (high[0], high[0], operands[2]));
21139
21140       if (TARGET_CMOVE && scratch)
21141         {
21142           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
21143             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
21144
21145           emit_move_insn (scratch, high[0]);
21146           emit_insn (gen_ashr3 (scratch, scratch,
21147                                 GEN_INT (half_width - 1)));
21148           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
21149                                           scratch));
21150         }
21151       else
21152         {
21153           rtx (*gen_x86_shift_adj_3)(rtx, rtx, rtx)
21154             = mode == DImode ? gen_x86_shiftsi_adj_3 : gen_x86_shiftdi_adj_3;
21155
21156           emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
21157         }
21158     }
21159 }
21160
21161 void
21162 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
21163 {
21164   rtx (*gen_lshr3)(rtx, rtx, rtx)
21165     = mode == DImode ? gen_lshrsi3 : gen_lshrdi3;
21166   rtx (*gen_shrd)(rtx, rtx, rtx);
21167   int half_width = GET_MODE_BITSIZE (mode) >> 1;
21168
21169   rtx low[2], high[2];
21170   int count;
21171
21172   if (CONST_INT_P (operands[2]))
21173     {
21174       split_double_mode (mode, operands, 2, low, high);
21175       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
21176
21177       if (count >= half_width)
21178         {
21179           emit_move_insn (low[0], high[1]);
21180           ix86_expand_clear (high[0]);
21181
21182           if (count > half_width)
21183             emit_insn (gen_lshr3 (low[0], low[0],
21184                                   GEN_INT (count - half_width)));
21185         }
21186       else
21187         {
21188           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
21189
21190           if (!rtx_equal_p (operands[0], operands[1]))
21191             emit_move_insn (operands[0], operands[1]);
21192
21193           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
21194           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (count)));
21195         }
21196     }
21197   else
21198     {
21199       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
21200
21201       if (!rtx_equal_p (operands[0], operands[1]))
21202         emit_move_insn (operands[0], operands[1]);
21203
21204       split_double_mode (mode, operands, 1, low, high);
21205
21206       emit_insn (gen_shrd (low[0], high[0], operands[2]));
21207       emit_insn (gen_lshr3 (high[0], high[0], operands[2]));
21208
21209       if (TARGET_CMOVE && scratch)
21210         {
21211           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
21212             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
21213
21214           ix86_expand_clear (scratch);
21215           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
21216                                           scratch));
21217         }
21218       else
21219         {
21220           rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
21221             = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
21222
21223           emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
21224         }
21225     }
21226 }
21227
21228 /* Predict just emitted jump instruction to be taken with probability PROB.  */
21229 static void
21230 predict_jump (int prob)
21231 {
21232   rtx insn = get_last_insn ();
21233   gcc_assert (JUMP_P (insn));
21234   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
21235 }
21236
21237 /* Helper function for the string operations below.  Dest VARIABLE whether
21238    it is aligned to VALUE bytes.  If true, jump to the label.  */
21239 static rtx
21240 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
21241 {
21242   rtx label = gen_label_rtx ();
21243   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
21244   if (GET_MODE (variable) == DImode)
21245     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
21246   else
21247     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
21248   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
21249                            1, label);
21250   if (epilogue)
21251     predict_jump (REG_BR_PROB_BASE * 50 / 100);
21252   else
21253     predict_jump (REG_BR_PROB_BASE * 90 / 100);
21254   return label;
21255 }
21256
21257 /* Adjust COUNTER by the VALUE.  */
21258 static void
21259 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
21260 {
21261   rtx (*gen_add)(rtx, rtx, rtx)
21262     = GET_MODE (countreg) == DImode ? gen_adddi3 : gen_addsi3;
21263
21264   emit_insn (gen_add (countreg, countreg, GEN_INT (-value)));
21265 }
21266
21267 /* Zero extend possibly SImode EXP to Pmode register.  */
21268 rtx
21269 ix86_zero_extend_to_Pmode (rtx exp)
21270 {
21271   rtx r;
21272   if (GET_MODE (exp) == VOIDmode)
21273     return force_reg (Pmode, exp);
21274   if (GET_MODE (exp) == Pmode)
21275     return copy_to_mode_reg (Pmode, exp);
21276   r = gen_reg_rtx (Pmode);
21277   emit_insn (gen_zero_extendsidi2 (r, exp));
21278   return r;
21279 }
21280
21281 /* Divide COUNTREG by SCALE.  */
21282 static rtx
21283 scale_counter (rtx countreg, int scale)
21284 {
21285   rtx sc;
21286
21287   if (scale == 1)
21288     return countreg;
21289   if (CONST_INT_P (countreg))
21290     return GEN_INT (INTVAL (countreg) / scale);
21291   gcc_assert (REG_P (countreg));
21292
21293   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
21294                             GEN_INT (exact_log2 (scale)),
21295                             NULL, 1, OPTAB_DIRECT);
21296   return sc;
21297 }
21298
21299 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
21300    DImode for constant loop counts.  */
21301
21302 static enum machine_mode
21303 counter_mode (rtx count_exp)
21304 {
21305   if (GET_MODE (count_exp) != VOIDmode)
21306     return GET_MODE (count_exp);
21307   if (!CONST_INT_P (count_exp))
21308     return Pmode;
21309   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
21310     return DImode;
21311   return SImode;
21312 }
21313
21314 /* When SRCPTR is non-NULL, output simple loop to move memory
21315    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
21316    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
21317    equivalent loop to set memory by VALUE (supposed to be in MODE).
21318
21319    The size is rounded down to whole number of chunk size moved at once.
21320    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
21321
21322
21323 static void
21324 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
21325                                rtx destptr, rtx srcptr, rtx value,
21326                                rtx count, enum machine_mode mode, int unroll,
21327                                int expected_size)
21328 {
21329   rtx out_label, top_label, iter, tmp;
21330   enum machine_mode iter_mode = counter_mode (count);
21331   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
21332   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
21333   rtx size;
21334   rtx x_addr;
21335   rtx y_addr;
21336   int i;
21337
21338   top_label = gen_label_rtx ();
21339   out_label = gen_label_rtx ();
21340   iter = gen_reg_rtx (iter_mode);
21341
21342   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
21343                               NULL, 1, OPTAB_DIRECT);
21344   /* Those two should combine.  */
21345   if (piece_size == const1_rtx)
21346     {
21347       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
21348                                true, out_label);
21349       predict_jump (REG_BR_PROB_BASE * 10 / 100);
21350     }
21351   emit_move_insn (iter, const0_rtx);
21352
21353   emit_label (top_label);
21354
21355   tmp = convert_modes (Pmode, iter_mode, iter, true);
21356   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
21357   destmem = change_address (destmem, mode, x_addr);
21358
21359   if (srcmem)
21360     {
21361       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
21362       srcmem = change_address (srcmem, mode, y_addr);
21363
21364       /* When unrolling for chips that reorder memory reads and writes,
21365          we can save registers by using single temporary.
21366          Also using 4 temporaries is overkill in 32bit mode.  */
21367       if (!TARGET_64BIT && 0)
21368         {
21369           for (i = 0; i < unroll; i++)
21370             {
21371               if (i)
21372                 {
21373                   destmem =
21374                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
21375                   srcmem =
21376                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
21377                 }
21378               emit_move_insn (destmem, srcmem);
21379             }
21380         }
21381       else
21382         {
21383           rtx tmpreg[4];
21384           gcc_assert (unroll <= 4);
21385           for (i = 0; i < unroll; i++)
21386             {
21387               tmpreg[i] = gen_reg_rtx (mode);
21388               if (i)
21389                 {
21390                   srcmem =
21391                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
21392                 }
21393               emit_move_insn (tmpreg[i], srcmem);
21394             }
21395           for (i = 0; i < unroll; i++)
21396             {
21397               if (i)
21398                 {
21399                   destmem =
21400                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
21401                 }
21402               emit_move_insn (destmem, tmpreg[i]);
21403             }
21404         }
21405     }
21406   else
21407     for (i = 0; i < unroll; i++)
21408       {
21409         if (i)
21410           destmem =
21411             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
21412         emit_move_insn (destmem, value);
21413       }
21414
21415   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
21416                              true, OPTAB_LIB_WIDEN);
21417   if (tmp != iter)
21418     emit_move_insn (iter, tmp);
21419
21420   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
21421                            true, top_label);
21422   if (expected_size != -1)
21423     {
21424       expected_size /= GET_MODE_SIZE (mode) * unroll;
21425       if (expected_size == 0)
21426         predict_jump (0);
21427       else if (expected_size > REG_BR_PROB_BASE)
21428         predict_jump (REG_BR_PROB_BASE - 1);
21429       else
21430         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
21431     }
21432   else
21433     predict_jump (REG_BR_PROB_BASE * 80 / 100);
21434   iter = ix86_zero_extend_to_Pmode (iter);
21435   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
21436                              true, OPTAB_LIB_WIDEN);
21437   if (tmp != destptr)
21438     emit_move_insn (destptr, tmp);
21439   if (srcptr)
21440     {
21441       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
21442                                  true, OPTAB_LIB_WIDEN);
21443       if (tmp != srcptr)
21444         emit_move_insn (srcptr, tmp);
21445     }
21446   emit_label (out_label);
21447 }
21448
21449 /* Output "rep; mov" instruction.
21450    Arguments have same meaning as for previous function */
21451 static void
21452 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
21453                            rtx destptr, rtx srcptr,
21454                            rtx count,
21455                            enum machine_mode mode)
21456 {
21457   rtx destexp;
21458   rtx srcexp;
21459   rtx countreg;
21460   HOST_WIDE_INT rounded_count;
21461
21462   /* If the size is known, it is shorter to use rep movs.  */
21463   if (mode == QImode && CONST_INT_P (count)
21464       && !(INTVAL (count) & 3))
21465     mode = SImode;
21466
21467   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
21468     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
21469   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
21470     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
21471   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
21472   if (mode != QImode)
21473     {
21474       destexp = gen_rtx_ASHIFT (Pmode, countreg,
21475                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
21476       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
21477       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
21478                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
21479       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
21480     }
21481   else
21482     {
21483       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
21484       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
21485     }
21486   if (CONST_INT_P (count))
21487     {
21488       rounded_count = (INTVAL (count)
21489                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
21490       destmem = shallow_copy_rtx (destmem);
21491       srcmem = shallow_copy_rtx (srcmem);
21492       set_mem_size (destmem, rounded_count);
21493       set_mem_size (srcmem, rounded_count);
21494     }
21495   else
21496     {
21497       if (MEM_SIZE_KNOWN_P (destmem))
21498         clear_mem_size (destmem);
21499       if (MEM_SIZE_KNOWN_P (srcmem))
21500         clear_mem_size (srcmem);
21501     }
21502   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
21503                           destexp, srcexp));
21504 }
21505
21506 /* Output "rep; stos" instruction.
21507    Arguments have same meaning as for previous function */
21508 static void
21509 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
21510                             rtx count, enum machine_mode mode,
21511                             rtx orig_value)
21512 {
21513   rtx destexp;
21514   rtx countreg;
21515   HOST_WIDE_INT rounded_count;
21516
21517   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
21518     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
21519   value = force_reg (mode, gen_lowpart (mode, value));
21520   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
21521   if (mode != QImode)
21522     {
21523       destexp = gen_rtx_ASHIFT (Pmode, countreg,
21524                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
21525       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
21526     }
21527   else
21528     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
21529   if (orig_value == const0_rtx && CONST_INT_P (count))
21530     {
21531       rounded_count = (INTVAL (count)
21532                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
21533       destmem = shallow_copy_rtx (destmem);
21534       set_mem_size (destmem, rounded_count);
21535     }
21536   else if (MEM_SIZE_KNOWN_P (destmem))
21537     clear_mem_size (destmem);
21538   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
21539 }
21540
21541 static void
21542 emit_strmov (rtx destmem, rtx srcmem,
21543              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
21544 {
21545   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
21546   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
21547   emit_insn (gen_strmov (destptr, dest, srcptr, src));
21548 }
21549
21550 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
21551 static void
21552 expand_movmem_epilogue (rtx destmem, rtx srcmem,
21553                         rtx destptr, rtx srcptr, rtx count, int max_size)
21554 {
21555   rtx src, dest;
21556   if (CONST_INT_P (count))
21557     {
21558       HOST_WIDE_INT countval = INTVAL (count);
21559       int offset = 0;
21560
21561       if ((countval & 0x10) && max_size > 16)
21562         {
21563           if (TARGET_64BIT)
21564             {
21565               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
21566               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
21567             }
21568           else
21569             gcc_unreachable ();
21570           offset += 16;
21571         }
21572       if ((countval & 0x08) && max_size > 8)
21573         {
21574           if (TARGET_64BIT)
21575             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
21576           else
21577             {
21578               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
21579               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
21580             }
21581           offset += 8;
21582         }
21583       if ((countval & 0x04) && max_size > 4)
21584         {
21585           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
21586           offset += 4;
21587         }
21588       if ((countval & 0x02) && max_size > 2)
21589         {
21590           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
21591           offset += 2;
21592         }
21593       if ((countval & 0x01) && max_size > 1)
21594         {
21595           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
21596           offset += 1;
21597         }
21598       return;
21599     }
21600   if (max_size > 8)
21601     {
21602       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
21603                                     count, 1, OPTAB_DIRECT);
21604       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
21605                                      count, QImode, 1, 4);
21606       return;
21607     }
21608
21609   /* When there are stringops, we can cheaply increase dest and src pointers.
21610      Otherwise we save code size by maintaining offset (zero is readily
21611      available from preceding rep operation) and using x86 addressing modes.
21612    */
21613   if (TARGET_SINGLE_STRINGOP)
21614     {
21615       if (max_size > 4)
21616         {
21617           rtx label = ix86_expand_aligntest (count, 4, true);
21618           src = change_address (srcmem, SImode, srcptr);
21619           dest = change_address (destmem, SImode, destptr);
21620           emit_insn (gen_strmov (destptr, dest, srcptr, src));
21621           emit_label (label);
21622           LABEL_NUSES (label) = 1;
21623         }
21624       if (max_size > 2)
21625         {
21626           rtx label = ix86_expand_aligntest (count, 2, true);
21627           src = change_address (srcmem, HImode, srcptr);
21628           dest = change_address (destmem, HImode, destptr);
21629           emit_insn (gen_strmov (destptr, dest, srcptr, src));
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           src = change_address (srcmem, QImode, srcptr);
21637           dest = change_address (destmem, QImode, destptr);
21638           emit_insn (gen_strmov (destptr, dest, srcptr, src));
21639           emit_label (label);
21640           LABEL_NUSES (label) = 1;
21641         }
21642     }
21643   else
21644     {
21645       rtx offset = force_reg (Pmode, const0_rtx);
21646       rtx tmp;
21647
21648       if (max_size > 4)
21649         {
21650           rtx label = ix86_expand_aligntest (count, 4, true);
21651           src = change_address (srcmem, SImode, srcptr);
21652           dest = change_address (destmem, SImode, destptr);
21653           emit_move_insn (dest, src);
21654           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
21655                                      true, OPTAB_LIB_WIDEN);
21656           if (tmp != offset)
21657             emit_move_insn (offset, tmp);
21658           emit_label (label);
21659           LABEL_NUSES (label) = 1;
21660         }
21661       if (max_size > 2)
21662         {
21663           rtx label = ix86_expand_aligntest (count, 2, true);
21664           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
21665           src = change_address (srcmem, HImode, tmp);
21666           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
21667           dest = change_address (destmem, HImode, tmp);
21668           emit_move_insn (dest, src);
21669           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
21670                                      true, OPTAB_LIB_WIDEN);
21671           if (tmp != offset)
21672             emit_move_insn (offset, tmp);
21673           emit_label (label);
21674           LABEL_NUSES (label) = 1;
21675         }
21676       if (max_size > 1)
21677         {
21678           rtx label = ix86_expand_aligntest (count, 1, true);
21679           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
21680           src = change_address (srcmem, QImode, tmp);
21681           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
21682           dest = change_address (destmem, QImode, tmp);
21683           emit_move_insn (dest, src);
21684           emit_label (label);
21685           LABEL_NUSES (label) = 1;
21686         }
21687     }
21688 }
21689
21690 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
21691 static void
21692 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
21693                                  rtx count, int max_size)
21694 {
21695   count =
21696     expand_simple_binop (counter_mode (count), AND, count,
21697                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
21698   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
21699                                  gen_lowpart (QImode, value), count, QImode,
21700                                  1, max_size / 2);
21701 }
21702
21703 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
21704 static void
21705 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
21706 {
21707   rtx dest;
21708
21709   if (CONST_INT_P (count))
21710     {
21711       HOST_WIDE_INT countval = INTVAL (count);
21712       int offset = 0;
21713
21714       if ((countval & 0x10) && max_size > 16)
21715         {
21716           if (TARGET_64BIT)
21717             {
21718               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
21719               emit_insn (gen_strset (destptr, dest, value));
21720               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
21721               emit_insn (gen_strset (destptr, dest, value));
21722             }
21723           else
21724             gcc_unreachable ();
21725           offset += 16;
21726         }
21727       if ((countval & 0x08) && max_size > 8)
21728         {
21729           if (TARGET_64BIT)
21730             {
21731               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
21732               emit_insn (gen_strset (destptr, dest, value));
21733             }
21734           else
21735             {
21736               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
21737               emit_insn (gen_strset (destptr, dest, value));
21738               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
21739               emit_insn (gen_strset (destptr, dest, value));
21740             }
21741           offset += 8;
21742         }
21743       if ((countval & 0x04) && max_size > 4)
21744         {
21745           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
21746           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
21747           offset += 4;
21748         }
21749       if ((countval & 0x02) && max_size > 2)
21750         {
21751           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
21752           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
21753           offset += 2;
21754         }
21755       if ((countval & 0x01) && max_size > 1)
21756         {
21757           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
21758           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
21759           offset += 1;
21760         }
21761       return;
21762     }
21763   if (max_size > 32)
21764     {
21765       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
21766       return;
21767     }
21768   if (max_size > 16)
21769     {
21770       rtx label = ix86_expand_aligntest (count, 16, true);
21771       if (TARGET_64BIT)
21772         {
21773           dest = change_address (destmem, DImode, destptr);
21774           emit_insn (gen_strset (destptr, dest, value));
21775           emit_insn (gen_strset (destptr, dest, value));
21776         }
21777       else
21778         {
21779           dest = change_address (destmem, SImode, destptr);
21780           emit_insn (gen_strset (destptr, dest, value));
21781           emit_insn (gen_strset (destptr, dest, value));
21782           emit_insn (gen_strset (destptr, dest, value));
21783           emit_insn (gen_strset (destptr, dest, value));
21784         }
21785       emit_label (label);
21786       LABEL_NUSES (label) = 1;
21787     }
21788   if (max_size > 8)
21789     {
21790       rtx label = ix86_expand_aligntest (count, 8, true);
21791       if (TARGET_64BIT)
21792         {
21793           dest = change_address (destmem, DImode, destptr);
21794           emit_insn (gen_strset (destptr, dest, value));
21795         }
21796       else
21797         {
21798           dest = change_address (destmem, SImode, destptr);
21799           emit_insn (gen_strset (destptr, dest, value));
21800           emit_insn (gen_strset (destptr, dest, value));
21801         }
21802       emit_label (label);
21803       LABEL_NUSES (label) = 1;
21804     }
21805   if (max_size > 4)
21806     {
21807       rtx label = ix86_expand_aligntest (count, 4, true);
21808       dest = change_address (destmem, SImode, destptr);
21809       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
21810       emit_label (label);
21811       LABEL_NUSES (label) = 1;
21812     }
21813   if (max_size > 2)
21814     {
21815       rtx label = ix86_expand_aligntest (count, 2, true);
21816       dest = change_address (destmem, HImode, destptr);
21817       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
21818       emit_label (label);
21819       LABEL_NUSES (label) = 1;
21820     }
21821   if (max_size > 1)
21822     {
21823       rtx label = ix86_expand_aligntest (count, 1, true);
21824       dest = change_address (destmem, QImode, destptr);
21825       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
21826       emit_label (label);
21827       LABEL_NUSES (label) = 1;
21828     }
21829 }
21830
21831 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
21832    DESIRED_ALIGNMENT.  */
21833 static void
21834 expand_movmem_prologue (rtx destmem, rtx srcmem,
21835                         rtx destptr, rtx srcptr, rtx count,
21836                         int align, int desired_alignment)
21837 {
21838   if (align <= 1 && desired_alignment > 1)
21839     {
21840       rtx label = ix86_expand_aligntest (destptr, 1, false);
21841       srcmem = change_address (srcmem, QImode, srcptr);
21842       destmem = change_address (destmem, QImode, destptr);
21843       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
21844       ix86_adjust_counter (count, 1);
21845       emit_label (label);
21846       LABEL_NUSES (label) = 1;
21847     }
21848   if (align <= 2 && desired_alignment > 2)
21849     {
21850       rtx label = ix86_expand_aligntest (destptr, 2, false);
21851       srcmem = change_address (srcmem, HImode, srcptr);
21852       destmem = change_address (destmem, HImode, destptr);
21853       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
21854       ix86_adjust_counter (count, 2);
21855       emit_label (label);
21856       LABEL_NUSES (label) = 1;
21857     }
21858   if (align <= 4 && desired_alignment > 4)
21859     {
21860       rtx label = ix86_expand_aligntest (destptr, 4, false);
21861       srcmem = change_address (srcmem, SImode, srcptr);
21862       destmem = change_address (destmem, SImode, destptr);
21863       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
21864       ix86_adjust_counter (count, 4);
21865       emit_label (label);
21866       LABEL_NUSES (label) = 1;
21867     }
21868   gcc_assert (desired_alignment <= 8);
21869 }
21870
21871 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
21872    ALIGN_BYTES is how many bytes need to be copied.  */
21873 static rtx
21874 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
21875                                  int desired_align, int align_bytes)
21876 {
21877   rtx src = *srcp;
21878   rtx orig_dst = dst;
21879   rtx orig_src = src;
21880   int off = 0;
21881   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
21882   if (src_align_bytes >= 0)
21883     src_align_bytes = desired_align - src_align_bytes;
21884   if (align_bytes & 1)
21885     {
21886       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
21887       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
21888       off = 1;
21889       emit_insn (gen_strmov (destreg, dst, srcreg, src));
21890     }
21891   if (align_bytes & 2)
21892     {
21893       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
21894       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
21895       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
21896         set_mem_align (dst, 2 * BITS_PER_UNIT);
21897       if (src_align_bytes >= 0
21898           && (src_align_bytes & 1) == (align_bytes & 1)
21899           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
21900         set_mem_align (src, 2 * BITS_PER_UNIT);
21901       off = 2;
21902       emit_insn (gen_strmov (destreg, dst, srcreg, src));
21903     }
21904   if (align_bytes & 4)
21905     {
21906       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
21907       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
21908       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
21909         set_mem_align (dst, 4 * BITS_PER_UNIT);
21910       if (src_align_bytes >= 0)
21911         {
21912           unsigned int src_align = 0;
21913           if ((src_align_bytes & 3) == (align_bytes & 3))
21914             src_align = 4;
21915           else if ((src_align_bytes & 1) == (align_bytes & 1))
21916             src_align = 2;
21917           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
21918             set_mem_align (src, src_align * BITS_PER_UNIT);
21919         }
21920       off = 4;
21921       emit_insn (gen_strmov (destreg, dst, srcreg, src));
21922     }
21923   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
21924   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
21925   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
21926     set_mem_align (dst, desired_align * BITS_PER_UNIT);
21927   if (src_align_bytes >= 0)
21928     {
21929       unsigned int src_align = 0;
21930       if ((src_align_bytes & 7) == (align_bytes & 7))
21931         src_align = 8;
21932       else if ((src_align_bytes & 3) == (align_bytes & 3))
21933         src_align = 4;
21934       else if ((src_align_bytes & 1) == (align_bytes & 1))
21935         src_align = 2;
21936       if (src_align > (unsigned int) desired_align)
21937         src_align = desired_align;
21938       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
21939         set_mem_align (src, src_align * BITS_PER_UNIT);
21940     }
21941   if (MEM_SIZE_KNOWN_P (orig_dst))
21942     set_mem_size (dst, MEM_SIZE (orig_dst) - align_bytes);
21943   if (MEM_SIZE_KNOWN_P (orig_src))
21944     set_mem_size (src, MEM_SIZE (orig_src) - align_bytes);
21945   *srcp = src;
21946   return dst;
21947 }
21948
21949 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
21950    DESIRED_ALIGNMENT.  */
21951 static void
21952 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
21953                         int align, int desired_alignment)
21954 {
21955   if (align <= 1 && desired_alignment > 1)
21956     {
21957       rtx label = ix86_expand_aligntest (destptr, 1, false);
21958       destmem = change_address (destmem, QImode, destptr);
21959       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
21960       ix86_adjust_counter (count, 1);
21961       emit_label (label);
21962       LABEL_NUSES (label) = 1;
21963     }
21964   if (align <= 2 && desired_alignment > 2)
21965     {
21966       rtx label = ix86_expand_aligntest (destptr, 2, false);
21967       destmem = change_address (destmem, HImode, destptr);
21968       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
21969       ix86_adjust_counter (count, 2);
21970       emit_label (label);
21971       LABEL_NUSES (label) = 1;
21972     }
21973   if (align <= 4 && desired_alignment > 4)
21974     {
21975       rtx label = ix86_expand_aligntest (destptr, 4, false);
21976       destmem = change_address (destmem, SImode, destptr);
21977       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
21978       ix86_adjust_counter (count, 4);
21979       emit_label (label);
21980       LABEL_NUSES (label) = 1;
21981     }
21982   gcc_assert (desired_alignment <= 8);
21983 }
21984
21985 /* Set enough from DST to align DST known to by aligned by ALIGN to
21986    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
21987 static rtx
21988 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
21989                                  int desired_align, int align_bytes)
21990 {
21991   int off = 0;
21992   rtx orig_dst = dst;
21993   if (align_bytes & 1)
21994     {
21995       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
21996       off = 1;
21997       emit_insn (gen_strset (destreg, dst,
21998                              gen_lowpart (QImode, value)));
21999     }
22000   if (align_bytes & 2)
22001     {
22002       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
22003       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
22004         set_mem_align (dst, 2 * BITS_PER_UNIT);
22005       off = 2;
22006       emit_insn (gen_strset (destreg, dst,
22007                              gen_lowpart (HImode, value)));
22008     }
22009   if (align_bytes & 4)
22010     {
22011       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
22012       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
22013         set_mem_align (dst, 4 * BITS_PER_UNIT);
22014       off = 4;
22015       emit_insn (gen_strset (destreg, dst,
22016                              gen_lowpart (SImode, value)));
22017     }
22018   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
22019   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
22020     set_mem_align (dst, desired_align * BITS_PER_UNIT);
22021   if (MEM_SIZE_KNOWN_P (orig_dst))
22022     set_mem_size (dst, MEM_SIZE (orig_dst) - align_bytes);
22023   return dst;
22024 }
22025
22026 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
22027 static enum stringop_alg
22028 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
22029             int *dynamic_check)
22030 {
22031   const struct stringop_algs * algs;
22032   bool optimize_for_speed;
22033   /* Algorithms using the rep prefix want at least edi and ecx;
22034      additionally, memset wants eax and memcpy wants esi.  Don't
22035      consider such algorithms if the user has appropriated those
22036      registers for their own purposes.  */
22037   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
22038                              || (memset
22039                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
22040
22041 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
22042                            || (alg != rep_prefix_1_byte         \
22043                                && alg != rep_prefix_4_byte      \
22044                                && alg != rep_prefix_8_byte))
22045   const struct processor_costs *cost;
22046
22047   /* Even if the string operation call is cold, we still might spend a lot
22048      of time processing large blocks.  */
22049   if (optimize_function_for_size_p (cfun)
22050       || (optimize_insn_for_size_p ()
22051           && expected_size != -1 && expected_size < 256))
22052     optimize_for_speed = false;
22053   else
22054     optimize_for_speed = true;
22055
22056   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
22057
22058   *dynamic_check = -1;
22059   if (memset)
22060     algs = &cost->memset[TARGET_64BIT != 0];
22061   else
22062     algs = &cost->memcpy[TARGET_64BIT != 0];
22063   if (ix86_stringop_alg != no_stringop && ALG_USABLE_P (ix86_stringop_alg))
22064     return ix86_stringop_alg;
22065   /* rep; movq or rep; movl is the smallest variant.  */
22066   else if (!optimize_for_speed)
22067     {
22068       if (!count || (count & 3))
22069         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
22070       else
22071         return rep_prefix_usable ? rep_prefix_4_byte : loop;
22072     }
22073   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
22074    */
22075   else if (expected_size != -1 && expected_size < 4)
22076     return loop_1_byte;
22077   else if (expected_size != -1)
22078     {
22079       unsigned int i;
22080       enum stringop_alg alg = libcall;
22081       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
22082         {
22083           /* We get here if the algorithms that were not libcall-based
22084              were rep-prefix based and we are unable to use rep prefixes
22085              based on global register usage.  Break out of the loop and
22086              use the heuristic below.  */
22087           if (algs->size[i].max == 0)
22088             break;
22089           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
22090             {
22091               enum stringop_alg candidate = algs->size[i].alg;
22092
22093               if (candidate != libcall && ALG_USABLE_P (candidate))
22094                 alg = candidate;
22095               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
22096                  last non-libcall inline algorithm.  */
22097               if (TARGET_INLINE_ALL_STRINGOPS)
22098                 {
22099                   /* When the current size is best to be copied by a libcall,
22100                      but we are still forced to inline, run the heuristic below
22101                      that will pick code for medium sized blocks.  */
22102                   if (alg != libcall)
22103                     return alg;
22104                   break;
22105                 }
22106               else if (ALG_USABLE_P (candidate))
22107                 return candidate;
22108             }
22109         }
22110       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
22111     }
22112   /* When asked to inline the call anyway, try to pick meaningful choice.
22113      We look for maximal size of block that is faster to copy by hand and
22114      take blocks of at most of that size guessing that average size will
22115      be roughly half of the block.
22116
22117      If this turns out to be bad, we might simply specify the preferred
22118      choice in ix86_costs.  */
22119   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
22120       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
22121     {
22122       int max = -1;
22123       enum stringop_alg alg;
22124       int i;
22125       bool any_alg_usable_p = true;
22126
22127       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
22128         {
22129           enum stringop_alg candidate = algs->size[i].alg;
22130           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
22131
22132           if (candidate != libcall && candidate
22133               && ALG_USABLE_P (candidate))
22134               max = algs->size[i].max;
22135         }
22136       /* If there aren't any usable algorithms, then recursing on
22137          smaller sizes isn't going to find anything.  Just return the
22138          simple byte-at-a-time copy loop.  */
22139       if (!any_alg_usable_p)
22140         {
22141           /* Pick something reasonable.  */
22142           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
22143             *dynamic_check = 128;
22144           return loop_1_byte;
22145         }
22146       if (max == -1)
22147         max = 4096;
22148       alg = decide_alg (count, max / 2, memset, dynamic_check);
22149       gcc_assert (*dynamic_check == -1);
22150       gcc_assert (alg != libcall);
22151       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
22152         *dynamic_check = max;
22153       return alg;
22154     }
22155   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
22156 #undef ALG_USABLE_P
22157 }
22158
22159 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
22160    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
22161 static int
22162 decide_alignment (int align,
22163                   enum stringop_alg alg,
22164                   int expected_size)
22165 {
22166   int desired_align = 0;
22167   switch (alg)
22168     {
22169       case no_stringop:
22170         gcc_unreachable ();
22171       case loop:
22172       case unrolled_loop:
22173         desired_align = GET_MODE_SIZE (Pmode);
22174         break;
22175       case rep_prefix_8_byte:
22176         desired_align = 8;
22177         break;
22178       case rep_prefix_4_byte:
22179         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
22180            copying whole cacheline at once.  */
22181         if (TARGET_PENTIUMPRO)
22182           desired_align = 8;
22183         else
22184           desired_align = 4;
22185         break;
22186       case rep_prefix_1_byte:
22187         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
22188            copying whole cacheline at once.  */
22189         if (TARGET_PENTIUMPRO)
22190           desired_align = 8;
22191         else
22192           desired_align = 1;
22193         break;
22194       case loop_1_byte:
22195         desired_align = 1;
22196         break;
22197       case libcall:
22198         return 0;
22199     }
22200
22201   if (optimize_size)
22202     desired_align = 1;
22203   if (desired_align < align)
22204     desired_align = align;
22205   if (expected_size != -1 && expected_size < 4)
22206     desired_align = align;
22207   return desired_align;
22208 }
22209
22210 /* Return the smallest power of 2 greater than VAL.  */
22211 static int
22212 smallest_pow2_greater_than (int val)
22213 {
22214   int ret = 1;
22215   while (ret <= val)
22216     ret <<= 1;
22217   return ret;
22218 }
22219
22220 /* Expand string move (memcpy) operation.  Use i386 string operations
22221    when profitable.  expand_setmem contains similar code.  The code
22222    depends upon architecture, block size and alignment, but always has
22223    the same overall structure:
22224
22225    1) Prologue guard: Conditional that jumps up to epilogues for small
22226       blocks that can be handled by epilogue alone.  This is faster
22227       but also needed for correctness, since prologue assume the block
22228       is larger than the desired alignment.
22229
22230       Optional dynamic check for size and libcall for large
22231       blocks is emitted here too, with -minline-stringops-dynamically.
22232
22233    2) Prologue: copy first few bytes in order to get destination
22234       aligned to DESIRED_ALIGN.  It is emitted only when ALIGN is less
22235       than DESIRED_ALIGN and up to DESIRED_ALIGN - ALIGN bytes can be
22236       copied.  We emit either a jump tree on power of two sized
22237       blocks, or a byte loop.
22238
22239    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
22240       with specified algorithm.
22241
22242    4) Epilogue: code copying tail of the block that is too small to be
22243       handled by main body (or up to size guarded by prologue guard).  */
22244
22245 bool
22246 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
22247                     rtx expected_align_exp, rtx expected_size_exp)
22248 {
22249   rtx destreg;
22250   rtx srcreg;
22251   rtx label = NULL;
22252   rtx tmp;
22253   rtx jump_around_label = NULL;
22254   HOST_WIDE_INT align = 1;
22255   unsigned HOST_WIDE_INT count = 0;
22256   HOST_WIDE_INT expected_size = -1;
22257   int size_needed = 0, epilogue_size_needed;
22258   int desired_align = 0, align_bytes = 0;
22259   enum stringop_alg alg;
22260   int dynamic_check;
22261   bool need_zero_guard = false;
22262
22263   if (CONST_INT_P (align_exp))
22264     align = INTVAL (align_exp);
22265   /* i386 can do misaligned access on reasonably increased cost.  */
22266   if (CONST_INT_P (expected_align_exp)
22267       && INTVAL (expected_align_exp) > align)
22268     align = INTVAL (expected_align_exp);
22269   /* ALIGN is the minimum of destination and source alignment, but we care here
22270      just about destination alignment.  */
22271   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
22272     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
22273
22274   if (CONST_INT_P (count_exp))
22275     count = expected_size = INTVAL (count_exp);
22276   if (CONST_INT_P (expected_size_exp) && count == 0)
22277     expected_size = INTVAL (expected_size_exp);
22278
22279   /* Make sure we don't need to care about overflow later on.  */
22280   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
22281     return false;
22282
22283   /* Step 0: Decide on preferred algorithm, desired alignment and
22284      size of chunks to be copied by main loop.  */
22285
22286   alg = decide_alg (count, expected_size, false, &dynamic_check);
22287   desired_align = decide_alignment (align, alg, expected_size);
22288
22289   if (!TARGET_ALIGN_STRINGOPS)
22290     align = desired_align;
22291
22292   if (alg == libcall)
22293     return false;
22294   gcc_assert (alg != no_stringop);
22295   if (!count)
22296     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
22297   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
22298   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
22299   switch (alg)
22300     {
22301     case libcall:
22302     case no_stringop:
22303       gcc_unreachable ();
22304     case loop:
22305       need_zero_guard = true;
22306       size_needed = GET_MODE_SIZE (Pmode);
22307       break;
22308     case unrolled_loop:
22309       need_zero_guard = true;
22310       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
22311       break;
22312     case rep_prefix_8_byte:
22313       size_needed = 8;
22314       break;
22315     case rep_prefix_4_byte:
22316       size_needed = 4;
22317       break;
22318     case rep_prefix_1_byte:
22319       size_needed = 1;
22320       break;
22321     case loop_1_byte:
22322       need_zero_guard = true;
22323       size_needed = 1;
22324       break;
22325     }
22326
22327   epilogue_size_needed = size_needed;
22328
22329   /* Step 1: Prologue guard.  */
22330
22331   /* Alignment code needs count to be in register.  */
22332   if (CONST_INT_P (count_exp) && desired_align > align)
22333     {
22334       if (INTVAL (count_exp) > desired_align
22335           && INTVAL (count_exp) > size_needed)
22336         {
22337           align_bytes
22338             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
22339           if (align_bytes <= 0)
22340             align_bytes = 0;
22341           else
22342             align_bytes = desired_align - align_bytes;
22343         }
22344       if (align_bytes == 0)
22345         count_exp = force_reg (counter_mode (count_exp), count_exp);
22346     }
22347   gcc_assert (desired_align >= 1 && align >= 1);
22348
22349   /* Ensure that alignment prologue won't copy past end of block.  */
22350   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
22351     {
22352       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
22353       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
22354          Make sure it is power of 2.  */
22355       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
22356
22357       if (count)
22358         {
22359           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
22360             {
22361               /* If main algorithm works on QImode, no epilogue is needed.
22362                  For small sizes just don't align anything.  */
22363               if (size_needed == 1)
22364                 desired_align = align;
22365               else
22366                 goto epilogue;
22367             }
22368         }
22369       else
22370         {
22371           label = gen_label_rtx ();
22372           emit_cmp_and_jump_insns (count_exp,
22373                                    GEN_INT (epilogue_size_needed),
22374                                    LTU, 0, counter_mode (count_exp), 1, label);
22375           if (expected_size == -1 || expected_size < epilogue_size_needed)
22376             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22377           else
22378             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22379         }
22380     }
22381
22382   /* Emit code to decide on runtime whether library call or inline should be
22383      used.  */
22384   if (dynamic_check != -1)
22385     {
22386       if (CONST_INT_P (count_exp))
22387         {
22388           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
22389             {
22390               emit_block_move_via_libcall (dst, src, count_exp, false);
22391               count_exp = const0_rtx;
22392               goto epilogue;
22393             }
22394         }
22395       else
22396         {
22397           rtx hot_label = gen_label_rtx ();
22398           jump_around_label = gen_label_rtx ();
22399           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
22400                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
22401           predict_jump (REG_BR_PROB_BASE * 90 / 100);
22402           emit_block_move_via_libcall (dst, src, count_exp, false);
22403           emit_jump (jump_around_label);
22404           emit_label (hot_label);
22405         }
22406     }
22407
22408   /* Step 2: Alignment prologue.  */
22409
22410   if (desired_align > align)
22411     {
22412       if (align_bytes == 0)
22413         {
22414           /* Except for the first move in epilogue, we no longer know
22415              constant offset in aliasing info.  It don't seems to worth
22416              the pain to maintain it for the first move, so throw away
22417              the info early.  */
22418           src = change_address (src, BLKmode, srcreg);
22419           dst = change_address (dst, BLKmode, destreg);
22420           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
22421                                   desired_align);
22422         }
22423       else
22424         {
22425           /* If we know how many bytes need to be stored before dst is
22426              sufficiently aligned, maintain aliasing info accurately.  */
22427           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
22428                                                  desired_align, align_bytes);
22429           count_exp = plus_constant (count_exp, -align_bytes);
22430           count -= align_bytes;
22431         }
22432       if (need_zero_guard
22433           && (count < (unsigned HOST_WIDE_INT) size_needed
22434               || (align_bytes == 0
22435                   && count < ((unsigned HOST_WIDE_INT) size_needed
22436                               + desired_align - align))))
22437         {
22438           /* It is possible that we copied enough so the main loop will not
22439              execute.  */
22440           gcc_assert (size_needed > 1);
22441           if (label == NULL_RTX)
22442             label = gen_label_rtx ();
22443           emit_cmp_and_jump_insns (count_exp,
22444                                    GEN_INT (size_needed),
22445                                    LTU, 0, counter_mode (count_exp), 1, label);
22446           if (expected_size == -1
22447               || expected_size < (desired_align - align) / 2 + size_needed)
22448             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22449           else
22450             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22451         }
22452     }
22453   if (label && size_needed == 1)
22454     {
22455       emit_label (label);
22456       LABEL_NUSES (label) = 1;
22457       label = NULL;
22458       epilogue_size_needed = 1;
22459     }
22460   else if (label == NULL_RTX)
22461     epilogue_size_needed = size_needed;
22462
22463   /* Step 3: Main loop.  */
22464
22465   switch (alg)
22466     {
22467     case libcall:
22468     case no_stringop:
22469       gcc_unreachable ();
22470     case loop_1_byte:
22471       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
22472                                      count_exp, QImode, 1, expected_size);
22473       break;
22474     case loop:
22475       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
22476                                      count_exp, Pmode, 1, expected_size);
22477       break;
22478     case unrolled_loop:
22479       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
22480          registers for 4 temporaries anyway.  */
22481       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
22482                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
22483                                      expected_size);
22484       break;
22485     case rep_prefix_8_byte:
22486       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
22487                                  DImode);
22488       break;
22489     case rep_prefix_4_byte:
22490       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
22491                                  SImode);
22492       break;
22493     case rep_prefix_1_byte:
22494       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
22495                                  QImode);
22496       break;
22497     }
22498   /* Adjust properly the offset of src and dest memory for aliasing.  */
22499   if (CONST_INT_P (count_exp))
22500     {
22501       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
22502                                           (count / size_needed) * size_needed);
22503       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
22504                                           (count / size_needed) * size_needed);
22505     }
22506   else
22507     {
22508       src = change_address (src, BLKmode, srcreg);
22509       dst = change_address (dst, BLKmode, destreg);
22510     }
22511
22512   /* Step 4: Epilogue to copy the remaining bytes.  */
22513  epilogue:
22514   if (label)
22515     {
22516       /* When the main loop is done, COUNT_EXP might hold original count,
22517          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
22518          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
22519          bytes. Compensate if needed.  */
22520
22521       if (size_needed < epilogue_size_needed)
22522         {
22523           tmp =
22524             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
22525                                  GEN_INT (size_needed - 1), count_exp, 1,
22526                                  OPTAB_DIRECT);
22527           if (tmp != count_exp)
22528             emit_move_insn (count_exp, tmp);
22529         }
22530       emit_label (label);
22531       LABEL_NUSES (label) = 1;
22532     }
22533
22534   if (count_exp != const0_rtx && epilogue_size_needed > 1)
22535     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
22536                             epilogue_size_needed);
22537   if (jump_around_label)
22538     emit_label (jump_around_label);
22539   return true;
22540 }
22541
22542 /* Helper function for memcpy.  For QImode value 0xXY produce
22543    0xXYXYXYXY of wide specified by MODE.  This is essentially
22544    a * 0x10101010, but we can do slightly better than
22545    synth_mult by unwinding the sequence by hand on CPUs with
22546    slow multiply.  */
22547 static rtx
22548 promote_duplicated_reg (enum machine_mode mode, rtx val)
22549 {
22550   enum machine_mode valmode = GET_MODE (val);
22551   rtx tmp;
22552   int nops = mode == DImode ? 3 : 2;
22553
22554   gcc_assert (mode == SImode || mode == DImode);
22555   if (val == const0_rtx)
22556     return copy_to_mode_reg (mode, const0_rtx);
22557   if (CONST_INT_P (val))
22558     {
22559       HOST_WIDE_INT v = INTVAL (val) & 255;
22560
22561       v |= v << 8;
22562       v |= v << 16;
22563       if (mode == DImode)
22564         v |= (v << 16) << 16;
22565       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
22566     }
22567
22568   if (valmode == VOIDmode)
22569     valmode = QImode;
22570   if (valmode != QImode)
22571     val = gen_lowpart (QImode, val);
22572   if (mode == QImode)
22573     return val;
22574   if (!TARGET_PARTIAL_REG_STALL)
22575     nops--;
22576   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
22577       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
22578       <= (ix86_cost->shift_const + ix86_cost->add) * nops
22579           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
22580     {
22581       rtx reg = convert_modes (mode, QImode, val, true);
22582       tmp = promote_duplicated_reg (mode, const1_rtx);
22583       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
22584                                   OPTAB_DIRECT);
22585     }
22586   else
22587     {
22588       rtx reg = convert_modes (mode, QImode, val, true);
22589
22590       if (!TARGET_PARTIAL_REG_STALL)
22591         if (mode == SImode)
22592           emit_insn (gen_movsi_insv_1 (reg, reg));
22593         else
22594           emit_insn (gen_movdi_insv_1 (reg, reg));
22595       else
22596         {
22597           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
22598                                      NULL, 1, OPTAB_DIRECT);
22599           reg =
22600             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
22601         }
22602       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
22603                                  NULL, 1, OPTAB_DIRECT);
22604       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
22605       if (mode == SImode)
22606         return reg;
22607       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
22608                                  NULL, 1, OPTAB_DIRECT);
22609       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
22610       return reg;
22611     }
22612 }
22613
22614 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
22615    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
22616    alignment from ALIGN to DESIRED_ALIGN.  */
22617 static rtx
22618 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
22619 {
22620   rtx promoted_val;
22621
22622   if (TARGET_64BIT
22623       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
22624     promoted_val = promote_duplicated_reg (DImode, val);
22625   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
22626     promoted_val = promote_duplicated_reg (SImode, val);
22627   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
22628     promoted_val = promote_duplicated_reg (HImode, val);
22629   else
22630     promoted_val = val;
22631
22632   return promoted_val;
22633 }
22634
22635 /* Expand string clear operation (bzero).  Use i386 string operations when
22636    profitable.  See expand_movmem comment for explanation of individual
22637    steps performed.  */
22638 bool
22639 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
22640                     rtx expected_align_exp, rtx expected_size_exp)
22641 {
22642   rtx destreg;
22643   rtx label = NULL;
22644   rtx tmp;
22645   rtx jump_around_label = NULL;
22646   HOST_WIDE_INT align = 1;
22647   unsigned HOST_WIDE_INT count = 0;
22648   HOST_WIDE_INT expected_size = -1;
22649   int size_needed = 0, epilogue_size_needed;
22650   int desired_align = 0, align_bytes = 0;
22651   enum stringop_alg alg;
22652   rtx promoted_val = NULL;
22653   bool force_loopy_epilogue = false;
22654   int dynamic_check;
22655   bool need_zero_guard = false;
22656
22657   if (CONST_INT_P (align_exp))
22658     align = INTVAL (align_exp);
22659   /* i386 can do misaligned access on reasonably increased cost.  */
22660   if (CONST_INT_P (expected_align_exp)
22661       && INTVAL (expected_align_exp) > align)
22662     align = INTVAL (expected_align_exp);
22663   if (CONST_INT_P (count_exp))
22664     count = expected_size = INTVAL (count_exp);
22665   if (CONST_INT_P (expected_size_exp) && count == 0)
22666     expected_size = INTVAL (expected_size_exp);
22667
22668   /* Make sure we don't need to care about overflow later on.  */
22669   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
22670     return false;
22671
22672   /* Step 0: Decide on preferred algorithm, desired alignment and
22673      size of chunks to be copied by main loop.  */
22674
22675   alg = decide_alg (count, expected_size, true, &dynamic_check);
22676   desired_align = decide_alignment (align, alg, expected_size);
22677
22678   if (!TARGET_ALIGN_STRINGOPS)
22679     align = desired_align;
22680
22681   if (alg == libcall)
22682     return false;
22683   gcc_assert (alg != no_stringop);
22684   if (!count)
22685     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
22686   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
22687   switch (alg)
22688     {
22689     case libcall:
22690     case no_stringop:
22691       gcc_unreachable ();
22692     case loop:
22693       need_zero_guard = true;
22694       size_needed = GET_MODE_SIZE (Pmode);
22695       break;
22696     case unrolled_loop:
22697       need_zero_guard = true;
22698       size_needed = GET_MODE_SIZE (Pmode) * 4;
22699       break;
22700     case rep_prefix_8_byte:
22701       size_needed = 8;
22702       break;
22703     case rep_prefix_4_byte:
22704       size_needed = 4;
22705       break;
22706     case rep_prefix_1_byte:
22707       size_needed = 1;
22708       break;
22709     case loop_1_byte:
22710       need_zero_guard = true;
22711       size_needed = 1;
22712       break;
22713     }
22714   epilogue_size_needed = size_needed;
22715
22716   /* Step 1: Prologue guard.  */
22717
22718   /* Alignment code needs count to be in register.  */
22719   if (CONST_INT_P (count_exp) && desired_align > align)
22720     {
22721       if (INTVAL (count_exp) > desired_align
22722           && INTVAL (count_exp) > size_needed)
22723         {
22724           align_bytes
22725             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
22726           if (align_bytes <= 0)
22727             align_bytes = 0;
22728           else
22729             align_bytes = desired_align - align_bytes;
22730         }
22731       if (align_bytes == 0)
22732         {
22733           enum machine_mode mode = SImode;
22734           if (TARGET_64BIT && (count & ~0xffffffff))
22735             mode = DImode;
22736           count_exp = force_reg (mode, count_exp);
22737         }
22738     }
22739   /* Do the cheap promotion to allow better CSE across the
22740      main loop and epilogue (ie one load of the big constant in the
22741      front of all code.  */
22742   if (CONST_INT_P (val_exp))
22743     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
22744                                                    desired_align, align);
22745   /* Ensure that alignment prologue won't copy past end of block.  */
22746   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
22747     {
22748       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
22749       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
22750          Make sure it is power of 2.  */
22751       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
22752
22753       /* To improve performance of small blocks, we jump around the VAL
22754          promoting mode.  This mean that if the promoted VAL is not constant,
22755          we might not use it in the epilogue and have to use byte
22756          loop variant.  */
22757       if (epilogue_size_needed > 2 && !promoted_val)
22758         force_loopy_epilogue = true;
22759       if (count)
22760         {
22761           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
22762             {
22763               /* If main algorithm works on QImode, no epilogue is needed.
22764                  For small sizes just don't align anything.  */
22765               if (size_needed == 1)
22766                 desired_align = align;
22767               else
22768                 goto epilogue;
22769             }
22770         }
22771       else
22772         {
22773           label = gen_label_rtx ();
22774           emit_cmp_and_jump_insns (count_exp,
22775                                    GEN_INT (epilogue_size_needed),
22776                                    LTU, 0, counter_mode (count_exp), 1, label);
22777           if (expected_size == -1 || expected_size <= epilogue_size_needed)
22778             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22779           else
22780             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22781         }
22782     }
22783   if (dynamic_check != -1)
22784     {
22785       rtx hot_label = gen_label_rtx ();
22786       jump_around_label = gen_label_rtx ();
22787       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
22788                                LEU, 0, counter_mode (count_exp), 1, hot_label);
22789       predict_jump (REG_BR_PROB_BASE * 90 / 100);
22790       set_storage_via_libcall (dst, count_exp, val_exp, false);
22791       emit_jump (jump_around_label);
22792       emit_label (hot_label);
22793     }
22794
22795   /* Step 2: Alignment prologue.  */
22796
22797   /* Do the expensive promotion once we branched off the small blocks.  */
22798   if (!promoted_val)
22799     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
22800                                                    desired_align, align);
22801   gcc_assert (desired_align >= 1 && align >= 1);
22802
22803   if (desired_align > align)
22804     {
22805       if (align_bytes == 0)
22806         {
22807           /* Except for the first move in epilogue, we no longer know
22808              constant offset in aliasing info.  It don't seems to worth
22809              the pain to maintain it for the first move, so throw away
22810              the info early.  */
22811           dst = change_address (dst, BLKmode, destreg);
22812           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
22813                                   desired_align);
22814         }
22815       else
22816         {
22817           /* If we know how many bytes need to be stored before dst is
22818              sufficiently aligned, maintain aliasing info accurately.  */
22819           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
22820                                                  desired_align, align_bytes);
22821           count_exp = plus_constant (count_exp, -align_bytes);
22822           count -= align_bytes;
22823         }
22824       if (need_zero_guard
22825           && (count < (unsigned HOST_WIDE_INT) size_needed
22826               || (align_bytes == 0
22827                   && count < ((unsigned HOST_WIDE_INT) size_needed
22828                               + desired_align - align))))
22829         {
22830           /* It is possible that we copied enough so the main loop will not
22831              execute.  */
22832           gcc_assert (size_needed > 1);
22833           if (label == NULL_RTX)
22834             label = gen_label_rtx ();
22835           emit_cmp_and_jump_insns (count_exp,
22836                                    GEN_INT (size_needed),
22837                                    LTU, 0, counter_mode (count_exp), 1, label);
22838           if (expected_size == -1
22839               || expected_size < (desired_align - align) / 2 + size_needed)
22840             predict_jump (REG_BR_PROB_BASE * 20 / 100);
22841           else
22842             predict_jump (REG_BR_PROB_BASE * 60 / 100);
22843         }
22844     }
22845   if (label && size_needed == 1)
22846     {
22847       emit_label (label);
22848       LABEL_NUSES (label) = 1;
22849       label = NULL;
22850       promoted_val = val_exp;
22851       epilogue_size_needed = 1;
22852     }
22853   else if (label == NULL_RTX)
22854     epilogue_size_needed = size_needed;
22855
22856   /* Step 3: Main loop.  */
22857
22858   switch (alg)
22859     {
22860     case libcall:
22861     case no_stringop:
22862       gcc_unreachable ();
22863     case loop_1_byte:
22864       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
22865                                      count_exp, QImode, 1, expected_size);
22866       break;
22867     case loop:
22868       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
22869                                      count_exp, Pmode, 1, expected_size);
22870       break;
22871     case unrolled_loop:
22872       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
22873                                      count_exp, Pmode, 4, expected_size);
22874       break;
22875     case rep_prefix_8_byte:
22876       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
22877                                   DImode, val_exp);
22878       break;
22879     case rep_prefix_4_byte:
22880       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
22881                                   SImode, val_exp);
22882       break;
22883     case rep_prefix_1_byte:
22884       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
22885                                   QImode, val_exp);
22886       break;
22887     }
22888   /* Adjust properly the offset of src and dest memory for aliasing.  */
22889   if (CONST_INT_P (count_exp))
22890     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
22891                                         (count / size_needed) * size_needed);
22892   else
22893     dst = change_address (dst, BLKmode, destreg);
22894
22895   /* Step 4: Epilogue to copy the remaining bytes.  */
22896
22897   if (label)
22898     {
22899       /* When the main loop is done, COUNT_EXP might hold original count,
22900          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
22901          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
22902          bytes. Compensate if needed.  */
22903
22904       if (size_needed < epilogue_size_needed)
22905         {
22906           tmp =
22907             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
22908                                  GEN_INT (size_needed - 1), count_exp, 1,
22909                                  OPTAB_DIRECT);
22910           if (tmp != count_exp)
22911             emit_move_insn (count_exp, tmp);
22912         }
22913       emit_label (label);
22914       LABEL_NUSES (label) = 1;
22915     }
22916  epilogue:
22917   if (count_exp != const0_rtx && epilogue_size_needed > 1)
22918     {
22919       if (force_loopy_epilogue)
22920         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
22921                                          epilogue_size_needed);
22922       else
22923         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
22924                                 epilogue_size_needed);
22925     }
22926   if (jump_around_label)
22927     emit_label (jump_around_label);
22928   return true;
22929 }
22930
22931 /* Expand the appropriate insns for doing strlen if not just doing
22932    repnz; scasb
22933
22934    out = result, initialized with the start address
22935    align_rtx = alignment of the address.
22936    scratch = scratch register, initialized with the startaddress when
22937         not aligned, otherwise undefined
22938
22939    This is just the body. It needs the initializations mentioned above and
22940    some address computing at the end.  These things are done in i386.md.  */
22941
22942 static void
22943 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
22944 {
22945   int align;
22946   rtx tmp;
22947   rtx align_2_label = NULL_RTX;
22948   rtx align_3_label = NULL_RTX;
22949   rtx align_4_label = gen_label_rtx ();
22950   rtx end_0_label = gen_label_rtx ();
22951   rtx mem;
22952   rtx tmpreg = gen_reg_rtx (SImode);
22953   rtx scratch = gen_reg_rtx (SImode);
22954   rtx cmp;
22955
22956   align = 0;
22957   if (CONST_INT_P (align_rtx))
22958     align = INTVAL (align_rtx);
22959
22960   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
22961
22962   /* Is there a known alignment and is it less than 4?  */
22963   if (align < 4)
22964     {
22965       rtx scratch1 = gen_reg_rtx (Pmode);
22966       emit_move_insn (scratch1, out);
22967       /* Is there a known alignment and is it not 2? */
22968       if (align != 2)
22969         {
22970           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
22971           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
22972
22973           /* Leave just the 3 lower bits.  */
22974           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
22975                                     NULL_RTX, 0, OPTAB_WIDEN);
22976
22977           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
22978                                    Pmode, 1, align_4_label);
22979           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
22980                                    Pmode, 1, align_2_label);
22981           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
22982                                    Pmode, 1, align_3_label);
22983         }
22984       else
22985         {
22986           /* Since the alignment is 2, we have to check 2 or 0 bytes;
22987              check if is aligned to 4 - byte.  */
22988
22989           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
22990                                     NULL_RTX, 0, OPTAB_WIDEN);
22991
22992           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
22993                                    Pmode, 1, align_4_label);
22994         }
22995
22996       mem = change_address (src, QImode, out);
22997
22998       /* Now compare the bytes.  */
22999
23000       /* Compare the first n unaligned byte on a byte per byte basis.  */
23001       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
23002                                QImode, 1, end_0_label);
23003
23004       /* Increment the address.  */
23005       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
23006
23007       /* Not needed with an alignment of 2 */
23008       if (align != 2)
23009         {
23010           emit_label (align_2_label);
23011
23012           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
23013                                    end_0_label);
23014
23015           emit_insn (ix86_gen_add3 (out, out, const1_rtx));
23016
23017           emit_label (align_3_label);
23018         }
23019
23020       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
23021                                end_0_label);
23022
23023       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
23024     }
23025
23026   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
23027      align this loop.  It gives only huge programs, but does not help to
23028      speed up.  */
23029   emit_label (align_4_label);
23030
23031   mem = change_address (src, SImode, out);
23032   emit_move_insn (scratch, mem);
23033   emit_insn (ix86_gen_add3 (out, out, GEN_INT (4)));
23034
23035   /* This formula yields a nonzero result iff one of the bytes is zero.
23036      This saves three branches inside loop and many cycles.  */
23037
23038   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
23039   emit_insn (gen_one_cmplsi2 (scratch, scratch));
23040   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
23041   emit_insn (gen_andsi3 (tmpreg, tmpreg,
23042                          gen_int_mode (0x80808080, SImode)));
23043   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
23044                            align_4_label);
23045
23046   if (TARGET_CMOVE)
23047     {
23048        rtx reg = gen_reg_rtx (SImode);
23049        rtx reg2 = gen_reg_rtx (Pmode);
23050        emit_move_insn (reg, tmpreg);
23051        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
23052
23053        /* If zero is not in the first two bytes, move two bytes forward.  */
23054        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
23055        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
23056        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
23057        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
23058                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
23059                                                      reg,
23060                                                      tmpreg)));
23061        /* Emit lea manually to avoid clobbering of flags.  */
23062        emit_insn (gen_rtx_SET (SImode, reg2,
23063                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
23064
23065        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
23066        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
23067        emit_insn (gen_rtx_SET (VOIDmode, out,
23068                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
23069                                                      reg2,
23070                                                      out)));
23071     }
23072   else
23073     {
23074        rtx end_2_label = gen_label_rtx ();
23075        /* Is zero in the first two bytes? */
23076
23077        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
23078        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
23079        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
23080        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
23081                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
23082                             pc_rtx);
23083        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
23084        JUMP_LABEL (tmp) = end_2_label;
23085
23086        /* Not in the first two.  Move two bytes forward.  */
23087        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
23088        emit_insn (ix86_gen_add3 (out, out, const2_rtx));
23089
23090        emit_label (end_2_label);
23091
23092     }
23093
23094   /* Avoid branch in fixing the byte.  */
23095   tmpreg = gen_lowpart (QImode, tmpreg);
23096   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
23097   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
23098   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
23099   emit_insn (ix86_gen_sub3_carry (out, out, GEN_INT (3), tmp, cmp));
23100
23101   emit_label (end_0_label);
23102 }
23103
23104 /* Expand strlen.  */
23105
23106 bool
23107 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
23108 {
23109   rtx addr, scratch1, scratch2, scratch3, scratch4;
23110
23111   /* The generic case of strlen expander is long.  Avoid it's
23112      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
23113
23114   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
23115       && !TARGET_INLINE_ALL_STRINGOPS
23116       && !optimize_insn_for_size_p ()
23117       && (!CONST_INT_P (align) || INTVAL (align) < 4))
23118     return false;
23119
23120   addr = force_reg (Pmode, XEXP (src, 0));
23121   scratch1 = gen_reg_rtx (Pmode);
23122
23123   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
23124       && !optimize_insn_for_size_p ())
23125     {
23126       /* Well it seems that some optimizer does not combine a call like
23127          foo(strlen(bar), strlen(bar));
23128          when the move and the subtraction is done here.  It does calculate
23129          the length just once when these instructions are done inside of
23130          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
23131          often used and I use one fewer register for the lifetime of
23132          output_strlen_unroll() this is better.  */
23133
23134       emit_move_insn (out, addr);
23135
23136       ix86_expand_strlensi_unroll_1 (out, src, align);
23137
23138       /* strlensi_unroll_1 returns the address of the zero at the end of
23139          the string, like memchr(), so compute the length by subtracting
23140          the start address.  */
23141       emit_insn (ix86_gen_sub3 (out, out, addr));
23142     }
23143   else
23144     {
23145       rtx unspec;
23146
23147       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
23148       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
23149         return false;
23150
23151       scratch2 = gen_reg_rtx (Pmode);
23152       scratch3 = gen_reg_rtx (Pmode);
23153       scratch4 = force_reg (Pmode, constm1_rtx);
23154
23155       emit_move_insn (scratch3, addr);
23156       eoschar = force_reg (QImode, eoschar);
23157
23158       src = replace_equiv_address_nv (src, scratch3);
23159
23160       /* If .md starts supporting :P, this can be done in .md.  */
23161       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
23162                                                  scratch4), UNSPEC_SCAS);
23163       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
23164       emit_insn (ix86_gen_one_cmpl2 (scratch2, scratch1));
23165       emit_insn (ix86_gen_add3 (out, scratch2, constm1_rtx));
23166     }
23167   return true;
23168 }
23169
23170 /* For given symbol (function) construct code to compute address of it's PLT
23171    entry in large x86-64 PIC model.  */
23172 rtx
23173 construct_plt_address (rtx symbol)
23174 {
23175   rtx tmp = gen_reg_rtx (Pmode);
23176   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
23177
23178   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
23179   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
23180
23181   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
23182   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
23183   return tmp;
23184 }
23185
23186 rtx
23187 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
23188                   rtx callarg2,
23189                   rtx pop, bool sibcall)
23190 {
23191   /* We need to represent that SI and DI registers are clobbered
23192      by SYSV calls.  */
23193   static int clobbered_registers[] = {
23194         XMM6_REG, XMM7_REG, XMM8_REG,
23195         XMM9_REG, XMM10_REG, XMM11_REG,
23196         XMM12_REG, XMM13_REG, XMM14_REG,
23197         XMM15_REG, SI_REG, DI_REG
23198   };
23199   rtx vec[ARRAY_SIZE (clobbered_registers) + 3];
23200   rtx use = NULL, call;
23201   unsigned int vec_len;
23202
23203   if (pop == const0_rtx)
23204     pop = NULL;
23205   gcc_assert (!TARGET_64BIT || !pop);
23206
23207   if (TARGET_MACHO && !TARGET_64BIT)
23208     {
23209 #if TARGET_MACHO
23210       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
23211         fnaddr = machopic_indirect_call_target (fnaddr);
23212 #endif
23213     }
23214   else
23215     {
23216       /* Static functions and indirect calls don't need the pic register.  */
23217       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
23218           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
23219           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
23220         use_reg (&use, pic_offset_table_rtx);
23221     }
23222
23223   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
23224     {
23225       rtx al = gen_rtx_REG (QImode, AX_REG);
23226       emit_move_insn (al, callarg2);
23227       use_reg (&use, al);
23228     }
23229
23230   if (ix86_cmodel == CM_LARGE_PIC
23231       && MEM_P (fnaddr)
23232       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
23233       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
23234     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
23235   else if (sibcall
23236            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
23237            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
23238     {
23239       fnaddr = XEXP (fnaddr, 0);
23240       if (GET_MODE (fnaddr) != Pmode)
23241         fnaddr = convert_to_mode (Pmode, fnaddr, 1);
23242       fnaddr = gen_rtx_MEM (QImode, copy_to_mode_reg (Pmode, fnaddr));
23243     }
23244
23245   vec_len = 0;
23246   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
23247   if (retval)
23248     call = gen_rtx_SET (VOIDmode, retval, call);
23249   vec[vec_len++] = call;
23250
23251   if (pop)
23252     {
23253       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
23254       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
23255       vec[vec_len++] = pop;
23256     }
23257
23258   if (TARGET_64BIT_MS_ABI
23259       && (!callarg2 || INTVAL (callarg2) != -2))
23260     {
23261       unsigned i;
23262
23263       vec[vec_len++] = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
23264                                        UNSPEC_MS_TO_SYSV_CALL);
23265
23266       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
23267         vec[vec_len++]
23268           = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
23269                              ? TImode : DImode,
23270                              gen_rtx_REG (SSE_REGNO_P (clobbered_registers[i])
23271                                           ? TImode : DImode,
23272                                           clobbered_registers[i]));
23273     }
23274
23275   /* Add UNSPEC_CALL_NEEDS_VZEROUPPER decoration.  */
23276   if (TARGET_VZEROUPPER)
23277     {
23278       int avx256;
23279       if (cfun->machine->callee_pass_avx256_p)
23280         {
23281           if (cfun->machine->callee_return_avx256_p)
23282             avx256 = callee_return_pass_avx256;
23283           else
23284             avx256 = callee_pass_avx256;
23285         }
23286       else if (cfun->machine->callee_return_avx256_p)
23287         avx256 = callee_return_avx256;
23288       else
23289         avx256 = call_no_avx256;
23290
23291       if (reload_completed)
23292         emit_insn (gen_avx_vzeroupper (GEN_INT (avx256)));
23293       else
23294         vec[vec_len++] = gen_rtx_UNSPEC (VOIDmode,
23295                                          gen_rtvec (1, GEN_INT (avx256)),
23296                                          UNSPEC_CALL_NEEDS_VZEROUPPER);
23297     }
23298
23299   if (vec_len > 1)
23300     call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (vec_len, vec));
23301   call = emit_call_insn (call);
23302   if (use)
23303     CALL_INSN_FUNCTION_USAGE (call) = use;
23304
23305   return call;
23306 }
23307
23308 void
23309 ix86_split_call_vzeroupper (rtx insn, rtx vzeroupper)
23310 {
23311   rtx pat = PATTERN (insn);
23312   rtvec vec = XVEC (pat, 0);
23313   int len = GET_NUM_ELEM (vec) - 1;
23314
23315   /* Strip off the last entry of the parallel.  */
23316   gcc_assert (GET_CODE (RTVEC_ELT (vec, len)) == UNSPEC);
23317   gcc_assert (XINT (RTVEC_ELT (vec, len), 1) == UNSPEC_CALL_NEEDS_VZEROUPPER);
23318   if (len == 1)
23319     pat = RTVEC_ELT (vec, 0);
23320   else
23321     pat = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (len, &RTVEC_ELT (vec, 0)));
23322
23323   emit_insn (gen_avx_vzeroupper (vzeroupper));
23324   emit_call_insn (pat);
23325 }
23326
23327 /* Output the assembly for a call instruction.  */
23328
23329 const char *
23330 ix86_output_call_insn (rtx insn, rtx call_op)
23331 {
23332   bool direct_p = constant_call_address_operand (call_op, Pmode);
23333   bool seh_nop_p = false;
23334   const char *xasm;
23335
23336   if (SIBLING_CALL_P (insn))
23337     {
23338       if (direct_p)
23339         xasm = "jmp\t%P0";
23340       /* SEH epilogue detection requires the indirect branch case
23341          to include REX.W.  */
23342       else if (TARGET_SEH)
23343         xasm = "rex.W jmp %A0";
23344       else
23345         xasm = "jmp\t%A0";
23346
23347       output_asm_insn (xasm, &call_op);
23348       return "";
23349     }
23350
23351   /* SEH unwinding can require an extra nop to be emitted in several
23352      circumstances.  Determine if we have one of those.  */
23353   if (TARGET_SEH)
23354     {
23355       rtx i;
23356
23357       for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
23358         {
23359           /* If we get to another real insn, we don't need the nop.  */
23360           if (INSN_P (i))
23361             break;
23362
23363           /* If we get to the epilogue note, prevent a catch region from
23364              being adjacent to the standard epilogue sequence.  If non-
23365              call-exceptions, we'll have done this during epilogue emission. */
23366           if (NOTE_P (i) && NOTE_KIND (i) == NOTE_INSN_EPILOGUE_BEG
23367               && !flag_non_call_exceptions
23368               && !can_throw_internal (insn))
23369             {
23370               seh_nop_p = true;
23371               break;
23372             }
23373         }
23374
23375       /* If we didn't find a real insn following the call, prevent the
23376          unwinder from looking into the next function.  */
23377       if (i == NULL)
23378         seh_nop_p = true;
23379     }
23380
23381   if (direct_p)
23382     xasm = "call\t%P0";
23383   else
23384     xasm = "call\t%A0";
23385
23386   output_asm_insn (xasm, &call_op);
23387
23388   if (seh_nop_p)
23389     return "nop";
23390
23391   return "";
23392 }
23393 \f
23394 /* Clear stack slot assignments remembered from previous functions.
23395    This is called from INIT_EXPANDERS once before RTL is emitted for each
23396    function.  */
23397
23398 static struct machine_function *
23399 ix86_init_machine_status (void)
23400 {
23401   struct machine_function *f;
23402
23403   f = ggc_alloc_cleared_machine_function ();
23404   f->use_fast_prologue_epilogue_nregs = -1;
23405   f->call_abi = ix86_abi;
23406
23407   return f;
23408 }
23409
23410 /* Return a MEM corresponding to a stack slot with mode MODE.
23411    Allocate a new slot if necessary.
23412
23413    The RTL for a function can have several slots available: N is
23414    which slot to use.  */
23415
23416 rtx
23417 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
23418 {
23419   struct stack_local_entry *s;
23420
23421   gcc_assert (n < MAX_386_STACK_LOCALS);
23422
23423   for (s = ix86_stack_locals; s; s = s->next)
23424     if (s->mode == mode && s->n == n)
23425       return validize_mem (copy_rtx (s->rtl));
23426
23427   s = ggc_alloc_stack_local_entry ();
23428   s->n = n;
23429   s->mode = mode;
23430   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
23431
23432   s->next = ix86_stack_locals;
23433   ix86_stack_locals = s;
23434   return validize_mem (s->rtl);
23435 }
23436
23437 static void
23438 ix86_instantiate_decls (void)
23439 {
23440   struct stack_local_entry *s;
23441
23442   for (s = ix86_stack_locals; s; s = s->next)
23443     if (s->rtl != NULL_RTX)
23444       instantiate_decl_rtl (s->rtl);
23445 }
23446 \f
23447 /* Calculate the length of the memory address in the instruction encoding.
23448    Includes addr32 prefix, does not include the one-byte modrm, opcode,
23449    or other prefixes.  We never generate addr32 prefix for LEA insn.  */
23450
23451 int
23452 memory_address_length (rtx addr, bool lea)
23453 {
23454   struct ix86_address parts;
23455   rtx base, index, disp;
23456   int len;
23457   int ok;
23458
23459   if (GET_CODE (addr) == PRE_DEC
23460       || GET_CODE (addr) == POST_INC
23461       || GET_CODE (addr) == PRE_MODIFY
23462       || GET_CODE (addr) == POST_MODIFY)
23463     return 0;
23464
23465   ok = ix86_decompose_address (addr, &parts);
23466   gcc_assert (ok);
23467
23468   len = (parts.seg == SEG_DEFAULT) ? 0 : 1;
23469
23470   /*  If this is not LEA instruction, add the length of addr32 prefix.  */
23471   if (TARGET_64BIT && !lea
23472       && (SImode_address_operand (addr, VOIDmode)
23473           || (parts.base && GET_MODE (parts.base) == SImode)
23474           || (parts.index && GET_MODE (parts.index) == SImode)))
23475     len++;
23476
23477   base = parts.base;
23478   index = parts.index;
23479   disp = parts.disp;
23480
23481   if (base && GET_CODE (base) == SUBREG)
23482     base = SUBREG_REG (base);
23483   if (index && GET_CODE (index) == SUBREG)
23484     index = SUBREG_REG (index);
23485
23486   gcc_assert (base == NULL_RTX || REG_P (base));
23487   gcc_assert (index == NULL_RTX || REG_P (index));
23488
23489   /* Rule of thumb:
23490        - esp as the base always wants an index,
23491        - ebp as the base always wants a displacement,
23492        - r12 as the base always wants an index,
23493        - r13 as the base always wants a displacement.  */
23494
23495   /* Register Indirect.  */
23496   if (base && !index && !disp)
23497     {
23498       /* esp (for its index) and ebp (for its displacement) need
23499          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
23500          code.  */
23501       if (base == arg_pointer_rtx
23502           || base == frame_pointer_rtx
23503           || REGNO (base) == SP_REG
23504           || REGNO (base) == BP_REG
23505           || REGNO (base) == R12_REG
23506           || REGNO (base) == R13_REG)
23507         len++;
23508     }
23509
23510   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
23511      is not disp32, but disp32(%rip), so for disp32
23512      SIB byte is needed, unless print_operand_address
23513      optimizes it into disp32(%rip) or (%rip) is implied
23514      by UNSPEC.  */
23515   else if (disp && !base && !index)
23516     {
23517       len += 4;
23518       if (TARGET_64BIT)
23519         {
23520           rtx symbol = disp;
23521
23522           if (GET_CODE (disp) == CONST)
23523             symbol = XEXP (disp, 0);
23524           if (GET_CODE (symbol) == PLUS
23525               && CONST_INT_P (XEXP (symbol, 1)))
23526             symbol = XEXP (symbol, 0);
23527
23528           if (GET_CODE (symbol) != LABEL_REF
23529               && (GET_CODE (symbol) != SYMBOL_REF
23530                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
23531               && (GET_CODE (symbol) != UNSPEC
23532                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
23533                       && XINT (symbol, 1) != UNSPEC_PCREL
23534                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
23535             len++;
23536         }
23537     }
23538   else
23539     {
23540       /* Find the length of the displacement constant.  */
23541       if (disp)
23542         {
23543           if (base && satisfies_constraint_K (disp))
23544             len += 1;
23545           else
23546             len += 4;
23547         }
23548       /* ebp always wants a displacement.  Similarly r13.  */
23549       else if (base && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
23550         len++;
23551
23552       /* An index requires the two-byte modrm form....  */
23553       if (index
23554           /* ...like esp (or r12), which always wants an index.  */
23555           || base == arg_pointer_rtx
23556           || base == frame_pointer_rtx
23557           || (base && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
23558         len++;
23559     }
23560
23561   return len;
23562 }
23563
23564 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
23565    is set, expect that insn have 8bit immediate alternative.  */
23566 int
23567 ix86_attr_length_immediate_default (rtx insn, bool shortform)
23568 {
23569   int len = 0;
23570   int i;
23571   extract_insn_cached (insn);
23572   for (i = recog_data.n_operands - 1; i >= 0; --i)
23573     if (CONSTANT_P (recog_data.operand[i]))
23574       {
23575         enum attr_mode mode = get_attr_mode (insn);
23576
23577         gcc_assert (!len);
23578         if (shortform && CONST_INT_P (recog_data.operand[i]))
23579           {
23580             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
23581             switch (mode)
23582               {
23583               case MODE_QI:
23584                 len = 1;
23585                 continue;
23586               case MODE_HI:
23587                 ival = trunc_int_for_mode (ival, HImode);
23588                 break;
23589               case MODE_SI:
23590                 ival = trunc_int_for_mode (ival, SImode);
23591                 break;
23592               default:
23593                 break;
23594               }
23595             if (IN_RANGE (ival, -128, 127))
23596               {
23597                 len = 1;
23598                 continue;
23599               }
23600           }
23601         switch (mode)
23602           {
23603           case MODE_QI:
23604             len = 1;
23605             break;
23606           case MODE_HI:
23607             len = 2;
23608             break;
23609           case MODE_SI:
23610             len = 4;
23611             break;
23612           /* Immediates for DImode instructions are encoded
23613              as 32bit sign extended values.  */
23614           case MODE_DI:
23615             len = 4;
23616             break;
23617           default:
23618             fatal_insn ("unknown insn mode", insn);
23619         }
23620       }
23621   return len;
23622 }
23623
23624 /* Compute default value for "length_address" attribute.  */
23625 int
23626 ix86_attr_length_address_default (rtx insn)
23627 {
23628   int i;
23629
23630   if (get_attr_type (insn) == TYPE_LEA)
23631     {
23632       rtx set = PATTERN (insn), addr;
23633
23634       if (GET_CODE (set) == PARALLEL)
23635         set = XVECEXP (set, 0, 0);
23636
23637       gcc_assert (GET_CODE (set) == SET);
23638
23639       addr = SET_SRC (set);
23640
23641       return memory_address_length (addr, true);
23642     }
23643
23644   extract_insn_cached (insn);
23645   for (i = recog_data.n_operands - 1; i >= 0; --i)
23646     if (MEM_P (recog_data.operand[i]))
23647       {
23648         constrain_operands_cached (reload_completed);
23649         if (which_alternative != -1)
23650           {
23651             const char *constraints = recog_data.constraints[i];
23652             int alt = which_alternative;
23653
23654             while (*constraints == '=' || *constraints == '+')
23655               constraints++;
23656             while (alt-- > 0)
23657               while (*constraints++ != ',')
23658                 ;
23659             /* Skip ignored operands.  */
23660             if (*constraints == 'X')
23661               continue;
23662           }
23663         return memory_address_length (XEXP (recog_data.operand[i], 0), false);
23664       }
23665   return 0;
23666 }
23667
23668 /* Compute default value for "length_vex" attribute. It includes
23669    2 or 3 byte VEX prefix and 1 opcode byte.  */
23670
23671 int
23672 ix86_attr_length_vex_default (rtx insn, bool has_0f_opcode, bool has_vex_w)
23673 {
23674   int i;
23675
23676   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
23677      byte VEX prefix.  */
23678   if (!has_0f_opcode || has_vex_w)
23679     return 3 + 1;
23680
23681  /* We can always use 2 byte VEX prefix in 32bit.  */
23682   if (!TARGET_64BIT)
23683     return 2 + 1;
23684
23685   extract_insn_cached (insn);
23686
23687   for (i = recog_data.n_operands - 1; i >= 0; --i)
23688     if (REG_P (recog_data.operand[i]))
23689       {
23690         /* REX.W bit uses 3 byte VEX prefix.  */
23691         if (GET_MODE (recog_data.operand[i]) == DImode
23692             && GENERAL_REG_P (recog_data.operand[i]))
23693           return 3 + 1;
23694       }
23695     else
23696       {
23697         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
23698         if (MEM_P (recog_data.operand[i])
23699             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
23700           return 3 + 1;
23701       }
23702
23703   return 2 + 1;
23704 }
23705 \f
23706 /* Return the maximum number of instructions a cpu can issue.  */
23707
23708 static int
23709 ix86_issue_rate (void)
23710 {
23711   switch (ix86_tune)
23712     {
23713     case PROCESSOR_PENTIUM:
23714     case PROCESSOR_ATOM:
23715     case PROCESSOR_K6:
23716       return 2;
23717
23718     case PROCESSOR_PENTIUMPRO:
23719     case PROCESSOR_PENTIUM4:
23720     case PROCESSOR_CORE2_32:
23721     case PROCESSOR_CORE2_64:
23722     case PROCESSOR_COREI7_32:
23723     case PROCESSOR_COREI7_64:
23724     case PROCESSOR_ATHLON:
23725     case PROCESSOR_K8:
23726     case PROCESSOR_AMDFAM10:
23727     case PROCESSOR_NOCONA:
23728     case PROCESSOR_GENERIC32:
23729     case PROCESSOR_GENERIC64:
23730     case PROCESSOR_BDVER1:
23731     case PROCESSOR_BDVER2:
23732     case PROCESSOR_BTVER1:
23733       return 3;
23734
23735     default:
23736       return 1;
23737     }
23738 }
23739
23740 /* A subroutine of ix86_adjust_cost -- return TRUE iff INSN reads flags set
23741    by DEP_INSN and nothing set by DEP_INSN.  */
23742
23743 static bool
23744 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
23745 {
23746   rtx set, set2;
23747
23748   /* Simplify the test for uninteresting insns.  */
23749   if (insn_type != TYPE_SETCC
23750       && insn_type != TYPE_ICMOV
23751       && insn_type != TYPE_FCMOV
23752       && insn_type != TYPE_IBR)
23753     return false;
23754
23755   if ((set = single_set (dep_insn)) != 0)
23756     {
23757       set = SET_DEST (set);
23758       set2 = NULL_RTX;
23759     }
23760   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
23761            && XVECLEN (PATTERN (dep_insn), 0) == 2
23762            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
23763            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
23764     {
23765       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
23766       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
23767     }
23768   else
23769     return false;
23770
23771   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
23772     return false;
23773
23774   /* This test is true if the dependent insn reads the flags but
23775      not any other potentially set register.  */
23776   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
23777     return false;
23778
23779   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
23780     return false;
23781
23782   return true;
23783 }
23784
23785 /* Return true iff USE_INSN has a memory address with operands set by
23786    SET_INSN.  */
23787
23788 bool
23789 ix86_agi_dependent (rtx set_insn, rtx use_insn)
23790 {
23791   int i;
23792   extract_insn_cached (use_insn);
23793   for (i = recog_data.n_operands - 1; i >= 0; --i)
23794     if (MEM_P (recog_data.operand[i]))
23795       {
23796         rtx addr = XEXP (recog_data.operand[i], 0);
23797         return modified_in_p (addr, set_insn) != 0;
23798       }
23799   return false;
23800 }
23801
23802 static int
23803 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
23804 {
23805   enum attr_type insn_type, dep_insn_type;
23806   enum attr_memory memory;
23807   rtx set, set2;
23808   int dep_insn_code_number;
23809
23810   /* Anti and output dependencies have zero cost on all CPUs.  */
23811   if (REG_NOTE_KIND (link) != 0)
23812     return 0;
23813
23814   dep_insn_code_number = recog_memoized (dep_insn);
23815
23816   /* If we can't recognize the insns, we can't really do anything.  */
23817   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
23818     return cost;
23819
23820   insn_type = get_attr_type (insn);
23821   dep_insn_type = get_attr_type (dep_insn);
23822
23823   switch (ix86_tune)
23824     {
23825     case PROCESSOR_PENTIUM:
23826       /* Address Generation Interlock adds a cycle of latency.  */
23827       if (insn_type == TYPE_LEA)
23828         {
23829           rtx addr = PATTERN (insn);
23830
23831           if (GET_CODE (addr) == PARALLEL)
23832             addr = XVECEXP (addr, 0, 0);
23833
23834           gcc_assert (GET_CODE (addr) == SET);
23835
23836           addr = SET_SRC (addr);
23837           if (modified_in_p (addr, dep_insn))
23838             cost += 1;
23839         }
23840       else if (ix86_agi_dependent (dep_insn, insn))
23841         cost += 1;
23842
23843       /* ??? Compares pair with jump/setcc.  */
23844       if (ix86_flags_dependent (insn, dep_insn, insn_type))
23845         cost = 0;
23846
23847       /* Floating point stores require value to be ready one cycle earlier.  */
23848       if (insn_type == TYPE_FMOV
23849           && get_attr_memory (insn) == MEMORY_STORE
23850           && !ix86_agi_dependent (dep_insn, insn))
23851         cost += 1;
23852       break;
23853
23854     case PROCESSOR_PENTIUMPRO:
23855       memory = get_attr_memory (insn);
23856
23857       /* INT->FP conversion is expensive.  */
23858       if (get_attr_fp_int_src (dep_insn))
23859         cost += 5;
23860
23861       /* There is one cycle extra latency between an FP op and a store.  */
23862       if (insn_type == TYPE_FMOV
23863           && (set = single_set (dep_insn)) != NULL_RTX
23864           && (set2 = single_set (insn)) != NULL_RTX
23865           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
23866           && MEM_P (SET_DEST (set2)))
23867         cost += 1;
23868
23869       /* Show ability of reorder buffer to hide latency of load by executing
23870          in parallel with previous instruction in case
23871          previous instruction is not needed to compute the address.  */
23872       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
23873           && !ix86_agi_dependent (dep_insn, insn))
23874         {
23875           /* Claim moves to take one cycle, as core can issue one load
23876              at time and the next load can start cycle later.  */
23877           if (dep_insn_type == TYPE_IMOV
23878               || dep_insn_type == TYPE_FMOV)
23879             cost = 1;
23880           else if (cost > 1)
23881             cost--;
23882         }
23883       break;
23884
23885     case PROCESSOR_K6:
23886       memory = get_attr_memory (insn);
23887
23888       /* The esp dependency is resolved before the instruction is really
23889          finished.  */
23890       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
23891           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
23892         return 1;
23893
23894       /* INT->FP conversion is expensive.  */
23895       if (get_attr_fp_int_src (dep_insn))
23896         cost += 5;
23897
23898       /* Show ability of reorder buffer to hide latency of load by executing
23899          in parallel with previous instruction in case
23900          previous instruction is not needed to compute the address.  */
23901       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
23902           && !ix86_agi_dependent (dep_insn, insn))
23903         {
23904           /* Claim moves to take one cycle, as core can issue one load
23905              at time and the next load can start cycle later.  */
23906           if (dep_insn_type == TYPE_IMOV
23907               || dep_insn_type == TYPE_FMOV)
23908             cost = 1;
23909           else if (cost > 2)
23910             cost -= 2;
23911           else
23912             cost = 1;
23913         }
23914       break;
23915
23916     case PROCESSOR_ATHLON:
23917     case PROCESSOR_K8:
23918     case PROCESSOR_AMDFAM10:
23919     case PROCESSOR_BDVER1:
23920     case PROCESSOR_BDVER2:
23921     case PROCESSOR_BTVER1:
23922     case PROCESSOR_ATOM:
23923     case PROCESSOR_GENERIC32:
23924     case PROCESSOR_GENERIC64:
23925       memory = get_attr_memory (insn);
23926
23927       /* Show ability of reorder buffer to hide latency of load by executing
23928          in parallel with previous instruction in case
23929          previous instruction is not needed to compute the address.  */
23930       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
23931           && !ix86_agi_dependent (dep_insn, insn))
23932         {
23933           enum attr_unit unit = get_attr_unit (insn);
23934           int loadcost = 3;
23935
23936           /* Because of the difference between the length of integer and
23937              floating unit pipeline preparation stages, the memory operands
23938              for floating point are cheaper.
23939
23940              ??? For Athlon it the difference is most probably 2.  */
23941           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
23942             loadcost = 3;
23943           else
23944             loadcost = TARGET_ATHLON ? 2 : 0;
23945
23946           if (cost >= loadcost)
23947             cost -= loadcost;
23948           else
23949             cost = 0;
23950         }
23951
23952     default:
23953       break;
23954     }
23955
23956   return cost;
23957 }
23958
23959 /* How many alternative schedules to try.  This should be as wide as the
23960    scheduling freedom in the DFA, but no wider.  Making this value too
23961    large results extra work for the scheduler.  */
23962
23963 static int
23964 ia32_multipass_dfa_lookahead (void)
23965 {
23966   switch (ix86_tune)
23967     {
23968     case PROCESSOR_PENTIUM:
23969       return 2;
23970
23971     case PROCESSOR_PENTIUMPRO:
23972     case PROCESSOR_K6:
23973       return 1;
23974
23975     case PROCESSOR_CORE2_32:
23976     case PROCESSOR_CORE2_64:
23977     case PROCESSOR_COREI7_32:
23978     case PROCESSOR_COREI7_64:
23979     case PROCESSOR_ATOM:
23980       /* Generally, we want haifa-sched:max_issue() to look ahead as far
23981          as many instructions can be executed on a cycle, i.e.,
23982          issue_rate.  I wonder why tuning for many CPUs does not do this.  */
23983       return ix86_issue_rate ();
23984
23985     default:
23986       return 0;
23987     }
23988 }
23989
23990 \f
23991
23992 /* Model decoder of Core 2/i7.
23993    Below hooks for multipass scheduling (see haifa-sched.c:max_issue)
23994    track the instruction fetch block boundaries and make sure that long
23995    (9+ bytes) instructions are assigned to D0.  */
23996
23997 /* Maximum length of an insn that can be handled by
23998    a secondary decoder unit.  '8' for Core 2/i7.  */
23999 static int core2i7_secondary_decoder_max_insn_size;
24000
24001 /* Ifetch block size, i.e., number of bytes decoder reads per cycle.
24002    '16' for Core 2/i7.  */
24003 static int core2i7_ifetch_block_size;
24004
24005 /* Maximum number of instructions decoder can handle per cycle.
24006    '6' for Core 2/i7.  */
24007 static int core2i7_ifetch_block_max_insns;
24008
24009 typedef struct ix86_first_cycle_multipass_data_ *
24010   ix86_first_cycle_multipass_data_t;
24011 typedef const struct ix86_first_cycle_multipass_data_ *
24012   const_ix86_first_cycle_multipass_data_t;
24013
24014 /* A variable to store target state across calls to max_issue within
24015    one cycle.  */
24016 static struct ix86_first_cycle_multipass_data_ _ix86_first_cycle_multipass_data,
24017   *ix86_first_cycle_multipass_data = &_ix86_first_cycle_multipass_data;
24018
24019 /* Initialize DATA.  */
24020 static void
24021 core2i7_first_cycle_multipass_init (void *_data)
24022 {
24023   ix86_first_cycle_multipass_data_t data
24024     = (ix86_first_cycle_multipass_data_t) _data;
24025
24026   data->ifetch_block_len = 0;
24027   data->ifetch_block_n_insns = 0;
24028   data->ready_try_change = NULL;
24029   data->ready_try_change_size = 0;
24030 }
24031
24032 /* Advancing the cycle; reset ifetch block counts.  */
24033 static void
24034 core2i7_dfa_post_advance_cycle (void)
24035 {
24036   ix86_first_cycle_multipass_data_t data = ix86_first_cycle_multipass_data;
24037
24038   gcc_assert (data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
24039
24040   data->ifetch_block_len = 0;
24041   data->ifetch_block_n_insns = 0;
24042 }
24043
24044 static int min_insn_size (rtx);
24045
24046 /* Filter out insns from ready_try that the core will not be able to issue
24047    on current cycle due to decoder.  */
24048 static void
24049 core2i7_first_cycle_multipass_filter_ready_try
24050 (const_ix86_first_cycle_multipass_data_t data,
24051  char *ready_try, int n_ready, bool first_cycle_insn_p)
24052 {
24053   while (n_ready--)
24054     {
24055       rtx insn;
24056       int insn_size;
24057
24058       if (ready_try[n_ready])
24059         continue;
24060
24061       insn = get_ready_element (n_ready);
24062       insn_size = min_insn_size (insn);
24063
24064       if (/* If this is a too long an insn for a secondary decoder ...  */
24065           (!first_cycle_insn_p
24066            && insn_size > core2i7_secondary_decoder_max_insn_size)
24067           /* ... or it would not fit into the ifetch block ...  */
24068           || data->ifetch_block_len + insn_size > core2i7_ifetch_block_size
24069           /* ... or the decoder is full already ...  */
24070           || data->ifetch_block_n_insns + 1 > core2i7_ifetch_block_max_insns)
24071         /* ... mask the insn out.  */
24072         {
24073           ready_try[n_ready] = 1;
24074
24075           if (data->ready_try_change)
24076             SET_BIT (data->ready_try_change, n_ready);
24077         }
24078     }
24079 }
24080
24081 /* Prepare for a new round of multipass lookahead scheduling.  */
24082 static void
24083 core2i7_first_cycle_multipass_begin (void *_data, char *ready_try, int n_ready,
24084                                      bool first_cycle_insn_p)
24085 {
24086   ix86_first_cycle_multipass_data_t data
24087     = (ix86_first_cycle_multipass_data_t) _data;
24088   const_ix86_first_cycle_multipass_data_t prev_data
24089     = ix86_first_cycle_multipass_data;
24090
24091   /* Restore the state from the end of the previous round.  */
24092   data->ifetch_block_len = prev_data->ifetch_block_len;
24093   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns;
24094
24095   /* Filter instructions that cannot be issued on current cycle due to
24096      decoder restrictions.  */
24097   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
24098                                                   first_cycle_insn_p);
24099 }
24100
24101 /* INSN is being issued in current solution.  Account for its impact on
24102    the decoder model.  */
24103 static void
24104 core2i7_first_cycle_multipass_issue (void *_data, char *ready_try, int n_ready,
24105                                      rtx insn, const void *_prev_data)
24106 {
24107   ix86_first_cycle_multipass_data_t data
24108     = (ix86_first_cycle_multipass_data_t) _data;
24109   const_ix86_first_cycle_multipass_data_t prev_data
24110     = (const_ix86_first_cycle_multipass_data_t) _prev_data;
24111
24112   int insn_size = min_insn_size (insn);
24113
24114   data->ifetch_block_len = prev_data->ifetch_block_len + insn_size;
24115   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns + 1;
24116   gcc_assert (data->ifetch_block_len <= core2i7_ifetch_block_size
24117               && data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
24118
24119   /* Allocate or resize the bitmap for storing INSN's effect on ready_try.  */
24120   if (!data->ready_try_change)
24121     {
24122       data->ready_try_change = sbitmap_alloc (n_ready);
24123       data->ready_try_change_size = n_ready;
24124     }
24125   else if (data->ready_try_change_size < n_ready)
24126     {
24127       data->ready_try_change = sbitmap_resize (data->ready_try_change,
24128                                                n_ready, 0);
24129       data->ready_try_change_size = n_ready;
24130     }
24131   sbitmap_zero (data->ready_try_change);
24132
24133   /* Filter out insns from ready_try that the core will not be able to issue
24134      on current cycle due to decoder.  */
24135   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
24136                                                   false);
24137 }
24138
24139 /* Revert the effect on ready_try.  */
24140 static void
24141 core2i7_first_cycle_multipass_backtrack (const void *_data,
24142                                          char *ready_try,
24143                                          int n_ready ATTRIBUTE_UNUSED)
24144 {
24145   const_ix86_first_cycle_multipass_data_t data
24146     = (const_ix86_first_cycle_multipass_data_t) _data;
24147   unsigned int i = 0;
24148   sbitmap_iterator sbi;
24149
24150   gcc_assert (sbitmap_last_set_bit (data->ready_try_change) < n_ready);
24151   EXECUTE_IF_SET_IN_SBITMAP (data->ready_try_change, 0, i, sbi)
24152     {
24153       ready_try[i] = 0;
24154     }
24155 }
24156
24157 /* Save the result of multipass lookahead scheduling for the next round.  */
24158 static void
24159 core2i7_first_cycle_multipass_end (const void *_data)
24160 {
24161   const_ix86_first_cycle_multipass_data_t data
24162     = (const_ix86_first_cycle_multipass_data_t) _data;
24163   ix86_first_cycle_multipass_data_t next_data
24164     = ix86_first_cycle_multipass_data;
24165
24166   if (data != NULL)
24167     {
24168       next_data->ifetch_block_len = data->ifetch_block_len;
24169       next_data->ifetch_block_n_insns = data->ifetch_block_n_insns;
24170     }
24171 }
24172
24173 /* Deallocate target data.  */
24174 static void
24175 core2i7_first_cycle_multipass_fini (void *_data)
24176 {
24177   ix86_first_cycle_multipass_data_t data
24178     = (ix86_first_cycle_multipass_data_t) _data;
24179
24180   if (data->ready_try_change)
24181     {
24182       sbitmap_free (data->ready_try_change);
24183       data->ready_try_change = NULL;
24184       data->ready_try_change_size = 0;
24185     }
24186 }
24187
24188 /* Prepare for scheduling pass.  */
24189 static void
24190 ix86_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
24191                         int verbose ATTRIBUTE_UNUSED,
24192                         int max_uid ATTRIBUTE_UNUSED)
24193 {
24194   /* Install scheduling hooks for current CPU.  Some of these hooks are used
24195      in time-critical parts of the scheduler, so we only set them up when
24196      they are actually used.  */
24197   switch (ix86_tune)
24198     {
24199     case PROCESSOR_CORE2_32:
24200     case PROCESSOR_CORE2_64:
24201     case PROCESSOR_COREI7_32:
24202     case PROCESSOR_COREI7_64:
24203       targetm.sched.dfa_post_advance_cycle
24204         = core2i7_dfa_post_advance_cycle;
24205       targetm.sched.first_cycle_multipass_init
24206         = core2i7_first_cycle_multipass_init;
24207       targetm.sched.first_cycle_multipass_begin
24208         = core2i7_first_cycle_multipass_begin;
24209       targetm.sched.first_cycle_multipass_issue
24210         = core2i7_first_cycle_multipass_issue;
24211       targetm.sched.first_cycle_multipass_backtrack
24212         = core2i7_first_cycle_multipass_backtrack;
24213       targetm.sched.first_cycle_multipass_end
24214         = core2i7_first_cycle_multipass_end;
24215       targetm.sched.first_cycle_multipass_fini
24216         = core2i7_first_cycle_multipass_fini;
24217
24218       /* Set decoder parameters.  */
24219       core2i7_secondary_decoder_max_insn_size = 8;
24220       core2i7_ifetch_block_size = 16;
24221       core2i7_ifetch_block_max_insns = 6;
24222       break;
24223
24224     default:
24225       targetm.sched.dfa_post_advance_cycle = NULL;
24226       targetm.sched.first_cycle_multipass_init = NULL;
24227       targetm.sched.first_cycle_multipass_begin = NULL;
24228       targetm.sched.first_cycle_multipass_issue = NULL;
24229       targetm.sched.first_cycle_multipass_backtrack = NULL;
24230       targetm.sched.first_cycle_multipass_end = NULL;
24231       targetm.sched.first_cycle_multipass_fini = NULL;
24232       break;
24233     }
24234 }
24235
24236 \f
24237 /* Compute the alignment given to a constant that is being placed in memory.
24238    EXP is the constant and ALIGN is the alignment that the object would
24239    ordinarily have.
24240    The value of this function is used instead of that alignment to align
24241    the object.  */
24242
24243 int
24244 ix86_constant_alignment (tree exp, int align)
24245 {
24246   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
24247       || TREE_CODE (exp) == INTEGER_CST)
24248     {
24249       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
24250         return 64;
24251       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
24252         return 128;
24253     }
24254   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
24255            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
24256     return BITS_PER_WORD;
24257
24258   return align;
24259 }
24260
24261 /* Compute the alignment for a static variable.
24262    TYPE is the data type, and ALIGN is the alignment that
24263    the object would ordinarily have.  The value of this function is used
24264    instead of that alignment to align the object.  */
24265
24266 int
24267 ix86_data_alignment (tree type, int align)
24268 {
24269   int max_align
24270     = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
24271
24272   if (AGGREGATE_TYPE_P (type)
24273       && TYPE_SIZE (type)
24274       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
24275       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
24276           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
24277       && align < max_align)
24278     align = max_align;
24279
24280   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
24281      to 16byte boundary.  */
24282   if (TARGET_64BIT)
24283     {
24284       if (AGGREGATE_TYPE_P (type)
24285            && TYPE_SIZE (type)
24286            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
24287            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
24288                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
24289         return 128;
24290     }
24291
24292   if (TREE_CODE (type) == ARRAY_TYPE)
24293     {
24294       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
24295         return 64;
24296       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
24297         return 128;
24298     }
24299   else if (TREE_CODE (type) == COMPLEX_TYPE)
24300     {
24301
24302       if (TYPE_MODE (type) == DCmode && align < 64)
24303         return 64;
24304       if ((TYPE_MODE (type) == XCmode
24305            || TYPE_MODE (type) == TCmode) && align < 128)
24306         return 128;
24307     }
24308   else if ((TREE_CODE (type) == RECORD_TYPE
24309             || TREE_CODE (type) == UNION_TYPE
24310             || TREE_CODE (type) == QUAL_UNION_TYPE)
24311            && TYPE_FIELDS (type))
24312     {
24313       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
24314         return 64;
24315       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
24316         return 128;
24317     }
24318   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
24319            || TREE_CODE (type) == INTEGER_TYPE)
24320     {
24321       if (TYPE_MODE (type) == DFmode && align < 64)
24322         return 64;
24323       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
24324         return 128;
24325     }
24326
24327   return align;
24328 }
24329
24330 /* Compute the alignment for a local variable or a stack slot.  EXP is
24331    the data type or decl itself, MODE is the widest mode available and
24332    ALIGN is the alignment that the object would ordinarily have.  The
24333    value of this macro is used instead of that alignment to align the
24334    object.  */
24335
24336 unsigned int
24337 ix86_local_alignment (tree exp, enum machine_mode mode,
24338                       unsigned int align)
24339 {
24340   tree type, decl;
24341
24342   if (exp && DECL_P (exp))
24343     {
24344       type = TREE_TYPE (exp);
24345       decl = exp;
24346     }
24347   else
24348     {
24349       type = exp;
24350       decl = NULL;
24351     }
24352
24353   /* Don't do dynamic stack realignment for long long objects with
24354      -mpreferred-stack-boundary=2.  */
24355   if (!TARGET_64BIT
24356       && align == 64
24357       && ix86_preferred_stack_boundary < 64
24358       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
24359       && (!type || !TYPE_USER_ALIGN (type))
24360       && (!decl || !DECL_USER_ALIGN (decl)))
24361     align = 32;
24362
24363   /* If TYPE is NULL, we are allocating a stack slot for caller-save
24364      register in MODE.  We will return the largest alignment of XF
24365      and DF.  */
24366   if (!type)
24367     {
24368       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
24369         align = GET_MODE_ALIGNMENT (DFmode);
24370       return align;
24371     }
24372
24373   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
24374      to 16byte boundary.  Exact wording is:
24375
24376      An array uses the same alignment as its elements, except that a local or
24377      global array variable of length at least 16 bytes or
24378      a C99 variable-length array variable always has alignment of at least 16 bytes.
24379
24380      This was added to allow use of aligned SSE instructions at arrays.  This
24381      rule is meant for static storage (where compiler can not do the analysis
24382      by itself).  We follow it for automatic variables only when convenient.
24383      We fully control everything in the function compiled and functions from
24384      other unit can not rely on the alignment.
24385
24386      Exclude va_list type.  It is the common case of local array where
24387      we can not benefit from the alignment.  */
24388   if (TARGET_64BIT && optimize_function_for_speed_p (cfun)
24389       && TARGET_SSE)
24390     {
24391       if (AGGREGATE_TYPE_P (type)
24392            && (va_list_type_node == NULL_TREE
24393                || (TYPE_MAIN_VARIANT (type)
24394                    != TYPE_MAIN_VARIANT (va_list_type_node)))
24395            && TYPE_SIZE (type)
24396            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
24397            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
24398                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
24399         return 128;
24400     }
24401   if (TREE_CODE (type) == ARRAY_TYPE)
24402     {
24403       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
24404         return 64;
24405       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
24406         return 128;
24407     }
24408   else if (TREE_CODE (type) == COMPLEX_TYPE)
24409     {
24410       if (TYPE_MODE (type) == DCmode && align < 64)
24411         return 64;
24412       if ((TYPE_MODE (type) == XCmode
24413            || TYPE_MODE (type) == TCmode) && align < 128)
24414         return 128;
24415     }
24416   else if ((TREE_CODE (type) == RECORD_TYPE
24417             || TREE_CODE (type) == UNION_TYPE
24418             || TREE_CODE (type) == QUAL_UNION_TYPE)
24419            && TYPE_FIELDS (type))
24420     {
24421       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
24422         return 64;
24423       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
24424         return 128;
24425     }
24426   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
24427            || TREE_CODE (type) == INTEGER_TYPE)
24428     {
24429
24430       if (TYPE_MODE (type) == DFmode && align < 64)
24431         return 64;
24432       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
24433         return 128;
24434     }
24435   return align;
24436 }
24437
24438 /* Compute the minimum required alignment for dynamic stack realignment
24439    purposes for a local variable, parameter or a stack slot.  EXP is
24440    the data type or decl itself, MODE is its mode and ALIGN is the
24441    alignment that the object would ordinarily have.  */
24442
24443 unsigned int
24444 ix86_minimum_alignment (tree exp, enum machine_mode mode,
24445                         unsigned int align)
24446 {
24447   tree type, decl;
24448
24449   if (exp && DECL_P (exp))
24450     {
24451       type = TREE_TYPE (exp);
24452       decl = exp;
24453     }
24454   else
24455     {
24456       type = exp;
24457       decl = NULL;
24458     }
24459
24460   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
24461     return align;
24462
24463   /* Don't do dynamic stack realignment for long long objects with
24464      -mpreferred-stack-boundary=2.  */
24465   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
24466       && (!type || !TYPE_USER_ALIGN (type))
24467       && (!decl || !DECL_USER_ALIGN (decl)))
24468     return 32;
24469
24470   return align;
24471 }
24472 \f
24473 /* Find a location for the static chain incoming to a nested function.
24474    This is a register, unless all free registers are used by arguments.  */
24475
24476 static rtx
24477 ix86_static_chain (const_tree fndecl, bool incoming_p)
24478 {
24479   unsigned regno;
24480
24481   if (!DECL_STATIC_CHAIN (fndecl))
24482     return NULL;
24483
24484   if (TARGET_64BIT)
24485     {
24486       /* We always use R10 in 64-bit mode.  */
24487       regno = R10_REG;
24488     }
24489   else
24490     {
24491       tree fntype;
24492       unsigned int ccvt;
24493
24494       /* By default in 32-bit mode we use ECX to pass the static chain.  */
24495       regno = CX_REG;
24496
24497       fntype = TREE_TYPE (fndecl);
24498       ccvt = ix86_get_callcvt (fntype);
24499       if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
24500         {
24501           /* Fastcall functions use ecx/edx for arguments, which leaves
24502              us with EAX for the static chain.
24503              Thiscall functions use ecx for arguments, which also
24504              leaves us with EAX for the static chain.  */
24505           regno = AX_REG;
24506         }
24507       else if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
24508         {
24509           /* Thiscall functions use ecx for arguments, which leaves
24510              us with EAX and EDX for the static chain.
24511              We are using for abi-compatibility EAX.  */
24512           regno = AX_REG;
24513         }
24514       else if (ix86_function_regparm (fntype, fndecl) == 3)
24515         {
24516           /* For regparm 3, we have no free call-clobbered registers in
24517              which to store the static chain.  In order to implement this,
24518              we have the trampoline push the static chain to the stack.
24519              However, we can't push a value below the return address when
24520              we call the nested function directly, so we have to use an
24521              alternate entry point.  For this we use ESI, and have the
24522              alternate entry point push ESI, so that things appear the
24523              same once we're executing the nested function.  */
24524           if (incoming_p)
24525             {
24526               if (fndecl == current_function_decl)
24527                 ix86_static_chain_on_stack = true;
24528               return gen_frame_mem (SImode,
24529                                     plus_constant (arg_pointer_rtx, -8));
24530             }
24531           regno = SI_REG;
24532         }
24533     }
24534
24535   return gen_rtx_REG (Pmode, regno);
24536 }
24537
24538 /* Emit RTL insns to initialize the variable parts of a trampoline.
24539    FNDECL is the decl of the target address; M_TRAMP is a MEM for
24540    the trampoline, and CHAIN_VALUE is an RTX for the static chain
24541    to be passed to the target function.  */
24542
24543 static void
24544 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
24545 {
24546   rtx mem, fnaddr;
24547   int opcode;
24548   int offset = 0;
24549
24550   fnaddr = XEXP (DECL_RTL (fndecl), 0);
24551
24552   if (TARGET_64BIT)
24553     {
24554       int size;
24555
24556       /* Load the function address to r11.  Try to load address using
24557          the shorter movl instead of movabs.  We may want to support
24558          movq for kernel mode, but kernel does not use trampolines at
24559          the moment.  */
24560       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
24561         {
24562           fnaddr = copy_to_mode_reg (DImode, fnaddr);
24563
24564           mem = adjust_address (m_tramp, HImode, offset);
24565           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
24566
24567           mem = adjust_address (m_tramp, SImode, offset + 2);
24568           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
24569           offset += 6;
24570         }
24571       else
24572         {
24573           mem = adjust_address (m_tramp, HImode, offset);
24574           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
24575
24576           mem = adjust_address (m_tramp, DImode, offset + 2);
24577           emit_move_insn (mem, fnaddr);
24578           offset += 10;
24579         }
24580
24581       /* Load static chain using movabs to r10.  Use the
24582          shorter movl instead of movabs for x32.  */
24583       if (TARGET_X32)
24584         {
24585           opcode = 0xba41;
24586           size = 6;
24587         }
24588       else
24589         {
24590           opcode = 0xba49;
24591           size = 10;
24592         }
24593
24594       mem = adjust_address (m_tramp, HImode, offset);
24595       emit_move_insn (mem, gen_int_mode (opcode, HImode));
24596
24597       mem = adjust_address (m_tramp, ptr_mode, offset + 2);
24598       emit_move_insn (mem, chain_value);
24599       offset += size;
24600
24601       /* Jump to r11; the last (unused) byte is a nop, only there to
24602          pad the write out to a single 32-bit store.  */
24603       mem = adjust_address (m_tramp, SImode, offset);
24604       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
24605       offset += 4;
24606     }
24607   else
24608     {
24609       rtx disp, chain;
24610
24611       /* Depending on the static chain location, either load a register
24612          with a constant, or push the constant to the stack.  All of the
24613          instructions are the same size.  */
24614       chain = ix86_static_chain (fndecl, true);
24615       if (REG_P (chain))
24616         {
24617           switch (REGNO (chain))
24618             {
24619             case AX_REG:
24620               opcode = 0xb8; break;
24621             case CX_REG:
24622               opcode = 0xb9; break;
24623             default:
24624               gcc_unreachable ();
24625             }
24626         }
24627       else
24628         opcode = 0x68;
24629
24630       mem = adjust_address (m_tramp, QImode, offset);
24631       emit_move_insn (mem, gen_int_mode (opcode, QImode));
24632
24633       mem = adjust_address (m_tramp, SImode, offset + 1);
24634       emit_move_insn (mem, chain_value);
24635       offset += 5;
24636
24637       mem = adjust_address (m_tramp, QImode, offset);
24638       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
24639
24640       mem = adjust_address (m_tramp, SImode, offset + 1);
24641
24642       /* Compute offset from the end of the jmp to the target function.
24643          In the case in which the trampoline stores the static chain on
24644          the stack, we need to skip the first insn which pushes the
24645          (call-saved) register static chain; this push is 1 byte.  */
24646       offset += 5;
24647       disp = expand_binop (SImode, sub_optab, fnaddr,
24648                            plus_constant (XEXP (m_tramp, 0),
24649                                           offset - (MEM_P (chain) ? 1 : 0)),
24650                            NULL_RTX, 1, OPTAB_DIRECT);
24651       emit_move_insn (mem, disp);
24652     }
24653
24654   gcc_assert (offset <= TRAMPOLINE_SIZE);
24655
24656 #ifdef HAVE_ENABLE_EXECUTE_STACK
24657 #ifdef CHECK_EXECUTE_STACK_ENABLED
24658   if (CHECK_EXECUTE_STACK_ENABLED)
24659 #endif
24660   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
24661                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
24662 #endif
24663 }
24664 \f
24665 /* The following file contains several enumerations and data structures
24666    built from the definitions in i386-builtin-types.def.  */
24667
24668 #include "i386-builtin-types.inc"
24669
24670 /* Table for the ix86 builtin non-function types.  */
24671 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
24672
24673 /* Retrieve an element from the above table, building some of
24674    the types lazily.  */
24675
24676 static tree
24677 ix86_get_builtin_type (enum ix86_builtin_type tcode)
24678 {
24679   unsigned int index;
24680   tree type, itype;
24681
24682   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
24683
24684   type = ix86_builtin_type_tab[(int) tcode];
24685   if (type != NULL)
24686     return type;
24687
24688   gcc_assert (tcode > IX86_BT_LAST_PRIM);
24689   if (tcode <= IX86_BT_LAST_VECT)
24690     {
24691       enum machine_mode mode;
24692
24693       index = tcode - IX86_BT_LAST_PRIM - 1;
24694       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
24695       mode = ix86_builtin_type_vect_mode[index];
24696
24697       type = build_vector_type_for_mode (itype, mode);
24698     }
24699   else
24700     {
24701       int quals;
24702
24703       index = tcode - IX86_BT_LAST_VECT - 1;
24704       if (tcode <= IX86_BT_LAST_PTR)
24705         quals = TYPE_UNQUALIFIED;
24706       else
24707         quals = TYPE_QUAL_CONST;
24708
24709       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
24710       if (quals != TYPE_UNQUALIFIED)
24711         itype = build_qualified_type (itype, quals);
24712
24713       type = build_pointer_type (itype);
24714     }
24715
24716   ix86_builtin_type_tab[(int) tcode] = type;
24717   return type;
24718 }
24719
24720 /* Table for the ix86 builtin function types.  */
24721 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
24722
24723 /* Retrieve an element from the above table, building some of
24724    the types lazily.  */
24725
24726 static tree
24727 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
24728 {
24729   tree type;
24730
24731   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
24732
24733   type = ix86_builtin_func_type_tab[(int) tcode];
24734   if (type != NULL)
24735     return type;
24736
24737   if (tcode <= IX86_BT_LAST_FUNC)
24738     {
24739       unsigned start = ix86_builtin_func_start[(int) tcode];
24740       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
24741       tree rtype, atype, args = void_list_node;
24742       unsigned i;
24743
24744       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
24745       for (i = after - 1; i > start; --i)
24746         {
24747           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
24748           args = tree_cons (NULL, atype, args);
24749         }
24750
24751       type = build_function_type (rtype, args);
24752     }
24753   else
24754     {
24755       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
24756       enum ix86_builtin_func_type icode;
24757
24758       icode = ix86_builtin_func_alias_base[index];
24759       type = ix86_get_builtin_func_type (icode);
24760     }
24761
24762   ix86_builtin_func_type_tab[(int) tcode] = type;
24763   return type;
24764 }
24765
24766
24767 /* Codes for all the SSE/MMX builtins.  */
24768 enum ix86_builtins
24769 {
24770   IX86_BUILTIN_ADDPS,
24771   IX86_BUILTIN_ADDSS,
24772   IX86_BUILTIN_DIVPS,
24773   IX86_BUILTIN_DIVSS,
24774   IX86_BUILTIN_MULPS,
24775   IX86_BUILTIN_MULSS,
24776   IX86_BUILTIN_SUBPS,
24777   IX86_BUILTIN_SUBSS,
24778
24779   IX86_BUILTIN_CMPEQPS,
24780   IX86_BUILTIN_CMPLTPS,
24781   IX86_BUILTIN_CMPLEPS,
24782   IX86_BUILTIN_CMPGTPS,
24783   IX86_BUILTIN_CMPGEPS,
24784   IX86_BUILTIN_CMPNEQPS,
24785   IX86_BUILTIN_CMPNLTPS,
24786   IX86_BUILTIN_CMPNLEPS,
24787   IX86_BUILTIN_CMPNGTPS,
24788   IX86_BUILTIN_CMPNGEPS,
24789   IX86_BUILTIN_CMPORDPS,
24790   IX86_BUILTIN_CMPUNORDPS,
24791   IX86_BUILTIN_CMPEQSS,
24792   IX86_BUILTIN_CMPLTSS,
24793   IX86_BUILTIN_CMPLESS,
24794   IX86_BUILTIN_CMPNEQSS,
24795   IX86_BUILTIN_CMPNLTSS,
24796   IX86_BUILTIN_CMPNLESS,
24797   IX86_BUILTIN_CMPNGTSS,
24798   IX86_BUILTIN_CMPNGESS,
24799   IX86_BUILTIN_CMPORDSS,
24800   IX86_BUILTIN_CMPUNORDSS,
24801
24802   IX86_BUILTIN_COMIEQSS,
24803   IX86_BUILTIN_COMILTSS,
24804   IX86_BUILTIN_COMILESS,
24805   IX86_BUILTIN_COMIGTSS,
24806   IX86_BUILTIN_COMIGESS,
24807   IX86_BUILTIN_COMINEQSS,
24808   IX86_BUILTIN_UCOMIEQSS,
24809   IX86_BUILTIN_UCOMILTSS,
24810   IX86_BUILTIN_UCOMILESS,
24811   IX86_BUILTIN_UCOMIGTSS,
24812   IX86_BUILTIN_UCOMIGESS,
24813   IX86_BUILTIN_UCOMINEQSS,
24814
24815   IX86_BUILTIN_CVTPI2PS,
24816   IX86_BUILTIN_CVTPS2PI,
24817   IX86_BUILTIN_CVTSI2SS,
24818   IX86_BUILTIN_CVTSI642SS,
24819   IX86_BUILTIN_CVTSS2SI,
24820   IX86_BUILTIN_CVTSS2SI64,
24821   IX86_BUILTIN_CVTTPS2PI,
24822   IX86_BUILTIN_CVTTSS2SI,
24823   IX86_BUILTIN_CVTTSS2SI64,
24824
24825   IX86_BUILTIN_MAXPS,
24826   IX86_BUILTIN_MAXSS,
24827   IX86_BUILTIN_MINPS,
24828   IX86_BUILTIN_MINSS,
24829
24830   IX86_BUILTIN_LOADUPS,
24831   IX86_BUILTIN_STOREUPS,
24832   IX86_BUILTIN_MOVSS,
24833
24834   IX86_BUILTIN_MOVHLPS,
24835   IX86_BUILTIN_MOVLHPS,
24836   IX86_BUILTIN_LOADHPS,
24837   IX86_BUILTIN_LOADLPS,
24838   IX86_BUILTIN_STOREHPS,
24839   IX86_BUILTIN_STORELPS,
24840
24841   IX86_BUILTIN_MASKMOVQ,
24842   IX86_BUILTIN_MOVMSKPS,
24843   IX86_BUILTIN_PMOVMSKB,
24844
24845   IX86_BUILTIN_MOVNTPS,
24846   IX86_BUILTIN_MOVNTQ,
24847
24848   IX86_BUILTIN_LOADDQU,
24849   IX86_BUILTIN_STOREDQU,
24850
24851   IX86_BUILTIN_PACKSSWB,
24852   IX86_BUILTIN_PACKSSDW,
24853   IX86_BUILTIN_PACKUSWB,
24854
24855   IX86_BUILTIN_PADDB,
24856   IX86_BUILTIN_PADDW,
24857   IX86_BUILTIN_PADDD,
24858   IX86_BUILTIN_PADDQ,
24859   IX86_BUILTIN_PADDSB,
24860   IX86_BUILTIN_PADDSW,
24861   IX86_BUILTIN_PADDUSB,
24862   IX86_BUILTIN_PADDUSW,
24863   IX86_BUILTIN_PSUBB,
24864   IX86_BUILTIN_PSUBW,
24865   IX86_BUILTIN_PSUBD,
24866   IX86_BUILTIN_PSUBQ,
24867   IX86_BUILTIN_PSUBSB,
24868   IX86_BUILTIN_PSUBSW,
24869   IX86_BUILTIN_PSUBUSB,
24870   IX86_BUILTIN_PSUBUSW,
24871
24872   IX86_BUILTIN_PAND,
24873   IX86_BUILTIN_PANDN,
24874   IX86_BUILTIN_POR,
24875   IX86_BUILTIN_PXOR,
24876
24877   IX86_BUILTIN_PAVGB,
24878   IX86_BUILTIN_PAVGW,
24879
24880   IX86_BUILTIN_PCMPEQB,
24881   IX86_BUILTIN_PCMPEQW,
24882   IX86_BUILTIN_PCMPEQD,
24883   IX86_BUILTIN_PCMPGTB,
24884   IX86_BUILTIN_PCMPGTW,
24885   IX86_BUILTIN_PCMPGTD,
24886
24887   IX86_BUILTIN_PMADDWD,
24888
24889   IX86_BUILTIN_PMAXSW,
24890   IX86_BUILTIN_PMAXUB,
24891   IX86_BUILTIN_PMINSW,
24892   IX86_BUILTIN_PMINUB,
24893
24894   IX86_BUILTIN_PMULHUW,
24895   IX86_BUILTIN_PMULHW,
24896   IX86_BUILTIN_PMULLW,
24897
24898   IX86_BUILTIN_PSADBW,
24899   IX86_BUILTIN_PSHUFW,
24900
24901   IX86_BUILTIN_PSLLW,
24902   IX86_BUILTIN_PSLLD,
24903   IX86_BUILTIN_PSLLQ,
24904   IX86_BUILTIN_PSRAW,
24905   IX86_BUILTIN_PSRAD,
24906   IX86_BUILTIN_PSRLW,
24907   IX86_BUILTIN_PSRLD,
24908   IX86_BUILTIN_PSRLQ,
24909   IX86_BUILTIN_PSLLWI,
24910   IX86_BUILTIN_PSLLDI,
24911   IX86_BUILTIN_PSLLQI,
24912   IX86_BUILTIN_PSRAWI,
24913   IX86_BUILTIN_PSRADI,
24914   IX86_BUILTIN_PSRLWI,
24915   IX86_BUILTIN_PSRLDI,
24916   IX86_BUILTIN_PSRLQI,
24917
24918   IX86_BUILTIN_PUNPCKHBW,
24919   IX86_BUILTIN_PUNPCKHWD,
24920   IX86_BUILTIN_PUNPCKHDQ,
24921   IX86_BUILTIN_PUNPCKLBW,
24922   IX86_BUILTIN_PUNPCKLWD,
24923   IX86_BUILTIN_PUNPCKLDQ,
24924
24925   IX86_BUILTIN_SHUFPS,
24926
24927   IX86_BUILTIN_RCPPS,
24928   IX86_BUILTIN_RCPSS,
24929   IX86_BUILTIN_RSQRTPS,
24930   IX86_BUILTIN_RSQRTPS_NR,
24931   IX86_BUILTIN_RSQRTSS,
24932   IX86_BUILTIN_RSQRTF,
24933   IX86_BUILTIN_SQRTPS,
24934   IX86_BUILTIN_SQRTPS_NR,
24935   IX86_BUILTIN_SQRTSS,
24936
24937   IX86_BUILTIN_UNPCKHPS,
24938   IX86_BUILTIN_UNPCKLPS,
24939
24940   IX86_BUILTIN_ANDPS,
24941   IX86_BUILTIN_ANDNPS,
24942   IX86_BUILTIN_ORPS,
24943   IX86_BUILTIN_XORPS,
24944
24945   IX86_BUILTIN_EMMS,
24946   IX86_BUILTIN_LDMXCSR,
24947   IX86_BUILTIN_STMXCSR,
24948   IX86_BUILTIN_SFENCE,
24949
24950   /* 3DNow! Original */
24951   IX86_BUILTIN_FEMMS,
24952   IX86_BUILTIN_PAVGUSB,
24953   IX86_BUILTIN_PF2ID,
24954   IX86_BUILTIN_PFACC,
24955   IX86_BUILTIN_PFADD,
24956   IX86_BUILTIN_PFCMPEQ,
24957   IX86_BUILTIN_PFCMPGE,
24958   IX86_BUILTIN_PFCMPGT,
24959   IX86_BUILTIN_PFMAX,
24960   IX86_BUILTIN_PFMIN,
24961   IX86_BUILTIN_PFMUL,
24962   IX86_BUILTIN_PFRCP,
24963   IX86_BUILTIN_PFRCPIT1,
24964   IX86_BUILTIN_PFRCPIT2,
24965   IX86_BUILTIN_PFRSQIT1,
24966   IX86_BUILTIN_PFRSQRT,
24967   IX86_BUILTIN_PFSUB,
24968   IX86_BUILTIN_PFSUBR,
24969   IX86_BUILTIN_PI2FD,
24970   IX86_BUILTIN_PMULHRW,
24971
24972   /* 3DNow! Athlon Extensions */
24973   IX86_BUILTIN_PF2IW,
24974   IX86_BUILTIN_PFNACC,
24975   IX86_BUILTIN_PFPNACC,
24976   IX86_BUILTIN_PI2FW,
24977   IX86_BUILTIN_PSWAPDSI,
24978   IX86_BUILTIN_PSWAPDSF,
24979
24980   /* SSE2 */
24981   IX86_BUILTIN_ADDPD,
24982   IX86_BUILTIN_ADDSD,
24983   IX86_BUILTIN_DIVPD,
24984   IX86_BUILTIN_DIVSD,
24985   IX86_BUILTIN_MULPD,
24986   IX86_BUILTIN_MULSD,
24987   IX86_BUILTIN_SUBPD,
24988   IX86_BUILTIN_SUBSD,
24989
24990   IX86_BUILTIN_CMPEQPD,
24991   IX86_BUILTIN_CMPLTPD,
24992   IX86_BUILTIN_CMPLEPD,
24993   IX86_BUILTIN_CMPGTPD,
24994   IX86_BUILTIN_CMPGEPD,
24995   IX86_BUILTIN_CMPNEQPD,
24996   IX86_BUILTIN_CMPNLTPD,
24997   IX86_BUILTIN_CMPNLEPD,
24998   IX86_BUILTIN_CMPNGTPD,
24999   IX86_BUILTIN_CMPNGEPD,
25000   IX86_BUILTIN_CMPORDPD,
25001   IX86_BUILTIN_CMPUNORDPD,
25002   IX86_BUILTIN_CMPEQSD,
25003   IX86_BUILTIN_CMPLTSD,
25004   IX86_BUILTIN_CMPLESD,
25005   IX86_BUILTIN_CMPNEQSD,
25006   IX86_BUILTIN_CMPNLTSD,
25007   IX86_BUILTIN_CMPNLESD,
25008   IX86_BUILTIN_CMPORDSD,
25009   IX86_BUILTIN_CMPUNORDSD,
25010
25011   IX86_BUILTIN_COMIEQSD,
25012   IX86_BUILTIN_COMILTSD,
25013   IX86_BUILTIN_COMILESD,
25014   IX86_BUILTIN_COMIGTSD,
25015   IX86_BUILTIN_COMIGESD,
25016   IX86_BUILTIN_COMINEQSD,
25017   IX86_BUILTIN_UCOMIEQSD,
25018   IX86_BUILTIN_UCOMILTSD,
25019   IX86_BUILTIN_UCOMILESD,
25020   IX86_BUILTIN_UCOMIGTSD,
25021   IX86_BUILTIN_UCOMIGESD,
25022   IX86_BUILTIN_UCOMINEQSD,
25023
25024   IX86_BUILTIN_MAXPD,
25025   IX86_BUILTIN_MAXSD,
25026   IX86_BUILTIN_MINPD,
25027   IX86_BUILTIN_MINSD,
25028
25029   IX86_BUILTIN_ANDPD,
25030   IX86_BUILTIN_ANDNPD,
25031   IX86_BUILTIN_ORPD,
25032   IX86_BUILTIN_XORPD,
25033
25034   IX86_BUILTIN_SQRTPD,
25035   IX86_BUILTIN_SQRTSD,
25036
25037   IX86_BUILTIN_UNPCKHPD,
25038   IX86_BUILTIN_UNPCKLPD,
25039
25040   IX86_BUILTIN_SHUFPD,
25041
25042   IX86_BUILTIN_LOADUPD,
25043   IX86_BUILTIN_STOREUPD,
25044   IX86_BUILTIN_MOVSD,
25045
25046   IX86_BUILTIN_LOADHPD,
25047   IX86_BUILTIN_LOADLPD,
25048
25049   IX86_BUILTIN_CVTDQ2PD,
25050   IX86_BUILTIN_CVTDQ2PS,
25051
25052   IX86_BUILTIN_CVTPD2DQ,
25053   IX86_BUILTIN_CVTPD2PI,
25054   IX86_BUILTIN_CVTPD2PS,
25055   IX86_BUILTIN_CVTTPD2DQ,
25056   IX86_BUILTIN_CVTTPD2PI,
25057
25058   IX86_BUILTIN_CVTPI2PD,
25059   IX86_BUILTIN_CVTSI2SD,
25060   IX86_BUILTIN_CVTSI642SD,
25061
25062   IX86_BUILTIN_CVTSD2SI,
25063   IX86_BUILTIN_CVTSD2SI64,
25064   IX86_BUILTIN_CVTSD2SS,
25065   IX86_BUILTIN_CVTSS2SD,
25066   IX86_BUILTIN_CVTTSD2SI,
25067   IX86_BUILTIN_CVTTSD2SI64,
25068
25069   IX86_BUILTIN_CVTPS2DQ,
25070   IX86_BUILTIN_CVTPS2PD,
25071   IX86_BUILTIN_CVTTPS2DQ,
25072
25073   IX86_BUILTIN_MOVNTI,
25074   IX86_BUILTIN_MOVNTI64,
25075   IX86_BUILTIN_MOVNTPD,
25076   IX86_BUILTIN_MOVNTDQ,
25077
25078   IX86_BUILTIN_MOVQ128,
25079
25080   /* SSE2 MMX */
25081   IX86_BUILTIN_MASKMOVDQU,
25082   IX86_BUILTIN_MOVMSKPD,
25083   IX86_BUILTIN_PMOVMSKB128,
25084
25085   IX86_BUILTIN_PACKSSWB128,
25086   IX86_BUILTIN_PACKSSDW128,
25087   IX86_BUILTIN_PACKUSWB128,
25088
25089   IX86_BUILTIN_PADDB128,
25090   IX86_BUILTIN_PADDW128,
25091   IX86_BUILTIN_PADDD128,
25092   IX86_BUILTIN_PADDQ128,
25093   IX86_BUILTIN_PADDSB128,
25094   IX86_BUILTIN_PADDSW128,
25095   IX86_BUILTIN_PADDUSB128,
25096   IX86_BUILTIN_PADDUSW128,
25097   IX86_BUILTIN_PSUBB128,
25098   IX86_BUILTIN_PSUBW128,
25099   IX86_BUILTIN_PSUBD128,
25100   IX86_BUILTIN_PSUBQ128,
25101   IX86_BUILTIN_PSUBSB128,
25102   IX86_BUILTIN_PSUBSW128,
25103   IX86_BUILTIN_PSUBUSB128,
25104   IX86_BUILTIN_PSUBUSW128,
25105
25106   IX86_BUILTIN_PAND128,
25107   IX86_BUILTIN_PANDN128,
25108   IX86_BUILTIN_POR128,
25109   IX86_BUILTIN_PXOR128,
25110
25111   IX86_BUILTIN_PAVGB128,
25112   IX86_BUILTIN_PAVGW128,
25113
25114   IX86_BUILTIN_PCMPEQB128,
25115   IX86_BUILTIN_PCMPEQW128,
25116   IX86_BUILTIN_PCMPEQD128,
25117   IX86_BUILTIN_PCMPGTB128,
25118   IX86_BUILTIN_PCMPGTW128,
25119   IX86_BUILTIN_PCMPGTD128,
25120
25121   IX86_BUILTIN_PMADDWD128,
25122
25123   IX86_BUILTIN_PMAXSW128,
25124   IX86_BUILTIN_PMAXUB128,
25125   IX86_BUILTIN_PMINSW128,
25126   IX86_BUILTIN_PMINUB128,
25127
25128   IX86_BUILTIN_PMULUDQ,
25129   IX86_BUILTIN_PMULUDQ128,
25130   IX86_BUILTIN_PMULHUW128,
25131   IX86_BUILTIN_PMULHW128,
25132   IX86_BUILTIN_PMULLW128,
25133
25134   IX86_BUILTIN_PSADBW128,
25135   IX86_BUILTIN_PSHUFHW,
25136   IX86_BUILTIN_PSHUFLW,
25137   IX86_BUILTIN_PSHUFD,
25138
25139   IX86_BUILTIN_PSLLDQI128,
25140   IX86_BUILTIN_PSLLWI128,
25141   IX86_BUILTIN_PSLLDI128,
25142   IX86_BUILTIN_PSLLQI128,
25143   IX86_BUILTIN_PSRAWI128,
25144   IX86_BUILTIN_PSRADI128,
25145   IX86_BUILTIN_PSRLDQI128,
25146   IX86_BUILTIN_PSRLWI128,
25147   IX86_BUILTIN_PSRLDI128,
25148   IX86_BUILTIN_PSRLQI128,
25149
25150   IX86_BUILTIN_PSLLDQ128,
25151   IX86_BUILTIN_PSLLW128,
25152   IX86_BUILTIN_PSLLD128,
25153   IX86_BUILTIN_PSLLQ128,
25154   IX86_BUILTIN_PSRAW128,
25155   IX86_BUILTIN_PSRAD128,
25156   IX86_BUILTIN_PSRLW128,
25157   IX86_BUILTIN_PSRLD128,
25158   IX86_BUILTIN_PSRLQ128,
25159
25160   IX86_BUILTIN_PUNPCKHBW128,
25161   IX86_BUILTIN_PUNPCKHWD128,
25162   IX86_BUILTIN_PUNPCKHDQ128,
25163   IX86_BUILTIN_PUNPCKHQDQ128,
25164   IX86_BUILTIN_PUNPCKLBW128,
25165   IX86_BUILTIN_PUNPCKLWD128,
25166   IX86_BUILTIN_PUNPCKLDQ128,
25167   IX86_BUILTIN_PUNPCKLQDQ128,
25168
25169   IX86_BUILTIN_CLFLUSH,
25170   IX86_BUILTIN_MFENCE,
25171   IX86_BUILTIN_LFENCE,
25172   IX86_BUILTIN_PAUSE,
25173
25174   IX86_BUILTIN_BSRSI,
25175   IX86_BUILTIN_BSRDI,
25176   IX86_BUILTIN_RDPMC,
25177   IX86_BUILTIN_RDTSC,
25178   IX86_BUILTIN_RDTSCP,
25179   IX86_BUILTIN_ROLQI,
25180   IX86_BUILTIN_ROLHI,
25181   IX86_BUILTIN_RORQI,
25182   IX86_BUILTIN_RORHI,
25183
25184   /* SSE3.  */
25185   IX86_BUILTIN_ADDSUBPS,
25186   IX86_BUILTIN_HADDPS,
25187   IX86_BUILTIN_HSUBPS,
25188   IX86_BUILTIN_MOVSHDUP,
25189   IX86_BUILTIN_MOVSLDUP,
25190   IX86_BUILTIN_ADDSUBPD,
25191   IX86_BUILTIN_HADDPD,
25192   IX86_BUILTIN_HSUBPD,
25193   IX86_BUILTIN_LDDQU,
25194
25195   IX86_BUILTIN_MONITOR,
25196   IX86_BUILTIN_MWAIT,
25197
25198   /* SSSE3.  */
25199   IX86_BUILTIN_PHADDW,
25200   IX86_BUILTIN_PHADDD,
25201   IX86_BUILTIN_PHADDSW,
25202   IX86_BUILTIN_PHSUBW,
25203   IX86_BUILTIN_PHSUBD,
25204   IX86_BUILTIN_PHSUBSW,
25205   IX86_BUILTIN_PMADDUBSW,
25206   IX86_BUILTIN_PMULHRSW,
25207   IX86_BUILTIN_PSHUFB,
25208   IX86_BUILTIN_PSIGNB,
25209   IX86_BUILTIN_PSIGNW,
25210   IX86_BUILTIN_PSIGND,
25211   IX86_BUILTIN_PALIGNR,
25212   IX86_BUILTIN_PABSB,
25213   IX86_BUILTIN_PABSW,
25214   IX86_BUILTIN_PABSD,
25215
25216   IX86_BUILTIN_PHADDW128,
25217   IX86_BUILTIN_PHADDD128,
25218   IX86_BUILTIN_PHADDSW128,
25219   IX86_BUILTIN_PHSUBW128,
25220   IX86_BUILTIN_PHSUBD128,
25221   IX86_BUILTIN_PHSUBSW128,
25222   IX86_BUILTIN_PMADDUBSW128,
25223   IX86_BUILTIN_PMULHRSW128,
25224   IX86_BUILTIN_PSHUFB128,
25225   IX86_BUILTIN_PSIGNB128,
25226   IX86_BUILTIN_PSIGNW128,
25227   IX86_BUILTIN_PSIGND128,
25228   IX86_BUILTIN_PALIGNR128,
25229   IX86_BUILTIN_PABSB128,
25230   IX86_BUILTIN_PABSW128,
25231   IX86_BUILTIN_PABSD128,
25232
25233   /* AMDFAM10 - SSE4A New Instructions.  */
25234   IX86_BUILTIN_MOVNTSD,
25235   IX86_BUILTIN_MOVNTSS,
25236   IX86_BUILTIN_EXTRQI,
25237   IX86_BUILTIN_EXTRQ,
25238   IX86_BUILTIN_INSERTQI,
25239   IX86_BUILTIN_INSERTQ,
25240
25241   /* SSE4.1.  */
25242   IX86_BUILTIN_BLENDPD,
25243   IX86_BUILTIN_BLENDPS,
25244   IX86_BUILTIN_BLENDVPD,
25245   IX86_BUILTIN_BLENDVPS,
25246   IX86_BUILTIN_PBLENDVB128,
25247   IX86_BUILTIN_PBLENDW128,
25248
25249   IX86_BUILTIN_DPPD,
25250   IX86_BUILTIN_DPPS,
25251
25252   IX86_BUILTIN_INSERTPS128,
25253
25254   IX86_BUILTIN_MOVNTDQA,
25255   IX86_BUILTIN_MPSADBW128,
25256   IX86_BUILTIN_PACKUSDW128,
25257   IX86_BUILTIN_PCMPEQQ,
25258   IX86_BUILTIN_PHMINPOSUW128,
25259
25260   IX86_BUILTIN_PMAXSB128,
25261   IX86_BUILTIN_PMAXSD128,
25262   IX86_BUILTIN_PMAXUD128,
25263   IX86_BUILTIN_PMAXUW128,
25264
25265   IX86_BUILTIN_PMINSB128,
25266   IX86_BUILTIN_PMINSD128,
25267   IX86_BUILTIN_PMINUD128,
25268   IX86_BUILTIN_PMINUW128,
25269
25270   IX86_BUILTIN_PMOVSXBW128,
25271   IX86_BUILTIN_PMOVSXBD128,
25272   IX86_BUILTIN_PMOVSXBQ128,
25273   IX86_BUILTIN_PMOVSXWD128,
25274   IX86_BUILTIN_PMOVSXWQ128,
25275   IX86_BUILTIN_PMOVSXDQ128,
25276
25277   IX86_BUILTIN_PMOVZXBW128,
25278   IX86_BUILTIN_PMOVZXBD128,
25279   IX86_BUILTIN_PMOVZXBQ128,
25280   IX86_BUILTIN_PMOVZXWD128,
25281   IX86_BUILTIN_PMOVZXWQ128,
25282   IX86_BUILTIN_PMOVZXDQ128,
25283
25284   IX86_BUILTIN_PMULDQ128,
25285   IX86_BUILTIN_PMULLD128,
25286
25287   IX86_BUILTIN_ROUNDSD,
25288   IX86_BUILTIN_ROUNDSS,
25289
25290   IX86_BUILTIN_ROUNDPD,
25291   IX86_BUILTIN_ROUNDPS,
25292
25293   IX86_BUILTIN_FLOORPD,
25294   IX86_BUILTIN_CEILPD,
25295   IX86_BUILTIN_TRUNCPD,
25296   IX86_BUILTIN_RINTPD,
25297   IX86_BUILTIN_ROUNDPD_AZ,
25298
25299   IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX,
25300   IX86_BUILTIN_CEILPD_VEC_PACK_SFIX,
25301   IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX,
25302
25303   IX86_BUILTIN_FLOORPS,
25304   IX86_BUILTIN_CEILPS,
25305   IX86_BUILTIN_TRUNCPS,
25306   IX86_BUILTIN_RINTPS,
25307   IX86_BUILTIN_ROUNDPS_AZ,
25308
25309   IX86_BUILTIN_FLOORPS_SFIX,
25310   IX86_BUILTIN_CEILPS_SFIX,
25311   IX86_BUILTIN_ROUNDPS_AZ_SFIX,
25312
25313   IX86_BUILTIN_PTESTZ,
25314   IX86_BUILTIN_PTESTC,
25315   IX86_BUILTIN_PTESTNZC,
25316
25317   IX86_BUILTIN_VEC_INIT_V2SI,
25318   IX86_BUILTIN_VEC_INIT_V4HI,
25319   IX86_BUILTIN_VEC_INIT_V8QI,
25320   IX86_BUILTIN_VEC_EXT_V2DF,
25321   IX86_BUILTIN_VEC_EXT_V2DI,
25322   IX86_BUILTIN_VEC_EXT_V4SF,
25323   IX86_BUILTIN_VEC_EXT_V4SI,
25324   IX86_BUILTIN_VEC_EXT_V8HI,
25325   IX86_BUILTIN_VEC_EXT_V2SI,
25326   IX86_BUILTIN_VEC_EXT_V4HI,
25327   IX86_BUILTIN_VEC_EXT_V16QI,
25328   IX86_BUILTIN_VEC_SET_V2DI,
25329   IX86_BUILTIN_VEC_SET_V4SF,
25330   IX86_BUILTIN_VEC_SET_V4SI,
25331   IX86_BUILTIN_VEC_SET_V8HI,
25332   IX86_BUILTIN_VEC_SET_V4HI,
25333   IX86_BUILTIN_VEC_SET_V16QI,
25334
25335   IX86_BUILTIN_VEC_PACK_SFIX,
25336   IX86_BUILTIN_VEC_PACK_SFIX256,
25337
25338   /* SSE4.2.  */
25339   IX86_BUILTIN_CRC32QI,
25340   IX86_BUILTIN_CRC32HI,
25341   IX86_BUILTIN_CRC32SI,
25342   IX86_BUILTIN_CRC32DI,
25343
25344   IX86_BUILTIN_PCMPESTRI128,
25345   IX86_BUILTIN_PCMPESTRM128,
25346   IX86_BUILTIN_PCMPESTRA128,
25347   IX86_BUILTIN_PCMPESTRC128,
25348   IX86_BUILTIN_PCMPESTRO128,
25349   IX86_BUILTIN_PCMPESTRS128,
25350   IX86_BUILTIN_PCMPESTRZ128,
25351   IX86_BUILTIN_PCMPISTRI128,
25352   IX86_BUILTIN_PCMPISTRM128,
25353   IX86_BUILTIN_PCMPISTRA128,
25354   IX86_BUILTIN_PCMPISTRC128,
25355   IX86_BUILTIN_PCMPISTRO128,
25356   IX86_BUILTIN_PCMPISTRS128,
25357   IX86_BUILTIN_PCMPISTRZ128,
25358
25359   IX86_BUILTIN_PCMPGTQ,
25360
25361   /* AES instructions */
25362   IX86_BUILTIN_AESENC128,
25363   IX86_BUILTIN_AESENCLAST128,
25364   IX86_BUILTIN_AESDEC128,
25365   IX86_BUILTIN_AESDECLAST128,
25366   IX86_BUILTIN_AESIMC128,
25367   IX86_BUILTIN_AESKEYGENASSIST128,
25368
25369   /* PCLMUL instruction */
25370   IX86_BUILTIN_PCLMULQDQ128,
25371
25372   /* AVX */
25373   IX86_BUILTIN_ADDPD256,
25374   IX86_BUILTIN_ADDPS256,
25375   IX86_BUILTIN_ADDSUBPD256,
25376   IX86_BUILTIN_ADDSUBPS256,
25377   IX86_BUILTIN_ANDPD256,
25378   IX86_BUILTIN_ANDPS256,
25379   IX86_BUILTIN_ANDNPD256,
25380   IX86_BUILTIN_ANDNPS256,
25381   IX86_BUILTIN_BLENDPD256,
25382   IX86_BUILTIN_BLENDPS256,
25383   IX86_BUILTIN_BLENDVPD256,
25384   IX86_BUILTIN_BLENDVPS256,
25385   IX86_BUILTIN_DIVPD256,
25386   IX86_BUILTIN_DIVPS256,
25387   IX86_BUILTIN_DPPS256,
25388   IX86_BUILTIN_HADDPD256,
25389   IX86_BUILTIN_HADDPS256,
25390   IX86_BUILTIN_HSUBPD256,
25391   IX86_BUILTIN_HSUBPS256,
25392   IX86_BUILTIN_MAXPD256,
25393   IX86_BUILTIN_MAXPS256,
25394   IX86_BUILTIN_MINPD256,
25395   IX86_BUILTIN_MINPS256,
25396   IX86_BUILTIN_MULPD256,
25397   IX86_BUILTIN_MULPS256,
25398   IX86_BUILTIN_ORPD256,
25399   IX86_BUILTIN_ORPS256,
25400   IX86_BUILTIN_SHUFPD256,
25401   IX86_BUILTIN_SHUFPS256,
25402   IX86_BUILTIN_SUBPD256,
25403   IX86_BUILTIN_SUBPS256,
25404   IX86_BUILTIN_XORPD256,
25405   IX86_BUILTIN_XORPS256,
25406   IX86_BUILTIN_CMPSD,
25407   IX86_BUILTIN_CMPSS,
25408   IX86_BUILTIN_CMPPD,
25409   IX86_BUILTIN_CMPPS,
25410   IX86_BUILTIN_CMPPD256,
25411   IX86_BUILTIN_CMPPS256,
25412   IX86_BUILTIN_CVTDQ2PD256,
25413   IX86_BUILTIN_CVTDQ2PS256,
25414   IX86_BUILTIN_CVTPD2PS256,
25415   IX86_BUILTIN_CVTPS2DQ256,
25416   IX86_BUILTIN_CVTPS2PD256,
25417   IX86_BUILTIN_CVTTPD2DQ256,
25418   IX86_BUILTIN_CVTPD2DQ256,
25419   IX86_BUILTIN_CVTTPS2DQ256,
25420   IX86_BUILTIN_EXTRACTF128PD256,
25421   IX86_BUILTIN_EXTRACTF128PS256,
25422   IX86_BUILTIN_EXTRACTF128SI256,
25423   IX86_BUILTIN_VZEROALL,
25424   IX86_BUILTIN_VZEROUPPER,
25425   IX86_BUILTIN_VPERMILVARPD,
25426   IX86_BUILTIN_VPERMILVARPS,
25427   IX86_BUILTIN_VPERMILVARPD256,
25428   IX86_BUILTIN_VPERMILVARPS256,
25429   IX86_BUILTIN_VPERMILPD,
25430   IX86_BUILTIN_VPERMILPS,
25431   IX86_BUILTIN_VPERMILPD256,
25432   IX86_BUILTIN_VPERMILPS256,
25433   IX86_BUILTIN_VPERMIL2PD,
25434   IX86_BUILTIN_VPERMIL2PS,
25435   IX86_BUILTIN_VPERMIL2PD256,
25436   IX86_BUILTIN_VPERMIL2PS256,
25437   IX86_BUILTIN_VPERM2F128PD256,
25438   IX86_BUILTIN_VPERM2F128PS256,
25439   IX86_BUILTIN_VPERM2F128SI256,
25440   IX86_BUILTIN_VBROADCASTSS,
25441   IX86_BUILTIN_VBROADCASTSD256,
25442   IX86_BUILTIN_VBROADCASTSS256,
25443   IX86_BUILTIN_VBROADCASTPD256,
25444   IX86_BUILTIN_VBROADCASTPS256,
25445   IX86_BUILTIN_VINSERTF128PD256,
25446   IX86_BUILTIN_VINSERTF128PS256,
25447   IX86_BUILTIN_VINSERTF128SI256,
25448   IX86_BUILTIN_LOADUPD256,
25449   IX86_BUILTIN_LOADUPS256,
25450   IX86_BUILTIN_STOREUPD256,
25451   IX86_BUILTIN_STOREUPS256,
25452   IX86_BUILTIN_LDDQU256,
25453   IX86_BUILTIN_MOVNTDQ256,
25454   IX86_BUILTIN_MOVNTPD256,
25455   IX86_BUILTIN_MOVNTPS256,
25456   IX86_BUILTIN_LOADDQU256,
25457   IX86_BUILTIN_STOREDQU256,
25458   IX86_BUILTIN_MASKLOADPD,
25459   IX86_BUILTIN_MASKLOADPS,
25460   IX86_BUILTIN_MASKSTOREPD,
25461   IX86_BUILTIN_MASKSTOREPS,
25462   IX86_BUILTIN_MASKLOADPD256,
25463   IX86_BUILTIN_MASKLOADPS256,
25464   IX86_BUILTIN_MASKSTOREPD256,
25465   IX86_BUILTIN_MASKSTOREPS256,
25466   IX86_BUILTIN_MOVSHDUP256,
25467   IX86_BUILTIN_MOVSLDUP256,
25468   IX86_BUILTIN_MOVDDUP256,
25469
25470   IX86_BUILTIN_SQRTPD256,
25471   IX86_BUILTIN_SQRTPS256,
25472   IX86_BUILTIN_SQRTPS_NR256,
25473   IX86_BUILTIN_RSQRTPS256,
25474   IX86_BUILTIN_RSQRTPS_NR256,
25475
25476   IX86_BUILTIN_RCPPS256,
25477
25478   IX86_BUILTIN_ROUNDPD256,
25479   IX86_BUILTIN_ROUNDPS256,
25480
25481   IX86_BUILTIN_FLOORPD256,
25482   IX86_BUILTIN_CEILPD256,
25483   IX86_BUILTIN_TRUNCPD256,
25484   IX86_BUILTIN_RINTPD256,
25485   IX86_BUILTIN_ROUNDPD_AZ256,
25486
25487   IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX256,
25488   IX86_BUILTIN_CEILPD_VEC_PACK_SFIX256,
25489   IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX256,
25490
25491   IX86_BUILTIN_FLOORPS256,
25492   IX86_BUILTIN_CEILPS256,
25493   IX86_BUILTIN_TRUNCPS256,
25494   IX86_BUILTIN_RINTPS256,
25495   IX86_BUILTIN_ROUNDPS_AZ256,
25496
25497   IX86_BUILTIN_FLOORPS_SFIX256,
25498   IX86_BUILTIN_CEILPS_SFIX256,
25499   IX86_BUILTIN_ROUNDPS_AZ_SFIX256,
25500
25501   IX86_BUILTIN_UNPCKHPD256,
25502   IX86_BUILTIN_UNPCKLPD256,
25503   IX86_BUILTIN_UNPCKHPS256,
25504   IX86_BUILTIN_UNPCKLPS256,
25505
25506   IX86_BUILTIN_SI256_SI,
25507   IX86_BUILTIN_PS256_PS,
25508   IX86_BUILTIN_PD256_PD,
25509   IX86_BUILTIN_SI_SI256,
25510   IX86_BUILTIN_PS_PS256,
25511   IX86_BUILTIN_PD_PD256,
25512
25513   IX86_BUILTIN_VTESTZPD,
25514   IX86_BUILTIN_VTESTCPD,
25515   IX86_BUILTIN_VTESTNZCPD,
25516   IX86_BUILTIN_VTESTZPS,
25517   IX86_BUILTIN_VTESTCPS,
25518   IX86_BUILTIN_VTESTNZCPS,
25519   IX86_BUILTIN_VTESTZPD256,
25520   IX86_BUILTIN_VTESTCPD256,
25521   IX86_BUILTIN_VTESTNZCPD256,
25522   IX86_BUILTIN_VTESTZPS256,
25523   IX86_BUILTIN_VTESTCPS256,
25524   IX86_BUILTIN_VTESTNZCPS256,
25525   IX86_BUILTIN_PTESTZ256,
25526   IX86_BUILTIN_PTESTC256,
25527   IX86_BUILTIN_PTESTNZC256,
25528
25529   IX86_BUILTIN_MOVMSKPD256,
25530   IX86_BUILTIN_MOVMSKPS256,
25531
25532   /* AVX2 */
25533   IX86_BUILTIN_MPSADBW256,
25534   IX86_BUILTIN_PABSB256,
25535   IX86_BUILTIN_PABSW256,
25536   IX86_BUILTIN_PABSD256,
25537   IX86_BUILTIN_PACKSSDW256,
25538   IX86_BUILTIN_PACKSSWB256,
25539   IX86_BUILTIN_PACKUSDW256,
25540   IX86_BUILTIN_PACKUSWB256,
25541   IX86_BUILTIN_PADDB256,
25542   IX86_BUILTIN_PADDW256,
25543   IX86_BUILTIN_PADDD256,
25544   IX86_BUILTIN_PADDQ256,
25545   IX86_BUILTIN_PADDSB256,
25546   IX86_BUILTIN_PADDSW256,
25547   IX86_BUILTIN_PADDUSB256,
25548   IX86_BUILTIN_PADDUSW256,
25549   IX86_BUILTIN_PALIGNR256,
25550   IX86_BUILTIN_AND256I,
25551   IX86_BUILTIN_ANDNOT256I,
25552   IX86_BUILTIN_PAVGB256,
25553   IX86_BUILTIN_PAVGW256,
25554   IX86_BUILTIN_PBLENDVB256,
25555   IX86_BUILTIN_PBLENDVW256,
25556   IX86_BUILTIN_PCMPEQB256,
25557   IX86_BUILTIN_PCMPEQW256,
25558   IX86_BUILTIN_PCMPEQD256,
25559   IX86_BUILTIN_PCMPEQQ256,
25560   IX86_BUILTIN_PCMPGTB256,
25561   IX86_BUILTIN_PCMPGTW256,
25562   IX86_BUILTIN_PCMPGTD256,
25563   IX86_BUILTIN_PCMPGTQ256,
25564   IX86_BUILTIN_PHADDW256,
25565   IX86_BUILTIN_PHADDD256,
25566   IX86_BUILTIN_PHADDSW256,
25567   IX86_BUILTIN_PHSUBW256,
25568   IX86_BUILTIN_PHSUBD256,
25569   IX86_BUILTIN_PHSUBSW256,
25570   IX86_BUILTIN_PMADDUBSW256,
25571   IX86_BUILTIN_PMADDWD256,
25572   IX86_BUILTIN_PMAXSB256,
25573   IX86_BUILTIN_PMAXSW256,
25574   IX86_BUILTIN_PMAXSD256,
25575   IX86_BUILTIN_PMAXUB256,
25576   IX86_BUILTIN_PMAXUW256,
25577   IX86_BUILTIN_PMAXUD256,
25578   IX86_BUILTIN_PMINSB256,
25579   IX86_BUILTIN_PMINSW256,
25580   IX86_BUILTIN_PMINSD256,
25581   IX86_BUILTIN_PMINUB256,
25582   IX86_BUILTIN_PMINUW256,
25583   IX86_BUILTIN_PMINUD256,
25584   IX86_BUILTIN_PMOVMSKB256,
25585   IX86_BUILTIN_PMOVSXBW256,
25586   IX86_BUILTIN_PMOVSXBD256,
25587   IX86_BUILTIN_PMOVSXBQ256,
25588   IX86_BUILTIN_PMOVSXWD256,
25589   IX86_BUILTIN_PMOVSXWQ256,
25590   IX86_BUILTIN_PMOVSXDQ256,
25591   IX86_BUILTIN_PMOVZXBW256,
25592   IX86_BUILTIN_PMOVZXBD256,
25593   IX86_BUILTIN_PMOVZXBQ256,
25594   IX86_BUILTIN_PMOVZXWD256,
25595   IX86_BUILTIN_PMOVZXWQ256,
25596   IX86_BUILTIN_PMOVZXDQ256,
25597   IX86_BUILTIN_PMULDQ256,
25598   IX86_BUILTIN_PMULHRSW256,
25599   IX86_BUILTIN_PMULHUW256,
25600   IX86_BUILTIN_PMULHW256,
25601   IX86_BUILTIN_PMULLW256,
25602   IX86_BUILTIN_PMULLD256,
25603   IX86_BUILTIN_PMULUDQ256,
25604   IX86_BUILTIN_POR256,
25605   IX86_BUILTIN_PSADBW256,
25606   IX86_BUILTIN_PSHUFB256,
25607   IX86_BUILTIN_PSHUFD256,
25608   IX86_BUILTIN_PSHUFHW256,
25609   IX86_BUILTIN_PSHUFLW256,
25610   IX86_BUILTIN_PSIGNB256,
25611   IX86_BUILTIN_PSIGNW256,
25612   IX86_BUILTIN_PSIGND256,
25613   IX86_BUILTIN_PSLLDQI256,
25614   IX86_BUILTIN_PSLLWI256,
25615   IX86_BUILTIN_PSLLW256,
25616   IX86_BUILTIN_PSLLDI256,
25617   IX86_BUILTIN_PSLLD256,
25618   IX86_BUILTIN_PSLLQI256,
25619   IX86_BUILTIN_PSLLQ256,
25620   IX86_BUILTIN_PSRAWI256,
25621   IX86_BUILTIN_PSRAW256,
25622   IX86_BUILTIN_PSRADI256,
25623   IX86_BUILTIN_PSRAD256,
25624   IX86_BUILTIN_PSRLDQI256,
25625   IX86_BUILTIN_PSRLWI256,
25626   IX86_BUILTIN_PSRLW256,
25627   IX86_BUILTIN_PSRLDI256,
25628   IX86_BUILTIN_PSRLD256,
25629   IX86_BUILTIN_PSRLQI256,
25630   IX86_BUILTIN_PSRLQ256,
25631   IX86_BUILTIN_PSUBB256,
25632   IX86_BUILTIN_PSUBW256,
25633   IX86_BUILTIN_PSUBD256,
25634   IX86_BUILTIN_PSUBQ256,
25635   IX86_BUILTIN_PSUBSB256,
25636   IX86_BUILTIN_PSUBSW256,
25637   IX86_BUILTIN_PSUBUSB256,
25638   IX86_BUILTIN_PSUBUSW256,
25639   IX86_BUILTIN_PUNPCKHBW256,
25640   IX86_BUILTIN_PUNPCKHWD256,
25641   IX86_BUILTIN_PUNPCKHDQ256,
25642   IX86_BUILTIN_PUNPCKHQDQ256,
25643   IX86_BUILTIN_PUNPCKLBW256,
25644   IX86_BUILTIN_PUNPCKLWD256,
25645   IX86_BUILTIN_PUNPCKLDQ256,
25646   IX86_BUILTIN_PUNPCKLQDQ256,
25647   IX86_BUILTIN_PXOR256,
25648   IX86_BUILTIN_MOVNTDQA256,
25649   IX86_BUILTIN_VBROADCASTSS_PS,
25650   IX86_BUILTIN_VBROADCASTSS_PS256,
25651   IX86_BUILTIN_VBROADCASTSD_PD256,
25652   IX86_BUILTIN_VBROADCASTSI256,
25653   IX86_BUILTIN_PBLENDD256,
25654   IX86_BUILTIN_PBLENDD128,
25655   IX86_BUILTIN_PBROADCASTB256,
25656   IX86_BUILTIN_PBROADCASTW256,
25657   IX86_BUILTIN_PBROADCASTD256,
25658   IX86_BUILTIN_PBROADCASTQ256,
25659   IX86_BUILTIN_PBROADCASTB128,
25660   IX86_BUILTIN_PBROADCASTW128,
25661   IX86_BUILTIN_PBROADCASTD128,
25662   IX86_BUILTIN_PBROADCASTQ128,
25663   IX86_BUILTIN_VPERMVARSI256,
25664   IX86_BUILTIN_VPERMDF256,
25665   IX86_BUILTIN_VPERMVARSF256,
25666   IX86_BUILTIN_VPERMDI256,
25667   IX86_BUILTIN_VPERMTI256,
25668   IX86_BUILTIN_VEXTRACT128I256,
25669   IX86_BUILTIN_VINSERT128I256,
25670   IX86_BUILTIN_MASKLOADD,
25671   IX86_BUILTIN_MASKLOADQ,
25672   IX86_BUILTIN_MASKLOADD256,
25673   IX86_BUILTIN_MASKLOADQ256,
25674   IX86_BUILTIN_MASKSTORED,
25675   IX86_BUILTIN_MASKSTOREQ,
25676   IX86_BUILTIN_MASKSTORED256,
25677   IX86_BUILTIN_MASKSTOREQ256,
25678   IX86_BUILTIN_PSLLVV4DI,
25679   IX86_BUILTIN_PSLLVV2DI,
25680   IX86_BUILTIN_PSLLVV8SI,
25681   IX86_BUILTIN_PSLLVV4SI,
25682   IX86_BUILTIN_PSRAVV8SI,
25683   IX86_BUILTIN_PSRAVV4SI,
25684   IX86_BUILTIN_PSRLVV4DI,
25685   IX86_BUILTIN_PSRLVV2DI,
25686   IX86_BUILTIN_PSRLVV8SI,
25687   IX86_BUILTIN_PSRLVV4SI,
25688
25689   IX86_BUILTIN_GATHERSIV2DF,
25690   IX86_BUILTIN_GATHERSIV4DF,
25691   IX86_BUILTIN_GATHERDIV2DF,
25692   IX86_BUILTIN_GATHERDIV4DF,
25693   IX86_BUILTIN_GATHERSIV4SF,
25694   IX86_BUILTIN_GATHERSIV8SF,
25695   IX86_BUILTIN_GATHERDIV4SF,
25696   IX86_BUILTIN_GATHERDIV8SF,
25697   IX86_BUILTIN_GATHERSIV2DI,
25698   IX86_BUILTIN_GATHERSIV4DI,
25699   IX86_BUILTIN_GATHERDIV2DI,
25700   IX86_BUILTIN_GATHERDIV4DI,
25701   IX86_BUILTIN_GATHERSIV4SI,
25702   IX86_BUILTIN_GATHERSIV8SI,
25703   IX86_BUILTIN_GATHERDIV4SI,
25704   IX86_BUILTIN_GATHERDIV8SI,
25705
25706   /* Alternate 4 element gather for the vectorizer where
25707      all operands are 32-byte wide.  */
25708   IX86_BUILTIN_GATHERALTSIV4DF,
25709   IX86_BUILTIN_GATHERALTDIV8SF,
25710   IX86_BUILTIN_GATHERALTSIV4DI,
25711   IX86_BUILTIN_GATHERALTDIV8SI,
25712
25713   /* TFmode support builtins.  */
25714   IX86_BUILTIN_INFQ,
25715   IX86_BUILTIN_HUGE_VALQ,
25716   IX86_BUILTIN_FABSQ,
25717   IX86_BUILTIN_COPYSIGNQ,
25718
25719   /* Vectorizer support builtins.  */
25720   IX86_BUILTIN_CPYSGNPS,
25721   IX86_BUILTIN_CPYSGNPD,
25722   IX86_BUILTIN_CPYSGNPS256,
25723   IX86_BUILTIN_CPYSGNPD256,
25724
25725   /* FMA4 instructions.  */
25726   IX86_BUILTIN_VFMADDSS,
25727   IX86_BUILTIN_VFMADDSD,
25728   IX86_BUILTIN_VFMADDPS,
25729   IX86_BUILTIN_VFMADDPD,
25730   IX86_BUILTIN_VFMADDPS256,
25731   IX86_BUILTIN_VFMADDPD256,
25732   IX86_BUILTIN_VFMADDSUBPS,
25733   IX86_BUILTIN_VFMADDSUBPD,
25734   IX86_BUILTIN_VFMADDSUBPS256,
25735   IX86_BUILTIN_VFMADDSUBPD256,
25736
25737   /* FMA3 instructions.  */
25738   IX86_BUILTIN_VFMADDSS3,
25739   IX86_BUILTIN_VFMADDSD3,
25740
25741   /* XOP instructions.  */
25742   IX86_BUILTIN_VPCMOV,
25743   IX86_BUILTIN_VPCMOV_V2DI,
25744   IX86_BUILTIN_VPCMOV_V4SI,
25745   IX86_BUILTIN_VPCMOV_V8HI,
25746   IX86_BUILTIN_VPCMOV_V16QI,
25747   IX86_BUILTIN_VPCMOV_V4SF,
25748   IX86_BUILTIN_VPCMOV_V2DF,
25749   IX86_BUILTIN_VPCMOV256,
25750   IX86_BUILTIN_VPCMOV_V4DI256,
25751   IX86_BUILTIN_VPCMOV_V8SI256,
25752   IX86_BUILTIN_VPCMOV_V16HI256,
25753   IX86_BUILTIN_VPCMOV_V32QI256,
25754   IX86_BUILTIN_VPCMOV_V8SF256,
25755   IX86_BUILTIN_VPCMOV_V4DF256,
25756
25757   IX86_BUILTIN_VPPERM,
25758
25759   IX86_BUILTIN_VPMACSSWW,
25760   IX86_BUILTIN_VPMACSWW,
25761   IX86_BUILTIN_VPMACSSWD,
25762   IX86_BUILTIN_VPMACSWD,
25763   IX86_BUILTIN_VPMACSSDD,
25764   IX86_BUILTIN_VPMACSDD,
25765   IX86_BUILTIN_VPMACSSDQL,
25766   IX86_BUILTIN_VPMACSSDQH,
25767   IX86_BUILTIN_VPMACSDQL,
25768   IX86_BUILTIN_VPMACSDQH,
25769   IX86_BUILTIN_VPMADCSSWD,
25770   IX86_BUILTIN_VPMADCSWD,
25771
25772   IX86_BUILTIN_VPHADDBW,
25773   IX86_BUILTIN_VPHADDBD,
25774   IX86_BUILTIN_VPHADDBQ,
25775   IX86_BUILTIN_VPHADDWD,
25776   IX86_BUILTIN_VPHADDWQ,
25777   IX86_BUILTIN_VPHADDDQ,
25778   IX86_BUILTIN_VPHADDUBW,
25779   IX86_BUILTIN_VPHADDUBD,
25780   IX86_BUILTIN_VPHADDUBQ,
25781   IX86_BUILTIN_VPHADDUWD,
25782   IX86_BUILTIN_VPHADDUWQ,
25783   IX86_BUILTIN_VPHADDUDQ,
25784   IX86_BUILTIN_VPHSUBBW,
25785   IX86_BUILTIN_VPHSUBWD,
25786   IX86_BUILTIN_VPHSUBDQ,
25787
25788   IX86_BUILTIN_VPROTB,
25789   IX86_BUILTIN_VPROTW,
25790   IX86_BUILTIN_VPROTD,
25791   IX86_BUILTIN_VPROTQ,
25792   IX86_BUILTIN_VPROTB_IMM,
25793   IX86_BUILTIN_VPROTW_IMM,
25794   IX86_BUILTIN_VPROTD_IMM,
25795   IX86_BUILTIN_VPROTQ_IMM,
25796
25797   IX86_BUILTIN_VPSHLB,
25798   IX86_BUILTIN_VPSHLW,
25799   IX86_BUILTIN_VPSHLD,
25800   IX86_BUILTIN_VPSHLQ,
25801   IX86_BUILTIN_VPSHAB,
25802   IX86_BUILTIN_VPSHAW,
25803   IX86_BUILTIN_VPSHAD,
25804   IX86_BUILTIN_VPSHAQ,
25805
25806   IX86_BUILTIN_VFRCZSS,
25807   IX86_BUILTIN_VFRCZSD,
25808   IX86_BUILTIN_VFRCZPS,
25809   IX86_BUILTIN_VFRCZPD,
25810   IX86_BUILTIN_VFRCZPS256,
25811   IX86_BUILTIN_VFRCZPD256,
25812
25813   IX86_BUILTIN_VPCOMEQUB,
25814   IX86_BUILTIN_VPCOMNEUB,
25815   IX86_BUILTIN_VPCOMLTUB,
25816   IX86_BUILTIN_VPCOMLEUB,
25817   IX86_BUILTIN_VPCOMGTUB,
25818   IX86_BUILTIN_VPCOMGEUB,
25819   IX86_BUILTIN_VPCOMFALSEUB,
25820   IX86_BUILTIN_VPCOMTRUEUB,
25821
25822   IX86_BUILTIN_VPCOMEQUW,
25823   IX86_BUILTIN_VPCOMNEUW,
25824   IX86_BUILTIN_VPCOMLTUW,
25825   IX86_BUILTIN_VPCOMLEUW,
25826   IX86_BUILTIN_VPCOMGTUW,
25827   IX86_BUILTIN_VPCOMGEUW,
25828   IX86_BUILTIN_VPCOMFALSEUW,
25829   IX86_BUILTIN_VPCOMTRUEUW,
25830
25831   IX86_BUILTIN_VPCOMEQUD,
25832   IX86_BUILTIN_VPCOMNEUD,
25833   IX86_BUILTIN_VPCOMLTUD,
25834   IX86_BUILTIN_VPCOMLEUD,
25835   IX86_BUILTIN_VPCOMGTUD,
25836   IX86_BUILTIN_VPCOMGEUD,
25837   IX86_BUILTIN_VPCOMFALSEUD,
25838   IX86_BUILTIN_VPCOMTRUEUD,
25839
25840   IX86_BUILTIN_VPCOMEQUQ,
25841   IX86_BUILTIN_VPCOMNEUQ,
25842   IX86_BUILTIN_VPCOMLTUQ,
25843   IX86_BUILTIN_VPCOMLEUQ,
25844   IX86_BUILTIN_VPCOMGTUQ,
25845   IX86_BUILTIN_VPCOMGEUQ,
25846   IX86_BUILTIN_VPCOMFALSEUQ,
25847   IX86_BUILTIN_VPCOMTRUEUQ,
25848
25849   IX86_BUILTIN_VPCOMEQB,
25850   IX86_BUILTIN_VPCOMNEB,
25851   IX86_BUILTIN_VPCOMLTB,
25852   IX86_BUILTIN_VPCOMLEB,
25853   IX86_BUILTIN_VPCOMGTB,
25854   IX86_BUILTIN_VPCOMGEB,
25855   IX86_BUILTIN_VPCOMFALSEB,
25856   IX86_BUILTIN_VPCOMTRUEB,
25857
25858   IX86_BUILTIN_VPCOMEQW,
25859   IX86_BUILTIN_VPCOMNEW,
25860   IX86_BUILTIN_VPCOMLTW,
25861   IX86_BUILTIN_VPCOMLEW,
25862   IX86_BUILTIN_VPCOMGTW,
25863   IX86_BUILTIN_VPCOMGEW,
25864   IX86_BUILTIN_VPCOMFALSEW,
25865   IX86_BUILTIN_VPCOMTRUEW,
25866
25867   IX86_BUILTIN_VPCOMEQD,
25868   IX86_BUILTIN_VPCOMNED,
25869   IX86_BUILTIN_VPCOMLTD,
25870   IX86_BUILTIN_VPCOMLED,
25871   IX86_BUILTIN_VPCOMGTD,
25872   IX86_BUILTIN_VPCOMGED,
25873   IX86_BUILTIN_VPCOMFALSED,
25874   IX86_BUILTIN_VPCOMTRUED,
25875
25876   IX86_BUILTIN_VPCOMEQQ,
25877   IX86_BUILTIN_VPCOMNEQ,
25878   IX86_BUILTIN_VPCOMLTQ,
25879   IX86_BUILTIN_VPCOMLEQ,
25880   IX86_BUILTIN_VPCOMGTQ,
25881   IX86_BUILTIN_VPCOMGEQ,
25882   IX86_BUILTIN_VPCOMFALSEQ,
25883   IX86_BUILTIN_VPCOMTRUEQ,
25884
25885   /* LWP instructions.  */
25886   IX86_BUILTIN_LLWPCB,
25887   IX86_BUILTIN_SLWPCB,
25888   IX86_BUILTIN_LWPVAL32,
25889   IX86_BUILTIN_LWPVAL64,
25890   IX86_BUILTIN_LWPINS32,
25891   IX86_BUILTIN_LWPINS64,
25892
25893   IX86_BUILTIN_CLZS,
25894
25895   /* BMI instructions.  */
25896   IX86_BUILTIN_BEXTR32,
25897   IX86_BUILTIN_BEXTR64,
25898   IX86_BUILTIN_CTZS,
25899
25900   /* TBM instructions.  */
25901   IX86_BUILTIN_BEXTRI32,
25902   IX86_BUILTIN_BEXTRI64,
25903
25904   /* BMI2 instructions. */
25905   IX86_BUILTIN_BZHI32,
25906   IX86_BUILTIN_BZHI64,
25907   IX86_BUILTIN_PDEP32,
25908   IX86_BUILTIN_PDEP64,
25909   IX86_BUILTIN_PEXT32,
25910   IX86_BUILTIN_PEXT64,
25911
25912   /* FSGSBASE instructions.  */
25913   IX86_BUILTIN_RDFSBASE32,
25914   IX86_BUILTIN_RDFSBASE64,
25915   IX86_BUILTIN_RDGSBASE32,
25916   IX86_BUILTIN_RDGSBASE64,
25917   IX86_BUILTIN_WRFSBASE32,
25918   IX86_BUILTIN_WRFSBASE64,
25919   IX86_BUILTIN_WRGSBASE32,
25920   IX86_BUILTIN_WRGSBASE64,
25921
25922   /* RDRND instructions.  */
25923   IX86_BUILTIN_RDRAND16_STEP,
25924   IX86_BUILTIN_RDRAND32_STEP,
25925   IX86_BUILTIN_RDRAND64_STEP,
25926
25927   /* F16C instructions.  */
25928   IX86_BUILTIN_CVTPH2PS,
25929   IX86_BUILTIN_CVTPH2PS256,
25930   IX86_BUILTIN_CVTPS2PH,
25931   IX86_BUILTIN_CVTPS2PH256,
25932
25933   /* CFString built-in for darwin */
25934   IX86_BUILTIN_CFSTRING,
25935
25936   IX86_BUILTIN_MAX
25937 };
25938
25939 /* Table for the ix86 builtin decls.  */
25940 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
25941
25942 /* Table of all of the builtin functions that are possible with different ISA's
25943    but are waiting to be built until a function is declared to use that
25944    ISA.  */
25945 struct builtin_isa {
25946   const char *name;             /* function name */
25947   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
25948   HOST_WIDE_INT isa;            /* isa_flags this builtin is defined for */
25949   bool const_p;                 /* true if the declaration is constant */
25950   bool set_and_not_built_p;
25951 };
25952
25953 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
25954
25955
25956 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
25957    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
25958    function decl in the ix86_builtins array.  Returns the function decl or
25959    NULL_TREE, if the builtin was not added.
25960
25961    If the front end has a special hook for builtin functions, delay adding
25962    builtin functions that aren't in the current ISA until the ISA is changed
25963    with function specific optimization.  Doing so, can save about 300K for the
25964    default compiler.  When the builtin is expanded, check at that time whether
25965    it is valid.
25966
25967    If the front end doesn't have a special hook, record all builtins, even if
25968    it isn't an instruction set in the current ISA in case the user uses
25969    function specific options for a different ISA, so that we don't get scope
25970    errors if a builtin is added in the middle of a function scope.  */
25971
25972 static inline tree
25973 def_builtin (HOST_WIDE_INT mask, const char *name,
25974              enum ix86_builtin_func_type tcode,
25975              enum ix86_builtins code)
25976 {
25977   tree decl = NULL_TREE;
25978
25979   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
25980     {
25981       ix86_builtins_isa[(int) code].isa = mask;
25982
25983       mask &= ~OPTION_MASK_ISA_64BIT;
25984       if (mask == 0
25985           || (mask & ix86_isa_flags) != 0
25986           || (lang_hooks.builtin_function
25987               == lang_hooks.builtin_function_ext_scope))
25988
25989         {
25990           tree type = ix86_get_builtin_func_type (tcode);
25991           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
25992                                        NULL, NULL_TREE);
25993           ix86_builtins[(int) code] = decl;
25994           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
25995         }
25996       else
25997         {
25998           ix86_builtins[(int) code] = NULL_TREE;
25999           ix86_builtins_isa[(int) code].tcode = tcode;
26000           ix86_builtins_isa[(int) code].name = name;
26001           ix86_builtins_isa[(int) code].const_p = false;
26002           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
26003         }
26004     }
26005
26006   return decl;
26007 }
26008
26009 /* Like def_builtin, but also marks the function decl "const".  */
26010
26011 static inline tree
26012 def_builtin_const (HOST_WIDE_INT mask, const char *name,
26013                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
26014 {
26015   tree decl = def_builtin (mask, name, tcode, code);
26016   if (decl)
26017     TREE_READONLY (decl) = 1;
26018   else
26019     ix86_builtins_isa[(int) code].const_p = true;
26020
26021   return decl;
26022 }
26023
26024 /* Add any new builtin functions for a given ISA that may not have been
26025    declared.  This saves a bit of space compared to adding all of the
26026    declarations to the tree, even if we didn't use them.  */
26027
26028 static void
26029 ix86_add_new_builtins (HOST_WIDE_INT isa)
26030 {
26031   int i;
26032
26033   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
26034     {
26035       if ((ix86_builtins_isa[i].isa & isa) != 0
26036           && ix86_builtins_isa[i].set_and_not_built_p)
26037         {
26038           tree decl, type;
26039
26040           /* Don't define the builtin again.  */
26041           ix86_builtins_isa[i].set_and_not_built_p = false;
26042
26043           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
26044           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
26045                                                  type, i, BUILT_IN_MD, NULL,
26046                                                  NULL_TREE);
26047
26048           ix86_builtins[i] = decl;
26049           if (ix86_builtins_isa[i].const_p)
26050             TREE_READONLY (decl) = 1;
26051         }
26052     }
26053 }
26054
26055 /* Bits for builtin_description.flag.  */
26056
26057 /* Set when we don't support the comparison natively, and should
26058    swap_comparison in order to support it.  */
26059 #define BUILTIN_DESC_SWAP_OPERANDS      1
26060
26061 struct builtin_description
26062 {
26063   const HOST_WIDE_INT mask;
26064   const enum insn_code icode;
26065   const char *const name;
26066   const enum ix86_builtins code;
26067   const enum rtx_code comparison;
26068   const int flag;
26069 };
26070
26071 static const struct builtin_description bdesc_comi[] =
26072 {
26073   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
26074   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
26075   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
26076   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
26077   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
26078   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
26079   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
26080   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
26081   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
26082   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
26083   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
26084   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
26085   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
26086   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
26087   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
26088   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
26089   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
26090   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
26091   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
26092   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
26093   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
26094   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
26095   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
26096   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
26097 };
26098
26099 static const struct builtin_description bdesc_pcmpestr[] =
26100 {
26101   /* SSE4.2 */
26102   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
26103   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
26104   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
26105   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
26106   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
26107   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
26108   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
26109 };
26110
26111 static const struct builtin_description bdesc_pcmpistr[] =
26112 {
26113   /* SSE4.2 */
26114   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
26115   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
26116   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
26117   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
26118   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
26119   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
26120   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
26121 };
26122
26123 /* Special builtins with variable number of arguments.  */
26124 static const struct builtin_description bdesc_special_args[] =
26125 {
26126   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
26127   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
26128   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_pause, "__builtin_ia32_pause", IX86_BUILTIN_PAUSE, UNKNOWN, (int) VOID_FTYPE_VOID },
26129
26130   /* MMX */
26131   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
26132
26133   /* 3DNow! */
26134   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
26135
26136   /* SSE */
26137   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storeups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
26138   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
26139   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
26140
26141   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
26142   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
26143   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
26144   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
26145
26146   /* SSE or 3DNow!A  */
26147   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
26148   { 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 },
26149
26150   /* SSE2 */
26151   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
26152   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
26153   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_storeupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
26154   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_storedqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
26155   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
26156   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
26157   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntisi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
26158   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_movntidi, "__builtin_ia32_movnti64", IX86_BUILTIN_MOVNTI64, UNKNOWN, (int) VOID_FTYPE_PLONGLONG_LONGLONG },
26159   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
26160   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loaddqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
26161
26162   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
26163   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
26164
26165   /* SSE3 */
26166   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
26167
26168   /* SSE4.1 */
26169   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
26170
26171   /* SSE4A */
26172   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
26173   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
26174
26175   /* AVX */
26176   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
26177   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
26178
26179   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
26180   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
26181   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
26182   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
26183   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
26184
26185   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_loadupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
26186   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_loadups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
26187   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_storeupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
26188   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_storeups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
26189   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_loaddqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
26190   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_storedqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
26191   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
26192
26193   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
26194   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
26195   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
26196
26197   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DI },
26198   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SI },
26199   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DI },
26200   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SI },
26201   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DI_V2DF },
26202   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SI_V4SF },
26203   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DI_V4DF },
26204   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SI_V8SF },
26205
26206   /* AVX2 */
26207   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_movntdqa, "__builtin_ia32_movntdqa256", IX86_BUILTIN_MOVNTDQA256, UNKNOWN, (int) V4DI_FTYPE_PV4DI },
26208   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadd, "__builtin_ia32_maskloadd", IX86_BUILTIN_MASKLOADD, UNKNOWN, (int) V4SI_FTYPE_PCV4SI_V4SI },
26209   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadq, "__builtin_ia32_maskloadq", IX86_BUILTIN_MASKLOADQ, UNKNOWN, (int) V2DI_FTYPE_PCV2DI_V2DI },
26210   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadd256, "__builtin_ia32_maskloadd256", IX86_BUILTIN_MASKLOADD256, UNKNOWN, (int) V8SI_FTYPE_PCV8SI_V8SI },
26211   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskloadq256, "__builtin_ia32_maskloadq256", IX86_BUILTIN_MASKLOADQ256, UNKNOWN, (int) V4DI_FTYPE_PCV4DI_V4DI },
26212   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstored, "__builtin_ia32_maskstored", IX86_BUILTIN_MASKSTORED, UNKNOWN, (int) VOID_FTYPE_PV4SI_V4SI_V4SI },
26213   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstoreq, "__builtin_ia32_maskstoreq", IX86_BUILTIN_MASKSTOREQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI_V2DI },
26214   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstored256, "__builtin_ia32_maskstored256", IX86_BUILTIN_MASKSTORED256, UNKNOWN, (int) VOID_FTYPE_PV8SI_V8SI_V8SI },
26215   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_maskstoreq256, "__builtin_ia32_maskstoreq256", IX86_BUILTIN_MASKSTOREQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI_V4DI },
26216
26217   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
26218   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
26219   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
26220   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
26221   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
26222   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
26223
26224   /* FSGSBASE */
26225   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasesi, "__builtin_ia32_rdfsbase32", IX86_BUILTIN_RDFSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
26226   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasedi, "__builtin_ia32_rdfsbase64", IX86_BUILTIN_RDFSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
26227   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasesi, "__builtin_ia32_rdgsbase32", IX86_BUILTIN_RDGSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
26228   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasedi, "__builtin_ia32_rdgsbase64", IX86_BUILTIN_RDGSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
26229   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasesi, "__builtin_ia32_wrfsbase32", IX86_BUILTIN_WRFSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
26230   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasedi, "__builtin_ia32_wrfsbase64", IX86_BUILTIN_WRFSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
26231   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasesi, "__builtin_ia32_wrgsbase32", IX86_BUILTIN_WRGSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
26232   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasedi, "__builtin_ia32_wrgsbase64", IX86_BUILTIN_WRGSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
26233 };
26234
26235 /* Builtins with variable number of arguments.  */
26236 static const struct builtin_description bdesc_args[] =
26237 {
26238   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
26239   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
26240   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
26241   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
26242   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
26243   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
26244   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
26245
26246   /* MMX */
26247   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26248   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26249   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26250   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26251   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26252   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26253
26254   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26255   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26256   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26257   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26258   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26259   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26260   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26261   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26262
26263   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26264   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26265
26266   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26267   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26268   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26269   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26270
26271   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26272   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26273   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26274   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26275   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26276   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26277
26278   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26279   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26280   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26281   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26282   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
26283   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
26284
26285   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
26286   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
26287   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
26288
26289   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
26290
26291   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
26292   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
26293   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
26294   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
26295   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
26296   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
26297
26298   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
26299   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
26300   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
26301   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
26302   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
26303   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
26304
26305   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
26306   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
26307   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
26308   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
26309
26310   /* 3DNow! */
26311   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
26312   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
26313   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
26314   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
26315
26316   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26317   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26318   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26319   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
26320   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
26321   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
26322   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26323   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26324   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26325   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26326   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26327   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26328   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26329   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26330   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26331
26332   /* 3DNow!A */
26333   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
26334   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
26335   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
26336   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
26337   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26338   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
26339
26340   /* SSE */
26341   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
26342   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26343   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26344   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26345   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26346   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26347   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
26348   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
26349   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
26350   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
26351   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
26352   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
26353
26354   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26355
26356   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26357   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26358   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26359   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26360   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26361   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26362   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26363   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26364
26365   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
26366   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
26367   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
26368   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26369   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26370   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26371   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
26372   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
26373   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
26374   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26375   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
26376   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26377   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
26378   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
26379   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
26380   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26381   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
26382   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
26383   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
26384   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26385   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
26386   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
26387
26388   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26389   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26390   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26391   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26392
26393   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26394   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26395   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26396   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26397
26398   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26399
26400   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26401   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26402   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26403   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26404   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26405
26406   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
26407   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
26408   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
26409
26410   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
26411
26412   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
26413   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
26414   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
26415
26416   /* SSE MMX or 3Dnow!A */
26417   { 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 },
26418   { 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 },
26419   { 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 },
26420
26421   { 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 },
26422   { 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 },
26423   { 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 },
26424   { 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 },
26425
26426   { 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 },
26427   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
26428
26429   { 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 },
26430
26431   /* SSE2 */
26432   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26433
26434   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
26435   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
26436   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
26437   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
26438   { OPTION_MASK_ISA_SSE2, CODE_FOR_floatv4siv4sf2, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
26439
26440   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
26441   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
26442   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
26443   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
26444   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
26445
26446   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
26447
26448   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
26449   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
26450   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
26451   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
26452
26453   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
26454   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
26455   { OPTION_MASK_ISA_SSE2, CODE_FOR_fix_truncv4sfv4si2, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
26456
26457   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26458   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26459   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26460   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26461   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26462   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26463   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26464   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26465
26466   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
26467   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
26468   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
26469   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
26470   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
26471   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26472   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
26473   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
26474   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
26475   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
26476   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
26477   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26478   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
26479   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
26480   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
26481   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26482   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
26483   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
26484   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
26485   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
26486
26487   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26488   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26489   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26490   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26491
26492   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26493   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26494   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26495   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26496
26497   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26498
26499   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26500   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26501   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26502
26503   { 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 },
26504
26505   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26506   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26507   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26508   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26509   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26510   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26511   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26512   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26513
26514   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26515   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26516   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26517   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26518   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26519   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26520   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26521   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26522
26523   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26524   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
26525
26526   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26527   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26528   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26529   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26530
26531   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26532   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26533
26534   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26535   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26536   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
26537   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26538   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26539   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
26540
26541   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26542   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26543   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26544   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26545
26546   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26547   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
26548   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
26549   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26550   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26551   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26552   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26553   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26554
26555   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
26556   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
26557   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
26558
26559   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26560   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
26561
26562   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
26563   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
26564
26565   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
26566
26567   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
26568   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
26569   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
26570   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
26571
26572   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
26573   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
26574   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
26575   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
26576   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
26577   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
26578   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
26579
26580   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
26581   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
26582   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
26583   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
26584   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
26585   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
26586   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
26587
26588   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
26589   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
26590   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
26591   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
26592
26593   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
26594   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
26595   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
26596
26597   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
26598
26599   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
26600   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
26601
26602   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
26603
26604   /* SSE2 MMX */
26605   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
26606   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
26607
26608   /* SSE3 */
26609   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
26610   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26611
26612   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26613   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26614   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26615   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26616   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
26617   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
26618
26619   /* SSSE3 */
26620   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
26621   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
26622   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
26623   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
26624   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
26625   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
26626
26627   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26628   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26629   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26630   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26631   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26632   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26633   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26634   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26635   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26636   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26637   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26638   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26639   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
26640   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
26641   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26642   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26643   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26644   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26645   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26646   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
26647   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26648   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
26649   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26650   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
26651
26652   /* SSSE3.  */
26653   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
26654   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
26655
26656   /* SSE4.1 */
26657   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26658   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26659   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
26660   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
26661   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26662   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26663   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26664   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
26665   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
26666   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
26667
26668   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
26669   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
26670   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
26671   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
26672   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
26673   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
26674   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
26675   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
26676   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
26677   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
26678   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
26679   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
26680   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
26681
26682   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
26683   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26684   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26685   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26686   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26687   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26688   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
26689   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26690   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26691   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
26692   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
26693   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
26694
26695   /* SSE4.1 */
26696   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
26697   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
26698   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26699   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26700
26701   { 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 },
26702   { 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 },
26703   { 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 },
26704   { 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 },
26705
26706   { 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 },
26707   { 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 },
26708
26709   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv2df2, "__builtin_ia32_roundpd_az", IX86_BUILTIN_ROUNDPD_AZ, UNKNOWN, (int) V2DF_FTYPE_V2DF },
26710   { 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 },
26711
26712   { 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 },
26713   { 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 },
26714   { 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 },
26715   { 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 },
26716
26717   { 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 },
26718   { 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 },
26719
26720   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv4sf2, "__builtin_ia32_roundps_az", IX86_BUILTIN_ROUNDPS_AZ, UNKNOWN, (int) V4SF_FTYPE_V4SF },
26721   { OPTION_MASK_ISA_ROUND, CODE_FOR_roundv4sf2_sfix, "__builtin_ia32_roundps_az_sfix", IX86_BUILTIN_ROUNDPS_AZ_SFIX, UNKNOWN, (int) V4SI_FTYPE_V4SF },
26722
26723   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
26724   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
26725   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
26726
26727   /* SSE4.2 */
26728   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26729   { 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 },
26730   { 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 },
26731   { 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 },
26732   { 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 },
26733
26734   /* SSE4A */
26735   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
26736   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
26737   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
26738   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26739
26740   /* AES */
26741   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
26742   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
26743
26744   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26745   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26746   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26747   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
26748
26749   /* PCLMUL */
26750   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
26751
26752   /* AVX */
26753   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26754   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26755   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26756   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26757   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26758   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26759   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26760   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26761   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26762   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26763   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26764   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26765   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26766   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26767   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26768   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26769   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26770   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26771   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26772   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26773   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26774   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26775   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26776   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26777   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26778   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26779
26780   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
26781   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
26782   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
26783   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
26784
26785   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26786   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26787   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
26788   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
26789   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26790   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26791   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26792   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vmcmpv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26793   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vmcmpv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26794   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
26795   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
26796   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26797   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26798   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
26799   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
26800   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
26801   { OPTION_MASK_ISA_AVX, CODE_FOR_floatv4siv4df2, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
26802   { OPTION_MASK_ISA_AVX, CODE_FOR_floatv8siv8sf2, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
26803   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
26804   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
26805   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
26806   { OPTION_MASK_ISA_AVX, CODE_FOR_fix_truncv4dfv4si2, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
26807   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
26808   { OPTION_MASK_ISA_AVX, CODE_FOR_fix_truncv8sfv8si2, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
26809   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
26810   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
26811   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
26812   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
26813   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
26814   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
26815   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
26816   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
26817   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
26818   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
26819
26820   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26821   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26822   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
26823
26824   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
26825   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26826   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26827   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26828   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26829
26830   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26831
26832   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
26833   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
26834
26835   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_floorpd256", IX86_BUILTIN_FLOORPD256, (enum rtx_code) ROUND_FLOOR, (int) V4DF_FTYPE_V4DF_ROUND },
26836   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_ceilpd256", IX86_BUILTIN_CEILPD256, (enum rtx_code) ROUND_CEIL, (int) V4DF_FTYPE_V4DF_ROUND },
26837   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_truncpd256", IX86_BUILTIN_TRUNCPD256, (enum rtx_code) ROUND_TRUNC, (int) V4DF_FTYPE_V4DF_ROUND },
26838   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_rintpd256", IX86_BUILTIN_RINTPD256, (enum rtx_code) ROUND_MXCSR, (int) V4DF_FTYPE_V4DF_ROUND },
26839
26840   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv4df2, "__builtin_ia32_roundpd_az256", IX86_BUILTIN_ROUNDPD_AZ256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
26841   { 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 },
26842
26843   { 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 },
26844   { 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 },
26845
26846   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_floorps256", IX86_BUILTIN_FLOORPS256, (enum rtx_code) ROUND_FLOOR, (int) V8SF_FTYPE_V8SF_ROUND },
26847   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_ceilps256", IX86_BUILTIN_CEILPS256, (enum rtx_code) ROUND_CEIL, (int) V8SF_FTYPE_V8SF_ROUND },
26848   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_truncps256", IX86_BUILTIN_TRUNCPS256, (enum rtx_code) ROUND_TRUNC, (int) V8SF_FTYPE_V8SF_ROUND },
26849   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_rintps256", IX86_BUILTIN_RINTPS256, (enum rtx_code) ROUND_MXCSR, (int) V8SF_FTYPE_V8SF_ROUND },
26850
26851   { 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 },
26852   { 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 },
26853
26854   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv8sf2, "__builtin_ia32_roundps_az256", IX86_BUILTIN_ROUNDPS_AZ256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
26855   { OPTION_MASK_ISA_AVX, CODE_FOR_roundv8sf2_sfix, "__builtin_ia32_roundps_az_sfix256", IX86_BUILTIN_ROUNDPS_AZ_SFIX256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
26856
26857   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26858   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26859   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26860   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26861
26862   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
26863   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
26864   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
26865   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8si, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
26866   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8sf, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
26867   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v4df, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
26868
26869   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
26870   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
26871   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
26872   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
26873   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
26874   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
26875   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
26876   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
26877   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
26878   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
26879   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
26880   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
26881   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
26882   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
26883   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
26884
26885   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
26886   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
26887
26888   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv8sf3,  "__builtin_ia32_copysignps256", IX86_BUILTIN_CPYSGNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
26889   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv4df3,  "__builtin_ia32_copysignpd256", IX86_BUILTIN_CPYSGNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
26890
26891   { 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 },
26892
26893   /* AVX2 */
26894   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_mpsadbw, "__builtin_ia32_mpsadbw256", IX86_BUILTIN_MPSADBW256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI_INT },
26895   { OPTION_MASK_ISA_AVX2, CODE_FOR_absv32qi2, "__builtin_ia32_pabsb256", IX86_BUILTIN_PABSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI },
26896   { OPTION_MASK_ISA_AVX2, CODE_FOR_absv16hi2, "__builtin_ia32_pabsw256", IX86_BUILTIN_PABSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI },
26897   { OPTION_MASK_ISA_AVX2, CODE_FOR_absv8si2, "__builtin_ia32_pabsd256", IX86_BUILTIN_PABSD256, UNKNOWN, (int) V8SI_FTYPE_V8SI },
26898   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packssdw, "__builtin_ia32_packssdw256",  IX86_BUILTIN_PACKSSDW256, UNKNOWN, (int) V16HI_FTYPE_V8SI_V8SI },
26899   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packsswb, "__builtin_ia32_packsswb256",  IX86_BUILTIN_PACKSSWB256, UNKNOWN, (int) V32QI_FTYPE_V16HI_V16HI },
26900   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packusdw, "__builtin_ia32_packusdw256",  IX86_BUILTIN_PACKUSDW256, UNKNOWN, (int) V16HI_FTYPE_V8SI_V8SI },
26901   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_packuswb, "__builtin_ia32_packuswb256",  IX86_BUILTIN_PACKUSWB256, UNKNOWN, (int) V32QI_FTYPE_V16HI_V16HI },
26902   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv32qi3, "__builtin_ia32_paddb256", IX86_BUILTIN_PADDB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26903   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv16hi3, "__builtin_ia32_paddw256", IX86_BUILTIN_PADDW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26904   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv8si3, "__builtin_ia32_paddd256", IX86_BUILTIN_PADDD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26905   { OPTION_MASK_ISA_AVX2, CODE_FOR_addv4di3, "__builtin_ia32_paddq256", IX86_BUILTIN_PADDQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26906   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ssaddv32qi3, "__builtin_ia32_paddsb256", IX86_BUILTIN_PADDSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26907   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ssaddv16hi3, "__builtin_ia32_paddsw256", IX86_BUILTIN_PADDSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26908   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_usaddv32qi3, "__builtin_ia32_paddusb256", IX86_BUILTIN_PADDUSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26909   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_usaddv16hi3, "__builtin_ia32_paddusw256", IX86_BUILTIN_PADDUSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26910   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_palignrv2ti, "__builtin_ia32_palignr256", IX86_BUILTIN_PALIGNR256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI_INT_CONVERT },
26911   { OPTION_MASK_ISA_AVX2, CODE_FOR_andv4di3, "__builtin_ia32_andsi256", IX86_BUILTIN_AND256I, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26912   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_andnotv4di3, "__builtin_ia32_andnotsi256", IX86_BUILTIN_ANDNOT256I, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26913   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_uavgv32qi3, "__builtin_ia32_pavgb256",  IX86_BUILTIN_PAVGB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26914   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_uavgv16hi3, "__builtin_ia32_pavgw256",  IX86_BUILTIN_PAVGW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26915   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblendvb, "__builtin_ia32_pblendvb256", IX86_BUILTIN_PBLENDVB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI_V32QI },
26916   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblendw, "__builtin_ia32_pblendw256", IX86_BUILTIN_PBLENDVW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI_INT },
26917   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv32qi3, "__builtin_ia32_pcmpeqb256", IX86_BUILTIN_PCMPEQB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26918   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv16hi3, "__builtin_ia32_pcmpeqw256", IX86_BUILTIN_PCMPEQW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26919   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv8si3, "__builtin_ia32_pcmpeqd256", IX86_BUILTIN_PCMPEQD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI  },
26920   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_eqv4di3, "__builtin_ia32_pcmpeqq256", IX86_BUILTIN_PCMPEQQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI  },
26921   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv32qi3, "__builtin_ia32_pcmpgtb256", IX86_BUILTIN_PCMPGTB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26922   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv16hi3, "__builtin_ia32_pcmpgtw256", IX86_BUILTIN_PCMPGTW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26923   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv8si3, "__builtin_ia32_pcmpgtd256", IX86_BUILTIN_PCMPGTD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI  },
26924   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_gtv4di3, "__builtin_ia32_pcmpgtq256", IX86_BUILTIN_PCMPGTQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI  },
26925   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phaddwv16hi3, "__builtin_ia32_phaddw256", IX86_BUILTIN_PHADDW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26926   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phadddv8si3, "__builtin_ia32_phaddd256", IX86_BUILTIN_PHADDD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26927   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phaddswv16hi3, "__builtin_ia32_phaddsw256", IX86_BUILTIN_PHADDSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26928   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phsubwv16hi3, "__builtin_ia32_phsubw256", IX86_BUILTIN_PHSUBW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26929   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phsubdv8si3, "__builtin_ia32_phsubd256", IX86_BUILTIN_PHSUBD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26930   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_phsubswv16hi3, "__builtin_ia32_phsubsw256", IX86_BUILTIN_PHSUBSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26931   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pmaddubsw256, "__builtin_ia32_pmaddubsw256", IX86_BUILTIN_PMADDUBSW256, UNKNOWN, (int) V16HI_FTYPE_V32QI_V32QI },
26932   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pmaddwd, "__builtin_ia32_pmaddwd256", IX86_BUILTIN_PMADDWD256, UNKNOWN, (int) V8SI_FTYPE_V16HI_V16HI },
26933   { OPTION_MASK_ISA_AVX2, CODE_FOR_smaxv32qi3, "__builtin_ia32_pmaxsb256", IX86_BUILTIN_PMAXSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26934   { OPTION_MASK_ISA_AVX2, CODE_FOR_smaxv16hi3, "__builtin_ia32_pmaxsw256", IX86_BUILTIN_PMAXSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26935   { OPTION_MASK_ISA_AVX2, CODE_FOR_smaxv8si3 , "__builtin_ia32_pmaxsd256", IX86_BUILTIN_PMAXSD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26936   { OPTION_MASK_ISA_AVX2, CODE_FOR_umaxv32qi3, "__builtin_ia32_pmaxub256", IX86_BUILTIN_PMAXUB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26937   { OPTION_MASK_ISA_AVX2, CODE_FOR_umaxv16hi3, "__builtin_ia32_pmaxuw256", IX86_BUILTIN_PMAXUW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26938   { OPTION_MASK_ISA_AVX2, CODE_FOR_umaxv8si3 , "__builtin_ia32_pmaxud256", IX86_BUILTIN_PMAXUD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26939   { OPTION_MASK_ISA_AVX2, CODE_FOR_sminv32qi3, "__builtin_ia32_pminsb256", IX86_BUILTIN_PMINSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26940   { OPTION_MASK_ISA_AVX2, CODE_FOR_sminv16hi3, "__builtin_ia32_pminsw256", IX86_BUILTIN_PMINSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26941   { OPTION_MASK_ISA_AVX2, CODE_FOR_sminv8si3 , "__builtin_ia32_pminsd256", IX86_BUILTIN_PMINSD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26942   { OPTION_MASK_ISA_AVX2, CODE_FOR_uminv32qi3, "__builtin_ia32_pminub256", IX86_BUILTIN_PMINUB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26943   { OPTION_MASK_ISA_AVX2, CODE_FOR_uminv16hi3, "__builtin_ia32_pminuw256", IX86_BUILTIN_PMINUW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26944   { OPTION_MASK_ISA_AVX2, CODE_FOR_uminv8si3 , "__builtin_ia32_pminud256", IX86_BUILTIN_PMINUD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26945   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pmovmskb, "__builtin_ia32_pmovmskb256", IX86_BUILTIN_PMOVMSKB256, UNKNOWN, (int) INT_FTYPE_V32QI },
26946   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv16qiv16hi2, "__builtin_ia32_pmovsxbw256", IX86_BUILTIN_PMOVSXBW256, UNKNOWN, (int) V16HI_FTYPE_V16QI },
26947   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv8qiv8si2  , "__builtin_ia32_pmovsxbd256", IX86_BUILTIN_PMOVSXBD256, UNKNOWN, (int) V8SI_FTYPE_V16QI },
26948   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv4qiv4di2  , "__builtin_ia32_pmovsxbq256", IX86_BUILTIN_PMOVSXBQ256, UNKNOWN, (int) V4DI_FTYPE_V16QI },
26949   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv8hiv8si2  , "__builtin_ia32_pmovsxwd256", IX86_BUILTIN_PMOVSXWD256, UNKNOWN, (int) V8SI_FTYPE_V8HI },
26950   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv4hiv4di2  , "__builtin_ia32_pmovsxwq256", IX86_BUILTIN_PMOVSXWQ256, UNKNOWN, (int) V4DI_FTYPE_V8HI },
26951   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sign_extendv4siv4di2  , "__builtin_ia32_pmovsxdq256", IX86_BUILTIN_PMOVSXDQ256, UNKNOWN, (int) V4DI_FTYPE_V4SI },
26952   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv16qiv16hi2, "__builtin_ia32_pmovzxbw256", IX86_BUILTIN_PMOVZXBW256, UNKNOWN, (int) V16HI_FTYPE_V16QI },
26953   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv8qiv8si2  , "__builtin_ia32_pmovzxbd256", IX86_BUILTIN_PMOVZXBD256, UNKNOWN, (int) V8SI_FTYPE_V16QI },
26954   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv4qiv4di2  , "__builtin_ia32_pmovzxbq256", IX86_BUILTIN_PMOVZXBQ256, UNKNOWN, (int) V4DI_FTYPE_V16QI },
26955   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv8hiv8si2  , "__builtin_ia32_pmovzxwd256", IX86_BUILTIN_PMOVZXWD256, UNKNOWN, (int) V8SI_FTYPE_V8HI },
26956   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv4hiv4di2  , "__builtin_ia32_pmovzxwq256", IX86_BUILTIN_PMOVZXWQ256, UNKNOWN, (int) V4DI_FTYPE_V8HI },
26957   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_zero_extendv4siv4di2  , "__builtin_ia32_pmovzxdq256", IX86_BUILTIN_PMOVZXDQ256, UNKNOWN, (int) V4DI_FTYPE_V4SI },
26958   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_mulv4siv4di3  , "__builtin_ia32_pmuldq256"  , IX86_BUILTIN_PMULDQ256  , UNKNOWN, (int) V4DI_FTYPE_V8SI_V8SI },
26959   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_umulhrswv16hi3 , "__builtin_ia32_pmulhrsw256", IX86_BUILTIN_PMULHRSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26960   { OPTION_MASK_ISA_AVX2, CODE_FOR_umulv16hi3_highpart, "__builtin_ia32_pmulhuw256" , IX86_BUILTIN_PMULHUW256 , UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26961   { OPTION_MASK_ISA_AVX2, CODE_FOR_smulv16hi3_highpart, "__builtin_ia32_pmulhw256"  , IX86_BUILTIN_PMULHW256  , UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26962   { OPTION_MASK_ISA_AVX2, CODE_FOR_mulv16hi3, "__builtin_ia32_pmullw256"  , IX86_BUILTIN_PMULLW256  , UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26963   { OPTION_MASK_ISA_AVX2, CODE_FOR_mulv8si3, "__builtin_ia32_pmulld256"  , IX86_BUILTIN_PMULLD256  , UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26964   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_umulv4siv4di3  , "__builtin_ia32_pmuludq256" , IX86_BUILTIN_PMULUDQ256 , UNKNOWN, (int) V4DI_FTYPE_V8SI_V8SI },
26965   { OPTION_MASK_ISA_AVX2, CODE_FOR_iorv4di3, "__builtin_ia32_por256", IX86_BUILTIN_POR256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26966   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psadbw, "__builtin_ia32_psadbw256", IX86_BUILTIN_PSADBW256, UNKNOWN, (int) V16HI_FTYPE_V32QI_V32QI },
26967   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshufbv32qi3, "__builtin_ia32_pshufb256", IX86_BUILTIN_PSHUFB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26968   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshufdv3, "__builtin_ia32_pshufd256", IX86_BUILTIN_PSHUFD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_INT },
26969   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshufhwv3, "__builtin_ia32_pshufhw256", IX86_BUILTIN_PSHUFHW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_INT },
26970   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pshuflwv3, "__builtin_ia32_pshuflw256", IX86_BUILTIN_PSHUFLW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_INT },
26971   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psignv32qi3, "__builtin_ia32_psignb256", IX86_BUILTIN_PSIGNB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26972   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psignv16hi3, "__builtin_ia32_psignw256", IX86_BUILTIN_PSIGNW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26973   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_psignv8si3 , "__builtin_ia32_psignd256", IX86_BUILTIN_PSIGND256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26974   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlv2ti3, "__builtin_ia32_pslldqi256", IX86_BUILTIN_PSLLDQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_CONVERT },
26975   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv16hi3, "__builtin_ia32_psllwi256", IX86_BUILTIN_PSLLWI256 , UNKNOWN, (int) V16HI_FTYPE_V16HI_SI_COUNT },
26976   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv16hi3, "__builtin_ia32_psllw256", IX86_BUILTIN_PSLLW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V8HI_COUNT },
26977   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv8si3, "__builtin_ia32_pslldi256", IX86_BUILTIN_PSLLDI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_SI_COUNT },
26978   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv8si3, "__builtin_ia32_pslld256", IX86_BUILTIN_PSLLD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_COUNT },
26979   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv4di3, "__builtin_ia32_psllqi256", IX86_BUILTIN_PSLLQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_COUNT },
26980   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashlv4di3, "__builtin_ia32_psllq256", IX86_BUILTIN_PSLLQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V2DI_COUNT },
26981   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv16hi3, "__builtin_ia32_psrawi256", IX86_BUILTIN_PSRAWI256, UNKNOWN, (int) V16HI_FTYPE_V16HI_SI_COUNT },
26982   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv16hi3, "__builtin_ia32_psraw256", IX86_BUILTIN_PSRAW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V8HI_COUNT },
26983   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv8si3, "__builtin_ia32_psradi256", IX86_BUILTIN_PSRADI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_SI_COUNT },
26984   { OPTION_MASK_ISA_AVX2, CODE_FOR_ashrv8si3, "__builtin_ia32_psrad256", IX86_BUILTIN_PSRAD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_COUNT },
26985   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrv2ti3, "__builtin_ia32_psrldqi256", IX86_BUILTIN_PSRLDQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_CONVERT },
26986   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv16hi3, "__builtin_ia32_psrlwi256", IX86_BUILTIN_PSRLWI256 , UNKNOWN, (int) V16HI_FTYPE_V16HI_SI_COUNT },
26987   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv16hi3, "__builtin_ia32_psrlw256", IX86_BUILTIN_PSRLW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V8HI_COUNT },
26988   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv8si3, "__builtin_ia32_psrldi256", IX86_BUILTIN_PSRLDI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_SI_COUNT },
26989   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv8si3, "__builtin_ia32_psrld256", IX86_BUILTIN_PSRLD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_COUNT },
26990   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv4di3, "__builtin_ia32_psrlqi256", IX86_BUILTIN_PSRLQI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT_COUNT },
26991   { OPTION_MASK_ISA_AVX2, CODE_FOR_lshrv4di3, "__builtin_ia32_psrlq256", IX86_BUILTIN_PSRLQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V2DI_COUNT },
26992   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv32qi3, "__builtin_ia32_psubb256", IX86_BUILTIN_PSUBB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26993   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv16hi3, "__builtin_ia32_psubw256", IX86_BUILTIN_PSUBW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26994   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv8si3, "__builtin_ia32_psubd256", IX86_BUILTIN_PSUBD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
26995   { OPTION_MASK_ISA_AVX2, CODE_FOR_subv4di3, "__builtin_ia32_psubq256", IX86_BUILTIN_PSUBQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
26996   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sssubv32qi3, "__builtin_ia32_psubsb256", IX86_BUILTIN_PSUBSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26997   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_sssubv16hi3, "__builtin_ia32_psubsw256", IX86_BUILTIN_PSUBSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
26998   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ussubv32qi3, "__builtin_ia32_psubusb256", IX86_BUILTIN_PSUBUSB256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
26999   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ussubv16hi3, "__builtin_ia32_psubusw256", IX86_BUILTIN_PSUBUSW256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
27000   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv32qi, "__builtin_ia32_punpckhbw256", IX86_BUILTIN_PUNPCKHBW256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
27001   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv16hi, "__builtin_ia32_punpckhwd256", IX86_BUILTIN_PUNPCKHWD256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI  },
27002   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv8si, "__builtin_ia32_punpckhdq256", IX86_BUILTIN_PUNPCKHDQ256, UNKNOWN,  (int) V8SI_FTYPE_V8SI_V8SI },
27003   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_highv4di, "__builtin_ia32_punpckhqdq256", IX86_BUILTIN_PUNPCKHQDQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
27004   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv32qi, "__builtin_ia32_punpcklbw256", IX86_BUILTIN_PUNPCKLBW256, UNKNOWN, (int) V32QI_FTYPE_V32QI_V32QI },
27005   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv16hi, "__builtin_ia32_punpcklwd256", IX86_BUILTIN_PUNPCKLWD256, UNKNOWN, (int) V16HI_FTYPE_V16HI_V16HI },
27006   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv8si, "__builtin_ia32_punpckldq256", IX86_BUILTIN_PUNPCKLDQ256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
27007   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_interleave_lowv4di, "__builtin_ia32_punpcklqdq256", IX86_BUILTIN_PUNPCKLQDQ256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
27008   { OPTION_MASK_ISA_AVX2, CODE_FOR_xorv4di3, "__builtin_ia32_pxor256", IX86_BUILTIN_PXOR256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
27009   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vec_dupv4sf, "__builtin_ia32_vbroadcastss_ps", IX86_BUILTIN_VBROADCASTSS_PS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
27010   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vec_dupv8sf, "__builtin_ia32_vbroadcastss_ps256", IX86_BUILTIN_VBROADCASTSS_PS256, UNKNOWN, (int) V8SF_FTYPE_V4SF },
27011   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vec_dupv4df, "__builtin_ia32_vbroadcastsd_pd256", IX86_BUILTIN_VBROADCASTSD_PD256, UNKNOWN, (int) V4DF_FTYPE_V2DF },
27012   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_vbroadcasti128_v4di, "__builtin_ia32_vbroadcastsi256", IX86_BUILTIN_VBROADCASTSI256, UNKNOWN, (int) V4DI_FTYPE_V2DI },
27013   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblenddv4si, "__builtin_ia32_pblendd128", IX86_BUILTIN_PBLENDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_INT },
27014   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pblenddv8si, "__builtin_ia32_pblendd256", IX86_BUILTIN_PBLENDD256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
27015   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv32qi, "__builtin_ia32_pbroadcastb256", IX86_BUILTIN_PBROADCASTB256, UNKNOWN, (int) V32QI_FTYPE_V16QI },
27016   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv16hi, "__builtin_ia32_pbroadcastw256", IX86_BUILTIN_PBROADCASTW256, UNKNOWN, (int) V16HI_FTYPE_V8HI },
27017   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv8si, "__builtin_ia32_pbroadcastd256", IX86_BUILTIN_PBROADCASTD256, UNKNOWN, (int) V8SI_FTYPE_V4SI },
27018   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv4di, "__builtin_ia32_pbroadcastq256", IX86_BUILTIN_PBROADCASTQ256, UNKNOWN, (int) V4DI_FTYPE_V2DI },
27019   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv16qi, "__builtin_ia32_pbroadcastb128", IX86_BUILTIN_PBROADCASTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
27020   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv8hi, "__builtin_ia32_pbroadcastw128", IX86_BUILTIN_PBROADCASTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
27021   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv4si, "__builtin_ia32_pbroadcastd128", IX86_BUILTIN_PBROADCASTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
27022   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_pbroadcastv2di, "__builtin_ia32_pbroadcastq128", IX86_BUILTIN_PBROADCASTQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
27023   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permvarv8si, "__builtin_ia32_permvarsi256", IX86_BUILTIN_VPERMVARSI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
27024   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permv4df, "__builtin_ia32_permdf256", IX86_BUILTIN_VPERMDF256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
27025   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permvarv8sf, "__builtin_ia32_permvarsf256", IX86_BUILTIN_VPERMVARSF256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
27026   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permv4di, "__builtin_ia32_permdi256", IX86_BUILTIN_VPERMDI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_INT },
27027   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_permv2ti, "__builtin_ia32_permti256", IX86_BUILTIN_VPERMTI256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI_INT },
27028   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_extracti128, "__builtin_ia32_extract128i256", IX86_BUILTIN_VEXTRACT128I256, UNKNOWN, (int) V2DI_FTYPE_V4DI_INT },
27029   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_inserti128, "__builtin_ia32_insert128i256", IX86_BUILTIN_VINSERT128I256, UNKNOWN, (int) V4DI_FTYPE_V4DI_V2DI_INT },
27030   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv4di, "__builtin_ia32_psllv4di", IX86_BUILTIN_PSLLVV4DI, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
27031   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv2di, "__builtin_ia32_psllv2di", IX86_BUILTIN_PSLLVV2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
27032   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv8si, "__builtin_ia32_psllv8si", IX86_BUILTIN_PSLLVV8SI, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
27033   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashlvv4si, "__builtin_ia32_psllv4si", IX86_BUILTIN_PSLLVV4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
27034   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashrvv8si, "__builtin_ia32_psrav8si", IX86_BUILTIN_PSRAVV8SI, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
27035   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_ashrvv4si, "__builtin_ia32_psrav4si", IX86_BUILTIN_PSRAVV4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
27036   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv4di, "__builtin_ia32_psrlv4di", IX86_BUILTIN_PSRLVV4DI, UNKNOWN, (int) V4DI_FTYPE_V4DI_V4DI },
27037   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv2di, "__builtin_ia32_psrlv2di", IX86_BUILTIN_PSRLVV2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
27038   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv8si, "__builtin_ia32_psrlv8si", IX86_BUILTIN_PSRLVV8SI, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI },
27039   { OPTION_MASK_ISA_AVX2, CODE_FOR_avx2_lshrvv4si, "__builtin_ia32_psrlv4si", IX86_BUILTIN_PSRLVV4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
27040
27041   { OPTION_MASK_ISA_LZCNT, CODE_FOR_clzhi2_lzcnt,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
27042
27043   /* BMI */
27044   { OPTION_MASK_ISA_BMI, CODE_FOR_bmi_bextr_si, "__builtin_ia32_bextr_u32", IX86_BUILTIN_BEXTR32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
27045   { OPTION_MASK_ISA_BMI, CODE_FOR_bmi_bextr_di, "__builtin_ia32_bextr_u64", IX86_BUILTIN_BEXTR64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
27046   { OPTION_MASK_ISA_BMI, CODE_FOR_ctzhi2,       "__builtin_ctzs",           IX86_BUILTIN_CTZS,    UNKNOWN, (int) UINT16_FTYPE_UINT16 },
27047
27048   /* TBM */
27049   { OPTION_MASK_ISA_TBM, CODE_FOR_tbm_bextri_si, "__builtin_ia32_bextri_u32", IX86_BUILTIN_BEXTRI32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
27050   { OPTION_MASK_ISA_TBM, CODE_FOR_tbm_bextri_di, "__builtin_ia32_bextri_u64", IX86_BUILTIN_BEXTRI64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
27051
27052   /* F16C */
27053   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps, "__builtin_ia32_vcvtph2ps", IX86_BUILTIN_CVTPH2PS, UNKNOWN, (int) V4SF_FTYPE_V8HI },
27054   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps256, "__builtin_ia32_vcvtph2ps256", IX86_BUILTIN_CVTPH2PS256, UNKNOWN, (int) V8SF_FTYPE_V8HI },
27055   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph, "__builtin_ia32_vcvtps2ph", IX86_BUILTIN_CVTPS2PH, UNKNOWN, (int) V8HI_FTYPE_V4SF_INT },
27056   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph256, "__builtin_ia32_vcvtps2ph256", IX86_BUILTIN_CVTPS2PH256, UNKNOWN, (int) V8HI_FTYPE_V8SF_INT },
27057
27058   /* BMI2 */
27059   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_bzhi_si3, "__builtin_ia32_bzhi_si", IX86_BUILTIN_BZHI32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
27060   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_bzhi_di3, "__builtin_ia32_bzhi_di", IX86_BUILTIN_BZHI64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
27061   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pdep_si3, "__builtin_ia32_pdep_si", IX86_BUILTIN_PDEP32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
27062   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pdep_di3, "__builtin_ia32_pdep_di", IX86_BUILTIN_PDEP64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
27063   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pext_si3, "__builtin_ia32_pext_si", IX86_BUILTIN_PEXT32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
27064   { OPTION_MASK_ISA_BMI2, CODE_FOR_bmi2_pext_di3, "__builtin_ia32_pext_di", IX86_BUILTIN_PEXT64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
27065 };
27066
27067 /* FMA4 and XOP.  */
27068 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
27069 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
27070 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
27071 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
27072 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
27073 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
27074 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
27075 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
27076 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
27077 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
27078 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
27079 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
27080 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
27081 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
27082 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
27083 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
27084 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
27085 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
27086 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
27087 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
27088 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
27089 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
27090 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
27091 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
27092 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
27093 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
27094 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
27095 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
27096 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
27097 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
27098 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
27099 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
27100 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
27101 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
27102 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
27103 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
27104 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
27105 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
27106 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
27107 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
27108 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
27109 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
27110 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
27111 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
27112 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
27113 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
27114 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
27115 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
27116 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
27117 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
27118 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
27119 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
27120
27121 static const struct builtin_description bdesc_multi_arg[] =
27122 {
27123   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v4sf,
27124     "__builtin_ia32_vfmaddss", IX86_BUILTIN_VFMADDSS,
27125     UNKNOWN, (int)MULTI_ARG_3_SF },
27126   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v2df,
27127     "__builtin_ia32_vfmaddsd", IX86_BUILTIN_VFMADDSD,
27128     UNKNOWN, (int)MULTI_ARG_3_DF },
27129
27130   { OPTION_MASK_ISA_FMA, CODE_FOR_fmai_vmfmadd_v4sf,
27131     "__builtin_ia32_vfmaddss3", IX86_BUILTIN_VFMADDSS3,
27132     UNKNOWN, (int)MULTI_ARG_3_SF },
27133   { OPTION_MASK_ISA_FMA, CODE_FOR_fmai_vmfmadd_v2df,
27134     "__builtin_ia32_vfmaddsd3", IX86_BUILTIN_VFMADDSD3,
27135     UNKNOWN, (int)MULTI_ARG_3_DF },
27136
27137   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4sf,
27138     "__builtin_ia32_vfmaddps", IX86_BUILTIN_VFMADDPS,
27139     UNKNOWN, (int)MULTI_ARG_3_SF },
27140   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v2df,
27141     "__builtin_ia32_vfmaddpd", IX86_BUILTIN_VFMADDPD,
27142     UNKNOWN, (int)MULTI_ARG_3_DF },
27143   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v8sf,
27144     "__builtin_ia32_vfmaddps256", IX86_BUILTIN_VFMADDPS256,
27145     UNKNOWN, (int)MULTI_ARG_3_SF2 },
27146   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4df,
27147     "__builtin_ia32_vfmaddpd256", IX86_BUILTIN_VFMADDPD256,
27148     UNKNOWN, (int)MULTI_ARG_3_DF2 },
27149
27150   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4sf,
27151     "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,
27152     UNKNOWN, (int)MULTI_ARG_3_SF },
27153   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v2df,
27154     "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,
27155     UNKNOWN, (int)MULTI_ARG_3_DF },
27156   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v8sf,
27157     "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,
27158     UNKNOWN, (int)MULTI_ARG_3_SF2 },
27159   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4df,
27160     "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,
27161     UNKNOWN, (int)MULTI_ARG_3_DF2 },
27162
27163   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
27164   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
27165   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
27166   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
27167   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
27168   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
27169   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
27170
27171   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
27172   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
27173   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
27174   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
27175   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
27176   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
27177   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
27178
27179   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
27180
27181   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
27182   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
27183   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
27184   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
27185   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
27186   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
27187   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
27188   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
27189   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
27190   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
27191   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
27192   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
27193
27194   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
27195   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
27196   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
27197   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
27198   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
27199   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
27200   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
27201   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
27202   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
27203   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
27204   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
27205   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shav16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
27206   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
27207   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
27208   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
27209   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_shlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
27210
27211   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
27212   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
27213   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
27214   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
27215   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
27216   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
27217
27218   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
27219   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
27220   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
27221   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
27222   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
27223   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
27224   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
27225   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
27226   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
27227   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
27228   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
27229   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
27230   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
27231   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
27232   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
27233
27234   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
27235   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
27236   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
27237   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
27238   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
27239   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
27240   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
27241
27242   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
27243   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
27244   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
27245   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
27246   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
27247   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
27248   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
27249
27250   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
27251   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
27252   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
27253   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
27254   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
27255   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
27256   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
27257
27258   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
27259   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
27260   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
27261   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
27262   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
27263   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
27264   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
27265
27266   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
27267   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
27268   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
27269   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
27270   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
27271   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
27272   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
27273
27274   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
27275   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
27276   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
27277   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
27278   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
27279   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
27280   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
27281
27282   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
27283   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
27284   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
27285   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
27286   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
27287   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
27288   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
27289
27290   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
27291   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
27292   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
27293   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
27294   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
27295   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
27296   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
27297
27298   { 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 },
27299   { 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 },
27300   { 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 },
27301   { 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 },
27302   { 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 },
27303   { 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 },
27304   { 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 },
27305   { 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 },
27306
27307   { 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 },
27308   { 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 },
27309   { 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 },
27310   { 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 },
27311   { 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 },
27312   { 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 },
27313   { 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 },
27314   { 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 },
27315
27316   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
27317   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
27318   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
27319   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
27320
27321 };
27322 \f
27323 /* TM vector builtins.  */
27324
27325 /* Reuse the existing x86-specific `struct builtin_description' cause
27326    we're lazy.  Add casts to make them fit.  */
27327 static const struct builtin_description bdesc_tm[] =
27328 {
27329   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_WM64", (enum ix86_builtins) BUILT_IN_TM_STORE_M64, UNKNOWN, VOID_FTYPE_PV2SI_V2SI },
27330   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_WaRM64", (enum ix86_builtins) BUILT_IN_TM_STORE_WAR_M64, UNKNOWN, VOID_FTYPE_PV2SI_V2SI },
27331   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_WaWM64", (enum ix86_builtins) BUILT_IN_TM_STORE_WAW_M64, UNKNOWN, VOID_FTYPE_PV2SI_V2SI },
27332   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27333   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RaRM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAR_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27334   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RaWM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAW_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27335   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_RfWM64", (enum ix86_builtins) BUILT_IN_TM_LOAD_RFW_M64, UNKNOWN, V2SI_FTYPE_PCV2SI },
27336
27337   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_WM128", (enum ix86_builtins) BUILT_IN_TM_STORE_M128, UNKNOWN, VOID_FTYPE_PV4SF_V4SF },
27338   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_WaRM128", (enum ix86_builtins) BUILT_IN_TM_STORE_WAR_M128, UNKNOWN, VOID_FTYPE_PV4SF_V4SF },
27339   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_WaWM128", (enum ix86_builtins) BUILT_IN_TM_STORE_WAW_M128, UNKNOWN, VOID_FTYPE_PV4SF_V4SF },
27340   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27341   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RaRM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAR_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27342   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RaWM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAW_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27343   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_RfWM128", (enum ix86_builtins) BUILT_IN_TM_LOAD_RFW_M128, UNKNOWN, V4SF_FTYPE_PCV4SF },
27344
27345   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_WM256", (enum ix86_builtins) BUILT_IN_TM_STORE_M256, UNKNOWN, VOID_FTYPE_PV8SF_V8SF },
27346   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_WaRM256", (enum ix86_builtins) BUILT_IN_TM_STORE_WAR_M256, UNKNOWN, VOID_FTYPE_PV8SF_V8SF },
27347   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_WaWM256", (enum ix86_builtins) BUILT_IN_TM_STORE_WAW_M256, UNKNOWN, VOID_FTYPE_PV8SF_V8SF },
27348   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27349   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RaRM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAR_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27350   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RaWM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_RAW_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27351   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_RfWM256", (enum ix86_builtins) BUILT_IN_TM_LOAD_RFW_M256, UNKNOWN, V8SF_FTYPE_PCV8SF },
27352
27353   { OPTION_MASK_ISA_MMX, CODE_FOR_nothing, "__builtin__ITM_LM64", (enum ix86_builtins) BUILT_IN_TM_LOG_M64, UNKNOWN, VOID_FTYPE_PCVOID },
27354   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin__ITM_LM128", (enum ix86_builtins) BUILT_IN_TM_LOG_M128, UNKNOWN, VOID_FTYPE_PCVOID },
27355   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin__ITM_LM256", (enum ix86_builtins) BUILT_IN_TM_LOG_M256, UNKNOWN, VOID_FTYPE_PCVOID },
27356 };
27357
27358 /* TM callbacks.  */
27359
27360 /* Return the builtin decl needed to load a vector of TYPE.  */
27361
27362 static tree
27363 ix86_builtin_tm_load (tree type)
27364 {
27365   if (TREE_CODE (type) == VECTOR_TYPE)
27366     {
27367       switch (tree_low_cst (TYPE_SIZE (type), 1))
27368         {
27369         case 64:
27370           return builtin_decl_explicit (BUILT_IN_TM_LOAD_M64);
27371         case 128:
27372           return builtin_decl_explicit (BUILT_IN_TM_LOAD_M128);
27373         case 256:
27374           return builtin_decl_explicit (BUILT_IN_TM_LOAD_M256);
27375         }
27376     }
27377   return NULL_TREE;
27378 }
27379
27380 /* Return the builtin decl needed to store a vector of TYPE.  */
27381
27382 static tree
27383 ix86_builtin_tm_store (tree type)
27384 {
27385   if (TREE_CODE (type) == VECTOR_TYPE)
27386     {
27387       switch (tree_low_cst (TYPE_SIZE (type), 1))
27388         {
27389         case 64:
27390           return builtin_decl_explicit (BUILT_IN_TM_STORE_M64);
27391         case 128:
27392           return builtin_decl_explicit (BUILT_IN_TM_STORE_M128);
27393         case 256:
27394           return builtin_decl_explicit (BUILT_IN_TM_STORE_M256);
27395         }
27396     }
27397   return NULL_TREE;
27398 }
27399 \f
27400 /* Initialize the transactional memory vector load/store builtins.  */
27401
27402 static void
27403 ix86_init_tm_builtins (void)
27404 {
27405   enum ix86_builtin_func_type ftype;
27406   const struct builtin_description *d;
27407   size_t i;
27408   tree decl;
27409   tree attrs_load, attrs_type_load, attrs_store, attrs_type_store;
27410   tree attrs_log, attrs_type_log;
27411
27412   if (!flag_tm)
27413     return;
27414
27415   /* If there are no builtins defined, we must be compiling in a
27416      language without trans-mem support.  */
27417   if (!builtin_decl_explicit_p (BUILT_IN_TM_LOAD_1))
27418     return;
27419
27420   /* Use whatever attributes a normal TM load has.  */
27421   decl = builtin_decl_explicit (BUILT_IN_TM_LOAD_1);
27422   attrs_load = DECL_ATTRIBUTES (decl);
27423   attrs_type_load = TYPE_ATTRIBUTES (TREE_TYPE (decl));
27424   /* Use whatever attributes a normal TM store has.  */
27425   decl = builtin_decl_explicit (BUILT_IN_TM_STORE_1);
27426   attrs_store = DECL_ATTRIBUTES (decl);
27427   attrs_type_store = TYPE_ATTRIBUTES (TREE_TYPE (decl));
27428   /* Use whatever attributes a normal TM log has.  */
27429   decl = builtin_decl_explicit (BUILT_IN_TM_LOG);
27430   attrs_log = DECL_ATTRIBUTES (decl);
27431   attrs_type_log = TYPE_ATTRIBUTES (TREE_TYPE (decl));
27432
27433   for (i = 0, d = bdesc_tm;
27434        i < ARRAY_SIZE (bdesc_tm);
27435        i++, d++)
27436     {
27437       if ((d->mask & ix86_isa_flags) != 0
27438           || (lang_hooks.builtin_function
27439               == lang_hooks.builtin_function_ext_scope))
27440         {
27441           tree type, attrs, attrs_type;
27442           enum built_in_function code = (enum built_in_function) d->code;
27443
27444           ftype = (enum ix86_builtin_func_type) d->flag;
27445           type = ix86_get_builtin_func_type (ftype);
27446
27447           if (BUILTIN_TM_LOAD_P (code))
27448             {
27449               attrs = attrs_load;
27450               attrs_type = attrs_type_load;
27451             }
27452           else if (BUILTIN_TM_STORE_P (code))
27453             {
27454               attrs = attrs_store;
27455               attrs_type = attrs_type_store;
27456             }
27457           else
27458             {
27459               attrs = attrs_log;
27460               attrs_type = attrs_type_log;
27461             }
27462           decl = add_builtin_function (d->name, type, code, BUILT_IN_NORMAL,
27463                                        /* The builtin without the prefix for
27464                                           calling it directly.  */
27465                                        d->name + strlen ("__builtin_"),
27466                                        attrs);
27467           /* add_builtin_function() will set the DECL_ATTRIBUTES, now
27468              set the TYPE_ATTRIBUTES.  */
27469           decl_attributes (&TREE_TYPE (decl), attrs_type, ATTR_FLAG_BUILT_IN);
27470
27471           set_builtin_decl (code, decl, false);
27472         }
27473     }
27474 }
27475
27476 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
27477    in the current target ISA to allow the user to compile particular modules
27478    with different target specific options that differ from the command line
27479    options.  */
27480 static void
27481 ix86_init_mmx_sse_builtins (void)
27482 {
27483   const struct builtin_description * d;
27484   enum ix86_builtin_func_type ftype;
27485   size_t i;
27486
27487   /* Add all special builtins with variable number of operands.  */
27488   for (i = 0, d = bdesc_special_args;
27489        i < ARRAY_SIZE (bdesc_special_args);
27490        i++, d++)
27491     {
27492       if (d->name == 0)
27493         continue;
27494
27495       ftype = (enum ix86_builtin_func_type) d->flag;
27496       def_builtin (d->mask, d->name, ftype, d->code);
27497     }
27498
27499   /* Add all builtins with variable number of operands.  */
27500   for (i = 0, d = bdesc_args;
27501        i < ARRAY_SIZE (bdesc_args);
27502        i++, d++)
27503     {
27504       if (d->name == 0)
27505         continue;
27506
27507       ftype = (enum ix86_builtin_func_type) d->flag;
27508       def_builtin_const (d->mask, d->name, ftype, d->code);
27509     }
27510
27511   /* pcmpestr[im] insns.  */
27512   for (i = 0, d = bdesc_pcmpestr;
27513        i < ARRAY_SIZE (bdesc_pcmpestr);
27514        i++, d++)
27515     {
27516       if (d->code == IX86_BUILTIN_PCMPESTRM128)
27517         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
27518       else
27519         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
27520       def_builtin_const (d->mask, d->name, ftype, d->code);
27521     }
27522
27523   /* pcmpistr[im] insns.  */
27524   for (i = 0, d = bdesc_pcmpistr;
27525        i < ARRAY_SIZE (bdesc_pcmpistr);
27526        i++, d++)
27527     {
27528       if (d->code == IX86_BUILTIN_PCMPISTRM128)
27529         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
27530       else
27531         ftype = INT_FTYPE_V16QI_V16QI_INT;
27532       def_builtin_const (d->mask, d->name, ftype, d->code);
27533     }
27534
27535   /* comi/ucomi insns.  */
27536   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
27537     {
27538       if (d->mask == OPTION_MASK_ISA_SSE2)
27539         ftype = INT_FTYPE_V2DF_V2DF;
27540       else
27541         ftype = INT_FTYPE_V4SF_V4SF;
27542       def_builtin_const (d->mask, d->name, ftype, d->code);
27543     }
27544
27545   /* SSE */
27546   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
27547                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
27548   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
27549                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
27550
27551   /* SSE or 3DNow!A */
27552   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
27553                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
27554                IX86_BUILTIN_MASKMOVQ);
27555
27556   /* SSE2 */
27557   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
27558                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
27559
27560   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
27561                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
27562   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
27563                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
27564
27565   /* SSE3.  */
27566   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
27567                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
27568   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
27569                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
27570
27571   /* AES */
27572   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
27573                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
27574   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
27575                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
27576   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
27577                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
27578   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
27579                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
27580   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
27581                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
27582   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
27583                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
27584
27585   /* PCLMUL */
27586   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
27587                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
27588
27589   /* RDRND */
27590   def_builtin (OPTION_MASK_ISA_RDRND, "__builtin_ia32_rdrand16_step",
27591                INT_FTYPE_PUSHORT, IX86_BUILTIN_RDRAND16_STEP);
27592   def_builtin (OPTION_MASK_ISA_RDRND, "__builtin_ia32_rdrand32_step",
27593                INT_FTYPE_PUNSIGNED, IX86_BUILTIN_RDRAND32_STEP);
27594   def_builtin (OPTION_MASK_ISA_RDRND | OPTION_MASK_ISA_64BIT,
27595                "__builtin_ia32_rdrand64_step", INT_FTYPE_PULONGLONG,
27596                IX86_BUILTIN_RDRAND64_STEP);
27597
27598   /* AVX2 */
27599   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv2df",
27600                V2DF_FTYPE_V2DF_PCDOUBLE_V4SI_V2DF_INT,
27601                IX86_BUILTIN_GATHERSIV2DF);
27602
27603   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4df",
27604                V4DF_FTYPE_V4DF_PCDOUBLE_V4SI_V4DF_INT,
27605                IX86_BUILTIN_GATHERSIV4DF);
27606
27607   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv2df",
27608                V2DF_FTYPE_V2DF_PCDOUBLE_V2DI_V2DF_INT,
27609                IX86_BUILTIN_GATHERDIV2DF);
27610
27611   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4df",
27612                V4DF_FTYPE_V4DF_PCDOUBLE_V4DI_V4DF_INT,
27613                IX86_BUILTIN_GATHERDIV4DF);
27614
27615   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4sf",
27616                V4SF_FTYPE_V4SF_PCFLOAT_V4SI_V4SF_INT,
27617                IX86_BUILTIN_GATHERSIV4SF);
27618
27619   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv8sf",
27620                V8SF_FTYPE_V8SF_PCFLOAT_V8SI_V8SF_INT,
27621                IX86_BUILTIN_GATHERSIV8SF);
27622
27623   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4sf",
27624                V4SF_FTYPE_V4SF_PCFLOAT_V2DI_V4SF_INT,
27625                IX86_BUILTIN_GATHERDIV4SF);
27626
27627   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4sf256",
27628                V4SF_FTYPE_V4SF_PCFLOAT_V4DI_V4SF_INT,
27629                IX86_BUILTIN_GATHERDIV8SF);
27630
27631   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv2di",
27632                V2DI_FTYPE_V2DI_PCINT64_V4SI_V2DI_INT,
27633                IX86_BUILTIN_GATHERSIV2DI);
27634
27635   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4di",
27636                V4DI_FTYPE_V4DI_PCINT64_V4SI_V4DI_INT,
27637                IX86_BUILTIN_GATHERSIV4DI);
27638
27639   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv2di",
27640                V2DI_FTYPE_V2DI_PCINT64_V2DI_V2DI_INT,
27641                IX86_BUILTIN_GATHERDIV2DI);
27642
27643   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4di",
27644                V4DI_FTYPE_V4DI_PCINT64_V4DI_V4DI_INT,
27645                IX86_BUILTIN_GATHERDIV4DI);
27646
27647   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv4si",
27648                V4SI_FTYPE_V4SI_PCINT_V4SI_V4SI_INT,
27649                IX86_BUILTIN_GATHERSIV4SI);
27650
27651   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gathersiv8si",
27652                V8SI_FTYPE_V8SI_PCINT_V8SI_V8SI_INT,
27653                IX86_BUILTIN_GATHERSIV8SI);
27654
27655   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4si",
27656                V4SI_FTYPE_V4SI_PCINT_V2DI_V4SI_INT,
27657                IX86_BUILTIN_GATHERDIV4SI);
27658
27659   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatherdiv4si256",
27660                V4SI_FTYPE_V4SI_PCINT_V4DI_V4SI_INT,
27661                IX86_BUILTIN_GATHERDIV8SI);
27662
27663   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltsiv4df ",
27664                V4DF_FTYPE_V4DF_PCDOUBLE_V8SI_V4DF_INT,
27665                IX86_BUILTIN_GATHERALTSIV4DF);
27666
27667   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltdiv4sf256 ",
27668                V8SF_FTYPE_V8SF_PCFLOAT_V4DI_V8SF_INT,
27669                IX86_BUILTIN_GATHERALTDIV8SF);
27670
27671   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltsiv4di ",
27672                V4DI_FTYPE_V4DI_PCINT64_V8SI_V4DI_INT,
27673                IX86_BUILTIN_GATHERALTSIV4DI);
27674
27675   def_builtin (OPTION_MASK_ISA_AVX2, "__builtin_ia32_gatheraltdiv4si256 ",
27676                V8SI_FTYPE_V8SI_PCINT_V4DI_V8SI_INT,
27677                IX86_BUILTIN_GATHERALTDIV8SI);
27678
27679   /* MMX access to the vec_init patterns.  */
27680   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
27681                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
27682
27683   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
27684                      V4HI_FTYPE_HI_HI_HI_HI,
27685                      IX86_BUILTIN_VEC_INIT_V4HI);
27686
27687   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
27688                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
27689                      IX86_BUILTIN_VEC_INIT_V8QI);
27690
27691   /* Access to the vec_extract patterns.  */
27692   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
27693                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
27694   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
27695                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
27696   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
27697                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
27698   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
27699                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
27700   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
27701                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
27702
27703   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
27704                      "__builtin_ia32_vec_ext_v4hi",
27705                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
27706
27707   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
27708                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
27709
27710   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
27711                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
27712
27713   /* Access to the vec_set patterns.  */
27714   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
27715                      "__builtin_ia32_vec_set_v2di",
27716                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
27717
27718   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
27719                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
27720
27721   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
27722                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
27723
27724   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
27725                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
27726
27727   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
27728                      "__builtin_ia32_vec_set_v4hi",
27729                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
27730
27731   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
27732                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
27733
27734   /* Add FMA4 multi-arg argument instructions */
27735   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
27736     {
27737       if (d->name == 0)
27738         continue;
27739
27740       ftype = (enum ix86_builtin_func_type) d->flag;
27741       def_builtin_const (d->mask, d->name, ftype, d->code);
27742     }
27743 }
27744
27745 /* Internal method for ix86_init_builtins.  */
27746
27747 static void
27748 ix86_init_builtins_va_builtins_abi (void)
27749 {
27750   tree ms_va_ref, sysv_va_ref;
27751   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
27752   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
27753   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
27754   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
27755
27756   if (!TARGET_64BIT)
27757     return;
27758   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
27759   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
27760   ms_va_ref = build_reference_type (ms_va_list_type_node);
27761   sysv_va_ref =
27762     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
27763
27764   fnvoid_va_end_ms =
27765     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
27766   fnvoid_va_start_ms =
27767     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
27768   fnvoid_va_end_sysv =
27769     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
27770   fnvoid_va_start_sysv =
27771     build_varargs_function_type_list (void_type_node, sysv_va_ref,
27772                                        NULL_TREE);
27773   fnvoid_va_copy_ms =
27774     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
27775                               NULL_TREE);
27776   fnvoid_va_copy_sysv =
27777     build_function_type_list (void_type_node, sysv_va_ref,
27778                               sysv_va_ref, NULL_TREE);
27779
27780   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
27781                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
27782   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
27783                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
27784   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
27785                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
27786   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
27787                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
27788   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
27789                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
27790   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
27791                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
27792 }
27793
27794 static void
27795 ix86_init_builtin_types (void)
27796 {
27797   tree float128_type_node, float80_type_node;
27798
27799   /* The __float80 type.  */
27800   float80_type_node = long_double_type_node;
27801   if (TYPE_MODE (float80_type_node) != XFmode)
27802     {
27803       /* The __float80 type.  */
27804       float80_type_node = make_node (REAL_TYPE);
27805
27806       TYPE_PRECISION (float80_type_node) = 80;
27807       layout_type (float80_type_node);
27808     }
27809   lang_hooks.types.register_builtin_type (float80_type_node, "__float80");
27810
27811   /* The __float128 type.  */
27812   float128_type_node = make_node (REAL_TYPE);
27813   TYPE_PRECISION (float128_type_node) = 128;
27814   layout_type (float128_type_node);
27815   lang_hooks.types.register_builtin_type (float128_type_node, "__float128");
27816
27817   /* This macro is built by i386-builtin-types.awk.  */
27818   DEFINE_BUILTIN_PRIMITIVE_TYPES;
27819 }
27820
27821 static void
27822 ix86_init_builtins (void)
27823 {
27824   tree t;
27825
27826   ix86_init_builtin_types ();
27827
27828   /* TFmode support builtins.  */
27829   def_builtin_const (0, "__builtin_infq",
27830                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
27831   def_builtin_const (0, "__builtin_huge_valq",
27832                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
27833
27834   /* We will expand them to normal call if SSE2 isn't available since
27835      they are used by libgcc. */
27836   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
27837   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
27838                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
27839   TREE_READONLY (t) = 1;
27840   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
27841
27842   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
27843   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
27844                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
27845   TREE_READONLY (t) = 1;
27846   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
27847
27848   ix86_init_tm_builtins ();
27849   ix86_init_mmx_sse_builtins ();
27850
27851   if (TARGET_LP64)
27852     ix86_init_builtins_va_builtins_abi ();
27853
27854 #ifdef SUBTARGET_INIT_BUILTINS
27855   SUBTARGET_INIT_BUILTINS;
27856 #endif
27857 }
27858
27859 /* Return the ix86 builtin for CODE.  */
27860
27861 static tree
27862 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
27863 {
27864   if (code >= IX86_BUILTIN_MAX)
27865     return error_mark_node;
27866
27867   return ix86_builtins[code];
27868 }
27869
27870 /* Errors in the source file can cause expand_expr to return const0_rtx
27871    where we expect a vector.  To avoid crashing, use one of the vector
27872    clear instructions.  */
27873 static rtx
27874 safe_vector_operand (rtx x, enum machine_mode mode)
27875 {
27876   if (x == const0_rtx)
27877     x = CONST0_RTX (mode);
27878   return x;
27879 }
27880
27881 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
27882
27883 static rtx
27884 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
27885 {
27886   rtx pat;
27887   tree arg0 = CALL_EXPR_ARG (exp, 0);
27888   tree arg1 = CALL_EXPR_ARG (exp, 1);
27889   rtx op0 = expand_normal (arg0);
27890   rtx op1 = expand_normal (arg1);
27891   enum machine_mode tmode = insn_data[icode].operand[0].mode;
27892   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
27893   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
27894
27895   if (VECTOR_MODE_P (mode0))
27896     op0 = safe_vector_operand (op0, mode0);
27897   if (VECTOR_MODE_P (mode1))
27898     op1 = safe_vector_operand (op1, mode1);
27899
27900   if (optimize || !target
27901       || GET_MODE (target) != tmode
27902       || !insn_data[icode].operand[0].predicate (target, tmode))
27903     target = gen_reg_rtx (tmode);
27904
27905   if (GET_MODE (op1) == SImode && mode1 == TImode)
27906     {
27907       rtx x = gen_reg_rtx (V4SImode);
27908       emit_insn (gen_sse2_loadd (x, op1));
27909       op1 = gen_lowpart (TImode, x);
27910     }
27911
27912   if (!insn_data[icode].operand[1].predicate (op0, mode0))
27913     op0 = copy_to_mode_reg (mode0, op0);
27914   if (!insn_data[icode].operand[2].predicate (op1, mode1))
27915     op1 = copy_to_mode_reg (mode1, op1);
27916
27917   pat = GEN_FCN (icode) (target, op0, op1);
27918   if (! pat)
27919     return 0;
27920
27921   emit_insn (pat);
27922
27923   return target;
27924 }
27925
27926 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
27927
27928 static rtx
27929 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
27930                                enum ix86_builtin_func_type m_type,
27931                                enum rtx_code sub_code)
27932 {
27933   rtx pat;
27934   int i;
27935   int nargs;
27936   bool comparison_p = false;
27937   bool tf_p = false;
27938   bool last_arg_constant = false;
27939   int num_memory = 0;
27940   struct {
27941     rtx op;
27942     enum machine_mode mode;
27943   } args[4];
27944
27945   enum machine_mode tmode = insn_data[icode].operand[0].mode;
27946
27947   switch (m_type)
27948     {
27949     case MULTI_ARG_4_DF2_DI_I:
27950     case MULTI_ARG_4_DF2_DI_I1:
27951     case MULTI_ARG_4_SF2_SI_I:
27952     case MULTI_ARG_4_SF2_SI_I1:
27953       nargs = 4;
27954       last_arg_constant = true;
27955       break;
27956
27957     case MULTI_ARG_3_SF:
27958     case MULTI_ARG_3_DF:
27959     case MULTI_ARG_3_SF2:
27960     case MULTI_ARG_3_DF2:
27961     case MULTI_ARG_3_DI:
27962     case MULTI_ARG_3_SI:
27963     case MULTI_ARG_3_SI_DI:
27964     case MULTI_ARG_3_HI:
27965     case MULTI_ARG_3_HI_SI:
27966     case MULTI_ARG_3_QI:
27967     case MULTI_ARG_3_DI2:
27968     case MULTI_ARG_3_SI2:
27969     case MULTI_ARG_3_HI2:
27970     case MULTI_ARG_3_QI2:
27971       nargs = 3;
27972       break;
27973
27974     case MULTI_ARG_2_SF:
27975     case MULTI_ARG_2_DF:
27976     case MULTI_ARG_2_DI:
27977     case MULTI_ARG_2_SI:
27978     case MULTI_ARG_2_HI:
27979     case MULTI_ARG_2_QI:
27980       nargs = 2;
27981       break;
27982
27983     case MULTI_ARG_2_DI_IMM:
27984     case MULTI_ARG_2_SI_IMM:
27985     case MULTI_ARG_2_HI_IMM:
27986     case MULTI_ARG_2_QI_IMM:
27987       nargs = 2;
27988       last_arg_constant = true;
27989       break;
27990
27991     case MULTI_ARG_1_SF:
27992     case MULTI_ARG_1_DF:
27993     case MULTI_ARG_1_SF2:
27994     case MULTI_ARG_1_DF2:
27995     case MULTI_ARG_1_DI:
27996     case MULTI_ARG_1_SI:
27997     case MULTI_ARG_1_HI:
27998     case MULTI_ARG_1_QI:
27999     case MULTI_ARG_1_SI_DI:
28000     case MULTI_ARG_1_HI_DI:
28001     case MULTI_ARG_1_HI_SI:
28002     case MULTI_ARG_1_QI_DI:
28003     case MULTI_ARG_1_QI_SI:
28004     case MULTI_ARG_1_QI_HI:
28005       nargs = 1;
28006       break;
28007
28008     case MULTI_ARG_2_DI_CMP:
28009     case MULTI_ARG_2_SI_CMP:
28010     case MULTI_ARG_2_HI_CMP:
28011     case MULTI_ARG_2_QI_CMP:
28012       nargs = 2;
28013       comparison_p = true;
28014       break;
28015
28016     case MULTI_ARG_2_SF_TF:
28017     case MULTI_ARG_2_DF_TF:
28018     case MULTI_ARG_2_DI_TF:
28019     case MULTI_ARG_2_SI_TF:
28020     case MULTI_ARG_2_HI_TF:
28021     case MULTI_ARG_2_QI_TF:
28022       nargs = 2;
28023       tf_p = true;
28024       break;
28025
28026     default:
28027       gcc_unreachable ();
28028     }
28029
28030   if (optimize || !target
28031       || GET_MODE (target) != tmode
28032       || !insn_data[icode].operand[0].predicate (target, tmode))
28033     target = gen_reg_rtx (tmode);
28034
28035   gcc_assert (nargs <= 4);
28036
28037   for (i = 0; i < nargs; i++)
28038     {
28039       tree arg = CALL_EXPR_ARG (exp, i);
28040       rtx op = expand_normal (arg);
28041       int adjust = (comparison_p) ? 1 : 0;
28042       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
28043
28044       if (last_arg_constant && i == nargs - 1)
28045         {
28046           if (!insn_data[icode].operand[i + 1].predicate (op, mode))
28047             {
28048               enum insn_code new_icode = icode;
28049               switch (icode)
28050                 {
28051                 case CODE_FOR_xop_vpermil2v2df3:
28052                 case CODE_FOR_xop_vpermil2v4sf3:
28053                 case CODE_FOR_xop_vpermil2v4df3:
28054                 case CODE_FOR_xop_vpermil2v8sf3:
28055                   error ("the last argument must be a 2-bit immediate");
28056                   return gen_reg_rtx (tmode);
28057                 case CODE_FOR_xop_rotlv2di3:
28058                   new_icode = CODE_FOR_rotlv2di3;
28059                   goto xop_rotl;
28060                 case CODE_FOR_xop_rotlv4si3:
28061                   new_icode = CODE_FOR_rotlv4si3;
28062                   goto xop_rotl;
28063                 case CODE_FOR_xop_rotlv8hi3:
28064                   new_icode = CODE_FOR_rotlv8hi3;
28065                   goto xop_rotl;
28066                 case CODE_FOR_xop_rotlv16qi3:
28067                   new_icode = CODE_FOR_rotlv16qi3;
28068                 xop_rotl:
28069                   if (CONST_INT_P (op))
28070                     {
28071                       int mask = GET_MODE_BITSIZE (GET_MODE_INNER (tmode)) - 1;
28072                       op = GEN_INT (INTVAL (op) & mask);
28073                       gcc_checking_assert
28074                         (insn_data[icode].operand[i + 1].predicate (op, mode));
28075                     }
28076                   else
28077                     {
28078                       gcc_checking_assert
28079                         (nargs == 2
28080                          && insn_data[new_icode].operand[0].mode == tmode
28081                          && insn_data[new_icode].operand[1].mode == tmode
28082                          && insn_data[new_icode].operand[2].mode == mode
28083                          && insn_data[new_icode].operand[0].predicate
28084                             == insn_data[icode].operand[0].predicate
28085                          && insn_data[new_icode].operand[1].predicate
28086                             == insn_data[icode].operand[1].predicate);
28087                       icode = new_icode;
28088                       goto non_constant;
28089                     }
28090                   break;
28091                 default:
28092                   gcc_unreachable ();
28093                 }
28094             }
28095         }
28096       else
28097         {
28098         non_constant:
28099           if (VECTOR_MODE_P (mode))
28100             op = safe_vector_operand (op, mode);
28101
28102           /* If we aren't optimizing, only allow one memory operand to be
28103              generated.  */
28104           if (memory_operand (op, mode))
28105             num_memory++;
28106
28107           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
28108
28109           if (optimize
28110               || !insn_data[icode].operand[i+adjust+1].predicate (op, mode)
28111               || num_memory > 1)
28112             op = force_reg (mode, op);
28113         }
28114
28115       args[i].op = op;
28116       args[i].mode = mode;
28117     }
28118
28119   switch (nargs)
28120     {
28121     case 1:
28122       pat = GEN_FCN (icode) (target, args[0].op);
28123       break;
28124
28125     case 2:
28126       if (tf_p)
28127         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
28128                                GEN_INT ((int)sub_code));
28129       else if (! comparison_p)
28130         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
28131       else
28132         {
28133           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
28134                                        args[0].op,
28135                                        args[1].op);
28136
28137           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
28138         }
28139       break;
28140
28141     case 3:
28142       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
28143       break;
28144
28145     case 4:
28146       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
28147       break;
28148
28149     default:
28150       gcc_unreachable ();
28151     }
28152
28153   if (! pat)
28154     return 0;
28155
28156   emit_insn (pat);
28157   return target;
28158 }
28159
28160 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
28161    insns with vec_merge.  */
28162
28163 static rtx
28164 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
28165                                     rtx target)
28166 {
28167   rtx pat;
28168   tree arg0 = CALL_EXPR_ARG (exp, 0);
28169   rtx op1, op0 = expand_normal (arg0);
28170   enum machine_mode tmode = insn_data[icode].operand[0].mode;
28171   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
28172
28173   if (optimize || !target
28174       || GET_MODE (target) != tmode
28175       || !insn_data[icode].operand[0].predicate (target, tmode))
28176     target = gen_reg_rtx (tmode);
28177
28178   if (VECTOR_MODE_P (mode0))
28179     op0 = safe_vector_operand (op0, mode0);
28180
28181   if ((optimize && !register_operand (op0, mode0))
28182       || !insn_data[icode].operand[1].predicate (op0, mode0))
28183     op0 = copy_to_mode_reg (mode0, op0);
28184
28185   op1 = op0;
28186   if (!insn_data[icode].operand[2].predicate (op1, mode0))
28187     op1 = copy_to_mode_reg (mode0, op1);
28188
28189   pat = GEN_FCN (icode) (target, op0, op1);
28190   if (! pat)
28191     return 0;
28192   emit_insn (pat);
28193   return target;
28194 }
28195
28196 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
28197
28198 static rtx
28199 ix86_expand_sse_compare (const struct builtin_description *d,
28200                          tree exp, rtx target, bool swap)
28201 {
28202   rtx pat;
28203   tree arg0 = CALL_EXPR_ARG (exp, 0);
28204   tree arg1 = CALL_EXPR_ARG (exp, 1);
28205   rtx op0 = expand_normal (arg0);
28206   rtx op1 = expand_normal (arg1);
28207   rtx op2;
28208   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
28209   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
28210   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
28211   enum rtx_code comparison = d->comparison;
28212
28213   if (VECTOR_MODE_P (mode0))
28214     op0 = safe_vector_operand (op0, mode0);
28215   if (VECTOR_MODE_P (mode1))
28216     op1 = safe_vector_operand (op1, mode1);
28217
28218   /* Swap operands if we have a comparison that isn't available in
28219      hardware.  */
28220   if (swap)
28221     {
28222       rtx tmp = gen_reg_rtx (mode1);
28223       emit_move_insn (tmp, op1);
28224       op1 = op0;
28225       op0 = tmp;
28226     }
28227
28228   if (optimize || !target
28229       || GET_MODE (target) != tmode
28230       || !insn_data[d->icode].operand[0].predicate (target, tmode))
28231     target = gen_reg_rtx (tmode);
28232
28233   if ((optimize && !register_operand (op0, mode0))
28234       || !insn_data[d->icode].operand[1].predicate (op0, mode0))
28235     op0 = copy_to_mode_reg (mode0, op0);
28236   if ((optimize && !register_operand (op1, mode1))
28237       || !insn_data[d->icode].operand[2].predicate (op1, mode1))
28238     op1 = copy_to_mode_reg (mode1, op1);
28239
28240   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
28241   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
28242   if (! pat)
28243     return 0;
28244   emit_insn (pat);
28245   return target;
28246 }
28247
28248 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
28249
28250 static rtx
28251 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
28252                       rtx target)
28253 {
28254   rtx pat;
28255   tree arg0 = CALL_EXPR_ARG (exp, 0);
28256   tree arg1 = CALL_EXPR_ARG (exp, 1);
28257   rtx op0 = expand_normal (arg0);
28258   rtx op1 = expand_normal (arg1);
28259   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
28260   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
28261   enum rtx_code comparison = d->comparison;
28262
28263   if (VECTOR_MODE_P (mode0))
28264     op0 = safe_vector_operand (op0, mode0);
28265   if (VECTOR_MODE_P (mode1))
28266     op1 = safe_vector_operand (op1, mode1);
28267
28268   /* Swap operands if we have a comparison that isn't available in
28269      hardware.  */
28270   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
28271     {
28272       rtx tmp = op1;
28273       op1 = op0;
28274       op0 = tmp;
28275     }
28276
28277   target = gen_reg_rtx (SImode);
28278   emit_move_insn (target, const0_rtx);
28279   target = gen_rtx_SUBREG (QImode, target, 0);
28280
28281   if ((optimize && !register_operand (op0, mode0))
28282       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28283     op0 = copy_to_mode_reg (mode0, op0);
28284   if ((optimize && !register_operand (op1, mode1))
28285       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
28286     op1 = copy_to_mode_reg (mode1, op1);
28287
28288   pat = GEN_FCN (d->icode) (op0, op1);
28289   if (! pat)
28290     return 0;
28291   emit_insn (pat);
28292   emit_insn (gen_rtx_SET (VOIDmode,
28293                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28294                           gen_rtx_fmt_ee (comparison, QImode,
28295                                           SET_DEST (pat),
28296                                           const0_rtx)));
28297
28298   return SUBREG_REG (target);
28299 }
28300
28301 /* Subroutines of ix86_expand_args_builtin to take care of round insns.  */
28302
28303 static rtx
28304 ix86_expand_sse_round (const struct builtin_description *d, tree exp,
28305                        rtx target)
28306 {
28307   rtx pat;
28308   tree arg0 = CALL_EXPR_ARG (exp, 0);
28309   rtx op1, op0 = expand_normal (arg0);
28310   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
28311   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
28312
28313   if (optimize || target == 0
28314       || GET_MODE (target) != tmode
28315       || !insn_data[d->icode].operand[0].predicate (target, tmode))
28316     target = gen_reg_rtx (tmode);
28317
28318   if (VECTOR_MODE_P (mode0))
28319     op0 = safe_vector_operand (op0, mode0);
28320
28321   if ((optimize && !register_operand (op0, mode0))
28322       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28323     op0 = copy_to_mode_reg (mode0, op0);
28324
28325   op1 = GEN_INT (d->comparison);
28326
28327   pat = GEN_FCN (d->icode) (target, op0, op1);
28328   if (! pat)
28329     return 0;
28330   emit_insn (pat);
28331   return target;
28332 }
28333
28334 static rtx
28335 ix86_expand_sse_round_vec_pack_sfix (const struct builtin_description *d,
28336                                      tree exp, rtx target)
28337 {
28338   rtx pat;
28339   tree arg0 = CALL_EXPR_ARG (exp, 0);
28340   tree arg1 = CALL_EXPR_ARG (exp, 1);
28341   rtx op0 = expand_normal (arg0);
28342   rtx op1 = expand_normal (arg1);
28343   rtx op2;
28344   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
28345   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
28346   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
28347
28348   if (optimize || target == 0
28349       || GET_MODE (target) != tmode
28350       || !insn_data[d->icode].operand[0].predicate (target, tmode))
28351     target = gen_reg_rtx (tmode);
28352
28353   op0 = safe_vector_operand (op0, mode0);
28354   op1 = safe_vector_operand (op1, mode1);
28355
28356   if ((optimize && !register_operand (op0, mode0))
28357       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28358     op0 = copy_to_mode_reg (mode0, op0);
28359   if ((optimize && !register_operand (op1, mode1))
28360       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
28361     op1 = copy_to_mode_reg (mode1, op1);
28362
28363   op2 = GEN_INT (d->comparison);
28364
28365   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
28366   if (! pat)
28367     return 0;
28368   emit_insn (pat);
28369   return target;
28370 }
28371
28372 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
28373
28374 static rtx
28375 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
28376                        rtx target)
28377 {
28378   rtx pat;
28379   tree arg0 = CALL_EXPR_ARG (exp, 0);
28380   tree arg1 = CALL_EXPR_ARG (exp, 1);
28381   rtx op0 = expand_normal (arg0);
28382   rtx op1 = expand_normal (arg1);
28383   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
28384   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
28385   enum rtx_code comparison = d->comparison;
28386
28387   if (VECTOR_MODE_P (mode0))
28388     op0 = safe_vector_operand (op0, mode0);
28389   if (VECTOR_MODE_P (mode1))
28390     op1 = safe_vector_operand (op1, mode1);
28391
28392   target = gen_reg_rtx (SImode);
28393   emit_move_insn (target, const0_rtx);
28394   target = gen_rtx_SUBREG (QImode, target, 0);
28395
28396   if ((optimize && !register_operand (op0, mode0))
28397       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
28398     op0 = copy_to_mode_reg (mode0, op0);
28399   if ((optimize && !register_operand (op1, mode1))
28400       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
28401     op1 = copy_to_mode_reg (mode1, op1);
28402
28403   pat = GEN_FCN (d->icode) (op0, op1);
28404   if (! pat)
28405     return 0;
28406   emit_insn (pat);
28407   emit_insn (gen_rtx_SET (VOIDmode,
28408                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28409                           gen_rtx_fmt_ee (comparison, QImode,
28410                                           SET_DEST (pat),
28411                                           const0_rtx)));
28412
28413   return SUBREG_REG (target);
28414 }
28415
28416 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
28417
28418 static rtx
28419 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
28420                           tree exp, rtx target)
28421 {
28422   rtx pat;
28423   tree arg0 = CALL_EXPR_ARG (exp, 0);
28424   tree arg1 = CALL_EXPR_ARG (exp, 1);
28425   tree arg2 = CALL_EXPR_ARG (exp, 2);
28426   tree arg3 = CALL_EXPR_ARG (exp, 3);
28427   tree arg4 = CALL_EXPR_ARG (exp, 4);
28428   rtx scratch0, scratch1;
28429   rtx op0 = expand_normal (arg0);
28430   rtx op1 = expand_normal (arg1);
28431   rtx op2 = expand_normal (arg2);
28432   rtx op3 = expand_normal (arg3);
28433   rtx op4 = expand_normal (arg4);
28434   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
28435
28436   tmode0 = insn_data[d->icode].operand[0].mode;
28437   tmode1 = insn_data[d->icode].operand[1].mode;
28438   modev2 = insn_data[d->icode].operand[2].mode;
28439   modei3 = insn_data[d->icode].operand[3].mode;
28440   modev4 = insn_data[d->icode].operand[4].mode;
28441   modei5 = insn_data[d->icode].operand[5].mode;
28442   modeimm = insn_data[d->icode].operand[6].mode;
28443
28444   if (VECTOR_MODE_P (modev2))
28445     op0 = safe_vector_operand (op0, modev2);
28446   if (VECTOR_MODE_P (modev4))
28447     op2 = safe_vector_operand (op2, modev4);
28448
28449   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
28450     op0 = copy_to_mode_reg (modev2, op0);
28451   if (!insn_data[d->icode].operand[3].predicate (op1, modei3))
28452     op1 = copy_to_mode_reg (modei3, op1);
28453   if ((optimize && !register_operand (op2, modev4))
28454       || !insn_data[d->icode].operand[4].predicate (op2, modev4))
28455     op2 = copy_to_mode_reg (modev4, op2);
28456   if (!insn_data[d->icode].operand[5].predicate (op3, modei5))
28457     op3 = copy_to_mode_reg (modei5, op3);
28458
28459   if (!insn_data[d->icode].operand[6].predicate (op4, modeimm))
28460     {
28461       error ("the fifth argument must be an 8-bit immediate");
28462       return const0_rtx;
28463     }
28464
28465   if (d->code == IX86_BUILTIN_PCMPESTRI128)
28466     {
28467       if (optimize || !target
28468           || GET_MODE (target) != tmode0
28469           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
28470         target = gen_reg_rtx (tmode0);
28471
28472       scratch1 = gen_reg_rtx (tmode1);
28473
28474       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
28475     }
28476   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
28477     {
28478       if (optimize || !target
28479           || GET_MODE (target) != tmode1
28480           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
28481         target = gen_reg_rtx (tmode1);
28482
28483       scratch0 = gen_reg_rtx (tmode0);
28484
28485       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
28486     }
28487   else
28488     {
28489       gcc_assert (d->flag);
28490
28491       scratch0 = gen_reg_rtx (tmode0);
28492       scratch1 = gen_reg_rtx (tmode1);
28493
28494       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
28495     }
28496
28497   if (! pat)
28498     return 0;
28499
28500   emit_insn (pat);
28501
28502   if (d->flag)
28503     {
28504       target = gen_reg_rtx (SImode);
28505       emit_move_insn (target, const0_rtx);
28506       target = gen_rtx_SUBREG (QImode, target, 0);
28507
28508       emit_insn
28509         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28510                       gen_rtx_fmt_ee (EQ, QImode,
28511                                       gen_rtx_REG ((enum machine_mode) d->flag,
28512                                                    FLAGS_REG),
28513                                       const0_rtx)));
28514       return SUBREG_REG (target);
28515     }
28516   else
28517     return target;
28518 }
28519
28520
28521 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
28522
28523 static rtx
28524 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
28525                           tree exp, rtx target)
28526 {
28527   rtx pat;
28528   tree arg0 = CALL_EXPR_ARG (exp, 0);
28529   tree arg1 = CALL_EXPR_ARG (exp, 1);
28530   tree arg2 = CALL_EXPR_ARG (exp, 2);
28531   rtx scratch0, scratch1;
28532   rtx op0 = expand_normal (arg0);
28533   rtx op1 = expand_normal (arg1);
28534   rtx op2 = expand_normal (arg2);
28535   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
28536
28537   tmode0 = insn_data[d->icode].operand[0].mode;
28538   tmode1 = insn_data[d->icode].operand[1].mode;
28539   modev2 = insn_data[d->icode].operand[2].mode;
28540   modev3 = insn_data[d->icode].operand[3].mode;
28541   modeimm = insn_data[d->icode].operand[4].mode;
28542
28543   if (VECTOR_MODE_P (modev2))
28544     op0 = safe_vector_operand (op0, modev2);
28545   if (VECTOR_MODE_P (modev3))
28546     op1 = safe_vector_operand (op1, modev3);
28547
28548   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
28549     op0 = copy_to_mode_reg (modev2, op0);
28550   if ((optimize && !register_operand (op1, modev3))
28551       || !insn_data[d->icode].operand[3].predicate (op1, modev3))
28552     op1 = copy_to_mode_reg (modev3, op1);
28553
28554   if (!insn_data[d->icode].operand[4].predicate (op2, modeimm))
28555     {
28556       error ("the third argument must be an 8-bit immediate");
28557       return const0_rtx;
28558     }
28559
28560   if (d->code == IX86_BUILTIN_PCMPISTRI128)
28561     {
28562       if (optimize || !target
28563           || GET_MODE (target) != tmode0
28564           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
28565         target = gen_reg_rtx (tmode0);
28566
28567       scratch1 = gen_reg_rtx (tmode1);
28568
28569       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
28570     }
28571   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
28572     {
28573       if (optimize || !target
28574           || GET_MODE (target) != tmode1
28575           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
28576         target = gen_reg_rtx (tmode1);
28577
28578       scratch0 = gen_reg_rtx (tmode0);
28579
28580       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
28581     }
28582   else
28583     {
28584       gcc_assert (d->flag);
28585
28586       scratch0 = gen_reg_rtx (tmode0);
28587       scratch1 = gen_reg_rtx (tmode1);
28588
28589       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
28590     }
28591
28592   if (! pat)
28593     return 0;
28594
28595   emit_insn (pat);
28596
28597   if (d->flag)
28598     {
28599       target = gen_reg_rtx (SImode);
28600       emit_move_insn (target, const0_rtx);
28601       target = gen_rtx_SUBREG (QImode, target, 0);
28602
28603       emit_insn
28604         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
28605                       gen_rtx_fmt_ee (EQ, QImode,
28606                                       gen_rtx_REG ((enum machine_mode) d->flag,
28607                                                    FLAGS_REG),
28608                                       const0_rtx)));
28609       return SUBREG_REG (target);
28610     }
28611   else
28612     return target;
28613 }
28614
28615 /* Subroutine of ix86_expand_builtin to take care of insns with
28616    variable number of operands.  */
28617
28618 static rtx
28619 ix86_expand_args_builtin (const struct builtin_description *d,
28620                           tree exp, rtx target)
28621 {
28622   rtx pat, real_target;
28623   unsigned int i, nargs;
28624   unsigned int nargs_constant = 0;
28625   int num_memory = 0;
28626   struct
28627     {
28628       rtx op;
28629       enum machine_mode mode;
28630     } args[4];
28631   bool last_arg_count = false;
28632   enum insn_code icode = d->icode;
28633   const struct insn_data_d *insn_p = &insn_data[icode];
28634   enum machine_mode tmode = insn_p->operand[0].mode;
28635   enum machine_mode rmode = VOIDmode;
28636   bool swap = false;
28637   enum rtx_code comparison = d->comparison;
28638
28639   switch ((enum ix86_builtin_func_type) d->flag)
28640     {
28641     case V2DF_FTYPE_V2DF_ROUND:
28642     case V4DF_FTYPE_V4DF_ROUND:
28643     case V4SF_FTYPE_V4SF_ROUND:
28644     case V8SF_FTYPE_V8SF_ROUND:
28645     case V4SI_FTYPE_V4SF_ROUND:
28646     case V8SI_FTYPE_V8SF_ROUND:
28647       return ix86_expand_sse_round (d, exp, target);
28648     case V4SI_FTYPE_V2DF_V2DF_ROUND:
28649     case V8SI_FTYPE_V4DF_V4DF_ROUND:
28650       return ix86_expand_sse_round_vec_pack_sfix (d, exp, target);
28651     case INT_FTYPE_V8SF_V8SF_PTEST:
28652     case INT_FTYPE_V4DI_V4DI_PTEST:
28653     case INT_FTYPE_V4DF_V4DF_PTEST:
28654     case INT_FTYPE_V4SF_V4SF_PTEST:
28655     case INT_FTYPE_V2DI_V2DI_PTEST:
28656     case INT_FTYPE_V2DF_V2DF_PTEST:
28657       return ix86_expand_sse_ptest (d, exp, target);
28658     case FLOAT128_FTYPE_FLOAT128:
28659     case FLOAT_FTYPE_FLOAT:
28660     case INT_FTYPE_INT:
28661     case UINT64_FTYPE_INT:
28662     case UINT16_FTYPE_UINT16:
28663     case INT64_FTYPE_INT64:
28664     case INT64_FTYPE_V4SF:
28665     case INT64_FTYPE_V2DF:
28666     case INT_FTYPE_V16QI:
28667     case INT_FTYPE_V8QI:
28668     case INT_FTYPE_V8SF:
28669     case INT_FTYPE_V4DF:
28670     case INT_FTYPE_V4SF:
28671     case INT_FTYPE_V2DF:
28672     case INT_FTYPE_V32QI:
28673     case V16QI_FTYPE_V16QI:
28674     case V8SI_FTYPE_V8SF:
28675     case V8SI_FTYPE_V4SI:
28676     case V8HI_FTYPE_V8HI:
28677     case V8HI_FTYPE_V16QI:
28678     case V8QI_FTYPE_V8QI:
28679     case V8SF_FTYPE_V8SF:
28680     case V8SF_FTYPE_V8SI:
28681     case V8SF_FTYPE_V4SF:
28682     case V8SF_FTYPE_V8HI:
28683     case V4SI_FTYPE_V4SI:
28684     case V4SI_FTYPE_V16QI:
28685     case V4SI_FTYPE_V4SF:
28686     case V4SI_FTYPE_V8SI:
28687     case V4SI_FTYPE_V8HI:
28688     case V4SI_FTYPE_V4DF:
28689     case V4SI_FTYPE_V2DF:
28690     case V4HI_FTYPE_V4HI:
28691     case V4DF_FTYPE_V4DF:
28692     case V4DF_FTYPE_V4SI:
28693     case V4DF_FTYPE_V4SF:
28694     case V4DF_FTYPE_V2DF:
28695     case V4SF_FTYPE_V4SF:
28696     case V4SF_FTYPE_V4SI:
28697     case V4SF_FTYPE_V8SF:
28698     case V4SF_FTYPE_V4DF:
28699     case V4SF_FTYPE_V8HI:
28700     case V4SF_FTYPE_V2DF:
28701     case V2DI_FTYPE_V2DI:
28702     case V2DI_FTYPE_V16QI:
28703     case V2DI_FTYPE_V8HI:
28704     case V2DI_FTYPE_V4SI:
28705     case V2DF_FTYPE_V2DF:
28706     case V2DF_FTYPE_V4SI:
28707     case V2DF_FTYPE_V4DF:
28708     case V2DF_FTYPE_V4SF:
28709     case V2DF_FTYPE_V2SI:
28710     case V2SI_FTYPE_V2SI:
28711     case V2SI_FTYPE_V4SF:
28712     case V2SI_FTYPE_V2SF:
28713     case V2SI_FTYPE_V2DF:
28714     case V2SF_FTYPE_V2SF:
28715     case V2SF_FTYPE_V2SI:
28716     case V32QI_FTYPE_V32QI:
28717     case V32QI_FTYPE_V16QI:
28718     case V16HI_FTYPE_V16HI:
28719     case V16HI_FTYPE_V8HI:
28720     case V8SI_FTYPE_V8SI:
28721     case V16HI_FTYPE_V16QI:
28722     case V8SI_FTYPE_V16QI:
28723     case V4DI_FTYPE_V16QI:
28724     case V8SI_FTYPE_V8HI:
28725     case V4DI_FTYPE_V8HI:
28726     case V4DI_FTYPE_V4SI:
28727     case V4DI_FTYPE_V2DI:
28728       nargs = 1;
28729       break;
28730     case V4SF_FTYPE_V4SF_VEC_MERGE:
28731     case V2DF_FTYPE_V2DF_VEC_MERGE:
28732       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
28733     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
28734     case V16QI_FTYPE_V16QI_V16QI:
28735     case V16QI_FTYPE_V8HI_V8HI:
28736     case V8QI_FTYPE_V8QI_V8QI:
28737     case V8QI_FTYPE_V4HI_V4HI:
28738     case V8HI_FTYPE_V8HI_V8HI:
28739     case V8HI_FTYPE_V16QI_V16QI:
28740     case V8HI_FTYPE_V4SI_V4SI:
28741     case V8SF_FTYPE_V8SF_V8SF:
28742     case V8SF_FTYPE_V8SF_V8SI:
28743     case V4SI_FTYPE_V4SI_V4SI:
28744     case V4SI_FTYPE_V8HI_V8HI:
28745     case V4SI_FTYPE_V4SF_V4SF:
28746     case V4SI_FTYPE_V2DF_V2DF:
28747     case V4HI_FTYPE_V4HI_V4HI:
28748     case V4HI_FTYPE_V8QI_V8QI:
28749     case V4HI_FTYPE_V2SI_V2SI:
28750     case V4DF_FTYPE_V4DF_V4DF:
28751     case V4DF_FTYPE_V4DF_V4DI:
28752     case V4SF_FTYPE_V4SF_V4SF:
28753     case V4SF_FTYPE_V4SF_V4SI:
28754     case V4SF_FTYPE_V4SF_V2SI:
28755     case V4SF_FTYPE_V4SF_V2DF:
28756     case V4SF_FTYPE_V4SF_DI:
28757     case V4SF_FTYPE_V4SF_SI:
28758     case V2DI_FTYPE_V2DI_V2DI:
28759     case V2DI_FTYPE_V16QI_V16QI:
28760     case V2DI_FTYPE_V4SI_V4SI:
28761     case V2DI_FTYPE_V2DI_V16QI:
28762     case V2DI_FTYPE_V2DF_V2DF:
28763     case V2SI_FTYPE_V2SI_V2SI:
28764     case V2SI_FTYPE_V4HI_V4HI:
28765     case V2SI_FTYPE_V2SF_V2SF:
28766     case V2DF_FTYPE_V2DF_V2DF:
28767     case V2DF_FTYPE_V2DF_V4SF:
28768     case V2DF_FTYPE_V2DF_V2DI:
28769     case V2DF_FTYPE_V2DF_DI:
28770     case V2DF_FTYPE_V2DF_SI:
28771     case V2SF_FTYPE_V2SF_V2SF:
28772     case V1DI_FTYPE_V1DI_V1DI:
28773     case V1DI_FTYPE_V8QI_V8QI:
28774     case V1DI_FTYPE_V2SI_V2SI:
28775     case V32QI_FTYPE_V16HI_V16HI:
28776     case V16HI_FTYPE_V8SI_V8SI:
28777     case V32QI_FTYPE_V32QI_V32QI:
28778     case V16HI_FTYPE_V32QI_V32QI:
28779     case V16HI_FTYPE_V16HI_V16HI:
28780     case V8SI_FTYPE_V4DF_V4DF:
28781     case V8SI_FTYPE_V8SI_V8SI:
28782     case V8SI_FTYPE_V16HI_V16HI:
28783     case V4DI_FTYPE_V4DI_V4DI:
28784     case V4DI_FTYPE_V8SI_V8SI:
28785       if (comparison == UNKNOWN)
28786         return ix86_expand_binop_builtin (icode, exp, target);
28787       nargs = 2;
28788       break;
28789     case V4SF_FTYPE_V4SF_V4SF_SWAP:
28790     case V2DF_FTYPE_V2DF_V2DF_SWAP:
28791       gcc_assert (comparison != UNKNOWN);
28792       nargs = 2;
28793       swap = true;
28794       break;
28795     case V16HI_FTYPE_V16HI_V8HI_COUNT:
28796     case V16HI_FTYPE_V16HI_SI_COUNT:
28797     case V8SI_FTYPE_V8SI_V4SI_COUNT:
28798     case V8SI_FTYPE_V8SI_SI_COUNT:
28799     case V4DI_FTYPE_V4DI_V2DI_COUNT:
28800     case V4DI_FTYPE_V4DI_INT_COUNT:
28801     case V8HI_FTYPE_V8HI_V8HI_COUNT:
28802     case V8HI_FTYPE_V8HI_SI_COUNT:
28803     case V4SI_FTYPE_V4SI_V4SI_COUNT:
28804     case V4SI_FTYPE_V4SI_SI_COUNT:
28805     case V4HI_FTYPE_V4HI_V4HI_COUNT:
28806     case V4HI_FTYPE_V4HI_SI_COUNT:
28807     case V2DI_FTYPE_V2DI_V2DI_COUNT:
28808     case V2DI_FTYPE_V2DI_SI_COUNT:
28809     case V2SI_FTYPE_V2SI_V2SI_COUNT:
28810     case V2SI_FTYPE_V2SI_SI_COUNT:
28811     case V1DI_FTYPE_V1DI_V1DI_COUNT:
28812     case V1DI_FTYPE_V1DI_SI_COUNT:
28813       nargs = 2;
28814       last_arg_count = true;
28815       break;
28816     case UINT64_FTYPE_UINT64_UINT64:
28817     case UINT_FTYPE_UINT_UINT:
28818     case UINT_FTYPE_UINT_USHORT:
28819     case UINT_FTYPE_UINT_UCHAR:
28820     case UINT16_FTYPE_UINT16_INT:
28821     case UINT8_FTYPE_UINT8_INT:
28822       nargs = 2;
28823       break;
28824     case V2DI_FTYPE_V2DI_INT_CONVERT:
28825       nargs = 2;
28826       rmode = V1TImode;
28827       nargs_constant = 1;
28828       break;
28829     case V4DI_FTYPE_V4DI_INT_CONVERT:
28830       nargs = 2;
28831       rmode = V2TImode;
28832       nargs_constant = 1;
28833       break;
28834     case V8HI_FTYPE_V8HI_INT:
28835     case V8HI_FTYPE_V8SF_INT:
28836     case V8HI_FTYPE_V4SF_INT:
28837     case V8SF_FTYPE_V8SF_INT:
28838     case V4SI_FTYPE_V4SI_INT:
28839     case V4SI_FTYPE_V8SI_INT:
28840     case V4HI_FTYPE_V4HI_INT:
28841     case V4DF_FTYPE_V4DF_INT:
28842     case V4SF_FTYPE_V4SF_INT:
28843     case V4SF_FTYPE_V8SF_INT:
28844     case V2DI_FTYPE_V2DI_INT:
28845     case V2DF_FTYPE_V2DF_INT:
28846     case V2DF_FTYPE_V4DF_INT:
28847     case V16HI_FTYPE_V16HI_INT:
28848     case V8SI_FTYPE_V8SI_INT:
28849     case V4DI_FTYPE_V4DI_INT:
28850     case V2DI_FTYPE_V4DI_INT:
28851       nargs = 2;
28852       nargs_constant = 1;
28853       break;
28854     case V16QI_FTYPE_V16QI_V16QI_V16QI:
28855     case V8SF_FTYPE_V8SF_V8SF_V8SF:
28856     case V4DF_FTYPE_V4DF_V4DF_V4DF:
28857     case V4SF_FTYPE_V4SF_V4SF_V4SF:
28858     case V2DF_FTYPE_V2DF_V2DF_V2DF:
28859     case V32QI_FTYPE_V32QI_V32QI_V32QI:
28860       nargs = 3;
28861       break;
28862     case V32QI_FTYPE_V32QI_V32QI_INT:
28863     case V16HI_FTYPE_V16HI_V16HI_INT:
28864     case V16QI_FTYPE_V16QI_V16QI_INT:
28865     case V4DI_FTYPE_V4DI_V4DI_INT:
28866     case V8HI_FTYPE_V8HI_V8HI_INT:
28867     case V8SI_FTYPE_V8SI_V8SI_INT:
28868     case V8SI_FTYPE_V8SI_V4SI_INT:
28869     case V8SF_FTYPE_V8SF_V8SF_INT:
28870     case V8SF_FTYPE_V8SF_V4SF_INT:
28871     case V4SI_FTYPE_V4SI_V4SI_INT:
28872     case V4DF_FTYPE_V4DF_V4DF_INT:
28873     case V4DF_FTYPE_V4DF_V2DF_INT:
28874     case V4SF_FTYPE_V4SF_V4SF_INT:
28875     case V2DI_FTYPE_V2DI_V2DI_INT:
28876     case V4DI_FTYPE_V4DI_V2DI_INT:
28877     case V2DF_FTYPE_V2DF_V2DF_INT:
28878       nargs = 3;
28879       nargs_constant = 1;
28880       break;
28881     case V4DI_FTYPE_V4DI_V4DI_INT_CONVERT:
28882       nargs = 3;
28883       rmode = V4DImode;
28884       nargs_constant = 1;
28885       break;
28886     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
28887       nargs = 3;
28888       rmode = V2DImode;
28889       nargs_constant = 1;
28890       break;
28891     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
28892       nargs = 3;
28893       rmode = DImode;
28894       nargs_constant = 1;
28895       break;
28896     case V2DI_FTYPE_V2DI_UINT_UINT:
28897       nargs = 3;
28898       nargs_constant = 2;
28899       break;
28900     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
28901     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
28902     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
28903     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
28904       nargs = 4;
28905       nargs_constant = 1;
28906       break;
28907     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
28908       nargs = 4;
28909       nargs_constant = 2;
28910       break;
28911     default:
28912       gcc_unreachable ();
28913     }
28914
28915   gcc_assert (nargs <= ARRAY_SIZE (args));
28916
28917   if (comparison != UNKNOWN)
28918     {
28919       gcc_assert (nargs == 2);
28920       return ix86_expand_sse_compare (d, exp, target, swap);
28921     }
28922
28923   if (rmode == VOIDmode || rmode == tmode)
28924     {
28925       if (optimize
28926           || target == 0
28927           || GET_MODE (target) != tmode
28928           || !insn_p->operand[0].predicate (target, tmode))
28929         target = gen_reg_rtx (tmode);
28930       real_target = target;
28931     }
28932   else
28933     {
28934       target = gen_reg_rtx (rmode);
28935       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
28936     }
28937
28938   for (i = 0; i < nargs; i++)
28939     {
28940       tree arg = CALL_EXPR_ARG (exp, i);
28941       rtx op = expand_normal (arg);
28942       enum machine_mode mode = insn_p->operand[i + 1].mode;
28943       bool match = insn_p->operand[i + 1].predicate (op, mode);
28944
28945       if (last_arg_count && (i + 1) == nargs)
28946         {
28947           /* SIMD shift insns take either an 8-bit immediate or
28948              register as count.  But builtin functions take int as
28949              count.  If count doesn't match, we put it in register.  */
28950           if (!match)
28951             {
28952               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
28953               if (!insn_p->operand[i + 1].predicate (op, mode))
28954                 op = copy_to_reg (op);
28955             }
28956         }
28957       else if ((nargs - i) <= nargs_constant)
28958         {
28959           if (!match)
28960             switch (icode)
28961               {
28962               case CODE_FOR_avx2_inserti128:
28963               case CODE_FOR_avx2_extracti128:
28964                 error ("the last argument must be an 1-bit immediate");
28965                 return const0_rtx;
28966
28967               case CODE_FOR_sse4_1_roundsd:
28968               case CODE_FOR_sse4_1_roundss:
28969
28970               case CODE_FOR_sse4_1_roundpd:
28971               case CODE_FOR_sse4_1_roundps:
28972               case CODE_FOR_avx_roundpd256:
28973               case CODE_FOR_avx_roundps256:
28974
28975               case CODE_FOR_sse4_1_roundpd_vec_pack_sfix:
28976               case CODE_FOR_sse4_1_roundps_sfix:
28977               case CODE_FOR_avx_roundpd_vec_pack_sfix256:
28978               case CODE_FOR_avx_roundps_sfix256:
28979
28980               case CODE_FOR_sse4_1_blendps:
28981               case CODE_FOR_avx_blendpd256:
28982               case CODE_FOR_avx_vpermilv4df:
28983                 error ("the last argument must be a 4-bit immediate");
28984                 return const0_rtx;
28985
28986               case CODE_FOR_sse4_1_blendpd:
28987               case CODE_FOR_avx_vpermilv2df:
28988               case CODE_FOR_xop_vpermil2v2df3:
28989               case CODE_FOR_xop_vpermil2v4sf3:
28990               case CODE_FOR_xop_vpermil2v4df3:
28991               case CODE_FOR_xop_vpermil2v8sf3:
28992                 error ("the last argument must be a 2-bit immediate");
28993                 return const0_rtx;
28994
28995               case CODE_FOR_avx_vextractf128v4df:
28996               case CODE_FOR_avx_vextractf128v8sf:
28997               case CODE_FOR_avx_vextractf128v8si:
28998               case CODE_FOR_avx_vinsertf128v4df:
28999               case CODE_FOR_avx_vinsertf128v8sf:
29000               case CODE_FOR_avx_vinsertf128v8si:
29001                 error ("the last argument must be a 1-bit immediate");
29002                 return const0_rtx;
29003
29004               case CODE_FOR_avx_vmcmpv2df3:
29005               case CODE_FOR_avx_vmcmpv4sf3:
29006               case CODE_FOR_avx_cmpv2df3:
29007               case CODE_FOR_avx_cmpv4sf3:
29008               case CODE_FOR_avx_cmpv4df3:
29009               case CODE_FOR_avx_cmpv8sf3:
29010                 error ("the last argument must be a 5-bit immediate");
29011                 return const0_rtx;
29012
29013              default:
29014                 switch (nargs_constant)
29015                   {
29016                   case 2:
29017                     if ((nargs - i) == nargs_constant)
29018                       {
29019                         error ("the next to last argument must be an 8-bit immediate");
29020                         break;
29021                       }
29022                   case 1:
29023                     error ("the last argument must be an 8-bit immediate");
29024                     break;
29025                   default:
29026                     gcc_unreachable ();
29027                   }
29028                 return const0_rtx;
29029               }
29030         }
29031       else
29032         {
29033           if (VECTOR_MODE_P (mode))
29034             op = safe_vector_operand (op, mode);
29035
29036           /* If we aren't optimizing, only allow one memory operand to
29037              be generated.  */
29038           if (memory_operand (op, mode))
29039             num_memory++;
29040
29041           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
29042             {
29043               if (optimize || !match || num_memory > 1)
29044                 op = copy_to_mode_reg (mode, op);
29045             }
29046           else
29047             {
29048               op = copy_to_reg (op);
29049               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
29050             }
29051         }
29052
29053       args[i].op = op;
29054       args[i].mode = mode;
29055     }
29056
29057   switch (nargs)
29058     {
29059     case 1:
29060       pat = GEN_FCN (icode) (real_target, args[0].op);
29061       break;
29062     case 2:
29063       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
29064       break;
29065     case 3:
29066       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
29067                              args[2].op);
29068       break;
29069     case 4:
29070       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
29071                              args[2].op, args[3].op);
29072       break;
29073     default:
29074       gcc_unreachable ();
29075     }
29076
29077   if (! pat)
29078     return 0;
29079
29080   emit_insn (pat);
29081   return target;
29082 }
29083
29084 /* Subroutine of ix86_expand_builtin to take care of special insns
29085    with variable number of operands.  */
29086
29087 static rtx
29088 ix86_expand_special_args_builtin (const struct builtin_description *d,
29089                                     tree exp, rtx target)
29090 {
29091   tree arg;
29092   rtx pat, op;
29093   unsigned int i, nargs, arg_adjust, memory;
29094   struct
29095     {
29096       rtx op;
29097       enum machine_mode mode;
29098     } args[3];
29099   enum insn_code icode = d->icode;
29100   bool last_arg_constant = false;
29101   const struct insn_data_d *insn_p = &insn_data[icode];
29102   enum machine_mode tmode = insn_p->operand[0].mode;
29103   enum { load, store } klass;
29104
29105   switch ((enum ix86_builtin_func_type) d->flag)
29106     {
29107     case VOID_FTYPE_VOID:
29108       if (icode == CODE_FOR_avx_vzeroupper)
29109         target = GEN_INT (vzeroupper_intrinsic);
29110       emit_insn (GEN_FCN (icode) (target));
29111       return 0;
29112     case VOID_FTYPE_UINT64:
29113     case VOID_FTYPE_UNSIGNED:
29114       nargs = 0;
29115       klass = store;
29116       memory = 0;
29117       break;
29118     case UINT64_FTYPE_VOID:
29119     case UNSIGNED_FTYPE_VOID:
29120       nargs = 0;
29121       klass = load;
29122       memory = 0;
29123       break;
29124     case UINT64_FTYPE_PUNSIGNED:
29125     case V2DI_FTYPE_PV2DI:
29126     case V4DI_FTYPE_PV4DI:
29127     case V32QI_FTYPE_PCCHAR:
29128     case V16QI_FTYPE_PCCHAR:
29129     case V8SF_FTYPE_PCV4SF:
29130     case V8SF_FTYPE_PCFLOAT:
29131     case V4SF_FTYPE_PCFLOAT:
29132     case V4DF_FTYPE_PCV2DF:
29133     case V4DF_FTYPE_PCDOUBLE:
29134     case V2DF_FTYPE_PCDOUBLE:
29135     case VOID_FTYPE_PVOID:
29136       nargs = 1;
29137       klass = load;
29138       memory = 0;
29139       break;
29140     case VOID_FTYPE_PV2SF_V4SF:
29141     case VOID_FTYPE_PV4DI_V4DI:
29142     case VOID_FTYPE_PV2DI_V2DI:
29143     case VOID_FTYPE_PCHAR_V32QI:
29144     case VOID_FTYPE_PCHAR_V16QI:
29145     case VOID_FTYPE_PFLOAT_V8SF:
29146     case VOID_FTYPE_PFLOAT_V4SF:
29147     case VOID_FTYPE_PDOUBLE_V4DF:
29148     case VOID_FTYPE_PDOUBLE_V2DF:
29149     case VOID_FTYPE_PLONGLONG_LONGLONG:
29150     case VOID_FTYPE_PULONGLONG_ULONGLONG:
29151     case VOID_FTYPE_PINT_INT:
29152       nargs = 1;
29153       klass = store;
29154       /* Reserve memory operand for target.  */
29155       memory = ARRAY_SIZE (args);
29156       break;
29157     case V4SF_FTYPE_V4SF_PCV2SF:
29158     case V2DF_FTYPE_V2DF_PCDOUBLE:
29159       nargs = 2;
29160       klass = load;
29161       memory = 1;
29162       break;
29163     case V8SF_FTYPE_PCV8SF_V8SI:
29164     case V4DF_FTYPE_PCV4DF_V4DI:
29165     case V4SF_FTYPE_PCV4SF_V4SI:
29166     case V2DF_FTYPE_PCV2DF_V2DI:
29167     case V8SI_FTYPE_PCV8SI_V8SI:
29168     case V4DI_FTYPE_PCV4DI_V4DI:
29169     case V4SI_FTYPE_PCV4SI_V4SI:
29170     case V2DI_FTYPE_PCV2DI_V2DI:
29171       nargs = 2;
29172       klass = load;
29173       memory = 0;
29174       break;
29175     case VOID_FTYPE_PV8SF_V8SI_V8SF:
29176     case VOID_FTYPE_PV4DF_V4DI_V4DF:
29177     case VOID_FTYPE_PV4SF_V4SI_V4SF:
29178     case VOID_FTYPE_PV2DF_V2DI_V2DF:
29179     case VOID_FTYPE_PV8SI_V8SI_V8SI:
29180     case VOID_FTYPE_PV4DI_V4DI_V4DI:
29181     case VOID_FTYPE_PV4SI_V4SI_V4SI:
29182     case VOID_FTYPE_PV2DI_V2DI_V2DI:
29183       nargs = 2;
29184       klass = store;
29185       /* Reserve memory operand for target.  */
29186       memory = ARRAY_SIZE (args);
29187       break;
29188     case VOID_FTYPE_UINT_UINT_UINT:
29189     case VOID_FTYPE_UINT64_UINT_UINT:
29190     case UCHAR_FTYPE_UINT_UINT_UINT:
29191     case UCHAR_FTYPE_UINT64_UINT_UINT:
29192       nargs = 3;
29193       klass = load;
29194       memory = ARRAY_SIZE (args);
29195       last_arg_constant = true;
29196       break;
29197     default:
29198       gcc_unreachable ();
29199     }
29200
29201   gcc_assert (nargs <= ARRAY_SIZE (args));
29202
29203   if (klass == store)
29204     {
29205       arg = CALL_EXPR_ARG (exp, 0);
29206       op = expand_normal (arg);
29207       gcc_assert (target == 0);
29208       if (memory)
29209         {
29210           if (GET_MODE (op) != Pmode)
29211             op = convert_to_mode (Pmode, op, 1);
29212           target = gen_rtx_MEM (tmode, force_reg (Pmode, op));
29213         }
29214       else
29215         target = force_reg (tmode, op);
29216       arg_adjust = 1;
29217     }
29218   else
29219     {
29220       arg_adjust = 0;
29221       if (optimize
29222           || target == 0
29223           || !register_operand (target, tmode)
29224           || GET_MODE (target) != tmode)
29225         target = gen_reg_rtx (tmode);
29226     }
29227
29228   for (i = 0; i < nargs; i++)
29229     {
29230       enum machine_mode mode = insn_p->operand[i + 1].mode;
29231       bool match;
29232
29233       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
29234       op = expand_normal (arg);
29235       match = insn_p->operand[i + 1].predicate (op, mode);
29236
29237       if (last_arg_constant && (i + 1) == nargs)
29238         {
29239           if (!match)
29240             {
29241               if (icode == CODE_FOR_lwp_lwpvalsi3
29242                   || icode == CODE_FOR_lwp_lwpinssi3
29243                   || icode == CODE_FOR_lwp_lwpvaldi3
29244                   || icode == CODE_FOR_lwp_lwpinsdi3)
29245                 error ("the last argument must be a 32-bit immediate");
29246               else
29247                 error ("the last argument must be an 8-bit immediate");
29248               return const0_rtx;
29249             }
29250         }
29251       else
29252         {
29253           if (i == memory)
29254             {
29255               /* This must be the memory operand.  */
29256               if (GET_MODE (op) != Pmode)
29257                 op = convert_to_mode (Pmode, op, 1);
29258               op = gen_rtx_MEM (mode, force_reg (Pmode, op));
29259               gcc_assert (GET_MODE (op) == mode
29260                           || GET_MODE (op) == VOIDmode);
29261             }
29262           else
29263             {
29264               /* This must be register.  */
29265               if (VECTOR_MODE_P (mode))
29266                 op = safe_vector_operand (op, mode);
29267
29268               gcc_assert (GET_MODE (op) == mode
29269                           || GET_MODE (op) == VOIDmode);
29270               op = copy_to_mode_reg (mode, op);
29271             }
29272         }
29273
29274       args[i].op = op;
29275       args[i].mode = mode;
29276     }
29277
29278   switch (nargs)
29279     {
29280     case 0:
29281       pat = GEN_FCN (icode) (target);
29282       break;
29283     case 1:
29284       pat = GEN_FCN (icode) (target, args[0].op);
29285       break;
29286     case 2:
29287       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
29288       break;
29289     case 3:
29290       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
29291       break;
29292     default:
29293       gcc_unreachable ();
29294     }
29295
29296   if (! pat)
29297     return 0;
29298   emit_insn (pat);
29299   return klass == store ? 0 : target;
29300 }
29301
29302 /* Return the integer constant in ARG.  Constrain it to be in the range
29303    of the subparts of VEC_TYPE; issue an error if not.  */
29304
29305 static int
29306 get_element_number (tree vec_type, tree arg)
29307 {
29308   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
29309
29310   if (!host_integerp (arg, 1)
29311       || (elt = tree_low_cst (arg, 1), elt > max))
29312     {
29313       error ("selector must be an integer constant in the range 0..%wi", max);
29314       return 0;
29315     }
29316
29317   return elt;
29318 }
29319
29320 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
29321    ix86_expand_vector_init.  We DO have language-level syntax for this, in
29322    the form of  (type){ init-list }.  Except that since we can't place emms
29323    instructions from inside the compiler, we can't allow the use of MMX
29324    registers unless the user explicitly asks for it.  So we do *not* define
29325    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
29326    we have builtins invoked by mmintrin.h that gives us license to emit
29327    these sorts of instructions.  */
29328
29329 static rtx
29330 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
29331 {
29332   enum machine_mode tmode = TYPE_MODE (type);
29333   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
29334   int i, n_elt = GET_MODE_NUNITS (tmode);
29335   rtvec v = rtvec_alloc (n_elt);
29336
29337   gcc_assert (VECTOR_MODE_P (tmode));
29338   gcc_assert (call_expr_nargs (exp) == n_elt);
29339
29340   for (i = 0; i < n_elt; ++i)
29341     {
29342       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
29343       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
29344     }
29345
29346   if (!target || !register_operand (target, tmode))
29347     target = gen_reg_rtx (tmode);
29348
29349   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
29350   return target;
29351 }
29352
29353 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
29354    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
29355    had a language-level syntax for referencing vector elements.  */
29356
29357 static rtx
29358 ix86_expand_vec_ext_builtin (tree exp, rtx target)
29359 {
29360   enum machine_mode tmode, mode0;
29361   tree arg0, arg1;
29362   int elt;
29363   rtx op0;
29364
29365   arg0 = CALL_EXPR_ARG (exp, 0);
29366   arg1 = CALL_EXPR_ARG (exp, 1);
29367
29368   op0 = expand_normal (arg0);
29369   elt = get_element_number (TREE_TYPE (arg0), arg1);
29370
29371   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
29372   mode0 = TYPE_MODE (TREE_TYPE (arg0));
29373   gcc_assert (VECTOR_MODE_P (mode0));
29374
29375   op0 = force_reg (mode0, op0);
29376
29377   if (optimize || !target || !register_operand (target, tmode))
29378     target = gen_reg_rtx (tmode);
29379
29380   ix86_expand_vector_extract (true, target, op0, elt);
29381
29382   return target;
29383 }
29384
29385 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
29386    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
29387    a language-level syntax for referencing vector elements.  */
29388
29389 static rtx
29390 ix86_expand_vec_set_builtin (tree exp)
29391 {
29392   enum machine_mode tmode, mode1;
29393   tree arg0, arg1, arg2;
29394   int elt;
29395   rtx op0, op1, target;
29396
29397   arg0 = CALL_EXPR_ARG (exp, 0);
29398   arg1 = CALL_EXPR_ARG (exp, 1);
29399   arg2 = CALL_EXPR_ARG (exp, 2);
29400
29401   tmode = TYPE_MODE (TREE_TYPE (arg0));
29402   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
29403   gcc_assert (VECTOR_MODE_P (tmode));
29404
29405   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
29406   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
29407   elt = get_element_number (TREE_TYPE (arg0), arg2);
29408
29409   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
29410     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
29411
29412   op0 = force_reg (tmode, op0);
29413   op1 = force_reg (mode1, op1);
29414
29415   /* OP0 is the source of these builtin functions and shouldn't be
29416      modified.  Create a copy, use it and return it as target.  */
29417   target = gen_reg_rtx (tmode);
29418   emit_move_insn (target, op0);
29419   ix86_expand_vector_set (true, target, op1, elt);
29420
29421   return target;
29422 }
29423
29424 /* Expand an expression EXP that calls a built-in function,
29425    with result going to TARGET if that's convenient
29426    (and in mode MODE if that's convenient).
29427    SUBTARGET may be used as the target for computing one of EXP's operands.
29428    IGNORE is nonzero if the value is to be ignored.  */
29429
29430 static rtx
29431 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
29432                      enum machine_mode mode ATTRIBUTE_UNUSED,
29433                      int ignore ATTRIBUTE_UNUSED)
29434 {
29435   const struct builtin_description *d;
29436   size_t i;
29437   enum insn_code icode;
29438   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
29439   tree arg0, arg1, arg2, arg3, arg4;
29440   rtx op0, op1, op2, op3, op4, pat;
29441   enum machine_mode mode0, mode1, mode2, mode3, mode4;
29442   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
29443
29444   /* Determine whether the builtin function is available under the current ISA.
29445      Originally the builtin was not created if it wasn't applicable to the
29446      current ISA based on the command line switches.  With function specific
29447      options, we need to check in the context of the function making the call
29448      whether it is supported.  */
29449   if (ix86_builtins_isa[fcode].isa
29450       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
29451     {
29452       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
29453                                        NULL, (enum fpmath_unit) 0, false);
29454
29455       if (!opts)
29456         error ("%qE needs unknown isa option", fndecl);
29457       else
29458         {
29459           gcc_assert (opts != NULL);
29460           error ("%qE needs isa option %s", fndecl, opts);
29461           free (opts);
29462         }
29463       return const0_rtx;
29464     }
29465
29466   switch (fcode)
29467     {
29468     case IX86_BUILTIN_MASKMOVQ:
29469     case IX86_BUILTIN_MASKMOVDQU:
29470       icode = (fcode == IX86_BUILTIN_MASKMOVQ
29471                ? CODE_FOR_mmx_maskmovq
29472                : CODE_FOR_sse2_maskmovdqu);
29473       /* Note the arg order is different from the operand order.  */
29474       arg1 = CALL_EXPR_ARG (exp, 0);
29475       arg2 = CALL_EXPR_ARG (exp, 1);
29476       arg0 = CALL_EXPR_ARG (exp, 2);
29477       op0 = expand_normal (arg0);
29478       op1 = expand_normal (arg1);
29479       op2 = expand_normal (arg2);
29480       mode0 = insn_data[icode].operand[0].mode;
29481       mode1 = insn_data[icode].operand[1].mode;
29482       mode2 = insn_data[icode].operand[2].mode;
29483
29484       if (GET_MODE (op0) != Pmode)
29485         op0 = convert_to_mode (Pmode, op0, 1);
29486       op0 = gen_rtx_MEM (mode1, force_reg (Pmode, op0));
29487
29488       if (!insn_data[icode].operand[0].predicate (op0, mode0))
29489         op0 = copy_to_mode_reg (mode0, op0);
29490       if (!insn_data[icode].operand[1].predicate (op1, mode1))
29491         op1 = copy_to_mode_reg (mode1, op1);
29492       if (!insn_data[icode].operand[2].predicate (op2, mode2))
29493         op2 = copy_to_mode_reg (mode2, op2);
29494       pat = GEN_FCN (icode) (op0, op1, op2);
29495       if (! pat)
29496         return 0;
29497       emit_insn (pat);
29498       return 0;
29499
29500     case IX86_BUILTIN_LDMXCSR:
29501       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
29502       target = assign_386_stack_local (SImode, SLOT_TEMP);
29503       emit_move_insn (target, op0);
29504       emit_insn (gen_sse_ldmxcsr (target));
29505       return 0;
29506
29507     case IX86_BUILTIN_STMXCSR:
29508       target = assign_386_stack_local (SImode, SLOT_TEMP);
29509       emit_insn (gen_sse_stmxcsr (target));
29510       return copy_to_mode_reg (SImode, target);
29511
29512     case IX86_BUILTIN_CLFLUSH:
29513         arg0 = CALL_EXPR_ARG (exp, 0);
29514         op0 = expand_normal (arg0);
29515         icode = CODE_FOR_sse2_clflush;
29516         if (!insn_data[icode].operand[0].predicate (op0, Pmode))
29517           {
29518             if (GET_MODE (op0) != Pmode)
29519               op0 = convert_to_mode (Pmode, op0, 1);
29520             op0 = force_reg (Pmode, op0);
29521           }
29522
29523         emit_insn (gen_sse2_clflush (op0));
29524         return 0;
29525
29526     case IX86_BUILTIN_MONITOR:
29527       arg0 = CALL_EXPR_ARG (exp, 0);
29528       arg1 = CALL_EXPR_ARG (exp, 1);
29529       arg2 = CALL_EXPR_ARG (exp, 2);
29530       op0 = expand_normal (arg0);
29531       op1 = expand_normal (arg1);
29532       op2 = expand_normal (arg2);
29533       if (!REG_P (op0))
29534         {
29535           if (GET_MODE (op0) != Pmode)
29536             op0 = convert_to_mode (Pmode, op0, 1);
29537           op0 = force_reg (Pmode, op0);
29538         }
29539       if (!REG_P (op1))
29540         op1 = copy_to_mode_reg (SImode, op1);
29541       if (!REG_P (op2))
29542         op2 = copy_to_mode_reg (SImode, op2);
29543       emit_insn (ix86_gen_monitor (op0, op1, op2));
29544       return 0;
29545
29546     case IX86_BUILTIN_MWAIT:
29547       arg0 = CALL_EXPR_ARG (exp, 0);
29548       arg1 = CALL_EXPR_ARG (exp, 1);
29549       op0 = expand_normal (arg0);
29550       op1 = expand_normal (arg1);
29551       if (!REG_P (op0))
29552         op0 = copy_to_mode_reg (SImode, op0);
29553       if (!REG_P (op1))
29554         op1 = copy_to_mode_reg (SImode, op1);
29555       emit_insn (gen_sse3_mwait (op0, op1));
29556       return 0;
29557
29558     case IX86_BUILTIN_VEC_INIT_V2SI:
29559     case IX86_BUILTIN_VEC_INIT_V4HI:
29560     case IX86_BUILTIN_VEC_INIT_V8QI:
29561       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
29562
29563     case IX86_BUILTIN_VEC_EXT_V2DF:
29564     case IX86_BUILTIN_VEC_EXT_V2DI:
29565     case IX86_BUILTIN_VEC_EXT_V4SF:
29566     case IX86_BUILTIN_VEC_EXT_V4SI:
29567     case IX86_BUILTIN_VEC_EXT_V8HI:
29568     case IX86_BUILTIN_VEC_EXT_V2SI:
29569     case IX86_BUILTIN_VEC_EXT_V4HI:
29570     case IX86_BUILTIN_VEC_EXT_V16QI:
29571       return ix86_expand_vec_ext_builtin (exp, target);
29572
29573     case IX86_BUILTIN_VEC_SET_V2DI:
29574     case IX86_BUILTIN_VEC_SET_V4SF:
29575     case IX86_BUILTIN_VEC_SET_V4SI:
29576     case IX86_BUILTIN_VEC_SET_V8HI:
29577     case IX86_BUILTIN_VEC_SET_V4HI:
29578     case IX86_BUILTIN_VEC_SET_V16QI:
29579       return ix86_expand_vec_set_builtin (exp);
29580
29581     case IX86_BUILTIN_INFQ:
29582     case IX86_BUILTIN_HUGE_VALQ:
29583       {
29584         REAL_VALUE_TYPE inf;
29585         rtx tmp;
29586
29587         real_inf (&inf);
29588         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
29589
29590         tmp = validize_mem (force_const_mem (mode, tmp));
29591
29592         if (target == 0)
29593           target = gen_reg_rtx (mode);
29594
29595         emit_move_insn (target, tmp);
29596         return target;
29597       }
29598
29599     case IX86_BUILTIN_LLWPCB:
29600       arg0 = CALL_EXPR_ARG (exp, 0);
29601       op0 = expand_normal (arg0);
29602       icode = CODE_FOR_lwp_llwpcb;
29603       if (!insn_data[icode].operand[0].predicate (op0, Pmode))
29604         {
29605           if (GET_MODE (op0) != Pmode)
29606             op0 = convert_to_mode (Pmode, op0, 1);
29607           op0 = force_reg (Pmode, op0);
29608         }
29609       emit_insn (gen_lwp_llwpcb (op0));
29610       return 0;
29611
29612     case IX86_BUILTIN_SLWPCB:
29613       icode = CODE_FOR_lwp_slwpcb;
29614       if (!target
29615           || !insn_data[icode].operand[0].predicate (target, Pmode))
29616         target = gen_reg_rtx (Pmode);
29617       emit_insn (gen_lwp_slwpcb (target));
29618       return target;
29619
29620     case IX86_BUILTIN_BEXTRI32:
29621     case IX86_BUILTIN_BEXTRI64:
29622       arg0 = CALL_EXPR_ARG (exp, 0);
29623       arg1 = CALL_EXPR_ARG (exp, 1);
29624       op0 = expand_normal (arg0);
29625       op1 = expand_normal (arg1);
29626       icode = (fcode == IX86_BUILTIN_BEXTRI32
29627           ? CODE_FOR_tbm_bextri_si
29628           : CODE_FOR_tbm_bextri_di);
29629       if (!CONST_INT_P (op1))
29630         {
29631           error ("last argument must be an immediate");
29632           return const0_rtx;
29633         }
29634       else
29635         {
29636           unsigned char length = (INTVAL (op1) >> 8) & 0xFF;
29637           unsigned char lsb_index = INTVAL (op1) & 0xFF;
29638           op1 = GEN_INT (length);
29639           op2 = GEN_INT (lsb_index);
29640           pat = GEN_FCN (icode) (target, op0, op1, op2);
29641           if (pat)
29642             emit_insn (pat);
29643           return target;
29644         }
29645
29646     case IX86_BUILTIN_RDRAND16_STEP:
29647       icode = CODE_FOR_rdrandhi_1;
29648       mode0 = HImode;
29649       goto rdrand_step;
29650
29651     case IX86_BUILTIN_RDRAND32_STEP:
29652       icode = CODE_FOR_rdrandsi_1;
29653       mode0 = SImode;
29654       goto rdrand_step;
29655
29656     case IX86_BUILTIN_RDRAND64_STEP:
29657       icode = CODE_FOR_rdranddi_1;
29658       mode0 = DImode;
29659
29660 rdrand_step:
29661       op0 = gen_reg_rtx (mode0);
29662       emit_insn (GEN_FCN (icode) (op0));
29663
29664       arg0 = CALL_EXPR_ARG (exp, 0);
29665       op1 = expand_normal (arg0);
29666       if (!address_operand (op1, VOIDmode))
29667         {
29668           op1 = convert_memory_address (Pmode, op1);
29669           op1 = copy_addr_to_reg (op1);
29670         }
29671       emit_move_insn (gen_rtx_MEM (mode0, op1), op0);
29672
29673       op1 = gen_reg_rtx (SImode);
29674       emit_move_insn (op1, CONST1_RTX (SImode));
29675
29676       /* Emit SImode conditional move.  */
29677       if (mode0 == HImode)
29678         {
29679           op2 = gen_reg_rtx (SImode);
29680           emit_insn (gen_zero_extendhisi2 (op2, op0));
29681         }
29682       else if (mode0 == SImode)
29683         op2 = op0;
29684       else
29685         op2 = gen_rtx_SUBREG (SImode, op0, 0);
29686
29687       if (target == 0)
29688         target = gen_reg_rtx (SImode);
29689
29690       pat = gen_rtx_GEU (VOIDmode, gen_rtx_REG (CCCmode, FLAGS_REG),
29691                          const0_rtx);
29692       emit_insn (gen_rtx_SET (VOIDmode, target,
29693                               gen_rtx_IF_THEN_ELSE (SImode, pat, op2, op1)));
29694       return target;
29695
29696     case IX86_BUILTIN_GATHERSIV2DF:
29697       icode = CODE_FOR_avx2_gathersiv2df;
29698       goto gather_gen;
29699     case IX86_BUILTIN_GATHERSIV4DF:
29700       icode = CODE_FOR_avx2_gathersiv4df;
29701       goto gather_gen;
29702     case IX86_BUILTIN_GATHERDIV2DF:
29703       icode = CODE_FOR_avx2_gatherdiv2df;
29704       goto gather_gen;
29705     case IX86_BUILTIN_GATHERDIV4DF:
29706       icode = CODE_FOR_avx2_gatherdiv4df;
29707       goto gather_gen;
29708     case IX86_BUILTIN_GATHERSIV4SF:
29709       icode = CODE_FOR_avx2_gathersiv4sf;
29710       goto gather_gen;
29711     case IX86_BUILTIN_GATHERSIV8SF:
29712       icode = CODE_FOR_avx2_gathersiv8sf;
29713       goto gather_gen;
29714     case IX86_BUILTIN_GATHERDIV4SF:
29715       icode = CODE_FOR_avx2_gatherdiv4sf;
29716       goto gather_gen;
29717     case IX86_BUILTIN_GATHERDIV8SF:
29718       icode = CODE_FOR_avx2_gatherdiv8sf;
29719       goto gather_gen;
29720     case IX86_BUILTIN_GATHERSIV2DI:
29721       icode = CODE_FOR_avx2_gathersiv2di;
29722       goto gather_gen;
29723     case IX86_BUILTIN_GATHERSIV4DI:
29724       icode = CODE_FOR_avx2_gathersiv4di;
29725       goto gather_gen;
29726     case IX86_BUILTIN_GATHERDIV2DI:
29727       icode = CODE_FOR_avx2_gatherdiv2di;
29728       goto gather_gen;
29729     case IX86_BUILTIN_GATHERDIV4DI:
29730       icode = CODE_FOR_avx2_gatherdiv4di;
29731       goto gather_gen;
29732     case IX86_BUILTIN_GATHERSIV4SI:
29733       icode = CODE_FOR_avx2_gathersiv4si;
29734       goto gather_gen;
29735     case IX86_BUILTIN_GATHERSIV8SI:
29736       icode = CODE_FOR_avx2_gathersiv8si;
29737       goto gather_gen;
29738     case IX86_BUILTIN_GATHERDIV4SI:
29739       icode = CODE_FOR_avx2_gatherdiv4si;
29740       goto gather_gen;
29741     case IX86_BUILTIN_GATHERDIV8SI:
29742       icode = CODE_FOR_avx2_gatherdiv8si;
29743       goto gather_gen;
29744     case IX86_BUILTIN_GATHERALTSIV4DF:
29745       icode = CODE_FOR_avx2_gathersiv4df;
29746       goto gather_gen;
29747     case IX86_BUILTIN_GATHERALTDIV8SF:
29748       icode = CODE_FOR_avx2_gatherdiv8sf;
29749       goto gather_gen;
29750     case IX86_BUILTIN_GATHERALTSIV4DI:
29751       icode = CODE_FOR_avx2_gathersiv4di;
29752       goto gather_gen;
29753     case IX86_BUILTIN_GATHERALTDIV8SI:
29754       icode = CODE_FOR_avx2_gatherdiv8si;
29755       goto gather_gen;
29756
29757     gather_gen:
29758       arg0 = CALL_EXPR_ARG (exp, 0);
29759       arg1 = CALL_EXPR_ARG (exp, 1);
29760       arg2 = CALL_EXPR_ARG (exp, 2);
29761       arg3 = CALL_EXPR_ARG (exp, 3);
29762       arg4 = CALL_EXPR_ARG (exp, 4);
29763       op0 = expand_normal (arg0);
29764       op1 = expand_normal (arg1);
29765       op2 = expand_normal (arg2);
29766       op3 = expand_normal (arg3);
29767       op4 = expand_normal (arg4);
29768       /* Note the arg order is different from the operand order.  */
29769       mode0 = insn_data[icode].operand[1].mode;
29770       mode2 = insn_data[icode].operand[3].mode;
29771       mode3 = insn_data[icode].operand[4].mode;
29772       mode4 = insn_data[icode].operand[5].mode;
29773
29774       if (target == NULL_RTX
29775           || GET_MODE (target) != insn_data[icode].operand[0].mode)
29776         subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
29777       else
29778         subtarget = target;
29779
29780       if (fcode == IX86_BUILTIN_GATHERALTSIV4DF
29781           || fcode == IX86_BUILTIN_GATHERALTSIV4DI)
29782         {
29783           rtx half = gen_reg_rtx (V4SImode);
29784           if (!nonimmediate_operand (op2, V8SImode))
29785             op2 = copy_to_mode_reg (V8SImode, op2);
29786           emit_insn (gen_vec_extract_lo_v8si (half, op2));
29787           op2 = half;
29788         }
29789       else if (fcode == IX86_BUILTIN_GATHERALTDIV8SF
29790                || fcode == IX86_BUILTIN_GATHERALTDIV8SI)
29791         {
29792           rtx (*gen) (rtx, rtx);
29793           rtx half = gen_reg_rtx (mode0);
29794           if (mode0 == V4SFmode)
29795             gen = gen_vec_extract_lo_v8sf;
29796           else
29797             gen = gen_vec_extract_lo_v8si;
29798           if (!nonimmediate_operand (op0, GET_MODE (op0)))
29799             op0 = copy_to_mode_reg (GET_MODE (op0), op0);
29800           emit_insn (gen (half, op0));
29801           op0 = half;
29802           if (!nonimmediate_operand (op3, GET_MODE (op3)))
29803             op3 = copy_to_mode_reg (GET_MODE (op3), op3);
29804           emit_insn (gen (half, op3));
29805           op3 = half;
29806         }
29807
29808       /* Force memory operand only with base register here.  But we
29809          don't want to do it on memory operand for other builtin
29810          functions.  */
29811       if (GET_MODE (op1) != Pmode)
29812         op1 = convert_to_mode (Pmode, op1, 1);
29813       op1 = force_reg (Pmode, op1);
29814
29815       if (!insn_data[icode].operand[1].predicate (op0, mode0))
29816         op0 = copy_to_mode_reg (mode0, op0);
29817       if (!insn_data[icode].operand[2].predicate (op1, Pmode))
29818         op1 = copy_to_mode_reg (Pmode, op1);
29819       if (!insn_data[icode].operand[3].predicate (op2, mode2))
29820         op2 = copy_to_mode_reg (mode2, op2);
29821       if (!insn_data[icode].operand[4].predicate (op3, mode3))
29822         op3 = copy_to_mode_reg (mode3, op3);
29823       if (!insn_data[icode].operand[5].predicate (op4, mode4))
29824         {
29825           error ("last argument must be scale 1, 2, 4, 8");
29826           return const0_rtx;
29827         }
29828
29829       /* Optimize.  If mask is known to have all high bits set,
29830          replace op0 with pc_rtx to signal that the instruction
29831          overwrites the whole destination and doesn't use its
29832          previous contents.  */
29833       if (optimize)
29834         {
29835           if (TREE_CODE (arg3) == VECTOR_CST)
29836             {
29837               tree elt;
29838               unsigned int negative = 0;
29839               for (elt = TREE_VECTOR_CST_ELTS (arg3);
29840                    elt; elt = TREE_CHAIN (elt))
29841                 {
29842                   tree cst = TREE_VALUE (elt);
29843                   if (TREE_CODE (cst) == INTEGER_CST
29844                       && tree_int_cst_sign_bit (cst))
29845                     negative++;
29846                   else if (TREE_CODE (cst) == REAL_CST
29847                            && REAL_VALUE_NEGATIVE (TREE_REAL_CST (cst)))
29848                     negative++;
29849                 }
29850               if (negative == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg3)))
29851                 op0 = pc_rtx;
29852             }
29853           else if (TREE_CODE (arg3) == SSA_NAME)
29854             {
29855               /* Recognize also when mask is like:
29856                  __v2df src = _mm_setzero_pd ();
29857                  __v2df mask = _mm_cmpeq_pd (src, src);
29858                  or
29859                  __v8sf src = _mm256_setzero_ps ();
29860                  __v8sf mask = _mm256_cmp_ps (src, src, _CMP_EQ_OQ);
29861                  as that is a cheaper way to load all ones into
29862                  a register than having to load a constant from
29863                  memory.  */
29864               gimple def_stmt = SSA_NAME_DEF_STMT (arg3);
29865               if (is_gimple_call (def_stmt))
29866                 {
29867                   tree fndecl = gimple_call_fndecl (def_stmt);
29868                   if (fndecl
29869                       && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
29870                     switch ((unsigned int) DECL_FUNCTION_CODE (fndecl))
29871                       {
29872                       case IX86_BUILTIN_CMPPD:
29873                       case IX86_BUILTIN_CMPPS:
29874                       case IX86_BUILTIN_CMPPD256:
29875                       case IX86_BUILTIN_CMPPS256:
29876                         if (!integer_zerop (gimple_call_arg (def_stmt, 2)))
29877                           break;
29878                         /* FALLTHRU */
29879                       case IX86_BUILTIN_CMPEQPD:
29880                       case IX86_BUILTIN_CMPEQPS:
29881                         if (initializer_zerop (gimple_call_arg (def_stmt, 0))
29882                             && initializer_zerop (gimple_call_arg (def_stmt,
29883                                                                    1)))
29884                           op0 = pc_rtx;
29885                         break;
29886                       default:
29887                         break;
29888                       }
29889                 }
29890             }
29891         }
29892
29893       pat = GEN_FCN (icode) (subtarget, op0, op1, op2, op3, op4);
29894       if (! pat)
29895         return const0_rtx;
29896       emit_insn (pat);
29897
29898       if (fcode == IX86_BUILTIN_GATHERDIV8SF
29899           || fcode == IX86_BUILTIN_GATHERDIV8SI)
29900         {
29901           enum machine_mode tmode = GET_MODE (subtarget) == V8SFmode
29902                                     ? V4SFmode : V4SImode;
29903           if (target == NULL_RTX)
29904             target = gen_reg_rtx (tmode);
29905           if (tmode == V4SFmode)
29906             emit_insn (gen_vec_extract_lo_v8sf (target, subtarget));
29907           else
29908             emit_insn (gen_vec_extract_lo_v8si (target, subtarget));
29909         }
29910       else
29911         target = subtarget;
29912
29913       return target;
29914
29915     default:
29916       break;
29917     }
29918
29919   for (i = 0, d = bdesc_special_args;
29920        i < ARRAY_SIZE (bdesc_special_args);
29921        i++, d++)
29922     if (d->code == fcode)
29923       return ix86_expand_special_args_builtin (d, exp, target);
29924
29925   for (i = 0, d = bdesc_args;
29926        i < ARRAY_SIZE (bdesc_args);
29927        i++, d++)
29928     if (d->code == fcode)
29929       switch (fcode)
29930         {
29931         case IX86_BUILTIN_FABSQ:
29932         case IX86_BUILTIN_COPYSIGNQ:
29933           if (!TARGET_SSE2)
29934             /* Emit a normal call if SSE2 isn't available.  */
29935             return expand_call (exp, target, ignore);
29936         default:
29937           return ix86_expand_args_builtin (d, exp, target);
29938         }
29939
29940   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
29941     if (d->code == fcode)
29942       return ix86_expand_sse_comi (d, exp, target);
29943
29944   for (i = 0, d = bdesc_pcmpestr;
29945        i < ARRAY_SIZE (bdesc_pcmpestr);
29946        i++, d++)
29947     if (d->code == fcode)
29948       return ix86_expand_sse_pcmpestr (d, exp, target);
29949
29950   for (i = 0, d = bdesc_pcmpistr;
29951        i < ARRAY_SIZE (bdesc_pcmpistr);
29952        i++, d++)
29953     if (d->code == fcode)
29954       return ix86_expand_sse_pcmpistr (d, exp, target);
29955
29956   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
29957     if (d->code == fcode)
29958       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
29959                                             (enum ix86_builtin_func_type)
29960                                             d->flag, d->comparison);
29961
29962   gcc_unreachable ();
29963 }
29964
29965 /* Returns a function decl for a vectorized version of the builtin function
29966    with builtin function code FN and the result vector type TYPE, or NULL_TREE
29967    if it is not available.  */
29968
29969 static tree
29970 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
29971                                   tree type_in)
29972 {
29973   enum machine_mode in_mode, out_mode;
29974   int in_n, out_n;
29975   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
29976
29977   if (TREE_CODE (type_out) != VECTOR_TYPE
29978       || TREE_CODE (type_in) != VECTOR_TYPE
29979       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
29980     return NULL_TREE;
29981
29982   out_mode = TYPE_MODE (TREE_TYPE (type_out));
29983   out_n = TYPE_VECTOR_SUBPARTS (type_out);
29984   in_mode = TYPE_MODE (TREE_TYPE (type_in));
29985   in_n = TYPE_VECTOR_SUBPARTS (type_in);
29986
29987   switch (fn)
29988     {
29989     case BUILT_IN_SQRT:
29990       if (out_mode == DFmode && in_mode == DFmode)
29991         {
29992           if (out_n == 2 && in_n == 2)
29993             return ix86_builtins[IX86_BUILTIN_SQRTPD];
29994           else if (out_n == 4 && in_n == 4)
29995             return ix86_builtins[IX86_BUILTIN_SQRTPD256];
29996         }
29997       break;
29998
29999     case BUILT_IN_SQRTF:
30000       if (out_mode == SFmode && in_mode == SFmode)
30001         {
30002           if (out_n == 4 && in_n == 4)
30003             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
30004           else if (out_n == 8 && in_n == 8)
30005             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR256];
30006         }
30007       break;
30008
30009     case BUILT_IN_IFLOOR:
30010     case BUILT_IN_LFLOOR:
30011     case BUILT_IN_LLFLOOR:
30012       /* The round insn does not trap on denormals.  */
30013       if (flag_trapping_math || !TARGET_ROUND)
30014         break;
30015
30016       if (out_mode == SImode && in_mode == DFmode)
30017         {
30018           if (out_n == 4 && in_n == 2)
30019             return ix86_builtins[IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX];
30020           else if (out_n == 8 && in_n == 4)
30021             return ix86_builtins[IX86_BUILTIN_FLOORPD_VEC_PACK_SFIX256];
30022         }
30023       break;
30024
30025     case BUILT_IN_IFLOORF:
30026     case BUILT_IN_LFLOORF:
30027     case BUILT_IN_LLFLOORF:
30028       /* The round insn does not trap on denormals.  */
30029       if (flag_trapping_math || !TARGET_ROUND)
30030         break;
30031
30032       if (out_mode == SImode && in_mode == SFmode)
30033         {
30034           if (out_n == 4 && in_n == 4)
30035             return ix86_builtins[IX86_BUILTIN_FLOORPS_SFIX];
30036           else if (out_n == 8 && in_n == 8)
30037             return ix86_builtins[IX86_BUILTIN_FLOORPS_SFIX256];
30038         }
30039       break;
30040
30041     case BUILT_IN_ICEIL:
30042     case BUILT_IN_LCEIL:
30043     case BUILT_IN_LLCEIL:
30044       /* The round insn does not trap on denormals.  */
30045       if (flag_trapping_math || !TARGET_ROUND)
30046         break;
30047
30048       if (out_mode == SImode && in_mode == DFmode)
30049         {
30050           if (out_n == 4 && in_n == 2)
30051             return ix86_builtins[IX86_BUILTIN_CEILPD_VEC_PACK_SFIX];
30052           else if (out_n == 8 && in_n == 4)
30053             return ix86_builtins[IX86_BUILTIN_CEILPD_VEC_PACK_SFIX256];
30054         }
30055       break;
30056
30057     case BUILT_IN_ICEILF:
30058     case BUILT_IN_LCEILF:
30059     case BUILT_IN_LLCEILF:
30060       /* The round insn does not trap on denormals.  */
30061       if (flag_trapping_math || !TARGET_ROUND)
30062         break;
30063
30064       if (out_mode == SImode && in_mode == SFmode)
30065         {
30066           if (out_n == 4 && in_n == 4)
30067             return ix86_builtins[IX86_BUILTIN_CEILPS_SFIX];
30068           else if (out_n == 8 && in_n == 8)
30069             return ix86_builtins[IX86_BUILTIN_CEILPS_SFIX256];
30070         }
30071       break;
30072
30073     case BUILT_IN_IRINT:
30074     case BUILT_IN_LRINT:
30075     case BUILT_IN_LLRINT:
30076       if (out_mode == SImode && in_mode == DFmode)
30077         {
30078           if (out_n == 4 && in_n == 2)
30079             return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
30080           else if (out_n == 8 && in_n == 4)
30081             return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX256];
30082         }
30083       break;
30084
30085     case BUILT_IN_IRINTF:
30086     case BUILT_IN_LRINTF:
30087     case BUILT_IN_LLRINTF:
30088       if (out_mode == SImode && in_mode == SFmode)
30089         {
30090           if (out_n == 4 && in_n == 4)
30091             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
30092           else if (out_n == 8 && in_n == 8)
30093             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ256];
30094         }
30095       break;
30096
30097     case BUILT_IN_IROUND:
30098     case BUILT_IN_LROUND:
30099     case BUILT_IN_LLROUND:
30100       /* The round insn does not trap on denormals.  */
30101       if (flag_trapping_math || !TARGET_ROUND)
30102         break;
30103
30104       if (out_mode == SImode && in_mode == DFmode)
30105         {
30106           if (out_n == 4 && in_n == 2)
30107             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX];
30108           else if (out_n == 8 && in_n == 4)
30109             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ_VEC_PACK_SFIX256];
30110         }
30111       break;
30112
30113     case BUILT_IN_IROUNDF:
30114     case BUILT_IN_LROUNDF:
30115     case BUILT_IN_LLROUNDF:
30116       /* The round insn does not trap on denormals.  */
30117       if (flag_trapping_math || !TARGET_ROUND)
30118         break;
30119
30120       if (out_mode == SImode && in_mode == SFmode)
30121         {
30122           if (out_n == 4 && in_n == 4)
30123             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ_SFIX];
30124           else if (out_n == 8 && in_n == 8)
30125             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ_SFIX256];
30126         }
30127       break;
30128
30129     case BUILT_IN_COPYSIGN:
30130       if (out_mode == DFmode && in_mode == DFmode)
30131         {
30132           if (out_n == 2 && in_n == 2)
30133             return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
30134           else if (out_n == 4 && in_n == 4)
30135             return ix86_builtins[IX86_BUILTIN_CPYSGNPD256];
30136         }
30137       break;
30138
30139     case BUILT_IN_COPYSIGNF:
30140       if (out_mode == SFmode && in_mode == SFmode)
30141         {
30142           if (out_n == 4 && in_n == 4)
30143             return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
30144           else if (out_n == 8 && in_n == 8)
30145             return ix86_builtins[IX86_BUILTIN_CPYSGNPS256];
30146         }
30147       break;
30148
30149     case BUILT_IN_FLOOR:
30150       /* The round insn does not trap on denormals.  */
30151       if (flag_trapping_math || !TARGET_ROUND)
30152         break;
30153
30154       if (out_mode == DFmode && in_mode == DFmode)
30155         {
30156           if (out_n == 2 && in_n == 2)
30157             return ix86_builtins[IX86_BUILTIN_FLOORPD];
30158           else if (out_n == 4 && in_n == 4)
30159             return ix86_builtins[IX86_BUILTIN_FLOORPD256];
30160         }
30161       break;
30162
30163     case BUILT_IN_FLOORF:
30164       /* The round insn does not trap on denormals.  */
30165       if (flag_trapping_math || !TARGET_ROUND)
30166         break;
30167
30168       if (out_mode == SFmode && in_mode == SFmode)
30169         {
30170           if (out_n == 4 && in_n == 4)
30171             return ix86_builtins[IX86_BUILTIN_FLOORPS];
30172           else if (out_n == 8 && in_n == 8)
30173             return ix86_builtins[IX86_BUILTIN_FLOORPS256];
30174         }
30175       break;
30176
30177     case BUILT_IN_CEIL:
30178       /* The round insn does not trap on denormals.  */
30179       if (flag_trapping_math || !TARGET_ROUND)
30180         break;
30181
30182       if (out_mode == DFmode && in_mode == DFmode)
30183         {
30184           if (out_n == 2 && in_n == 2)
30185             return ix86_builtins[IX86_BUILTIN_CEILPD];
30186           else if (out_n == 4 && in_n == 4)
30187             return ix86_builtins[IX86_BUILTIN_CEILPD256];
30188         }
30189       break;
30190
30191     case BUILT_IN_CEILF:
30192       /* The round insn does not trap on denormals.  */
30193       if (flag_trapping_math || !TARGET_ROUND)
30194         break;
30195
30196       if (out_mode == SFmode && in_mode == SFmode)
30197         {
30198           if (out_n == 4 && in_n == 4)
30199             return ix86_builtins[IX86_BUILTIN_CEILPS];
30200           else if (out_n == 8 && in_n == 8)
30201             return ix86_builtins[IX86_BUILTIN_CEILPS256];
30202         }
30203       break;
30204
30205     case BUILT_IN_TRUNC:
30206       /* The round insn does not trap on denormals.  */
30207       if (flag_trapping_math || !TARGET_ROUND)
30208         break;
30209
30210       if (out_mode == DFmode && in_mode == DFmode)
30211         {
30212           if (out_n == 2 && in_n == 2)
30213             return ix86_builtins[IX86_BUILTIN_TRUNCPD];
30214           else if (out_n == 4 && in_n == 4)
30215             return ix86_builtins[IX86_BUILTIN_TRUNCPD256];
30216         }
30217       break;
30218
30219     case BUILT_IN_TRUNCF:
30220       /* The round insn does not trap on denormals.  */
30221       if (flag_trapping_math || !TARGET_ROUND)
30222         break;
30223
30224       if (out_mode == SFmode && in_mode == SFmode)
30225         {
30226           if (out_n == 4 && in_n == 4)
30227             return ix86_builtins[IX86_BUILTIN_TRUNCPS];
30228           else if (out_n == 8 && in_n == 8)
30229             return ix86_builtins[IX86_BUILTIN_TRUNCPS256];
30230         }
30231       break;
30232
30233     case BUILT_IN_RINT:
30234       /* The round insn does not trap on denormals.  */
30235       if (flag_trapping_math || !TARGET_ROUND)
30236         break;
30237
30238       if (out_mode == DFmode && in_mode == DFmode)
30239         {
30240           if (out_n == 2 && in_n == 2)
30241             return ix86_builtins[IX86_BUILTIN_RINTPD];
30242           else if (out_n == 4 && in_n == 4)
30243             return ix86_builtins[IX86_BUILTIN_RINTPD256];
30244         }
30245       break;
30246
30247     case BUILT_IN_RINTF:
30248       /* The round insn does not trap on denormals.  */
30249       if (flag_trapping_math || !TARGET_ROUND)
30250         break;
30251
30252       if (out_mode == SFmode && in_mode == SFmode)
30253         {
30254           if (out_n == 4 && in_n == 4)
30255             return ix86_builtins[IX86_BUILTIN_RINTPS];
30256           else if (out_n == 8 && in_n == 8)
30257             return ix86_builtins[IX86_BUILTIN_RINTPS256];
30258         }
30259       break;
30260
30261     case BUILT_IN_ROUND:
30262       /* The round insn does not trap on denormals.  */
30263       if (flag_trapping_math || !TARGET_ROUND)
30264         break;
30265
30266       if (out_mode == DFmode && in_mode == DFmode)
30267         {
30268           if (out_n == 2 && in_n == 2)
30269             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ];
30270           else if (out_n == 4 && in_n == 4)
30271             return ix86_builtins[IX86_BUILTIN_ROUNDPD_AZ256];
30272         }
30273       break;
30274
30275     case BUILT_IN_ROUNDF:
30276       /* The round insn does not trap on denormals.  */
30277       if (flag_trapping_math || !TARGET_ROUND)
30278         break;
30279
30280       if (out_mode == SFmode && in_mode == SFmode)
30281         {
30282           if (out_n == 4 && in_n == 4)
30283             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ];
30284           else if (out_n == 8 && in_n == 8)
30285             return ix86_builtins[IX86_BUILTIN_ROUNDPS_AZ256];
30286         }
30287       break;
30288
30289     case BUILT_IN_FMA:
30290       if (out_mode == DFmode && in_mode == DFmode)
30291         {
30292           if (out_n == 2 && in_n == 2)
30293             return ix86_builtins[IX86_BUILTIN_VFMADDPD];
30294           if (out_n == 4 && in_n == 4)
30295             return ix86_builtins[IX86_BUILTIN_VFMADDPD256];
30296         }
30297       break;
30298
30299     case BUILT_IN_FMAF:
30300       if (out_mode == SFmode && in_mode == SFmode)
30301         {
30302           if (out_n == 4 && in_n == 4)
30303             return ix86_builtins[IX86_BUILTIN_VFMADDPS];
30304           if (out_n == 8 && in_n == 8)
30305             return ix86_builtins[IX86_BUILTIN_VFMADDPS256];
30306         }
30307       break;
30308
30309     default:
30310       break;
30311     }
30312
30313   /* Dispatch to a handler for a vectorization library.  */
30314   if (ix86_veclib_handler)
30315     return ix86_veclib_handler ((enum built_in_function) fn, type_out,
30316                                 type_in);
30317
30318   return NULL_TREE;
30319 }
30320
30321 /* Handler for an SVML-style interface to
30322    a library with vectorized intrinsics.  */
30323
30324 static tree
30325 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
30326 {
30327   char name[20];
30328   tree fntype, new_fndecl, args;
30329   unsigned arity;
30330   const char *bname;
30331   enum machine_mode el_mode, in_mode;
30332   int n, in_n;
30333
30334   /* The SVML is suitable for unsafe math only.  */
30335   if (!flag_unsafe_math_optimizations)
30336     return NULL_TREE;
30337
30338   el_mode = TYPE_MODE (TREE_TYPE (type_out));
30339   n = TYPE_VECTOR_SUBPARTS (type_out);
30340   in_mode = TYPE_MODE (TREE_TYPE (type_in));
30341   in_n = TYPE_VECTOR_SUBPARTS (type_in);
30342   if (el_mode != in_mode
30343       || n != in_n)
30344     return NULL_TREE;
30345
30346   switch (fn)
30347     {
30348     case BUILT_IN_EXP:
30349     case BUILT_IN_LOG:
30350     case BUILT_IN_LOG10:
30351     case BUILT_IN_POW:
30352     case BUILT_IN_TANH:
30353     case BUILT_IN_TAN:
30354     case BUILT_IN_ATAN:
30355     case BUILT_IN_ATAN2:
30356     case BUILT_IN_ATANH:
30357     case BUILT_IN_CBRT:
30358     case BUILT_IN_SINH:
30359     case BUILT_IN_SIN:
30360     case BUILT_IN_ASINH:
30361     case BUILT_IN_ASIN:
30362     case BUILT_IN_COSH:
30363     case BUILT_IN_COS:
30364     case BUILT_IN_ACOSH:
30365     case BUILT_IN_ACOS:
30366       if (el_mode != DFmode || n != 2)
30367         return NULL_TREE;
30368       break;
30369
30370     case BUILT_IN_EXPF:
30371     case BUILT_IN_LOGF:
30372     case BUILT_IN_LOG10F:
30373     case BUILT_IN_POWF:
30374     case BUILT_IN_TANHF:
30375     case BUILT_IN_TANF:
30376     case BUILT_IN_ATANF:
30377     case BUILT_IN_ATAN2F:
30378     case BUILT_IN_ATANHF:
30379     case BUILT_IN_CBRTF:
30380     case BUILT_IN_SINHF:
30381     case BUILT_IN_SINF:
30382     case BUILT_IN_ASINHF:
30383     case BUILT_IN_ASINF:
30384     case BUILT_IN_COSHF:
30385     case BUILT_IN_COSF:
30386     case BUILT_IN_ACOSHF:
30387     case BUILT_IN_ACOSF:
30388       if (el_mode != SFmode || n != 4)
30389         return NULL_TREE;
30390       break;
30391
30392     default:
30393       return NULL_TREE;
30394     }
30395
30396   bname = IDENTIFIER_POINTER (DECL_NAME (builtin_decl_implicit (fn)));
30397
30398   if (fn == BUILT_IN_LOGF)
30399     strcpy (name, "vmlsLn4");
30400   else if (fn == BUILT_IN_LOG)
30401     strcpy (name, "vmldLn2");
30402   else if (n == 4)
30403     {
30404       sprintf (name, "vmls%s", bname+10);
30405       name[strlen (name)-1] = '4';
30406     }
30407   else
30408     sprintf (name, "vmld%s2", bname+10);
30409
30410   /* Convert to uppercase. */
30411   name[4] &= ~0x20;
30412
30413   arity = 0;
30414   for (args = DECL_ARGUMENTS (builtin_decl_implicit (fn));
30415        args;
30416        args = TREE_CHAIN (args))
30417     arity++;
30418
30419   if (arity == 1)
30420     fntype = build_function_type_list (type_out, type_in, NULL);
30421   else
30422     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
30423
30424   /* Build a function declaration for the vectorized function.  */
30425   new_fndecl = build_decl (BUILTINS_LOCATION,
30426                            FUNCTION_DECL, get_identifier (name), fntype);
30427   TREE_PUBLIC (new_fndecl) = 1;
30428   DECL_EXTERNAL (new_fndecl) = 1;
30429   DECL_IS_NOVOPS (new_fndecl) = 1;
30430   TREE_READONLY (new_fndecl) = 1;
30431
30432   return new_fndecl;
30433 }
30434
30435 /* Handler for an ACML-style interface to
30436    a library with vectorized intrinsics.  */
30437
30438 static tree
30439 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
30440 {
30441   char name[20] = "__vr.._";
30442   tree fntype, new_fndecl, args;
30443   unsigned arity;
30444   const char *bname;
30445   enum machine_mode el_mode, in_mode;
30446   int n, in_n;
30447
30448   /* The ACML is 64bits only and suitable for unsafe math only as
30449      it does not correctly support parts of IEEE with the required
30450      precision such as denormals.  */
30451   if (!TARGET_64BIT
30452       || !flag_unsafe_math_optimizations)
30453     return NULL_TREE;
30454
30455   el_mode = TYPE_MODE (TREE_TYPE (type_out));
30456   n = TYPE_VECTOR_SUBPARTS (type_out);
30457   in_mode = TYPE_MODE (TREE_TYPE (type_in));
30458   in_n = TYPE_VECTOR_SUBPARTS (type_in);
30459   if (el_mode != in_mode
30460       || n != in_n)
30461     return NULL_TREE;
30462
30463   switch (fn)
30464     {
30465     case BUILT_IN_SIN:
30466     case BUILT_IN_COS:
30467     case BUILT_IN_EXP:
30468     case BUILT_IN_LOG:
30469     case BUILT_IN_LOG2:
30470     case BUILT_IN_LOG10:
30471       name[4] = 'd';
30472       name[5] = '2';
30473       if (el_mode != DFmode
30474           || n != 2)
30475         return NULL_TREE;
30476       break;
30477
30478     case BUILT_IN_SINF:
30479     case BUILT_IN_COSF:
30480     case BUILT_IN_EXPF:
30481     case BUILT_IN_POWF:
30482     case BUILT_IN_LOGF:
30483     case BUILT_IN_LOG2F:
30484     case BUILT_IN_LOG10F:
30485       name[4] = 's';
30486       name[5] = '4';
30487       if (el_mode != SFmode
30488           || n != 4)
30489         return NULL_TREE;
30490       break;
30491
30492     default:
30493       return NULL_TREE;
30494     }
30495
30496   bname = IDENTIFIER_POINTER (DECL_NAME (builtin_decl_implicit (fn)));
30497   sprintf (name + 7, "%s", bname+10);
30498
30499   arity = 0;
30500   for (args = DECL_ARGUMENTS (builtin_decl_implicit (fn));
30501        args;
30502        args = TREE_CHAIN (args))
30503     arity++;
30504
30505   if (arity == 1)
30506     fntype = build_function_type_list (type_out, type_in, NULL);
30507   else
30508     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
30509
30510   /* Build a function declaration for the vectorized function.  */
30511   new_fndecl = build_decl (BUILTINS_LOCATION,
30512                            FUNCTION_DECL, get_identifier (name), fntype);
30513   TREE_PUBLIC (new_fndecl) = 1;
30514   DECL_EXTERNAL (new_fndecl) = 1;
30515   DECL_IS_NOVOPS (new_fndecl) = 1;
30516   TREE_READONLY (new_fndecl) = 1;
30517
30518   return new_fndecl;
30519 }
30520
30521 /* Returns a decl of a function that implements gather load with
30522    memory type MEM_VECTYPE and index type INDEX_VECTYPE and SCALE.
30523    Return NULL_TREE if it is not available.  */
30524
30525 static tree
30526 ix86_vectorize_builtin_gather (const_tree mem_vectype,
30527                                const_tree index_type, int scale)
30528 {
30529   bool si;
30530   enum ix86_builtins code;
30531
30532   if (! TARGET_AVX2)
30533     return NULL_TREE;
30534
30535   if ((TREE_CODE (index_type) != INTEGER_TYPE
30536        && !POINTER_TYPE_P (index_type))
30537       || (TYPE_MODE (index_type) != SImode
30538           && TYPE_MODE (index_type) != DImode))
30539     return NULL_TREE;
30540
30541   if (TYPE_PRECISION (index_type) > POINTER_SIZE)
30542     return NULL_TREE;
30543
30544   /* v*gather* insn sign extends index to pointer mode.  */
30545   if (TYPE_PRECISION (index_type) < POINTER_SIZE
30546       && TYPE_UNSIGNED (index_type))
30547     return NULL_TREE;
30548
30549   if (scale <= 0
30550       || scale > 8
30551       || (scale & (scale - 1)) != 0)
30552     return NULL_TREE;
30553
30554   si = TYPE_MODE (index_type) == SImode;
30555   switch (TYPE_MODE (mem_vectype))
30556     {
30557     case V2DFmode:
30558       code = si ? IX86_BUILTIN_GATHERSIV2DF : IX86_BUILTIN_GATHERDIV2DF;
30559       break;
30560     case V4DFmode:
30561       code = si ? IX86_BUILTIN_GATHERALTSIV4DF : IX86_BUILTIN_GATHERDIV4DF;
30562       break;
30563     case V2DImode:
30564       code = si ? IX86_BUILTIN_GATHERSIV2DI : IX86_BUILTIN_GATHERDIV2DI;
30565       break;
30566     case V4DImode:
30567       code = si ? IX86_BUILTIN_GATHERALTSIV4DI : IX86_BUILTIN_GATHERDIV4DI;
30568       break;
30569     case V4SFmode:
30570       code = si ? IX86_BUILTIN_GATHERSIV4SF : IX86_BUILTIN_GATHERDIV4SF;
30571       break;
30572     case V8SFmode:
30573       code = si ? IX86_BUILTIN_GATHERSIV8SF : IX86_BUILTIN_GATHERALTDIV8SF;
30574       break;
30575     case V4SImode:
30576       code = si ? IX86_BUILTIN_GATHERSIV4SI : IX86_BUILTIN_GATHERDIV4SI;
30577       break;
30578     case V8SImode:
30579       code = si ? IX86_BUILTIN_GATHERSIV8SI : IX86_BUILTIN_GATHERALTDIV8SI;
30580       break;
30581     default:
30582       return NULL_TREE;
30583     }
30584
30585   return ix86_builtins[code];
30586 }
30587
30588 /* Returns a code for a target-specific builtin that implements
30589    reciprocal of the function, or NULL_TREE if not available.  */
30590
30591 static tree
30592 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
30593                          bool sqrt ATTRIBUTE_UNUSED)
30594 {
30595   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
30596          && flag_finite_math_only && !flag_trapping_math
30597          && flag_unsafe_math_optimizations))
30598     return NULL_TREE;
30599
30600   if (md_fn)
30601     /* Machine dependent builtins.  */
30602     switch (fn)
30603       {
30604         /* Vectorized version of sqrt to rsqrt conversion.  */
30605       case IX86_BUILTIN_SQRTPS_NR:
30606         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
30607
30608       case IX86_BUILTIN_SQRTPS_NR256:
30609         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR256];
30610
30611       default:
30612         return NULL_TREE;
30613       }
30614   else
30615     /* Normal builtins.  */
30616     switch (fn)
30617       {
30618         /* Sqrt to rsqrt conversion.  */
30619       case BUILT_IN_SQRTF:
30620         return ix86_builtins[IX86_BUILTIN_RSQRTF];
30621
30622       default:
30623         return NULL_TREE;
30624       }
30625 }
30626 \f
30627 /* Helper for avx_vpermilps256_operand et al.  This is also used by
30628    the expansion functions to turn the parallel back into a mask.
30629    The return value is 0 for no match and the imm8+1 for a match.  */
30630
30631 int
30632 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
30633 {
30634   unsigned i, nelt = GET_MODE_NUNITS (mode);
30635   unsigned mask = 0;
30636   unsigned char ipar[8];
30637
30638   if (XVECLEN (par, 0) != (int) nelt)
30639     return 0;
30640
30641   /* Validate that all of the elements are constants, and not totally
30642      out of range.  Copy the data into an integral array to make the
30643      subsequent checks easier.  */
30644   for (i = 0; i < nelt; ++i)
30645     {
30646       rtx er = XVECEXP (par, 0, i);
30647       unsigned HOST_WIDE_INT ei;
30648
30649       if (!CONST_INT_P (er))
30650         return 0;
30651       ei = INTVAL (er);
30652       if (ei >= nelt)
30653         return 0;
30654       ipar[i] = ei;
30655     }
30656
30657   switch (mode)
30658     {
30659     case V4DFmode:
30660       /* In the 256-bit DFmode case, we can only move elements within
30661          a 128-bit lane.  */
30662       for (i = 0; i < 2; ++i)
30663         {
30664           if (ipar[i] >= 2)
30665             return 0;
30666           mask |= ipar[i] << i;
30667         }
30668       for (i = 2; i < 4; ++i)
30669         {
30670           if (ipar[i] < 2)
30671             return 0;
30672           mask |= (ipar[i] - 2) << i;
30673         }
30674       break;
30675
30676     case V8SFmode:
30677       /* In the 256-bit SFmode case, we have full freedom of movement
30678          within the low 128-bit lane, but the high 128-bit lane must
30679          mirror the exact same pattern.  */
30680       for (i = 0; i < 4; ++i)
30681         if (ipar[i] + 4 != ipar[i + 4])
30682           return 0;
30683       nelt = 4;
30684       /* FALLTHRU */
30685
30686     case V2DFmode:
30687     case V4SFmode:
30688       /* In the 128-bit case, we've full freedom in the placement of
30689          the elements from the source operand.  */
30690       for (i = 0; i < nelt; ++i)
30691         mask |= ipar[i] << (i * (nelt / 2));
30692       break;
30693
30694     default:
30695       gcc_unreachable ();
30696     }
30697
30698   /* Make sure success has a non-zero value by adding one.  */
30699   return mask + 1;
30700 }
30701
30702 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
30703    the expansion functions to turn the parallel back into a mask.
30704    The return value is 0 for no match and the imm8+1 for a match.  */
30705
30706 int
30707 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
30708 {
30709   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
30710   unsigned mask = 0;
30711   unsigned char ipar[8];
30712
30713   if (XVECLEN (par, 0) != (int) nelt)
30714     return 0;
30715
30716   /* Validate that all of the elements are constants, and not totally
30717      out of range.  Copy the data into an integral array to make the
30718      subsequent checks easier.  */
30719   for (i = 0; i < nelt; ++i)
30720     {
30721       rtx er = XVECEXP (par, 0, i);
30722       unsigned HOST_WIDE_INT ei;
30723
30724       if (!CONST_INT_P (er))
30725         return 0;
30726       ei = INTVAL (er);
30727       if (ei >= 2 * nelt)
30728         return 0;
30729       ipar[i] = ei;
30730     }
30731
30732   /* Validate that the halves of the permute are halves.  */
30733   for (i = 0; i < nelt2 - 1; ++i)
30734     if (ipar[i] + 1 != ipar[i + 1])
30735       return 0;
30736   for (i = nelt2; i < nelt - 1; ++i)
30737     if (ipar[i] + 1 != ipar[i + 1])
30738       return 0;
30739
30740   /* Reconstruct the mask.  */
30741   for (i = 0; i < 2; ++i)
30742     {
30743       unsigned e = ipar[i * nelt2];
30744       if (e % nelt2)
30745         return 0;
30746       e /= nelt2;
30747       mask |= e << (i * 4);
30748     }
30749
30750   /* Make sure success has a non-zero value by adding one.  */
30751   return mask + 1;
30752 }
30753 \f
30754 /* Store OPERAND to the memory after reload is completed.  This means
30755    that we can't easily use assign_stack_local.  */
30756 rtx
30757 ix86_force_to_memory (enum machine_mode mode, rtx operand)
30758 {
30759   rtx result;
30760
30761   gcc_assert (reload_completed);
30762   if (ix86_using_red_zone ())
30763     {
30764       result = gen_rtx_MEM (mode,
30765                             gen_rtx_PLUS (Pmode,
30766                                           stack_pointer_rtx,
30767                                           GEN_INT (-RED_ZONE_SIZE)));
30768       emit_move_insn (result, operand);
30769     }
30770   else if (TARGET_64BIT)
30771     {
30772       switch (mode)
30773         {
30774         case HImode:
30775         case SImode:
30776           operand = gen_lowpart (DImode, operand);
30777           /* FALLTHRU */
30778         case DImode:
30779           emit_insn (
30780                       gen_rtx_SET (VOIDmode,
30781                                    gen_rtx_MEM (DImode,
30782                                                 gen_rtx_PRE_DEC (DImode,
30783                                                         stack_pointer_rtx)),
30784                                    operand));
30785           break;
30786         default:
30787           gcc_unreachable ();
30788         }
30789       result = gen_rtx_MEM (mode, stack_pointer_rtx);
30790     }
30791   else
30792     {
30793       switch (mode)
30794         {
30795         case DImode:
30796           {
30797             rtx operands[2];
30798             split_double_mode (mode, &operand, 1, operands, operands + 1);
30799             emit_insn (
30800                         gen_rtx_SET (VOIDmode,
30801                                      gen_rtx_MEM (SImode,
30802                                                   gen_rtx_PRE_DEC (Pmode,
30803                                                         stack_pointer_rtx)),
30804                                      operands[1]));
30805             emit_insn (
30806                         gen_rtx_SET (VOIDmode,
30807                                      gen_rtx_MEM (SImode,
30808                                                   gen_rtx_PRE_DEC (Pmode,
30809                                                         stack_pointer_rtx)),
30810                                      operands[0]));
30811           }
30812           break;
30813         case HImode:
30814           /* Store HImodes as SImodes.  */
30815           operand = gen_lowpart (SImode, operand);
30816           /* FALLTHRU */
30817         case SImode:
30818           emit_insn (
30819                       gen_rtx_SET (VOIDmode,
30820                                    gen_rtx_MEM (GET_MODE (operand),
30821                                                 gen_rtx_PRE_DEC (SImode,
30822                                                         stack_pointer_rtx)),
30823                                    operand));
30824           break;
30825         default:
30826           gcc_unreachable ();
30827         }
30828       result = gen_rtx_MEM (mode, stack_pointer_rtx);
30829     }
30830   return result;
30831 }
30832
30833 /* Free operand from the memory.  */
30834 void
30835 ix86_free_from_memory (enum machine_mode mode)
30836 {
30837   if (!ix86_using_red_zone ())
30838     {
30839       int size;
30840
30841       if (mode == DImode || TARGET_64BIT)
30842         size = 8;
30843       else
30844         size = 4;
30845       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
30846          to pop or add instruction if registers are available.  */
30847       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
30848                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
30849                                             GEN_INT (size))));
30850     }
30851 }
30852
30853 /* Implement TARGET_PREFERRED_RELOAD_CLASS.
30854
30855    Put float CONST_DOUBLE in the constant pool instead of fp regs.
30856    QImode must go into class Q_REGS.
30857    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
30858    movdf to do mem-to-mem moves through integer regs.  */
30859
30860 static reg_class_t
30861 ix86_preferred_reload_class (rtx x, reg_class_t regclass)
30862 {
30863   enum machine_mode mode = GET_MODE (x);
30864
30865   /* We're only allowed to return a subclass of CLASS.  Many of the
30866      following checks fail for NO_REGS, so eliminate that early.  */
30867   if (regclass == NO_REGS)
30868     return NO_REGS;
30869
30870   /* All classes can load zeros.  */
30871   if (x == CONST0_RTX (mode))
30872     return regclass;
30873
30874   /* Force constants into memory if we are loading a (nonzero) constant into
30875      an MMX or SSE register.  This is because there are no MMX/SSE instructions
30876      to load from a constant.  */
30877   if (CONSTANT_P (x)
30878       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
30879     return NO_REGS;
30880
30881   /* Prefer SSE regs only, if we can use them for math.  */
30882   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
30883     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
30884
30885   /* Floating-point constants need more complex checks.  */
30886   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
30887     {
30888       /* General regs can load everything.  */
30889       if (reg_class_subset_p (regclass, GENERAL_REGS))
30890         return regclass;
30891
30892       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
30893          zero above.  We only want to wind up preferring 80387 registers if
30894          we plan on doing computation with them.  */
30895       if (TARGET_80387
30896           && standard_80387_constant_p (x) > 0)
30897         {
30898           /* Limit class to non-sse.  */
30899           if (regclass == FLOAT_SSE_REGS)
30900             return FLOAT_REGS;
30901           if (regclass == FP_TOP_SSE_REGS)
30902             return FP_TOP_REG;
30903           if (regclass == FP_SECOND_SSE_REGS)
30904             return FP_SECOND_REG;
30905           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
30906             return regclass;
30907         }
30908
30909       return NO_REGS;
30910     }
30911
30912   /* Generally when we see PLUS here, it's the function invariant
30913      (plus soft-fp const_int).  Which can only be computed into general
30914      regs.  */
30915   if (GET_CODE (x) == PLUS)
30916     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
30917
30918   /* QImode constants are easy to load, but non-constant QImode data
30919      must go into Q_REGS.  */
30920   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
30921     {
30922       if (reg_class_subset_p (regclass, Q_REGS))
30923         return regclass;
30924       if (reg_class_subset_p (Q_REGS, regclass))
30925         return Q_REGS;
30926       return NO_REGS;
30927     }
30928
30929   return regclass;
30930 }
30931
30932 /* Discourage putting floating-point values in SSE registers unless
30933    SSE math is being used, and likewise for the 387 registers.  */
30934 static reg_class_t
30935 ix86_preferred_output_reload_class (rtx x, reg_class_t regclass)
30936 {
30937   enum machine_mode mode = GET_MODE (x);
30938
30939   /* Restrict the output reload class to the register bank that we are doing
30940      math on.  If we would like not to return a subset of CLASS, reject this
30941      alternative: if reload cannot do this, it will still use its choice.  */
30942   mode = GET_MODE (x);
30943   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
30944     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
30945
30946   if (X87_FLOAT_MODE_P (mode))
30947     {
30948       if (regclass == FP_TOP_SSE_REGS)
30949         return FP_TOP_REG;
30950       else if (regclass == FP_SECOND_SSE_REGS)
30951         return FP_SECOND_REG;
30952       else
30953         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
30954     }
30955
30956   return regclass;
30957 }
30958
30959 static reg_class_t
30960 ix86_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
30961                        enum machine_mode mode, secondary_reload_info *sri)
30962 {
30963   /* Double-word spills from general registers to non-offsettable memory
30964      references (zero-extended addresses) require special handling.  */
30965   if (TARGET_64BIT
30966       && MEM_P (x)
30967       && GET_MODE_SIZE (mode) > UNITS_PER_WORD
30968       && rclass == GENERAL_REGS
30969       && !offsettable_memref_p (x))
30970     {
30971       sri->icode = (in_p
30972                     ? CODE_FOR_reload_noff_load
30973                     : CODE_FOR_reload_noff_store);
30974       /* Add the cost of moving address to a temporary.  */
30975       sri->extra_cost = 1;
30976
30977       return NO_REGS;
30978     }
30979
30980   /* QImode spills from non-QI registers require
30981      intermediate register on 32bit targets.  */
30982   if (!TARGET_64BIT
30983       && !in_p && mode == QImode
30984       && (rclass == GENERAL_REGS
30985           || rclass == LEGACY_REGS
30986           || rclass == INDEX_REGS))
30987     {
30988       int regno;
30989
30990       if (REG_P (x))
30991         regno = REGNO (x);
30992       else
30993         regno = -1;
30994
30995       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
30996         regno = true_regnum (x);
30997
30998       /* Return Q_REGS if the operand is in memory.  */
30999       if (regno == -1)
31000         return Q_REGS;
31001     }
31002
31003   /* This condition handles corner case where an expression involving
31004      pointers gets vectorized.  We're trying to use the address of a
31005      stack slot as a vector initializer.
31006
31007      (set (reg:V2DI 74 [ vect_cst_.2 ])
31008           (vec_duplicate:V2DI (reg/f:DI 20 frame)))
31009
31010      Eventually frame gets turned into sp+offset like this:
31011
31012      (set (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
31013           (vec_duplicate:V2DI (plus:DI (reg/f:DI 7 sp)
31014                                        (const_int 392 [0x188]))))
31015
31016      That later gets turned into:
31017
31018      (set (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
31019           (vec_duplicate:V2DI (plus:DI (reg/f:DI 7 sp)
31020             (mem/u/c/i:DI (symbol_ref/u:DI ("*.LC0") [flags 0x2]) [0 S8 A64]))))
31021
31022      We'll have the following reload recorded:
31023
31024      Reload 0: reload_in (DI) =
31025            (plus:DI (reg/f:DI 7 sp)
31026             (mem/u/c/i:DI (symbol_ref/u:DI ("*.LC0") [flags 0x2]) [0 S8 A64]))
31027      reload_out (V2DI) = (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
31028      SSE_REGS, RELOAD_OTHER (opnum = 0), can't combine
31029      reload_in_reg: (plus:DI (reg/f:DI 7 sp) (const_int 392 [0x188]))
31030      reload_out_reg: (reg:V2DI 21 xmm0 [orig:74 vect_cst_.2 ] [74])
31031      reload_reg_rtx: (reg:V2DI 22 xmm1)
31032
31033      Which isn't going to work since SSE instructions can't handle scalar
31034      additions.  Returning GENERAL_REGS forces the addition into integer
31035      register and reload can handle subsequent reloads without problems.  */
31036
31037   if (in_p && GET_CODE (x) == PLUS
31038       && SSE_CLASS_P (rclass)
31039       && SCALAR_INT_MODE_P (mode))
31040     return GENERAL_REGS;
31041
31042   return NO_REGS;
31043 }
31044
31045 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.  */
31046
31047 static bool
31048 ix86_class_likely_spilled_p (reg_class_t rclass)
31049 {
31050   switch (rclass)
31051     {
31052       case AREG:
31053       case DREG:
31054       case CREG:
31055       case BREG:
31056       case AD_REGS:
31057       case SIREG:
31058       case DIREG:
31059       case SSE_FIRST_REG:
31060       case FP_TOP_REG:
31061       case FP_SECOND_REG:
31062         return true;
31063
31064       default:
31065         break;
31066     }
31067
31068   return false;
31069 }
31070
31071 /* If we are copying between general and FP registers, we need a memory
31072    location. The same is true for SSE and MMX registers.
31073
31074    To optimize register_move_cost performance, allow inline variant.
31075
31076    The macro can't work reliably when one of the CLASSES is class containing
31077    registers from multiple units (SSE, MMX, integer).  We avoid this by never
31078    combining those units in single alternative in the machine description.
31079    Ensure that this constraint holds to avoid unexpected surprises.
31080
31081    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
31082    enforce these sanity checks.  */
31083
31084 static inline bool
31085 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
31086                                 enum machine_mode mode, int strict)
31087 {
31088   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
31089       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
31090       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
31091       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
31092       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
31093       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
31094     {
31095       gcc_assert (!strict);
31096       return true;
31097     }
31098
31099   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
31100     return true;
31101
31102   /* ??? This is a lie.  We do have moves between mmx/general, and for
31103      mmx/sse2.  But by saying we need secondary memory we discourage the
31104      register allocator from using the mmx registers unless needed.  */
31105   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
31106     return true;
31107
31108   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
31109     {
31110       /* SSE1 doesn't have any direct moves from other classes.  */
31111       if (!TARGET_SSE2)
31112         return true;
31113
31114       /* If the target says that inter-unit moves are more expensive
31115          than moving through memory, then don't generate them.  */
31116       if (!TARGET_INTER_UNIT_MOVES)
31117         return true;
31118
31119       /* Between SSE and general, we have moves no larger than word size.  */
31120       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
31121         return true;
31122     }
31123
31124   return false;
31125 }
31126
31127 bool
31128 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
31129                               enum machine_mode mode, int strict)
31130 {
31131   return inline_secondary_memory_needed (class1, class2, mode, strict);
31132 }
31133
31134 /* Implement the TARGET_CLASS_MAX_NREGS hook.
31135
31136    On the 80386, this is the size of MODE in words,
31137    except in the FP regs, where a single reg is always enough.  */
31138
31139 static unsigned char
31140 ix86_class_max_nregs (reg_class_t rclass, enum machine_mode mode)
31141 {
31142   if (MAYBE_INTEGER_CLASS_P (rclass))
31143     {
31144       if (mode == XFmode)
31145         return (TARGET_64BIT ? 2 : 3);
31146       else if (mode == XCmode)
31147         return (TARGET_64BIT ? 4 : 6);
31148       else
31149         return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
31150     }
31151   else
31152     {
31153       if (COMPLEX_MODE_P (mode))
31154         return 2;
31155       else
31156         return 1;
31157     }
31158 }
31159
31160 /* Return true if the registers in CLASS cannot represent the change from
31161    modes FROM to TO.  */
31162
31163 bool
31164 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
31165                                enum reg_class regclass)
31166 {
31167   if (from == to)
31168     return false;
31169
31170   /* x87 registers can't do subreg at all, as all values are reformatted
31171      to extended precision.  */
31172   if (MAYBE_FLOAT_CLASS_P (regclass))
31173     return true;
31174
31175   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
31176     {
31177       /* Vector registers do not support QI or HImode loads.  If we don't
31178          disallow a change to these modes, reload will assume it's ok to
31179          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
31180          the vec_dupv4hi pattern.  */
31181       if (GET_MODE_SIZE (from) < 4)
31182         return true;
31183
31184       /* Vector registers do not support subreg with nonzero offsets, which
31185          are otherwise valid for integer registers.  Since we can't see
31186          whether we have a nonzero offset from here, prohibit all
31187          nonparadoxical subregs changing size.  */
31188       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
31189         return true;
31190     }
31191
31192   return false;
31193 }
31194
31195 /* Return the cost of moving data of mode M between a
31196    register and memory.  A value of 2 is the default; this cost is
31197    relative to those in `REGISTER_MOVE_COST'.
31198
31199    This function is used extensively by register_move_cost that is used to
31200    build tables at startup.  Make it inline in this case.
31201    When IN is 2, return maximum of in and out move cost.
31202
31203    If moving between registers and memory is more expensive than
31204    between two registers, you should define this macro to express the
31205    relative cost.
31206
31207    Model also increased moving costs of QImode registers in non
31208    Q_REGS classes.
31209  */
31210 static inline int
31211 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
31212                          int in)
31213 {
31214   int cost;
31215   if (FLOAT_CLASS_P (regclass))
31216     {
31217       int index;
31218       switch (mode)
31219         {
31220           case SFmode:
31221             index = 0;
31222             break;
31223           case DFmode:
31224             index = 1;
31225             break;
31226           case XFmode:
31227             index = 2;
31228             break;
31229           default:
31230             return 100;
31231         }
31232       if (in == 2)
31233         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
31234       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
31235     }
31236   if (SSE_CLASS_P (regclass))
31237     {
31238       int index;
31239       switch (GET_MODE_SIZE (mode))
31240         {
31241           case 4:
31242             index = 0;
31243             break;
31244           case 8:
31245             index = 1;
31246             break;
31247           case 16:
31248             index = 2;
31249             break;
31250           default:
31251             return 100;
31252         }
31253       if (in == 2)
31254         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
31255       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
31256     }
31257   if (MMX_CLASS_P (regclass))
31258     {
31259       int index;
31260       switch (GET_MODE_SIZE (mode))
31261         {
31262           case 4:
31263             index = 0;
31264             break;
31265           case 8:
31266             index = 1;
31267             break;
31268           default:
31269             return 100;
31270         }
31271       if (in)
31272         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
31273       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
31274     }
31275   switch (GET_MODE_SIZE (mode))
31276     {
31277       case 1:
31278         if (Q_CLASS_P (regclass) || TARGET_64BIT)
31279           {
31280             if (!in)
31281               return ix86_cost->int_store[0];
31282             if (TARGET_PARTIAL_REG_DEPENDENCY
31283                 && optimize_function_for_speed_p (cfun))
31284               cost = ix86_cost->movzbl_load;
31285             else
31286               cost = ix86_cost->int_load[0];
31287             if (in == 2)
31288               return MAX (cost, ix86_cost->int_store[0]);
31289             return cost;
31290           }
31291         else
31292           {
31293            if (in == 2)
31294              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
31295            if (in)
31296              return ix86_cost->movzbl_load;
31297            else
31298              return ix86_cost->int_store[0] + 4;
31299           }
31300         break;
31301       case 2:
31302         if (in == 2)
31303           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
31304         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
31305       default:
31306         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
31307         if (mode == TFmode)
31308           mode = XFmode;
31309         if (in == 2)
31310           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
31311         else if (in)
31312           cost = ix86_cost->int_load[2];
31313         else
31314           cost = ix86_cost->int_store[2];
31315         return (cost * (((int) GET_MODE_SIZE (mode)
31316                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
31317     }
31318 }
31319
31320 static int
31321 ix86_memory_move_cost (enum machine_mode mode, reg_class_t regclass,
31322                        bool in)
31323 {
31324   return inline_memory_move_cost (mode, (enum reg_class) regclass, in ? 1 : 0);
31325 }
31326
31327
31328 /* Return the cost of moving data from a register in class CLASS1 to
31329    one in class CLASS2.
31330
31331    It is not required that the cost always equal 2 when FROM is the same as TO;
31332    on some machines it is expensive to move between registers if they are not
31333    general registers.  */
31334
31335 static int
31336 ix86_register_move_cost (enum machine_mode mode, reg_class_t class1_i,
31337                          reg_class_t class2_i)
31338 {
31339   enum reg_class class1 = (enum reg_class) class1_i;
31340   enum reg_class class2 = (enum reg_class) class2_i;
31341
31342   /* In case we require secondary memory, compute cost of the store followed
31343      by load.  In order to avoid bad register allocation choices, we need
31344      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
31345
31346   if (inline_secondary_memory_needed (class1, class2, mode, 0))
31347     {
31348       int cost = 1;
31349
31350       cost += inline_memory_move_cost (mode, class1, 2);
31351       cost += inline_memory_move_cost (mode, class2, 2);
31352
31353       /* In case of copying from general_purpose_register we may emit multiple
31354          stores followed by single load causing memory size mismatch stall.
31355          Count this as arbitrarily high cost of 20.  */
31356       if (targetm.class_max_nregs (class1, mode)
31357           > targetm.class_max_nregs (class2, mode))
31358         cost += 20;
31359
31360       /* In the case of FP/MMX moves, the registers actually overlap, and we
31361          have to switch modes in order to treat them differently.  */
31362       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
31363           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
31364         cost += 20;
31365
31366       return cost;
31367     }
31368
31369   /* Moves between SSE/MMX and integer unit are expensive.  */
31370   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
31371       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
31372
31373     /* ??? By keeping returned value relatively high, we limit the number
31374        of moves between integer and MMX/SSE registers for all targets.
31375        Additionally, high value prevents problem with x86_modes_tieable_p(),
31376        where integer modes in MMX/SSE registers are not tieable
31377        because of missing QImode and HImode moves to, from or between
31378        MMX/SSE registers.  */
31379     return MAX (8, ix86_cost->mmxsse_to_integer);
31380
31381   if (MAYBE_FLOAT_CLASS_P (class1))
31382     return ix86_cost->fp_move;
31383   if (MAYBE_SSE_CLASS_P (class1))
31384     return ix86_cost->sse_move;
31385   if (MAYBE_MMX_CLASS_P (class1))
31386     return ix86_cost->mmx_move;
31387   return 2;
31388 }
31389
31390 /* Return TRUE if hard register REGNO can hold a value of machine-mode
31391    MODE.  */
31392
31393 bool
31394 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
31395 {
31396   /* Flags and only flags can only hold CCmode values.  */
31397   if (CC_REGNO_P (regno))
31398     return GET_MODE_CLASS (mode) == MODE_CC;
31399   if (GET_MODE_CLASS (mode) == MODE_CC
31400       || GET_MODE_CLASS (mode) == MODE_RANDOM
31401       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
31402     return false;
31403   if (FP_REGNO_P (regno))
31404     return VALID_FP_MODE_P (mode);
31405   if (SSE_REGNO_P (regno))
31406     {
31407       /* We implement the move patterns for all vector modes into and
31408          out of SSE registers, even when no operation instructions
31409          are available.  OImode move is available only when AVX is
31410          enabled.  */
31411       return ((TARGET_AVX && mode == OImode)
31412               || VALID_AVX256_REG_MODE (mode)
31413               || VALID_SSE_REG_MODE (mode)
31414               || VALID_SSE2_REG_MODE (mode)
31415               || VALID_MMX_REG_MODE (mode)
31416               || VALID_MMX_REG_MODE_3DNOW (mode));
31417     }
31418   if (MMX_REGNO_P (regno))
31419     {
31420       /* We implement the move patterns for 3DNOW modes even in MMX mode,
31421          so if the register is available at all, then we can move data of
31422          the given mode into or out of it.  */
31423       return (VALID_MMX_REG_MODE (mode)
31424               || VALID_MMX_REG_MODE_3DNOW (mode));
31425     }
31426
31427   if (mode == QImode)
31428     {
31429       /* Take care for QImode values - they can be in non-QI regs,
31430          but then they do cause partial register stalls.  */
31431       if (regno <= BX_REG || TARGET_64BIT)
31432         return true;
31433       if (!TARGET_PARTIAL_REG_STALL)
31434         return true;
31435       return !can_create_pseudo_p ();
31436     }
31437   /* We handle both integer and floats in the general purpose registers.  */
31438   else if (VALID_INT_MODE_P (mode))
31439     return true;
31440   else if (VALID_FP_MODE_P (mode))
31441     return true;
31442   else if (VALID_DFP_MODE_P (mode))
31443     return true;
31444   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
31445      on to use that value in smaller contexts, this can easily force a
31446      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
31447      supporting DImode, allow it.  */
31448   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
31449     return true;
31450
31451   return false;
31452 }
31453
31454 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
31455    tieable integer mode.  */
31456
31457 static bool
31458 ix86_tieable_integer_mode_p (enum machine_mode mode)
31459 {
31460   switch (mode)
31461     {
31462     case HImode:
31463     case SImode:
31464       return true;
31465
31466     case QImode:
31467       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
31468
31469     case DImode:
31470       return TARGET_64BIT;
31471
31472     default:
31473       return false;
31474     }
31475 }
31476
31477 /* Return true if MODE1 is accessible in a register that can hold MODE2
31478    without copying.  That is, all register classes that can hold MODE2
31479    can also hold MODE1.  */
31480
31481 bool
31482 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
31483 {
31484   if (mode1 == mode2)
31485     return true;
31486
31487   if (ix86_tieable_integer_mode_p (mode1)
31488       && ix86_tieable_integer_mode_p (mode2))
31489     return true;
31490
31491   /* MODE2 being XFmode implies fp stack or general regs, which means we
31492      can tie any smaller floating point modes to it.  Note that we do not
31493      tie this with TFmode.  */
31494   if (mode2 == XFmode)
31495     return mode1 == SFmode || mode1 == DFmode;
31496
31497   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
31498      that we can tie it with SFmode.  */
31499   if (mode2 == DFmode)
31500     return mode1 == SFmode;
31501
31502   /* If MODE2 is only appropriate for an SSE register, then tie with
31503      any other mode acceptable to SSE registers.  */
31504   if (GET_MODE_SIZE (mode2) == 16
31505       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
31506     return (GET_MODE_SIZE (mode1) == 16
31507             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
31508
31509   /* If MODE2 is appropriate for an MMX register, then tie
31510      with any other mode acceptable to MMX registers.  */
31511   if (GET_MODE_SIZE (mode2) == 8
31512       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
31513     return (GET_MODE_SIZE (mode1) == 8
31514             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
31515
31516   return false;
31517 }
31518
31519 /* Compute a (partial) cost for rtx X.  Return true if the complete
31520    cost has been computed, and false if subexpressions should be
31521    scanned.  In either case, *TOTAL contains the cost result.  */
31522
31523 static bool
31524 ix86_rtx_costs (rtx x, int code, int outer_code_i, int opno, int *total,
31525                 bool speed)
31526 {
31527   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
31528   enum machine_mode mode = GET_MODE (x);
31529   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
31530
31531   switch (code)
31532     {
31533     case CONST_INT:
31534     case CONST:
31535     case LABEL_REF:
31536     case SYMBOL_REF:
31537       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
31538         *total = 3;
31539       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
31540         *total = 2;
31541       else if (flag_pic && SYMBOLIC_CONST (x)
31542                && (!TARGET_64BIT
31543                    || (!GET_CODE (x) != LABEL_REF
31544                        && (GET_CODE (x) != SYMBOL_REF
31545                            || !SYMBOL_REF_LOCAL_P (x)))))
31546         *total = 1;
31547       else
31548         *total = 0;
31549       return true;
31550
31551     case CONST_DOUBLE:
31552       if (mode == VOIDmode)
31553         *total = 0;
31554       else
31555         switch (standard_80387_constant_p (x))
31556           {
31557           case 1: /* 0.0 */
31558             *total = 1;
31559             break;
31560           default: /* Other constants */
31561             *total = 2;
31562             break;
31563           case 0:
31564           case -1:
31565             /* Start with (MEM (SYMBOL_REF)), since that's where
31566                it'll probably end up.  Add a penalty for size.  */
31567             *total = (COSTS_N_INSNS (1)
31568                       + (flag_pic != 0 && !TARGET_64BIT)
31569                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
31570             break;
31571           }
31572       return true;
31573
31574     case ZERO_EXTEND:
31575       /* The zero extensions is often completely free on x86_64, so make
31576          it as cheap as possible.  */
31577       if (TARGET_64BIT && mode == DImode
31578           && GET_MODE (XEXP (x, 0)) == SImode)
31579         *total = 1;
31580       else if (TARGET_ZERO_EXTEND_WITH_AND)
31581         *total = cost->add;
31582       else
31583         *total = cost->movzx;
31584       return false;
31585
31586     case SIGN_EXTEND:
31587       *total = cost->movsx;
31588       return false;
31589
31590     case ASHIFT:
31591       if (CONST_INT_P (XEXP (x, 1))
31592           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
31593         {
31594           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
31595           if (value == 1)
31596             {
31597               *total = cost->add;
31598               return false;
31599             }
31600           if ((value == 2 || value == 3)
31601               && cost->lea <= cost->shift_const)
31602             {
31603               *total = cost->lea;
31604               return false;
31605             }
31606         }
31607       /* FALLTHRU */
31608
31609     case ROTATE:
31610     case ASHIFTRT:
31611     case LSHIFTRT:
31612     case ROTATERT:
31613       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
31614         {
31615           if (CONST_INT_P (XEXP (x, 1)))
31616             {
31617               if (INTVAL (XEXP (x, 1)) > 32)
31618                 *total = cost->shift_const + COSTS_N_INSNS (2);
31619               else
31620                 *total = cost->shift_const * 2;
31621             }
31622           else
31623             {
31624               if (GET_CODE (XEXP (x, 1)) == AND)
31625                 *total = cost->shift_var * 2;
31626               else
31627                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
31628             }
31629         }
31630       else
31631         {
31632           if (CONST_INT_P (XEXP (x, 1)))
31633             *total = cost->shift_const;
31634           else if (GET_CODE (XEXP (x, 1)) == SUBREG
31635                    && GET_CODE (XEXP (XEXP (x, 1), 0)) == AND)
31636             {
31637               /* Return the cost after shift-and truncation.  */
31638               *total = cost->shift_var;
31639               return true;
31640             }
31641           else
31642             *total = cost->shift_var;
31643         }
31644       return false;
31645
31646     case FMA:
31647       {
31648         rtx sub;
31649
31650         gcc_assert (FLOAT_MODE_P (mode));
31651         gcc_assert (TARGET_FMA || TARGET_FMA4);
31652
31653         /* ??? SSE scalar/vector cost should be used here.  */
31654         /* ??? Bald assumption that fma has the same cost as fmul.  */
31655         *total = cost->fmul;
31656         *total += rtx_cost (XEXP (x, 1), FMA, 1, speed);
31657
31658         /* Negate in op0 or op2 is free: FMS, FNMA, FNMS.  */
31659         sub = XEXP (x, 0);
31660         if (GET_CODE (sub) == NEG)
31661           sub = XEXP (sub, 0);
31662         *total += rtx_cost (sub, FMA, 0, speed);
31663
31664         sub = XEXP (x, 2);
31665         if (GET_CODE (sub) == NEG)
31666           sub = XEXP (sub, 0);
31667         *total += rtx_cost (sub, FMA, 2, speed);
31668         return true;
31669       }
31670
31671     case MULT:
31672       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31673         {
31674           /* ??? SSE scalar cost should be used here.  */
31675           *total = cost->fmul;
31676           return false;
31677         }
31678       else if (X87_FLOAT_MODE_P (mode))
31679         {
31680           *total = cost->fmul;
31681           return false;
31682         }
31683       else if (FLOAT_MODE_P (mode))
31684         {
31685           /* ??? SSE vector cost should be used here.  */
31686           *total = cost->fmul;
31687           return false;
31688         }
31689       else
31690         {
31691           rtx op0 = XEXP (x, 0);
31692           rtx op1 = XEXP (x, 1);
31693           int nbits;
31694           if (CONST_INT_P (XEXP (x, 1)))
31695             {
31696               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
31697               for (nbits = 0; value != 0; value &= value - 1)
31698                 nbits++;
31699             }
31700           else
31701             /* This is arbitrary.  */
31702             nbits = 7;
31703
31704           /* Compute costs correctly for widening multiplication.  */
31705           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
31706               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
31707                  == GET_MODE_SIZE (mode))
31708             {
31709               int is_mulwiden = 0;
31710               enum machine_mode inner_mode = GET_MODE (op0);
31711
31712               if (GET_CODE (op0) == GET_CODE (op1))
31713                 is_mulwiden = 1, op1 = XEXP (op1, 0);
31714               else if (CONST_INT_P (op1))
31715                 {
31716                   if (GET_CODE (op0) == SIGN_EXTEND)
31717                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
31718                                   == INTVAL (op1);
31719                   else
31720                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
31721                 }
31722
31723               if (is_mulwiden)
31724                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
31725             }
31726
31727           *total = (cost->mult_init[MODE_INDEX (mode)]
31728                     + nbits * cost->mult_bit
31729                     + rtx_cost (op0, outer_code, opno, speed)
31730                     + rtx_cost (op1, outer_code, opno, speed));
31731
31732           return true;
31733         }
31734
31735     case DIV:
31736     case UDIV:
31737     case MOD:
31738     case UMOD:
31739       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31740         /* ??? SSE cost should be used here.  */
31741         *total = cost->fdiv;
31742       else if (X87_FLOAT_MODE_P (mode))
31743         *total = cost->fdiv;
31744       else if (FLOAT_MODE_P (mode))
31745         /* ??? SSE vector cost should be used here.  */
31746         *total = cost->fdiv;
31747       else
31748         *total = cost->divide[MODE_INDEX (mode)];
31749       return false;
31750
31751     case PLUS:
31752       if (GET_MODE_CLASS (mode) == MODE_INT
31753                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
31754         {
31755           if (GET_CODE (XEXP (x, 0)) == PLUS
31756               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
31757               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
31758               && CONSTANT_P (XEXP (x, 1)))
31759             {
31760               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
31761               if (val == 2 || val == 4 || val == 8)
31762                 {
31763                   *total = cost->lea;
31764                   *total += rtx_cost (XEXP (XEXP (x, 0), 1),
31765                                       outer_code, opno, speed);
31766                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
31767                                       outer_code, opno, speed);
31768                   *total += rtx_cost (XEXP (x, 1), outer_code, opno, speed);
31769                   return true;
31770                 }
31771             }
31772           else if (GET_CODE (XEXP (x, 0)) == MULT
31773                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
31774             {
31775               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
31776               if (val == 2 || val == 4 || val == 8)
31777                 {
31778                   *total = cost->lea;
31779                   *total += rtx_cost (XEXP (XEXP (x, 0), 0),
31780                                       outer_code, opno, speed);
31781                   *total += rtx_cost (XEXP (x, 1), outer_code, opno, speed);
31782                   return true;
31783                 }
31784             }
31785           else if (GET_CODE (XEXP (x, 0)) == PLUS)
31786             {
31787               *total = cost->lea;
31788               *total += rtx_cost (XEXP (XEXP (x, 0), 0),
31789                                   outer_code, opno, speed);
31790               *total += rtx_cost (XEXP (XEXP (x, 0), 1),
31791                                   outer_code, opno, speed);
31792               *total += rtx_cost (XEXP (x, 1), outer_code, opno, speed);
31793               return true;
31794             }
31795         }
31796       /* FALLTHRU */
31797
31798     case MINUS:
31799       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31800         {
31801           /* ??? SSE cost should be used here.  */
31802           *total = cost->fadd;
31803           return false;
31804         }
31805       else if (X87_FLOAT_MODE_P (mode))
31806         {
31807           *total = cost->fadd;
31808           return false;
31809         }
31810       else if (FLOAT_MODE_P (mode))
31811         {
31812           /* ??? SSE vector cost should be used here.  */
31813           *total = cost->fadd;
31814           return false;
31815         }
31816       /* FALLTHRU */
31817
31818     case AND:
31819     case IOR:
31820     case XOR:
31821       if (!TARGET_64BIT && mode == DImode)
31822         {
31823           *total = (cost->add * 2
31824                     + (rtx_cost (XEXP (x, 0), outer_code, opno, speed)
31825                        << (GET_MODE (XEXP (x, 0)) != DImode))
31826                     + (rtx_cost (XEXP (x, 1), outer_code, opno, speed)
31827                        << (GET_MODE (XEXP (x, 1)) != DImode)));
31828           return true;
31829         }
31830       /* FALLTHRU */
31831
31832     case NEG:
31833       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31834         {
31835           /* ??? SSE cost should be used here.  */
31836           *total = cost->fchs;
31837           return false;
31838         }
31839       else if (X87_FLOAT_MODE_P (mode))
31840         {
31841           *total = cost->fchs;
31842           return false;
31843         }
31844       else if (FLOAT_MODE_P (mode))
31845         {
31846           /* ??? SSE vector cost should be used here.  */
31847           *total = cost->fchs;
31848           return false;
31849         }
31850       /* FALLTHRU */
31851
31852     case NOT:
31853       if (!TARGET_64BIT && mode == DImode)
31854         *total = cost->add * 2;
31855       else
31856         *total = cost->add;
31857       return false;
31858
31859     case COMPARE:
31860       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
31861           && XEXP (XEXP (x, 0), 1) == const1_rtx
31862           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
31863           && XEXP (x, 1) == const0_rtx)
31864         {
31865           /* This kind of construct is implemented using test[bwl].
31866              Treat it as if we had an AND.  */
31867           *total = (cost->add
31868                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, opno, speed)
31869                     + rtx_cost (const1_rtx, outer_code, opno, speed));
31870           return true;
31871         }
31872       return false;
31873
31874     case FLOAT_EXTEND:
31875       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
31876         *total = 0;
31877       return false;
31878
31879     case ABS:
31880       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31881         /* ??? SSE cost should be used here.  */
31882         *total = cost->fabs;
31883       else if (X87_FLOAT_MODE_P (mode))
31884         *total = cost->fabs;
31885       else if (FLOAT_MODE_P (mode))
31886         /* ??? SSE vector cost should be used here.  */
31887         *total = cost->fabs;
31888       return false;
31889
31890     case SQRT:
31891       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
31892         /* ??? SSE cost should be used here.  */
31893         *total = cost->fsqrt;
31894       else if (X87_FLOAT_MODE_P (mode))
31895         *total = cost->fsqrt;
31896       else if (FLOAT_MODE_P (mode))
31897         /* ??? SSE vector cost should be used here.  */
31898         *total = cost->fsqrt;
31899       return false;
31900
31901     case UNSPEC:
31902       if (XINT (x, 1) == UNSPEC_TP)
31903         *total = 0;
31904       return false;
31905
31906     case VEC_SELECT:
31907     case VEC_CONCAT:
31908     case VEC_MERGE:
31909     case VEC_DUPLICATE:
31910       /* ??? Assume all of these vector manipulation patterns are
31911          recognizable.  In which case they all pretty much have the
31912          same cost.  */
31913      *total = COSTS_N_INSNS (1);
31914      return true;
31915
31916     default:
31917       return false;
31918     }
31919 }
31920
31921 #if TARGET_MACHO
31922
31923 static int current_machopic_label_num;
31924
31925 /* Given a symbol name and its associated stub, write out the
31926    definition of the stub.  */
31927
31928 void
31929 machopic_output_stub (FILE *file, const char *symb, const char *stub)
31930 {
31931   unsigned int length;
31932   char *binder_name, *symbol_name, lazy_ptr_name[32];
31933   int label = ++current_machopic_label_num;
31934
31935   /* For 64-bit we shouldn't get here.  */
31936   gcc_assert (!TARGET_64BIT);
31937
31938   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
31939   symb = targetm.strip_name_encoding (symb);
31940
31941   length = strlen (stub);
31942   binder_name = XALLOCAVEC (char, length + 32);
31943   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
31944
31945   length = strlen (symb);
31946   symbol_name = XALLOCAVEC (char, length + 32);
31947   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
31948
31949   sprintf (lazy_ptr_name, "L%d$lz", label);
31950
31951   if (MACHOPIC_ATT_STUB)
31952     switch_to_section (darwin_sections[machopic_picsymbol_stub3_section]);
31953   else if (MACHOPIC_PURE)
31954     switch_to_section (darwin_sections[machopic_picsymbol_stub2_section]);
31955   else
31956     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
31957
31958   fprintf (file, "%s:\n", stub);
31959   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
31960
31961   if (MACHOPIC_ATT_STUB)
31962     {
31963       fprintf (file, "\thlt ; hlt ; hlt ; hlt ; hlt\n");
31964     }
31965   else if (MACHOPIC_PURE)
31966     {
31967       /* PIC stub.  */
31968       /* 25-byte PIC stub using "CALL get_pc_thunk".  */
31969       rtx tmp = gen_rtx_REG (SImode, 2 /* ECX */);
31970       output_set_got (tmp, NULL_RTX);   /* "CALL ___<cpu>.get_pc_thunk.cx".  */
31971       fprintf (file, "LPC$%d:\tmovl\t%s-LPC$%d(%%ecx),%%ecx\n",
31972                label, lazy_ptr_name, label);
31973       fprintf (file, "\tjmp\t*%%ecx\n");
31974     }
31975   else
31976     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
31977
31978   /* The AT&T-style ("self-modifying") stub is not lazily bound, thus
31979      it needs no stub-binding-helper.  */
31980   if (MACHOPIC_ATT_STUB)
31981     return;
31982
31983   fprintf (file, "%s:\n", binder_name);
31984
31985   if (MACHOPIC_PURE)
31986     {
31987       fprintf (file, "\tlea\t%s-%s(%%ecx),%%ecx\n", lazy_ptr_name, binder_name);
31988       fprintf (file, "\tpushl\t%%ecx\n");
31989     }
31990   else
31991     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
31992
31993   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
31994
31995   /* N.B. Keep the correspondence of these
31996      'symbol_ptr/symbol_ptr2/symbol_ptr3' sections consistent with the
31997      old-pic/new-pic/non-pic stubs; altering this will break
31998      compatibility with existing dylibs.  */
31999   if (MACHOPIC_PURE)
32000     {
32001       /* 25-byte PIC stub using "CALL get_pc_thunk".  */
32002       switch_to_section (darwin_sections[machopic_lazy_symbol_ptr2_section]);
32003     }
32004   else
32005     /* 16-byte -mdynamic-no-pic stub.  */
32006     switch_to_section(darwin_sections[machopic_lazy_symbol_ptr3_section]);
32007
32008   fprintf (file, "%s:\n", lazy_ptr_name);
32009   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
32010   fprintf (file, ASM_LONG "%s\n", binder_name);
32011 }
32012 #endif /* TARGET_MACHO */
32013
32014 /* Order the registers for register allocator.  */
32015
32016 void
32017 x86_order_regs_for_local_alloc (void)
32018 {
32019    int pos = 0;
32020    int i;
32021
32022    /* First allocate the local general purpose registers.  */
32023    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
32024      if (GENERAL_REGNO_P (i) && call_used_regs[i])
32025         reg_alloc_order [pos++] = i;
32026
32027    /* Global general purpose registers.  */
32028    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
32029      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
32030         reg_alloc_order [pos++] = i;
32031
32032    /* x87 registers come first in case we are doing FP math
32033       using them.  */
32034    if (!TARGET_SSE_MATH)
32035      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
32036        reg_alloc_order [pos++] = i;
32037
32038    /* SSE registers.  */
32039    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
32040      reg_alloc_order [pos++] = i;
32041    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
32042      reg_alloc_order [pos++] = i;
32043
32044    /* x87 registers.  */
32045    if (TARGET_SSE_MATH)
32046      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
32047        reg_alloc_order [pos++] = i;
32048
32049    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
32050      reg_alloc_order [pos++] = i;
32051
32052    /* Initialize the rest of array as we do not allocate some registers
32053       at all.  */
32054    while (pos < FIRST_PSEUDO_REGISTER)
32055      reg_alloc_order [pos++] = 0;
32056 }
32057
32058 /* Handle a "callee_pop_aggregate_return" attribute; arguments as
32059    in struct attribute_spec handler.  */
32060 static tree
32061 ix86_handle_callee_pop_aggregate_return (tree *node, tree name,
32062                                               tree args,
32063                                               int flags ATTRIBUTE_UNUSED,
32064                                               bool *no_add_attrs)
32065 {
32066   if (TREE_CODE (*node) != FUNCTION_TYPE
32067       && TREE_CODE (*node) != METHOD_TYPE
32068       && TREE_CODE (*node) != FIELD_DECL
32069       && TREE_CODE (*node) != TYPE_DECL)
32070     {
32071       warning (OPT_Wattributes, "%qE attribute only applies to functions",
32072                name);
32073       *no_add_attrs = true;
32074       return NULL_TREE;
32075     }
32076   if (TARGET_64BIT)
32077     {
32078       warning (OPT_Wattributes, "%qE attribute only available for 32-bit",
32079                name);
32080       *no_add_attrs = true;
32081       return NULL_TREE;
32082     }
32083   if (is_attribute_p ("callee_pop_aggregate_return", name))
32084     {
32085       tree cst;
32086
32087       cst = TREE_VALUE (args);
32088       if (TREE_CODE (cst) != INTEGER_CST)
32089         {
32090           warning (OPT_Wattributes,
32091                    "%qE attribute requires an integer constant argument",
32092                    name);
32093           *no_add_attrs = true;
32094         }
32095       else if (compare_tree_int (cst, 0) != 0
32096                && compare_tree_int (cst, 1) != 0)
32097         {
32098           warning (OPT_Wattributes,
32099                    "argument to %qE attribute is neither zero, nor one",
32100                    name);
32101           *no_add_attrs = true;
32102         }
32103
32104       return NULL_TREE;
32105     }
32106
32107   return NULL_TREE;
32108 }
32109
32110 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
32111    struct attribute_spec.handler.  */
32112 static tree
32113 ix86_handle_abi_attribute (tree *node, tree name,
32114                               tree args ATTRIBUTE_UNUSED,
32115                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
32116 {
32117   if (TREE_CODE (*node) != FUNCTION_TYPE
32118       && TREE_CODE (*node) != METHOD_TYPE
32119       && TREE_CODE (*node) != FIELD_DECL
32120       && TREE_CODE (*node) != TYPE_DECL)
32121     {
32122       warning (OPT_Wattributes, "%qE attribute only applies to functions",
32123                name);
32124       *no_add_attrs = true;
32125       return NULL_TREE;
32126     }
32127
32128   /* Can combine regparm with all attributes but fastcall.  */
32129   if (is_attribute_p ("ms_abi", name))
32130     {
32131       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
32132         {
32133           error ("ms_abi and sysv_abi attributes are not compatible");
32134         }
32135
32136       return NULL_TREE;
32137     }
32138   else if (is_attribute_p ("sysv_abi", name))
32139     {
32140       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
32141         {
32142           error ("ms_abi and sysv_abi attributes are not compatible");
32143         }
32144
32145       return NULL_TREE;
32146     }
32147
32148   return NULL_TREE;
32149 }
32150
32151 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
32152    struct attribute_spec.handler.  */
32153 static tree
32154 ix86_handle_struct_attribute (tree *node, tree name,
32155                               tree args ATTRIBUTE_UNUSED,
32156                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
32157 {
32158   tree *type = NULL;
32159   if (DECL_P (*node))
32160     {
32161       if (TREE_CODE (*node) == TYPE_DECL)
32162         type = &TREE_TYPE (*node);
32163     }
32164   else
32165     type = node;
32166
32167   if (!(type && RECORD_OR_UNION_TYPE_P (*type)))
32168     {
32169       warning (OPT_Wattributes, "%qE attribute ignored",
32170                name);
32171       *no_add_attrs = true;
32172     }
32173
32174   else if ((is_attribute_p ("ms_struct", name)
32175             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
32176            || ((is_attribute_p ("gcc_struct", name)
32177                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
32178     {
32179       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
32180                name);
32181       *no_add_attrs = true;
32182     }
32183
32184   return NULL_TREE;
32185 }
32186
32187 static tree
32188 ix86_handle_fndecl_attribute (tree *node, tree name,
32189                               tree args ATTRIBUTE_UNUSED,
32190                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
32191 {
32192   if (TREE_CODE (*node) != FUNCTION_DECL)
32193     {
32194       warning (OPT_Wattributes, "%qE attribute only applies to functions",
32195                name);
32196       *no_add_attrs = true;
32197     }
32198   return NULL_TREE;
32199 }
32200
32201 static bool
32202 ix86_ms_bitfield_layout_p (const_tree record_type)
32203 {
32204   return ((TARGET_MS_BITFIELD_LAYOUT
32205            && !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
32206           || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type)));
32207 }
32208
32209 /* Returns an expression indicating where the this parameter is
32210    located on entry to the FUNCTION.  */
32211
32212 static rtx
32213 x86_this_parameter (tree function)
32214 {
32215   tree type = TREE_TYPE (function);
32216   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
32217   int nregs;
32218
32219   if (TARGET_64BIT)
32220     {
32221       const int *parm_regs;
32222
32223       if (ix86_function_type_abi (type) == MS_ABI)
32224         parm_regs = x86_64_ms_abi_int_parameter_registers;
32225       else
32226         parm_regs = x86_64_int_parameter_registers;
32227       return gen_rtx_REG (DImode, parm_regs[aggr]);
32228     }
32229
32230   nregs = ix86_function_regparm (type, function);
32231
32232   if (nregs > 0 && !stdarg_p (type))
32233     {
32234       int regno;
32235       unsigned int ccvt = ix86_get_callcvt (type);
32236
32237       if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
32238         regno = aggr ? DX_REG : CX_REG;
32239       else if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
32240         {
32241           regno = CX_REG;
32242           if (aggr)
32243             return gen_rtx_MEM (SImode,
32244                                 plus_constant (stack_pointer_rtx, 4));
32245         }
32246       else
32247         {
32248           regno = AX_REG;
32249           if (aggr)
32250             {
32251               regno = DX_REG;
32252               if (nregs == 1)
32253                 return gen_rtx_MEM (SImode,
32254                                     plus_constant (stack_pointer_rtx, 4));
32255             }
32256         }
32257       return gen_rtx_REG (SImode, regno);
32258     }
32259
32260   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
32261 }
32262
32263 /* Determine whether x86_output_mi_thunk can succeed.  */
32264
32265 static bool
32266 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
32267                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
32268                          HOST_WIDE_INT vcall_offset, const_tree function)
32269 {
32270   /* 64-bit can handle anything.  */
32271   if (TARGET_64BIT)
32272     return true;
32273
32274   /* For 32-bit, everything's fine if we have one free register.  */
32275   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
32276     return true;
32277
32278   /* Need a free register for vcall_offset.  */
32279   if (vcall_offset)
32280     return false;
32281
32282   /* Need a free register for GOT references.  */
32283   if (flag_pic && !targetm.binds_local_p (function))
32284     return false;
32285
32286   /* Otherwise ok.  */
32287   return true;
32288 }
32289
32290 /* Output the assembler code for a thunk function.  THUNK_DECL is the
32291    declaration for the thunk function itself, FUNCTION is the decl for
32292    the target function.  DELTA is an immediate constant offset to be
32293    added to THIS.  If VCALL_OFFSET is nonzero, the word at
32294    *(*this + vcall_offset) should be added to THIS.  */
32295
32296 static void
32297 x86_output_mi_thunk (FILE *file,
32298                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
32299                      HOST_WIDE_INT vcall_offset, tree function)
32300 {
32301   rtx this_param = x86_this_parameter (function);
32302   rtx this_reg, tmp, fnaddr;
32303   unsigned int tmp_regno;
32304
32305   if (TARGET_64BIT)
32306     tmp_regno = R10_REG;
32307   else
32308     {
32309       unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (function));
32310       if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
32311         tmp_regno = AX_REG;
32312       else if ((ccvt & IX86_CALLCVT_THISCALL) != 0)
32313         tmp_regno = DX_REG;
32314       else
32315         tmp_regno = CX_REG;
32316     }
32317
32318   emit_note (NOTE_INSN_PROLOGUE_END);
32319
32320   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
32321      pull it in now and let DELTA benefit.  */
32322   if (REG_P (this_param))
32323     this_reg = this_param;
32324   else if (vcall_offset)
32325     {
32326       /* Put the this parameter into %eax.  */
32327       this_reg = gen_rtx_REG (Pmode, AX_REG);
32328       emit_move_insn (this_reg, this_param);
32329     }
32330   else
32331     this_reg = NULL_RTX;
32332
32333   /* Adjust the this parameter by a fixed constant.  */
32334   if (delta)
32335     {
32336       rtx delta_rtx = GEN_INT (delta);
32337       rtx delta_dst = this_reg ? this_reg : this_param;
32338
32339       if (TARGET_64BIT)
32340         {
32341           if (!x86_64_general_operand (delta_rtx, Pmode))
32342             {
32343               tmp = gen_rtx_REG (Pmode, tmp_regno);
32344               emit_move_insn (tmp, delta_rtx);
32345               delta_rtx = tmp;
32346             }
32347         }
32348
32349       ix86_emit_binop (PLUS, Pmode, delta_dst, delta_rtx);
32350     }
32351
32352   /* Adjust the this parameter by a value stored in the vtable.  */
32353   if (vcall_offset)
32354     {
32355       rtx vcall_addr, vcall_mem, this_mem;
32356
32357       tmp = gen_rtx_REG (Pmode, tmp_regno);
32358
32359       this_mem = gen_rtx_MEM (ptr_mode, this_reg);
32360       if (Pmode != ptr_mode)
32361         this_mem = gen_rtx_ZERO_EXTEND (Pmode, this_mem);
32362       emit_move_insn (tmp, this_mem);
32363
32364       /* Adjust the this parameter.  */
32365       vcall_addr = plus_constant (tmp, vcall_offset);
32366       if (TARGET_64BIT
32367           && !ix86_legitimate_address_p (ptr_mode, vcall_addr, true))
32368         {
32369           rtx tmp2 = gen_rtx_REG (Pmode, R11_REG);
32370           emit_move_insn (tmp2, GEN_INT (vcall_offset));
32371           vcall_addr = gen_rtx_PLUS (Pmode, tmp, tmp2);
32372         }
32373
32374       vcall_mem = gen_rtx_MEM (ptr_mode, vcall_addr);
32375       if (Pmode != ptr_mode)
32376         emit_insn (gen_addsi_1_zext (this_reg,
32377                                      gen_rtx_REG (ptr_mode,
32378                                                   REGNO (this_reg)),
32379                                      vcall_mem));
32380       else
32381         ix86_emit_binop (PLUS, Pmode, this_reg, vcall_mem);
32382     }
32383
32384   /* If necessary, drop THIS back to its stack slot.  */
32385   if (this_reg && this_reg != this_param)
32386     emit_move_insn (this_param, this_reg);
32387
32388   fnaddr = XEXP (DECL_RTL (function), 0);
32389   if (TARGET_64BIT)
32390     {
32391       if (!flag_pic || targetm.binds_local_p (function)
32392           || cfun->machine->call_abi == MS_ABI)
32393         ;
32394       else
32395         {
32396           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, fnaddr), UNSPEC_GOTPCREL);
32397           tmp = gen_rtx_CONST (Pmode, tmp);
32398           fnaddr = gen_rtx_MEM (Pmode, tmp);
32399         }
32400     }
32401   else
32402     {
32403       if (!flag_pic || targetm.binds_local_p (function))
32404         ;
32405 #if TARGET_MACHO
32406       else if (TARGET_MACHO)
32407         {
32408           fnaddr = machopic_indirect_call_target (DECL_RTL (function));
32409           fnaddr = XEXP (fnaddr, 0);
32410         }
32411 #endif /* TARGET_MACHO */
32412       else
32413         {
32414           tmp = gen_rtx_REG (Pmode, CX_REG);
32415           output_set_got (tmp, NULL_RTX);
32416
32417           fnaddr = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, fnaddr), UNSPEC_GOT);
32418           fnaddr = gen_rtx_PLUS (Pmode, fnaddr, tmp);
32419           fnaddr = gen_rtx_MEM (Pmode, fnaddr);
32420         }
32421     }
32422
32423   /* Our sibling call patterns do not allow memories, because we have no
32424      predicate that can distinguish between frame and non-frame memory.
32425      For our purposes here, we can get away with (ab)using a jump pattern,
32426      because we're going to do no optimization.  */
32427   if (MEM_P (fnaddr))
32428     emit_jump_insn (gen_indirect_jump (fnaddr));
32429   else
32430     {
32431       if (ix86_cmodel == CM_LARGE_PIC && SYMBOLIC_CONST (fnaddr))
32432         fnaddr = legitimize_pic_address (fnaddr,
32433                                          gen_rtx_REG (Pmode, tmp_regno));
32434
32435       if (!sibcall_insn_operand (fnaddr, Pmode))
32436         {
32437           tmp = gen_rtx_REG (Pmode, tmp_regno);
32438           if (GET_MODE (fnaddr) != Pmode)
32439             fnaddr = gen_rtx_ZERO_EXTEND (Pmode, fnaddr);
32440           emit_move_insn (tmp, fnaddr);
32441           fnaddr = tmp;
32442         }
32443
32444       tmp = gen_rtx_MEM (QImode, fnaddr);
32445       tmp = gen_rtx_CALL (VOIDmode, tmp, const0_rtx);
32446       tmp = emit_call_insn (tmp);
32447       SIBLING_CALL_P (tmp) = 1;
32448     }
32449   emit_barrier ();
32450
32451   /* Emit just enough of rest_of_compilation to get the insns emitted.
32452      Note that use_thunk calls assemble_start_function et al.  */
32453   tmp = get_insns ();
32454   insn_locators_alloc ();
32455   shorten_branches (tmp);
32456   final_start_function (tmp, file, 1);
32457   final (tmp, file, 1);
32458   final_end_function ();
32459 }
32460
32461 static void
32462 x86_file_start (void)
32463 {
32464   default_file_start ();
32465 #if TARGET_MACHO
32466   darwin_file_start ();
32467 #endif
32468   if (X86_FILE_START_VERSION_DIRECTIVE)
32469     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
32470   if (X86_FILE_START_FLTUSED)
32471     fputs ("\t.global\t__fltused\n", asm_out_file);
32472   if (ix86_asm_dialect == ASM_INTEL)
32473     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
32474 }
32475
32476 int
32477 x86_field_alignment (tree field, int computed)
32478 {
32479   enum machine_mode mode;
32480   tree type = TREE_TYPE (field);
32481
32482   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
32483     return computed;
32484   mode = TYPE_MODE (strip_array_types (type));
32485   if (mode == DFmode || mode == DCmode
32486       || GET_MODE_CLASS (mode) == MODE_INT
32487       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
32488     return MIN (32, computed);
32489   return computed;
32490 }
32491
32492 /* Output assembler code to FILE to increment profiler label # LABELNO
32493    for profiling a function entry.  */
32494 void
32495 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
32496 {
32497   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
32498                                          : MCOUNT_NAME);
32499
32500   if (TARGET_64BIT)
32501     {
32502 #ifndef NO_PROFILE_COUNTERS
32503       fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
32504 #endif
32505
32506       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
32507         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name);
32508       else
32509         fprintf (file, "\tcall\t%s\n", mcount_name);
32510     }
32511   else if (flag_pic)
32512     {
32513 #ifndef NO_PROFILE_COUNTERS
32514       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
32515                LPREFIX, labelno);
32516 #endif
32517       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
32518     }
32519   else
32520     {
32521 #ifndef NO_PROFILE_COUNTERS
32522       fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
32523                LPREFIX, labelno);
32524 #endif
32525       fprintf (file, "\tcall\t%s\n", mcount_name);
32526     }
32527 }
32528
32529 /* We don't have exact information about the insn sizes, but we may assume
32530    quite safely that we are informed about all 1 byte insns and memory
32531    address sizes.  This is enough to eliminate unnecessary padding in
32532    99% of cases.  */
32533
32534 static int
32535 min_insn_size (rtx insn)
32536 {
32537   int l = 0, len;
32538
32539   if (!INSN_P (insn) || !active_insn_p (insn))
32540     return 0;
32541
32542   /* Discard alignments we've emit and jump instructions.  */
32543   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
32544       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
32545     return 0;
32546   if (JUMP_TABLE_DATA_P (insn))
32547     return 0;
32548
32549   /* Important case - calls are always 5 bytes.
32550      It is common to have many calls in the row.  */
32551   if (CALL_P (insn)
32552       && symbolic_reference_mentioned_p (PATTERN (insn))
32553       && !SIBLING_CALL_P (insn))
32554     return 5;
32555   len = get_attr_length (insn);
32556   if (len <= 1)
32557     return 1;
32558
32559   /* For normal instructions we rely on get_attr_length being exact,
32560      with a few exceptions.  */
32561   if (!JUMP_P (insn))
32562     {
32563       enum attr_type type = get_attr_type (insn);
32564
32565       switch (type)
32566         {
32567         case TYPE_MULTI:
32568           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
32569               || asm_noperands (PATTERN (insn)) >= 0)
32570             return 0;
32571           break;
32572         case TYPE_OTHER:
32573         case TYPE_FCMP:
32574           break;
32575         default:
32576           /* Otherwise trust get_attr_length.  */
32577           return len;
32578         }
32579
32580       l = get_attr_length_address (insn);
32581       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
32582         l = 4;
32583     }
32584   if (l)
32585     return 1+l;
32586   else
32587     return 2;
32588 }
32589
32590 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
32591
32592 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
32593    window.  */
32594
32595 static void
32596 ix86_avoid_jump_mispredicts (void)
32597 {
32598   rtx insn, start = get_insns ();
32599   int nbytes = 0, njumps = 0;
32600   int isjump = 0;
32601
32602   /* Look for all minimal intervals of instructions containing 4 jumps.
32603      The intervals are bounded by START and INSN.  NBYTES is the total
32604      size of instructions in the interval including INSN and not including
32605      START.  When the NBYTES is smaller than 16 bytes, it is possible
32606      that the end of START and INSN ends up in the same 16byte page.
32607
32608      The smallest offset in the page INSN can start is the case where START
32609      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
32610      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
32611      */
32612   for (insn = start; insn; insn = NEXT_INSN (insn))
32613     {
32614       int min_size;
32615
32616       if (LABEL_P (insn))
32617         {
32618           int align = label_to_alignment (insn);
32619           int max_skip = label_to_max_skip (insn);
32620
32621           if (max_skip > 15)
32622             max_skip = 15;
32623           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
32624              already in the current 16 byte page, because otherwise
32625              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
32626              bytes to reach 16 byte boundary.  */
32627           if (align <= 0
32628               || (align <= 3 && max_skip != (1 << align) - 1))
32629             max_skip = 0;
32630           if (dump_file)
32631             fprintf (dump_file, "Label %i with max_skip %i\n",
32632                      INSN_UID (insn), max_skip);
32633           if (max_skip)
32634             {
32635               while (nbytes + max_skip >= 16)
32636                 {
32637                   start = NEXT_INSN (start);
32638                   if ((JUMP_P (start)
32639                        && GET_CODE (PATTERN (start)) != ADDR_VEC
32640                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
32641                       || CALL_P (start))
32642                     njumps--, isjump = 1;
32643                   else
32644                     isjump = 0;
32645                   nbytes -= min_insn_size (start);
32646                 }
32647             }
32648           continue;
32649         }
32650
32651       min_size = min_insn_size (insn);
32652       nbytes += min_size;
32653       if (dump_file)
32654         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
32655                  INSN_UID (insn), min_size);
32656       if ((JUMP_P (insn)
32657            && GET_CODE (PATTERN (insn)) != ADDR_VEC
32658            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
32659           || CALL_P (insn))
32660         njumps++;
32661       else
32662         continue;
32663
32664       while (njumps > 3)
32665         {
32666           start = NEXT_INSN (start);
32667           if ((JUMP_P (start)
32668                && GET_CODE (PATTERN (start)) != ADDR_VEC
32669                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
32670               || CALL_P (start))
32671             njumps--, isjump = 1;
32672           else
32673             isjump = 0;
32674           nbytes -= min_insn_size (start);
32675         }
32676       gcc_assert (njumps >= 0);
32677       if (dump_file)
32678         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
32679                  INSN_UID (start), INSN_UID (insn), nbytes);
32680
32681       if (njumps == 3 && isjump && nbytes < 16)
32682         {
32683           int padsize = 15 - nbytes + min_insn_size (insn);
32684
32685           if (dump_file)
32686             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
32687                      INSN_UID (insn), padsize);
32688           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
32689         }
32690     }
32691 }
32692 #endif
32693
32694 /* AMD Athlon works faster
32695    when RET is not destination of conditional jump or directly preceded
32696    by other jump instruction.  We avoid the penalty by inserting NOP just
32697    before the RET instructions in such cases.  */
32698 static void
32699 ix86_pad_returns (void)
32700 {
32701   edge e;
32702   edge_iterator ei;
32703
32704   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
32705     {
32706       basic_block bb = e->src;
32707       rtx ret = BB_END (bb);
32708       rtx prev;
32709       bool replace = false;
32710
32711       if (!JUMP_P (ret) || !ANY_RETURN_P (PATTERN (ret))
32712           || optimize_bb_for_size_p (bb))
32713         continue;
32714       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
32715         if (active_insn_p (prev) || LABEL_P (prev))
32716           break;
32717       if (prev && LABEL_P (prev))
32718         {
32719           edge e;
32720           edge_iterator ei;
32721
32722           FOR_EACH_EDGE (e, ei, bb->preds)
32723             if (EDGE_FREQUENCY (e) && e->src->index >= 0
32724                 && !(e->flags & EDGE_FALLTHRU))
32725               replace = true;
32726         }
32727       if (!replace)
32728         {
32729           prev = prev_active_insn (ret);
32730           if (prev
32731               && ((JUMP_P (prev) && any_condjump_p (prev))
32732                   || CALL_P (prev)))
32733             replace = true;
32734           /* Empty functions get branch mispredict even when
32735              the jump destination is not visible to us.  */
32736           if (!prev && !optimize_function_for_size_p (cfun))
32737             replace = true;
32738         }
32739       if (replace)
32740         {
32741           emit_jump_insn_before (gen_simple_return_internal_long (), ret);
32742           delete_insn (ret);
32743         }
32744     }
32745 }
32746
32747 /* Count the minimum number of instructions in BB.  Return 4 if the
32748    number of instructions >= 4.  */
32749
32750 static int
32751 ix86_count_insn_bb (basic_block bb)
32752 {
32753   rtx insn;
32754   int insn_count = 0;
32755
32756   /* Count number of instructions in this block.  Return 4 if the number
32757      of instructions >= 4.  */
32758   FOR_BB_INSNS (bb, insn)
32759     {
32760       /* Only happen in exit blocks.  */
32761       if (JUMP_P (insn)
32762           && ANY_RETURN_P (PATTERN (insn)))
32763         break;
32764
32765       if (NONDEBUG_INSN_P (insn)
32766           && GET_CODE (PATTERN (insn)) != USE
32767           && GET_CODE (PATTERN (insn)) != CLOBBER)
32768         {
32769           insn_count++;
32770           if (insn_count >= 4)
32771             return insn_count;
32772         }
32773     }
32774
32775   return insn_count;
32776 }
32777
32778
32779 /* Count the minimum number of instructions in code path in BB.
32780    Return 4 if the number of instructions >= 4.  */
32781
32782 static int
32783 ix86_count_insn (basic_block bb)
32784 {
32785   edge e;
32786   edge_iterator ei;
32787   int min_prev_count;
32788
32789   /* Only bother counting instructions along paths with no
32790      more than 2 basic blocks between entry and exit.  Given
32791      that BB has an edge to exit, determine if a predecessor
32792      of BB has an edge from entry.  If so, compute the number
32793      of instructions in the predecessor block.  If there
32794      happen to be multiple such blocks, compute the minimum.  */
32795   min_prev_count = 4;
32796   FOR_EACH_EDGE (e, ei, bb->preds)
32797     {
32798       edge prev_e;
32799       edge_iterator prev_ei;
32800
32801       if (e->src == ENTRY_BLOCK_PTR)
32802         {
32803           min_prev_count = 0;
32804           break;
32805         }
32806       FOR_EACH_EDGE (prev_e, prev_ei, e->src->preds)
32807         {
32808           if (prev_e->src == ENTRY_BLOCK_PTR)
32809             {
32810               int count = ix86_count_insn_bb (e->src);
32811               if (count < min_prev_count)
32812                 min_prev_count = count;
32813               break;
32814             }
32815         }
32816     }
32817
32818   if (min_prev_count < 4)
32819     min_prev_count += ix86_count_insn_bb (bb);
32820
32821   return min_prev_count;
32822 }
32823
32824 /* Pad short funtion to 4 instructions.   */
32825
32826 static void
32827 ix86_pad_short_function (void)
32828 {
32829   edge e;
32830   edge_iterator ei;
32831
32832   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
32833     {
32834       rtx ret = BB_END (e->src);
32835       if (JUMP_P (ret) && ANY_RETURN_P (PATTERN (ret)))
32836         {
32837           int insn_count = ix86_count_insn (e->src);
32838
32839           /* Pad short function.  */
32840           if (insn_count < 4)
32841             {
32842               rtx insn = ret;
32843
32844               /* Find epilogue.  */
32845               while (insn
32846                      && (!NOTE_P (insn)
32847                          || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG))
32848                 insn = PREV_INSN (insn);
32849
32850               if (!insn)
32851                 insn = ret;
32852
32853               /* Two NOPs count as one instruction.  */
32854               insn_count = 2 * (4 - insn_count);
32855               emit_insn_before (gen_nops (GEN_INT (insn_count)), insn);
32856             }
32857         }
32858     }
32859 }
32860
32861 /* Implement machine specific optimizations.  We implement padding of returns
32862    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
32863 static void
32864 ix86_reorg (void)
32865 {
32866   /* We are freeing block_for_insn in the toplev to keep compatibility
32867      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
32868   compute_bb_for_insn ();
32869
32870   /* Run the vzeroupper optimization if needed.  */
32871   if (TARGET_VZEROUPPER)
32872     move_or_delete_vzeroupper ();
32873
32874   if (optimize && optimize_function_for_speed_p (cfun))
32875     {
32876       if (TARGET_PAD_SHORT_FUNCTION)
32877         ix86_pad_short_function ();
32878       else if (TARGET_PAD_RETURNS)
32879         ix86_pad_returns ();
32880 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
32881       if (TARGET_FOUR_JUMP_LIMIT)
32882         ix86_avoid_jump_mispredicts ();
32883 #endif
32884     }
32885 }
32886
32887 /* Return nonzero when QImode register that must be represented via REX prefix
32888    is used.  */
32889 bool
32890 x86_extended_QIreg_mentioned_p (rtx insn)
32891 {
32892   int i;
32893   extract_insn_cached (insn);
32894   for (i = 0; i < recog_data.n_operands; i++)
32895     if (REG_P (recog_data.operand[i])
32896         && REGNO (recog_data.operand[i]) > BX_REG)
32897        return true;
32898   return false;
32899 }
32900
32901 /* Return nonzero when P points to register encoded via REX prefix.
32902    Called via for_each_rtx.  */
32903 static int
32904 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
32905 {
32906    unsigned int regno;
32907    if (!REG_P (*p))
32908      return 0;
32909    regno = REGNO (*p);
32910    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
32911 }
32912
32913 /* Return true when INSN mentions register that must be encoded using REX
32914    prefix.  */
32915 bool
32916 x86_extended_reg_mentioned_p (rtx insn)
32917 {
32918   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
32919                        extended_reg_mentioned_1, NULL);
32920 }
32921
32922 /* If profitable, negate (without causing overflow) integer constant
32923    of mode MODE at location LOC.  Return true in this case.  */
32924 bool
32925 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
32926 {
32927   HOST_WIDE_INT val;
32928
32929   if (!CONST_INT_P (*loc))
32930     return false;
32931
32932   switch (mode)
32933     {
32934     case DImode:
32935       /* DImode x86_64 constants must fit in 32 bits.  */
32936       gcc_assert (x86_64_immediate_operand (*loc, mode));
32937
32938       mode = SImode;
32939       break;
32940
32941     case SImode:
32942     case HImode:
32943     case QImode:
32944       break;
32945
32946     default:
32947       gcc_unreachable ();
32948     }
32949
32950   /* Avoid overflows.  */
32951   if (mode_signbit_p (mode, *loc))
32952     return false;
32953
32954   val = INTVAL (*loc);
32955
32956   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
32957      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
32958   if ((val < 0 && val != -128)
32959       || val == 128)
32960     {
32961       *loc = GEN_INT (-val);
32962       return true;
32963     }
32964
32965   return false;
32966 }
32967
32968 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
32969    optabs would emit if we didn't have TFmode patterns.  */
32970
32971 void
32972 x86_emit_floatuns (rtx operands[2])
32973 {
32974   rtx neglab, donelab, i0, i1, f0, in, out;
32975   enum machine_mode mode, inmode;
32976
32977   inmode = GET_MODE (operands[1]);
32978   gcc_assert (inmode == SImode || inmode == DImode);
32979
32980   out = operands[0];
32981   in = force_reg (inmode, operands[1]);
32982   mode = GET_MODE (out);
32983   neglab = gen_label_rtx ();
32984   donelab = gen_label_rtx ();
32985   f0 = gen_reg_rtx (mode);
32986
32987   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
32988
32989   expand_float (out, in, 0);
32990
32991   emit_jump_insn (gen_jump (donelab));
32992   emit_barrier ();
32993
32994   emit_label (neglab);
32995
32996   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
32997                             1, OPTAB_DIRECT);
32998   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
32999                             1, OPTAB_DIRECT);
33000   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
33001
33002   expand_float (f0, i0, 0);
33003
33004   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
33005
33006   emit_label (donelab);
33007 }
33008 \f
33009 /* AVX2 does support 32-byte integer vector operations,
33010    thus the longest vector we are faced with is V32QImode.  */
33011 #define MAX_VECT_LEN    32
33012
33013 struct expand_vec_perm_d
33014 {
33015   rtx target, op0, op1;
33016   unsigned char perm[MAX_VECT_LEN];
33017   enum machine_mode vmode;
33018   unsigned char nelt;
33019   bool testing_p;
33020 };
33021
33022 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
33023 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
33024
33025 /* Get a vector mode of the same size as the original but with elements
33026    twice as wide.  This is only guaranteed to apply to integral vectors.  */
33027
33028 static inline enum machine_mode
33029 get_mode_wider_vector (enum machine_mode o)
33030 {
33031   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
33032   enum machine_mode n = GET_MODE_WIDER_MODE (o);
33033   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
33034   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
33035   return n;
33036 }
33037
33038 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
33039    with all elements equal to VAR.  Return true if successful.  */
33040
33041 static bool
33042 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
33043                                    rtx target, rtx val)
33044 {
33045   bool ok;
33046
33047   switch (mode)
33048     {
33049     case V2SImode:
33050     case V2SFmode:
33051       if (!mmx_ok)
33052         return false;
33053       /* FALLTHRU */
33054
33055     case V4DFmode:
33056     case V4DImode:
33057     case V8SFmode:
33058     case V8SImode:
33059     case V2DFmode:
33060     case V2DImode:
33061     case V4SFmode:
33062     case V4SImode:
33063       {
33064         rtx insn, dup;
33065
33066         /* First attempt to recognize VAL as-is.  */
33067         dup = gen_rtx_VEC_DUPLICATE (mode, val);
33068         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
33069         if (recog_memoized (insn) < 0)
33070           {
33071             rtx seq;
33072             /* If that fails, force VAL into a register.  */
33073
33074             start_sequence ();
33075             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
33076             seq = get_insns ();
33077             end_sequence ();
33078             if (seq)
33079               emit_insn_before (seq, insn);
33080
33081             ok = recog_memoized (insn) >= 0;
33082             gcc_assert (ok);
33083           }
33084       }
33085       return true;
33086
33087     case V4HImode:
33088       if (!mmx_ok)
33089         return false;
33090       if (TARGET_SSE || TARGET_3DNOW_A)
33091         {
33092           rtx x;
33093
33094           val = gen_lowpart (SImode, val);
33095           x = gen_rtx_TRUNCATE (HImode, val);
33096           x = gen_rtx_VEC_DUPLICATE (mode, x);
33097           emit_insn (gen_rtx_SET (VOIDmode, target, x));
33098           return true;
33099         }
33100       goto widen;
33101
33102     case V8QImode:
33103       if (!mmx_ok)
33104         return false;
33105       goto widen;
33106
33107     case V8HImode:
33108       if (TARGET_SSE2)
33109         {
33110           struct expand_vec_perm_d dperm;
33111           rtx tmp1, tmp2;
33112
33113         permute:
33114           memset (&dperm, 0, sizeof (dperm));
33115           dperm.target = target;
33116           dperm.vmode = mode;
33117           dperm.nelt = GET_MODE_NUNITS (mode);
33118           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
33119
33120           /* Extend to SImode using a paradoxical SUBREG.  */
33121           tmp1 = gen_reg_rtx (SImode);
33122           emit_move_insn (tmp1, gen_lowpart (SImode, val));
33123
33124           /* Insert the SImode value as low element of a V4SImode vector. */
33125           tmp2 = gen_lowpart (V4SImode, dperm.op0);
33126           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
33127
33128           ok = (expand_vec_perm_1 (&dperm)
33129                 || expand_vec_perm_broadcast_1 (&dperm));
33130           gcc_assert (ok);
33131           return ok;
33132         }
33133       goto widen;
33134
33135     case V16QImode:
33136       if (TARGET_SSE2)
33137         goto permute;
33138       goto widen;
33139
33140     widen:
33141       /* Replicate the value once into the next wider mode and recurse.  */
33142       {
33143         enum machine_mode smode, wsmode, wvmode;
33144         rtx x;
33145
33146         smode = GET_MODE_INNER (mode);
33147         wvmode = get_mode_wider_vector (mode);
33148         wsmode = GET_MODE_INNER (wvmode);
33149
33150         val = convert_modes (wsmode, smode, val, true);
33151         x = expand_simple_binop (wsmode, ASHIFT, val,
33152                                  GEN_INT (GET_MODE_BITSIZE (smode)),
33153                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
33154         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
33155
33156         x = gen_lowpart (wvmode, target);
33157         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
33158         gcc_assert (ok);
33159         return ok;
33160       }
33161
33162     case V16HImode:
33163     case V32QImode:
33164       {
33165         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
33166         rtx x = gen_reg_rtx (hvmode);
33167
33168         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
33169         gcc_assert (ok);
33170
33171         x = gen_rtx_VEC_CONCAT (mode, x, x);
33172         emit_insn (gen_rtx_SET (VOIDmode, target, x));
33173       }
33174       return true;
33175
33176     default:
33177       return false;
33178     }
33179 }
33180
33181 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
33182    whose ONE_VAR element is VAR, and other elements are zero.  Return true
33183    if successful.  */
33184
33185 static bool
33186 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
33187                                      rtx target, rtx var, int one_var)
33188 {
33189   enum machine_mode vsimode;
33190   rtx new_target;
33191   rtx x, tmp;
33192   bool use_vector_set = false;
33193
33194   switch (mode)
33195     {
33196     case V2DImode:
33197       /* For SSE4.1, we normally use vector set.  But if the second
33198          element is zero and inter-unit moves are OK, we use movq
33199          instead.  */
33200       use_vector_set = (TARGET_64BIT
33201                         && TARGET_SSE4_1
33202                         && !(TARGET_INTER_UNIT_MOVES
33203                              && one_var == 0));
33204       break;
33205     case V16QImode:
33206     case V4SImode:
33207     case V4SFmode:
33208       use_vector_set = TARGET_SSE4_1;
33209       break;
33210     case V8HImode:
33211       use_vector_set = TARGET_SSE2;
33212       break;
33213     case V4HImode:
33214       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
33215       break;
33216     case V32QImode:
33217     case V16HImode:
33218     case V8SImode:
33219     case V8SFmode:
33220     case V4DFmode:
33221       use_vector_set = TARGET_AVX;
33222       break;
33223     case V4DImode:
33224       /* Use ix86_expand_vector_set in 64bit mode only.  */
33225       use_vector_set = TARGET_AVX && TARGET_64BIT;
33226       break;
33227     default:
33228       break;
33229     }
33230
33231   if (use_vector_set)
33232     {
33233       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
33234       var = force_reg (GET_MODE_INNER (mode), var);
33235       ix86_expand_vector_set (mmx_ok, target, var, one_var);
33236       return true;
33237     }
33238
33239   switch (mode)
33240     {
33241     case V2SFmode:
33242     case V2SImode:
33243       if (!mmx_ok)
33244         return false;
33245       /* FALLTHRU */
33246
33247     case V2DFmode:
33248     case V2DImode:
33249       if (one_var != 0)
33250         return false;
33251       var = force_reg (GET_MODE_INNER (mode), var);
33252       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
33253       emit_insn (gen_rtx_SET (VOIDmode, target, x));
33254       return true;
33255
33256     case V4SFmode:
33257     case V4SImode:
33258       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
33259         new_target = gen_reg_rtx (mode);
33260       else
33261         new_target = target;
33262       var = force_reg (GET_MODE_INNER (mode), var);
33263       x = gen_rtx_VEC_DUPLICATE (mode, var);
33264       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
33265       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
33266       if (one_var != 0)
33267         {
33268           /* We need to shuffle the value to the correct position, so
33269              create a new pseudo to store the intermediate result.  */
33270
33271           /* With SSE2, we can use the integer shuffle insns.  */
33272           if (mode != V4SFmode && TARGET_SSE2)
33273             {
33274               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
33275                                             const1_rtx,
33276                                             GEN_INT (one_var == 1 ? 0 : 1),
33277                                             GEN_INT (one_var == 2 ? 0 : 1),
33278                                             GEN_INT (one_var == 3 ? 0 : 1)));
33279               if (target != new_target)
33280                 emit_move_insn (target, new_target);
33281               return true;
33282             }
33283
33284           /* Otherwise convert the intermediate result to V4SFmode and
33285              use the SSE1 shuffle instructions.  */
33286           if (mode != V4SFmode)
33287             {
33288               tmp = gen_reg_rtx (V4SFmode);
33289               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
33290             }
33291           else
33292             tmp = new_target;
33293
33294           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
33295                                        const1_rtx,
33296                                        GEN_INT (one_var == 1 ? 0 : 1),
33297                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
33298                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
33299
33300           if (mode != V4SFmode)
33301             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
33302           else if (tmp != target)
33303             emit_move_insn (target, tmp);
33304         }
33305       else if (target != new_target)
33306         emit_move_insn (target, new_target);
33307       return true;
33308
33309     case V8HImode:
33310     case V16QImode:
33311       vsimode = V4SImode;
33312       goto widen;
33313     case V4HImode:
33314     case V8QImode:
33315       if (!mmx_ok)
33316         return false;
33317       vsimode = V2SImode;
33318       goto widen;
33319     widen:
33320       if (one_var != 0)
33321         return false;
33322
33323       /* Zero extend the variable element to SImode and recurse.  */
33324       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
33325
33326       x = gen_reg_rtx (vsimode);
33327       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
33328                                                 var, one_var))
33329         gcc_unreachable ();
33330
33331       emit_move_insn (target, gen_lowpart (mode, x));
33332       return true;
33333
33334     default:
33335       return false;
33336     }
33337 }
33338
33339 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
33340    consisting of the values in VALS.  It is known that all elements
33341    except ONE_VAR are constants.  Return true if successful.  */
33342
33343 static bool
33344 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
33345                                  rtx target, rtx vals, int one_var)
33346 {
33347   rtx var = XVECEXP (vals, 0, one_var);
33348   enum machine_mode wmode;
33349   rtx const_vec, x;
33350
33351   const_vec = copy_rtx (vals);
33352   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
33353   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
33354
33355   switch (mode)
33356     {
33357     case V2DFmode:
33358     case V2DImode:
33359     case V2SFmode:
33360     case V2SImode:
33361       /* For the two element vectors, it's just as easy to use
33362          the general case.  */
33363       return false;
33364
33365     case V4DImode:
33366       /* Use ix86_expand_vector_set in 64bit mode only.  */
33367       if (!TARGET_64BIT)
33368         return false;
33369     case V4DFmode:
33370     case V8SFmode:
33371     case V8SImode:
33372     case V16HImode:
33373     case V32QImode:
33374     case V4SFmode:
33375     case V4SImode:
33376     case V8HImode:
33377     case V4HImode:
33378       break;
33379
33380     case V16QImode:
33381       if (TARGET_SSE4_1)
33382         break;
33383       wmode = V8HImode;
33384       goto widen;
33385     case V8QImode:
33386       wmode = V4HImode;
33387       goto widen;
33388     widen:
33389       /* There's no way to set one QImode entry easily.  Combine
33390          the variable value with its adjacent constant value, and
33391          promote to an HImode set.  */
33392       x = XVECEXP (vals, 0, one_var ^ 1);
33393       if (one_var & 1)
33394         {
33395           var = convert_modes (HImode, QImode, var, true);
33396           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
33397                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
33398           x = GEN_INT (INTVAL (x) & 0xff);
33399         }
33400       else
33401         {
33402           var = convert_modes (HImode, QImode, var, true);
33403           x = gen_int_mode (INTVAL (x) << 8, HImode);
33404         }
33405       if (x != const0_rtx)
33406         var = expand_simple_binop (HImode, IOR, var, x, var,
33407                                    1, OPTAB_LIB_WIDEN);
33408
33409       x = gen_reg_rtx (wmode);
33410       emit_move_insn (x, gen_lowpart (wmode, const_vec));
33411       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
33412
33413       emit_move_insn (target, gen_lowpart (mode, x));
33414       return true;
33415
33416     default:
33417       return false;
33418     }
33419
33420   emit_move_insn (target, const_vec);
33421   ix86_expand_vector_set (mmx_ok, target, var, one_var);
33422   return true;
33423 }
33424
33425 /* A subroutine of ix86_expand_vector_init_general.  Use vector
33426    concatenate to handle the most general case: all values variable,
33427    and none identical.  */
33428
33429 static void
33430 ix86_expand_vector_init_concat (enum machine_mode mode,
33431                                 rtx target, rtx *ops, int n)
33432 {
33433   enum machine_mode cmode, hmode = VOIDmode;
33434   rtx first[8], second[4];
33435   rtvec v;
33436   int i, j;
33437
33438   switch (n)
33439     {
33440     case 2:
33441       switch (mode)
33442         {
33443         case V8SImode:
33444           cmode = V4SImode;
33445           break;
33446         case V8SFmode:
33447           cmode = V4SFmode;
33448           break;
33449         case V4DImode:
33450           cmode = V2DImode;
33451           break;
33452         case V4DFmode:
33453           cmode = V2DFmode;
33454           break;
33455         case V4SImode:
33456           cmode = V2SImode;
33457           break;
33458         case V4SFmode:
33459           cmode = V2SFmode;
33460           break;
33461         case V2DImode:
33462           cmode = DImode;
33463           break;
33464         case V2SImode:
33465           cmode = SImode;
33466           break;
33467         case V2DFmode:
33468           cmode = DFmode;
33469           break;
33470         case V2SFmode:
33471           cmode = SFmode;
33472           break;
33473         default:
33474           gcc_unreachable ();
33475         }
33476
33477       if (!register_operand (ops[1], cmode))
33478         ops[1] = force_reg (cmode, ops[1]);
33479       if (!register_operand (ops[0], cmode))
33480         ops[0] = force_reg (cmode, ops[0]);
33481       emit_insn (gen_rtx_SET (VOIDmode, target,
33482                               gen_rtx_VEC_CONCAT (mode, ops[0],
33483                                                   ops[1])));
33484       break;
33485
33486     case 4:
33487       switch (mode)
33488         {
33489         case V4DImode:
33490           cmode = V2DImode;
33491           break;
33492         case V4DFmode:
33493           cmode = V2DFmode;
33494           break;
33495         case V4SImode:
33496           cmode = V2SImode;
33497           break;
33498         case V4SFmode:
33499           cmode = V2SFmode;
33500           break;
33501         default:
33502           gcc_unreachable ();
33503         }
33504       goto half;
33505
33506     case 8:
33507       switch (mode)
33508         {
33509         case V8SImode:
33510           cmode = V2SImode;
33511           hmode = V4SImode;
33512           break;
33513         case V8SFmode:
33514           cmode = V2SFmode;
33515           hmode = V4SFmode;
33516           break;
33517         default:
33518           gcc_unreachable ();
33519         }
33520       goto half;
33521
33522 half:
33523       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
33524       i = n - 1;
33525       j = (n >> 1) - 1;
33526       for (; i > 0; i -= 2, j--)
33527         {
33528           first[j] = gen_reg_rtx (cmode);
33529           v = gen_rtvec (2, ops[i - 1], ops[i]);
33530           ix86_expand_vector_init (false, first[j],
33531                                    gen_rtx_PARALLEL (cmode, v));
33532         }
33533
33534       n >>= 1;
33535       if (n > 2)
33536         {
33537           gcc_assert (hmode != VOIDmode);
33538           for (i = j = 0; i < n; i += 2, j++)
33539             {
33540               second[j] = gen_reg_rtx (hmode);
33541               ix86_expand_vector_init_concat (hmode, second [j],
33542                                               &first [i], 2);
33543             }
33544           n >>= 1;
33545           ix86_expand_vector_init_concat (mode, target, second, n);
33546         }
33547       else
33548         ix86_expand_vector_init_concat (mode, target, first, n);
33549       break;
33550
33551     default:
33552       gcc_unreachable ();
33553     }
33554 }
33555
33556 /* A subroutine of ix86_expand_vector_init_general.  Use vector
33557    interleave to handle the most general case: all values variable,
33558    and none identical.  */
33559
33560 static void
33561 ix86_expand_vector_init_interleave (enum machine_mode mode,
33562                                     rtx target, rtx *ops, int n)
33563 {
33564   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
33565   int i, j;
33566   rtx op0, op1;
33567   rtx (*gen_load_even) (rtx, rtx, rtx);
33568   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
33569   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
33570
33571   switch (mode)
33572     {
33573     case V8HImode:
33574       gen_load_even = gen_vec_setv8hi;
33575       gen_interleave_first_low = gen_vec_interleave_lowv4si;
33576       gen_interleave_second_low = gen_vec_interleave_lowv2di;
33577       inner_mode = HImode;
33578       first_imode = V4SImode;
33579       second_imode = V2DImode;
33580       third_imode = VOIDmode;
33581       break;
33582     case V16QImode:
33583       gen_load_even = gen_vec_setv16qi;
33584       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
33585       gen_interleave_second_low = gen_vec_interleave_lowv4si;
33586       inner_mode = QImode;
33587       first_imode = V8HImode;
33588       second_imode = V4SImode;
33589       third_imode = V2DImode;
33590       break;
33591     default:
33592       gcc_unreachable ();
33593     }
33594
33595   for (i = 0; i < n; i++)
33596     {
33597       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
33598       op0 = gen_reg_rtx (SImode);
33599       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
33600
33601       /* Insert the SImode value as low element of V4SImode vector. */
33602       op1 = gen_reg_rtx (V4SImode);
33603       op0 = gen_rtx_VEC_MERGE (V4SImode,
33604                                gen_rtx_VEC_DUPLICATE (V4SImode,
33605                                                       op0),
33606                                CONST0_RTX (V4SImode),
33607                                const1_rtx);
33608       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
33609
33610       /* Cast the V4SImode vector back to a vector in orignal mode.  */
33611       op0 = gen_reg_rtx (mode);
33612       emit_move_insn (op0, gen_lowpart (mode, op1));
33613
33614       /* Load even elements into the second positon.  */
33615       emit_insn (gen_load_even (op0,
33616                                 force_reg (inner_mode,
33617                                            ops [i + i + 1]),
33618                                 const1_rtx));
33619
33620       /* Cast vector to FIRST_IMODE vector.  */
33621       ops[i] = gen_reg_rtx (first_imode);
33622       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
33623     }
33624
33625   /* Interleave low FIRST_IMODE vectors.  */
33626   for (i = j = 0; i < n; i += 2, j++)
33627     {
33628       op0 = gen_reg_rtx (first_imode);
33629       emit_insn (gen_interleave_first_low (op0, ops[i], ops[i + 1]));
33630
33631       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
33632       ops[j] = gen_reg_rtx (second_imode);
33633       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
33634     }
33635
33636   /* Interleave low SECOND_IMODE vectors.  */
33637   switch (second_imode)
33638     {
33639     case V4SImode:
33640       for (i = j = 0; i < n / 2; i += 2, j++)
33641         {
33642           op0 = gen_reg_rtx (second_imode);
33643           emit_insn (gen_interleave_second_low (op0, ops[i],
33644                                                 ops[i + 1]));
33645
33646           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
33647              vector.  */
33648           ops[j] = gen_reg_rtx (third_imode);
33649           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
33650         }
33651       second_imode = V2DImode;
33652       gen_interleave_second_low = gen_vec_interleave_lowv2di;
33653       /* FALLTHRU */
33654
33655     case V2DImode:
33656       op0 = gen_reg_rtx (second_imode);
33657       emit_insn (gen_interleave_second_low (op0, ops[0],
33658                                             ops[1]));
33659
33660       /* Cast the SECOND_IMODE vector back to a vector on original
33661          mode.  */
33662       emit_insn (gen_rtx_SET (VOIDmode, target,
33663                               gen_lowpart (mode, op0)));
33664       break;
33665
33666     default:
33667       gcc_unreachable ();
33668     }
33669 }
33670
33671 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
33672    all values variable, and none identical.  */
33673
33674 static void
33675 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
33676                                  rtx target, rtx vals)
33677 {
33678   rtx ops[32], op0, op1;
33679   enum machine_mode half_mode = VOIDmode;
33680   int n, i;
33681
33682   switch (mode)
33683     {
33684     case V2SFmode:
33685     case V2SImode:
33686       if (!mmx_ok && !TARGET_SSE)
33687         break;
33688       /* FALLTHRU */
33689
33690     case V8SFmode:
33691     case V8SImode:
33692     case V4DFmode:
33693     case V4DImode:
33694     case V4SFmode:
33695     case V4SImode:
33696     case V2DFmode:
33697     case V2DImode:
33698       n = GET_MODE_NUNITS (mode);
33699       for (i = 0; i < n; i++)
33700         ops[i] = XVECEXP (vals, 0, i);
33701       ix86_expand_vector_init_concat (mode, target, ops, n);
33702       return;
33703
33704     case V32QImode:
33705       half_mode = V16QImode;
33706       goto half;
33707
33708     case V16HImode:
33709       half_mode = V8HImode;
33710       goto half;
33711
33712 half:
33713       n = GET_MODE_NUNITS (mode);
33714       for (i = 0; i < n; i++)
33715         ops[i] = XVECEXP (vals, 0, i);
33716       op0 = gen_reg_rtx (half_mode);
33717       op1 = gen_reg_rtx (half_mode);
33718       ix86_expand_vector_init_interleave (half_mode, op0, ops,
33719                                           n >> 2);
33720       ix86_expand_vector_init_interleave (half_mode, op1,
33721                                           &ops [n >> 1], n >> 2);
33722       emit_insn (gen_rtx_SET (VOIDmode, target,
33723                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
33724       return;
33725
33726     case V16QImode:
33727       if (!TARGET_SSE4_1)
33728         break;
33729       /* FALLTHRU */
33730
33731     case V8HImode:
33732       if (!TARGET_SSE2)
33733         break;
33734
33735       /* Don't use ix86_expand_vector_init_interleave if we can't
33736          move from GPR to SSE register directly.  */
33737       if (!TARGET_INTER_UNIT_MOVES)
33738         break;
33739
33740       n = GET_MODE_NUNITS (mode);
33741       for (i = 0; i < n; i++)
33742         ops[i] = XVECEXP (vals, 0, i);
33743       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
33744       return;
33745
33746     case V4HImode:
33747     case V8QImode:
33748       break;
33749
33750     default:
33751       gcc_unreachable ();
33752     }
33753
33754     {
33755       int i, j, n_elts, n_words, n_elt_per_word;
33756       enum machine_mode inner_mode;
33757       rtx words[4], shift;
33758
33759       inner_mode = GET_MODE_INNER (mode);
33760       n_elts = GET_MODE_NUNITS (mode);
33761       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
33762       n_elt_per_word = n_elts / n_words;
33763       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
33764
33765       for (i = 0; i < n_words; ++i)
33766         {
33767           rtx word = NULL_RTX;
33768
33769           for (j = 0; j < n_elt_per_word; ++j)
33770             {
33771               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
33772               elt = convert_modes (word_mode, inner_mode, elt, true);
33773
33774               if (j == 0)
33775                 word = elt;
33776               else
33777                 {
33778                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
33779                                               word, 1, OPTAB_LIB_WIDEN);
33780                   word = expand_simple_binop (word_mode, IOR, word, elt,
33781                                               word, 1, OPTAB_LIB_WIDEN);
33782                 }
33783             }
33784
33785           words[i] = word;
33786         }
33787
33788       if (n_words == 1)
33789         emit_move_insn (target, gen_lowpart (mode, words[0]));
33790       else if (n_words == 2)
33791         {
33792           rtx tmp = gen_reg_rtx (mode);
33793           emit_clobber (tmp);
33794           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
33795           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
33796           emit_move_insn (target, tmp);
33797         }
33798       else if (n_words == 4)
33799         {
33800           rtx tmp = gen_reg_rtx (V4SImode);
33801           gcc_assert (word_mode == SImode);
33802           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
33803           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
33804           emit_move_insn (target, gen_lowpart (mode, tmp));
33805         }
33806       else
33807         gcc_unreachable ();
33808     }
33809 }
33810
33811 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
33812    instructions unless MMX_OK is true.  */
33813
33814 void
33815 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
33816 {
33817   enum machine_mode mode = GET_MODE (target);
33818   enum machine_mode inner_mode = GET_MODE_INNER (mode);
33819   int n_elts = GET_MODE_NUNITS (mode);
33820   int n_var = 0, one_var = -1;
33821   bool all_same = true, all_const_zero = true;
33822   int i;
33823   rtx x;
33824
33825   for (i = 0; i < n_elts; ++i)
33826     {
33827       x = XVECEXP (vals, 0, i);
33828       if (!(CONST_INT_P (x)
33829             || GET_CODE (x) == CONST_DOUBLE
33830             || GET_CODE (x) == CONST_FIXED))
33831         n_var++, one_var = i;
33832       else if (x != CONST0_RTX (inner_mode))
33833         all_const_zero = false;
33834       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
33835         all_same = false;
33836     }
33837
33838   /* Constants are best loaded from the constant pool.  */
33839   if (n_var == 0)
33840     {
33841       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
33842       return;
33843     }
33844
33845   /* If all values are identical, broadcast the value.  */
33846   if (all_same
33847       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
33848                                             XVECEXP (vals, 0, 0)))
33849     return;
33850
33851   /* Values where only one field is non-constant are best loaded from
33852      the pool and overwritten via move later.  */
33853   if (n_var == 1)
33854     {
33855       if (all_const_zero
33856           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
33857                                                   XVECEXP (vals, 0, one_var),
33858                                                   one_var))
33859         return;
33860
33861       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
33862         return;
33863     }
33864
33865   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
33866 }
33867
33868 void
33869 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
33870 {
33871   enum machine_mode mode = GET_MODE (target);
33872   enum machine_mode inner_mode = GET_MODE_INNER (mode);
33873   enum machine_mode half_mode;
33874   bool use_vec_merge = false;
33875   rtx tmp;
33876   static rtx (*gen_extract[6][2]) (rtx, rtx)
33877     = {
33878         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
33879         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
33880         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
33881         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
33882         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
33883         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
33884       };
33885   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
33886     = {
33887         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
33888         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
33889         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
33890         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
33891         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
33892         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
33893       };
33894   int i, j, n;
33895
33896   switch (mode)
33897     {
33898     case V2SFmode:
33899     case V2SImode:
33900       if (mmx_ok)
33901         {
33902           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
33903           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
33904           if (elt == 0)
33905             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
33906           else
33907             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
33908           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33909           return;
33910         }
33911       break;
33912
33913     case V2DImode:
33914       use_vec_merge = TARGET_SSE4_1 && TARGET_64BIT;
33915       if (use_vec_merge)
33916         break;
33917
33918       tmp = gen_reg_rtx (GET_MODE_INNER (mode));
33919       ix86_expand_vector_extract (false, tmp, target, 1 - elt);
33920       if (elt == 0)
33921         tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
33922       else
33923         tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
33924       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33925       return;
33926
33927     case V2DFmode:
33928       {
33929         rtx op0, op1;
33930
33931         /* For the two element vectors, we implement a VEC_CONCAT with
33932            the extraction of the other element.  */
33933
33934         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
33935         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
33936
33937         if (elt == 0)
33938           op0 = val, op1 = tmp;
33939         else
33940           op0 = tmp, op1 = val;
33941
33942         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
33943         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
33944       }
33945       return;
33946
33947     case V4SFmode:
33948       use_vec_merge = TARGET_SSE4_1;
33949       if (use_vec_merge)
33950         break;
33951
33952       switch (elt)
33953         {
33954         case 0:
33955           use_vec_merge = true;
33956           break;
33957
33958         case 1:
33959           /* tmp = target = A B C D */
33960           tmp = copy_to_reg (target);
33961           /* target = A A B B */
33962           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
33963           /* target = X A B B */
33964           ix86_expand_vector_set (false, target, val, 0);
33965           /* target = A X C D  */
33966           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
33967                                           const1_rtx, const0_rtx,
33968                                           GEN_INT (2+4), GEN_INT (3+4)));
33969           return;
33970
33971         case 2:
33972           /* tmp = target = A B C D */
33973           tmp = copy_to_reg (target);
33974           /* tmp = X B C D */
33975           ix86_expand_vector_set (false, tmp, val, 0);
33976           /* target = A B X D */
33977           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
33978                                           const0_rtx, const1_rtx,
33979                                           GEN_INT (0+4), GEN_INT (3+4)));
33980           return;
33981
33982         case 3:
33983           /* tmp = target = A B C D */
33984           tmp = copy_to_reg (target);
33985           /* tmp = X B C D */
33986           ix86_expand_vector_set (false, tmp, val, 0);
33987           /* target = A B X D */
33988           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
33989                                           const0_rtx, const1_rtx,
33990                                           GEN_INT (2+4), GEN_INT (0+4)));
33991           return;
33992
33993         default:
33994           gcc_unreachable ();
33995         }
33996       break;
33997
33998     case V4SImode:
33999       use_vec_merge = TARGET_SSE4_1;
34000       if (use_vec_merge)
34001         break;
34002
34003       /* Element 0 handled by vec_merge below.  */
34004       if (elt == 0)
34005         {
34006           use_vec_merge = true;
34007           break;
34008         }
34009
34010       if (TARGET_SSE2)
34011         {
34012           /* With SSE2, use integer shuffles to swap element 0 and ELT,
34013              store into element 0, then shuffle them back.  */
34014
34015           rtx order[4];
34016
34017           order[0] = GEN_INT (elt);
34018           order[1] = const1_rtx;
34019           order[2] = const2_rtx;
34020           order[3] = GEN_INT (3);
34021           order[elt] = const0_rtx;
34022
34023           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
34024                                         order[1], order[2], order[3]));
34025
34026           ix86_expand_vector_set (false, target, val, 0);
34027
34028           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
34029                                         order[1], order[2], order[3]));
34030         }
34031       else
34032         {
34033           /* For SSE1, we have to reuse the V4SF code.  */
34034           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
34035                                   gen_lowpart (SFmode, val), elt);
34036         }
34037       return;
34038
34039     case V8HImode:
34040       use_vec_merge = TARGET_SSE2;
34041       break;
34042     case V4HImode:
34043       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
34044       break;
34045
34046     case V16QImode:
34047       use_vec_merge = TARGET_SSE4_1;
34048       break;
34049
34050     case V8QImode:
34051       break;
34052
34053     case V32QImode:
34054       half_mode = V16QImode;
34055       j = 0;
34056       n = 16;
34057       goto half;
34058
34059     case V16HImode:
34060       half_mode = V8HImode;
34061       j = 1;
34062       n = 8;
34063       goto half;
34064
34065     case V8SImode:
34066       half_mode = V4SImode;
34067       j = 2;
34068       n = 4;
34069       goto half;
34070
34071     case V4DImode:
34072       half_mode = V2DImode;
34073       j = 3;
34074       n = 2;
34075       goto half;
34076
34077     case V8SFmode:
34078       half_mode = V4SFmode;
34079       j = 4;
34080       n = 4;
34081       goto half;
34082
34083     case V4DFmode:
34084       half_mode = V2DFmode;
34085       j = 5;
34086       n = 2;
34087       goto half;
34088
34089 half:
34090       /* Compute offset.  */
34091       i = elt / n;
34092       elt %= n;
34093
34094       gcc_assert (i <= 1);
34095
34096       /* Extract the half.  */
34097       tmp = gen_reg_rtx (half_mode);
34098       emit_insn (gen_extract[j][i] (tmp, target));
34099
34100       /* Put val in tmp at elt.  */
34101       ix86_expand_vector_set (false, tmp, val, elt);
34102
34103       /* Put it back.  */
34104       emit_insn (gen_insert[j][i] (target, target, tmp));
34105       return;
34106
34107     default:
34108       break;
34109     }
34110
34111   if (use_vec_merge)
34112     {
34113       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
34114       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
34115       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
34116     }
34117   else
34118     {
34119       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
34120
34121       emit_move_insn (mem, target);
34122
34123       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
34124       emit_move_insn (tmp, val);
34125
34126       emit_move_insn (target, mem);
34127     }
34128 }
34129
34130 void
34131 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
34132 {
34133   enum machine_mode mode = GET_MODE (vec);
34134   enum machine_mode inner_mode = GET_MODE_INNER (mode);
34135   bool use_vec_extr = false;
34136   rtx tmp;
34137
34138   switch (mode)
34139     {
34140     case V2SImode:
34141     case V2SFmode:
34142       if (!mmx_ok)
34143         break;
34144       /* FALLTHRU */
34145
34146     case V2DFmode:
34147     case V2DImode:
34148       use_vec_extr = true;
34149       break;
34150
34151     case V4SFmode:
34152       use_vec_extr = TARGET_SSE4_1;
34153       if (use_vec_extr)
34154         break;
34155
34156       switch (elt)
34157         {
34158         case 0:
34159           tmp = vec;
34160           break;
34161
34162         case 1:
34163         case 3:
34164           tmp = gen_reg_rtx (mode);
34165           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
34166                                        GEN_INT (elt), GEN_INT (elt),
34167                                        GEN_INT (elt+4), GEN_INT (elt+4)));
34168           break;
34169
34170         case 2:
34171           tmp = gen_reg_rtx (mode);
34172           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
34173           break;
34174
34175         default:
34176           gcc_unreachable ();
34177         }
34178       vec = tmp;
34179       use_vec_extr = true;
34180       elt = 0;
34181       break;
34182
34183     case V4SImode:
34184       use_vec_extr = TARGET_SSE4_1;
34185       if (use_vec_extr)
34186         break;
34187
34188       if (TARGET_SSE2)
34189         {
34190           switch (elt)
34191             {
34192             case 0:
34193               tmp = vec;
34194               break;
34195
34196             case 1:
34197             case 3:
34198               tmp = gen_reg_rtx (mode);
34199               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
34200                                             GEN_INT (elt), GEN_INT (elt),
34201                                             GEN_INT (elt), GEN_INT (elt)));
34202               break;
34203
34204             case 2:
34205               tmp = gen_reg_rtx (mode);
34206               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
34207               break;
34208
34209             default:
34210               gcc_unreachable ();
34211             }
34212           vec = tmp;
34213           use_vec_extr = true;
34214           elt = 0;
34215         }
34216       else
34217         {
34218           /* For SSE1, we have to reuse the V4SF code.  */
34219           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
34220                                       gen_lowpart (V4SFmode, vec), elt);
34221           return;
34222         }
34223       break;
34224
34225     case V8HImode:
34226       use_vec_extr = TARGET_SSE2;
34227       break;
34228     case V4HImode:
34229       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
34230       break;
34231
34232     case V16QImode:
34233       use_vec_extr = TARGET_SSE4_1;
34234       break;
34235
34236     case V8SFmode:
34237       if (TARGET_AVX)
34238         {
34239           tmp = gen_reg_rtx (V4SFmode);
34240           if (elt < 4)
34241             emit_insn (gen_vec_extract_lo_v8sf (tmp, vec));
34242           else
34243             emit_insn (gen_vec_extract_hi_v8sf (tmp, vec));
34244           ix86_expand_vector_extract (false, target, tmp, elt & 3);
34245           return;
34246         }
34247       break;
34248
34249     case V4DFmode:
34250       if (TARGET_AVX)
34251         {
34252           tmp = gen_reg_rtx (V2DFmode);
34253           if (elt < 2)
34254             emit_insn (gen_vec_extract_lo_v4df (tmp, vec));
34255           else
34256             emit_insn (gen_vec_extract_hi_v4df (tmp, vec));
34257           ix86_expand_vector_extract (false, target, tmp, elt & 1);
34258           return;
34259         }
34260       break;
34261
34262     case V32QImode:
34263       if (TARGET_AVX)
34264         {
34265           tmp = gen_reg_rtx (V16QImode);
34266           if (elt < 16)
34267             emit_insn (gen_vec_extract_lo_v32qi (tmp, vec));
34268           else
34269             emit_insn (gen_vec_extract_hi_v32qi (tmp, vec));
34270           ix86_expand_vector_extract (false, target, tmp, elt & 15);
34271           return;
34272         }
34273       break;
34274
34275     case V16HImode:
34276       if (TARGET_AVX)
34277         {
34278           tmp = gen_reg_rtx (V8HImode);
34279           if (elt < 8)
34280             emit_insn (gen_vec_extract_lo_v16hi (tmp, vec));
34281           else
34282             emit_insn (gen_vec_extract_hi_v16hi (tmp, vec));
34283           ix86_expand_vector_extract (false, target, tmp, elt & 7);
34284           return;
34285         }
34286       break;
34287
34288     case V8SImode:
34289       if (TARGET_AVX)
34290         {
34291           tmp = gen_reg_rtx (V4SImode);
34292           if (elt < 4)
34293             emit_insn (gen_vec_extract_lo_v8si (tmp, vec));
34294           else
34295             emit_insn (gen_vec_extract_hi_v8si (tmp, vec));
34296           ix86_expand_vector_extract (false, target, tmp, elt & 3);
34297           return;
34298         }
34299       break;
34300
34301     case V4DImode:
34302       if (TARGET_AVX)
34303         {
34304           tmp = gen_reg_rtx (V2DImode);
34305           if (elt < 2)
34306             emit_insn (gen_vec_extract_lo_v4di (tmp, vec));
34307           else
34308             emit_insn (gen_vec_extract_hi_v4di (tmp, vec));
34309           ix86_expand_vector_extract (false, target, tmp, elt & 1);
34310           return;
34311         }
34312       break;
34313
34314     case V8QImode:
34315       /* ??? Could extract the appropriate HImode element and shift.  */
34316     default:
34317       break;
34318     }
34319
34320   if (use_vec_extr)
34321     {
34322       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
34323       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
34324
34325       /* Let the rtl optimizers know about the zero extension performed.  */
34326       if (inner_mode == QImode || inner_mode == HImode)
34327         {
34328           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
34329           target = gen_lowpart (SImode, target);
34330         }
34331
34332       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
34333     }
34334   else
34335     {
34336       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
34337
34338       emit_move_insn (mem, vec);
34339
34340       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
34341       emit_move_insn (target, tmp);
34342     }
34343 }
34344
34345 /* Generate code to copy vector bits i / 2 ... i - 1 from vector SRC
34346    to bits 0 ... i / 2 - 1 of vector DEST, which has the same mode.
34347    The upper bits of DEST are undefined, though they shouldn't cause
34348    exceptions (some bits from src or all zeros are ok).  */
34349
34350 static void
34351 emit_reduc_half (rtx dest, rtx src, int i)
34352 {
34353   rtx tem;
34354   switch (GET_MODE (src))
34355     {
34356     case V4SFmode:
34357       if (i == 128)
34358         tem = gen_sse_movhlps (dest, src, src);
34359       else
34360         tem = gen_sse_shufps_v4sf (dest, src, src, const1_rtx, const1_rtx,
34361                                    GEN_INT (1 + 4), GEN_INT (1 + 4));
34362       break;
34363     case V2DFmode:
34364       tem = gen_vec_interleave_highv2df (dest, src, src);
34365       break;
34366     case V16QImode:
34367     case V8HImode:
34368     case V4SImode:
34369     case V2DImode:
34370       tem = gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, dest),
34371                                 gen_lowpart (V1TImode, src),
34372                                 GEN_INT (i / 2));
34373       break;
34374     case V8SFmode:
34375       if (i == 256)
34376         tem = gen_avx_vperm2f128v8sf3 (dest, src, src, const1_rtx);
34377       else
34378         tem = gen_avx_shufps256 (dest, src, src,
34379                                  GEN_INT (i == 128 ? 2 + (3 << 2) : 1));
34380       break;
34381     case V4DFmode:
34382       if (i == 256)
34383         tem = gen_avx_vperm2f128v4df3 (dest, src, src, const1_rtx);
34384       else
34385         tem = gen_avx_shufpd256 (dest, src, src, const1_rtx);
34386       break;
34387     case V32QImode:
34388     case V16HImode:
34389     case V8SImode:
34390     case V4DImode:
34391       if (i == 256)
34392         tem = gen_avx2_permv2ti (gen_lowpart (V4DImode, dest),
34393                                  gen_lowpart (V4DImode, src),
34394                                  gen_lowpart (V4DImode, src),
34395                                  const1_rtx);
34396       else
34397         tem = gen_avx2_lshrv2ti3 (gen_lowpart (V2TImode, dest),
34398                                   gen_lowpart (V2TImode, src),
34399                                   GEN_INT (i / 2));
34400       break;
34401     default:
34402       gcc_unreachable ();
34403     }
34404   emit_insn (tem);
34405 }
34406
34407 /* Expand a vector reduction.  FN is the binary pattern to reduce;
34408    DEST is the destination; IN is the input vector.  */
34409
34410 void
34411 ix86_expand_reduc (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
34412 {
34413   rtx half, dst, vec = in;
34414   enum machine_mode mode = GET_MODE (in);
34415   int i;
34416
34417   /* SSE4 has a special instruction for V8HImode UMIN reduction.  */
34418   if (TARGET_SSE4_1
34419       && mode == V8HImode
34420       && fn == gen_uminv8hi3)
34421     {
34422       emit_insn (gen_sse4_1_phminposuw (dest, in));
34423       return;
34424     }
34425
34426   for (i = GET_MODE_BITSIZE (mode);
34427        i > GET_MODE_BITSIZE (GET_MODE_INNER (mode));
34428        i >>= 1)
34429     {
34430       half = gen_reg_rtx (mode);
34431       emit_reduc_half (half, vec, i);
34432       if (i == GET_MODE_BITSIZE (GET_MODE_INNER (mode)) * 2)
34433         dst = dest;
34434       else
34435         dst = gen_reg_rtx (mode);
34436       emit_insn (fn (dst, half, vec));
34437       vec = dst;
34438     }
34439 }
34440 \f
34441 /* Target hook for scalar_mode_supported_p.  */
34442 static bool
34443 ix86_scalar_mode_supported_p (enum machine_mode mode)
34444 {
34445   if (DECIMAL_FLOAT_MODE_P (mode))
34446     return default_decimal_float_supported_p ();
34447   else if (mode == TFmode)
34448     return true;
34449   else
34450     return default_scalar_mode_supported_p (mode);
34451 }
34452
34453 /* Implements target hook vector_mode_supported_p.  */
34454 static bool
34455 ix86_vector_mode_supported_p (enum machine_mode mode)
34456 {
34457   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
34458     return true;
34459   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
34460     return true;
34461   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
34462     return true;
34463   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
34464     return true;
34465   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
34466     return true;
34467   return false;
34468 }
34469
34470 /* Target hook for c_mode_for_suffix.  */
34471 static enum machine_mode
34472 ix86_c_mode_for_suffix (char suffix)
34473 {
34474   if (suffix == 'q')
34475     return TFmode;
34476   if (suffix == 'w')
34477     return XFmode;
34478
34479   return VOIDmode;
34480 }
34481
34482 /* Worker function for TARGET_MD_ASM_CLOBBERS.
34483
34484    We do this in the new i386 backend to maintain source compatibility
34485    with the old cc0-based compiler.  */
34486
34487 static tree
34488 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
34489                       tree inputs ATTRIBUTE_UNUSED,
34490                       tree clobbers)
34491 {
34492   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
34493                         clobbers);
34494   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
34495                         clobbers);
34496   return clobbers;
34497 }
34498
34499 /* Implements target vector targetm.asm.encode_section_info.  */
34500
34501 static void ATTRIBUTE_UNUSED
34502 ix86_encode_section_info (tree decl, rtx rtl, int first)
34503 {
34504   default_encode_section_info (decl, rtl, first);
34505
34506   if (TREE_CODE (decl) == VAR_DECL
34507       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
34508       && ix86_in_large_data_p (decl))
34509     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
34510 }
34511
34512 /* Worker function for REVERSE_CONDITION.  */
34513
34514 enum rtx_code
34515 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
34516 {
34517   return (mode != CCFPmode && mode != CCFPUmode
34518           ? reverse_condition (code)
34519           : reverse_condition_maybe_unordered (code));
34520 }
34521
34522 /* Output code to perform an x87 FP register move, from OPERANDS[1]
34523    to OPERANDS[0].  */
34524
34525 const char *
34526 output_387_reg_move (rtx insn, rtx *operands)
34527 {
34528   if (REG_P (operands[0]))
34529     {
34530       if (REG_P (operands[1])
34531           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
34532         {
34533           if (REGNO (operands[0]) == FIRST_STACK_REG)
34534             return output_387_ffreep (operands, 0);
34535           return "fstp\t%y0";
34536         }
34537       if (STACK_TOP_P (operands[0]))
34538         return "fld%Z1\t%y1";
34539       return "fst\t%y0";
34540     }
34541   else if (MEM_P (operands[0]))
34542     {
34543       gcc_assert (REG_P (operands[1]));
34544       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
34545         return "fstp%Z0\t%y0";
34546       else
34547         {
34548           /* There is no non-popping store to memory for XFmode.
34549              So if we need one, follow the store with a load.  */
34550           if (GET_MODE (operands[0]) == XFmode)
34551             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
34552           else
34553             return "fst%Z0\t%y0";
34554         }
34555     }
34556   else
34557     gcc_unreachable();
34558 }
34559
34560 /* Output code to perform a conditional jump to LABEL, if C2 flag in
34561    FP status register is set.  */
34562
34563 void
34564 ix86_emit_fp_unordered_jump (rtx label)
34565 {
34566   rtx reg = gen_reg_rtx (HImode);
34567   rtx temp;
34568
34569   emit_insn (gen_x86_fnstsw_1 (reg));
34570
34571   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
34572     {
34573       emit_insn (gen_x86_sahf_1 (reg));
34574
34575       temp = gen_rtx_REG (CCmode, FLAGS_REG);
34576       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
34577     }
34578   else
34579     {
34580       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
34581
34582       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
34583       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
34584     }
34585
34586   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
34587                               gen_rtx_LABEL_REF (VOIDmode, label),
34588                               pc_rtx);
34589   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
34590
34591   emit_jump_insn (temp);
34592   predict_jump (REG_BR_PROB_BASE * 10 / 100);
34593 }
34594
34595 /* Output code to perform a log1p XFmode calculation.  */
34596
34597 void ix86_emit_i387_log1p (rtx op0, rtx op1)
34598 {
34599   rtx label1 = gen_label_rtx ();
34600   rtx label2 = gen_label_rtx ();
34601
34602   rtx tmp = gen_reg_rtx (XFmode);
34603   rtx tmp2 = gen_reg_rtx (XFmode);
34604   rtx test;
34605
34606   emit_insn (gen_absxf2 (tmp, op1));
34607   test = gen_rtx_GE (VOIDmode, tmp,
34608     CONST_DOUBLE_FROM_REAL_VALUE (
34609        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
34610        XFmode));
34611   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
34612
34613   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
34614   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
34615   emit_jump (label2);
34616
34617   emit_label (label1);
34618   emit_move_insn (tmp, CONST1_RTX (XFmode));
34619   emit_insn (gen_addxf3 (tmp, op1, tmp));
34620   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
34621   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
34622
34623   emit_label (label2);
34624 }
34625
34626 /* Emit code for round calculation.  */
34627 void ix86_emit_i387_round (rtx op0, rtx op1)
34628 {
34629   enum machine_mode inmode = GET_MODE (op1);
34630   enum machine_mode outmode = GET_MODE (op0);
34631   rtx e1, e2, res, tmp, tmp1, half;
34632   rtx scratch = gen_reg_rtx (HImode);
34633   rtx flags = gen_rtx_REG (CCNOmode, FLAGS_REG);
34634   rtx jump_label = gen_label_rtx ();
34635   rtx insn;
34636   rtx (*gen_abs) (rtx, rtx);
34637   rtx (*gen_neg) (rtx, rtx);
34638
34639   switch (inmode)
34640     {
34641     case SFmode:
34642       gen_abs = gen_abssf2;
34643       break;
34644     case DFmode:
34645       gen_abs = gen_absdf2;
34646       break;
34647     case XFmode:
34648       gen_abs = gen_absxf2;
34649       break;
34650     default:
34651       gcc_unreachable ();
34652     }
34653
34654   switch (outmode)
34655     {
34656     case SFmode:
34657       gen_neg = gen_negsf2;
34658       break;
34659     case DFmode:
34660       gen_neg = gen_negdf2;
34661       break;
34662     case XFmode:
34663       gen_neg = gen_negxf2;
34664       break;
34665     case HImode:
34666       gen_neg = gen_neghi2;
34667       break;
34668     case SImode:
34669       gen_neg = gen_negsi2;
34670       break;
34671     case DImode:
34672       gen_neg = gen_negdi2;
34673       break;
34674     default:
34675       gcc_unreachable ();
34676     }
34677
34678   e1 = gen_reg_rtx (inmode);
34679   e2 = gen_reg_rtx (inmode);
34680   res = gen_reg_rtx (outmode);
34681
34682   half = CONST_DOUBLE_FROM_REAL_VALUE (dconsthalf, inmode);
34683
34684   /* round(a) = sgn(a) * floor(fabs(a) + 0.5) */
34685
34686   /* scratch = fxam(op1) */
34687   emit_insn (gen_rtx_SET (VOIDmode, scratch,
34688                           gen_rtx_UNSPEC (HImode, gen_rtvec (1, op1),
34689                                           UNSPEC_FXAM)));
34690   /* e1 = fabs(op1) */
34691   emit_insn (gen_abs (e1, op1));
34692
34693   /* e2 = e1 + 0.5 */
34694   half = force_reg (inmode, half);
34695   emit_insn (gen_rtx_SET (VOIDmode, e2,
34696                           gen_rtx_PLUS (inmode, e1, half)));
34697
34698   /* res = floor(e2) */
34699   if (inmode != XFmode)
34700     {
34701       tmp1 = gen_reg_rtx (XFmode);
34702
34703       emit_insn (gen_rtx_SET (VOIDmode, tmp1,
34704                               gen_rtx_FLOAT_EXTEND (XFmode, e2)));
34705     }
34706   else
34707     tmp1 = e2;
34708
34709   switch (outmode)
34710     {
34711     case SFmode:
34712     case DFmode:
34713       {
34714         rtx tmp0 = gen_reg_rtx (XFmode);
34715
34716         emit_insn (gen_frndintxf2_floor (tmp0, tmp1));
34717
34718         emit_insn (gen_rtx_SET (VOIDmode, res,
34719                                 gen_rtx_UNSPEC (outmode, gen_rtvec (1, tmp0),
34720                                                 UNSPEC_TRUNC_NOOP)));
34721       }
34722       break;
34723     case XFmode:
34724       emit_insn (gen_frndintxf2_floor (res, tmp1));
34725       break;
34726     case HImode:
34727       emit_insn (gen_lfloorxfhi2 (res, tmp1));
34728       break;
34729     case SImode:
34730       emit_insn (gen_lfloorxfsi2 (res, tmp1));
34731       break;
34732     case DImode:
34733       emit_insn (gen_lfloorxfdi2 (res, tmp1));
34734         break;
34735     default:
34736       gcc_unreachable ();
34737     }
34738
34739   /* flags = signbit(a) */
34740   emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x02)));
34741
34742   /* if (flags) then res = -res */
34743   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode,
34744                               gen_rtx_EQ (VOIDmode, flags, const0_rtx),
34745                               gen_rtx_LABEL_REF (VOIDmode, jump_label),
34746                               pc_rtx);
34747   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
34748   predict_jump (REG_BR_PROB_BASE * 50 / 100);
34749   JUMP_LABEL (insn) = jump_label;
34750
34751   emit_insn (gen_neg (res, res));
34752
34753   emit_label (jump_label);
34754   LABEL_NUSES (jump_label) = 1;
34755
34756   emit_move_insn (op0, res);
34757 }
34758
34759 /* Output code to perform a Newton-Rhapson approximation of a single precision
34760    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
34761
34762 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
34763 {
34764   rtx x0, x1, e0, e1;
34765
34766   x0 = gen_reg_rtx (mode);
34767   e0 = gen_reg_rtx (mode);
34768   e1 = gen_reg_rtx (mode);
34769   x1 = gen_reg_rtx (mode);
34770
34771   /* a / b = a * ((rcp(b) + rcp(b)) - (b * rcp(b) * rcp (b))) */
34772
34773   b = force_reg (mode, b);
34774
34775   /* x0 = rcp(b) estimate */
34776   emit_insn (gen_rtx_SET (VOIDmode, x0,
34777                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
34778                                           UNSPEC_RCP)));
34779   /* e0 = x0 * b */
34780   emit_insn (gen_rtx_SET (VOIDmode, e0,
34781                           gen_rtx_MULT (mode, x0, b)));
34782
34783   /* e0 = x0 * e0 */
34784   emit_insn (gen_rtx_SET (VOIDmode, e0,
34785                           gen_rtx_MULT (mode, x0, e0)));
34786
34787   /* e1 = x0 + x0 */
34788   emit_insn (gen_rtx_SET (VOIDmode, e1,
34789                           gen_rtx_PLUS (mode, x0, x0)));
34790
34791   /* x1 = e1 - e0 */
34792   emit_insn (gen_rtx_SET (VOIDmode, x1,
34793                           gen_rtx_MINUS (mode, e1, e0)));
34794
34795   /* res = a * x1 */
34796   emit_insn (gen_rtx_SET (VOIDmode, res,
34797                           gen_rtx_MULT (mode, a, x1)));
34798 }
34799
34800 /* Output code to perform a Newton-Rhapson approximation of a
34801    single precision floating point [reciprocal] square root.  */
34802
34803 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
34804                          bool recip)
34805 {
34806   rtx x0, e0, e1, e2, e3, mthree, mhalf;
34807   REAL_VALUE_TYPE r;
34808
34809   x0 = gen_reg_rtx (mode);
34810   e0 = gen_reg_rtx (mode);
34811   e1 = gen_reg_rtx (mode);
34812   e2 = gen_reg_rtx (mode);
34813   e3 = gen_reg_rtx (mode);
34814
34815   real_from_integer (&r, VOIDmode, -3, -1, 0);
34816   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
34817
34818   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
34819   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
34820
34821   if (VECTOR_MODE_P (mode))
34822     {
34823       mthree = ix86_build_const_vector (mode, true, mthree);
34824       mhalf = ix86_build_const_vector (mode, true, mhalf);
34825     }
34826
34827   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
34828      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
34829
34830   a = force_reg (mode, a);
34831
34832   /* x0 = rsqrt(a) estimate */
34833   emit_insn (gen_rtx_SET (VOIDmode, x0,
34834                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
34835                                           UNSPEC_RSQRT)));
34836
34837   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
34838   if (!recip)
34839     {
34840       rtx zero, mask;
34841
34842       zero = gen_reg_rtx (mode);
34843       mask = gen_reg_rtx (mode);
34844
34845       zero = force_reg (mode, CONST0_RTX(mode));
34846       emit_insn (gen_rtx_SET (VOIDmode, mask,
34847                               gen_rtx_NE (mode, zero, a)));
34848
34849       emit_insn (gen_rtx_SET (VOIDmode, x0,
34850                               gen_rtx_AND (mode, x0, mask)));
34851     }
34852
34853   /* e0 = x0 * a */
34854   emit_insn (gen_rtx_SET (VOIDmode, e0,
34855                           gen_rtx_MULT (mode, x0, a)));
34856   /* e1 = e0 * x0 */
34857   emit_insn (gen_rtx_SET (VOIDmode, e1,
34858                           gen_rtx_MULT (mode, e0, x0)));
34859
34860   /* e2 = e1 - 3. */
34861   mthree = force_reg (mode, mthree);
34862   emit_insn (gen_rtx_SET (VOIDmode, e2,
34863                           gen_rtx_PLUS (mode, e1, mthree)));
34864
34865   mhalf = force_reg (mode, mhalf);
34866   if (recip)
34867     /* e3 = -.5 * x0 */
34868     emit_insn (gen_rtx_SET (VOIDmode, e3,
34869                             gen_rtx_MULT (mode, x0, mhalf)));
34870   else
34871     /* e3 = -.5 * e0 */
34872     emit_insn (gen_rtx_SET (VOIDmode, e3,
34873                             gen_rtx_MULT (mode, e0, mhalf)));
34874   /* ret = e2 * e3 */
34875   emit_insn (gen_rtx_SET (VOIDmode, res,
34876                           gen_rtx_MULT (mode, e2, e3)));
34877 }
34878
34879 #ifdef TARGET_SOLARIS
34880 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
34881
34882 static void
34883 i386_solaris_elf_named_section (const char *name, unsigned int flags,
34884                                 tree decl)
34885 {
34886   /* With Binutils 2.15, the "@unwind" marker must be specified on
34887      every occurrence of the ".eh_frame" section, not just the first
34888      one.  */
34889   if (TARGET_64BIT
34890       && strcmp (name, ".eh_frame") == 0)
34891     {
34892       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
34893                flags & SECTION_WRITE ? "aw" : "a");
34894       return;
34895     }
34896
34897 #ifndef USE_GAS
34898   if (HAVE_COMDAT_GROUP && flags & SECTION_LINKONCE)
34899     {
34900       solaris_elf_asm_comdat_section (name, flags, decl);
34901       return;
34902     }
34903 #endif
34904
34905   default_elf_asm_named_section (name, flags, decl);
34906 }
34907 #endif /* TARGET_SOLARIS */
34908
34909 /* Return the mangling of TYPE if it is an extended fundamental type.  */
34910
34911 static const char *
34912 ix86_mangle_type (const_tree type)
34913 {
34914   type = TYPE_MAIN_VARIANT (type);
34915
34916   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
34917       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
34918     return NULL;
34919
34920   switch (TYPE_MODE (type))
34921     {
34922     case TFmode:
34923       /* __float128 is "g".  */
34924       return "g";
34925     case XFmode:
34926       /* "long double" or __float80 is "e".  */
34927       return "e";
34928     default:
34929       return NULL;
34930     }
34931 }
34932
34933 /* For 32-bit code we can save PIC register setup by using
34934    __stack_chk_fail_local hidden function instead of calling
34935    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
34936    register, so it is better to call __stack_chk_fail directly.  */
34937
34938 static tree ATTRIBUTE_UNUSED
34939 ix86_stack_protect_fail (void)
34940 {
34941   return TARGET_64BIT
34942          ? default_external_stack_protect_fail ()
34943          : default_hidden_stack_protect_fail ();
34944 }
34945
34946 /* Select a format to encode pointers in exception handling data.  CODE
34947    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
34948    true if the symbol may be affected by dynamic relocations.
34949
34950    ??? All x86 object file formats are capable of representing this.
34951    After all, the relocation needed is the same as for the call insn.
34952    Whether or not a particular assembler allows us to enter such, I
34953    guess we'll have to see.  */
34954 int
34955 asm_preferred_eh_data_format (int code, int global)
34956 {
34957   if (flag_pic)
34958     {
34959       int type = DW_EH_PE_sdata8;
34960       if (!TARGET_64BIT
34961           || ix86_cmodel == CM_SMALL_PIC
34962           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
34963         type = DW_EH_PE_sdata4;
34964       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
34965     }
34966   if (ix86_cmodel == CM_SMALL
34967       || (ix86_cmodel == CM_MEDIUM && code))
34968     return DW_EH_PE_udata4;
34969   return DW_EH_PE_absptr;
34970 }
34971 \f
34972 /* Expand copysign from SIGN to the positive value ABS_VALUE
34973    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
34974    the sign-bit.  */
34975 static void
34976 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
34977 {
34978   enum machine_mode mode = GET_MODE (sign);
34979   rtx sgn = gen_reg_rtx (mode);
34980   if (mask == NULL_RTX)
34981     {
34982       enum machine_mode vmode;
34983
34984       if (mode == SFmode)
34985         vmode = V4SFmode;
34986       else if (mode == DFmode)
34987         vmode = V2DFmode;
34988       else
34989         vmode = mode;
34990
34991       mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), false);
34992       if (!VECTOR_MODE_P (mode))
34993         {
34994           /* We need to generate a scalar mode mask in this case.  */
34995           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
34996           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
34997           mask = gen_reg_rtx (mode);
34998           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
34999         }
35000     }
35001   else
35002     mask = gen_rtx_NOT (mode, mask);
35003   emit_insn (gen_rtx_SET (VOIDmode, sgn,
35004                           gen_rtx_AND (mode, mask, sign)));
35005   emit_insn (gen_rtx_SET (VOIDmode, result,
35006                           gen_rtx_IOR (mode, abs_value, sgn)));
35007 }
35008
35009 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
35010    mask for masking out the sign-bit is stored in *SMASK, if that is
35011    non-null.  */
35012 static rtx
35013 ix86_expand_sse_fabs (rtx op0, rtx *smask)
35014 {
35015   enum machine_mode vmode, mode = GET_MODE (op0);
35016   rtx xa, mask;
35017
35018   xa = gen_reg_rtx (mode);
35019   if (mode == SFmode)
35020     vmode = V4SFmode;
35021   else if (mode == DFmode)
35022     vmode = V2DFmode;
35023   else
35024     vmode = mode;
35025   mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), true);
35026   if (!VECTOR_MODE_P (mode))
35027     {
35028       /* We need to generate a scalar mode mask in this case.  */
35029       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
35030       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
35031       mask = gen_reg_rtx (mode);
35032       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
35033     }
35034   emit_insn (gen_rtx_SET (VOIDmode, xa,
35035                           gen_rtx_AND (mode, op0, mask)));
35036
35037   if (smask)
35038     *smask = mask;
35039
35040   return xa;
35041 }
35042
35043 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
35044    swapping the operands if SWAP_OPERANDS is true.  The expanded
35045    code is a forward jump to a newly created label in case the
35046    comparison is true.  The generated label rtx is returned.  */
35047 static rtx
35048 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
35049                                   bool swap_operands)
35050 {
35051   rtx label, tmp;
35052
35053   if (swap_operands)
35054     {
35055       tmp = op0;
35056       op0 = op1;
35057       op1 = tmp;
35058     }
35059
35060   label = gen_label_rtx ();
35061   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
35062   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35063                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
35064   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
35065   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
35066                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
35067   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
35068   JUMP_LABEL (tmp) = label;
35069
35070   return label;
35071 }
35072
35073 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
35074    using comparison code CODE.  Operands are swapped for the comparison if
35075    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
35076 static rtx
35077 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
35078                               bool swap_operands)
35079 {
35080   rtx (*insn)(rtx, rtx, rtx, rtx);
35081   enum machine_mode mode = GET_MODE (op0);
35082   rtx mask = gen_reg_rtx (mode);
35083
35084   if (swap_operands)
35085     {
35086       rtx tmp = op0;
35087       op0 = op1;
35088       op1 = tmp;
35089     }
35090
35091   insn = mode == DFmode ? gen_setcc_df_sse : gen_setcc_sf_sse;
35092
35093   emit_insn (insn (mask, op0, op1,
35094                    gen_rtx_fmt_ee (code, mode, op0, op1)));
35095   return mask;
35096 }
35097
35098 /* Generate and return a rtx of mode MODE for 2**n where n is the number
35099    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
35100 static rtx
35101 ix86_gen_TWO52 (enum machine_mode mode)
35102 {
35103   REAL_VALUE_TYPE TWO52r;
35104   rtx TWO52;
35105
35106   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
35107   TWO52 = const_double_from_real_value (TWO52r, mode);
35108   TWO52 = force_reg (mode, TWO52);
35109
35110   return TWO52;
35111 }
35112
35113 /* Expand SSE sequence for computing lround from OP1 storing
35114    into OP0.  */
35115 void
35116 ix86_expand_lround (rtx op0, rtx op1)
35117 {
35118   /* C code for the stuff we're doing below:
35119        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
35120        return (long)tmp;
35121    */
35122   enum machine_mode mode = GET_MODE (op1);
35123   const struct real_format *fmt;
35124   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
35125   rtx adj;
35126
35127   /* load nextafter (0.5, 0.0) */
35128   fmt = REAL_MODE_FORMAT (mode);
35129   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
35130   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
35131
35132   /* adj = copysign (0.5, op1) */
35133   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
35134   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
35135
35136   /* adj = op1 + adj */
35137   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
35138
35139   /* op0 = (imode)adj */
35140   expand_fix (op0, adj, 0);
35141 }
35142
35143 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
35144    into OPERAND0.  */
35145 void
35146 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
35147 {
35148   /* C code for the stuff we're doing below (for do_floor):
35149         xi = (long)op1;
35150         xi -= (double)xi > op1 ? 1 : 0;
35151         return xi;
35152    */
35153   enum machine_mode fmode = GET_MODE (op1);
35154   enum machine_mode imode = GET_MODE (op0);
35155   rtx ireg, freg, label, tmp;
35156
35157   /* reg = (long)op1 */
35158   ireg = gen_reg_rtx (imode);
35159   expand_fix (ireg, op1, 0);
35160
35161   /* freg = (double)reg */
35162   freg = gen_reg_rtx (fmode);
35163   expand_float (freg, ireg, 0);
35164
35165   /* ireg = (freg > op1) ? ireg - 1 : ireg */
35166   label = ix86_expand_sse_compare_and_jump (UNLE,
35167                                             freg, op1, !do_floor);
35168   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
35169                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
35170   emit_move_insn (ireg, tmp);
35171
35172   emit_label (label);
35173   LABEL_NUSES (label) = 1;
35174
35175   emit_move_insn (op0, ireg);
35176 }
35177
35178 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
35179    result in OPERAND0.  */
35180 void
35181 ix86_expand_rint (rtx operand0, rtx operand1)
35182 {
35183   /* C code for the stuff we're doing below:
35184         xa = fabs (operand1);
35185         if (!isless (xa, 2**52))
35186           return operand1;
35187         xa = xa + 2**52 - 2**52;
35188         return copysign (xa, operand1);
35189    */
35190   enum machine_mode mode = GET_MODE (operand0);
35191   rtx res, xa, label, TWO52, mask;
35192
35193   res = gen_reg_rtx (mode);
35194   emit_move_insn (res, operand1);
35195
35196   /* xa = abs (operand1) */
35197   xa = ix86_expand_sse_fabs (res, &mask);
35198
35199   /* if (!isless (xa, TWO52)) goto label; */
35200   TWO52 = ix86_gen_TWO52 (mode);
35201   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35202
35203   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35204   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
35205
35206   ix86_sse_copysign_to_positive (res, xa, res, mask);
35207
35208   emit_label (label);
35209   LABEL_NUSES (label) = 1;
35210
35211   emit_move_insn (operand0, res);
35212 }
35213
35214 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
35215    into OPERAND0.  */
35216 void
35217 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
35218 {
35219   /* C code for the stuff we expand below.
35220         double xa = fabs (x), x2;
35221         if (!isless (xa, TWO52))
35222           return x;
35223         xa = xa + TWO52 - TWO52;
35224         x2 = copysign (xa, x);
35225      Compensate.  Floor:
35226         if (x2 > x)
35227           x2 -= 1;
35228      Compensate.  Ceil:
35229         if (x2 < x)
35230           x2 -= -1;
35231         return x2;
35232    */
35233   enum machine_mode mode = GET_MODE (operand0);
35234   rtx xa, TWO52, tmp, label, one, res, mask;
35235
35236   TWO52 = ix86_gen_TWO52 (mode);
35237
35238   /* Temporary for holding the result, initialized to the input
35239      operand to ease control flow.  */
35240   res = gen_reg_rtx (mode);
35241   emit_move_insn (res, operand1);
35242
35243   /* xa = abs (operand1) */
35244   xa = ix86_expand_sse_fabs (res, &mask);
35245
35246   /* if (!isless (xa, TWO52)) goto label; */
35247   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35248
35249   /* xa = xa + TWO52 - TWO52; */
35250   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35251   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
35252
35253   /* xa = copysign (xa, operand1) */
35254   ix86_sse_copysign_to_positive (xa, xa, res, mask);
35255
35256   /* generate 1.0 or -1.0 */
35257   one = force_reg (mode,
35258                    const_double_from_real_value (do_floor
35259                                                  ? dconst1 : dconstm1, mode));
35260
35261   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
35262   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
35263   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35264                           gen_rtx_AND (mode, one, tmp)));
35265   /* We always need to subtract here to preserve signed zero.  */
35266   tmp = expand_simple_binop (mode, MINUS,
35267                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35268   emit_move_insn (res, tmp);
35269
35270   emit_label (label);
35271   LABEL_NUSES (label) = 1;
35272
35273   emit_move_insn (operand0, res);
35274 }
35275
35276 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
35277    into OPERAND0.  */
35278 void
35279 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
35280 {
35281   /* C code for the stuff we expand below.
35282         double xa = fabs (x), x2;
35283         if (!isless (xa, TWO52))
35284           return x;
35285         x2 = (double)(long)x;
35286      Compensate.  Floor:
35287         if (x2 > x)
35288           x2 -= 1;
35289      Compensate.  Ceil:
35290         if (x2 < x)
35291           x2 += 1;
35292         if (HONOR_SIGNED_ZEROS (mode))
35293           return copysign (x2, x);
35294         return x2;
35295    */
35296   enum machine_mode mode = GET_MODE (operand0);
35297   rtx xa, xi, TWO52, tmp, label, one, res, mask;
35298
35299   TWO52 = ix86_gen_TWO52 (mode);
35300
35301   /* Temporary for holding the result, initialized to the input
35302      operand to ease control flow.  */
35303   res = gen_reg_rtx (mode);
35304   emit_move_insn (res, operand1);
35305
35306   /* xa = abs (operand1) */
35307   xa = ix86_expand_sse_fabs (res, &mask);
35308
35309   /* if (!isless (xa, TWO52)) goto label; */
35310   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35311
35312   /* xa = (double)(long)x */
35313   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
35314   expand_fix (xi, res, 0);
35315   expand_float (xa, xi, 0);
35316
35317   /* generate 1.0 */
35318   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
35319
35320   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
35321   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
35322   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35323                           gen_rtx_AND (mode, one, tmp)));
35324   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
35325                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35326   emit_move_insn (res, tmp);
35327
35328   if (HONOR_SIGNED_ZEROS (mode))
35329     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
35330
35331   emit_label (label);
35332   LABEL_NUSES (label) = 1;
35333
35334   emit_move_insn (operand0, res);
35335 }
35336
35337 /* Expand SSE sequence for computing round from OPERAND1 storing
35338    into OPERAND0.  Sequence that works without relying on DImode truncation
35339    via cvttsd2siq that is only available on 64bit targets.  */
35340 void
35341 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
35342 {
35343   /* C code for the stuff we expand below.
35344         double xa = fabs (x), xa2, x2;
35345         if (!isless (xa, TWO52))
35346           return x;
35347      Using the absolute value and copying back sign makes
35348      -0.0 -> -0.0 correct.
35349         xa2 = xa + TWO52 - TWO52;
35350      Compensate.
35351         dxa = xa2 - xa;
35352         if (dxa <= -0.5)
35353           xa2 += 1;
35354         else if (dxa > 0.5)
35355           xa2 -= 1;
35356         x2 = copysign (xa2, x);
35357         return x2;
35358    */
35359   enum machine_mode mode = GET_MODE (operand0);
35360   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
35361
35362   TWO52 = ix86_gen_TWO52 (mode);
35363
35364   /* Temporary for holding the result, initialized to the input
35365      operand to ease control flow.  */
35366   res = gen_reg_rtx (mode);
35367   emit_move_insn (res, operand1);
35368
35369   /* xa = abs (operand1) */
35370   xa = ix86_expand_sse_fabs (res, &mask);
35371
35372   /* if (!isless (xa, TWO52)) goto label; */
35373   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35374
35375   /* xa2 = xa + TWO52 - TWO52; */
35376   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35377   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
35378
35379   /* dxa = xa2 - xa; */
35380   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
35381
35382   /* generate 0.5, 1.0 and -0.5 */
35383   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
35384   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
35385   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
35386                                0, OPTAB_DIRECT);
35387
35388   /* Compensate.  */
35389   tmp = gen_reg_rtx (mode);
35390   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
35391   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
35392   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35393                           gen_rtx_AND (mode, one, tmp)));
35394   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35395   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
35396   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
35397   emit_insn (gen_rtx_SET (VOIDmode, tmp,
35398                           gen_rtx_AND (mode, one, tmp)));
35399   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
35400
35401   /* res = copysign (xa2, operand1) */
35402   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
35403
35404   emit_label (label);
35405   LABEL_NUSES (label) = 1;
35406
35407   emit_move_insn (operand0, res);
35408 }
35409
35410 /* Expand SSE sequence for computing trunc from OPERAND1 storing
35411    into OPERAND0.  */
35412 void
35413 ix86_expand_trunc (rtx operand0, rtx operand1)
35414 {
35415   /* C code for SSE variant we expand below.
35416         double xa = fabs (x), x2;
35417         if (!isless (xa, TWO52))
35418           return x;
35419         x2 = (double)(long)x;
35420         if (HONOR_SIGNED_ZEROS (mode))
35421           return copysign (x2, x);
35422         return x2;
35423    */
35424   enum machine_mode mode = GET_MODE (operand0);
35425   rtx xa, xi, TWO52, label, res, mask;
35426
35427   TWO52 = ix86_gen_TWO52 (mode);
35428
35429   /* Temporary for holding the result, initialized to the input
35430      operand to ease control flow.  */
35431   res = gen_reg_rtx (mode);
35432   emit_move_insn (res, operand1);
35433
35434   /* xa = abs (operand1) */
35435   xa = ix86_expand_sse_fabs (res, &mask);
35436
35437   /* if (!isless (xa, TWO52)) goto label; */
35438   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35439
35440   /* x = (double)(long)x */
35441   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
35442   expand_fix (xi, res, 0);
35443   expand_float (res, xi, 0);
35444
35445   if (HONOR_SIGNED_ZEROS (mode))
35446     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
35447
35448   emit_label (label);
35449   LABEL_NUSES (label) = 1;
35450
35451   emit_move_insn (operand0, res);
35452 }
35453
35454 /* Expand SSE sequence for computing trunc from OPERAND1 storing
35455    into OPERAND0.  */
35456 void
35457 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
35458 {
35459   enum machine_mode mode = GET_MODE (operand0);
35460   rtx xa, mask, TWO52, label, one, res, smask, tmp;
35461
35462   /* C code for SSE variant we expand below.
35463         double xa = fabs (x), x2;
35464         if (!isless (xa, TWO52))
35465           return x;
35466         xa2 = xa + TWO52 - TWO52;
35467      Compensate:
35468         if (xa2 > xa)
35469           xa2 -= 1.0;
35470         x2 = copysign (xa2, x);
35471         return x2;
35472    */
35473
35474   TWO52 = ix86_gen_TWO52 (mode);
35475
35476   /* Temporary for holding the result, initialized to the input
35477      operand to ease control flow.  */
35478   res = gen_reg_rtx (mode);
35479   emit_move_insn (res, operand1);
35480
35481   /* xa = abs (operand1) */
35482   xa = ix86_expand_sse_fabs (res, &smask);
35483
35484   /* if (!isless (xa, TWO52)) goto label; */
35485   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35486
35487   /* res = xa + TWO52 - TWO52; */
35488   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
35489   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
35490   emit_move_insn (res, tmp);
35491
35492   /* generate 1.0 */
35493   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
35494
35495   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
35496   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
35497   emit_insn (gen_rtx_SET (VOIDmode, mask,
35498                           gen_rtx_AND (mode, mask, one)));
35499   tmp = expand_simple_binop (mode, MINUS,
35500                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
35501   emit_move_insn (res, tmp);
35502
35503   /* res = copysign (res, operand1) */
35504   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
35505
35506   emit_label (label);
35507   LABEL_NUSES (label) = 1;
35508
35509   emit_move_insn (operand0, res);
35510 }
35511
35512 /* Expand SSE sequence for computing round from OPERAND1 storing
35513    into OPERAND0.  */
35514 void
35515 ix86_expand_round (rtx operand0, rtx operand1)
35516 {
35517   /* C code for the stuff we're doing below:
35518         double xa = fabs (x);
35519         if (!isless (xa, TWO52))
35520           return x;
35521         xa = (double)(long)(xa + nextafter (0.5, 0.0));
35522         return copysign (xa, x);
35523    */
35524   enum machine_mode mode = GET_MODE (operand0);
35525   rtx res, TWO52, xa, label, xi, half, mask;
35526   const struct real_format *fmt;
35527   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
35528
35529   /* Temporary for holding the result, initialized to the input
35530      operand to ease control flow.  */
35531   res = gen_reg_rtx (mode);
35532   emit_move_insn (res, operand1);
35533
35534   TWO52 = ix86_gen_TWO52 (mode);
35535   xa = ix86_expand_sse_fabs (res, &mask);
35536   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
35537
35538   /* load nextafter (0.5, 0.0) */
35539   fmt = REAL_MODE_FORMAT (mode);
35540   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
35541   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
35542
35543   /* xa = xa + 0.5 */
35544   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
35545   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
35546
35547   /* xa = (double)(int64_t)xa */
35548   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
35549   expand_fix (xi, xa, 0);
35550   expand_float (xa, xi, 0);
35551
35552   /* res = copysign (xa, operand1) */
35553   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
35554
35555   emit_label (label);
35556   LABEL_NUSES (label) = 1;
35557
35558   emit_move_insn (operand0, res);
35559 }
35560
35561 /* Expand SSE sequence for computing round
35562    from OP1 storing into OP0 using sse4 round insn.  */
35563 void
35564 ix86_expand_round_sse4 (rtx op0, rtx op1)
35565 {
35566   enum machine_mode mode = GET_MODE (op0);
35567   rtx e1, e2, res, half;
35568   const struct real_format *fmt;
35569   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
35570   rtx (*gen_copysign) (rtx, rtx, rtx);
35571   rtx (*gen_round) (rtx, rtx, rtx);
35572
35573   switch (mode)
35574     {
35575     case SFmode:
35576       gen_copysign = gen_copysignsf3;
35577       gen_round = gen_sse4_1_roundsf2;
35578       break;
35579     case DFmode:
35580       gen_copysign = gen_copysigndf3;
35581       gen_round = gen_sse4_1_rounddf2;
35582       break;
35583     default:
35584       gcc_unreachable ();
35585     }
35586
35587   /* round (a) = trunc (a + copysign (0.5, a)) */
35588
35589   /* load nextafter (0.5, 0.0) */
35590   fmt = REAL_MODE_FORMAT (mode);
35591   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
35592   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
35593   half = const_double_from_real_value (pred_half, mode);
35594
35595   /* e1 = copysign (0.5, op1) */
35596   e1 = gen_reg_rtx (mode);
35597   emit_insn (gen_copysign (e1, half, op1));
35598
35599   /* e2 = op1 + e1 */
35600   e2 = expand_simple_binop (mode, PLUS, op1, e1, NULL_RTX, 0, OPTAB_DIRECT);
35601
35602   /* res = trunc (e2) */
35603   res = gen_reg_rtx (mode);
35604   emit_insn (gen_round (res, e2, GEN_INT (ROUND_TRUNC)));
35605
35606   emit_move_insn (op0, res);
35607 }
35608 \f
35609
35610 /* Table of valid machine attributes.  */
35611 static const struct attribute_spec ix86_attribute_table[] =
35612 {
35613   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
35614        affects_type_identity } */
35615   /* Stdcall attribute says callee is responsible for popping arguments
35616      if they are not variable.  */
35617   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35618     true },
35619   /* Fastcall attribute says callee is responsible for popping arguments
35620      if they are not variable.  */
35621   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35622     true },
35623   /* Thiscall attribute says callee is responsible for popping arguments
35624      if they are not variable.  */
35625   { "thiscall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35626     true },
35627   /* Cdecl attribute says the callee is a normal C declaration */
35628   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute,
35629     true },
35630   /* Regparm attribute specifies how many integer arguments are to be
35631      passed in registers.  */
35632   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute,
35633     true },
35634   /* Sseregparm attribute says we are using x86_64 calling conventions
35635      for FP arguments.  */
35636   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute,
35637     true },
35638   /* The transactional memory builtins are implicitly regparm or fastcall
35639      depending on the ABI.  Override the generic do-nothing attribute that
35640      these builtins were declared with.  */
35641   { "*tm regparm", 0, 0, false, true, true, ix86_handle_tm_regparm_attribute,
35642     true },
35643   /* force_align_arg_pointer says this function realigns the stack at entry.  */
35644   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
35645     false, true,  true, ix86_handle_cconv_attribute, false },
35646 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
35647   { "dllimport", 0, 0, false, false, false, handle_dll_attribute, false },
35648   { "dllexport", 0, 0, false, false, false, handle_dll_attribute, false },
35649   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute,
35650     false },
35651 #endif
35652   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute,
35653     false },
35654   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute,
35655     false },
35656 #ifdef SUBTARGET_ATTRIBUTE_TABLE
35657   SUBTARGET_ATTRIBUTE_TABLE,
35658 #endif
35659   /* ms_abi and sysv_abi calling convention function attributes.  */
35660   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute, true },
35661   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute, true },
35662   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute,
35663     false },
35664   { "callee_pop_aggregate_return", 1, 1, false, true, true,
35665     ix86_handle_callee_pop_aggregate_return, true },
35666   /* End element.  */
35667   { NULL,        0, 0, false, false, false, NULL, false }
35668 };
35669
35670 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
35671 static int
35672 ix86_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
35673                                  tree vectype ATTRIBUTE_UNUSED,
35674                                  int misalign ATTRIBUTE_UNUSED)
35675 {
35676   switch (type_of_cost)
35677     {
35678       case scalar_stmt:
35679         return ix86_cost->scalar_stmt_cost;
35680
35681       case scalar_load:
35682         return ix86_cost->scalar_load_cost;
35683
35684       case scalar_store:
35685         return ix86_cost->scalar_store_cost;
35686
35687       case vector_stmt:
35688         return ix86_cost->vec_stmt_cost;
35689
35690       case vector_load:
35691         return ix86_cost->vec_align_load_cost;
35692
35693       case vector_store:
35694         return ix86_cost->vec_store_cost;
35695
35696       case vec_to_scalar:
35697         return ix86_cost->vec_to_scalar_cost;
35698
35699       case scalar_to_vec:
35700         return ix86_cost->scalar_to_vec_cost;
35701
35702       case unaligned_load:
35703       case unaligned_store:
35704         return ix86_cost->vec_unalign_load_cost;
35705
35706       case cond_branch_taken:
35707         return ix86_cost->cond_taken_branch_cost;
35708
35709       case cond_branch_not_taken:
35710         return ix86_cost->cond_not_taken_branch_cost;
35711
35712       case vec_perm:
35713       case vec_promote_demote:
35714         return ix86_cost->vec_stmt_cost;
35715
35716       default:
35717         gcc_unreachable ();
35718     }
35719 }
35720
35721 /* Construct (set target (vec_select op0 (parallel perm))) and
35722    return true if that's a valid instruction in the active ISA.  */
35723
35724 static bool
35725 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
35726 {
35727   rtx rperm[MAX_VECT_LEN], x;
35728   unsigned i;
35729
35730   for (i = 0; i < nelt; ++i)
35731     rperm[i] = GEN_INT (perm[i]);
35732
35733   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
35734   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
35735   x = gen_rtx_SET (VOIDmode, target, x);
35736
35737   x = emit_insn (x);
35738   if (recog_memoized (x) < 0)
35739     {
35740       remove_insn (x);
35741       return false;
35742     }
35743   return true;
35744 }
35745
35746 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
35747
35748 static bool
35749 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
35750                         const unsigned char *perm, unsigned nelt)
35751 {
35752   enum machine_mode v2mode;
35753   rtx x;
35754
35755   v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
35756   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
35757   return expand_vselect (target, x, perm, nelt);
35758 }
35759
35760 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
35761    in terms of blendp[sd] / pblendw / pblendvb / vpblendd.  */
35762
35763 static bool
35764 expand_vec_perm_blend (struct expand_vec_perm_d *d)
35765 {
35766   enum machine_mode vmode = d->vmode;
35767   unsigned i, mask, nelt = d->nelt;
35768   rtx target, op0, op1, x;
35769   rtx rperm[32], vperm;
35770
35771   if (d->op0 == d->op1)
35772     return false;
35773   if (TARGET_AVX2 && GET_MODE_SIZE (vmode) == 32)
35774     ;
35775   else if (TARGET_AVX && (vmode == V4DFmode || vmode == V8SFmode))
35776     ;
35777   else if (TARGET_SSE4_1 && GET_MODE_SIZE (vmode) == 16)
35778     ;
35779   else
35780     return false;
35781
35782   /* This is a blend, not a permute.  Elements must stay in their
35783      respective lanes.  */
35784   for (i = 0; i < nelt; ++i)
35785     {
35786       unsigned e = d->perm[i];
35787       if (!(e == i || e == i + nelt))
35788         return false;
35789     }
35790
35791   if (d->testing_p)
35792     return true;
35793
35794   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
35795      decision should be extracted elsewhere, so that we only try that
35796      sequence once all budget==3 options have been tried.  */
35797   target = d->target;
35798   op0 = d->op0;
35799   op1 = d->op1;
35800   mask = 0;
35801
35802   switch (vmode)
35803     {
35804     case V4DFmode:
35805     case V8SFmode:
35806     case V2DFmode:
35807     case V4SFmode:
35808     case V8HImode:
35809     case V8SImode:
35810       for (i = 0; i < nelt; ++i)
35811         mask |= (d->perm[i] >= nelt) << i;
35812       break;
35813
35814     case V2DImode:
35815       for (i = 0; i < 2; ++i)
35816         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
35817       vmode = V8HImode;
35818       goto do_subreg;
35819
35820     case V4SImode:
35821       for (i = 0; i < 4; ++i)
35822         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
35823       vmode = V8HImode;
35824       goto do_subreg;
35825
35826     case V16QImode:
35827       /* See if bytes move in pairs so we can use pblendw with
35828          an immediate argument, rather than pblendvb with a vector
35829          argument.  */
35830       for (i = 0; i < 16; i += 2)
35831         if (d->perm[i] + 1 != d->perm[i + 1])
35832           {
35833           use_pblendvb:
35834             for (i = 0; i < nelt; ++i)
35835               rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
35836
35837           finish_pblendvb:
35838             vperm = gen_rtx_CONST_VECTOR (vmode, gen_rtvec_v (nelt, rperm));
35839             vperm = force_reg (vmode, vperm);
35840
35841             if (GET_MODE_SIZE (vmode) == 16)
35842               emit_insn (gen_sse4_1_pblendvb (target, op0, op1, vperm));
35843             else
35844               emit_insn (gen_avx2_pblendvb (target, op0, op1, vperm));
35845             return true;
35846           }
35847
35848       for (i = 0; i < 8; ++i)
35849         mask |= (d->perm[i * 2] >= 16) << i;
35850       vmode = V8HImode;
35851       /* FALLTHRU */
35852
35853     do_subreg:
35854       target = gen_lowpart (vmode, target);
35855       op0 = gen_lowpart (vmode, op0);
35856       op1 = gen_lowpart (vmode, op1);
35857       break;
35858
35859     case V32QImode:
35860       /* See if bytes move in pairs.  If not, vpblendvb must be used.  */
35861       for (i = 0; i < 32; i += 2)
35862         if (d->perm[i] + 1 != d->perm[i + 1])
35863           goto use_pblendvb;
35864       /* See if bytes move in quadruplets.  If yes, vpblendd
35865          with immediate can be used.  */
35866       for (i = 0; i < 32; i += 4)
35867         if (d->perm[i] + 2 != d->perm[i + 2])
35868           break;
35869       if (i < 32)
35870         {
35871           /* See if bytes move the same in both lanes.  If yes,
35872              vpblendw with immediate can be used.  */
35873           for (i = 0; i < 16; i += 2)
35874             if (d->perm[i] + 16 != d->perm[i + 16])
35875               goto use_pblendvb;
35876
35877           /* Use vpblendw.  */
35878           for (i = 0; i < 16; ++i)
35879             mask |= (d->perm[i * 2] >= 32) << i;
35880           vmode = V16HImode;
35881           goto do_subreg;
35882         }
35883
35884       /* Use vpblendd.  */
35885       for (i = 0; i < 8; ++i)
35886         mask |= (d->perm[i * 4] >= 32) << i;
35887       vmode = V8SImode;
35888       goto do_subreg;
35889
35890     case V16HImode:
35891       /* See if words move in pairs.  If yes, vpblendd can be used.  */
35892       for (i = 0; i < 16; i += 2)
35893         if (d->perm[i] + 1 != d->perm[i + 1])
35894           break;
35895       if (i < 16)
35896         {
35897           /* See if words move the same in both lanes.  If not,
35898              vpblendvb must be used.  */
35899           for (i = 0; i < 8; i++)
35900             if (d->perm[i] + 8 != d->perm[i + 8])
35901               {
35902                 /* Use vpblendvb.  */
35903                 for (i = 0; i < 32; ++i)
35904                   rperm[i] = (d->perm[i / 2] < 16 ? const0_rtx : constm1_rtx);
35905
35906                 vmode = V32QImode;
35907                 nelt = 32;
35908                 target = gen_lowpart (vmode, target);
35909                 op0 = gen_lowpart (vmode, op0);
35910                 op1 = gen_lowpart (vmode, op1);
35911                 goto finish_pblendvb;
35912               }
35913
35914           /* Use vpblendw.  */
35915           for (i = 0; i < 16; ++i)
35916             mask |= (d->perm[i] >= 16) << i;
35917           break;
35918         }
35919
35920       /* Use vpblendd.  */
35921       for (i = 0; i < 8; ++i)
35922         mask |= (d->perm[i * 2] >= 16) << i;
35923       vmode = V8SImode;
35924       goto do_subreg;
35925
35926     case V4DImode:
35927       /* Use vpblendd.  */
35928       for (i = 0; i < 4; ++i)
35929         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
35930       vmode = V8SImode;
35931       goto do_subreg;
35932
35933     default:
35934       gcc_unreachable ();
35935     }
35936
35937   /* This matches five different patterns with the different modes.  */
35938   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
35939   x = gen_rtx_SET (VOIDmode, target, x);
35940   emit_insn (x);
35941
35942   return true;
35943 }
35944
35945 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
35946    in terms of the variable form of vpermilps.
35947
35948    Note that we will have already failed the immediate input vpermilps,
35949    which requires that the high and low part shuffle be identical; the
35950    variable form doesn't require that.  */
35951
35952 static bool
35953 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
35954 {
35955   rtx rperm[8], vperm;
35956   unsigned i;
35957
35958   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
35959     return false;
35960
35961   /* We can only permute within the 128-bit lane.  */
35962   for (i = 0; i < 8; ++i)
35963     {
35964       unsigned e = d->perm[i];
35965       if (i < 4 ? e >= 4 : e < 4)
35966         return false;
35967     }
35968
35969   if (d->testing_p)
35970     return true;
35971
35972   for (i = 0; i < 8; ++i)
35973     {
35974       unsigned e = d->perm[i];
35975
35976       /* Within each 128-bit lane, the elements of op0 are numbered
35977          from 0 and the elements of op1 are numbered from 4.  */
35978       if (e >= 8 + 4)
35979         e -= 8;
35980       else if (e >= 4)
35981         e -= 4;
35982
35983       rperm[i] = GEN_INT (e);
35984     }
35985
35986   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
35987   vperm = force_reg (V8SImode, vperm);
35988   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
35989
35990   return true;
35991 }
35992
35993 /* Return true if permutation D can be performed as VMODE permutation
35994    instead.  */
35995
35996 static bool
35997 valid_perm_using_mode_p (enum machine_mode vmode, struct expand_vec_perm_d *d)
35998 {
35999   unsigned int i, j, chunk;
36000
36001   if (GET_MODE_CLASS (vmode) != MODE_VECTOR_INT
36002       || GET_MODE_CLASS (d->vmode) != MODE_VECTOR_INT
36003       || GET_MODE_SIZE (vmode) != GET_MODE_SIZE (d->vmode))
36004     return false;
36005
36006   if (GET_MODE_NUNITS (vmode) >= d->nelt)
36007     return true;
36008
36009   chunk = d->nelt / GET_MODE_NUNITS (vmode);
36010   for (i = 0; i < d->nelt; i += chunk)
36011     if (d->perm[i] & (chunk - 1))
36012       return false;
36013     else
36014       for (j = 1; j < chunk; ++j)
36015         if (d->perm[i] + j != d->perm[i + j])
36016           return false;
36017
36018   return true;
36019 }
36020
36021 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
36022    in terms of pshufb, vpperm, vpermq, vpermd or vperm2i128.  */
36023
36024 static bool
36025 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
36026 {
36027   unsigned i, nelt, eltsz, mask;
36028   unsigned char perm[32];
36029   enum machine_mode vmode = V16QImode;
36030   rtx rperm[32], vperm, target, op0, op1;
36031
36032   nelt = d->nelt;
36033
36034   if (d->op0 != d->op1)
36035     {
36036       if (!TARGET_XOP || GET_MODE_SIZE (d->vmode) != 16)
36037         {
36038           if (TARGET_AVX2
36039               && valid_perm_using_mode_p (V2TImode, d))
36040             {
36041               if (d->testing_p)
36042                 return true;
36043
36044               /* Use vperm2i128 insn.  The pattern uses
36045                  V4DImode instead of V2TImode.  */
36046               target = gen_lowpart (V4DImode, d->target);
36047               op0 = gen_lowpart (V4DImode, d->op0);
36048               op1 = gen_lowpart (V4DImode, d->op1);
36049               rperm[0]
36050                 = GEN_INT (((d->perm[0] & (nelt / 2)) ? 1 : 0)
36051                            || ((d->perm[nelt / 2] & (nelt / 2)) ? 2 : 0));
36052               emit_insn (gen_avx2_permv2ti (target, op0, op1, rperm[0]));
36053               return true;
36054             }
36055           return false;
36056         }
36057     }
36058   else
36059     {
36060       if (GET_MODE_SIZE (d->vmode) == 16)
36061         {
36062           if (!TARGET_SSSE3)
36063             return false;
36064         }
36065       else if (GET_MODE_SIZE (d->vmode) == 32)
36066         {
36067           if (!TARGET_AVX2)
36068             return false;
36069
36070           /* V4DImode should be already handled through
36071              expand_vselect by vpermq instruction.  */
36072           gcc_assert (d->vmode != V4DImode);
36073
36074           vmode = V32QImode;
36075           if (d->vmode == V8SImode
36076               || d->vmode == V16HImode
36077               || d->vmode == V32QImode)
36078             {
36079               /* First see if vpermq can be used for
36080                  V8SImode/V16HImode/V32QImode.  */
36081               if (valid_perm_using_mode_p (V4DImode, d))
36082                 {
36083                   for (i = 0; i < 4; i++)
36084                     perm[i] = (d->perm[i * nelt / 4] * 4 / nelt) & 3;
36085                   if (d->testing_p)
36086                     return true;
36087                   return expand_vselect (gen_lowpart (V4DImode, d->target),
36088                                          gen_lowpart (V4DImode, d->op0),
36089                                          perm, 4);
36090                 }
36091
36092               /* Next see if vpermd can be used.  */
36093               if (valid_perm_using_mode_p (V8SImode, d))
36094                 vmode = V8SImode;
36095             }
36096
36097           if (vmode == V32QImode)
36098             {
36099               /* vpshufb only works intra lanes, it is not
36100                  possible to shuffle bytes in between the lanes.  */
36101               for (i = 0; i < nelt; ++i)
36102                 if ((d->perm[i] ^ i) & (nelt / 2))
36103                   return false;
36104             }
36105         }
36106       else
36107         return false;
36108     }
36109
36110   if (d->testing_p)
36111     return true;
36112
36113   if (vmode == V8SImode)
36114     for (i = 0; i < 8; ++i)
36115       rperm[i] = GEN_INT ((d->perm[i * nelt / 8] * 8 / nelt) & 7);
36116   else
36117     {
36118       eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36119       if (d->op0 != d->op1)
36120         mask = 2 * nelt - 1;
36121       else if (vmode == V16QImode)
36122         mask = nelt - 1;
36123       else
36124         mask = nelt / 2 - 1;
36125
36126       for (i = 0; i < nelt; ++i)
36127         {
36128           unsigned j, e = d->perm[i] & mask;
36129           for (j = 0; j < eltsz; ++j)
36130             rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
36131         }
36132     }
36133
36134   vperm = gen_rtx_CONST_VECTOR (vmode,
36135                                 gen_rtvec_v (GET_MODE_NUNITS (vmode), rperm));
36136   vperm = force_reg (vmode, vperm);
36137
36138   target = gen_lowpart (vmode, d->target);
36139   op0 = gen_lowpart (vmode, d->op0);
36140   if (d->op0 == d->op1)
36141     {
36142       if (vmode == V16QImode)
36143         emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
36144       else if (vmode == V32QImode)
36145         emit_insn (gen_avx2_pshufbv32qi3 (target, op0, vperm));
36146       else
36147         emit_insn (gen_avx2_permvarv8si (target, op0, vperm));
36148     }
36149   else
36150     {
36151       op1 = gen_lowpart (vmode, d->op1);
36152       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
36153     }
36154
36155   return true;
36156 }
36157
36158 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
36159    in a single instruction.  */
36160
36161 static bool
36162 expand_vec_perm_1 (struct expand_vec_perm_d *d)
36163 {
36164   unsigned i, nelt = d->nelt;
36165   unsigned char perm2[MAX_VECT_LEN];
36166
36167   /* Check plain VEC_SELECT first, because AVX has instructions that could
36168      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
36169      input where SEL+CONCAT may not.  */
36170   if (d->op0 == d->op1)
36171     {
36172       int mask = nelt - 1;
36173       bool identity_perm = true;
36174       bool broadcast_perm = true;
36175
36176       for (i = 0; i < nelt; i++)
36177         {
36178           perm2[i] = d->perm[i] & mask;
36179           if (perm2[i] != i)
36180             identity_perm = false;
36181           if (perm2[i])
36182             broadcast_perm = false;
36183         }
36184
36185       if (identity_perm)
36186         {
36187           if (!d->testing_p)
36188             emit_move_insn (d->target, d->op0);
36189           return true;
36190         }
36191       else if (broadcast_perm && TARGET_AVX2)
36192         {
36193           /* Use vpbroadcast{b,w,d}.  */
36194           rtx op = d->op0, (*gen) (rtx, rtx) = NULL;
36195           switch (d->vmode)
36196             {
36197             case V32QImode:
36198               op = gen_lowpart (V16QImode, op);
36199               gen = gen_avx2_pbroadcastv32qi;
36200               break;
36201             case V16HImode:
36202               op = gen_lowpart (V8HImode, op);
36203               gen = gen_avx2_pbroadcastv16hi;
36204               break;
36205             case V8SImode:
36206               op = gen_lowpart (V4SImode, op);
36207               gen = gen_avx2_pbroadcastv8si;
36208               break;
36209             case V16QImode:
36210               gen = gen_avx2_pbroadcastv16qi;
36211               break;
36212             case V8HImode:
36213               gen = gen_avx2_pbroadcastv8hi;
36214               break;
36215             /* For other modes prefer other shuffles this function creates.  */
36216             default: break;
36217             }
36218           if (gen != NULL)
36219             {
36220               if (!d->testing_p)
36221                 emit_insn (gen (d->target, op));
36222               return true;
36223             }
36224         }
36225
36226       if (expand_vselect (d->target, d->op0, perm2, nelt))
36227         return true;
36228
36229       /* There are plenty of patterns in sse.md that are written for
36230          SEL+CONCAT and are not replicated for a single op.  Perhaps
36231          that should be changed, to avoid the nastiness here.  */
36232
36233       /* Recognize interleave style patterns, which means incrementing
36234          every other permutation operand.  */
36235       for (i = 0; i < nelt; i += 2)
36236         {
36237           perm2[i] = d->perm[i] & mask;
36238           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
36239         }
36240       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
36241         return true;
36242
36243       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
36244       if (nelt >= 4)
36245         {
36246           for (i = 0; i < nelt; i += 4)
36247             {
36248               perm2[i + 0] = d->perm[i + 0] & mask;
36249               perm2[i + 1] = d->perm[i + 1] & mask;
36250               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
36251               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
36252             }
36253
36254           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
36255             return true;
36256         }
36257     }
36258
36259   /* Finally, try the fully general two operand permute.  */
36260   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
36261     return true;
36262
36263   /* Recognize interleave style patterns with reversed operands.  */
36264   if (d->op0 != d->op1)
36265     {
36266       for (i = 0; i < nelt; ++i)
36267         {
36268           unsigned e = d->perm[i];
36269           if (e >= nelt)
36270             e -= nelt;
36271           else
36272             e += nelt;
36273           perm2[i] = e;
36274         }
36275
36276       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
36277         return true;
36278     }
36279
36280   /* Try the SSE4.1 blend variable merge instructions.  */
36281   if (expand_vec_perm_blend (d))
36282     return true;
36283
36284   /* Try one of the AVX vpermil variable permutations.  */
36285   if (expand_vec_perm_vpermil (d))
36286     return true;
36287
36288   /* Try the SSSE3 pshufb or XOP vpperm or AVX2 vperm2i128,
36289      vpshufb, vpermd or vpermq variable permutation.  */
36290   if (expand_vec_perm_pshufb (d))
36291     return true;
36292
36293   return false;
36294 }
36295
36296 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
36297    in terms of a pair of pshuflw + pshufhw instructions.  */
36298
36299 static bool
36300 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
36301 {
36302   unsigned char perm2[MAX_VECT_LEN];
36303   unsigned i;
36304   bool ok;
36305
36306   if (d->vmode != V8HImode || d->op0 != d->op1)
36307     return false;
36308
36309   /* The two permutations only operate in 64-bit lanes.  */
36310   for (i = 0; i < 4; ++i)
36311     if (d->perm[i] >= 4)
36312       return false;
36313   for (i = 4; i < 8; ++i)
36314     if (d->perm[i] < 4)
36315       return false;
36316
36317   if (d->testing_p)
36318     return true;
36319
36320   /* Emit the pshuflw.  */
36321   memcpy (perm2, d->perm, 4);
36322   for (i = 4; i < 8; ++i)
36323     perm2[i] = i;
36324   ok = expand_vselect (d->target, d->op0, perm2, 8);
36325   gcc_assert (ok);
36326
36327   /* Emit the pshufhw.  */
36328   memcpy (perm2 + 4, d->perm + 4, 4);
36329   for (i = 0; i < 4; ++i)
36330     perm2[i] = i;
36331   ok = expand_vselect (d->target, d->target, perm2, 8);
36332   gcc_assert (ok);
36333
36334   return true;
36335 }
36336
36337 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36338    the permutation using the SSSE3 palignr instruction.  This succeeds
36339    when all of the elements in PERM fit within one vector and we merely
36340    need to shift them down so that a single vector permutation has a
36341    chance to succeed.  */
36342
36343 static bool
36344 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
36345 {
36346   unsigned i, nelt = d->nelt;
36347   unsigned min, max;
36348   bool in_order, ok;
36349   rtx shift;
36350
36351   /* Even with AVX, palignr only operates on 128-bit vectors.  */
36352   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
36353     return false;
36354
36355   min = nelt, max = 0;
36356   for (i = 0; i < nelt; ++i)
36357     {
36358       unsigned e = d->perm[i];
36359       if (e < min)
36360         min = e;
36361       if (e > max)
36362         max = e;
36363     }
36364   if (min == 0 || max - min >= nelt)
36365     return false;
36366
36367   /* Given that we have SSSE3, we know we'll be able to implement the
36368      single operand permutation after the palignr with pshufb.  */
36369   if (d->testing_p)
36370     return true;
36371
36372   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
36373   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
36374                                   gen_lowpart (TImode, d->op1),
36375                                   gen_lowpart (TImode, d->op0), shift));
36376
36377   d->op0 = d->op1 = d->target;
36378
36379   in_order = true;
36380   for (i = 0; i < nelt; ++i)
36381     {
36382       unsigned e = d->perm[i] - min;
36383       if (e != i)
36384         in_order = false;
36385       d->perm[i] = e;
36386     }
36387
36388   /* Test for the degenerate case where the alignment by itself
36389      produces the desired permutation.  */
36390   if (in_order)
36391     return true;
36392
36393   ok = expand_vec_perm_1 (d);
36394   gcc_assert (ok);
36395
36396   return ok;
36397 }
36398
36399 static bool expand_vec_perm_interleave3 (struct expand_vec_perm_d *d);
36400
36401 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36402    a two vector permutation into a single vector permutation by using
36403    an interleave operation to merge the vectors.  */
36404
36405 static bool
36406 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
36407 {
36408   struct expand_vec_perm_d dremap, dfinal;
36409   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
36410   unsigned HOST_WIDE_INT contents;
36411   unsigned char remap[2 * MAX_VECT_LEN];
36412   rtx seq;
36413   bool ok, same_halves = false;
36414
36415   if (GET_MODE_SIZE (d->vmode) == 16)
36416     {
36417       if (d->op0 == d->op1)
36418         return false;
36419     }
36420   else if (GET_MODE_SIZE (d->vmode) == 32)
36421     {
36422       if (!TARGET_AVX)
36423         return false;
36424       /* For 32-byte modes allow even d->op0 == d->op1.
36425          The lack of cross-lane shuffling in some instructions
36426          might prevent a single insn shuffle.  */
36427       dfinal = *d;
36428       dfinal.testing_p = true;
36429       /* If expand_vec_perm_interleave3 can expand this into
36430          a 3 insn sequence, give up and let it be expanded as
36431          3 insn sequence.  While that is one insn longer,
36432          it doesn't need a memory operand and in the common
36433          case that both interleave low and high permutations
36434          with the same operands are adjacent needs 4 insns
36435          for both after CSE.  */
36436       if (expand_vec_perm_interleave3 (&dfinal))
36437         return false;
36438     }
36439   else
36440     return false;
36441
36442   /* Examine from whence the elements come.  */
36443   contents = 0;
36444   for (i = 0; i < nelt; ++i)
36445     contents |= ((unsigned HOST_WIDE_INT) 1) << d->perm[i];
36446
36447   memset (remap, 0xff, sizeof (remap));
36448   dremap = *d;
36449
36450   if (GET_MODE_SIZE (d->vmode) == 16)
36451     {
36452       unsigned HOST_WIDE_INT h1, h2, h3, h4;
36453
36454       /* Split the two input vectors into 4 halves.  */
36455       h1 = (((unsigned HOST_WIDE_INT) 1) << nelt2) - 1;
36456       h2 = h1 << nelt2;
36457       h3 = h2 << nelt2;
36458       h4 = h3 << nelt2;
36459
36460       /* If the elements from the low halves use interleave low, and similarly
36461          for interleave high.  If the elements are from mis-matched halves, we
36462          can use shufps for V4SF/V4SI or do a DImode shuffle.  */
36463       if ((contents & (h1 | h3)) == contents)
36464         {
36465           /* punpckl* */
36466           for (i = 0; i < nelt2; ++i)
36467             {
36468               remap[i] = i * 2;
36469               remap[i + nelt] = i * 2 + 1;
36470               dremap.perm[i * 2] = i;
36471               dremap.perm[i * 2 + 1] = i + nelt;
36472             }
36473           if (!TARGET_SSE2 && d->vmode == V4SImode)
36474             dremap.vmode = V4SFmode;
36475         }
36476       else if ((contents & (h2 | h4)) == contents)
36477         {
36478           /* punpckh* */
36479           for (i = 0; i < nelt2; ++i)
36480             {
36481               remap[i + nelt2] = i * 2;
36482               remap[i + nelt + nelt2] = i * 2 + 1;
36483               dremap.perm[i * 2] = i + nelt2;
36484               dremap.perm[i * 2 + 1] = i + nelt + nelt2;
36485             }
36486           if (!TARGET_SSE2 && d->vmode == V4SImode)
36487             dremap.vmode = V4SFmode;
36488         }
36489       else if ((contents & (h1 | h4)) == contents)
36490         {
36491           /* shufps */
36492           for (i = 0; i < nelt2; ++i)
36493             {
36494               remap[i] = i;
36495               remap[i + nelt + nelt2] = i + nelt2;
36496               dremap.perm[i] = i;
36497               dremap.perm[i + nelt2] = i + nelt + nelt2;
36498             }
36499           if (nelt != 4)
36500             {
36501               /* shufpd */
36502               dremap.vmode = V2DImode;
36503               dremap.nelt = 2;
36504               dremap.perm[0] = 0;
36505               dremap.perm[1] = 3;
36506             }
36507         }
36508       else if ((contents & (h2 | h3)) == contents)
36509         {
36510           /* shufps */
36511           for (i = 0; i < nelt2; ++i)
36512             {
36513               remap[i + nelt2] = i;
36514               remap[i + nelt] = i + nelt2;
36515               dremap.perm[i] = i + nelt2;
36516               dremap.perm[i + nelt2] = i + nelt;
36517             }
36518           if (nelt != 4)
36519             {
36520               /* shufpd */
36521               dremap.vmode = V2DImode;
36522               dremap.nelt = 2;
36523               dremap.perm[0] = 1;
36524               dremap.perm[1] = 2;
36525             }
36526         }
36527       else
36528         return false;
36529     }
36530   else
36531     {
36532       unsigned int nelt4 = nelt / 4, nzcnt = 0;
36533       unsigned HOST_WIDE_INT q[8];
36534       unsigned int nonzero_halves[4];
36535
36536       /* Split the two input vectors into 8 quarters.  */
36537       q[0] = (((unsigned HOST_WIDE_INT) 1) << nelt4) - 1;
36538       for (i = 1; i < 8; ++i)
36539         q[i] = q[0] << (nelt4 * i);
36540       for (i = 0; i < 4; ++i)
36541         if (((q[2 * i] | q[2 * i + 1]) & contents) != 0)
36542           {
36543             nonzero_halves[nzcnt] = i;
36544             ++nzcnt;
36545           }
36546
36547       if (nzcnt == 1)
36548         {
36549           gcc_assert (d->op0 == d->op1);
36550           nonzero_halves[1] = nonzero_halves[0];
36551           same_halves = true;
36552         }
36553       else if (d->op0 == d->op1)
36554         {
36555           gcc_assert (nonzero_halves[0] == 0);
36556           gcc_assert (nonzero_halves[1] == 1);
36557         }
36558
36559       if (nzcnt <= 2)
36560         {
36561           if (d->perm[0] / nelt2 == nonzero_halves[1])
36562             {
36563               /* Attempt to increase the likelyhood that dfinal
36564                  shuffle will be intra-lane.  */
36565               char tmph = nonzero_halves[0];
36566               nonzero_halves[0] = nonzero_halves[1];
36567               nonzero_halves[1] = tmph;
36568             }
36569
36570           /* vperm2f128 or vperm2i128.  */
36571           for (i = 0; i < nelt2; ++i)
36572             {
36573               remap[i + nonzero_halves[1] * nelt2] = i + nelt2;
36574               remap[i + nonzero_halves[0] * nelt2] = i;
36575               dremap.perm[i + nelt2] = i + nonzero_halves[1] * nelt2;
36576               dremap.perm[i] = i + nonzero_halves[0] * nelt2;
36577             }
36578
36579           if (d->vmode != V8SFmode
36580               && d->vmode != V4DFmode
36581               && d->vmode != V8SImode)
36582             {
36583               dremap.vmode = V8SImode;
36584               dremap.nelt = 8;
36585               for (i = 0; i < 4; ++i)
36586                 {
36587                   dremap.perm[i] = i + nonzero_halves[0] * 4;
36588                   dremap.perm[i + 4] = i + nonzero_halves[1] * 4;
36589                 }
36590             }
36591         }
36592       else if (d->op0 == d->op1)
36593         return false;
36594       else if (TARGET_AVX2
36595                && (contents & (q[0] | q[2] | q[4] | q[6])) == contents)
36596         {
36597           /* vpunpckl* */
36598           for (i = 0; i < nelt4; ++i)
36599             {
36600               remap[i] = i * 2;
36601               remap[i + nelt] = i * 2 + 1;
36602               remap[i + nelt2] = i * 2 + nelt2;
36603               remap[i + nelt + nelt2] = i * 2 + nelt2 + 1;
36604               dremap.perm[i * 2] = i;
36605               dremap.perm[i * 2 + 1] = i + nelt;
36606               dremap.perm[i * 2 + nelt2] = i + nelt2;
36607               dremap.perm[i * 2 + nelt2 + 1] = i + nelt + nelt2;
36608             }
36609         }
36610       else if (TARGET_AVX2
36611                && (contents & (q[1] | q[3] | q[5] | q[7])) == contents)
36612         {
36613           /* vpunpckh* */
36614           for (i = 0; i < nelt4; ++i)
36615             {
36616               remap[i + nelt4] = i * 2;
36617               remap[i + nelt + nelt4] = i * 2 + 1;
36618               remap[i + nelt2 + nelt4] = i * 2 + nelt2;
36619               remap[i + nelt + nelt2 + nelt4] = i * 2 + nelt2 + 1;
36620               dremap.perm[i * 2] = i + nelt4;
36621               dremap.perm[i * 2 + 1] = i + nelt + nelt4;
36622               dremap.perm[i * 2 + nelt2] = i + nelt2 + nelt4;
36623               dremap.perm[i * 2 + nelt2 + 1] = i + nelt + nelt2 + nelt4;
36624             }
36625         }
36626       else
36627         return false;
36628     }
36629
36630   /* Use the remapping array set up above to move the elements from their
36631      swizzled locations into their final destinations.  */
36632   dfinal = *d;
36633   for (i = 0; i < nelt; ++i)
36634     {
36635       unsigned e = remap[d->perm[i]];
36636       gcc_assert (e < nelt);
36637       /* If same_halves is true, both halves of the remapped vector are the
36638          same.  Avoid cross-lane accesses if possible.  */
36639       if (same_halves && i >= nelt2)
36640         {
36641           gcc_assert (e < nelt2);
36642           dfinal.perm[i] = e + nelt2;
36643         }
36644       else
36645         dfinal.perm[i] = e;
36646     }
36647   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
36648   dfinal.op1 = dfinal.op0;
36649   dremap.target = dfinal.op0;
36650
36651   /* Test if the final remap can be done with a single insn.  For V4SFmode or
36652      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
36653   start_sequence ();
36654   ok = expand_vec_perm_1 (&dfinal);
36655   seq = get_insns ();
36656   end_sequence ();
36657
36658   if (!ok)
36659     return false;
36660
36661   if (d->testing_p)
36662     return true;
36663
36664   if (dremap.vmode != dfinal.vmode)
36665     {
36666       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
36667       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
36668       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
36669     }
36670
36671   ok = expand_vec_perm_1 (&dremap);
36672   gcc_assert (ok);
36673
36674   emit_insn (seq);
36675   return true;
36676 }
36677
36678 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36679    a single vector cross-lane permutation into vpermq followed
36680    by any of the single insn permutations.  */
36681
36682 static bool
36683 expand_vec_perm_vpermq_perm_1 (struct expand_vec_perm_d *d)
36684 {
36685   struct expand_vec_perm_d dremap, dfinal;
36686   unsigned i, j, nelt = d->nelt, nelt2 = nelt / 2, nelt4 = nelt / 4;
36687   unsigned contents[2];
36688   bool ok;
36689
36690   if (!(TARGET_AVX2
36691         && (d->vmode == V32QImode || d->vmode == V16HImode)
36692         && d->op0 == d->op1))
36693     return false;
36694
36695   contents[0] = 0;
36696   contents[1] = 0;
36697   for (i = 0; i < nelt2; ++i)
36698     {
36699       contents[0] |= 1u << (d->perm[i] / nelt4);
36700       contents[1] |= 1u << (d->perm[i + nelt2] / nelt4);
36701     }
36702
36703   for (i = 0; i < 2; ++i)
36704     {
36705       unsigned int cnt = 0;
36706       for (j = 0; j < 4; ++j)
36707         if ((contents[i] & (1u << j)) != 0 && ++cnt > 2)
36708           return false;
36709     }
36710
36711   if (d->testing_p)
36712     return true;
36713
36714   dremap = *d;
36715   dremap.vmode = V4DImode;
36716   dremap.nelt = 4;
36717   dremap.target = gen_reg_rtx (V4DImode);
36718   dremap.op0 = gen_lowpart (V4DImode, d->op0);
36719   dremap.op1 = dremap.op0;
36720   for (i = 0; i < 2; ++i)
36721     {
36722       unsigned int cnt = 0;
36723       for (j = 0; j < 4; ++j)
36724         if ((contents[i] & (1u << j)) != 0)
36725           dremap.perm[2 * i + cnt++] = j;
36726       for (; cnt < 2; ++cnt)
36727         dremap.perm[2 * i + cnt] = 0;
36728     }
36729
36730   dfinal = *d;
36731   dfinal.op0 = gen_lowpart (dfinal.vmode, dremap.target);
36732   dfinal.op1 = dfinal.op0;
36733   for (i = 0, j = 0; i < nelt; ++i)
36734     {
36735       if (i == nelt2)
36736         j = 2;
36737       dfinal.perm[i] = (d->perm[i] & (nelt4 - 1)) | (j ? nelt2 : 0);
36738       if ((d->perm[i] / nelt4) == dremap.perm[j])
36739         ;
36740       else if ((d->perm[i] / nelt4) == dremap.perm[j + 1])
36741         dfinal.perm[i] |= nelt4;
36742       else
36743         gcc_unreachable ();
36744     }
36745
36746   ok = expand_vec_perm_1 (&dremap);
36747   gcc_assert (ok);
36748
36749   ok = expand_vec_perm_1 (&dfinal);
36750   gcc_assert (ok);
36751
36752   return true;
36753 }
36754
36755 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
36756    a two vector permutation using 2 intra-lane interleave insns
36757    and cross-lane shuffle for 32-byte vectors.  */
36758
36759 static bool
36760 expand_vec_perm_interleave3 (struct expand_vec_perm_d *d)
36761 {
36762   unsigned i, nelt;
36763   rtx (*gen) (rtx, rtx, rtx);
36764
36765   if (d->op0 == d->op1)
36766     return false;
36767   if (TARGET_AVX2 && GET_MODE_SIZE (d->vmode) == 32)
36768     ;
36769   else if (TARGET_AVX && (d->vmode == V8SFmode || d->vmode == V4DFmode))
36770     ;
36771   else
36772     return false;
36773
36774   nelt = d->nelt;
36775   if (d->perm[0] != 0 && d->perm[0] != nelt / 2)
36776     return false;
36777   for (i = 0; i < nelt; i += 2)
36778     if (d->perm[i] != d->perm[0] + i / 2
36779         || d->perm[i + 1] != d->perm[0] + i / 2 + nelt)
36780       return false;
36781
36782   if (d->testing_p)
36783     return true;
36784
36785   switch (d->vmode)
36786     {
36787     case V32QImode:
36788       if (d->perm[0])
36789         gen = gen_vec_interleave_highv32qi;
36790       else
36791         gen = gen_vec_interleave_lowv32qi;
36792       break;
36793     case V16HImode:
36794       if (d->perm[0])
36795         gen = gen_vec_interleave_highv16hi;
36796       else
36797         gen = gen_vec_interleave_lowv16hi;
36798       break;
36799     case V8SImode:
36800       if (d->perm[0])
36801         gen = gen_vec_interleave_highv8si;
36802       else
36803         gen = gen_vec_interleave_lowv8si;
36804       break;
36805     case V4DImode:
36806       if (d->perm[0])
36807         gen = gen_vec_interleave_highv4di;
36808       else
36809         gen = gen_vec_interleave_lowv4di;
36810       break;
36811     case V8SFmode:
36812       if (d->perm[0])
36813         gen = gen_vec_interleave_highv8sf;
36814       else
36815         gen = gen_vec_interleave_lowv8sf;
36816       break;
36817     case V4DFmode:
36818       if (d->perm[0])
36819         gen = gen_vec_interleave_highv4df;
36820       else
36821         gen = gen_vec_interleave_lowv4df;
36822       break;
36823     default:
36824       gcc_unreachable ();
36825     }
36826
36827   emit_insn (gen (d->target, d->op0, d->op1));
36828   return true;
36829 }
36830
36831 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
36832    permutation with two pshufb insns and an ior.  We should have already
36833    failed all two instruction sequences.  */
36834
36835 static bool
36836 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
36837 {
36838   rtx rperm[2][16], vperm, l, h, op, m128;
36839   unsigned int i, nelt, eltsz;
36840
36841   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
36842     return false;
36843   gcc_assert (d->op0 != d->op1);
36844
36845   nelt = d->nelt;
36846   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36847
36848   /* Generate two permutation masks.  If the required element is within
36849      the given vector it is shuffled into the proper lane.  If the required
36850      element is in the other vector, force a zero into the lane by setting
36851      bit 7 in the permutation mask.  */
36852   m128 = GEN_INT (-128);
36853   for (i = 0; i < nelt; ++i)
36854     {
36855       unsigned j, e = d->perm[i];
36856       unsigned which = (e >= nelt);
36857       if (e >= nelt)
36858         e -= nelt;
36859
36860       for (j = 0; j < eltsz; ++j)
36861         {
36862           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
36863           rperm[1-which][i*eltsz + j] = m128;
36864         }
36865     }
36866
36867   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
36868   vperm = force_reg (V16QImode, vperm);
36869
36870   l = gen_reg_rtx (V16QImode);
36871   op = gen_lowpart (V16QImode, d->op0);
36872   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
36873
36874   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
36875   vperm = force_reg (V16QImode, vperm);
36876
36877   h = gen_reg_rtx (V16QImode);
36878   op = gen_lowpart (V16QImode, d->op1);
36879   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
36880
36881   op = gen_lowpart (V16QImode, d->target);
36882   emit_insn (gen_iorv16qi3 (op, l, h));
36883
36884   return true;
36885 }
36886
36887 /* Implement arbitrary permutation of one V32QImode and V16QImode operand
36888    with two vpshufb insns, vpermq and vpor.  We should have already failed
36889    all two or three instruction sequences.  */
36890
36891 static bool
36892 expand_vec_perm_vpshufb2_vpermq (struct expand_vec_perm_d *d)
36893 {
36894   rtx rperm[2][32], vperm, l, h, hp, op, m128;
36895   unsigned int i, nelt, eltsz;
36896
36897   if (!TARGET_AVX2
36898       || d->op0 != d->op1
36899       || (d->vmode != V32QImode && d->vmode != V16HImode))
36900     return false;
36901
36902   if (d->testing_p)
36903     return true;
36904
36905   nelt = d->nelt;
36906   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36907
36908   /* Generate two permutation masks.  If the required element is within
36909      the same lane, it is shuffled in.  If the required element from the
36910      other lane, force a zero by setting bit 7 in the permutation mask.
36911      In the other mask the mask has non-negative elements if element
36912      is requested from the other lane, but also moved to the other lane,
36913      so that the result of vpshufb can have the two V2TImode halves
36914      swapped.  */
36915   m128 = GEN_INT (-128);
36916   for (i = 0; i < nelt; ++i)
36917     {
36918       unsigned j, e = d->perm[i] & (nelt / 2 - 1);
36919       unsigned which = ((d->perm[i] ^ i) & (nelt / 2)) * eltsz;
36920
36921       for (j = 0; j < eltsz; ++j)
36922         {
36923           rperm[!!which][(i * eltsz + j) ^ which] = GEN_INT (e * eltsz + j);
36924           rperm[!which][(i * eltsz + j) ^ (which ^ 16)] = m128;
36925         }
36926     }
36927
36928   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[1]));
36929   vperm = force_reg (V32QImode, vperm);
36930
36931   h = gen_reg_rtx (V32QImode);
36932   op = gen_lowpart (V32QImode, d->op0);
36933   emit_insn (gen_avx2_pshufbv32qi3 (h, op, vperm));
36934
36935   /* Swap the 128-byte lanes of h into hp.  */
36936   hp = gen_reg_rtx (V4DImode);
36937   op = gen_lowpart (V4DImode, h);
36938   emit_insn (gen_avx2_permv4di_1 (hp, op, const2_rtx, GEN_INT (3), const0_rtx,
36939                                   const1_rtx));
36940
36941   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[0]));
36942   vperm = force_reg (V32QImode, vperm);
36943
36944   l = gen_reg_rtx (V32QImode);
36945   op = gen_lowpart (V32QImode, d->op0);
36946   emit_insn (gen_avx2_pshufbv32qi3 (l, op, vperm));
36947
36948   op = gen_lowpart (V32QImode, d->target);
36949   emit_insn (gen_iorv32qi3 (op, l, gen_lowpart (V32QImode, hp)));
36950
36951   return true;
36952 }
36953
36954 /* A subroutine of expand_vec_perm_even_odd_1.  Implement extract-even
36955    and extract-odd permutations of two V32QImode and V16QImode operand
36956    with two vpshufb insns, vpor and vpermq.  We should have already
36957    failed all two or three instruction sequences.  */
36958
36959 static bool
36960 expand_vec_perm_vpshufb2_vpermq_even_odd (struct expand_vec_perm_d *d)
36961 {
36962   rtx rperm[2][32], vperm, l, h, ior, op, m128;
36963   unsigned int i, nelt, eltsz;
36964
36965   if (!TARGET_AVX2
36966       || d->op0 == d->op1
36967       || (d->vmode != V32QImode && d->vmode != V16HImode))
36968     return false;
36969
36970   for (i = 0; i < d->nelt; ++i)
36971     if ((d->perm[i] ^ (i * 2)) & (3 * d->nelt / 2))
36972       return false;
36973
36974   if (d->testing_p)
36975     return true;
36976
36977   nelt = d->nelt;
36978   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
36979
36980   /* Generate two permutation masks.  In the first permutation mask
36981      the first quarter will contain indexes for the first half
36982      of the op0, the second quarter will contain bit 7 set, third quarter
36983      will contain indexes for the second half of the op0 and the
36984      last quarter bit 7 set.  In the second permutation mask
36985      the first quarter will contain bit 7 set, the second quarter
36986      indexes for the first half of the op1, the third quarter bit 7 set
36987      and last quarter indexes for the second half of the op1.
36988      I.e. the first mask e.g. for V32QImode extract even will be:
36989      0, 2, ..., 0xe, -128, ..., -128, 0, 2, ..., 0xe, -128, ..., -128
36990      (all values masked with 0xf except for -128) and second mask
36991      for extract even will be
36992      -128, ..., -128, 0, 2, ..., 0xe, -128, ..., -128, 0, 2, ..., 0xe.  */
36993   m128 = GEN_INT (-128);
36994   for (i = 0; i < nelt; ++i)
36995     {
36996       unsigned j, e = d->perm[i] & (nelt / 2 - 1);
36997       unsigned which = d->perm[i] >= nelt;
36998       unsigned xorv = (i >= nelt / 4 && i < 3 * nelt / 4) ? 24 : 0;
36999
37000       for (j = 0; j < eltsz; ++j)
37001         {
37002           rperm[which][(i * eltsz + j) ^ xorv] = GEN_INT (e * eltsz + j);
37003           rperm[1 - which][(i * eltsz + j) ^ xorv] = m128;
37004         }
37005     }
37006
37007   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[0]));
37008   vperm = force_reg (V32QImode, vperm);
37009
37010   l = gen_reg_rtx (V32QImode);
37011   op = gen_lowpart (V32QImode, d->op0);
37012   emit_insn (gen_avx2_pshufbv32qi3 (l, op, vperm));
37013
37014   vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[1]));
37015   vperm = force_reg (V32QImode, vperm);
37016
37017   h = gen_reg_rtx (V32QImode);
37018   op = gen_lowpart (V32QImode, d->op1);
37019   emit_insn (gen_avx2_pshufbv32qi3 (h, op, vperm));
37020
37021   ior = gen_reg_rtx (V32QImode);
37022   emit_insn (gen_iorv32qi3 (ior, l, h));
37023
37024   /* Permute the V4DImode quarters using { 0, 2, 1, 3 } permutation.  */
37025   op = gen_lowpart (V4DImode, d->target);
37026   ior = gen_lowpart (V4DImode, ior);
37027   emit_insn (gen_avx2_permv4di_1 (op, ior, const0_rtx, const2_rtx,
37028                                   const1_rtx, GEN_INT (3)));
37029
37030   return true;
37031 }
37032
37033 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
37034    and extract-odd permutations.  */
37035
37036 static bool
37037 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
37038 {
37039   rtx t1, t2, t3;
37040
37041   switch (d->vmode)
37042     {
37043     case V4DFmode:
37044       t1 = gen_reg_rtx (V4DFmode);
37045       t2 = gen_reg_rtx (V4DFmode);
37046
37047       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
37048       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
37049       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
37050
37051       /* Now an unpck[lh]pd will produce the result required.  */
37052       if (odd)
37053         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
37054       else
37055         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
37056       emit_insn (t3);
37057       break;
37058
37059     case V8SFmode:
37060       {
37061         int mask = odd ? 0xdd : 0x88;
37062
37063         t1 = gen_reg_rtx (V8SFmode);
37064         t2 = gen_reg_rtx (V8SFmode);
37065         t3 = gen_reg_rtx (V8SFmode);
37066
37067         /* Shuffle within the 128-bit lanes to produce:
37068            { 0 2 8 a 4 6 c e } | { 1 3 9 b 5 7 d f }.  */
37069         emit_insn (gen_avx_shufps256 (t1, d->op0, d->op1,
37070                                       GEN_INT (mask)));
37071
37072         /* Shuffle the lanes around to produce:
37073            { 4 6 c e 0 2 8 a } and { 5 7 d f 1 3 9 b }.  */
37074         emit_insn (gen_avx_vperm2f128v8sf3 (t2, t1, t1,
37075                                             GEN_INT (0x3)));
37076
37077         /* Shuffle within the 128-bit lanes to produce:
37078            { 0 2 4 6 4 6 0 2 } | { 1 3 5 7 5 7 1 3 }.  */
37079         emit_insn (gen_avx_shufps256 (t3, t1, t2, GEN_INT (0x44)));
37080
37081         /* Shuffle within the 128-bit lanes to produce:
37082            { 8 a c e c e 8 a } | { 9 b d f d f 9 b }.  */
37083         emit_insn (gen_avx_shufps256 (t2, t1, t2, GEN_INT (0xee)));
37084
37085         /* Shuffle the lanes around to produce:
37086            { 0 2 4 6 8 a c e } | { 1 3 5 7 9 b d f }.  */
37087         emit_insn (gen_avx_vperm2f128v8sf3 (d->target, t3, t2,
37088                                             GEN_INT (0x20)));
37089       }
37090       break;
37091
37092     case V2DFmode:
37093     case V4SFmode:
37094     case V2DImode:
37095     case V4SImode:
37096       /* These are always directly implementable by expand_vec_perm_1.  */
37097       gcc_unreachable ();
37098
37099     case V8HImode:
37100       if (TARGET_SSSE3)
37101         return expand_vec_perm_pshufb2 (d);
37102       else
37103         {
37104           /* We need 2*log2(N)-1 operations to achieve odd/even
37105              with interleave. */
37106           t1 = gen_reg_rtx (V8HImode);
37107           t2 = gen_reg_rtx (V8HImode);
37108           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
37109           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
37110           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
37111           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
37112           if (odd)
37113             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
37114           else
37115             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
37116           emit_insn (t3);
37117         }
37118       break;
37119
37120     case V16QImode:
37121       if (TARGET_SSSE3)
37122         return expand_vec_perm_pshufb2 (d);
37123       else
37124         {
37125           t1 = gen_reg_rtx (V16QImode);
37126           t2 = gen_reg_rtx (V16QImode);
37127           t3 = gen_reg_rtx (V16QImode);
37128           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
37129           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
37130           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
37131           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
37132           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
37133           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
37134           if (odd)
37135             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
37136           else
37137             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
37138           emit_insn (t3);
37139         }
37140       break;
37141
37142     case V16HImode:
37143     case V32QImode:
37144       return expand_vec_perm_vpshufb2_vpermq_even_odd (d);
37145
37146     case V4DImode:
37147       if (!TARGET_AVX2)
37148         {
37149           struct expand_vec_perm_d d_copy = *d;
37150           d_copy.vmode = V4DFmode;
37151           d_copy.target = gen_lowpart (V4DFmode, d->target);
37152           d_copy.op0 = gen_lowpart (V4DFmode, d->op0);
37153           d_copy.op1 = gen_lowpart (V4DFmode, d->op1);
37154           return expand_vec_perm_even_odd_1 (&d_copy, odd);
37155         }
37156
37157       t1 = gen_reg_rtx (V4DImode);
37158       t2 = gen_reg_rtx (V4DImode);
37159
37160       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
37161       emit_insn (gen_avx2_permv2ti (t1, d->op0, d->op1, GEN_INT (0x20)));
37162       emit_insn (gen_avx2_permv2ti (t2, d->op0, d->op1, GEN_INT (0x31)));
37163
37164       /* Now an vpunpck[lh]qdq will produce the result required.  */
37165       if (odd)
37166         t3 = gen_avx2_interleave_highv4di (d->target, t1, t2);
37167       else
37168         t3 = gen_avx2_interleave_lowv4di (d->target, t1, t2);
37169       emit_insn (t3);
37170       break;
37171
37172     case V8SImode:
37173       if (!TARGET_AVX2)
37174         {
37175           struct expand_vec_perm_d d_copy = *d;
37176           d_copy.vmode = V8SFmode;
37177           d_copy.target = gen_lowpart (V8SFmode, d->target);
37178           d_copy.op0 = gen_lowpart (V8SFmode, d->op0);
37179           d_copy.op1 = gen_lowpart (V8SFmode, d->op1);
37180           return expand_vec_perm_even_odd_1 (&d_copy, odd);
37181         }
37182
37183       t1 = gen_reg_rtx (V8SImode);
37184       t2 = gen_reg_rtx (V8SImode);
37185
37186       /* Shuffle the lanes around into
37187          { 0 1 2 3 8 9 a b } and { 4 5 6 7 c d e f }.  */
37188       emit_insn (gen_avx2_permv2ti (gen_lowpart (V4DImode, t1),
37189                                     gen_lowpart (V4DImode, d->op0),
37190                                     gen_lowpart (V4DImode, d->op1),
37191                                     GEN_INT (0x20)));
37192       emit_insn (gen_avx2_permv2ti (gen_lowpart (V4DImode, t2),
37193                                     gen_lowpart (V4DImode, d->op0),
37194                                     gen_lowpart (V4DImode, d->op1),
37195                                     GEN_INT (0x31)));
37196
37197       /* Swap the 2nd and 3rd position in each lane into
37198          { 0 2 1 3 8 a 9 b } and { 4 6 5 7 c e d f }.  */
37199       emit_insn (gen_avx2_pshufdv3 (t1, t1,
37200                                     GEN_INT (2 * 4 + 1 * 16 + 3 * 64)));
37201       emit_insn (gen_avx2_pshufdv3 (t2, t2,
37202                                     GEN_INT (2 * 4 + 1 * 16 + 3 * 64)));
37203
37204       /* Now an vpunpck[lh]qdq will produce
37205          { 0 2 4 6 8 a c e } resp. { 1 3 5 7 9 b d f }.  */
37206       if (odd)
37207         t3 = gen_avx2_interleave_highv4di (gen_lowpart (V4DImode, d->target),
37208                                            gen_lowpart (V4DImode, t1),
37209                                            gen_lowpart (V4DImode, t2));
37210       else
37211         t3 = gen_avx2_interleave_lowv4di (gen_lowpart (V4DImode, d->target),
37212                                           gen_lowpart (V4DImode, t1),
37213                                           gen_lowpart (V4DImode, t2));
37214       emit_insn (t3);
37215       break;
37216
37217     default:
37218       gcc_unreachable ();
37219     }
37220
37221   return true;
37222 }
37223
37224 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
37225    extract-even and extract-odd permutations.  */
37226
37227 static bool
37228 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
37229 {
37230   unsigned i, odd, nelt = d->nelt;
37231
37232   odd = d->perm[0];
37233   if (odd != 0 && odd != 1)
37234     return false;
37235
37236   for (i = 1; i < nelt; ++i)
37237     if (d->perm[i] != 2 * i + odd)
37238       return false;
37239
37240   return expand_vec_perm_even_odd_1 (d, odd);
37241 }
37242
37243 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
37244    permutations.  We assume that expand_vec_perm_1 has already failed.  */
37245
37246 static bool
37247 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
37248 {
37249   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
37250   enum machine_mode vmode = d->vmode;
37251   unsigned char perm2[4];
37252   rtx op0 = d->op0;
37253   bool ok;
37254
37255   switch (vmode)
37256     {
37257     case V4DFmode:
37258     case V8SFmode:
37259       /* These are special-cased in sse.md so that we can optionally
37260          use the vbroadcast instruction.  They expand to two insns
37261          if the input happens to be in a register.  */
37262       gcc_unreachable ();
37263
37264     case V2DFmode:
37265     case V2DImode:
37266     case V4SFmode:
37267     case V4SImode:
37268       /* These are always implementable using standard shuffle patterns.  */
37269       gcc_unreachable ();
37270
37271     case V8HImode:
37272     case V16QImode:
37273       /* These can be implemented via interleave.  We save one insn by
37274          stopping once we have promoted to V4SImode and then use pshufd.  */
37275       do
37276         {
37277           rtx dest;
37278           rtx (*gen) (rtx, rtx, rtx)
37279             = vmode == V16QImode ? gen_vec_interleave_lowv16qi
37280                                  : gen_vec_interleave_lowv8hi;
37281
37282           if (elt >= nelt2)
37283             {
37284               gen = vmode == V16QImode ? gen_vec_interleave_highv16qi
37285                                        : gen_vec_interleave_highv8hi;
37286               elt -= nelt2;
37287             }
37288           nelt2 /= 2;
37289
37290           dest = gen_reg_rtx (vmode);
37291           emit_insn (gen (dest, op0, op0));
37292           vmode = get_mode_wider_vector (vmode);
37293           op0 = gen_lowpart (vmode, dest);
37294         }
37295       while (vmode != V4SImode);
37296
37297       memset (perm2, elt, 4);
37298       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
37299       gcc_assert (ok);
37300       return true;
37301
37302     case V32QImode:
37303     case V16HImode:
37304     case V8SImode:
37305     case V4DImode:
37306       /* For AVX2 broadcasts of the first element vpbroadcast* or
37307          vpermq should be used by expand_vec_perm_1.  */
37308       gcc_assert (!TARGET_AVX2 || d->perm[0]);
37309       return false;
37310
37311     default:
37312       gcc_unreachable ();
37313     }
37314 }
37315
37316 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
37317    broadcast permutations.  */
37318
37319 static bool
37320 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
37321 {
37322   unsigned i, elt, nelt = d->nelt;
37323
37324   if (d->op0 != d->op1)
37325     return false;
37326
37327   elt = d->perm[0];
37328   for (i = 1; i < nelt; ++i)
37329     if (d->perm[i] != elt)
37330       return false;
37331
37332   return expand_vec_perm_broadcast_1 (d);
37333 }
37334
37335 /* Implement arbitrary permutation of two V32QImode and V16QImode operands
37336    with 4 vpshufb insns, 2 vpermq and 3 vpor.  We should have already failed
37337    all the shorter instruction sequences.  */
37338
37339 static bool
37340 expand_vec_perm_vpshufb4_vpermq2 (struct expand_vec_perm_d *d)
37341 {
37342   rtx rperm[4][32], vperm, l[2], h[2], op, m128;
37343   unsigned int i, nelt, eltsz;
37344   bool used[4];
37345
37346   if (!TARGET_AVX2
37347       || d->op0 == d->op1
37348       || (d->vmode != V32QImode && d->vmode != V16HImode))
37349     return false;
37350
37351   if (d->testing_p)
37352     return true;
37353
37354   nelt = d->nelt;
37355   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
37356
37357   /* Generate 4 permutation masks.  If the required element is within
37358      the same lane, it is shuffled in.  If the required element from the
37359      other lane, force a zero by setting bit 7 in the permutation mask.
37360      In the other mask the mask has non-negative elements if element
37361      is requested from the other lane, but also moved to the other lane,
37362      so that the result of vpshufb can have the two V2TImode halves
37363      swapped.  */
37364   m128 = GEN_INT (-128);
37365   for (i = 0; i < 32; ++i)
37366     {
37367       rperm[0][i] = m128;
37368       rperm[1][i] = m128;
37369       rperm[2][i] = m128;
37370       rperm[3][i] = m128;
37371     }
37372   used[0] = false;
37373   used[1] = false;
37374   used[2] = false;
37375   used[3] = false;
37376   for (i = 0; i < nelt; ++i)
37377     {
37378       unsigned j, e = d->perm[i] & (nelt / 2 - 1);
37379       unsigned xlane = ((d->perm[i] ^ i) & (nelt / 2)) * eltsz;
37380       unsigned int which = ((d->perm[i] & nelt) ? 2 : 0) + (xlane ? 1 : 0);
37381
37382       for (j = 0; j < eltsz; ++j)
37383         rperm[which][(i * eltsz + j) ^ xlane] = GEN_INT (e * eltsz + j);
37384       used[which] = true;
37385     }
37386
37387   for (i = 0; i < 2; ++i)
37388     {
37389       if (!used[2 * i + 1])
37390         {
37391           h[i] = NULL_RTX;
37392           continue;
37393         }
37394       vperm = gen_rtx_CONST_VECTOR (V32QImode,
37395                                     gen_rtvec_v (32, rperm[2 * i + 1]));
37396       vperm = force_reg (V32QImode, vperm);
37397       h[i] = gen_reg_rtx (V32QImode);
37398       op = gen_lowpart (V32QImode, i ? d->op1 : d->op0);
37399       emit_insn (gen_avx2_pshufbv32qi3 (h[i], op, vperm));
37400     }
37401
37402   /* Swap the 128-byte lanes of h[X].  */
37403   for (i = 0; i < 2; ++i)
37404    {
37405      if (h[i] == NULL_RTX)
37406        continue;
37407      op = gen_reg_rtx (V4DImode);
37408      emit_insn (gen_avx2_permv4di_1 (op, gen_lowpart (V4DImode, h[i]),
37409                                      const2_rtx, GEN_INT (3), const0_rtx,
37410                                      const1_rtx));
37411      h[i] = gen_lowpart (V32QImode, op);
37412    }
37413
37414   for (i = 0; i < 2; ++i)
37415     {
37416       if (!used[2 * i])
37417         {
37418           l[i] = NULL_RTX;
37419           continue;
37420         }
37421       vperm = gen_rtx_CONST_VECTOR (V32QImode, gen_rtvec_v (32, rperm[2 * i]));
37422       vperm = force_reg (V32QImode, vperm);
37423       l[i] = gen_reg_rtx (V32QImode);
37424       op = gen_lowpart (V32QImode, i ? d->op1 : d->op0);
37425       emit_insn (gen_avx2_pshufbv32qi3 (l[i], op, vperm));
37426     }
37427
37428   for (i = 0; i < 2; ++i)
37429     {
37430       if (h[i] && l[i])
37431         {
37432           op = gen_reg_rtx (V32QImode);
37433           emit_insn (gen_iorv32qi3 (op, l[i], h[i]));
37434           l[i] = op;
37435         }
37436       else if (h[i])
37437         l[i] = h[i];
37438     }
37439
37440   gcc_assert (l[0] && l[1]);
37441   op = gen_lowpart (V32QImode, d->target);
37442   emit_insn (gen_iorv32qi3 (op, l[0], l[1]));
37443   return true;
37444 }
37445
37446 /* The guts of ix86_expand_vec_perm_const, also used by the ok hook.
37447    With all of the interface bits taken care of, perform the expansion
37448    in D and return true on success.  */
37449
37450 static bool
37451 ix86_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
37452 {
37453   /* Try a single instruction expansion.  */
37454   if (expand_vec_perm_1 (d))
37455     return true;
37456
37457   /* Try sequences of two instructions.  */
37458
37459   if (expand_vec_perm_pshuflw_pshufhw (d))
37460     return true;
37461
37462   if (expand_vec_perm_palignr (d))
37463     return true;
37464
37465   if (expand_vec_perm_interleave2 (d))
37466     return true;
37467
37468   if (expand_vec_perm_broadcast (d))
37469     return true;
37470
37471   if (expand_vec_perm_vpermq_perm_1 (d))
37472     return true;
37473
37474   /* Try sequences of three instructions.  */
37475
37476   if (expand_vec_perm_pshufb2 (d))
37477     return true;
37478
37479   if (expand_vec_perm_interleave3 (d))
37480     return true;
37481
37482   /* Try sequences of four instructions.  */
37483
37484   if (expand_vec_perm_vpshufb2_vpermq (d))
37485     return true;
37486
37487   if (expand_vec_perm_vpshufb2_vpermq_even_odd (d))
37488     return true;
37489
37490   /* ??? Look for narrow permutations whose element orderings would
37491      allow the promotion to a wider mode.  */
37492
37493   /* ??? Look for sequences of interleave or a wider permute that place
37494      the data into the correct lanes for a half-vector shuffle like
37495      pshuf[lh]w or vpermilps.  */
37496
37497   /* ??? Look for sequences of interleave that produce the desired results.
37498      The combinatorics of punpck[lh] get pretty ugly... */
37499
37500   if (expand_vec_perm_even_odd (d))
37501     return true;
37502
37503   /* Even longer sequences.  */
37504   if (expand_vec_perm_vpshufb4_vpermq2 (d))
37505     return true;
37506
37507   return false;
37508 }
37509
37510 bool
37511 ix86_expand_vec_perm_const (rtx operands[4])
37512 {
37513   struct expand_vec_perm_d d;
37514   unsigned char perm[MAX_VECT_LEN];
37515   int i, nelt, which;
37516   rtx sel;
37517
37518   d.target = operands[0];
37519   d.op0 = operands[1];
37520   d.op1 = operands[2];
37521   sel = operands[3];
37522
37523   d.vmode = GET_MODE (d.target);
37524   gcc_assert (VECTOR_MODE_P (d.vmode));
37525   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
37526   d.testing_p = false;
37527
37528   gcc_assert (GET_CODE (sel) == CONST_VECTOR);
37529   gcc_assert (XVECLEN (sel, 0) == nelt);
37530   gcc_checking_assert (sizeof (d.perm) == sizeof (perm));
37531
37532   for (i = which = 0; i < nelt; ++i)
37533     {
37534       rtx e = XVECEXP (sel, 0, i);
37535       int ei = INTVAL (e) & (2 * nelt - 1);
37536
37537       which |= (ei < nelt ? 1 : 2);
37538       d.perm[i] = ei;
37539       perm[i] = ei;
37540     }
37541
37542   switch (which)
37543     {
37544     default:
37545       gcc_unreachable();
37546
37547     case 3:
37548       if (!rtx_equal_p (d.op0, d.op1))
37549         break;
37550
37551       /* The elements of PERM do not suggest that only the first operand
37552          is used, but both operands are identical.  Allow easier matching
37553          of the permutation by folding the permutation into the single
37554          input vector.  */
37555       for (i = 0; i < nelt; ++i)
37556         if (d.perm[i] >= nelt)
37557           d.perm[i] -= nelt;
37558       /* FALLTHRU */
37559
37560     case 1:
37561       d.op1 = d.op0;
37562       break;
37563
37564     case 2:
37565       for (i = 0; i < nelt; ++i)
37566         d.perm[i] -= nelt;
37567       d.op0 = d.op1;
37568       break;
37569     }
37570
37571   if (ix86_expand_vec_perm_const_1 (&d))
37572     return true;
37573
37574   /* If the mask says both arguments are needed, but they are the same,
37575      the above tried to expand with d.op0 == d.op1.  If that didn't work,
37576      retry with d.op0 != d.op1 as that is what testing has been done with.  */
37577   if (which == 3 && d.op0 == d.op1)
37578     {
37579       rtx seq;
37580       bool ok;
37581
37582       memcpy (d.perm, perm, sizeof (perm));
37583       d.op1 = gen_reg_rtx (d.vmode);
37584       start_sequence ();
37585       ok = ix86_expand_vec_perm_const_1 (&d);
37586       seq = get_insns ();
37587       end_sequence ();
37588       if (ok)
37589         {
37590           emit_move_insn (d.op1, d.op0);
37591           emit_insn (seq);
37592           return true;
37593         }
37594     }
37595
37596   return false;
37597 }
37598
37599 /* Implement targetm.vectorize.vec_perm_const_ok.  */
37600
37601 static bool
37602 ix86_vectorize_vec_perm_const_ok (enum machine_mode vmode,
37603                                   const unsigned char *sel)
37604 {
37605   struct expand_vec_perm_d d;
37606   unsigned int i, nelt, which;
37607   bool ret, one_vec;
37608
37609   d.vmode = vmode;
37610   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
37611   d.testing_p = true;
37612
37613   /* Given sufficient ISA support we can just return true here
37614      for selected vector modes.  */
37615   if (GET_MODE_SIZE (d.vmode) == 16)
37616     {
37617       /* All implementable with a single vpperm insn.  */
37618       if (TARGET_XOP)
37619         return true;
37620       /* All implementable with 2 pshufb + 1 ior.  */
37621       if (TARGET_SSSE3)
37622         return true;
37623       /* All implementable with shufpd or unpck[lh]pd.  */
37624       if (d.nelt == 2)
37625         return true;
37626     }
37627
37628   /* Extract the values from the vector CST into the permutation
37629      array in D.  */
37630   memcpy (d.perm, sel, nelt);
37631   for (i = which = 0; i < nelt; ++i)
37632     {
37633       unsigned char e = d.perm[i];
37634       gcc_assert (e < 2 * nelt);
37635       which |= (e < nelt ? 1 : 2);
37636     }
37637
37638   /* For all elements from second vector, fold the elements to first.  */
37639   if (which == 2)
37640     for (i = 0; i < nelt; ++i)
37641       d.perm[i] -= nelt;
37642
37643   /* Check whether the mask can be applied to the vector type.  */
37644   one_vec = (which != 3);
37645
37646   /* Implementable with shufps or pshufd.  */
37647   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
37648     return true;
37649
37650   /* Otherwise we have to go through the motions and see if we can
37651      figure out how to generate the requested permutation.  */
37652   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
37653   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
37654   if (!one_vec)
37655     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
37656
37657   start_sequence ();
37658   ret = ix86_expand_vec_perm_const_1 (&d);
37659   end_sequence ();
37660
37661   return ret;
37662 }
37663
37664 void
37665 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
37666 {
37667   struct expand_vec_perm_d d;
37668   unsigned i, nelt;
37669
37670   d.target = targ;
37671   d.op0 = op0;
37672   d.op1 = op1;
37673   d.vmode = GET_MODE (targ);
37674   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
37675   d.testing_p = false;
37676
37677   for (i = 0; i < nelt; ++i)
37678     d.perm[i] = i * 2 + odd;
37679
37680   /* We'll either be able to implement the permutation directly...  */
37681   if (expand_vec_perm_1 (&d))
37682     return;
37683
37684   /* ... or we use the special-case patterns.  */
37685   expand_vec_perm_even_odd_1 (&d, odd);
37686 }
37687
37688 /* Expand an insert into a vector register through pinsr insn.
37689    Return true if successful.  */
37690
37691 bool
37692 ix86_expand_pinsr (rtx *operands)
37693 {
37694   rtx dst = operands[0];
37695   rtx src = operands[3];
37696
37697   unsigned int size = INTVAL (operands[1]);
37698   unsigned int pos = INTVAL (operands[2]);
37699
37700   if (GET_CODE (dst) == SUBREG)
37701     {
37702       pos += SUBREG_BYTE (dst) * BITS_PER_UNIT;
37703       dst = SUBREG_REG (dst);
37704     }
37705
37706   if (GET_CODE (src) == SUBREG)
37707     src = SUBREG_REG (src);
37708
37709   switch (GET_MODE (dst))
37710     {
37711     case V16QImode:
37712     case V8HImode:
37713     case V4SImode:
37714     case V2DImode:
37715       {
37716         enum machine_mode srcmode, dstmode;
37717         rtx (*pinsr)(rtx, rtx, rtx, rtx);
37718
37719         srcmode = mode_for_size (size, MODE_INT, 0);
37720
37721         switch (srcmode)
37722           {
37723           case QImode:
37724             if (!TARGET_SSE4_1)
37725               return false;
37726             dstmode = V16QImode;
37727             pinsr = gen_sse4_1_pinsrb;
37728             break;
37729
37730           case HImode:
37731             if (!TARGET_SSE2)
37732               return false;
37733             dstmode = V8HImode;
37734             pinsr = gen_sse2_pinsrw;
37735             break;
37736
37737           case SImode:
37738             if (!TARGET_SSE4_1)
37739               return false;
37740             dstmode = V4SImode;
37741             pinsr = gen_sse4_1_pinsrd;
37742             break;
37743
37744           case DImode:
37745             gcc_assert (TARGET_64BIT);
37746             if (!TARGET_SSE4_1)
37747               return false;
37748             dstmode = V2DImode;
37749             pinsr = gen_sse4_1_pinsrq;
37750             break;
37751
37752           default:
37753             return false;
37754           }
37755
37756         dst = gen_lowpart (dstmode, dst);
37757         src = gen_lowpart (srcmode, src);
37758
37759         pos /= size;
37760
37761         emit_insn (pinsr (dst, dst, src, GEN_INT (1 << pos)));
37762         return true;
37763       }
37764
37765     default:
37766       return false;
37767     }
37768 }
37769 \f
37770 /* This function returns the calling abi specific va_list type node.
37771    It returns  the FNDECL specific va_list type.  */
37772
37773 static tree
37774 ix86_fn_abi_va_list (tree fndecl)
37775 {
37776   if (!TARGET_64BIT)
37777     return va_list_type_node;
37778   gcc_assert (fndecl != NULL_TREE);
37779
37780   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
37781     return ms_va_list_type_node;
37782   else
37783     return sysv_va_list_type_node;
37784 }
37785
37786 /* Returns the canonical va_list type specified by TYPE. If there
37787    is no valid TYPE provided, it return NULL_TREE.  */
37788
37789 static tree
37790 ix86_canonical_va_list_type (tree type)
37791 {
37792   tree wtype, htype;
37793
37794   /* Resolve references and pointers to va_list type.  */
37795   if (TREE_CODE (type) == MEM_REF)
37796     type = TREE_TYPE (type);
37797   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
37798     type = TREE_TYPE (type);
37799   else if (POINTER_TYPE_P (type) && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
37800     type = TREE_TYPE (type);
37801
37802   if (TARGET_64BIT && va_list_type_node != NULL_TREE)
37803     {
37804       wtype = va_list_type_node;
37805           gcc_assert (wtype != NULL_TREE);
37806       htype = type;
37807       if (TREE_CODE (wtype) == ARRAY_TYPE)
37808         {
37809           /* If va_list is an array type, the argument may have decayed
37810              to a pointer type, e.g. by being passed to another function.
37811              In that case, unwrap both types so that we can compare the
37812              underlying records.  */
37813           if (TREE_CODE (htype) == ARRAY_TYPE
37814               || POINTER_TYPE_P (htype))
37815             {
37816               wtype = TREE_TYPE (wtype);
37817               htype = TREE_TYPE (htype);
37818             }
37819         }
37820       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
37821         return va_list_type_node;
37822       wtype = sysv_va_list_type_node;
37823           gcc_assert (wtype != NULL_TREE);
37824       htype = type;
37825       if (TREE_CODE (wtype) == ARRAY_TYPE)
37826         {
37827           /* If va_list is an array type, the argument may have decayed
37828              to a pointer type, e.g. by being passed to another function.
37829              In that case, unwrap both types so that we can compare the
37830              underlying records.  */
37831           if (TREE_CODE (htype) == ARRAY_TYPE
37832               || POINTER_TYPE_P (htype))
37833             {
37834               wtype = TREE_TYPE (wtype);
37835               htype = TREE_TYPE (htype);
37836             }
37837         }
37838       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
37839         return sysv_va_list_type_node;
37840       wtype = ms_va_list_type_node;
37841           gcc_assert (wtype != NULL_TREE);
37842       htype = type;
37843       if (TREE_CODE (wtype) == ARRAY_TYPE)
37844         {
37845           /* If va_list is an array type, the argument may have decayed
37846              to a pointer type, e.g. by being passed to another function.
37847              In that case, unwrap both types so that we can compare the
37848              underlying records.  */
37849           if (TREE_CODE (htype) == ARRAY_TYPE
37850               || POINTER_TYPE_P (htype))
37851             {
37852               wtype = TREE_TYPE (wtype);
37853               htype = TREE_TYPE (htype);
37854             }
37855         }
37856       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
37857         return ms_va_list_type_node;
37858       return NULL_TREE;
37859     }
37860   return std_canonical_va_list_type (type);
37861 }
37862
37863 /* Iterate through the target-specific builtin types for va_list.
37864    IDX denotes the iterator, *PTREE is set to the result type of
37865    the va_list builtin, and *PNAME to its internal type.
37866    Returns zero if there is no element for this index, otherwise
37867    IDX should be increased upon the next call.
37868    Note, do not iterate a base builtin's name like __builtin_va_list.
37869    Used from c_common_nodes_and_builtins.  */
37870
37871 static int
37872 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
37873 {
37874   if (TARGET_64BIT)
37875     {
37876       switch (idx)
37877         {
37878         default:
37879           break;
37880
37881         case 0:
37882           *ptree = ms_va_list_type_node;
37883           *pname = "__builtin_ms_va_list";
37884           return 1;
37885
37886         case 1:
37887           *ptree = sysv_va_list_type_node;
37888           *pname = "__builtin_sysv_va_list";
37889           return 1;
37890         }
37891     }
37892
37893   return 0;
37894 }
37895
37896 #undef TARGET_SCHED_DISPATCH
37897 #define TARGET_SCHED_DISPATCH has_dispatch
37898 #undef TARGET_SCHED_DISPATCH_DO
37899 #define TARGET_SCHED_DISPATCH_DO do_dispatch
37900 #undef TARGET_SCHED_REASSOCIATION_WIDTH
37901 #define TARGET_SCHED_REASSOCIATION_WIDTH ix86_reassociation_width
37902
37903 /* The size of the dispatch window is the total number of bytes of
37904    object code allowed in a window.  */
37905 #define DISPATCH_WINDOW_SIZE 16
37906
37907 /* Number of dispatch windows considered for scheduling.  */
37908 #define MAX_DISPATCH_WINDOWS 3
37909
37910 /* Maximum number of instructions in a window.  */
37911 #define MAX_INSN 4
37912
37913 /* Maximum number of immediate operands in a window.  */
37914 #define MAX_IMM 4
37915
37916 /* Maximum number of immediate bits allowed in a window.  */
37917 #define MAX_IMM_SIZE 128
37918
37919 /* Maximum number of 32 bit immediates allowed in a window.  */
37920 #define MAX_IMM_32 4
37921
37922 /* Maximum number of 64 bit immediates allowed in a window.  */
37923 #define MAX_IMM_64 2
37924
37925 /* Maximum total of loads or prefetches allowed in a window.  */
37926 #define MAX_LOAD 2
37927
37928 /* Maximum total of stores allowed in a window.  */
37929 #define MAX_STORE 1
37930
37931 #undef BIG
37932 #define BIG 100
37933
37934
37935 /* Dispatch groups.  Istructions that affect the mix in a dispatch window.  */
37936 enum dispatch_group {
37937   disp_no_group = 0,
37938   disp_load,
37939   disp_store,
37940   disp_load_store,
37941   disp_prefetch,
37942   disp_imm,
37943   disp_imm_32,
37944   disp_imm_64,
37945   disp_branch,
37946   disp_cmp,
37947   disp_jcc,
37948   disp_last
37949 };
37950
37951 /* Number of allowable groups in a dispatch window.  It is an array
37952    indexed by dispatch_group enum.  100 is used as a big number,
37953    because the number of these kind of operations does not have any
37954    effect in dispatch window, but we need them for other reasons in
37955    the table.  */
37956 static unsigned int num_allowable_groups[disp_last] = {
37957   0, 2, 1, 1, 2, 4, 4, 2, 1, BIG, BIG
37958 };
37959
37960 char group_name[disp_last + 1][16] = {
37961   "disp_no_group", "disp_load", "disp_store", "disp_load_store",
37962   "disp_prefetch", "disp_imm", "disp_imm_32", "disp_imm_64",
37963   "disp_branch", "disp_cmp", "disp_jcc", "disp_last"
37964 };
37965
37966 /* Instruction path.  */
37967 enum insn_path {
37968   no_path = 0,
37969   path_single, /* Single micro op.  */
37970   path_double, /* Double micro op.  */
37971   path_multi,  /* Instructions with more than 2 micro op..  */
37972   last_path
37973 };
37974
37975 /* sched_insn_info defines a window to the instructions scheduled in
37976    the basic block.  It contains a pointer to the insn_info table and
37977    the instruction scheduled.
37978
37979    Windows are allocated for each basic block and are linked
37980    together.  */
37981 typedef struct sched_insn_info_s {
37982   rtx insn;
37983   enum dispatch_group group;
37984   enum insn_path path;
37985   int byte_len;
37986   int imm_bytes;
37987 } sched_insn_info;
37988
37989 /* Linked list of dispatch windows.  This is a two way list of
37990    dispatch windows of a basic block.  It contains information about
37991    the number of uops in the window and the total number of
37992    instructions and of bytes in the object code for this dispatch
37993    window.  */
37994 typedef struct dispatch_windows_s {
37995   int num_insn;            /* Number of insn in the window.  */
37996   int num_uops;            /* Number of uops in the window.  */
37997   int window_size;         /* Number of bytes in the window.  */
37998   int window_num;          /* Window number between 0 or 1.  */
37999   int num_imm;             /* Number of immediates in an insn.  */
38000   int num_imm_32;          /* Number of 32 bit immediates in an insn.  */
38001   int num_imm_64;          /* Number of 64 bit immediates in an insn.  */
38002   int imm_size;            /* Total immediates in the window.  */
38003   int num_loads;           /* Total memory loads in the window.  */
38004   int num_stores;          /* Total memory stores in the window.  */
38005   int violation;          /* Violation exists in window.  */
38006   sched_insn_info *window; /* Pointer to the window.  */
38007   struct dispatch_windows_s *next;
38008   struct dispatch_windows_s *prev;
38009 } dispatch_windows;
38010
38011 /* Immediate valuse used in an insn.  */
38012 typedef struct imm_info_s
38013   {
38014     int imm;
38015     int imm32;
38016     int imm64;
38017   } imm_info;
38018
38019 static dispatch_windows *dispatch_window_list;
38020 static dispatch_windows *dispatch_window_list1;
38021
38022 /* Get dispatch group of insn.  */
38023
38024 static enum dispatch_group
38025 get_mem_group (rtx insn)
38026 {
38027   enum attr_memory memory;
38028
38029   if (INSN_CODE (insn) < 0)
38030     return disp_no_group;
38031   memory = get_attr_memory (insn);
38032   if (memory == MEMORY_STORE)
38033     return disp_store;
38034
38035   if (memory == MEMORY_LOAD)
38036     return disp_load;
38037
38038   if (memory == MEMORY_BOTH)
38039     return disp_load_store;
38040
38041   return disp_no_group;
38042 }
38043
38044 /* Return true if insn is a compare instruction.  */
38045
38046 static bool
38047 is_cmp (rtx insn)
38048 {
38049   enum attr_type type;
38050
38051   type = get_attr_type (insn);
38052   return (type == TYPE_TEST
38053           || type == TYPE_ICMP
38054           || type == TYPE_FCMP
38055           || GET_CODE (PATTERN (insn)) == COMPARE);
38056 }
38057
38058 /* Return true if a dispatch violation encountered.  */
38059
38060 static bool
38061 dispatch_violation (void)
38062 {
38063   if (dispatch_window_list->next)
38064     return dispatch_window_list->next->violation;
38065   return dispatch_window_list->violation;
38066 }
38067
38068 /* Return true if insn is a branch instruction.  */
38069
38070 static bool
38071 is_branch (rtx insn)
38072 {
38073   return (CALL_P (insn) || JUMP_P (insn));
38074 }
38075
38076 /* Return true if insn is a prefetch instruction.  */
38077
38078 static bool
38079 is_prefetch (rtx insn)
38080 {
38081   return NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == PREFETCH;
38082 }
38083
38084 /* This function initializes a dispatch window and the list container holding a
38085    pointer to the window.  */
38086
38087 static void
38088 init_window (int window_num)
38089 {
38090   int i;
38091   dispatch_windows *new_list;
38092
38093   if (window_num == 0)
38094     new_list = dispatch_window_list;
38095   else
38096     new_list = dispatch_window_list1;
38097
38098   new_list->num_insn = 0;
38099   new_list->num_uops = 0;
38100   new_list->window_size = 0;
38101   new_list->next = NULL;
38102   new_list->prev = NULL;
38103   new_list->window_num = window_num;
38104   new_list->num_imm = 0;
38105   new_list->num_imm_32 = 0;
38106   new_list->num_imm_64 = 0;
38107   new_list->imm_size = 0;
38108   new_list->num_loads = 0;
38109   new_list->num_stores = 0;
38110   new_list->violation = false;
38111
38112   for (i = 0; i < MAX_INSN; i++)
38113     {
38114       new_list->window[i].insn = NULL;
38115       new_list->window[i].group = disp_no_group;
38116       new_list->window[i].path = no_path;
38117       new_list->window[i].byte_len = 0;
38118       new_list->window[i].imm_bytes = 0;
38119     }
38120   return;
38121 }
38122
38123 /* This function allocates and initializes a dispatch window and the
38124    list container holding a pointer to the window.  */
38125
38126 static dispatch_windows *
38127 allocate_window (void)
38128 {
38129   dispatch_windows *new_list = XNEW (struct dispatch_windows_s);
38130   new_list->window = XNEWVEC (struct sched_insn_info_s, MAX_INSN + 1);
38131
38132   return new_list;
38133 }
38134
38135 /* This routine initializes the dispatch scheduling information.  It
38136    initiates building dispatch scheduler tables and constructs the
38137    first dispatch window.  */
38138
38139 static void
38140 init_dispatch_sched (void)
38141 {
38142   /* Allocate a dispatch list and a window.  */
38143   dispatch_window_list = allocate_window ();
38144   dispatch_window_list1 = allocate_window ();
38145   init_window (0);
38146   init_window (1);
38147 }
38148
38149 /* This function returns true if a branch is detected.  End of a basic block
38150    does not have to be a branch, but here we assume only branches end a
38151    window.  */
38152
38153 static bool
38154 is_end_basic_block (enum dispatch_group group)
38155 {
38156   return group == disp_branch;
38157 }
38158
38159 /* This function is called when the end of a window processing is reached.  */
38160
38161 static void
38162 process_end_window (void)
38163 {
38164   gcc_assert (dispatch_window_list->num_insn <= MAX_INSN);
38165   if (dispatch_window_list->next)
38166     {
38167       gcc_assert (dispatch_window_list1->num_insn <= MAX_INSN);
38168       gcc_assert (dispatch_window_list->window_size
38169                   + dispatch_window_list1->window_size <= 48);
38170       init_window (1);
38171     }
38172   init_window (0);
38173 }
38174
38175 /* Allocates a new dispatch window and adds it to WINDOW_LIST.
38176    WINDOW_NUM is either 0 or 1.  A maximum of two windows are generated
38177    for 48 bytes of instructions.  Note that these windows are not dispatch
38178    windows that their sizes are DISPATCH_WINDOW_SIZE.  */
38179
38180 static dispatch_windows *
38181 allocate_next_window (int window_num)
38182 {
38183   if (window_num == 0)
38184     {
38185       if (dispatch_window_list->next)
38186           init_window (1);
38187       init_window (0);
38188       return dispatch_window_list;
38189     }
38190
38191   dispatch_window_list->next = dispatch_window_list1;
38192   dispatch_window_list1->prev = dispatch_window_list;
38193
38194   return dispatch_window_list1;
38195 }
38196
38197 /* Increment the number of immediate operands of an instruction.  */
38198
38199 static int
38200 find_constant_1 (rtx *in_rtx, imm_info *imm_values)
38201 {
38202   if (*in_rtx == 0)
38203     return 0;
38204
38205     switch ( GET_CODE (*in_rtx))
38206     {
38207     case CONST:
38208     case SYMBOL_REF:
38209     case CONST_INT:
38210       (imm_values->imm)++;
38211       if (x86_64_immediate_operand (*in_rtx, SImode))
38212         (imm_values->imm32)++;
38213       else
38214         (imm_values->imm64)++;
38215       break;
38216
38217     case CONST_DOUBLE:
38218       (imm_values->imm)++;
38219       (imm_values->imm64)++;
38220       break;
38221
38222     case CODE_LABEL:
38223       if (LABEL_KIND (*in_rtx) == LABEL_NORMAL)
38224         {
38225           (imm_values->imm)++;
38226           (imm_values->imm32)++;
38227         }
38228       break;
38229
38230     default:
38231       break;
38232     }
38233
38234   return 0;
38235 }
38236
38237 /* Compute number of immediate operands of an instruction.  */
38238
38239 static void
38240 find_constant (rtx in_rtx, imm_info *imm_values)
38241 {
38242   for_each_rtx (INSN_P (in_rtx) ? &PATTERN (in_rtx) : &in_rtx,
38243                 (rtx_function) find_constant_1, (void *) imm_values);
38244 }
38245
38246 /* Return total size of immediate operands of an instruction along with number
38247    of corresponding immediate-operands.  It initializes its parameters to zero
38248    befor calling FIND_CONSTANT.
38249    INSN is the input instruction.  IMM is the total of immediates.
38250    IMM32 is the number of 32 bit immediates.  IMM64 is the number of 64
38251    bit immediates.  */
38252
38253 static int
38254 get_num_immediates (rtx insn, int *imm, int *imm32, int *imm64)
38255 {
38256   imm_info imm_values = {0, 0, 0};
38257
38258   find_constant (insn, &imm_values);
38259   *imm = imm_values.imm;
38260   *imm32 = imm_values.imm32;
38261   *imm64 = imm_values.imm64;
38262   return imm_values.imm32 * 4 + imm_values.imm64 * 8;
38263 }
38264
38265 /* This function indicates if an operand of an instruction is an
38266    immediate.  */
38267
38268 static bool
38269 has_immediate (rtx insn)
38270 {
38271   int num_imm_operand;
38272   int num_imm32_operand;
38273   int num_imm64_operand;
38274
38275   if (insn)
38276     return get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38277                                &num_imm64_operand);
38278   return false;
38279 }
38280
38281 /* Return single or double path for instructions.  */
38282
38283 static enum insn_path
38284 get_insn_path (rtx insn)
38285 {
38286   enum attr_amdfam10_decode path = get_attr_amdfam10_decode (insn);
38287
38288   if ((int)path == 0)
38289     return path_single;
38290
38291   if ((int)path == 1)
38292     return path_double;
38293
38294   return path_multi;
38295 }
38296
38297 /* Return insn dispatch group.  */
38298
38299 static enum dispatch_group
38300 get_insn_group (rtx insn)
38301 {
38302   enum dispatch_group group = get_mem_group (insn);
38303   if (group)
38304     return group;
38305
38306   if (is_branch (insn))
38307     return disp_branch;
38308
38309   if (is_cmp (insn))
38310     return disp_cmp;
38311
38312   if (has_immediate (insn))
38313     return disp_imm;
38314
38315   if (is_prefetch (insn))
38316     return disp_prefetch;
38317
38318   return disp_no_group;
38319 }
38320
38321 /* Count number of GROUP restricted instructions in a dispatch
38322    window WINDOW_LIST.  */
38323
38324 static int
38325 count_num_restricted (rtx insn, dispatch_windows *window_list)
38326 {
38327   enum dispatch_group group = get_insn_group (insn);
38328   int imm_size;
38329   int num_imm_operand;
38330   int num_imm32_operand;
38331   int num_imm64_operand;
38332
38333   if (group == disp_no_group)
38334     return 0;
38335
38336   if (group == disp_imm)
38337     {
38338       imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38339                               &num_imm64_operand);
38340       if (window_list->imm_size + imm_size > MAX_IMM_SIZE
38341           || num_imm_operand + window_list->num_imm > MAX_IMM
38342           || (num_imm32_operand > 0
38343               && (window_list->num_imm_32 + num_imm32_operand > MAX_IMM_32
38344                   || window_list->num_imm_64 * 2 + num_imm32_operand > MAX_IMM_32))
38345           || (num_imm64_operand > 0
38346               && (window_list->num_imm_64 + num_imm64_operand > MAX_IMM_64
38347                   || window_list->num_imm_32 + num_imm64_operand * 2 > MAX_IMM_32))
38348           || (window_list->imm_size + imm_size == MAX_IMM_SIZE
38349               && num_imm64_operand > 0
38350               && ((window_list->num_imm_64 > 0
38351                    && window_list->num_insn >= 2)
38352                   || window_list->num_insn >= 3)))
38353         return BIG;
38354
38355       return 1;
38356     }
38357
38358   if ((group == disp_load_store
38359        && (window_list->num_loads >= MAX_LOAD
38360            || window_list->num_stores >= MAX_STORE))
38361       || ((group == disp_load
38362            || group == disp_prefetch)
38363           && window_list->num_loads >= MAX_LOAD)
38364       || (group == disp_store
38365           && window_list->num_stores >= MAX_STORE))
38366     return BIG;
38367
38368   return 1;
38369 }
38370
38371 /* This function returns true if insn satisfies dispatch rules on the
38372    last window scheduled.  */
38373
38374 static bool
38375 fits_dispatch_window (rtx insn)
38376 {
38377   dispatch_windows *window_list = dispatch_window_list;
38378   dispatch_windows *window_list_next = dispatch_window_list->next;
38379   unsigned int num_restrict;
38380   enum dispatch_group group = get_insn_group (insn);
38381   enum insn_path path = get_insn_path (insn);
38382   int sum;
38383
38384   /* Make disp_cmp and disp_jcc get scheduled at the latest.  These
38385      instructions should be given the lowest priority in the
38386      scheduling process in Haifa scheduler to make sure they will be
38387      scheduled in the same dispatch window as the refrence to them.  */
38388   if (group == disp_jcc || group == disp_cmp)
38389     return false;
38390
38391   /* Check nonrestricted.  */
38392   if (group == disp_no_group || group == disp_branch)
38393     return true;
38394
38395   /* Get last dispatch window.  */
38396   if (window_list_next)
38397     window_list = window_list_next;
38398
38399   if (window_list->window_num == 1)
38400     {
38401       sum = window_list->prev->window_size + window_list->window_size;
38402
38403       if (sum == 32
38404           || (min_insn_size (insn) + sum) >= 48)
38405         /* Window 1 is full.  Go for next window.  */
38406         return true;
38407     }
38408
38409   num_restrict = count_num_restricted (insn, window_list);
38410
38411   if (num_restrict > num_allowable_groups[group])
38412     return false;
38413
38414   /* See if it fits in the first window.  */
38415   if (window_list->window_num == 0)
38416     {
38417       /* The first widow should have only single and double path
38418          uops.  */
38419       if (path == path_double
38420           && (window_list->num_uops + 2) > MAX_INSN)
38421         return false;
38422       else if (path != path_single)
38423         return false;
38424     }
38425   return true;
38426 }
38427
38428 /* Add an instruction INSN with NUM_UOPS micro-operations to the
38429    dispatch window WINDOW_LIST.  */
38430
38431 static void
38432 add_insn_window (rtx insn, dispatch_windows *window_list, int num_uops)
38433 {
38434   int byte_len = min_insn_size (insn);
38435   int num_insn = window_list->num_insn;
38436   int imm_size;
38437   sched_insn_info *window = window_list->window;
38438   enum dispatch_group group = get_insn_group (insn);
38439   enum insn_path path = get_insn_path (insn);
38440   int num_imm_operand;
38441   int num_imm32_operand;
38442   int num_imm64_operand;
38443
38444   if (!window_list->violation && group != disp_cmp
38445       && !fits_dispatch_window (insn))
38446     window_list->violation = true;
38447
38448   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38449                                  &num_imm64_operand);
38450
38451   /* Initialize window with new instruction.  */
38452   window[num_insn].insn = insn;
38453   window[num_insn].byte_len = byte_len;
38454   window[num_insn].group = group;
38455   window[num_insn].path = path;
38456   window[num_insn].imm_bytes = imm_size;
38457
38458   window_list->window_size += byte_len;
38459   window_list->num_insn = num_insn + 1;
38460   window_list->num_uops = window_list->num_uops + num_uops;
38461   window_list->imm_size += imm_size;
38462   window_list->num_imm += num_imm_operand;
38463   window_list->num_imm_32 += num_imm32_operand;
38464   window_list->num_imm_64 += num_imm64_operand;
38465
38466   if (group == disp_store)
38467     window_list->num_stores += 1;
38468   else if (group == disp_load
38469            || group == disp_prefetch)
38470     window_list->num_loads += 1;
38471   else if (group == disp_load_store)
38472     {
38473       window_list->num_stores += 1;
38474       window_list->num_loads += 1;
38475     }
38476 }
38477
38478 /* Adds a scheduled instruction, INSN, to the current dispatch window.
38479    If the total bytes of instructions or the number of instructions in
38480    the window exceed allowable, it allocates a new window.  */
38481
38482 static void
38483 add_to_dispatch_window (rtx insn)
38484 {
38485   int byte_len;
38486   dispatch_windows *window_list;
38487   dispatch_windows *next_list;
38488   dispatch_windows *window0_list;
38489   enum insn_path path;
38490   enum dispatch_group insn_group;
38491   bool insn_fits;
38492   int num_insn;
38493   int num_uops;
38494   int window_num;
38495   int insn_num_uops;
38496   int sum;
38497
38498   if (INSN_CODE (insn) < 0)
38499     return;
38500
38501   byte_len = min_insn_size (insn);
38502   window_list = dispatch_window_list;
38503   next_list = window_list->next;
38504   path = get_insn_path (insn);
38505   insn_group = get_insn_group (insn);
38506
38507   /* Get the last dispatch window.  */
38508   if (next_list)
38509       window_list = dispatch_window_list->next;
38510
38511   if (path == path_single)
38512     insn_num_uops = 1;
38513   else if (path == path_double)
38514     insn_num_uops = 2;
38515   else
38516     insn_num_uops = (int) path;
38517
38518   /* If current window is full, get a new window.
38519      Window number zero is full, if MAX_INSN uops are scheduled in it.
38520      Window number one is full, if window zero's bytes plus window
38521      one's bytes is 32, or if the bytes of the new instruction added
38522      to the total makes it greater than 48, or it has already MAX_INSN
38523      instructions in it.  */
38524   num_insn = window_list->num_insn;
38525   num_uops = window_list->num_uops;
38526   window_num = window_list->window_num;
38527   insn_fits = fits_dispatch_window (insn);
38528
38529   if (num_insn >= MAX_INSN
38530       || num_uops + insn_num_uops > MAX_INSN
38531       || !(insn_fits))
38532     {
38533       window_num = ~window_num & 1;
38534       window_list = allocate_next_window (window_num);
38535     }
38536
38537   if (window_num == 0)
38538     {
38539       add_insn_window (insn, window_list, insn_num_uops);
38540       if (window_list->num_insn >= MAX_INSN
38541           && insn_group == disp_branch)
38542         {
38543           process_end_window ();
38544           return;
38545         }
38546     }
38547   else if (window_num == 1)
38548     {
38549       window0_list = window_list->prev;
38550       sum = window0_list->window_size + window_list->window_size;
38551       if (sum == 32
38552           || (byte_len + sum) >= 48)
38553         {
38554           process_end_window ();
38555           window_list = dispatch_window_list;
38556         }
38557
38558       add_insn_window (insn, window_list, insn_num_uops);
38559     }
38560   else
38561     gcc_unreachable ();
38562
38563   if (is_end_basic_block (insn_group))
38564     {
38565       /* End of basic block is reached do end-basic-block process.  */
38566       process_end_window ();
38567       return;
38568     }
38569 }
38570
38571 /* Print the dispatch window, WINDOW_NUM, to FILE.  */
38572
38573 DEBUG_FUNCTION static void
38574 debug_dispatch_window_file (FILE *file, int window_num)
38575 {
38576   dispatch_windows *list;
38577   int i;
38578
38579   if (window_num == 0)
38580     list = dispatch_window_list;
38581   else
38582     list = dispatch_window_list1;
38583
38584   fprintf (file, "Window #%d:\n", list->window_num);
38585   fprintf (file, "  num_insn = %d, num_uops = %d, window_size = %d\n",
38586           list->num_insn, list->num_uops, list->window_size);
38587   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
38588            list->num_imm, list->num_imm_32, list->num_imm_64, list->imm_size);
38589
38590   fprintf (file, "  num_loads = %d, num_stores = %d\n", list->num_loads,
38591           list->num_stores);
38592   fprintf (file, " insn info:\n");
38593
38594   for (i = 0; i < MAX_INSN; i++)
38595     {
38596       if (!list->window[i].insn)
38597         break;
38598       fprintf (file, "    group[%d] = %s, insn[%d] = %p, path[%d] = %d byte_len[%d] = %d, imm_bytes[%d] = %d\n",
38599               i, group_name[list->window[i].group],
38600               i, (void *)list->window[i].insn,
38601               i, list->window[i].path,
38602               i, list->window[i].byte_len,
38603               i, list->window[i].imm_bytes);
38604     }
38605 }
38606
38607 /* Print to stdout a dispatch window.  */
38608
38609 DEBUG_FUNCTION void
38610 debug_dispatch_window (int window_num)
38611 {
38612   debug_dispatch_window_file (stdout, window_num);
38613 }
38614
38615 /* Print INSN dispatch information to FILE.  */
38616
38617 DEBUG_FUNCTION static void
38618 debug_insn_dispatch_info_file (FILE *file, rtx insn)
38619 {
38620   int byte_len;
38621   enum insn_path path;
38622   enum dispatch_group group;
38623   int imm_size;
38624   int num_imm_operand;
38625   int num_imm32_operand;
38626   int num_imm64_operand;
38627
38628   if (INSN_CODE (insn) < 0)
38629     return;
38630
38631   byte_len = min_insn_size (insn);
38632   path = get_insn_path (insn);
38633   group = get_insn_group (insn);
38634   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
38635                                  &num_imm64_operand);
38636
38637   fprintf (file, " insn info:\n");
38638   fprintf (file, "  group = %s, path = %d, byte_len = %d\n",
38639            group_name[group], path, byte_len);
38640   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
38641            num_imm_operand, num_imm32_operand, num_imm64_operand, imm_size);
38642 }
38643
38644 /* Print to STDERR the status of the ready list with respect to
38645    dispatch windows.  */
38646
38647 DEBUG_FUNCTION void
38648 debug_ready_dispatch (void)
38649 {
38650   int i;
38651   int no_ready = number_in_ready ();
38652
38653   fprintf (stdout, "Number of ready: %d\n", no_ready);
38654
38655   for (i = 0; i < no_ready; i++)
38656     debug_insn_dispatch_info_file (stdout, get_ready_element (i));
38657 }
38658
38659 /* This routine is the driver of the dispatch scheduler.  */
38660
38661 static void
38662 do_dispatch (rtx insn, int mode)
38663 {
38664   if (mode == DISPATCH_INIT)
38665     init_dispatch_sched ();
38666   else if (mode == ADD_TO_DISPATCH_WINDOW)
38667     add_to_dispatch_window (insn);
38668 }
38669
38670 /* Return TRUE if Dispatch Scheduling is supported.  */
38671
38672 static bool
38673 has_dispatch (rtx insn, int action)
38674 {
38675   if ((ix86_tune == PROCESSOR_BDVER1 || ix86_tune == PROCESSOR_BDVER2)
38676       && flag_dispatch_scheduler)
38677     switch (action)
38678       {
38679       default:
38680         return false;
38681
38682       case IS_DISPATCH_ON:
38683         return true;
38684         break;
38685
38686       case IS_CMP:
38687         return is_cmp (insn);
38688
38689       case DISPATCH_VIOLATION:
38690         return dispatch_violation ();
38691
38692       case FITS_DISPATCH_WINDOW:
38693         return fits_dispatch_window (insn);
38694       }
38695
38696   return false;
38697 }
38698
38699 /* Implementation of reassociation_width target hook used by
38700    reassoc phase to identify parallelism level in reassociated
38701    tree.  Statements tree_code is passed in OPC.  Arguments type
38702    is passed in MODE.
38703
38704    Currently parallel reassociation is enabled for Atom
38705    processors only and we set reassociation width to be 2
38706    because Atom may issue up to 2 instructions per cycle.
38707
38708    Return value should be fixed if parallel reassociation is
38709    enabled for other processors.  */
38710
38711 static int
38712 ix86_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
38713                           enum machine_mode mode)
38714 {
38715   int res = 1;
38716
38717   if (INTEGRAL_MODE_P (mode) && TARGET_REASSOC_INT_TO_PARALLEL)
38718     res = 2;
38719   else if (FLOAT_MODE_P (mode) && TARGET_REASSOC_FP_TO_PARALLEL)
38720     res = 2;
38721
38722   return res;
38723 }
38724
38725 /* ??? No autovectorization into MMX or 3DNOW until we can reliably
38726    place emms and femms instructions.  */
38727
38728 static enum machine_mode
38729 ix86_preferred_simd_mode (enum machine_mode mode)
38730 {
38731   if (!TARGET_SSE)
38732     return word_mode;
38733
38734   switch (mode)
38735     {
38736     case QImode:
38737       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V32QImode : V16QImode;
38738     case HImode:
38739       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V16HImode : V8HImode;
38740     case SImode:
38741       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V8SImode : V4SImode;
38742     case DImode:
38743       return (TARGET_AVX && !TARGET_PREFER_AVX128) ? V4DImode : V2DImode;
38744
38745     case SFmode:
38746       if (TARGET_AVX && !TARGET_PREFER_AVX128)
38747         return V8SFmode;
38748       else
38749         return V4SFmode;
38750
38751     case DFmode:
38752       if (!TARGET_VECTORIZE_DOUBLE)
38753         return word_mode;
38754       else if (TARGET_AVX && !TARGET_PREFER_AVX128)
38755         return V4DFmode;
38756       else if (TARGET_SSE2)
38757         return V2DFmode;
38758       /* FALLTHRU */
38759
38760     default:
38761       return word_mode;
38762     }
38763 }
38764
38765 /* If AVX is enabled then try vectorizing with both 256bit and 128bit
38766    vectors.  */
38767
38768 static unsigned int
38769 ix86_autovectorize_vector_sizes (void)
38770 {
38771   return (TARGET_AVX && !TARGET_PREFER_AVX128) ? 32 | 16 : 0;
38772 }
38773
38774 /* Initialize the GCC target structure.  */
38775 #undef TARGET_RETURN_IN_MEMORY
38776 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
38777
38778 #undef TARGET_LEGITIMIZE_ADDRESS
38779 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
38780
38781 #undef TARGET_ATTRIBUTE_TABLE
38782 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
38783 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
38784 #  undef TARGET_MERGE_DECL_ATTRIBUTES
38785 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
38786 #endif
38787
38788 #undef TARGET_COMP_TYPE_ATTRIBUTES
38789 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
38790
38791 #undef TARGET_INIT_BUILTINS
38792 #define TARGET_INIT_BUILTINS ix86_init_builtins
38793 #undef TARGET_BUILTIN_DECL
38794 #define TARGET_BUILTIN_DECL ix86_builtin_decl
38795 #undef TARGET_EXPAND_BUILTIN
38796 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
38797
38798 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
38799 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
38800   ix86_builtin_vectorized_function
38801
38802 #undef TARGET_VECTORIZE_BUILTIN_TM_LOAD
38803 #define TARGET_VECTORIZE_BUILTIN_TM_LOAD ix86_builtin_tm_load
38804
38805 #undef TARGET_VECTORIZE_BUILTIN_TM_STORE
38806 #define TARGET_VECTORIZE_BUILTIN_TM_STORE ix86_builtin_tm_store
38807
38808 #undef TARGET_VECTORIZE_BUILTIN_GATHER
38809 #define TARGET_VECTORIZE_BUILTIN_GATHER ix86_vectorize_builtin_gather
38810
38811 #undef TARGET_BUILTIN_RECIPROCAL
38812 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
38813
38814 #undef TARGET_ASM_FUNCTION_EPILOGUE
38815 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
38816
38817 #undef TARGET_ENCODE_SECTION_INFO
38818 #ifndef SUBTARGET_ENCODE_SECTION_INFO
38819 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
38820 #else
38821 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
38822 #endif
38823
38824 #undef TARGET_ASM_OPEN_PAREN
38825 #define TARGET_ASM_OPEN_PAREN ""
38826 #undef TARGET_ASM_CLOSE_PAREN
38827 #define TARGET_ASM_CLOSE_PAREN ""
38828
38829 #undef TARGET_ASM_BYTE_OP
38830 #define TARGET_ASM_BYTE_OP ASM_BYTE
38831
38832 #undef TARGET_ASM_ALIGNED_HI_OP
38833 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
38834 #undef TARGET_ASM_ALIGNED_SI_OP
38835 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
38836 #ifdef ASM_QUAD
38837 #undef TARGET_ASM_ALIGNED_DI_OP
38838 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
38839 #endif
38840
38841 #undef TARGET_PROFILE_BEFORE_PROLOGUE
38842 #define TARGET_PROFILE_BEFORE_PROLOGUE ix86_profile_before_prologue
38843
38844 #undef TARGET_ASM_UNALIGNED_HI_OP
38845 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
38846 #undef TARGET_ASM_UNALIGNED_SI_OP
38847 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
38848 #undef TARGET_ASM_UNALIGNED_DI_OP
38849 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
38850
38851 #undef TARGET_PRINT_OPERAND
38852 #define TARGET_PRINT_OPERAND ix86_print_operand
38853 #undef TARGET_PRINT_OPERAND_ADDRESS
38854 #define TARGET_PRINT_OPERAND_ADDRESS ix86_print_operand_address
38855 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
38856 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ix86_print_operand_punct_valid_p
38857 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
38858 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA i386_asm_output_addr_const_extra
38859
38860 #undef TARGET_SCHED_INIT_GLOBAL
38861 #define TARGET_SCHED_INIT_GLOBAL ix86_sched_init_global
38862 #undef TARGET_SCHED_ADJUST_COST
38863 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
38864 #undef TARGET_SCHED_ISSUE_RATE
38865 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
38866 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
38867 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
38868   ia32_multipass_dfa_lookahead
38869
38870 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
38871 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
38872
38873 #ifdef HAVE_AS_TLS
38874 #undef TARGET_HAVE_TLS
38875 #define TARGET_HAVE_TLS true
38876 #endif
38877 #undef TARGET_CANNOT_FORCE_CONST_MEM
38878 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
38879 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
38880 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
38881
38882 #undef TARGET_DELEGITIMIZE_ADDRESS
38883 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
38884
38885 #undef TARGET_MS_BITFIELD_LAYOUT_P
38886 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
38887
38888 #if TARGET_MACHO
38889 #undef TARGET_BINDS_LOCAL_P
38890 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
38891 #endif
38892 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
38893 #undef TARGET_BINDS_LOCAL_P
38894 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
38895 #endif
38896
38897 #undef TARGET_ASM_OUTPUT_MI_THUNK
38898 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
38899 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
38900 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
38901
38902 #undef TARGET_ASM_FILE_START
38903 #define TARGET_ASM_FILE_START x86_file_start
38904
38905 #undef TARGET_OPTION_OVERRIDE
38906 #define TARGET_OPTION_OVERRIDE ix86_option_override
38907
38908 #undef TARGET_REGISTER_MOVE_COST
38909 #define TARGET_REGISTER_MOVE_COST ix86_register_move_cost
38910 #undef TARGET_MEMORY_MOVE_COST
38911 #define TARGET_MEMORY_MOVE_COST ix86_memory_move_cost
38912 #undef TARGET_RTX_COSTS
38913 #define TARGET_RTX_COSTS ix86_rtx_costs
38914 #undef TARGET_ADDRESS_COST
38915 #define TARGET_ADDRESS_COST ix86_address_cost
38916
38917 #undef TARGET_FIXED_CONDITION_CODE_REGS
38918 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
38919 #undef TARGET_CC_MODES_COMPATIBLE
38920 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
38921
38922 #undef TARGET_MACHINE_DEPENDENT_REORG
38923 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
38924
38925 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
38926 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
38927
38928 #undef TARGET_BUILD_BUILTIN_VA_LIST
38929 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
38930
38931 #undef TARGET_ENUM_VA_LIST_P
38932 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
38933
38934 #undef TARGET_FN_ABI_VA_LIST
38935 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
38936
38937 #undef TARGET_CANONICAL_VA_LIST_TYPE
38938 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
38939
38940 #undef TARGET_EXPAND_BUILTIN_VA_START
38941 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
38942
38943 #undef TARGET_MD_ASM_CLOBBERS
38944 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
38945
38946 #undef TARGET_PROMOTE_PROTOTYPES
38947 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
38948 #undef TARGET_STRUCT_VALUE_RTX
38949 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
38950 #undef TARGET_SETUP_INCOMING_VARARGS
38951 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
38952 #undef TARGET_MUST_PASS_IN_STACK
38953 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
38954 #undef TARGET_FUNCTION_ARG_ADVANCE
38955 #define TARGET_FUNCTION_ARG_ADVANCE ix86_function_arg_advance
38956 #undef TARGET_FUNCTION_ARG
38957 #define TARGET_FUNCTION_ARG ix86_function_arg
38958 #undef TARGET_FUNCTION_ARG_BOUNDARY
38959 #define TARGET_FUNCTION_ARG_BOUNDARY ix86_function_arg_boundary
38960 #undef TARGET_PASS_BY_REFERENCE
38961 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
38962 #undef TARGET_INTERNAL_ARG_POINTER
38963 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
38964 #undef TARGET_UPDATE_STACK_BOUNDARY
38965 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
38966 #undef TARGET_GET_DRAP_RTX
38967 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
38968 #undef TARGET_STRICT_ARGUMENT_NAMING
38969 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
38970 #undef TARGET_STATIC_CHAIN
38971 #define TARGET_STATIC_CHAIN ix86_static_chain
38972 #undef TARGET_TRAMPOLINE_INIT
38973 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
38974 #undef TARGET_RETURN_POPS_ARGS
38975 #define TARGET_RETURN_POPS_ARGS ix86_return_pops_args
38976
38977 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
38978 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
38979
38980 #undef TARGET_SCALAR_MODE_SUPPORTED_P
38981 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
38982
38983 #undef TARGET_VECTOR_MODE_SUPPORTED_P
38984 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
38985
38986 #undef TARGET_C_MODE_FOR_SUFFIX
38987 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
38988
38989 #ifdef HAVE_AS_TLS
38990 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
38991 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
38992 #endif
38993
38994 #ifdef SUBTARGET_INSERT_ATTRIBUTES
38995 #undef TARGET_INSERT_ATTRIBUTES
38996 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
38997 #endif
38998
38999 #undef TARGET_MANGLE_TYPE
39000 #define TARGET_MANGLE_TYPE ix86_mangle_type
39001
39002 #if !TARGET_MACHO
39003 #undef TARGET_STACK_PROTECT_FAIL
39004 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
39005 #endif
39006
39007 #undef TARGET_FUNCTION_VALUE
39008 #define TARGET_FUNCTION_VALUE ix86_function_value
39009
39010 #undef TARGET_FUNCTION_VALUE_REGNO_P
39011 #define TARGET_FUNCTION_VALUE_REGNO_P ix86_function_value_regno_p
39012
39013 #undef TARGET_PROMOTE_FUNCTION_MODE
39014 #define TARGET_PROMOTE_FUNCTION_MODE ix86_promote_function_mode
39015
39016 #undef TARGET_INSTANTIATE_DECLS
39017 #define TARGET_INSTANTIATE_DECLS ix86_instantiate_decls
39018
39019 #undef TARGET_SECONDARY_RELOAD
39020 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
39021
39022 #undef TARGET_CLASS_MAX_NREGS
39023 #define TARGET_CLASS_MAX_NREGS ix86_class_max_nregs
39024
39025 #undef TARGET_PREFERRED_RELOAD_CLASS
39026 #define TARGET_PREFERRED_RELOAD_CLASS ix86_preferred_reload_class
39027 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
39028 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS ix86_preferred_output_reload_class
39029 #undef TARGET_CLASS_LIKELY_SPILLED_P
39030 #define TARGET_CLASS_LIKELY_SPILLED_P ix86_class_likely_spilled_p
39031
39032 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
39033 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
39034   ix86_builtin_vectorization_cost
39035 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
39036 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK \
39037   ix86_vectorize_vec_perm_const_ok
39038 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
39039 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
39040   ix86_preferred_simd_mode
39041 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
39042 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
39043   ix86_autovectorize_vector_sizes
39044
39045 #undef TARGET_SET_CURRENT_FUNCTION
39046 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
39047
39048 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
39049 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
39050
39051 #undef TARGET_OPTION_SAVE
39052 #define TARGET_OPTION_SAVE ix86_function_specific_save
39053
39054 #undef TARGET_OPTION_RESTORE
39055 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
39056
39057 #undef TARGET_OPTION_PRINT
39058 #define TARGET_OPTION_PRINT ix86_function_specific_print
39059
39060 #undef TARGET_CAN_INLINE_P
39061 #define TARGET_CAN_INLINE_P ix86_can_inline_p
39062
39063 #undef TARGET_EXPAND_TO_RTL_HOOK
39064 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
39065
39066 #undef TARGET_LEGITIMATE_ADDRESS_P
39067 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
39068
39069 #undef TARGET_LEGITIMATE_CONSTANT_P
39070 #define TARGET_LEGITIMATE_CONSTANT_P ix86_legitimate_constant_p
39071
39072 #undef TARGET_FRAME_POINTER_REQUIRED
39073 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
39074
39075 #undef TARGET_CAN_ELIMINATE
39076 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
39077
39078 #undef TARGET_EXTRA_LIVE_ON_ENTRY
39079 #define TARGET_EXTRA_LIVE_ON_ENTRY ix86_live_on_entry
39080
39081 #undef TARGET_ASM_CODE_END
39082 #define TARGET_ASM_CODE_END ix86_code_end
39083
39084 #undef TARGET_CONDITIONAL_REGISTER_USAGE
39085 #define TARGET_CONDITIONAL_REGISTER_USAGE ix86_conditional_register_usage
39086
39087 #if TARGET_MACHO
39088 #undef TARGET_INIT_LIBFUNCS
39089 #define TARGET_INIT_LIBFUNCS darwin_rename_builtins
39090 #endif
39091
39092 struct gcc_target targetm = TARGET_INITIALIZER;
39093 \f
39094 #include "gt-i386.h"