OSDN Git Service

1484deab9d007672ad510c0ebc49f181935f003e
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 "langhooks.h"
49 #include "cgraph.h"
50 #include "gimple.h"
51 #include "dwarf2.h"
52 #include "df.h"
53 #include "tm-constrs.h"
54 #include "params.h"
55 #include "cselib.h"
56 #include "debug.h"
57 #include "dwarf2out.h"
58 #include "sched-int.h"
59
60 enum upper_128bits_state
61 {
62   unknown = 0,
63   unused,
64   used
65 };
66
67 typedef struct block_info_def
68 {
69   /* State of the upper 128bits of AVX registers at exit.  */
70   enum upper_128bits_state state;
71   /* TRUE if state of the upper 128bits of AVX registers is unchanged
72      in this block.  */
73   bool unchanged;
74   /* TRUE if block has been processed.  */
75   bool processed;
76 } *block_info;
77
78 #define BLOCK_INFO(B)   ((block_info) (B)->aux)
79
80 enum call_avx256_state
81 {
82   /* Callee returns 256bit AVX register.  */
83   callee_return_avx256 = -1,
84   /* Callee returns and passes 256bit AVX register.  */
85   callee_return_pass_avx256,
86   /* Callee passes 256bit AVX register.  */
87   callee_pass_avx256,
88   /* Callee doesn't return nor passe 256bit AVX register, or no
89      256bit AVX register in function return.  */
90   call_no_avx256,
91   /* vzeroupper intrinsic.  */
92   vzeroupper_intrinsic
93 };
94
95 /* Check if a 256bit AVX register is referenced in stores.   */
96
97 static void
98 check_avx256_stores (rtx dest, const_rtx set, void *data)
99 {
100   if ((REG_P (dest)
101        && VALID_AVX256_REG_MODE (GET_MODE (dest)))
102       || (GET_CODE (set) == SET
103           && REG_P (SET_SRC (set))
104           && VALID_AVX256_REG_MODE (GET_MODE (SET_SRC (set)))))
105     {
106       enum upper_128bits_state *state
107         = (enum upper_128bits_state *) data;
108       *state = used;
109     }
110 }
111
112 /* Helper function for move_or_delete_vzeroupper_1.  Look for vzeroupper
113    in basic block BB.  Delete it if upper 128bit AVX registers are
114    unused.  If it isn't deleted, move it to just before a jump insn.
115    
116    STATE is state of the upper 128bits of AVX registers at entry.  */
117
118 static void
119 move_or_delete_vzeroupper_2 (basic_block bb,
120                              enum upper_128bits_state state)
121 {
122   rtx insn, bb_end;
123   rtx vzeroupper_insn = NULL_RTX;
124   rtx pat;
125   int avx256;
126   bool unchanged;
127
128   if (BLOCK_INFO (bb)->unchanged)
129     {
130       if (dump_file)
131         fprintf (dump_file, " [bb %i] unchanged: upper 128bits: %d\n",
132                  bb->index, state);
133
134       BLOCK_INFO (bb)->state = state;
135       return;
136     }
137
138   if (dump_file)
139     fprintf (dump_file, " [bb %i] entry: upper 128bits: %d\n",
140              bb->index, state);
141
142   unchanged = true;
143
144   /* BB_END changes when it is deleted.  */
145   bb_end = BB_END (bb);
146   insn = BB_HEAD (bb);
147   while (insn != bb_end)
148     {
149       insn = NEXT_INSN (insn);
150
151       if (!NONDEBUG_INSN_P (insn))
152         continue;
153
154       /* Move vzeroupper before jump/call.  */
155       if (JUMP_P (insn) || CALL_P (insn))
156         {
157           if (!vzeroupper_insn)
158             continue;
159
160           if (PREV_INSN (insn) != vzeroupper_insn)
161             {
162               if (dump_file)
163                 {
164                   fprintf (dump_file, "Move vzeroupper after:\n");
165                   print_rtl_single (dump_file, PREV_INSN (insn));
166                   fprintf (dump_file, "before:\n");
167                   print_rtl_single (dump_file, insn);
168                 }
169               reorder_insns_nobb (vzeroupper_insn, vzeroupper_insn,
170                                   PREV_INSN (insn));
171             }
172           vzeroupper_insn = NULL_RTX;
173           continue;
174         }
175
176       pat = PATTERN (insn);
177
178       /* Check insn for vzeroupper intrinsic.  */
179       if (GET_CODE (pat) == UNSPEC_VOLATILE
180           && XINT (pat, 1) == UNSPECV_VZEROUPPER)
181         {
182           if (dump_file)
183             {
184               /* Found vzeroupper intrinsic.  */
185               fprintf (dump_file, "Found vzeroupper:\n");
186               print_rtl_single (dump_file, insn);
187             }
188         }
189       else
190         {
191           /* Check insn for vzeroall intrinsic.  */
192           if (GET_CODE (pat) == PARALLEL
193               && GET_CODE (XVECEXP (pat, 0, 0)) == UNSPEC_VOLATILE
194               && XINT (XVECEXP (pat, 0, 0), 1) == UNSPECV_VZEROALL)
195             {
196               state = unused;
197               unchanged = false;
198
199               /* Delete pending vzeroupper insertion.  */
200               if (vzeroupper_insn)
201                 {
202                   delete_insn (vzeroupper_insn);
203                   vzeroupper_insn = NULL_RTX;
204                 }
205             }
206           else if (state != used)
207             {
208               note_stores (pat, check_avx256_stores, &state);
209               if (state == used)
210                 unchanged = false;
211             }
212           continue;
213         }
214
215       /* Process vzeroupper intrinsic.  */
216       avx256 = INTVAL (XVECEXP (pat, 0, 0));
217
218       if (state == unused)
219         {
220           /* Since the upper 128bits are cleared, callee must not pass
221              256bit AVX register.  We only need to check if callee
222              returns 256bit AVX register.  */
223           if (avx256 == callee_return_avx256)
224             {
225               state = used;
226               unchanged = false;
227             }
228
229           /* Remove unnecessary vzeroupper since upper 128bits are
230              cleared.  */
231           if (dump_file)
232             {
233               fprintf (dump_file, "Delete redundant vzeroupper:\n");
234               print_rtl_single (dump_file, insn);
235             }
236           delete_insn (insn);
237         }
238       else
239         {
240           /* Set state to UNUSED if callee doesn't return 256bit AVX
241              register.  */
242           if (avx256 != callee_return_pass_avx256)
243             state = unused;
244
245           if (avx256 == callee_return_pass_avx256
246               || avx256 == callee_pass_avx256)
247             {
248               /* Must remove vzeroupper since callee passes in 256bit
249                  AVX register.  */
250               if (dump_file)
251                 {
252                   fprintf (dump_file, "Delete callee pass vzeroupper:\n");
253                   print_rtl_single (dump_file, insn);
254                 }
255               delete_insn (insn);
256             }
257           else
258             {
259               vzeroupper_insn = insn;
260               unchanged = false;
261             }
262         }
263     }
264
265   BLOCK_INFO (bb)->state = state;
266   BLOCK_INFO (bb)->unchanged = unchanged;
267
268   if (dump_file)
269     fprintf (dump_file, " [bb %i] exit: %s: upper 128bits: %d\n",
270              bb->index, unchanged ? "unchanged" : "changed",
271              state);
272 }
273
274 /* Helper function for move_or_delete_vzeroupper.  Process vzeroupper
275    in BLOCK and check its predecessor blocks.  Treat UNKNOWN state
276    as USED if UNKNOWN_IS_UNUSED is true.  */
277
278 static void
279 move_or_delete_vzeroupper_1 (basic_block block, bool unknown_is_unused)
280 {
281   edge e;
282   edge_iterator ei;
283   enum upper_128bits_state state, old_state, new_state;
284   bool seen_unknown;
285
286   if (dump_file)
287     fprintf (dump_file, " Process [bb %i]: status: %d\n",
288              block->index, BLOCK_INFO (block)->processed);
289
290   if (BLOCK_INFO (block)->processed)
291     return;
292
293   state = unused;
294
295   /* Check all predecessor edges of this block.  */
296   seen_unknown = false;
297   FOR_EACH_EDGE (e, ei, block->preds)
298     {
299       if (e->src == block)
300         continue;
301       switch (BLOCK_INFO (e->src)->state)
302         {
303         case unknown:
304           if (!unknown_is_unused)
305             seen_unknown = true;
306         case unused:
307           break;
308         case used:
309           state = used;
310           goto done;
311         }
312     }
313
314   if (seen_unknown)
315     state = unknown;
316
317 done:
318   old_state = BLOCK_INFO (block)->state;
319   move_or_delete_vzeroupper_2 (block, state);
320   new_state = BLOCK_INFO (block)->state;
321
322   if (state != unknown || new_state == used)
323     BLOCK_INFO (block)->processed = true;
324
325   /* Need to rescan if the upper 128bits of AVX registers are changed
326      to USED at exit.  */
327   if (new_state != old_state && new_state == used)
328     cfun->machine->rescan_vzeroupper_p = 1;
329 }
330
331 /* Go through the instruction stream looking for vzeroupper.  Delete
332    it if upper 128bit AVX registers are unused.  If it isn't deleted,
333    move it to just before a jump insn.  */
334
335 static void
336 move_or_delete_vzeroupper (void)
337 {
338   edge e;
339   edge_iterator ei;
340   basic_block bb;
341   unsigned int count;
342
343   /* Set up block info for each basic block.  */
344   alloc_aux_for_blocks (sizeof (struct block_info_def));
345
346   /* Process successor blocks of all entry points.  */
347   if (dump_file)
348     fprintf (dump_file, "Process all entry points\n");
349
350   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
351     {
352       move_or_delete_vzeroupper_2 (e->dest,
353                                    cfun->machine->caller_pass_avx256_p
354                                    ? used : unused);
355       BLOCK_INFO (e->dest)->processed = true;
356     }
357
358   /* Process all basic blocks.  */
359   count = 0;
360   do
361     {
362       if (dump_file)
363         fprintf (dump_file, "Process all basic blocks: trip %d\n",
364                  count);
365       cfun->machine->rescan_vzeroupper_p = 0;
366       FOR_EACH_BB (bb)
367         move_or_delete_vzeroupper_1 (bb, false);
368     }
369   while (cfun->machine->rescan_vzeroupper_p && count++ < 20);
370
371   /* FIXME: Is 20 big enough?  */
372   if (count >= 20)
373     gcc_unreachable ();
374
375   if (dump_file)
376     fprintf (dump_file, "Process all basic blocks\n");
377
378   FOR_EACH_BB (bb)
379     move_or_delete_vzeroupper_1 (bb, true);
380
381   free_aux_for_blocks ();
382 }
383
384 static rtx legitimize_dllimport_symbol (rtx, bool);
385
386 #ifndef CHECK_STACK_LIMIT
387 #define CHECK_STACK_LIMIT (-1)
388 #endif
389
390 /* Return index of given mode in mult and division cost tables.  */
391 #define MODE_INDEX(mode)                                        \
392   ((mode) == QImode ? 0                                         \
393    : (mode) == HImode ? 1                                       \
394    : (mode) == SImode ? 2                                       \
395    : (mode) == DImode ? 3                                       \
396    : 4)
397
398 /* Processor costs (relative to an add) */
399 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
400 #define COSTS_N_BYTES(N) ((N) * 2)
401
402 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
403
404 const
405 struct processor_costs ix86_size_cost = {/* costs for tuning for size */
406   COSTS_N_BYTES (2),                    /* cost of an add instruction */
407   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
408   COSTS_N_BYTES (2),                    /* variable shift costs */
409   COSTS_N_BYTES (3),                    /* constant shift costs */
410   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
411    COSTS_N_BYTES (3),                   /*                               HI */
412    COSTS_N_BYTES (3),                   /*                               SI */
413    COSTS_N_BYTES (3),                   /*                               DI */
414    COSTS_N_BYTES (5)},                  /*                            other */
415   0,                                    /* cost of multiply per each bit set */
416   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
417    COSTS_N_BYTES (3),                   /*                          HI */
418    COSTS_N_BYTES (3),                   /*                          SI */
419    COSTS_N_BYTES (3),                   /*                          DI */
420    COSTS_N_BYTES (5)},                  /*                          other */
421   COSTS_N_BYTES (3),                    /* cost of movsx */
422   COSTS_N_BYTES (3),                    /* cost of movzx */
423   0,                                    /* "large" insn */
424   2,                                    /* MOVE_RATIO */
425   2,                                 /* cost for loading QImode using movzbl */
426   {2, 2, 2},                            /* cost of loading integer registers
427                                            in QImode, HImode and SImode.
428                                            Relative to reg-reg move (2).  */
429   {2, 2, 2},                            /* cost of storing integer registers */
430   2,                                    /* cost of reg,reg fld/fst */
431   {2, 2, 2},                            /* cost of loading fp registers
432                                            in SFmode, DFmode and XFmode */
433   {2, 2, 2},                            /* cost of storing fp registers
434                                            in SFmode, DFmode and XFmode */
435   3,                                    /* cost of moving MMX register */
436   {3, 3},                               /* cost of loading MMX registers
437                                            in SImode and DImode */
438   {3, 3},                               /* cost of storing MMX registers
439                                            in SImode and DImode */
440   3,                                    /* cost of moving SSE register */
441   {3, 3, 3},                            /* cost of loading SSE registers
442                                            in SImode, DImode and TImode */
443   {3, 3, 3},                            /* cost of storing SSE registers
444                                            in SImode, DImode and TImode */
445   3,                                    /* MMX or SSE register to integer */
446   0,                                    /* size of l1 cache  */
447   0,                                    /* size of l2 cache  */
448   0,                                    /* size of prefetch block */
449   0,                                    /* number of parallel prefetches */
450   2,                                    /* Branch cost */
451   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
452   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
453   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
454   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
455   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
456   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
457   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
458    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
459   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
460    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
461   1,                                    /* scalar_stmt_cost.  */
462   1,                                    /* scalar load_cost.  */
463   1,                                    /* scalar_store_cost.  */
464   1,                                    /* vec_stmt_cost.  */
465   1,                                    /* vec_to_scalar_cost.  */
466   1,                                    /* scalar_to_vec_cost.  */
467   1,                                    /* vec_align_load_cost.  */
468   1,                                    /* vec_unalign_load_cost.  */
469   1,                                    /* vec_store_cost.  */
470   1,                                    /* cond_taken_branch_cost.  */
471   1,                                    /* cond_not_taken_branch_cost.  */
472 };
473
474 /* Processor costs (relative to an add) */
475 static const
476 struct processor_costs i386_cost = {    /* 386 specific costs */
477   COSTS_N_INSNS (1),                    /* cost of an add instruction */
478   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
479   COSTS_N_INSNS (3),                    /* variable shift costs */
480   COSTS_N_INSNS (2),                    /* constant shift costs */
481   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
482    COSTS_N_INSNS (6),                   /*                               HI */
483    COSTS_N_INSNS (6),                   /*                               SI */
484    COSTS_N_INSNS (6),                   /*                               DI */
485    COSTS_N_INSNS (6)},                  /*                            other */
486   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
487   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
488    COSTS_N_INSNS (23),                  /*                          HI */
489    COSTS_N_INSNS (23),                  /*                          SI */
490    COSTS_N_INSNS (23),                  /*                          DI */
491    COSTS_N_INSNS (23)},                 /*                          other */
492   COSTS_N_INSNS (3),                    /* cost of movsx */
493   COSTS_N_INSNS (2),                    /* cost of movzx */
494   15,                                   /* "large" insn */
495   3,                                    /* MOVE_RATIO */
496   4,                                 /* cost for loading QImode using movzbl */
497   {2, 4, 2},                            /* cost of loading integer registers
498                                            in QImode, HImode and SImode.
499                                            Relative to reg-reg move (2).  */
500   {2, 4, 2},                            /* cost of storing integer registers */
501   2,                                    /* cost of reg,reg fld/fst */
502   {8, 8, 8},                            /* cost of loading fp registers
503                                            in SFmode, DFmode and XFmode */
504   {8, 8, 8},                            /* cost of storing fp registers
505                                            in SFmode, DFmode and XFmode */
506   2,                                    /* cost of moving MMX register */
507   {4, 8},                               /* cost of loading MMX registers
508                                            in SImode and DImode */
509   {4, 8},                               /* cost of storing MMX registers
510                                            in SImode and DImode */
511   2,                                    /* cost of moving SSE register */
512   {4, 8, 16},                           /* cost of loading SSE registers
513                                            in SImode, DImode and TImode */
514   {4, 8, 16},                           /* cost of storing SSE registers
515                                            in SImode, DImode and TImode */
516   3,                                    /* MMX or SSE register to integer */
517   0,                                    /* size of l1 cache  */
518   0,                                    /* size of l2 cache  */
519   0,                                    /* size of prefetch block */
520   0,                                    /* number of parallel prefetches */
521   1,                                    /* Branch cost */
522   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
523   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
524   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
525   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
526   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
527   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
528   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
529    DUMMY_STRINGOP_ALGS},
530   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
531    DUMMY_STRINGOP_ALGS},
532   1,                                    /* scalar_stmt_cost.  */
533   1,                                    /* scalar load_cost.  */
534   1,                                    /* scalar_store_cost.  */
535   1,                                    /* vec_stmt_cost.  */
536   1,                                    /* vec_to_scalar_cost.  */
537   1,                                    /* scalar_to_vec_cost.  */
538   1,                                    /* vec_align_load_cost.  */
539   2,                                    /* vec_unalign_load_cost.  */
540   1,                                    /* vec_store_cost.  */
541   3,                                    /* cond_taken_branch_cost.  */
542   1,                                    /* cond_not_taken_branch_cost.  */
543 };
544
545 static const
546 struct processor_costs i486_cost = {    /* 486 specific costs */
547   COSTS_N_INSNS (1),                    /* cost of an add instruction */
548   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
549   COSTS_N_INSNS (3),                    /* variable shift costs */
550   COSTS_N_INSNS (2),                    /* constant shift costs */
551   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
552    COSTS_N_INSNS (12),                  /*                               HI */
553    COSTS_N_INSNS (12),                  /*                               SI */
554    COSTS_N_INSNS (12),                  /*                               DI */
555    COSTS_N_INSNS (12)},                 /*                            other */
556   1,                                    /* cost of multiply per each bit set */
557   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
558    COSTS_N_INSNS (40),                  /*                          HI */
559    COSTS_N_INSNS (40),                  /*                          SI */
560    COSTS_N_INSNS (40),                  /*                          DI */
561    COSTS_N_INSNS (40)},                 /*                          other */
562   COSTS_N_INSNS (3),                    /* cost of movsx */
563   COSTS_N_INSNS (2),                    /* cost of movzx */
564   15,                                   /* "large" insn */
565   3,                                    /* MOVE_RATIO */
566   4,                                 /* cost for loading QImode using movzbl */
567   {2, 4, 2},                            /* cost of loading integer registers
568                                            in QImode, HImode and SImode.
569                                            Relative to reg-reg move (2).  */
570   {2, 4, 2},                            /* cost of storing integer registers */
571   2,                                    /* cost of reg,reg fld/fst */
572   {8, 8, 8},                            /* cost of loading fp registers
573                                            in SFmode, DFmode and XFmode */
574   {8, 8, 8},                            /* cost of storing fp registers
575                                            in SFmode, DFmode and XFmode */
576   2,                                    /* cost of moving MMX register */
577   {4, 8},                               /* cost of loading MMX registers
578                                            in SImode and DImode */
579   {4, 8},                               /* cost of storing MMX registers
580                                            in SImode and DImode */
581   2,                                    /* cost of moving SSE register */
582   {4, 8, 16},                           /* cost of loading SSE registers
583                                            in SImode, DImode and TImode */
584   {4, 8, 16},                           /* cost of storing SSE registers
585                                            in SImode, DImode and TImode */
586   3,                                    /* MMX or SSE register to integer */
587   4,                                    /* size of l1 cache.  486 has 8kB cache
588                                            shared for code and data, so 4kB is
589                                            not really precise.  */
590   4,                                    /* size of l2 cache  */
591   0,                                    /* size of prefetch block */
592   0,                                    /* number of parallel prefetches */
593   1,                                    /* Branch cost */
594   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
595   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
596   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
597   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
598   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
599   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
600   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
601    DUMMY_STRINGOP_ALGS},
602   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
603    DUMMY_STRINGOP_ALGS},
604   1,                                    /* scalar_stmt_cost.  */
605   1,                                    /* scalar load_cost.  */
606   1,                                    /* scalar_store_cost.  */
607   1,                                    /* vec_stmt_cost.  */
608   1,                                    /* vec_to_scalar_cost.  */
609   1,                                    /* scalar_to_vec_cost.  */
610   1,                                    /* vec_align_load_cost.  */
611   2,                                    /* vec_unalign_load_cost.  */
612   1,                                    /* vec_store_cost.  */
613   3,                                    /* cond_taken_branch_cost.  */
614   1,                                    /* cond_not_taken_branch_cost.  */
615 };
616
617 static const
618 struct processor_costs pentium_cost = {
619   COSTS_N_INSNS (1),                    /* cost of an add instruction */
620   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
621   COSTS_N_INSNS (4),                    /* variable shift costs */
622   COSTS_N_INSNS (1),                    /* constant shift costs */
623   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
624    COSTS_N_INSNS (11),                  /*                               HI */
625    COSTS_N_INSNS (11),                  /*                               SI */
626    COSTS_N_INSNS (11),                  /*                               DI */
627    COSTS_N_INSNS (11)},                 /*                            other */
628   0,                                    /* cost of multiply per each bit set */
629   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
630    COSTS_N_INSNS (25),                  /*                          HI */
631    COSTS_N_INSNS (25),                  /*                          SI */
632    COSTS_N_INSNS (25),                  /*                          DI */
633    COSTS_N_INSNS (25)},                 /*                          other */
634   COSTS_N_INSNS (3),                    /* cost of movsx */
635   COSTS_N_INSNS (2),                    /* cost of movzx */
636   8,                                    /* "large" insn */
637   6,                                    /* MOVE_RATIO */
638   6,                                 /* cost for loading QImode using movzbl */
639   {2, 4, 2},                            /* cost of loading integer registers
640                                            in QImode, HImode and SImode.
641                                            Relative to reg-reg move (2).  */
642   {2, 4, 2},                            /* cost of storing integer registers */
643   2,                                    /* cost of reg,reg fld/fst */
644   {2, 2, 6},                            /* cost of loading fp registers
645                                            in SFmode, DFmode and XFmode */
646   {4, 4, 6},                            /* cost of storing fp registers
647                                            in SFmode, DFmode and XFmode */
648   8,                                    /* cost of moving MMX register */
649   {8, 8},                               /* cost of loading MMX registers
650                                            in SImode and DImode */
651   {8, 8},                               /* cost of storing MMX registers
652                                            in SImode and DImode */
653   2,                                    /* cost of moving SSE register */
654   {4, 8, 16},                           /* cost of loading SSE registers
655                                            in SImode, DImode and TImode */
656   {4, 8, 16},                           /* cost of storing SSE registers
657                                            in SImode, DImode and TImode */
658   3,                                    /* MMX or SSE register to integer */
659   8,                                    /* size of l1 cache.  */
660   8,                                    /* size of l2 cache  */
661   0,                                    /* size of prefetch block */
662   0,                                    /* number of parallel prefetches */
663   2,                                    /* Branch cost */
664   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
665   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
666   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
667   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
668   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
669   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
670   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
671    DUMMY_STRINGOP_ALGS},
672   {{libcall, {{-1, rep_prefix_4_byte}}},
673    DUMMY_STRINGOP_ALGS},
674   1,                                    /* scalar_stmt_cost.  */
675   1,                                    /* scalar load_cost.  */
676   1,                                    /* scalar_store_cost.  */
677   1,                                    /* vec_stmt_cost.  */
678   1,                                    /* vec_to_scalar_cost.  */
679   1,                                    /* scalar_to_vec_cost.  */
680   1,                                    /* vec_align_load_cost.  */
681   2,                                    /* vec_unalign_load_cost.  */
682   1,                                    /* vec_store_cost.  */
683   3,                                    /* cond_taken_branch_cost.  */
684   1,                                    /* cond_not_taken_branch_cost.  */
685 };
686
687 static const
688 struct processor_costs pentiumpro_cost = {
689   COSTS_N_INSNS (1),                    /* cost of an add instruction */
690   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
691   COSTS_N_INSNS (1),                    /* variable shift costs */
692   COSTS_N_INSNS (1),                    /* constant shift costs */
693   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
694    COSTS_N_INSNS (4),                   /*                               HI */
695    COSTS_N_INSNS (4),                   /*                               SI */
696    COSTS_N_INSNS (4),                   /*                               DI */
697    COSTS_N_INSNS (4)},                  /*                            other */
698   0,                                    /* cost of multiply per each bit set */
699   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
700    COSTS_N_INSNS (17),                  /*                          HI */
701    COSTS_N_INSNS (17),                  /*                          SI */
702    COSTS_N_INSNS (17),                  /*                          DI */
703    COSTS_N_INSNS (17)},                 /*                          other */
704   COSTS_N_INSNS (1),                    /* cost of movsx */
705   COSTS_N_INSNS (1),                    /* cost of movzx */
706   8,                                    /* "large" insn */
707   6,                                    /* MOVE_RATIO */
708   2,                                 /* cost for loading QImode using movzbl */
709   {4, 4, 4},                            /* cost of loading integer registers
710                                            in QImode, HImode and SImode.
711                                            Relative to reg-reg move (2).  */
712   {2, 2, 2},                            /* cost of storing integer registers */
713   2,                                    /* cost of reg,reg fld/fst */
714   {2, 2, 6},                            /* cost of loading fp registers
715                                            in SFmode, DFmode and XFmode */
716   {4, 4, 6},                            /* cost of storing fp registers
717                                            in SFmode, DFmode and XFmode */
718   2,                                    /* cost of moving MMX register */
719   {2, 2},                               /* cost of loading MMX registers
720                                            in SImode and DImode */
721   {2, 2},                               /* cost of storing MMX registers
722                                            in SImode and DImode */
723   2,                                    /* cost of moving SSE register */
724   {2, 2, 8},                            /* cost of loading SSE registers
725                                            in SImode, DImode and TImode */
726   {2, 2, 8},                            /* cost of storing SSE registers
727                                            in SImode, DImode and TImode */
728   3,                                    /* MMX or SSE register to integer */
729   8,                                    /* size of l1 cache.  */
730   256,                                  /* size of l2 cache  */
731   32,                                   /* size of prefetch block */
732   6,                                    /* number of parallel prefetches */
733   2,                                    /* Branch cost */
734   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
735   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
736   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
737   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
738   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
739   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
740   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes
741      (we ensure the alignment).  For small blocks inline loop is still a
742      noticeable win, for bigger blocks either rep movsl or rep movsb is
743      way to go.  Rep movsb has apparently more expensive startup time in CPU,
744      but after 4K the difference is down in the noise.  */
745   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
746                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
747    DUMMY_STRINGOP_ALGS},
748   {{rep_prefix_4_byte, {{1024, unrolled_loop},
749                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
750    DUMMY_STRINGOP_ALGS},
751   1,                                    /* scalar_stmt_cost.  */
752   1,                                    /* scalar load_cost.  */
753   1,                                    /* scalar_store_cost.  */
754   1,                                    /* vec_stmt_cost.  */
755   1,                                    /* vec_to_scalar_cost.  */
756   1,                                    /* scalar_to_vec_cost.  */
757   1,                                    /* vec_align_load_cost.  */
758   2,                                    /* vec_unalign_load_cost.  */
759   1,                                    /* vec_store_cost.  */
760   3,                                    /* cond_taken_branch_cost.  */
761   1,                                    /* cond_not_taken_branch_cost.  */
762 };
763
764 static const
765 struct processor_costs geode_cost = {
766   COSTS_N_INSNS (1),                    /* cost of an add instruction */
767   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
768   COSTS_N_INSNS (2),                    /* variable shift costs */
769   COSTS_N_INSNS (1),                    /* constant shift costs */
770   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
771    COSTS_N_INSNS (4),                   /*                               HI */
772    COSTS_N_INSNS (7),                   /*                               SI */
773    COSTS_N_INSNS (7),                   /*                               DI */
774    COSTS_N_INSNS (7)},                  /*                            other */
775   0,                                    /* cost of multiply per each bit set */
776   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
777    COSTS_N_INSNS (23),                  /*                          HI */
778    COSTS_N_INSNS (39),                  /*                          SI */
779    COSTS_N_INSNS (39),                  /*                          DI */
780    COSTS_N_INSNS (39)},                 /*                          other */
781   COSTS_N_INSNS (1),                    /* cost of movsx */
782   COSTS_N_INSNS (1),                    /* cost of movzx */
783   8,                                    /* "large" insn */
784   4,                                    /* MOVE_RATIO */
785   1,                                 /* cost for loading QImode using movzbl */
786   {1, 1, 1},                            /* cost of loading integer registers
787                                            in QImode, HImode and SImode.
788                                            Relative to reg-reg move (2).  */
789   {1, 1, 1},                            /* cost of storing integer registers */
790   1,                                    /* cost of reg,reg fld/fst */
791   {1, 1, 1},                            /* cost of loading fp registers
792                                            in SFmode, DFmode and XFmode */
793   {4, 6, 6},                            /* cost of storing fp registers
794                                            in SFmode, DFmode and XFmode */
795
796   1,                                    /* cost of moving MMX register */
797   {1, 1},                               /* cost of loading MMX registers
798                                            in SImode and DImode */
799   {1, 1},                               /* cost of storing MMX registers
800                                            in SImode and DImode */
801   1,                                    /* cost of moving SSE register */
802   {1, 1, 1},                            /* cost of loading SSE registers
803                                            in SImode, DImode and TImode */
804   {1, 1, 1},                            /* cost of storing SSE registers
805                                            in SImode, DImode and TImode */
806   1,                                    /* MMX or SSE register to integer */
807   64,                                   /* size of l1 cache.  */
808   128,                                  /* size of l2 cache.  */
809   32,                                   /* size of prefetch block */
810   1,                                    /* number of parallel prefetches */
811   1,                                    /* Branch cost */
812   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
813   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
814   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
815   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
816   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
817   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
818   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
819    DUMMY_STRINGOP_ALGS},
820   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
821    DUMMY_STRINGOP_ALGS},
822   1,                                    /* scalar_stmt_cost.  */
823   1,                                    /* scalar load_cost.  */
824   1,                                    /* scalar_store_cost.  */
825   1,                                    /* vec_stmt_cost.  */
826   1,                                    /* vec_to_scalar_cost.  */
827   1,                                    /* scalar_to_vec_cost.  */
828   1,                                    /* vec_align_load_cost.  */
829   2,                                    /* vec_unalign_load_cost.  */
830   1,                                    /* vec_store_cost.  */
831   3,                                    /* cond_taken_branch_cost.  */
832   1,                                    /* cond_not_taken_branch_cost.  */
833 };
834
835 static const
836 struct processor_costs k6_cost = {
837   COSTS_N_INSNS (1),                    /* cost of an add instruction */
838   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
839   COSTS_N_INSNS (1),                    /* variable shift costs */
840   COSTS_N_INSNS (1),                    /* constant shift costs */
841   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
842    COSTS_N_INSNS (3),                   /*                               HI */
843    COSTS_N_INSNS (3),                   /*                               SI */
844    COSTS_N_INSNS (3),                   /*                               DI */
845    COSTS_N_INSNS (3)},                  /*                            other */
846   0,                                    /* cost of multiply per each bit set */
847   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
848    COSTS_N_INSNS (18),                  /*                          HI */
849    COSTS_N_INSNS (18),                  /*                          SI */
850    COSTS_N_INSNS (18),                  /*                          DI */
851    COSTS_N_INSNS (18)},                 /*                          other */
852   COSTS_N_INSNS (2),                    /* cost of movsx */
853   COSTS_N_INSNS (2),                    /* cost of movzx */
854   8,                                    /* "large" insn */
855   4,                                    /* MOVE_RATIO */
856   3,                                 /* cost for loading QImode using movzbl */
857   {4, 5, 4},                            /* cost of loading integer registers
858                                            in QImode, HImode and SImode.
859                                            Relative to reg-reg move (2).  */
860   {2, 3, 2},                            /* cost of storing integer registers */
861   4,                                    /* cost of reg,reg fld/fst */
862   {6, 6, 6},                            /* cost of loading fp registers
863                                            in SFmode, DFmode and XFmode */
864   {4, 4, 4},                            /* cost of storing fp registers
865                                            in SFmode, DFmode and XFmode */
866   2,                                    /* cost of moving MMX register */
867   {2, 2},                               /* cost of loading MMX registers
868                                            in SImode and DImode */
869   {2, 2},                               /* cost of storing MMX registers
870                                            in SImode and DImode */
871   2,                                    /* cost of moving SSE register */
872   {2, 2, 8},                            /* cost of loading SSE registers
873                                            in SImode, DImode and TImode */
874   {2, 2, 8},                            /* cost of storing SSE registers
875                                            in SImode, DImode and TImode */
876   6,                                    /* MMX or SSE register to integer */
877   32,                                   /* size of l1 cache.  */
878   32,                                   /* size of l2 cache.  Some models
879                                            have integrated l2 cache, but
880                                            optimizing for k6 is not important
881                                            enough to worry about that.  */
882   32,                                   /* size of prefetch block */
883   1,                                    /* number of parallel prefetches */
884   1,                                    /* Branch cost */
885   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
886   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
887   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
888   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
889   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
890   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
891   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
892    DUMMY_STRINGOP_ALGS},
893   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
894    DUMMY_STRINGOP_ALGS},
895   1,                                    /* scalar_stmt_cost.  */
896   1,                                    /* scalar load_cost.  */
897   1,                                    /* scalar_store_cost.  */
898   1,                                    /* vec_stmt_cost.  */
899   1,                                    /* vec_to_scalar_cost.  */
900   1,                                    /* scalar_to_vec_cost.  */
901   1,                                    /* vec_align_load_cost.  */
902   2,                                    /* vec_unalign_load_cost.  */
903   1,                                    /* vec_store_cost.  */
904   3,                                    /* cond_taken_branch_cost.  */
905   1,                                    /* cond_not_taken_branch_cost.  */
906 };
907
908 static const
909 struct processor_costs athlon_cost = {
910   COSTS_N_INSNS (1),                    /* cost of an add instruction */
911   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
912   COSTS_N_INSNS (1),                    /* variable shift costs */
913   COSTS_N_INSNS (1),                    /* constant shift costs */
914   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
915    COSTS_N_INSNS (5),                   /*                               HI */
916    COSTS_N_INSNS (5),                   /*                               SI */
917    COSTS_N_INSNS (5),                   /*                               DI */
918    COSTS_N_INSNS (5)},                  /*                            other */
919   0,                                    /* cost of multiply per each bit set */
920   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
921    COSTS_N_INSNS (26),                  /*                          HI */
922    COSTS_N_INSNS (42),                  /*                          SI */
923    COSTS_N_INSNS (74),                  /*                          DI */
924    COSTS_N_INSNS (74)},                 /*                          other */
925   COSTS_N_INSNS (1),                    /* cost of movsx */
926   COSTS_N_INSNS (1),                    /* cost of movzx */
927   8,                                    /* "large" insn */
928   9,                                    /* MOVE_RATIO */
929   4,                                 /* cost for loading QImode using movzbl */
930   {3, 4, 3},                            /* cost of loading integer registers
931                                            in QImode, HImode and SImode.
932                                            Relative to reg-reg move (2).  */
933   {3, 4, 3},                            /* cost of storing integer registers */
934   4,                                    /* cost of reg,reg fld/fst */
935   {4, 4, 12},                           /* cost of loading fp registers
936                                            in SFmode, DFmode and XFmode */
937   {6, 6, 8},                            /* cost of storing fp registers
938                                            in SFmode, DFmode and XFmode */
939   2,                                    /* cost of moving MMX register */
940   {4, 4},                               /* cost of loading MMX registers
941                                            in SImode and DImode */
942   {4, 4},                               /* cost of storing MMX registers
943                                            in SImode and DImode */
944   2,                                    /* cost of moving SSE register */
945   {4, 4, 6},                            /* cost of loading SSE registers
946                                            in SImode, DImode and TImode */
947   {4, 4, 5},                            /* cost of storing SSE registers
948                                            in SImode, DImode and TImode */
949   5,                                    /* MMX or SSE register to integer */
950   64,                                   /* size of l1 cache.  */
951   256,                                  /* size of l2 cache.  */
952   64,                                   /* size of prefetch block */
953   6,                                    /* number of parallel prefetches */
954   5,                                    /* Branch cost */
955   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
956   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
957   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
958   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
959   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
960   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
961   /* For some reason, Athlon deals better with REP prefix (relative to loops)
962      compared to K8. Alignment becomes important after 8 bytes for memcpy and
963      128 bytes for memset.  */
964   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
965    DUMMY_STRINGOP_ALGS},
966   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
967    DUMMY_STRINGOP_ALGS},
968   1,                                    /* scalar_stmt_cost.  */
969   1,                                    /* scalar load_cost.  */
970   1,                                    /* scalar_store_cost.  */
971   1,                                    /* vec_stmt_cost.  */
972   1,                                    /* vec_to_scalar_cost.  */
973   1,                                    /* scalar_to_vec_cost.  */
974   1,                                    /* vec_align_load_cost.  */
975   2,                                    /* vec_unalign_load_cost.  */
976   1,                                    /* vec_store_cost.  */
977   3,                                    /* cond_taken_branch_cost.  */
978   1,                                    /* cond_not_taken_branch_cost.  */
979 };
980
981 static const
982 struct processor_costs k8_cost = {
983   COSTS_N_INSNS (1),                    /* cost of an add instruction */
984   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
985   COSTS_N_INSNS (1),                    /* variable shift costs */
986   COSTS_N_INSNS (1),                    /* constant shift costs */
987   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
988    COSTS_N_INSNS (4),                   /*                               HI */
989    COSTS_N_INSNS (3),                   /*                               SI */
990    COSTS_N_INSNS (4),                   /*                               DI */
991    COSTS_N_INSNS (5)},                  /*                            other */
992   0,                                    /* cost of multiply per each bit set */
993   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
994    COSTS_N_INSNS (26),                  /*                          HI */
995    COSTS_N_INSNS (42),                  /*                          SI */
996    COSTS_N_INSNS (74),                  /*                          DI */
997    COSTS_N_INSNS (74)},                 /*                          other */
998   COSTS_N_INSNS (1),                    /* cost of movsx */
999   COSTS_N_INSNS (1),                    /* cost of movzx */
1000   8,                                    /* "large" insn */
1001   9,                                    /* MOVE_RATIO */
1002   4,                                 /* cost for loading QImode using movzbl */
1003   {3, 4, 3},                            /* cost of loading integer registers
1004                                            in QImode, HImode and SImode.
1005                                            Relative to reg-reg move (2).  */
1006   {3, 4, 3},                            /* cost of storing integer registers */
1007   4,                                    /* cost of reg,reg fld/fst */
1008   {4, 4, 12},                           /* cost of loading fp registers
1009                                            in SFmode, DFmode and XFmode */
1010   {6, 6, 8},                            /* cost of storing fp registers
1011                                            in SFmode, DFmode and XFmode */
1012   2,                                    /* cost of moving MMX register */
1013   {3, 3},                               /* cost of loading MMX registers
1014                                            in SImode and DImode */
1015   {4, 4},                               /* cost of storing MMX registers
1016                                            in SImode and DImode */
1017   2,                                    /* cost of moving SSE register */
1018   {4, 3, 6},                            /* cost of loading SSE registers
1019                                            in SImode, DImode and TImode */
1020   {4, 4, 5},                            /* cost of storing SSE registers
1021                                            in SImode, DImode and TImode */
1022   5,                                    /* MMX or SSE register to integer */
1023   64,                                   /* size of l1 cache.  */
1024   512,                                  /* size of l2 cache.  */
1025   64,                                   /* size of prefetch block */
1026   /* New AMD processors never drop prefetches; if they cannot be performed
1027      immediately, they are queued.  We set number of simultaneous prefetches
1028      to a large constant to reflect this (it probably is not a good idea not
1029      to limit number of prefetches at all, as their execution also takes some
1030      time).  */
1031   100,                                  /* number of parallel prefetches */
1032   3,                                    /* Branch cost */
1033   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1034   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1035   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1036   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1037   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1038   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1039   /* K8 has optimized REP instruction for medium sized blocks, but for very
1040      small blocks it is better to use loop. For large blocks, libcall can
1041      do nontemporary accesses and beat inline considerably.  */
1042   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1043    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1044   {{libcall, {{8, loop}, {24, unrolled_loop},
1045               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1046    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1047   4,                                    /* scalar_stmt_cost.  */
1048   2,                                    /* scalar load_cost.  */
1049   2,                                    /* scalar_store_cost.  */
1050   5,                                    /* vec_stmt_cost.  */
1051   0,                                    /* vec_to_scalar_cost.  */
1052   2,                                    /* scalar_to_vec_cost.  */
1053   2,                                    /* vec_align_load_cost.  */
1054   3,                                    /* vec_unalign_load_cost.  */
1055   3,                                    /* vec_store_cost.  */
1056   3,                                    /* cond_taken_branch_cost.  */
1057   2,                                    /* cond_not_taken_branch_cost.  */
1058 };
1059
1060 struct processor_costs amdfam10_cost = {
1061   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1062   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1063   COSTS_N_INSNS (1),                    /* variable shift costs */
1064   COSTS_N_INSNS (1),                    /* constant shift costs */
1065   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1066    COSTS_N_INSNS (4),                   /*                               HI */
1067    COSTS_N_INSNS (3),                   /*                               SI */
1068    COSTS_N_INSNS (4),                   /*                               DI */
1069    COSTS_N_INSNS (5)},                  /*                            other */
1070   0,                                    /* cost of multiply per each bit set */
1071   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1072    COSTS_N_INSNS (35),                  /*                          HI */
1073    COSTS_N_INSNS (51),                  /*                          SI */
1074    COSTS_N_INSNS (83),                  /*                          DI */
1075    COSTS_N_INSNS (83)},                 /*                          other */
1076   COSTS_N_INSNS (1),                    /* cost of movsx */
1077   COSTS_N_INSNS (1),                    /* cost of movzx */
1078   8,                                    /* "large" insn */
1079   9,                                    /* MOVE_RATIO */
1080   4,                                 /* cost for loading QImode using movzbl */
1081   {3, 4, 3},                            /* cost of loading integer registers
1082                                            in QImode, HImode and SImode.
1083                                            Relative to reg-reg move (2).  */
1084   {3, 4, 3},                            /* cost of storing integer registers */
1085   4,                                    /* cost of reg,reg fld/fst */
1086   {4, 4, 12},                           /* cost of loading fp registers
1087                                            in SFmode, DFmode and XFmode */
1088   {6, 6, 8},                            /* cost of storing fp registers
1089                                            in SFmode, DFmode and XFmode */
1090   2,                                    /* cost of moving MMX register */
1091   {3, 3},                               /* cost of loading MMX registers
1092                                            in SImode and DImode */
1093   {4, 4},                               /* cost of storing MMX registers
1094                                            in SImode and DImode */
1095   2,                                    /* cost of moving SSE register */
1096   {4, 4, 3},                            /* cost of loading SSE registers
1097                                            in SImode, DImode and TImode */
1098   {4, 4, 5},                            /* cost of storing SSE registers
1099                                            in SImode, DImode and TImode */
1100   3,                                    /* MMX or SSE register to integer */
1101                                         /* On K8:
1102                                             MOVD reg64, xmmreg Double FSTORE 4
1103                                             MOVD reg32, xmmreg Double FSTORE 4
1104                                            On AMDFAM10:
1105                                             MOVD reg64, xmmreg Double FADD 3
1106                                                                1/1  1/1
1107                                             MOVD reg32, xmmreg Double FADD 3
1108                                                                1/1  1/1 */
1109   64,                                   /* size of l1 cache.  */
1110   512,                                  /* size of l2 cache.  */
1111   64,                                   /* size of prefetch block */
1112   /* New AMD processors never drop prefetches; if they cannot be performed
1113      immediately, they are queued.  We set number of simultaneous prefetches
1114      to a large constant to reflect this (it probably is not a good idea not
1115      to limit number of prefetches at all, as their execution also takes some
1116      time).  */
1117   100,                                  /* number of parallel prefetches */
1118   2,                                    /* Branch cost */
1119   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1120   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1121   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1122   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1123   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1124   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1125
1126   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
1127      very small blocks it is better to use loop. For large blocks, libcall can
1128      do nontemporary accesses and beat inline considerably.  */
1129   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1130    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1131   {{libcall, {{8, loop}, {24, unrolled_loop},
1132               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1133    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1134   4,                                    /* scalar_stmt_cost.  */
1135   2,                                    /* scalar load_cost.  */
1136   2,                                    /* scalar_store_cost.  */
1137   6,                                    /* vec_stmt_cost.  */
1138   0,                                    /* vec_to_scalar_cost.  */
1139   2,                                    /* scalar_to_vec_cost.  */
1140   2,                                    /* vec_align_load_cost.  */
1141   2,                                    /* vec_unalign_load_cost.  */
1142   2,                                    /* vec_store_cost.  */
1143   2,                                    /* cond_taken_branch_cost.  */
1144   1,                                    /* cond_not_taken_branch_cost.  */
1145 };
1146
1147 struct processor_costs bdver1_cost = {
1148   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1149   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1150   COSTS_N_INSNS (1),                    /* variable shift costs */
1151   COSTS_N_INSNS (1),                    /* constant shift costs */
1152   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
1153    COSTS_N_INSNS (4),                   /*                               HI */
1154    COSTS_N_INSNS (4),                   /*                               SI */
1155    COSTS_N_INSNS (6),                   /*                               DI */
1156    COSTS_N_INSNS (6)},                  /*                            other */
1157   0,                                    /* cost of multiply per each bit set */
1158   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1159    COSTS_N_INSNS (35),                  /*                          HI */
1160    COSTS_N_INSNS (51),                  /*                          SI */
1161    COSTS_N_INSNS (83),                  /*                          DI */
1162    COSTS_N_INSNS (83)},                 /*                          other */
1163   COSTS_N_INSNS (1),                    /* cost of movsx */
1164   COSTS_N_INSNS (1),                    /* cost of movzx */
1165   8,                                    /* "large" insn */
1166   9,                                    /* MOVE_RATIO */
1167   4,                                 /* cost for loading QImode using movzbl */
1168   {5, 5, 4},                            /* cost of loading integer registers
1169                                            in QImode, HImode and SImode.
1170                                            Relative to reg-reg move (2).  */
1171   {4, 4, 4},                            /* cost of storing integer registers */
1172   2,                                    /* cost of reg,reg fld/fst */
1173   {5, 5, 12},                           /* cost of loading fp registers
1174                                            in SFmode, DFmode and XFmode */
1175   {4, 4, 8},                            /* cost of storing fp registers
1176                                            in SFmode, DFmode and XFmode */
1177   2,                                    /* cost of moving MMX register */
1178   {4, 4},                               /* cost of loading MMX registers
1179                                            in SImode and DImode */
1180   {4, 4},                               /* cost of storing MMX registers
1181                                            in SImode and DImode */
1182   2,                                    /* cost of moving SSE register */
1183   {4, 4, 4},                            /* cost of loading SSE registers
1184                                            in SImode, DImode and TImode */
1185   {4, 4, 4},                            /* cost of storing SSE registers
1186                                            in SImode, DImode and TImode */
1187   2,                                    /* MMX or SSE register to integer */
1188                                         /* On K8:
1189                                             MOVD reg64, xmmreg Double FSTORE 4
1190                                             MOVD reg32, xmmreg Double FSTORE 4
1191                                            On AMDFAM10:
1192                                             MOVD reg64, xmmreg Double FADD 3
1193                                                                1/1  1/1
1194                                             MOVD reg32, xmmreg Double FADD 3
1195                                                                1/1  1/1 */
1196   16,                                   /* size of l1 cache.  */
1197   2048,                                 /* size of l2 cache.  */
1198   64,                                   /* size of prefetch block */
1199   /* New AMD processors never drop prefetches; if they cannot be performed
1200      immediately, they are queued.  We set number of simultaneous prefetches
1201      to a large constant to reflect this (it probably is not a good idea not
1202      to limit number of prefetches at all, as their execution also takes some
1203      time).  */
1204   100,                                  /* number of parallel prefetches */
1205   2,                                    /* Branch cost */
1206   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1207   COSTS_N_INSNS (6),                    /* cost of FMUL instruction.  */
1208   COSTS_N_INSNS (42),                   /* cost of FDIV instruction.  */
1209   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1210   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1211   COSTS_N_INSNS (52),                   /* cost of FSQRT instruction.  */
1212
1213   /*  BDVER1 has optimized REP instruction for medium sized blocks, but for
1214       very small blocks it is better to use loop. For large blocks, libcall
1215       can do nontemporary accesses and beat inline considerably.  */
1216   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1217    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1218   {{libcall, {{8, loop}, {24, unrolled_loop},
1219               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1220    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1221   6,                                    /* scalar_stmt_cost.  */
1222   4,                                    /* scalar load_cost.  */
1223   4,                                    /* scalar_store_cost.  */
1224   6,                                    /* vec_stmt_cost.  */
1225   0,                                    /* vec_to_scalar_cost.  */
1226   2,                                    /* scalar_to_vec_cost.  */
1227   4,                                    /* vec_align_load_cost.  */
1228   4,                                    /* vec_unalign_load_cost.  */
1229   4,                                    /* vec_store_cost.  */
1230   2,                                    /* cond_taken_branch_cost.  */
1231   1,                                    /* cond_not_taken_branch_cost.  */
1232 };
1233
1234 struct processor_costs btver1_cost = {
1235   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1236   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1237   COSTS_N_INSNS (1),                    /* variable shift costs */
1238   COSTS_N_INSNS (1),                    /* constant shift costs */
1239   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1240    COSTS_N_INSNS (4),                   /*                               HI */
1241    COSTS_N_INSNS (3),                   /*                               SI */
1242    COSTS_N_INSNS (4),                   /*                               DI */
1243    COSTS_N_INSNS (5)},                  /*                            other */
1244   0,                                    /* cost of multiply per each bit set */
1245   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1246    COSTS_N_INSNS (35),                  /*                          HI */
1247    COSTS_N_INSNS (51),                  /*                          SI */
1248    COSTS_N_INSNS (83),                  /*                          DI */
1249    COSTS_N_INSNS (83)},                 /*                          other */
1250   COSTS_N_INSNS (1),                    /* cost of movsx */
1251   COSTS_N_INSNS (1),                    /* cost of movzx */
1252   8,                                    /* "large" insn */
1253   9,                                    /* MOVE_RATIO */
1254   4,                                 /* cost for loading QImode using movzbl */
1255   {3, 4, 3},                            /* cost of loading integer registers
1256                                            in QImode, HImode and SImode.
1257                                            Relative to reg-reg move (2).  */
1258   {3, 4, 3},                            /* cost of storing integer registers */
1259   4,                                    /* cost of reg,reg fld/fst */
1260   {4, 4, 12},                           /* cost of loading fp registers
1261                                            in SFmode, DFmode and XFmode */
1262   {6, 6, 8},                            /* cost of storing fp registers
1263                                            in SFmode, DFmode and XFmode */
1264   2,                                    /* cost of moving MMX register */
1265   {3, 3},                               /* cost of loading MMX registers
1266                                            in SImode and DImode */
1267   {4, 4},                               /* cost of storing MMX registers
1268                                            in SImode and DImode */
1269   2,                                    /* cost of moving SSE register */
1270   {4, 4, 3},                            /* cost of loading SSE registers
1271                                            in SImode, DImode and TImode */
1272   {4, 4, 5},                            /* cost of storing SSE registers
1273                                            in SImode, DImode and TImode */
1274   3,                                    /* MMX or SSE register to integer */
1275                                         /* On K8:
1276                                            MOVD reg64, xmmreg Double FSTORE 4
1277                                            MOVD reg32, xmmreg Double FSTORE 4
1278                                            On AMDFAM10:
1279                                            MOVD reg64, xmmreg Double FADD 3
1280                                                                1/1  1/1
1281                                             MOVD reg32, xmmreg Double FADD 3
1282                                                                1/1  1/1 */
1283   32,                                   /* size of l1 cache.  */
1284   512,                                  /* size of l2 cache.  */
1285   64,                                   /* size of prefetch block */
1286   100,                                  /* number of parallel prefetches */
1287   2,                                    /* Branch cost */
1288   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1289   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1290   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1291   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1292   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1293   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1294
1295   /* BTVER1 has optimized REP instruction for medium sized blocks, but for
1296      very small blocks it is better to use loop. For large blocks, libcall can
1297      do nontemporary accesses and beat inline considerably.  */
1298   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1299    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1300   {{libcall, {{8, loop}, {24, unrolled_loop},
1301               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1302    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1303   4,                                    /* scalar_stmt_cost.  */
1304   2,                                    /* scalar load_cost.  */
1305   2,                                    /* scalar_store_cost.  */
1306   6,                                    /* vec_stmt_cost.  */
1307   0,                                    /* vec_to_scalar_cost.  */
1308   2,                                    /* scalar_to_vec_cost.  */
1309   2,                                    /* vec_align_load_cost.  */
1310   2,                                    /* vec_unalign_load_cost.  */
1311   2,                                    /* vec_store_cost.  */
1312   2,                                    /* cond_taken_branch_cost.  */
1313   1,                                    /* cond_not_taken_branch_cost.  */
1314 };
1315
1316 static const
1317 struct processor_costs pentium4_cost = {
1318   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1319   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
1320   COSTS_N_INSNS (4),                    /* variable shift costs */
1321   COSTS_N_INSNS (4),                    /* constant shift costs */
1322   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
1323    COSTS_N_INSNS (15),                  /*                               HI */
1324    COSTS_N_INSNS (15),                  /*                               SI */
1325    COSTS_N_INSNS (15),                  /*                               DI */
1326    COSTS_N_INSNS (15)},                 /*                            other */
1327   0,                                    /* cost of multiply per each bit set */
1328   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
1329    COSTS_N_INSNS (56),                  /*                          HI */
1330    COSTS_N_INSNS (56),                  /*                          SI */
1331    COSTS_N_INSNS (56),                  /*                          DI */
1332    COSTS_N_INSNS (56)},                 /*                          other */
1333   COSTS_N_INSNS (1),                    /* cost of movsx */
1334   COSTS_N_INSNS (1),                    /* cost of movzx */
1335   16,                                   /* "large" insn */
1336   6,                                    /* MOVE_RATIO */
1337   2,                                 /* cost for loading QImode using movzbl */
1338   {4, 5, 4},                            /* cost of loading integer registers
1339                                            in QImode, HImode and SImode.
1340                                            Relative to reg-reg move (2).  */
1341   {2, 3, 2},                            /* cost of storing integer registers */
1342   2,                                    /* cost of reg,reg fld/fst */
1343   {2, 2, 6},                            /* cost of loading fp registers
1344                                            in SFmode, DFmode and XFmode */
1345   {4, 4, 6},                            /* cost of storing fp registers
1346                                            in SFmode, DFmode and XFmode */
1347   2,                                    /* cost of moving MMX register */
1348   {2, 2},                               /* cost of loading MMX registers
1349                                            in SImode and DImode */
1350   {2, 2},                               /* cost of storing MMX registers
1351                                            in SImode and DImode */
1352   12,                                   /* cost of moving SSE register */
1353   {12, 12, 12},                         /* cost of loading SSE registers
1354                                            in SImode, DImode and TImode */
1355   {2, 2, 8},                            /* cost of storing SSE registers
1356                                            in SImode, DImode and TImode */
1357   10,                                   /* MMX or SSE register to integer */
1358   8,                                    /* size of l1 cache.  */
1359   256,                                  /* size of l2 cache.  */
1360   64,                                   /* size of prefetch block */
1361   6,                                    /* number of parallel prefetches */
1362   2,                                    /* Branch cost */
1363   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
1364   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
1365   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
1366   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1367   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1368   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
1369   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1370    DUMMY_STRINGOP_ALGS},
1371   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1372    {-1, libcall}}},
1373    DUMMY_STRINGOP_ALGS},
1374   1,                                    /* scalar_stmt_cost.  */
1375   1,                                    /* scalar load_cost.  */
1376   1,                                    /* scalar_store_cost.  */
1377   1,                                    /* vec_stmt_cost.  */
1378   1,                                    /* vec_to_scalar_cost.  */
1379   1,                                    /* scalar_to_vec_cost.  */
1380   1,                                    /* vec_align_load_cost.  */
1381   2,                                    /* vec_unalign_load_cost.  */
1382   1,                                    /* vec_store_cost.  */
1383   3,                                    /* cond_taken_branch_cost.  */
1384   1,                                    /* cond_not_taken_branch_cost.  */
1385 };
1386
1387 static const
1388 struct processor_costs nocona_cost = {
1389   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1390   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1391   COSTS_N_INSNS (1),                    /* variable shift costs */
1392   COSTS_N_INSNS (1),                    /* constant shift costs */
1393   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
1394    COSTS_N_INSNS (10),                  /*                               HI */
1395    COSTS_N_INSNS (10),                  /*                               SI */
1396    COSTS_N_INSNS (10),                  /*                               DI */
1397    COSTS_N_INSNS (10)},                 /*                            other */
1398   0,                                    /* cost of multiply per each bit set */
1399   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
1400    COSTS_N_INSNS (66),                  /*                          HI */
1401    COSTS_N_INSNS (66),                  /*                          SI */
1402    COSTS_N_INSNS (66),                  /*                          DI */
1403    COSTS_N_INSNS (66)},                 /*                          other */
1404   COSTS_N_INSNS (1),                    /* cost of movsx */
1405   COSTS_N_INSNS (1),                    /* cost of movzx */
1406   16,                                   /* "large" insn */
1407   17,                                   /* MOVE_RATIO */
1408   4,                                 /* cost for loading QImode using movzbl */
1409   {4, 4, 4},                            /* cost of loading integer registers
1410                                            in QImode, HImode and SImode.
1411                                            Relative to reg-reg move (2).  */
1412   {4, 4, 4},                            /* cost of storing integer registers */
1413   3,                                    /* cost of reg,reg fld/fst */
1414   {12, 12, 12},                         /* cost of loading fp registers
1415                                            in SFmode, DFmode and XFmode */
1416   {4, 4, 4},                            /* cost of storing fp registers
1417                                            in SFmode, DFmode and XFmode */
1418   6,                                    /* cost of moving MMX register */
1419   {12, 12},                             /* cost of loading MMX registers
1420                                            in SImode and DImode */
1421   {12, 12},                             /* cost of storing MMX registers
1422                                            in SImode and DImode */
1423   6,                                    /* cost of moving SSE register */
1424   {12, 12, 12},                         /* cost of loading SSE registers
1425                                            in SImode, DImode and TImode */
1426   {12, 12, 12},                         /* cost of storing SSE registers
1427                                            in SImode, DImode and TImode */
1428   8,                                    /* MMX or SSE register to integer */
1429   8,                                    /* size of l1 cache.  */
1430   1024,                                 /* size of l2 cache.  */
1431   128,                                  /* size of prefetch block */
1432   8,                                    /* number of parallel prefetches */
1433   1,                                    /* Branch cost */
1434   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1435   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1436   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
1437   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
1438   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
1439   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
1440   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1441    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
1442               {100000, unrolled_loop}, {-1, libcall}}}},
1443   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1444    {-1, libcall}}},
1445    {libcall, {{24, loop}, {64, unrolled_loop},
1446               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1447   1,                                    /* scalar_stmt_cost.  */
1448   1,                                    /* scalar load_cost.  */
1449   1,                                    /* scalar_store_cost.  */
1450   1,                                    /* vec_stmt_cost.  */
1451   1,                                    /* vec_to_scalar_cost.  */
1452   1,                                    /* scalar_to_vec_cost.  */
1453   1,                                    /* vec_align_load_cost.  */
1454   2,                                    /* vec_unalign_load_cost.  */
1455   1,                                    /* vec_store_cost.  */
1456   3,                                    /* cond_taken_branch_cost.  */
1457   1,                                    /* cond_not_taken_branch_cost.  */
1458 };
1459
1460 static const
1461 struct processor_costs atom_cost = {
1462   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1463   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1464   COSTS_N_INSNS (1),                    /* variable shift costs */
1465   COSTS_N_INSNS (1),                    /* constant shift costs */
1466   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1467    COSTS_N_INSNS (4),                   /*                               HI */
1468    COSTS_N_INSNS (3),                   /*                               SI */
1469    COSTS_N_INSNS (4),                   /*                               DI */
1470    COSTS_N_INSNS (2)},                  /*                            other */
1471   0,                                    /* cost of multiply per each bit set */
1472   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1473    COSTS_N_INSNS (26),                  /*                          HI */
1474    COSTS_N_INSNS (42),                  /*                          SI */
1475    COSTS_N_INSNS (74),                  /*                          DI */
1476    COSTS_N_INSNS (74)},                 /*                          other */
1477   COSTS_N_INSNS (1),                    /* cost of movsx */
1478   COSTS_N_INSNS (1),                    /* cost of movzx */
1479   8,                                    /* "large" insn */
1480   17,                                   /* MOVE_RATIO */
1481   2,                                 /* cost for loading QImode using movzbl */
1482   {4, 4, 4},                            /* cost of loading integer registers
1483                                            in QImode, HImode and SImode.
1484                                            Relative to reg-reg move (2).  */
1485   {4, 4, 4},                            /* cost of storing integer registers */
1486   4,                                    /* cost of reg,reg fld/fst */
1487   {12, 12, 12},                         /* cost of loading fp registers
1488                                            in SFmode, DFmode and XFmode */
1489   {6, 6, 8},                            /* cost of storing fp registers
1490                                            in SFmode, DFmode and XFmode */
1491   2,                                    /* cost of moving MMX register */
1492   {8, 8},                               /* cost of loading MMX registers
1493                                            in SImode and DImode */
1494   {8, 8},                               /* cost of storing MMX registers
1495                                            in SImode and DImode */
1496   2,                                    /* cost of moving SSE register */
1497   {8, 8, 8},                            /* cost of loading SSE registers
1498                                            in SImode, DImode and TImode */
1499   {8, 8, 8},                            /* cost of storing SSE registers
1500                                            in SImode, DImode and TImode */
1501   5,                                    /* MMX or SSE register to integer */
1502   32,                                   /* size of l1 cache.  */
1503   256,                                  /* size of l2 cache.  */
1504   64,                                   /* size of prefetch block */
1505   6,                                    /* number of parallel prefetches */
1506   3,                                    /* Branch cost */
1507   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1508   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1509   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1510   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1511   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1512   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1513   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1514    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1515           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1516   {{libcall, {{8, loop}, {15, unrolled_loop},
1517           {2048, rep_prefix_4_byte}, {-1, libcall}}},
1518    {libcall, {{24, loop}, {32, unrolled_loop},
1519           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1520   1,                                    /* scalar_stmt_cost.  */
1521   1,                                    /* scalar load_cost.  */
1522   1,                                    /* scalar_store_cost.  */
1523   1,                                    /* vec_stmt_cost.  */
1524   1,                                    /* vec_to_scalar_cost.  */
1525   1,                                    /* scalar_to_vec_cost.  */
1526   1,                                    /* vec_align_load_cost.  */
1527   2,                                    /* vec_unalign_load_cost.  */
1528   1,                                    /* vec_store_cost.  */
1529   3,                                    /* cond_taken_branch_cost.  */
1530   1,                                    /* cond_not_taken_branch_cost.  */
1531 };
1532
1533 /* Generic64 should produce code tuned for Nocona and K8.  */
1534 static const
1535 struct processor_costs generic64_cost = {
1536   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1537   /* On all chips taken into consideration lea is 2 cycles and more.  With
1538      this cost however our current implementation of synth_mult results in
1539      use of unnecessary temporary registers causing regression on several
1540      SPECfp benchmarks.  */
1541   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1542   COSTS_N_INSNS (1),                    /* variable shift costs */
1543   COSTS_N_INSNS (1),                    /* constant shift costs */
1544   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1545    COSTS_N_INSNS (4),                   /*                               HI */
1546    COSTS_N_INSNS (3),                   /*                               SI */
1547    COSTS_N_INSNS (4),                   /*                               DI */
1548    COSTS_N_INSNS (2)},                  /*                            other */
1549   0,                                    /* cost of multiply per each bit set */
1550   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1551    COSTS_N_INSNS (26),                  /*                          HI */
1552    COSTS_N_INSNS (42),                  /*                          SI */
1553    COSTS_N_INSNS (74),                  /*                          DI */
1554    COSTS_N_INSNS (74)},                 /*                          other */
1555   COSTS_N_INSNS (1),                    /* cost of movsx */
1556   COSTS_N_INSNS (1),                    /* cost of movzx */
1557   8,                                    /* "large" insn */
1558   17,                                   /* MOVE_RATIO */
1559   4,                                 /* cost for loading QImode using movzbl */
1560   {4, 4, 4},                            /* cost of loading integer registers
1561                                            in QImode, HImode and SImode.
1562                                            Relative to reg-reg move (2).  */
1563   {4, 4, 4},                            /* cost of storing integer registers */
1564   4,                                    /* cost of reg,reg fld/fst */
1565   {12, 12, 12},                         /* cost of loading fp registers
1566                                            in SFmode, DFmode and XFmode */
1567   {6, 6, 8},                            /* cost of storing fp registers
1568                                            in SFmode, DFmode and XFmode */
1569   2,                                    /* cost of moving MMX register */
1570   {8, 8},                               /* cost of loading MMX registers
1571                                            in SImode and DImode */
1572   {8, 8},                               /* cost of storing MMX registers
1573                                            in SImode and DImode */
1574   2,                                    /* cost of moving SSE register */
1575   {8, 8, 8},                            /* cost of loading SSE registers
1576                                            in SImode, DImode and TImode */
1577   {8, 8, 8},                            /* cost of storing SSE registers
1578                                            in SImode, DImode and TImode */
1579   5,                                    /* MMX or SSE register to integer */
1580   32,                                   /* size of l1 cache.  */
1581   512,                                  /* size of l2 cache.  */
1582   64,                                   /* size of prefetch block */
1583   6,                                    /* number of parallel prefetches */
1584   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this
1585      value is increased to perhaps more appropriate value of 5.  */
1586   3,                                    /* Branch cost */
1587   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1588   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1589   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1590   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1591   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1592   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1593   {DUMMY_STRINGOP_ALGS,
1594    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1595   {DUMMY_STRINGOP_ALGS,
1596    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1597   1,                                    /* scalar_stmt_cost.  */
1598   1,                                    /* scalar load_cost.  */
1599   1,                                    /* scalar_store_cost.  */
1600   1,                                    /* vec_stmt_cost.  */
1601   1,                                    /* vec_to_scalar_cost.  */
1602   1,                                    /* scalar_to_vec_cost.  */
1603   1,                                    /* vec_align_load_cost.  */
1604   2,                                    /* vec_unalign_load_cost.  */
1605   1,                                    /* vec_store_cost.  */
1606   3,                                    /* cond_taken_branch_cost.  */
1607   1,                                    /* cond_not_taken_branch_cost.  */
1608 };
1609
1610 /* Generic32 should produce code tuned for PPro, Pentium4, Nocona,
1611    Athlon and K8.  */
1612 static const
1613 struct processor_costs generic32_cost = {
1614   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1615   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1616   COSTS_N_INSNS (1),                    /* variable shift costs */
1617   COSTS_N_INSNS (1),                    /* constant shift costs */
1618   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1619    COSTS_N_INSNS (4),                   /*                               HI */
1620    COSTS_N_INSNS (3),                   /*                               SI */
1621    COSTS_N_INSNS (4),                   /*                               DI */
1622    COSTS_N_INSNS (2)},                  /*                            other */
1623   0,                                    /* cost of multiply per each bit set */
1624   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1625    COSTS_N_INSNS (26),                  /*                          HI */
1626    COSTS_N_INSNS (42),                  /*                          SI */
1627    COSTS_N_INSNS (74),                  /*                          DI */
1628    COSTS_N_INSNS (74)},                 /*                          other */
1629   COSTS_N_INSNS (1),                    /* cost of movsx */
1630   COSTS_N_INSNS (1),                    /* cost of movzx */
1631   8,                                    /* "large" insn */
1632   17,                                   /* MOVE_RATIO */
1633   4,                                 /* cost for loading QImode using movzbl */
1634   {4, 4, 4},                            /* cost of loading integer registers
1635                                            in QImode, HImode and SImode.
1636                                            Relative to reg-reg move (2).  */
1637   {4, 4, 4},                            /* cost of storing integer registers */
1638   4,                                    /* cost of reg,reg fld/fst */
1639   {12, 12, 12},                         /* cost of loading fp registers
1640                                            in SFmode, DFmode and XFmode */
1641   {6, 6, 8},                            /* cost of storing fp registers
1642                                            in SFmode, DFmode and XFmode */
1643   2,                                    /* cost of moving MMX register */
1644   {8, 8},                               /* cost of loading MMX registers
1645                                            in SImode and DImode */
1646   {8, 8},                               /* cost of storing MMX registers
1647                                            in SImode and DImode */
1648   2,                                    /* cost of moving SSE register */
1649   {8, 8, 8},                            /* cost of loading SSE registers
1650                                            in SImode, DImode and TImode */
1651   {8, 8, 8},                            /* cost of storing SSE registers
1652                                            in SImode, DImode and TImode */
1653   5,                                    /* MMX or SSE register to integer */
1654   32,                                   /* size of l1 cache.  */
1655   256,                                  /* size of l2 cache.  */
1656   64,                                   /* size of prefetch block */
1657   6,                                    /* number of parallel prefetches */
1658   3,                                    /* Branch cost */
1659   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1660   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1661   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1662   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1663   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1664   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1665   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1666    DUMMY_STRINGOP_ALGS},
1667   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1668    DUMMY_STRINGOP_ALGS},
1669   1,                                    /* scalar_stmt_cost.  */
1670   1,                                    /* scalar load_cost.  */
1671   1,                                    /* scalar_store_cost.  */
1672   1,                                    /* vec_stmt_cost.  */
1673   1,                                    /* vec_to_scalar_cost.  */
1674   1,                                    /* scalar_to_vec_cost.  */
1675   1,                                    /* vec_align_load_cost.  */
1676   2,                                    /* vec_unalign_load_cost.  */
1677   1,                                    /* vec_store_cost.  */
1678   3,                                    /* cond_taken_branch_cost.  */
1679   1,                                    /* cond_not_taken_branch_cost.  */
1680 };
1681
1682 const struct processor_costs *ix86_cost = &pentium_cost;
1683
1684 /* Processor feature/optimization bitmasks.  */
1685 #define m_386 (1<<PROCESSOR_I386)
1686 #define m_486 (1<<PROCESSOR_I486)
1687 #define m_PENT (1<<PROCESSOR_PENTIUM)
1688 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
1689 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
1690 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
1691 #define m_CORE2_32  (1<<PROCESSOR_CORE2_32)
1692 #define m_CORE2_64  (1<<PROCESSOR_CORE2_64)
1693 #define m_COREI7_32  (1<<PROCESSOR_COREI7_32)
1694 #define m_COREI7_64  (1<<PROCESSOR_COREI7_64)
1695 #define m_COREI7  (m_COREI7_32 | m_COREI7_64)
1696 #define m_CORE2I7_32  (m_CORE2_32 | m_COREI7_32)
1697 #define m_CORE2I7_64  (m_CORE2_64 | m_COREI7_64)
1698 #define m_CORE2I7  (m_CORE2I7_32 | m_CORE2I7_64)
1699 #define m_ATOM  (1<<PROCESSOR_ATOM)
1700
1701 #define m_GEODE  (1<<PROCESSOR_GEODE)
1702 #define m_K6  (1<<PROCESSOR_K6)
1703 #define m_K6_GEODE  (m_K6 | m_GEODE)
1704 #define m_K8  (1<<PROCESSOR_K8)
1705 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
1706 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
1707 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
1708 #define m_BDVER1  (1<<PROCESSOR_BDVER1)
1709 #define m_BTVER1  (1<<PROCESSOR_BTVER1)
1710 #define m_AMD_MULTIPLE  (m_K8 | m_ATHLON | m_AMDFAM10 | m_BDVER1 | m_BTVER1)
1711
1712 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1713 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1714
1715 /* Generic instruction choice should be common subset of supported CPUs
1716    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1717 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1718
1719 /* Feature tests against the various tunings.  */
1720 unsigned char ix86_tune_features[X86_TUNE_LAST];
1721
1722 /* Feature tests against the various tunings used to create ix86_tune_features
1723    based on the processor mask.  */
1724 static unsigned int initial_ix86_tune_features[X86_TUNE_LAST] = {
1725   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1726      negatively, so enabling for Generic64 seems like good code size
1727      tradeoff.  We can't enable it for 32bit generic because it does not
1728      work well with PPro base chips.  */
1729   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_CORE2I7_64 | m_GENERIC64,
1730
1731   /* X86_TUNE_PUSH_MEMORY */
1732   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4
1733   | m_NOCONA | m_CORE2I7 | m_GENERIC,
1734
1735   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1736   m_486 | m_PENT,
1737
1738   /* X86_TUNE_UNROLL_STRLEN */
1739   m_486 | m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_K6
1740   | m_CORE2I7 | m_GENERIC,
1741
1742   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1743   m_ATOM | m_PPRO | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4
1744   | m_CORE2I7 | m_GENERIC,
1745
1746   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1747      on simulation result. But after P4 was made, no performance benefit
1748      was observed with branch hints.  It also increases the code size.
1749      As a result, icc never generates branch hints.  */
1750   0,
1751
1752   /* X86_TUNE_DOUBLE_WITH_ADD */
1753   ~m_386,
1754
1755   /* X86_TUNE_USE_SAHF */
1756   m_ATOM | m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_BDVER1 | m_BTVER1
1757   | m_PENT4 | m_NOCONA | m_CORE2I7 | m_GENERIC,
1758
1759   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1760      partial dependencies.  */
1761   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_PENT4 | m_NOCONA
1762   | m_CORE2I7 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1763
1764   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1765      register stalls on Generic32 compilation setting as well.  However
1766      in current implementation the partial register stalls are not eliminated
1767      very well - they can be introduced via subregs synthesized by combine
1768      and can happen in caller/callee saving sequences.  Because this option
1769      pays back little on PPro based chips and is in conflict with partial reg
1770      dependencies used by Athlon/P4 based chips, it is better to leave it off
1771      for generic32 for now.  */
1772   m_PPRO,
1773
1774   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1775   m_CORE2I7 | m_GENERIC,
1776
1777   /* X86_TUNE_USE_HIMODE_FIOP */
1778   m_386 | m_486 | m_K6_GEODE,
1779
1780   /* X86_TUNE_USE_SIMODE_FIOP */
1781   ~(m_PPRO | m_AMD_MULTIPLE | m_PENT | m_ATOM | m_CORE2I7 | m_GENERIC),
1782
1783   /* X86_TUNE_USE_MOV0 */
1784   m_K6,
1785
1786   /* X86_TUNE_USE_CLTD */
1787   ~(m_PENT | m_ATOM | m_K6 | m_CORE2I7 | m_GENERIC),
1788
1789   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1790   m_PENT4,
1791
1792   /* X86_TUNE_SPLIT_LONG_MOVES */
1793   m_PPRO,
1794
1795   /* X86_TUNE_READ_MODIFY_WRITE */
1796   ~m_PENT,
1797
1798   /* X86_TUNE_READ_MODIFY */
1799   ~(m_PENT | m_PPRO),
1800
1801   /* X86_TUNE_PROMOTE_QIMODE */
1802   m_K6_GEODE | m_PENT | m_ATOM | m_386 | m_486 | m_AMD_MULTIPLE
1803   | m_CORE2I7 | m_GENERIC /* | m_PENT4 ? */,
1804
1805   /* X86_TUNE_FAST_PREFIX */
1806   ~(m_PENT | m_486 | m_386),
1807
1808   /* X86_TUNE_SINGLE_STRINGOP */
1809   m_386 | m_PENT4 | m_NOCONA,
1810
1811   /* X86_TUNE_QIMODE_MATH */
1812   ~0,
1813
1814   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1815      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1816      might be considered for Generic32 if our scheme for avoiding partial
1817      stalls was more effective.  */
1818   ~m_PPRO,
1819
1820   /* X86_TUNE_PROMOTE_QI_REGS */
1821   0,
1822
1823   /* X86_TUNE_PROMOTE_HI_REGS */
1824   m_PPRO,
1825
1826   /* X86_TUNE_SINGLE_POP: Enable if single pop insn is preferred
1827      over esp addition.  */
1828   m_386 | m_486 | m_PENT | m_PPRO,
1829
1830   /* X86_TUNE_DOUBLE_POP: Enable if double pop insn is preferred
1831      over esp addition.  */
1832   m_PENT,
1833
1834   /* X86_TUNE_SINGLE_PUSH: Enable if single push insn is preferred
1835      over esp subtraction.  */
1836   m_386 | m_486 | m_PENT | m_K6_GEODE,
1837
1838   /* X86_TUNE_DOUBLE_PUSH. Enable if double push insn is preferred
1839      over esp subtraction.  */
1840   m_PENT | m_K6_GEODE,
1841
1842   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1843      for DFmode copies */
1844   ~(m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2I7
1845     | m_GENERIC | m_GEODE),
1846
1847   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1848   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2I7 | m_GENERIC,
1849
1850   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1851      conflict here in between PPro/Pentium4 based chips that thread 128bit
1852      SSE registers as single units versus K8 based chips that divide SSE
1853      registers to two 64bit halves.  This knob promotes all store destinations
1854      to be 128bit to allow register renaming on 128bit SSE units, but usually
1855      results in one extra microop on 64bit SSE units.  Experimental results
1856      shows that disabling this option on P4 brings over 20% SPECfp regression,
1857      while enabling it on K8 brings roughly 2.4% regression that can be partly
1858      masked by careful scheduling of moves.  */
1859   m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2I7 | m_GENERIC
1860   | m_AMDFAM10 | m_BDVER1,
1861
1862   /* X86_TUNE_SSE_UNALIGNED_LOAD_OPTIMAL */
1863   m_AMDFAM10 | m_BDVER1 | m_BTVER1 | m_COREI7,
1864
1865   /* X86_TUNE_SSE_UNALIGNED_STORE_OPTIMAL */
1866   m_BDVER1 | m_COREI7,
1867
1868   /* X86_TUNE_SSE_PACKED_SINGLE_INSN_OPTIMAL */
1869   m_BDVER1,
1870
1871   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1872      are resolved on SSE register parts instead of whole registers, so we may
1873      maintain just lower part of scalar values in proper format leaving the
1874      upper part undefined.  */
1875   m_ATHLON_K8,
1876
1877   /* X86_TUNE_SSE_TYPELESS_STORES */
1878   m_AMD_MULTIPLE,
1879
1880   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1881   m_PPRO | m_PENT4 | m_NOCONA,
1882
1883   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1884   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2I7 | m_GENERIC,
1885
1886   /* X86_TUNE_PROLOGUE_USING_MOVE */
1887   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2I7 | m_GENERIC,
1888
1889   /* X86_TUNE_EPILOGUE_USING_MOVE */
1890   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2I7 | m_GENERIC,
1891
1892   /* X86_TUNE_SHIFT1 */
1893   ~m_486,
1894
1895   /* X86_TUNE_USE_FFREEP */
1896   m_AMD_MULTIPLE,
1897
1898   /* X86_TUNE_INTER_UNIT_MOVES */
1899   ~(m_AMD_MULTIPLE | m_GENERIC),
1900
1901   /* X86_TUNE_INTER_UNIT_CONVERSIONS */
1902   ~(m_AMDFAM10 | m_BDVER1),
1903
1904   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1905      than 4 branch instructions in the 16 byte window.  */
1906   m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2I7
1907   | m_GENERIC,
1908
1909   /* X86_TUNE_SCHEDULE */
1910   m_PPRO | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT | m_ATOM | m_CORE2I7
1911   | m_GENERIC,
1912
1913   /* X86_TUNE_USE_BT */
1914   m_AMD_MULTIPLE | m_ATOM | m_CORE2I7 | m_GENERIC,
1915
1916   /* X86_TUNE_USE_INCDEC */
1917   ~(m_PENT4 | m_NOCONA | m_CORE2I7 | m_GENERIC | m_ATOM),
1918
1919   /* X86_TUNE_PAD_RETURNS */
1920   m_AMD_MULTIPLE | m_CORE2I7 | m_GENERIC,
1921
1922   /* X86_TUNE_PAD_SHORT_FUNCTION: Pad short funtion.  */
1923   m_ATOM,
1924
1925   /* X86_TUNE_EXT_80387_CONSTANTS */
1926   m_K6_GEODE | m_ATHLON_K8 | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO
1927   | m_CORE2I7 | m_GENERIC,
1928
1929   /* X86_TUNE_SHORTEN_X87_SSE */
1930   ~m_K8,
1931
1932   /* X86_TUNE_AVOID_VECTOR_DECODE */
1933   m_K8 | m_CORE2I7_64 | m_GENERIC64,
1934
1935   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1936      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1937   ~(m_386 | m_486),
1938
1939   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1940      vector path on AMD machines.  */
1941   m_K8 | m_CORE2I7_64 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1 | m_BTVER1,
1942
1943   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1944      machines.  */
1945   m_K8 | m_CORE2I7_64 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1 | m_BTVER1,
1946
1947   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1948      than a MOV.  */
1949   m_PENT,
1950
1951   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1952      but one byte longer.  */
1953   m_PENT,
1954
1955   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1956      operand that cannot be represented using a modRM byte.  The XOR
1957      replacement is long decoded, so this split helps here as well.  */
1958   m_K6,
1959
1960   /* X86_TUNE_USE_VECTOR_FP_CONVERTS: Prefer vector packed SSE conversion
1961      from FP to FP. */
1962   m_AMDFAM10 | m_CORE2I7 | m_GENERIC,
1963
1964   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
1965      from integer to FP. */
1966   m_AMDFAM10,
1967
1968   /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction
1969      with a subsequent conditional jump instruction into a single
1970      compare-and-branch uop.  */
1971   m_BDVER1,
1972
1973   /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag
1974      will impact LEA instruction selection. */
1975   m_ATOM,
1976
1977   /* X86_TUNE_VECTORIZE_DOUBLE: Enable double precision vector
1978      instructions.  */
1979   ~m_ATOM,
1980 };
1981
1982 /* Feature tests against the various architecture variations.  */
1983 unsigned char ix86_arch_features[X86_ARCH_LAST];
1984
1985 /* Feature tests against the various architecture variations, used to create
1986    ix86_arch_features based on the processor mask.  */
1987 static unsigned int initial_ix86_arch_features[X86_ARCH_LAST] = {
1988   /* X86_ARCH_CMOVE: Conditional move was added for pentiumpro.  */
1989   ~(m_386 | m_486 | m_PENT | m_K6),
1990
1991   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1992   ~m_386,
1993
1994   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1995   ~(m_386 | m_486),
1996
1997   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1998   ~m_386,
1999
2000   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
2001   ~m_386,
2002 };
2003
2004 static const unsigned int x86_accumulate_outgoing_args
2005   = m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2I7
2006     | m_GENERIC;
2007
2008 static const unsigned int x86_arch_always_fancy_math_387
2009   = m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4
2010     | m_NOCONA | m_CORE2I7 | m_GENERIC;
2011
2012 static enum stringop_alg stringop_alg = no_stringop;
2013
2014 /* In case the average insn count for single function invocation is
2015    lower than this constant, emit fast (but longer) prologue and
2016    epilogue code.  */
2017 #define FAST_PROLOGUE_INSN_COUNT 20
2018
2019 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
2020 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
2021 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
2022 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
2023
2024 /* Array of the smallest class containing reg number REGNO, indexed by
2025    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
2026
2027 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
2028 {
2029   /* ax, dx, cx, bx */
2030   AREG, DREG, CREG, BREG,
2031   /* si, di, bp, sp */
2032   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
2033   /* FP registers */
2034   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
2035   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
2036   /* arg pointer */
2037   NON_Q_REGS,
2038   /* flags, fpsr, fpcr, frame */
2039   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
2040   /* SSE registers */
2041   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
2042   SSE_REGS, SSE_REGS,
2043   /* MMX registers */
2044   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
2045   MMX_REGS, MMX_REGS,
2046   /* REX registers */
2047   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
2048   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
2049   /* SSE REX registers */
2050   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
2051   SSE_REGS, SSE_REGS,
2052 };
2053
2054 /* The "default" register map used in 32bit mode.  */
2055
2056 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
2057 {
2058   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
2059   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
2060   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
2061   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
2062   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
2063   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
2064   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
2065 };
2066
2067 /* The "default" register map used in 64bit mode.  */
2068
2069 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
2070 {
2071   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
2072   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
2073   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
2074   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
2075   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
2076   8,9,10,11,12,13,14,15,                /* extended integer registers */
2077   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
2078 };
2079
2080 /* Define the register numbers to be used in Dwarf debugging information.
2081    The SVR4 reference port C compiler uses the following register numbers
2082    in its Dwarf output code:
2083         0 for %eax (gcc regno = 0)
2084         1 for %ecx (gcc regno = 2)
2085         2 for %edx (gcc regno = 1)
2086         3 for %ebx (gcc regno = 3)
2087         4 for %esp (gcc regno = 7)
2088         5 for %ebp (gcc regno = 6)
2089         6 for %esi (gcc regno = 4)
2090         7 for %edi (gcc regno = 5)
2091    The following three DWARF register numbers are never generated by
2092    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
2093    believes these numbers have these meanings.
2094         8  for %eip    (no gcc equivalent)
2095         9  for %eflags (gcc regno = 17)
2096         10 for %trapno (no gcc equivalent)
2097    It is not at all clear how we should number the FP stack registers
2098    for the x86 architecture.  If the version of SDB on x86/svr4 were
2099    a bit less brain dead with respect to floating-point then we would
2100    have a precedent to follow with respect to DWARF register numbers
2101    for x86 FP registers, but the SDB on x86/svr4 is so completely
2102    broken with respect to FP registers that it is hardly worth thinking
2103    of it as something to strive for compatibility with.
2104    The version of x86/svr4 SDB I have at the moment does (partially)
2105    seem to believe that DWARF register number 11 is associated with
2106    the x86 register %st(0), but that's about all.  Higher DWARF
2107    register numbers don't seem to be associated with anything in
2108    particular, and even for DWARF regno 11, SDB only seems to under-
2109    stand that it should say that a variable lives in %st(0) (when
2110    asked via an `=' command) if we said it was in DWARF regno 11,
2111    but SDB still prints garbage when asked for the value of the
2112    variable in question (via a `/' command).
2113    (Also note that the labels SDB prints for various FP stack regs
2114    when doing an `x' command are all wrong.)
2115    Note that these problems generally don't affect the native SVR4
2116    C compiler because it doesn't allow the use of -O with -g and
2117    because when it is *not* optimizing, it allocates a memory
2118    location for each floating-point variable, and the memory
2119    location is what gets described in the DWARF AT_location
2120    attribute for the variable in question.
2121    Regardless of the severe mental illness of the x86/svr4 SDB, we
2122    do something sensible here and we use the following DWARF
2123    register numbers.  Note that these are all stack-top-relative
2124    numbers.
2125         11 for %st(0) (gcc regno = 8)
2126         12 for %st(1) (gcc regno = 9)
2127         13 for %st(2) (gcc regno = 10)
2128         14 for %st(3) (gcc regno = 11)
2129         15 for %st(4) (gcc regno = 12)
2130         16 for %st(5) (gcc regno = 13)
2131         17 for %st(6) (gcc regno = 14)
2132         18 for %st(7) (gcc regno = 15)
2133 */
2134 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
2135 {
2136   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
2137   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
2138   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
2139   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
2140   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
2141   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
2142   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
2143 };
2144
2145 /* Define parameter passing and return registers.  */
2146
2147 static int const x86_64_int_parameter_registers[6] =
2148 {
2149   DI_REG, SI_REG, DX_REG, CX_REG, R8_REG, R9_REG
2150 };
2151
2152 static int const x86_64_ms_abi_int_parameter_registers[4] =
2153 {
2154   CX_REG, DX_REG, R8_REG, R9_REG
2155 };
2156
2157 static int const x86_64_int_return_registers[4] =
2158 {
2159   AX_REG, DX_REG, DI_REG, SI_REG
2160 };
2161
2162 /* Define the structure for the machine field in struct function.  */
2163
2164 struct GTY(()) stack_local_entry {
2165   unsigned short mode;
2166   unsigned short n;
2167   rtx rtl;
2168   struct stack_local_entry *next;
2169 };
2170
2171 /* Structure describing stack frame layout.
2172    Stack grows downward:
2173
2174    [arguments]
2175                                         <- ARG_POINTER
2176    saved pc
2177
2178    saved static chain                   if ix86_static_chain_on_stack
2179
2180    saved frame pointer                  if frame_pointer_needed
2181                                         <- HARD_FRAME_POINTER
2182    [saved regs]
2183                                         <- regs_save_offset
2184    [padding0]
2185
2186    [saved SSE regs]
2187                                         <- sse_regs_save_offset
2188    [padding1]          |
2189                        |                <- FRAME_POINTER
2190    [va_arg registers]  |
2191                        |
2192    [frame]             |
2193                        |
2194    [padding2]          | = to_allocate
2195                                         <- STACK_POINTER
2196   */
2197 struct ix86_frame
2198 {
2199   int nsseregs;
2200   int nregs;
2201   int va_arg_size;
2202   int red_zone_size;
2203   int outgoing_arguments_size;
2204   HOST_WIDE_INT frame;
2205
2206   /* The offsets relative to ARG_POINTER.  */
2207   HOST_WIDE_INT frame_pointer_offset;
2208   HOST_WIDE_INT hard_frame_pointer_offset;
2209   HOST_WIDE_INT stack_pointer_offset;
2210   HOST_WIDE_INT hfp_save_offset;
2211   HOST_WIDE_INT reg_save_offset;
2212   HOST_WIDE_INT sse_reg_save_offset;
2213
2214   /* When save_regs_using_mov is set, emit prologue using
2215      move instead of push instructions.  */
2216   bool save_regs_using_mov;
2217 };
2218
2219 /* Code model option.  */
2220 enum cmodel ix86_cmodel;
2221 /* Asm dialect.  */
2222 enum asm_dialect ix86_asm_dialect = ASM_ATT;
2223 /* TLS dialects.  */
2224 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
2225
2226 /* Which unit we are generating floating point math for.  */
2227 enum fpmath_unit ix86_fpmath;
2228
2229 /* Which cpu are we scheduling for.  */
2230 enum attr_cpu ix86_schedule;
2231
2232 /* Which cpu are we optimizing for.  */
2233 enum processor_type ix86_tune;
2234
2235 /* Which instruction set architecture to use.  */
2236 enum processor_type ix86_arch;
2237
2238 /* true if sse prefetch instruction is not NOOP.  */
2239 int x86_prefetch_sse;
2240
2241 /* ix86_regparm_string as a number */
2242 static int ix86_regparm;
2243
2244 /* -mstackrealign option */
2245 static const char ix86_force_align_arg_pointer_string[]
2246   = "force_align_arg_pointer";
2247
2248 static rtx (*ix86_gen_leave) (void);
2249 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
2250 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
2251 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
2252 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
2253 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
2254 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
2255 static rtx (*ix86_gen_allocate_stack_worker) (rtx, rtx);
2256 static rtx (*ix86_gen_adjust_stack_and_probe) (rtx, rtx, rtx);
2257 static rtx (*ix86_gen_probe_stack_range) (rtx, rtx, rtx);
2258
2259 /* Preferred alignment for stack boundary in bits.  */
2260 unsigned int ix86_preferred_stack_boundary;
2261
2262 /* Alignment for incoming stack boundary in bits specified at
2263    command line.  */
2264 static unsigned int ix86_user_incoming_stack_boundary;
2265
2266 /* Default alignment for incoming stack boundary in bits.  */
2267 static unsigned int ix86_default_incoming_stack_boundary;
2268
2269 /* Alignment for incoming stack boundary in bits.  */
2270 unsigned int ix86_incoming_stack_boundary;
2271
2272 /* The abi used by target.  */
2273 enum calling_abi ix86_abi;
2274
2275 /* Values 1-5: see jump.c */
2276 int ix86_branch_cost;
2277
2278 /* Calling abi specific va_list type nodes.  */
2279 static GTY(()) tree sysv_va_list_type_node;
2280 static GTY(()) tree ms_va_list_type_node;
2281
2282 /* Variables which are this size or smaller are put in the data/bss
2283    or ldata/lbss sections.  */
2284
2285 int ix86_section_threshold = 65536;
2286
2287 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
2288 char internal_label_prefix[16];
2289 int internal_label_prefix_len;
2290
2291 /* Fence to use after loop using movnt.  */
2292 tree x86_mfence;
2293
2294 /* Register class used for passing given 64bit part of the argument.
2295    These represent classes as documented by the PS ABI, with the exception
2296    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
2297    use SF or DFmode move instead of DImode to avoid reformatting penalties.
2298
2299    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
2300    whenever possible (upper half does contain padding).  */
2301 enum x86_64_reg_class
2302   {
2303     X86_64_NO_CLASS,
2304     X86_64_INTEGER_CLASS,
2305     X86_64_INTEGERSI_CLASS,
2306     X86_64_SSE_CLASS,
2307     X86_64_SSESF_CLASS,
2308     X86_64_SSEDF_CLASS,
2309     X86_64_SSEUP_CLASS,
2310     X86_64_X87_CLASS,
2311     X86_64_X87UP_CLASS,
2312     X86_64_COMPLEX_X87_CLASS,
2313     X86_64_MEMORY_CLASS
2314   };
2315
2316 #define MAX_CLASSES 4
2317
2318 /* Table of constants used by fldpi, fldln2, etc....  */
2319 static REAL_VALUE_TYPE ext_80387_constants_table [5];
2320 static bool ext_80387_constants_init = 0;
2321
2322 \f
2323 static struct machine_function * ix86_init_machine_status (void);
2324 static rtx ix86_function_value (const_tree, const_tree, bool);
2325 static bool ix86_function_value_regno_p (const unsigned int);
2326 static unsigned int ix86_function_arg_boundary (enum machine_mode,
2327                                                 const_tree);
2328 static rtx ix86_static_chain (const_tree, bool);
2329 static int ix86_function_regparm (const_tree, const_tree);
2330 static void ix86_compute_frame_layout (struct ix86_frame *);
2331 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
2332                                                  rtx, rtx, int);
2333 static void ix86_add_new_builtins (int);
2334 static rtx ix86_expand_vec_perm_builtin (tree);
2335 static tree ix86_canonical_va_list_type (tree);
2336 static void predict_jump (int);
2337 static unsigned int split_stack_prologue_scratch_regno (void);
2338 static bool i386_asm_output_addr_const_extra (FILE *, rtx);
2339
2340 enum ix86_function_specific_strings
2341 {
2342   IX86_FUNCTION_SPECIFIC_ARCH,
2343   IX86_FUNCTION_SPECIFIC_TUNE,
2344   IX86_FUNCTION_SPECIFIC_FPMATH,
2345   IX86_FUNCTION_SPECIFIC_MAX
2346 };
2347
2348 static char *ix86_target_string (int, int, const char *, const char *,
2349                                  const char *, bool);
2350 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
2351 static void ix86_function_specific_save (struct cl_target_option *);
2352 static void ix86_function_specific_restore (struct cl_target_option *);
2353 static void ix86_function_specific_print (FILE *, int,
2354                                           struct cl_target_option *);
2355 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
2356 static bool ix86_valid_target_attribute_inner_p (tree, char *[]);
2357 static bool ix86_can_inline_p (tree, tree);
2358 static void ix86_set_current_function (tree);
2359 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
2360
2361 static enum calling_abi ix86_function_abi (const_tree);
2362
2363 \f
2364 #ifndef SUBTARGET32_DEFAULT_CPU
2365 #define SUBTARGET32_DEFAULT_CPU "i386"
2366 #endif
2367
2368 /* The svr4 ABI for the i386 says that records and unions are returned
2369    in memory.  */
2370 #ifndef DEFAULT_PCC_STRUCT_RETURN
2371 #define DEFAULT_PCC_STRUCT_RETURN 1
2372 #endif
2373
2374 /* Whether -mtune= or -march= were specified */
2375 static int ix86_tune_defaulted;
2376 static int ix86_arch_specified;
2377
2378 /* A mask of ix86_isa_flags that includes bit X if X
2379    was set or cleared on the command line.  */
2380 static int ix86_isa_flags_explicit;
2381
2382 /* Define a set of ISAs which are available when a given ISA is
2383    enabled.  MMX and SSE ISAs are handled separately.  */
2384
2385 #define OPTION_MASK_ISA_MMX_SET OPTION_MASK_ISA_MMX
2386 #define OPTION_MASK_ISA_3DNOW_SET \
2387   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_MMX_SET)
2388
2389 #define OPTION_MASK_ISA_SSE_SET OPTION_MASK_ISA_SSE
2390 #define OPTION_MASK_ISA_SSE2_SET \
2391   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE_SET)
2392 #define OPTION_MASK_ISA_SSE3_SET \
2393   (OPTION_MASK_ISA_SSE3 | OPTION_MASK_ISA_SSE2_SET)
2394 #define OPTION_MASK_ISA_SSSE3_SET \
2395   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE3_SET)
2396 #define OPTION_MASK_ISA_SSE4_1_SET \
2397   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSSE3_SET)
2398 #define OPTION_MASK_ISA_SSE4_2_SET \
2399   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_SSE4_1_SET)
2400 #define OPTION_MASK_ISA_AVX_SET \
2401   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_SSE4_2_SET)
2402 #define OPTION_MASK_ISA_FMA_SET \
2403   (OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_AVX_SET)
2404
2405 /* SSE4 includes both SSE4.1 and SSE4.2. -msse4 should be the same
2406    as -msse4.2.  */
2407 #define OPTION_MASK_ISA_SSE4_SET OPTION_MASK_ISA_SSE4_2_SET
2408
2409 #define OPTION_MASK_ISA_SSE4A_SET \
2410   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE3_SET)
2411 #define OPTION_MASK_ISA_FMA4_SET \
2412   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_SSE4A_SET \
2413    | OPTION_MASK_ISA_AVX_SET)
2414 #define OPTION_MASK_ISA_XOP_SET \
2415   (OPTION_MASK_ISA_XOP | OPTION_MASK_ISA_FMA4_SET)
2416 #define OPTION_MASK_ISA_LWP_SET \
2417   OPTION_MASK_ISA_LWP
2418
2419 /* AES and PCLMUL need SSE2 because they use xmm registers */
2420 #define OPTION_MASK_ISA_AES_SET \
2421   (OPTION_MASK_ISA_AES | OPTION_MASK_ISA_SSE2_SET)
2422 #define OPTION_MASK_ISA_PCLMUL_SET \
2423   (OPTION_MASK_ISA_PCLMUL | OPTION_MASK_ISA_SSE2_SET)
2424
2425 #define OPTION_MASK_ISA_ABM_SET \
2426   (OPTION_MASK_ISA_ABM | OPTION_MASK_ISA_POPCNT)
2427
2428 #define OPTION_MASK_ISA_BMI_SET OPTION_MASK_ISA_BMI
2429 #define OPTION_MASK_ISA_TBM_SET OPTION_MASK_ISA_TBM
2430 #define OPTION_MASK_ISA_POPCNT_SET OPTION_MASK_ISA_POPCNT
2431 #define OPTION_MASK_ISA_CX16_SET OPTION_MASK_ISA_CX16
2432 #define OPTION_MASK_ISA_SAHF_SET OPTION_MASK_ISA_SAHF
2433 #define OPTION_MASK_ISA_MOVBE_SET OPTION_MASK_ISA_MOVBE
2434 #define OPTION_MASK_ISA_CRC32_SET OPTION_MASK_ISA_CRC32
2435
2436 #define OPTION_MASK_ISA_FSGSBASE_SET OPTION_MASK_ISA_FSGSBASE
2437 #define OPTION_MASK_ISA_RDRND_SET OPTION_MASK_ISA_RDRND
2438 #define OPTION_MASK_ISA_F16C_SET \
2439   (OPTION_MASK_ISA_F16C | OPTION_MASK_ISA_AVX_SET)
2440
2441 /* Define a set of ISAs which aren't available when a given ISA is
2442    disabled.  MMX and SSE ISAs are handled separately.  */
2443
2444 #define OPTION_MASK_ISA_MMX_UNSET \
2445   (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_3DNOW_UNSET)
2446 #define OPTION_MASK_ISA_3DNOW_UNSET \
2447   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_3DNOW_A_UNSET)
2448 #define OPTION_MASK_ISA_3DNOW_A_UNSET OPTION_MASK_ISA_3DNOW_A
2449
2450 #define OPTION_MASK_ISA_SSE_UNSET \
2451   (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_SSE2_UNSET)
2452 #define OPTION_MASK_ISA_SSE2_UNSET \
2453   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3_UNSET)
2454 #define OPTION_MASK_ISA_SSE3_UNSET \
2455   (OPTION_MASK_ISA_SSE3 \
2456    | OPTION_MASK_ISA_SSSE3_UNSET \
2457    | OPTION_MASK_ISA_SSE4A_UNSET )
2458 #define OPTION_MASK_ISA_SSSE3_UNSET \
2459   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE4_1_UNSET)
2460 #define OPTION_MASK_ISA_SSE4_1_UNSET \
2461   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSE4_2_UNSET)
2462 #define OPTION_MASK_ISA_SSE4_2_UNSET \
2463   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_AVX_UNSET )
2464 #define OPTION_MASK_ISA_AVX_UNSET \
2465   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_FMA_UNSET \
2466    | OPTION_MASK_ISA_FMA4_UNSET | OPTION_MASK_ISA_F16C_UNSET)
2467 #define OPTION_MASK_ISA_FMA_UNSET OPTION_MASK_ISA_FMA
2468
2469 /* SSE4 includes both SSE4.1 and SSE4.2.  -mno-sse4 should the same
2470    as -mno-sse4.1. */
2471 #define OPTION_MASK_ISA_SSE4_UNSET OPTION_MASK_ISA_SSE4_1_UNSET
2472
2473 #define OPTION_MASK_ISA_SSE4A_UNSET \
2474   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_FMA4_UNSET)
2475
2476 #define OPTION_MASK_ISA_FMA4_UNSET \
2477   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_XOP_UNSET)
2478 #define OPTION_MASK_ISA_XOP_UNSET OPTION_MASK_ISA_XOP
2479 #define OPTION_MASK_ISA_LWP_UNSET OPTION_MASK_ISA_LWP
2480
2481 #define OPTION_MASK_ISA_AES_UNSET OPTION_MASK_ISA_AES
2482 #define OPTION_MASK_ISA_PCLMUL_UNSET OPTION_MASK_ISA_PCLMUL
2483 #define OPTION_MASK_ISA_ABM_UNSET OPTION_MASK_ISA_ABM
2484 #define OPTION_MASK_ISA_BMI_UNSET OPTION_MASK_ISA_BMI
2485 #define OPTION_MASK_ISA_TBM_UNSET OPTION_MASK_ISA_TBM
2486 #define OPTION_MASK_ISA_POPCNT_UNSET OPTION_MASK_ISA_POPCNT
2487 #define OPTION_MASK_ISA_CX16_UNSET OPTION_MASK_ISA_CX16
2488 #define OPTION_MASK_ISA_SAHF_UNSET OPTION_MASK_ISA_SAHF
2489 #define OPTION_MASK_ISA_MOVBE_UNSET OPTION_MASK_ISA_MOVBE
2490 #define OPTION_MASK_ISA_CRC32_UNSET OPTION_MASK_ISA_CRC32
2491
2492 #define OPTION_MASK_ISA_FSGSBASE_UNSET OPTION_MASK_ISA_FSGSBASE
2493 #define OPTION_MASK_ISA_RDRND_UNSET OPTION_MASK_ISA_RDRND
2494 #define OPTION_MASK_ISA_F16C_UNSET OPTION_MASK_ISA_F16C
2495
2496 /* Vectorization library interface and handlers.  */
2497 static tree (*ix86_veclib_handler) (enum built_in_function, tree, tree);
2498
2499 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2500 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2501
2502 /* Processor target table, indexed by processor number */
2503 struct ptt
2504 {
2505   const struct processor_costs *cost;           /* Processor costs */
2506   const int align_loop;                         /* Default alignments.  */
2507   const int align_loop_max_skip;
2508   const int align_jump;
2509   const int align_jump_max_skip;
2510   const int align_func;
2511 };
2512
2513 static const struct ptt processor_target_table[PROCESSOR_max] =
2514 {
2515   {&i386_cost, 4, 3, 4, 3, 4},
2516   {&i486_cost, 16, 15, 16, 15, 16},
2517   {&pentium_cost, 16, 7, 16, 7, 16},
2518   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2519   {&geode_cost, 0, 0, 0, 0, 0},
2520   {&k6_cost, 32, 7, 32, 7, 32},
2521   {&athlon_cost, 16, 7, 16, 7, 16},
2522   {&pentium4_cost, 0, 0, 0, 0, 0},
2523   {&k8_cost, 16, 7, 16, 7, 16},
2524   {&nocona_cost, 0, 0, 0, 0, 0},
2525   /* Core 2 32-bit.  */
2526   {&generic32_cost, 16, 10, 16, 10, 16},
2527   /* Core 2 64-bit.  */
2528   {&generic64_cost, 16, 10, 16, 10, 16},
2529   /* Core i7 32-bit.  */
2530   {&generic32_cost, 16, 10, 16, 10, 16},
2531   /* Core i7 64-bit.  */
2532   {&generic64_cost, 16, 10, 16, 10, 16},
2533   {&generic32_cost, 16, 7, 16, 7, 16},
2534   {&generic64_cost, 16, 10, 16, 10, 16},
2535   {&amdfam10_cost, 32, 24, 32, 7, 32},
2536   {&bdver1_cost, 32, 24, 32, 7, 32},
2537   {&btver1_cost, 32, 24, 32, 7, 32},
2538   {&atom_cost, 16, 7, 16, 7, 16}
2539 };
2540
2541 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2542 {
2543   "generic",
2544   "i386",
2545   "i486",
2546   "pentium",
2547   "pentium-mmx",
2548   "pentiumpro",
2549   "pentium2",
2550   "pentium3",
2551   "pentium4",
2552   "pentium-m",
2553   "prescott",
2554   "nocona",
2555   "core2",
2556   "corei7",
2557   "atom",
2558   "geode",
2559   "k6",
2560   "k6-2",
2561   "k6-3",
2562   "athlon",
2563   "athlon-4",
2564   "k8",
2565   "amdfam10",
2566   "bdver1",
2567   "btver1"
2568 };
2569 \f
2570 /* Return true if a red-zone is in use.  */
2571
2572 static inline bool
2573 ix86_using_red_zone (void)
2574 {
2575   return TARGET_RED_ZONE && !TARGET_64BIT_MS_ABI;
2576 }
2577
2578 /* Implement TARGET_HANDLE_OPTION.  */
2579
2580 static bool
2581 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
2582 {
2583   switch (code)
2584     {
2585     case OPT_mmmx:
2586       if (value)
2587         {
2588           ix86_isa_flags |= OPTION_MASK_ISA_MMX_SET;
2589           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_SET;
2590         }
2591       else
2592         {
2593           ix86_isa_flags &= ~OPTION_MASK_ISA_MMX_UNSET;
2594           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_UNSET;
2595         }
2596       return true;
2597
2598     case OPT_m3dnow:
2599       if (value)
2600         {
2601           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_SET;
2602           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_SET;
2603         }
2604       else
2605         {
2606           ix86_isa_flags &= ~OPTION_MASK_ISA_3DNOW_UNSET;
2607           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_UNSET;
2608         }
2609       return true;
2610
2611     case OPT_m3dnowa:
2612       return false;
2613
2614     case OPT_msse:
2615       if (value)
2616         {
2617           ix86_isa_flags |= OPTION_MASK_ISA_SSE_SET;
2618           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_SET;
2619         }
2620       else
2621         {
2622           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE_UNSET;
2623           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_UNSET;
2624         }
2625       return true;
2626
2627     case OPT_msse2:
2628       if (value)
2629         {
2630           ix86_isa_flags |= OPTION_MASK_ISA_SSE2_SET;
2631           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_SET;
2632         }
2633       else
2634         {
2635           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE2_UNSET;
2636           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_UNSET;
2637         }
2638       return true;
2639
2640     case OPT_msse3:
2641       if (value)
2642         {
2643           ix86_isa_flags |= OPTION_MASK_ISA_SSE3_SET;
2644           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_SET;
2645         }
2646       else
2647         {
2648           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE3_UNSET;
2649           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_UNSET;
2650         }
2651       return true;
2652
2653     case OPT_mssse3:
2654       if (value)
2655         {
2656           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3_SET;
2657           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_SET;
2658         }
2659       else
2660         {
2661           ix86_isa_flags &= ~OPTION_MASK_ISA_SSSE3_UNSET;
2662           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_UNSET;
2663         }
2664       return true;
2665
2666     case OPT_msse4_1:
2667       if (value)
2668         {
2669           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1_SET;
2670           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_SET;
2671         }
2672       else
2673         {
2674           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_1_UNSET;
2675           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_UNSET;
2676         }
2677       return true;
2678
2679     case OPT_msse4_2:
2680       if (value)
2681         {
2682           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2_SET;
2683           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_SET;
2684         }
2685       else
2686         {
2687           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_2_UNSET;
2688           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_UNSET;
2689         }
2690       return true;
2691
2692     case OPT_mavx:
2693       if (value)
2694         {
2695           ix86_isa_flags |= OPTION_MASK_ISA_AVX_SET;
2696           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_SET;
2697         }
2698       else
2699         {
2700           ix86_isa_flags &= ~OPTION_MASK_ISA_AVX_UNSET;
2701           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_UNSET;
2702         }
2703       return true;
2704
2705     case OPT_mfma:
2706       if (value)
2707         {
2708           ix86_isa_flags |= OPTION_MASK_ISA_FMA_SET;
2709           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_SET;
2710         }
2711       else
2712         {
2713           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA_UNSET;
2714           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_UNSET;
2715         }
2716       return true;
2717
2718     case OPT_msse4:
2719       ix86_isa_flags |= OPTION_MASK_ISA_SSE4_SET;
2720       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_SET;
2721       return true;
2722
2723     case OPT_mno_sse4:
2724       ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_UNSET;
2725       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_UNSET;
2726       return true;
2727
2728     case OPT_msse4a:
2729       if (value)
2730         {
2731           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A_SET;
2732           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_SET;
2733         }
2734       else
2735         {
2736           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4A_UNSET;
2737           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_UNSET;
2738         }
2739       return true;
2740
2741     case OPT_mfma4:
2742       if (value)
2743         {
2744           ix86_isa_flags |= OPTION_MASK_ISA_FMA4_SET;
2745           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_SET;
2746         }
2747       else
2748         {
2749           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA4_UNSET;
2750           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_UNSET;
2751         }
2752       return true;
2753
2754    case OPT_mxop:
2755       if (value)
2756         {
2757           ix86_isa_flags |= OPTION_MASK_ISA_XOP_SET;
2758           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_SET;
2759         }
2760       else
2761         {
2762           ix86_isa_flags &= ~OPTION_MASK_ISA_XOP_UNSET;
2763           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_UNSET;
2764         }
2765       return true;
2766
2767    case OPT_mlwp:
2768       if (value)
2769         {
2770           ix86_isa_flags |= OPTION_MASK_ISA_LWP_SET;
2771           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_SET;
2772         }
2773       else
2774         {
2775           ix86_isa_flags &= ~OPTION_MASK_ISA_LWP_UNSET;
2776           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_UNSET;
2777         }
2778       return true;
2779
2780     case OPT_mabm:
2781       if (value)
2782         {
2783           ix86_isa_flags |= OPTION_MASK_ISA_ABM_SET;
2784           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_SET;
2785         }
2786       else
2787         {
2788           ix86_isa_flags &= ~OPTION_MASK_ISA_ABM_UNSET;
2789           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_UNSET;
2790         }
2791       return true;
2792
2793     case OPT_mbmi:
2794       if (value)
2795         {
2796           ix86_isa_flags |= OPTION_MASK_ISA_BMI_SET;
2797           ix86_isa_flags_explicit |= OPTION_MASK_ISA_BMI_SET;
2798         }
2799       else
2800         {
2801           ix86_isa_flags &= ~OPTION_MASK_ISA_BMI_UNSET;
2802           ix86_isa_flags_explicit |= OPTION_MASK_ISA_BMI_UNSET;
2803         }
2804       return true;
2805
2806     case OPT_mtbm:
2807       if (value)
2808         {
2809           ix86_isa_flags |= OPTION_MASK_ISA_TBM_SET;
2810           ix86_isa_flags_explicit |= OPTION_MASK_ISA_TBM_SET;
2811         }
2812       else
2813         {
2814           ix86_isa_flags &= ~OPTION_MASK_ISA_TBM_UNSET;
2815           ix86_isa_flags_explicit |= OPTION_MASK_ISA_TBM_UNSET;
2816         }
2817       return true;
2818
2819     case OPT_mpopcnt:
2820       if (value)
2821         {
2822           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT_SET;
2823           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_SET;
2824         }
2825       else
2826         {
2827           ix86_isa_flags &= ~OPTION_MASK_ISA_POPCNT_UNSET;
2828           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_UNSET;
2829         }
2830       return true;
2831
2832     case OPT_msahf:
2833       if (value)
2834         {
2835           ix86_isa_flags |= OPTION_MASK_ISA_SAHF_SET;
2836           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_SET;
2837         }
2838       else
2839         {
2840           ix86_isa_flags &= ~OPTION_MASK_ISA_SAHF_UNSET;
2841           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_UNSET;
2842         }
2843       return true;
2844
2845     case OPT_mcx16:
2846       if (value)
2847         {
2848           ix86_isa_flags |= OPTION_MASK_ISA_CX16_SET;
2849           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_SET;
2850         }
2851       else
2852         {
2853           ix86_isa_flags &= ~OPTION_MASK_ISA_CX16_UNSET;
2854           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_UNSET;
2855         }
2856       return true;
2857
2858     case OPT_mmovbe:
2859       if (value)
2860         {
2861           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE_SET;
2862           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_SET;
2863         }
2864       else
2865         {
2866           ix86_isa_flags &= ~OPTION_MASK_ISA_MOVBE_UNSET;
2867           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_UNSET;
2868         }
2869       return true;
2870
2871     case OPT_mcrc32:
2872       if (value)
2873         {
2874           ix86_isa_flags |= OPTION_MASK_ISA_CRC32_SET;
2875           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_SET;
2876         }
2877       else
2878         {
2879           ix86_isa_flags &= ~OPTION_MASK_ISA_CRC32_UNSET;
2880           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_UNSET;
2881         }
2882       return true;
2883
2884     case OPT_maes:
2885       if (value)
2886         {
2887           ix86_isa_flags |= OPTION_MASK_ISA_AES_SET;
2888           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_SET;
2889         }
2890       else
2891         {
2892           ix86_isa_flags &= ~OPTION_MASK_ISA_AES_UNSET;
2893           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_UNSET;
2894         }
2895       return true;
2896
2897     case OPT_mpclmul:
2898       if (value)
2899         {
2900           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL_SET;
2901           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_SET;
2902         }
2903       else
2904         {
2905           ix86_isa_flags &= ~OPTION_MASK_ISA_PCLMUL_UNSET;
2906           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_UNSET;
2907         }
2908       return true;
2909
2910     case OPT_mfsgsbase:
2911       if (value)
2912         {
2913           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE_SET;
2914           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_SET;
2915         }
2916       else
2917         {
2918           ix86_isa_flags &= ~OPTION_MASK_ISA_FSGSBASE_UNSET;
2919           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_UNSET;
2920         }
2921       return true;
2922
2923     case OPT_mrdrnd:
2924       if (value)
2925         {
2926           ix86_isa_flags |= OPTION_MASK_ISA_RDRND_SET;
2927           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_SET;
2928         }
2929       else
2930         {
2931           ix86_isa_flags &= ~OPTION_MASK_ISA_RDRND_UNSET;
2932           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_UNSET;
2933         }
2934       return true;
2935
2936     case OPT_mf16c:
2937       if (value)
2938         {
2939           ix86_isa_flags |= OPTION_MASK_ISA_F16C_SET;
2940           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_SET;
2941         }
2942       else
2943         {
2944           ix86_isa_flags &= ~OPTION_MASK_ISA_F16C_UNSET;
2945           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_UNSET;
2946         }
2947       return true;
2948
2949     default:
2950       return true;
2951     }
2952 }
2953 \f
2954 /* Return a string that documents the current -m options.  The caller is
2955    responsible for freeing the string.  */
2956
2957 static char *
2958 ix86_target_string (int isa, int flags, const char *arch, const char *tune,
2959                     const char *fpmath, bool add_nl_p)
2960 {
2961   struct ix86_target_opts
2962   {
2963     const char *option;         /* option string */
2964     int mask;                   /* isa mask options */
2965   };
2966
2967   /* This table is ordered so that options like -msse4.2 that imply
2968      preceding options while match those first.  */
2969   static struct ix86_target_opts isa_opts[] =
2970   {
2971     { "-m64",           OPTION_MASK_ISA_64BIT },
2972     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2973     { "-mfma",          OPTION_MASK_ISA_FMA },
2974     { "-mxop",          OPTION_MASK_ISA_XOP },
2975     { "-mlwp",          OPTION_MASK_ISA_LWP },
2976     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2977     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2978     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2979     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2980     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2981     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2982     { "-msse",          OPTION_MASK_ISA_SSE },
2983     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2984     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2985     { "-mmmx",          OPTION_MASK_ISA_MMX },
2986     { "-mabm",          OPTION_MASK_ISA_ABM },
2987     { "-mbmi",          OPTION_MASK_ISA_BMI },
2988     { "-mtbm",          OPTION_MASK_ISA_TBM },
2989     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2990     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2991     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2992     { "-maes",          OPTION_MASK_ISA_AES },
2993     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2994     { "-mfsgsbase",     OPTION_MASK_ISA_FSGSBASE },
2995     { "-mrdrnd",        OPTION_MASK_ISA_RDRND },
2996     { "-mf16c",         OPTION_MASK_ISA_F16C },
2997   };
2998
2999   /* Flag options.  */
3000   static struct ix86_target_opts flag_opts[] =
3001   {
3002     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
3003     { "-m80387",                        MASK_80387 },
3004     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
3005     { "-malign-double",                 MASK_ALIGN_DOUBLE },
3006     { "-mcld",                          MASK_CLD },
3007     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
3008     { "-mieee-fp",                      MASK_IEEE_FP },
3009     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
3010     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
3011     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
3012     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
3013     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
3014     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
3015     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
3016     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
3017     { "-mrecip",                        MASK_RECIP },
3018     { "-mrtd",                          MASK_RTD },
3019     { "-msseregparm",                   MASK_SSEREGPARM },
3020     { "-mstack-arg-probe",              MASK_STACK_PROBE },
3021     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
3022     { "-mvect8-ret-in-mem",             MASK_VECT8_RETURNS },
3023     { "-m8bit-idiv",                    MASK_USE_8BIT_IDIV },
3024     { "-mvzeroupper",                   MASK_VZEROUPPER },
3025   };
3026
3027   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
3028
3029   char isa_other[40];
3030   char target_other[40];
3031   unsigned num = 0;
3032   unsigned i, j;
3033   char *ret;
3034   char *ptr;
3035   size_t len;
3036   size_t line_len;
3037   size_t sep_len;
3038
3039   memset (opts, '\0', sizeof (opts));
3040
3041   /* Add -march= option.  */
3042   if (arch)
3043     {
3044       opts[num][0] = "-march=";
3045       opts[num++][1] = arch;
3046     }
3047
3048   /* Add -mtune= option.  */
3049   if (tune)
3050     {
3051       opts[num][0] = "-mtune=";
3052       opts[num++][1] = tune;
3053     }
3054
3055   /* Pick out the options in isa options.  */
3056   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
3057     {
3058       if ((isa & isa_opts[i].mask) != 0)
3059         {
3060           opts[num++][0] = isa_opts[i].option;
3061           isa &= ~ isa_opts[i].mask;
3062         }
3063     }
3064
3065   if (isa && add_nl_p)
3066     {
3067       opts[num++][0] = isa_other;
3068       sprintf (isa_other, "(other isa: %#x)", isa);
3069     }
3070
3071   /* Add flag options.  */
3072   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
3073     {
3074       if ((flags & flag_opts[i].mask) != 0)
3075         {
3076           opts[num++][0] = flag_opts[i].option;
3077           flags &= ~ flag_opts[i].mask;
3078         }
3079     }
3080
3081   if (flags && add_nl_p)
3082     {
3083       opts[num++][0] = target_other;
3084       sprintf (target_other, "(other flags: %#x)", flags);
3085     }
3086
3087   /* Add -fpmath= option.  */
3088   if (fpmath)
3089     {
3090       opts[num][0] = "-mfpmath=";
3091       opts[num++][1] = fpmath;
3092     }
3093
3094   /* Any options?  */
3095   if (num == 0)
3096     return NULL;
3097
3098   gcc_assert (num < ARRAY_SIZE (opts));
3099
3100   /* Size the string.  */
3101   len = 0;
3102   sep_len = (add_nl_p) ? 3 : 1;
3103   for (i = 0; i < num; i++)
3104     {
3105       len += sep_len;
3106       for (j = 0; j < 2; j++)
3107         if (opts[i][j])
3108           len += strlen (opts[i][j]);
3109     }
3110
3111   /* Build the string.  */
3112   ret = ptr = (char *) xmalloc (len);
3113   line_len = 0;
3114
3115   for (i = 0; i < num; i++)
3116     {
3117       size_t len2[2];
3118
3119       for (j = 0; j < 2; j++)
3120         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
3121
3122       if (i != 0)
3123         {
3124           *ptr++ = ' ';
3125           line_len++;
3126
3127           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
3128             {
3129               *ptr++ = '\\';
3130               *ptr++ = '\n';
3131               line_len = 0;
3132             }
3133         }
3134
3135       for (j = 0; j < 2; j++)
3136         if (opts[i][j])
3137           {
3138             memcpy (ptr, opts[i][j], len2[j]);
3139             ptr += len2[j];
3140             line_len += len2[j];
3141           }
3142     }
3143
3144   *ptr = '\0';
3145   gcc_assert (ret + len >= ptr);
3146
3147   return ret;
3148 }
3149
3150 /* Return TRUE if software prefetching is beneficial for the
3151    given CPU. */
3152
3153 static bool
3154 software_prefetching_beneficial_p (void)
3155 {
3156   switch (ix86_tune)
3157     {
3158     case PROCESSOR_GEODE:
3159     case PROCESSOR_K6:
3160     case PROCESSOR_ATHLON:
3161     case PROCESSOR_K8:
3162     case PROCESSOR_AMDFAM10:
3163     case PROCESSOR_BTVER1:
3164       return true;
3165
3166     default:
3167       return false;
3168     }
3169 }
3170
3171 /* Return true, if profiling code should be emitted before
3172    prologue. Otherwise it returns false.
3173    Note: For x86 with "hotfix" it is sorried.  */
3174 static bool
3175 ix86_profile_before_prologue (void)
3176 {
3177   return flag_fentry != 0;
3178 }
3179
3180 /* Function that is callable from the debugger to print the current
3181    options.  */
3182 void
3183 ix86_debug_options (void)
3184 {
3185   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
3186                                    ix86_arch_string, ix86_tune_string,
3187                                    ix86_fpmath_string, true);
3188
3189   if (opts)
3190     {
3191       fprintf (stderr, "%s\n\n", opts);
3192       free (opts);
3193     }
3194   else
3195     fputs ("<no options>\n\n", stderr);
3196
3197   return;
3198 }
3199 \f
3200 /* Override various settings based on options.  If MAIN_ARGS_P, the
3201    options are from the command line, otherwise they are from
3202    attributes.  */
3203
3204 static void
3205 ix86_option_override_internal (bool main_args_p)
3206 {
3207   int i;
3208   unsigned int ix86_arch_mask, ix86_tune_mask;
3209   const bool ix86_tune_specified = (ix86_tune_string != NULL);
3210   const char *prefix;
3211   const char *suffix;
3212   const char *sw;
3213
3214   /* Comes from final.c -- no real reason to change it.  */
3215 #define MAX_CODE_ALIGN 16
3216
3217   enum pta_flags
3218     {
3219       PTA_SSE = 1 << 0,
3220       PTA_SSE2 = 1 << 1,
3221       PTA_SSE3 = 1 << 2,
3222       PTA_MMX = 1 << 3,
3223       PTA_PREFETCH_SSE = 1 << 4,
3224       PTA_3DNOW = 1 << 5,
3225       PTA_3DNOW_A = 1 << 6,
3226       PTA_64BIT = 1 << 7,
3227       PTA_SSSE3 = 1 << 8,
3228       PTA_CX16 = 1 << 9,
3229       PTA_POPCNT = 1 << 10,
3230       PTA_ABM = 1 << 11,
3231       PTA_SSE4A = 1 << 12,
3232       PTA_NO_SAHF = 1 << 13,
3233       PTA_SSE4_1 = 1 << 14,
3234       PTA_SSE4_2 = 1 << 15,
3235       PTA_AES = 1 << 16,
3236       PTA_PCLMUL = 1 << 17,
3237       PTA_AVX = 1 << 18,
3238       PTA_FMA = 1 << 19,
3239       PTA_MOVBE = 1 << 20,
3240       PTA_FMA4 = 1 << 21,
3241       PTA_XOP = 1 << 22,
3242       PTA_LWP = 1 << 23,
3243       PTA_FSGSBASE = 1 << 24,
3244       PTA_RDRND = 1 << 25,
3245       PTA_F16C = 1 << 26,
3246       PTA_BMI = 1 << 27,
3247       PTA_TBM = 1 << 28
3248       /* if this reaches 32, need to widen struct pta flags below */
3249     };
3250
3251   static struct pta
3252     {
3253       const char *const name;           /* processor name or nickname.  */
3254       const enum processor_type processor;
3255       const enum attr_cpu schedule;
3256       const unsigned /*enum pta_flags*/ flags;
3257     }
3258   const processor_alias_table[] =
3259     {
3260       {"i386", PROCESSOR_I386, CPU_NONE, 0},
3261       {"i486", PROCESSOR_I486, CPU_NONE, 0},
3262       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
3263       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
3264       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
3265       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
3266       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
3267       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
3268       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
3269       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
3270       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
3271       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
3272       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
3273         PTA_MMX | PTA_SSE},
3274       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
3275         PTA_MMX | PTA_SSE},
3276       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
3277         PTA_MMX | PTA_SSE | PTA_SSE2},
3278       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
3279         PTA_MMX |PTA_SSE | PTA_SSE2},
3280       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
3281         PTA_MMX | PTA_SSE | PTA_SSE2},
3282       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
3283         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
3284       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
3285         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3286         | PTA_CX16 | PTA_NO_SAHF},
3287       {"core2", PROCESSOR_CORE2_64, CPU_CORE2,
3288         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3289         | PTA_SSSE3 | PTA_CX16},
3290       {"corei7", PROCESSOR_COREI7_64, CPU_COREI7,
3291         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3292         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_CX16},
3293       {"corei7-avx", PROCESSOR_COREI7_64, CPU_COREI7,
3294         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3295         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AVX
3296         | PTA_CX16 | PTA_POPCNT | PTA_AES | PTA_PCLMUL},
3297       {"atom", PROCESSOR_ATOM, CPU_ATOM,
3298         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3299         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
3300       {"geode", PROCESSOR_GEODE, CPU_GEODE,
3301         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A |PTA_PREFETCH_SSE},
3302       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
3303       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3304       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3305       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
3306         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3307       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
3308         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3309       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
3310         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3311       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
3312         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3313       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
3314         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3315       {"x86-64", PROCESSOR_K8, CPU_K8,
3316         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
3317       {"k8", PROCESSOR_K8, CPU_K8,
3318         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3319         | PTA_SSE2 | PTA_NO_SAHF},
3320       {"k8-sse3", PROCESSOR_K8, CPU_K8,
3321         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3322         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3323       {"opteron", PROCESSOR_K8, CPU_K8,
3324         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3325         | PTA_SSE2 | PTA_NO_SAHF},
3326       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
3327         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3328         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3329       {"athlon64", PROCESSOR_K8, CPU_K8,
3330         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3331         | PTA_SSE2 | PTA_NO_SAHF},
3332       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
3333         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3334         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3335       {"athlon-fx", PROCESSOR_K8, CPU_K8,
3336         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3337         | PTA_SSE2 | PTA_NO_SAHF},
3338       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3339         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3340         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3341       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3342         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3343         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3344       {"bdver1", PROCESSOR_BDVER1, CPU_BDVER1,
3345         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3346         | PTA_SSE4A | PTA_CX16 | PTA_ABM | PTA_SSSE3 | PTA_SSE4_1
3347         | PTA_SSE4_2 | PTA_AES | PTA_PCLMUL | PTA_AVX | PTA_FMA4
3348         | PTA_XOP | PTA_LWP},
3349       {"btver1", PROCESSOR_BTVER1, CPU_GENERIC64,
3350         PTA_64BIT | PTA_MMX |  PTA_SSE  | PTA_SSE2 | PTA_SSE3
3351         | PTA_SSSE3 | PTA_SSE4A |PTA_ABM | PTA_CX16},
3352       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
3353         0 /* flags are only used for -march switch.  */ },
3354       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
3355         PTA_64BIT /* flags are only used for -march switch.  */ },
3356     };
3357
3358   int const pta_size = ARRAY_SIZE (processor_alias_table);
3359
3360   /* Set up prefix/suffix so the error messages refer to either the command
3361      line argument, or the attribute(target).  */
3362   if (main_args_p)
3363     {
3364       prefix = "-m";
3365       suffix = "";
3366       sw = "switch";
3367     }
3368   else
3369     {
3370       prefix = "option(\"";
3371       suffix = "\")";
3372       sw = "attribute";
3373     }
3374
3375 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3376   SUBTARGET_OVERRIDE_OPTIONS;
3377 #endif
3378
3379 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3380   SUBSUBTARGET_OVERRIDE_OPTIONS;
3381 #endif
3382
3383   /* -fPIC is the default for x86_64.  */
3384   if (TARGET_MACHO && TARGET_64BIT)
3385     flag_pic = 2;
3386
3387   /* Need to check -mtune=generic first.  */
3388   if (ix86_tune_string)
3389     {
3390       if (!strcmp (ix86_tune_string, "generic")
3391           || !strcmp (ix86_tune_string, "i686")
3392           /* As special support for cross compilers we read -mtune=native
3393              as -mtune=generic.  With native compilers we won't see the
3394              -mtune=native, as it was changed by the driver.  */
3395           || !strcmp (ix86_tune_string, "native"))
3396         {
3397           if (TARGET_64BIT)
3398             ix86_tune_string = "generic64";
3399           else
3400             ix86_tune_string = "generic32";
3401         }
3402       /* If this call is for setting the option attribute, allow the
3403          generic32/generic64 that was previously set.  */
3404       else if (!main_args_p
3405                && (!strcmp (ix86_tune_string, "generic32")
3406                    || !strcmp (ix86_tune_string, "generic64")))
3407         ;
3408       else if (!strncmp (ix86_tune_string, "generic", 7))
3409         error ("bad value (%s) for %stune=%s %s",
3410                ix86_tune_string, prefix, suffix, sw);
3411       else if (!strcmp (ix86_tune_string, "x86-64"))
3412         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated; use "
3413                  "%stune=k8%s or %stune=generic%s instead as appropriate",
3414                  prefix, suffix, prefix, suffix, prefix, suffix);
3415     }
3416   else
3417     {
3418       if (ix86_arch_string)
3419         ix86_tune_string = ix86_arch_string;
3420       if (!ix86_tune_string)
3421         {
3422           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
3423           ix86_tune_defaulted = 1;
3424         }
3425
3426       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
3427          need to use a sensible tune option.  */
3428       if (!strcmp (ix86_tune_string, "generic")
3429           || !strcmp (ix86_tune_string, "x86-64")
3430           || !strcmp (ix86_tune_string, "i686"))
3431         {
3432           if (TARGET_64BIT)
3433             ix86_tune_string = "generic64";
3434           else
3435             ix86_tune_string = "generic32";
3436         }
3437     }
3438
3439   if (ix86_stringop_string)
3440     {
3441       if (!strcmp (ix86_stringop_string, "rep_byte"))
3442         stringop_alg = rep_prefix_1_byte;
3443       else if (!strcmp (ix86_stringop_string, "libcall"))
3444         stringop_alg = libcall;
3445       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
3446         stringop_alg = rep_prefix_4_byte;
3447       else if (!strcmp (ix86_stringop_string, "rep_8byte")
3448                && TARGET_64BIT)
3449         /* rep; movq isn't available in 32-bit code.  */
3450         stringop_alg = rep_prefix_8_byte;
3451       else if (!strcmp (ix86_stringop_string, "byte_loop"))
3452         stringop_alg = loop_1_byte;
3453       else if (!strcmp (ix86_stringop_string, "loop"))
3454         stringop_alg = loop;
3455       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
3456         stringop_alg = unrolled_loop;
3457       else
3458         error ("bad value (%s) for %sstringop-strategy=%s %s",
3459                ix86_stringop_string, prefix, suffix, sw);
3460     }
3461
3462   if (!ix86_arch_string)
3463     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
3464   else
3465     ix86_arch_specified = 1;
3466
3467   /* Validate -mabi= value.  */
3468   if (ix86_abi_string)
3469     {
3470       if (strcmp (ix86_abi_string, "sysv") == 0)
3471         ix86_abi = SYSV_ABI;
3472       else if (strcmp (ix86_abi_string, "ms") == 0)
3473         ix86_abi = MS_ABI;
3474       else
3475         error ("unknown ABI (%s) for %sabi=%s %s",
3476                ix86_abi_string, prefix, suffix, sw);
3477     }
3478   else
3479     ix86_abi = DEFAULT_ABI;
3480
3481   if (ix86_cmodel_string != 0)
3482     {
3483       if (!strcmp (ix86_cmodel_string, "small"))
3484         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3485       else if (!strcmp (ix86_cmodel_string, "medium"))
3486         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
3487       else if (!strcmp (ix86_cmodel_string, "large"))
3488         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
3489       else if (flag_pic)
3490         error ("code model %s does not support PIC mode", ix86_cmodel_string);
3491       else if (!strcmp (ix86_cmodel_string, "32"))
3492         ix86_cmodel = CM_32;
3493       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
3494         ix86_cmodel = CM_KERNEL;
3495       else
3496         error ("bad value (%s) for %scmodel=%s %s",
3497                ix86_cmodel_string, prefix, suffix, sw);
3498     }
3499   else
3500     {
3501       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
3502          use of rip-relative addressing.  This eliminates fixups that
3503          would otherwise be needed if this object is to be placed in a
3504          DLL, and is essentially just as efficient as direct addressing.  */
3505       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
3506         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
3507       else if (TARGET_64BIT)
3508         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3509       else
3510         ix86_cmodel = CM_32;
3511     }
3512   if (ix86_asm_string != 0)
3513     {
3514       if (! TARGET_MACHO
3515           && !strcmp (ix86_asm_string, "intel"))
3516         ix86_asm_dialect = ASM_INTEL;
3517       else if (!strcmp (ix86_asm_string, "att"))
3518         ix86_asm_dialect = ASM_ATT;
3519       else
3520         error ("bad value (%s) for %sasm=%s %s",
3521                ix86_asm_string, prefix, suffix, sw);
3522     }
3523   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
3524     error ("code model %qs not supported in the %s bit mode",
3525            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
3526   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
3527     sorry ("%i-bit mode not compiled in",
3528            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
3529
3530   for (i = 0; i < pta_size; i++)
3531     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
3532       {
3533         ix86_schedule = processor_alias_table[i].schedule;
3534         ix86_arch = processor_alias_table[i].processor;
3535         /* Default cpu tuning to the architecture.  */
3536         ix86_tune = ix86_arch;
3537
3538         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3539           error ("CPU you selected does not support x86-64 "
3540                  "instruction set");
3541
3542         if (processor_alias_table[i].flags & PTA_MMX
3543             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
3544           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
3545         if (processor_alias_table[i].flags & PTA_3DNOW
3546             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
3547           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
3548         if (processor_alias_table[i].flags & PTA_3DNOW_A
3549             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
3550           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
3551         if (processor_alias_table[i].flags & PTA_SSE
3552             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
3553           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
3554         if (processor_alias_table[i].flags & PTA_SSE2
3555             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
3556           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
3557         if (processor_alias_table[i].flags & PTA_SSE3
3558             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
3559           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
3560         if (processor_alias_table[i].flags & PTA_SSSE3
3561             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
3562           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
3563         if (processor_alias_table[i].flags & PTA_SSE4_1
3564             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
3565           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
3566         if (processor_alias_table[i].flags & PTA_SSE4_2
3567             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
3568           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
3569         if (processor_alias_table[i].flags & PTA_AVX
3570             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
3571           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
3572         if (processor_alias_table[i].flags & PTA_FMA
3573             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
3574           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
3575         if (processor_alias_table[i].flags & PTA_SSE4A
3576             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3577           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3578         if (processor_alias_table[i].flags & PTA_FMA4
3579             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3580           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3581         if (processor_alias_table[i].flags & PTA_XOP
3582             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3583           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3584         if (processor_alias_table[i].flags & PTA_LWP
3585             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3586           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3587         if (processor_alias_table[i].flags & PTA_ABM
3588             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3589           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3590         if (processor_alias_table[i].flags & PTA_BMI
3591             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_BMI))
3592           ix86_isa_flags |= OPTION_MASK_ISA_BMI;
3593         if (processor_alias_table[i].flags & PTA_TBM
3594             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_TBM))
3595           ix86_isa_flags |= OPTION_MASK_ISA_TBM;
3596         if (processor_alias_table[i].flags & PTA_CX16
3597             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3598           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3599         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3600             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3601           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3602         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3603             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3604           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3605         if (processor_alias_table[i].flags & PTA_MOVBE
3606             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3607           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3608         if (processor_alias_table[i].flags & PTA_AES
3609             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3610           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3611         if (processor_alias_table[i].flags & PTA_PCLMUL
3612             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3613           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3614         if (processor_alias_table[i].flags & PTA_FSGSBASE
3615             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FSGSBASE))
3616           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE;
3617         if (processor_alias_table[i].flags & PTA_RDRND
3618             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_RDRND))
3619           ix86_isa_flags |= OPTION_MASK_ISA_RDRND;
3620         if (processor_alias_table[i].flags & PTA_F16C
3621             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_F16C))
3622           ix86_isa_flags |= OPTION_MASK_ISA_F16C;
3623         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3624           x86_prefetch_sse = true;
3625
3626         break;
3627       }
3628
3629   if (!strcmp (ix86_arch_string, "generic"))
3630     error ("generic CPU can be used only for %stune=%s %s",
3631            prefix, suffix, sw);
3632   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3633     error ("bad value (%s) for %sarch=%s %s",
3634            ix86_arch_string, prefix, suffix, sw);
3635
3636   ix86_arch_mask = 1u << ix86_arch;
3637   for (i = 0; i < X86_ARCH_LAST; ++i)
3638     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3639
3640   for (i = 0; i < pta_size; i++)
3641     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3642       {
3643         ix86_schedule = processor_alias_table[i].schedule;
3644         ix86_tune = processor_alias_table[i].processor;
3645         if (TARGET_64BIT)
3646           {
3647             if (!(processor_alias_table[i].flags & PTA_64BIT))
3648               {
3649                 if (ix86_tune_defaulted)
3650                   {
3651                     ix86_tune_string = "x86-64";
3652                     for (i = 0; i < pta_size; i++)
3653                       if (! strcmp (ix86_tune_string,
3654                                     processor_alias_table[i].name))
3655                         break;
3656                     ix86_schedule = processor_alias_table[i].schedule;
3657                     ix86_tune = processor_alias_table[i].processor;
3658                   }
3659                 else
3660                   error ("CPU you selected does not support x86-64 "
3661                          "instruction set");
3662               }
3663           }
3664         else
3665           {
3666             /* Adjust tuning when compiling for 32-bit ABI.  */
3667             switch (ix86_tune)
3668               {
3669               case PROCESSOR_GENERIC64:
3670                 ix86_tune = PROCESSOR_GENERIC32;
3671                 ix86_schedule = CPU_PENTIUMPRO;
3672                 break;
3673
3674               case PROCESSOR_CORE2_64:
3675                 ix86_tune = PROCESSOR_CORE2_32;
3676                 break;
3677
3678               case PROCESSOR_COREI7_64:
3679                 ix86_tune = PROCESSOR_COREI7_32;
3680                 break;
3681
3682               default:
3683                 break;
3684               }
3685           }
3686         /* Intel CPUs have always interpreted SSE prefetch instructions as
3687            NOPs; so, we can enable SSE prefetch instructions even when
3688            -mtune (rather than -march) points us to a processor that has them.
3689            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3690            higher processors.  */
3691         if (TARGET_CMOVE
3692             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3693           x86_prefetch_sse = true;
3694         break;
3695       }
3696
3697   if (ix86_tune_specified && i == pta_size)
3698     error ("bad value (%s) for %stune=%s %s",
3699            ix86_tune_string, prefix, suffix, sw);
3700
3701   ix86_tune_mask = 1u << ix86_tune;
3702   for (i = 0; i < X86_TUNE_LAST; ++i)
3703     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3704
3705 #ifndef USE_IX86_FRAME_POINTER
3706 #define USE_IX86_FRAME_POINTER 0
3707 #endif
3708
3709 #ifndef USE_X86_64_FRAME_POINTER
3710 #define USE_X86_64_FRAME_POINTER 0
3711 #endif
3712
3713   /* Set the default values for switches whose default depends on TARGET_64BIT
3714      in case they weren't overwritten by command line options.  */
3715   if (TARGET_64BIT)
3716     {
3717       if (optimize > 1 && !global_options_set.x_flag_zee)
3718         flag_zee = 1;
3719       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3720         flag_omit_frame_pointer = !USE_X86_64_FRAME_POINTER;
3721       if (flag_asynchronous_unwind_tables == 2)
3722         flag_unwind_tables = flag_asynchronous_unwind_tables = 1;
3723       if (flag_pcc_struct_return == 2)
3724         flag_pcc_struct_return = 0;
3725     }
3726   else
3727     {
3728       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3729         flag_omit_frame_pointer = !(USE_IX86_FRAME_POINTER || optimize_size);
3730       if (flag_asynchronous_unwind_tables == 2)
3731         flag_asynchronous_unwind_tables = !USE_IX86_FRAME_POINTER;
3732       if (flag_pcc_struct_return == 2)
3733         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
3734     }
3735
3736   if (optimize_size)
3737     ix86_cost = &ix86_size_cost;
3738   else
3739     ix86_cost = processor_target_table[ix86_tune].cost;
3740
3741   /* Arrange to set up i386_stack_locals for all functions.  */
3742   init_machine_status = ix86_init_machine_status;
3743
3744   /* Validate -mregparm= value.  */
3745   if (ix86_regparm_string)
3746     {
3747       if (TARGET_64BIT)
3748         warning (0, "%sregparm%s is ignored in 64-bit mode", prefix, suffix);
3749       i = atoi (ix86_regparm_string);
3750       if (i < 0 || i > REGPARM_MAX)
3751         error ("%sregparm=%d%s is not between 0 and %d",
3752                prefix, i, suffix, REGPARM_MAX);
3753       else
3754         ix86_regparm = i;
3755     }
3756   if (TARGET_64BIT)
3757     ix86_regparm = REGPARM_MAX;
3758
3759   /* If the user has provided any of the -malign-* options,
3760      warn and use that value only if -falign-* is not set.
3761      Remove this code in GCC 3.2 or later.  */
3762   if (ix86_align_loops_string)
3763     {
3764       warning (0, "%salign-loops%s is obsolete, use -falign-loops%s",
3765                prefix, suffix, suffix);
3766       if (align_loops == 0)
3767         {
3768           i = atoi (ix86_align_loops_string);
3769           if (i < 0 || i > MAX_CODE_ALIGN)
3770             error ("%salign-loops=%d%s is not between 0 and %d",
3771                    prefix, i, suffix, MAX_CODE_ALIGN);
3772           else
3773             align_loops = 1 << i;
3774         }
3775     }
3776
3777   if (ix86_align_jumps_string)
3778     {
3779       warning (0, "%salign-jumps%s is obsolete, use -falign-jumps%s",
3780                prefix, suffix, suffix);
3781       if (align_jumps == 0)
3782         {
3783           i = atoi (ix86_align_jumps_string);
3784           if (i < 0 || i > MAX_CODE_ALIGN)
3785             error ("%salign-loops=%d%s is not between 0 and %d",
3786                    prefix, i, suffix, MAX_CODE_ALIGN);
3787           else
3788             align_jumps = 1 << i;
3789         }
3790     }
3791
3792   if (ix86_align_funcs_string)
3793     {
3794       warning (0, "%salign-functions%s is obsolete, use -falign-functions%s",
3795                prefix, suffix, suffix);
3796       if (align_functions == 0)
3797         {
3798           i = atoi (ix86_align_funcs_string);
3799           if (i < 0 || i > MAX_CODE_ALIGN)
3800             error ("%salign-loops=%d%s is not between 0 and %d",
3801                    prefix, i, suffix, MAX_CODE_ALIGN);
3802           else
3803             align_functions = 1 << i;
3804         }
3805     }
3806
3807   /* Default align_* from the processor table.  */
3808   if (align_loops == 0)
3809     {
3810       align_loops = processor_target_table[ix86_tune].align_loop;
3811       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3812     }
3813   if (align_jumps == 0)
3814     {
3815       align_jumps = processor_target_table[ix86_tune].align_jump;
3816       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3817     }
3818   if (align_functions == 0)
3819     {
3820       align_functions = processor_target_table[ix86_tune].align_func;
3821     }
3822
3823   /* Validate -mbranch-cost= value, or provide default.  */
3824   ix86_branch_cost = ix86_cost->branch_cost;
3825   if (ix86_branch_cost_string)
3826     {
3827       i = atoi (ix86_branch_cost_string);
3828       if (i < 0 || i > 5)
3829         error ("%sbranch-cost=%d%s is not between 0 and 5", prefix, i, suffix);
3830       else
3831         ix86_branch_cost = i;
3832     }
3833   if (ix86_section_threshold_string)
3834     {
3835       i = atoi (ix86_section_threshold_string);
3836       if (i < 0)
3837         error ("%slarge-data-threshold=%d%s is negative", prefix, i, suffix);
3838       else
3839         ix86_section_threshold = i;
3840     }
3841
3842   if (ix86_tls_dialect_string)
3843     {
3844       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
3845         ix86_tls_dialect = TLS_DIALECT_GNU;
3846       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
3847         ix86_tls_dialect = TLS_DIALECT_GNU2;
3848       else
3849         error ("bad value (%s) for %stls-dialect=%s %s",
3850                ix86_tls_dialect_string, prefix, suffix, sw);
3851     }
3852
3853   if (ix87_precision_string)
3854     {
3855       i = atoi (ix87_precision_string);
3856       if (i != 32 && i != 64 && i != 80)
3857         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
3858     }
3859
3860   if (TARGET_64BIT)
3861     {
3862       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3863
3864       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3865          explicitly disable any of these.  In particular, disabling SSE and
3866          MMX for kernel code is extremely useful.  */
3867       if (!ix86_arch_specified)
3868       ix86_isa_flags
3869         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3870              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3871
3872       if (TARGET_RTD)
3873         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3874     }
3875   else
3876     {
3877       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3878
3879       if (!ix86_arch_specified)
3880       ix86_isa_flags
3881         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3882
3883       /* i386 ABI does not specify red zone.  It still makes sense to use it
3884          when programmer takes care to stack from being destroyed.  */
3885       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3886         target_flags |= MASK_NO_RED_ZONE;
3887     }
3888
3889   /* Keep nonleaf frame pointers.  */
3890   if (flag_omit_frame_pointer)
3891     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3892   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3893     flag_omit_frame_pointer = 1;
3894
3895   /* If we're doing fast math, we don't care about comparison order
3896      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3897   if (flag_finite_math_only)
3898     target_flags &= ~MASK_IEEE_FP;
3899
3900   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3901      since the insns won't need emulation.  */
3902   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3903     target_flags &= ~MASK_NO_FANCY_MATH_387;
3904
3905   /* Likewise, if the target doesn't have a 387, or we've specified
3906      software floating point, don't use 387 inline intrinsics.  */
3907   if (!TARGET_80387)
3908     target_flags |= MASK_NO_FANCY_MATH_387;
3909
3910   /* Turn on MMX builtins for -msse.  */
3911   if (TARGET_SSE)
3912     {
3913       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3914       x86_prefetch_sse = true;
3915     }
3916
3917   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3918   if (TARGET_SSE4_2 || TARGET_ABM)
3919     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3920
3921   /* Validate -mpreferred-stack-boundary= value or default it to
3922      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3923   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3924   if (ix86_preferred_stack_boundary_string)
3925     {
3926       int min = (TARGET_64BIT ? 4 : 2);
3927       int max = (TARGET_SEH ? 4 : 12);
3928
3929       i = atoi (ix86_preferred_stack_boundary_string);
3930       if (i < min || i > max)
3931         {
3932           if (min == max)
3933             error ("%spreferred-stack-boundary%s is not supported "
3934                    "for this target", prefix, suffix);
3935           else
3936             error ("%spreferred-stack-boundary=%d%s is not between %d and %d",
3937                    prefix, i, suffix, min, max);
3938         }
3939       else
3940         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
3941     }
3942
3943   /* Set the default value for -mstackrealign.  */
3944   if (ix86_force_align_arg_pointer == -1)
3945     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3946
3947   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3948
3949   /* Validate -mincoming-stack-boundary= value or default it to
3950      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3951   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3952   if (ix86_incoming_stack_boundary_string)
3953     {
3954       i = atoi (ix86_incoming_stack_boundary_string);
3955       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3956         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3957                i, TARGET_64BIT ? 4 : 2);
3958       else
3959         {
3960           ix86_user_incoming_stack_boundary = (1 << i) * BITS_PER_UNIT;
3961           ix86_incoming_stack_boundary
3962             = ix86_user_incoming_stack_boundary;
3963         }
3964     }
3965
3966   /* Accept -msseregparm only if at least SSE support is enabled.  */
3967   if (TARGET_SSEREGPARM
3968       && ! TARGET_SSE)
3969     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3970
3971   ix86_fpmath = TARGET_FPMATH_DEFAULT;
3972   if (ix86_fpmath_string != 0)
3973     {
3974       if (! strcmp (ix86_fpmath_string, "387"))
3975         ix86_fpmath = FPMATH_387;
3976       else if (! strcmp (ix86_fpmath_string, "sse"))
3977         {
3978           if (!TARGET_SSE)
3979             {
3980               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3981               ix86_fpmath = FPMATH_387;
3982             }
3983           else
3984             ix86_fpmath = FPMATH_SSE;
3985         }
3986       else if (! strcmp (ix86_fpmath_string, "387,sse")
3987                || ! strcmp (ix86_fpmath_string, "387+sse")
3988                || ! strcmp (ix86_fpmath_string, "sse,387")
3989                || ! strcmp (ix86_fpmath_string, "sse+387")
3990                || ! strcmp (ix86_fpmath_string, "both"))
3991         {
3992           if (!TARGET_SSE)
3993             {
3994               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3995               ix86_fpmath = FPMATH_387;
3996             }
3997           else if (!TARGET_80387)
3998             {
3999               warning (0, "387 instruction set disabled, using SSE arithmetics");
4000               ix86_fpmath = FPMATH_SSE;
4001             }
4002           else
4003             ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
4004         }
4005       else
4006         error ("bad value (%s) for %sfpmath=%s %s",
4007                ix86_fpmath_string, prefix, suffix, sw);
4008     }
4009
4010   /* If the i387 is disabled, then do not return values in it. */
4011   if (!TARGET_80387)
4012     target_flags &= ~MASK_FLOAT_RETURNS;
4013
4014   /* Use external vectorized library in vectorizing intrinsics.  */
4015   if (ix86_veclibabi_string)
4016     {
4017       if (strcmp (ix86_veclibabi_string, "svml") == 0)
4018         ix86_veclib_handler = ix86_veclibabi_svml;
4019       else if (strcmp (ix86_veclibabi_string, "acml") == 0)
4020         ix86_veclib_handler = ix86_veclibabi_acml;
4021       else
4022         error ("unknown vectorization library ABI type (%s) for "
4023                "%sveclibabi=%s %s", ix86_veclibabi_string,
4024                prefix, suffix, sw);
4025     }
4026
4027   if ((!USE_IX86_FRAME_POINTER
4028        || (x86_accumulate_outgoing_args & ix86_tune_mask))
4029       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
4030       && !optimize_size)
4031     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
4032
4033   /* ??? Unwind info is not correct around the CFG unless either a frame
4034      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
4035      unwind info generation to be aware of the CFG and propagating states
4036      around edges.  */
4037   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
4038        || flag_exceptions || flag_non_call_exceptions)
4039       && flag_omit_frame_pointer
4040       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
4041     {
4042       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
4043         warning (0, "unwind tables currently require either a frame pointer "
4044                  "or %saccumulate-outgoing-args%s for correctness",
4045                  prefix, suffix);
4046       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
4047     }
4048
4049   /* If stack probes are required, the space used for large function
4050      arguments on the stack must also be probed, so enable
4051      -maccumulate-outgoing-args so this happens in the prologue.  */
4052   if (TARGET_STACK_PROBE
4053       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
4054     {
4055       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
4056         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
4057                  "for correctness", prefix, suffix);
4058       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
4059     }
4060
4061   /* For sane SSE instruction set generation we need fcomi instruction.
4062      It is safe to enable all CMOVE instructions.  */
4063   if (TARGET_SSE)
4064     TARGET_CMOVE = 1;
4065
4066   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
4067   {
4068     char *p;
4069     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
4070     p = strchr (internal_label_prefix, 'X');
4071     internal_label_prefix_len = p - internal_label_prefix;
4072     *p = '\0';
4073   }
4074
4075   /* When scheduling description is not available, disable scheduler pass
4076      so it won't slow down the compilation and make x87 code slower.  */
4077   if (!TARGET_SCHEDULE)
4078     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
4079
4080   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
4081                          ix86_cost->simultaneous_prefetches,
4082                          global_options.x_param_values,
4083                          global_options_set.x_param_values);
4084   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, ix86_cost->prefetch_block,
4085                          global_options.x_param_values,
4086                          global_options_set.x_param_values);
4087   maybe_set_param_value (PARAM_L1_CACHE_SIZE, ix86_cost->l1_cache_size,
4088                          global_options.x_param_values,
4089                          global_options_set.x_param_values);
4090   maybe_set_param_value (PARAM_L2_CACHE_SIZE, ix86_cost->l2_cache_size,
4091                          global_options.x_param_values,
4092                          global_options_set.x_param_values);
4093
4094   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
4095   if (flag_prefetch_loop_arrays < 0
4096       && HAVE_prefetch
4097       && optimize >= 3
4098       && software_prefetching_beneficial_p ())
4099     flag_prefetch_loop_arrays = 1;
4100
4101   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
4102      can be optimized to ap = __builtin_next_arg (0).  */
4103   if (!TARGET_64BIT && !flag_split_stack)
4104     targetm.expand_builtin_va_start = NULL;
4105
4106   if (TARGET_64BIT)
4107     {
4108       ix86_gen_leave = gen_leave_rex64;
4109       ix86_gen_add3 = gen_adddi3;
4110       ix86_gen_sub3 = gen_subdi3;
4111       ix86_gen_sub3_carry = gen_subdi3_carry;
4112       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
4113       ix86_gen_monitor = gen_sse3_monitor64;
4114       ix86_gen_andsp = gen_anddi3;
4115       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_di;
4116       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probedi;
4117       ix86_gen_probe_stack_range = gen_probe_stack_rangedi;
4118     }
4119   else
4120     {
4121       ix86_gen_leave = gen_leave;
4122       ix86_gen_add3 = gen_addsi3;
4123       ix86_gen_sub3 = gen_subsi3;
4124       ix86_gen_sub3_carry = gen_subsi3_carry;
4125       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
4126       ix86_gen_monitor = gen_sse3_monitor;
4127       ix86_gen_andsp = gen_andsi3;
4128       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_si;
4129       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probesi;
4130       ix86_gen_probe_stack_range = gen_probe_stack_rangesi;
4131     }
4132
4133 #ifdef USE_IX86_CLD
4134   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
4135   if (!TARGET_64BIT)
4136     target_flags |= MASK_CLD & ~target_flags_explicit;
4137 #endif
4138
4139   if (!TARGET_64BIT && flag_pic)
4140     {
4141       if (flag_fentry > 0)
4142         sorry ("-mfentry isn%'t supported for 32-bit in combination "
4143                "with -fpic");
4144       flag_fentry = 0;
4145     }
4146   else if (TARGET_SEH)
4147     {
4148       if (flag_fentry == 0)
4149         sorry ("-mno-fentry isn%'t compatible with SEH");
4150       flag_fentry = 1;
4151     }
4152   else if (flag_fentry < 0)
4153    {
4154 #if defined(PROFILE_BEFORE_PROLOGUE)
4155      flag_fentry = 1;
4156 #else
4157      flag_fentry = 0;
4158 #endif
4159    }
4160
4161   /* Save the initial options in case the user does function specific options */
4162   if (main_args_p)
4163     target_option_default_node = target_option_current_node
4164       = build_target_option_node ();
4165
4166   if (TARGET_AVX)
4167     {
4168       /* When not optimize for size, enable vzeroupper optimization for
4169          TARGET_AVX with -fexpensive-optimizations.  */
4170       if (!optimize_size
4171           && flag_expensive_optimizations
4172           && !(target_flags_explicit & MASK_VZEROUPPER))
4173         target_flags |= MASK_VZEROUPPER;
4174     }
4175   else 
4176     {
4177       /* Disable vzeroupper pass if TARGET_AVX is disabled.  */
4178       target_flags &= ~MASK_VZEROUPPER;
4179     }
4180 }
4181
4182 /* Return TRUE if VAL is passed in register with 256bit AVX modes.  */
4183
4184 static bool
4185 function_pass_avx256_p (const_rtx val)
4186 {
4187   if (!val)
4188     return false;
4189
4190   if (REG_P (val) && VALID_AVX256_REG_MODE (GET_MODE (val)))
4191     return true;
4192
4193   if (GET_CODE (val) == PARALLEL)
4194     {
4195       int i;
4196       rtx r;
4197
4198       for (i = XVECLEN (val, 0) - 1; i >= 0; i--)
4199         {
4200           r = XVECEXP (val, 0, i);
4201           if (GET_CODE (r) == EXPR_LIST
4202               && XEXP (r, 0)
4203               && REG_P (XEXP (r, 0))
4204               && (GET_MODE (XEXP (r, 0)) == OImode
4205                   || VALID_AVX256_REG_MODE (GET_MODE (XEXP (r, 0)))))
4206             return true;
4207         }
4208     }
4209
4210   return false;
4211 }
4212
4213 /* Implement the TARGET_OPTION_OVERRIDE hook.  */
4214
4215 static void
4216 ix86_option_override (void)
4217 {
4218   ix86_option_override_internal (true);
4219 }
4220
4221 /* Update register usage after having seen the compiler flags.  */
4222
4223 static void
4224 ix86_conditional_register_usage (void)
4225 {
4226   int i;
4227   unsigned int j;
4228
4229   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4230     {
4231       if (fixed_regs[i] > 1)
4232         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
4233       if (call_used_regs[i] > 1)
4234         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
4235     }
4236
4237   /* The PIC register, if it exists, is fixed.  */
4238   j = PIC_OFFSET_TABLE_REGNUM;
4239   if (j != INVALID_REGNUM)
4240     fixed_regs[j] = call_used_regs[j] = 1;
4241
4242   /* The MS_ABI changes the set of call-used registers.  */
4243   if (TARGET_64BIT && ix86_cfun_abi () == MS_ABI)
4244     {
4245       call_used_regs[SI_REG] = 0;
4246       call_used_regs[DI_REG] = 0;
4247       call_used_regs[XMM6_REG] = 0;
4248       call_used_regs[XMM7_REG] = 0;
4249       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
4250         call_used_regs[i] = 0;
4251     }
4252
4253   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
4254      other call-clobbered regs for 64-bit.  */
4255   if (TARGET_64BIT)
4256     {
4257       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
4258
4259       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4260         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
4261             && call_used_regs[i])
4262           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
4263     }
4264
4265   /* If MMX is disabled, squash the registers.  */
4266   if (! TARGET_MMX)
4267     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4268       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
4269         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
4270
4271   /* If SSE is disabled, squash the registers.  */
4272   if (! TARGET_SSE)
4273     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4274       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
4275         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
4276
4277   /* If the FPU is disabled, squash the registers.  */
4278   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
4279     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4280       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
4281         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
4282
4283   /* If 32-bit, squash the 64-bit registers.  */
4284   if (! TARGET_64BIT)
4285     {
4286       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
4287         reg_names[i] = "";
4288       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
4289         reg_names[i] = "";
4290     }
4291 }
4292
4293 \f
4294 /* Save the current options */
4295
4296 static void
4297 ix86_function_specific_save (struct cl_target_option *ptr)
4298 {
4299   ptr->arch = ix86_arch;
4300   ptr->schedule = ix86_schedule;
4301   ptr->tune = ix86_tune;
4302   ptr->fpmath = ix86_fpmath;
4303   ptr->branch_cost = ix86_branch_cost;
4304   ptr->tune_defaulted = ix86_tune_defaulted;
4305   ptr->arch_specified = ix86_arch_specified;
4306   ptr->ix86_isa_flags_explicit = ix86_isa_flags_explicit;
4307   ptr->ix86_target_flags_explicit = target_flags_explicit;
4308
4309   /* The fields are char but the variables are not; make sure the
4310      values fit in the fields.  */
4311   gcc_assert (ptr->arch == ix86_arch);
4312   gcc_assert (ptr->schedule == ix86_schedule);
4313   gcc_assert (ptr->tune == ix86_tune);
4314   gcc_assert (ptr->fpmath == ix86_fpmath);
4315   gcc_assert (ptr->branch_cost == ix86_branch_cost);
4316 }
4317
4318 /* Restore the current options */
4319
4320 static void
4321 ix86_function_specific_restore (struct cl_target_option *ptr)
4322 {
4323   enum processor_type old_tune = ix86_tune;
4324   enum processor_type old_arch = ix86_arch;
4325   unsigned int ix86_arch_mask, ix86_tune_mask;
4326   int i;
4327
4328   ix86_arch = (enum processor_type) ptr->arch;
4329   ix86_schedule = (enum attr_cpu) ptr->schedule;
4330   ix86_tune = (enum processor_type) ptr->tune;
4331   ix86_fpmath = (enum fpmath_unit) ptr->fpmath;
4332   ix86_branch_cost = ptr->branch_cost;
4333   ix86_tune_defaulted = ptr->tune_defaulted;
4334   ix86_arch_specified = ptr->arch_specified;
4335   ix86_isa_flags_explicit = ptr->ix86_isa_flags_explicit;
4336   target_flags_explicit = ptr->ix86_target_flags_explicit;
4337
4338   /* Recreate the arch feature tests if the arch changed */
4339   if (old_arch != ix86_arch)
4340     {
4341       ix86_arch_mask = 1u << ix86_arch;
4342       for (i = 0; i < X86_ARCH_LAST; ++i)
4343         ix86_arch_features[i]
4344           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
4345     }
4346
4347   /* Recreate the tune optimization tests */
4348   if (old_tune != ix86_tune)
4349     {
4350       ix86_tune_mask = 1u << ix86_tune;
4351       for (i = 0; i < X86_TUNE_LAST; ++i)
4352         ix86_tune_features[i]
4353           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
4354     }
4355 }
4356
4357 /* Print the current options */
4358
4359 static void
4360 ix86_function_specific_print (FILE *file, int indent,
4361                               struct cl_target_option *ptr)
4362 {
4363   char *target_string
4364     = ix86_target_string (ptr->x_ix86_isa_flags, ptr->x_target_flags,
4365                           NULL, NULL, NULL, false);
4366
4367   fprintf (file, "%*sarch = %d (%s)\n",
4368            indent, "",
4369            ptr->arch,
4370            ((ptr->arch < TARGET_CPU_DEFAULT_max)
4371             ? cpu_names[ptr->arch]
4372             : "<unknown>"));
4373
4374   fprintf (file, "%*stune = %d (%s)\n",
4375            indent, "",
4376            ptr->tune,
4377            ((ptr->tune < TARGET_CPU_DEFAULT_max)
4378             ? cpu_names[ptr->tune]
4379             : "<unknown>"));
4380
4381   fprintf (file, "%*sfpmath = %d%s%s\n", indent, "", ptr->fpmath,
4382            (ptr->fpmath & FPMATH_387) ? ", 387" : "",
4383            (ptr->fpmath & FPMATH_SSE) ? ", sse" : "");
4384   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
4385
4386   if (target_string)
4387     {
4388       fprintf (file, "%*s%s\n", indent, "", target_string);
4389       free (target_string);
4390     }
4391 }
4392
4393 \f
4394 /* Inner function to process the attribute((target(...))), take an argument and
4395    set the current options from the argument. If we have a list, recursively go
4396    over the list.  */
4397
4398 static bool
4399 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[])
4400 {
4401   char *next_optstr;
4402   bool ret = true;
4403
4404 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
4405 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
4406 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
4407 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
4408
4409   enum ix86_opt_type
4410   {
4411     ix86_opt_unknown,
4412     ix86_opt_yes,
4413     ix86_opt_no,
4414     ix86_opt_str,
4415     ix86_opt_isa
4416   };
4417
4418   static const struct
4419   {
4420     const char *string;
4421     size_t len;
4422     enum ix86_opt_type type;
4423     int opt;
4424     int mask;
4425   } attrs[] = {
4426     /* isa options */
4427     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
4428     IX86_ATTR_ISA ("abm",       OPT_mabm),
4429     IX86_ATTR_ISA ("bmi",       OPT_mbmi),
4430     IX86_ATTR_ISA ("tbm",       OPT_mtbm),
4431     IX86_ATTR_ISA ("aes",       OPT_maes),
4432     IX86_ATTR_ISA ("avx",       OPT_mavx),
4433     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
4434     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
4435     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
4436     IX86_ATTR_ISA ("sse",       OPT_msse),
4437     IX86_ATTR_ISA ("sse2",      OPT_msse2),
4438     IX86_ATTR_ISA ("sse3",      OPT_msse3),
4439     IX86_ATTR_ISA ("sse4",      OPT_msse4),
4440     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
4441     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
4442     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
4443     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
4444     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
4445     IX86_ATTR_ISA ("xop",       OPT_mxop),
4446     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
4447     IX86_ATTR_ISA ("fsgsbase",  OPT_mfsgsbase),
4448     IX86_ATTR_ISA ("rdrnd",     OPT_mrdrnd),
4449     IX86_ATTR_ISA ("f16c",      OPT_mf16c),
4450
4451     /* string options */
4452     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
4453     IX86_ATTR_STR ("fpmath=",   IX86_FUNCTION_SPECIFIC_FPMATH),
4454     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
4455
4456     /* flag options */
4457     IX86_ATTR_YES ("cld",
4458                    OPT_mcld,
4459                    MASK_CLD),
4460
4461     IX86_ATTR_NO ("fancy-math-387",
4462                   OPT_mfancy_math_387,
4463                   MASK_NO_FANCY_MATH_387),
4464
4465     IX86_ATTR_YES ("ieee-fp",
4466                    OPT_mieee_fp,
4467                    MASK_IEEE_FP),
4468
4469     IX86_ATTR_YES ("inline-all-stringops",
4470                    OPT_minline_all_stringops,
4471                    MASK_INLINE_ALL_STRINGOPS),
4472
4473     IX86_ATTR_YES ("inline-stringops-dynamically",
4474                    OPT_minline_stringops_dynamically,
4475                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
4476
4477     IX86_ATTR_NO ("align-stringops",
4478                   OPT_mno_align_stringops,
4479                   MASK_NO_ALIGN_STRINGOPS),
4480
4481     IX86_ATTR_YES ("recip",
4482                    OPT_mrecip,
4483                    MASK_RECIP),
4484
4485   };
4486
4487   /* If this is a list, recurse to get the options.  */
4488   if (TREE_CODE (args) == TREE_LIST)
4489     {
4490       bool ret = true;
4491
4492       for (; args; args = TREE_CHAIN (args))
4493         if (TREE_VALUE (args)
4494             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args), p_strings))
4495           ret = false;
4496
4497       return ret;
4498     }
4499
4500   else if (TREE_CODE (args) != STRING_CST)
4501     gcc_unreachable ();
4502
4503   /* Handle multiple arguments separated by commas.  */
4504   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
4505
4506   while (next_optstr && *next_optstr != '\0')
4507     {
4508       char *p = next_optstr;
4509       char *orig_p = p;
4510       char *comma = strchr (next_optstr, ',');
4511       const char *opt_string;
4512       size_t len, opt_len;
4513       int opt;
4514       bool opt_set_p;
4515       char ch;
4516       unsigned i;
4517       enum ix86_opt_type type = ix86_opt_unknown;
4518       int mask = 0;
4519
4520       if (comma)
4521         {
4522           *comma = '\0';
4523           len = comma - next_optstr;
4524           next_optstr = comma + 1;
4525         }
4526       else
4527         {
4528           len = strlen (p);
4529           next_optstr = NULL;
4530         }
4531
4532       /* Recognize no-xxx.  */
4533       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
4534         {
4535           opt_set_p = false;
4536           p += 3;
4537           len -= 3;
4538         }
4539       else
4540         opt_set_p = true;
4541
4542       /* Find the option.  */
4543       ch = *p;
4544       opt = N_OPTS;
4545       for (i = 0; i < ARRAY_SIZE (attrs); i++)
4546         {
4547           type = attrs[i].type;
4548           opt_len = attrs[i].len;
4549           if (ch == attrs[i].string[0]
4550               && ((type != ix86_opt_str) ? len == opt_len : len > opt_len)
4551               && memcmp (p, attrs[i].string, opt_len) == 0)
4552             {
4553               opt = attrs[i].opt;
4554               mask = attrs[i].mask;
4555               opt_string = attrs[i].string;
4556               break;
4557             }
4558         }
4559
4560       /* Process the option.  */
4561       if (opt == N_OPTS)
4562         {
4563           error ("attribute(target(\"%s\")) is unknown", orig_p);
4564           ret = false;
4565         }
4566
4567       else if (type == ix86_opt_isa)
4568         ix86_handle_option (opt, p, opt_set_p);
4569
4570       else if (type == ix86_opt_yes || type == ix86_opt_no)
4571         {
4572           if (type == ix86_opt_no)
4573             opt_set_p = !opt_set_p;
4574
4575           if (opt_set_p)
4576             target_flags |= mask;
4577           else
4578             target_flags &= ~mask;
4579         }
4580
4581       else if (type == ix86_opt_str)
4582         {
4583           if (p_strings[opt])
4584             {
4585               error ("option(\"%s\") was already specified", opt_string);
4586               ret = false;
4587             }
4588           else
4589             p_strings[opt] = xstrdup (p + opt_len);
4590         }
4591
4592       else
4593         gcc_unreachable ();
4594     }
4595
4596   return ret;
4597 }
4598
4599 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
4600
4601 tree
4602 ix86_valid_target_attribute_tree (tree args)
4603 {
4604   const char *orig_arch_string = ix86_arch_string;
4605   const char *orig_tune_string = ix86_tune_string;
4606   const char *orig_fpmath_string = ix86_fpmath_string;
4607   int orig_tune_defaulted = ix86_tune_defaulted;
4608   int orig_arch_specified = ix86_arch_specified;
4609   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL, NULL };
4610   tree t = NULL_TREE;
4611   int i;
4612   struct cl_target_option *def
4613     = TREE_TARGET_OPTION (target_option_default_node);
4614
4615   /* Process each of the options on the chain.  */
4616   if (! ix86_valid_target_attribute_inner_p (args, option_strings))
4617     return NULL_TREE;
4618
4619   /* If the changed options are different from the default, rerun
4620      ix86_option_override_internal, and then save the options away.
4621      The string options are are attribute options, and will be undone
4622      when we copy the save structure.  */
4623   if (ix86_isa_flags != def->x_ix86_isa_flags
4624       || target_flags != def->x_target_flags
4625       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
4626       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
4627       || option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4628     {
4629       /* If we are using the default tune= or arch=, undo the string assigned,
4630          and use the default.  */
4631       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
4632         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
4633       else if (!orig_arch_specified)
4634         ix86_arch_string = NULL;
4635
4636       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
4637         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
4638       else if (orig_tune_defaulted)
4639         ix86_tune_string = NULL;
4640
4641       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
4642       if (option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4643         ix86_fpmath_string = option_strings[IX86_FUNCTION_SPECIFIC_FPMATH];
4644       else if (!TARGET_64BIT && TARGET_SSE)
4645         ix86_fpmath_string = "sse,387";
4646
4647       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
4648       ix86_option_override_internal (false);
4649
4650       /* Add any builtin functions with the new isa if any.  */
4651       ix86_add_new_builtins (ix86_isa_flags);
4652
4653       /* Save the current options unless we are validating options for
4654          #pragma.  */
4655       t = build_target_option_node ();
4656
4657       ix86_arch_string = orig_arch_string;
4658       ix86_tune_string = orig_tune_string;
4659       ix86_fpmath_string = orig_fpmath_string;
4660
4661       /* Free up memory allocated to hold the strings */
4662       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
4663         if (option_strings[i])
4664           free (option_strings[i]);
4665     }
4666
4667   return t;
4668 }
4669
4670 /* Hook to validate attribute((target("string"))).  */
4671
4672 static bool
4673 ix86_valid_target_attribute_p (tree fndecl,
4674                                tree ARG_UNUSED (name),
4675                                tree args,
4676                                int ARG_UNUSED (flags))
4677 {
4678   struct cl_target_option cur_target;
4679   bool ret = true;
4680   tree old_optimize = build_optimization_node ();
4681   tree new_target, new_optimize;
4682   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
4683
4684   /* If the function changed the optimization levels as well as setting target
4685      options, start with the optimizations specified.  */
4686   if (func_optimize && func_optimize != old_optimize)
4687     cl_optimization_restore (&global_options,
4688                              TREE_OPTIMIZATION (func_optimize));
4689
4690   /* The target attributes may also change some optimization flags, so update
4691      the optimization options if necessary.  */
4692   cl_target_option_save (&cur_target, &global_options);
4693   new_target = ix86_valid_target_attribute_tree (args);
4694   new_optimize = build_optimization_node ();
4695
4696   if (!new_target)
4697     ret = false;
4698
4699   else if (fndecl)
4700     {
4701       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
4702
4703       if (old_optimize != new_optimize)
4704         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
4705     }
4706
4707   cl_target_option_restore (&global_options, &cur_target);
4708
4709   if (old_optimize != new_optimize)
4710     cl_optimization_restore (&global_options,
4711                              TREE_OPTIMIZATION (old_optimize));
4712
4713   return ret;
4714 }
4715
4716 \f
4717 /* Hook to determine if one function can safely inline another.  */
4718
4719 static bool
4720 ix86_can_inline_p (tree caller, tree callee)
4721 {
4722   bool ret = false;
4723   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
4724   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
4725
4726   /* If callee has no option attributes, then it is ok to inline.  */
4727   if (!callee_tree)
4728     ret = true;
4729
4730   /* If caller has no option attributes, but callee does then it is not ok to
4731      inline.  */
4732   else if (!caller_tree)
4733     ret = false;
4734
4735   else
4736     {
4737       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
4738       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
4739
4740       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
4741          can inline a SSE2 function but a SSE2 function can't inline a SSE4
4742          function.  */
4743       if ((caller_opts->x_ix86_isa_flags & callee_opts->x_ix86_isa_flags)
4744           != callee_opts->x_ix86_isa_flags)
4745         ret = false;
4746
4747       /* See if we have the same non-isa options.  */
4748       else if (caller_opts->x_target_flags != callee_opts->x_target_flags)
4749         ret = false;
4750
4751       /* See if arch, tune, etc. are the same.  */
4752       else if (caller_opts->arch != callee_opts->arch)
4753         ret = false;
4754
4755       else if (caller_opts->tune != callee_opts->tune)
4756         ret = false;
4757
4758       else if (caller_opts->fpmath != callee_opts->fpmath)
4759         ret = false;
4760
4761       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4762         ret = false;
4763
4764       else
4765         ret = true;
4766     }
4767
4768   return ret;
4769 }
4770
4771 \f
4772 /* Remember the last target of ix86_set_current_function.  */
4773 static GTY(()) tree ix86_previous_fndecl;
4774
4775 /* Establish appropriate back-end context for processing the function
4776    FNDECL.  The argument might be NULL to indicate processing at top
4777    level, outside of any function scope.  */
4778 static void
4779 ix86_set_current_function (tree fndecl)
4780 {
4781   /* Only change the context if the function changes.  This hook is called
4782      several times in the course of compiling a function, and we don't want to
4783      slow things down too much or call target_reinit when it isn't safe.  */
4784   if (fndecl && fndecl != ix86_previous_fndecl)
4785     {
4786       tree old_tree = (ix86_previous_fndecl
4787                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4788                        : NULL_TREE);
4789
4790       tree new_tree = (fndecl
4791                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4792                        : NULL_TREE);
4793
4794       ix86_previous_fndecl = fndecl;
4795       if (old_tree == new_tree)
4796         ;
4797
4798       else if (new_tree)
4799         {
4800           cl_target_option_restore (&global_options,
4801                                     TREE_TARGET_OPTION (new_tree));
4802           target_reinit ();
4803         }
4804
4805       else if (old_tree)
4806         {
4807           struct cl_target_option *def
4808             = TREE_TARGET_OPTION (target_option_current_node);
4809
4810           cl_target_option_restore (&global_options, def);
4811           target_reinit ();
4812         }
4813     }
4814 }
4815
4816 \f
4817 /* Return true if this goes in large data/bss.  */
4818
4819 static bool
4820 ix86_in_large_data_p (tree exp)
4821 {
4822   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4823     return false;
4824
4825   /* Functions are never large data.  */
4826   if (TREE_CODE (exp) == FUNCTION_DECL)
4827     return false;
4828
4829   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4830     {
4831       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4832       if (strcmp (section, ".ldata") == 0
4833           || strcmp (section, ".lbss") == 0)
4834         return true;
4835       return false;
4836     }
4837   else
4838     {
4839       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4840
4841       /* If this is an incomplete type with size 0, then we can't put it
4842          in data because it might be too big when completed.  */
4843       if (!size || size > ix86_section_threshold)
4844         return true;
4845     }
4846
4847   return false;
4848 }
4849
4850 /* Switch to the appropriate section for output of DECL.
4851    DECL is either a `VAR_DECL' node or a constant of some sort.
4852    RELOC indicates whether forming the initial value of DECL requires
4853    link-time relocations.  */
4854
4855 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4856         ATTRIBUTE_UNUSED;
4857
4858 static section *
4859 x86_64_elf_select_section (tree decl, int reloc,
4860                            unsigned HOST_WIDE_INT align)
4861 {
4862   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4863       && ix86_in_large_data_p (decl))
4864     {
4865       const char *sname = NULL;
4866       unsigned int flags = SECTION_WRITE;
4867       switch (categorize_decl_for_section (decl, reloc))
4868         {
4869         case SECCAT_DATA:
4870           sname = ".ldata";
4871           break;
4872         case SECCAT_DATA_REL:
4873           sname = ".ldata.rel";
4874           break;
4875         case SECCAT_DATA_REL_LOCAL:
4876           sname = ".ldata.rel.local";
4877           break;
4878         case SECCAT_DATA_REL_RO:
4879           sname = ".ldata.rel.ro";
4880           break;
4881         case SECCAT_DATA_REL_RO_LOCAL:
4882           sname = ".ldata.rel.ro.local";
4883           break;
4884         case SECCAT_BSS:
4885           sname = ".lbss";
4886           flags |= SECTION_BSS;
4887           break;
4888         case SECCAT_RODATA:
4889         case SECCAT_RODATA_MERGE_STR:
4890         case SECCAT_RODATA_MERGE_STR_INIT:
4891         case SECCAT_RODATA_MERGE_CONST:
4892           sname = ".lrodata";
4893           flags = 0;
4894           break;
4895         case SECCAT_SRODATA:
4896         case SECCAT_SDATA:
4897         case SECCAT_SBSS:
4898           gcc_unreachable ();
4899         case SECCAT_TEXT:
4900         case SECCAT_TDATA:
4901         case SECCAT_TBSS:
4902           /* We don't split these for medium model.  Place them into
4903              default sections and hope for best.  */
4904           break;
4905         }
4906       if (sname)
4907         {
4908           /* We might get called with string constants, but get_named_section
4909              doesn't like them as they are not DECLs.  Also, we need to set
4910              flags in that case.  */
4911           if (!DECL_P (decl))
4912             return get_section (sname, flags, NULL);
4913           return get_named_section (decl, sname, reloc);
4914         }
4915     }
4916   return default_elf_select_section (decl, reloc, align);
4917 }
4918
4919 /* Build up a unique section name, expressed as a
4920    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4921    RELOC indicates whether the initial value of EXP requires
4922    link-time relocations.  */
4923
4924 static void ATTRIBUTE_UNUSED
4925 x86_64_elf_unique_section (tree decl, int reloc)
4926 {
4927   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4928       && ix86_in_large_data_p (decl))
4929     {
4930       const char *prefix = NULL;
4931       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4932       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4933
4934       switch (categorize_decl_for_section (decl, reloc))
4935         {
4936         case SECCAT_DATA:
4937         case SECCAT_DATA_REL:
4938         case SECCAT_DATA_REL_LOCAL:
4939         case SECCAT_DATA_REL_RO:
4940         case SECCAT_DATA_REL_RO_LOCAL:
4941           prefix = one_only ? ".ld" : ".ldata";
4942           break;
4943         case SECCAT_BSS:
4944           prefix = one_only ? ".lb" : ".lbss";
4945           break;
4946         case SECCAT_RODATA:
4947         case SECCAT_RODATA_MERGE_STR:
4948         case SECCAT_RODATA_MERGE_STR_INIT:
4949         case SECCAT_RODATA_MERGE_CONST:
4950           prefix = one_only ? ".lr" : ".lrodata";
4951           break;
4952         case SECCAT_SRODATA:
4953         case SECCAT_SDATA:
4954         case SECCAT_SBSS:
4955           gcc_unreachable ();
4956         case SECCAT_TEXT:
4957         case SECCAT_TDATA:
4958         case SECCAT_TBSS:
4959           /* We don't split these for medium model.  Place them into
4960              default sections and hope for best.  */
4961           break;
4962         }
4963       if (prefix)
4964         {
4965           const char *name, *linkonce;
4966           char *string;
4967
4968           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4969           name = targetm.strip_name_encoding (name);
4970
4971           /* If we're using one_only, then there needs to be a .gnu.linkonce
4972              prefix to the section name.  */
4973           linkonce = one_only ? ".gnu.linkonce" : "";
4974
4975           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4976
4977           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4978           return;
4979         }
4980     }
4981   default_unique_section (decl, reloc);
4982 }
4983
4984 #ifdef COMMON_ASM_OP
4985 /* This says how to output assembler code to declare an
4986    uninitialized external linkage data object.
4987
4988    For medium model x86-64 we need to use .largecomm opcode for
4989    large objects.  */
4990 void
4991 x86_elf_aligned_common (FILE *file,
4992                         const char *name, unsigned HOST_WIDE_INT size,
4993                         int align)
4994 {
4995   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4996       && size > (unsigned int)ix86_section_threshold)
4997     fputs (".largecomm\t", file);
4998   else
4999     fputs (COMMON_ASM_OP, file);
5000   assemble_name (file, name);
5001   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
5002            size, align / BITS_PER_UNIT);
5003 }
5004 #endif
5005
5006 /* Utility function for targets to use in implementing
5007    ASM_OUTPUT_ALIGNED_BSS.  */
5008
5009 void
5010 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
5011                         const char *name, unsigned HOST_WIDE_INT size,
5012                         int align)
5013 {
5014   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
5015       && size > (unsigned int)ix86_section_threshold)
5016     switch_to_section (get_named_section (decl, ".lbss", 0));
5017   else
5018     switch_to_section (bss_section);
5019   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
5020 #ifdef ASM_DECLARE_OBJECT_NAME
5021   last_assemble_variable_decl = decl;
5022   ASM_DECLARE_OBJECT_NAME (file, name, decl);
5023 #else
5024   /* Standard thing is just output label for the object.  */
5025   ASM_OUTPUT_LABEL (file, name);
5026 #endif /* ASM_DECLARE_OBJECT_NAME */
5027   ASM_OUTPUT_SKIP (file, size ? size : 1);
5028 }
5029 \f
5030 static const struct default_options ix86_option_optimization_table[] =
5031   {
5032     /* Turn off -fschedule-insns by default.  It tends to make the
5033        problem with not enough registers even worse.  */
5034 #ifdef INSN_SCHEDULING
5035     { OPT_LEVELS_ALL, OPT_fschedule_insns, NULL, 0 },
5036 #endif
5037
5038 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
5039     SUBTARGET_OPTIMIZATION_OPTIONS,
5040 #endif
5041     { OPT_LEVELS_NONE, 0, NULL, 0 }
5042   };
5043
5044 /* Implement TARGET_OPTION_INIT_STRUCT.  */
5045
5046 static void
5047 ix86_option_init_struct (struct gcc_options *opts)
5048 {
5049   if (TARGET_MACHO)
5050     /* The Darwin libraries never set errno, so we might as well
5051        avoid calling them when that's the only reason we would.  */
5052     opts->x_flag_errno_math = 0;
5053
5054   opts->x_flag_pcc_struct_return = 2;
5055   opts->x_flag_asynchronous_unwind_tables = 2;
5056   opts->x_flag_vect_cost_model = 1;
5057 }
5058
5059 /* Decide whether we must probe the stack before any space allocation
5060    on this target.  It's essentially TARGET_STACK_PROBE except when
5061    -fstack-check causes the stack to be already probed differently.  */
5062
5063 bool
5064 ix86_target_stack_probe (void)
5065 {
5066   /* Do not probe the stack twice if static stack checking is enabled.  */
5067   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
5068     return false;
5069
5070   return TARGET_STACK_PROBE;
5071 }
5072 \f
5073 /* Decide whether we can make a sibling call to a function.  DECL is the
5074    declaration of the function being targeted by the call and EXP is the
5075    CALL_EXPR representing the call.  */
5076
5077 static bool
5078 ix86_function_ok_for_sibcall (tree decl, tree exp)
5079 {
5080   tree type, decl_or_type;
5081   rtx a, b;
5082
5083   /* If we are generating position-independent code, we cannot sibcall
5084      optimize any indirect call, or a direct call to a global function,
5085      as the PLT requires %ebx be live. (Darwin does not have a PLT.)  */
5086   if (!TARGET_MACHO
5087       && !TARGET_64BIT 
5088       && flag_pic 
5089       && (!decl || !targetm.binds_local_p (decl)))
5090     return false;
5091
5092   /* If we need to align the outgoing stack, then sibcalling would
5093      unalign the stack, which may break the called function.  */
5094   if (ix86_minimum_incoming_stack_boundary (true)
5095       < PREFERRED_STACK_BOUNDARY)
5096     return false;
5097
5098   if (decl)
5099     {
5100       decl_or_type = decl;
5101       type = TREE_TYPE (decl);
5102     }
5103   else
5104     {
5105       /* We're looking at the CALL_EXPR, we need the type of the function.  */
5106       type = CALL_EXPR_FN (exp);                /* pointer expression */
5107       type = TREE_TYPE (type);                  /* pointer type */
5108       type = TREE_TYPE (type);                  /* function type */
5109       decl_or_type = type;
5110     }
5111
5112   /* Check that the return value locations are the same.  Like
5113      if we are returning floats on the 80387 register stack, we cannot
5114      make a sibcall from a function that doesn't return a float to a
5115      function that does or, conversely, from a function that does return
5116      a float to a function that doesn't; the necessary stack adjustment
5117      would not be executed.  This is also the place we notice
5118      differences in the return value ABI.  Note that it is ok for one
5119      of the functions to have void return type as long as the return
5120      value of the other is passed in a register.  */
5121   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
5122   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5123                            cfun->decl, false);
5124   if (STACK_REG_P (a) || STACK_REG_P (b))
5125     {
5126       if (!rtx_equal_p (a, b))
5127         return false;
5128     }
5129   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5130     {
5131       /* Disable sibcall if we need to generate vzeroupper after
5132          callee returns.  */
5133       if (TARGET_VZEROUPPER
5134           && cfun->machine->callee_return_avx256_p
5135           && !cfun->machine->caller_return_avx256_p)
5136         return false;
5137     }
5138   else if (!rtx_equal_p (a, b))
5139     return false;
5140
5141   if (TARGET_64BIT)
5142     {
5143       /* The SYSV ABI has more call-clobbered registers;
5144          disallow sibcalls from MS to SYSV.  */
5145       if (cfun->machine->call_abi == MS_ABI
5146           && ix86_function_type_abi (type) == SYSV_ABI)
5147         return false;
5148     }
5149   else
5150     {
5151       /* If this call is indirect, we'll need to be able to use a
5152          call-clobbered register for the address of the target function.
5153          Make sure that all such registers are not used for passing
5154          parameters.  Note that DLLIMPORT functions are indirect.  */
5155       if (!decl
5156           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
5157         {
5158           if (ix86_function_regparm (type, NULL) >= 3)
5159             {
5160               /* ??? Need to count the actual number of registers to be used,
5161                  not the possible number of registers.  Fix later.  */
5162               return false;
5163             }
5164         }
5165     }
5166
5167   /* Otherwise okay.  That also includes certain types of indirect calls.  */
5168   return true;
5169 }
5170
5171 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
5172    and "sseregparm" calling convention attributes;
5173    arguments as in struct attribute_spec.handler.  */
5174
5175 static tree
5176 ix86_handle_cconv_attribute (tree *node, tree name,
5177                                    tree args,
5178                                    int flags ATTRIBUTE_UNUSED,
5179                                    bool *no_add_attrs)
5180 {
5181   if (TREE_CODE (*node) != FUNCTION_TYPE
5182       && TREE_CODE (*node) != METHOD_TYPE
5183       && TREE_CODE (*node) != FIELD_DECL
5184       && TREE_CODE (*node) != TYPE_DECL)
5185     {
5186       warning (OPT_Wattributes, "%qE attribute only applies to functions",
5187                name);
5188       *no_add_attrs = true;
5189       return NULL_TREE;
5190     }
5191
5192   /* Can combine regparm with all attributes but fastcall.  */
5193   if (is_attribute_p ("regparm", name))
5194     {
5195       tree cst;
5196
5197       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5198         {
5199           error ("fastcall and regparm attributes are not compatible");
5200         }
5201
5202       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5203         {
5204           error ("regparam and thiscall attributes are not compatible");
5205         }
5206
5207       cst = TREE_VALUE (args);
5208       if (TREE_CODE (cst) != INTEGER_CST)
5209         {
5210           warning (OPT_Wattributes,
5211                    "%qE attribute requires an integer constant argument",
5212                    name);
5213           *no_add_attrs = true;
5214         }
5215       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
5216         {
5217           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
5218                    name, REGPARM_MAX);
5219           *no_add_attrs = true;
5220         }
5221
5222       return NULL_TREE;
5223     }
5224
5225   if (TARGET_64BIT)
5226     {
5227       /* Do not warn when emulating the MS ABI.  */
5228       if ((TREE_CODE (*node) != FUNCTION_TYPE
5229            && TREE_CODE (*node) != METHOD_TYPE)
5230           || ix86_function_type_abi (*node) != MS_ABI)
5231         warning (OPT_Wattributes, "%qE attribute ignored",
5232                  name);
5233       *no_add_attrs = true;
5234       return NULL_TREE;
5235     }
5236
5237   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
5238   if (is_attribute_p ("fastcall", name))
5239     {
5240       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5241         {
5242           error ("fastcall and cdecl attributes are not compatible");
5243         }
5244       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5245         {
5246           error ("fastcall and stdcall attributes are not compatible");
5247         }
5248       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
5249         {
5250           error ("fastcall and regparm attributes are not compatible");
5251         }
5252       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5253         {
5254           error ("fastcall and thiscall attributes are not compatible");
5255         }
5256     }
5257
5258   /* Can combine stdcall with fastcall (redundant), regparm and
5259      sseregparm.  */
5260   else if (is_attribute_p ("stdcall", name))
5261     {
5262       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5263         {
5264           error ("stdcall and cdecl attributes are not compatible");
5265         }
5266       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5267         {
5268           error ("stdcall and fastcall attributes are not compatible");
5269         }
5270       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5271         {
5272           error ("stdcall and thiscall attributes are not compatible");
5273         }
5274     }
5275
5276   /* Can combine cdecl with regparm and sseregparm.  */
5277   else if (is_attribute_p ("cdecl", name))
5278     {
5279       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5280         {
5281           error ("stdcall and cdecl attributes are not compatible");
5282         }
5283       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5284         {
5285           error ("fastcall and cdecl attributes are not compatible");
5286         }
5287       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5288         {
5289           error ("cdecl and thiscall attributes are not compatible");
5290         }
5291     }
5292   else if (is_attribute_p ("thiscall", name))
5293     {
5294       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
5295         warning (OPT_Wattributes, "%qE attribute is used for none class-method",
5296                  name);
5297       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5298         {
5299           error ("stdcall and thiscall attributes are not compatible");
5300         }
5301       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5302         {
5303           error ("fastcall and thiscall attributes are not compatible");
5304         }
5305       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5306         {
5307           error ("cdecl and thiscall attributes are not compatible");
5308         }
5309     }
5310
5311   /* Can combine sseregparm with all attributes.  */
5312
5313   return NULL_TREE;
5314 }
5315
5316 /* Return 0 if the attributes for two types are incompatible, 1 if they
5317    are compatible, and 2 if they are nearly compatible (which causes a
5318    warning to be generated).  */
5319
5320 static int
5321 ix86_comp_type_attributes (const_tree type1, const_tree type2)
5322 {
5323   /* Check for mismatch of non-default calling convention.  */
5324   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
5325
5326   if (TREE_CODE (type1) != FUNCTION_TYPE
5327       && TREE_CODE (type1) != METHOD_TYPE)
5328     return 1;
5329
5330   /* Check for mismatched fastcall/regparm types.  */
5331   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
5332        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
5333       || (ix86_function_regparm (type1, NULL)
5334           != ix86_function_regparm (type2, NULL)))
5335     return 0;
5336
5337   /* Check for mismatched sseregparm types.  */
5338   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
5339       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
5340     return 0;
5341
5342   /* Check for mismatched thiscall types.  */
5343   if (!lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type1))
5344       != !lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type2)))
5345     return 0;
5346
5347   /* Check for mismatched return types (cdecl vs stdcall).  */
5348   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
5349       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
5350     return 0;
5351
5352   return 1;
5353 }
5354 \f
5355 /* Return the regparm value for a function with the indicated TYPE and DECL.
5356    DECL may be NULL when calling function indirectly
5357    or considering a libcall.  */
5358
5359 static int
5360 ix86_function_regparm (const_tree type, const_tree decl)
5361 {
5362   tree attr;
5363   int regparm;
5364
5365   if (TARGET_64BIT)
5366     return (ix86_function_type_abi (type) == SYSV_ABI
5367             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
5368
5369   regparm = ix86_regparm;
5370   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
5371   if (attr)
5372     {
5373       regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
5374       return regparm;
5375     }
5376
5377   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
5378     return 2;
5379
5380   if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
5381     return 1;
5382
5383   /* Use register calling convention for local functions when possible.  */
5384   if (decl
5385       && TREE_CODE (decl) == FUNCTION_DECL
5386       && optimize
5387       && !(profile_flag && !flag_fentry))
5388     {
5389       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5390       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
5391       if (i && i->local)
5392         {
5393           int local_regparm, globals = 0, regno;
5394
5395           /* Make sure no regparm register is taken by a
5396              fixed register variable.  */
5397           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
5398             if (fixed_regs[local_regparm])
5399               break;
5400
5401           /* We don't want to use regparm(3) for nested functions as
5402              these use a static chain pointer in the third argument.  */
5403           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
5404             local_regparm = 2;
5405
5406           /* In 32-bit mode save a register for the split stack.  */
5407           if (!TARGET_64BIT && local_regparm == 3 && flag_split_stack)
5408             local_regparm = 2;
5409
5410           /* Each fixed register usage increases register pressure,
5411              so less registers should be used for argument passing.
5412              This functionality can be overriden by an explicit
5413              regparm value.  */
5414           for (regno = 0; regno <= DI_REG; regno++)
5415             if (fixed_regs[regno])
5416               globals++;
5417
5418           local_regparm
5419             = globals < local_regparm ? local_regparm - globals : 0;
5420
5421           if (local_regparm > regparm)
5422             regparm = local_regparm;
5423         }
5424     }
5425
5426   return regparm;
5427 }
5428
5429 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
5430    DFmode (2) arguments in SSE registers for a function with the
5431    indicated TYPE and DECL.  DECL may be NULL when calling function
5432    indirectly or considering a libcall.  Otherwise return 0.  */
5433
5434 static int
5435 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
5436 {
5437   gcc_assert (!TARGET_64BIT);
5438
5439   /* Use SSE registers to pass SFmode and DFmode arguments if requested
5440      by the sseregparm attribute.  */
5441   if (TARGET_SSEREGPARM
5442       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
5443     {
5444       if (!TARGET_SSE)
5445         {
5446           if (warn)
5447             {
5448               if (decl)
5449                 error ("calling %qD with attribute sseregparm without "
5450                        "SSE/SSE2 enabled", decl);
5451               else
5452                 error ("calling %qT with attribute sseregparm without "
5453                        "SSE/SSE2 enabled", type);
5454             }
5455           return 0;
5456         }
5457
5458       return 2;
5459     }
5460
5461   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
5462      (and DFmode for SSE2) arguments in SSE registers.  */
5463   if (decl && TARGET_SSE_MATH && optimize
5464       && !(profile_flag && !flag_fentry))
5465     {
5466       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5467       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
5468       if (i && i->local)
5469         return TARGET_SSE2 ? 2 : 1;
5470     }
5471
5472   return 0;
5473 }
5474
5475 /* Return true if EAX is live at the start of the function.  Used by
5476    ix86_expand_prologue to determine if we need special help before
5477    calling allocate_stack_worker.  */
5478
5479 static bool
5480 ix86_eax_live_at_start_p (void)
5481 {
5482   /* Cheat.  Don't bother working forward from ix86_function_regparm
5483      to the function type to whether an actual argument is located in
5484      eax.  Instead just look at cfg info, which is still close enough
5485      to correct at this point.  This gives false positives for broken
5486      functions that might use uninitialized data that happens to be
5487      allocated in eax, but who cares?  */
5488   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
5489 }
5490
5491 static bool
5492 ix86_keep_aggregate_return_pointer (tree fntype)
5493 {
5494   tree attr;
5495
5496   attr = lookup_attribute ("callee_pop_aggregate_return",
5497                            TYPE_ATTRIBUTES (fntype));
5498   if (attr)
5499     return (TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr))) == 0);
5500
5501   return KEEP_AGGREGATE_RETURN_POINTER != 0;
5502 }
5503
5504 /* Value is the number of bytes of arguments automatically
5505    popped when returning from a subroutine call.
5506    FUNDECL is the declaration node of the function (as a tree),
5507    FUNTYPE is the data type of the function (as a tree),
5508    or for a library call it is an identifier node for the subroutine name.
5509    SIZE is the number of bytes of arguments passed on the stack.
5510
5511    On the 80386, the RTD insn may be used to pop them if the number
5512      of args is fixed, but if the number is variable then the caller
5513      must pop them all.  RTD can't be used for library calls now
5514      because the library is compiled with the Unix compiler.
5515    Use of RTD is a selectable option, since it is incompatible with
5516    standard Unix calling sequences.  If the option is not selected,
5517    the caller must always pop the args.
5518
5519    The attribute stdcall is equivalent to RTD on a per module basis.  */
5520
5521 static int
5522 ix86_return_pops_args (tree fundecl, tree funtype, int size)
5523 {
5524   int rtd;
5525
5526   /* None of the 64-bit ABIs pop arguments.  */
5527   if (TARGET_64BIT)
5528     return 0;
5529
5530   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
5531
5532   /* Cdecl functions override -mrtd, and never pop the stack.  */
5533   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
5534     {
5535       /* Stdcall and fastcall functions will pop the stack if not
5536          variable args.  */
5537       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
5538           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype))
5539           || lookup_attribute ("thiscall", TYPE_ATTRIBUTES (funtype)))
5540         rtd = 1;
5541
5542       if (rtd && ! stdarg_p (funtype))
5543         return size;
5544     }
5545
5546   /* Lose any fake structure return argument if it is passed on the stack.  */
5547   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
5548       && !ix86_keep_aggregate_return_pointer (funtype))
5549     {
5550       int nregs = ix86_function_regparm (funtype, fundecl);
5551       if (nregs == 0)
5552         return GET_MODE_SIZE (Pmode);
5553     }
5554
5555   return 0;
5556 }
5557 \f
5558 /* Argument support functions.  */
5559
5560 /* Return true when register may be used to pass function parameters.  */
5561 bool
5562 ix86_function_arg_regno_p (int regno)
5563 {
5564   int i;
5565   const int *parm_regs;
5566
5567   if (!TARGET_64BIT)
5568     {
5569       if (TARGET_MACHO)
5570         return (regno < REGPARM_MAX
5571                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
5572       else
5573         return (regno < REGPARM_MAX
5574                 || (TARGET_MMX && MMX_REGNO_P (regno)
5575                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
5576                 || (TARGET_SSE && SSE_REGNO_P (regno)
5577                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
5578     }
5579
5580   if (TARGET_MACHO)
5581     {
5582       if (SSE_REGNO_P (regno) && TARGET_SSE)
5583         return true;
5584     }
5585   else
5586     {
5587       if (TARGET_SSE && SSE_REGNO_P (regno)
5588           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
5589         return true;
5590     }
5591
5592   /* TODO: The function should depend on current function ABI but
5593      builtins.c would need updating then. Therefore we use the
5594      default ABI.  */
5595
5596   /* RAX is used as hidden argument to va_arg functions.  */
5597   if (ix86_abi == SYSV_ABI && regno == AX_REG)
5598     return true;
5599
5600   if (ix86_abi == MS_ABI)
5601     parm_regs = x86_64_ms_abi_int_parameter_registers;
5602   else
5603     parm_regs = x86_64_int_parameter_registers;
5604   for (i = 0; i < (ix86_abi == MS_ABI
5605                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
5606     if (regno == parm_regs[i])
5607       return true;
5608   return false;
5609 }
5610
5611 /* Return if we do not know how to pass TYPE solely in registers.  */
5612
5613 static bool
5614 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
5615 {
5616   if (must_pass_in_stack_var_size_or_pad (mode, type))
5617     return true;
5618
5619   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
5620      The layout_type routine is crafty and tries to trick us into passing
5621      currently unsupported vector types on the stack by using TImode.  */
5622   return (!TARGET_64BIT && mode == TImode
5623           && type && TREE_CODE (type) != VECTOR_TYPE);
5624 }
5625
5626 /* It returns the size, in bytes, of the area reserved for arguments passed
5627    in registers for the function represented by fndecl dependent to the used
5628    abi format.  */
5629 int
5630 ix86_reg_parm_stack_space (const_tree fndecl)
5631 {
5632   enum calling_abi call_abi = SYSV_ABI;
5633   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
5634     call_abi = ix86_function_abi (fndecl);
5635   else
5636     call_abi = ix86_function_type_abi (fndecl);
5637   if (call_abi == MS_ABI)
5638     return 32;
5639   return 0;
5640 }
5641
5642 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
5643    call abi used.  */
5644 enum calling_abi
5645 ix86_function_type_abi (const_tree fntype)
5646 {
5647   if (TARGET_64BIT && fntype != NULL)
5648     {
5649       enum calling_abi abi = ix86_abi;
5650       if (abi == SYSV_ABI)
5651         {
5652           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
5653             abi = MS_ABI;
5654         }
5655       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
5656         abi = SYSV_ABI;
5657       return abi;
5658     }
5659   return ix86_abi;
5660 }
5661
5662 static bool
5663 ix86_function_ms_hook_prologue (const_tree fn)
5664 {
5665   if (fn && lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fn)))
5666     {
5667       if (decl_function_context (fn) != NULL_TREE)
5668         error_at (DECL_SOURCE_LOCATION (fn),
5669                   "ms_hook_prologue is not compatible with nested function");
5670       else
5671         return true;
5672     }
5673   return false;
5674 }
5675
5676 static enum calling_abi
5677 ix86_function_abi (const_tree fndecl)
5678 {
5679   if (! fndecl)
5680     return ix86_abi;
5681   return ix86_function_type_abi (TREE_TYPE (fndecl));
5682 }
5683
5684 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
5685    call abi used.  */
5686 enum calling_abi
5687 ix86_cfun_abi (void)
5688 {
5689   if (! cfun || ! TARGET_64BIT)
5690     return ix86_abi;
5691   return cfun->machine->call_abi;
5692 }
5693
5694 /* Write the extra assembler code needed to declare a function properly.  */
5695
5696 void
5697 ix86_asm_output_function_label (FILE *asm_out_file, const char *fname,
5698                                 tree decl)
5699 {
5700   bool is_ms_hook = ix86_function_ms_hook_prologue (decl);
5701
5702   if (is_ms_hook)
5703     {
5704       int i, filler_count = (TARGET_64BIT ? 32 : 16);
5705       unsigned int filler_cc = 0xcccccccc;
5706
5707       for (i = 0; i < filler_count; i += 4)
5708         fprintf (asm_out_file, ASM_LONG " %#x\n", filler_cc);
5709     }
5710
5711 #ifdef SUBTARGET_ASM_UNWIND_INIT
5712   SUBTARGET_ASM_UNWIND_INIT (asm_out_file);
5713 #endif
5714
5715   ASM_OUTPUT_LABEL (asm_out_file, fname);
5716
5717   /* Output magic byte marker, if hot-patch attribute is set.  */
5718   if (is_ms_hook)
5719     {
5720       if (TARGET_64BIT)
5721         {
5722           /* leaq [%rsp + 0], %rsp  */
5723           asm_fprintf (asm_out_file, ASM_BYTE
5724                        "0x48, 0x8d, 0xa4, 0x24, 0x00, 0x00, 0x00, 0x00\n");
5725         }
5726       else
5727         {
5728           /* movl.s %edi, %edi
5729              push   %ebp
5730              movl.s %esp, %ebp */
5731           asm_fprintf (asm_out_file, ASM_BYTE
5732                        "0x8b, 0xff, 0x55, 0x8b, 0xec\n");
5733         }
5734     }
5735 }
5736
5737 /* regclass.c  */
5738 extern void init_regs (void);
5739
5740 /* Implementation of call abi switching target hook. Specific to FNDECL
5741    the specific call register sets are set.  See also
5742    ix86_conditional_register_usage for more details.  */
5743 void
5744 ix86_call_abi_override (const_tree fndecl)
5745 {
5746   if (fndecl == NULL_TREE)
5747     cfun->machine->call_abi = ix86_abi;
5748   else
5749     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
5750 }
5751
5752 /* MS and SYSV ABI have different set of call used registers.  Avoid expensive
5753    re-initialization of init_regs each time we switch function context since
5754    this is needed only during RTL expansion.  */
5755 static void
5756 ix86_maybe_switch_abi (void)
5757 {
5758   if (TARGET_64BIT &&
5759       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
5760     reinit_regs ();
5761 }
5762
5763 /* Initialize a variable CUM of type CUMULATIVE_ARGS
5764    for a call to a function whose data type is FNTYPE.
5765    For a library call, FNTYPE is 0.  */
5766
5767 void
5768 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
5769                       tree fntype,      /* tree ptr for function decl */
5770                       rtx libname,      /* SYMBOL_REF of library name or 0 */
5771                       tree fndecl,
5772                       int caller)
5773 {
5774   struct cgraph_local_info *i;
5775   tree fnret_type;
5776
5777   memset (cum, 0, sizeof (*cum));
5778
5779   /* Initialize for the current callee.  */
5780   if (caller)
5781     {
5782       cfun->machine->callee_pass_avx256_p = false;
5783       cfun->machine->callee_return_avx256_p = false;
5784     }
5785
5786   if (fndecl)
5787     {
5788       i = cgraph_local_info (fndecl);
5789       cum->call_abi = ix86_function_abi (fndecl);
5790       fnret_type = TREE_TYPE (TREE_TYPE (fndecl));
5791     }
5792   else
5793     {
5794       i = NULL;
5795       cum->call_abi = ix86_function_type_abi (fntype);
5796       if (fntype)
5797         fnret_type = TREE_TYPE (fntype);
5798       else
5799         fnret_type = NULL;
5800     }
5801
5802   if (TARGET_VZEROUPPER && fnret_type)
5803     {
5804       rtx fnret_value = ix86_function_value (fnret_type, fntype,
5805                                              false);
5806       if (function_pass_avx256_p (fnret_value))
5807         {
5808           /* The return value of this function uses 256bit AVX modes.  */
5809           if (caller)
5810             cfun->machine->callee_return_avx256_p = true;
5811           else
5812             cfun->machine->caller_return_avx256_p = true;
5813         }
5814     }
5815
5816   cum->caller = caller;
5817
5818   /* Set up the number of registers to use for passing arguments.  */
5819
5820   if (cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
5821     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
5822            "or subtarget optimization implying it");
5823   cum->nregs = ix86_regparm;
5824   if (TARGET_64BIT)
5825     {
5826       cum->nregs = (cum->call_abi == SYSV_ABI
5827                    ? X86_64_REGPARM_MAX
5828                    : X86_64_MS_REGPARM_MAX);
5829     }
5830   if (TARGET_SSE)
5831     {
5832       cum->sse_nregs = SSE_REGPARM_MAX;
5833       if (TARGET_64BIT)
5834         {
5835           cum->sse_nregs = (cum->call_abi == SYSV_ABI
5836                            ? X86_64_SSE_REGPARM_MAX
5837                            : X86_64_MS_SSE_REGPARM_MAX);
5838         }
5839     }
5840   if (TARGET_MMX)
5841     cum->mmx_nregs = MMX_REGPARM_MAX;
5842   cum->warn_avx = true;
5843   cum->warn_sse = true;
5844   cum->warn_mmx = true;
5845
5846   /* Because type might mismatch in between caller and callee, we need to
5847      use actual type of function for local calls.
5848      FIXME: cgraph_analyze can be told to actually record if function uses
5849      va_start so for local functions maybe_vaarg can be made aggressive
5850      helping K&R code.
5851      FIXME: once typesytem is fixed, we won't need this code anymore.  */
5852   if (i && i->local)
5853     fntype = TREE_TYPE (fndecl);
5854   cum->maybe_vaarg = (fntype
5855                       ? (!prototype_p (fntype) || stdarg_p (fntype))
5856                       : !libname);
5857
5858   if (!TARGET_64BIT)
5859     {
5860       /* If there are variable arguments, then we won't pass anything
5861          in registers in 32-bit mode. */
5862       if (stdarg_p (fntype))
5863         {
5864           cum->nregs = 0;
5865           cum->sse_nregs = 0;
5866           cum->mmx_nregs = 0;
5867           cum->warn_avx = 0;
5868           cum->warn_sse = 0;
5869           cum->warn_mmx = 0;
5870           return;
5871         }
5872
5873       /* Use ecx and edx registers if function has fastcall attribute,
5874          else look for regparm information.  */
5875       if (fntype)
5876         {
5877           if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
5878             {
5879               cum->nregs = 1;
5880               cum->fastcall = 1; /* Same first register as in fastcall.  */
5881             }
5882           else if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
5883             {
5884               cum->nregs = 2;
5885               cum->fastcall = 1;
5886             }
5887           else
5888             cum->nregs = ix86_function_regparm (fntype, fndecl);
5889         }
5890
5891       /* Set up the number of SSE registers used for passing SFmode
5892          and DFmode arguments.  Warn for mismatching ABI.  */
5893       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
5894     }
5895 }
5896
5897 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
5898    But in the case of vector types, it is some vector mode.
5899
5900    When we have only some of our vector isa extensions enabled, then there
5901    are some modes for which vector_mode_supported_p is false.  For these
5902    modes, the generic vector support in gcc will choose some non-vector mode
5903    in order to implement the type.  By computing the natural mode, we'll
5904    select the proper ABI location for the operand and not depend on whatever
5905    the middle-end decides to do with these vector types.
5906
5907    The midde-end can't deal with the vector types > 16 bytes.  In this
5908    case, we return the original mode and warn ABI change if CUM isn't
5909    NULL.  */
5910
5911 static enum machine_mode
5912 type_natural_mode (const_tree type, const CUMULATIVE_ARGS *cum)
5913 {
5914   enum machine_mode mode = TYPE_MODE (type);
5915
5916   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5917     {
5918       HOST_WIDE_INT size = int_size_in_bytes (type);
5919       if ((size == 8 || size == 16 || size == 32)
5920           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5921           && TYPE_VECTOR_SUBPARTS (type) > 1)
5922         {
5923           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5924
5925           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5926             mode = MIN_MODE_VECTOR_FLOAT;
5927           else
5928             mode = MIN_MODE_VECTOR_INT;
5929
5930           /* Get the mode which has this inner mode and number of units.  */
5931           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5932             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5933                 && GET_MODE_INNER (mode) == innermode)
5934               {
5935                 if (size == 32 && !TARGET_AVX)
5936                   {
5937                     static bool warnedavx;
5938
5939                     if (cum
5940                         && !warnedavx
5941                         && cum->warn_avx)
5942                       {
5943                         warnedavx = true;
5944                         warning (0, "AVX vector argument without AVX "
5945                                  "enabled changes the ABI");
5946                       }
5947                     return TYPE_MODE (type);
5948                   }
5949                 else
5950                   return mode;
5951               }
5952
5953           gcc_unreachable ();
5954         }
5955     }
5956
5957   return mode;
5958 }
5959
5960 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5961    this may not agree with the mode that the type system has chosen for the
5962    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5963    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5964
5965 static rtx
5966 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5967                      unsigned int regno)
5968 {
5969   rtx tmp;
5970
5971   if (orig_mode != BLKmode)
5972     tmp = gen_rtx_REG (orig_mode, regno);
5973   else
5974     {
5975       tmp = gen_rtx_REG (mode, regno);
5976       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5977       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5978     }
5979
5980   return tmp;
5981 }
5982
5983 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5984    of this code is to classify each 8bytes of incoming argument by the register
5985    class and assign registers accordingly.  */
5986
5987 /* Return the union class of CLASS1 and CLASS2.
5988    See the x86-64 PS ABI for details.  */
5989
5990 static enum x86_64_reg_class
5991 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5992 {
5993   /* Rule #1: If both classes are equal, this is the resulting class.  */
5994   if (class1 == class2)
5995     return class1;
5996
5997   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5998      the other class.  */
5999   if (class1 == X86_64_NO_CLASS)
6000     return class2;
6001   if (class2 == X86_64_NO_CLASS)
6002     return class1;
6003
6004   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
6005   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
6006     return X86_64_MEMORY_CLASS;
6007
6008   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
6009   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
6010       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
6011     return X86_64_INTEGERSI_CLASS;
6012   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
6013       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
6014     return X86_64_INTEGER_CLASS;
6015
6016   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
6017      MEMORY is used.  */
6018   if (class1 == X86_64_X87_CLASS
6019       || class1 == X86_64_X87UP_CLASS
6020       || class1 == X86_64_COMPLEX_X87_CLASS
6021       || class2 == X86_64_X87_CLASS
6022       || class2 == X86_64_X87UP_CLASS
6023       || class2 == X86_64_COMPLEX_X87_CLASS)
6024     return X86_64_MEMORY_CLASS;
6025
6026   /* Rule #6: Otherwise class SSE is used.  */
6027   return X86_64_SSE_CLASS;
6028 }
6029
6030 /* Classify the argument of type TYPE and mode MODE.
6031    CLASSES will be filled by the register class used to pass each word
6032    of the operand.  The number of words is returned.  In case the parameter
6033    should be passed in memory, 0 is returned. As a special case for zero
6034    sized containers, classes[0] will be NO_CLASS and 1 is returned.
6035
6036    BIT_OFFSET is used internally for handling records and specifies offset
6037    of the offset in bits modulo 256 to avoid overflow cases.
6038
6039    See the x86-64 PS ABI for details.
6040 */
6041
6042 static int
6043 classify_argument (enum machine_mode mode, const_tree type,
6044                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
6045 {
6046   HOST_WIDE_INT bytes =
6047     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
6048   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6049
6050   /* Variable sized entities are always passed/returned in memory.  */
6051   if (bytes < 0)
6052     return 0;
6053
6054   if (mode != VOIDmode
6055       && targetm.calls.must_pass_in_stack (mode, type))
6056     return 0;
6057
6058   if (type && AGGREGATE_TYPE_P (type))
6059     {
6060       int i;
6061       tree field;
6062       enum x86_64_reg_class subclasses[MAX_CLASSES];
6063
6064       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
6065       if (bytes > 32)
6066         return 0;
6067
6068       for (i = 0; i < words; i++)
6069         classes[i] = X86_64_NO_CLASS;
6070
6071       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
6072          signalize memory class, so handle it as special case.  */
6073       if (!words)
6074         {
6075           classes[0] = X86_64_NO_CLASS;
6076           return 1;
6077         }
6078
6079       /* Classify each field of record and merge classes.  */
6080       switch (TREE_CODE (type))
6081         {
6082         case RECORD_TYPE:
6083           /* And now merge the fields of structure.  */
6084           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6085             {
6086               if (TREE_CODE (field) == FIELD_DECL)
6087                 {
6088                   int num;
6089
6090                   if (TREE_TYPE (field) == error_mark_node)
6091                     continue;
6092
6093                   /* Bitfields are always classified as integer.  Handle them
6094                      early, since later code would consider them to be
6095                      misaligned integers.  */
6096                   if (DECL_BIT_FIELD (field))
6097                     {
6098                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
6099                            i < ((int_bit_position (field) + (bit_offset % 64))
6100                                 + tree_low_cst (DECL_SIZE (field), 0)
6101                                 + 63) / 8 / 8; i++)
6102                         classes[i] =
6103                           merge_classes (X86_64_INTEGER_CLASS,
6104                                          classes[i]);
6105                     }
6106                   else
6107                     {
6108                       int pos;
6109
6110                       type = TREE_TYPE (field);
6111
6112                       /* Flexible array member is ignored.  */
6113                       if (TYPE_MODE (type) == BLKmode
6114                           && TREE_CODE (type) == ARRAY_TYPE
6115                           && TYPE_SIZE (type) == NULL_TREE
6116                           && TYPE_DOMAIN (type) != NULL_TREE
6117                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
6118                               == NULL_TREE))
6119                         {
6120                           static bool warned;
6121
6122                           if (!warned && warn_psabi)
6123                             {
6124                               warned = true;
6125                               inform (input_location,
6126                                       "the ABI of passing struct with"
6127                                       " a flexible array member has"
6128                                       " changed in GCC 4.4");
6129                             }
6130                           continue;
6131                         }
6132                       num = classify_argument (TYPE_MODE (type), type,
6133                                                subclasses,
6134                                                (int_bit_position (field)
6135                                                 + bit_offset) % 256);
6136                       if (!num)
6137                         return 0;
6138                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
6139                       for (i = 0; i < num && (i + pos) < words; i++)
6140                         classes[i + pos] =
6141                           merge_classes (subclasses[i], classes[i + pos]);
6142                     }
6143                 }
6144             }
6145           break;
6146
6147         case ARRAY_TYPE:
6148           /* Arrays are handled as small records.  */
6149           {
6150             int num;
6151             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
6152                                      TREE_TYPE (type), subclasses, bit_offset);
6153             if (!num)
6154               return 0;
6155
6156             /* The partial classes are now full classes.  */
6157             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
6158               subclasses[0] = X86_64_SSE_CLASS;
6159             if (subclasses[0] == X86_64_INTEGERSI_CLASS
6160                 && !((bit_offset % 64) == 0 && bytes == 4))
6161               subclasses[0] = X86_64_INTEGER_CLASS;
6162
6163             for (i = 0; i < words; i++)
6164               classes[i] = subclasses[i % num];
6165
6166             break;
6167           }
6168         case UNION_TYPE:
6169         case QUAL_UNION_TYPE:
6170           /* Unions are similar to RECORD_TYPE but offset is always 0.
6171              */
6172           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6173             {
6174               if (TREE_CODE (field) == FIELD_DECL)
6175                 {
6176                   int num;
6177
6178                   if (TREE_TYPE (field) == error_mark_node)
6179                     continue;
6180
6181                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
6182                                            TREE_TYPE (field), subclasses,
6183                                            bit_offset);
6184                   if (!num)
6185                     return 0;
6186                   for (i = 0; i < num; i++)
6187                     classes[i] = merge_classes (subclasses[i], classes[i]);
6188                 }
6189             }
6190           break;
6191
6192         default:
6193           gcc_unreachable ();
6194         }
6195
6196       if (words > 2)
6197         {
6198           /* When size > 16 bytes, if the first one isn't
6199              X86_64_SSE_CLASS or any other ones aren't
6200              X86_64_SSEUP_CLASS, everything should be passed in
6201              memory.  */
6202           if (classes[0] != X86_64_SSE_CLASS)
6203               return 0;
6204
6205           for (i = 1; i < words; i++)
6206             if (classes[i] != X86_64_SSEUP_CLASS)
6207               return 0;
6208         }
6209
6210       /* Final merger cleanup.  */
6211       for (i = 0; i < words; i++)
6212         {
6213           /* If one class is MEMORY, everything should be passed in
6214              memory.  */
6215           if (classes[i] == X86_64_MEMORY_CLASS)
6216             return 0;
6217
6218           /* The X86_64_SSEUP_CLASS should be always preceded by
6219              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
6220           if (classes[i] == X86_64_SSEUP_CLASS
6221               && classes[i - 1] != X86_64_SSE_CLASS
6222               && classes[i - 1] != X86_64_SSEUP_CLASS)
6223             {
6224               /* The first one should never be X86_64_SSEUP_CLASS.  */
6225               gcc_assert (i != 0);
6226               classes[i] = X86_64_SSE_CLASS;
6227             }
6228
6229           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
6230                everything should be passed in memory.  */
6231           if (classes[i] == X86_64_X87UP_CLASS
6232               && (classes[i - 1] != X86_64_X87_CLASS))
6233             {
6234               static bool warned;
6235
6236               /* The first one should never be X86_64_X87UP_CLASS.  */
6237               gcc_assert (i != 0);
6238               if (!warned && warn_psabi)
6239                 {
6240                   warned = true;
6241                   inform (input_location,
6242                           "the ABI of passing union with long double"
6243                           " has changed in GCC 4.4");
6244                 }
6245               return 0;
6246             }
6247         }
6248       return words;
6249     }
6250
6251   /* Compute alignment needed.  We align all types to natural boundaries with
6252      exception of XFmode that is aligned to 64bits.  */
6253   if (mode != VOIDmode && mode != BLKmode)
6254     {
6255       int mode_alignment = GET_MODE_BITSIZE (mode);
6256
6257       if (mode == XFmode)
6258         mode_alignment = 128;
6259       else if (mode == XCmode)
6260         mode_alignment = 256;
6261       if (COMPLEX_MODE_P (mode))
6262         mode_alignment /= 2;
6263       /* Misaligned fields are always returned in memory.  */
6264       if (bit_offset % mode_alignment)
6265         return 0;
6266     }
6267
6268   /* for V1xx modes, just use the base mode */
6269   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
6270       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
6271     mode = GET_MODE_INNER (mode);
6272
6273   /* Classification of atomic types.  */
6274   switch (mode)
6275     {
6276     case SDmode:
6277     case DDmode:
6278       classes[0] = X86_64_SSE_CLASS;
6279       return 1;
6280     case TDmode:
6281       classes[0] = X86_64_SSE_CLASS;
6282       classes[1] = X86_64_SSEUP_CLASS;
6283       return 2;
6284     case DImode:
6285     case SImode:
6286     case HImode:
6287     case QImode:
6288     case CSImode:
6289     case CHImode:
6290     case CQImode:
6291       {
6292         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
6293
6294         if (size <= 32)
6295           {
6296             classes[0] = X86_64_INTEGERSI_CLASS;
6297             return 1;
6298           }
6299         else if (size <= 64)
6300           {
6301             classes[0] = X86_64_INTEGER_CLASS;
6302             return 1;
6303           }
6304         else if (size <= 64+32)
6305           {
6306             classes[0] = X86_64_INTEGER_CLASS;
6307             classes[1] = X86_64_INTEGERSI_CLASS;
6308             return 2;
6309           }
6310         else if (size <= 64+64)
6311           {
6312             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6313             return 2;
6314           }
6315         else
6316           gcc_unreachable ();
6317       }
6318     case CDImode:
6319     case TImode:
6320       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6321       return 2;
6322     case COImode:
6323     case OImode:
6324       /* OImode shouldn't be used directly.  */
6325       gcc_unreachable ();
6326     case CTImode:
6327       return 0;
6328     case SFmode:
6329       if (!(bit_offset % 64))
6330         classes[0] = X86_64_SSESF_CLASS;
6331       else
6332         classes[0] = X86_64_SSE_CLASS;
6333       return 1;
6334     case DFmode:
6335       classes[0] = X86_64_SSEDF_CLASS;
6336       return 1;
6337     case XFmode:
6338       classes[0] = X86_64_X87_CLASS;
6339       classes[1] = X86_64_X87UP_CLASS;
6340       return 2;
6341     case TFmode:
6342       classes[0] = X86_64_SSE_CLASS;
6343       classes[1] = X86_64_SSEUP_CLASS;
6344       return 2;
6345     case SCmode:
6346       classes[0] = X86_64_SSE_CLASS;
6347       if (!(bit_offset % 64))
6348         return 1;
6349       else
6350         {
6351           static bool warned;
6352
6353           if (!warned && warn_psabi)
6354             {
6355               warned = true;
6356               inform (input_location,
6357                       "the ABI of passing structure with complex float"
6358                       " member has changed in GCC 4.4");
6359             }
6360           classes[1] = X86_64_SSESF_CLASS;
6361           return 2;
6362         }
6363     case DCmode:
6364       classes[0] = X86_64_SSEDF_CLASS;
6365       classes[1] = X86_64_SSEDF_CLASS;
6366       return 2;
6367     case XCmode:
6368       classes[0] = X86_64_COMPLEX_X87_CLASS;
6369       return 1;
6370     case TCmode:
6371       /* This modes is larger than 16 bytes.  */
6372       return 0;
6373     case V8SFmode:
6374     case V8SImode:
6375     case V32QImode:
6376     case V16HImode:
6377     case V4DFmode:
6378     case V4DImode:
6379       classes[0] = X86_64_SSE_CLASS;
6380       classes[1] = X86_64_SSEUP_CLASS;
6381       classes[2] = X86_64_SSEUP_CLASS;
6382       classes[3] = X86_64_SSEUP_CLASS;
6383       return 4;
6384     case V4SFmode:
6385     case V4SImode:
6386     case V16QImode:
6387     case V8HImode:
6388     case V2DFmode:
6389     case V2DImode:
6390       classes[0] = X86_64_SSE_CLASS;
6391       classes[1] = X86_64_SSEUP_CLASS;
6392       return 2;
6393     case V1TImode:
6394     case V1DImode:
6395     case V2SFmode:
6396     case V2SImode:
6397     case V4HImode:
6398     case V8QImode:
6399       classes[0] = X86_64_SSE_CLASS;
6400       return 1;
6401     case BLKmode:
6402     case VOIDmode:
6403       return 0;
6404     default:
6405       gcc_assert (VECTOR_MODE_P (mode));
6406
6407       if (bytes > 16)
6408         return 0;
6409
6410       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
6411
6412       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
6413         classes[0] = X86_64_INTEGERSI_CLASS;
6414       else
6415         classes[0] = X86_64_INTEGER_CLASS;
6416       classes[1] = X86_64_INTEGER_CLASS;
6417       return 1 + (bytes > 8);
6418     }
6419 }
6420
6421 /* Examine the argument and return set number of register required in each
6422    class.  Return 0 iff parameter should be passed in memory.  */
6423 static int
6424 examine_argument (enum machine_mode mode, const_tree type, int in_return,
6425                   int *int_nregs, int *sse_nregs)
6426 {
6427   enum x86_64_reg_class regclass[MAX_CLASSES];
6428   int n = classify_argument (mode, type, regclass, 0);
6429
6430   *int_nregs = 0;
6431   *sse_nregs = 0;
6432   if (!n)
6433     return 0;
6434   for (n--; n >= 0; n--)
6435     switch (regclass[n])
6436       {
6437       case X86_64_INTEGER_CLASS:
6438       case X86_64_INTEGERSI_CLASS:
6439         (*int_nregs)++;
6440         break;
6441       case X86_64_SSE_CLASS:
6442       case X86_64_SSESF_CLASS:
6443       case X86_64_SSEDF_CLASS:
6444         (*sse_nregs)++;
6445         break;
6446       case X86_64_NO_CLASS:
6447       case X86_64_SSEUP_CLASS:
6448         break;
6449       case X86_64_X87_CLASS:
6450       case X86_64_X87UP_CLASS:
6451         if (!in_return)
6452           return 0;
6453         break;
6454       case X86_64_COMPLEX_X87_CLASS:
6455         return in_return ? 2 : 0;
6456       case X86_64_MEMORY_CLASS:
6457         gcc_unreachable ();
6458       }
6459   return 1;
6460 }
6461
6462 /* Construct container for the argument used by GCC interface.  See
6463    FUNCTION_ARG for the detailed description.  */
6464
6465 static rtx
6466 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
6467                      const_tree type, int in_return, int nintregs, int nsseregs,
6468                      const int *intreg, int sse_regno)
6469 {
6470   /* The following variables hold the static issued_error state.  */
6471   static bool issued_sse_arg_error;
6472   static bool issued_sse_ret_error;
6473   static bool issued_x87_ret_error;
6474
6475   enum machine_mode tmpmode;
6476   int bytes =
6477     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
6478   enum x86_64_reg_class regclass[MAX_CLASSES];
6479   int n;
6480   int i;
6481   int nexps = 0;
6482   int needed_sseregs, needed_intregs;
6483   rtx exp[MAX_CLASSES];
6484   rtx ret;
6485
6486   n = classify_argument (mode, type, regclass, 0);
6487   if (!n)
6488     return NULL;
6489   if (!examine_argument (mode, type, in_return, &needed_intregs,
6490                          &needed_sseregs))
6491     return NULL;
6492   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
6493     return NULL;
6494
6495   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
6496      some less clueful developer tries to use floating-point anyway.  */
6497   if (needed_sseregs && !TARGET_SSE)
6498     {
6499       if (in_return)
6500         {
6501           if (!issued_sse_ret_error)
6502             {
6503               error ("SSE register return with SSE disabled");
6504               issued_sse_ret_error = true;
6505             }
6506         }
6507       else if (!issued_sse_arg_error)
6508         {
6509           error ("SSE register argument with SSE disabled");
6510           issued_sse_arg_error = true;
6511         }
6512       return NULL;
6513     }
6514
6515   /* Likewise, error if the ABI requires us to return values in the
6516      x87 registers and the user specified -mno-80387.  */
6517   if (!TARGET_80387 && in_return)
6518     for (i = 0; i < n; i++)
6519       if (regclass[i] == X86_64_X87_CLASS
6520           || regclass[i] == X86_64_X87UP_CLASS
6521           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
6522         {
6523           if (!issued_x87_ret_error)
6524             {
6525               error ("x87 register return with x87 disabled");
6526               issued_x87_ret_error = true;
6527             }
6528           return NULL;
6529         }
6530
6531   /* First construct simple cases.  Avoid SCmode, since we want to use
6532      single register to pass this type.  */
6533   if (n == 1 && mode != SCmode)
6534     switch (regclass[0])
6535       {
6536       case X86_64_INTEGER_CLASS:
6537       case X86_64_INTEGERSI_CLASS:
6538         return gen_rtx_REG (mode, intreg[0]);
6539       case X86_64_SSE_CLASS:
6540       case X86_64_SSESF_CLASS:
6541       case X86_64_SSEDF_CLASS:
6542         if (mode != BLKmode)
6543           return gen_reg_or_parallel (mode, orig_mode,
6544                                       SSE_REGNO (sse_regno));
6545         break;
6546       case X86_64_X87_CLASS:
6547       case X86_64_COMPLEX_X87_CLASS:
6548         return gen_rtx_REG (mode, FIRST_STACK_REG);
6549       case X86_64_NO_CLASS:
6550         /* Zero sized array, struct or class.  */
6551         return NULL;
6552       default:
6553         gcc_unreachable ();
6554       }
6555   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
6556       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
6557     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6558   if (n == 4
6559       && regclass[0] == X86_64_SSE_CLASS
6560       && regclass[1] == X86_64_SSEUP_CLASS
6561       && regclass[2] == X86_64_SSEUP_CLASS
6562       && regclass[3] == X86_64_SSEUP_CLASS
6563       && mode != BLKmode)
6564     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6565
6566   if (n == 2
6567       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
6568     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
6569   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
6570       && regclass[1] == X86_64_INTEGER_CLASS
6571       && (mode == CDImode || mode == TImode || mode == TFmode)
6572       && intreg[0] + 1 == intreg[1])
6573     return gen_rtx_REG (mode, intreg[0]);
6574
6575   /* Otherwise figure out the entries of the PARALLEL.  */
6576   for (i = 0; i < n; i++)
6577     {
6578       int pos;
6579
6580       switch (regclass[i])
6581         {
6582           case X86_64_NO_CLASS:
6583             break;
6584           case X86_64_INTEGER_CLASS:
6585           case X86_64_INTEGERSI_CLASS:
6586             /* Merge TImodes on aligned occasions here too.  */
6587             if (i * 8 + 8 > bytes)
6588               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
6589             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
6590               tmpmode = SImode;
6591             else
6592               tmpmode = DImode;
6593             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
6594             if (tmpmode == BLKmode)
6595               tmpmode = DImode;
6596             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6597                                                gen_rtx_REG (tmpmode, *intreg),
6598                                                GEN_INT (i*8));
6599             intreg++;
6600             break;
6601           case X86_64_SSESF_CLASS:
6602             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6603                                                gen_rtx_REG (SFmode,
6604                                                             SSE_REGNO (sse_regno)),
6605                                                GEN_INT (i*8));
6606             sse_regno++;
6607             break;
6608           case X86_64_SSEDF_CLASS:
6609             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6610                                                gen_rtx_REG (DFmode,
6611                                                             SSE_REGNO (sse_regno)),
6612                                                GEN_INT (i*8));
6613             sse_regno++;
6614             break;
6615           case X86_64_SSE_CLASS:
6616             pos = i;
6617             switch (n)
6618               {
6619               case 1:
6620                 tmpmode = DImode;
6621                 break;
6622               case 2:
6623                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
6624                   {
6625                     tmpmode = TImode;
6626                     i++;
6627                   }
6628                 else
6629                   tmpmode = DImode;
6630                 break;
6631               case 4:
6632                 gcc_assert (i == 0
6633                             && regclass[1] == X86_64_SSEUP_CLASS
6634                             && regclass[2] == X86_64_SSEUP_CLASS
6635                             && regclass[3] == X86_64_SSEUP_CLASS);
6636                 tmpmode = OImode;
6637                 i += 3;
6638                 break;
6639               default:
6640                 gcc_unreachable ();
6641               }
6642             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6643                                                gen_rtx_REG (tmpmode,
6644                                                             SSE_REGNO (sse_regno)),
6645                                                GEN_INT (pos*8));
6646             sse_regno++;
6647             break;
6648           default:
6649             gcc_unreachable ();
6650         }
6651     }
6652
6653   /* Empty aligned struct, union or class.  */
6654   if (nexps == 0)
6655     return NULL;
6656
6657   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
6658   for (i = 0; i < nexps; i++)
6659     XVECEXP (ret, 0, i) = exp [i];
6660   return ret;
6661 }
6662
6663 /* Update the data in CUM to advance over an argument of mode MODE
6664    and data type TYPE.  (TYPE is null for libcalls where that information
6665    may not be available.)  */
6666
6667 static void
6668 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6669                          const_tree type, HOST_WIDE_INT bytes,
6670                          HOST_WIDE_INT words)
6671 {
6672   switch (mode)
6673     {
6674     default:
6675       break;
6676
6677     case BLKmode:
6678       if (bytes < 0)
6679         break;
6680       /* FALLTHRU */
6681
6682     case DImode:
6683     case SImode:
6684     case HImode:
6685     case QImode:
6686       cum->words += words;
6687       cum->nregs -= words;
6688       cum->regno += words;
6689
6690       if (cum->nregs <= 0)
6691         {
6692           cum->nregs = 0;
6693           cum->regno = 0;
6694         }
6695       break;
6696
6697     case OImode:
6698       /* OImode shouldn't be used directly.  */
6699       gcc_unreachable ();
6700
6701     case DFmode:
6702       if (cum->float_in_sse < 2)
6703         break;
6704     case SFmode:
6705       if (cum->float_in_sse < 1)
6706         break;
6707       /* FALLTHRU */
6708
6709     case V8SFmode:
6710     case V8SImode:
6711     case V32QImode:
6712     case V16HImode:
6713     case V4DFmode:
6714     case V4DImode:
6715     case TImode:
6716     case V16QImode:
6717     case V8HImode:
6718     case V4SImode:
6719     case V2DImode:
6720     case V4SFmode:
6721     case V2DFmode:
6722       if (!type || !AGGREGATE_TYPE_P (type))
6723         {
6724           cum->sse_words += words;
6725           cum->sse_nregs -= 1;
6726           cum->sse_regno += 1;
6727           if (cum->sse_nregs <= 0)
6728             {
6729               cum->sse_nregs = 0;
6730               cum->sse_regno = 0;
6731             }
6732         }
6733       break;
6734
6735     case V8QImode:
6736     case V4HImode:
6737     case V2SImode:
6738     case V2SFmode:
6739     case V1TImode:
6740     case V1DImode:
6741       if (!type || !AGGREGATE_TYPE_P (type))
6742         {
6743           cum->mmx_words += words;
6744           cum->mmx_nregs -= 1;
6745           cum->mmx_regno += 1;
6746           if (cum->mmx_nregs <= 0)
6747             {
6748               cum->mmx_nregs = 0;
6749               cum->mmx_regno = 0;
6750             }
6751         }
6752       break;
6753     }
6754 }
6755
6756 static void
6757 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6758                          const_tree type, HOST_WIDE_INT words, bool named)
6759 {
6760   int int_nregs, sse_nregs;
6761
6762   /* Unnamed 256bit vector mode parameters are passed on stack.  */
6763   if (!named && VALID_AVX256_REG_MODE (mode))
6764     return;
6765
6766   if (examine_argument (mode, type, 0, &int_nregs, &sse_nregs)
6767       && sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
6768     {
6769       cum->nregs -= int_nregs;
6770       cum->sse_nregs -= sse_nregs;
6771       cum->regno += int_nregs;
6772       cum->sse_regno += sse_nregs;
6773     }
6774   else
6775     {
6776       int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD;
6777       cum->words = (cum->words + align - 1) & ~(align - 1);
6778       cum->words += words;
6779     }
6780 }
6781
6782 static void
6783 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
6784                             HOST_WIDE_INT words)
6785 {
6786   /* Otherwise, this should be passed indirect.  */
6787   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
6788
6789   cum->words += words;
6790   if (cum->nregs > 0)
6791     {
6792       cum->nregs -= 1;
6793       cum->regno += 1;
6794     }
6795 }
6796
6797 /* Update the data in CUM to advance over an argument of mode MODE and
6798    data type TYPE.  (TYPE is null for libcalls where that information
6799    may not be available.)  */
6800
6801 static void
6802 ix86_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6803                            const_tree type, bool named)
6804 {
6805   HOST_WIDE_INT bytes, words;
6806
6807   if (mode == BLKmode)
6808     bytes = int_size_in_bytes (type);
6809   else
6810     bytes = GET_MODE_SIZE (mode);
6811   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6812
6813   if (type)
6814     mode = type_natural_mode (type, NULL);
6815
6816   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6817     function_arg_advance_ms_64 (cum, bytes, words);
6818   else if (TARGET_64BIT)
6819     function_arg_advance_64 (cum, mode, type, words, named);
6820   else
6821     function_arg_advance_32 (cum, mode, type, bytes, words);
6822 }
6823
6824 /* Define where to put the arguments to a function.
6825    Value is zero to push the argument on the stack,
6826    or a hard register in which to store the argument.
6827
6828    MODE is the argument's machine mode.
6829    TYPE is the data type of the argument (as a tree).
6830     This is null for libcalls where that information may
6831     not be available.
6832    CUM is a variable of type CUMULATIVE_ARGS which gives info about
6833     the preceding args and about the function being called.
6834    NAMED is nonzero if this argument is a named parameter
6835     (otherwise it is an extra parameter matching an ellipsis).  */
6836
6837 static rtx
6838 function_arg_32 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6839                  enum machine_mode orig_mode, const_tree type,
6840                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
6841 {
6842   static bool warnedsse, warnedmmx;
6843
6844   /* Avoid the AL settings for the Unix64 ABI.  */
6845   if (mode == VOIDmode)
6846     return constm1_rtx;
6847
6848   switch (mode)
6849     {
6850     default:
6851       break;
6852
6853     case BLKmode:
6854       if (bytes < 0)
6855         break;
6856       /* FALLTHRU */
6857     case DImode:
6858     case SImode:
6859     case HImode:
6860     case QImode:
6861       if (words <= cum->nregs)
6862         {
6863           int regno = cum->regno;
6864
6865           /* Fastcall allocates the first two DWORD (SImode) or
6866             smaller arguments to ECX and EDX if it isn't an
6867             aggregate type .  */
6868           if (cum->fastcall)
6869             {
6870               if (mode == BLKmode
6871                   || mode == DImode
6872                   || (type && AGGREGATE_TYPE_P (type)))
6873                 break;
6874
6875               /* ECX not EAX is the first allocated register.  */
6876               if (regno == AX_REG)
6877                 regno = CX_REG;
6878             }
6879           return gen_rtx_REG (mode, regno);
6880         }
6881       break;
6882
6883     case DFmode:
6884       if (cum->float_in_sse < 2)
6885         break;
6886     case SFmode:
6887       if (cum->float_in_sse < 1)
6888         break;
6889       /* FALLTHRU */
6890     case TImode:
6891       /* In 32bit, we pass TImode in xmm registers.  */
6892     case V16QImode:
6893     case V8HImode:
6894     case V4SImode:
6895     case V2DImode:
6896     case V4SFmode:
6897     case V2DFmode:
6898       if (!type || !AGGREGATE_TYPE_P (type))
6899         {
6900           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
6901             {
6902               warnedsse = true;
6903               warning (0, "SSE vector argument without SSE enabled "
6904                        "changes the ABI");
6905             }
6906           if (cum->sse_nregs)
6907             return gen_reg_or_parallel (mode, orig_mode,
6908                                         cum->sse_regno + FIRST_SSE_REG);
6909         }
6910       break;
6911
6912     case OImode:
6913       /* OImode shouldn't be used directly.  */
6914       gcc_unreachable ();
6915
6916     case V8SFmode:
6917     case V8SImode:
6918     case V32QImode:
6919     case V16HImode:
6920     case V4DFmode:
6921     case V4DImode:
6922       if (!type || !AGGREGATE_TYPE_P (type))
6923         {
6924           if (cum->sse_nregs)
6925             return gen_reg_or_parallel (mode, orig_mode,
6926                                         cum->sse_regno + FIRST_SSE_REG);
6927         }
6928       break;
6929
6930     case V8QImode:
6931     case V4HImode:
6932     case V2SImode:
6933     case V2SFmode:
6934     case V1TImode:
6935     case V1DImode:
6936       if (!type || !AGGREGATE_TYPE_P (type))
6937         {
6938           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6939             {
6940               warnedmmx = true;
6941               warning (0, "MMX vector argument without MMX enabled "
6942                        "changes the ABI");
6943             }
6944           if (cum->mmx_nregs)
6945             return gen_reg_or_parallel (mode, orig_mode,
6946                                         cum->mmx_regno + FIRST_MMX_REG);
6947         }
6948       break;
6949     }
6950
6951   return NULL_RTX;
6952 }
6953
6954 static rtx
6955 function_arg_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6956                  enum machine_mode orig_mode, const_tree type, bool named)
6957 {
6958   /* Handle a hidden AL argument containing number of registers
6959      for varargs x86-64 functions.  */
6960   if (mode == VOIDmode)
6961     return GEN_INT (cum->maybe_vaarg
6962                     ? (cum->sse_nregs < 0
6963                        ? X86_64_SSE_REGPARM_MAX
6964                        : cum->sse_regno)
6965                     : -1);
6966
6967   switch (mode)
6968     {
6969     default:
6970       break;
6971
6972     case V8SFmode:
6973     case V8SImode:
6974     case V32QImode:
6975     case V16HImode:
6976     case V4DFmode:
6977     case V4DImode:
6978       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6979       if (!named)
6980         return NULL;
6981       break;
6982     }
6983
6984   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6985                               cum->sse_nregs,
6986                               &x86_64_int_parameter_registers [cum->regno],
6987                               cum->sse_regno);
6988 }
6989
6990 static rtx
6991 function_arg_ms_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6992                     enum machine_mode orig_mode, bool named,
6993                     HOST_WIDE_INT bytes)
6994 {
6995   unsigned int regno;
6996
6997   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6998      We use value of -2 to specify that current function call is MSABI.  */
6999   if (mode == VOIDmode)
7000     return GEN_INT (-2);
7001
7002   /* If we've run out of registers, it goes on the stack.  */
7003   if (cum->nregs == 0)
7004     return NULL_RTX;
7005
7006   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
7007
7008   /* Only floating point modes are passed in anything but integer regs.  */
7009   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
7010     {
7011       if (named)
7012         regno = cum->regno + FIRST_SSE_REG;
7013       else
7014         {
7015           rtx t1, t2;
7016
7017           /* Unnamed floating parameters are passed in both the
7018              SSE and integer registers.  */
7019           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
7020           t2 = gen_rtx_REG (mode, regno);
7021           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
7022           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
7023           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
7024         }
7025     }
7026   /* Handle aggregated types passed in register.  */
7027   if (orig_mode == BLKmode)
7028     {
7029       if (bytes > 0 && bytes <= 8)
7030         mode = (bytes > 4 ? DImode : SImode);
7031       if (mode == BLKmode)
7032         mode = DImode;
7033     }
7034
7035   return gen_reg_or_parallel (mode, orig_mode, regno);
7036 }
7037
7038 /* Return where to put the arguments to a function.
7039    Return zero to push the argument on the stack, or a hard register in which to store the argument.
7040
7041    MODE is the argument's machine mode.  TYPE is the data type of the
7042    argument.  It is null for libcalls where that information may not be
7043    available.  CUM gives information about the preceding args and about
7044    the function being called.  NAMED is nonzero if this argument is a
7045    named parameter (otherwise it is an extra parameter matching an
7046    ellipsis).  */
7047
7048 static rtx
7049 ix86_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
7050                    const_tree type, bool named)
7051 {
7052   enum machine_mode mode = omode;
7053   HOST_WIDE_INT bytes, words;
7054   rtx arg;
7055
7056   if (mode == BLKmode)
7057     bytes = int_size_in_bytes (type);
7058   else
7059     bytes = GET_MODE_SIZE (mode);
7060   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7061
7062   /* To simplify the code below, represent vector types with a vector mode
7063      even if MMX/SSE are not active.  */
7064   if (type && TREE_CODE (type) == VECTOR_TYPE)
7065     mode = type_natural_mode (type, cum);
7066
7067   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
7068     arg = function_arg_ms_64 (cum, mode, omode, named, bytes);
7069   else if (TARGET_64BIT)
7070     arg = function_arg_64 (cum, mode, omode, type, named);
7071   else
7072     arg = function_arg_32 (cum, mode, omode, type, bytes, words);
7073
7074   if (TARGET_VZEROUPPER && function_pass_avx256_p (arg))
7075     {
7076       /* This argument uses 256bit AVX modes.  */
7077       if (cum->caller)
7078         cfun->machine->callee_pass_avx256_p = true;
7079       else
7080         cfun->machine->caller_pass_avx256_p = true;
7081     }
7082
7083   return arg;
7084 }
7085
7086 /* A C expression that indicates when an argument must be passed by
7087    reference.  If nonzero for an argument, a copy of that argument is
7088    made in memory and a pointer to the argument is passed instead of
7089    the argument itself.  The pointer is passed in whatever way is
7090    appropriate for passing a pointer to that type.  */
7091
7092 static bool
7093 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
7094                         enum machine_mode mode ATTRIBUTE_UNUSED,
7095                         const_tree type, bool named ATTRIBUTE_UNUSED)
7096 {
7097   /* See Windows x64 Software Convention.  */
7098   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
7099     {
7100       int msize = (int) GET_MODE_SIZE (mode);
7101       if (type)
7102         {
7103           /* Arrays are passed by reference.  */
7104           if (TREE_CODE (type) == ARRAY_TYPE)
7105             return true;
7106
7107           if (AGGREGATE_TYPE_P (type))
7108             {
7109               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
7110                  are passed by reference.  */
7111               msize = int_size_in_bytes (type);
7112             }
7113         }
7114
7115       /* __m128 is passed by reference.  */
7116       switch (msize) {
7117       case 1: case 2: case 4: case 8:
7118         break;
7119       default:
7120         return true;
7121       }
7122     }
7123   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
7124     return 1;
7125
7126   return 0;
7127 }
7128
7129 /* Return true when TYPE should be 128bit aligned for 32bit argument
7130    passing ABI.  XXX: This function is obsolete and is only used for
7131    checking psABI compatibility with previous versions of GCC.  */
7132
7133 static bool
7134 ix86_compat_aligned_value_p (const_tree type)
7135 {
7136   enum machine_mode mode = TYPE_MODE (type);
7137   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
7138        || mode == TDmode
7139        || mode == TFmode
7140        || mode == TCmode)
7141       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
7142     return true;
7143   if (TYPE_ALIGN (type) < 128)
7144     return false;
7145
7146   if (AGGREGATE_TYPE_P (type))
7147     {
7148       /* Walk the aggregates recursively.  */
7149       switch (TREE_CODE (type))
7150         {
7151         case RECORD_TYPE:
7152         case UNION_TYPE:
7153         case QUAL_UNION_TYPE:
7154           {
7155             tree field;
7156
7157             /* Walk all the structure fields.  */
7158             for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7159               {
7160                 if (TREE_CODE (field) == FIELD_DECL
7161                     && ix86_compat_aligned_value_p (TREE_TYPE (field)))
7162                   return true;
7163               }
7164             break;
7165           }
7166
7167         case ARRAY_TYPE:
7168           /* Just for use if some languages passes arrays by value.  */
7169           if (ix86_compat_aligned_value_p (TREE_TYPE (type)))
7170             return true;
7171           break;
7172
7173         default:
7174           gcc_unreachable ();
7175         }
7176     }
7177   return false;
7178 }
7179
7180 /* Return the alignment boundary for MODE and TYPE with alignment ALIGN.
7181    XXX: This function is obsolete and is only used for checking psABI
7182    compatibility with previous versions of GCC.  */
7183
7184 static unsigned int
7185 ix86_compat_function_arg_boundary (enum machine_mode mode,
7186                                    const_tree type, unsigned int align)
7187 {
7188   /* In 32bit, only _Decimal128 and __float128 are aligned to their
7189      natural boundaries.  */
7190   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
7191     {
7192       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
7193          make an exception for SSE modes since these require 128bit
7194          alignment.
7195
7196          The handling here differs from field_alignment.  ICC aligns MMX
7197          arguments to 4 byte boundaries, while structure fields are aligned
7198          to 8 byte boundaries.  */
7199       if (!type)
7200         {
7201           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
7202             align = PARM_BOUNDARY;
7203         }
7204       else
7205         {
7206           if (!ix86_compat_aligned_value_p (type))
7207             align = PARM_BOUNDARY;
7208         }
7209     }
7210   if (align > BIGGEST_ALIGNMENT)
7211     align = BIGGEST_ALIGNMENT;
7212   return align;
7213 }
7214
7215 /* Return true when TYPE should be 128bit aligned for 32bit argument
7216    passing ABI.  */
7217
7218 static bool
7219 ix86_contains_aligned_value_p (const_tree type)
7220 {
7221   enum machine_mode mode = TYPE_MODE (type);
7222
7223   if (mode == XFmode || mode == XCmode)
7224     return false;
7225
7226   if (TYPE_ALIGN (type) < 128)
7227     return false;
7228
7229   if (AGGREGATE_TYPE_P (type))
7230     {
7231       /* Walk the aggregates recursively.  */
7232       switch (TREE_CODE (type))
7233         {
7234         case RECORD_TYPE:
7235         case UNION_TYPE:
7236         case QUAL_UNION_TYPE:
7237           {
7238             tree field;
7239
7240             /* Walk all the structure fields.  */
7241             for (field = TYPE_FIELDS (type);
7242                  field;
7243                  field = DECL_CHAIN (field))
7244               {
7245                 if (TREE_CODE (field) == FIELD_DECL
7246                     && ix86_contains_aligned_value_p (TREE_TYPE (field)))
7247                   return true;
7248               }
7249             break;
7250           }
7251
7252         case ARRAY_TYPE:
7253           /* Just for use if some languages passes arrays by value.  */
7254           if (ix86_contains_aligned_value_p (TREE_TYPE (type)))
7255             return true;
7256           break;
7257
7258         default:
7259           gcc_unreachable ();
7260         }
7261     }
7262   else
7263     return TYPE_ALIGN (type) >= 128;
7264
7265   return false;
7266 }
7267
7268 /* Gives the alignment boundary, in bits, of an argument with the
7269    specified mode and type.  */
7270
7271 static unsigned int
7272 ix86_function_arg_boundary (enum machine_mode mode, const_tree type)
7273 {
7274   unsigned int align;
7275   if (type)
7276     {
7277       /* Since the main variant type is used for call, we convert it to
7278          the main variant type.  */
7279       type = TYPE_MAIN_VARIANT (type);
7280       align = TYPE_ALIGN (type);
7281     }
7282   else
7283     align = GET_MODE_ALIGNMENT (mode);
7284   if (align < PARM_BOUNDARY)
7285     align = PARM_BOUNDARY;
7286   else
7287     {
7288       static bool warned;
7289       unsigned int saved_align = align;
7290
7291       if (!TARGET_64BIT)
7292         {
7293           /* i386 ABI defines XFmode arguments to be 4 byte aligned.  */
7294           if (!type)
7295             {
7296               if (mode == XFmode || mode == XCmode)
7297                 align = PARM_BOUNDARY;
7298             }
7299           else if (!ix86_contains_aligned_value_p (type))
7300             align = PARM_BOUNDARY;
7301
7302           if (align < 128)
7303             align = PARM_BOUNDARY;
7304         }
7305
7306       if (warn_psabi
7307           && !warned
7308           && align != ix86_compat_function_arg_boundary (mode, type,
7309                                                          saved_align))
7310         {
7311           warned = true;
7312           inform (input_location,
7313                   "The ABI for passing parameters with %d-byte"
7314                   " alignment has changed in GCC 4.6",
7315                   align / BITS_PER_UNIT);
7316         }
7317     }
7318
7319   return align;
7320 }
7321
7322 /* Return true if N is a possible register number of function value.  */
7323
7324 static bool
7325 ix86_function_value_regno_p (const unsigned int regno)
7326 {
7327   switch (regno)
7328     {
7329     case 0:
7330       return true;
7331
7332     case FIRST_FLOAT_REG:
7333       /* TODO: The function should depend on current function ABI but
7334        builtins.c would need updating then. Therefore we use the
7335        default ABI.  */
7336       if (TARGET_64BIT && ix86_abi == MS_ABI)
7337         return false;
7338       return TARGET_FLOAT_RETURNS_IN_80387;
7339
7340     case FIRST_SSE_REG:
7341       return TARGET_SSE;
7342
7343     case FIRST_MMX_REG:
7344       if (TARGET_MACHO || TARGET_64BIT)
7345         return false;
7346       return TARGET_MMX;
7347     }
7348
7349   return false;
7350 }
7351
7352 /* Define how to find the value returned by a function.
7353    VALTYPE is the data type of the value (as a tree).
7354    If the precise function being called is known, FUNC is its FUNCTION_DECL;
7355    otherwise, FUNC is 0.  */
7356
7357 static rtx
7358 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
7359                    const_tree fntype, const_tree fn)
7360 {
7361   unsigned int regno;
7362
7363   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
7364      we normally prevent this case when mmx is not available.  However
7365      some ABIs may require the result to be returned like DImode.  */
7366   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7367     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
7368
7369   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
7370      we prevent this case when sse is not available.  However some ABIs
7371      may require the result to be returned like integer TImode.  */
7372   else if (mode == TImode
7373            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7374     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
7375
7376   /* 32-byte vector modes in %ymm0.   */
7377   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
7378     regno = TARGET_AVX ? FIRST_SSE_REG : 0;
7379
7380   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
7381   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
7382     regno = FIRST_FLOAT_REG;
7383   else
7384     /* Most things go in %eax.  */
7385     regno = AX_REG;
7386
7387   /* Override FP return register with %xmm0 for local functions when
7388      SSE math is enabled or for functions with sseregparm attribute.  */
7389   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
7390     {
7391       int sse_level = ix86_function_sseregparm (fntype, fn, false);
7392       if ((sse_level >= 1 && mode == SFmode)
7393           || (sse_level == 2 && mode == DFmode))
7394         regno = FIRST_SSE_REG;
7395     }
7396
7397   /* OImode shouldn't be used directly.  */
7398   gcc_assert (mode != OImode);
7399
7400   return gen_rtx_REG (orig_mode, regno);
7401 }
7402
7403 static rtx
7404 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
7405                    const_tree valtype)
7406 {
7407   rtx ret;
7408
7409   /* Handle libcalls, which don't provide a type node.  */
7410   if (valtype == NULL)
7411     {
7412       switch (mode)
7413         {
7414         case SFmode:
7415         case SCmode:
7416         case DFmode:
7417         case DCmode:
7418         case TFmode:
7419         case SDmode:
7420         case DDmode:
7421         case TDmode:
7422           return gen_rtx_REG (mode, FIRST_SSE_REG);
7423         case XFmode:
7424         case XCmode:
7425           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
7426         case TCmode:
7427           return NULL;
7428         default:
7429           return gen_rtx_REG (mode, AX_REG);
7430         }
7431     }
7432
7433   ret = construct_container (mode, orig_mode, valtype, 1,
7434                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
7435                              x86_64_int_return_registers, 0);
7436
7437   /* For zero sized structures, construct_container returns NULL, but we
7438      need to keep rest of compiler happy by returning meaningful value.  */
7439   if (!ret)
7440     ret = gen_rtx_REG (orig_mode, AX_REG);
7441
7442   return ret;
7443 }
7444
7445 static rtx
7446 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
7447 {
7448   unsigned int regno = AX_REG;
7449
7450   if (TARGET_SSE)
7451     {
7452       switch (GET_MODE_SIZE (mode))
7453         {
7454         case 16:
7455           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7456              && !COMPLEX_MODE_P (mode))
7457             regno = FIRST_SSE_REG;
7458           break;
7459         case 8:
7460         case 4:
7461           if (mode == SFmode || mode == DFmode)
7462             regno = FIRST_SSE_REG;
7463           break;
7464         default:
7465           break;
7466         }
7467     }
7468   return gen_rtx_REG (orig_mode, regno);
7469 }
7470
7471 static rtx
7472 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
7473                        enum machine_mode orig_mode, enum machine_mode mode)
7474 {
7475   const_tree fn, fntype;
7476
7477   fn = NULL_TREE;
7478   if (fntype_or_decl && DECL_P (fntype_or_decl))
7479     fn = fntype_or_decl;
7480   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
7481
7482   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
7483     return function_value_ms_64 (orig_mode, mode);
7484   else if (TARGET_64BIT)
7485     return function_value_64 (orig_mode, mode, valtype);
7486   else
7487     return function_value_32 (orig_mode, mode, fntype, fn);
7488 }
7489
7490 static rtx
7491 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
7492                      bool outgoing ATTRIBUTE_UNUSED)
7493 {
7494   enum machine_mode mode, orig_mode;
7495
7496   orig_mode = TYPE_MODE (valtype);
7497   mode = type_natural_mode (valtype, NULL);
7498   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
7499 }
7500
7501 rtx
7502 ix86_libcall_value (enum machine_mode mode)
7503 {
7504   return ix86_function_value_1 (NULL, NULL, mode, mode);
7505 }
7506
7507 /* Return true iff type is returned in memory.  */
7508
7509 static bool ATTRIBUTE_UNUSED
7510 return_in_memory_32 (const_tree type, enum machine_mode mode)
7511 {
7512   HOST_WIDE_INT size;
7513
7514   if (mode == BLKmode)
7515     return true;
7516
7517   size = int_size_in_bytes (type);
7518
7519   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
7520     return false;
7521
7522   if (VECTOR_MODE_P (mode) || mode == TImode)
7523     {
7524       /* User-created vectors small enough to fit in EAX.  */
7525       if (size < 8)
7526         return false;
7527
7528       /* MMX/3dNow values are returned in MM0,
7529          except when it doesn't exits or the ABI prescribes otherwise.  */
7530       if (size == 8)
7531         return !TARGET_MMX || TARGET_VECT8_RETURNS;
7532
7533       /* SSE values are returned in XMM0, except when it doesn't exist.  */
7534       if (size == 16)
7535         return !TARGET_SSE;
7536
7537       /* AVX values are returned in YMM0, except when it doesn't exist.  */
7538       if (size == 32)
7539         return !TARGET_AVX;
7540     }
7541
7542   if (mode == XFmode)
7543     return false;
7544
7545   if (size > 12)
7546     return true;
7547
7548   /* OImode shouldn't be used directly.  */
7549   gcc_assert (mode != OImode);
7550
7551   return false;
7552 }
7553
7554 static bool ATTRIBUTE_UNUSED
7555 return_in_memory_64 (const_tree type, enum machine_mode mode)
7556 {
7557   int needed_intregs, needed_sseregs;
7558   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
7559 }
7560
7561 static bool ATTRIBUTE_UNUSED
7562 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
7563 {
7564   HOST_WIDE_INT size = int_size_in_bytes (type);
7565
7566   /* __m128 is returned in xmm0.  */
7567   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7568       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
7569     return false;
7570
7571   /* Otherwise, the size must be exactly in [1248]. */
7572   return size != 1 && size != 2 && size != 4 && size != 8;
7573 }
7574
7575 static bool
7576 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7577 {
7578 #ifdef SUBTARGET_RETURN_IN_MEMORY
7579   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
7580 #else
7581   const enum machine_mode mode = type_natural_mode (type, NULL);
7582
7583   if (TARGET_64BIT)
7584     {
7585       if (ix86_function_type_abi (fntype) == MS_ABI)
7586         return return_in_memory_ms_64 (type, mode);
7587       else
7588         return return_in_memory_64 (type, mode);
7589     }
7590   else
7591     return return_in_memory_32 (type, mode);
7592 #endif
7593 }
7594
7595 /* When returning SSE vector types, we have a choice of either
7596      (1) being abi incompatible with a -march switch, or
7597      (2) generating an error.
7598    Given no good solution, I think the safest thing is one warning.
7599    The user won't be able to use -Werror, but....
7600
7601    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
7602    called in response to actually generating a caller or callee that
7603    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
7604    via aggregate_value_p for general type probing from tree-ssa.  */
7605
7606 static rtx
7607 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
7608 {
7609   static bool warnedsse, warnedmmx;
7610
7611   if (!TARGET_64BIT && type)
7612     {
7613       /* Look at the return type of the function, not the function type.  */
7614       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
7615
7616       if (!TARGET_SSE && !warnedsse)
7617         {
7618           if (mode == TImode
7619               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7620             {
7621               warnedsse = true;
7622               warning (0, "SSE vector return without SSE enabled "
7623                        "changes the ABI");
7624             }
7625         }
7626
7627       if (!TARGET_MMX && !warnedmmx)
7628         {
7629           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7630             {
7631               warnedmmx = true;
7632               warning (0, "MMX vector return without MMX enabled "
7633                        "changes the ABI");
7634             }
7635         }
7636     }
7637
7638   return NULL;
7639 }
7640
7641 \f
7642 /* Create the va_list data type.  */
7643
7644 /* Returns the calling convention specific va_list date type.
7645    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
7646
7647 static tree
7648 ix86_build_builtin_va_list_abi (enum calling_abi abi)
7649 {
7650   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
7651
7652   /* For i386 we use plain pointer to argument area.  */
7653   if (!TARGET_64BIT || abi == MS_ABI)
7654     return build_pointer_type (char_type_node);
7655
7656   record = lang_hooks.types.make_type (RECORD_TYPE);
7657   type_decl = build_decl (BUILTINS_LOCATION,
7658                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
7659
7660   f_gpr = build_decl (BUILTINS_LOCATION,
7661                       FIELD_DECL, get_identifier ("gp_offset"),
7662                       unsigned_type_node);
7663   f_fpr = build_decl (BUILTINS_LOCATION,
7664                       FIELD_DECL, get_identifier ("fp_offset"),
7665                       unsigned_type_node);
7666   f_ovf = build_decl (BUILTINS_LOCATION,
7667                       FIELD_DECL, get_identifier ("overflow_arg_area"),
7668                       ptr_type_node);
7669   f_sav = build_decl (BUILTINS_LOCATION,
7670                       FIELD_DECL, get_identifier ("reg_save_area"),
7671                       ptr_type_node);
7672
7673   va_list_gpr_counter_field = f_gpr;
7674   va_list_fpr_counter_field = f_fpr;
7675
7676   DECL_FIELD_CONTEXT (f_gpr) = record;
7677   DECL_FIELD_CONTEXT (f_fpr) = record;
7678   DECL_FIELD_CONTEXT (f_ovf) = record;
7679   DECL_FIELD_CONTEXT (f_sav) = record;
7680
7681   TYPE_STUB_DECL (record) = type_decl;
7682   TYPE_NAME (record) = type_decl;
7683   TYPE_FIELDS (record) = f_gpr;
7684   DECL_CHAIN (f_gpr) = f_fpr;
7685   DECL_CHAIN (f_fpr) = f_ovf;
7686   DECL_CHAIN (f_ovf) = f_sav;
7687
7688   layout_type (record);
7689
7690   /* The correct type is an array type of one element.  */
7691   return build_array_type (record, build_index_type (size_zero_node));
7692 }
7693
7694 /* Setup the builtin va_list data type and for 64-bit the additional
7695    calling convention specific va_list data types.  */
7696
7697 static tree
7698 ix86_build_builtin_va_list (void)
7699 {
7700   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
7701
7702   /* Initialize abi specific va_list builtin types.  */
7703   if (TARGET_64BIT)
7704     {
7705       tree t;
7706       if (ix86_abi == MS_ABI)
7707         {
7708           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
7709           if (TREE_CODE (t) != RECORD_TYPE)
7710             t = build_variant_type_copy (t);
7711           sysv_va_list_type_node = t;
7712         }
7713       else
7714         {
7715           t = ret;
7716           if (TREE_CODE (t) != RECORD_TYPE)
7717             t = build_variant_type_copy (t);
7718           sysv_va_list_type_node = t;
7719         }
7720       if (ix86_abi != MS_ABI)
7721         {
7722           t = ix86_build_builtin_va_list_abi (MS_ABI);
7723           if (TREE_CODE (t) != RECORD_TYPE)
7724             t = build_variant_type_copy (t);
7725           ms_va_list_type_node = t;
7726         }
7727       else
7728         {
7729           t = ret;
7730           if (TREE_CODE (t) != RECORD_TYPE)
7731             t = build_variant_type_copy (t);
7732           ms_va_list_type_node = t;
7733         }
7734     }
7735
7736   return ret;
7737 }
7738
7739 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
7740
7741 static void
7742 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
7743 {
7744   rtx save_area, mem;
7745   alias_set_type set;
7746   int i, max;
7747
7748   /* GPR size of varargs save area.  */
7749   if (cfun->va_list_gpr_size)
7750     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
7751   else
7752     ix86_varargs_gpr_size = 0;
7753
7754   /* FPR size of varargs save area.  We don't need it if we don't pass
7755      anything in SSE registers.  */
7756   if (TARGET_SSE && cfun->va_list_fpr_size)
7757     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
7758   else
7759     ix86_varargs_fpr_size = 0;
7760
7761   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
7762     return;
7763
7764   save_area = frame_pointer_rtx;
7765   set = get_varargs_alias_set ();
7766
7767   max = cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
7768   if (max > X86_64_REGPARM_MAX)
7769     max = X86_64_REGPARM_MAX;
7770
7771   for (i = cum->regno; i < max; i++)
7772     {
7773       mem = gen_rtx_MEM (Pmode,
7774                          plus_constant (save_area, i * UNITS_PER_WORD));
7775       MEM_NOTRAP_P (mem) = 1;
7776       set_mem_alias_set (mem, set);
7777       emit_move_insn (mem, gen_rtx_REG (Pmode,
7778                                         x86_64_int_parameter_registers[i]));
7779     }
7780
7781   if (ix86_varargs_fpr_size)
7782     {
7783       enum machine_mode smode;
7784       rtx label, test;
7785
7786       /* Now emit code to save SSE registers.  The AX parameter contains number
7787          of SSE parameter registers used to call this function, though all we
7788          actually check here is the zero/non-zero status.  */
7789
7790       label = gen_label_rtx ();
7791       test = gen_rtx_EQ (VOIDmode, gen_rtx_REG (QImode, AX_REG), const0_rtx);
7792       emit_jump_insn (gen_cbranchqi4 (test, XEXP (test, 0), XEXP (test, 1),
7793                                       label));
7794
7795       /* ??? If !TARGET_SSE_TYPELESS_STORES, would we perform better if
7796          we used movdqa (i.e. TImode) instead?  Perhaps even better would
7797          be if we could determine the real mode of the data, via a hook
7798          into pass_stdarg.  Ignore all that for now.  */
7799       smode = V4SFmode;
7800       if (crtl->stack_alignment_needed < GET_MODE_ALIGNMENT (smode))
7801         crtl->stack_alignment_needed = GET_MODE_ALIGNMENT (smode);
7802
7803       max = cum->sse_regno + cfun->va_list_fpr_size / 16;
7804       if (max > X86_64_SSE_REGPARM_MAX)
7805         max = X86_64_SSE_REGPARM_MAX;
7806
7807       for (i = cum->sse_regno; i < max; ++i)
7808         {
7809           mem = plus_constant (save_area, i * 16 + ix86_varargs_gpr_size);
7810           mem = gen_rtx_MEM (smode, mem);
7811           MEM_NOTRAP_P (mem) = 1;
7812           set_mem_alias_set (mem, set);
7813           set_mem_align (mem, GET_MODE_ALIGNMENT (smode));
7814
7815           emit_move_insn (mem, gen_rtx_REG (smode, SSE_REGNO (i)));
7816         }
7817
7818       emit_label (label);
7819     }
7820 }
7821
7822 static void
7823 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
7824 {
7825   alias_set_type set = get_varargs_alias_set ();
7826   int i;
7827
7828   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
7829     {
7830       rtx reg, mem;
7831
7832       mem = gen_rtx_MEM (Pmode,
7833                          plus_constant (virtual_incoming_args_rtx,
7834                                         i * UNITS_PER_WORD));
7835       MEM_NOTRAP_P (mem) = 1;
7836       set_mem_alias_set (mem, set);
7837
7838       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
7839       emit_move_insn (mem, reg);
7840     }
7841 }
7842
7843 static void
7844 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7845                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
7846                              int no_rtl)
7847 {
7848   CUMULATIVE_ARGS next_cum;
7849   tree fntype;
7850
7851   /* This argument doesn't appear to be used anymore.  Which is good,
7852      because the old code here didn't suppress rtl generation.  */
7853   gcc_assert (!no_rtl);
7854
7855   if (!TARGET_64BIT)
7856     return;
7857
7858   fntype = TREE_TYPE (current_function_decl);
7859
7860   /* For varargs, we do not want to skip the dummy va_dcl argument.
7861      For stdargs, we do want to skip the last named argument.  */
7862   next_cum = *cum;
7863   if (stdarg_p (fntype))
7864     ix86_function_arg_advance (&next_cum, mode, type, true);
7865
7866   if (cum->call_abi == MS_ABI)
7867     setup_incoming_varargs_ms_64 (&next_cum);
7868   else
7869     setup_incoming_varargs_64 (&next_cum);
7870 }
7871
7872 /* Checks if TYPE is of kind va_list char *.  */
7873
7874 static bool
7875 is_va_list_char_pointer (tree type)
7876 {
7877   tree canonic;
7878
7879   /* For 32-bit it is always true.  */
7880   if (!TARGET_64BIT)
7881     return true;
7882   canonic = ix86_canonical_va_list_type (type);
7883   return (canonic == ms_va_list_type_node
7884           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
7885 }
7886
7887 /* Implement va_start.  */
7888
7889 static void
7890 ix86_va_start (tree valist, rtx nextarg)
7891 {
7892   HOST_WIDE_INT words, n_gpr, n_fpr;
7893   tree f_gpr, f_fpr, f_ovf, f_sav;
7894   tree gpr, fpr, ovf, sav, t;
7895   tree type;
7896   rtx ovf_rtx;
7897
7898   if (flag_split_stack
7899       && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7900     {
7901       unsigned int scratch_regno;
7902
7903       /* When we are splitting the stack, we can't refer to the stack
7904          arguments using internal_arg_pointer, because they may be on
7905          the old stack.  The split stack prologue will arrange to
7906          leave a pointer to the old stack arguments in a scratch
7907          register, which we here copy to a pseudo-register.  The split
7908          stack prologue can't set the pseudo-register directly because
7909          it (the prologue) runs before any registers have been saved.  */
7910
7911       scratch_regno = split_stack_prologue_scratch_regno ();
7912       if (scratch_regno != INVALID_REGNUM)
7913         {
7914           rtx reg, seq;
7915
7916           reg = gen_reg_rtx (Pmode);
7917           cfun->machine->split_stack_varargs_pointer = reg;
7918
7919           start_sequence ();
7920           emit_move_insn (reg, gen_rtx_REG (Pmode, scratch_regno));
7921           seq = get_insns ();
7922           end_sequence ();
7923
7924           push_topmost_sequence ();
7925           emit_insn_after (seq, entry_of_function ());
7926           pop_topmost_sequence ();
7927         }
7928     }
7929
7930   /* Only 64bit target needs something special.  */
7931   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7932     {
7933       if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7934         std_expand_builtin_va_start (valist, nextarg);
7935       else
7936         {
7937           rtx va_r, next;
7938
7939           va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
7940           next = expand_binop (ptr_mode, add_optab,
7941                                cfun->machine->split_stack_varargs_pointer,
7942                                crtl->args.arg_offset_rtx,
7943                                NULL_RTX, 0, OPTAB_LIB_WIDEN);
7944           convert_move (va_r, next, 0);
7945         }
7946       return;
7947     }
7948
7949   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7950   f_fpr = DECL_CHAIN (f_gpr);
7951   f_ovf = DECL_CHAIN (f_fpr);
7952   f_sav = DECL_CHAIN (f_ovf);
7953
7954   valist = build_simple_mem_ref (valist);
7955   TREE_TYPE (valist) = TREE_TYPE (sysv_va_list_type_node);
7956   /* The following should be folded into the MEM_REF offset.  */
7957   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), unshare_expr (valist),
7958                 f_gpr, NULL_TREE);
7959   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
7960                 f_fpr, NULL_TREE);
7961   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
7962                 f_ovf, NULL_TREE);
7963   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
7964                 f_sav, NULL_TREE);
7965
7966   /* Count number of gp and fp argument registers used.  */
7967   words = crtl->args.info.words;
7968   n_gpr = crtl->args.info.regno;
7969   n_fpr = crtl->args.info.sse_regno;
7970
7971   if (cfun->va_list_gpr_size)
7972     {
7973       type = TREE_TYPE (gpr);
7974       t = build2 (MODIFY_EXPR, type,
7975                   gpr, build_int_cst (type, n_gpr * 8));
7976       TREE_SIDE_EFFECTS (t) = 1;
7977       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7978     }
7979
7980   if (TARGET_SSE && cfun->va_list_fpr_size)
7981     {
7982       type = TREE_TYPE (fpr);
7983       t = build2 (MODIFY_EXPR, type, fpr,
7984                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
7985       TREE_SIDE_EFFECTS (t) = 1;
7986       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7987     }
7988
7989   /* Find the overflow area.  */
7990   type = TREE_TYPE (ovf);
7991   if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7992     ovf_rtx = crtl->args.internal_arg_pointer;
7993   else
7994     ovf_rtx = cfun->machine->split_stack_varargs_pointer;
7995   t = make_tree (type, ovf_rtx);
7996   if (words != 0)
7997     t = build2 (POINTER_PLUS_EXPR, type, t,
7998                 size_int (words * UNITS_PER_WORD));
7999   t = build2 (MODIFY_EXPR, type, ovf, t);
8000   TREE_SIDE_EFFECTS (t) = 1;
8001   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8002
8003   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
8004     {
8005       /* Find the register save area.
8006          Prologue of the function save it right above stack frame.  */
8007       type = TREE_TYPE (sav);
8008       t = make_tree (type, frame_pointer_rtx);
8009       if (!ix86_varargs_gpr_size)
8010         t = build2 (POINTER_PLUS_EXPR, type, t,
8011                     size_int (-8 * X86_64_REGPARM_MAX));
8012       t = build2 (MODIFY_EXPR, type, sav, t);
8013       TREE_SIDE_EFFECTS (t) = 1;
8014       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
8015     }
8016 }
8017
8018 /* Implement va_arg.  */
8019
8020 static tree
8021 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
8022                       gimple_seq *post_p)
8023 {
8024   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
8025   tree f_gpr, f_fpr, f_ovf, f_sav;
8026   tree gpr, fpr, ovf, sav, t;
8027   int size, rsize;
8028   tree lab_false, lab_over = NULL_TREE;
8029   tree addr, t2;
8030   rtx container;
8031   int indirect_p = 0;
8032   tree ptrtype;
8033   enum machine_mode nat_mode;
8034   unsigned int arg_boundary;
8035
8036   /* Only 64bit target needs something special.  */
8037   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
8038     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
8039
8040   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
8041   f_fpr = DECL_CHAIN (f_gpr);
8042   f_ovf = DECL_CHAIN (f_fpr);
8043   f_sav = DECL_CHAIN (f_ovf);
8044
8045   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
8046                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
8047   valist = build_va_arg_indirect_ref (valist);
8048   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
8049   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
8050   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
8051
8052   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
8053   if (indirect_p)
8054     type = build_pointer_type (type);
8055   size = int_size_in_bytes (type);
8056   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
8057
8058   nat_mode = type_natural_mode (type, NULL);
8059   switch (nat_mode)
8060     {
8061     case V8SFmode:
8062     case V8SImode:
8063     case V32QImode:
8064     case V16HImode:
8065     case V4DFmode:
8066     case V4DImode:
8067       /* Unnamed 256bit vector mode parameters are passed on stack.  */
8068       if (ix86_cfun_abi () == SYSV_ABI)
8069         {
8070           container = NULL;
8071           break;
8072         }
8073
8074     default:
8075       container = construct_container (nat_mode, TYPE_MODE (type),
8076                                        type, 0, X86_64_REGPARM_MAX,
8077                                        X86_64_SSE_REGPARM_MAX, intreg,
8078                                        0);
8079       break;
8080     }
8081
8082   /* Pull the value out of the saved registers.  */
8083
8084   addr = create_tmp_var (ptr_type_node, "addr");
8085
8086   if (container)
8087     {
8088       int needed_intregs, needed_sseregs;
8089       bool need_temp;
8090       tree int_addr, sse_addr;
8091
8092       lab_false = create_artificial_label (UNKNOWN_LOCATION);
8093       lab_over = create_artificial_label (UNKNOWN_LOCATION);
8094
8095       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
8096
8097       need_temp = (!REG_P (container)
8098                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
8099                        || TYPE_ALIGN (type) > 128));
8100
8101       /* In case we are passing structure, verify that it is consecutive block
8102          on the register save area.  If not we need to do moves.  */
8103       if (!need_temp && !REG_P (container))
8104         {
8105           /* Verify that all registers are strictly consecutive  */
8106           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
8107             {
8108               int i;
8109
8110               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
8111                 {
8112                   rtx slot = XVECEXP (container, 0, i);
8113                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
8114                       || INTVAL (XEXP (slot, 1)) != i * 16)
8115                     need_temp = 1;
8116                 }
8117             }
8118           else
8119             {
8120               int i;
8121
8122               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
8123                 {
8124                   rtx slot = XVECEXP (container, 0, i);
8125                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
8126                       || INTVAL (XEXP (slot, 1)) != i * 8)
8127                     need_temp = 1;
8128                 }
8129             }
8130         }
8131       if (!need_temp)
8132         {
8133           int_addr = addr;
8134           sse_addr = addr;
8135         }
8136       else
8137         {
8138           int_addr = create_tmp_var (ptr_type_node, "int_addr");
8139           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
8140         }
8141
8142       /* First ensure that we fit completely in registers.  */
8143       if (needed_intregs)
8144         {
8145           t = build_int_cst (TREE_TYPE (gpr),
8146                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
8147           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
8148           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
8149           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
8150           gimplify_and_add (t, pre_p);
8151         }
8152       if (needed_sseregs)
8153         {
8154           t = build_int_cst (TREE_TYPE (fpr),
8155                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
8156                              + X86_64_REGPARM_MAX * 8);
8157           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
8158           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
8159           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
8160           gimplify_and_add (t, pre_p);
8161         }
8162
8163       /* Compute index to start of area used for integer regs.  */
8164       if (needed_intregs)
8165         {
8166           /* int_addr = gpr + sav; */
8167           t = fold_convert (sizetype, gpr);
8168           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
8169           gimplify_assign (int_addr, t, pre_p);
8170         }
8171       if (needed_sseregs)
8172         {
8173           /* sse_addr = fpr + sav; */
8174           t = fold_convert (sizetype, fpr);
8175           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
8176           gimplify_assign (sse_addr, t, pre_p);
8177         }
8178       if (need_temp)
8179         {
8180           int i, prev_size = 0;
8181           tree temp = create_tmp_var (type, "va_arg_tmp");
8182
8183           /* addr = &temp; */
8184           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
8185           gimplify_assign (addr, t, pre_p);
8186
8187           for (i = 0; i < XVECLEN (container, 0); i++)
8188             {
8189               rtx slot = XVECEXP (container, 0, i);
8190               rtx reg = XEXP (slot, 0);
8191               enum machine_mode mode = GET_MODE (reg);
8192               tree piece_type;
8193               tree addr_type;
8194               tree daddr_type;
8195               tree src_addr, src;
8196               int src_offset;
8197               tree dest_addr, dest;
8198               int cur_size = GET_MODE_SIZE (mode);
8199
8200               gcc_assert (prev_size <= INTVAL (XEXP (slot, 1)));
8201               prev_size = INTVAL (XEXP (slot, 1));
8202               if (prev_size + cur_size > size)
8203                 {
8204                   cur_size = size - prev_size;
8205                   mode = mode_for_size (cur_size * BITS_PER_UNIT, MODE_INT, 1);
8206                   if (mode == BLKmode)
8207                     mode = QImode;
8208                 }
8209               piece_type = lang_hooks.types.type_for_mode (mode, 1);
8210               if (mode == GET_MODE (reg))
8211                 addr_type = build_pointer_type (piece_type);
8212               else
8213                 addr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8214                                                          true);
8215               daddr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8216                                                         true);
8217
8218               if (SSE_REGNO_P (REGNO (reg)))
8219                 {
8220                   src_addr = sse_addr;
8221                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
8222                 }
8223               else
8224                 {
8225                   src_addr = int_addr;
8226                   src_offset = REGNO (reg) * 8;
8227                 }
8228               src_addr = fold_convert (addr_type, src_addr);
8229               src_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, src_addr,
8230                                       size_int (src_offset));
8231
8232               dest_addr = fold_convert (daddr_type, addr);
8233               dest_addr = fold_build2 (POINTER_PLUS_EXPR, daddr_type, dest_addr,
8234                                        size_int (prev_size));
8235               if (cur_size == GET_MODE_SIZE (mode))
8236                 {
8237                   src = build_va_arg_indirect_ref (src_addr);
8238                   dest = build_va_arg_indirect_ref (dest_addr);
8239
8240                   gimplify_assign (dest, src, pre_p);
8241                 }
8242               else
8243                 {
8244                   tree copy
8245                     = build_call_expr (implicit_built_in_decls[BUILT_IN_MEMCPY],
8246                                        3, dest_addr, src_addr,
8247                                        size_int (cur_size));
8248                   gimplify_and_add (copy, pre_p);
8249                 }
8250               prev_size += cur_size;
8251             }
8252         }
8253
8254       if (needed_intregs)
8255         {
8256           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
8257                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
8258           gimplify_assign (gpr, t, pre_p);
8259         }
8260
8261       if (needed_sseregs)
8262         {
8263           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
8264                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
8265           gimplify_assign (fpr, t, pre_p);
8266         }
8267
8268       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
8269
8270       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
8271     }
8272
8273   /* ... otherwise out of the overflow area.  */
8274
8275   /* When we align parameter on stack for caller, if the parameter
8276      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
8277      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
8278      here with caller.  */
8279   arg_boundary = ix86_function_arg_boundary (VOIDmode, type);
8280   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
8281     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
8282
8283   /* Care for on-stack alignment if needed.  */
8284   if (arg_boundary <= 64 || size == 0)
8285     t = ovf;
8286  else
8287     {
8288       HOST_WIDE_INT align = arg_boundary / 8;
8289       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), ovf,
8290                   size_int (align - 1));
8291       t = fold_convert (sizetype, t);
8292       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
8293                   size_int (-align));
8294       t = fold_convert (TREE_TYPE (ovf), t);
8295     }
8296
8297   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
8298   gimplify_assign (addr, t, pre_p);
8299
8300   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
8301               size_int (rsize * UNITS_PER_WORD));
8302   gimplify_assign (unshare_expr (ovf), t, pre_p);
8303
8304   if (container)
8305     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
8306
8307   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
8308   addr = fold_convert (ptrtype, addr);
8309
8310   if (indirect_p)
8311     addr = build_va_arg_indirect_ref (addr);
8312   return build_va_arg_indirect_ref (addr);
8313 }
8314 \f
8315 /* Return true if OPNUM's MEM should be matched
8316    in movabs* patterns.  */
8317
8318 bool
8319 ix86_check_movabs (rtx insn, int opnum)
8320 {
8321   rtx set, mem;
8322
8323   set = PATTERN (insn);
8324   if (GET_CODE (set) == PARALLEL)
8325     set = XVECEXP (set, 0, 0);
8326   gcc_assert (GET_CODE (set) == SET);
8327   mem = XEXP (set, opnum);
8328   while (GET_CODE (mem) == SUBREG)
8329     mem = SUBREG_REG (mem);
8330   gcc_assert (MEM_P (mem));
8331   return volatile_ok || !MEM_VOLATILE_P (mem);
8332 }
8333 \f
8334 /* Initialize the table of extra 80387 mathematical constants.  */
8335
8336 static void
8337 init_ext_80387_constants (void)
8338 {
8339   static const char * cst[5] =
8340   {
8341     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
8342     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
8343     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
8344     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
8345     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
8346   };
8347   int i;
8348
8349   for (i = 0; i < 5; i++)
8350     {
8351       real_from_string (&ext_80387_constants_table[i], cst[i]);
8352       /* Ensure each constant is rounded to XFmode precision.  */
8353       real_convert (&ext_80387_constants_table[i],
8354                     XFmode, &ext_80387_constants_table[i]);
8355     }
8356
8357   ext_80387_constants_init = 1;
8358 }
8359
8360 /* Return non-zero if the constant is something that
8361    can be loaded with a special instruction.  */
8362
8363 int
8364 standard_80387_constant_p (rtx x)
8365 {
8366   enum machine_mode mode = GET_MODE (x);
8367
8368   REAL_VALUE_TYPE r;
8369
8370   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
8371     return -1;
8372
8373   if (x == CONST0_RTX (mode))
8374     return 1;
8375   if (x == CONST1_RTX (mode))
8376     return 2;
8377
8378   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8379
8380   /* For XFmode constants, try to find a special 80387 instruction when
8381      optimizing for size or on those CPUs that benefit from them.  */
8382   if (mode == XFmode
8383       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
8384     {
8385       int i;
8386
8387       if (! ext_80387_constants_init)
8388         init_ext_80387_constants ();
8389
8390       for (i = 0; i < 5; i++)
8391         if (real_identical (&r, &ext_80387_constants_table[i]))
8392           return i + 3;
8393     }
8394
8395   /* Load of the constant -0.0 or -1.0 will be split as
8396      fldz;fchs or fld1;fchs sequence.  */
8397   if (real_isnegzero (&r))
8398     return 8;
8399   if (real_identical (&r, &dconstm1))
8400     return 9;
8401
8402   return 0;
8403 }
8404
8405 /* Return the opcode of the special instruction to be used to load
8406    the constant X.  */
8407
8408 const char *
8409 standard_80387_constant_opcode (rtx x)
8410 {
8411   switch (standard_80387_constant_p (x))
8412     {
8413     case 1:
8414       return "fldz";
8415     case 2:
8416       return "fld1";
8417     case 3:
8418       return "fldlg2";
8419     case 4:
8420       return "fldln2";
8421     case 5:
8422       return "fldl2e";
8423     case 6:
8424       return "fldl2t";
8425     case 7:
8426       return "fldpi";
8427     case 8:
8428     case 9:
8429       return "#";
8430     default:
8431       gcc_unreachable ();
8432     }
8433 }
8434
8435 /* Return the CONST_DOUBLE representing the 80387 constant that is
8436    loaded by the specified special instruction.  The argument IDX
8437    matches the return value from standard_80387_constant_p.  */
8438
8439 rtx
8440 standard_80387_constant_rtx (int idx)
8441 {
8442   int i;
8443
8444   if (! ext_80387_constants_init)
8445     init_ext_80387_constants ();
8446
8447   switch (idx)
8448     {
8449     case 3:
8450     case 4:
8451     case 5:
8452     case 6:
8453     case 7:
8454       i = idx - 3;
8455       break;
8456
8457     default:
8458       gcc_unreachable ();
8459     }
8460
8461   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
8462                                        XFmode);
8463 }
8464
8465 /* Return 1 if X is all 0s and 2 if x is all 1s
8466    in supported SSE vector mode.  */
8467
8468 int
8469 standard_sse_constant_p (rtx x)
8470 {
8471   enum machine_mode mode = GET_MODE (x);
8472
8473   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
8474     return 1;
8475   if (vector_all_ones_operand (x, mode))
8476     switch (mode)
8477       {
8478       case V16QImode:
8479       case V8HImode:
8480       case V4SImode:
8481       case V2DImode:
8482         if (TARGET_SSE2)
8483           return 2;
8484       default:
8485         break;
8486       }
8487
8488   return 0;
8489 }
8490
8491 /* Return the opcode of the special instruction to be used to load
8492    the constant X.  */
8493
8494 const char *
8495 standard_sse_constant_opcode (rtx insn, rtx x)
8496 {
8497   switch (standard_sse_constant_p (x))
8498     {
8499     case 1:
8500       switch (get_attr_mode (insn))
8501         {
8502         case MODE_V4SF:
8503           return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
8504         case MODE_V2DF:
8505           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8506             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
8507           else
8508             return TARGET_AVX ? "vxorpd\t%0, %0, %0" : "xorpd\t%0, %0";
8509         case MODE_TI:
8510           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8511             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
8512           else
8513             return TARGET_AVX ? "vpxor\t%0, %0, %0" : "pxor\t%0, %0";
8514         case MODE_V8SF:
8515           return "vxorps\t%x0, %x0, %x0";
8516         case MODE_V4DF:
8517           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8518             return "vxorps\t%x0, %x0, %x0";
8519           else
8520             return "vxorpd\t%x0, %x0, %x0";
8521         case MODE_OI:
8522           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8523             return "vxorps\t%x0, %x0, %x0";
8524           else
8525             return "vpxor\t%x0, %x0, %x0";
8526         default:
8527           break;
8528         }
8529     case 2:
8530       return TARGET_AVX ? "vpcmpeqd\t%0, %0, %0" : "pcmpeqd\t%0, %0";
8531     default:
8532       break;
8533     }
8534   gcc_unreachable ();
8535 }
8536
8537 /* Returns true if OP contains a symbol reference */
8538
8539 bool
8540 symbolic_reference_mentioned_p (rtx op)
8541 {
8542   const char *fmt;
8543   int i;
8544
8545   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
8546     return true;
8547
8548   fmt = GET_RTX_FORMAT (GET_CODE (op));
8549   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
8550     {
8551       if (fmt[i] == 'E')
8552         {
8553           int j;
8554
8555           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
8556             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
8557               return true;
8558         }
8559
8560       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
8561         return true;
8562     }
8563
8564   return false;
8565 }
8566
8567 /* Return true if it is appropriate to emit `ret' instructions in the
8568    body of a function.  Do this only if the epilogue is simple, needing a
8569    couple of insns.  Prior to reloading, we can't tell how many registers
8570    must be saved, so return false then.  Return false if there is no frame
8571    marker to de-allocate.  */
8572
8573 bool
8574 ix86_can_use_return_insn_p (void)
8575 {
8576   struct ix86_frame frame;
8577
8578   if (! reload_completed || frame_pointer_needed)
8579     return 0;
8580
8581   /* Don't allow more than 32k pop, since that's all we can do
8582      with one instruction.  */
8583   if (crtl->args.pops_args && crtl->args.size >= 32768)
8584     return 0;
8585
8586   ix86_compute_frame_layout (&frame);
8587   return (frame.stack_pointer_offset == UNITS_PER_WORD
8588           && (frame.nregs + frame.nsseregs) == 0);
8589 }
8590 \f
8591 /* Value should be nonzero if functions must have frame pointers.
8592    Zero means the frame pointer need not be set up (and parms may
8593    be accessed via the stack pointer) in functions that seem suitable.  */
8594
8595 static bool
8596 ix86_frame_pointer_required (void)
8597 {
8598   /* If we accessed previous frames, then the generated code expects
8599      to be able to access the saved ebp value in our frame.  */
8600   if (cfun->machine->accesses_prev_frame)
8601     return true;
8602
8603   /* Several x86 os'es need a frame pointer for other reasons,
8604      usually pertaining to setjmp.  */
8605   if (SUBTARGET_FRAME_POINTER_REQUIRED)
8606     return true;
8607
8608   /* In ix86_option_override_internal, TARGET_OMIT_LEAF_FRAME_POINTER
8609      turns off the frame pointer by default.  Turn it back on now if
8610      we've not got a leaf function.  */
8611   if (TARGET_OMIT_LEAF_FRAME_POINTER
8612       && (!current_function_is_leaf
8613           || ix86_current_function_calls_tls_descriptor))
8614     return true;
8615
8616   if (crtl->profile && !flag_fentry)
8617     return true;
8618
8619   return false;
8620 }
8621
8622 /* Record that the current function accesses previous call frames.  */
8623
8624 void
8625 ix86_setup_frame_addresses (void)
8626 {
8627   cfun->machine->accesses_prev_frame = 1;
8628 }
8629 \f
8630 #ifndef USE_HIDDEN_LINKONCE
8631 # if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
8632 #  define USE_HIDDEN_LINKONCE 1
8633 # else
8634 #  define USE_HIDDEN_LINKONCE 0
8635 # endif
8636 #endif
8637
8638 static int pic_labels_used;
8639
8640 /* Fills in the label name that should be used for a pc thunk for
8641    the given register.  */
8642
8643 static void
8644 get_pc_thunk_name (char name[32], unsigned int regno)
8645 {
8646   gcc_assert (!TARGET_64BIT);
8647
8648   if (USE_HIDDEN_LINKONCE)
8649     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
8650   else
8651     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
8652 }
8653
8654
8655 /* This function generates code for -fpic that loads %ebx with
8656    the return address of the caller and then returns.  */
8657
8658 static void
8659 ix86_code_end (void)
8660 {
8661   rtx xops[2];
8662   int regno;
8663
8664   for (regno = AX_REG; regno <= SP_REG; regno++)
8665     {
8666       char name[32];
8667       tree decl;
8668
8669       if (!(pic_labels_used & (1 << regno)))
8670         continue;
8671
8672       get_pc_thunk_name (name, regno);
8673
8674       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
8675                          get_identifier (name),
8676                          build_function_type (void_type_node, void_list_node));
8677       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
8678                                        NULL_TREE, void_type_node);
8679       TREE_PUBLIC (decl) = 1;
8680       TREE_STATIC (decl) = 1;
8681
8682 #if TARGET_MACHO
8683       if (TARGET_MACHO)
8684         {
8685           switch_to_section (darwin_sections[text_coal_section]);
8686           fputs ("\t.weak_definition\t", asm_out_file);
8687           assemble_name (asm_out_file, name);
8688           fputs ("\n\t.private_extern\t", asm_out_file);
8689           assemble_name (asm_out_file, name);
8690           putc ('\n', asm_out_file);
8691           ASM_OUTPUT_LABEL (asm_out_file, name);
8692           DECL_WEAK (decl) = 1;
8693         }
8694       else
8695 #endif
8696       if (USE_HIDDEN_LINKONCE)
8697         {
8698           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
8699
8700           targetm.asm_out.unique_section (decl, 0);
8701           switch_to_section (get_named_section (decl, NULL, 0));
8702
8703           targetm.asm_out.globalize_label (asm_out_file, name);
8704           fputs ("\t.hidden\t", asm_out_file);
8705           assemble_name (asm_out_file, name);
8706           putc ('\n', asm_out_file);
8707           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
8708         }
8709       else
8710         {
8711           switch_to_section (text_section);
8712           ASM_OUTPUT_LABEL (asm_out_file, name);
8713         }
8714
8715       DECL_INITIAL (decl) = make_node (BLOCK);
8716       current_function_decl = decl;
8717       init_function_start (decl);
8718       first_function_block_is_cold = false;
8719       /* Make sure unwind info is emitted for the thunk if needed.  */
8720       final_start_function (emit_barrier (), asm_out_file, 1);
8721
8722       /* Pad stack IP move with 4 instructions (two NOPs count
8723          as one instruction).  */
8724       if (TARGET_PAD_SHORT_FUNCTION)
8725         {
8726           int i = 8;
8727
8728           while (i--)
8729             fputs ("\tnop\n", asm_out_file);
8730         }
8731
8732       xops[0] = gen_rtx_REG (Pmode, regno);
8733       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8734       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
8735       fputs ("\tret\n", asm_out_file);
8736       final_end_function ();
8737       init_insn_lengths ();
8738       free_after_compilation (cfun);
8739       set_cfun (NULL);
8740       current_function_decl = NULL;
8741     }
8742
8743   if (flag_split_stack)
8744     file_end_indicate_split_stack ();
8745 }
8746
8747 /* Emit code for the SET_GOT patterns.  */
8748
8749 const char *
8750 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
8751 {
8752   rtx xops[3];
8753
8754   xops[0] = dest;
8755
8756   if (TARGET_VXWORKS_RTP && flag_pic)
8757     {
8758       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
8759       xops[2] = gen_rtx_MEM (Pmode,
8760                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
8761       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
8762
8763       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
8764          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
8765          an unadorned address.  */
8766       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
8767       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
8768       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
8769       return "";
8770     }
8771
8772   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
8773
8774   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
8775     {
8776       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
8777
8778       if (!flag_pic)
8779         output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
8780       else
8781         {
8782           output_asm_insn ("call\t%a2", xops);
8783 #ifdef DWARF2_UNWIND_INFO
8784           /* The call to next label acts as a push.  */
8785           if (dwarf2out_do_frame ())
8786             {
8787               rtx insn;
8788               start_sequence ();
8789               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8790                                              gen_rtx_PLUS (Pmode,
8791                                                            stack_pointer_rtx,
8792                                                            GEN_INT (-4))));
8793               RTX_FRAME_RELATED_P (insn) = 1;
8794               dwarf2out_frame_debug (insn, true);
8795               end_sequence ();
8796             }
8797 #endif
8798         }
8799
8800 #if TARGET_MACHO
8801       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8802          is what will be referenced by the Mach-O PIC subsystem.  */
8803       if (!label)
8804         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8805 #endif
8806
8807       targetm.asm_out.internal_label (asm_out_file, "L",
8808                                       CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
8809
8810       if (flag_pic)
8811         {
8812           output_asm_insn ("pop%z0\t%0", xops);
8813 #ifdef DWARF2_UNWIND_INFO
8814           /* The pop is a pop and clobbers dest, but doesn't restore it
8815              for unwind info purposes.  */
8816           if (dwarf2out_do_frame ())
8817             {
8818               rtx insn;
8819               start_sequence ();
8820               insn = emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
8821               dwarf2out_frame_debug (insn, true);
8822               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8823                                              gen_rtx_PLUS (Pmode,
8824                                                            stack_pointer_rtx,
8825                                                            GEN_INT (4))));
8826               RTX_FRAME_RELATED_P (insn) = 1;
8827               dwarf2out_frame_debug (insn, true);
8828               end_sequence ();
8829             }
8830 #endif
8831         }
8832     }
8833   else
8834     {
8835       char name[32];
8836       get_pc_thunk_name (name, REGNO (dest));
8837       pic_labels_used |= 1 << REGNO (dest);
8838
8839 #ifdef DWARF2_UNWIND_INFO
8840       /* Ensure all queued register saves are flushed before the
8841          call.  */
8842       if (dwarf2out_do_frame ())
8843         dwarf2out_flush_queued_reg_saves ();
8844 #endif
8845       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
8846       xops[2] = gen_rtx_MEM (QImode, xops[2]);
8847       output_asm_insn ("call\t%X2", xops);
8848       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8849          is what will be referenced by the Mach-O PIC subsystem.  */
8850 #if TARGET_MACHO
8851       if (!label)
8852         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8853       else
8854         targetm.asm_out.internal_label (asm_out_file, "L",
8855                                            CODE_LABEL_NUMBER (label));
8856 #endif
8857     }
8858
8859   if (TARGET_MACHO)
8860     return "";
8861
8862   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
8863     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
8864   else
8865     output_asm_insn ("add%z0\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
8866
8867   return "";
8868 }
8869
8870 /* Generate an "push" pattern for input ARG.  */
8871
8872 static rtx
8873 gen_push (rtx arg)
8874 {
8875   struct machine_function *m = cfun->machine;
8876
8877   if (m->fs.cfa_reg == stack_pointer_rtx)
8878     m->fs.cfa_offset += UNITS_PER_WORD;
8879   m->fs.sp_offset += UNITS_PER_WORD;
8880
8881   return gen_rtx_SET (VOIDmode,
8882                       gen_rtx_MEM (Pmode,
8883                                    gen_rtx_PRE_DEC (Pmode,
8884                                                     stack_pointer_rtx)),
8885                       arg);
8886 }
8887
8888 /* Generate an "pop" pattern for input ARG.  */
8889
8890 static rtx
8891 gen_pop (rtx arg)
8892 {
8893   return gen_rtx_SET (VOIDmode,
8894                       arg,
8895                       gen_rtx_MEM (Pmode,
8896                                    gen_rtx_POST_INC (Pmode,
8897                                                      stack_pointer_rtx)));
8898 }
8899
8900 /* Return >= 0 if there is an unused call-clobbered register available
8901    for the entire function.  */
8902
8903 static unsigned int
8904 ix86_select_alt_pic_regnum (void)
8905 {
8906   if (current_function_is_leaf
8907       && !crtl->profile
8908       && !ix86_current_function_calls_tls_descriptor)
8909     {
8910       int i, drap;
8911       /* Can't use the same register for both PIC and DRAP.  */
8912       if (crtl->drap_reg)
8913         drap = REGNO (crtl->drap_reg);
8914       else
8915         drap = -1;
8916       for (i = 2; i >= 0; --i)
8917         if (i != drap && !df_regs_ever_live_p (i))
8918           return i;
8919     }
8920
8921   return INVALID_REGNUM;
8922 }
8923
8924 /* Return 1 if we need to save REGNO.  */
8925 static int
8926 ix86_save_reg (unsigned int regno, int maybe_eh_return)
8927 {
8928   if (pic_offset_table_rtx
8929       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
8930       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8931           || crtl->profile
8932           || crtl->calls_eh_return
8933           || crtl->uses_const_pool))
8934     {
8935       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
8936         return 0;
8937       return 1;
8938     }
8939
8940   if (crtl->calls_eh_return && maybe_eh_return)
8941     {
8942       unsigned i;
8943       for (i = 0; ; i++)
8944         {
8945           unsigned test = EH_RETURN_DATA_REGNO (i);
8946           if (test == INVALID_REGNUM)
8947             break;
8948           if (test == regno)
8949             return 1;
8950         }
8951     }
8952
8953   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
8954     return 1;
8955
8956   return (df_regs_ever_live_p (regno)
8957           && !call_used_regs[regno]
8958           && !fixed_regs[regno]
8959           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
8960 }
8961
8962 /* Return number of saved general prupose registers.  */
8963
8964 static int
8965 ix86_nsaved_regs (void)
8966 {
8967   int nregs = 0;
8968   int regno;
8969
8970   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8971     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8972       nregs ++;
8973   return nregs;
8974 }
8975
8976 /* Return number of saved SSE registrers.  */
8977
8978 static int
8979 ix86_nsaved_sseregs (void)
8980 {
8981   int nregs = 0;
8982   int regno;
8983
8984   if (ix86_cfun_abi () != MS_ABI)
8985     return 0;
8986   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8987     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8988       nregs ++;
8989   return nregs;
8990 }
8991
8992 /* Given FROM and TO register numbers, say whether this elimination is
8993    allowed.  If stack alignment is needed, we can only replace argument
8994    pointer with hard frame pointer, or replace frame pointer with stack
8995    pointer.  Otherwise, frame pointer elimination is automatically
8996    handled and all other eliminations are valid.  */
8997
8998 static bool
8999 ix86_can_eliminate (const int from, const int to)
9000 {
9001   if (stack_realign_fp)
9002     return ((from == ARG_POINTER_REGNUM
9003              && to == HARD_FRAME_POINTER_REGNUM)
9004             || (from == FRAME_POINTER_REGNUM
9005                 && to == STACK_POINTER_REGNUM));
9006   else
9007     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
9008 }
9009
9010 /* Return the offset between two registers, one to be eliminated, and the other
9011    its replacement, at the start of a routine.  */
9012
9013 HOST_WIDE_INT
9014 ix86_initial_elimination_offset (int from, int to)
9015 {
9016   struct ix86_frame frame;
9017   ix86_compute_frame_layout (&frame);
9018
9019   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
9020     return frame.hard_frame_pointer_offset;
9021   else if (from == FRAME_POINTER_REGNUM
9022            && to == HARD_FRAME_POINTER_REGNUM)
9023     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
9024   else
9025     {
9026       gcc_assert (to == STACK_POINTER_REGNUM);
9027
9028       if (from == ARG_POINTER_REGNUM)
9029         return frame.stack_pointer_offset;
9030
9031       gcc_assert (from == FRAME_POINTER_REGNUM);
9032       return frame.stack_pointer_offset - frame.frame_pointer_offset;
9033     }
9034 }
9035
9036 /* In a dynamically-aligned function, we can't know the offset from
9037    stack pointer to frame pointer, so we must ensure that setjmp
9038    eliminates fp against the hard fp (%ebp) rather than trying to
9039    index from %esp up to the top of the frame across a gap that is
9040    of unknown (at compile-time) size.  */
9041 static rtx
9042 ix86_builtin_setjmp_frame_value (void)
9043 {
9044   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
9045 }
9046
9047 /* On the x86 -fsplit-stack and -fstack-protector both use the same
9048    field in the TCB, so they can not be used together.  */
9049
9050 static bool
9051 ix86_supports_split_stack (bool report ATTRIBUTE_UNUSED,
9052                            struct gcc_options *opts ATTRIBUTE_UNUSED)
9053 {
9054   bool ret = true;
9055
9056 #ifndef TARGET_THREAD_SPLIT_STACK_OFFSET
9057   if (report)
9058     error ("%<-fsplit-stack%> currently only supported on GNU/Linux");
9059   ret = false;
9060 #else
9061   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
9062     {
9063       if (report)
9064         error ("%<-fsplit-stack%> requires "
9065                "assembler support for CFI directives");
9066       ret = false;
9067     }
9068 #endif
9069
9070   return ret;
9071 }
9072
9073 /* When using -fsplit-stack, the allocation routines set a field in
9074    the TCB to the bottom of the stack plus this much space, measured
9075    in bytes.  */
9076
9077 #define SPLIT_STACK_AVAILABLE 256
9078
9079 /* Fill structure ix86_frame about frame of currently computed function.  */
9080
9081 static void
9082 ix86_compute_frame_layout (struct ix86_frame *frame)
9083 {
9084   unsigned int stack_alignment_needed;
9085   HOST_WIDE_INT offset;
9086   unsigned int preferred_alignment;
9087   HOST_WIDE_INT size = get_frame_size ();
9088   HOST_WIDE_INT to_allocate;
9089
9090   frame->nregs = ix86_nsaved_regs ();
9091   frame->nsseregs = ix86_nsaved_sseregs ();
9092
9093   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
9094   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
9095
9096   /* MS ABI seem to require stack alignment to be always 16 except for function
9097      prologues and leaf.  */
9098   if ((ix86_cfun_abi () == MS_ABI && preferred_alignment < 16)
9099       && (!current_function_is_leaf || cfun->calls_alloca != 0
9100           || ix86_current_function_calls_tls_descriptor))
9101     {
9102       preferred_alignment = 16;
9103       stack_alignment_needed = 16;
9104       crtl->preferred_stack_boundary = 128;
9105       crtl->stack_alignment_needed = 128;
9106     }
9107
9108   gcc_assert (!size || stack_alignment_needed);
9109   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
9110   gcc_assert (preferred_alignment <= stack_alignment_needed);
9111
9112   /* For SEH we have to limit the amount of code movement into the prologue.
9113      At present we do this via a BLOCKAGE, at which point there's very little
9114      scheduling that can be done, which means that there's very little point
9115      in doing anything except PUSHs.  */
9116   if (TARGET_SEH)
9117     cfun->machine->use_fast_prologue_epilogue = false;
9118
9119   /* During reload iteration the amount of registers saved can change.
9120      Recompute the value as needed.  Do not recompute when amount of registers
9121      didn't change as reload does multiple calls to the function and does not
9122      expect the decision to change within single iteration.  */
9123   else if (!optimize_function_for_size_p (cfun)
9124            && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
9125     {
9126       int count = frame->nregs;
9127       struct cgraph_node *node = cgraph_node (current_function_decl);
9128
9129       cfun->machine->use_fast_prologue_epilogue_nregs = count;
9130
9131       /* The fast prologue uses move instead of push to save registers.  This
9132          is significantly longer, but also executes faster as modern hardware
9133          can execute the moves in parallel, but can't do that for push/pop.
9134
9135          Be careful about choosing what prologue to emit:  When function takes
9136          many instructions to execute we may use slow version as well as in
9137          case function is known to be outside hot spot (this is known with
9138          feedback only).  Weight the size of function by number of registers
9139          to save as it is cheap to use one or two push instructions but very
9140          slow to use many of them.  */
9141       if (count)
9142         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
9143       if (node->frequency < NODE_FREQUENCY_NORMAL
9144           || (flag_branch_probabilities
9145               && node->frequency < NODE_FREQUENCY_HOT))
9146         cfun->machine->use_fast_prologue_epilogue = false;
9147       else
9148         cfun->machine->use_fast_prologue_epilogue
9149            = !expensive_function_p (count);
9150     }
9151   if (TARGET_PROLOGUE_USING_MOVE
9152       && cfun->machine->use_fast_prologue_epilogue)
9153     frame->save_regs_using_mov = true;
9154   else
9155     frame->save_regs_using_mov = false;
9156
9157   /* If static stack checking is enabled and done with probes, the registers
9158      need to be saved before allocating the frame.  */
9159   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
9160     frame->save_regs_using_mov = false;
9161
9162   /* Skip return address.  */
9163   offset = UNITS_PER_WORD;
9164
9165   /* Skip pushed static chain.  */
9166   if (ix86_static_chain_on_stack)
9167     offset += UNITS_PER_WORD;
9168
9169   /* Skip saved base pointer.  */
9170   if (frame_pointer_needed)
9171     offset += UNITS_PER_WORD;
9172   frame->hfp_save_offset = offset;
9173
9174   /* The traditional frame pointer location is at the top of the frame.  */
9175   frame->hard_frame_pointer_offset = offset;
9176
9177   /* Register save area */
9178   offset += frame->nregs * UNITS_PER_WORD;
9179   frame->reg_save_offset = offset;
9180
9181   /* Align and set SSE register save area.  */
9182   if (frame->nsseregs)
9183     {
9184       /* The only ABI that has saved SSE registers (Win64) also has a
9185          16-byte aligned default stack, and thus we don't need to be
9186          within the re-aligned local stack frame to save them.  */
9187       gcc_assert (INCOMING_STACK_BOUNDARY >= 128);
9188       offset = (offset + 16 - 1) & -16;
9189       offset += frame->nsseregs * 16;
9190     }
9191   frame->sse_reg_save_offset = offset;
9192
9193   /* The re-aligned stack starts here.  Values before this point are not
9194      directly comparable with values below this point.  In order to make
9195      sure that no value happens to be the same before and after, force
9196      the alignment computation below to add a non-zero value.  */
9197   if (stack_realign_fp)
9198     offset = (offset + stack_alignment_needed) & -stack_alignment_needed;
9199
9200   /* Va-arg area */
9201   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
9202   offset += frame->va_arg_size;
9203
9204   /* Align start of frame for local function.  */
9205   offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed;
9206
9207   /* Frame pointer points here.  */
9208   frame->frame_pointer_offset = offset;
9209
9210   offset += size;
9211
9212   /* Add outgoing arguments area.  Can be skipped if we eliminated
9213      all the function calls as dead code.
9214      Skipping is however impossible when function calls alloca.  Alloca
9215      expander assumes that last crtl->outgoing_args_size
9216      of stack frame are unused.  */
9217   if (ACCUMULATE_OUTGOING_ARGS
9218       && (!current_function_is_leaf || cfun->calls_alloca
9219           || ix86_current_function_calls_tls_descriptor))
9220     {
9221       offset += crtl->outgoing_args_size;
9222       frame->outgoing_arguments_size = crtl->outgoing_args_size;
9223     }
9224   else
9225     frame->outgoing_arguments_size = 0;
9226
9227   /* Align stack boundary.  Only needed if we're calling another function
9228      or using alloca.  */
9229   if (!current_function_is_leaf || cfun->calls_alloca
9230       || ix86_current_function_calls_tls_descriptor)
9231     offset = (offset + preferred_alignment - 1) & -preferred_alignment;
9232
9233   /* We've reached end of stack frame.  */
9234   frame->stack_pointer_offset = offset;
9235
9236   /* Size prologue needs to allocate.  */
9237   to_allocate = offset - frame->sse_reg_save_offset;
9238
9239   if ((!to_allocate && frame->nregs <= 1)
9240       || (TARGET_64BIT && to_allocate >= (HOST_WIDE_INT) 0x80000000))
9241     frame->save_regs_using_mov = false;
9242
9243   if (ix86_using_red_zone ()
9244       && current_function_sp_is_unchanging
9245       && current_function_is_leaf
9246       && !ix86_current_function_calls_tls_descriptor)
9247     {
9248       frame->red_zone_size = to_allocate;
9249       if (frame->save_regs_using_mov)
9250         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
9251       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
9252         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
9253     }
9254   else
9255     frame->red_zone_size = 0;
9256   frame->stack_pointer_offset -= frame->red_zone_size;
9257
9258   /* The SEH frame pointer location is near the bottom of the frame.
9259      This is enforced by the fact that the difference between the
9260      stack pointer and the frame pointer is limited to 240 bytes in
9261      the unwind data structure.  */
9262   if (TARGET_SEH)
9263     {
9264       HOST_WIDE_INT diff;
9265
9266       /* If we can leave the frame pointer where it is, do so.  */
9267       diff = frame->stack_pointer_offset - frame->hard_frame_pointer_offset;
9268       if (diff > 240 || (diff & 15) != 0)
9269         {
9270           /* Ideally we'd determine what portion of the local stack frame
9271              (within the constraint of the lowest 240) is most heavily used.
9272              But without that complication, simply bias the frame pointer
9273              by 128 bytes so as to maximize the amount of the local stack
9274              frame that is addressable with 8-bit offsets.  */
9275           frame->hard_frame_pointer_offset = frame->stack_pointer_offset - 128;
9276         }
9277     }
9278 }
9279
9280 /* This is semi-inlined memory_address_length, but simplified
9281    since we know that we're always dealing with reg+offset, and
9282    to avoid having to create and discard all that rtl.  */
9283
9284 static inline int
9285 choose_baseaddr_len (unsigned int regno, HOST_WIDE_INT offset)
9286 {
9287   int len = 4;
9288
9289   if (offset == 0)
9290     {
9291       /* EBP and R13 cannot be encoded without an offset.  */
9292       len = (regno == BP_REG || regno == R13_REG);
9293     }
9294   else if (IN_RANGE (offset, -128, 127))
9295     len = 1;
9296
9297   /* ESP and R12 must be encoded with a SIB byte.  */
9298   if (regno == SP_REG || regno == R12_REG)
9299     len++;
9300
9301   return len;
9302 }
9303   
9304 /* Return an RTX that points to CFA_OFFSET within the stack frame.
9305    The valid base registers are taken from CFUN->MACHINE->FS.  */
9306
9307 static rtx
9308 choose_baseaddr (HOST_WIDE_INT cfa_offset)
9309 {
9310   const struct machine_function *m = cfun->machine;
9311   rtx base_reg = NULL;
9312   HOST_WIDE_INT base_offset = 0;
9313
9314   if (m->use_fast_prologue_epilogue)
9315     {
9316       /* Choose the base register most likely to allow the most scheduling
9317          opportunities.  Generally FP is valid througout the function,
9318          while DRAP must be reloaded within the epilogue.  But choose either
9319          over the SP due to increased encoding size.  */
9320
9321       if (m->fs.fp_valid)
9322         {
9323           base_reg = hard_frame_pointer_rtx;
9324           base_offset = m->fs.fp_offset - cfa_offset;
9325         }
9326       else if (m->fs.drap_valid)
9327         {
9328           base_reg = crtl->drap_reg;
9329           base_offset = 0 - cfa_offset;
9330         }
9331       else if (m->fs.sp_valid)
9332         {
9333           base_reg = stack_pointer_rtx;
9334           base_offset = m->fs.sp_offset - cfa_offset;
9335         }
9336     }
9337   else
9338     {
9339       HOST_WIDE_INT toffset;
9340       int len = 16, tlen;
9341
9342       /* Choose the base register with the smallest address encoding.
9343          With a tie, choose FP > DRAP > SP.  */
9344       if (m->fs.sp_valid)
9345         {
9346           base_reg = stack_pointer_rtx;
9347           base_offset = m->fs.sp_offset - cfa_offset;
9348           len = choose_baseaddr_len (STACK_POINTER_REGNUM, base_offset);
9349         }
9350       if (m->fs.drap_valid)
9351         {
9352           toffset = 0 - cfa_offset;
9353           tlen = choose_baseaddr_len (REGNO (crtl->drap_reg), toffset);
9354           if (tlen <= len)
9355             {
9356               base_reg = crtl->drap_reg;
9357               base_offset = toffset;
9358               len = tlen;
9359             }
9360         }
9361       if (m->fs.fp_valid)
9362         {
9363           toffset = m->fs.fp_offset - cfa_offset;
9364           tlen = choose_baseaddr_len (HARD_FRAME_POINTER_REGNUM, toffset);
9365           if (tlen <= len)
9366             {
9367               base_reg = hard_frame_pointer_rtx;
9368               base_offset = toffset;
9369               len = tlen;
9370             }
9371         }
9372     }
9373   gcc_assert (base_reg != NULL);
9374
9375   return plus_constant (base_reg, base_offset);
9376 }
9377
9378 /* Emit code to save registers in the prologue.  */
9379
9380 static void
9381 ix86_emit_save_regs (void)
9382 {
9383   unsigned int regno;
9384   rtx insn;
9385
9386   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
9387     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9388       {
9389         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
9390         RTX_FRAME_RELATED_P (insn) = 1;
9391       }
9392 }
9393
9394 /* Emit a single register save at CFA - CFA_OFFSET.  */
9395
9396 static void
9397 ix86_emit_save_reg_using_mov (enum machine_mode mode, unsigned int regno,
9398                               HOST_WIDE_INT cfa_offset)
9399 {
9400   struct machine_function *m = cfun->machine;
9401   rtx reg = gen_rtx_REG (mode, regno);
9402   rtx mem, addr, base, insn;
9403
9404   addr = choose_baseaddr (cfa_offset);
9405   mem = gen_frame_mem (mode, addr);
9406
9407   /* For SSE saves, we need to indicate the 128-bit alignment.  */
9408   set_mem_align (mem, GET_MODE_ALIGNMENT (mode));
9409
9410   insn = emit_move_insn (mem, reg);
9411   RTX_FRAME_RELATED_P (insn) = 1;
9412
9413   base = addr;
9414   if (GET_CODE (base) == PLUS)
9415     base = XEXP (base, 0);
9416   gcc_checking_assert (REG_P (base));
9417
9418   /* When saving registers into a re-aligned local stack frame, avoid
9419      any tricky guessing by dwarf2out.  */
9420   if (m->fs.realigned)
9421     {
9422       gcc_checking_assert (stack_realign_drap);
9423
9424       if (regno == REGNO (crtl->drap_reg))
9425         {
9426           /* A bit of a hack.  We force the DRAP register to be saved in
9427              the re-aligned stack frame, which provides us with a copy
9428              of the CFA that will last past the prologue.  Install it.  */
9429           gcc_checking_assert (cfun->machine->fs.fp_valid);
9430           addr = plus_constant (hard_frame_pointer_rtx,
9431                                 cfun->machine->fs.fp_offset - cfa_offset);
9432           mem = gen_rtx_MEM (mode, addr);
9433           add_reg_note (insn, REG_CFA_DEF_CFA, mem);
9434         }
9435       else
9436         {
9437           /* The frame pointer is a stable reference within the
9438              aligned frame.  Use it.  */
9439           gcc_checking_assert (cfun->machine->fs.fp_valid);
9440           addr = plus_constant (hard_frame_pointer_rtx,
9441                                 cfun->machine->fs.fp_offset - cfa_offset);
9442           mem = gen_rtx_MEM (mode, addr);
9443           add_reg_note (insn, REG_CFA_EXPRESSION,
9444                         gen_rtx_SET (VOIDmode, mem, reg));
9445         }
9446     }
9447
9448   /* The memory may not be relative to the current CFA register,
9449      which means that we may need to generate a new pattern for
9450      use by the unwind info.  */
9451   else if (base != m->fs.cfa_reg)
9452     {
9453       addr = plus_constant (m->fs.cfa_reg, m->fs.cfa_offset - cfa_offset);
9454       mem = gen_rtx_MEM (mode, addr);
9455       add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (VOIDmode, mem, reg));
9456     }
9457 }
9458
9459 /* Emit code to save registers using MOV insns.
9460    First register is stored at CFA - CFA_OFFSET.  */
9461 static void
9462 ix86_emit_save_regs_using_mov (HOST_WIDE_INT cfa_offset)
9463 {
9464   unsigned int regno;
9465
9466   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9467     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9468       {
9469         ix86_emit_save_reg_using_mov (Pmode, regno, cfa_offset);
9470         cfa_offset -= UNITS_PER_WORD;
9471       }
9472 }
9473
9474 /* Emit code to save SSE registers using MOV insns.
9475    First register is stored at CFA - CFA_OFFSET.  */
9476 static void
9477 ix86_emit_save_sse_regs_using_mov (HOST_WIDE_INT cfa_offset)
9478 {
9479   unsigned int regno;
9480
9481   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9482     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9483       {
9484         ix86_emit_save_reg_using_mov (V4SFmode, regno, cfa_offset);
9485         cfa_offset -= 16;
9486       }
9487 }
9488
9489 static GTY(()) rtx queued_cfa_restores;
9490
9491 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
9492    manipulation insn.  The value is on the stack at CFA - CFA_OFFSET.
9493    Don't add the note if the previously saved value will be left untouched
9494    within stack red-zone till return, as unwinders can find the same value
9495    in the register and on the stack.  */
9496
9497 static void
9498 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT cfa_offset)
9499 {
9500   if (cfa_offset <= cfun->machine->fs.red_zone_offset)
9501     return;
9502
9503   if (insn)
9504     {
9505       add_reg_note (insn, REG_CFA_RESTORE, reg);
9506       RTX_FRAME_RELATED_P (insn) = 1;
9507     }
9508   else
9509     queued_cfa_restores
9510       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
9511 }
9512
9513 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
9514
9515 static void
9516 ix86_add_queued_cfa_restore_notes (rtx insn)
9517 {
9518   rtx last;
9519   if (!queued_cfa_restores)
9520     return;
9521   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
9522     ;
9523   XEXP (last, 1) = REG_NOTES (insn);
9524   REG_NOTES (insn) = queued_cfa_restores;
9525   queued_cfa_restores = NULL_RTX;
9526   RTX_FRAME_RELATED_P (insn) = 1;
9527 }
9528
9529 /* Expand prologue or epilogue stack adjustment.
9530    The pattern exist to put a dependency on all ebp-based memory accesses.
9531    STYLE should be negative if instructions should be marked as frame related,
9532    zero if %r11 register is live and cannot be freely used and positive
9533    otherwise.  */
9534
9535 static void
9536 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
9537                            int style, bool set_cfa)
9538 {
9539   struct machine_function *m = cfun->machine;
9540   rtx insn;
9541   bool add_frame_related_expr = false;
9542
9543   if (! TARGET_64BIT)
9544     insn = gen_pro_epilogue_adjust_stack_si_add (dest, src, offset);
9545   else if (x86_64_immediate_operand (offset, DImode))
9546     insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, offset);
9547   else
9548     {
9549       rtx tmp;
9550       /* r11 is used by indirect sibcall return as well, set before the
9551          epilogue and used after the epilogue.  */
9552       if (style)
9553         tmp = gen_rtx_REG (DImode, R11_REG);
9554       else
9555         {
9556           gcc_assert (src != hard_frame_pointer_rtx
9557                       && dest != hard_frame_pointer_rtx);
9558           tmp = hard_frame_pointer_rtx;
9559         }
9560       insn = emit_insn (gen_rtx_SET (DImode, tmp, offset));
9561       if (style < 0)
9562         add_frame_related_expr = true;
9563
9564       insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, tmp);
9565     }
9566
9567   insn = emit_insn (insn);
9568   if (style >= 0)
9569     ix86_add_queued_cfa_restore_notes (insn);
9570
9571   if (set_cfa)
9572     {
9573       rtx r;
9574
9575       gcc_assert (m->fs.cfa_reg == src);
9576       m->fs.cfa_offset += INTVAL (offset);
9577       m->fs.cfa_reg = dest;
9578
9579       r = gen_rtx_PLUS (Pmode, src, offset);
9580       r = gen_rtx_SET (VOIDmode, dest, r);
9581       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
9582       RTX_FRAME_RELATED_P (insn) = 1;
9583     }
9584   else if (style < 0)
9585     {
9586       RTX_FRAME_RELATED_P (insn) = 1;
9587       if (add_frame_related_expr)
9588         {
9589           rtx r = gen_rtx_PLUS (Pmode, src, offset);
9590           r = gen_rtx_SET (VOIDmode, dest, r);
9591           add_reg_note (insn, REG_FRAME_RELATED_EXPR, r);
9592         }
9593     }
9594
9595   if (dest == stack_pointer_rtx)
9596     {
9597       HOST_WIDE_INT ooffset = m->fs.sp_offset;
9598       bool valid = m->fs.sp_valid;
9599
9600       if (src == hard_frame_pointer_rtx)
9601         {
9602           valid = m->fs.fp_valid;
9603           ooffset = m->fs.fp_offset;
9604         }
9605       else if (src == crtl->drap_reg)
9606         {
9607           valid = m->fs.drap_valid;
9608           ooffset = 0;
9609         }
9610       else
9611         {
9612           /* Else there are two possibilities: SP itself, which we set
9613              up as the default above.  Or EH_RETURN_STACKADJ_RTX, which is
9614              taken care of this by hand along the eh_return path.  */
9615           gcc_checking_assert (src == stack_pointer_rtx
9616                                || offset == const0_rtx);
9617         }
9618
9619       m->fs.sp_offset = ooffset - INTVAL (offset);
9620       m->fs.sp_valid = valid;
9621     }
9622 }
9623
9624 /* Find an available register to be used as dynamic realign argument
9625    pointer regsiter.  Such a register will be written in prologue and
9626    used in begin of body, so it must not be
9627         1. parameter passing register.
9628         2. GOT pointer.
9629    We reuse static-chain register if it is available.  Otherwise, we
9630    use DI for i386 and R13 for x86-64.  We chose R13 since it has
9631    shorter encoding.
9632
9633    Return: the regno of chosen register.  */
9634
9635 static unsigned int
9636 find_drap_reg (void)
9637 {
9638   tree decl = cfun->decl;
9639
9640   if (TARGET_64BIT)
9641     {
9642       /* Use R13 for nested function or function need static chain.
9643          Since function with tail call may use any caller-saved
9644          registers in epilogue, DRAP must not use caller-saved
9645          register in such case.  */
9646       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9647         return R13_REG;
9648
9649       return R10_REG;
9650     }
9651   else
9652     {
9653       /* Use DI for nested function or function need static chain.
9654          Since function with tail call may use any caller-saved
9655          registers in epilogue, DRAP must not use caller-saved
9656          register in such case.  */
9657       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9658         return DI_REG;
9659
9660       /* Reuse static chain register if it isn't used for parameter
9661          passing.  */
9662       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2
9663           && !lookup_attribute ("fastcall",
9664                                 TYPE_ATTRIBUTES (TREE_TYPE (decl)))
9665           && !lookup_attribute ("thiscall",
9666                                 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
9667         return CX_REG;
9668       else
9669         return DI_REG;
9670     }
9671 }
9672
9673 /* Return minimum incoming stack alignment.  */
9674
9675 static unsigned int
9676 ix86_minimum_incoming_stack_boundary (bool sibcall)
9677 {
9678   unsigned int incoming_stack_boundary;
9679
9680   /* Prefer the one specified at command line. */
9681   if (ix86_user_incoming_stack_boundary)
9682     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
9683   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
9684      if -mstackrealign is used, it isn't used for sibcall check and
9685      estimated stack alignment is 128bit.  */
9686   else if (!sibcall
9687            && !TARGET_64BIT
9688            && ix86_force_align_arg_pointer
9689            && crtl->stack_alignment_estimated == 128)
9690     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9691   else
9692     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
9693
9694   /* Incoming stack alignment can be changed on individual functions
9695      via force_align_arg_pointer attribute.  We use the smallest
9696      incoming stack boundary.  */
9697   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
9698       && lookup_attribute (ix86_force_align_arg_pointer_string,
9699                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
9700     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9701
9702   /* The incoming stack frame has to be aligned at least at
9703      parm_stack_boundary.  */
9704   if (incoming_stack_boundary < crtl->parm_stack_boundary)
9705     incoming_stack_boundary = crtl->parm_stack_boundary;
9706
9707   /* Stack at entrance of main is aligned by runtime.  We use the
9708      smallest incoming stack boundary. */
9709   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
9710       && DECL_NAME (current_function_decl)
9711       && MAIN_NAME_P (DECL_NAME (current_function_decl))
9712       && DECL_FILE_SCOPE_P (current_function_decl))
9713     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
9714
9715   return incoming_stack_boundary;
9716 }
9717
9718 /* Update incoming stack boundary and estimated stack alignment.  */
9719
9720 static void
9721 ix86_update_stack_boundary (void)
9722 {
9723   ix86_incoming_stack_boundary
9724     = ix86_minimum_incoming_stack_boundary (false);
9725
9726   /* x86_64 vararg needs 16byte stack alignment for register save
9727      area.  */
9728   if (TARGET_64BIT
9729       && cfun->stdarg
9730       && crtl->stack_alignment_estimated < 128)
9731     crtl->stack_alignment_estimated = 128;
9732 }
9733
9734 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
9735    needed or an rtx for DRAP otherwise.  */
9736
9737 static rtx
9738 ix86_get_drap_rtx (void)
9739 {
9740   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
9741     crtl->need_drap = true;
9742
9743   if (stack_realign_drap)
9744     {
9745       /* Assign DRAP to vDRAP and returns vDRAP */
9746       unsigned int regno = find_drap_reg ();
9747       rtx drap_vreg;
9748       rtx arg_ptr;
9749       rtx seq, insn;
9750
9751       arg_ptr = gen_rtx_REG (Pmode, regno);
9752       crtl->drap_reg = arg_ptr;
9753
9754       start_sequence ();
9755       drap_vreg = copy_to_reg (arg_ptr);
9756       seq = get_insns ();
9757       end_sequence ();
9758
9759       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
9760       if (!optimize)
9761         {
9762           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
9763           RTX_FRAME_RELATED_P (insn) = 1;
9764         }
9765       return drap_vreg;
9766     }
9767   else
9768     return NULL;
9769 }
9770
9771 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
9772
9773 static rtx
9774 ix86_internal_arg_pointer (void)
9775 {
9776   return virtual_incoming_args_rtx;
9777 }
9778
9779 struct scratch_reg {
9780   rtx reg;
9781   bool saved;
9782 };
9783
9784 /* Return a short-lived scratch register for use on function entry.
9785    In 32-bit mode, it is valid only after the registers are saved
9786    in the prologue.  This register must be released by means of
9787    release_scratch_register_on_entry once it is dead.  */
9788
9789 static void
9790 get_scratch_register_on_entry (struct scratch_reg *sr)
9791 {
9792   int regno;
9793
9794   sr->saved = false;
9795
9796   if (TARGET_64BIT)
9797     {
9798       /* We always use R11 in 64-bit mode.  */
9799       regno = R11_REG;
9800     }
9801   else
9802     {
9803       tree decl = current_function_decl, fntype = TREE_TYPE (decl);
9804       bool fastcall_p
9805         = lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
9806       bool static_chain_p = DECL_STATIC_CHAIN (decl);
9807       int regparm = ix86_function_regparm (fntype, decl);
9808       int drap_regno
9809         = crtl->drap_reg ? REGNO (crtl->drap_reg) : INVALID_REGNUM;
9810
9811       /* 'fastcall' sets regparm to 2, uses ecx/edx for arguments and eax
9812           for the static chain register.  */
9813       if ((regparm < 1 || (fastcall_p && !static_chain_p))
9814           && drap_regno != AX_REG)
9815         regno = AX_REG;
9816       else if (regparm < 2 && drap_regno != DX_REG)
9817         regno = DX_REG;
9818       /* ecx is the static chain register.  */
9819       else if (regparm < 3 && !fastcall_p && !static_chain_p
9820                && drap_regno != CX_REG)
9821         regno = CX_REG;
9822       else if (ix86_save_reg (BX_REG, true))
9823         regno = BX_REG;
9824       /* esi is the static chain register.  */
9825       else if (!(regparm == 3 && static_chain_p)
9826                && ix86_save_reg (SI_REG, true))
9827         regno = SI_REG;
9828       else if (ix86_save_reg (DI_REG, true))
9829         regno = DI_REG;
9830       else
9831         {
9832           regno = (drap_regno == AX_REG ? DX_REG : AX_REG);
9833           sr->saved = true;
9834         }
9835     }
9836
9837   sr->reg = gen_rtx_REG (Pmode, regno);
9838   if (sr->saved)
9839     {
9840       rtx insn = emit_insn (gen_push (sr->reg));
9841       RTX_FRAME_RELATED_P (insn) = 1;
9842     }
9843 }
9844
9845 /* Release a scratch register obtained from the preceding function.  */
9846
9847 static void
9848 release_scratch_register_on_entry (struct scratch_reg *sr)
9849 {
9850   if (sr->saved)
9851     {
9852       rtx x, insn = emit_insn (gen_pop (sr->reg));
9853
9854       /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
9855       RTX_FRAME_RELATED_P (insn) = 1;
9856       x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
9857       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
9858       add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
9859     }
9860 }
9861
9862 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
9863
9864 /* Emit code to adjust the stack pointer by SIZE bytes while probing it.  */
9865
9866 static void
9867 ix86_adjust_stack_and_probe (const HOST_WIDE_INT size)
9868 {
9869   /* We skip the probe for the first interval + a small dope of 4 words and
9870      probe that many bytes past the specified size to maintain a protection
9871      area at the botton of the stack.  */
9872   const int dope = 4 * UNITS_PER_WORD;
9873   rtx size_rtx = GEN_INT (size);
9874
9875   /* See if we have a constant small number of probes to generate.  If so,
9876      that's the easy case.  The run-time loop is made up of 11 insns in the
9877      generic case while the compile-time loop is made up of 3+2*(n-1) insns
9878      for n # of intervals.  */
9879   if (size <= 5 * PROBE_INTERVAL)
9880     {
9881       HOST_WIDE_INT i, adjust;
9882       bool first_probe = true;
9883
9884       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
9885          values of N from 1 until it exceeds SIZE.  If only one probe is
9886          needed, this will not generate any code.  Then adjust and probe
9887          to PROBE_INTERVAL + SIZE.  */
9888       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9889         {
9890           if (first_probe)
9891             {
9892               adjust = 2 * PROBE_INTERVAL + dope;
9893               first_probe = false;
9894             }
9895           else
9896             adjust = PROBE_INTERVAL;
9897
9898           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9899                                   plus_constant (stack_pointer_rtx, -adjust)));
9900           emit_stack_probe (stack_pointer_rtx);
9901         }
9902
9903       if (first_probe)
9904         adjust = size + PROBE_INTERVAL + dope;
9905       else
9906         adjust = size + PROBE_INTERVAL - i;
9907
9908       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9909                               plus_constant (stack_pointer_rtx, -adjust)));
9910       emit_stack_probe (stack_pointer_rtx);
9911
9912       /* Adjust back to account for the additional first interval.  */
9913       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9914                               plus_constant (stack_pointer_rtx,
9915                                              PROBE_INTERVAL + dope)));
9916     }
9917
9918   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9919      extra careful with variables wrapping around because we might be at
9920      the very top (or the very bottom) of the address space and we have
9921      to be able to handle this case properly; in particular, we use an
9922      equality test for the loop condition.  */
9923   else
9924     {
9925       HOST_WIDE_INT rounded_size;
9926       struct scratch_reg sr;
9927
9928       get_scratch_register_on_entry (&sr);
9929
9930
9931       /* Step 1: round SIZE to the previous multiple of the interval.  */
9932
9933       rounded_size = size & -PROBE_INTERVAL;
9934
9935
9936       /* Step 2: compute initial and final value of the loop counter.  */
9937
9938       /* SP = SP_0 + PROBE_INTERVAL.  */
9939       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9940                               plus_constant (stack_pointer_rtx,
9941                                              - (PROBE_INTERVAL + dope))));
9942
9943       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
9944       emit_move_insn (sr.reg, GEN_INT (-rounded_size));
9945       emit_insn (gen_rtx_SET (VOIDmode, sr.reg,
9946                               gen_rtx_PLUS (Pmode, sr.reg,
9947                                             stack_pointer_rtx)));
9948
9949
9950       /* Step 3: the loop
9951
9952          while (SP != LAST_ADDR)
9953            {
9954              SP = SP + PROBE_INTERVAL
9955              probe at SP
9956            }
9957
9958          adjusts SP and probes to PROBE_INTERVAL + N * PROBE_INTERVAL for
9959          values of N from 1 until it is equal to ROUNDED_SIZE.  */
9960
9961       emit_insn (ix86_gen_adjust_stack_and_probe (sr.reg, sr.reg, size_rtx));
9962
9963
9964       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
9965          assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
9966
9967       if (size != rounded_size)
9968         {
9969           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9970                                   plus_constant (stack_pointer_rtx,
9971                                                  rounded_size - size)));
9972           emit_stack_probe (stack_pointer_rtx);
9973         }
9974
9975       /* Adjust back to account for the additional first interval.  */
9976       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9977                               plus_constant (stack_pointer_rtx,
9978                                              PROBE_INTERVAL + dope)));
9979
9980       release_scratch_register_on_entry (&sr);
9981     }
9982
9983   gcc_assert (cfun->machine->fs.cfa_reg != stack_pointer_rtx);
9984   cfun->machine->fs.sp_offset += size;
9985
9986   /* Make sure nothing is scheduled before we are done.  */
9987   emit_insn (gen_blockage ());
9988 }
9989
9990 /* Adjust the stack pointer up to REG while probing it.  */
9991
9992 const char *
9993 output_adjust_stack_and_probe (rtx reg)
9994 {
9995   static int labelno = 0;
9996   char loop_lab[32], end_lab[32];
9997   rtx xops[2];
9998
9999   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
10000   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
10001
10002   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
10003
10004   /* Jump to END_LAB if SP == LAST_ADDR.  */
10005   xops[0] = stack_pointer_rtx;
10006   xops[1] = reg;
10007   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
10008   fputs ("\tje\t", asm_out_file);
10009   assemble_name_raw (asm_out_file, end_lab);
10010   fputc ('\n', asm_out_file);
10011
10012   /* SP = SP + PROBE_INTERVAL.  */
10013   xops[1] = GEN_INT (PROBE_INTERVAL);
10014   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
10015
10016   /* Probe at SP.  */
10017   xops[1] = const0_rtx;
10018   output_asm_insn ("or%z0\t{%1, (%0)|DWORD PTR [%0], %1}", xops);
10019
10020   fprintf (asm_out_file, "\tjmp\t");
10021   assemble_name_raw (asm_out_file, loop_lab);
10022   fputc ('\n', asm_out_file);
10023
10024   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
10025
10026   return "";
10027 }
10028
10029 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10030    inclusive.  These are offsets from the current stack pointer.  */
10031
10032 static void
10033 ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10034 {
10035   /* See if we have a constant small number of probes to generate.  If so,
10036      that's the easy case.  The run-time loop is made up of 7 insns in the
10037      generic case while the compile-time loop is made up of n insns for n #
10038      of intervals.  */
10039   if (size <= 7 * PROBE_INTERVAL)
10040     {
10041       HOST_WIDE_INT i;
10042
10043       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10044          it exceeds SIZE.  If only one probe is needed, this will not
10045          generate any code.  Then probe at FIRST + SIZE.  */
10046       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10047         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
10048
10049       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
10050     }
10051
10052   /* Otherwise, do the same as above, but in a loop.  Note that we must be
10053      extra careful with variables wrapping around because we might be at
10054      the very top (or the very bottom) of the address space and we have
10055      to be able to handle this case properly; in particular, we use an
10056      equality test for the loop condition.  */
10057   else
10058     {
10059       HOST_WIDE_INT rounded_size, last;
10060       struct scratch_reg sr;
10061
10062       get_scratch_register_on_entry (&sr);
10063
10064
10065       /* Step 1: round SIZE to the previous multiple of the interval.  */
10066
10067       rounded_size = size & -PROBE_INTERVAL;
10068
10069
10070       /* Step 2: compute initial and final value of the loop counter.  */
10071
10072       /* TEST_OFFSET = FIRST.  */
10073       emit_move_insn (sr.reg, GEN_INT (-first));
10074
10075       /* LAST_OFFSET = FIRST + ROUNDED_SIZE.  */
10076       last = first + rounded_size;
10077
10078
10079       /* Step 3: the loop
10080
10081          while (TEST_ADDR != LAST_ADDR)
10082            {
10083              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
10084              probe at TEST_ADDR
10085            }
10086
10087          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
10088          until it is equal to ROUNDED_SIZE.  */
10089
10090       emit_insn (ix86_gen_probe_stack_range (sr.reg, sr.reg, GEN_INT (-last)));
10091
10092
10093       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
10094          that SIZE is equal to ROUNDED_SIZE.  */
10095
10096       if (size != rounded_size)
10097         emit_stack_probe (plus_constant (gen_rtx_PLUS (Pmode,
10098                                                        stack_pointer_rtx,
10099                                                        sr.reg),
10100                                          rounded_size - size));
10101
10102       release_scratch_register_on_entry (&sr);
10103     }
10104
10105   /* Make sure nothing is scheduled before we are done.  */
10106   emit_insn (gen_blockage ());
10107 }
10108
10109 /* Probe a range of stack addresses from REG to END, inclusive.  These are
10110    offsets from the current stack pointer.  */
10111
10112 const char *
10113 output_probe_stack_range (rtx reg, rtx end)
10114 {
10115   static int labelno = 0;
10116   char loop_lab[32], end_lab[32];
10117   rtx xops[3];
10118
10119   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
10120   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
10121
10122   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
10123
10124   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
10125   xops[0] = reg;
10126   xops[1] = end;
10127   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
10128   fputs ("\tje\t", asm_out_file);
10129   assemble_name_raw (asm_out_file, end_lab);
10130   fputc ('\n', asm_out_file);
10131
10132   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
10133   xops[1] = GEN_INT (PROBE_INTERVAL);
10134   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
10135
10136   /* Probe at TEST_ADDR.  */
10137   xops[0] = stack_pointer_rtx;
10138   xops[1] = reg;
10139   xops[2] = const0_rtx;
10140   output_asm_insn ("or%z0\t{%2, (%0,%1)|DWORD PTR [%0+%1], %2}", xops);
10141
10142   fprintf (asm_out_file, "\tjmp\t");
10143   assemble_name_raw (asm_out_file, loop_lab);
10144   fputc ('\n', asm_out_file);
10145
10146   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
10147
10148   return "";
10149 }
10150
10151 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
10152    to be generated in correct form.  */
10153 static void
10154 ix86_finalize_stack_realign_flags (void)
10155 {
10156   /* Check if stack realign is really needed after reload, and
10157      stores result in cfun */
10158   unsigned int incoming_stack_boundary
10159     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
10160        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
10161   unsigned int stack_realign = (incoming_stack_boundary
10162                                 < (current_function_is_leaf
10163                                    ? crtl->max_used_stack_slot_alignment
10164                                    : crtl->stack_alignment_needed));
10165
10166   if (crtl->stack_realign_finalized)
10167     {
10168       /* After stack_realign_needed is finalized, we can't no longer
10169          change it.  */
10170       gcc_assert (crtl->stack_realign_needed == stack_realign);
10171     }
10172   else
10173     {
10174       crtl->stack_realign_needed = stack_realign;
10175       crtl->stack_realign_finalized = true;
10176     }
10177 }
10178
10179 /* Expand the prologue into a bunch of separate insns.  */
10180
10181 void
10182 ix86_expand_prologue (void)
10183 {
10184   struct machine_function *m = cfun->machine;
10185   rtx insn, t;
10186   bool pic_reg_used;
10187   struct ix86_frame frame;
10188   HOST_WIDE_INT allocate;
10189   bool int_registers_saved;
10190
10191   ix86_finalize_stack_realign_flags ();
10192
10193   /* DRAP should not coexist with stack_realign_fp */
10194   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
10195
10196   memset (&m->fs, 0, sizeof (m->fs));
10197
10198   /* Initialize CFA state for before the prologue.  */
10199   m->fs.cfa_reg = stack_pointer_rtx;
10200   m->fs.cfa_offset = INCOMING_FRAME_SP_OFFSET;
10201
10202   /* Track SP offset to the CFA.  We continue tracking this after we've
10203      swapped the CFA register away from SP.  In the case of re-alignment
10204      this is fudged; we're interested to offsets within the local frame.  */
10205   m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10206   m->fs.sp_valid = true;
10207
10208   ix86_compute_frame_layout (&frame);
10209
10210   if (!TARGET_64BIT && ix86_function_ms_hook_prologue (current_function_decl))
10211     {
10212       /* We should have already generated an error for any use of
10213          ms_hook on a nested function.  */
10214       gcc_checking_assert (!ix86_static_chain_on_stack);
10215
10216       /* Check if profiling is active and we shall use profiling before
10217          prologue variant. If so sorry.  */
10218       if (crtl->profile && flag_fentry != 0)
10219         sorry ("ms_hook_prologue attribute isn%'t compatible "
10220                "with -mfentry for 32-bit");
10221
10222       /* In ix86_asm_output_function_label we emitted:
10223          8b ff     movl.s %edi,%edi
10224          55        push   %ebp
10225          8b ec     movl.s %esp,%ebp
10226
10227          This matches the hookable function prologue in Win32 API
10228          functions in Microsoft Windows XP Service Pack 2 and newer.
10229          Wine uses this to enable Windows apps to hook the Win32 API
10230          functions provided by Wine.
10231
10232          What that means is that we've already set up the frame pointer.  */
10233
10234       if (frame_pointer_needed
10235           && !(crtl->drap_reg && crtl->stack_realign_needed))
10236         {
10237           rtx push, mov;
10238
10239           /* We've decided to use the frame pointer already set up.
10240              Describe this to the unwinder by pretending that both
10241              push and mov insns happen right here.
10242
10243              Putting the unwind info here at the end of the ms_hook
10244              is done so that we can make absolutely certain we get
10245              the required byte sequence at the start of the function,
10246              rather than relying on an assembler that can produce
10247              the exact encoding required.
10248
10249              However it does mean (in the unpatched case) that we have
10250              a 1 insn window where the asynchronous unwind info is
10251              incorrect.  However, if we placed the unwind info at
10252              its correct location we would have incorrect unwind info
10253              in the patched case.  Which is probably all moot since
10254              I don't expect Wine generates dwarf2 unwind info for the
10255              system libraries that use this feature.  */
10256
10257           insn = emit_insn (gen_blockage ());
10258
10259           push = gen_push (hard_frame_pointer_rtx);
10260           mov = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10261                              stack_pointer_rtx);
10262           RTX_FRAME_RELATED_P (push) = 1;
10263           RTX_FRAME_RELATED_P (mov) = 1;
10264
10265           RTX_FRAME_RELATED_P (insn) = 1;
10266           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10267                         gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, push, mov)));
10268
10269           /* Note that gen_push incremented m->fs.cfa_offset, even
10270              though we didn't emit the push insn here.  */
10271           m->fs.cfa_reg = hard_frame_pointer_rtx;
10272           m->fs.fp_offset = m->fs.cfa_offset;
10273           m->fs.fp_valid = true;
10274         }
10275       else
10276         {
10277           /* The frame pointer is not needed so pop %ebp again.
10278              This leaves us with a pristine state.  */
10279           emit_insn (gen_pop (hard_frame_pointer_rtx));
10280         }
10281     }
10282
10283   /* The first insn of a function that accepts its static chain on the
10284      stack is to push the register that would be filled in by a direct
10285      call.  This insn will be skipped by the trampoline.  */
10286   else if (ix86_static_chain_on_stack)
10287     {
10288       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
10289       emit_insn (gen_blockage ());
10290
10291       /* We don't want to interpret this push insn as a register save,
10292          only as a stack adjustment.  The real copy of the register as
10293          a save will be done later, if needed.  */
10294       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
10295       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
10296       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
10297       RTX_FRAME_RELATED_P (insn) = 1;
10298     }
10299
10300   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
10301      of DRAP is needed and stack realignment is really needed after reload */
10302   if (stack_realign_drap)
10303     {
10304       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10305
10306       /* Only need to push parameter pointer reg if it is caller saved.  */
10307       if (!call_used_regs[REGNO (crtl->drap_reg)])
10308         {
10309           /* Push arg pointer reg */
10310           insn = emit_insn (gen_push (crtl->drap_reg));
10311           RTX_FRAME_RELATED_P (insn) = 1;
10312         }
10313
10314       /* Grab the argument pointer.  */
10315       t = plus_constant (stack_pointer_rtx, m->fs.sp_offset);
10316       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10317       RTX_FRAME_RELATED_P (insn) = 1;
10318       m->fs.cfa_reg = crtl->drap_reg;
10319       m->fs.cfa_offset = 0;
10320
10321       /* Align the stack.  */
10322       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10323                                         stack_pointer_rtx,
10324                                         GEN_INT (-align_bytes)));
10325       RTX_FRAME_RELATED_P (insn) = 1;
10326
10327       /* Replicate the return address on the stack so that return
10328          address can be reached via (argp - 1) slot.  This is needed
10329          to implement macro RETURN_ADDR_RTX and intrinsic function
10330          expand_builtin_return_addr etc.  */
10331       t = plus_constant (crtl->drap_reg, -UNITS_PER_WORD);
10332       t = gen_frame_mem (Pmode, t);
10333       insn = emit_insn (gen_push (t));
10334       RTX_FRAME_RELATED_P (insn) = 1;
10335
10336       /* For the purposes of frame and register save area addressing,
10337          we've started over with a new frame.  */
10338       m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10339       m->fs.realigned = true;
10340     }
10341
10342   if (frame_pointer_needed && !m->fs.fp_valid)
10343     {
10344       /* Note: AT&T enter does NOT have reversed args.  Enter is probably
10345          slower on all targets.  Also sdb doesn't like it.  */
10346       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
10347       RTX_FRAME_RELATED_P (insn) = 1;
10348
10349       if (m->fs.sp_offset == frame.hard_frame_pointer_offset)
10350         {
10351           insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
10352           RTX_FRAME_RELATED_P (insn) = 1;
10353
10354           if (m->fs.cfa_reg == stack_pointer_rtx)
10355             m->fs.cfa_reg = hard_frame_pointer_rtx;
10356           m->fs.fp_offset = m->fs.sp_offset;
10357           m->fs.fp_valid = true;
10358         }
10359     }
10360
10361   int_registers_saved = (frame.nregs == 0);
10362
10363   if (!int_registers_saved)
10364     {
10365       /* If saving registers via PUSH, do so now.  */
10366       if (!frame.save_regs_using_mov)
10367         {
10368           ix86_emit_save_regs ();
10369           int_registers_saved = true;
10370           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
10371         }
10372
10373       /* When using red zone we may start register saving before allocating
10374          the stack frame saving one cycle of the prologue.  However, avoid
10375          doing this if we have to probe the stack; at least on x86_64 the
10376          stack probe can turn into a call that clobbers a red zone location. */
10377       else if (ix86_using_red_zone ()
10378                && (! TARGET_STACK_PROBE
10379                    || frame.stack_pointer_offset < CHECK_STACK_LIMIT))
10380         {
10381           ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10382           int_registers_saved = true;
10383         }
10384     }
10385
10386   if (stack_realign_fp)
10387     {
10388       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10389       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
10390
10391       /* The computation of the size of the re-aligned stack frame means
10392          that we must allocate the size of the register save area before
10393          performing the actual alignment.  Otherwise we cannot guarantee
10394          that there's enough storage above the realignment point.  */
10395       if (m->fs.sp_offset != frame.sse_reg_save_offset)
10396         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10397                                    GEN_INT (m->fs.sp_offset
10398                                             - frame.sse_reg_save_offset),
10399                                    -1, false);
10400
10401       /* Align the stack.  */
10402       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10403                                         stack_pointer_rtx,
10404                                         GEN_INT (-align_bytes)));
10405
10406       /* For the purposes of register save area addressing, the stack
10407          pointer is no longer valid.  As for the value of sp_offset,
10408          see ix86_compute_frame_layout, which we need to match in order
10409          to pass verification of stack_pointer_offset at the end.  */
10410       m->fs.sp_offset = (m->fs.sp_offset + align_bytes) & -align_bytes;
10411       m->fs.sp_valid = false;
10412     }
10413
10414   allocate = frame.stack_pointer_offset - m->fs.sp_offset;
10415
10416   if (flag_stack_usage)
10417     {
10418       /* We start to count from ARG_POINTER.  */
10419       HOST_WIDE_INT stack_size = frame.stack_pointer_offset;
10420
10421       /* If it was realigned, take into account the fake frame.  */
10422       if (stack_realign_drap)
10423         {
10424           if (ix86_static_chain_on_stack)
10425             stack_size += UNITS_PER_WORD;
10426
10427           if (!call_used_regs[REGNO (crtl->drap_reg)])
10428             stack_size += UNITS_PER_WORD;
10429
10430           /* This over-estimates by 1 minimal-stack-alignment-unit but
10431              mitigates that by counting in the new return address slot.  */
10432           current_function_dynamic_stack_size
10433             += crtl->stack_alignment_needed / BITS_PER_UNIT;
10434         }
10435
10436       current_function_static_stack_size = stack_size;
10437     }
10438
10439   /* The stack has already been decremented by the instruction calling us
10440      so we need to probe unconditionally to preserve the protection area.  */
10441   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
10442     {
10443       /* We expect the registers to be saved when probes are used.  */
10444       gcc_assert (int_registers_saved);
10445
10446       if (STACK_CHECK_MOVING_SP)
10447         {
10448           ix86_adjust_stack_and_probe (allocate);
10449           allocate = 0;
10450         }
10451       else
10452         {
10453           HOST_WIDE_INT size = allocate;
10454
10455           if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
10456             size = 0x80000000 - STACK_CHECK_PROTECT - 1;
10457
10458           if (TARGET_STACK_PROBE)
10459             ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
10460           else
10461             ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
10462         }
10463     }
10464
10465   if (allocate == 0)
10466     ;
10467   else if (!ix86_target_stack_probe ()
10468            || frame.stack_pointer_offset < CHECK_STACK_LIMIT)
10469     {
10470       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10471                                  GEN_INT (-allocate), -1,
10472                                  m->fs.cfa_reg == stack_pointer_rtx);
10473     }
10474   else
10475     {
10476       rtx eax = gen_rtx_REG (Pmode, AX_REG);
10477       rtx r10 = NULL;
10478       rtx (*adjust_stack_insn)(rtx, rtx, rtx);
10479
10480       bool eax_live = false;
10481       bool r10_live = false;
10482
10483       if (TARGET_64BIT)
10484         r10_live = (DECL_STATIC_CHAIN (current_function_decl) != 0);
10485       if (!TARGET_64BIT_MS_ABI)
10486         eax_live = ix86_eax_live_at_start_p ();
10487
10488       if (eax_live)
10489         {
10490           emit_insn (gen_push (eax));
10491           allocate -= UNITS_PER_WORD;
10492         }
10493       if (r10_live)
10494         {
10495           r10 = gen_rtx_REG (Pmode, R10_REG);
10496           emit_insn (gen_push (r10));
10497           allocate -= UNITS_PER_WORD;
10498         }
10499
10500       emit_move_insn (eax, GEN_INT (allocate));
10501       emit_insn (ix86_gen_allocate_stack_worker (eax, eax));
10502
10503       /* Use the fact that AX still contains ALLOCATE.  */
10504       adjust_stack_insn = (TARGET_64BIT
10505                            ? gen_pro_epilogue_adjust_stack_di_sub
10506                            : gen_pro_epilogue_adjust_stack_si_sub);
10507
10508       insn = emit_insn (adjust_stack_insn (stack_pointer_rtx,
10509                                            stack_pointer_rtx, eax));
10510
10511       /* Note that SEH directives need to continue tracking the stack
10512          pointer even after the frame pointer has been set up.  */
10513       if (m->fs.cfa_reg == stack_pointer_rtx || TARGET_SEH)
10514         {
10515           if (m->fs.cfa_reg == stack_pointer_rtx)
10516             m->fs.cfa_offset += allocate;
10517
10518           RTX_FRAME_RELATED_P (insn) = 1;
10519           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10520                         gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10521                                      plus_constant (stack_pointer_rtx,
10522                                                     -allocate)));
10523         }
10524       m->fs.sp_offset += allocate;
10525
10526       if (r10_live && eax_live)
10527         {
10528           t = choose_baseaddr (m->fs.sp_offset - allocate);
10529           emit_move_insn (r10, gen_frame_mem (Pmode, t));
10530           t = choose_baseaddr (m->fs.sp_offset - allocate - UNITS_PER_WORD);
10531           emit_move_insn (eax, gen_frame_mem (Pmode, t));
10532         }
10533       else if (eax_live || r10_live)
10534         {
10535           t = choose_baseaddr (m->fs.sp_offset - allocate);
10536           emit_move_insn ((eax_live ? eax : r10), gen_frame_mem (Pmode, t));
10537         }
10538     }
10539   gcc_assert (m->fs.sp_offset == frame.stack_pointer_offset);
10540
10541   /* If we havn't already set up the frame pointer, do so now.  */
10542   if (frame_pointer_needed && !m->fs.fp_valid)
10543     {
10544       insn = ix86_gen_add3 (hard_frame_pointer_rtx, stack_pointer_rtx,
10545                             GEN_INT (frame.stack_pointer_offset
10546                                      - frame.hard_frame_pointer_offset));
10547       insn = emit_insn (insn);
10548       RTX_FRAME_RELATED_P (insn) = 1;
10549       add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
10550
10551       if (m->fs.cfa_reg == stack_pointer_rtx)
10552         m->fs.cfa_reg = hard_frame_pointer_rtx;
10553       m->fs.fp_offset = frame.hard_frame_pointer_offset;
10554       m->fs.fp_valid = true;
10555     }
10556
10557   if (!int_registers_saved)
10558     ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10559   if (frame.nsseregs)
10560     ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
10561
10562   pic_reg_used = false;
10563   if (pic_offset_table_rtx
10564       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
10565           || crtl->profile))
10566     {
10567       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
10568
10569       if (alt_pic_reg_used != INVALID_REGNUM)
10570         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
10571
10572       pic_reg_used = true;
10573     }
10574
10575   if (pic_reg_used)
10576     {
10577       if (TARGET_64BIT)
10578         {
10579           if (ix86_cmodel == CM_LARGE_PIC)
10580             {
10581               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
10582               rtx label = gen_label_rtx ();
10583               emit_label (label);
10584               LABEL_PRESERVE_P (label) = 1;
10585               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
10586               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
10587               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
10588               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
10589                                             pic_offset_table_rtx, tmp_reg));
10590             }
10591           else
10592             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
10593         }
10594       else
10595         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
10596     }
10597
10598   /* In the pic_reg_used case, make sure that the got load isn't deleted
10599      when mcount needs it.  Blockage to avoid call movement across mcount
10600      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
10601      note.  */
10602   if (crtl->profile && !flag_fentry && pic_reg_used)
10603     emit_insn (gen_prologue_use (pic_offset_table_rtx));
10604
10605   if (crtl->drap_reg && !crtl->stack_realign_needed)
10606     {
10607       /* vDRAP is setup but after reload it turns out stack realign
10608          isn't necessary, here we will emit prologue to setup DRAP
10609          without stack realign adjustment */
10610       t = choose_baseaddr (0);
10611       emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10612     }
10613
10614   /* Prevent instructions from being scheduled into register save push
10615      sequence when access to the redzone area is done through frame pointer.
10616      The offset between the frame pointer and the stack pointer is calculated
10617      relative to the value of the stack pointer at the end of the function
10618      prologue, and moving instructions that access redzone area via frame
10619      pointer inside push sequence violates this assumption.  */
10620   if (frame_pointer_needed && frame.red_zone_size)
10621     emit_insn (gen_memory_blockage ());
10622
10623   /* Emit cld instruction if stringops are used in the function.  */
10624   if (TARGET_CLD && ix86_current_function_needs_cld)
10625     emit_insn (gen_cld ());
10626
10627   /* SEH requires that the prologue end within 256 bytes of the start of
10628      the function.  Prevent instruction schedules that would extend that.  */
10629   if (TARGET_SEH)
10630     emit_insn (gen_blockage ());
10631 }
10632
10633 /* Emit code to restore REG using a POP insn.  */
10634
10635 static void
10636 ix86_emit_restore_reg_using_pop (rtx reg)
10637 {
10638   struct machine_function *m = cfun->machine;
10639   rtx insn = emit_insn (gen_pop (reg));
10640
10641   ix86_add_cfa_restore_note (insn, reg, m->fs.sp_offset);
10642   m->fs.sp_offset -= UNITS_PER_WORD;
10643
10644   if (m->fs.cfa_reg == crtl->drap_reg
10645       && REGNO (reg) == REGNO (crtl->drap_reg))
10646     {
10647       /* Previously we'd represented the CFA as an expression
10648          like *(%ebp - 8).  We've just popped that value from
10649          the stack, which means we need to reset the CFA to
10650          the drap register.  This will remain until we restore
10651          the stack pointer.  */
10652       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10653       RTX_FRAME_RELATED_P (insn) = 1;
10654
10655       /* This means that the DRAP register is valid for addressing too.  */
10656       m->fs.drap_valid = true;
10657       return;
10658     }
10659
10660   if (m->fs.cfa_reg == stack_pointer_rtx)
10661     {
10662       rtx x = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
10663       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
10664       add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
10665       RTX_FRAME_RELATED_P (insn) = 1;
10666
10667       m->fs.cfa_offset -= UNITS_PER_WORD;
10668     }
10669
10670   /* When the frame pointer is the CFA, and we pop it, we are
10671      swapping back to the stack pointer as the CFA.  This happens
10672      for stack frames that don't allocate other data, so we assume
10673      the stack pointer is now pointing at the return address, i.e.
10674      the function entry state, which makes the offset be 1 word.  */
10675   if (reg == hard_frame_pointer_rtx)
10676     {
10677       m->fs.fp_valid = false;
10678       if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10679         {
10680           m->fs.cfa_reg = stack_pointer_rtx;
10681           m->fs.cfa_offset -= UNITS_PER_WORD;
10682
10683           add_reg_note (insn, REG_CFA_DEF_CFA,
10684                         gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10685                                       GEN_INT (m->fs.cfa_offset)));
10686           RTX_FRAME_RELATED_P (insn) = 1;
10687         }
10688     }
10689 }
10690
10691 /* Emit code to restore saved registers using POP insns.  */
10692
10693 static void
10694 ix86_emit_restore_regs_using_pop (void)
10695 {
10696   unsigned int regno;
10697
10698   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10699     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
10700       ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno));
10701 }
10702
10703 /* Emit code and notes for the LEAVE instruction.  */
10704
10705 static void
10706 ix86_emit_leave (void)
10707 {
10708   struct machine_function *m = cfun->machine;
10709   rtx insn = emit_insn (ix86_gen_leave ());
10710
10711   ix86_add_queued_cfa_restore_notes (insn);
10712
10713   gcc_assert (m->fs.fp_valid);
10714   m->fs.sp_valid = true;
10715   m->fs.sp_offset = m->fs.fp_offset - UNITS_PER_WORD;
10716   m->fs.fp_valid = false;
10717
10718   if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10719     {
10720       m->fs.cfa_reg = stack_pointer_rtx;
10721       m->fs.cfa_offset = m->fs.sp_offset;
10722
10723       add_reg_note (insn, REG_CFA_DEF_CFA,
10724                     plus_constant (stack_pointer_rtx, m->fs.sp_offset));
10725       RTX_FRAME_RELATED_P (insn) = 1;
10726       ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx,
10727                                  m->fs.fp_offset);
10728     }
10729 }
10730
10731 /* Emit code to restore saved registers using MOV insns.
10732    First register is restored from CFA - CFA_OFFSET.  */
10733 static void
10734 ix86_emit_restore_regs_using_mov (HOST_WIDE_INT cfa_offset,
10735                                   int maybe_eh_return)
10736 {
10737   struct machine_function *m = cfun->machine;
10738   unsigned int regno;
10739
10740   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10741     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10742       {
10743         rtx reg = gen_rtx_REG (Pmode, regno);
10744         rtx insn, mem;
10745         
10746         mem = choose_baseaddr (cfa_offset);
10747         mem = gen_frame_mem (Pmode, mem);
10748         insn = emit_move_insn (reg, mem);
10749
10750         if (m->fs.cfa_reg == crtl->drap_reg && regno == REGNO (crtl->drap_reg))
10751           {
10752             /* Previously we'd represented the CFA as an expression
10753                like *(%ebp - 8).  We've just popped that value from
10754                the stack, which means we need to reset the CFA to
10755                the drap register.  This will remain until we restore
10756                the stack pointer.  */
10757             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10758             RTX_FRAME_RELATED_P (insn) = 1;
10759
10760             /* This means that the DRAP register is valid for addressing.  */
10761             m->fs.drap_valid = true;
10762           }
10763         else
10764           ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10765
10766         cfa_offset -= UNITS_PER_WORD;
10767       }
10768 }
10769
10770 /* Emit code to restore saved registers using MOV insns.
10771    First register is restored from CFA - CFA_OFFSET.  */
10772 static void
10773 ix86_emit_restore_sse_regs_using_mov (HOST_WIDE_INT cfa_offset,
10774                                       int maybe_eh_return)
10775 {
10776   unsigned int regno;
10777
10778   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10779     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10780       {
10781         rtx reg = gen_rtx_REG (V4SFmode, regno);
10782         rtx mem;
10783
10784         mem = choose_baseaddr (cfa_offset);
10785         mem = gen_rtx_MEM (V4SFmode, mem);
10786         set_mem_align (mem, 128);
10787         emit_move_insn (reg, mem);
10788
10789         ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10790
10791         cfa_offset -= 16;
10792       }
10793 }
10794
10795 /* Restore function stack, frame, and registers.  */
10796
10797 void
10798 ix86_expand_epilogue (int style)
10799 {
10800   struct machine_function *m = cfun->machine;
10801   struct machine_frame_state frame_state_save = m->fs;
10802   struct ix86_frame frame;
10803   bool restore_regs_via_mov;
10804   bool using_drap;
10805
10806   ix86_finalize_stack_realign_flags ();
10807   ix86_compute_frame_layout (&frame);
10808
10809   m->fs.sp_valid = (!frame_pointer_needed
10810                     || (current_function_sp_is_unchanging
10811                         && !stack_realign_fp));
10812   gcc_assert (!m->fs.sp_valid
10813               || m->fs.sp_offset == frame.stack_pointer_offset);
10814
10815   /* The FP must be valid if the frame pointer is present.  */
10816   gcc_assert (frame_pointer_needed == m->fs.fp_valid);
10817   gcc_assert (!m->fs.fp_valid
10818               || m->fs.fp_offset == frame.hard_frame_pointer_offset);
10819
10820   /* We must have *some* valid pointer to the stack frame.  */
10821   gcc_assert (m->fs.sp_valid || m->fs.fp_valid);
10822
10823   /* The DRAP is never valid at this point.  */
10824   gcc_assert (!m->fs.drap_valid);
10825
10826   /* See the comment about red zone and frame
10827      pointer usage in ix86_expand_prologue.  */
10828   if (frame_pointer_needed && frame.red_zone_size)
10829     emit_insn (gen_memory_blockage ());
10830
10831   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
10832   gcc_assert (!using_drap || m->fs.cfa_reg == crtl->drap_reg);
10833
10834   /* Determine the CFA offset of the end of the red-zone.  */
10835   m->fs.red_zone_offset = 0;
10836   if (ix86_using_red_zone () && crtl->args.pops_args < 65536)
10837     {
10838       /* The red-zone begins below the return address.  */
10839       m->fs.red_zone_offset = RED_ZONE_SIZE + UNITS_PER_WORD;
10840
10841       /* When the register save area is in the aligned portion of
10842          the stack, determine the maximum runtime displacement that
10843          matches up with the aligned frame.  */
10844       if (stack_realign_drap)
10845         m->fs.red_zone_offset -= (crtl->stack_alignment_needed / BITS_PER_UNIT
10846                                   + UNITS_PER_WORD);
10847     }
10848
10849   /* Special care must be taken for the normal return case of a function
10850      using eh_return: the eax and edx registers are marked as saved, but
10851      not restored along this path.  Adjust the save location to match.  */
10852   if (crtl->calls_eh_return && style != 2)
10853     frame.reg_save_offset -= 2 * UNITS_PER_WORD;
10854
10855   /* EH_RETURN requires the use of moves to function properly.  */
10856   if (crtl->calls_eh_return)
10857     restore_regs_via_mov = true;
10858   /* SEH requires the use of pops to identify the epilogue.  */
10859   else if (TARGET_SEH)
10860     restore_regs_via_mov = false;
10861   /* If we're only restoring one register and sp is not valid then
10862      using a move instruction to restore the register since it's
10863      less work than reloading sp and popping the register.  */
10864   else if (!m->fs.sp_valid && frame.nregs <= 1)
10865     restore_regs_via_mov = true;
10866   else if (TARGET_EPILOGUE_USING_MOVE
10867            && cfun->machine->use_fast_prologue_epilogue
10868            && (frame.nregs > 1
10869                || m->fs.sp_offset != frame.reg_save_offset))
10870     restore_regs_via_mov = true;
10871   else if (frame_pointer_needed
10872            && !frame.nregs
10873            && m->fs.sp_offset != frame.reg_save_offset)
10874     restore_regs_via_mov = true;
10875   else if (frame_pointer_needed
10876            && TARGET_USE_LEAVE
10877            && cfun->machine->use_fast_prologue_epilogue
10878            && frame.nregs == 1)
10879     restore_regs_via_mov = true;
10880   else
10881     restore_regs_via_mov = false;
10882
10883   if (restore_regs_via_mov || frame.nsseregs)
10884     {
10885       /* Ensure that the entire register save area is addressable via
10886          the stack pointer, if we will restore via sp.  */
10887       if (TARGET_64BIT
10888           && m->fs.sp_offset > 0x7fffffff
10889           && !(m->fs.fp_valid || m->fs.drap_valid)
10890           && (frame.nsseregs + frame.nregs) != 0)
10891         {
10892           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10893                                      GEN_INT (m->fs.sp_offset
10894                                               - frame.sse_reg_save_offset),
10895                                      style,
10896                                      m->fs.cfa_reg == stack_pointer_rtx);
10897         }
10898     }
10899
10900   /* If there are any SSE registers to restore, then we have to do it
10901      via moves, since there's obviously no pop for SSE regs.  */
10902   if (frame.nsseregs)
10903     ix86_emit_restore_sse_regs_using_mov (frame.sse_reg_save_offset,
10904                                           style == 2);
10905
10906   if (restore_regs_via_mov)
10907     {
10908       rtx t;
10909
10910       if (frame.nregs)
10911         ix86_emit_restore_regs_using_mov (frame.reg_save_offset, style == 2);
10912
10913       /* eh_return epilogues need %ecx added to the stack pointer.  */
10914       if (style == 2)
10915         {
10916           rtx insn, sa = EH_RETURN_STACKADJ_RTX;
10917
10918           /* Stack align doesn't work with eh_return.  */
10919           gcc_assert (!stack_realign_drap);
10920           /* Neither does regparm nested functions.  */
10921           gcc_assert (!ix86_static_chain_on_stack);
10922
10923           if (frame_pointer_needed)
10924             {
10925               t = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
10926               t = plus_constant (t, m->fs.fp_offset - UNITS_PER_WORD);
10927               emit_insn (gen_rtx_SET (VOIDmode, sa, t));
10928
10929               t = gen_frame_mem (Pmode, hard_frame_pointer_rtx);
10930               insn = emit_move_insn (hard_frame_pointer_rtx, t);
10931
10932               /* Note that we use SA as a temporary CFA, as the return
10933                  address is at the proper place relative to it.  We
10934                  pretend this happens at the FP restore insn because
10935                  prior to this insn the FP would be stored at the wrong
10936                  offset relative to SA, and after this insn we have no
10937                  other reasonable register to use for the CFA.  We don't
10938                  bother resetting the CFA to the SP for the duration of
10939                  the return insn.  */
10940               add_reg_note (insn, REG_CFA_DEF_CFA,
10941                             plus_constant (sa, UNITS_PER_WORD));
10942               ix86_add_queued_cfa_restore_notes (insn);
10943               add_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx);
10944               RTX_FRAME_RELATED_P (insn) = 1;
10945
10946               m->fs.cfa_reg = sa;
10947               m->fs.cfa_offset = UNITS_PER_WORD;
10948               m->fs.fp_valid = false;
10949
10950               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
10951                                          const0_rtx, style, false);
10952             }
10953           else
10954             {
10955               t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
10956               t = plus_constant (t, m->fs.sp_offset - UNITS_PER_WORD);
10957               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, t));
10958               ix86_add_queued_cfa_restore_notes (insn);
10959
10960               gcc_assert (m->fs.cfa_reg == stack_pointer_rtx);
10961               if (m->fs.cfa_offset != UNITS_PER_WORD)
10962                 {
10963                   m->fs.cfa_offset = UNITS_PER_WORD;
10964                   add_reg_note (insn, REG_CFA_DEF_CFA,
10965                                 plus_constant (stack_pointer_rtx,
10966                                                UNITS_PER_WORD));
10967                   RTX_FRAME_RELATED_P (insn) = 1;
10968                 }
10969             }
10970           m->fs.sp_offset = UNITS_PER_WORD;
10971           m->fs.sp_valid = true;
10972         }
10973     }
10974   else
10975     {
10976       /* SEH requires that the function end with (1) a stack adjustment
10977          if necessary, (2) a sequence of pops, and (3) a return or
10978          jump instruction.  Prevent insns from the function body from
10979          being scheduled into this sequence.  */
10980       if (TARGET_SEH)
10981         {
10982           /* Prevent a catch region from being adjacent to the standard
10983              epilogue sequence.  Unfortuantely crtl->uses_eh_lsda nor
10984              several other flags that would be interesting to test are
10985              not yet set up.  */
10986           if (flag_non_call_exceptions)
10987             emit_insn (gen_nops (const1_rtx));
10988           else
10989             emit_insn (gen_blockage ());
10990         }
10991
10992       /* First step is to deallocate the stack frame so that we can
10993          pop the registers.  */
10994       if (!m->fs.sp_valid)
10995         {
10996           pro_epilogue_adjust_stack (stack_pointer_rtx, hard_frame_pointer_rtx,
10997                                      GEN_INT (m->fs.fp_offset
10998                                               - frame.reg_save_offset),
10999                                      style, false);
11000         }
11001       else if (m->fs.sp_offset != frame.reg_save_offset)
11002         {
11003           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
11004                                      GEN_INT (m->fs.sp_offset
11005                                               - frame.reg_save_offset),
11006                                      style,
11007                                      m->fs.cfa_reg == stack_pointer_rtx);
11008         }
11009
11010       ix86_emit_restore_regs_using_pop ();
11011     }
11012
11013   /* If we used a stack pointer and haven't already got rid of it,
11014      then do so now.  */
11015   if (m->fs.fp_valid)
11016     {
11017       /* If the stack pointer is valid and pointing at the frame
11018          pointer store address, then we only need a pop.  */
11019       if (m->fs.sp_valid && m->fs.sp_offset == frame.hfp_save_offset)
11020         ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
11021       /* Leave results in shorter dependency chains on CPUs that are
11022          able to grok it fast.  */
11023       else if (TARGET_USE_LEAVE
11024                || optimize_function_for_size_p (cfun)
11025                || !cfun->machine->use_fast_prologue_epilogue)
11026         ix86_emit_leave ();
11027       else
11028         {
11029           pro_epilogue_adjust_stack (stack_pointer_rtx,
11030                                      hard_frame_pointer_rtx,
11031                                      const0_rtx, style, !using_drap);
11032           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
11033         }
11034     }
11035
11036   if (using_drap)
11037     {
11038       int param_ptr_offset = UNITS_PER_WORD;
11039       rtx insn;
11040
11041       gcc_assert (stack_realign_drap);
11042
11043       if (ix86_static_chain_on_stack)
11044         param_ptr_offset += UNITS_PER_WORD;
11045       if (!call_used_regs[REGNO (crtl->drap_reg)])
11046         param_ptr_offset += UNITS_PER_WORD;
11047
11048       insn = emit_insn (gen_rtx_SET
11049                         (VOIDmode, stack_pointer_rtx,
11050                          gen_rtx_PLUS (Pmode,
11051                                        crtl->drap_reg,
11052                                        GEN_INT (-param_ptr_offset))));
11053       m->fs.cfa_reg = stack_pointer_rtx;
11054       m->fs.cfa_offset = param_ptr_offset;
11055       m->fs.sp_offset = param_ptr_offset;
11056       m->fs.realigned = false;
11057
11058       add_reg_note (insn, REG_CFA_DEF_CFA,
11059                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11060                                   GEN_INT (param_ptr_offset)));
11061       RTX_FRAME_RELATED_P (insn) = 1;
11062
11063       if (!call_used_regs[REGNO (crtl->drap_reg)])
11064         ix86_emit_restore_reg_using_pop (crtl->drap_reg);
11065     }
11066
11067   /* At this point the stack pointer must be valid, and we must have
11068      restored all of the registers.  We may not have deallocated the
11069      entire stack frame.  We've delayed this until now because it may
11070      be possible to merge the local stack deallocation with the
11071      deallocation forced by ix86_static_chain_on_stack.   */
11072   gcc_assert (m->fs.sp_valid);
11073   gcc_assert (!m->fs.fp_valid);
11074   gcc_assert (!m->fs.realigned);
11075   if (m->fs.sp_offset != UNITS_PER_WORD)
11076     {
11077       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
11078                                  GEN_INT (m->fs.sp_offset - UNITS_PER_WORD),
11079                                  style, true);
11080     }
11081
11082   /* Sibcall epilogues don't want a return instruction.  */
11083   if (style == 0)
11084     {
11085       m->fs = frame_state_save;
11086       return;
11087     }
11088
11089   /* Emit vzeroupper if needed.  */
11090   if (TARGET_VZEROUPPER
11091       && !TREE_THIS_VOLATILE (cfun->decl)
11092       && !cfun->machine->caller_return_avx256_p)
11093     emit_insn (gen_avx_vzeroupper (GEN_INT (call_no_avx256))); 
11094
11095   if (crtl->args.pops_args && crtl->args.size)
11096     {
11097       rtx popc = GEN_INT (crtl->args.pops_args);
11098
11099       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
11100          address, do explicit add, and jump indirectly to the caller.  */
11101
11102       if (crtl->args.pops_args >= 65536)
11103         {
11104           rtx ecx = gen_rtx_REG (SImode, CX_REG);
11105           rtx insn;
11106
11107           /* There is no "pascal" calling convention in any 64bit ABI.  */
11108           gcc_assert (!TARGET_64BIT);
11109
11110           insn = emit_insn (gen_pop (ecx));
11111           m->fs.cfa_offset -= UNITS_PER_WORD;
11112           m->fs.sp_offset -= UNITS_PER_WORD;
11113
11114           add_reg_note (insn, REG_CFA_ADJUST_CFA,
11115                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
11116           add_reg_note (insn, REG_CFA_REGISTER,
11117                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
11118           RTX_FRAME_RELATED_P (insn) = 1;
11119
11120           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
11121                                      popc, -1, true);
11122           emit_jump_insn (gen_return_indirect_internal (ecx));
11123         }
11124       else
11125         emit_jump_insn (gen_return_pop_internal (popc));
11126     }
11127   else
11128     emit_jump_insn (gen_return_internal ());
11129
11130   /* Restore the state back to the state from the prologue,
11131      so that it's correct for the next epilogue.  */
11132   m->fs = frame_state_save;
11133 }
11134
11135 /* Reset from the function's potential modifications.  */
11136
11137 static void
11138 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11139                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11140 {
11141   if (pic_offset_table_rtx)
11142     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
11143 #if TARGET_MACHO
11144   /* Mach-O doesn't support labels at the end of objects, so if
11145      it looks like we might want one, insert a NOP.  */
11146   {
11147     rtx insn = get_last_insn ();
11148     while (insn
11149            && NOTE_P (insn)
11150            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
11151       insn = PREV_INSN (insn);
11152     if (insn
11153         && (LABEL_P (insn)
11154             || (NOTE_P (insn)
11155                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
11156       fputs ("\tnop\n", file);
11157   }
11158 #endif
11159
11160 }
11161
11162 /* Return a scratch register to use in the split stack prologue.  The
11163    split stack prologue is used for -fsplit-stack.  It is the first
11164    instructions in the function, even before the regular prologue.
11165    The scratch register can be any caller-saved register which is not
11166    used for parameters or for the static chain.  */
11167
11168 static unsigned int
11169 split_stack_prologue_scratch_regno (void)
11170 {
11171   if (TARGET_64BIT)
11172     return R11_REG;
11173   else
11174     {
11175       bool is_fastcall;
11176       int regparm;
11177
11178       is_fastcall = (lookup_attribute ("fastcall",
11179                                        TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
11180                      != NULL);
11181       regparm = ix86_function_regparm (TREE_TYPE (cfun->decl), cfun->decl);
11182
11183       if (is_fastcall)
11184         {
11185           if (DECL_STATIC_CHAIN (cfun->decl))
11186             {
11187               sorry ("-fsplit-stack does not support fastcall with "
11188                      "nested function");
11189               return INVALID_REGNUM;
11190             }
11191           return AX_REG;
11192         }
11193       else if (regparm < 3)
11194         {
11195           if (!DECL_STATIC_CHAIN (cfun->decl))
11196             return CX_REG;
11197           else
11198             {
11199               if (regparm >= 2)
11200                 {
11201                   sorry ("-fsplit-stack does not support 2 register "
11202                          " parameters for a nested function");
11203                   return INVALID_REGNUM;
11204                 }
11205               return DX_REG;
11206             }
11207         }
11208       else
11209         {
11210           /* FIXME: We could make this work by pushing a register
11211              around the addition and comparison.  */
11212           sorry ("-fsplit-stack does not support 3 register parameters");
11213           return INVALID_REGNUM;
11214         }
11215     }
11216 }
11217
11218 /* A SYMBOL_REF for the function which allocates new stackspace for
11219    -fsplit-stack.  */
11220
11221 static GTY(()) rtx split_stack_fn;
11222
11223 /* A SYMBOL_REF for the more stack function when using the large
11224    model.  */
11225
11226 static GTY(()) rtx split_stack_fn_large;
11227
11228 /* Handle -fsplit-stack.  These are the first instructions in the
11229    function, even before the regular prologue.  */
11230
11231 void
11232 ix86_expand_split_stack_prologue (void)
11233 {
11234   struct ix86_frame frame;
11235   HOST_WIDE_INT allocate;
11236   unsigned HOST_WIDE_INT args_size;
11237   rtx label, limit, current, jump_insn, allocate_rtx, call_insn, call_fusage;
11238   rtx scratch_reg = NULL_RTX;
11239   rtx varargs_label = NULL_RTX;
11240   rtx fn;
11241
11242   gcc_assert (flag_split_stack && reload_completed);
11243
11244   ix86_finalize_stack_realign_flags ();
11245   ix86_compute_frame_layout (&frame);
11246   allocate = frame.stack_pointer_offset - INCOMING_FRAME_SP_OFFSET;
11247
11248   /* This is the label we will branch to if we have enough stack
11249      space.  We expect the basic block reordering pass to reverse this
11250      branch if optimizing, so that we branch in the unlikely case.  */
11251   label = gen_label_rtx ();
11252
11253   /* We need to compare the stack pointer minus the frame size with
11254      the stack boundary in the TCB.  The stack boundary always gives
11255      us SPLIT_STACK_AVAILABLE bytes, so if we need less than that we
11256      can compare directly.  Otherwise we need to do an addition.  */
11257
11258   limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
11259                           UNSPEC_STACK_CHECK);
11260   limit = gen_rtx_CONST (Pmode, limit);
11261   limit = gen_rtx_MEM (Pmode, limit);
11262   if (allocate < SPLIT_STACK_AVAILABLE)
11263     current = stack_pointer_rtx;
11264   else
11265     {
11266       unsigned int scratch_regno;
11267       rtx offset;
11268
11269       /* We need a scratch register to hold the stack pointer minus
11270          the required frame size.  Since this is the very start of the
11271          function, the scratch register can be any caller-saved
11272          register which is not used for parameters.  */
11273       offset = GEN_INT (- allocate);
11274       scratch_regno = split_stack_prologue_scratch_regno ();
11275       if (scratch_regno == INVALID_REGNUM)
11276         return;
11277       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11278       if (!TARGET_64BIT || x86_64_immediate_operand (offset, Pmode))
11279         {
11280           /* We don't use ix86_gen_add3 in this case because it will
11281              want to split to lea, but when not optimizing the insn
11282              will not be split after this point.  */
11283           emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11284                                   gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11285                                                 offset)));
11286         }
11287       else
11288         {
11289           emit_move_insn (scratch_reg, offset);
11290           emit_insn (gen_adddi3 (scratch_reg, scratch_reg,
11291                                  stack_pointer_rtx));
11292         }
11293       current = scratch_reg;
11294     }
11295
11296   ix86_expand_branch (GEU, current, limit, label);
11297   jump_insn = get_last_insn ();
11298   JUMP_LABEL (jump_insn) = label;
11299
11300   /* Mark the jump as very likely to be taken.  */
11301   add_reg_note (jump_insn, REG_BR_PROB,
11302                 GEN_INT (REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100));
11303
11304   if (split_stack_fn == NULL_RTX)
11305     split_stack_fn = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
11306   fn = split_stack_fn;
11307
11308   /* Get more stack space.  We pass in the desired stack space and the
11309      size of the arguments to copy to the new stack.  In 32-bit mode
11310      we push the parameters; __morestack will return on a new stack
11311      anyhow.  In 64-bit mode we pass the parameters in r10 and
11312      r11.  */
11313   allocate_rtx = GEN_INT (allocate);
11314   args_size = crtl->args.size >= 0 ? crtl->args.size : 0;
11315   call_fusage = NULL_RTX;
11316   if (TARGET_64BIT)
11317     {
11318       rtx reg10, reg11;
11319
11320       reg10 = gen_rtx_REG (Pmode, R10_REG);
11321       reg11 = gen_rtx_REG (Pmode, R11_REG);
11322
11323       /* If this function uses a static chain, it will be in %r10.
11324          Preserve it across the call to __morestack.  */
11325       if (DECL_STATIC_CHAIN (cfun->decl))
11326         {
11327           rtx rax;
11328
11329           rax = gen_rtx_REG (Pmode, AX_REG);
11330           emit_move_insn (rax, reg10);
11331           use_reg (&call_fusage, rax);
11332         }
11333
11334       if (ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
11335         {
11336           HOST_WIDE_INT argval;
11337
11338           /* When using the large model we need to load the address
11339              into a register, and we've run out of registers.  So we
11340              switch to a different calling convention, and we call a
11341              different function: __morestack_large.  We pass the
11342              argument size in the upper 32 bits of r10 and pass the
11343              frame size in the lower 32 bits.  */
11344           gcc_assert ((allocate & (HOST_WIDE_INT) 0xffffffff) == allocate);
11345           gcc_assert ((args_size & 0xffffffff) == args_size);
11346
11347           if (split_stack_fn_large == NULL_RTX)
11348             split_stack_fn_large =
11349               gen_rtx_SYMBOL_REF (Pmode, "__morestack_large_model");
11350
11351           if (ix86_cmodel == CM_LARGE_PIC)
11352             {
11353               rtx label, x;
11354
11355               label = gen_label_rtx ();
11356               emit_label (label);
11357               LABEL_PRESERVE_P (label) = 1;
11358               emit_insn (gen_set_rip_rex64 (reg10, label));
11359               emit_insn (gen_set_got_offset_rex64 (reg11, label));
11360               emit_insn (gen_adddi3 (reg10, reg10, reg11));
11361               x = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, split_stack_fn_large),
11362                                   UNSPEC_GOT);
11363               x = gen_rtx_CONST (Pmode, x);
11364               emit_move_insn (reg11, x);
11365               x = gen_rtx_PLUS (Pmode, reg10, reg11);
11366               x = gen_const_mem (Pmode, x);
11367               emit_move_insn (reg11, x);
11368             }
11369           else
11370             emit_move_insn (reg11, split_stack_fn_large);
11371
11372           fn = reg11;
11373
11374           argval = ((args_size << 16) << 16) + allocate;
11375           emit_move_insn (reg10, GEN_INT (argval));
11376         }
11377       else
11378         {
11379           emit_move_insn (reg10, allocate_rtx);
11380           emit_move_insn (reg11, GEN_INT (args_size));
11381           use_reg (&call_fusage, reg11);
11382         }
11383
11384       use_reg (&call_fusage, reg10);
11385     }
11386   else
11387     {
11388       emit_insn (gen_push (GEN_INT (args_size)));
11389       emit_insn (gen_push (allocate_rtx));
11390     }
11391   call_insn = ix86_expand_call (NULL_RTX, gen_rtx_MEM (QImode, fn),
11392                                 GEN_INT (UNITS_PER_WORD), constm1_rtx,
11393                                 NULL_RTX, 0);
11394   add_function_usage_to (call_insn, call_fusage);
11395
11396   /* In order to make call/return prediction work right, we now need
11397      to execute a return instruction.  See
11398      libgcc/config/i386/morestack.S for the details on how this works.
11399
11400      For flow purposes gcc must not see this as a return
11401      instruction--we need control flow to continue at the subsequent
11402      label.  Therefore, we use an unspec.  */
11403   gcc_assert (crtl->args.pops_args < 65536);
11404   emit_insn (gen_split_stack_return (GEN_INT (crtl->args.pops_args)));
11405
11406   /* If we are in 64-bit mode and this function uses a static chain,
11407      we saved %r10 in %rax before calling _morestack.  */
11408   if (TARGET_64BIT && DECL_STATIC_CHAIN (cfun->decl))
11409     emit_move_insn (gen_rtx_REG (Pmode, R10_REG),
11410                     gen_rtx_REG (Pmode, AX_REG));
11411
11412   /* If this function calls va_start, we need to store a pointer to
11413      the arguments on the old stack, because they may not have been
11414      all copied to the new stack.  At this point the old stack can be
11415      found at the frame pointer value used by __morestack, because
11416      __morestack has set that up before calling back to us.  Here we
11417      store that pointer in a scratch register, and in
11418      ix86_expand_prologue we store the scratch register in a stack
11419      slot.  */
11420   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11421     {
11422       unsigned int scratch_regno;
11423       rtx frame_reg;
11424       int words;
11425
11426       scratch_regno = split_stack_prologue_scratch_regno ();
11427       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11428       frame_reg = gen_rtx_REG (Pmode, BP_REG);
11429
11430       /* 64-bit:
11431          fp -> old fp value
11432                return address within this function
11433                return address of caller of this function
11434                stack arguments
11435          So we add three words to get to the stack arguments.
11436
11437          32-bit:
11438          fp -> old fp value
11439                return address within this function
11440                first argument to __morestack
11441                second argument to __morestack
11442                return address of caller of this function
11443                stack arguments
11444          So we add five words to get to the stack arguments.
11445       */
11446       words = TARGET_64BIT ? 3 : 5;
11447       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11448                               gen_rtx_PLUS (Pmode, frame_reg,
11449                                             GEN_INT (words * UNITS_PER_WORD))));
11450
11451       varargs_label = gen_label_rtx ();
11452       emit_jump_insn (gen_jump (varargs_label));
11453       JUMP_LABEL (get_last_insn ()) = varargs_label;
11454
11455       emit_barrier ();
11456     }
11457
11458   emit_label (label);
11459   LABEL_NUSES (label) = 1;
11460
11461   /* If this function calls va_start, we now have to set the scratch
11462      register for the case where we do not call __morestack.  In this
11463      case we need to set it based on the stack pointer.  */
11464   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11465     {
11466       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11467                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11468                                             GEN_INT (UNITS_PER_WORD))));
11469
11470       emit_label (varargs_label);
11471       LABEL_NUSES (varargs_label) = 1;
11472     }
11473 }
11474
11475 /* We may have to tell the dataflow pass that the split stack prologue
11476    is initializing a scratch register.  */
11477
11478 static void
11479 ix86_live_on_entry (bitmap regs)
11480 {
11481   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11482     {
11483       gcc_assert (flag_split_stack);
11484       bitmap_set_bit (regs, split_stack_prologue_scratch_regno ());
11485     }
11486 }
11487 \f
11488 /* Extract the parts of an RTL expression that is a valid memory address
11489    for an instruction.  Return 0 if the structure of the address is
11490    grossly off.  Return -1 if the address contains ASHIFT, so it is not
11491    strictly valid, but still used for computing length of lea instruction.  */
11492
11493 int
11494 ix86_decompose_address (rtx addr, struct ix86_address *out)
11495 {
11496   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
11497   rtx base_reg, index_reg;
11498   HOST_WIDE_INT scale = 1;
11499   rtx scale_rtx = NULL_RTX;
11500   rtx tmp;
11501   int retval = 1;
11502   enum ix86_address_seg seg = SEG_DEFAULT;
11503
11504   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
11505     base = addr;
11506   else if (GET_CODE (addr) == PLUS)
11507     {
11508       rtx addends[4], op;
11509       int n = 0, i;
11510
11511       op = addr;
11512       do
11513         {
11514           if (n >= 4)
11515             return 0;
11516           addends[n++] = XEXP (op, 1);
11517           op = XEXP (op, 0);
11518         }
11519       while (GET_CODE (op) == PLUS);
11520       if (n >= 4)
11521         return 0;
11522       addends[n] = op;
11523
11524       for (i = n; i >= 0; --i)
11525         {
11526           op = addends[i];
11527           switch (GET_CODE (op))
11528             {
11529             case MULT:
11530               if (index)
11531                 return 0;
11532               index = XEXP (op, 0);
11533               scale_rtx = XEXP (op, 1);
11534               break;
11535
11536             case ASHIFT:
11537               if (index)
11538                 return 0;
11539               index = XEXP (op, 0);
11540               tmp = XEXP (op, 1);
11541               if (!CONST_INT_P (tmp))
11542                 return 0;
11543               scale = INTVAL (tmp);
11544               if ((unsigned HOST_WIDE_INT) scale > 3)
11545                 return 0;
11546               scale = 1 << scale;
11547               break;
11548
11549             case UNSPEC:
11550               if (XINT (op, 1) == UNSPEC_TP
11551                   && TARGET_TLS_DIRECT_SEG_REFS
11552                   && seg == SEG_DEFAULT)
11553                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
11554               else
11555                 return 0;
11556               break;
11557
11558             case REG:
11559             case SUBREG:
11560               if (!base)
11561                 base = op;
11562               else if (!index)
11563                 index = op;
11564               else
11565                 return 0;
11566               break;
11567
11568             case CONST:
11569             case CONST_INT:
11570             case SYMBOL_REF:
11571             case LABEL_REF:
11572               if (disp)
11573                 return 0;
11574               disp = op;
11575               break;
11576
11577             default:
11578               return 0;
11579             }
11580         }
11581     }
11582   else if (GET_CODE (addr) == MULT)
11583     {
11584       index = XEXP (addr, 0);           /* index*scale */
11585       scale_rtx = XEXP (addr, 1);
11586     }
11587   else if (GET_CODE (addr) == ASHIFT)
11588     {
11589       /* We're called for lea too, which implements ashift on occasion.  */
11590       index = XEXP (addr, 0);
11591       tmp = XEXP (addr, 1);
11592       if (!CONST_INT_P (tmp))
11593         return 0;
11594       scale = INTVAL (tmp);
11595       if ((unsigned HOST_WIDE_INT) scale > 3)
11596         return 0;
11597       scale = 1 << scale;
11598       retval = -1;
11599     }
11600   else
11601     disp = addr;                        /* displacement */
11602
11603   /* Extract the integral value of scale.  */
11604   if (scale_rtx)
11605     {
11606       if (!CONST_INT_P (scale_rtx))
11607         return 0;
11608       scale = INTVAL (scale_rtx);
11609     }
11610
11611   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
11612   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
11613
11614   /* Avoid useless 0 displacement.  */
11615   if (disp == const0_rtx && (base || index))
11616     disp = NULL_RTX;
11617
11618   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
11619   if (base_reg && index_reg && scale == 1
11620       && (index_reg == arg_pointer_rtx
11621           || index_reg == frame_pointer_rtx
11622           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
11623     {
11624       rtx tmp;
11625       tmp = base, base = index, index = tmp;
11626       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
11627     }
11628
11629   /* Special case: %ebp cannot be encoded as a base without a displacement.
11630      Similarly %r13.  */
11631   if (!disp
11632       && base_reg
11633       && (base_reg == hard_frame_pointer_rtx
11634           || base_reg == frame_pointer_rtx
11635           || base_reg == arg_pointer_rtx
11636           || (REG_P (base_reg)
11637               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
11638                   || REGNO (base_reg) == R13_REG))))
11639     disp = const0_rtx;
11640
11641   /* Special case: on K6, [%esi] makes the instruction vector decoded.
11642      Avoid this by transforming to [%esi+0].
11643      Reload calls address legitimization without cfun defined, so we need
11644      to test cfun for being non-NULL. */
11645   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
11646       && base_reg && !index_reg && !disp
11647       && REG_P (base_reg) && REGNO (base_reg) == SI_REG)
11648     disp = const0_rtx;
11649
11650   /* Special case: encode reg+reg instead of reg*2.  */
11651   if (!base && index && scale == 2)
11652     base = index, base_reg = index_reg, scale = 1;
11653
11654   /* Special case: scaling cannot be encoded without base or displacement.  */
11655   if (!base && !disp && index && scale != 1)
11656     disp = const0_rtx;
11657
11658   out->base = base;
11659   out->index = index;
11660   out->disp = disp;
11661   out->scale = scale;
11662   out->seg = seg;
11663
11664   return retval;
11665 }
11666 \f
11667 /* Return cost of the memory address x.
11668    For i386, it is better to use a complex address than let gcc copy
11669    the address into a reg and make a new pseudo.  But not if the address
11670    requires to two regs - that would mean more pseudos with longer
11671    lifetimes.  */
11672 static int
11673 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
11674 {
11675   struct ix86_address parts;
11676   int cost = 1;
11677   int ok = ix86_decompose_address (x, &parts);
11678
11679   gcc_assert (ok);
11680
11681   if (parts.base && GET_CODE (parts.base) == SUBREG)
11682     parts.base = SUBREG_REG (parts.base);
11683   if (parts.index && GET_CODE (parts.index) == SUBREG)
11684     parts.index = SUBREG_REG (parts.index);
11685
11686   /* Attempt to minimize number of registers in the address.  */
11687   if ((parts.base
11688        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
11689       || (parts.index
11690           && (!REG_P (parts.index)
11691               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
11692     cost++;
11693
11694   if (parts.base
11695       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
11696       && parts.index
11697       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
11698       && parts.base != parts.index)
11699     cost++;
11700
11701   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
11702      since it's predecode logic can't detect the length of instructions
11703      and it degenerates to vector decoded.  Increase cost of such
11704      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
11705      to split such addresses or even refuse such addresses at all.
11706
11707      Following addressing modes are affected:
11708       [base+scale*index]
11709       [scale*index+disp]
11710       [base+index]
11711
11712      The first and last case  may be avoidable by explicitly coding the zero in
11713      memory address, but I don't have AMD-K6 machine handy to check this
11714      theory.  */
11715
11716   if (TARGET_K6
11717       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
11718           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
11719           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
11720     cost += 10;
11721
11722   return cost;
11723 }
11724 \f
11725 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
11726    this is used for to form addresses to local data when -fPIC is in
11727    use.  */
11728
11729 static bool
11730 darwin_local_data_pic (rtx disp)
11731 {
11732   return (GET_CODE (disp) == UNSPEC
11733           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
11734 }
11735
11736 /* Determine if a given RTX is a valid constant.  We already know this
11737    satisfies CONSTANT_P.  */
11738
11739 bool
11740 legitimate_constant_p (rtx x)
11741 {
11742   switch (GET_CODE (x))
11743     {
11744     case CONST:
11745       x = XEXP (x, 0);
11746
11747       if (GET_CODE (x) == PLUS)
11748         {
11749           if (!CONST_INT_P (XEXP (x, 1)))
11750             return false;
11751           x = XEXP (x, 0);
11752         }
11753
11754       if (TARGET_MACHO && darwin_local_data_pic (x))
11755         return true;
11756
11757       /* Only some unspecs are valid as "constants".  */
11758       if (GET_CODE (x) == UNSPEC)
11759         switch (XINT (x, 1))
11760           {
11761           case UNSPEC_GOT:
11762           case UNSPEC_GOTOFF:
11763           case UNSPEC_PLTOFF:
11764             return TARGET_64BIT;
11765           case UNSPEC_TPOFF:
11766           case UNSPEC_NTPOFF:
11767             x = XVECEXP (x, 0, 0);
11768             return (GET_CODE (x) == SYMBOL_REF
11769                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11770           case UNSPEC_DTPOFF:
11771             x = XVECEXP (x, 0, 0);
11772             return (GET_CODE (x) == SYMBOL_REF
11773                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
11774           default:
11775             return false;
11776           }
11777
11778       /* We must have drilled down to a symbol.  */
11779       if (GET_CODE (x) == LABEL_REF)
11780         return true;
11781       if (GET_CODE (x) != SYMBOL_REF)
11782         return false;
11783       /* FALLTHRU */
11784
11785     case SYMBOL_REF:
11786       /* TLS symbols are never valid.  */
11787       if (SYMBOL_REF_TLS_MODEL (x))
11788         return false;
11789
11790       /* DLLIMPORT symbols are never valid.  */
11791       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
11792           && SYMBOL_REF_DLLIMPORT_P (x))
11793         return false;
11794
11795 #if TARGET_MACHO
11796       /* mdynamic-no-pic */
11797       if (MACHO_DYNAMIC_NO_PIC_P)
11798         return machopic_symbol_defined_p (x);
11799 #endif
11800       break;
11801
11802     case CONST_DOUBLE:
11803       if (GET_MODE (x) == TImode
11804           && x != CONST0_RTX (TImode)
11805           && !TARGET_64BIT)
11806         return false;
11807       break;
11808
11809     case CONST_VECTOR:
11810       if (!standard_sse_constant_p (x))
11811         return false;
11812
11813     default:
11814       break;
11815     }
11816
11817   /* Otherwise we handle everything else in the move patterns.  */
11818   return true;
11819 }
11820
11821 /* Determine if it's legal to put X into the constant pool.  This
11822    is not possible for the address of thread-local symbols, which
11823    is checked above.  */
11824
11825 static bool
11826 ix86_cannot_force_const_mem (rtx x)
11827 {
11828   /* We can always put integral constants and vectors in memory.  */
11829   switch (GET_CODE (x))
11830     {
11831     case CONST_INT:
11832     case CONST_DOUBLE:
11833     case CONST_VECTOR:
11834       return false;
11835
11836     default:
11837       break;
11838     }
11839   return !legitimate_constant_p (x);
11840 }
11841
11842
11843 /* Nonzero if the constant value X is a legitimate general operand
11844    when generating PIC code.  It is given that flag_pic is on and
11845    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
11846
11847 bool
11848 legitimate_pic_operand_p (rtx x)
11849 {
11850   rtx inner;
11851
11852   switch (GET_CODE (x))
11853     {
11854     case CONST:
11855       inner = XEXP (x, 0);
11856       if (GET_CODE (inner) == PLUS
11857           && CONST_INT_P (XEXP (inner, 1)))
11858         inner = XEXP (inner, 0);
11859
11860       /* Only some unspecs are valid as "constants".  */
11861       if (GET_CODE (inner) == UNSPEC)
11862         switch (XINT (inner, 1))
11863           {
11864           case UNSPEC_GOT:
11865           case UNSPEC_GOTOFF:
11866           case UNSPEC_PLTOFF:
11867             return TARGET_64BIT;
11868           case UNSPEC_TPOFF:
11869             x = XVECEXP (inner, 0, 0);
11870             return (GET_CODE (x) == SYMBOL_REF
11871                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11872           case UNSPEC_MACHOPIC_OFFSET:
11873             return legitimate_pic_address_disp_p (x);
11874           default:
11875             return false;
11876           }
11877       /* FALLTHRU */
11878
11879     case SYMBOL_REF:
11880     case LABEL_REF:
11881       return legitimate_pic_address_disp_p (x);
11882
11883     default:
11884       return true;
11885     }
11886 }
11887
11888 /* Determine if a given CONST RTX is a valid memory displacement
11889    in PIC mode.  */
11890
11891 bool
11892 legitimate_pic_address_disp_p (rtx disp)
11893 {
11894   bool saw_plus;
11895
11896   /* In 64bit mode we can allow direct addresses of symbols and labels
11897      when they are not dynamic symbols.  */
11898   if (TARGET_64BIT)
11899     {
11900       rtx op0 = disp, op1;
11901
11902       switch (GET_CODE (disp))
11903         {
11904         case LABEL_REF:
11905           return true;
11906
11907         case CONST:
11908           if (GET_CODE (XEXP (disp, 0)) != PLUS)
11909             break;
11910           op0 = XEXP (XEXP (disp, 0), 0);
11911           op1 = XEXP (XEXP (disp, 0), 1);
11912           if (!CONST_INT_P (op1)
11913               || INTVAL (op1) >= 16*1024*1024
11914               || INTVAL (op1) < -16*1024*1024)
11915             break;
11916           if (GET_CODE (op0) == LABEL_REF)
11917             return true;
11918           if (GET_CODE (op0) != SYMBOL_REF)
11919             break;
11920           /* FALLTHRU */
11921
11922         case SYMBOL_REF:
11923           /* TLS references should always be enclosed in UNSPEC.  */
11924           if (SYMBOL_REF_TLS_MODEL (op0))
11925             return false;
11926           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
11927               && ix86_cmodel != CM_LARGE_PIC)
11928             return true;
11929           break;
11930
11931         default:
11932           break;
11933         }
11934     }
11935   if (GET_CODE (disp) != CONST)
11936     return false;
11937   disp = XEXP (disp, 0);
11938
11939   if (TARGET_64BIT)
11940     {
11941       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
11942          of GOT tables.  We should not need these anyway.  */
11943       if (GET_CODE (disp) != UNSPEC
11944           || (XINT (disp, 1) != UNSPEC_GOTPCREL
11945               && XINT (disp, 1) != UNSPEC_GOTOFF
11946               && XINT (disp, 1) != UNSPEC_PCREL
11947               && XINT (disp, 1) != UNSPEC_PLTOFF))
11948         return false;
11949
11950       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
11951           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
11952         return false;
11953       return true;
11954     }
11955
11956   saw_plus = false;
11957   if (GET_CODE (disp) == PLUS)
11958     {
11959       if (!CONST_INT_P (XEXP (disp, 1)))
11960         return false;
11961       disp = XEXP (disp, 0);
11962       saw_plus = true;
11963     }
11964
11965   if (TARGET_MACHO && darwin_local_data_pic (disp))
11966     return true;
11967
11968   if (GET_CODE (disp) != UNSPEC)
11969     return false;
11970
11971   switch (XINT (disp, 1))
11972     {
11973     case UNSPEC_GOT:
11974       if (saw_plus)
11975         return false;
11976       /* We need to check for both symbols and labels because VxWorks loads
11977          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
11978          details.  */
11979       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11980               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
11981     case UNSPEC_GOTOFF:
11982       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
11983          While ABI specify also 32bit relocation but we don't produce it in
11984          small PIC model at all.  */
11985       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11986            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
11987           && !TARGET_64BIT)
11988         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
11989       return false;
11990     case UNSPEC_GOTTPOFF:
11991     case UNSPEC_GOTNTPOFF:
11992     case UNSPEC_INDNTPOFF:
11993       if (saw_plus)
11994         return false;
11995       disp = XVECEXP (disp, 0, 0);
11996       return (GET_CODE (disp) == SYMBOL_REF
11997               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
11998     case UNSPEC_NTPOFF:
11999       disp = XVECEXP (disp, 0, 0);
12000       return (GET_CODE (disp) == SYMBOL_REF
12001               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
12002     case UNSPEC_DTPOFF:
12003       disp = XVECEXP (disp, 0, 0);
12004       return (GET_CODE (disp) == SYMBOL_REF
12005               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
12006     }
12007
12008   return false;
12009 }
12010
12011 /* Recognizes RTL expressions that are valid memory addresses for an
12012    instruction.  The MODE argument is the machine mode for the MEM
12013    expression that wants to use this address.
12014
12015    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
12016    convert common non-canonical forms to canonical form so that they will
12017    be recognized.  */
12018
12019 static bool
12020 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
12021                            rtx addr, bool strict)
12022 {
12023   struct ix86_address parts;
12024   rtx base, index, disp;
12025   HOST_WIDE_INT scale;
12026
12027   if (ix86_decompose_address (addr, &parts) <= 0)
12028     /* Decomposition failed.  */
12029     return false;
12030
12031   base = parts.base;
12032   index = parts.index;
12033   disp = parts.disp;
12034   scale = parts.scale;
12035
12036   /* Validate base register.
12037
12038      Don't allow SUBREG's that span more than a word here.  It can lead to spill
12039      failures when the base is one word out of a two word structure, which is
12040      represented internally as a DImode int.  */
12041
12042   if (base)
12043     {
12044       rtx reg;
12045
12046       if (REG_P (base))
12047         reg = base;
12048       else if (GET_CODE (base) == SUBREG
12049                && REG_P (SUBREG_REG (base))
12050                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
12051                   <= UNITS_PER_WORD)
12052         reg = SUBREG_REG (base);
12053       else
12054         /* Base is not a register.  */
12055         return false;
12056
12057       if (GET_MODE (base) != Pmode)
12058         /* Base is not in Pmode.  */
12059         return false;
12060
12061       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
12062           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
12063         /* Base is not valid.  */
12064         return false;
12065     }
12066
12067   /* Validate index register.
12068
12069      Don't allow SUBREG's that span more than a word here -- same as above.  */
12070
12071   if (index)
12072     {
12073       rtx reg;
12074
12075       if (REG_P (index))
12076         reg = index;
12077       else if (GET_CODE (index) == SUBREG
12078                && REG_P (SUBREG_REG (index))
12079                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
12080                   <= UNITS_PER_WORD)
12081         reg = SUBREG_REG (index);
12082       else
12083         /* Index is not a register.  */
12084         return false;
12085
12086       if (GET_MODE (index) != Pmode)
12087         /* Index is not in Pmode.  */
12088         return false;
12089
12090       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
12091           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
12092         /* Index is not valid.  */
12093         return false;
12094     }
12095
12096   /* Validate scale factor.  */
12097   if (scale != 1)
12098     {
12099       if (!index)
12100         /* Scale without index.  */
12101         return false;
12102
12103       if (scale != 2 && scale != 4 && scale != 8)
12104         /* Scale is not a valid multiplier.  */
12105         return false;
12106     }
12107
12108   /* Validate displacement.  */
12109   if (disp)
12110     {
12111       if (GET_CODE (disp) == CONST
12112           && GET_CODE (XEXP (disp, 0)) == UNSPEC
12113           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
12114         switch (XINT (XEXP (disp, 0), 1))
12115           {
12116           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
12117              used.  While ABI specify also 32bit relocations, we don't produce
12118              them at all and use IP relative instead.  */
12119           case UNSPEC_GOT:
12120           case UNSPEC_GOTOFF:
12121             gcc_assert (flag_pic);
12122             if (!TARGET_64BIT)
12123               goto is_legitimate_pic;
12124
12125             /* 64bit address unspec.  */
12126             return false;
12127
12128           case UNSPEC_GOTPCREL:
12129           case UNSPEC_PCREL:
12130             gcc_assert (flag_pic);
12131             goto is_legitimate_pic;
12132
12133           case UNSPEC_GOTTPOFF:
12134           case UNSPEC_GOTNTPOFF:
12135           case UNSPEC_INDNTPOFF:
12136           case UNSPEC_NTPOFF:
12137           case UNSPEC_DTPOFF:
12138             break;
12139
12140           case UNSPEC_STACK_CHECK:
12141             gcc_assert (flag_split_stack);
12142             break;
12143
12144           default:
12145             /* Invalid address unspec.  */
12146             return false;
12147           }
12148
12149       else if (SYMBOLIC_CONST (disp)
12150                && (flag_pic
12151                    || (TARGET_MACHO
12152 #if TARGET_MACHO
12153                        && MACHOPIC_INDIRECT
12154                        && !machopic_operand_p (disp)
12155 #endif
12156                )))
12157         {
12158
12159         is_legitimate_pic:
12160           if (TARGET_64BIT && (index || base))
12161             {
12162               /* foo@dtpoff(%rX) is ok.  */
12163               if (GET_CODE (disp) != CONST
12164                   || GET_CODE (XEXP (disp, 0)) != PLUS
12165                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
12166                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
12167                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
12168                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
12169                 /* Non-constant pic memory reference.  */
12170                 return false;
12171             }
12172           else if ((!TARGET_MACHO || flag_pic)
12173                     && ! legitimate_pic_address_disp_p (disp))
12174             /* Displacement is an invalid pic construct.  */
12175             return false;
12176 #if TARGET_MACHO
12177           else if (MACHO_DYNAMIC_NO_PIC_P && !legitimate_constant_p (disp))
12178             /* displacment must be referenced via non_lazy_pointer */
12179             return false;
12180 #endif
12181
12182           /* This code used to verify that a symbolic pic displacement
12183              includes the pic_offset_table_rtx register.
12184
12185              While this is good idea, unfortunately these constructs may
12186              be created by "adds using lea" optimization for incorrect
12187              code like:
12188
12189              int a;
12190              int foo(int i)
12191                {
12192                  return *(&a+i);
12193                }
12194
12195              This code is nonsensical, but results in addressing
12196              GOT table with pic_offset_table_rtx base.  We can't
12197              just refuse it easily, since it gets matched by
12198              "addsi3" pattern, that later gets split to lea in the
12199              case output register differs from input.  While this
12200              can be handled by separate addsi pattern for this case
12201              that never results in lea, this seems to be easier and
12202              correct fix for crash to disable this test.  */
12203         }
12204       else if (GET_CODE (disp) != LABEL_REF
12205                && !CONST_INT_P (disp)
12206                && (GET_CODE (disp) != CONST
12207                    || !legitimate_constant_p (disp))
12208                && (GET_CODE (disp) != SYMBOL_REF
12209                    || !legitimate_constant_p (disp)))
12210         /* Displacement is not constant.  */
12211         return false;
12212       else if (TARGET_64BIT
12213                && !x86_64_immediate_operand (disp, VOIDmode))
12214         /* Displacement is out of range.  */
12215         return false;
12216     }
12217
12218   /* Everything looks valid.  */
12219   return true;
12220 }
12221
12222 /* Determine if a given RTX is a valid constant address.  */
12223
12224 bool
12225 constant_address_p (rtx x)
12226 {
12227   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
12228 }
12229 \f
12230 /* Return a unique alias set for the GOT.  */
12231
12232 static alias_set_type
12233 ix86_GOT_alias_set (void)
12234 {
12235   static alias_set_type set = -1;
12236   if (set == -1)
12237     set = new_alias_set ();
12238   return set;
12239 }
12240
12241 /* Return a legitimate reference for ORIG (an address) using the
12242    register REG.  If REG is 0, a new pseudo is generated.
12243
12244    There are two types of references that must be handled:
12245
12246    1. Global data references must load the address from the GOT, via
12247       the PIC reg.  An insn is emitted to do this load, and the reg is
12248       returned.
12249
12250    2. Static data references, constant pool addresses, and code labels
12251       compute the address as an offset from the GOT, whose base is in
12252       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
12253       differentiate them from global data objects.  The returned
12254       address is the PIC reg + an unspec constant.
12255
12256    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
12257    reg also appears in the address.  */
12258
12259 static rtx
12260 legitimize_pic_address (rtx orig, rtx reg)
12261 {
12262   rtx addr = orig;
12263   rtx new_rtx = orig;
12264   rtx base;
12265
12266 #if TARGET_MACHO
12267   if (TARGET_MACHO && !TARGET_64BIT)
12268     {
12269       if (reg == 0)
12270         reg = gen_reg_rtx (Pmode);
12271       /* Use the generic Mach-O PIC machinery.  */
12272       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
12273     }
12274 #endif
12275
12276   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
12277     new_rtx = addr;
12278   else if (TARGET_64BIT
12279            && ix86_cmodel != CM_SMALL_PIC
12280            && gotoff_operand (addr, Pmode))
12281     {
12282       rtx tmpreg;
12283       /* This symbol may be referenced via a displacement from the PIC
12284          base address (@GOTOFF).  */
12285
12286       if (reload_in_progress)
12287         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12288       if (GET_CODE (addr) == CONST)
12289         addr = XEXP (addr, 0);
12290       if (GET_CODE (addr) == PLUS)
12291           {
12292             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12293                                       UNSPEC_GOTOFF);
12294             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12295           }
12296         else
12297           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12298       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12299       if (!reg)
12300         tmpreg = gen_reg_rtx (Pmode);
12301       else
12302         tmpreg = reg;
12303       emit_move_insn (tmpreg, new_rtx);
12304
12305       if (reg != 0)
12306         {
12307           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
12308                                          tmpreg, 1, OPTAB_DIRECT);
12309           new_rtx = reg;
12310         }
12311       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
12312     }
12313   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
12314     {
12315       /* This symbol may be referenced via a displacement from the PIC
12316          base address (@GOTOFF).  */
12317
12318       if (reload_in_progress)
12319         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12320       if (GET_CODE (addr) == CONST)
12321         addr = XEXP (addr, 0);
12322       if (GET_CODE (addr) == PLUS)
12323           {
12324             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12325                                       UNSPEC_GOTOFF);
12326             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12327           }
12328         else
12329           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12330       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12331       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12332
12333       if (reg != 0)
12334         {
12335           emit_move_insn (reg, new_rtx);
12336           new_rtx = reg;
12337         }
12338     }
12339   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
12340            /* We can't use @GOTOFF for text labels on VxWorks;
12341               see gotoff_operand.  */
12342            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
12343     {
12344       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12345         {
12346           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
12347             return legitimize_dllimport_symbol (addr, true);
12348           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
12349               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
12350               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
12351             {
12352               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
12353               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
12354             }
12355         }
12356
12357       /* For x64 PE-COFF there is no GOT table.  So we use address
12358          directly.  */
12359       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
12360       {
12361           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_PCREL);
12362           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12363
12364           if (reg == 0)
12365             reg = gen_reg_rtx (Pmode);
12366           emit_move_insn (reg, new_rtx);
12367           new_rtx = reg;
12368       }
12369       else if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
12370         {
12371           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
12372           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12373           new_rtx = gen_const_mem (Pmode, new_rtx);
12374           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12375
12376           if (reg == 0)
12377             reg = gen_reg_rtx (Pmode);
12378           /* Use directly gen_movsi, otherwise the address is loaded
12379              into register for CSE.  We don't want to CSE this addresses,
12380              instead we CSE addresses from the GOT table, so skip this.  */
12381           emit_insn (gen_movsi (reg, new_rtx));
12382           new_rtx = reg;
12383         }
12384       else
12385         {
12386           /* This symbol must be referenced via a load from the
12387              Global Offset Table (@GOT).  */
12388
12389           if (reload_in_progress)
12390             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12391           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
12392           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12393           if (TARGET_64BIT)
12394             new_rtx = force_reg (Pmode, new_rtx);
12395           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12396           new_rtx = gen_const_mem (Pmode, new_rtx);
12397           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12398
12399           if (reg == 0)
12400             reg = gen_reg_rtx (Pmode);
12401           emit_move_insn (reg, new_rtx);
12402           new_rtx = reg;
12403         }
12404     }
12405   else
12406     {
12407       if (CONST_INT_P (addr)
12408           && !x86_64_immediate_operand (addr, VOIDmode))
12409         {
12410           if (reg)
12411             {
12412               emit_move_insn (reg, addr);
12413               new_rtx = reg;
12414             }
12415           else
12416             new_rtx = force_reg (Pmode, addr);
12417         }
12418       else if (GET_CODE (addr) == CONST)
12419         {
12420           addr = XEXP (addr, 0);
12421
12422           /* We must match stuff we generate before.  Assume the only
12423              unspecs that can get here are ours.  Not that we could do
12424              anything with them anyway....  */
12425           if (GET_CODE (addr) == UNSPEC
12426               || (GET_CODE (addr) == PLUS
12427                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
12428             return orig;
12429           gcc_assert (GET_CODE (addr) == PLUS);
12430         }
12431       if (GET_CODE (addr) == PLUS)
12432         {
12433           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
12434
12435           /* Check first to see if this is a constant offset from a @GOTOFF
12436              symbol reference.  */
12437           if (gotoff_operand (op0, Pmode)
12438               && CONST_INT_P (op1))
12439             {
12440               if (!TARGET_64BIT)
12441                 {
12442                   if (reload_in_progress)
12443                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12444                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
12445                                             UNSPEC_GOTOFF);
12446                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
12447                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12448                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12449
12450                   if (reg != 0)
12451                     {
12452                       emit_move_insn (reg, new_rtx);
12453                       new_rtx = reg;
12454                     }
12455                 }
12456               else
12457                 {
12458                   if (INTVAL (op1) < -16*1024*1024
12459                       || INTVAL (op1) >= 16*1024*1024)
12460                     {
12461                       if (!x86_64_immediate_operand (op1, Pmode))
12462                         op1 = force_reg (Pmode, op1);
12463                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
12464                     }
12465                 }
12466             }
12467           else
12468             {
12469               base = legitimize_pic_address (XEXP (addr, 0), reg);
12470               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
12471                                                  base == reg ? NULL_RTX : reg);
12472
12473               if (CONST_INT_P (new_rtx))
12474                 new_rtx = plus_constant (base, INTVAL (new_rtx));
12475               else
12476                 {
12477                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
12478                     {
12479                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
12480                       new_rtx = XEXP (new_rtx, 1);
12481                     }
12482                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
12483                 }
12484             }
12485         }
12486     }
12487   return new_rtx;
12488 }
12489 \f
12490 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
12491
12492 static rtx
12493 get_thread_pointer (int to_reg)
12494 {
12495   rtx tp, reg, insn;
12496
12497   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
12498   if (!to_reg)
12499     return tp;
12500
12501   reg = gen_reg_rtx (Pmode);
12502   insn = gen_rtx_SET (VOIDmode, reg, tp);
12503   insn = emit_insn (insn);
12504
12505   return reg;
12506 }
12507
12508 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
12509    false if we expect this to be used for a memory address and true if
12510    we expect to load the address into a register.  */
12511
12512 static rtx
12513 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
12514 {
12515   rtx dest, base, off, pic, tp;
12516   int type;
12517
12518   switch (model)
12519     {
12520     case TLS_MODEL_GLOBAL_DYNAMIC:
12521       dest = gen_reg_rtx (Pmode);
12522       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
12523
12524       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
12525         {
12526           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
12527
12528           start_sequence ();
12529           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
12530           insns = get_insns ();
12531           end_sequence ();
12532
12533           RTL_CONST_CALL_P (insns) = 1;
12534           emit_libcall_block (insns, dest, rax, x);
12535         }
12536       else if (TARGET_64BIT && TARGET_GNU2_TLS)
12537         emit_insn (gen_tls_global_dynamic_64 (dest, x));
12538       else
12539         emit_insn (gen_tls_global_dynamic_32 (dest, x));
12540
12541       if (TARGET_GNU2_TLS)
12542         {
12543           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
12544
12545           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
12546         }
12547       break;
12548
12549     case TLS_MODEL_LOCAL_DYNAMIC:
12550       base = gen_reg_rtx (Pmode);
12551       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
12552
12553       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
12554         {
12555           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, note;
12556
12557           start_sequence ();
12558           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
12559           insns = get_insns ();
12560           end_sequence ();
12561
12562           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
12563           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
12564           RTL_CONST_CALL_P (insns) = 1;
12565           emit_libcall_block (insns, base, rax, note);
12566         }
12567       else if (TARGET_64BIT && TARGET_GNU2_TLS)
12568         emit_insn (gen_tls_local_dynamic_base_64 (base));
12569       else
12570         emit_insn (gen_tls_local_dynamic_base_32 (base));
12571
12572       if (TARGET_GNU2_TLS)
12573         {
12574           rtx x = ix86_tls_module_base ();
12575
12576           set_unique_reg_note (get_last_insn (), REG_EQUIV,
12577                                gen_rtx_MINUS (Pmode, x, tp));
12578         }
12579
12580       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
12581       off = gen_rtx_CONST (Pmode, off);
12582
12583       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
12584
12585       if (TARGET_GNU2_TLS)
12586         {
12587           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
12588
12589           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
12590         }
12591
12592       break;
12593
12594     case TLS_MODEL_INITIAL_EXEC:
12595       if (TARGET_64BIT)
12596         {
12597           if (TARGET_SUN_TLS)
12598             {
12599               /* The Sun linker took the AMD64 TLS spec literally
12600                  and can only handle %rax as destination of the
12601                  initial executable code sequence.  */
12602
12603               dest = gen_reg_rtx (Pmode);
12604               emit_insn (gen_tls_initial_exec_64_sun (dest, x));
12605               return dest;
12606             }
12607
12608           pic = NULL;
12609           type = UNSPEC_GOTNTPOFF;
12610         }
12611       else if (flag_pic)
12612         {
12613           if (reload_in_progress)
12614             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12615           pic = pic_offset_table_rtx;
12616           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
12617         }
12618       else if (!TARGET_ANY_GNU_TLS)
12619         {
12620           pic = gen_reg_rtx (Pmode);
12621           emit_insn (gen_set_got (pic));
12622           type = UNSPEC_GOTTPOFF;
12623         }
12624       else
12625         {
12626           pic = NULL;
12627           type = UNSPEC_INDNTPOFF;
12628         }
12629
12630       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
12631       off = gen_rtx_CONST (Pmode, off);
12632       if (pic)
12633         off = gen_rtx_PLUS (Pmode, pic, off);
12634       off = gen_const_mem (Pmode, off);
12635       set_mem_alias_set (off, ix86_GOT_alias_set ());
12636
12637       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12638         {
12639           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12640           off = force_reg (Pmode, off);
12641           return gen_rtx_PLUS (Pmode, base, off);
12642         }
12643       else
12644         {
12645           base = get_thread_pointer (true);
12646           dest = gen_reg_rtx (Pmode);
12647           emit_insn (gen_subsi3 (dest, base, off));
12648         }
12649       break;
12650
12651     case TLS_MODEL_LOCAL_EXEC:
12652       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
12653                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12654                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
12655       off = gen_rtx_CONST (Pmode, off);
12656
12657       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12658         {
12659           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12660           return gen_rtx_PLUS (Pmode, base, off);
12661         }
12662       else
12663         {
12664           base = get_thread_pointer (true);
12665           dest = gen_reg_rtx (Pmode);
12666           emit_insn (gen_subsi3 (dest, base, off));
12667         }
12668       break;
12669
12670     default:
12671       gcc_unreachable ();
12672     }
12673
12674   return dest;
12675 }
12676
12677 /* Create or return the unique __imp_DECL dllimport symbol corresponding
12678    to symbol DECL.  */
12679
12680 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
12681   htab_t dllimport_map;
12682
12683 static tree
12684 get_dllimport_decl (tree decl)
12685 {
12686   struct tree_map *h, in;
12687   void **loc;
12688   const char *name;
12689   const char *prefix;
12690   size_t namelen, prefixlen;
12691   char *imp_name;
12692   tree to;
12693   rtx rtl;
12694
12695   if (!dllimport_map)
12696     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
12697
12698   in.hash = htab_hash_pointer (decl);
12699   in.base.from = decl;
12700   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
12701   h = (struct tree_map *) *loc;
12702   if (h)
12703     return h->to;
12704
12705   *loc = h = ggc_alloc_tree_map ();
12706   h->hash = in.hash;
12707   h->base.from = decl;
12708   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
12709                            VAR_DECL, NULL, ptr_type_node);
12710   DECL_ARTIFICIAL (to) = 1;
12711   DECL_IGNORED_P (to) = 1;
12712   DECL_EXTERNAL (to) = 1;
12713   TREE_READONLY (to) = 1;
12714
12715   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
12716   name = targetm.strip_name_encoding (name);
12717   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
12718     ? "*__imp_" : "*__imp__";
12719   namelen = strlen (name);
12720   prefixlen = strlen (prefix);
12721   imp_name = (char *) alloca (namelen + prefixlen + 1);
12722   memcpy (imp_name, prefix, prefixlen);
12723   memcpy (imp_name + prefixlen, name, namelen + 1);
12724
12725   name = ggc_alloc_string (imp_name, namelen + prefixlen);
12726   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
12727   SET_SYMBOL_REF_DECL (rtl, to);
12728   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
12729
12730   rtl = gen_const_mem (Pmode, rtl);
12731   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
12732
12733   SET_DECL_RTL (to, rtl);
12734   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
12735
12736   return to;
12737 }
12738
12739 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
12740    true if we require the result be a register.  */
12741
12742 static rtx
12743 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
12744 {
12745   tree imp_decl;
12746   rtx x;
12747
12748   gcc_assert (SYMBOL_REF_DECL (symbol));
12749   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
12750
12751   x = DECL_RTL (imp_decl);
12752   if (want_reg)
12753     x = force_reg (Pmode, x);
12754   return x;
12755 }
12756
12757 /* Try machine-dependent ways of modifying an illegitimate address
12758    to be legitimate.  If we find one, return the new, valid address.
12759    This macro is used in only one place: `memory_address' in explow.c.
12760
12761    OLDX is the address as it was before break_out_memory_refs was called.
12762    In some cases it is useful to look at this to decide what needs to be done.
12763
12764    It is always safe for this macro to do nothing.  It exists to recognize
12765    opportunities to optimize the output.
12766
12767    For the 80386, we handle X+REG by loading X into a register R and
12768    using R+REG.  R will go in a general reg and indexing will be used.
12769    However, if REG is a broken-out memory address or multiplication,
12770    nothing needs to be done because REG can certainly go in a general reg.
12771
12772    When -fpic is used, special handling is needed for symbolic references.
12773    See comments by legitimize_pic_address in i386.c for details.  */
12774
12775 static rtx
12776 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
12777                          enum machine_mode mode)
12778 {
12779   int changed = 0;
12780   unsigned log;
12781
12782   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
12783   if (log)
12784     return legitimize_tls_address (x, (enum tls_model) log, false);
12785   if (GET_CODE (x) == CONST
12786       && GET_CODE (XEXP (x, 0)) == PLUS
12787       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12788       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
12789     {
12790       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
12791                                       (enum tls_model) log, false);
12792       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12793     }
12794
12795   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12796     {
12797       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
12798         return legitimize_dllimport_symbol (x, true);
12799       if (GET_CODE (x) == CONST
12800           && GET_CODE (XEXP (x, 0)) == PLUS
12801           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12802           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
12803         {
12804           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
12805           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12806         }
12807     }
12808
12809   if (flag_pic && SYMBOLIC_CONST (x))
12810     return legitimize_pic_address (x, 0);
12811
12812 #if TARGET_MACHO
12813   if (MACHO_DYNAMIC_NO_PIC_P && SYMBOLIC_CONST (x))
12814     return machopic_indirect_data_reference (x, 0);
12815 #endif
12816
12817   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
12818   if (GET_CODE (x) == ASHIFT
12819       && CONST_INT_P (XEXP (x, 1))
12820       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
12821     {
12822       changed = 1;
12823       log = INTVAL (XEXP (x, 1));
12824       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
12825                         GEN_INT (1 << log));
12826     }
12827
12828   if (GET_CODE (x) == PLUS)
12829     {
12830       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
12831
12832       if (GET_CODE (XEXP (x, 0)) == ASHIFT
12833           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
12834           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
12835         {
12836           changed = 1;
12837           log = INTVAL (XEXP (XEXP (x, 0), 1));
12838           XEXP (x, 0) = gen_rtx_MULT (Pmode,
12839                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
12840                                       GEN_INT (1 << log));
12841         }
12842
12843       if (GET_CODE (XEXP (x, 1)) == ASHIFT
12844           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
12845           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
12846         {
12847           changed = 1;
12848           log = INTVAL (XEXP (XEXP (x, 1), 1));
12849           XEXP (x, 1) = gen_rtx_MULT (Pmode,
12850                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
12851                                       GEN_INT (1 << log));
12852         }
12853
12854       /* Put multiply first if it isn't already.  */
12855       if (GET_CODE (XEXP (x, 1)) == MULT)
12856         {
12857           rtx tmp = XEXP (x, 0);
12858           XEXP (x, 0) = XEXP (x, 1);
12859           XEXP (x, 1) = tmp;
12860           changed = 1;
12861         }
12862
12863       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
12864          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
12865          created by virtual register instantiation, register elimination, and
12866          similar optimizations.  */
12867       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
12868         {
12869           changed = 1;
12870           x = gen_rtx_PLUS (Pmode,
12871                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
12872                                           XEXP (XEXP (x, 1), 0)),
12873                             XEXP (XEXP (x, 1), 1));
12874         }
12875
12876       /* Canonicalize
12877          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
12878          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
12879       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
12880                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
12881                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
12882                && CONSTANT_P (XEXP (x, 1)))
12883         {
12884           rtx constant;
12885           rtx other = NULL_RTX;
12886
12887           if (CONST_INT_P (XEXP (x, 1)))
12888             {
12889               constant = XEXP (x, 1);
12890               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
12891             }
12892           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
12893             {
12894               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
12895               other = XEXP (x, 1);
12896             }
12897           else
12898             constant = 0;
12899
12900           if (constant)
12901             {
12902               changed = 1;
12903               x = gen_rtx_PLUS (Pmode,
12904                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
12905                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
12906                                 plus_constant (other, INTVAL (constant)));
12907             }
12908         }
12909
12910       if (changed && ix86_legitimate_address_p (mode, x, false))
12911         return x;
12912
12913       if (GET_CODE (XEXP (x, 0)) == MULT)
12914         {
12915           changed = 1;
12916           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
12917         }
12918
12919       if (GET_CODE (XEXP (x, 1)) == MULT)
12920         {
12921           changed = 1;
12922           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
12923         }
12924
12925       if (changed
12926           && REG_P (XEXP (x, 1))
12927           && REG_P (XEXP (x, 0)))
12928         return x;
12929
12930       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
12931         {
12932           changed = 1;
12933           x = legitimize_pic_address (x, 0);
12934         }
12935
12936       if (changed && ix86_legitimate_address_p (mode, x, false))
12937         return x;
12938
12939       if (REG_P (XEXP (x, 0)))
12940         {
12941           rtx temp = gen_reg_rtx (Pmode);
12942           rtx val  = force_operand (XEXP (x, 1), temp);
12943           if (val != temp)
12944             emit_move_insn (temp, val);
12945
12946           XEXP (x, 1) = temp;
12947           return x;
12948         }
12949
12950       else if (REG_P (XEXP (x, 1)))
12951         {
12952           rtx temp = gen_reg_rtx (Pmode);
12953           rtx val  = force_operand (XEXP (x, 0), temp);
12954           if (val != temp)
12955             emit_move_insn (temp, val);
12956
12957           XEXP (x, 0) = temp;
12958           return x;
12959         }
12960     }
12961
12962   return x;
12963 }
12964 \f
12965 /* Print an integer constant expression in assembler syntax.  Addition
12966    and subtraction are the only arithmetic that may appear in these
12967    expressions.  FILE is the stdio stream to write to, X is the rtx, and
12968    CODE is the operand print code from the output string.  */
12969
12970 static void
12971 output_pic_addr_const (FILE *file, rtx x, int code)
12972 {
12973   char buf[256];
12974
12975   switch (GET_CODE (x))
12976     {
12977     case PC:
12978       gcc_assert (flag_pic);
12979       putc ('.', file);
12980       break;
12981
12982     case SYMBOL_REF:
12983       if (TARGET_64BIT || ! TARGET_MACHO_BRANCH_ISLANDS)
12984         output_addr_const (file, x);
12985       else
12986         {
12987           const char *name = XSTR (x, 0);
12988
12989           /* Mark the decl as referenced so that cgraph will
12990              output the function.  */
12991           if (SYMBOL_REF_DECL (x))
12992             mark_decl_referenced (SYMBOL_REF_DECL (x));
12993
12994 #if TARGET_MACHO
12995           if (MACHOPIC_INDIRECT
12996               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
12997             name = machopic_indirection_name (x, /*stub_p=*/true);
12998 #endif
12999           assemble_name (file, name);
13000         }
13001       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
13002           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
13003         fputs ("@PLT", file);
13004       break;
13005
13006     case LABEL_REF:
13007       x = XEXP (x, 0);
13008       /* FALLTHRU */
13009     case CODE_LABEL:
13010       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
13011       assemble_name (asm_out_file, buf);
13012       break;
13013
13014     case CONST_INT:
13015       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13016       break;
13017
13018     case CONST:
13019       /* This used to output parentheses around the expression,
13020          but that does not work on the 386 (either ATT or BSD assembler).  */
13021       output_pic_addr_const (file, XEXP (x, 0), code);
13022       break;
13023
13024     case CONST_DOUBLE:
13025       if (GET_MODE (x) == VOIDmode)
13026         {
13027           /* We can use %d if the number is <32 bits and positive.  */
13028           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
13029             fprintf (file, "0x%lx%08lx",
13030                      (unsigned long) CONST_DOUBLE_HIGH (x),
13031                      (unsigned long) CONST_DOUBLE_LOW (x));
13032           else
13033             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
13034         }
13035       else
13036         /* We can't handle floating point constants;
13037            TARGET_PRINT_OPERAND must handle them.  */
13038         output_operand_lossage ("floating constant misused");
13039       break;
13040
13041     case PLUS:
13042       /* Some assemblers need integer constants to appear first.  */
13043       if (CONST_INT_P (XEXP (x, 0)))
13044         {
13045           output_pic_addr_const (file, XEXP (x, 0), code);
13046           putc ('+', file);
13047           output_pic_addr_const (file, XEXP (x, 1), code);
13048         }
13049       else
13050         {
13051           gcc_assert (CONST_INT_P (XEXP (x, 1)));
13052           output_pic_addr_const (file, XEXP (x, 1), code);
13053           putc ('+', file);
13054           output_pic_addr_const (file, XEXP (x, 0), code);
13055         }
13056       break;
13057
13058     case MINUS:
13059       if (!TARGET_MACHO)
13060         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
13061       output_pic_addr_const (file, XEXP (x, 0), code);
13062       putc ('-', file);
13063       output_pic_addr_const (file, XEXP (x, 1), code);
13064       if (!TARGET_MACHO)
13065         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
13066       break;
13067
13068      case UNSPEC:
13069        if (XINT (x, 1) == UNSPEC_STACK_CHECK)
13070          {
13071            bool f = i386_asm_output_addr_const_extra (file, x);
13072            gcc_assert (f);
13073            break;
13074          }
13075
13076        gcc_assert (XVECLEN (x, 0) == 1);
13077        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
13078        switch (XINT (x, 1))
13079         {
13080         case UNSPEC_GOT:
13081           fputs ("@GOT", file);
13082           break;
13083         case UNSPEC_GOTOFF:
13084           fputs ("@GOTOFF", file);
13085           break;
13086         case UNSPEC_PLTOFF:
13087           fputs ("@PLTOFF", file);
13088           break;
13089         case UNSPEC_PCREL:
13090           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13091                  "(%rip)" : "[rip]", file);
13092           break;
13093         case UNSPEC_GOTPCREL:
13094           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13095                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
13096           break;
13097         case UNSPEC_GOTTPOFF:
13098           /* FIXME: This might be @TPOFF in Sun ld too.  */
13099           fputs ("@gottpoff", file);
13100           break;
13101         case UNSPEC_TPOFF:
13102           fputs ("@tpoff", file);
13103           break;
13104         case UNSPEC_NTPOFF:
13105           if (TARGET_64BIT)
13106             fputs ("@tpoff", file);
13107           else
13108             fputs ("@ntpoff", file);
13109           break;
13110         case UNSPEC_DTPOFF:
13111           fputs ("@dtpoff", file);
13112           break;
13113         case UNSPEC_GOTNTPOFF:
13114           if (TARGET_64BIT)
13115             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13116                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
13117           else
13118             fputs ("@gotntpoff", file);
13119           break;
13120         case UNSPEC_INDNTPOFF:
13121           fputs ("@indntpoff", file);
13122           break;
13123 #if TARGET_MACHO
13124         case UNSPEC_MACHOPIC_OFFSET:
13125           putc ('-', file);
13126           machopic_output_function_base_name (file);
13127           break;
13128 #endif
13129         default:
13130           output_operand_lossage ("invalid UNSPEC as operand");
13131           break;
13132         }
13133        break;
13134
13135     default:
13136       output_operand_lossage ("invalid expression as operand");
13137     }
13138 }
13139
13140 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
13141    We need to emit DTP-relative relocations.  */
13142
13143 static void ATTRIBUTE_UNUSED
13144 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
13145 {
13146   fputs (ASM_LONG, file);
13147   output_addr_const (file, x);
13148   fputs ("@dtpoff", file);
13149   switch (size)
13150     {
13151     case 4:
13152       break;
13153     case 8:
13154       fputs (", 0", file);
13155       break;
13156     default:
13157       gcc_unreachable ();
13158    }
13159 }
13160
13161 /* Return true if X is a representation of the PIC register.  This copes
13162    with calls from ix86_find_base_term, where the register might have
13163    been replaced by a cselib value.  */
13164
13165 static bool
13166 ix86_pic_register_p (rtx x)
13167 {
13168   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
13169     return (pic_offset_table_rtx
13170             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
13171   else
13172     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
13173 }
13174
13175 /* Helper function for ix86_delegitimize_address.
13176    Attempt to delegitimize TLS local-exec accesses.  */
13177
13178 static rtx
13179 ix86_delegitimize_tls_address (rtx orig_x)
13180 {
13181   rtx x = orig_x, unspec;
13182   struct ix86_address addr;
13183
13184   if (!TARGET_TLS_DIRECT_SEG_REFS)
13185     return orig_x;
13186   if (MEM_P (x))
13187     x = XEXP (x, 0);
13188   if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
13189     return orig_x;
13190   if (ix86_decompose_address (x, &addr) == 0
13191       || addr.seg != (TARGET_64BIT ? SEG_FS : SEG_GS)
13192       || addr.disp == NULL_RTX
13193       || GET_CODE (addr.disp) != CONST)
13194     return orig_x;
13195   unspec = XEXP (addr.disp, 0);
13196   if (GET_CODE (unspec) == PLUS && CONST_INT_P (XEXP (unspec, 1)))
13197     unspec = XEXP (unspec, 0);
13198   if (GET_CODE (unspec) != UNSPEC || XINT (unspec, 1) != UNSPEC_NTPOFF)
13199     return orig_x;
13200   x = XVECEXP (unspec, 0, 0);
13201   gcc_assert (GET_CODE (x) == SYMBOL_REF);
13202   if (unspec != XEXP (addr.disp, 0))
13203     x = gen_rtx_PLUS (Pmode, x, XEXP (XEXP (addr.disp, 0), 1));
13204   if (addr.index)
13205     {
13206       rtx idx = addr.index;
13207       if (addr.scale != 1)
13208         idx = gen_rtx_MULT (Pmode, idx, GEN_INT (addr.scale));
13209       x = gen_rtx_PLUS (Pmode, idx, x);
13210     }
13211   if (addr.base)
13212     x = gen_rtx_PLUS (Pmode, addr.base, x);
13213   if (MEM_P (orig_x))
13214     x = replace_equiv_address_nv (orig_x, x);
13215   return x;
13216 }
13217
13218 /* In the name of slightly smaller debug output, and to cater to
13219    general assembler lossage, recognize PIC+GOTOFF and turn it back
13220    into a direct symbol reference.
13221
13222    On Darwin, this is necessary to avoid a crash, because Darwin
13223    has a different PIC label for each routine but the DWARF debugging
13224    information is not associated with any particular routine, so it's
13225    necessary to remove references to the PIC label from RTL stored by
13226    the DWARF output code.  */
13227
13228 static rtx
13229 ix86_delegitimize_address (rtx x)
13230 {
13231   rtx orig_x = delegitimize_mem_from_attrs (x);
13232   /* addend is NULL or some rtx if x is something+GOTOFF where
13233      something doesn't include the PIC register.  */
13234   rtx addend = NULL_RTX;
13235   /* reg_addend is NULL or a multiple of some register.  */
13236   rtx reg_addend = NULL_RTX;
13237   /* const_addend is NULL or a const_int.  */
13238   rtx const_addend = NULL_RTX;
13239   /* This is the result, or NULL.  */
13240   rtx result = NULL_RTX;
13241
13242   x = orig_x;
13243
13244   if (MEM_P (x))
13245     x = XEXP (x, 0);
13246
13247   if (TARGET_64BIT)
13248     {
13249       if (GET_CODE (x) != CONST
13250           || GET_CODE (XEXP (x, 0)) != UNSPEC
13251           || (XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
13252               && XINT (XEXP (x, 0), 1) != UNSPEC_PCREL)
13253           || !MEM_P (orig_x))
13254         return ix86_delegitimize_tls_address (orig_x);
13255       x = XVECEXP (XEXP (x, 0), 0, 0);
13256       if (GET_MODE (orig_x) != Pmode)
13257         {
13258           x = simplify_gen_subreg (GET_MODE (orig_x), x, Pmode, 0);
13259           if (x == NULL_RTX)
13260             return orig_x;
13261         }
13262       return x;
13263     }
13264
13265   if (GET_CODE (x) != PLUS
13266       || GET_CODE (XEXP (x, 1)) != CONST)
13267     return ix86_delegitimize_tls_address (orig_x);
13268
13269   if (ix86_pic_register_p (XEXP (x, 0)))
13270     /* %ebx + GOT/GOTOFF */
13271     ;
13272   else if (GET_CODE (XEXP (x, 0)) == PLUS)
13273     {
13274       /* %ebx + %reg * scale + GOT/GOTOFF */
13275       reg_addend = XEXP (x, 0);
13276       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
13277         reg_addend = XEXP (reg_addend, 1);
13278       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
13279         reg_addend = XEXP (reg_addend, 0);
13280       else
13281         {
13282           reg_addend = NULL_RTX;
13283           addend = XEXP (x, 0);
13284         }
13285     }
13286   else
13287     addend = XEXP (x, 0);
13288
13289   x = XEXP (XEXP (x, 1), 0);
13290   if (GET_CODE (x) == PLUS
13291       && CONST_INT_P (XEXP (x, 1)))
13292     {
13293       const_addend = XEXP (x, 1);
13294       x = XEXP (x, 0);
13295     }
13296
13297   if (GET_CODE (x) == UNSPEC
13298       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
13299           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
13300     result = XVECEXP (x, 0, 0);
13301
13302   if (TARGET_MACHO && darwin_local_data_pic (x)
13303       && !MEM_P (orig_x))
13304     result = XVECEXP (x, 0, 0);
13305
13306   if (! result)
13307     return ix86_delegitimize_tls_address (orig_x);
13308
13309   if (const_addend)
13310     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
13311   if (reg_addend)
13312     result = gen_rtx_PLUS (Pmode, reg_addend, result);
13313   if (addend)
13314     {
13315       /* If the rest of original X doesn't involve the PIC register, add
13316          addend and subtract pic_offset_table_rtx.  This can happen e.g.
13317          for code like:
13318          leal (%ebx, %ecx, 4), %ecx
13319          ...
13320          movl foo@GOTOFF(%ecx), %edx
13321          in which case we return (%ecx - %ebx) + foo.  */
13322       if (pic_offset_table_rtx)
13323         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
13324                                                      pic_offset_table_rtx),
13325                                result);
13326       else
13327         return orig_x;
13328     }
13329   if (GET_MODE (orig_x) != Pmode && MEM_P (orig_x))
13330     {
13331       result = simplify_gen_subreg (GET_MODE (orig_x), result, Pmode, 0);
13332       if (result == NULL_RTX)
13333         return orig_x;
13334     }
13335   return result;
13336 }
13337
13338 /* If X is a machine specific address (i.e. a symbol or label being
13339    referenced as a displacement from the GOT implemented using an
13340    UNSPEC), then return the base term.  Otherwise return X.  */
13341
13342 rtx
13343 ix86_find_base_term (rtx x)
13344 {
13345   rtx term;
13346
13347   if (TARGET_64BIT)
13348     {
13349       if (GET_CODE (x) != CONST)
13350         return x;
13351       term = XEXP (x, 0);
13352       if (GET_CODE (term) == PLUS
13353           && (CONST_INT_P (XEXP (term, 1))
13354               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
13355         term = XEXP (term, 0);
13356       if (GET_CODE (term) != UNSPEC
13357           || (XINT (term, 1) != UNSPEC_GOTPCREL
13358               && XINT (term, 1) != UNSPEC_PCREL))
13359         return x;
13360
13361       return XVECEXP (term, 0, 0);
13362     }
13363
13364   return ix86_delegitimize_address (x);
13365 }
13366 \f
13367 static void
13368 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
13369                     int fp, FILE *file)
13370 {
13371   const char *suffix;
13372
13373   if (mode == CCFPmode || mode == CCFPUmode)
13374     {
13375       code = ix86_fp_compare_code_to_integer (code);
13376       mode = CCmode;
13377     }
13378   if (reverse)
13379     code = reverse_condition (code);
13380
13381   switch (code)
13382     {
13383     case EQ:
13384       switch (mode)
13385         {
13386         case CCAmode:
13387           suffix = "a";
13388           break;
13389
13390         case CCCmode:
13391           suffix = "c";
13392           break;
13393
13394         case CCOmode:
13395           suffix = "o";
13396           break;
13397
13398         case CCSmode:
13399           suffix = "s";
13400           break;
13401
13402         default:
13403           suffix = "e";
13404         }
13405       break;
13406     case NE:
13407       switch (mode)
13408         {
13409         case CCAmode:
13410           suffix = "na";
13411           break;
13412
13413         case CCCmode:
13414           suffix = "nc";
13415           break;
13416
13417         case CCOmode:
13418           suffix = "no";
13419           break;
13420
13421         case CCSmode:
13422           suffix = "ns";
13423           break;
13424
13425         default:
13426           suffix = "ne";
13427         }
13428       break;
13429     case GT:
13430       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
13431       suffix = "g";
13432       break;
13433     case GTU:
13434       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
13435          Those same assemblers have the same but opposite lossage on cmov.  */
13436       if (mode == CCmode)
13437         suffix = fp ? "nbe" : "a";
13438       else if (mode == CCCmode)
13439         suffix = "b";
13440       else
13441         gcc_unreachable ();
13442       break;
13443     case LT:
13444       switch (mode)
13445         {
13446         case CCNOmode:
13447         case CCGOCmode:
13448           suffix = "s";
13449           break;
13450
13451         case CCmode:
13452         case CCGCmode:
13453           suffix = "l";
13454           break;
13455
13456         default:
13457           gcc_unreachable ();
13458         }
13459       break;
13460     case LTU:
13461       gcc_assert (mode == CCmode || mode == CCCmode);
13462       suffix = "b";
13463       break;
13464     case GE:
13465       switch (mode)
13466         {
13467         case CCNOmode:
13468         case CCGOCmode:
13469           suffix = "ns";
13470           break;
13471
13472         case CCmode:
13473         case CCGCmode:
13474           suffix = "ge";
13475           break;
13476
13477         default:
13478           gcc_unreachable ();
13479         }
13480       break;
13481     case GEU:
13482       /* ??? As above.  */
13483       gcc_assert (mode == CCmode || mode == CCCmode);
13484       suffix = fp ? "nb" : "ae";
13485       break;
13486     case LE:
13487       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
13488       suffix = "le";
13489       break;
13490     case LEU:
13491       /* ??? As above.  */
13492       if (mode == CCmode)
13493         suffix = "be";
13494       else if (mode == CCCmode)
13495         suffix = fp ? "nb" : "ae";
13496       else
13497         gcc_unreachable ();
13498       break;
13499     case UNORDERED:
13500       suffix = fp ? "u" : "p";
13501       break;
13502     case ORDERED:
13503       suffix = fp ? "nu" : "np";
13504       break;
13505     default:
13506       gcc_unreachable ();
13507     }
13508   fputs (suffix, file);
13509 }
13510
13511 /* Print the name of register X to FILE based on its machine mode and number.
13512    If CODE is 'w', pretend the mode is HImode.
13513    If CODE is 'b', pretend the mode is QImode.
13514    If CODE is 'k', pretend the mode is SImode.
13515    If CODE is 'q', pretend the mode is DImode.
13516    If CODE is 'x', pretend the mode is V4SFmode.
13517    If CODE is 't', pretend the mode is V8SFmode.
13518    If CODE is 'h', pretend the reg is the 'high' byte register.
13519    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
13520    If CODE is 'd', duplicate the operand for AVX instruction.
13521  */
13522
13523 void
13524 print_reg (rtx x, int code, FILE *file)
13525 {
13526   const char *reg;
13527   bool duplicated = code == 'd' && TARGET_AVX;
13528
13529   gcc_assert (x == pc_rtx
13530               || (REGNO (x) != ARG_POINTER_REGNUM
13531                   && REGNO (x) != FRAME_POINTER_REGNUM
13532                   && REGNO (x) != FLAGS_REG
13533                   && REGNO (x) != FPSR_REG
13534                   && REGNO (x) != FPCR_REG));
13535
13536   if (ASSEMBLER_DIALECT == ASM_ATT)
13537     putc ('%', file);
13538
13539   if (x == pc_rtx)
13540     {
13541       gcc_assert (TARGET_64BIT);
13542       fputs ("rip", file);
13543       return;
13544     }
13545
13546   if (code == 'w' || MMX_REG_P (x))
13547     code = 2;
13548   else if (code == 'b')
13549     code = 1;
13550   else if (code == 'k')
13551     code = 4;
13552   else if (code == 'q')
13553     code = 8;
13554   else if (code == 'y')
13555     code = 3;
13556   else if (code == 'h')
13557     code = 0;
13558   else if (code == 'x')
13559     code = 16;
13560   else if (code == 't')
13561     code = 32;
13562   else
13563     code = GET_MODE_SIZE (GET_MODE (x));
13564
13565   /* Irritatingly, AMD extended registers use different naming convention
13566      from the normal registers.  */
13567   if (REX_INT_REG_P (x))
13568     {
13569       gcc_assert (TARGET_64BIT);
13570       switch (code)
13571         {
13572           case 0:
13573             error ("extended registers have no high halves");
13574             break;
13575           case 1:
13576             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
13577             break;
13578           case 2:
13579             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
13580             break;
13581           case 4:
13582             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
13583             break;
13584           case 8:
13585             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
13586             break;
13587           default:
13588             error ("unsupported operand size for extended register");
13589             break;
13590         }
13591       return;
13592     }
13593
13594   reg = NULL;
13595   switch (code)
13596     {
13597     case 3:
13598       if (STACK_TOP_P (x))
13599         {
13600           reg = "st(0)";
13601           break;
13602         }
13603       /* FALLTHRU */
13604     case 8:
13605     case 4:
13606     case 12:
13607       if (! ANY_FP_REG_P (x))
13608         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
13609       /* FALLTHRU */
13610     case 16:
13611     case 2:
13612     normal:
13613       reg = hi_reg_name[REGNO (x)];
13614       break;
13615     case 1:
13616       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
13617         goto normal;
13618       reg = qi_reg_name[REGNO (x)];
13619       break;
13620     case 0:
13621       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
13622         goto normal;
13623       reg = qi_high_reg_name[REGNO (x)];
13624       break;
13625     case 32:
13626       if (SSE_REG_P (x))
13627         {
13628           gcc_assert (!duplicated);
13629           putc ('y', file);
13630           fputs (hi_reg_name[REGNO (x)] + 1, file);
13631           return;
13632         }
13633       break;
13634     default:
13635       gcc_unreachable ();
13636     }
13637
13638   fputs (reg, file);
13639   if (duplicated)
13640     {
13641       if (ASSEMBLER_DIALECT == ASM_ATT)
13642         fprintf (file, ", %%%s", reg);
13643       else
13644         fprintf (file, ", %s", reg);
13645     }
13646 }
13647
13648 /* Locate some local-dynamic symbol still in use by this function
13649    so that we can print its name in some tls_local_dynamic_base
13650    pattern.  */
13651
13652 static int
13653 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
13654 {
13655   rtx x = *px;
13656
13657   if (GET_CODE (x) == SYMBOL_REF
13658       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
13659     {
13660       cfun->machine->some_ld_name = XSTR (x, 0);
13661       return 1;
13662     }
13663
13664   return 0;
13665 }
13666
13667 static const char *
13668 get_some_local_dynamic_name (void)
13669 {
13670   rtx insn;
13671
13672   if (cfun->machine->some_ld_name)
13673     return cfun->machine->some_ld_name;
13674
13675   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
13676     if (NONDEBUG_INSN_P (insn)
13677         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
13678       return cfun->machine->some_ld_name;
13679
13680   return NULL;
13681 }
13682
13683 /* Meaning of CODE:
13684    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
13685    C -- print opcode suffix for set/cmov insn.
13686    c -- like C, but print reversed condition
13687    F,f -- likewise, but for floating-point.
13688    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
13689         otherwise nothing
13690    R -- print the prefix for register names.
13691    z -- print the opcode suffix for the size of the current operand.
13692    Z -- likewise, with special suffixes for x87 instructions.
13693    * -- print a star (in certain assembler syntax)
13694    A -- print an absolute memory reference.
13695    w -- print the operand as if it's a "word" (HImode) even if it isn't.
13696    s -- print a shift double count, followed by the assemblers argument
13697         delimiter.
13698    b -- print the QImode name of the register for the indicated operand.
13699         %b0 would print %al if operands[0] is reg 0.
13700    w --  likewise, print the HImode name of the register.
13701    k --  likewise, print the SImode name of the register.
13702    q --  likewise, print the DImode name of the register.
13703    x --  likewise, print the V4SFmode name of the register.
13704    t --  likewise, print the V8SFmode name of the register.
13705    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
13706    y -- print "st(0)" instead of "st" as a register.
13707    d -- print duplicated register operand for AVX instruction.
13708    D -- print condition for SSE cmp instruction.
13709    P -- if PIC, print an @PLT suffix.
13710    X -- don't print any sort of PIC '@' suffix for a symbol.
13711    & -- print some in-use local-dynamic symbol name.
13712    H -- print a memory address offset by 8; used for sse high-parts
13713    Y -- print condition for XOP pcom* instruction.
13714    + -- print a branch hint as 'cs' or 'ds' prefix
13715    ; -- print a semicolon (after prefixes due to bug in older gas).
13716    @ -- print a segment register of thread base pointer load
13717  */
13718
13719 void
13720 ix86_print_operand (FILE *file, rtx x, int code)
13721 {
13722   if (code)
13723     {
13724       switch (code)
13725         {
13726         case '*':
13727           if (ASSEMBLER_DIALECT == ASM_ATT)
13728             putc ('*', file);
13729           return;
13730
13731         case '&':
13732           {
13733             const char *name = get_some_local_dynamic_name ();
13734             if (name == NULL)
13735               output_operand_lossage ("'%%&' used without any "
13736                                       "local dynamic TLS references");
13737             else
13738               assemble_name (file, name);
13739             return;
13740           }
13741
13742         case 'A':
13743           switch (ASSEMBLER_DIALECT)
13744             {
13745             case ASM_ATT:
13746               putc ('*', file);
13747               break;
13748
13749             case ASM_INTEL:
13750               /* Intel syntax. For absolute addresses, registers should not
13751                  be surrounded by braces.  */
13752               if (!REG_P (x))
13753                 {
13754                   putc ('[', file);
13755                   ix86_print_operand (file, x, 0);
13756                   putc (']', file);
13757                   return;
13758                 }
13759               break;
13760
13761             default:
13762               gcc_unreachable ();
13763             }
13764
13765           ix86_print_operand (file, x, 0);
13766           return;
13767
13768
13769         case 'L':
13770           if (ASSEMBLER_DIALECT == ASM_ATT)
13771             putc ('l', file);
13772           return;
13773
13774         case 'W':
13775           if (ASSEMBLER_DIALECT == ASM_ATT)
13776             putc ('w', file);
13777           return;
13778
13779         case 'B':
13780           if (ASSEMBLER_DIALECT == ASM_ATT)
13781             putc ('b', file);
13782           return;
13783
13784         case 'Q':
13785           if (ASSEMBLER_DIALECT == ASM_ATT)
13786             putc ('l', file);
13787           return;
13788
13789         case 'S':
13790           if (ASSEMBLER_DIALECT == ASM_ATT)
13791             putc ('s', file);
13792           return;
13793
13794         case 'T':
13795           if (ASSEMBLER_DIALECT == ASM_ATT)
13796             putc ('t', file);
13797           return;
13798
13799         case 'z':
13800           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
13801             {
13802               /* Opcodes don't get size suffixes if using Intel opcodes.  */
13803               if (ASSEMBLER_DIALECT == ASM_INTEL)
13804                 return;
13805
13806               switch (GET_MODE_SIZE (GET_MODE (x)))
13807                 {
13808                 case 1:
13809                   putc ('b', file);
13810                   return;
13811
13812                 case 2:
13813                   putc ('w', file);
13814                   return;
13815
13816                 case 4:
13817                   putc ('l', file);
13818                   return;
13819
13820                 case 8:
13821                   putc ('q', file);
13822                   return;
13823
13824                 default:
13825                   output_operand_lossage
13826                     ("invalid operand size for operand code '%c'", code);
13827                   return;
13828                 }
13829             }
13830
13831           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
13832             warning
13833               (0, "non-integer operand used with operand code '%c'", code);
13834           /* FALLTHRU */
13835
13836         case 'Z':
13837           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
13838           if (ASSEMBLER_DIALECT == ASM_INTEL)
13839             return;
13840
13841           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
13842             {
13843               switch (GET_MODE_SIZE (GET_MODE (x)))
13844                 {
13845                 case 2:
13846 #ifdef HAVE_AS_IX86_FILDS
13847                   putc ('s', file);
13848 #endif
13849                   return;
13850
13851                 case 4:
13852                   putc ('l', file);
13853                   return;
13854
13855                 case 8:
13856 #ifdef HAVE_AS_IX86_FILDQ
13857                   putc ('q', file);
13858 #else
13859                   fputs ("ll", file);
13860 #endif
13861                   return;
13862
13863                 default:
13864                   break;
13865                 }
13866             }
13867           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
13868             {
13869               /* 387 opcodes don't get size suffixes
13870                  if the operands are registers.  */
13871               if (STACK_REG_P (x))
13872                 return;
13873
13874               switch (GET_MODE_SIZE (GET_MODE (x)))
13875                 {
13876                 case 4:
13877                   putc ('s', file);
13878                   return;
13879
13880                 case 8:
13881                   putc ('l', file);
13882                   return;
13883
13884                 case 12:
13885                 case 16:
13886                   putc ('t', file);
13887                   return;
13888
13889                 default:
13890                   break;
13891                 }
13892             }
13893           else
13894             {
13895               output_operand_lossage
13896                 ("invalid operand type used with operand code '%c'", code);
13897               return;
13898             }
13899
13900           output_operand_lossage
13901             ("invalid operand size for operand code '%c'", code);
13902           return;
13903
13904         case 'd':
13905         case 'b':
13906         case 'w':
13907         case 'k':
13908         case 'q':
13909         case 'h':
13910         case 't':
13911         case 'y':
13912         case 'x':
13913         case 'X':
13914         case 'P':
13915           break;
13916
13917         case 's':
13918           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
13919             {
13920               ix86_print_operand (file, x, 0);
13921               fputs (", ", file);
13922             }
13923           return;
13924
13925         case 'D':
13926           /* Little bit of braindamage here.  The SSE compare instructions
13927              does use completely different names for the comparisons that the
13928              fp conditional moves.  */
13929           if (TARGET_AVX)
13930             {
13931               switch (GET_CODE (x))
13932                 {
13933                 case EQ:
13934                   fputs ("eq", file);
13935                   break;
13936                 case UNEQ:
13937                   fputs ("eq_us", file);
13938                   break;
13939                 case LT:
13940                   fputs ("lt", file);
13941                   break;
13942                 case UNLT:
13943                   fputs ("nge", file);
13944                   break;
13945                 case LE:
13946                   fputs ("le", file);
13947                   break;
13948                 case UNLE:
13949                   fputs ("ngt", file);
13950                   break;
13951                 case UNORDERED:
13952                   fputs ("unord", file);
13953                   break;
13954                 case NE:
13955                   fputs ("neq", file);
13956                   break;
13957                 case LTGT:
13958                   fputs ("neq_oq", file);
13959                   break;
13960                 case GE:
13961                   fputs ("ge", file);
13962                   break;
13963                 case UNGE:
13964                   fputs ("nlt", file);
13965                   break;
13966                 case GT:
13967                   fputs ("gt", file);
13968                   break;
13969                 case UNGT:
13970                   fputs ("nle", file);
13971                   break;
13972                 case ORDERED:
13973                   fputs ("ord", file);
13974                   break;
13975                 default:
13976                   output_operand_lossage ("operand is not a condition code, "
13977                                           "invalid operand code 'D'");
13978                   return;
13979                 }
13980             }
13981           else
13982             {
13983               switch (GET_CODE (x))
13984                 {
13985                 case EQ:
13986                 case UNEQ:
13987                   fputs ("eq", file);
13988                   break;
13989                 case LT:
13990                 case UNLT:
13991                   fputs ("lt", file);
13992                   break;
13993                 case LE:
13994                 case UNLE:
13995                   fputs ("le", file);
13996                   break;
13997                 case UNORDERED:
13998                   fputs ("unord", file);
13999                   break;
14000                 case NE:
14001                 case LTGT:
14002                   fputs ("neq", file);
14003                   break;
14004                 case UNGE:
14005                 case GE:
14006                   fputs ("nlt", file);
14007                   break;
14008                 case UNGT:
14009                 case GT:
14010                   fputs ("nle", file);
14011                   break;
14012                 case ORDERED:
14013                   fputs ("ord", file);
14014                   break;
14015                 default:
14016                   output_operand_lossage ("operand is not a condition code, "
14017                                           "invalid operand code 'D'");
14018                   return;
14019                 }
14020             }
14021           return;
14022         case 'O':
14023 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14024           if (ASSEMBLER_DIALECT == ASM_ATT)
14025             {
14026               switch (GET_MODE (x))
14027                 {
14028                 case HImode: putc ('w', file); break;
14029                 case SImode:
14030                 case SFmode: putc ('l', file); break;
14031                 case DImode:
14032                 case DFmode: putc ('q', file); break;
14033                 default: gcc_unreachable ();
14034                 }
14035               putc ('.', file);
14036             }
14037 #endif
14038           return;
14039         case 'C':
14040           if (!COMPARISON_P (x))
14041             {
14042               output_operand_lossage ("operand is neither a constant nor a "
14043                                       "condition code, invalid operand code "
14044                                       "'C'");
14045               return;
14046             }
14047           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
14048           return;
14049         case 'F':
14050           if (!COMPARISON_P (x))
14051             {
14052               output_operand_lossage ("operand is neither a constant nor a "
14053                                       "condition code, invalid operand code "
14054                                       "'F'");
14055               return;
14056             }
14057 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14058           if (ASSEMBLER_DIALECT == ASM_ATT)
14059             putc ('.', file);
14060 #endif
14061           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
14062           return;
14063
14064           /* Like above, but reverse condition */
14065         case 'c':
14066           /* Check to see if argument to %c is really a constant
14067              and not a condition code which needs to be reversed.  */
14068           if (!COMPARISON_P (x))
14069             {
14070               output_operand_lossage ("operand is neither a constant nor a "
14071                                       "condition code, invalid operand "
14072                                       "code 'c'");
14073               return;
14074             }
14075           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
14076           return;
14077         case 'f':
14078           if (!COMPARISON_P (x))
14079             {
14080               output_operand_lossage ("operand is neither a constant nor a "
14081                                       "condition code, invalid operand "
14082                                       "code 'f'");
14083               return;
14084             }
14085 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
14086           if (ASSEMBLER_DIALECT == ASM_ATT)
14087             putc ('.', file);
14088 #endif
14089           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
14090           return;
14091
14092         case 'H':
14093           /* It doesn't actually matter what mode we use here, as we're
14094              only going to use this for printing.  */
14095           x = adjust_address_nv (x, DImode, 8);
14096           break;
14097
14098         case '+':
14099           {
14100             rtx x;
14101
14102             if (!optimize
14103                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
14104               return;
14105
14106             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
14107             if (x)
14108               {
14109                 int pred_val = INTVAL (XEXP (x, 0));
14110
14111                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
14112                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
14113                   {
14114                     int taken = pred_val > REG_BR_PROB_BASE / 2;
14115                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
14116
14117                     /* Emit hints only in the case default branch prediction
14118                        heuristics would fail.  */
14119                     if (taken != cputaken)
14120                       {
14121                         /* We use 3e (DS) prefix for taken branches and
14122                            2e (CS) prefix for not taken branches.  */
14123                         if (taken)
14124                           fputs ("ds ; ", file);
14125                         else
14126                           fputs ("cs ; ", file);
14127                       }
14128                   }
14129               }
14130             return;
14131           }
14132
14133         case 'Y':
14134           switch (GET_CODE (x))
14135             {
14136             case NE:
14137               fputs ("neq", file);
14138               break;
14139             case EQ:
14140               fputs ("eq", file);
14141               break;
14142             case GE:
14143             case GEU:
14144               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
14145               break;
14146             case GT:
14147             case GTU:
14148               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
14149               break;
14150             case LE:
14151             case LEU:
14152               fputs ("le", file);
14153               break;
14154             case LT:
14155             case LTU:
14156               fputs ("lt", file);
14157               break;
14158             case UNORDERED:
14159               fputs ("unord", file);
14160               break;
14161             case ORDERED:
14162               fputs ("ord", file);
14163               break;
14164             case UNEQ:
14165               fputs ("ueq", file);
14166               break;
14167             case UNGE:
14168               fputs ("nlt", file);
14169               break;
14170             case UNGT:
14171               fputs ("nle", file);
14172               break;
14173             case UNLE:
14174               fputs ("ule", file);
14175               break;
14176             case UNLT:
14177               fputs ("ult", file);
14178               break;
14179             case LTGT:
14180               fputs ("une", file);
14181               break;
14182             default:
14183               output_operand_lossage ("operand is not a condition code, "
14184                                       "invalid operand code 'Y'");
14185               return;
14186             }
14187           return;
14188
14189         case ';':
14190 #ifndef HAVE_AS_IX86_REP_LOCK_PREFIX
14191           putc (';', file);
14192 #endif
14193           return;
14194
14195         case '@':
14196           if (ASSEMBLER_DIALECT == ASM_ATT)
14197             putc ('%', file);
14198
14199           /* The kernel uses a different segment register for performance
14200              reasons; a system call would not have to trash the userspace
14201              segment register, which would be expensive.  */
14202           if (TARGET_64BIT && ix86_cmodel != CM_KERNEL)
14203             fputs ("fs", file);
14204           else
14205             fputs ("gs", file);
14206           return;
14207
14208         default:
14209             output_operand_lossage ("invalid operand code '%c'", code);
14210         }
14211     }
14212
14213   if (REG_P (x))
14214     print_reg (x, code, file);
14215
14216   else if (MEM_P (x))
14217     {
14218       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
14219       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
14220           && GET_MODE (x) != BLKmode)
14221         {
14222           const char * size;
14223           switch (GET_MODE_SIZE (GET_MODE (x)))
14224             {
14225             case 1: size = "BYTE"; break;
14226             case 2: size = "WORD"; break;
14227             case 4: size = "DWORD"; break;
14228             case 8: size = "QWORD"; break;
14229             case 12: size = "TBYTE"; break;
14230             case 16:
14231               if (GET_MODE (x) == XFmode)
14232                 size = "TBYTE";
14233               else
14234                 size = "XMMWORD";
14235               break;
14236             case 32: size = "YMMWORD"; break;
14237             default:
14238               gcc_unreachable ();
14239             }
14240
14241           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
14242           if (code == 'b')
14243             size = "BYTE";
14244           else if (code == 'w')
14245             size = "WORD";
14246           else if (code == 'k')
14247             size = "DWORD";
14248
14249           fputs (size, file);
14250           fputs (" PTR ", file);
14251         }
14252
14253       x = XEXP (x, 0);
14254       /* Avoid (%rip) for call operands.  */
14255       if (CONSTANT_ADDRESS_P (x) && code == 'P'
14256           && !CONST_INT_P (x))
14257         output_addr_const (file, x);
14258       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
14259         output_operand_lossage ("invalid constraints for operand");
14260       else
14261         output_address (x);
14262     }
14263
14264   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
14265     {
14266       REAL_VALUE_TYPE r;
14267       long l;
14268
14269       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14270       REAL_VALUE_TO_TARGET_SINGLE (r, l);
14271
14272       if (ASSEMBLER_DIALECT == ASM_ATT)
14273         putc ('$', file);
14274       /* Sign extend 32bit SFmode immediate to 8 bytes.  */
14275       if (code == 'q')
14276         fprintf (file, "0x%08llx", (unsigned long long) (int) l);
14277       else
14278         fprintf (file, "0x%08x", (unsigned int) l);
14279     }
14280
14281   /* These float cases don't actually occur as immediate operands.  */
14282   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
14283     {
14284       char dstr[30];
14285
14286       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
14287       fputs (dstr, file);
14288     }
14289
14290   else if (GET_CODE (x) == CONST_DOUBLE
14291            && GET_MODE (x) == XFmode)
14292     {
14293       char dstr[30];
14294
14295       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
14296       fputs (dstr, file);
14297     }
14298
14299   else
14300     {
14301       /* We have patterns that allow zero sets of memory, for instance.
14302          In 64-bit mode, we should probably support all 8-byte vectors,
14303          since we can in fact encode that into an immediate.  */
14304       if (GET_CODE (x) == CONST_VECTOR)
14305         {
14306           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
14307           x = const0_rtx;
14308         }
14309
14310       if (code != 'P')
14311         {
14312           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
14313             {
14314               if (ASSEMBLER_DIALECT == ASM_ATT)
14315                 putc ('$', file);
14316             }
14317           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
14318                    || GET_CODE (x) == LABEL_REF)
14319             {
14320               if (ASSEMBLER_DIALECT == ASM_ATT)
14321                 putc ('$', file);
14322               else
14323                 fputs ("OFFSET FLAT:", file);
14324             }
14325         }
14326       if (CONST_INT_P (x))
14327         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
14328       else if (flag_pic || MACHOPIC_INDIRECT)
14329         output_pic_addr_const (file, x, code);
14330       else
14331         output_addr_const (file, x);
14332     }
14333 }
14334
14335 static bool
14336 ix86_print_operand_punct_valid_p (unsigned char code)
14337 {
14338   return (code == '@' || code == '*' || code == '+'
14339           || code == '&' || code == ';');
14340 }
14341 \f
14342 /* Print a memory operand whose address is ADDR.  */
14343
14344 static void
14345 ix86_print_operand_address (FILE *file, rtx addr)
14346 {
14347   struct ix86_address parts;
14348   rtx base, index, disp;
14349   int scale;
14350   int ok = ix86_decompose_address (addr, &parts);
14351
14352   gcc_assert (ok);
14353
14354   base = parts.base;
14355   index = parts.index;
14356   disp = parts.disp;
14357   scale = parts.scale;
14358
14359   switch (parts.seg)
14360     {
14361     case SEG_DEFAULT:
14362       break;
14363     case SEG_FS:
14364     case SEG_GS:
14365       if (ASSEMBLER_DIALECT == ASM_ATT)
14366         putc ('%', file);
14367       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
14368       break;
14369     default:
14370       gcc_unreachable ();
14371     }
14372
14373   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
14374   if (TARGET_64BIT && !base && !index)
14375     {
14376       rtx symbol = disp;
14377
14378       if (GET_CODE (disp) == CONST
14379           && GET_CODE (XEXP (disp, 0)) == PLUS
14380           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14381         symbol = XEXP (XEXP (disp, 0), 0);
14382
14383       if (GET_CODE (symbol) == LABEL_REF
14384           || (GET_CODE (symbol) == SYMBOL_REF
14385               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
14386         base = pc_rtx;
14387     }
14388   if (!base && !index)
14389     {
14390       /* Displacement only requires special attention.  */
14391
14392       if (CONST_INT_P (disp))
14393         {
14394           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
14395             fputs ("ds:", file);
14396           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
14397         }
14398       else if (flag_pic)
14399         output_pic_addr_const (file, disp, 0);
14400       else
14401         output_addr_const (file, disp);
14402     }
14403   else
14404     {
14405       if (ASSEMBLER_DIALECT == ASM_ATT)
14406         {
14407           if (disp)
14408             {
14409               if (flag_pic)
14410                 output_pic_addr_const (file, disp, 0);
14411               else if (GET_CODE (disp) == LABEL_REF)
14412                 output_asm_label (disp);
14413               else
14414                 output_addr_const (file, disp);
14415             }
14416
14417           putc ('(', file);
14418           if (base)
14419             print_reg (base, 0, file);
14420           if (index)
14421             {
14422               putc (',', file);
14423               print_reg (index, 0, file);
14424               if (scale != 1)
14425                 fprintf (file, ",%d", scale);
14426             }
14427           putc (')', file);
14428         }
14429       else
14430         {
14431           rtx offset = NULL_RTX;
14432
14433           if (disp)
14434             {
14435               /* Pull out the offset of a symbol; print any symbol itself.  */
14436               if (GET_CODE (disp) == CONST
14437                   && GET_CODE (XEXP (disp, 0)) == PLUS
14438                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14439                 {
14440                   offset = XEXP (XEXP (disp, 0), 1);
14441                   disp = gen_rtx_CONST (VOIDmode,
14442                                         XEXP (XEXP (disp, 0), 0));
14443                 }
14444
14445               if (flag_pic)
14446                 output_pic_addr_const (file, disp, 0);
14447               else if (GET_CODE (disp) == LABEL_REF)
14448                 output_asm_label (disp);
14449               else if (CONST_INT_P (disp))
14450                 offset = disp;
14451               else
14452                 output_addr_const (file, disp);
14453             }
14454
14455           putc ('[', file);
14456           if (base)
14457             {
14458               print_reg (base, 0, file);
14459               if (offset)
14460                 {
14461                   if (INTVAL (offset) >= 0)
14462                     putc ('+', file);
14463                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14464                 }
14465             }
14466           else if (offset)
14467             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14468           else
14469             putc ('0', file);
14470
14471           if (index)
14472             {
14473               putc ('+', file);
14474               print_reg (index, 0, file);
14475               if (scale != 1)
14476                 fprintf (file, "*%d", scale);
14477             }
14478           putc (']', file);
14479         }
14480     }
14481 }
14482
14483 /* Implementation of TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
14484
14485 static bool
14486 i386_asm_output_addr_const_extra (FILE *file, rtx x)
14487 {
14488   rtx op;
14489
14490   if (GET_CODE (x) != UNSPEC)
14491     return false;
14492
14493   op = XVECEXP (x, 0, 0);
14494   switch (XINT (x, 1))
14495     {
14496     case UNSPEC_GOTTPOFF:
14497       output_addr_const (file, op);
14498       /* FIXME: This might be @TPOFF in Sun ld.  */
14499       fputs ("@gottpoff", file);
14500       break;
14501     case UNSPEC_TPOFF:
14502       output_addr_const (file, op);
14503       fputs ("@tpoff", file);
14504       break;
14505     case UNSPEC_NTPOFF:
14506       output_addr_const (file, op);
14507       if (TARGET_64BIT)
14508         fputs ("@tpoff", file);
14509       else
14510         fputs ("@ntpoff", file);
14511       break;
14512     case UNSPEC_DTPOFF:
14513       output_addr_const (file, op);
14514       fputs ("@dtpoff", file);
14515       break;
14516     case UNSPEC_GOTNTPOFF:
14517       output_addr_const (file, op);
14518       if (TARGET_64BIT)
14519         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
14520                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
14521       else
14522         fputs ("@gotntpoff", file);
14523       break;
14524     case UNSPEC_INDNTPOFF:
14525       output_addr_const (file, op);
14526       fputs ("@indntpoff", file);
14527       break;
14528 #if TARGET_MACHO
14529     case UNSPEC_MACHOPIC_OFFSET:
14530       output_addr_const (file, op);
14531       putc ('-', file);
14532       machopic_output_function_base_name (file);
14533       break;
14534 #endif
14535
14536     case UNSPEC_STACK_CHECK:
14537       {
14538         int offset;
14539
14540         gcc_assert (flag_split_stack);
14541
14542 #ifdef TARGET_THREAD_SPLIT_STACK_OFFSET
14543         offset = TARGET_THREAD_SPLIT_STACK_OFFSET;
14544 #else
14545         gcc_unreachable ();
14546 #endif
14547
14548         fprintf (file, "%s:%d", TARGET_64BIT ? "%fs" : "%gs", offset);
14549       }
14550       break;
14551
14552     default:
14553       return false;
14554     }
14555
14556   return true;
14557 }
14558 \f
14559 /* Split one or more double-mode RTL references into pairs of half-mode
14560    references.  The RTL can be REG, offsettable MEM, integer constant, or
14561    CONST_DOUBLE.  "operands" is a pointer to an array of double-mode RTLs to
14562    split and "num" is its length.  lo_half and hi_half are output arrays
14563    that parallel "operands".  */
14564
14565 void
14566 split_double_mode (enum machine_mode mode, rtx operands[],
14567                    int num, rtx lo_half[], rtx hi_half[])
14568 {
14569   enum machine_mode half_mode;
14570   unsigned int byte;
14571
14572   switch (mode)
14573     {
14574     case TImode:
14575       half_mode = DImode;
14576       break;
14577     case DImode:
14578       half_mode = SImode;
14579       break;
14580     default:
14581       gcc_unreachable ();
14582     }
14583
14584   byte = GET_MODE_SIZE (half_mode);
14585
14586   while (num--)
14587     {
14588       rtx op = operands[num];
14589
14590       /* simplify_subreg refuse to split volatile memory addresses,
14591          but we still have to handle it.  */
14592       if (MEM_P (op))
14593         {
14594           lo_half[num] = adjust_address (op, half_mode, 0);
14595           hi_half[num] = adjust_address (op, half_mode, byte);
14596         }
14597       else
14598         {
14599           lo_half[num] = simplify_gen_subreg (half_mode, op,
14600                                               GET_MODE (op) == VOIDmode
14601                                               ? mode : GET_MODE (op), 0);
14602           hi_half[num] = simplify_gen_subreg (half_mode, op,
14603                                               GET_MODE (op) == VOIDmode
14604                                               ? mode : GET_MODE (op), byte);
14605         }
14606     }
14607 }
14608 \f
14609 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
14610    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
14611    is the expression of the binary operation.  The output may either be
14612    emitted here, or returned to the caller, like all output_* functions.
14613
14614    There is no guarantee that the operands are the same mode, as they
14615    might be within FLOAT or FLOAT_EXTEND expressions.  */
14616
14617 #ifndef SYSV386_COMPAT
14618 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
14619    wants to fix the assemblers because that causes incompatibility
14620    with gcc.  No-one wants to fix gcc because that causes
14621    incompatibility with assemblers...  You can use the option of
14622    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
14623 #define SYSV386_COMPAT 1
14624 #endif
14625
14626 const char *
14627 output_387_binary_op (rtx insn, rtx *operands)
14628 {
14629   static char buf[40];
14630   const char *p;
14631   const char *ssep;
14632   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
14633
14634 #ifdef ENABLE_CHECKING
14635   /* Even if we do not want to check the inputs, this documents input
14636      constraints.  Which helps in understanding the following code.  */
14637   if (STACK_REG_P (operands[0])
14638       && ((REG_P (operands[1])
14639            && REGNO (operands[0]) == REGNO (operands[1])
14640            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
14641           || (REG_P (operands[2])
14642               && REGNO (operands[0]) == REGNO (operands[2])
14643               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
14644       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
14645     ; /* ok */
14646   else
14647     gcc_assert (is_sse);
14648 #endif
14649
14650   switch (GET_CODE (operands[3]))
14651     {
14652     case PLUS:
14653       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14654           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14655         p = "fiadd";
14656       else
14657         p = "fadd";
14658       ssep = "vadd";
14659       break;
14660
14661     case MINUS:
14662       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14663           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14664         p = "fisub";
14665       else
14666         p = "fsub";
14667       ssep = "vsub";
14668       break;
14669
14670     case MULT:
14671       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14672           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14673         p = "fimul";
14674       else
14675         p = "fmul";
14676       ssep = "vmul";
14677       break;
14678
14679     case DIV:
14680       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14681           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14682         p = "fidiv";
14683       else
14684         p = "fdiv";
14685       ssep = "vdiv";
14686       break;
14687
14688     default:
14689       gcc_unreachable ();
14690     }
14691
14692   if (is_sse)
14693    {
14694      if (TARGET_AVX)
14695        {
14696          strcpy (buf, ssep);
14697          if (GET_MODE (operands[0]) == SFmode)
14698            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
14699          else
14700            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
14701        }
14702      else
14703        {
14704          strcpy (buf, ssep + 1);
14705          if (GET_MODE (operands[0]) == SFmode)
14706            strcat (buf, "ss\t{%2, %0|%0, %2}");
14707          else
14708            strcat (buf, "sd\t{%2, %0|%0, %2}");
14709        }
14710       return buf;
14711    }
14712   strcpy (buf, p);
14713
14714   switch (GET_CODE (operands[3]))
14715     {
14716     case MULT:
14717     case PLUS:
14718       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
14719         {
14720           rtx temp = operands[2];
14721           operands[2] = operands[1];
14722           operands[1] = temp;
14723         }
14724
14725       /* know operands[0] == operands[1].  */
14726
14727       if (MEM_P (operands[2]))
14728         {
14729           p = "%Z2\t%2";
14730           break;
14731         }
14732
14733       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
14734         {
14735           if (STACK_TOP_P (operands[0]))
14736             /* How is it that we are storing to a dead operand[2]?
14737                Well, presumably operands[1] is dead too.  We can't
14738                store the result to st(0) as st(0) gets popped on this
14739                instruction.  Instead store to operands[2] (which I
14740                think has to be st(1)).  st(1) will be popped later.
14741                gcc <= 2.8.1 didn't have this check and generated
14742                assembly code that the Unixware assembler rejected.  */
14743             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
14744           else
14745             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
14746           break;
14747         }
14748
14749       if (STACK_TOP_P (operands[0]))
14750         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
14751       else
14752         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
14753       break;
14754
14755     case MINUS:
14756     case DIV:
14757       if (MEM_P (operands[1]))
14758         {
14759           p = "r%Z1\t%1";
14760           break;
14761         }
14762
14763       if (MEM_P (operands[2]))
14764         {
14765           p = "%Z2\t%2";
14766           break;
14767         }
14768
14769       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
14770         {
14771 #if SYSV386_COMPAT
14772           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
14773              derived assemblers, confusingly reverse the direction of
14774              the operation for fsub{r} and fdiv{r} when the
14775              destination register is not st(0).  The Intel assembler
14776              doesn't have this brain damage.  Read !SYSV386_COMPAT to
14777              figure out what the hardware really does.  */
14778           if (STACK_TOP_P (operands[0]))
14779             p = "{p\t%0, %2|rp\t%2, %0}";
14780           else
14781             p = "{rp\t%2, %0|p\t%0, %2}";
14782 #else
14783           if (STACK_TOP_P (operands[0]))
14784             /* As above for fmul/fadd, we can't store to st(0).  */
14785             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
14786           else
14787             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
14788 #endif
14789           break;
14790         }
14791
14792       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
14793         {
14794 #if SYSV386_COMPAT
14795           if (STACK_TOP_P (operands[0]))
14796             p = "{rp\t%0, %1|p\t%1, %0}";
14797           else
14798             p = "{p\t%1, %0|rp\t%0, %1}";
14799 #else
14800           if (STACK_TOP_P (operands[0]))
14801             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
14802           else
14803             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
14804 #endif
14805           break;
14806         }
14807
14808       if (STACK_TOP_P (operands[0]))
14809         {
14810           if (STACK_TOP_P (operands[1]))
14811             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
14812           else
14813             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
14814           break;
14815         }
14816       else if (STACK_TOP_P (operands[1]))
14817         {
14818 #if SYSV386_COMPAT
14819           p = "{\t%1, %0|r\t%0, %1}";
14820 #else
14821           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
14822 #endif
14823         }
14824       else
14825         {
14826 #if SYSV386_COMPAT
14827           p = "{r\t%2, %0|\t%0, %2}";
14828 #else
14829           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
14830 #endif
14831         }
14832       break;
14833
14834     default:
14835       gcc_unreachable ();
14836     }
14837
14838   strcat (buf, p);
14839   return buf;
14840 }
14841
14842 /* Return needed mode for entity in optimize_mode_switching pass.  */
14843
14844 int
14845 ix86_mode_needed (int entity, rtx insn)
14846 {
14847   enum attr_i387_cw mode;
14848
14849   /* The mode UNINITIALIZED is used to store control word after a
14850      function call or ASM pattern.  The mode ANY specify that function
14851      has no requirements on the control word and make no changes in the
14852      bits we are interested in.  */
14853
14854   if (CALL_P (insn)
14855       || (NONJUMP_INSN_P (insn)
14856           && (asm_noperands (PATTERN (insn)) >= 0
14857               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
14858     return I387_CW_UNINITIALIZED;
14859
14860   if (recog_memoized (insn) < 0)
14861     return I387_CW_ANY;
14862
14863   mode = get_attr_i387_cw (insn);
14864
14865   switch (entity)
14866     {
14867     case I387_TRUNC:
14868       if (mode == I387_CW_TRUNC)
14869         return mode;
14870       break;
14871
14872     case I387_FLOOR:
14873       if (mode == I387_CW_FLOOR)
14874         return mode;
14875       break;
14876
14877     case I387_CEIL:
14878       if (mode == I387_CW_CEIL)
14879         return mode;
14880       break;
14881
14882     case I387_MASK_PM:
14883       if (mode == I387_CW_MASK_PM)
14884         return mode;
14885       break;
14886
14887     default:
14888       gcc_unreachable ();
14889     }
14890
14891   return I387_CW_ANY;
14892 }
14893
14894 /* Output code to initialize control word copies used by trunc?f?i and
14895    rounding patterns.  CURRENT_MODE is set to current control word,
14896    while NEW_MODE is set to new control word.  */
14897
14898 void
14899 emit_i387_cw_initialization (int mode)
14900 {
14901   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
14902   rtx new_mode;
14903
14904   enum ix86_stack_slot slot;
14905
14906   rtx reg = gen_reg_rtx (HImode);
14907
14908   emit_insn (gen_x86_fnstcw_1 (stored_mode));
14909   emit_move_insn (reg, copy_rtx (stored_mode));
14910
14911   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
14912       || optimize_function_for_size_p (cfun))
14913     {
14914       switch (mode)
14915         {
14916         case I387_CW_TRUNC:
14917           /* round toward zero (truncate) */
14918           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
14919           slot = SLOT_CW_TRUNC;
14920           break;
14921
14922         case I387_CW_FLOOR:
14923           /* round down toward -oo */
14924           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
14925           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
14926           slot = SLOT_CW_FLOOR;
14927           break;
14928
14929         case I387_CW_CEIL:
14930           /* round up toward +oo */
14931           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
14932           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
14933           slot = SLOT_CW_CEIL;
14934           break;
14935
14936         case I387_CW_MASK_PM:
14937           /* mask precision exception for nearbyint() */
14938           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
14939           slot = SLOT_CW_MASK_PM;
14940           break;
14941
14942         default:
14943           gcc_unreachable ();
14944         }
14945     }
14946   else
14947     {
14948       switch (mode)
14949         {
14950         case I387_CW_TRUNC:
14951           /* round toward zero (truncate) */
14952           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
14953           slot = SLOT_CW_TRUNC;
14954           break;
14955
14956         case I387_CW_FLOOR:
14957           /* round down toward -oo */
14958           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
14959           slot = SLOT_CW_FLOOR;
14960           break;
14961
14962         case I387_CW_CEIL:
14963           /* round up toward +oo */
14964           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
14965           slot = SLOT_CW_CEIL;
14966           break;
14967
14968         case I387_CW_MASK_PM:
14969           /* mask precision exception for nearbyint() */
14970           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
14971           slot = SLOT_CW_MASK_PM;
14972           break;
14973
14974         default:
14975           gcc_unreachable ();
14976         }
14977     }
14978
14979   gcc_assert (slot < MAX_386_STACK_LOCALS);
14980
14981   new_mode = assign_386_stack_local (HImode, slot);
14982   emit_move_insn (new_mode, reg);
14983 }
14984
14985 /* Output code for INSN to convert a float to a signed int.  OPERANDS
14986    are the insn operands.  The output may be [HSD]Imode and the input
14987    operand may be [SDX]Fmode.  */
14988
14989 const char *
14990 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
14991 {
14992   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
14993   int dimode_p = GET_MODE (operands[0]) == DImode;
14994   int round_mode = get_attr_i387_cw (insn);
14995
14996   /* Jump through a hoop or two for DImode, since the hardware has no
14997      non-popping instruction.  We used to do this a different way, but
14998      that was somewhat fragile and broke with post-reload splitters.  */
14999   if ((dimode_p || fisttp) && !stack_top_dies)
15000     output_asm_insn ("fld\t%y1", operands);
15001
15002   gcc_assert (STACK_TOP_P (operands[1]));
15003   gcc_assert (MEM_P (operands[0]));
15004   gcc_assert (GET_MODE (operands[1]) != TFmode);
15005
15006   if (fisttp)
15007       output_asm_insn ("fisttp%Z0\t%0", operands);
15008   else
15009     {
15010       if (round_mode != I387_CW_ANY)
15011         output_asm_insn ("fldcw\t%3", operands);
15012       if (stack_top_dies || dimode_p)
15013         output_asm_insn ("fistp%Z0\t%0", operands);
15014       else
15015         output_asm_insn ("fist%Z0\t%0", operands);
15016       if (round_mode != I387_CW_ANY)
15017         output_asm_insn ("fldcw\t%2", operands);
15018     }
15019
15020   return "";
15021 }
15022
15023 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
15024    have the values zero or one, indicates the ffreep insn's operand
15025    from the OPERANDS array.  */
15026
15027 static const char *
15028 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
15029 {
15030   if (TARGET_USE_FFREEP)
15031 #ifdef HAVE_AS_IX86_FFREEP
15032     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
15033 #else
15034     {
15035       static char retval[32];
15036       int regno = REGNO (operands[opno]);
15037
15038       gcc_assert (FP_REGNO_P (regno));
15039
15040       regno -= FIRST_STACK_REG;
15041
15042       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
15043       return retval;
15044     }
15045 #endif
15046
15047   return opno ? "fstp\t%y1" : "fstp\t%y0";
15048 }
15049
15050
15051 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
15052    should be used.  UNORDERED_P is true when fucom should be used.  */
15053
15054 const char *
15055 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
15056 {
15057   int stack_top_dies;
15058   rtx cmp_op0, cmp_op1;
15059   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
15060
15061   if (eflags_p)
15062     {
15063       cmp_op0 = operands[0];
15064       cmp_op1 = operands[1];
15065     }
15066   else
15067     {
15068       cmp_op0 = operands[1];
15069       cmp_op1 = operands[2];
15070     }
15071
15072   if (is_sse)
15073     {
15074       static const char ucomiss[] = "vucomiss\t{%1, %0|%0, %1}";
15075       static const char ucomisd[] = "vucomisd\t{%1, %0|%0, %1}";
15076       static const char comiss[] = "vcomiss\t{%1, %0|%0, %1}";
15077       static const char comisd[] = "vcomisd\t{%1, %0|%0, %1}";
15078
15079       if (GET_MODE (operands[0]) == SFmode)
15080         if (unordered_p)
15081           return &ucomiss[TARGET_AVX ? 0 : 1];
15082         else
15083           return &comiss[TARGET_AVX ? 0 : 1];
15084       else
15085         if (unordered_p)
15086           return &ucomisd[TARGET_AVX ? 0 : 1];
15087         else
15088           return &comisd[TARGET_AVX ? 0 : 1];
15089     }
15090
15091   gcc_assert (STACK_TOP_P (cmp_op0));
15092
15093   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
15094
15095   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
15096     {
15097       if (stack_top_dies)
15098         {
15099           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
15100           return output_387_ffreep (operands, 1);
15101         }
15102       else
15103         return "ftst\n\tfnstsw\t%0";
15104     }
15105
15106   if (STACK_REG_P (cmp_op1)
15107       && stack_top_dies
15108       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
15109       && REGNO (cmp_op1) != FIRST_STACK_REG)
15110     {
15111       /* If both the top of the 387 stack dies, and the other operand
15112          is also a stack register that dies, then this must be a
15113          `fcompp' float compare */
15114
15115       if (eflags_p)
15116         {
15117           /* There is no double popping fcomi variant.  Fortunately,
15118              eflags is immune from the fstp's cc clobbering.  */
15119           if (unordered_p)
15120             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
15121           else
15122             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
15123           return output_387_ffreep (operands, 0);
15124         }
15125       else
15126         {
15127           if (unordered_p)
15128             return "fucompp\n\tfnstsw\t%0";
15129           else
15130             return "fcompp\n\tfnstsw\t%0";
15131         }
15132     }
15133   else
15134     {
15135       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
15136
15137       static const char * const alt[16] =
15138       {
15139         "fcom%Z2\t%y2\n\tfnstsw\t%0",
15140         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
15141         "fucom%Z2\t%y2\n\tfnstsw\t%0",
15142         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
15143
15144         "ficom%Z2\t%y2\n\tfnstsw\t%0",
15145         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
15146         NULL,
15147         NULL,
15148
15149         "fcomi\t{%y1, %0|%0, %y1}",
15150         "fcomip\t{%y1, %0|%0, %y1}",
15151         "fucomi\t{%y1, %0|%0, %y1}",
15152         "fucomip\t{%y1, %0|%0, %y1}",
15153
15154         NULL,
15155         NULL,
15156         NULL,
15157         NULL
15158       };
15159
15160       int mask;
15161       const char *ret;
15162
15163       mask  = eflags_p << 3;
15164       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
15165       mask |= unordered_p << 1;
15166       mask |= stack_top_dies;
15167
15168       gcc_assert (mask < 16);
15169       ret = alt[mask];
15170       gcc_assert (ret);
15171
15172       return ret;
15173     }
15174 }
15175
15176 void
15177 ix86_output_addr_vec_elt (FILE *file, int value)
15178 {
15179   const char *directive = ASM_LONG;
15180
15181 #ifdef ASM_QUAD
15182   if (TARGET_64BIT)
15183     directive = ASM_QUAD;
15184 #else
15185   gcc_assert (!TARGET_64BIT);
15186 #endif
15187
15188   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
15189 }
15190
15191 void
15192 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
15193 {
15194   const char *directive = ASM_LONG;
15195
15196 #ifdef ASM_QUAD
15197   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
15198     directive = ASM_QUAD;
15199 #else
15200   gcc_assert (!TARGET_64BIT);
15201 #endif
15202   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
15203   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
15204     fprintf (file, "%s%s%d-%s%d\n",
15205              directive, LPREFIX, value, LPREFIX, rel);
15206   else if (HAVE_AS_GOTOFF_IN_DATA)
15207     fprintf (file, ASM_LONG "%s%d@GOTOFF\n", LPREFIX, value);
15208 #if TARGET_MACHO
15209   else if (TARGET_MACHO)
15210     {
15211       fprintf (file, ASM_LONG "%s%d-", LPREFIX, value);
15212       machopic_output_function_base_name (file);
15213       putc ('\n', file);
15214     }
15215 #endif
15216   else
15217     asm_fprintf (file, ASM_LONG "%U%s+[.-%s%d]\n",
15218                  GOT_SYMBOL_NAME, LPREFIX, value);
15219 }
15220 \f
15221 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
15222    for the target.  */
15223
15224 void
15225 ix86_expand_clear (rtx dest)
15226 {
15227   rtx tmp;
15228
15229   /* We play register width games, which are only valid after reload.  */
15230   gcc_assert (reload_completed);
15231
15232   /* Avoid HImode and its attendant prefix byte.  */
15233   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
15234     dest = gen_rtx_REG (SImode, REGNO (dest));
15235   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
15236
15237   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
15238   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
15239     {
15240       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15241       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
15242     }
15243
15244   emit_insn (tmp);
15245 }
15246
15247 /* X is an unchanging MEM.  If it is a constant pool reference, return
15248    the constant pool rtx, else NULL.  */
15249
15250 rtx
15251 maybe_get_pool_constant (rtx x)
15252 {
15253   x = ix86_delegitimize_address (XEXP (x, 0));
15254
15255   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
15256     return get_pool_constant (x);
15257
15258   return NULL_RTX;
15259 }
15260
15261 void
15262 ix86_expand_move (enum machine_mode mode, rtx operands[])
15263 {
15264   rtx op0, op1;
15265   enum tls_model model;
15266
15267   op0 = operands[0];
15268   op1 = operands[1];
15269
15270   if (GET_CODE (op1) == SYMBOL_REF)
15271     {
15272       model = SYMBOL_REF_TLS_MODEL (op1);
15273       if (model)
15274         {
15275           op1 = legitimize_tls_address (op1, model, true);
15276           op1 = force_operand (op1, op0);
15277           if (op1 == op0)
15278             return;
15279         }
15280       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15281                && SYMBOL_REF_DLLIMPORT_P (op1))
15282         op1 = legitimize_dllimport_symbol (op1, false);
15283     }
15284   else if (GET_CODE (op1) == CONST
15285            && GET_CODE (XEXP (op1, 0)) == PLUS
15286            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
15287     {
15288       rtx addend = XEXP (XEXP (op1, 0), 1);
15289       rtx symbol = XEXP (XEXP (op1, 0), 0);
15290       rtx tmp = NULL;
15291
15292       model = SYMBOL_REF_TLS_MODEL (symbol);
15293       if (model)
15294         tmp = legitimize_tls_address (symbol, model, true);
15295       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15296                && SYMBOL_REF_DLLIMPORT_P (symbol))
15297         tmp = legitimize_dllimport_symbol (symbol, true);
15298
15299       if (tmp)
15300         {
15301           tmp = force_operand (tmp, NULL);
15302           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
15303                                      op0, 1, OPTAB_DIRECT);
15304           if (tmp == op0)
15305             return;
15306         }
15307     }
15308
15309   if ((flag_pic || MACHOPIC_INDIRECT) 
15310        && mode == Pmode && symbolic_operand (op1, Pmode))
15311     {
15312       if (TARGET_MACHO && !TARGET_64BIT)
15313         {
15314 #if TARGET_MACHO
15315           /* dynamic-no-pic */
15316           if (MACHOPIC_INDIRECT)
15317             {
15318               rtx temp = ((reload_in_progress
15319                            || ((op0 && REG_P (op0))
15320                                && mode == Pmode))
15321                           ? op0 : gen_reg_rtx (Pmode));
15322               op1 = machopic_indirect_data_reference (op1, temp);
15323               if (MACHOPIC_PURE)
15324                 op1 = machopic_legitimize_pic_address (op1, mode,
15325                                                        temp == op1 ? 0 : temp);
15326             }
15327           if (op0 != op1 && GET_CODE (op0) != MEM)
15328             {
15329               rtx insn = gen_rtx_SET (VOIDmode, op0, op1);
15330               emit_insn (insn);
15331               return;
15332             }
15333           if (GET_CODE (op0) == MEM)
15334             op1 = force_reg (Pmode, op1);
15335           else
15336             {
15337               rtx temp = op0;
15338               if (GET_CODE (temp) != REG)
15339                 temp = gen_reg_rtx (Pmode);
15340               temp = legitimize_pic_address (op1, temp);
15341               if (temp == op0)
15342             return;
15343               op1 = temp;
15344             }
15345       /* dynamic-no-pic */
15346 #endif
15347         }
15348       else
15349         {
15350           if (MEM_P (op0))
15351             op1 = force_reg (Pmode, op1);
15352           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
15353             {
15354               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
15355               op1 = legitimize_pic_address (op1, reg);
15356               if (op0 == op1)
15357                 return;
15358             }
15359         }
15360     }
15361   else
15362     {
15363       if (MEM_P (op0)
15364           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
15365               || !push_operand (op0, mode))
15366           && MEM_P (op1))
15367         op1 = force_reg (mode, op1);
15368
15369       if (push_operand (op0, mode)
15370           && ! general_no_elim_operand (op1, mode))
15371         op1 = copy_to_mode_reg (mode, op1);
15372
15373       /* Force large constants in 64bit compilation into register
15374          to get them CSEed.  */
15375       if (can_create_pseudo_p ()
15376           && (mode == DImode) && TARGET_64BIT
15377           && immediate_operand (op1, mode)
15378           && !x86_64_zext_immediate_operand (op1, VOIDmode)
15379           && !register_operand (op0, mode)
15380           && optimize)
15381         op1 = copy_to_mode_reg (mode, op1);
15382
15383       if (can_create_pseudo_p ()
15384           && FLOAT_MODE_P (mode)
15385           && GET_CODE (op1) == CONST_DOUBLE)
15386         {
15387           /* If we are loading a floating point constant to a register,
15388              force the value to memory now, since we'll get better code
15389              out the back end.  */
15390
15391           op1 = validize_mem (force_const_mem (mode, op1));
15392           if (!register_operand (op0, mode))
15393             {
15394               rtx temp = gen_reg_rtx (mode);
15395               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
15396               emit_move_insn (op0, temp);
15397               return;
15398             }
15399         }
15400     }
15401
15402   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15403 }
15404
15405 void
15406 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
15407 {
15408   rtx op0 = operands[0], op1 = operands[1];
15409   unsigned int align = GET_MODE_ALIGNMENT (mode);
15410
15411   /* Force constants other than zero into memory.  We do not know how
15412      the instructions used to build constants modify the upper 64 bits
15413      of the register, once we have that information we may be able
15414      to handle some of them more efficiently.  */
15415   if (can_create_pseudo_p ()
15416       && register_operand (op0, mode)
15417       && (CONSTANT_P (op1)
15418           || (GET_CODE (op1) == SUBREG
15419               && CONSTANT_P (SUBREG_REG (op1))))
15420       && !standard_sse_constant_p (op1))
15421     op1 = validize_mem (force_const_mem (mode, op1));
15422
15423   /* We need to check memory alignment for SSE mode since attribute
15424      can make operands unaligned.  */
15425   if (can_create_pseudo_p ()
15426       && SSE_REG_MODE_P (mode)
15427       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
15428           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
15429     {
15430       rtx tmp[2];
15431
15432       /* ix86_expand_vector_move_misalign() does not like constants ... */
15433       if (CONSTANT_P (op1)
15434           || (GET_CODE (op1) == SUBREG
15435               && CONSTANT_P (SUBREG_REG (op1))))
15436         op1 = validize_mem (force_const_mem (mode, op1));
15437
15438       /* ... nor both arguments in memory.  */
15439       if (!register_operand (op0, mode)
15440           && !register_operand (op1, mode))
15441         op1 = force_reg (mode, op1);
15442
15443       tmp[0] = op0; tmp[1] = op1;
15444       ix86_expand_vector_move_misalign (mode, tmp);
15445       return;
15446     }
15447
15448   /* Make operand1 a register if it isn't already.  */
15449   if (can_create_pseudo_p ()
15450       && !register_operand (op0, mode)
15451       && !register_operand (op1, mode))
15452     {
15453       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
15454       return;
15455     }
15456
15457   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15458 }
15459
15460 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
15461    straight to ix86_expand_vector_move.  */
15462 /* Code generation for scalar reg-reg moves of single and double precision data:
15463      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
15464        movaps reg, reg
15465      else
15466        movss reg, reg
15467      if (x86_sse_partial_reg_dependency == true)
15468        movapd reg, reg
15469      else
15470        movsd reg, reg
15471
15472    Code generation for scalar loads of double precision data:
15473      if (x86_sse_split_regs == true)
15474        movlpd mem, reg      (gas syntax)
15475      else
15476        movsd mem, reg
15477
15478    Code generation for unaligned packed loads of single precision data
15479    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
15480      if (x86_sse_unaligned_move_optimal)
15481        movups mem, reg
15482
15483      if (x86_sse_partial_reg_dependency == true)
15484        {
15485          xorps  reg, reg
15486          movlps mem, reg
15487          movhps mem+8, reg
15488        }
15489      else
15490        {
15491          movlps mem, reg
15492          movhps mem+8, reg
15493        }
15494
15495    Code generation for unaligned packed loads of double precision data
15496    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
15497      if (x86_sse_unaligned_move_optimal)
15498        movupd mem, reg
15499
15500      if (x86_sse_split_regs == true)
15501        {
15502          movlpd mem, reg
15503          movhpd mem+8, reg
15504        }
15505      else
15506        {
15507          movsd  mem, reg
15508          movhpd mem+8, reg
15509        }
15510  */
15511
15512 void
15513 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
15514 {
15515   rtx op0, op1, m;
15516
15517   op0 = operands[0];
15518   op1 = operands[1];
15519
15520   if (TARGET_AVX)
15521     {
15522       switch (GET_MODE_CLASS (mode))
15523         {
15524         case MODE_VECTOR_INT:
15525         case MODE_INT:
15526           switch (GET_MODE_SIZE (mode))
15527             {
15528             case 16:
15529               /*  If we're optimizing for size, movups is the smallest.  */
15530               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15531                 {
15532                   op0 = gen_lowpart (V4SFmode, op0);
15533                   op1 = gen_lowpart (V4SFmode, op1);
15534                   emit_insn (gen_avx_movups (op0, op1));
15535                   return;
15536                 }
15537               op0 = gen_lowpart (V16QImode, op0);
15538               op1 = gen_lowpart (V16QImode, op1);
15539               emit_insn (gen_avx_movdqu (op0, op1));
15540               break;
15541             case 32:
15542               op0 = gen_lowpart (V32QImode, op0);
15543               op1 = gen_lowpart (V32QImode, op1);
15544               emit_insn (gen_avx_movdqu256 (op0, op1));
15545               break;
15546             default:
15547               gcc_unreachable ();
15548             }
15549           break;
15550         case MODE_VECTOR_FLOAT:
15551           op0 = gen_lowpart (mode, op0);
15552           op1 = gen_lowpart (mode, op1);
15553
15554           switch (mode)
15555             {
15556             case V4SFmode:
15557               emit_insn (gen_avx_movups (op0, op1));
15558               break;
15559             case V8SFmode:
15560               emit_insn (gen_avx_movups256 (op0, op1));
15561               break;
15562             case V2DFmode:
15563               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15564                 {
15565                   op0 = gen_lowpart (V4SFmode, op0);
15566                   op1 = gen_lowpart (V4SFmode, op1);
15567                   emit_insn (gen_avx_movups (op0, op1));
15568                   return;
15569                 }
15570               emit_insn (gen_avx_movupd (op0, op1));
15571               break;
15572             case V4DFmode:
15573               emit_insn (gen_avx_movupd256 (op0, op1));
15574               break;
15575             default:
15576               gcc_unreachable ();
15577             }
15578           break;
15579
15580         default:
15581           gcc_unreachable ();
15582         }
15583
15584       return;
15585     }
15586
15587   if (MEM_P (op1))
15588     {
15589       /* If we're optimizing for size, movups is the smallest.  */
15590       if (optimize_insn_for_size_p ()
15591           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15592         {
15593           op0 = gen_lowpart (V4SFmode, op0);
15594           op1 = gen_lowpart (V4SFmode, op1);
15595           emit_insn (gen_sse_movups (op0, op1));
15596           return;
15597         }
15598
15599       /* ??? If we have typed data, then it would appear that using
15600          movdqu is the only way to get unaligned data loaded with
15601          integer type.  */
15602       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15603         {
15604           op0 = gen_lowpart (V16QImode, op0);
15605           op1 = gen_lowpart (V16QImode, op1);
15606           emit_insn (gen_sse2_movdqu (op0, op1));
15607           return;
15608         }
15609
15610       if (TARGET_SSE2 && mode == V2DFmode)
15611         {
15612           rtx zero;
15613
15614           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
15615             {
15616               op0 = gen_lowpart (V2DFmode, op0);
15617               op1 = gen_lowpart (V2DFmode, op1);
15618               emit_insn (gen_sse2_movupd (op0, op1));
15619               return;
15620             }
15621
15622           /* When SSE registers are split into halves, we can avoid
15623              writing to the top half twice.  */
15624           if (TARGET_SSE_SPLIT_REGS)
15625             {
15626               emit_clobber (op0);
15627               zero = op0;
15628             }
15629           else
15630             {
15631               /* ??? Not sure about the best option for the Intel chips.
15632                  The following would seem to satisfy; the register is
15633                  entirely cleared, breaking the dependency chain.  We
15634                  then store to the upper half, with a dependency depth
15635                  of one.  A rumor has it that Intel recommends two movsd
15636                  followed by an unpacklpd, but this is unconfirmed.  And
15637                  given that the dependency depth of the unpacklpd would
15638                  still be one, I'm not sure why this would be better.  */
15639               zero = CONST0_RTX (V2DFmode);
15640             }
15641
15642           m = adjust_address (op1, DFmode, 0);
15643           emit_insn (gen_sse2_loadlpd (op0, zero, m));
15644           m = adjust_address (op1, DFmode, 8);
15645           emit_insn (gen_sse2_loadhpd (op0, op0, m));
15646         }
15647       else
15648         {
15649           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
15650             {
15651               op0 = gen_lowpart (V4SFmode, op0);
15652               op1 = gen_lowpart (V4SFmode, op1);
15653               emit_insn (gen_sse_movups (op0, op1));
15654               return;
15655             }
15656
15657           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
15658             emit_move_insn (op0, CONST0_RTX (mode));
15659           else
15660             emit_clobber (op0);
15661
15662           if (mode != V4SFmode)
15663             op0 = gen_lowpart (V4SFmode, op0);
15664           m = adjust_address (op1, V2SFmode, 0);
15665           emit_insn (gen_sse_loadlps (op0, op0, m));
15666           m = adjust_address (op1, V2SFmode, 8);
15667           emit_insn (gen_sse_loadhps (op0, op0, m));
15668         }
15669     }
15670   else if (MEM_P (op0))
15671     {
15672       /* If we're optimizing for size, movups is the smallest.  */
15673       if (optimize_insn_for_size_p ()
15674           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15675         {
15676           op0 = gen_lowpart (V4SFmode, op0);
15677           op1 = gen_lowpart (V4SFmode, op1);
15678           emit_insn (gen_sse_movups (op0, op1));
15679           return;
15680         }
15681
15682       /* ??? Similar to above, only less clear because of quote
15683          typeless stores unquote.  */
15684       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
15685           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15686         {
15687           op0 = gen_lowpart (V16QImode, op0);
15688           op1 = gen_lowpart (V16QImode, op1);
15689           emit_insn (gen_sse2_movdqu (op0, op1));
15690           return;
15691         }
15692
15693       if (TARGET_SSE2 && mode == V2DFmode)
15694         {
15695           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
15696             {
15697               op0 = gen_lowpart (V2DFmode, op0);
15698               op1 = gen_lowpart (V2DFmode, op1);
15699               emit_insn (gen_sse2_movupd (op0, op1));
15700             }
15701           else
15702             {
15703               m = adjust_address (op0, DFmode, 0);
15704               emit_insn (gen_sse2_storelpd (m, op1));
15705               m = adjust_address (op0, DFmode, 8);
15706               emit_insn (gen_sse2_storehpd (m, op1));
15707             }
15708         }
15709       else
15710         {
15711           if (mode != V4SFmode)
15712             op1 = gen_lowpart (V4SFmode, op1);
15713
15714           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
15715             {
15716               op0 = gen_lowpart (V4SFmode, op0);
15717               emit_insn (gen_sse_movups (op0, op1));
15718             }
15719           else
15720             {
15721               m = adjust_address (op0, V2SFmode, 0);
15722               emit_insn (gen_sse_storelps (m, op1));
15723               m = adjust_address (op0, V2SFmode, 8);
15724               emit_insn (gen_sse_storehps (m, op1));
15725             }
15726         }
15727     }
15728   else
15729     gcc_unreachable ();
15730 }
15731
15732 /* Expand a push in MODE.  This is some mode for which we do not support
15733    proper push instructions, at least from the registers that we expect
15734    the value to live in.  */
15735
15736 void
15737 ix86_expand_push (enum machine_mode mode, rtx x)
15738 {
15739   rtx tmp;
15740
15741   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
15742                              GEN_INT (-GET_MODE_SIZE (mode)),
15743                              stack_pointer_rtx, 1, OPTAB_DIRECT);
15744   if (tmp != stack_pointer_rtx)
15745     emit_move_insn (stack_pointer_rtx, tmp);
15746
15747   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
15748
15749   /* When we push an operand onto stack, it has to be aligned at least
15750      at the function argument boundary.  However since we don't have
15751      the argument type, we can't determine the actual argument
15752      boundary.  */
15753   emit_move_insn (tmp, x);
15754 }
15755
15756 /* Helper function of ix86_fixup_binary_operands to canonicalize
15757    operand order.  Returns true if the operands should be swapped.  */
15758
15759 static bool
15760 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
15761                              rtx operands[])
15762 {
15763   rtx dst = operands[0];
15764   rtx src1 = operands[1];
15765   rtx src2 = operands[2];
15766
15767   /* If the operation is not commutative, we can't do anything.  */
15768   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
15769     return false;
15770
15771   /* Highest priority is that src1 should match dst.  */
15772   if (rtx_equal_p (dst, src1))
15773     return false;
15774   if (rtx_equal_p (dst, src2))
15775     return true;
15776
15777   /* Next highest priority is that immediate constants come second.  */
15778   if (immediate_operand (src2, mode))
15779     return false;
15780   if (immediate_operand (src1, mode))
15781     return true;
15782
15783   /* Lowest priority is that memory references should come second.  */
15784   if (MEM_P (src2))
15785     return false;
15786   if (MEM_P (src1))
15787     return true;
15788
15789   return false;
15790 }
15791
15792
15793 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
15794    destination to use for the operation.  If different from the true
15795    destination in operands[0], a copy operation will be required.  */
15796
15797 rtx
15798 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
15799                             rtx operands[])
15800 {
15801   rtx dst = operands[0];
15802   rtx src1 = operands[1];
15803   rtx src2 = operands[2];
15804
15805   /* Canonicalize operand order.  */
15806   if (ix86_swap_binary_operands_p (code, mode, operands))
15807     {
15808       rtx temp;
15809
15810       /* It is invalid to swap operands of different modes.  */
15811       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
15812
15813       temp = src1;
15814       src1 = src2;
15815       src2 = temp;
15816     }
15817
15818   /* Both source operands cannot be in memory.  */
15819   if (MEM_P (src1) && MEM_P (src2))
15820     {
15821       /* Optimization: Only read from memory once.  */
15822       if (rtx_equal_p (src1, src2))
15823         {
15824           src2 = force_reg (mode, src2);
15825           src1 = src2;
15826         }
15827       else
15828         src2 = force_reg (mode, src2);
15829     }
15830
15831   /* If the destination is memory, and we do not have matching source
15832      operands, do things in registers.  */
15833   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
15834     dst = gen_reg_rtx (mode);
15835
15836   /* Source 1 cannot be a constant.  */
15837   if (CONSTANT_P (src1))
15838     src1 = force_reg (mode, src1);
15839
15840   /* Source 1 cannot be a non-matching memory.  */
15841   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
15842     src1 = force_reg (mode, src1);
15843
15844   operands[1] = src1;
15845   operands[2] = src2;
15846   return dst;
15847 }
15848
15849 /* Similarly, but assume that the destination has already been
15850    set up properly.  */
15851
15852 void
15853 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
15854                                     enum machine_mode mode, rtx operands[])
15855 {
15856   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
15857   gcc_assert (dst == operands[0]);
15858 }
15859
15860 /* Attempt to expand a binary operator.  Make the expansion closer to the
15861    actual machine, then just general_operand, which will allow 3 separate
15862    memory references (one output, two input) in a single insn.  */
15863
15864 void
15865 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
15866                              rtx operands[])
15867 {
15868   rtx src1, src2, dst, op, clob;
15869
15870   dst = ix86_fixup_binary_operands (code, mode, operands);
15871   src1 = operands[1];
15872   src2 = operands[2];
15873
15874  /* Emit the instruction.  */
15875
15876   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
15877   if (reload_in_progress)
15878     {
15879       /* Reload doesn't know about the flags register, and doesn't know that
15880          it doesn't want to clobber it.  We can only do this with PLUS.  */
15881       gcc_assert (code == PLUS);
15882       emit_insn (op);
15883     }
15884   else if (reload_completed
15885            && code == PLUS
15886            && !rtx_equal_p (dst, src1))
15887     {
15888       /* This is going to be an LEA; avoid splitting it later.  */
15889       emit_insn (op);
15890     }
15891   else
15892     {
15893       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15894       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
15895     }
15896
15897   /* Fix up the destination if needed.  */
15898   if (dst != operands[0])
15899     emit_move_insn (operands[0], dst);
15900 }
15901
15902 /* Return TRUE or FALSE depending on whether the binary operator meets the
15903    appropriate constraints.  */
15904
15905 bool
15906 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
15907                          rtx operands[3])
15908 {
15909   rtx dst = operands[0];
15910   rtx src1 = operands[1];
15911   rtx src2 = operands[2];
15912
15913   /* Both source operands cannot be in memory.  */
15914   if (MEM_P (src1) && MEM_P (src2))
15915     return false;
15916
15917   /* Canonicalize operand order for commutative operators.  */
15918   if (ix86_swap_binary_operands_p (code, mode, operands))
15919     {
15920       rtx temp = src1;
15921       src1 = src2;
15922       src2 = temp;
15923     }
15924
15925   /* If the destination is memory, we must have a matching source operand.  */
15926   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
15927       return false;
15928
15929   /* Source 1 cannot be a constant.  */
15930   if (CONSTANT_P (src1))
15931     return false;
15932
15933   /* Source 1 cannot be a non-matching memory.  */
15934   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
15935     {
15936       /* Support "andhi/andsi/anddi" as a zero-extending move.  */
15937       return (code == AND
15938               && (mode == HImode
15939                   || mode == SImode
15940                   || (TARGET_64BIT && mode == DImode))
15941               && CONST_INT_P (src2)
15942               && (INTVAL (src2) == 0xff
15943                   || INTVAL (src2) == 0xffff));
15944     }
15945
15946   return true;
15947 }
15948
15949 /* Attempt to expand a unary operator.  Make the expansion closer to the
15950    actual machine, then just general_operand, which will allow 2 separate
15951    memory references (one output, one input) in a single insn.  */
15952
15953 void
15954 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
15955                             rtx operands[])
15956 {
15957   int matching_memory;
15958   rtx src, dst, op, clob;
15959
15960   dst = operands[0];
15961   src = operands[1];
15962
15963   /* If the destination is memory, and we do not have matching source
15964      operands, do things in registers.  */
15965   matching_memory = 0;
15966   if (MEM_P (dst))
15967     {
15968       if (rtx_equal_p (dst, src))
15969         matching_memory = 1;
15970       else
15971         dst = gen_reg_rtx (mode);
15972     }
15973
15974   /* When source operand is memory, destination must match.  */
15975   if (MEM_P (src) && !matching_memory)
15976     src = force_reg (mode, src);
15977
15978   /* Emit the instruction.  */
15979
15980   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
15981   if (reload_in_progress || code == NOT)
15982     {
15983       /* Reload doesn't know about the flags register, and doesn't know that
15984          it doesn't want to clobber it.  */
15985       gcc_assert (code == NOT);
15986       emit_insn (op);
15987     }
15988   else
15989     {
15990       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15991       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
15992     }
15993
15994   /* Fix up the destination if needed.  */
15995   if (dst != operands[0])
15996     emit_move_insn (operands[0], dst);
15997 }
15998
15999 /* Split 32bit/64bit divmod with 8bit unsigned divmod if dividend and
16000    divisor are within the the range [0-255].  */
16001
16002 void
16003 ix86_split_idivmod (enum machine_mode mode, rtx operands[],
16004                     bool signed_p)
16005 {
16006   rtx end_label, qimode_label;
16007   rtx insn, div, mod;
16008   rtx scratch, tmp0, tmp1, tmp2;
16009   rtx (*gen_divmod4_1) (rtx, rtx, rtx, rtx);
16010   rtx (*gen_zero_extend) (rtx, rtx);
16011   rtx (*gen_test_ccno_1) (rtx, rtx);
16012
16013   switch (mode)
16014     {
16015     case SImode:
16016       gen_divmod4_1 = signed_p ? gen_divmodsi4_1 : gen_udivmodsi4_1;
16017       gen_test_ccno_1 = gen_testsi_ccno_1;
16018       gen_zero_extend = gen_zero_extendqisi2;
16019       break;
16020     case DImode:
16021       gen_divmod4_1 = signed_p ? gen_divmoddi4_1 : gen_udivmoddi4_1;
16022       gen_test_ccno_1 = gen_testdi_ccno_1;
16023       gen_zero_extend = gen_zero_extendqidi2;
16024       break;
16025     default:
16026       gcc_unreachable ();
16027     }
16028
16029   end_label = gen_label_rtx ();
16030   qimode_label = gen_label_rtx ();
16031
16032   scratch = gen_reg_rtx (mode);
16033
16034   /* Use 8bit unsigned divimod if dividend and divisor are within the
16035      the range [0-255].  */
16036   emit_move_insn (scratch, operands[2]);
16037   scratch = expand_simple_binop (mode, IOR, scratch, operands[3],
16038                                  scratch, 1, OPTAB_DIRECT);
16039   emit_insn (gen_test_ccno_1 (scratch, GEN_INT (-0x100)));
16040   tmp0 = gen_rtx_REG (CCNOmode, FLAGS_REG);
16041   tmp0 = gen_rtx_EQ (VOIDmode, tmp0, const0_rtx);
16042   tmp0 = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp0,
16043                                gen_rtx_LABEL_REF (VOIDmode, qimode_label),
16044                                pc_rtx);
16045   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp0));
16046   predict_jump (REG_BR_PROB_BASE * 50 / 100);
16047   JUMP_LABEL (insn) = qimode_label;
16048
16049   /* Generate original signed/unsigned divimod.  */
16050   div = gen_divmod4_1 (operands[0], operands[1],
16051                        operands[2], operands[3]);
16052   emit_insn (div);
16053
16054   /* Branch to the end.  */
16055   emit_jump_insn (gen_jump (end_label));
16056   emit_barrier ();
16057
16058   /* Generate 8bit unsigned divide.  */
16059   emit_label (qimode_label);
16060   /* Don't use operands[0] for result of 8bit divide since not all
16061      registers support QImode ZERO_EXTRACT.  */
16062   tmp0 = simplify_gen_subreg (HImode, scratch, mode, 0);
16063   tmp1 = simplify_gen_subreg (HImode, operands[2], mode, 0);
16064   tmp2 = simplify_gen_subreg (QImode, operands[3], mode, 0);
16065   emit_insn (gen_udivmodhiqi3 (tmp0, tmp1, tmp2));
16066
16067   if (signed_p)
16068     {
16069       div = gen_rtx_DIV (SImode, operands[2], operands[3]);
16070       mod = gen_rtx_MOD (SImode, operands[2], operands[3]);
16071     }
16072   else
16073     {
16074       div = gen_rtx_UDIV (SImode, operands[2], operands[3]);
16075       mod = gen_rtx_UMOD (SImode, operands[2], operands[3]);
16076     }
16077
16078   /* Extract remainder from AH.  */
16079   tmp1 = gen_rtx_ZERO_EXTRACT (mode, tmp0, GEN_INT (8), GEN_INT (8));
16080   if (REG_P (operands[1]))
16081     insn = emit_move_insn (operands[1], tmp1);
16082   else
16083     {
16084       /* Need a new scratch register since the old one has result 
16085          of 8bit divide.  */
16086       scratch = gen_reg_rtx (mode);
16087       emit_move_insn (scratch, tmp1);
16088       insn = emit_move_insn (operands[1], scratch);
16089     }
16090   set_unique_reg_note (insn, REG_EQUAL, mod);
16091
16092   /* Zero extend quotient from AL.  */
16093   tmp1 = gen_lowpart (QImode, tmp0);
16094   insn = emit_insn (gen_zero_extend (operands[0], tmp1));
16095   set_unique_reg_note (insn, REG_EQUAL, div);
16096
16097   emit_label (end_label);
16098 }
16099
16100 #define LEA_SEARCH_THRESHOLD 12
16101
16102 /* Search backward for non-agu definition of register number REGNO1
16103    or register number REGNO2 in INSN's basic block until
16104    1. Pass LEA_SEARCH_THRESHOLD instructions, or
16105    2. Reach BB boundary, or
16106    3. Reach agu definition.
16107    Returns the distance between the non-agu definition point and INSN.
16108    If no definition point, returns -1.  */
16109
16110 static int
16111 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
16112                          rtx insn)
16113 {
16114   basic_block bb = BLOCK_FOR_INSN (insn);
16115   int distance = 0;
16116   df_ref *def_rec;
16117   enum attr_type insn_type;
16118
16119   if (insn != BB_HEAD (bb))
16120     {
16121       rtx prev = PREV_INSN (insn);
16122       while (prev && distance < LEA_SEARCH_THRESHOLD)
16123         {
16124           if (NONDEBUG_INSN_P (prev))
16125             {
16126               distance++;
16127               for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
16128                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
16129                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
16130                     && (regno1 == DF_REF_REGNO (*def_rec)
16131                         || regno2 == DF_REF_REGNO (*def_rec)))
16132                   {
16133                     insn_type = get_attr_type (prev);
16134                     if (insn_type != TYPE_LEA)
16135                       goto done;
16136                   }
16137             }
16138           if (prev == BB_HEAD (bb))
16139             break;
16140           prev = PREV_INSN (prev);
16141         }
16142     }
16143
16144   if (distance < LEA_SEARCH_THRESHOLD)
16145     {
16146       edge e;
16147       edge_iterator ei;
16148       bool simple_loop = false;
16149
16150       FOR_EACH_EDGE (e, ei, bb->preds)
16151         if (e->src == bb)
16152           {
16153             simple_loop = true;
16154             break;
16155           }
16156
16157       if (simple_loop)
16158         {
16159           rtx prev = BB_END (bb);
16160           while (prev
16161                  && prev != insn
16162                  && distance < LEA_SEARCH_THRESHOLD)
16163             {
16164               if (NONDEBUG_INSN_P (prev))
16165                 {
16166                   distance++;
16167                   for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
16168                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
16169                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
16170                         && (regno1 == DF_REF_REGNO (*def_rec)
16171                             || regno2 == DF_REF_REGNO (*def_rec)))
16172                       {
16173                         insn_type = get_attr_type (prev);
16174                         if (insn_type != TYPE_LEA)
16175                           goto done;
16176                       }
16177                 }
16178               prev = PREV_INSN (prev);
16179             }
16180         }
16181     }
16182
16183   distance = -1;
16184
16185 done:
16186   /* get_attr_type may modify recog data.  We want to make sure
16187      that recog data is valid for instruction INSN, on which
16188      distance_non_agu_define is called.  INSN is unchanged here.  */
16189   extract_insn_cached (insn);
16190   return distance;
16191 }
16192
16193 /* Return the distance between INSN and the next insn that uses
16194    register number REGNO0 in memory address.  Return -1 if no such
16195    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
16196
16197 static int
16198 distance_agu_use (unsigned int regno0, rtx insn)
16199 {
16200   basic_block bb = BLOCK_FOR_INSN (insn);
16201   int distance = 0;
16202   df_ref *def_rec;
16203   df_ref *use_rec;
16204
16205   if (insn != BB_END (bb))
16206     {
16207       rtx next = NEXT_INSN (insn);
16208       while (next && distance < LEA_SEARCH_THRESHOLD)
16209         {
16210           if (NONDEBUG_INSN_P (next))
16211             {
16212               distance++;
16213
16214               for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
16215                 if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
16216                      || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
16217                     && regno0 == DF_REF_REGNO (*use_rec))
16218                   {
16219                     /* Return DISTANCE if OP0 is used in memory
16220                        address in NEXT.  */
16221                     return distance;
16222                   }
16223
16224               for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
16225                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
16226                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
16227                     && regno0 == DF_REF_REGNO (*def_rec))
16228                   {
16229                     /* Return -1 if OP0 is set in NEXT.  */
16230                     return -1;
16231                   }
16232             }
16233           if (next == BB_END (bb))
16234             break;
16235           next = NEXT_INSN (next);
16236         }
16237     }
16238
16239   if (distance < LEA_SEARCH_THRESHOLD)
16240     {
16241       edge e;
16242       edge_iterator ei;
16243       bool simple_loop = false;
16244
16245       FOR_EACH_EDGE (e, ei, bb->succs)
16246         if (e->dest == bb)
16247           {
16248             simple_loop = true;
16249             break;
16250           }
16251
16252       if (simple_loop)
16253         {
16254           rtx next = BB_HEAD (bb);
16255           while (next
16256                  && next != insn
16257                  && distance < LEA_SEARCH_THRESHOLD)
16258             {
16259               if (NONDEBUG_INSN_P (next))
16260                 {
16261                   distance++;
16262
16263                   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
16264                     if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
16265                          || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
16266                         && regno0 == DF_REF_REGNO (*use_rec))
16267                       {
16268                         /* Return DISTANCE if OP0 is used in memory
16269                            address in NEXT.  */
16270                         return distance;
16271                       }
16272
16273                   for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
16274                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
16275                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
16276                         && regno0 == DF_REF_REGNO (*def_rec))
16277                       {
16278                         /* Return -1 if OP0 is set in NEXT.  */
16279                         return -1;
16280                       }
16281
16282                 }
16283               next = NEXT_INSN (next);
16284             }
16285         }
16286     }
16287
16288   return -1;
16289 }
16290
16291 /* Define this macro to tune LEA priority vs ADD, it take effect when
16292    there is a dilemma of choicing LEA or ADD
16293    Negative value: ADD is more preferred than LEA
16294    Zero: Netrual
16295    Positive value: LEA is more preferred than ADD*/
16296 #define IX86_LEA_PRIORITY 2
16297
16298 /* Return true if it is ok to optimize an ADD operation to LEA
16299    operation to avoid flag register consumation.  For most processors,
16300    ADD is faster than LEA.  For the processors like ATOM, if the
16301    destination register of LEA holds an actual address which will be
16302    used soon, LEA is better and otherwise ADD is better.  */
16303
16304 bool
16305 ix86_lea_for_add_ok (rtx insn, rtx operands[])
16306 {
16307   unsigned int regno0 = true_regnum (operands[0]);
16308   unsigned int regno1 = true_regnum (operands[1]);
16309   unsigned int regno2 = true_regnum (operands[2]);
16310
16311   /* If a = b + c, (a!=b && a!=c), must use lea form. */
16312   if (regno0 != regno1 && regno0 != regno2)
16313     return true;
16314
16315   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16316     return false;
16317   else
16318     {
16319       int dist_define, dist_use;
16320
16321       /* Return false if REGNO0 isn't used in memory address. */
16322       dist_use = distance_agu_use (regno0, insn);
16323       if (dist_use <= 0)
16324         return false;
16325
16326       dist_define = distance_non_agu_define (regno1, regno2, insn);
16327       if (dist_define <= 0)
16328         return true;
16329
16330       /* If this insn has both backward non-agu dependence and forward
16331          agu dependence, the one with short distance take effect. */
16332       if ((dist_define + IX86_LEA_PRIORITY) < dist_use)
16333         return false;
16334
16335       return true;
16336     }
16337 }
16338
16339 /* Return true if destination reg of SET_BODY is shift count of
16340    USE_BODY.  */
16341
16342 static bool
16343 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
16344 {
16345   rtx set_dest;
16346   rtx shift_rtx;
16347   int i;
16348
16349   /* Retrieve destination of SET_BODY.  */
16350   switch (GET_CODE (set_body))
16351     {
16352     case SET:
16353       set_dest = SET_DEST (set_body);
16354       if (!set_dest || !REG_P (set_dest))
16355         return false;
16356       break;
16357     case PARALLEL:
16358       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
16359         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
16360                                           use_body))
16361           return true;
16362     default:
16363       return false;
16364       break;
16365     }
16366
16367   /* Retrieve shift count of USE_BODY.  */
16368   switch (GET_CODE (use_body))
16369     {
16370     case SET:
16371       shift_rtx = XEXP (use_body, 1);
16372       break;
16373     case PARALLEL:
16374       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
16375         if (ix86_dep_by_shift_count_body (set_body,
16376                                           XVECEXP (use_body, 0, i)))
16377           return true;
16378     default:
16379       return false;
16380       break;
16381     }
16382
16383   if (shift_rtx
16384       && (GET_CODE (shift_rtx) == ASHIFT
16385           || GET_CODE (shift_rtx) == LSHIFTRT
16386           || GET_CODE (shift_rtx) == ASHIFTRT
16387           || GET_CODE (shift_rtx) == ROTATE
16388           || GET_CODE (shift_rtx) == ROTATERT))
16389     {
16390       rtx shift_count = XEXP (shift_rtx, 1);
16391
16392       /* Return true if shift count is dest of SET_BODY.  */
16393       if (REG_P (shift_count)
16394           && true_regnum (set_dest) == true_regnum (shift_count))
16395         return true;
16396     }
16397
16398   return false;
16399 }
16400
16401 /* Return true if destination reg of SET_INSN is shift count of
16402    USE_INSN.  */
16403
16404 bool
16405 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
16406 {
16407   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
16408                                        PATTERN (use_insn));
16409 }
16410
16411 /* Return TRUE or FALSE depending on whether the unary operator meets the
16412    appropriate constraints.  */
16413
16414 bool
16415 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
16416                         enum machine_mode mode ATTRIBUTE_UNUSED,
16417                         rtx operands[2] ATTRIBUTE_UNUSED)
16418 {
16419   /* If one of operands is memory, source and destination must match.  */
16420   if ((MEM_P (operands[0])
16421        || MEM_P (operands[1]))
16422       && ! rtx_equal_p (operands[0], operands[1]))
16423     return false;
16424   return true;
16425 }
16426
16427 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
16428    are ok, keeping in mind the possible movddup alternative.  */
16429
16430 bool
16431 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
16432 {
16433   if (MEM_P (operands[0]))
16434     return rtx_equal_p (operands[0], operands[1 + high]);
16435   if (MEM_P (operands[1]) && MEM_P (operands[2]))
16436     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
16437   return true;
16438 }
16439
16440 /* Post-reload splitter for converting an SF or DFmode value in an
16441    SSE register into an unsigned SImode.  */
16442
16443 void
16444 ix86_split_convert_uns_si_sse (rtx operands[])
16445 {
16446   enum machine_mode vecmode;
16447   rtx value, large, zero_or_two31, input, two31, x;
16448
16449   large = operands[1];
16450   zero_or_two31 = operands[2];
16451   input = operands[3];
16452   two31 = operands[4];
16453   vecmode = GET_MODE (large);
16454   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
16455
16456   /* Load up the value into the low element.  We must ensure that the other
16457      elements are valid floats -- zero is the easiest such value.  */
16458   if (MEM_P (input))
16459     {
16460       if (vecmode == V4SFmode)
16461         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
16462       else
16463         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
16464     }
16465   else
16466     {
16467       input = gen_rtx_REG (vecmode, REGNO (input));
16468       emit_move_insn (value, CONST0_RTX (vecmode));
16469       if (vecmode == V4SFmode)
16470         emit_insn (gen_sse_movss (value, value, input));
16471       else
16472         emit_insn (gen_sse2_movsd (value, value, input));
16473     }
16474
16475   emit_move_insn (large, two31);
16476   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
16477
16478   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
16479   emit_insn (gen_rtx_SET (VOIDmode, large, x));
16480
16481   x = gen_rtx_AND (vecmode, zero_or_two31, large);
16482   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
16483
16484   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
16485   emit_insn (gen_rtx_SET (VOIDmode, value, x));
16486
16487   large = gen_rtx_REG (V4SImode, REGNO (large));
16488   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
16489
16490   x = gen_rtx_REG (V4SImode, REGNO (value));
16491   if (vecmode == V4SFmode)
16492     emit_insn (gen_sse2_cvttps2dq (x, value));
16493   else
16494     emit_insn (gen_sse2_cvttpd2dq (x, value));
16495   value = x;
16496
16497   emit_insn (gen_xorv4si3 (value, value, large));
16498 }
16499
16500 /* Convert an unsigned DImode value into a DFmode, using only SSE.
16501    Expects the 64-bit DImode to be supplied in a pair of integral
16502    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
16503    -mfpmath=sse, !optimize_size only.  */
16504
16505 void
16506 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
16507 {
16508   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
16509   rtx int_xmm, fp_xmm;
16510   rtx biases, exponents;
16511   rtx x;
16512
16513   int_xmm = gen_reg_rtx (V4SImode);
16514   if (TARGET_INTER_UNIT_MOVES)
16515     emit_insn (gen_movdi_to_sse (int_xmm, input));
16516   else if (TARGET_SSE_SPLIT_REGS)
16517     {
16518       emit_clobber (int_xmm);
16519       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
16520     }
16521   else
16522     {
16523       x = gen_reg_rtx (V2DImode);
16524       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
16525       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
16526     }
16527
16528   x = gen_rtx_CONST_VECTOR (V4SImode,
16529                             gen_rtvec (4, GEN_INT (0x43300000UL),
16530                                        GEN_INT (0x45300000UL),
16531                                        const0_rtx, const0_rtx));
16532   exponents = validize_mem (force_const_mem (V4SImode, x));
16533
16534   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
16535   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
16536
16537   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
16538      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
16539      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
16540      (0x1.0p84 + double(fp_value_hi_xmm)).
16541      Note these exponents differ by 32.  */
16542
16543   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
16544
16545   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
16546      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
16547   real_ldexp (&bias_lo_rvt, &dconst1, 52);
16548   real_ldexp (&bias_hi_rvt, &dconst1, 84);
16549   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
16550   x = const_double_from_real_value (bias_hi_rvt, DFmode);
16551   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
16552   biases = validize_mem (force_const_mem (V2DFmode, biases));
16553   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
16554
16555   /* Add the upper and lower DFmode values together.  */
16556   if (TARGET_SSE3)
16557     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
16558   else
16559     {
16560       x = copy_to_mode_reg (V2DFmode, fp_xmm);
16561       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
16562       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
16563     }
16564
16565   ix86_expand_vector_extract (false, target, fp_xmm, 0);
16566 }
16567
16568 /* Not used, but eases macroization of patterns.  */
16569 void
16570 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
16571                                   rtx input ATTRIBUTE_UNUSED)
16572 {
16573   gcc_unreachable ();
16574 }
16575
16576 /* Convert an unsigned SImode value into a DFmode.  Only currently used
16577    for SSE, but applicable anywhere.  */
16578
16579 void
16580 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
16581 {
16582   REAL_VALUE_TYPE TWO31r;
16583   rtx x, fp;
16584
16585   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
16586                            NULL, 1, OPTAB_DIRECT);
16587
16588   fp = gen_reg_rtx (DFmode);
16589   emit_insn (gen_floatsidf2 (fp, x));
16590
16591   real_ldexp (&TWO31r, &dconst1, 31);
16592   x = const_double_from_real_value (TWO31r, DFmode);
16593
16594   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
16595   if (x != target)
16596     emit_move_insn (target, x);
16597 }
16598
16599 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
16600    32-bit mode; otherwise we have a direct convert instruction.  */
16601
16602 void
16603 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
16604 {
16605   REAL_VALUE_TYPE TWO32r;
16606   rtx fp_lo, fp_hi, x;
16607
16608   fp_lo = gen_reg_rtx (DFmode);
16609   fp_hi = gen_reg_rtx (DFmode);
16610
16611   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
16612
16613   real_ldexp (&TWO32r, &dconst1, 32);
16614   x = const_double_from_real_value (TWO32r, DFmode);
16615   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
16616
16617   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
16618
16619   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
16620                            0, OPTAB_DIRECT);
16621   if (x != target)
16622     emit_move_insn (target, x);
16623 }
16624
16625 /* Convert an unsigned SImode value into a SFmode, using only SSE.
16626    For x86_32, -mfpmath=sse, !optimize_size only.  */
16627 void
16628 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
16629 {
16630   REAL_VALUE_TYPE ONE16r;
16631   rtx fp_hi, fp_lo, int_hi, int_lo, x;
16632
16633   real_ldexp (&ONE16r, &dconst1, 16);
16634   x = const_double_from_real_value (ONE16r, SFmode);
16635   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
16636                                       NULL, 0, OPTAB_DIRECT);
16637   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
16638                                       NULL, 0, OPTAB_DIRECT);
16639   fp_hi = gen_reg_rtx (SFmode);
16640   fp_lo = gen_reg_rtx (SFmode);
16641   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
16642   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
16643   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
16644                                0, OPTAB_DIRECT);
16645   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
16646                                0, OPTAB_DIRECT);
16647   if (!rtx_equal_p (target, fp_hi))
16648     emit_move_insn (target, fp_hi);
16649 }
16650
16651 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
16652    then replicate the value for all elements of the vector
16653    register.  */
16654
16655 rtx
16656 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
16657 {
16658   rtvec v;
16659   switch (mode)
16660     {
16661     case V4SImode:
16662       gcc_assert (vect);
16663       v = gen_rtvec (4, value, value, value, value);
16664       return gen_rtx_CONST_VECTOR (V4SImode, v);
16665
16666     case V2DImode:
16667       gcc_assert (vect);
16668       v = gen_rtvec (2, value, value);
16669       return gen_rtx_CONST_VECTOR (V2DImode, v);
16670
16671     case V8SFmode:
16672       if (vect)
16673         v = gen_rtvec (8, value, value, value, value,
16674                        value, value, value, value);
16675       else
16676         v = gen_rtvec (8, value, CONST0_RTX (SFmode),
16677                        CONST0_RTX (SFmode), CONST0_RTX (SFmode),
16678                        CONST0_RTX (SFmode), CONST0_RTX (SFmode),
16679                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
16680       return gen_rtx_CONST_VECTOR (V8SFmode, v);
16681
16682     case V4SFmode:
16683       if (vect)
16684         v = gen_rtvec (4, value, value, value, value);
16685       else
16686         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
16687                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
16688       return gen_rtx_CONST_VECTOR (V4SFmode, v);
16689
16690     case V4DFmode:
16691       if (vect)
16692         v = gen_rtvec (4, value, value, value, value);
16693       else
16694         v = gen_rtvec (4, value, CONST0_RTX (DFmode),
16695                        CONST0_RTX (DFmode), CONST0_RTX (DFmode));
16696       return gen_rtx_CONST_VECTOR (V4DFmode, v);
16697
16698     case V2DFmode:
16699       if (vect)
16700         v = gen_rtvec (2, value, value);
16701       else
16702         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
16703       return gen_rtx_CONST_VECTOR (V2DFmode, v);
16704
16705     default:
16706       gcc_unreachable ();
16707     }
16708 }
16709
16710 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
16711    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
16712    for an SSE register.  If VECT is true, then replicate the mask for
16713    all elements of the vector register.  If INVERT is true, then create
16714    a mask excluding the sign bit.  */
16715
16716 rtx
16717 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
16718 {
16719   enum machine_mode vec_mode, imode;
16720   HOST_WIDE_INT hi, lo;
16721   int shift = 63;
16722   rtx v;
16723   rtx mask;
16724
16725   /* Find the sign bit, sign extended to 2*HWI.  */
16726   switch (mode)
16727     {
16728     case V4SImode:
16729     case V8SFmode:
16730     case V4SFmode:
16731       vec_mode = mode;
16732       mode = GET_MODE_INNER (mode);
16733       imode = SImode;
16734       lo = 0x80000000, hi = lo < 0;
16735       break;
16736
16737     case V2DImode:
16738     case V4DFmode:
16739     case V2DFmode:
16740       vec_mode = mode;
16741       mode = GET_MODE_INNER (mode);
16742       imode = DImode;
16743       if (HOST_BITS_PER_WIDE_INT >= 64)
16744         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
16745       else
16746         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
16747       break;
16748
16749     case TImode:
16750     case TFmode:
16751       vec_mode = VOIDmode;
16752       if (HOST_BITS_PER_WIDE_INT >= 64)
16753         {
16754           imode = TImode;
16755           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
16756         }
16757       else
16758         {
16759           rtvec vec;
16760
16761           imode = DImode;
16762           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
16763
16764           if (invert)
16765             {
16766               lo = ~lo, hi = ~hi;
16767               v = constm1_rtx;
16768             }
16769           else
16770             v = const0_rtx;
16771
16772           mask = immed_double_const (lo, hi, imode);
16773
16774           vec = gen_rtvec (2, v, mask);
16775           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
16776           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
16777
16778           return v;
16779         }
16780      break;
16781
16782     default:
16783       gcc_unreachable ();
16784     }
16785
16786   if (invert)
16787     lo = ~lo, hi = ~hi;
16788
16789   /* Force this value into the low part of a fp vector constant.  */
16790   mask = immed_double_const (lo, hi, imode);
16791   mask = gen_lowpart (mode, mask);
16792
16793   if (vec_mode == VOIDmode)
16794     return force_reg (mode, mask);
16795
16796   v = ix86_build_const_vector (vec_mode, vect, mask);
16797   return force_reg (vec_mode, v);
16798 }
16799
16800 /* Generate code for floating point ABS or NEG.  */
16801
16802 void
16803 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
16804                                 rtx operands[])
16805 {
16806   rtx mask, set, dst, src;
16807   bool use_sse = false;
16808   bool vector_mode = VECTOR_MODE_P (mode);
16809   enum machine_mode vmode = mode;
16810
16811   if (vector_mode)
16812     use_sse = true;
16813   else if (mode == TFmode)
16814     use_sse = true;
16815   else if (TARGET_SSE_MATH)
16816     {
16817       use_sse = SSE_FLOAT_MODE_P (mode);
16818       if (mode == SFmode)
16819         vmode = V4SFmode;
16820       else if (mode == DFmode)
16821         vmode = V2DFmode;
16822     }
16823
16824   /* NEG and ABS performed with SSE use bitwise mask operations.
16825      Create the appropriate mask now.  */
16826   if (use_sse)
16827     mask = ix86_build_signbit_mask (vmode, vector_mode, code == ABS);
16828   else
16829     mask = NULL_RTX;
16830
16831   dst = operands[0];
16832   src = operands[1];
16833
16834   set = gen_rtx_fmt_e (code, mode, src);
16835   set = gen_rtx_SET (VOIDmode, dst, set);
16836
16837   if (mask)
16838     {
16839       rtx use, clob;
16840       rtvec par;
16841
16842       use = gen_rtx_USE (VOIDmode, mask);
16843       if (vector_mode)
16844         par = gen_rtvec (2, set, use);
16845       else
16846         {
16847           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16848           par = gen_rtvec (3, set, use, clob);
16849         }
16850       emit_insn (gen_rtx_PARALLEL (VOIDmode, par));
16851     }
16852   else
16853     emit_insn (set);
16854 }
16855
16856 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
16857
16858 void
16859 ix86_expand_copysign (rtx operands[])
16860 {
16861   enum machine_mode mode, vmode;
16862   rtx dest, op0, op1, mask, nmask;
16863
16864   dest = operands[0];
16865   op0 = operands[1];
16866   op1 = operands[2];
16867
16868   mode = GET_MODE (dest);
16869
16870   if (mode == SFmode)
16871     vmode = V4SFmode;
16872   else if (mode == DFmode)
16873     vmode = V2DFmode;
16874   else
16875     vmode = mode;
16876
16877   if (GET_CODE (op0) == CONST_DOUBLE)
16878     {
16879       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
16880
16881       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
16882         op0 = simplify_unary_operation (ABS, mode, op0, mode);
16883
16884       if (mode == SFmode || mode == DFmode)
16885         {
16886           if (op0 == CONST0_RTX (mode))
16887             op0 = CONST0_RTX (vmode);
16888           else
16889             {
16890               rtx v = ix86_build_const_vector (vmode, false, op0);
16891
16892               op0 = force_reg (vmode, v);
16893             }
16894         }
16895       else if (op0 != CONST0_RTX (mode))
16896         op0 = force_reg (mode, op0);
16897
16898       mask = ix86_build_signbit_mask (vmode, 0, 0);
16899
16900       if (mode == SFmode)
16901         copysign_insn = gen_copysignsf3_const;
16902       else if (mode == DFmode)
16903         copysign_insn = gen_copysigndf3_const;
16904       else
16905         copysign_insn = gen_copysigntf3_const;
16906
16907         emit_insn (copysign_insn (dest, op0, op1, mask));
16908     }
16909   else
16910     {
16911       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
16912
16913       nmask = ix86_build_signbit_mask (vmode, 0, 1);
16914       mask = ix86_build_signbit_mask (vmode, 0, 0);
16915
16916       if (mode == SFmode)
16917         copysign_insn = gen_copysignsf3_var;
16918       else if (mode == DFmode)
16919         copysign_insn = gen_copysigndf3_var;
16920       else
16921         copysign_insn = gen_copysigntf3_var;
16922
16923       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
16924     }
16925 }
16926
16927 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
16928    be a constant, and so has already been expanded into a vector constant.  */
16929
16930 void
16931 ix86_split_copysign_const (rtx operands[])
16932 {
16933   enum machine_mode mode, vmode;
16934   rtx dest, op0, mask, x;
16935
16936   dest = operands[0];
16937   op0 = operands[1];
16938   mask = operands[3];
16939
16940   mode = GET_MODE (dest);
16941   vmode = GET_MODE (mask);
16942
16943   dest = simplify_gen_subreg (vmode, dest, mode, 0);
16944   x = gen_rtx_AND (vmode, dest, mask);
16945   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16946
16947   if (op0 != CONST0_RTX (vmode))
16948     {
16949       x = gen_rtx_IOR (vmode, dest, op0);
16950       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16951     }
16952 }
16953
16954 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
16955    so we have to do two masks.  */
16956
16957 void
16958 ix86_split_copysign_var (rtx operands[])
16959 {
16960   enum machine_mode mode, vmode;
16961   rtx dest, scratch, op0, op1, mask, nmask, x;
16962
16963   dest = operands[0];
16964   scratch = operands[1];
16965   op0 = operands[2];
16966   op1 = operands[3];
16967   nmask = operands[4];
16968   mask = operands[5];
16969
16970   mode = GET_MODE (dest);
16971   vmode = GET_MODE (mask);
16972
16973   if (rtx_equal_p (op0, op1))
16974     {
16975       /* Shouldn't happen often (it's useless, obviously), but when it does
16976          we'd generate incorrect code if we continue below.  */
16977       emit_move_insn (dest, op0);
16978       return;
16979     }
16980
16981   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
16982     {
16983       gcc_assert (REGNO (op1) == REGNO (scratch));
16984
16985       x = gen_rtx_AND (vmode, scratch, mask);
16986       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
16987
16988       dest = mask;
16989       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
16990       x = gen_rtx_NOT (vmode, dest);
16991       x = gen_rtx_AND (vmode, x, op0);
16992       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16993     }
16994   else
16995     {
16996       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
16997         {
16998           x = gen_rtx_AND (vmode, scratch, mask);
16999         }
17000       else                                              /* alternative 2,4 */
17001         {
17002           gcc_assert (REGNO (mask) == REGNO (scratch));
17003           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
17004           x = gen_rtx_AND (vmode, scratch, op1);
17005         }
17006       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
17007
17008       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
17009         {
17010           dest = simplify_gen_subreg (vmode, op0, mode, 0);
17011           x = gen_rtx_AND (vmode, dest, nmask);
17012         }
17013       else                                              /* alternative 3,4 */
17014         {
17015           gcc_assert (REGNO (nmask) == REGNO (dest));
17016           dest = nmask;
17017           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
17018           x = gen_rtx_AND (vmode, dest, op0);
17019         }
17020       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17021     }
17022
17023   x = gen_rtx_IOR (vmode, dest, scratch);
17024   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17025 }
17026
17027 /* Return TRUE or FALSE depending on whether the first SET in INSN
17028    has source and destination with matching CC modes, and that the
17029    CC mode is at least as constrained as REQ_MODE.  */
17030
17031 bool
17032 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
17033 {
17034   rtx set;
17035   enum machine_mode set_mode;
17036
17037   set = PATTERN (insn);
17038   if (GET_CODE (set) == PARALLEL)
17039     set = XVECEXP (set, 0, 0);
17040   gcc_assert (GET_CODE (set) == SET);
17041   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
17042
17043   set_mode = GET_MODE (SET_DEST (set));
17044   switch (set_mode)
17045     {
17046     case CCNOmode:
17047       if (req_mode != CCNOmode
17048           && (req_mode != CCmode
17049               || XEXP (SET_SRC (set), 1) != const0_rtx))
17050         return false;
17051       break;
17052     case CCmode:
17053       if (req_mode == CCGCmode)
17054         return false;
17055       /* FALLTHRU */
17056     case CCGCmode:
17057       if (req_mode == CCGOCmode || req_mode == CCNOmode)
17058         return false;
17059       /* FALLTHRU */
17060     case CCGOCmode:
17061       if (req_mode == CCZmode)
17062         return false;
17063       /* FALLTHRU */
17064     case CCAmode:
17065     case CCCmode:
17066     case CCOmode:
17067     case CCSmode:
17068     case CCZmode:
17069       break;
17070
17071     default:
17072       gcc_unreachable ();
17073     }
17074
17075   return GET_MODE (SET_SRC (set)) == set_mode;
17076 }
17077
17078 /* Generate insn patterns to do an integer compare of OPERANDS.  */
17079
17080 static rtx
17081 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
17082 {
17083   enum machine_mode cmpmode;
17084   rtx tmp, flags;
17085
17086   cmpmode = SELECT_CC_MODE (code, op0, op1);
17087   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
17088
17089   /* This is very simple, but making the interface the same as in the
17090      FP case makes the rest of the code easier.  */
17091   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
17092   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
17093
17094   /* Return the test that should be put into the flags user, i.e.
17095      the bcc, scc, or cmov instruction.  */
17096   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
17097 }
17098
17099 /* Figure out whether to use ordered or unordered fp comparisons.
17100    Return the appropriate mode to use.  */
17101
17102 enum machine_mode
17103 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
17104 {
17105   /* ??? In order to make all comparisons reversible, we do all comparisons
17106      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
17107      all forms trapping and nontrapping comparisons, we can make inequality
17108      comparisons trapping again, since it results in better code when using
17109      FCOM based compares.  */
17110   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
17111 }
17112
17113 enum machine_mode
17114 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
17115 {
17116   enum machine_mode mode = GET_MODE (op0);
17117
17118   if (SCALAR_FLOAT_MODE_P (mode))
17119     {
17120       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
17121       return ix86_fp_compare_mode (code);
17122     }
17123
17124   switch (code)
17125     {
17126       /* Only zero flag is needed.  */
17127     case EQ:                    /* ZF=0 */
17128     case NE:                    /* ZF!=0 */
17129       return CCZmode;
17130       /* Codes needing carry flag.  */
17131     case GEU:                   /* CF=0 */
17132     case LTU:                   /* CF=1 */
17133       /* Detect overflow checks.  They need just the carry flag.  */
17134       if (GET_CODE (op0) == PLUS
17135           && rtx_equal_p (op1, XEXP (op0, 0)))
17136         return CCCmode;
17137       else
17138         return CCmode;
17139     case GTU:                   /* CF=0 & ZF=0 */
17140     case LEU:                   /* CF=1 | ZF=1 */
17141       /* Detect overflow checks.  They need just the carry flag.  */
17142       if (GET_CODE (op0) == MINUS
17143           && rtx_equal_p (op1, XEXP (op0, 0)))
17144         return CCCmode;
17145       else
17146         return CCmode;
17147       /* Codes possibly doable only with sign flag when
17148          comparing against zero.  */
17149     case GE:                    /* SF=OF   or   SF=0 */
17150     case LT:                    /* SF<>OF  or   SF=1 */
17151       if (op1 == const0_rtx)
17152         return CCGOCmode;
17153       else
17154         /* For other cases Carry flag is not required.  */
17155         return CCGCmode;
17156       /* Codes doable only with sign flag when comparing
17157          against zero, but we miss jump instruction for it
17158          so we need to use relational tests against overflow
17159          that thus needs to be zero.  */
17160     case GT:                    /* ZF=0 & SF=OF */
17161     case LE:                    /* ZF=1 | SF<>OF */
17162       if (op1 == const0_rtx)
17163         return CCNOmode;
17164       else
17165         return CCGCmode;
17166       /* strcmp pattern do (use flags) and combine may ask us for proper
17167          mode.  */
17168     case USE:
17169       return CCmode;
17170     default:
17171       gcc_unreachable ();
17172     }
17173 }
17174
17175 /* Return the fixed registers used for condition codes.  */
17176
17177 static bool
17178 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
17179 {
17180   *p1 = FLAGS_REG;
17181   *p2 = FPSR_REG;
17182   return true;
17183 }
17184
17185 /* If two condition code modes are compatible, return a condition code
17186    mode which is compatible with both.  Otherwise, return
17187    VOIDmode.  */
17188
17189 static enum machine_mode
17190 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
17191 {
17192   if (m1 == m2)
17193     return m1;
17194
17195   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
17196     return VOIDmode;
17197
17198   if ((m1 == CCGCmode && m2 == CCGOCmode)
17199       || (m1 == CCGOCmode && m2 == CCGCmode))
17200     return CCGCmode;
17201
17202   switch (m1)
17203     {
17204     default:
17205       gcc_unreachable ();
17206
17207     case CCmode:
17208     case CCGCmode:
17209     case CCGOCmode:
17210     case CCNOmode:
17211     case CCAmode:
17212     case CCCmode:
17213     case CCOmode:
17214     case CCSmode:
17215     case CCZmode:
17216       switch (m2)
17217         {
17218         default:
17219           return VOIDmode;
17220
17221         case CCmode:
17222         case CCGCmode:
17223         case CCGOCmode:
17224         case CCNOmode:
17225         case CCAmode:
17226         case CCCmode:
17227         case CCOmode:
17228         case CCSmode:
17229         case CCZmode:
17230           return CCmode;
17231         }
17232
17233     case CCFPmode:
17234     case CCFPUmode:
17235       /* These are only compatible with themselves, which we already
17236          checked above.  */
17237       return VOIDmode;
17238     }
17239 }
17240
17241
17242 /* Return a comparison we can do and that it is equivalent to
17243    swap_condition (code) apart possibly from orderedness.
17244    But, never change orderedness if TARGET_IEEE_FP, returning
17245    UNKNOWN in that case if necessary.  */
17246
17247 static enum rtx_code
17248 ix86_fp_swap_condition (enum rtx_code code)
17249 {
17250   switch (code)
17251     {
17252     case GT:                   /* GTU - CF=0 & ZF=0 */
17253       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
17254     case GE:                   /* GEU - CF=0 */
17255       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
17256     case UNLT:                 /* LTU - CF=1 */
17257       return TARGET_IEEE_FP ? UNKNOWN : GT;
17258     case UNLE:                 /* LEU - CF=1 | ZF=1 */
17259       return TARGET_IEEE_FP ? UNKNOWN : GE;
17260     default:
17261       return swap_condition (code);
17262     }
17263 }
17264
17265 /* Return cost of comparison CODE using the best strategy for performance.
17266    All following functions do use number of instructions as a cost metrics.
17267    In future this should be tweaked to compute bytes for optimize_size and
17268    take into account performance of various instructions on various CPUs.  */
17269
17270 static int
17271 ix86_fp_comparison_cost (enum rtx_code code)
17272 {
17273   int arith_cost;
17274
17275   /* The cost of code using bit-twiddling on %ah.  */
17276   switch (code)
17277     {
17278     case UNLE:
17279     case UNLT:
17280     case LTGT:
17281     case GT:
17282     case GE:
17283     case UNORDERED:
17284     case ORDERED:
17285     case UNEQ:
17286       arith_cost = 4;
17287       break;
17288     case LT:
17289     case NE:
17290     case EQ:
17291     case UNGE:
17292       arith_cost = TARGET_IEEE_FP ? 5 : 4;
17293       break;
17294     case LE:
17295     case UNGT:
17296       arith_cost = TARGET_IEEE_FP ? 6 : 4;
17297       break;
17298     default:
17299       gcc_unreachable ();
17300     }
17301
17302   switch (ix86_fp_comparison_strategy (code))
17303     {
17304     case IX86_FPCMP_COMI:
17305       return arith_cost > 4 ? 3 : 2;
17306     case IX86_FPCMP_SAHF:
17307       return arith_cost > 4 ? 4 : 3;
17308     default:
17309       return arith_cost;
17310     }
17311 }
17312
17313 /* Return strategy to use for floating-point.  We assume that fcomi is always
17314    preferrable where available, since that is also true when looking at size
17315    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
17316
17317 enum ix86_fpcmp_strategy
17318 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
17319 {
17320   /* Do fcomi/sahf based test when profitable.  */
17321
17322   if (TARGET_CMOVE)
17323     return IX86_FPCMP_COMI;
17324
17325   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
17326     return IX86_FPCMP_SAHF;
17327
17328   return IX86_FPCMP_ARITH;
17329 }
17330
17331 /* Swap, force into registers, or otherwise massage the two operands
17332    to a fp comparison.  The operands are updated in place; the new
17333    comparison code is returned.  */
17334
17335 static enum rtx_code
17336 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
17337 {
17338   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
17339   rtx op0 = *pop0, op1 = *pop1;
17340   enum machine_mode op_mode = GET_MODE (op0);
17341   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
17342
17343   /* All of the unordered compare instructions only work on registers.
17344      The same is true of the fcomi compare instructions.  The XFmode
17345      compare instructions require registers except when comparing
17346      against zero or when converting operand 1 from fixed point to
17347      floating point.  */
17348
17349   if (!is_sse
17350       && (fpcmp_mode == CCFPUmode
17351           || (op_mode == XFmode
17352               && ! (standard_80387_constant_p (op0) == 1
17353                     || standard_80387_constant_p (op1) == 1)
17354               && GET_CODE (op1) != FLOAT)
17355           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
17356     {
17357       op0 = force_reg (op_mode, op0);
17358       op1 = force_reg (op_mode, op1);
17359     }
17360   else
17361     {
17362       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
17363          things around if they appear profitable, otherwise force op0
17364          into a register.  */
17365
17366       if (standard_80387_constant_p (op0) == 0
17367           || (MEM_P (op0)
17368               && ! (standard_80387_constant_p (op1) == 0
17369                     || MEM_P (op1))))
17370         {
17371           enum rtx_code new_code = ix86_fp_swap_condition (code);
17372           if (new_code != UNKNOWN)
17373             {
17374               rtx tmp;
17375               tmp = op0, op0 = op1, op1 = tmp;
17376               code = new_code;
17377             }
17378         }
17379
17380       if (!REG_P (op0))
17381         op0 = force_reg (op_mode, op0);
17382
17383       if (CONSTANT_P (op1))
17384         {
17385           int tmp = standard_80387_constant_p (op1);
17386           if (tmp == 0)
17387             op1 = validize_mem (force_const_mem (op_mode, op1));
17388           else if (tmp == 1)
17389             {
17390               if (TARGET_CMOVE)
17391                 op1 = force_reg (op_mode, op1);
17392             }
17393           else
17394             op1 = force_reg (op_mode, op1);
17395         }
17396     }
17397
17398   /* Try to rearrange the comparison to make it cheaper.  */
17399   if (ix86_fp_comparison_cost (code)
17400       > ix86_fp_comparison_cost (swap_condition (code))
17401       && (REG_P (op1) || can_create_pseudo_p ()))
17402     {
17403       rtx tmp;
17404       tmp = op0, op0 = op1, op1 = tmp;
17405       code = swap_condition (code);
17406       if (!REG_P (op0))
17407         op0 = force_reg (op_mode, op0);
17408     }
17409
17410   *pop0 = op0;
17411   *pop1 = op1;
17412   return code;
17413 }
17414
17415 /* Convert comparison codes we use to represent FP comparison to integer
17416    code that will result in proper branch.  Return UNKNOWN if no such code
17417    is available.  */
17418
17419 enum rtx_code
17420 ix86_fp_compare_code_to_integer (enum rtx_code code)
17421 {
17422   switch (code)
17423     {
17424     case GT:
17425       return GTU;
17426     case GE:
17427       return GEU;
17428     case ORDERED:
17429     case UNORDERED:
17430       return code;
17431       break;
17432     case UNEQ:
17433       return EQ;
17434       break;
17435     case UNLT:
17436       return LTU;
17437       break;
17438     case UNLE:
17439       return LEU;
17440       break;
17441     case LTGT:
17442       return NE;
17443       break;
17444     default:
17445       return UNKNOWN;
17446     }
17447 }
17448
17449 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
17450
17451 static rtx
17452 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
17453 {
17454   enum machine_mode fpcmp_mode, intcmp_mode;
17455   rtx tmp, tmp2;
17456
17457   fpcmp_mode = ix86_fp_compare_mode (code);
17458   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
17459
17460   /* Do fcomi/sahf based test when profitable.  */
17461   switch (ix86_fp_comparison_strategy (code))
17462     {
17463     case IX86_FPCMP_COMI:
17464       intcmp_mode = fpcmp_mode;
17465       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
17466       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
17467                          tmp);
17468       emit_insn (tmp);
17469       break;
17470
17471     case IX86_FPCMP_SAHF:
17472       intcmp_mode = fpcmp_mode;
17473       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
17474       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
17475                          tmp);
17476
17477       if (!scratch)
17478         scratch = gen_reg_rtx (HImode);
17479       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
17480       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
17481       break;
17482
17483     case IX86_FPCMP_ARITH:
17484       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
17485       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
17486       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
17487       if (!scratch)
17488         scratch = gen_reg_rtx (HImode);
17489       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
17490
17491       /* In the unordered case, we have to check C2 for NaN's, which
17492          doesn't happen to work out to anything nice combination-wise.
17493          So do some bit twiddling on the value we've got in AH to come
17494          up with an appropriate set of condition codes.  */
17495
17496       intcmp_mode = CCNOmode;
17497       switch (code)
17498         {
17499         case GT:
17500         case UNGT:
17501           if (code == GT || !TARGET_IEEE_FP)
17502             {
17503               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
17504               code = EQ;
17505             }
17506           else
17507             {
17508               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17509               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
17510               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
17511               intcmp_mode = CCmode;
17512               code = GEU;
17513             }
17514           break;
17515         case LT:
17516         case UNLT:
17517           if (code == LT && TARGET_IEEE_FP)
17518             {
17519               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17520               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
17521               intcmp_mode = CCmode;
17522               code = EQ;
17523             }
17524           else
17525             {
17526               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
17527               code = NE;
17528             }
17529           break;
17530         case GE:
17531         case UNGE:
17532           if (code == GE || !TARGET_IEEE_FP)
17533             {
17534               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
17535               code = EQ;
17536             }
17537           else
17538             {
17539               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17540               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
17541               code = NE;
17542             }
17543           break;
17544         case LE:
17545         case UNLE:
17546           if (code == LE && TARGET_IEEE_FP)
17547             {
17548               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17549               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
17550               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
17551               intcmp_mode = CCmode;
17552               code = LTU;
17553             }
17554           else
17555             {
17556               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
17557               code = NE;
17558             }
17559           break;
17560         case EQ:
17561         case UNEQ:
17562           if (code == EQ && TARGET_IEEE_FP)
17563             {
17564               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17565               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
17566               intcmp_mode = CCmode;
17567               code = EQ;
17568             }
17569           else
17570             {
17571               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
17572               code = NE;
17573             }
17574           break;
17575         case NE:
17576         case LTGT:
17577           if (code == NE && TARGET_IEEE_FP)
17578             {
17579               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17580               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
17581                                              GEN_INT (0x40)));
17582               code = NE;
17583             }
17584           else
17585             {
17586               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
17587               code = EQ;
17588             }
17589           break;
17590
17591         case UNORDERED:
17592           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
17593           code = NE;
17594           break;
17595         case ORDERED:
17596           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
17597           code = EQ;
17598           break;
17599
17600         default:
17601           gcc_unreachable ();
17602         }
17603         break;
17604
17605     default:
17606       gcc_unreachable();
17607     }
17608
17609   /* Return the test that should be put into the flags user, i.e.
17610      the bcc, scc, or cmov instruction.  */
17611   return gen_rtx_fmt_ee (code, VOIDmode,
17612                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
17613                          const0_rtx);
17614 }
17615
17616 static rtx
17617 ix86_expand_compare (enum rtx_code code, rtx op0, rtx op1)
17618 {
17619   rtx ret;
17620
17621   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
17622     ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
17623
17624   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
17625     {
17626       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
17627       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
17628     }
17629   else
17630     ret = ix86_expand_int_compare (code, op0, op1);
17631
17632   return ret;
17633 }
17634
17635 void
17636 ix86_expand_branch (enum rtx_code code, rtx op0, rtx op1, rtx label)
17637 {
17638   enum machine_mode mode = GET_MODE (op0);
17639   rtx tmp;
17640
17641   switch (mode)
17642     {
17643     case SFmode:
17644     case DFmode:
17645     case XFmode:
17646     case QImode:
17647     case HImode:
17648     case SImode:
17649       simple:
17650       tmp = ix86_expand_compare (code, op0, op1);
17651       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
17652                                   gen_rtx_LABEL_REF (VOIDmode, label),
17653                                   pc_rtx);
17654       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
17655       return;
17656
17657     case DImode:
17658       if (TARGET_64BIT)
17659         goto simple;
17660     case TImode:
17661       /* Expand DImode branch into multiple compare+branch.  */
17662       {
17663         rtx lo[2], hi[2], label2;
17664         enum rtx_code code1, code2, code3;
17665         enum machine_mode submode;
17666
17667         if (CONSTANT_P (op0) && !CONSTANT_P (op1))
17668           {
17669             tmp = op0, op0 = op1, op1 = tmp;
17670             code = swap_condition (code);
17671           }
17672
17673         split_double_mode (mode, &op0, 1, lo+0, hi+0);
17674         split_double_mode (mode, &op1, 1, lo+1, hi+1);
17675
17676         submode = mode == DImode ? SImode : DImode;
17677
17678         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
17679            avoid two branches.  This costs one extra insn, so disable when
17680            optimizing for size.  */
17681
17682         if ((code == EQ || code == NE)
17683             && (!optimize_insn_for_size_p ()
17684                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
17685           {
17686             rtx xor0, xor1;
17687
17688             xor1 = hi[0];
17689             if (hi[1] != const0_rtx)
17690               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
17691                                    NULL_RTX, 0, OPTAB_WIDEN);
17692
17693             xor0 = lo[0];
17694             if (lo[1] != const0_rtx)
17695               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
17696                                    NULL_RTX, 0, OPTAB_WIDEN);
17697
17698             tmp = expand_binop (submode, ior_optab, xor1, xor0,
17699                                 NULL_RTX, 0, OPTAB_WIDEN);
17700
17701             ix86_expand_branch (code, tmp, const0_rtx, label);
17702             return;
17703           }
17704
17705         /* Otherwise, if we are doing less-than or greater-or-equal-than,
17706            op1 is a constant and the low word is zero, then we can just
17707            examine the high word.  Similarly for low word -1 and
17708            less-or-equal-than or greater-than.  */
17709
17710         if (CONST_INT_P (hi[1]))
17711           switch (code)
17712             {
17713             case LT: case LTU: case GE: case GEU:
17714               if (lo[1] == const0_rtx)
17715                 {
17716                   ix86_expand_branch (code, hi[0], hi[1], label);
17717                   return;
17718                 }
17719               break;
17720             case LE: case LEU: case GT: case GTU:
17721               if (lo[1] == constm1_rtx)
17722                 {
17723                   ix86_expand_branch (code, hi[0], hi[1], label);
17724                   return;
17725                 }
17726               break;
17727             default:
17728               break;
17729             }
17730
17731         /* Otherwise, we need two or three jumps.  */
17732
17733         label2 = gen_label_rtx ();
17734
17735         code1 = code;
17736         code2 = swap_condition (code);
17737         code3 = unsigned_condition (code);
17738
17739         switch (code)
17740           {
17741           case LT: case GT: case LTU: case GTU:
17742             break;
17743
17744           case LE:   code1 = LT;  code2 = GT;  break;
17745           case GE:   code1 = GT;  code2 = LT;  break;
17746           case LEU:  code1 = LTU; code2 = GTU; break;
17747           case GEU:  code1 = GTU; code2 = LTU; break;
17748
17749           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
17750           case NE:   code2 = UNKNOWN; break;
17751
17752           default:
17753             gcc_unreachable ();
17754           }
17755
17756         /*
17757          * a < b =>
17758          *    if (hi(a) < hi(b)) goto true;
17759          *    if (hi(a) > hi(b)) goto false;
17760          *    if (lo(a) < lo(b)) goto true;
17761          *  false:
17762          */
17763
17764         if (code1 != UNKNOWN)
17765           ix86_expand_branch (code1, hi[0], hi[1], label);
17766         if (code2 != UNKNOWN)
17767           ix86_expand_branch (code2, hi[0], hi[1], label2);
17768
17769         ix86_expand_branch (code3, lo[0], lo[1], label);
17770
17771         if (code2 != UNKNOWN)
17772           emit_label (label2);
17773         return;
17774       }
17775
17776     default:
17777       gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC);
17778       goto simple;
17779     }
17780 }
17781
17782 /* Split branch based on floating point condition.  */
17783 void
17784 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
17785                       rtx target1, rtx target2, rtx tmp, rtx pushed)
17786 {
17787   rtx condition;
17788   rtx i;
17789
17790   if (target2 != pc_rtx)
17791     {
17792       rtx tmp = target2;
17793       code = reverse_condition_maybe_unordered (code);
17794       target2 = target1;
17795       target1 = tmp;
17796     }
17797
17798   condition = ix86_expand_fp_compare (code, op1, op2,
17799                                       tmp);
17800
17801   /* Remove pushed operand from stack.  */
17802   if (pushed)
17803     ix86_free_from_memory (GET_MODE (pushed));
17804
17805   i = emit_jump_insn (gen_rtx_SET
17806                       (VOIDmode, pc_rtx,
17807                        gen_rtx_IF_THEN_ELSE (VOIDmode,
17808                                              condition, target1, target2)));
17809   if (split_branch_probability >= 0)
17810     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
17811 }
17812
17813 void
17814 ix86_expand_setcc (rtx dest, enum rtx_code code, rtx op0, rtx op1)
17815 {
17816   rtx ret;
17817
17818   gcc_assert (GET_MODE (dest) == QImode);
17819
17820   ret = ix86_expand_compare (code, op0, op1);
17821   PUT_MODE (ret, QImode);
17822   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
17823 }
17824
17825 /* Expand comparison setting or clearing carry flag.  Return true when
17826    successful and set pop for the operation.  */
17827 static bool
17828 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
17829 {
17830   enum machine_mode mode =
17831     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
17832
17833   /* Do not handle double-mode compares that go through special path.  */
17834   if (mode == (TARGET_64BIT ? TImode : DImode))
17835     return false;
17836
17837   if (SCALAR_FLOAT_MODE_P (mode))
17838     {
17839       rtx compare_op, compare_seq;
17840
17841       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
17842
17843       /* Shortcut:  following common codes never translate
17844          into carry flag compares.  */
17845       if (code == EQ || code == NE || code == UNEQ || code == LTGT
17846           || code == ORDERED || code == UNORDERED)
17847         return false;
17848
17849       /* These comparisons require zero flag; swap operands so they won't.  */
17850       if ((code == GT || code == UNLE || code == LE || code == UNGT)
17851           && !TARGET_IEEE_FP)
17852         {
17853           rtx tmp = op0;
17854           op0 = op1;
17855           op1 = tmp;
17856           code = swap_condition (code);
17857         }
17858
17859       /* Try to expand the comparison and verify that we end up with
17860          carry flag based comparison.  This fails to be true only when
17861          we decide to expand comparison using arithmetic that is not
17862          too common scenario.  */
17863       start_sequence ();
17864       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
17865       compare_seq = get_insns ();
17866       end_sequence ();
17867
17868       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
17869           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
17870         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
17871       else
17872         code = GET_CODE (compare_op);
17873
17874       if (code != LTU && code != GEU)
17875         return false;
17876
17877       emit_insn (compare_seq);
17878       *pop = compare_op;
17879       return true;
17880     }
17881
17882   if (!INTEGRAL_MODE_P (mode))
17883     return false;
17884
17885   switch (code)
17886     {
17887     case LTU:
17888     case GEU:
17889       break;
17890
17891     /* Convert a==0 into (unsigned)a<1.  */
17892     case EQ:
17893     case NE:
17894       if (op1 != const0_rtx)
17895         return false;
17896       op1 = const1_rtx;
17897       code = (code == EQ ? LTU : GEU);
17898       break;
17899
17900     /* Convert a>b into b<a or a>=b-1.  */
17901     case GTU:
17902     case LEU:
17903       if (CONST_INT_P (op1))
17904         {
17905           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
17906           /* Bail out on overflow.  We still can swap operands but that
17907              would force loading of the constant into register.  */
17908           if (op1 == const0_rtx
17909               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
17910             return false;
17911           code = (code == GTU ? GEU : LTU);
17912         }
17913       else
17914         {
17915           rtx tmp = op1;
17916           op1 = op0;
17917           op0 = tmp;
17918           code = (code == GTU ? LTU : GEU);
17919         }
17920       break;
17921
17922     /* Convert a>=0 into (unsigned)a<0x80000000.  */
17923     case LT:
17924     case GE:
17925       if (mode == DImode || op1 != const0_rtx)
17926         return false;
17927       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
17928       code = (code == LT ? GEU : LTU);
17929       break;
17930     case LE:
17931     case GT:
17932       if (mode == DImode || op1 != constm1_rtx)
17933         return false;
17934       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
17935       code = (code == LE ? GEU : LTU);
17936       break;
17937
17938     default:
17939       return false;
17940     }
17941   /* Swapping operands may cause constant to appear as first operand.  */
17942   if (!nonimmediate_operand (op0, VOIDmode))
17943     {
17944       if (!can_create_pseudo_p ())
17945         return false;
17946       op0 = force_reg (mode, op0);
17947     }
17948   *pop = ix86_expand_compare (code, op0, op1);
17949   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
17950   return true;
17951 }
17952
17953 bool
17954 ix86_expand_int_movcc (rtx operands[])
17955 {
17956   enum rtx_code code = GET_CODE (operands[1]), compare_code;
17957   rtx compare_seq, compare_op;
17958   enum machine_mode mode = GET_MODE (operands[0]);
17959   bool sign_bit_compare_p = false;
17960   rtx op0 = XEXP (operands[1], 0);
17961   rtx op1 = XEXP (operands[1], 1);
17962
17963   start_sequence ();
17964   compare_op = ix86_expand_compare (code, op0, op1);
17965   compare_seq = get_insns ();
17966   end_sequence ();
17967
17968   compare_code = GET_CODE (compare_op);
17969
17970   if ((op1 == const0_rtx && (code == GE || code == LT))
17971       || (op1 == constm1_rtx && (code == GT || code == LE)))
17972     sign_bit_compare_p = true;
17973
17974   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
17975      HImode insns, we'd be swallowed in word prefix ops.  */
17976
17977   if ((mode != HImode || TARGET_FAST_PREFIX)
17978       && (mode != (TARGET_64BIT ? TImode : DImode))
17979       && CONST_INT_P (operands[2])
17980       && CONST_INT_P (operands[3]))
17981     {
17982       rtx out = operands[0];
17983       HOST_WIDE_INT ct = INTVAL (operands[2]);
17984       HOST_WIDE_INT cf = INTVAL (operands[3]);
17985       HOST_WIDE_INT diff;
17986
17987       diff = ct - cf;
17988       /*  Sign bit compares are better done using shifts than we do by using
17989           sbb.  */
17990       if (sign_bit_compare_p
17991           || ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
17992         {
17993           /* Detect overlap between destination and compare sources.  */
17994           rtx tmp = out;
17995
17996           if (!sign_bit_compare_p)
17997             {
17998               rtx flags;
17999               bool fpcmp = false;
18000
18001               compare_code = GET_CODE (compare_op);
18002
18003               flags = XEXP (compare_op, 0);
18004
18005               if (GET_MODE (flags) == CCFPmode
18006                   || GET_MODE (flags) == CCFPUmode)
18007                 {
18008                   fpcmp = true;
18009                   compare_code
18010                     = ix86_fp_compare_code_to_integer (compare_code);
18011                 }
18012
18013               /* To simplify rest of code, restrict to the GEU case.  */
18014               if (compare_code == LTU)
18015                 {
18016                   HOST_WIDE_INT tmp = ct;
18017                   ct = cf;
18018                   cf = tmp;
18019                   compare_code = reverse_condition (compare_code);
18020                   code = reverse_condition (code);
18021                 }
18022               else
18023                 {
18024                   if (fpcmp)
18025                     PUT_CODE (compare_op,
18026                               reverse_condition_maybe_unordered
18027                                 (GET_CODE (compare_op)));
18028                   else
18029                     PUT_CODE (compare_op,
18030                               reverse_condition (GET_CODE (compare_op)));
18031                 }
18032               diff = ct - cf;
18033
18034               if (reg_overlap_mentioned_p (out, op0)
18035                   || reg_overlap_mentioned_p (out, op1))
18036                 tmp = gen_reg_rtx (mode);
18037
18038               if (mode == DImode)
18039                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
18040               else
18041                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
18042                                                  flags, compare_op));
18043             }
18044           else
18045             {
18046               if (code == GT || code == GE)
18047                 code = reverse_condition (code);
18048               else
18049                 {
18050                   HOST_WIDE_INT tmp = ct;
18051                   ct = cf;
18052                   cf = tmp;
18053                   diff = ct - cf;
18054                 }
18055               tmp = emit_store_flag (tmp, code, op0, op1, VOIDmode, 0, -1);
18056             }
18057
18058           if (diff == 1)
18059             {
18060               /*
18061                * cmpl op0,op1
18062                * sbbl dest,dest
18063                * [addl dest, ct]
18064                *
18065                * Size 5 - 8.
18066                */
18067               if (ct)
18068                 tmp = expand_simple_binop (mode, PLUS,
18069                                            tmp, GEN_INT (ct),
18070                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18071             }
18072           else if (cf == -1)
18073             {
18074               /*
18075                * cmpl op0,op1
18076                * sbbl dest,dest
18077                * orl $ct, dest
18078                *
18079                * Size 8.
18080                */
18081               tmp = expand_simple_binop (mode, IOR,
18082                                          tmp, GEN_INT (ct),
18083                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
18084             }
18085           else if (diff == -1 && ct)
18086             {
18087               /*
18088                * cmpl op0,op1
18089                * sbbl dest,dest
18090                * notl dest
18091                * [addl dest, cf]
18092                *
18093                * Size 8 - 11.
18094                */
18095               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
18096               if (cf)
18097                 tmp = expand_simple_binop (mode, PLUS,
18098                                            copy_rtx (tmp), GEN_INT (cf),
18099                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18100             }
18101           else
18102             {
18103               /*
18104                * cmpl op0,op1
18105                * sbbl dest,dest
18106                * [notl dest]
18107                * andl cf - ct, dest
18108                * [addl dest, ct]
18109                *
18110                * Size 8 - 11.
18111                */
18112
18113               if (cf == 0)
18114                 {
18115                   cf = ct;
18116                   ct = 0;
18117                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
18118                 }
18119
18120               tmp = expand_simple_binop (mode, AND,
18121                                          copy_rtx (tmp),
18122                                          gen_int_mode (cf - ct, mode),
18123                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
18124               if (ct)
18125                 tmp = expand_simple_binop (mode, PLUS,
18126                                            copy_rtx (tmp), GEN_INT (ct),
18127                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
18128             }
18129
18130           if (!rtx_equal_p (tmp, out))
18131             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
18132
18133           return true;
18134         }
18135
18136       if (diff < 0)
18137         {
18138           enum machine_mode cmp_mode = GET_MODE (op0);
18139
18140           HOST_WIDE_INT tmp;
18141           tmp = ct, ct = cf, cf = tmp;
18142           diff = -diff;
18143
18144           if (SCALAR_FLOAT_MODE_P (cmp_mode))
18145             {
18146               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
18147
18148               /* We may be reversing unordered compare to normal compare, that
18149                  is not valid in general (we may convert non-trapping condition
18150                  to trapping one), however on i386 we currently emit all
18151                  comparisons unordered.  */
18152               compare_code = reverse_condition_maybe_unordered (compare_code);
18153               code = reverse_condition_maybe_unordered (code);
18154             }
18155           else
18156             {
18157               compare_code = reverse_condition (compare_code);
18158               code = reverse_condition (code);
18159             }
18160         }
18161
18162       compare_code = UNKNOWN;
18163       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
18164           && CONST_INT_P (op1))
18165         {
18166           if (op1 == const0_rtx
18167               && (code == LT || code == GE))
18168             compare_code = code;
18169           else if (op1 == constm1_rtx)
18170             {
18171               if (code == LE)
18172                 compare_code = LT;
18173               else if (code == GT)
18174                 compare_code = GE;
18175             }
18176         }
18177
18178       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
18179       if (compare_code != UNKNOWN
18180           && GET_MODE (op0) == GET_MODE (out)
18181           && (cf == -1 || ct == -1))
18182         {
18183           /* If lea code below could be used, only optimize
18184              if it results in a 2 insn sequence.  */
18185
18186           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
18187                  || diff == 3 || diff == 5 || diff == 9)
18188               || (compare_code == LT && ct == -1)
18189               || (compare_code == GE && cf == -1))
18190             {
18191               /*
18192                * notl op1       (if necessary)
18193                * sarl $31, op1
18194                * orl cf, op1
18195                */
18196               if (ct != -1)
18197                 {
18198                   cf = ct;
18199                   ct = -1;
18200                   code = reverse_condition (code);
18201                 }
18202
18203               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
18204
18205               out = expand_simple_binop (mode, IOR,
18206                                          out, GEN_INT (cf),
18207                                          out, 1, OPTAB_DIRECT);
18208               if (out != operands[0])
18209                 emit_move_insn (operands[0], out);
18210
18211               return true;
18212             }
18213         }
18214
18215
18216       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
18217            || diff == 3 || diff == 5 || diff == 9)
18218           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
18219           && (mode != DImode
18220               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
18221         {
18222           /*
18223            * xorl dest,dest
18224            * cmpl op1,op2
18225            * setcc dest
18226            * lea cf(dest*(ct-cf)),dest
18227            *
18228            * Size 14.
18229            *
18230            * This also catches the degenerate setcc-only case.
18231            */
18232
18233           rtx tmp;
18234           int nops;
18235
18236           out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
18237
18238           nops = 0;
18239           /* On x86_64 the lea instruction operates on Pmode, so we need
18240              to get arithmetics done in proper mode to match.  */
18241           if (diff == 1)
18242             tmp = copy_rtx (out);
18243           else
18244             {
18245               rtx out1;
18246               out1 = copy_rtx (out);
18247               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
18248               nops++;
18249               if (diff & 1)
18250                 {
18251                   tmp = gen_rtx_PLUS (mode, tmp, out1);
18252                   nops++;
18253                 }
18254             }
18255           if (cf != 0)
18256             {
18257               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
18258               nops++;
18259             }
18260           if (!rtx_equal_p (tmp, out))
18261             {
18262               if (nops == 1)
18263                 out = force_operand (tmp, copy_rtx (out));
18264               else
18265                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
18266             }
18267           if (!rtx_equal_p (out, operands[0]))
18268             emit_move_insn (operands[0], copy_rtx (out));
18269
18270           return true;
18271         }
18272
18273       /*
18274        * General case:                  Jumpful:
18275        *   xorl dest,dest               cmpl op1, op2
18276        *   cmpl op1, op2                movl ct, dest
18277        *   setcc dest                   jcc 1f
18278        *   decl dest                    movl cf, dest
18279        *   andl (cf-ct),dest            1:
18280        *   addl ct,dest
18281        *
18282        * Size 20.                       Size 14.
18283        *
18284        * This is reasonably steep, but branch mispredict costs are
18285        * high on modern cpus, so consider failing only if optimizing
18286        * for space.
18287        */
18288
18289       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
18290           && BRANCH_COST (optimize_insn_for_speed_p (),
18291                           false) >= 2)
18292         {
18293           if (cf == 0)
18294             {
18295               enum machine_mode cmp_mode = GET_MODE (op0);
18296
18297               cf = ct;
18298               ct = 0;
18299
18300               if (SCALAR_FLOAT_MODE_P (cmp_mode))
18301                 {
18302                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
18303
18304                   /* We may be reversing unordered compare to normal compare,
18305                      that is not valid in general (we may convert non-trapping
18306                      condition to trapping one), however on i386 we currently
18307                      emit all comparisons unordered.  */
18308                   code = reverse_condition_maybe_unordered (code);
18309                 }
18310               else
18311                 {
18312                   code = reverse_condition (code);
18313                   if (compare_code != UNKNOWN)
18314                     compare_code = reverse_condition (compare_code);
18315                 }
18316             }
18317
18318           if (compare_code != UNKNOWN)
18319             {
18320               /* notl op1       (if needed)
18321                  sarl $31, op1
18322                  andl (cf-ct), op1
18323                  addl ct, op1
18324
18325                  For x < 0 (resp. x <= -1) there will be no notl,
18326                  so if possible swap the constants to get rid of the
18327                  complement.
18328                  True/false will be -1/0 while code below (store flag
18329                  followed by decrement) is 0/-1, so the constants need
18330                  to be exchanged once more.  */
18331
18332               if (compare_code == GE || !cf)
18333                 {
18334                   code = reverse_condition (code);
18335                   compare_code = LT;
18336                 }
18337               else
18338                 {
18339                   HOST_WIDE_INT tmp = cf;
18340                   cf = ct;
18341                   ct = tmp;
18342                 }
18343
18344               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
18345             }
18346           else
18347             {
18348               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
18349
18350               out = expand_simple_binop (mode, PLUS, copy_rtx (out),
18351                                          constm1_rtx,
18352                                          copy_rtx (out), 1, OPTAB_DIRECT);
18353             }
18354
18355           out = expand_simple_binop (mode, AND, copy_rtx (out),
18356                                      gen_int_mode (cf - ct, mode),
18357                                      copy_rtx (out), 1, OPTAB_DIRECT);
18358           if (ct)
18359             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
18360                                        copy_rtx (out), 1, OPTAB_DIRECT);
18361           if (!rtx_equal_p (out, operands[0]))
18362             emit_move_insn (operands[0], copy_rtx (out));
18363
18364           return true;
18365         }
18366     }
18367
18368   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
18369     {
18370       /* Try a few things more with specific constants and a variable.  */
18371
18372       optab op;
18373       rtx var, orig_out, out, tmp;
18374
18375       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
18376         return false;
18377
18378       /* If one of the two operands is an interesting constant, load a
18379          constant with the above and mask it in with a logical operation.  */
18380
18381       if (CONST_INT_P (operands[2]))
18382         {
18383           var = operands[3];
18384           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
18385             operands[3] = constm1_rtx, op = and_optab;
18386           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
18387             operands[3] = const0_rtx, op = ior_optab;
18388           else
18389             return false;
18390         }
18391       else if (CONST_INT_P (operands[3]))
18392         {
18393           var = operands[2];
18394           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
18395             operands[2] = constm1_rtx, op = and_optab;
18396           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
18397             operands[2] = const0_rtx, op = ior_optab;
18398           else
18399             return false;
18400         }
18401       else
18402         return false;
18403
18404       orig_out = operands[0];
18405       tmp = gen_reg_rtx (mode);
18406       operands[0] = tmp;
18407
18408       /* Recurse to get the constant loaded.  */
18409       if (ix86_expand_int_movcc (operands) == 0)
18410         return false;
18411
18412       /* Mask in the interesting variable.  */
18413       out = expand_binop (mode, op, var, tmp, orig_out, 0,
18414                           OPTAB_WIDEN);
18415       if (!rtx_equal_p (out, orig_out))
18416         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
18417
18418       return true;
18419     }
18420
18421   /*
18422    * For comparison with above,
18423    *
18424    * movl cf,dest
18425    * movl ct,tmp
18426    * cmpl op1,op2
18427    * cmovcc tmp,dest
18428    *
18429    * Size 15.
18430    */
18431
18432   if (! nonimmediate_operand (operands[2], mode))
18433     operands[2] = force_reg (mode, operands[2]);
18434   if (! nonimmediate_operand (operands[3], mode))
18435     operands[3] = force_reg (mode, operands[3]);
18436
18437   if (! register_operand (operands[2], VOIDmode)
18438       && (mode == QImode
18439           || ! register_operand (operands[3], VOIDmode)))
18440     operands[2] = force_reg (mode, operands[2]);
18441
18442   if (mode == QImode
18443       && ! register_operand (operands[3], VOIDmode))
18444     operands[3] = force_reg (mode, operands[3]);
18445
18446   emit_insn (compare_seq);
18447   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
18448                           gen_rtx_IF_THEN_ELSE (mode,
18449                                                 compare_op, operands[2],
18450                                                 operands[3])));
18451   return true;
18452 }
18453
18454 /* Swap, force into registers, or otherwise massage the two operands
18455    to an sse comparison with a mask result.  Thus we differ a bit from
18456    ix86_prepare_fp_compare_args which expects to produce a flags result.
18457
18458    The DEST operand exists to help determine whether to commute commutative
18459    operators.  The POP0/POP1 operands are updated in place.  The new
18460    comparison code is returned, or UNKNOWN if not implementable.  */
18461
18462 static enum rtx_code
18463 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
18464                                   rtx *pop0, rtx *pop1)
18465 {
18466   rtx tmp;
18467
18468   switch (code)
18469     {
18470     case LTGT:
18471     case UNEQ:
18472       /* We have no LTGT as an operator.  We could implement it with
18473          NE & ORDERED, but this requires an extra temporary.  It's
18474          not clear that it's worth it.  */
18475       return UNKNOWN;
18476
18477     case LT:
18478     case LE:
18479     case UNGT:
18480     case UNGE:
18481       /* These are supported directly.  */
18482       break;
18483
18484     case EQ:
18485     case NE:
18486     case UNORDERED:
18487     case ORDERED:
18488       /* For commutative operators, try to canonicalize the destination
18489          operand to be first in the comparison - this helps reload to
18490          avoid extra moves.  */
18491       if (!dest || !rtx_equal_p (dest, *pop1))
18492         break;
18493       /* FALLTHRU */
18494
18495     case GE:
18496     case GT:
18497     case UNLE:
18498     case UNLT:
18499       /* These are not supported directly.  Swap the comparison operands
18500          to transform into something that is supported.  */
18501       tmp = *pop0;
18502       *pop0 = *pop1;
18503       *pop1 = tmp;
18504       code = swap_condition (code);
18505       break;
18506
18507     default:
18508       gcc_unreachable ();
18509     }
18510
18511   return code;
18512 }
18513
18514 /* Detect conditional moves that exactly match min/max operational
18515    semantics.  Note that this is IEEE safe, as long as we don't
18516    interchange the operands.
18517
18518    Returns FALSE if this conditional move doesn't match a MIN/MAX,
18519    and TRUE if the operation is successful and instructions are emitted.  */
18520
18521 static bool
18522 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
18523                            rtx cmp_op1, rtx if_true, rtx if_false)
18524 {
18525   enum machine_mode mode;
18526   bool is_min;
18527   rtx tmp;
18528
18529   if (code == LT)
18530     ;
18531   else if (code == UNGE)
18532     {
18533       tmp = if_true;
18534       if_true = if_false;
18535       if_false = tmp;
18536     }
18537   else
18538     return false;
18539
18540   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
18541     is_min = true;
18542   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
18543     is_min = false;
18544   else
18545     return false;
18546
18547   mode = GET_MODE (dest);
18548
18549   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
18550      but MODE may be a vector mode and thus not appropriate.  */
18551   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
18552     {
18553       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
18554       rtvec v;
18555
18556       if_true = force_reg (mode, if_true);
18557       v = gen_rtvec (2, if_true, if_false);
18558       tmp = gen_rtx_UNSPEC (mode, v, u);
18559     }
18560   else
18561     {
18562       code = is_min ? SMIN : SMAX;
18563       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
18564     }
18565
18566   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
18567   return true;
18568 }
18569
18570 /* Expand an sse vector comparison.  Return the register with the result.  */
18571
18572 static rtx
18573 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
18574                      rtx op_true, rtx op_false)
18575 {
18576   enum machine_mode mode = GET_MODE (dest);
18577   rtx x;
18578
18579   cmp_op0 = force_reg (mode, cmp_op0);
18580   if (!nonimmediate_operand (cmp_op1, mode))
18581     cmp_op1 = force_reg (mode, cmp_op1);
18582
18583   if (optimize
18584       || reg_overlap_mentioned_p (dest, op_true)
18585       || reg_overlap_mentioned_p (dest, op_false))
18586     dest = gen_reg_rtx (mode);
18587
18588   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
18589   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18590
18591   return dest;
18592 }
18593
18594 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
18595    operations.  This is used for both scalar and vector conditional moves.  */
18596
18597 static void
18598 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
18599 {
18600   enum machine_mode mode = GET_MODE (dest);
18601   rtx t2, t3, x;
18602
18603   if (op_false == CONST0_RTX (mode))
18604     {
18605       op_true = force_reg (mode, op_true);
18606       x = gen_rtx_AND (mode, cmp, op_true);
18607       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18608     }
18609   else if (op_true == CONST0_RTX (mode))
18610     {
18611       op_false = force_reg (mode, op_false);
18612       x = gen_rtx_NOT (mode, cmp);
18613       x = gen_rtx_AND (mode, x, op_false);
18614       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18615     }
18616   else if (TARGET_XOP)
18617     {
18618       rtx pcmov = gen_rtx_SET (mode, dest,
18619                                gen_rtx_IF_THEN_ELSE (mode, cmp,
18620                                                      op_true,
18621                                                      op_false));
18622       emit_insn (pcmov);
18623     }
18624   else
18625     {
18626       op_true = force_reg (mode, op_true);
18627       op_false = force_reg (mode, op_false);
18628
18629       t2 = gen_reg_rtx (mode);
18630       if (optimize)
18631         t3 = gen_reg_rtx (mode);
18632       else
18633         t3 = dest;
18634
18635       x = gen_rtx_AND (mode, op_true, cmp);
18636       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
18637
18638       x = gen_rtx_NOT (mode, cmp);
18639       x = gen_rtx_AND (mode, x, op_false);
18640       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
18641
18642       x = gen_rtx_IOR (mode, t3, t2);
18643       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18644     }
18645 }
18646
18647 /* Expand a floating-point conditional move.  Return true if successful.  */
18648
18649 bool
18650 ix86_expand_fp_movcc (rtx operands[])
18651 {
18652   enum machine_mode mode = GET_MODE (operands[0]);
18653   enum rtx_code code = GET_CODE (operands[1]);
18654   rtx tmp, compare_op;
18655   rtx op0 = XEXP (operands[1], 0);
18656   rtx op1 = XEXP (operands[1], 1);
18657
18658   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
18659     {
18660       enum machine_mode cmode;
18661
18662       /* Since we've no cmove for sse registers, don't force bad register
18663          allocation just to gain access to it.  Deny movcc when the
18664          comparison mode doesn't match the move mode.  */
18665       cmode = GET_MODE (op0);
18666       if (cmode == VOIDmode)
18667         cmode = GET_MODE (op1);
18668       if (cmode != mode)
18669         return false;
18670
18671       code = ix86_prepare_sse_fp_compare_args (operands[0], code, &op0, &op1);
18672       if (code == UNKNOWN)
18673         return false;
18674
18675       if (ix86_expand_sse_fp_minmax (operands[0], code, op0, op1,
18676                                      operands[2], operands[3]))
18677         return true;
18678
18679       tmp = ix86_expand_sse_cmp (operands[0], code, op0, op1,
18680                                  operands[2], operands[3]);
18681       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
18682       return true;
18683     }
18684
18685   /* The floating point conditional move instructions don't directly
18686      support conditions resulting from a signed integer comparison.  */
18687
18688   compare_op = ix86_expand_compare (code, op0, op1);
18689   if (!fcmov_comparison_operator (compare_op, VOIDmode))
18690     {
18691       tmp = gen_reg_rtx (QImode);
18692       ix86_expand_setcc (tmp, code, op0, op1);
18693
18694       compare_op = ix86_expand_compare (NE, tmp, const0_rtx);
18695     }
18696
18697   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
18698                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
18699                                                 operands[2], operands[3])));
18700
18701   return true;
18702 }
18703
18704 /* Expand a floating-point vector conditional move; a vcond operation
18705    rather than a movcc operation.  */
18706
18707 bool
18708 ix86_expand_fp_vcond (rtx operands[])
18709 {
18710   enum rtx_code code = GET_CODE (operands[3]);
18711   rtx cmp;
18712
18713   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
18714                                            &operands[4], &operands[5]);
18715   if (code == UNKNOWN)
18716     return false;
18717
18718   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
18719                                  operands[5], operands[1], operands[2]))
18720     return true;
18721
18722   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
18723                              operands[1], operands[2]);
18724   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
18725   return true;
18726 }
18727
18728 /* Expand a signed/unsigned integral vector conditional move.  */
18729
18730 bool
18731 ix86_expand_int_vcond (rtx operands[])
18732 {
18733   enum machine_mode mode = GET_MODE (operands[0]);
18734   enum rtx_code code = GET_CODE (operands[3]);
18735   bool negate = false;
18736   rtx x, cop0, cop1;
18737
18738   cop0 = operands[4];
18739   cop1 = operands[5];
18740
18741   /* XOP supports all of the comparisons on all vector int types.  */
18742   if (!TARGET_XOP)
18743     {
18744       /* Canonicalize the comparison to EQ, GT, GTU.  */
18745       switch (code)
18746         {
18747         case EQ:
18748         case GT:
18749         case GTU:
18750           break;
18751
18752         case NE:
18753         case LE:
18754         case LEU:
18755           code = reverse_condition (code);
18756           negate = true;
18757           break;
18758
18759         case GE:
18760         case GEU:
18761           code = reverse_condition (code);
18762           negate = true;
18763           /* FALLTHRU */
18764
18765         case LT:
18766         case LTU:
18767           code = swap_condition (code);
18768           x = cop0, cop0 = cop1, cop1 = x;
18769           break;
18770
18771         default:
18772           gcc_unreachable ();
18773         }
18774
18775       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
18776       if (mode == V2DImode)
18777         {
18778           switch (code)
18779             {
18780             case EQ:
18781               /* SSE4.1 supports EQ.  */
18782               if (!TARGET_SSE4_1)
18783                 return false;
18784               break;
18785
18786             case GT:
18787             case GTU:
18788               /* SSE4.2 supports GT/GTU.  */
18789               if (!TARGET_SSE4_2)
18790                 return false;
18791               break;
18792
18793             default:
18794               gcc_unreachable ();
18795             }
18796         }
18797
18798       /* Unsigned parallel compare is not supported by the hardware.
18799          Play some tricks to turn this into a signed comparison
18800          against 0.  */
18801       if (code == GTU)
18802         {
18803           cop0 = force_reg (mode, cop0);
18804
18805           switch (mode)
18806             {
18807             case V4SImode:
18808             case V2DImode:
18809                 {
18810                   rtx t1, t2, mask;
18811                   rtx (*gen_sub3) (rtx, rtx, rtx);
18812
18813                   /* Subtract (-(INT MAX) - 1) from both operands to make
18814                      them signed.  */
18815                   mask = ix86_build_signbit_mask (mode, true, false);
18816                   gen_sub3 = (mode == V4SImode
18817                               ? gen_subv4si3 : gen_subv2di3);
18818                   t1 = gen_reg_rtx (mode);
18819                   emit_insn (gen_sub3 (t1, cop0, mask));
18820
18821                   t2 = gen_reg_rtx (mode);
18822                   emit_insn (gen_sub3 (t2, cop1, mask));
18823
18824                   cop0 = t1;
18825                   cop1 = t2;
18826                   code = GT;
18827                 }
18828               break;
18829
18830             case V16QImode:
18831             case V8HImode:
18832               /* Perform a parallel unsigned saturating subtraction.  */
18833               x = gen_reg_rtx (mode);
18834               emit_insn (gen_rtx_SET (VOIDmode, x,
18835                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
18836
18837               cop0 = x;
18838               cop1 = CONST0_RTX (mode);
18839               code = EQ;
18840               negate = !negate;
18841               break;
18842
18843             default:
18844               gcc_unreachable ();
18845             }
18846         }
18847     }
18848
18849   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
18850                            operands[1+negate], operands[2-negate]);
18851
18852   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
18853                          operands[2-negate]);
18854   return true;
18855 }
18856
18857 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
18858    true if we should do zero extension, else sign extension.  HIGH_P is
18859    true if we want the N/2 high elements, else the low elements.  */
18860
18861 void
18862 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18863 {
18864   enum machine_mode imode = GET_MODE (operands[1]);
18865   rtx (*unpack)(rtx, rtx, rtx);
18866   rtx se, dest;
18867
18868   switch (imode)
18869     {
18870     case V16QImode:
18871       if (high_p)
18872         unpack = gen_vec_interleave_highv16qi;
18873       else
18874         unpack = gen_vec_interleave_lowv16qi;
18875       break;
18876     case V8HImode:
18877       if (high_p)
18878         unpack = gen_vec_interleave_highv8hi;
18879       else
18880         unpack = gen_vec_interleave_lowv8hi;
18881       break;
18882     case V4SImode:
18883       if (high_p)
18884         unpack = gen_vec_interleave_highv4si;
18885       else
18886         unpack = gen_vec_interleave_lowv4si;
18887       break;
18888     default:
18889       gcc_unreachable ();
18890     }
18891
18892   dest = gen_lowpart (imode, operands[0]);
18893
18894   if (unsigned_p)
18895     se = force_reg (imode, CONST0_RTX (imode));
18896   else
18897     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
18898                               operands[1], pc_rtx, pc_rtx);
18899
18900   emit_insn (unpack (dest, operands[1], se));
18901 }
18902
18903 /* This function performs the same task as ix86_expand_sse_unpack,
18904    but with SSE4.1 instructions.  */
18905
18906 void
18907 ix86_expand_sse4_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18908 {
18909   enum machine_mode imode = GET_MODE (operands[1]);
18910   rtx (*unpack)(rtx, rtx);
18911   rtx src, dest;
18912
18913   switch (imode)
18914     {
18915     case V16QImode:
18916       if (unsigned_p)
18917         unpack = gen_sse4_1_zero_extendv8qiv8hi2;
18918       else
18919         unpack = gen_sse4_1_sign_extendv8qiv8hi2;
18920       break;
18921     case V8HImode:
18922       if (unsigned_p)
18923         unpack = gen_sse4_1_zero_extendv4hiv4si2;
18924       else
18925         unpack = gen_sse4_1_sign_extendv4hiv4si2;
18926       break;
18927     case V4SImode:
18928       if (unsigned_p)
18929         unpack = gen_sse4_1_zero_extendv2siv2di2;
18930       else
18931         unpack = gen_sse4_1_sign_extendv2siv2di2;
18932       break;
18933     default:
18934       gcc_unreachable ();
18935     }
18936
18937   dest = operands[0];
18938   if (high_p)
18939     {
18940       /* Shift higher 8 bytes to lower 8 bytes.  */
18941       src = gen_reg_rtx (imode);
18942       emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, src),
18943                                      gen_lowpart (V1TImode, operands[1]),
18944                                      GEN_INT (64)));
18945     }
18946   else
18947     src = operands[1];
18948
18949   emit_insn (unpack (dest, src));
18950 }
18951
18952 /* Expand conditional increment or decrement using adb/sbb instructions.
18953    The default case using setcc followed by the conditional move can be
18954    done by generic code.  */
18955 bool
18956 ix86_expand_int_addcc (rtx operands[])
18957 {
18958   enum rtx_code code = GET_CODE (operands[1]);
18959   rtx flags;
18960   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
18961   rtx compare_op;
18962   rtx val = const0_rtx;
18963   bool fpcmp = false;
18964   enum machine_mode mode;
18965   rtx op0 = XEXP (operands[1], 0);
18966   rtx op1 = XEXP (operands[1], 1);
18967
18968   if (operands[3] != const1_rtx
18969       && operands[3] != constm1_rtx)
18970     return false;
18971   if (!ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
18972      return false;
18973   code = GET_CODE (compare_op);
18974
18975   flags = XEXP (compare_op, 0);
18976
18977   if (GET_MODE (flags) == CCFPmode
18978       || GET_MODE (flags) == CCFPUmode)
18979     {
18980       fpcmp = true;
18981       code = ix86_fp_compare_code_to_integer (code);
18982     }
18983
18984   if (code != LTU)
18985     {
18986       val = constm1_rtx;
18987       if (fpcmp)
18988         PUT_CODE (compare_op,
18989                   reverse_condition_maybe_unordered
18990                     (GET_CODE (compare_op)));
18991       else
18992         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
18993     }
18994
18995   mode = GET_MODE (operands[0]);
18996
18997   /* Construct either adc or sbb insn.  */
18998   if ((code == LTU) == (operands[3] == constm1_rtx))
18999     {
19000       switch (mode)
19001         {
19002           case QImode:
19003             insn = gen_subqi3_carry;
19004             break;
19005           case HImode:
19006             insn = gen_subhi3_carry;
19007             break;
19008           case SImode:
19009             insn = gen_subsi3_carry;
19010             break;
19011           case DImode:
19012             insn = gen_subdi3_carry;
19013             break;
19014           default:
19015             gcc_unreachable ();
19016         }
19017     }
19018   else
19019     {
19020       switch (mode)
19021         {
19022           case QImode:
19023             insn = gen_addqi3_carry;
19024             break;
19025           case HImode:
19026             insn = gen_addhi3_carry;
19027             break;
19028           case SImode:
19029             insn = gen_addsi3_carry;
19030             break;
19031           case DImode:
19032             insn = gen_adddi3_carry;
19033             break;
19034           default:
19035             gcc_unreachable ();
19036         }
19037     }
19038   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
19039
19040   return true;
19041 }
19042
19043
19044 /* Split operands 0 and 1 into half-mode parts.  Similar to split_double_mode,
19045    but works for floating pointer parameters and nonoffsetable memories.
19046    For pushes, it returns just stack offsets; the values will be saved
19047    in the right order.  Maximally three parts are generated.  */
19048
19049 static int
19050 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
19051 {
19052   int size;
19053
19054   if (!TARGET_64BIT)
19055     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
19056   else
19057     size = (GET_MODE_SIZE (mode) + 4) / 8;
19058
19059   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
19060   gcc_assert (size >= 2 && size <= 4);
19061
19062   /* Optimize constant pool reference to immediates.  This is used by fp
19063      moves, that force all constants to memory to allow combining.  */
19064   if (MEM_P (operand) && MEM_READONLY_P (operand))
19065     {
19066       rtx tmp = maybe_get_pool_constant (operand);
19067       if (tmp)
19068         operand = tmp;
19069     }
19070
19071   if (MEM_P (operand) && !offsettable_memref_p (operand))
19072     {
19073       /* The only non-offsetable memories we handle are pushes.  */
19074       int ok = push_operand (operand, VOIDmode);
19075
19076       gcc_assert (ok);
19077
19078       operand = copy_rtx (operand);
19079       PUT_MODE (operand, Pmode);
19080       parts[0] = parts[1] = parts[2] = parts[3] = operand;
19081       return size;
19082     }
19083
19084   if (GET_CODE (operand) == CONST_VECTOR)
19085     {
19086       enum machine_mode imode = int_mode_for_mode (mode);
19087       /* Caution: if we looked through a constant pool memory above,
19088          the operand may actually have a different mode now.  That's
19089          ok, since we want to pun this all the way back to an integer.  */
19090       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
19091       gcc_assert (operand != NULL);
19092       mode = imode;
19093     }
19094
19095   if (!TARGET_64BIT)
19096     {
19097       if (mode == DImode)
19098         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
19099       else
19100         {
19101           int i;
19102
19103           if (REG_P (operand))
19104             {
19105               gcc_assert (reload_completed);
19106               for (i = 0; i < size; i++)
19107                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
19108             }
19109           else if (offsettable_memref_p (operand))
19110             {
19111               operand = adjust_address (operand, SImode, 0);
19112               parts[0] = operand;
19113               for (i = 1; i < size; i++)
19114                 parts[i] = adjust_address (operand, SImode, 4 * i);
19115             }
19116           else if (GET_CODE (operand) == CONST_DOUBLE)
19117             {
19118               REAL_VALUE_TYPE r;
19119               long l[4];
19120
19121               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
19122               switch (mode)
19123                 {
19124                 case TFmode:
19125                   real_to_target (l, &r, mode);
19126                   parts[3] = gen_int_mode (l[3], SImode);
19127                   parts[2] = gen_int_mode (l[2], SImode);
19128                   break;
19129                 case XFmode:
19130                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
19131                   parts[2] = gen_int_mode (l[2], SImode);
19132                   break;
19133                 case DFmode:
19134                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
19135                   break;
19136                 default:
19137                   gcc_unreachable ();
19138                 }
19139               parts[1] = gen_int_mode (l[1], SImode);
19140               parts[0] = gen_int_mode (l[0], SImode);
19141             }
19142           else
19143             gcc_unreachable ();
19144         }
19145     }
19146   else
19147     {
19148       if (mode == TImode)
19149         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
19150       if (mode == XFmode || mode == TFmode)
19151         {
19152           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
19153           if (REG_P (operand))
19154             {
19155               gcc_assert (reload_completed);
19156               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
19157               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
19158             }
19159           else if (offsettable_memref_p (operand))
19160             {
19161               operand = adjust_address (operand, DImode, 0);
19162               parts[0] = operand;
19163               parts[1] = adjust_address (operand, upper_mode, 8);
19164             }
19165           else if (GET_CODE (operand) == CONST_DOUBLE)
19166             {
19167               REAL_VALUE_TYPE r;
19168               long l[4];
19169
19170               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
19171               real_to_target (l, &r, mode);
19172
19173               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
19174               if (HOST_BITS_PER_WIDE_INT >= 64)
19175                 parts[0]
19176                   = gen_int_mode
19177                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
19178                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
19179                        DImode);
19180               else
19181                 parts[0] = immed_double_const (l[0], l[1], DImode);
19182
19183               if (upper_mode == SImode)
19184                 parts[1] = gen_int_mode (l[2], SImode);
19185               else if (HOST_BITS_PER_WIDE_INT >= 64)
19186                 parts[1]
19187                   = gen_int_mode
19188                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
19189                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
19190                        DImode);
19191               else
19192                 parts[1] = immed_double_const (l[2], l[3], DImode);
19193             }
19194           else
19195             gcc_unreachable ();
19196         }
19197     }
19198
19199   return size;
19200 }
19201
19202 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
19203    Return false when normal moves are needed; true when all required
19204    insns have been emitted.  Operands 2-4 contain the input values
19205    int the correct order; operands 5-7 contain the output values.  */
19206
19207 void
19208 ix86_split_long_move (rtx operands[])
19209 {
19210   rtx part[2][4];
19211   int nparts, i, j;
19212   int push = 0;
19213   int collisions = 0;
19214   enum machine_mode mode = GET_MODE (operands[0]);
19215   bool collisionparts[4];
19216
19217   /* The DFmode expanders may ask us to move double.
19218      For 64bit target this is single move.  By hiding the fact
19219      here we simplify i386.md splitters.  */
19220   if (TARGET_64BIT && GET_MODE_SIZE (GET_MODE (operands[0])) == 8)
19221     {
19222       /* Optimize constant pool reference to immediates.  This is used by
19223          fp moves, that force all constants to memory to allow combining.  */
19224
19225       if (MEM_P (operands[1])
19226           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
19227           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
19228         operands[1] = get_pool_constant (XEXP (operands[1], 0));
19229       if (push_operand (operands[0], VOIDmode))
19230         {
19231           operands[0] = copy_rtx (operands[0]);
19232           PUT_MODE (operands[0], Pmode);
19233         }
19234       else
19235         operands[0] = gen_lowpart (DImode, operands[0]);
19236       operands[1] = gen_lowpart (DImode, operands[1]);
19237       emit_move_insn (operands[0], operands[1]);
19238       return;
19239     }
19240
19241   /* The only non-offsettable memory we handle is push.  */
19242   if (push_operand (operands[0], VOIDmode))
19243     push = 1;
19244   else
19245     gcc_assert (!MEM_P (operands[0])
19246                 || offsettable_memref_p (operands[0]));
19247
19248   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
19249   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
19250
19251   /* When emitting push, take care for source operands on the stack.  */
19252   if (push && MEM_P (operands[1])
19253       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
19254     {
19255       rtx src_base = XEXP (part[1][nparts - 1], 0);
19256
19257       /* Compensate for the stack decrement by 4.  */
19258       if (!TARGET_64BIT && nparts == 3
19259           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
19260         src_base = plus_constant (src_base, 4);
19261
19262       /* src_base refers to the stack pointer and is
19263          automatically decreased by emitted push.  */
19264       for (i = 0; i < nparts; i++)
19265         part[1][i] = change_address (part[1][i],
19266                                      GET_MODE (part[1][i]), src_base);
19267     }
19268
19269   /* We need to do copy in the right order in case an address register
19270      of the source overlaps the destination.  */
19271   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
19272     {
19273       rtx tmp;
19274
19275       for (i = 0; i < nparts; i++)
19276         {
19277           collisionparts[i]
19278             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
19279           if (collisionparts[i])
19280             collisions++;
19281         }
19282
19283       /* Collision in the middle part can be handled by reordering.  */
19284       if (collisions == 1 && nparts == 3 && collisionparts [1])
19285         {
19286           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
19287           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
19288         }
19289       else if (collisions == 1
19290                && nparts == 4
19291                && (collisionparts [1] || collisionparts [2]))
19292         {
19293           if (collisionparts [1])
19294             {
19295               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
19296               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
19297             }
19298           else
19299             {
19300               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
19301               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
19302             }
19303         }
19304
19305       /* If there are more collisions, we can't handle it by reordering.
19306          Do an lea to the last part and use only one colliding move.  */
19307       else if (collisions > 1)
19308         {
19309           rtx base;
19310
19311           collisions = 1;
19312
19313           base = part[0][nparts - 1];
19314
19315           /* Handle the case when the last part isn't valid for lea.
19316              Happens in 64-bit mode storing the 12-byte XFmode.  */
19317           if (GET_MODE (base) != Pmode)
19318             base = gen_rtx_REG (Pmode, REGNO (base));
19319
19320           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
19321           part[1][0] = replace_equiv_address (part[1][0], base);
19322           for (i = 1; i < nparts; i++)
19323             {
19324               tmp = plus_constant (base, UNITS_PER_WORD * i);
19325               part[1][i] = replace_equiv_address (part[1][i], tmp);
19326             }
19327         }
19328     }
19329
19330   if (push)
19331     {
19332       if (!TARGET_64BIT)
19333         {
19334           if (nparts == 3)
19335             {
19336               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
19337                 emit_insn (gen_addsi3 (stack_pointer_rtx,
19338                                        stack_pointer_rtx, GEN_INT (-4)));
19339               emit_move_insn (part[0][2], part[1][2]);
19340             }
19341           else if (nparts == 4)
19342             {
19343               emit_move_insn (part[0][3], part[1][3]);
19344               emit_move_insn (part[0][2], part[1][2]);
19345             }
19346         }
19347       else
19348         {
19349           /* In 64bit mode we don't have 32bit push available.  In case this is
19350              register, it is OK - we will just use larger counterpart.  We also
19351              retype memory - these comes from attempt to avoid REX prefix on
19352              moving of second half of TFmode value.  */
19353           if (GET_MODE (part[1][1]) == SImode)
19354             {
19355               switch (GET_CODE (part[1][1]))
19356                 {
19357                 case MEM:
19358                   part[1][1] = adjust_address (part[1][1], DImode, 0);
19359                   break;
19360
19361                 case REG:
19362                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
19363                   break;
19364
19365                 default:
19366                   gcc_unreachable ();
19367                 }
19368
19369               if (GET_MODE (part[1][0]) == SImode)
19370                 part[1][0] = part[1][1];
19371             }
19372         }
19373       emit_move_insn (part[0][1], part[1][1]);
19374       emit_move_insn (part[0][0], part[1][0]);
19375       return;
19376     }
19377
19378   /* Choose correct order to not overwrite the source before it is copied.  */
19379   if ((REG_P (part[0][0])
19380        && REG_P (part[1][1])
19381        && (REGNO (part[0][0]) == REGNO (part[1][1])
19382            || (nparts == 3
19383                && REGNO (part[0][0]) == REGNO (part[1][2]))
19384            || (nparts == 4
19385                && REGNO (part[0][0]) == REGNO (part[1][3]))))
19386       || (collisions > 0
19387           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
19388     {
19389       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
19390         {
19391           operands[2 + i] = part[0][j];
19392           operands[6 + i] = part[1][j];
19393         }
19394     }
19395   else
19396     {
19397       for (i = 0; i < nparts; i++)
19398         {
19399           operands[2 + i] = part[0][i];
19400           operands[6 + i] = part[1][i];
19401         }
19402     }
19403
19404   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
19405   if (optimize_insn_for_size_p ())
19406     {
19407       for (j = 0; j < nparts - 1; j++)
19408         if (CONST_INT_P (operands[6 + j])
19409             && operands[6 + j] != const0_rtx
19410             && REG_P (operands[2 + j]))
19411           for (i = j; i < nparts - 1; i++)
19412             if (CONST_INT_P (operands[7 + i])
19413                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
19414               operands[7 + i] = operands[2 + j];
19415     }
19416
19417   for (i = 0; i < nparts; i++)
19418     emit_move_insn (operands[2 + i], operands[6 + i]);
19419
19420   return;
19421 }
19422
19423 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
19424    left shift by a constant, either using a single shift or
19425    a sequence of add instructions.  */
19426
19427 static void
19428 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
19429 {
19430   rtx (*insn)(rtx, rtx, rtx);
19431
19432   if (count == 1
19433       || (count * ix86_cost->add <= ix86_cost->shift_const
19434           && !optimize_insn_for_size_p ()))
19435     {
19436       insn = mode == DImode ? gen_addsi3 : gen_adddi3;
19437       while (count-- > 0)
19438         emit_insn (insn (operand, operand, operand));
19439     }
19440   else
19441     {
19442       insn = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
19443       emit_insn (insn (operand, operand, GEN_INT (count)));
19444     }
19445 }
19446
19447 void
19448 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
19449 {
19450   rtx (*gen_ashl3)(rtx, rtx, rtx);
19451   rtx (*gen_shld)(rtx, rtx, rtx);
19452   int half_width = GET_MODE_BITSIZE (mode) >> 1;
19453
19454   rtx low[2], high[2];
19455   int count;
19456
19457   if (CONST_INT_P (operands[2]))
19458     {
19459       split_double_mode (mode, operands, 2, low, high);
19460       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
19461
19462       if (count >= half_width)
19463         {
19464           emit_move_insn (high[0], low[1]);
19465           emit_move_insn (low[0], const0_rtx);
19466
19467           if (count > half_width)
19468             ix86_expand_ashl_const (high[0], count - half_width, mode);
19469         }
19470       else
19471         {
19472           gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
19473
19474           if (!rtx_equal_p (operands[0], operands[1]))
19475             emit_move_insn (operands[0], operands[1]);
19476
19477           emit_insn (gen_shld (high[0], low[0], GEN_INT (count)));
19478           ix86_expand_ashl_const (low[0], count, mode);
19479         }
19480       return;
19481     }
19482
19483   split_double_mode (mode, operands, 1, low, high);
19484
19485   gen_ashl3 = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
19486
19487   if (operands[1] == const1_rtx)
19488     {
19489       /* Assuming we've chosen a QImode capable registers, then 1 << N
19490          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
19491       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
19492         {
19493           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
19494
19495           ix86_expand_clear (low[0]);
19496           ix86_expand_clear (high[0]);
19497           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (half_width)));
19498
19499           d = gen_lowpart (QImode, low[0]);
19500           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
19501           s = gen_rtx_EQ (QImode, flags, const0_rtx);
19502           emit_insn (gen_rtx_SET (VOIDmode, d, s));
19503
19504           d = gen_lowpart (QImode, high[0]);
19505           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
19506           s = gen_rtx_NE (QImode, flags, const0_rtx);
19507           emit_insn (gen_rtx_SET (VOIDmode, d, s));
19508         }
19509
19510       /* Otherwise, we can get the same results by manually performing
19511          a bit extract operation on bit 5/6, and then performing the two
19512          shifts.  The two methods of getting 0/1 into low/high are exactly
19513          the same size.  Avoiding the shift in the bit extract case helps
19514          pentium4 a bit; no one else seems to care much either way.  */
19515       else
19516         {
19517           enum machine_mode half_mode;
19518           rtx (*gen_lshr3)(rtx, rtx, rtx);
19519           rtx (*gen_and3)(rtx, rtx, rtx);
19520           rtx (*gen_xor3)(rtx, rtx, rtx);
19521           HOST_WIDE_INT bits;
19522           rtx x;
19523
19524           if (mode == DImode)
19525             {
19526               half_mode = SImode;
19527               gen_lshr3 = gen_lshrsi3;
19528               gen_and3 = gen_andsi3;
19529               gen_xor3 = gen_xorsi3;
19530               bits = 5;
19531             }
19532           else
19533             {
19534               half_mode = DImode;
19535               gen_lshr3 = gen_lshrdi3;
19536               gen_and3 = gen_anddi3;
19537               gen_xor3 = gen_xordi3;
19538               bits = 6;
19539             }
19540
19541           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
19542             x = gen_rtx_ZERO_EXTEND (half_mode, operands[2]);
19543           else
19544             x = gen_lowpart (half_mode, operands[2]);
19545           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
19546
19547           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (bits)));
19548           emit_insn (gen_and3 (high[0], high[0], const1_rtx));
19549           emit_move_insn (low[0], high[0]);
19550           emit_insn (gen_xor3 (low[0], low[0], const1_rtx));
19551         }
19552
19553       emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
19554       emit_insn (gen_ashl3 (high[0], high[0], operands[2]));
19555       return;
19556     }
19557
19558   if (operands[1] == constm1_rtx)
19559     {
19560       /* For -1 << N, we can avoid the shld instruction, because we
19561          know that we're shifting 0...31/63 ones into a -1.  */
19562       emit_move_insn (low[0], constm1_rtx);
19563       if (optimize_insn_for_size_p ())
19564         emit_move_insn (high[0], low[0]);
19565       else
19566         emit_move_insn (high[0], constm1_rtx);
19567     }
19568   else
19569     {
19570       gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
19571
19572       if (!rtx_equal_p (operands[0], operands[1]))
19573         emit_move_insn (operands[0], operands[1]);
19574
19575       split_double_mode (mode, operands, 1, low, high);
19576       emit_insn (gen_shld (high[0], low[0], operands[2]));
19577     }
19578
19579   emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
19580
19581   if (TARGET_CMOVE && scratch)
19582     {
19583       rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
19584         = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
19585
19586       ix86_expand_clear (scratch);
19587       emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2], scratch));
19588     }
19589   else
19590     {
19591       rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
19592         = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
19593
19594       emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
19595     }
19596 }
19597
19598 void
19599 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
19600 {
19601   rtx (*gen_ashr3)(rtx, rtx, rtx)
19602     = mode == DImode ? gen_ashrsi3 : gen_ashrdi3;
19603   rtx (*gen_shrd)(rtx, rtx, rtx);
19604   int half_width = GET_MODE_BITSIZE (mode) >> 1;
19605
19606   rtx low[2], high[2];
19607   int count;
19608
19609   if (CONST_INT_P (operands[2]))
19610     {
19611       split_double_mode (mode, operands, 2, low, high);
19612       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
19613
19614       if (count == GET_MODE_BITSIZE (mode) - 1)
19615         {
19616           emit_move_insn (high[0], high[1]);
19617           emit_insn (gen_ashr3 (high[0], high[0],
19618                                 GEN_INT (half_width - 1)));
19619           emit_move_insn (low[0], high[0]);
19620
19621         }
19622       else if (count >= half_width)
19623         {
19624           emit_move_insn (low[0], high[1]);
19625           emit_move_insn (high[0], low[0]);
19626           emit_insn (gen_ashr3 (high[0], high[0],
19627                                 GEN_INT (half_width - 1)));
19628
19629           if (count > half_width)
19630             emit_insn (gen_ashr3 (low[0], low[0],
19631                                   GEN_INT (count - half_width)));
19632         }
19633       else
19634         {
19635           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19636
19637           if (!rtx_equal_p (operands[0], operands[1]))
19638             emit_move_insn (operands[0], operands[1]);
19639
19640           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
19641           emit_insn (gen_ashr3 (high[0], high[0], GEN_INT (count)));
19642         }
19643     }
19644   else
19645     {
19646       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19647
19648      if (!rtx_equal_p (operands[0], operands[1]))
19649         emit_move_insn (operands[0], operands[1]);
19650
19651       split_double_mode (mode, operands, 1, low, high);
19652
19653       emit_insn (gen_shrd (low[0], high[0], operands[2]));
19654       emit_insn (gen_ashr3 (high[0], high[0], operands[2]));
19655
19656       if (TARGET_CMOVE && scratch)
19657         {
19658           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
19659             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
19660
19661           emit_move_insn (scratch, high[0]);
19662           emit_insn (gen_ashr3 (scratch, scratch,
19663                                 GEN_INT (half_width - 1)));
19664           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
19665                                           scratch));
19666         }
19667       else
19668         {
19669           rtx (*gen_x86_shift_adj_3)(rtx, rtx, rtx)
19670             = mode == DImode ? gen_x86_shiftsi_adj_3 : gen_x86_shiftdi_adj_3;
19671
19672           emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
19673         }
19674     }
19675 }
19676
19677 void
19678 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
19679 {
19680   rtx (*gen_lshr3)(rtx, rtx, rtx)
19681     = mode == DImode ? gen_lshrsi3 : gen_lshrdi3;
19682   rtx (*gen_shrd)(rtx, rtx, rtx);
19683   int half_width = GET_MODE_BITSIZE (mode) >> 1;
19684
19685   rtx low[2], high[2];
19686   int count;
19687
19688   if (CONST_INT_P (operands[2]))
19689     {
19690       split_double_mode (mode, operands, 2, low, high);
19691       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
19692
19693       if (count >= half_width)
19694         {
19695           emit_move_insn (low[0], high[1]);
19696           ix86_expand_clear (high[0]);
19697
19698           if (count > half_width)
19699             emit_insn (gen_lshr3 (low[0], low[0],
19700                                   GEN_INT (count - half_width)));
19701         }
19702       else
19703         {
19704           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19705
19706           if (!rtx_equal_p (operands[0], operands[1]))
19707             emit_move_insn (operands[0], operands[1]);
19708
19709           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
19710           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (count)));
19711         }
19712     }
19713   else
19714     {
19715       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19716
19717       if (!rtx_equal_p (operands[0], operands[1]))
19718         emit_move_insn (operands[0], operands[1]);
19719
19720       split_double_mode (mode, operands, 1, low, high);
19721
19722       emit_insn (gen_shrd (low[0], high[0], operands[2]));
19723       emit_insn (gen_lshr3 (high[0], high[0], operands[2]));
19724
19725       if (TARGET_CMOVE && scratch)
19726         {
19727           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
19728             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
19729
19730           ix86_expand_clear (scratch);
19731           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
19732                                           scratch));
19733         }
19734       else
19735         {
19736           rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
19737             = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
19738
19739           emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
19740         }
19741     }
19742 }
19743
19744 /* Predict just emitted jump instruction to be taken with probability PROB.  */
19745 static void
19746 predict_jump (int prob)
19747 {
19748   rtx insn = get_last_insn ();
19749   gcc_assert (JUMP_P (insn));
19750   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
19751 }
19752
19753 /* Helper function for the string operations below.  Dest VARIABLE whether
19754    it is aligned to VALUE bytes.  If true, jump to the label.  */
19755 static rtx
19756 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
19757 {
19758   rtx label = gen_label_rtx ();
19759   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
19760   if (GET_MODE (variable) == DImode)
19761     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
19762   else
19763     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
19764   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
19765                            1, label);
19766   if (epilogue)
19767     predict_jump (REG_BR_PROB_BASE * 50 / 100);
19768   else
19769     predict_jump (REG_BR_PROB_BASE * 90 / 100);
19770   return label;
19771 }
19772
19773 /* Adjust COUNTER by the VALUE.  */
19774 static void
19775 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
19776 {
19777   rtx (*gen_add)(rtx, rtx, rtx)
19778     = GET_MODE (countreg) == DImode ? gen_adddi3 : gen_addsi3;
19779
19780   emit_insn (gen_add (countreg, countreg, GEN_INT (-value)));
19781 }
19782
19783 /* Zero extend possibly SImode EXP to Pmode register.  */
19784 rtx
19785 ix86_zero_extend_to_Pmode (rtx exp)
19786 {
19787   rtx r;
19788   if (GET_MODE (exp) == VOIDmode)
19789     return force_reg (Pmode, exp);
19790   if (GET_MODE (exp) == Pmode)
19791     return copy_to_mode_reg (Pmode, exp);
19792   r = gen_reg_rtx (Pmode);
19793   emit_insn (gen_zero_extendsidi2 (r, exp));
19794   return r;
19795 }
19796
19797 /* Divide COUNTREG by SCALE.  */
19798 static rtx
19799 scale_counter (rtx countreg, int scale)
19800 {
19801   rtx sc;
19802
19803   if (scale == 1)
19804     return countreg;
19805   if (CONST_INT_P (countreg))
19806     return GEN_INT (INTVAL (countreg) / scale);
19807   gcc_assert (REG_P (countreg));
19808
19809   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
19810                             GEN_INT (exact_log2 (scale)),
19811                             NULL, 1, OPTAB_DIRECT);
19812   return sc;
19813 }
19814
19815 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
19816    DImode for constant loop counts.  */
19817
19818 static enum machine_mode
19819 counter_mode (rtx count_exp)
19820 {
19821   if (GET_MODE (count_exp) != VOIDmode)
19822     return GET_MODE (count_exp);
19823   if (!CONST_INT_P (count_exp))
19824     return Pmode;
19825   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
19826     return DImode;
19827   return SImode;
19828 }
19829
19830 /* When SRCPTR is non-NULL, output simple loop to move memory
19831    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
19832    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
19833    equivalent loop to set memory by VALUE (supposed to be in MODE).
19834
19835    The size is rounded down to whole number of chunk size moved at once.
19836    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
19837
19838
19839 static void
19840 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
19841                                rtx destptr, rtx srcptr, rtx value,
19842                                rtx count, enum machine_mode mode, int unroll,
19843                                int expected_size)
19844 {
19845   rtx out_label, top_label, iter, tmp;
19846   enum machine_mode iter_mode = counter_mode (count);
19847   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
19848   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
19849   rtx size;
19850   rtx x_addr;
19851   rtx y_addr;
19852   int i;
19853
19854   top_label = gen_label_rtx ();
19855   out_label = gen_label_rtx ();
19856   iter = gen_reg_rtx (iter_mode);
19857
19858   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
19859                               NULL, 1, OPTAB_DIRECT);
19860   /* Those two should combine.  */
19861   if (piece_size == const1_rtx)
19862     {
19863       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
19864                                true, out_label);
19865       predict_jump (REG_BR_PROB_BASE * 10 / 100);
19866     }
19867   emit_move_insn (iter, const0_rtx);
19868
19869   emit_label (top_label);
19870
19871   tmp = convert_modes (Pmode, iter_mode, iter, true);
19872   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
19873   destmem = change_address (destmem, mode, x_addr);
19874
19875   if (srcmem)
19876     {
19877       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
19878       srcmem = change_address (srcmem, mode, y_addr);
19879
19880       /* When unrolling for chips that reorder memory reads and writes,
19881          we can save registers by using single temporary.
19882          Also using 4 temporaries is overkill in 32bit mode.  */
19883       if (!TARGET_64BIT && 0)
19884         {
19885           for (i = 0; i < unroll; i++)
19886             {
19887               if (i)
19888                 {
19889                   destmem =
19890                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19891                   srcmem =
19892                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
19893                 }
19894               emit_move_insn (destmem, srcmem);
19895             }
19896         }
19897       else
19898         {
19899           rtx tmpreg[4];
19900           gcc_assert (unroll <= 4);
19901           for (i = 0; i < unroll; i++)
19902             {
19903               tmpreg[i] = gen_reg_rtx (mode);
19904               if (i)
19905                 {
19906                   srcmem =
19907                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
19908                 }
19909               emit_move_insn (tmpreg[i], srcmem);
19910             }
19911           for (i = 0; i < unroll; i++)
19912             {
19913               if (i)
19914                 {
19915                   destmem =
19916                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19917                 }
19918               emit_move_insn (destmem, tmpreg[i]);
19919             }
19920         }
19921     }
19922   else
19923     for (i = 0; i < unroll; i++)
19924       {
19925         if (i)
19926           destmem =
19927             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19928         emit_move_insn (destmem, value);
19929       }
19930
19931   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
19932                              true, OPTAB_LIB_WIDEN);
19933   if (tmp != iter)
19934     emit_move_insn (iter, tmp);
19935
19936   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
19937                            true, top_label);
19938   if (expected_size != -1)
19939     {
19940       expected_size /= GET_MODE_SIZE (mode) * unroll;
19941       if (expected_size == 0)
19942         predict_jump (0);
19943       else if (expected_size > REG_BR_PROB_BASE)
19944         predict_jump (REG_BR_PROB_BASE - 1);
19945       else
19946         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
19947     }
19948   else
19949     predict_jump (REG_BR_PROB_BASE * 80 / 100);
19950   iter = ix86_zero_extend_to_Pmode (iter);
19951   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
19952                              true, OPTAB_LIB_WIDEN);
19953   if (tmp != destptr)
19954     emit_move_insn (destptr, tmp);
19955   if (srcptr)
19956     {
19957       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
19958                                  true, OPTAB_LIB_WIDEN);
19959       if (tmp != srcptr)
19960         emit_move_insn (srcptr, tmp);
19961     }
19962   emit_label (out_label);
19963 }
19964
19965 /* Output "rep; mov" instruction.
19966    Arguments have same meaning as for previous function */
19967 static void
19968 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
19969                            rtx destptr, rtx srcptr,
19970                            rtx count,
19971                            enum machine_mode mode)
19972 {
19973   rtx destexp;
19974   rtx srcexp;
19975   rtx countreg;
19976
19977   /* If the size is known, it is shorter to use rep movs.  */
19978   if (mode == QImode && CONST_INT_P (count)
19979       && !(INTVAL (count) & 3))
19980     mode = SImode;
19981
19982   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
19983     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
19984   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
19985     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
19986   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
19987   if (mode != QImode)
19988     {
19989       destexp = gen_rtx_ASHIFT (Pmode, countreg,
19990                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19991       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
19992       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
19993                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19994       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
19995     }
19996   else
19997     {
19998       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
19999       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
20000     }
20001   if (CONST_INT_P (count))
20002     {
20003       count = GEN_INT (INTVAL (count)
20004                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
20005       destmem = shallow_copy_rtx (destmem);
20006       srcmem = shallow_copy_rtx (srcmem);
20007       set_mem_size (destmem, count);
20008       set_mem_size (srcmem, count);
20009     }
20010   else
20011     {
20012       if (MEM_SIZE (destmem))
20013         set_mem_size (destmem, NULL_RTX);
20014       if (MEM_SIZE (srcmem))
20015         set_mem_size (srcmem, NULL_RTX);
20016     }
20017   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
20018                           destexp, srcexp));
20019 }
20020
20021 /* Output "rep; stos" instruction.
20022    Arguments have same meaning as for previous function */
20023 static void
20024 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
20025                             rtx count, enum machine_mode mode,
20026                             rtx orig_value)
20027 {
20028   rtx destexp;
20029   rtx countreg;
20030
20031   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
20032     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
20033   value = force_reg (mode, gen_lowpart (mode, value));
20034   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
20035   if (mode != QImode)
20036     {
20037       destexp = gen_rtx_ASHIFT (Pmode, countreg,
20038                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
20039       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
20040     }
20041   else
20042     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
20043   if (orig_value == const0_rtx && CONST_INT_P (count))
20044     {
20045       count = GEN_INT (INTVAL (count)
20046                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
20047       destmem = shallow_copy_rtx (destmem);
20048       set_mem_size (destmem, count);
20049     }
20050   else if (MEM_SIZE (destmem))
20051     set_mem_size (destmem, NULL_RTX);
20052   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
20053 }
20054
20055 static void
20056 emit_strmov (rtx destmem, rtx srcmem,
20057              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
20058 {
20059   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
20060   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
20061   emit_insn (gen_strmov (destptr, dest, srcptr, src));
20062 }
20063
20064 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
20065 static void
20066 expand_movmem_epilogue (rtx destmem, rtx srcmem,
20067                         rtx destptr, rtx srcptr, rtx count, int max_size)
20068 {
20069   rtx src, dest;
20070   if (CONST_INT_P (count))
20071     {
20072       HOST_WIDE_INT countval = INTVAL (count);
20073       int offset = 0;
20074
20075       if ((countval & 0x10) && max_size > 16)
20076         {
20077           if (TARGET_64BIT)
20078             {
20079               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
20080               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
20081             }
20082           else
20083             gcc_unreachable ();
20084           offset += 16;
20085         }
20086       if ((countval & 0x08) && max_size > 8)
20087         {
20088           if (TARGET_64BIT)
20089             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
20090           else
20091             {
20092               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
20093               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
20094             }
20095           offset += 8;
20096         }
20097       if ((countval & 0x04) && max_size > 4)
20098         {
20099           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
20100           offset += 4;
20101         }
20102       if ((countval & 0x02) && max_size > 2)
20103         {
20104           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
20105           offset += 2;
20106         }
20107       if ((countval & 0x01) && max_size > 1)
20108         {
20109           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
20110           offset += 1;
20111         }
20112       return;
20113     }
20114   if (max_size > 8)
20115     {
20116       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
20117                                     count, 1, OPTAB_DIRECT);
20118       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
20119                                      count, QImode, 1, 4);
20120       return;
20121     }
20122
20123   /* When there are stringops, we can cheaply increase dest and src pointers.
20124      Otherwise we save code size by maintaining offset (zero is readily
20125      available from preceding rep operation) and using x86 addressing modes.
20126    */
20127   if (TARGET_SINGLE_STRINGOP)
20128     {
20129       if (max_size > 4)
20130         {
20131           rtx label = ix86_expand_aligntest (count, 4, true);
20132           src = change_address (srcmem, SImode, srcptr);
20133           dest = change_address (destmem, SImode, destptr);
20134           emit_insn (gen_strmov (destptr, dest, srcptr, src));
20135           emit_label (label);
20136           LABEL_NUSES (label) = 1;
20137         }
20138       if (max_size > 2)
20139         {
20140           rtx label = ix86_expand_aligntest (count, 2, true);
20141           src = change_address (srcmem, HImode, srcptr);
20142           dest = change_address (destmem, HImode, destptr);
20143           emit_insn (gen_strmov (destptr, dest, srcptr, src));
20144           emit_label (label);
20145           LABEL_NUSES (label) = 1;
20146         }
20147       if (max_size > 1)
20148         {
20149           rtx label = ix86_expand_aligntest (count, 1, true);
20150           src = change_address (srcmem, QImode, srcptr);
20151           dest = change_address (destmem, QImode, destptr);
20152           emit_insn (gen_strmov (destptr, dest, srcptr, src));
20153           emit_label (label);
20154           LABEL_NUSES (label) = 1;
20155         }
20156     }
20157   else
20158     {
20159       rtx offset = force_reg (Pmode, const0_rtx);
20160       rtx tmp;
20161
20162       if (max_size > 4)
20163         {
20164           rtx label = ix86_expand_aligntest (count, 4, true);
20165           src = change_address (srcmem, SImode, srcptr);
20166           dest = change_address (destmem, SImode, destptr);
20167           emit_move_insn (dest, src);
20168           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
20169                                      true, OPTAB_LIB_WIDEN);
20170           if (tmp != offset)
20171             emit_move_insn (offset, tmp);
20172           emit_label (label);
20173           LABEL_NUSES (label) = 1;
20174         }
20175       if (max_size > 2)
20176         {
20177           rtx label = ix86_expand_aligntest (count, 2, true);
20178           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
20179           src = change_address (srcmem, HImode, tmp);
20180           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
20181           dest = change_address (destmem, HImode, tmp);
20182           emit_move_insn (dest, src);
20183           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
20184                                      true, OPTAB_LIB_WIDEN);
20185           if (tmp != offset)
20186             emit_move_insn (offset, tmp);
20187           emit_label (label);
20188           LABEL_NUSES (label) = 1;
20189         }
20190       if (max_size > 1)
20191         {
20192           rtx label = ix86_expand_aligntest (count, 1, true);
20193           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
20194           src = change_address (srcmem, QImode, tmp);
20195           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
20196           dest = change_address (destmem, QImode, tmp);
20197           emit_move_insn (dest, src);
20198           emit_label (label);
20199           LABEL_NUSES (label) = 1;
20200         }
20201     }
20202 }
20203
20204 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
20205 static void
20206 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
20207                                  rtx count, int max_size)
20208 {
20209   count =
20210     expand_simple_binop (counter_mode (count), AND, count,
20211                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
20212   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
20213                                  gen_lowpart (QImode, value), count, QImode,
20214                                  1, max_size / 2);
20215 }
20216
20217 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
20218 static void
20219 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
20220 {
20221   rtx dest;
20222
20223   if (CONST_INT_P (count))
20224     {
20225       HOST_WIDE_INT countval = INTVAL (count);
20226       int offset = 0;
20227
20228       if ((countval & 0x10) && max_size > 16)
20229         {
20230           if (TARGET_64BIT)
20231             {
20232               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
20233               emit_insn (gen_strset (destptr, dest, value));
20234               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
20235               emit_insn (gen_strset (destptr, dest, value));
20236             }
20237           else
20238             gcc_unreachable ();
20239           offset += 16;
20240         }
20241       if ((countval & 0x08) && max_size > 8)
20242         {
20243           if (TARGET_64BIT)
20244             {
20245               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
20246               emit_insn (gen_strset (destptr, dest, value));
20247             }
20248           else
20249             {
20250               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
20251               emit_insn (gen_strset (destptr, dest, value));
20252               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
20253               emit_insn (gen_strset (destptr, dest, value));
20254             }
20255           offset += 8;
20256         }
20257       if ((countval & 0x04) && max_size > 4)
20258         {
20259           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
20260           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
20261           offset += 4;
20262         }
20263       if ((countval & 0x02) && max_size > 2)
20264         {
20265           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
20266           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
20267           offset += 2;
20268         }
20269       if ((countval & 0x01) && max_size > 1)
20270         {
20271           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
20272           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
20273           offset += 1;
20274         }
20275       return;
20276     }
20277   if (max_size > 32)
20278     {
20279       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
20280       return;
20281     }
20282   if (max_size > 16)
20283     {
20284       rtx label = ix86_expand_aligntest (count, 16, true);
20285       if (TARGET_64BIT)
20286         {
20287           dest = change_address (destmem, DImode, destptr);
20288           emit_insn (gen_strset (destptr, dest, value));
20289           emit_insn (gen_strset (destptr, dest, value));
20290         }
20291       else
20292         {
20293           dest = change_address (destmem, SImode, destptr);
20294           emit_insn (gen_strset (destptr, dest, value));
20295           emit_insn (gen_strset (destptr, dest, value));
20296           emit_insn (gen_strset (destptr, dest, value));
20297           emit_insn (gen_strset (destptr, dest, value));
20298         }
20299       emit_label (label);
20300       LABEL_NUSES (label) = 1;
20301     }
20302   if (max_size > 8)
20303     {
20304       rtx label = ix86_expand_aligntest (count, 8, true);
20305       if (TARGET_64BIT)
20306         {
20307           dest = change_address (destmem, DImode, destptr);
20308           emit_insn (gen_strset (destptr, dest, value));
20309         }
20310       else
20311         {
20312           dest = change_address (destmem, SImode, destptr);
20313           emit_insn (gen_strset (destptr, dest, value));
20314           emit_insn (gen_strset (destptr, dest, value));
20315         }
20316       emit_label (label);
20317       LABEL_NUSES (label) = 1;
20318     }
20319   if (max_size > 4)
20320     {
20321       rtx label = ix86_expand_aligntest (count, 4, true);
20322       dest = change_address (destmem, SImode, destptr);
20323       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
20324       emit_label (label);
20325       LABEL_NUSES (label) = 1;
20326     }
20327   if (max_size > 2)
20328     {
20329       rtx label = ix86_expand_aligntest (count, 2, true);
20330       dest = change_address (destmem, HImode, destptr);
20331       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
20332       emit_label (label);
20333       LABEL_NUSES (label) = 1;
20334     }
20335   if (max_size > 1)
20336     {
20337       rtx label = ix86_expand_aligntest (count, 1, true);
20338       dest = change_address (destmem, QImode, destptr);
20339       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
20340       emit_label (label);
20341       LABEL_NUSES (label) = 1;
20342     }
20343 }
20344
20345 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
20346    DESIRED_ALIGNMENT.  */
20347 static void
20348 expand_movmem_prologue (rtx destmem, rtx srcmem,
20349                         rtx destptr, rtx srcptr, rtx count,
20350                         int align, int desired_alignment)
20351 {
20352   if (align <= 1 && desired_alignment > 1)
20353     {
20354       rtx label = ix86_expand_aligntest (destptr, 1, false);
20355       srcmem = change_address (srcmem, QImode, srcptr);
20356       destmem = change_address (destmem, QImode, destptr);
20357       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
20358       ix86_adjust_counter (count, 1);
20359       emit_label (label);
20360       LABEL_NUSES (label) = 1;
20361     }
20362   if (align <= 2 && desired_alignment > 2)
20363     {
20364       rtx label = ix86_expand_aligntest (destptr, 2, false);
20365       srcmem = change_address (srcmem, HImode, srcptr);
20366       destmem = change_address (destmem, HImode, destptr);
20367       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
20368       ix86_adjust_counter (count, 2);
20369       emit_label (label);
20370       LABEL_NUSES (label) = 1;
20371     }
20372   if (align <= 4 && desired_alignment > 4)
20373     {
20374       rtx label = ix86_expand_aligntest (destptr, 4, false);
20375       srcmem = change_address (srcmem, SImode, srcptr);
20376       destmem = change_address (destmem, SImode, destptr);
20377       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
20378       ix86_adjust_counter (count, 4);
20379       emit_label (label);
20380       LABEL_NUSES (label) = 1;
20381     }
20382   gcc_assert (desired_alignment <= 8);
20383 }
20384
20385 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
20386    ALIGN_BYTES is how many bytes need to be copied.  */
20387 static rtx
20388 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
20389                                  int desired_align, int align_bytes)
20390 {
20391   rtx src = *srcp;
20392   rtx src_size, dst_size;
20393   int off = 0;
20394   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
20395   if (src_align_bytes >= 0)
20396     src_align_bytes = desired_align - src_align_bytes;
20397   src_size = MEM_SIZE (src);
20398   dst_size = MEM_SIZE (dst);
20399   if (align_bytes & 1)
20400     {
20401       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
20402       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
20403       off = 1;
20404       emit_insn (gen_strmov (destreg, dst, srcreg, src));
20405     }
20406   if (align_bytes & 2)
20407     {
20408       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
20409       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
20410       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
20411         set_mem_align (dst, 2 * BITS_PER_UNIT);
20412       if (src_align_bytes >= 0
20413           && (src_align_bytes & 1) == (align_bytes & 1)
20414           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
20415         set_mem_align (src, 2 * BITS_PER_UNIT);
20416       off = 2;
20417       emit_insn (gen_strmov (destreg, dst, srcreg, src));
20418     }
20419   if (align_bytes & 4)
20420     {
20421       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
20422       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
20423       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
20424         set_mem_align (dst, 4 * BITS_PER_UNIT);
20425       if (src_align_bytes >= 0)
20426         {
20427           unsigned int src_align = 0;
20428           if ((src_align_bytes & 3) == (align_bytes & 3))
20429             src_align = 4;
20430           else if ((src_align_bytes & 1) == (align_bytes & 1))
20431             src_align = 2;
20432           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
20433             set_mem_align (src, src_align * BITS_PER_UNIT);
20434         }
20435       off = 4;
20436       emit_insn (gen_strmov (destreg, dst, srcreg, src));
20437     }
20438   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
20439   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
20440   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
20441     set_mem_align (dst, desired_align * BITS_PER_UNIT);
20442   if (src_align_bytes >= 0)
20443     {
20444       unsigned int src_align = 0;
20445       if ((src_align_bytes & 7) == (align_bytes & 7))
20446         src_align = 8;
20447       else if ((src_align_bytes & 3) == (align_bytes & 3))
20448         src_align = 4;
20449       else if ((src_align_bytes & 1) == (align_bytes & 1))
20450         src_align = 2;
20451       if (src_align > (unsigned int) desired_align)
20452         src_align = desired_align;
20453       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
20454         set_mem_align (src, src_align * BITS_PER_UNIT);
20455     }
20456   if (dst_size)
20457     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
20458   if (src_size)
20459     set_mem_size (dst, GEN_INT (INTVAL (src_size) - align_bytes));
20460   *srcp = src;
20461   return dst;
20462 }
20463
20464 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
20465    DESIRED_ALIGNMENT.  */
20466 static void
20467 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
20468                         int align, int desired_alignment)
20469 {
20470   if (align <= 1 && desired_alignment > 1)
20471     {
20472       rtx label = ix86_expand_aligntest (destptr, 1, false);
20473       destmem = change_address (destmem, QImode, destptr);
20474       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
20475       ix86_adjust_counter (count, 1);
20476       emit_label (label);
20477       LABEL_NUSES (label) = 1;
20478     }
20479   if (align <= 2 && desired_alignment > 2)
20480     {
20481       rtx label = ix86_expand_aligntest (destptr, 2, false);
20482       destmem = change_address (destmem, HImode, destptr);
20483       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
20484       ix86_adjust_counter (count, 2);
20485       emit_label (label);
20486       LABEL_NUSES (label) = 1;
20487     }
20488   if (align <= 4 && desired_alignment > 4)
20489     {
20490       rtx label = ix86_expand_aligntest (destptr, 4, false);
20491       destmem = change_address (destmem, SImode, destptr);
20492       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
20493       ix86_adjust_counter (count, 4);
20494       emit_label (label);
20495       LABEL_NUSES (label) = 1;
20496     }
20497   gcc_assert (desired_alignment <= 8);
20498 }
20499
20500 /* Set enough from DST to align DST known to by aligned by ALIGN to
20501    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
20502 static rtx
20503 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
20504                                  int desired_align, int align_bytes)
20505 {
20506   int off = 0;
20507   rtx dst_size = MEM_SIZE (dst);
20508   if (align_bytes & 1)
20509     {
20510       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
20511       off = 1;
20512       emit_insn (gen_strset (destreg, dst,
20513                              gen_lowpart (QImode, value)));
20514     }
20515   if (align_bytes & 2)
20516     {
20517       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
20518       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
20519         set_mem_align (dst, 2 * BITS_PER_UNIT);
20520       off = 2;
20521       emit_insn (gen_strset (destreg, dst,
20522                              gen_lowpart (HImode, value)));
20523     }
20524   if (align_bytes & 4)
20525     {
20526       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
20527       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
20528         set_mem_align (dst, 4 * BITS_PER_UNIT);
20529       off = 4;
20530       emit_insn (gen_strset (destreg, dst,
20531                              gen_lowpart (SImode, value)));
20532     }
20533   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
20534   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
20535     set_mem_align (dst, desired_align * BITS_PER_UNIT);
20536   if (dst_size)
20537     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
20538   return dst;
20539 }
20540
20541 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
20542 static enum stringop_alg
20543 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
20544             int *dynamic_check)
20545 {
20546   const struct stringop_algs * algs;
20547   bool optimize_for_speed;
20548   /* Algorithms using the rep prefix want at least edi and ecx;
20549      additionally, memset wants eax and memcpy wants esi.  Don't
20550      consider such algorithms if the user has appropriated those
20551      registers for their own purposes.  */
20552   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
20553                              || (memset
20554                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
20555
20556 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
20557                            || (alg != rep_prefix_1_byte         \
20558                                && alg != rep_prefix_4_byte      \
20559                                && alg != rep_prefix_8_byte))
20560   const struct processor_costs *cost;
20561
20562   /* Even if the string operation call is cold, we still might spend a lot
20563      of time processing large blocks.  */
20564   if (optimize_function_for_size_p (cfun)
20565       || (optimize_insn_for_size_p ()
20566           && expected_size != -1 && expected_size < 256))
20567     optimize_for_speed = false;
20568   else
20569     optimize_for_speed = true;
20570
20571   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
20572
20573   *dynamic_check = -1;
20574   if (memset)
20575     algs = &cost->memset[TARGET_64BIT != 0];
20576   else
20577     algs = &cost->memcpy[TARGET_64BIT != 0];
20578   if (stringop_alg != no_stringop && ALG_USABLE_P (stringop_alg))
20579     return stringop_alg;
20580   /* rep; movq or rep; movl is the smallest variant.  */
20581   else if (!optimize_for_speed)
20582     {
20583       if (!count || (count & 3))
20584         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
20585       else
20586         return rep_prefix_usable ? rep_prefix_4_byte : loop;
20587     }
20588   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
20589    */
20590   else if (expected_size != -1 && expected_size < 4)
20591     return loop_1_byte;
20592   else if (expected_size != -1)
20593     {
20594       unsigned int i;
20595       enum stringop_alg alg = libcall;
20596       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
20597         {
20598           /* We get here if the algorithms that were not libcall-based
20599              were rep-prefix based and we are unable to use rep prefixes
20600              based on global register usage.  Break out of the loop and
20601              use the heuristic below.  */
20602           if (algs->size[i].max == 0)
20603             break;
20604           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
20605             {
20606               enum stringop_alg candidate = algs->size[i].alg;
20607
20608               if (candidate != libcall && ALG_USABLE_P (candidate))
20609                 alg = candidate;
20610               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
20611                  last non-libcall inline algorithm.  */
20612               if (TARGET_INLINE_ALL_STRINGOPS)
20613                 {
20614                   /* When the current size is best to be copied by a libcall,
20615                      but we are still forced to inline, run the heuristic below
20616                      that will pick code for medium sized blocks.  */
20617                   if (alg != libcall)
20618                     return alg;
20619                   break;
20620                 }
20621               else if (ALG_USABLE_P (candidate))
20622                 return candidate;
20623             }
20624         }
20625       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
20626     }
20627   /* When asked to inline the call anyway, try to pick meaningful choice.
20628      We look for maximal size of block that is faster to copy by hand and
20629      take blocks of at most of that size guessing that average size will
20630      be roughly half of the block.
20631
20632      If this turns out to be bad, we might simply specify the preferred
20633      choice in ix86_costs.  */
20634   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
20635       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
20636     {
20637       int max = -1;
20638       enum stringop_alg alg;
20639       int i;
20640       bool any_alg_usable_p = true;
20641
20642       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
20643         {
20644           enum stringop_alg candidate = algs->size[i].alg;
20645           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
20646
20647           if (candidate != libcall && candidate
20648               && ALG_USABLE_P (candidate))
20649               max = algs->size[i].max;
20650         }
20651       /* If there aren't any usable algorithms, then recursing on
20652          smaller sizes isn't going to find anything.  Just return the
20653          simple byte-at-a-time copy loop.  */
20654       if (!any_alg_usable_p)
20655         {
20656           /* Pick something reasonable.  */
20657           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
20658             *dynamic_check = 128;
20659           return loop_1_byte;
20660         }
20661       if (max == -1)
20662         max = 4096;
20663       alg = decide_alg (count, max / 2, memset, dynamic_check);
20664       gcc_assert (*dynamic_check == -1);
20665       gcc_assert (alg != libcall);
20666       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
20667         *dynamic_check = max;
20668       return alg;
20669     }
20670   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
20671 #undef ALG_USABLE_P
20672 }
20673
20674 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
20675    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
20676 static int
20677 decide_alignment (int align,
20678                   enum stringop_alg alg,
20679                   int expected_size)
20680 {
20681   int desired_align = 0;
20682   switch (alg)
20683     {
20684       case no_stringop:
20685         gcc_unreachable ();
20686       case loop:
20687       case unrolled_loop:
20688         desired_align = GET_MODE_SIZE (Pmode);
20689         break;
20690       case rep_prefix_8_byte:
20691         desired_align = 8;
20692         break;
20693       case rep_prefix_4_byte:
20694         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
20695            copying whole cacheline at once.  */
20696         if (TARGET_PENTIUMPRO)
20697           desired_align = 8;
20698         else
20699           desired_align = 4;
20700         break;
20701       case rep_prefix_1_byte:
20702         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
20703            copying whole cacheline at once.  */
20704         if (TARGET_PENTIUMPRO)
20705           desired_align = 8;
20706         else
20707           desired_align = 1;
20708         break;
20709       case loop_1_byte:
20710         desired_align = 1;
20711         break;
20712       case libcall:
20713         return 0;
20714     }
20715
20716   if (optimize_size)
20717     desired_align = 1;
20718   if (desired_align < align)
20719     desired_align = align;
20720   if (expected_size != -1 && expected_size < 4)
20721     desired_align = align;
20722   return desired_align;
20723 }
20724
20725 /* Return the smallest power of 2 greater than VAL.  */
20726 static int
20727 smallest_pow2_greater_than (int val)
20728 {
20729   int ret = 1;
20730   while (ret <= val)
20731     ret <<= 1;
20732   return ret;
20733 }
20734
20735 /* Expand string move (memcpy) operation.  Use i386 string operations when
20736    profitable.  expand_setmem contains similar code.  The code depends upon
20737    architecture, block size and alignment, but always has the same
20738    overall structure:
20739
20740    1) Prologue guard: Conditional that jumps up to epilogues for small
20741       blocks that can be handled by epilogue alone.  This is faster but
20742       also needed for correctness, since prologue assume the block is larger
20743       than the desired alignment.
20744
20745       Optional dynamic check for size and libcall for large
20746       blocks is emitted here too, with -minline-stringops-dynamically.
20747
20748    2) Prologue: copy first few bytes in order to get destination aligned
20749       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
20750       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
20751       We emit either a jump tree on power of two sized blocks, or a byte loop.
20752
20753    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
20754       with specified algorithm.
20755
20756    4) Epilogue: code copying tail of the block that is too small to be
20757       handled by main body (or up to size guarded by prologue guard).  */
20758
20759 bool
20760 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
20761                     rtx expected_align_exp, rtx expected_size_exp)
20762 {
20763   rtx destreg;
20764   rtx srcreg;
20765   rtx label = NULL;
20766   rtx tmp;
20767   rtx jump_around_label = NULL;
20768   HOST_WIDE_INT align = 1;
20769   unsigned HOST_WIDE_INT count = 0;
20770   HOST_WIDE_INT expected_size = -1;
20771   int size_needed = 0, epilogue_size_needed;
20772   int desired_align = 0, align_bytes = 0;
20773   enum stringop_alg alg;
20774   int dynamic_check;
20775   bool need_zero_guard = false;
20776
20777   if (CONST_INT_P (align_exp))
20778     align = INTVAL (align_exp);
20779   /* i386 can do misaligned access on reasonably increased cost.  */
20780   if (CONST_INT_P (expected_align_exp)
20781       && INTVAL (expected_align_exp) > align)
20782     align = INTVAL (expected_align_exp);
20783   /* ALIGN is the minimum of destination and source alignment, but we care here
20784      just about destination alignment.  */
20785   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
20786     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
20787
20788   if (CONST_INT_P (count_exp))
20789     count = expected_size = INTVAL (count_exp);
20790   if (CONST_INT_P (expected_size_exp) && count == 0)
20791     expected_size = INTVAL (expected_size_exp);
20792
20793   /* Make sure we don't need to care about overflow later on.  */
20794   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
20795     return false;
20796
20797   /* Step 0: Decide on preferred algorithm, desired alignment and
20798      size of chunks to be copied by main loop.  */
20799
20800   alg = decide_alg (count, expected_size, false, &dynamic_check);
20801   desired_align = decide_alignment (align, alg, expected_size);
20802
20803   if (!TARGET_ALIGN_STRINGOPS)
20804     align = desired_align;
20805
20806   if (alg == libcall)
20807     return false;
20808   gcc_assert (alg != no_stringop);
20809   if (!count)
20810     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
20811   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
20812   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
20813   switch (alg)
20814     {
20815     case libcall:
20816     case no_stringop:
20817       gcc_unreachable ();
20818     case loop:
20819       need_zero_guard = true;
20820       size_needed = GET_MODE_SIZE (Pmode);
20821       break;
20822     case unrolled_loop:
20823       need_zero_guard = true;
20824       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
20825       break;
20826     case rep_prefix_8_byte:
20827       size_needed = 8;
20828       break;
20829     case rep_prefix_4_byte:
20830       size_needed = 4;
20831       break;
20832     case rep_prefix_1_byte:
20833       size_needed = 1;
20834       break;
20835     case loop_1_byte:
20836       need_zero_guard = true;
20837       size_needed = 1;
20838       break;
20839     }
20840
20841   epilogue_size_needed = size_needed;
20842
20843   /* Step 1: Prologue guard.  */
20844
20845   /* Alignment code needs count to be in register.  */
20846   if (CONST_INT_P (count_exp) && desired_align > align)
20847     {
20848       if (INTVAL (count_exp) > desired_align
20849           && INTVAL (count_exp) > size_needed)
20850         {
20851           align_bytes
20852             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
20853           if (align_bytes <= 0)
20854             align_bytes = 0;
20855           else
20856             align_bytes = desired_align - align_bytes;
20857         }
20858       if (align_bytes == 0)
20859         count_exp = force_reg (counter_mode (count_exp), count_exp);
20860     }
20861   gcc_assert (desired_align >= 1 && align >= 1);
20862
20863   /* Ensure that alignment prologue won't copy past end of block.  */
20864   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
20865     {
20866       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
20867       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
20868          Make sure it is power of 2.  */
20869       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
20870
20871       if (count)
20872         {
20873           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
20874             {
20875               /* If main algorithm works on QImode, no epilogue is needed.
20876                  For small sizes just don't align anything.  */
20877               if (size_needed == 1)
20878                 desired_align = align;
20879               else
20880                 goto epilogue;
20881             }
20882         }
20883       else
20884         {
20885           label = gen_label_rtx ();
20886           emit_cmp_and_jump_insns (count_exp,
20887                                    GEN_INT (epilogue_size_needed),
20888                                    LTU, 0, counter_mode (count_exp), 1, label);
20889           if (expected_size == -1 || expected_size < epilogue_size_needed)
20890             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20891           else
20892             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20893         }
20894     }
20895
20896   /* Emit code to decide on runtime whether library call or inline should be
20897      used.  */
20898   if (dynamic_check != -1)
20899     {
20900       if (CONST_INT_P (count_exp))
20901         {
20902           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
20903             {
20904               emit_block_move_via_libcall (dst, src, count_exp, false);
20905               count_exp = const0_rtx;
20906               goto epilogue;
20907             }
20908         }
20909       else
20910         {
20911           rtx hot_label = gen_label_rtx ();
20912           jump_around_label = gen_label_rtx ();
20913           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
20914                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
20915           predict_jump (REG_BR_PROB_BASE * 90 / 100);
20916           emit_block_move_via_libcall (dst, src, count_exp, false);
20917           emit_jump (jump_around_label);
20918           emit_label (hot_label);
20919         }
20920     }
20921
20922   /* Step 2: Alignment prologue.  */
20923
20924   if (desired_align > align)
20925     {
20926       if (align_bytes == 0)
20927         {
20928           /* Except for the first move in epilogue, we no longer know
20929              constant offset in aliasing info.  It don't seems to worth
20930              the pain to maintain it for the first move, so throw away
20931              the info early.  */
20932           src = change_address (src, BLKmode, srcreg);
20933           dst = change_address (dst, BLKmode, destreg);
20934           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
20935                                   desired_align);
20936         }
20937       else
20938         {
20939           /* If we know how many bytes need to be stored before dst is
20940              sufficiently aligned, maintain aliasing info accurately.  */
20941           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
20942                                                  desired_align, align_bytes);
20943           count_exp = plus_constant (count_exp, -align_bytes);
20944           count -= align_bytes;
20945         }
20946       if (need_zero_guard
20947           && (count < (unsigned HOST_WIDE_INT) size_needed
20948               || (align_bytes == 0
20949                   && count < ((unsigned HOST_WIDE_INT) size_needed
20950                               + desired_align - align))))
20951         {
20952           /* It is possible that we copied enough so the main loop will not
20953              execute.  */
20954           gcc_assert (size_needed > 1);
20955           if (label == NULL_RTX)
20956             label = gen_label_rtx ();
20957           emit_cmp_and_jump_insns (count_exp,
20958                                    GEN_INT (size_needed),
20959                                    LTU, 0, counter_mode (count_exp), 1, label);
20960           if (expected_size == -1
20961               || expected_size < (desired_align - align) / 2 + size_needed)
20962             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20963           else
20964             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20965         }
20966     }
20967   if (label && size_needed == 1)
20968     {
20969       emit_label (label);
20970       LABEL_NUSES (label) = 1;
20971       label = NULL;
20972       epilogue_size_needed = 1;
20973     }
20974   else if (label == NULL_RTX)
20975     epilogue_size_needed = size_needed;
20976
20977   /* Step 3: Main loop.  */
20978
20979   switch (alg)
20980     {
20981     case libcall:
20982     case no_stringop:
20983       gcc_unreachable ();
20984     case loop_1_byte:
20985       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20986                                      count_exp, QImode, 1, expected_size);
20987       break;
20988     case loop:
20989       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20990                                      count_exp, Pmode, 1, expected_size);
20991       break;
20992     case unrolled_loop:
20993       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
20994          registers for 4 temporaries anyway.  */
20995       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20996                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
20997                                      expected_size);
20998       break;
20999     case rep_prefix_8_byte:
21000       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
21001                                  DImode);
21002       break;
21003     case rep_prefix_4_byte:
21004       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
21005                                  SImode);
21006       break;
21007     case rep_prefix_1_byte:
21008       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
21009                                  QImode);
21010       break;
21011     }
21012   /* Adjust properly the offset of src and dest memory for aliasing.  */
21013   if (CONST_INT_P (count_exp))
21014     {
21015       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
21016                                           (count / size_needed) * size_needed);
21017       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
21018                                           (count / size_needed) * size_needed);
21019     }
21020   else
21021     {
21022       src = change_address (src, BLKmode, srcreg);
21023       dst = change_address (dst, BLKmode, destreg);
21024     }
21025
21026   /* Step 4: Epilogue to copy the remaining bytes.  */
21027  epilogue:
21028   if (label)
21029     {
21030       /* When the main loop is done, COUNT_EXP might hold original count,
21031          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
21032          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
21033          bytes. Compensate if needed.  */
21034
21035       if (size_needed < epilogue_size_needed)
21036         {
21037           tmp =
21038             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
21039                                  GEN_INT (size_needed - 1), count_exp, 1,
21040                                  OPTAB_DIRECT);
21041           if (tmp != count_exp)
21042             emit_move_insn (count_exp, tmp);
21043         }
21044       emit_label (label);
21045       LABEL_NUSES (label) = 1;
21046     }
21047
21048   if (count_exp != const0_rtx && epilogue_size_needed > 1)
21049     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
21050                             epilogue_size_needed);
21051   if (jump_around_label)
21052     emit_label (jump_around_label);
21053   return true;
21054 }
21055
21056 /* Helper function for memcpy.  For QImode value 0xXY produce
21057    0xXYXYXYXY of wide specified by MODE.  This is essentially
21058    a * 0x10101010, but we can do slightly better than
21059    synth_mult by unwinding the sequence by hand on CPUs with
21060    slow multiply.  */
21061 static rtx
21062 promote_duplicated_reg (enum machine_mode mode, rtx val)
21063 {
21064   enum machine_mode valmode = GET_MODE (val);
21065   rtx tmp;
21066   int nops = mode == DImode ? 3 : 2;
21067
21068   gcc_assert (mode == SImode || mode == DImode);
21069   if (val == const0_rtx)
21070     return copy_to_mode_reg (mode, const0_rtx);
21071   if (CONST_INT_P (val))
21072     {
21073       HOST_WIDE_INT v = INTVAL (val) & 255;
21074
21075       v |= v << 8;
21076       v |= v << 16;
21077       if (mode == DImode)
21078         v |= (v << 16) << 16;
21079       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
21080     }
21081
21082   if (valmode == VOIDmode)
21083     valmode = QImode;
21084   if (valmode != QImode)
21085     val = gen_lowpart (QImode, val);
21086   if (mode == QImode)
21087     return val;
21088   if (!TARGET_PARTIAL_REG_STALL)
21089     nops--;
21090   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
21091       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
21092       <= (ix86_cost->shift_const + ix86_cost->add) * nops
21093           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
21094     {
21095       rtx reg = convert_modes (mode, QImode, val, true);
21096       tmp = promote_duplicated_reg (mode, const1_rtx);
21097       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
21098                                   OPTAB_DIRECT);
21099     }
21100   else
21101     {
21102       rtx reg = convert_modes (mode, QImode, val, true);
21103
21104       if (!TARGET_PARTIAL_REG_STALL)
21105         if (mode == SImode)
21106           emit_insn (gen_movsi_insv_1 (reg, reg));
21107         else
21108           emit_insn (gen_movdi_insv_1 (reg, reg));
21109       else
21110         {
21111           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
21112                                      NULL, 1, OPTAB_DIRECT);
21113           reg =
21114             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
21115         }
21116       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
21117                                  NULL, 1, OPTAB_DIRECT);
21118       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
21119       if (mode == SImode)
21120         return reg;
21121       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
21122                                  NULL, 1, OPTAB_DIRECT);
21123       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
21124       return reg;
21125     }
21126 }
21127
21128 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
21129    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
21130    alignment from ALIGN to DESIRED_ALIGN.  */
21131 static rtx
21132 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
21133 {
21134   rtx promoted_val;
21135
21136   if (TARGET_64BIT
21137       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
21138     promoted_val = promote_duplicated_reg (DImode, val);
21139   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
21140     promoted_val = promote_duplicated_reg (SImode, val);
21141   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
21142     promoted_val = promote_duplicated_reg (HImode, val);
21143   else
21144     promoted_val = val;
21145
21146   return promoted_val;
21147 }
21148
21149 /* Expand string clear operation (bzero).  Use i386 string operations when
21150    profitable.  See expand_movmem comment for explanation of individual
21151    steps performed.  */
21152 bool
21153 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
21154                     rtx expected_align_exp, rtx expected_size_exp)
21155 {
21156   rtx destreg;
21157   rtx label = NULL;
21158   rtx tmp;
21159   rtx jump_around_label = NULL;
21160   HOST_WIDE_INT align = 1;
21161   unsigned HOST_WIDE_INT count = 0;
21162   HOST_WIDE_INT expected_size = -1;
21163   int size_needed = 0, epilogue_size_needed;
21164   int desired_align = 0, align_bytes = 0;
21165   enum stringop_alg alg;
21166   rtx promoted_val = NULL;
21167   bool force_loopy_epilogue = false;
21168   int dynamic_check;
21169   bool need_zero_guard = false;
21170
21171   if (CONST_INT_P (align_exp))
21172     align = INTVAL (align_exp);
21173   /* i386 can do misaligned access on reasonably increased cost.  */
21174   if (CONST_INT_P (expected_align_exp)
21175       && INTVAL (expected_align_exp) > align)
21176     align = INTVAL (expected_align_exp);
21177   if (CONST_INT_P (count_exp))
21178     count = expected_size = INTVAL (count_exp);
21179   if (CONST_INT_P (expected_size_exp) && count == 0)
21180     expected_size = INTVAL (expected_size_exp);
21181
21182   /* Make sure we don't need to care about overflow later on.  */
21183   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
21184     return false;
21185
21186   /* Step 0: Decide on preferred algorithm, desired alignment and
21187      size of chunks to be copied by main loop.  */
21188
21189   alg = decide_alg (count, expected_size, true, &dynamic_check);
21190   desired_align = decide_alignment (align, alg, expected_size);
21191
21192   if (!TARGET_ALIGN_STRINGOPS)
21193     align = desired_align;
21194
21195   if (alg == libcall)
21196     return false;
21197   gcc_assert (alg != no_stringop);
21198   if (!count)
21199     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
21200   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
21201   switch (alg)
21202     {
21203     case libcall:
21204     case no_stringop:
21205       gcc_unreachable ();
21206     case loop:
21207       need_zero_guard = true;
21208       size_needed = GET_MODE_SIZE (Pmode);
21209       break;
21210     case unrolled_loop:
21211       need_zero_guard = true;
21212       size_needed = GET_MODE_SIZE (Pmode) * 4;
21213       break;
21214     case rep_prefix_8_byte:
21215       size_needed = 8;
21216       break;
21217     case rep_prefix_4_byte:
21218       size_needed = 4;
21219       break;
21220     case rep_prefix_1_byte:
21221       size_needed = 1;
21222       break;
21223     case loop_1_byte:
21224       need_zero_guard = true;
21225       size_needed = 1;
21226       break;
21227     }
21228   epilogue_size_needed = size_needed;
21229
21230   /* Step 1: Prologue guard.  */
21231
21232   /* Alignment code needs count to be in register.  */
21233   if (CONST_INT_P (count_exp) && desired_align > align)
21234     {
21235       if (INTVAL (count_exp) > desired_align
21236           && INTVAL (count_exp) > size_needed)
21237         {
21238           align_bytes
21239             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
21240           if (align_bytes <= 0)
21241             align_bytes = 0;
21242           else
21243             align_bytes = desired_align - align_bytes;
21244         }
21245       if (align_bytes == 0)
21246         {
21247           enum machine_mode mode = SImode;
21248           if (TARGET_64BIT && (count & ~0xffffffff))
21249             mode = DImode;
21250           count_exp = force_reg (mode, count_exp);
21251         }
21252     }
21253   /* Do the cheap promotion to allow better CSE across the
21254      main loop and epilogue (ie one load of the big constant in the
21255      front of all code.  */
21256   if (CONST_INT_P (val_exp))
21257     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
21258                                                    desired_align, align);
21259   /* Ensure that alignment prologue won't copy past end of block.  */
21260   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
21261     {
21262       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
21263       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
21264          Make sure it is power of 2.  */
21265       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
21266
21267       /* To improve performance of small blocks, we jump around the VAL
21268          promoting mode.  This mean that if the promoted VAL is not constant,
21269          we might not use it in the epilogue and have to use byte
21270          loop variant.  */
21271       if (epilogue_size_needed > 2 && !promoted_val)
21272         force_loopy_epilogue = true;
21273       if (count)
21274         {
21275           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
21276             {
21277               /* If main algorithm works on QImode, no epilogue is needed.
21278                  For small sizes just don't align anything.  */
21279               if (size_needed == 1)
21280                 desired_align = align;
21281               else
21282                 goto epilogue;
21283             }
21284         }
21285       else
21286         {
21287           label = gen_label_rtx ();
21288           emit_cmp_and_jump_insns (count_exp,
21289                                    GEN_INT (epilogue_size_needed),
21290                                    LTU, 0, counter_mode (count_exp), 1, label);
21291           if (expected_size == -1 || expected_size <= epilogue_size_needed)
21292             predict_jump (REG_BR_PROB_BASE * 60 / 100);
21293           else
21294             predict_jump (REG_BR_PROB_BASE * 20 / 100);
21295         }
21296     }
21297   if (dynamic_check != -1)
21298     {
21299       rtx hot_label = gen_label_rtx ();
21300       jump_around_label = gen_label_rtx ();
21301       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
21302                                LEU, 0, counter_mode (count_exp), 1, hot_label);
21303       predict_jump (REG_BR_PROB_BASE * 90 / 100);
21304       set_storage_via_libcall (dst, count_exp, val_exp, false);
21305       emit_jump (jump_around_label);
21306       emit_label (hot_label);
21307     }
21308
21309   /* Step 2: Alignment prologue.  */
21310
21311   /* Do the expensive promotion once we branched off the small blocks.  */
21312   if (!promoted_val)
21313     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
21314                                                    desired_align, align);
21315   gcc_assert (desired_align >= 1 && align >= 1);
21316
21317   if (desired_align > align)
21318     {
21319       if (align_bytes == 0)
21320         {
21321           /* Except for the first move in epilogue, we no longer know
21322              constant offset in aliasing info.  It don't seems to worth
21323              the pain to maintain it for the first move, so throw away
21324              the info early.  */
21325           dst = change_address (dst, BLKmode, destreg);
21326           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
21327                                   desired_align);
21328         }
21329       else
21330         {
21331           /* If we know how many bytes need to be stored before dst is
21332              sufficiently aligned, maintain aliasing info accurately.  */
21333           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
21334                                                  desired_align, align_bytes);
21335           count_exp = plus_constant (count_exp, -align_bytes);
21336           count -= align_bytes;
21337         }
21338       if (need_zero_guard
21339           && (count < (unsigned HOST_WIDE_INT) size_needed
21340               || (align_bytes == 0
21341                   && count < ((unsigned HOST_WIDE_INT) size_needed
21342                               + desired_align - align))))
21343         {
21344           /* It is possible that we copied enough so the main loop will not
21345              execute.  */
21346           gcc_assert (size_needed > 1);
21347           if (label == NULL_RTX)
21348             label = gen_label_rtx ();
21349           emit_cmp_and_jump_insns (count_exp,
21350                                    GEN_INT (size_needed),
21351                                    LTU, 0, counter_mode (count_exp), 1, label);
21352           if (expected_size == -1
21353               || expected_size < (desired_align - align) / 2 + size_needed)
21354             predict_jump (REG_BR_PROB_BASE * 20 / 100);
21355           else
21356             predict_jump (REG_BR_PROB_BASE * 60 / 100);
21357         }
21358     }
21359   if (label && size_needed == 1)
21360     {
21361       emit_label (label);
21362       LABEL_NUSES (label) = 1;
21363       label = NULL;
21364       promoted_val = val_exp;
21365       epilogue_size_needed = 1;
21366     }
21367   else if (label == NULL_RTX)
21368     epilogue_size_needed = size_needed;
21369
21370   /* Step 3: Main loop.  */
21371
21372   switch (alg)
21373     {
21374     case libcall:
21375     case no_stringop:
21376       gcc_unreachable ();
21377     case loop_1_byte:
21378       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
21379                                      count_exp, QImode, 1, expected_size);
21380       break;
21381     case loop:
21382       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
21383                                      count_exp, Pmode, 1, expected_size);
21384       break;
21385     case unrolled_loop:
21386       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
21387                                      count_exp, Pmode, 4, expected_size);
21388       break;
21389     case rep_prefix_8_byte:
21390       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
21391                                   DImode, val_exp);
21392       break;
21393     case rep_prefix_4_byte:
21394       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
21395                                   SImode, val_exp);
21396       break;
21397     case rep_prefix_1_byte:
21398       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
21399                                   QImode, val_exp);
21400       break;
21401     }
21402   /* Adjust properly the offset of src and dest memory for aliasing.  */
21403   if (CONST_INT_P (count_exp))
21404     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
21405                                         (count / size_needed) * size_needed);
21406   else
21407     dst = change_address (dst, BLKmode, destreg);
21408
21409   /* Step 4: Epilogue to copy the remaining bytes.  */
21410
21411   if (label)
21412     {
21413       /* When the main loop is done, COUNT_EXP might hold original count,
21414          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
21415          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
21416          bytes. Compensate if needed.  */
21417
21418       if (size_needed < epilogue_size_needed)
21419         {
21420           tmp =
21421             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
21422                                  GEN_INT (size_needed - 1), count_exp, 1,
21423                                  OPTAB_DIRECT);
21424           if (tmp != count_exp)
21425             emit_move_insn (count_exp, tmp);
21426         }
21427       emit_label (label);
21428       LABEL_NUSES (label) = 1;
21429     }
21430  epilogue:
21431   if (count_exp != const0_rtx && epilogue_size_needed > 1)
21432     {
21433       if (force_loopy_epilogue)
21434         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
21435                                          epilogue_size_needed);
21436       else
21437         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
21438                                 epilogue_size_needed);
21439     }
21440   if (jump_around_label)
21441     emit_label (jump_around_label);
21442   return true;
21443 }
21444
21445 /* Expand the appropriate insns for doing strlen if not just doing
21446    repnz; scasb
21447
21448    out = result, initialized with the start address
21449    align_rtx = alignment of the address.
21450    scratch = scratch register, initialized with the startaddress when
21451         not aligned, otherwise undefined
21452
21453    This is just the body. It needs the initializations mentioned above and
21454    some address computing at the end.  These things are done in i386.md.  */
21455
21456 static void
21457 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
21458 {
21459   int align;
21460   rtx tmp;
21461   rtx align_2_label = NULL_RTX;
21462   rtx align_3_label = NULL_RTX;
21463   rtx align_4_label = gen_label_rtx ();
21464   rtx end_0_label = gen_label_rtx ();
21465   rtx mem;
21466   rtx tmpreg = gen_reg_rtx (SImode);
21467   rtx scratch = gen_reg_rtx (SImode);
21468   rtx cmp;
21469
21470   align = 0;
21471   if (CONST_INT_P (align_rtx))
21472     align = INTVAL (align_rtx);
21473
21474   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
21475
21476   /* Is there a known alignment and is it less than 4?  */
21477   if (align < 4)
21478     {
21479       rtx scratch1 = gen_reg_rtx (Pmode);
21480       emit_move_insn (scratch1, out);
21481       /* Is there a known alignment and is it not 2? */
21482       if (align != 2)
21483         {
21484           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
21485           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
21486
21487           /* Leave just the 3 lower bits.  */
21488           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
21489                                     NULL_RTX, 0, OPTAB_WIDEN);
21490
21491           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
21492                                    Pmode, 1, align_4_label);
21493           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
21494                                    Pmode, 1, align_2_label);
21495           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
21496                                    Pmode, 1, align_3_label);
21497         }
21498       else
21499         {
21500           /* Since the alignment is 2, we have to check 2 or 0 bytes;
21501              check if is aligned to 4 - byte.  */
21502
21503           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
21504                                     NULL_RTX, 0, OPTAB_WIDEN);
21505
21506           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
21507                                    Pmode, 1, align_4_label);
21508         }
21509
21510       mem = change_address (src, QImode, out);
21511
21512       /* Now compare the bytes.  */
21513
21514       /* Compare the first n unaligned byte on a byte per byte basis.  */
21515       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
21516                                QImode, 1, end_0_label);
21517
21518       /* Increment the address.  */
21519       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
21520
21521       /* Not needed with an alignment of 2 */
21522       if (align != 2)
21523         {
21524           emit_label (align_2_label);
21525
21526           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
21527                                    end_0_label);
21528
21529           emit_insn (ix86_gen_add3 (out, out, const1_rtx));
21530
21531           emit_label (align_3_label);
21532         }
21533
21534       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
21535                                end_0_label);
21536
21537       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
21538     }
21539
21540   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
21541      align this loop.  It gives only huge programs, but does not help to
21542      speed up.  */
21543   emit_label (align_4_label);
21544
21545   mem = change_address (src, SImode, out);
21546   emit_move_insn (scratch, mem);
21547   emit_insn (ix86_gen_add3 (out, out, GEN_INT (4)));
21548
21549   /* This formula yields a nonzero result iff one of the bytes is zero.
21550      This saves three branches inside loop and many cycles.  */
21551
21552   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
21553   emit_insn (gen_one_cmplsi2 (scratch, scratch));
21554   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
21555   emit_insn (gen_andsi3 (tmpreg, tmpreg,
21556                          gen_int_mode (0x80808080, SImode)));
21557   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
21558                            align_4_label);
21559
21560   if (TARGET_CMOVE)
21561     {
21562        rtx reg = gen_reg_rtx (SImode);
21563        rtx reg2 = gen_reg_rtx (Pmode);
21564        emit_move_insn (reg, tmpreg);
21565        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
21566
21567        /* If zero is not in the first two bytes, move two bytes forward.  */
21568        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
21569        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21570        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
21571        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
21572                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
21573                                                      reg,
21574                                                      tmpreg)));
21575        /* Emit lea manually to avoid clobbering of flags.  */
21576        emit_insn (gen_rtx_SET (SImode, reg2,
21577                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
21578
21579        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21580        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
21581        emit_insn (gen_rtx_SET (VOIDmode, out,
21582                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
21583                                                      reg2,
21584                                                      out)));
21585     }
21586   else
21587     {
21588        rtx end_2_label = gen_label_rtx ();
21589        /* Is zero in the first two bytes? */
21590
21591        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
21592        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21593        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
21594        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
21595                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
21596                             pc_rtx);
21597        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
21598        JUMP_LABEL (tmp) = end_2_label;
21599
21600        /* Not in the first two.  Move two bytes forward.  */
21601        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
21602        emit_insn (ix86_gen_add3 (out, out, const2_rtx));
21603
21604        emit_label (end_2_label);
21605
21606     }
21607
21608   /* Avoid branch in fixing the byte.  */
21609   tmpreg = gen_lowpart (QImode, tmpreg);
21610   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
21611   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
21612   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
21613   emit_insn (ix86_gen_sub3_carry (out, out, GEN_INT (3), tmp, cmp));
21614
21615   emit_label (end_0_label);
21616 }
21617
21618 /* Expand strlen.  */
21619
21620 bool
21621 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
21622 {
21623   rtx addr, scratch1, scratch2, scratch3, scratch4;
21624
21625   /* The generic case of strlen expander is long.  Avoid it's
21626      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
21627
21628   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
21629       && !TARGET_INLINE_ALL_STRINGOPS
21630       && !optimize_insn_for_size_p ()
21631       && (!CONST_INT_P (align) || INTVAL (align) < 4))
21632     return false;
21633
21634   addr = force_reg (Pmode, XEXP (src, 0));
21635   scratch1 = gen_reg_rtx (Pmode);
21636
21637   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
21638       && !optimize_insn_for_size_p ())
21639     {
21640       /* Well it seems that some optimizer does not combine a call like
21641          foo(strlen(bar), strlen(bar));
21642          when the move and the subtraction is done here.  It does calculate
21643          the length just once when these instructions are done inside of
21644          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
21645          often used and I use one fewer register for the lifetime of
21646          output_strlen_unroll() this is better.  */
21647
21648       emit_move_insn (out, addr);
21649
21650       ix86_expand_strlensi_unroll_1 (out, src, align);
21651
21652       /* strlensi_unroll_1 returns the address of the zero at the end of
21653          the string, like memchr(), so compute the length by subtracting
21654          the start address.  */
21655       emit_insn (ix86_gen_sub3 (out, out, addr));
21656     }
21657   else
21658     {
21659       rtx unspec;
21660
21661       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
21662       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
21663         return false;
21664
21665       scratch2 = gen_reg_rtx (Pmode);
21666       scratch3 = gen_reg_rtx (Pmode);
21667       scratch4 = force_reg (Pmode, constm1_rtx);
21668
21669       emit_move_insn (scratch3, addr);
21670       eoschar = force_reg (QImode, eoschar);
21671
21672       src = replace_equiv_address_nv (src, scratch3);
21673
21674       /* If .md starts supporting :P, this can be done in .md.  */
21675       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
21676                                                  scratch4), UNSPEC_SCAS);
21677       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
21678       emit_insn (ix86_gen_one_cmpl2 (scratch2, scratch1));
21679       emit_insn (ix86_gen_add3 (out, scratch2, constm1_rtx));
21680     }
21681   return true;
21682 }
21683
21684 /* For given symbol (function) construct code to compute address of it's PLT
21685    entry in large x86-64 PIC model.  */
21686 rtx
21687 construct_plt_address (rtx symbol)
21688 {
21689   rtx tmp = gen_reg_rtx (Pmode);
21690   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
21691
21692   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
21693   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
21694
21695   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
21696   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
21697   return tmp;
21698 }
21699
21700 rtx
21701 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
21702                   rtx callarg2,
21703                   rtx pop, int sibcall)
21704 {
21705   rtx use = NULL, call;
21706
21707   if (pop == const0_rtx)
21708     pop = NULL;
21709   gcc_assert (!TARGET_64BIT || !pop);
21710
21711   if (TARGET_MACHO && !TARGET_64BIT)
21712     {
21713 #if TARGET_MACHO
21714       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
21715         fnaddr = machopic_indirect_call_target (fnaddr);
21716 #endif
21717     }
21718   else
21719     {
21720       /* Static functions and indirect calls don't need the pic register.  */
21721       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
21722           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
21723           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
21724         use_reg (&use, pic_offset_table_rtx);
21725     }
21726
21727   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
21728     {
21729       rtx al = gen_rtx_REG (QImode, AX_REG);
21730       emit_move_insn (al, callarg2);
21731       use_reg (&use, al);
21732     }
21733
21734   if (ix86_cmodel == CM_LARGE_PIC
21735       && MEM_P (fnaddr)
21736       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
21737       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
21738     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
21739   else if (sibcall
21740            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
21741            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
21742     {
21743       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
21744       fnaddr = gen_rtx_MEM (QImode, fnaddr);
21745     }
21746
21747   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
21748   if (retval)
21749     call = gen_rtx_SET (VOIDmode, retval, call);
21750   if (pop)
21751     {
21752       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
21753       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
21754       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
21755     }
21756   if (TARGET_64BIT
21757       && ix86_cfun_abi () == MS_ABI
21758       && (!callarg2 || INTVAL (callarg2) != -2))
21759     {
21760       /* We need to represent that SI and DI registers are clobbered
21761          by SYSV calls.  */
21762       static int clobbered_registers[] = {
21763         XMM6_REG, XMM7_REG, XMM8_REG,
21764         XMM9_REG, XMM10_REG, XMM11_REG,
21765         XMM12_REG, XMM13_REG, XMM14_REG,
21766         XMM15_REG, SI_REG, DI_REG
21767       };
21768       unsigned int i;
21769       rtx vec[ARRAY_SIZE (clobbered_registers) + 2];
21770       rtx unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
21771                                    UNSPEC_MS_TO_SYSV_CALL);
21772
21773       vec[0] = call;
21774       vec[1] = unspec;
21775       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
21776         vec[i + 2] = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
21777                                       ? TImode : DImode,
21778                                       gen_rtx_REG
21779                                         (SSE_REGNO_P (clobbered_registers[i])
21780                                                       ? TImode : DImode,
21781                                          clobbered_registers[i]));
21782
21783       call = gen_rtx_PARALLEL (VOIDmode,
21784                                gen_rtvec_v (ARRAY_SIZE (clobbered_registers)
21785                                + 2, vec));
21786     }
21787
21788   /* Add UNSPEC_CALL_NEEDS_VZEROUPPER decoration.  */
21789   if (TARGET_VZEROUPPER)
21790     {
21791       rtx unspec;
21792       int avx256;
21793
21794       if (cfun->machine->callee_pass_avx256_p)
21795         {
21796           if (cfun->machine->callee_return_avx256_p)
21797             avx256 = callee_return_pass_avx256;
21798           else
21799             avx256 = callee_pass_avx256;
21800         }
21801       else if (cfun->machine->callee_return_avx256_p)
21802         avx256 = callee_return_avx256;
21803       else
21804         avx256 = call_no_avx256;
21805
21806       if (reload_completed)
21807         emit_insn (gen_avx_vzeroupper (GEN_INT (avx256)));
21808       else
21809         {
21810           unspec = gen_rtx_UNSPEC (VOIDmode,
21811                                    gen_rtvec (1, GEN_INT (avx256)),
21812                                    UNSPEC_CALL_NEEDS_VZEROUPPER);
21813           call = gen_rtx_PARALLEL (VOIDmode,
21814                                    gen_rtvec (2, call, unspec));
21815         }
21816     }
21817
21818   call = emit_call_insn (call);
21819   if (use)
21820     CALL_INSN_FUNCTION_USAGE (call) = use;
21821
21822   return call;
21823 }
21824
21825 void
21826 ix86_split_call_vzeroupper (rtx insn, rtx vzeroupper)
21827 {
21828   rtx call = XVECEXP (PATTERN (insn), 0, 0);
21829   emit_insn (gen_avx_vzeroupper (vzeroupper));
21830   emit_call_insn (call);
21831 }
21832
21833 /* Output the assembly for a call instruction.  */
21834
21835 const char *
21836 ix86_output_call_insn (rtx insn, rtx call_op, int addr_op)
21837 {
21838   bool direct_p = constant_call_address_operand (call_op, Pmode);
21839   bool seh_nop_p = false;
21840
21841   gcc_assert (addr_op == 0 || addr_op == 1);
21842
21843   if (SIBLING_CALL_P (insn))
21844     {
21845       if (direct_p)
21846         return addr_op ? "jmp\t%P1" : "jmp\t%P0";
21847       /* SEH epilogue detection requires the indirect branch case
21848          to include REX.W.  */
21849       else if (TARGET_SEH)
21850         return addr_op ? "rex.W jmp %A1" : "rex.W jmp %A0";
21851       else
21852         return addr_op ? "jmp\t%A1" : "jmp\t%A0";
21853     }
21854
21855   /* SEH unwinding can require an extra nop to be emitted in several
21856      circumstances.  Determine if we have one of those.  */
21857   if (TARGET_SEH)
21858     {
21859       rtx i;
21860
21861       for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
21862         {
21863           /* If we get to another real insn, we don't need the nop.  */
21864           if (INSN_P (i))
21865             break;
21866
21867           /* If we get to the epilogue note, prevent a catch region from
21868              being adjacent to the standard epilogue sequence.  If non-
21869              call-exceptions, we'll have done this during epilogue emission. */
21870           if (NOTE_P (i) && NOTE_KIND (i) == NOTE_INSN_EPILOGUE_BEG
21871               && !flag_non_call_exceptions
21872               && !can_throw_internal (insn))
21873             {
21874               seh_nop_p = true;
21875               break;
21876             }
21877         }
21878
21879       /* If we didn't find a real insn following the call, prevent the
21880          unwinder from looking into the next function.  */
21881       if (i == NULL)
21882         seh_nop_p = true;
21883     }
21884
21885   if (direct_p)
21886     {
21887       if (seh_nop_p)
21888         return addr_op ? "call\t%P1\n\tnop" : "call\t%P0\n\tnop";
21889       else
21890         return addr_op ? "call\t%P1" : "call\t%P0";
21891     }
21892   else
21893     {
21894       if (seh_nop_p)
21895         return addr_op ? "call\t%A1\n\tnop" : "call\t%A0\n\tnop";
21896       else
21897         return addr_op ? "call\t%A1" : "call\t%A0";
21898     }
21899 }
21900 \f
21901 /* Clear stack slot assignments remembered from previous functions.
21902    This is called from INIT_EXPANDERS once before RTL is emitted for each
21903    function.  */
21904
21905 static struct machine_function *
21906 ix86_init_machine_status (void)
21907 {
21908   struct machine_function *f;
21909
21910   f = ggc_alloc_cleared_machine_function ();
21911   f->use_fast_prologue_epilogue_nregs = -1;
21912   f->tls_descriptor_call_expanded_p = 0;
21913   f->call_abi = ix86_abi;
21914
21915   return f;
21916 }
21917
21918 /* Return a MEM corresponding to a stack slot with mode MODE.
21919    Allocate a new slot if necessary.
21920
21921    The RTL for a function can have several slots available: N is
21922    which slot to use.  */
21923
21924 rtx
21925 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
21926 {
21927   struct stack_local_entry *s;
21928
21929   gcc_assert (n < MAX_386_STACK_LOCALS);
21930
21931   /* Virtual slot is valid only before vregs are instantiated.  */
21932   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
21933
21934   for (s = ix86_stack_locals; s; s = s->next)
21935     if (s->mode == mode && s->n == n)
21936       return copy_rtx (s->rtl);
21937
21938   s = ggc_alloc_stack_local_entry ();
21939   s->n = n;
21940   s->mode = mode;
21941   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
21942
21943   s->next = ix86_stack_locals;
21944   ix86_stack_locals = s;
21945   return s->rtl;
21946 }
21947
21948 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
21949
21950 static GTY(()) rtx ix86_tls_symbol;
21951 rtx
21952 ix86_tls_get_addr (void)
21953 {
21954
21955   if (!ix86_tls_symbol)
21956     {
21957       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
21958                                             (TARGET_ANY_GNU_TLS
21959                                              && !TARGET_64BIT)
21960                                             ? "___tls_get_addr"
21961                                             : "__tls_get_addr");
21962     }
21963
21964   return ix86_tls_symbol;
21965 }
21966
21967 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
21968
21969 static GTY(()) rtx ix86_tls_module_base_symbol;
21970 rtx
21971 ix86_tls_module_base (void)
21972 {
21973
21974   if (!ix86_tls_module_base_symbol)
21975     {
21976       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
21977                                                         "_TLS_MODULE_BASE_");
21978       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
21979         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
21980     }
21981
21982   return ix86_tls_module_base_symbol;
21983 }
21984 \f
21985 /* Calculate the length of the memory address in the instruction
21986    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
21987
21988 int
21989 memory_address_length (rtx addr)
21990 {
21991   struct ix86_address parts;
21992   rtx base, index, disp;
21993   int len;
21994   int ok;
21995
21996   if (GET_CODE (addr) == PRE_DEC
21997       || GET_CODE (addr) == POST_INC
21998       || GET_CODE (addr) == PRE_MODIFY
21999       || GET_CODE (addr) == POST_MODIFY)
22000     return 0;
22001
22002   ok = ix86_decompose_address (addr, &parts);
22003   gcc_assert (ok);
22004
22005   if (parts.base && GET_CODE (parts.base) == SUBREG)
22006     parts.base = SUBREG_REG (parts.base);
22007   if (parts.index && GET_CODE (parts.index) == SUBREG)
22008     parts.index = SUBREG_REG (parts.index);
22009
22010   base = parts.base;
22011   index = parts.index;
22012   disp = parts.disp;
22013   len = 0;
22014
22015   /* Rule of thumb:
22016        - esp as the base always wants an index,
22017        - ebp as the base always wants a displacement,
22018        - r12 as the base always wants an index,
22019        - r13 as the base always wants a displacement.  */
22020
22021   /* Register Indirect.  */
22022   if (base && !index && !disp)
22023     {
22024       /* esp (for its index) and ebp (for its displacement) need
22025          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
22026          code.  */
22027       if (REG_P (addr)
22028           && (addr == arg_pointer_rtx
22029               || addr == frame_pointer_rtx
22030               || REGNO (addr) == SP_REG
22031               || REGNO (addr) == BP_REG
22032               || REGNO (addr) == R12_REG
22033               || REGNO (addr) == R13_REG))
22034         len = 1;
22035     }
22036
22037   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
22038      is not disp32, but disp32(%rip), so for disp32
22039      SIB byte is needed, unless print_operand_address
22040      optimizes it into disp32(%rip) or (%rip) is implied
22041      by UNSPEC.  */
22042   else if (disp && !base && !index)
22043     {
22044       len = 4;
22045       if (TARGET_64BIT)
22046         {
22047           rtx symbol = disp;
22048
22049           if (GET_CODE (disp) == CONST)
22050             symbol = XEXP (disp, 0);
22051           if (GET_CODE (symbol) == PLUS
22052               && CONST_INT_P (XEXP (symbol, 1)))
22053             symbol = XEXP (symbol, 0);
22054
22055           if (GET_CODE (symbol) != LABEL_REF
22056               && (GET_CODE (symbol) != SYMBOL_REF
22057                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
22058               && (GET_CODE (symbol) != UNSPEC
22059                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
22060                       && XINT (symbol, 1) != UNSPEC_PCREL
22061                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
22062             len += 1;
22063         }
22064     }
22065
22066   else
22067     {
22068       /* Find the length of the displacement constant.  */
22069       if (disp)
22070         {
22071           if (base && satisfies_constraint_K (disp))
22072             len = 1;
22073           else
22074             len = 4;
22075         }
22076       /* ebp always wants a displacement.  Similarly r13.  */
22077       else if (base && REG_P (base)
22078                && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
22079         len = 1;
22080
22081       /* An index requires the two-byte modrm form....  */
22082       if (index
22083           /* ...like esp (or r12), which always wants an index.  */
22084           || base == arg_pointer_rtx
22085           || base == frame_pointer_rtx
22086           || (base && REG_P (base)
22087               && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
22088         len += 1;
22089     }
22090
22091   switch (parts.seg)
22092     {
22093     case SEG_FS:
22094     case SEG_GS:
22095       len += 1;
22096       break;
22097     default:
22098       break;
22099     }
22100
22101   return len;
22102 }
22103
22104 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
22105    is set, expect that insn have 8bit immediate alternative.  */
22106 int
22107 ix86_attr_length_immediate_default (rtx insn, int shortform)
22108 {
22109   int len = 0;
22110   int i;
22111   extract_insn_cached (insn);
22112   for (i = recog_data.n_operands - 1; i >= 0; --i)
22113     if (CONSTANT_P (recog_data.operand[i]))
22114       {
22115         enum attr_mode mode = get_attr_mode (insn);
22116
22117         gcc_assert (!len);
22118         if (shortform && CONST_INT_P (recog_data.operand[i]))
22119           {
22120             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
22121             switch (mode)
22122               {
22123               case MODE_QI:
22124                 len = 1;
22125                 continue;
22126               case MODE_HI:
22127                 ival = trunc_int_for_mode (ival, HImode);
22128                 break;
22129               case MODE_SI:
22130                 ival = trunc_int_for_mode (ival, SImode);
22131                 break;
22132               default:
22133                 break;
22134               }
22135             if (IN_RANGE (ival, -128, 127))
22136               {
22137                 len = 1;
22138                 continue;
22139               }
22140           }
22141         switch (mode)
22142           {
22143           case MODE_QI:
22144             len = 1;
22145             break;
22146           case MODE_HI:
22147             len = 2;
22148             break;
22149           case MODE_SI:
22150             len = 4;
22151             break;
22152           /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
22153           case MODE_DI:
22154             len = 4;
22155             break;
22156           default:
22157             fatal_insn ("unknown insn mode", insn);
22158         }
22159       }
22160   return len;
22161 }
22162 /* Compute default value for "length_address" attribute.  */
22163 int
22164 ix86_attr_length_address_default (rtx insn)
22165 {
22166   int i;
22167
22168   if (get_attr_type (insn) == TYPE_LEA)
22169     {
22170       rtx set = PATTERN (insn), addr;
22171
22172       if (GET_CODE (set) == PARALLEL)
22173         set = XVECEXP (set, 0, 0);
22174
22175       gcc_assert (GET_CODE (set) == SET);
22176
22177       addr = SET_SRC (set);
22178       if (TARGET_64BIT && get_attr_mode (insn) == MODE_SI)
22179         {
22180           if (GET_CODE (addr) == ZERO_EXTEND)
22181             addr = XEXP (addr, 0);
22182           if (GET_CODE (addr) == SUBREG)
22183             addr = SUBREG_REG (addr);
22184         }
22185
22186       return memory_address_length (addr);
22187     }
22188
22189   extract_insn_cached (insn);
22190   for (i = recog_data.n_operands - 1; i >= 0; --i)
22191     if (MEM_P (recog_data.operand[i]))
22192       {
22193         constrain_operands_cached (reload_completed);
22194         if (which_alternative != -1)
22195           {
22196             const char *constraints = recog_data.constraints[i];
22197             int alt = which_alternative;
22198
22199             while (*constraints == '=' || *constraints == '+')
22200               constraints++;
22201             while (alt-- > 0)
22202               while (*constraints++ != ',')
22203                 ;
22204             /* Skip ignored operands.  */
22205             if (*constraints == 'X')
22206               continue;
22207           }
22208         return memory_address_length (XEXP (recog_data.operand[i], 0));
22209       }
22210   return 0;
22211 }
22212
22213 /* Compute default value for "length_vex" attribute. It includes
22214    2 or 3 byte VEX prefix and 1 opcode byte.  */
22215
22216 int
22217 ix86_attr_length_vex_default (rtx insn, int has_0f_opcode,
22218                               int has_vex_w)
22219 {
22220   int i;
22221
22222   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
22223      byte VEX prefix.  */
22224   if (!has_0f_opcode || has_vex_w)
22225     return 3 + 1;
22226
22227  /* We can always use 2 byte VEX prefix in 32bit.  */
22228   if (!TARGET_64BIT)
22229     return 2 + 1;
22230
22231   extract_insn_cached (insn);
22232
22233   for (i = recog_data.n_operands - 1; i >= 0; --i)
22234     if (REG_P (recog_data.operand[i]))
22235       {
22236         /* REX.W bit uses 3 byte VEX prefix.  */
22237         if (GET_MODE (recog_data.operand[i]) == DImode
22238             && GENERAL_REG_P (recog_data.operand[i]))
22239           return 3 + 1;
22240       }
22241     else
22242       {
22243         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
22244         if (MEM_P (recog_data.operand[i])
22245             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
22246           return 3 + 1;
22247       }
22248
22249   return 2 + 1;
22250 }
22251 \f
22252 /* Return the maximum number of instructions a cpu can issue.  */
22253
22254 static int
22255 ix86_issue_rate (void)
22256 {
22257   switch (ix86_tune)
22258     {
22259     case PROCESSOR_PENTIUM:
22260     case PROCESSOR_ATOM:
22261     case PROCESSOR_K6:
22262       return 2;
22263
22264     case PROCESSOR_PENTIUMPRO:
22265     case PROCESSOR_PENTIUM4:
22266     case PROCESSOR_CORE2_32:
22267     case PROCESSOR_CORE2_64:
22268     case PROCESSOR_COREI7_32:
22269     case PROCESSOR_COREI7_64:
22270     case PROCESSOR_ATHLON:
22271     case PROCESSOR_K8:
22272     case PROCESSOR_AMDFAM10:
22273     case PROCESSOR_NOCONA:
22274     case PROCESSOR_GENERIC32:
22275     case PROCESSOR_GENERIC64:
22276     case PROCESSOR_BDVER1:
22277     case PROCESSOR_BTVER1:
22278       return 3;
22279
22280     default:
22281       return 1;
22282     }
22283 }
22284
22285 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
22286    by DEP_INSN and nothing set by DEP_INSN.  */
22287
22288 static int
22289 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
22290 {
22291   rtx set, set2;
22292
22293   /* Simplify the test for uninteresting insns.  */
22294   if (insn_type != TYPE_SETCC
22295       && insn_type != TYPE_ICMOV
22296       && insn_type != TYPE_FCMOV
22297       && insn_type != TYPE_IBR)
22298     return 0;
22299
22300   if ((set = single_set (dep_insn)) != 0)
22301     {
22302       set = SET_DEST (set);
22303       set2 = NULL_RTX;
22304     }
22305   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
22306            && XVECLEN (PATTERN (dep_insn), 0) == 2
22307            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
22308            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
22309     {
22310       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
22311       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
22312     }
22313   else
22314     return 0;
22315
22316   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
22317     return 0;
22318
22319   /* This test is true if the dependent insn reads the flags but
22320      not any other potentially set register.  */
22321   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
22322     return 0;
22323
22324   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
22325     return 0;
22326
22327   return 1;
22328 }
22329
22330 /* Return true iff USE_INSN has a memory address with operands set by
22331    SET_INSN.  */
22332
22333 bool
22334 ix86_agi_dependent (rtx set_insn, rtx use_insn)
22335 {
22336   int i;
22337   extract_insn_cached (use_insn);
22338   for (i = recog_data.n_operands - 1; i >= 0; --i)
22339     if (MEM_P (recog_data.operand[i]))
22340       {
22341         rtx addr = XEXP (recog_data.operand[i], 0);
22342         return modified_in_p (addr, set_insn) != 0;
22343       }
22344   return false;
22345 }
22346
22347 static int
22348 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22349 {
22350   enum attr_type insn_type, dep_insn_type;
22351   enum attr_memory memory;
22352   rtx set, set2;
22353   int dep_insn_code_number;
22354
22355   /* Anti and output dependencies have zero cost on all CPUs.  */
22356   if (REG_NOTE_KIND (link) != 0)
22357     return 0;
22358
22359   dep_insn_code_number = recog_memoized (dep_insn);
22360
22361   /* If we can't recognize the insns, we can't really do anything.  */
22362   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
22363     return cost;
22364
22365   insn_type = get_attr_type (insn);
22366   dep_insn_type = get_attr_type (dep_insn);
22367
22368   switch (ix86_tune)
22369     {
22370     case PROCESSOR_PENTIUM:
22371       /* Address Generation Interlock adds a cycle of latency.  */
22372       if (insn_type == TYPE_LEA)
22373         {
22374           rtx addr = PATTERN (insn);
22375
22376           if (GET_CODE (addr) == PARALLEL)
22377             addr = XVECEXP (addr, 0, 0);
22378
22379           gcc_assert (GET_CODE (addr) == SET);
22380
22381           addr = SET_SRC (addr);
22382           if (modified_in_p (addr, dep_insn))
22383             cost += 1;
22384         }
22385       else if (ix86_agi_dependent (dep_insn, insn))
22386         cost += 1;
22387
22388       /* ??? Compares pair with jump/setcc.  */
22389       if (ix86_flags_dependent (insn, dep_insn, insn_type))
22390         cost = 0;
22391
22392       /* Floating point stores require value to be ready one cycle earlier.  */
22393       if (insn_type == TYPE_FMOV
22394           && get_attr_memory (insn) == MEMORY_STORE
22395           && !ix86_agi_dependent (dep_insn, insn))
22396         cost += 1;
22397       break;
22398
22399     case PROCESSOR_PENTIUMPRO:
22400       memory = get_attr_memory (insn);
22401
22402       /* INT->FP conversion is expensive.  */
22403       if (get_attr_fp_int_src (dep_insn))
22404         cost += 5;
22405
22406       /* There is one cycle extra latency between an FP op and a store.  */
22407       if (insn_type == TYPE_FMOV
22408           && (set = single_set (dep_insn)) != NULL_RTX
22409           && (set2 = single_set (insn)) != NULL_RTX
22410           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
22411           && MEM_P (SET_DEST (set2)))
22412         cost += 1;
22413
22414       /* Show ability of reorder buffer to hide latency of load by executing
22415          in parallel with previous instruction in case
22416          previous instruction is not needed to compute the address.  */
22417       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
22418           && !ix86_agi_dependent (dep_insn, insn))
22419         {
22420           /* Claim moves to take one cycle, as core can issue one load
22421              at time and the next load can start cycle later.  */
22422           if (dep_insn_type == TYPE_IMOV
22423               || dep_insn_type == TYPE_FMOV)
22424             cost = 1;
22425           else if (cost > 1)
22426             cost--;
22427         }
22428       break;
22429
22430     case PROCESSOR_K6:
22431       memory = get_attr_memory (insn);
22432
22433       /* The esp dependency is resolved before the instruction is really
22434          finished.  */
22435       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
22436           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
22437         return 1;
22438
22439       /* INT->FP conversion is expensive.  */
22440       if (get_attr_fp_int_src (dep_insn))
22441         cost += 5;
22442
22443       /* Show ability of reorder buffer to hide latency of load by executing
22444          in parallel with previous instruction in case
22445          previous instruction is not needed to compute the address.  */
22446       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
22447           && !ix86_agi_dependent (dep_insn, insn))
22448         {
22449           /* Claim moves to take one cycle, as core can issue one load
22450              at time and the next load can start cycle later.  */
22451           if (dep_insn_type == TYPE_IMOV
22452               || dep_insn_type == TYPE_FMOV)
22453             cost = 1;
22454           else if (cost > 2)
22455             cost -= 2;
22456           else
22457             cost = 1;
22458         }
22459       break;
22460
22461     case PROCESSOR_ATHLON:
22462     case PROCESSOR_K8:
22463     case PROCESSOR_AMDFAM10:
22464     case PROCESSOR_BDVER1:
22465     case PROCESSOR_BTVER1:
22466     case PROCESSOR_ATOM:
22467     case PROCESSOR_GENERIC32:
22468     case PROCESSOR_GENERIC64:
22469       memory = get_attr_memory (insn);
22470
22471       /* Show ability of reorder buffer to hide latency of load by executing
22472          in parallel with previous instruction in case
22473          previous instruction is not needed to compute the address.  */
22474       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
22475           && !ix86_agi_dependent (dep_insn, insn))
22476         {
22477           enum attr_unit unit = get_attr_unit (insn);
22478           int loadcost = 3;
22479
22480           /* Because of the difference between the length of integer and
22481              floating unit pipeline preparation stages, the memory operands
22482              for floating point are cheaper.
22483
22484              ??? For Athlon it the difference is most probably 2.  */
22485           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
22486             loadcost = 3;
22487           else
22488             loadcost = TARGET_ATHLON ? 2 : 0;
22489
22490           if (cost >= loadcost)
22491             cost -= loadcost;
22492           else
22493             cost = 0;
22494         }
22495
22496     default:
22497       break;
22498     }
22499
22500   return cost;
22501 }
22502
22503 /* How many alternative schedules to try.  This should be as wide as the
22504    scheduling freedom in the DFA, but no wider.  Making this value too
22505    large results extra work for the scheduler.  */
22506
22507 static int
22508 ia32_multipass_dfa_lookahead (void)
22509 {
22510   switch (ix86_tune)
22511     {
22512     case PROCESSOR_PENTIUM:
22513       return 2;
22514
22515     case PROCESSOR_PENTIUMPRO:
22516     case PROCESSOR_K6:
22517       return 1;
22518
22519     case PROCESSOR_CORE2_32:
22520     case PROCESSOR_CORE2_64:
22521     case PROCESSOR_COREI7_32:
22522     case PROCESSOR_COREI7_64:
22523       /* Generally, we want haifa-sched:max_issue() to look ahead as far
22524          as many instructions can be executed on a cycle, i.e.,
22525          issue_rate.  I wonder why tuning for many CPUs does not do this.  */
22526       return ix86_issue_rate ();
22527
22528     default:
22529       return 0;
22530     }
22531 }
22532
22533 \f
22534
22535 /* Model decoder of Core 2/i7.
22536    Below hooks for multipass scheduling (see haifa-sched.c:max_issue)
22537    track the instruction fetch block boundaries and make sure that long
22538    (9+ bytes) instructions are assigned to D0.  */
22539
22540 /* Maximum length of an insn that can be handled by
22541    a secondary decoder unit.  '8' for Core 2/i7.  */
22542 static int core2i7_secondary_decoder_max_insn_size;
22543
22544 /* Ifetch block size, i.e., number of bytes decoder reads per cycle.
22545    '16' for Core 2/i7.  */
22546 static int core2i7_ifetch_block_size;
22547
22548 /* Maximum number of instructions decoder can handle per cycle.
22549    '6' for Core 2/i7.  */
22550 static int core2i7_ifetch_block_max_insns;
22551
22552 typedef struct ix86_first_cycle_multipass_data_ *
22553   ix86_first_cycle_multipass_data_t;
22554 typedef const struct ix86_first_cycle_multipass_data_ *
22555   const_ix86_first_cycle_multipass_data_t;
22556
22557 /* A variable to store target state across calls to max_issue within
22558    one cycle.  */
22559 static struct ix86_first_cycle_multipass_data_ _ix86_first_cycle_multipass_data,
22560   *ix86_first_cycle_multipass_data = &_ix86_first_cycle_multipass_data;
22561
22562 /* Initialize DATA.  */
22563 static void
22564 core2i7_first_cycle_multipass_init (void *_data)
22565 {
22566   ix86_first_cycle_multipass_data_t data
22567     = (ix86_first_cycle_multipass_data_t) _data;
22568
22569   data->ifetch_block_len = 0;
22570   data->ifetch_block_n_insns = 0;
22571   data->ready_try_change = NULL;
22572   data->ready_try_change_size = 0;
22573 }
22574
22575 /* Advancing the cycle; reset ifetch block counts.  */
22576 static void
22577 core2i7_dfa_post_advance_cycle (void)
22578 {
22579   ix86_first_cycle_multipass_data_t data = ix86_first_cycle_multipass_data;
22580
22581   gcc_assert (data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
22582
22583   data->ifetch_block_len = 0;
22584   data->ifetch_block_n_insns = 0;
22585 }
22586
22587 static int min_insn_size (rtx);
22588
22589 /* Filter out insns from ready_try that the core will not be able to issue
22590    on current cycle due to decoder.  */
22591 static void
22592 core2i7_first_cycle_multipass_filter_ready_try
22593 (const_ix86_first_cycle_multipass_data_t data,
22594  char *ready_try, int n_ready, bool first_cycle_insn_p)
22595 {
22596   while (n_ready--)
22597     {
22598       rtx insn;
22599       int insn_size;
22600
22601       if (ready_try[n_ready])
22602         continue;
22603
22604       insn = get_ready_element (n_ready);
22605       insn_size = min_insn_size (insn);
22606
22607       if (/* If this is a too long an insn for a secondary decoder ...  */
22608           (!first_cycle_insn_p
22609            && insn_size > core2i7_secondary_decoder_max_insn_size)
22610           /* ... or it would not fit into the ifetch block ...  */
22611           || data->ifetch_block_len + insn_size > core2i7_ifetch_block_size
22612           /* ... or the decoder is full already ...  */
22613           || data->ifetch_block_n_insns + 1 > core2i7_ifetch_block_max_insns)
22614         /* ... mask the insn out.  */
22615         {
22616           ready_try[n_ready] = 1;
22617
22618           if (data->ready_try_change)
22619             SET_BIT (data->ready_try_change, n_ready);
22620         }
22621     }
22622 }
22623
22624 /* Prepare for a new round of multipass lookahead scheduling.  */
22625 static void
22626 core2i7_first_cycle_multipass_begin (void *_data, char *ready_try, int n_ready,
22627                                      bool first_cycle_insn_p)
22628 {
22629   ix86_first_cycle_multipass_data_t data
22630     = (ix86_first_cycle_multipass_data_t) _data;
22631   const_ix86_first_cycle_multipass_data_t prev_data
22632     = ix86_first_cycle_multipass_data;
22633
22634   /* Restore the state from the end of the previous round.  */
22635   data->ifetch_block_len = prev_data->ifetch_block_len;
22636   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns;
22637
22638   /* Filter instructions that cannot be issued on current cycle due to
22639      decoder restrictions.  */
22640   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
22641                                                   first_cycle_insn_p);
22642 }
22643
22644 /* INSN is being issued in current solution.  Account for its impact on
22645    the decoder model.  */
22646 static void
22647 core2i7_first_cycle_multipass_issue (void *_data, char *ready_try, int n_ready,
22648                                      rtx insn, const void *_prev_data)
22649 {
22650   ix86_first_cycle_multipass_data_t data
22651     = (ix86_first_cycle_multipass_data_t) _data;
22652   const_ix86_first_cycle_multipass_data_t prev_data
22653     = (const_ix86_first_cycle_multipass_data_t) _prev_data;
22654
22655   int insn_size = min_insn_size (insn);
22656
22657   data->ifetch_block_len = prev_data->ifetch_block_len + insn_size;
22658   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns + 1;
22659   gcc_assert (data->ifetch_block_len <= core2i7_ifetch_block_size
22660               && data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
22661
22662   /* Allocate or resize the bitmap for storing INSN's effect on ready_try.  */
22663   if (!data->ready_try_change)
22664     {
22665       data->ready_try_change = sbitmap_alloc (n_ready);
22666       data->ready_try_change_size = n_ready;
22667     }
22668   else if (data->ready_try_change_size < n_ready)
22669     {
22670       data->ready_try_change = sbitmap_resize (data->ready_try_change,
22671                                                n_ready, 0);
22672       data->ready_try_change_size = n_ready;
22673     }
22674   sbitmap_zero (data->ready_try_change);
22675
22676   /* Filter out insns from ready_try that the core will not be able to issue
22677      on current cycle due to decoder.  */
22678   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
22679                                                   false);
22680 }
22681
22682 /* Revert the effect on ready_try.  */
22683 static void
22684 core2i7_first_cycle_multipass_backtrack (const void *_data,
22685                                          char *ready_try,
22686                                          int n_ready ATTRIBUTE_UNUSED)
22687 {
22688   const_ix86_first_cycle_multipass_data_t data
22689     = (const_ix86_first_cycle_multipass_data_t) _data;
22690   unsigned int i = 0;
22691   sbitmap_iterator sbi;
22692
22693   gcc_assert (sbitmap_last_set_bit (data->ready_try_change) < n_ready);
22694   EXECUTE_IF_SET_IN_SBITMAP (data->ready_try_change, 0, i, sbi)
22695     {
22696       ready_try[i] = 0;
22697     }
22698 }
22699
22700 /* Save the result of multipass lookahead scheduling for the next round.  */
22701 static void
22702 core2i7_first_cycle_multipass_end (const void *_data)
22703 {
22704   const_ix86_first_cycle_multipass_data_t data
22705     = (const_ix86_first_cycle_multipass_data_t) _data;
22706   ix86_first_cycle_multipass_data_t next_data
22707     = ix86_first_cycle_multipass_data;
22708
22709   if (data != NULL)
22710     {
22711       next_data->ifetch_block_len = data->ifetch_block_len;
22712       next_data->ifetch_block_n_insns = data->ifetch_block_n_insns;
22713     }
22714 }
22715
22716 /* Deallocate target data.  */
22717 static void
22718 core2i7_first_cycle_multipass_fini (void *_data)
22719 {
22720   ix86_first_cycle_multipass_data_t data
22721     = (ix86_first_cycle_multipass_data_t) _data;
22722
22723   if (data->ready_try_change)
22724     {
22725       sbitmap_free (data->ready_try_change);
22726       data->ready_try_change = NULL;
22727       data->ready_try_change_size = 0;
22728     }
22729 }
22730
22731 /* Prepare for scheduling pass.  */
22732 static void
22733 ix86_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
22734                         int verbose ATTRIBUTE_UNUSED,
22735                         int max_uid ATTRIBUTE_UNUSED)
22736 {
22737   /* Install scheduling hooks for current CPU.  Some of these hooks are used
22738      in time-critical parts of the scheduler, so we only set them up when
22739      they are actually used.  */
22740   switch (ix86_tune)
22741     {
22742     case PROCESSOR_CORE2_32:
22743     case PROCESSOR_CORE2_64:
22744     case PROCESSOR_COREI7_32:
22745     case PROCESSOR_COREI7_64:
22746       targetm.sched.dfa_post_advance_cycle
22747         = core2i7_dfa_post_advance_cycle;
22748       targetm.sched.first_cycle_multipass_init
22749         = core2i7_first_cycle_multipass_init;
22750       targetm.sched.first_cycle_multipass_begin
22751         = core2i7_first_cycle_multipass_begin;
22752       targetm.sched.first_cycle_multipass_issue
22753         = core2i7_first_cycle_multipass_issue;
22754       targetm.sched.first_cycle_multipass_backtrack
22755         = core2i7_first_cycle_multipass_backtrack;
22756       targetm.sched.first_cycle_multipass_end
22757         = core2i7_first_cycle_multipass_end;
22758       targetm.sched.first_cycle_multipass_fini
22759         = core2i7_first_cycle_multipass_fini;
22760
22761       /* Set decoder parameters.  */
22762       core2i7_secondary_decoder_max_insn_size = 8;
22763       core2i7_ifetch_block_size = 16;
22764       core2i7_ifetch_block_max_insns = 6;
22765       break;
22766
22767     default:
22768       targetm.sched.dfa_post_advance_cycle = NULL;
22769       targetm.sched.first_cycle_multipass_init = NULL;
22770       targetm.sched.first_cycle_multipass_begin = NULL;
22771       targetm.sched.first_cycle_multipass_issue = NULL;
22772       targetm.sched.first_cycle_multipass_backtrack = NULL;
22773       targetm.sched.first_cycle_multipass_end = NULL;
22774       targetm.sched.first_cycle_multipass_fini = NULL;
22775       break;
22776     }
22777 }
22778
22779 \f
22780 /* Compute the alignment given to a constant that is being placed in memory.
22781    EXP is the constant and ALIGN is the alignment that the object would
22782    ordinarily have.
22783    The value of this function is used instead of that alignment to align
22784    the object.  */
22785
22786 int
22787 ix86_constant_alignment (tree exp, int align)
22788 {
22789   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
22790       || TREE_CODE (exp) == INTEGER_CST)
22791     {
22792       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
22793         return 64;
22794       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
22795         return 128;
22796     }
22797   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
22798            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
22799     return BITS_PER_WORD;
22800
22801   return align;
22802 }
22803
22804 /* Compute the alignment for a static variable.
22805    TYPE is the data type, and ALIGN is the alignment that
22806    the object would ordinarily have.  The value of this function is used
22807    instead of that alignment to align the object.  */
22808
22809 int
22810 ix86_data_alignment (tree type, int align)
22811 {
22812   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
22813
22814   if (AGGREGATE_TYPE_P (type)
22815       && TYPE_SIZE (type)
22816       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
22817       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
22818           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
22819       && align < max_align)
22820     align = max_align;
22821
22822   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
22823      to 16byte boundary.  */
22824   if (TARGET_64BIT)
22825     {
22826       if (AGGREGATE_TYPE_P (type)
22827            && TYPE_SIZE (type)
22828            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
22829            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
22830                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
22831         return 128;
22832     }
22833
22834   if (TREE_CODE (type) == ARRAY_TYPE)
22835     {
22836       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
22837         return 64;
22838       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
22839         return 128;
22840     }
22841   else if (TREE_CODE (type) == COMPLEX_TYPE)
22842     {
22843
22844       if (TYPE_MODE (type) == DCmode && align < 64)
22845         return 64;
22846       if ((TYPE_MODE (type) == XCmode
22847            || TYPE_MODE (type) == TCmode) && align < 128)
22848         return 128;
22849     }
22850   else if ((TREE_CODE (type) == RECORD_TYPE
22851             || TREE_CODE (type) == UNION_TYPE
22852             || TREE_CODE (type) == QUAL_UNION_TYPE)
22853            && TYPE_FIELDS (type))
22854     {
22855       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
22856         return 64;
22857       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
22858         return 128;
22859     }
22860   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
22861            || TREE_CODE (type) == INTEGER_TYPE)
22862     {
22863       if (TYPE_MODE (type) == DFmode && align < 64)
22864         return 64;
22865       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
22866         return 128;
22867     }
22868
22869   return align;
22870 }
22871
22872 /* Compute the alignment for a local variable or a stack slot.  EXP is
22873    the data type or decl itself, MODE is the widest mode available and
22874    ALIGN is the alignment that the object would ordinarily have.  The
22875    value of this macro is used instead of that alignment to align the
22876    object.  */
22877
22878 unsigned int
22879 ix86_local_alignment (tree exp, enum machine_mode mode,
22880                       unsigned int align)
22881 {
22882   tree type, decl;
22883
22884   if (exp && DECL_P (exp))
22885     {
22886       type = TREE_TYPE (exp);
22887       decl = exp;
22888     }
22889   else
22890     {
22891       type = exp;
22892       decl = NULL;
22893     }
22894
22895   /* Don't do dynamic stack realignment for long long objects with
22896      -mpreferred-stack-boundary=2.  */
22897   if (!TARGET_64BIT
22898       && align == 64
22899       && ix86_preferred_stack_boundary < 64
22900       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
22901       && (!type || !TYPE_USER_ALIGN (type))
22902       && (!decl || !DECL_USER_ALIGN (decl)))
22903     align = 32;
22904
22905   /* If TYPE is NULL, we are allocating a stack slot for caller-save
22906      register in MODE.  We will return the largest alignment of XF
22907      and DF.  */
22908   if (!type)
22909     {
22910       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
22911         align = GET_MODE_ALIGNMENT (DFmode);
22912       return align;
22913     }
22914
22915   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
22916      to 16byte boundary.  Exact wording is:
22917
22918      An array uses the same alignment as its elements, except that a local or
22919      global array variable of length at least 16 bytes or
22920      a C99 variable-length array variable always has alignment of at least 16 bytes.
22921
22922      This was added to allow use of aligned SSE instructions at arrays.  This
22923      rule is meant for static storage (where compiler can not do the analysis
22924      by itself).  We follow it for automatic variables only when convenient.
22925      We fully control everything in the function compiled and functions from
22926      other unit can not rely on the alignment.
22927
22928      Exclude va_list type.  It is the common case of local array where
22929      we can not benefit from the alignment.  */
22930   if (TARGET_64BIT && optimize_function_for_speed_p (cfun)
22931       && TARGET_SSE)
22932     {
22933       if (AGGREGATE_TYPE_P (type)
22934            && (va_list_type_node == NULL_TREE
22935                || (TYPE_MAIN_VARIANT (type)
22936                    != TYPE_MAIN_VARIANT (va_list_type_node)))
22937            && TYPE_SIZE (type)
22938            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
22939            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
22940                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
22941         return 128;
22942     }
22943   if (TREE_CODE (type) == ARRAY_TYPE)
22944     {
22945       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
22946         return 64;
22947       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
22948         return 128;
22949     }
22950   else if (TREE_CODE (type) == COMPLEX_TYPE)
22951     {
22952       if (TYPE_MODE (type) == DCmode && align < 64)
22953         return 64;
22954       if ((TYPE_MODE (type) == XCmode
22955            || TYPE_MODE (type) == TCmode) && align < 128)
22956         return 128;
22957     }
22958   else if ((TREE_CODE (type) == RECORD_TYPE
22959             || TREE_CODE (type) == UNION_TYPE
22960             || TREE_CODE (type) == QUAL_UNION_TYPE)
22961            && TYPE_FIELDS (type))
22962     {
22963       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
22964         return 64;
22965       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
22966         return 128;
22967     }
22968   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
22969            || TREE_CODE (type) == INTEGER_TYPE)
22970     {
22971
22972       if (TYPE_MODE (type) == DFmode && align < 64)
22973         return 64;
22974       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
22975         return 128;
22976     }
22977   return align;
22978 }
22979
22980 /* Compute the minimum required alignment for dynamic stack realignment
22981    purposes for a local variable, parameter or a stack slot.  EXP is
22982    the data type or decl itself, MODE is its mode and ALIGN is the
22983    alignment that the object would ordinarily have.  */
22984
22985 unsigned int
22986 ix86_minimum_alignment (tree exp, enum machine_mode mode,
22987                         unsigned int align)
22988 {
22989   tree type, decl;
22990
22991   if (exp && DECL_P (exp))
22992     {
22993       type = TREE_TYPE (exp);
22994       decl = exp;
22995     }
22996   else
22997     {
22998       type = exp;
22999       decl = NULL;
23000     }
23001
23002   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
23003     return align;
23004
23005   /* Don't do dynamic stack realignment for long long objects with
23006      -mpreferred-stack-boundary=2.  */
23007   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
23008       && (!type || !TYPE_USER_ALIGN (type))
23009       && (!decl || !DECL_USER_ALIGN (decl)))
23010     return 32;
23011
23012   return align;
23013 }
23014 \f
23015 /* Find a location for the static chain incoming to a nested function.
23016    This is a register, unless all free registers are used by arguments.  */
23017
23018 static rtx
23019 ix86_static_chain (const_tree fndecl, bool incoming_p)
23020 {
23021   unsigned regno;
23022
23023   if (!DECL_STATIC_CHAIN (fndecl))
23024     return NULL;
23025
23026   if (TARGET_64BIT)
23027     {
23028       /* We always use R10 in 64-bit mode.  */
23029       regno = R10_REG;
23030     }
23031   else
23032     {
23033       tree fntype;
23034       /* By default in 32-bit mode we use ECX to pass the static chain.  */
23035       regno = CX_REG;
23036
23037       fntype = TREE_TYPE (fndecl);
23038       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
23039         {
23040           /* Fastcall functions use ecx/edx for arguments, which leaves
23041              us with EAX for the static chain.  */
23042           regno = AX_REG;
23043         }
23044       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
23045         {
23046           /* Thiscall functions use ecx for arguments, which leaves
23047              us with EAX for the static chain.  */
23048           regno = AX_REG;
23049         }
23050       else if (ix86_function_regparm (fntype, fndecl) == 3)
23051         {
23052           /* For regparm 3, we have no free call-clobbered registers in
23053              which to store the static chain.  In order to implement this,
23054              we have the trampoline push the static chain to the stack.
23055              However, we can't push a value below the return address when
23056              we call the nested function directly, so we have to use an
23057              alternate entry point.  For this we use ESI, and have the
23058              alternate entry point push ESI, so that things appear the
23059              same once we're executing the nested function.  */
23060           if (incoming_p)
23061             {
23062               if (fndecl == current_function_decl)
23063                 ix86_static_chain_on_stack = true;
23064               return gen_frame_mem (SImode,
23065                                     plus_constant (arg_pointer_rtx, -8));
23066             }
23067           regno = SI_REG;
23068         }
23069     }
23070
23071   return gen_rtx_REG (Pmode, regno);
23072 }
23073
23074 /* Emit RTL insns to initialize the variable parts of a trampoline.
23075    FNDECL is the decl of the target address; M_TRAMP is a MEM for
23076    the trampoline, and CHAIN_VALUE is an RTX for the static chain
23077    to be passed to the target function.  */
23078
23079 static void
23080 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
23081 {
23082   rtx mem, fnaddr;
23083
23084   fnaddr = XEXP (DECL_RTL (fndecl), 0);
23085
23086   if (!TARGET_64BIT)
23087     {
23088       rtx disp, chain;
23089       int opcode;
23090
23091       /* Depending on the static chain location, either load a register
23092          with a constant, or push the constant to the stack.  All of the
23093          instructions are the same size.  */
23094       chain = ix86_static_chain (fndecl, true);
23095       if (REG_P (chain))
23096         {
23097           if (REGNO (chain) == CX_REG)
23098             opcode = 0xb9;
23099           else if (REGNO (chain) == AX_REG)
23100             opcode = 0xb8;
23101           else
23102             gcc_unreachable ();
23103         }
23104       else
23105         opcode = 0x68;
23106
23107       mem = adjust_address (m_tramp, QImode, 0);
23108       emit_move_insn (mem, gen_int_mode (opcode, QImode));
23109
23110       mem = adjust_address (m_tramp, SImode, 1);
23111       emit_move_insn (mem, chain_value);
23112
23113       /* Compute offset from the end of the jmp to the target function.
23114          In the case in which the trampoline stores the static chain on
23115          the stack, we need to skip the first insn which pushes the
23116          (call-saved) register static chain; this push is 1 byte.  */
23117       disp = expand_binop (SImode, sub_optab, fnaddr,
23118                            plus_constant (XEXP (m_tramp, 0),
23119                                           MEM_P (chain) ? 9 : 10),
23120                            NULL_RTX, 1, OPTAB_DIRECT);
23121
23122       mem = adjust_address (m_tramp, QImode, 5);
23123       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
23124
23125       mem = adjust_address (m_tramp, SImode, 6);
23126       emit_move_insn (mem, disp);
23127     }
23128   else
23129     {
23130       int offset = 0;
23131
23132       /* Load the function address to r11.  Try to load address using
23133          the shorter movl instead of movabs.  We may want to support
23134          movq for kernel mode, but kernel does not use trampolines at
23135          the moment.  */
23136       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
23137         {
23138           fnaddr = copy_to_mode_reg (DImode, fnaddr);
23139
23140           mem = adjust_address (m_tramp, HImode, offset);
23141           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
23142
23143           mem = adjust_address (m_tramp, SImode, offset + 2);
23144           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
23145           offset += 6;
23146         }
23147       else
23148         {
23149           mem = adjust_address (m_tramp, HImode, offset);
23150           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
23151
23152           mem = adjust_address (m_tramp, DImode, offset + 2);
23153           emit_move_insn (mem, fnaddr);
23154           offset += 10;
23155         }
23156
23157       /* Load static chain using movabs to r10.  */
23158       mem = adjust_address (m_tramp, HImode, offset);
23159       emit_move_insn (mem, gen_int_mode (0xba49, HImode));
23160
23161       mem = adjust_address (m_tramp, DImode, offset + 2);
23162       emit_move_insn (mem, chain_value);
23163       offset += 10;
23164
23165       /* Jump to r11; the last (unused) byte is a nop, only there to
23166          pad the write out to a single 32-bit store.  */
23167       mem = adjust_address (m_tramp, SImode, offset);
23168       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
23169       offset += 4;
23170
23171       gcc_assert (offset <= TRAMPOLINE_SIZE);
23172     }
23173
23174 #ifdef ENABLE_EXECUTE_STACK
23175 #ifdef CHECK_EXECUTE_STACK_ENABLED
23176   if (CHECK_EXECUTE_STACK_ENABLED)
23177 #endif
23178   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
23179                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
23180 #endif
23181 }
23182 \f
23183 /* The following file contains several enumerations and data structures
23184    built from the definitions in i386-builtin-types.def.  */
23185
23186 #include "i386-builtin-types.inc"
23187
23188 /* Table for the ix86 builtin non-function types.  */
23189 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
23190
23191 /* Retrieve an element from the above table, building some of
23192    the types lazily.  */
23193
23194 static tree
23195 ix86_get_builtin_type (enum ix86_builtin_type tcode)
23196 {
23197   unsigned int index;
23198   tree type, itype;
23199
23200   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
23201
23202   type = ix86_builtin_type_tab[(int) tcode];
23203   if (type != NULL)
23204     return type;
23205
23206   gcc_assert (tcode > IX86_BT_LAST_PRIM);
23207   if (tcode <= IX86_BT_LAST_VECT)
23208     {
23209       enum machine_mode mode;
23210
23211       index = tcode - IX86_BT_LAST_PRIM - 1;
23212       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
23213       mode = ix86_builtin_type_vect_mode[index];
23214
23215       type = build_vector_type_for_mode (itype, mode);
23216     }
23217   else
23218     {
23219       int quals;
23220
23221       index = tcode - IX86_BT_LAST_VECT - 1;
23222       if (tcode <= IX86_BT_LAST_PTR)
23223         quals = TYPE_UNQUALIFIED;
23224       else
23225         quals = TYPE_QUAL_CONST;
23226
23227       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
23228       if (quals != TYPE_UNQUALIFIED)
23229         itype = build_qualified_type (itype, quals);
23230
23231       type = build_pointer_type (itype);
23232     }
23233
23234   ix86_builtin_type_tab[(int) tcode] = type;
23235   return type;
23236 }
23237
23238 /* Table for the ix86 builtin function types.  */
23239 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
23240
23241 /* Retrieve an element from the above table, building some of
23242    the types lazily.  */
23243
23244 static tree
23245 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
23246 {
23247   tree type;
23248
23249   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
23250
23251   type = ix86_builtin_func_type_tab[(int) tcode];
23252   if (type != NULL)
23253     return type;
23254
23255   if (tcode <= IX86_BT_LAST_FUNC)
23256     {
23257       unsigned start = ix86_builtin_func_start[(int) tcode];
23258       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
23259       tree rtype, atype, args = void_list_node;
23260       unsigned i;
23261
23262       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
23263       for (i = after - 1; i > start; --i)
23264         {
23265           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
23266           args = tree_cons (NULL, atype, args);
23267         }
23268
23269       type = build_function_type (rtype, args);
23270     }
23271   else
23272     {
23273       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
23274       enum ix86_builtin_func_type icode;
23275
23276       icode = ix86_builtin_func_alias_base[index];
23277       type = ix86_get_builtin_func_type (icode);
23278     }
23279
23280   ix86_builtin_func_type_tab[(int) tcode] = type;
23281   return type;
23282 }
23283
23284
23285 /* Codes for all the SSE/MMX builtins.  */
23286 enum ix86_builtins
23287 {
23288   IX86_BUILTIN_ADDPS,
23289   IX86_BUILTIN_ADDSS,
23290   IX86_BUILTIN_DIVPS,
23291   IX86_BUILTIN_DIVSS,
23292   IX86_BUILTIN_MULPS,
23293   IX86_BUILTIN_MULSS,
23294   IX86_BUILTIN_SUBPS,
23295   IX86_BUILTIN_SUBSS,
23296
23297   IX86_BUILTIN_CMPEQPS,
23298   IX86_BUILTIN_CMPLTPS,
23299   IX86_BUILTIN_CMPLEPS,
23300   IX86_BUILTIN_CMPGTPS,
23301   IX86_BUILTIN_CMPGEPS,
23302   IX86_BUILTIN_CMPNEQPS,
23303   IX86_BUILTIN_CMPNLTPS,
23304   IX86_BUILTIN_CMPNLEPS,
23305   IX86_BUILTIN_CMPNGTPS,
23306   IX86_BUILTIN_CMPNGEPS,
23307   IX86_BUILTIN_CMPORDPS,
23308   IX86_BUILTIN_CMPUNORDPS,
23309   IX86_BUILTIN_CMPEQSS,
23310   IX86_BUILTIN_CMPLTSS,
23311   IX86_BUILTIN_CMPLESS,
23312   IX86_BUILTIN_CMPNEQSS,
23313   IX86_BUILTIN_CMPNLTSS,
23314   IX86_BUILTIN_CMPNLESS,
23315   IX86_BUILTIN_CMPNGTSS,
23316   IX86_BUILTIN_CMPNGESS,
23317   IX86_BUILTIN_CMPORDSS,
23318   IX86_BUILTIN_CMPUNORDSS,
23319
23320   IX86_BUILTIN_COMIEQSS,
23321   IX86_BUILTIN_COMILTSS,
23322   IX86_BUILTIN_COMILESS,
23323   IX86_BUILTIN_COMIGTSS,
23324   IX86_BUILTIN_COMIGESS,
23325   IX86_BUILTIN_COMINEQSS,
23326   IX86_BUILTIN_UCOMIEQSS,
23327   IX86_BUILTIN_UCOMILTSS,
23328   IX86_BUILTIN_UCOMILESS,
23329   IX86_BUILTIN_UCOMIGTSS,
23330   IX86_BUILTIN_UCOMIGESS,
23331   IX86_BUILTIN_UCOMINEQSS,
23332
23333   IX86_BUILTIN_CVTPI2PS,
23334   IX86_BUILTIN_CVTPS2PI,
23335   IX86_BUILTIN_CVTSI2SS,
23336   IX86_BUILTIN_CVTSI642SS,
23337   IX86_BUILTIN_CVTSS2SI,
23338   IX86_BUILTIN_CVTSS2SI64,
23339   IX86_BUILTIN_CVTTPS2PI,
23340   IX86_BUILTIN_CVTTSS2SI,
23341   IX86_BUILTIN_CVTTSS2SI64,
23342
23343   IX86_BUILTIN_MAXPS,
23344   IX86_BUILTIN_MAXSS,
23345   IX86_BUILTIN_MINPS,
23346   IX86_BUILTIN_MINSS,
23347
23348   IX86_BUILTIN_LOADUPS,
23349   IX86_BUILTIN_STOREUPS,
23350   IX86_BUILTIN_MOVSS,
23351
23352   IX86_BUILTIN_MOVHLPS,
23353   IX86_BUILTIN_MOVLHPS,
23354   IX86_BUILTIN_LOADHPS,
23355   IX86_BUILTIN_LOADLPS,
23356   IX86_BUILTIN_STOREHPS,
23357   IX86_BUILTIN_STORELPS,
23358
23359   IX86_BUILTIN_MASKMOVQ,
23360   IX86_BUILTIN_MOVMSKPS,
23361   IX86_BUILTIN_PMOVMSKB,
23362
23363   IX86_BUILTIN_MOVNTPS,
23364   IX86_BUILTIN_MOVNTQ,
23365
23366   IX86_BUILTIN_LOADDQU,
23367   IX86_BUILTIN_STOREDQU,
23368
23369   IX86_BUILTIN_PACKSSWB,
23370   IX86_BUILTIN_PACKSSDW,
23371   IX86_BUILTIN_PACKUSWB,
23372
23373   IX86_BUILTIN_PADDB,
23374   IX86_BUILTIN_PADDW,
23375   IX86_BUILTIN_PADDD,
23376   IX86_BUILTIN_PADDQ,
23377   IX86_BUILTIN_PADDSB,
23378   IX86_BUILTIN_PADDSW,
23379   IX86_BUILTIN_PADDUSB,
23380   IX86_BUILTIN_PADDUSW,
23381   IX86_BUILTIN_PSUBB,
23382   IX86_BUILTIN_PSUBW,
23383   IX86_BUILTIN_PSUBD,
23384   IX86_BUILTIN_PSUBQ,
23385   IX86_BUILTIN_PSUBSB,
23386   IX86_BUILTIN_PSUBSW,
23387   IX86_BUILTIN_PSUBUSB,
23388   IX86_BUILTIN_PSUBUSW,
23389
23390   IX86_BUILTIN_PAND,
23391   IX86_BUILTIN_PANDN,
23392   IX86_BUILTIN_POR,
23393   IX86_BUILTIN_PXOR,
23394
23395   IX86_BUILTIN_PAVGB,
23396   IX86_BUILTIN_PAVGW,
23397
23398   IX86_BUILTIN_PCMPEQB,
23399   IX86_BUILTIN_PCMPEQW,
23400   IX86_BUILTIN_PCMPEQD,
23401   IX86_BUILTIN_PCMPGTB,
23402   IX86_BUILTIN_PCMPGTW,
23403   IX86_BUILTIN_PCMPGTD,
23404
23405   IX86_BUILTIN_PMADDWD,
23406
23407   IX86_BUILTIN_PMAXSW,
23408   IX86_BUILTIN_PMAXUB,
23409   IX86_BUILTIN_PMINSW,
23410   IX86_BUILTIN_PMINUB,
23411
23412   IX86_BUILTIN_PMULHUW,
23413   IX86_BUILTIN_PMULHW,
23414   IX86_BUILTIN_PMULLW,
23415
23416   IX86_BUILTIN_PSADBW,
23417   IX86_BUILTIN_PSHUFW,
23418
23419   IX86_BUILTIN_PSLLW,
23420   IX86_BUILTIN_PSLLD,
23421   IX86_BUILTIN_PSLLQ,
23422   IX86_BUILTIN_PSRAW,
23423   IX86_BUILTIN_PSRAD,
23424   IX86_BUILTIN_PSRLW,
23425   IX86_BUILTIN_PSRLD,
23426   IX86_BUILTIN_PSRLQ,
23427   IX86_BUILTIN_PSLLWI,
23428   IX86_BUILTIN_PSLLDI,
23429   IX86_BUILTIN_PSLLQI,
23430   IX86_BUILTIN_PSRAWI,
23431   IX86_BUILTIN_PSRADI,
23432   IX86_BUILTIN_PSRLWI,
23433   IX86_BUILTIN_PSRLDI,
23434   IX86_BUILTIN_PSRLQI,
23435
23436   IX86_BUILTIN_PUNPCKHBW,
23437   IX86_BUILTIN_PUNPCKHWD,
23438   IX86_BUILTIN_PUNPCKHDQ,
23439   IX86_BUILTIN_PUNPCKLBW,
23440   IX86_BUILTIN_PUNPCKLWD,
23441   IX86_BUILTIN_PUNPCKLDQ,
23442
23443   IX86_BUILTIN_SHUFPS,
23444
23445   IX86_BUILTIN_RCPPS,
23446   IX86_BUILTIN_RCPSS,
23447   IX86_BUILTIN_RSQRTPS,
23448   IX86_BUILTIN_RSQRTPS_NR,
23449   IX86_BUILTIN_RSQRTSS,
23450   IX86_BUILTIN_RSQRTF,
23451   IX86_BUILTIN_SQRTPS,
23452   IX86_BUILTIN_SQRTPS_NR,
23453   IX86_BUILTIN_SQRTSS,
23454
23455   IX86_BUILTIN_UNPCKHPS,
23456   IX86_BUILTIN_UNPCKLPS,
23457
23458   IX86_BUILTIN_ANDPS,
23459   IX86_BUILTIN_ANDNPS,
23460   IX86_BUILTIN_ORPS,
23461   IX86_BUILTIN_XORPS,
23462
23463   IX86_BUILTIN_EMMS,
23464   IX86_BUILTIN_LDMXCSR,
23465   IX86_BUILTIN_STMXCSR,
23466   IX86_BUILTIN_SFENCE,
23467
23468   /* 3DNow! Original */
23469   IX86_BUILTIN_FEMMS,
23470   IX86_BUILTIN_PAVGUSB,
23471   IX86_BUILTIN_PF2ID,
23472   IX86_BUILTIN_PFACC,
23473   IX86_BUILTIN_PFADD,
23474   IX86_BUILTIN_PFCMPEQ,
23475   IX86_BUILTIN_PFCMPGE,
23476   IX86_BUILTIN_PFCMPGT,
23477   IX86_BUILTIN_PFMAX,
23478   IX86_BUILTIN_PFMIN,
23479   IX86_BUILTIN_PFMUL,
23480   IX86_BUILTIN_PFRCP,
23481   IX86_BUILTIN_PFRCPIT1,
23482   IX86_BUILTIN_PFRCPIT2,
23483   IX86_BUILTIN_PFRSQIT1,
23484   IX86_BUILTIN_PFRSQRT,
23485   IX86_BUILTIN_PFSUB,
23486   IX86_BUILTIN_PFSUBR,
23487   IX86_BUILTIN_PI2FD,
23488   IX86_BUILTIN_PMULHRW,
23489
23490   /* 3DNow! Athlon Extensions */
23491   IX86_BUILTIN_PF2IW,
23492   IX86_BUILTIN_PFNACC,
23493   IX86_BUILTIN_PFPNACC,
23494   IX86_BUILTIN_PI2FW,
23495   IX86_BUILTIN_PSWAPDSI,
23496   IX86_BUILTIN_PSWAPDSF,
23497
23498   /* SSE2 */
23499   IX86_BUILTIN_ADDPD,
23500   IX86_BUILTIN_ADDSD,
23501   IX86_BUILTIN_DIVPD,
23502   IX86_BUILTIN_DIVSD,
23503   IX86_BUILTIN_MULPD,
23504   IX86_BUILTIN_MULSD,
23505   IX86_BUILTIN_SUBPD,
23506   IX86_BUILTIN_SUBSD,
23507
23508   IX86_BUILTIN_CMPEQPD,
23509   IX86_BUILTIN_CMPLTPD,
23510   IX86_BUILTIN_CMPLEPD,
23511   IX86_BUILTIN_CMPGTPD,
23512   IX86_BUILTIN_CMPGEPD,
23513   IX86_BUILTIN_CMPNEQPD,
23514   IX86_BUILTIN_CMPNLTPD,
23515   IX86_BUILTIN_CMPNLEPD,
23516   IX86_BUILTIN_CMPNGTPD,
23517   IX86_BUILTIN_CMPNGEPD,
23518   IX86_BUILTIN_CMPORDPD,
23519   IX86_BUILTIN_CMPUNORDPD,
23520   IX86_BUILTIN_CMPEQSD,
23521   IX86_BUILTIN_CMPLTSD,
23522   IX86_BUILTIN_CMPLESD,
23523   IX86_BUILTIN_CMPNEQSD,
23524   IX86_BUILTIN_CMPNLTSD,
23525   IX86_BUILTIN_CMPNLESD,
23526   IX86_BUILTIN_CMPORDSD,
23527   IX86_BUILTIN_CMPUNORDSD,
23528
23529   IX86_BUILTIN_COMIEQSD,
23530   IX86_BUILTIN_COMILTSD,
23531   IX86_BUILTIN_COMILESD,
23532   IX86_BUILTIN_COMIGTSD,
23533   IX86_BUILTIN_COMIGESD,
23534   IX86_BUILTIN_COMINEQSD,
23535   IX86_BUILTIN_UCOMIEQSD,
23536   IX86_BUILTIN_UCOMILTSD,
23537   IX86_BUILTIN_UCOMILESD,
23538   IX86_BUILTIN_UCOMIGTSD,
23539   IX86_BUILTIN_UCOMIGESD,
23540   IX86_BUILTIN_UCOMINEQSD,
23541
23542   IX86_BUILTIN_MAXPD,
23543   IX86_BUILTIN_MAXSD,
23544   IX86_BUILTIN_MINPD,
23545   IX86_BUILTIN_MINSD,
23546
23547   IX86_BUILTIN_ANDPD,
23548   IX86_BUILTIN_ANDNPD,
23549   IX86_BUILTIN_ORPD,
23550   IX86_BUILTIN_XORPD,
23551
23552   IX86_BUILTIN_SQRTPD,
23553   IX86_BUILTIN_SQRTSD,
23554
23555   IX86_BUILTIN_UNPCKHPD,
23556   IX86_BUILTIN_UNPCKLPD,
23557
23558   IX86_BUILTIN_SHUFPD,
23559
23560   IX86_BUILTIN_LOADUPD,
23561   IX86_BUILTIN_STOREUPD,
23562   IX86_BUILTIN_MOVSD,
23563
23564   IX86_BUILTIN_LOADHPD,
23565   IX86_BUILTIN_LOADLPD,
23566
23567   IX86_BUILTIN_CVTDQ2PD,
23568   IX86_BUILTIN_CVTDQ2PS,
23569
23570   IX86_BUILTIN_CVTPD2DQ,
23571   IX86_BUILTIN_CVTPD2PI,
23572   IX86_BUILTIN_CVTPD2PS,
23573   IX86_BUILTIN_CVTTPD2DQ,
23574   IX86_BUILTIN_CVTTPD2PI,
23575
23576   IX86_BUILTIN_CVTPI2PD,
23577   IX86_BUILTIN_CVTSI2SD,
23578   IX86_BUILTIN_CVTSI642SD,
23579
23580   IX86_BUILTIN_CVTSD2SI,
23581   IX86_BUILTIN_CVTSD2SI64,
23582   IX86_BUILTIN_CVTSD2SS,
23583   IX86_BUILTIN_CVTSS2SD,
23584   IX86_BUILTIN_CVTTSD2SI,
23585   IX86_BUILTIN_CVTTSD2SI64,
23586
23587   IX86_BUILTIN_CVTPS2DQ,
23588   IX86_BUILTIN_CVTPS2PD,
23589   IX86_BUILTIN_CVTTPS2DQ,
23590
23591   IX86_BUILTIN_MOVNTI,
23592   IX86_BUILTIN_MOVNTPD,
23593   IX86_BUILTIN_MOVNTDQ,
23594
23595   IX86_BUILTIN_MOVQ128,
23596
23597   /* SSE2 MMX */
23598   IX86_BUILTIN_MASKMOVDQU,
23599   IX86_BUILTIN_MOVMSKPD,
23600   IX86_BUILTIN_PMOVMSKB128,
23601
23602   IX86_BUILTIN_PACKSSWB128,
23603   IX86_BUILTIN_PACKSSDW128,
23604   IX86_BUILTIN_PACKUSWB128,
23605
23606   IX86_BUILTIN_PADDB128,
23607   IX86_BUILTIN_PADDW128,
23608   IX86_BUILTIN_PADDD128,
23609   IX86_BUILTIN_PADDQ128,
23610   IX86_BUILTIN_PADDSB128,
23611   IX86_BUILTIN_PADDSW128,
23612   IX86_BUILTIN_PADDUSB128,
23613   IX86_BUILTIN_PADDUSW128,
23614   IX86_BUILTIN_PSUBB128,
23615   IX86_BUILTIN_PSUBW128,
23616   IX86_BUILTIN_PSUBD128,
23617   IX86_BUILTIN_PSUBQ128,
23618   IX86_BUILTIN_PSUBSB128,
23619   IX86_BUILTIN_PSUBSW128,
23620   IX86_BUILTIN_PSUBUSB128,
23621   IX86_BUILTIN_PSUBUSW128,
23622
23623   IX86_BUILTIN_PAND128,
23624   IX86_BUILTIN_PANDN128,
23625   IX86_BUILTIN_POR128,
23626   IX86_BUILTIN_PXOR128,
23627
23628   IX86_BUILTIN_PAVGB128,
23629   IX86_BUILTIN_PAVGW128,
23630
23631   IX86_BUILTIN_PCMPEQB128,
23632   IX86_BUILTIN_PCMPEQW128,
23633   IX86_BUILTIN_PCMPEQD128,
23634   IX86_BUILTIN_PCMPGTB128,
23635   IX86_BUILTIN_PCMPGTW128,
23636   IX86_BUILTIN_PCMPGTD128,
23637
23638   IX86_BUILTIN_PMADDWD128,
23639
23640   IX86_BUILTIN_PMAXSW128,
23641   IX86_BUILTIN_PMAXUB128,
23642   IX86_BUILTIN_PMINSW128,
23643   IX86_BUILTIN_PMINUB128,
23644
23645   IX86_BUILTIN_PMULUDQ,
23646   IX86_BUILTIN_PMULUDQ128,
23647   IX86_BUILTIN_PMULHUW128,
23648   IX86_BUILTIN_PMULHW128,
23649   IX86_BUILTIN_PMULLW128,
23650
23651   IX86_BUILTIN_PSADBW128,
23652   IX86_BUILTIN_PSHUFHW,
23653   IX86_BUILTIN_PSHUFLW,
23654   IX86_BUILTIN_PSHUFD,
23655
23656   IX86_BUILTIN_PSLLDQI128,
23657   IX86_BUILTIN_PSLLWI128,
23658   IX86_BUILTIN_PSLLDI128,
23659   IX86_BUILTIN_PSLLQI128,
23660   IX86_BUILTIN_PSRAWI128,
23661   IX86_BUILTIN_PSRADI128,
23662   IX86_BUILTIN_PSRLDQI128,
23663   IX86_BUILTIN_PSRLWI128,
23664   IX86_BUILTIN_PSRLDI128,
23665   IX86_BUILTIN_PSRLQI128,
23666
23667   IX86_BUILTIN_PSLLDQ128,
23668   IX86_BUILTIN_PSLLW128,
23669   IX86_BUILTIN_PSLLD128,
23670   IX86_BUILTIN_PSLLQ128,
23671   IX86_BUILTIN_PSRAW128,
23672   IX86_BUILTIN_PSRAD128,
23673   IX86_BUILTIN_PSRLW128,
23674   IX86_BUILTIN_PSRLD128,
23675   IX86_BUILTIN_PSRLQ128,
23676
23677   IX86_BUILTIN_PUNPCKHBW128,
23678   IX86_BUILTIN_PUNPCKHWD128,
23679   IX86_BUILTIN_PUNPCKHDQ128,
23680   IX86_BUILTIN_PUNPCKHQDQ128,
23681   IX86_BUILTIN_PUNPCKLBW128,
23682   IX86_BUILTIN_PUNPCKLWD128,
23683   IX86_BUILTIN_PUNPCKLDQ128,
23684   IX86_BUILTIN_PUNPCKLQDQ128,
23685
23686   IX86_BUILTIN_CLFLUSH,
23687   IX86_BUILTIN_MFENCE,
23688   IX86_BUILTIN_LFENCE,
23689
23690   IX86_BUILTIN_BSRSI,
23691   IX86_BUILTIN_BSRDI,
23692   IX86_BUILTIN_RDPMC,
23693   IX86_BUILTIN_RDTSC,
23694   IX86_BUILTIN_RDTSCP,
23695   IX86_BUILTIN_ROLQI,
23696   IX86_BUILTIN_ROLHI,
23697   IX86_BUILTIN_RORQI,
23698   IX86_BUILTIN_RORHI,
23699
23700   /* SSE3.  */
23701   IX86_BUILTIN_ADDSUBPS,
23702   IX86_BUILTIN_HADDPS,
23703   IX86_BUILTIN_HSUBPS,
23704   IX86_BUILTIN_MOVSHDUP,
23705   IX86_BUILTIN_MOVSLDUP,
23706   IX86_BUILTIN_ADDSUBPD,
23707   IX86_BUILTIN_HADDPD,
23708   IX86_BUILTIN_HSUBPD,
23709   IX86_BUILTIN_LDDQU,
23710
23711   IX86_BUILTIN_MONITOR,
23712   IX86_BUILTIN_MWAIT,
23713
23714   /* SSSE3.  */
23715   IX86_BUILTIN_PHADDW,
23716   IX86_BUILTIN_PHADDD,
23717   IX86_BUILTIN_PHADDSW,
23718   IX86_BUILTIN_PHSUBW,
23719   IX86_BUILTIN_PHSUBD,
23720   IX86_BUILTIN_PHSUBSW,
23721   IX86_BUILTIN_PMADDUBSW,
23722   IX86_BUILTIN_PMULHRSW,
23723   IX86_BUILTIN_PSHUFB,
23724   IX86_BUILTIN_PSIGNB,
23725   IX86_BUILTIN_PSIGNW,
23726   IX86_BUILTIN_PSIGND,
23727   IX86_BUILTIN_PALIGNR,
23728   IX86_BUILTIN_PABSB,
23729   IX86_BUILTIN_PABSW,
23730   IX86_BUILTIN_PABSD,
23731
23732   IX86_BUILTIN_PHADDW128,
23733   IX86_BUILTIN_PHADDD128,
23734   IX86_BUILTIN_PHADDSW128,
23735   IX86_BUILTIN_PHSUBW128,
23736   IX86_BUILTIN_PHSUBD128,
23737   IX86_BUILTIN_PHSUBSW128,
23738   IX86_BUILTIN_PMADDUBSW128,
23739   IX86_BUILTIN_PMULHRSW128,
23740   IX86_BUILTIN_PSHUFB128,
23741   IX86_BUILTIN_PSIGNB128,
23742   IX86_BUILTIN_PSIGNW128,
23743   IX86_BUILTIN_PSIGND128,
23744   IX86_BUILTIN_PALIGNR128,
23745   IX86_BUILTIN_PABSB128,
23746   IX86_BUILTIN_PABSW128,
23747   IX86_BUILTIN_PABSD128,
23748
23749   /* AMDFAM10 - SSE4A New Instructions.  */
23750   IX86_BUILTIN_MOVNTSD,
23751   IX86_BUILTIN_MOVNTSS,
23752   IX86_BUILTIN_EXTRQI,
23753   IX86_BUILTIN_EXTRQ,
23754   IX86_BUILTIN_INSERTQI,
23755   IX86_BUILTIN_INSERTQ,
23756
23757   /* SSE4.1.  */
23758   IX86_BUILTIN_BLENDPD,
23759   IX86_BUILTIN_BLENDPS,
23760   IX86_BUILTIN_BLENDVPD,
23761   IX86_BUILTIN_BLENDVPS,
23762   IX86_BUILTIN_PBLENDVB128,
23763   IX86_BUILTIN_PBLENDW128,
23764
23765   IX86_BUILTIN_DPPD,
23766   IX86_BUILTIN_DPPS,
23767
23768   IX86_BUILTIN_INSERTPS128,
23769
23770   IX86_BUILTIN_MOVNTDQA,
23771   IX86_BUILTIN_MPSADBW128,
23772   IX86_BUILTIN_PACKUSDW128,
23773   IX86_BUILTIN_PCMPEQQ,
23774   IX86_BUILTIN_PHMINPOSUW128,
23775
23776   IX86_BUILTIN_PMAXSB128,
23777   IX86_BUILTIN_PMAXSD128,
23778   IX86_BUILTIN_PMAXUD128,
23779   IX86_BUILTIN_PMAXUW128,
23780
23781   IX86_BUILTIN_PMINSB128,
23782   IX86_BUILTIN_PMINSD128,
23783   IX86_BUILTIN_PMINUD128,
23784   IX86_BUILTIN_PMINUW128,
23785
23786   IX86_BUILTIN_PMOVSXBW128,
23787   IX86_BUILTIN_PMOVSXBD128,
23788   IX86_BUILTIN_PMOVSXBQ128,
23789   IX86_BUILTIN_PMOVSXWD128,
23790   IX86_BUILTIN_PMOVSXWQ128,
23791   IX86_BUILTIN_PMOVSXDQ128,
23792
23793   IX86_BUILTIN_PMOVZXBW128,
23794   IX86_BUILTIN_PMOVZXBD128,
23795   IX86_BUILTIN_PMOVZXBQ128,
23796   IX86_BUILTIN_PMOVZXWD128,
23797   IX86_BUILTIN_PMOVZXWQ128,
23798   IX86_BUILTIN_PMOVZXDQ128,
23799
23800   IX86_BUILTIN_PMULDQ128,
23801   IX86_BUILTIN_PMULLD128,
23802
23803   IX86_BUILTIN_ROUNDPD,
23804   IX86_BUILTIN_ROUNDPS,
23805   IX86_BUILTIN_ROUNDSD,
23806   IX86_BUILTIN_ROUNDSS,
23807
23808   IX86_BUILTIN_PTESTZ,
23809   IX86_BUILTIN_PTESTC,
23810   IX86_BUILTIN_PTESTNZC,
23811
23812   IX86_BUILTIN_VEC_INIT_V2SI,
23813   IX86_BUILTIN_VEC_INIT_V4HI,
23814   IX86_BUILTIN_VEC_INIT_V8QI,
23815   IX86_BUILTIN_VEC_EXT_V2DF,
23816   IX86_BUILTIN_VEC_EXT_V2DI,
23817   IX86_BUILTIN_VEC_EXT_V4SF,
23818   IX86_BUILTIN_VEC_EXT_V4SI,
23819   IX86_BUILTIN_VEC_EXT_V8HI,
23820   IX86_BUILTIN_VEC_EXT_V2SI,
23821   IX86_BUILTIN_VEC_EXT_V4HI,
23822   IX86_BUILTIN_VEC_EXT_V16QI,
23823   IX86_BUILTIN_VEC_SET_V2DI,
23824   IX86_BUILTIN_VEC_SET_V4SF,
23825   IX86_BUILTIN_VEC_SET_V4SI,
23826   IX86_BUILTIN_VEC_SET_V8HI,
23827   IX86_BUILTIN_VEC_SET_V4HI,
23828   IX86_BUILTIN_VEC_SET_V16QI,
23829
23830   IX86_BUILTIN_VEC_PACK_SFIX,
23831
23832   /* SSE4.2.  */
23833   IX86_BUILTIN_CRC32QI,
23834   IX86_BUILTIN_CRC32HI,
23835   IX86_BUILTIN_CRC32SI,
23836   IX86_BUILTIN_CRC32DI,
23837
23838   IX86_BUILTIN_PCMPESTRI128,
23839   IX86_BUILTIN_PCMPESTRM128,
23840   IX86_BUILTIN_PCMPESTRA128,
23841   IX86_BUILTIN_PCMPESTRC128,
23842   IX86_BUILTIN_PCMPESTRO128,
23843   IX86_BUILTIN_PCMPESTRS128,
23844   IX86_BUILTIN_PCMPESTRZ128,
23845   IX86_BUILTIN_PCMPISTRI128,
23846   IX86_BUILTIN_PCMPISTRM128,
23847   IX86_BUILTIN_PCMPISTRA128,
23848   IX86_BUILTIN_PCMPISTRC128,
23849   IX86_BUILTIN_PCMPISTRO128,
23850   IX86_BUILTIN_PCMPISTRS128,
23851   IX86_BUILTIN_PCMPISTRZ128,
23852
23853   IX86_BUILTIN_PCMPGTQ,
23854
23855   /* AES instructions */
23856   IX86_BUILTIN_AESENC128,
23857   IX86_BUILTIN_AESENCLAST128,
23858   IX86_BUILTIN_AESDEC128,
23859   IX86_BUILTIN_AESDECLAST128,
23860   IX86_BUILTIN_AESIMC128,
23861   IX86_BUILTIN_AESKEYGENASSIST128,
23862
23863   /* PCLMUL instruction */
23864   IX86_BUILTIN_PCLMULQDQ128,
23865
23866   /* AVX */
23867   IX86_BUILTIN_ADDPD256,
23868   IX86_BUILTIN_ADDPS256,
23869   IX86_BUILTIN_ADDSUBPD256,
23870   IX86_BUILTIN_ADDSUBPS256,
23871   IX86_BUILTIN_ANDPD256,
23872   IX86_BUILTIN_ANDPS256,
23873   IX86_BUILTIN_ANDNPD256,
23874   IX86_BUILTIN_ANDNPS256,
23875   IX86_BUILTIN_BLENDPD256,
23876   IX86_BUILTIN_BLENDPS256,
23877   IX86_BUILTIN_BLENDVPD256,
23878   IX86_BUILTIN_BLENDVPS256,
23879   IX86_BUILTIN_DIVPD256,
23880   IX86_BUILTIN_DIVPS256,
23881   IX86_BUILTIN_DPPS256,
23882   IX86_BUILTIN_HADDPD256,
23883   IX86_BUILTIN_HADDPS256,
23884   IX86_BUILTIN_HSUBPD256,
23885   IX86_BUILTIN_HSUBPS256,
23886   IX86_BUILTIN_MAXPD256,
23887   IX86_BUILTIN_MAXPS256,
23888   IX86_BUILTIN_MINPD256,
23889   IX86_BUILTIN_MINPS256,
23890   IX86_BUILTIN_MULPD256,
23891   IX86_BUILTIN_MULPS256,
23892   IX86_BUILTIN_ORPD256,
23893   IX86_BUILTIN_ORPS256,
23894   IX86_BUILTIN_SHUFPD256,
23895   IX86_BUILTIN_SHUFPS256,
23896   IX86_BUILTIN_SUBPD256,
23897   IX86_BUILTIN_SUBPS256,
23898   IX86_BUILTIN_XORPD256,
23899   IX86_BUILTIN_XORPS256,
23900   IX86_BUILTIN_CMPSD,
23901   IX86_BUILTIN_CMPSS,
23902   IX86_BUILTIN_CMPPD,
23903   IX86_BUILTIN_CMPPS,
23904   IX86_BUILTIN_CMPPD256,
23905   IX86_BUILTIN_CMPPS256,
23906   IX86_BUILTIN_CVTDQ2PD256,
23907   IX86_BUILTIN_CVTDQ2PS256,
23908   IX86_BUILTIN_CVTPD2PS256,
23909   IX86_BUILTIN_CVTPS2DQ256,
23910   IX86_BUILTIN_CVTPS2PD256,
23911   IX86_BUILTIN_CVTTPD2DQ256,
23912   IX86_BUILTIN_CVTPD2DQ256,
23913   IX86_BUILTIN_CVTTPS2DQ256,
23914   IX86_BUILTIN_EXTRACTF128PD256,
23915   IX86_BUILTIN_EXTRACTF128PS256,
23916   IX86_BUILTIN_EXTRACTF128SI256,
23917   IX86_BUILTIN_VZEROALL,
23918   IX86_BUILTIN_VZEROUPPER,
23919   IX86_BUILTIN_VPERMILVARPD,
23920   IX86_BUILTIN_VPERMILVARPS,
23921   IX86_BUILTIN_VPERMILVARPD256,
23922   IX86_BUILTIN_VPERMILVARPS256,
23923   IX86_BUILTIN_VPERMILPD,
23924   IX86_BUILTIN_VPERMILPS,
23925   IX86_BUILTIN_VPERMILPD256,
23926   IX86_BUILTIN_VPERMILPS256,
23927   IX86_BUILTIN_VPERMIL2PD,
23928   IX86_BUILTIN_VPERMIL2PS,
23929   IX86_BUILTIN_VPERMIL2PD256,
23930   IX86_BUILTIN_VPERMIL2PS256,
23931   IX86_BUILTIN_VPERM2F128PD256,
23932   IX86_BUILTIN_VPERM2F128PS256,
23933   IX86_BUILTIN_VPERM2F128SI256,
23934   IX86_BUILTIN_VBROADCASTSS,
23935   IX86_BUILTIN_VBROADCASTSD256,
23936   IX86_BUILTIN_VBROADCASTSS256,
23937   IX86_BUILTIN_VBROADCASTPD256,
23938   IX86_BUILTIN_VBROADCASTPS256,
23939   IX86_BUILTIN_VINSERTF128PD256,
23940   IX86_BUILTIN_VINSERTF128PS256,
23941   IX86_BUILTIN_VINSERTF128SI256,
23942   IX86_BUILTIN_LOADUPD256,
23943   IX86_BUILTIN_LOADUPS256,
23944   IX86_BUILTIN_STOREUPD256,
23945   IX86_BUILTIN_STOREUPS256,
23946   IX86_BUILTIN_LDDQU256,
23947   IX86_BUILTIN_MOVNTDQ256,
23948   IX86_BUILTIN_MOVNTPD256,
23949   IX86_BUILTIN_MOVNTPS256,
23950   IX86_BUILTIN_LOADDQU256,
23951   IX86_BUILTIN_STOREDQU256,
23952   IX86_BUILTIN_MASKLOADPD,
23953   IX86_BUILTIN_MASKLOADPS,
23954   IX86_BUILTIN_MASKSTOREPD,
23955   IX86_BUILTIN_MASKSTOREPS,
23956   IX86_BUILTIN_MASKLOADPD256,
23957   IX86_BUILTIN_MASKLOADPS256,
23958   IX86_BUILTIN_MASKSTOREPD256,
23959   IX86_BUILTIN_MASKSTOREPS256,
23960   IX86_BUILTIN_MOVSHDUP256,
23961   IX86_BUILTIN_MOVSLDUP256,
23962   IX86_BUILTIN_MOVDDUP256,
23963
23964   IX86_BUILTIN_SQRTPD256,
23965   IX86_BUILTIN_SQRTPS256,
23966   IX86_BUILTIN_SQRTPS_NR256,
23967   IX86_BUILTIN_RSQRTPS256,
23968   IX86_BUILTIN_RSQRTPS_NR256,
23969
23970   IX86_BUILTIN_RCPPS256,
23971
23972   IX86_BUILTIN_ROUNDPD256,
23973   IX86_BUILTIN_ROUNDPS256,
23974
23975   IX86_BUILTIN_UNPCKHPD256,
23976   IX86_BUILTIN_UNPCKLPD256,
23977   IX86_BUILTIN_UNPCKHPS256,
23978   IX86_BUILTIN_UNPCKLPS256,
23979
23980   IX86_BUILTIN_SI256_SI,
23981   IX86_BUILTIN_PS256_PS,
23982   IX86_BUILTIN_PD256_PD,
23983   IX86_BUILTIN_SI_SI256,
23984   IX86_BUILTIN_PS_PS256,
23985   IX86_BUILTIN_PD_PD256,
23986
23987   IX86_BUILTIN_VTESTZPD,
23988   IX86_BUILTIN_VTESTCPD,
23989   IX86_BUILTIN_VTESTNZCPD,
23990   IX86_BUILTIN_VTESTZPS,
23991   IX86_BUILTIN_VTESTCPS,
23992   IX86_BUILTIN_VTESTNZCPS,
23993   IX86_BUILTIN_VTESTZPD256,
23994   IX86_BUILTIN_VTESTCPD256,
23995   IX86_BUILTIN_VTESTNZCPD256,
23996   IX86_BUILTIN_VTESTZPS256,
23997   IX86_BUILTIN_VTESTCPS256,
23998   IX86_BUILTIN_VTESTNZCPS256,
23999   IX86_BUILTIN_PTESTZ256,
24000   IX86_BUILTIN_PTESTC256,
24001   IX86_BUILTIN_PTESTNZC256,
24002
24003   IX86_BUILTIN_MOVMSKPD256,
24004   IX86_BUILTIN_MOVMSKPS256,
24005
24006   /* TFmode support builtins.  */
24007   IX86_BUILTIN_INFQ,
24008   IX86_BUILTIN_HUGE_VALQ,
24009   IX86_BUILTIN_FABSQ,
24010   IX86_BUILTIN_COPYSIGNQ,
24011
24012   /* Vectorizer support builtins.  */
24013   IX86_BUILTIN_CPYSGNPS,
24014   IX86_BUILTIN_CPYSGNPD,
24015   IX86_BUILTIN_CPYSGNPS256,
24016   IX86_BUILTIN_CPYSGNPD256,
24017
24018   IX86_BUILTIN_CVTUDQ2PS,
24019
24020   IX86_BUILTIN_VEC_PERM_V2DF,
24021   IX86_BUILTIN_VEC_PERM_V4SF,
24022   IX86_BUILTIN_VEC_PERM_V2DI,
24023   IX86_BUILTIN_VEC_PERM_V4SI,
24024   IX86_BUILTIN_VEC_PERM_V8HI,
24025   IX86_BUILTIN_VEC_PERM_V16QI,
24026   IX86_BUILTIN_VEC_PERM_V2DI_U,
24027   IX86_BUILTIN_VEC_PERM_V4SI_U,
24028   IX86_BUILTIN_VEC_PERM_V8HI_U,
24029   IX86_BUILTIN_VEC_PERM_V16QI_U,
24030   IX86_BUILTIN_VEC_PERM_V4DF,
24031   IX86_BUILTIN_VEC_PERM_V8SF,
24032
24033   /* FMA4 and XOP instructions.  */
24034   IX86_BUILTIN_VFMADDSS,
24035   IX86_BUILTIN_VFMADDSD,
24036   IX86_BUILTIN_VFMADDPS,
24037   IX86_BUILTIN_VFMADDPD,
24038   IX86_BUILTIN_VFMADDPS256,
24039   IX86_BUILTIN_VFMADDPD256,
24040   IX86_BUILTIN_VFMADDSUBPS,
24041   IX86_BUILTIN_VFMADDSUBPD,
24042   IX86_BUILTIN_VFMADDSUBPS256,
24043   IX86_BUILTIN_VFMADDSUBPD256,
24044
24045   IX86_BUILTIN_VPCMOV,
24046   IX86_BUILTIN_VPCMOV_V2DI,
24047   IX86_BUILTIN_VPCMOV_V4SI,
24048   IX86_BUILTIN_VPCMOV_V8HI,
24049   IX86_BUILTIN_VPCMOV_V16QI,
24050   IX86_BUILTIN_VPCMOV_V4SF,
24051   IX86_BUILTIN_VPCMOV_V2DF,
24052   IX86_BUILTIN_VPCMOV256,
24053   IX86_BUILTIN_VPCMOV_V4DI256,
24054   IX86_BUILTIN_VPCMOV_V8SI256,
24055   IX86_BUILTIN_VPCMOV_V16HI256,
24056   IX86_BUILTIN_VPCMOV_V32QI256,
24057   IX86_BUILTIN_VPCMOV_V8SF256,
24058   IX86_BUILTIN_VPCMOV_V4DF256,
24059
24060   IX86_BUILTIN_VPPERM,
24061
24062   IX86_BUILTIN_VPMACSSWW,
24063   IX86_BUILTIN_VPMACSWW,
24064   IX86_BUILTIN_VPMACSSWD,
24065   IX86_BUILTIN_VPMACSWD,
24066   IX86_BUILTIN_VPMACSSDD,
24067   IX86_BUILTIN_VPMACSDD,
24068   IX86_BUILTIN_VPMACSSDQL,
24069   IX86_BUILTIN_VPMACSSDQH,
24070   IX86_BUILTIN_VPMACSDQL,
24071   IX86_BUILTIN_VPMACSDQH,
24072   IX86_BUILTIN_VPMADCSSWD,
24073   IX86_BUILTIN_VPMADCSWD,
24074
24075   IX86_BUILTIN_VPHADDBW,
24076   IX86_BUILTIN_VPHADDBD,
24077   IX86_BUILTIN_VPHADDBQ,
24078   IX86_BUILTIN_VPHADDWD,
24079   IX86_BUILTIN_VPHADDWQ,
24080   IX86_BUILTIN_VPHADDDQ,
24081   IX86_BUILTIN_VPHADDUBW,
24082   IX86_BUILTIN_VPHADDUBD,
24083   IX86_BUILTIN_VPHADDUBQ,
24084   IX86_BUILTIN_VPHADDUWD,
24085   IX86_BUILTIN_VPHADDUWQ,
24086   IX86_BUILTIN_VPHADDUDQ,
24087   IX86_BUILTIN_VPHSUBBW,
24088   IX86_BUILTIN_VPHSUBWD,
24089   IX86_BUILTIN_VPHSUBDQ,
24090
24091   IX86_BUILTIN_VPROTB,
24092   IX86_BUILTIN_VPROTW,
24093   IX86_BUILTIN_VPROTD,
24094   IX86_BUILTIN_VPROTQ,
24095   IX86_BUILTIN_VPROTB_IMM,
24096   IX86_BUILTIN_VPROTW_IMM,
24097   IX86_BUILTIN_VPROTD_IMM,
24098   IX86_BUILTIN_VPROTQ_IMM,
24099
24100   IX86_BUILTIN_VPSHLB,
24101   IX86_BUILTIN_VPSHLW,
24102   IX86_BUILTIN_VPSHLD,
24103   IX86_BUILTIN_VPSHLQ,
24104   IX86_BUILTIN_VPSHAB,
24105   IX86_BUILTIN_VPSHAW,
24106   IX86_BUILTIN_VPSHAD,
24107   IX86_BUILTIN_VPSHAQ,
24108
24109   IX86_BUILTIN_VFRCZSS,
24110   IX86_BUILTIN_VFRCZSD,
24111   IX86_BUILTIN_VFRCZPS,
24112   IX86_BUILTIN_VFRCZPD,
24113   IX86_BUILTIN_VFRCZPS256,
24114   IX86_BUILTIN_VFRCZPD256,
24115
24116   IX86_BUILTIN_VPCOMEQUB,
24117   IX86_BUILTIN_VPCOMNEUB,
24118   IX86_BUILTIN_VPCOMLTUB,
24119   IX86_BUILTIN_VPCOMLEUB,
24120   IX86_BUILTIN_VPCOMGTUB,
24121   IX86_BUILTIN_VPCOMGEUB,
24122   IX86_BUILTIN_VPCOMFALSEUB,
24123   IX86_BUILTIN_VPCOMTRUEUB,
24124
24125   IX86_BUILTIN_VPCOMEQUW,
24126   IX86_BUILTIN_VPCOMNEUW,
24127   IX86_BUILTIN_VPCOMLTUW,
24128   IX86_BUILTIN_VPCOMLEUW,
24129   IX86_BUILTIN_VPCOMGTUW,
24130   IX86_BUILTIN_VPCOMGEUW,
24131   IX86_BUILTIN_VPCOMFALSEUW,
24132   IX86_BUILTIN_VPCOMTRUEUW,
24133
24134   IX86_BUILTIN_VPCOMEQUD,
24135   IX86_BUILTIN_VPCOMNEUD,
24136   IX86_BUILTIN_VPCOMLTUD,
24137   IX86_BUILTIN_VPCOMLEUD,
24138   IX86_BUILTIN_VPCOMGTUD,
24139   IX86_BUILTIN_VPCOMGEUD,
24140   IX86_BUILTIN_VPCOMFALSEUD,
24141   IX86_BUILTIN_VPCOMTRUEUD,
24142
24143   IX86_BUILTIN_VPCOMEQUQ,
24144   IX86_BUILTIN_VPCOMNEUQ,
24145   IX86_BUILTIN_VPCOMLTUQ,
24146   IX86_BUILTIN_VPCOMLEUQ,
24147   IX86_BUILTIN_VPCOMGTUQ,
24148   IX86_BUILTIN_VPCOMGEUQ,
24149   IX86_BUILTIN_VPCOMFALSEUQ,
24150   IX86_BUILTIN_VPCOMTRUEUQ,
24151
24152   IX86_BUILTIN_VPCOMEQB,
24153   IX86_BUILTIN_VPCOMNEB,
24154   IX86_BUILTIN_VPCOMLTB,
24155   IX86_BUILTIN_VPCOMLEB,
24156   IX86_BUILTIN_VPCOMGTB,
24157   IX86_BUILTIN_VPCOMGEB,
24158   IX86_BUILTIN_VPCOMFALSEB,
24159   IX86_BUILTIN_VPCOMTRUEB,
24160
24161   IX86_BUILTIN_VPCOMEQW,
24162   IX86_BUILTIN_VPCOMNEW,
24163   IX86_BUILTIN_VPCOMLTW,
24164   IX86_BUILTIN_VPCOMLEW,
24165   IX86_BUILTIN_VPCOMGTW,
24166   IX86_BUILTIN_VPCOMGEW,
24167   IX86_BUILTIN_VPCOMFALSEW,
24168   IX86_BUILTIN_VPCOMTRUEW,
24169
24170   IX86_BUILTIN_VPCOMEQD,
24171   IX86_BUILTIN_VPCOMNED,
24172   IX86_BUILTIN_VPCOMLTD,
24173   IX86_BUILTIN_VPCOMLED,
24174   IX86_BUILTIN_VPCOMGTD,
24175   IX86_BUILTIN_VPCOMGED,
24176   IX86_BUILTIN_VPCOMFALSED,
24177   IX86_BUILTIN_VPCOMTRUED,
24178
24179   IX86_BUILTIN_VPCOMEQQ,
24180   IX86_BUILTIN_VPCOMNEQ,
24181   IX86_BUILTIN_VPCOMLTQ,
24182   IX86_BUILTIN_VPCOMLEQ,
24183   IX86_BUILTIN_VPCOMGTQ,
24184   IX86_BUILTIN_VPCOMGEQ,
24185   IX86_BUILTIN_VPCOMFALSEQ,
24186   IX86_BUILTIN_VPCOMTRUEQ,
24187
24188   /* LWP instructions.  */
24189   IX86_BUILTIN_LLWPCB,
24190   IX86_BUILTIN_SLWPCB,
24191   IX86_BUILTIN_LWPVAL32,
24192   IX86_BUILTIN_LWPVAL64,
24193   IX86_BUILTIN_LWPINS32,
24194   IX86_BUILTIN_LWPINS64,
24195
24196   IX86_BUILTIN_CLZS,
24197
24198   /* BMI instructions.  */
24199   IX86_BUILTIN_BEXTR32,
24200   IX86_BUILTIN_BEXTR64,
24201   IX86_BUILTIN_CTZS,
24202
24203   /* TBM instructions.  */
24204   IX86_BUILTIN_BEXTRI32,
24205   IX86_BUILTIN_BEXTRI64,
24206
24207
24208   /* FSGSBASE instructions.  */
24209   IX86_BUILTIN_RDFSBASE32,
24210   IX86_BUILTIN_RDFSBASE64,
24211   IX86_BUILTIN_RDGSBASE32,
24212   IX86_BUILTIN_RDGSBASE64,
24213   IX86_BUILTIN_WRFSBASE32,
24214   IX86_BUILTIN_WRFSBASE64,
24215   IX86_BUILTIN_WRGSBASE32,
24216   IX86_BUILTIN_WRGSBASE64,
24217
24218   /* RDRND instructions.  */
24219   IX86_BUILTIN_RDRAND16_STEP,
24220   IX86_BUILTIN_RDRAND32_STEP,
24221   IX86_BUILTIN_RDRAND64_STEP,
24222
24223   /* F16C instructions.  */
24224   IX86_BUILTIN_CVTPH2PS,
24225   IX86_BUILTIN_CVTPH2PS256,
24226   IX86_BUILTIN_CVTPS2PH,
24227   IX86_BUILTIN_CVTPS2PH256,
24228
24229   IX86_BUILTIN_MAX
24230 };
24231
24232 /* Table for the ix86 builtin decls.  */
24233 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
24234
24235 /* Table of all of the builtin functions that are possible with different ISA's
24236    but are waiting to be built until a function is declared to use that
24237    ISA.  */
24238 struct builtin_isa {
24239   const char *name;             /* function name */
24240   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
24241   int isa;                      /* isa_flags this builtin is defined for */
24242   bool const_p;                 /* true if the declaration is constant */
24243   bool set_and_not_built_p;
24244 };
24245
24246 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
24247
24248
24249 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
24250    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
24251    function decl in the ix86_builtins array.  Returns the function decl or
24252    NULL_TREE, if the builtin was not added.
24253
24254    If the front end has a special hook for builtin functions, delay adding
24255    builtin functions that aren't in the current ISA until the ISA is changed
24256    with function specific optimization.  Doing so, can save about 300K for the
24257    default compiler.  When the builtin is expanded, check at that time whether
24258    it is valid.
24259
24260    If the front end doesn't have a special hook, record all builtins, even if
24261    it isn't an instruction set in the current ISA in case the user uses
24262    function specific options for a different ISA, so that we don't get scope
24263    errors if a builtin is added in the middle of a function scope.  */
24264
24265 static inline tree
24266 def_builtin (int mask, const char *name, enum ix86_builtin_func_type tcode,
24267              enum ix86_builtins code)
24268 {
24269   tree decl = NULL_TREE;
24270
24271   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
24272     {
24273       ix86_builtins_isa[(int) code].isa = mask;
24274
24275       mask &= ~OPTION_MASK_ISA_64BIT;
24276       if (mask == 0
24277           || (mask & ix86_isa_flags) != 0
24278           || (lang_hooks.builtin_function
24279               == lang_hooks.builtin_function_ext_scope))
24280
24281         {
24282           tree type = ix86_get_builtin_func_type (tcode);
24283           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
24284                                        NULL, NULL_TREE);
24285           ix86_builtins[(int) code] = decl;
24286           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
24287         }
24288       else
24289         {
24290           ix86_builtins[(int) code] = NULL_TREE;
24291           ix86_builtins_isa[(int) code].tcode = tcode;
24292           ix86_builtins_isa[(int) code].name = name;
24293           ix86_builtins_isa[(int) code].const_p = false;
24294           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
24295         }
24296     }
24297
24298   return decl;
24299 }
24300
24301 /* Like def_builtin, but also marks the function decl "const".  */
24302
24303 static inline tree
24304 def_builtin_const (int mask, const char *name,
24305                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
24306 {
24307   tree decl = def_builtin (mask, name, tcode, code);
24308   if (decl)
24309     TREE_READONLY (decl) = 1;
24310   else
24311     ix86_builtins_isa[(int) code].const_p = true;
24312
24313   return decl;
24314 }
24315
24316 /* Add any new builtin functions for a given ISA that may not have been
24317    declared.  This saves a bit of space compared to adding all of the
24318    declarations to the tree, even if we didn't use them.  */
24319
24320 static void
24321 ix86_add_new_builtins (int isa)
24322 {
24323   int i;
24324
24325   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
24326     {
24327       if ((ix86_builtins_isa[i].isa & isa) != 0
24328           && ix86_builtins_isa[i].set_and_not_built_p)
24329         {
24330           tree decl, type;
24331
24332           /* Don't define the builtin again.  */
24333           ix86_builtins_isa[i].set_and_not_built_p = false;
24334
24335           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
24336           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
24337                                                  type, i, BUILT_IN_MD, NULL,
24338                                                  NULL_TREE);
24339
24340           ix86_builtins[i] = decl;
24341           if (ix86_builtins_isa[i].const_p)
24342             TREE_READONLY (decl) = 1;
24343         }
24344     }
24345 }
24346
24347 /* Bits for builtin_description.flag.  */
24348
24349 /* Set when we don't support the comparison natively, and should
24350    swap_comparison in order to support it.  */
24351 #define BUILTIN_DESC_SWAP_OPERANDS      1
24352
24353 struct builtin_description
24354 {
24355   const unsigned int mask;
24356   const enum insn_code icode;
24357   const char *const name;
24358   const enum ix86_builtins code;
24359   const enum rtx_code comparison;
24360   const int flag;
24361 };
24362
24363 static const struct builtin_description bdesc_comi[] =
24364 {
24365   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
24366   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
24367   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
24368   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
24369   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
24370   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
24371   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
24372   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
24373   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
24374   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
24375   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
24376   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
24377   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
24378   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
24379   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
24380   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
24381   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
24382   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
24383   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
24384   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
24385   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
24386   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
24387   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
24388   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
24389 };
24390
24391 static const struct builtin_description bdesc_pcmpestr[] =
24392 {
24393   /* SSE4.2 */
24394   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
24395   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
24396   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
24397   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
24398   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
24399   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
24400   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
24401 };
24402
24403 static const struct builtin_description bdesc_pcmpistr[] =
24404 {
24405   /* SSE4.2 */
24406   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
24407   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
24408   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
24409   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
24410   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
24411   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
24412   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
24413 };
24414
24415 /* Special builtins with variable number of arguments.  */
24416 static const struct builtin_description bdesc_special_args[] =
24417 {
24418   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
24419   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
24420
24421   /* MMX */
24422   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
24423
24424   /* 3DNow! */
24425   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
24426
24427   /* SSE */
24428   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
24429   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
24430   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
24431
24432   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
24433   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
24434   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
24435   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
24436
24437   /* SSE or 3DNow!A  */
24438   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
24439   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_movntdi, "__builtin_ia32_movntq", IX86_BUILTIN_MOVNTQ, UNKNOWN, (int) VOID_FTYPE_PULONGLONG_ULONGLONG },
24440
24441   /* SSE2 */
24442   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
24443   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
24444   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
24445   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
24446   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
24447   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
24448   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntsi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
24449   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
24450   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
24451
24452   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
24453   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
24454
24455   /* SSE3 */
24456   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
24457
24458   /* SSE4.1 */
24459   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
24460
24461   /* SSE4A */
24462   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
24463   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
24464
24465   /* AVX */
24466   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
24467   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
24468
24469   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
24470   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
24471   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
24472   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
24473   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
24474
24475   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
24476   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
24477   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
24478   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
24479   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
24480   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
24481   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
24482
24483   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
24484   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
24485   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
24486
24487   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DI },
24488   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SI },
24489   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DI },
24490   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SI },
24491   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DI_V2DF },
24492   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SI_V4SF },
24493   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DI_V4DF },
24494   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SI_V8SF },
24495
24496   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
24497   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
24498   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
24499   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
24500   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
24501   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
24502
24503   /* FSGSBASE */
24504   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasesi, "__builtin_ia32_rdfsbase32", IX86_BUILTIN_RDFSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
24505   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasedi, "__builtin_ia32_rdfsbase64", IX86_BUILTIN_RDFSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
24506   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasesi, "__builtin_ia32_rdgsbase32", IX86_BUILTIN_RDGSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
24507   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasedi, "__builtin_ia32_rdgsbase64", IX86_BUILTIN_RDGSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
24508   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasesi, "__builtin_ia32_wrfsbase32", IX86_BUILTIN_WRFSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
24509   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasedi, "__builtin_ia32_wrfsbase64", IX86_BUILTIN_WRFSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
24510   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasesi, "__builtin_ia32_wrgsbase32", IX86_BUILTIN_WRGSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
24511   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasedi, "__builtin_ia32_wrgsbase64", IX86_BUILTIN_WRGSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
24512 };
24513
24514 /* Builtins with variable number of arguments.  */
24515 static const struct builtin_description bdesc_args[] =
24516 {
24517   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
24518   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
24519   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
24520   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
24521   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
24522   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
24523   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
24524
24525   /* MMX */
24526   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24527   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24528   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24529   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24530   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24531   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24532
24533   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24534   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24535   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24536   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24537   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24538   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24539   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24540   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24541
24542   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24543   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24544
24545   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24546   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24547   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24548   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24549
24550   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24551   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24552   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24553   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24554   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24555   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24556
24557   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24558   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24559   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24560   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24561   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
24562   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
24563
24564   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
24565   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
24566   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
24567
24568   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
24569
24570   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
24571   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
24572   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
24573   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
24574   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
24575   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
24576
24577   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
24578   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
24579   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
24580   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
24581   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
24582   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
24583
24584   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
24585   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
24586   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
24587   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
24588
24589   /* 3DNow! */
24590   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
24591   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
24592   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
24593   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
24594
24595   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24596   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24597   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24598   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
24599   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
24600   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
24601   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24602   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24603   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24604   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24605   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24606   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24607   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24608   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24609   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24610
24611   /* 3DNow!A */
24612   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
24613   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
24614   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
24615   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
24616   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24617   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24618
24619   /* SSE */
24620   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
24621   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24622   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24623   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24624   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24625   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24626   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
24627   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
24628   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
24629   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
24630   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
24631   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
24632
24633   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24634
24635   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24636   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24637   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24638   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24639   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24640   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24641   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24642   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24643
24644   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
24645   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
24646   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
24647   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24648   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24649   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24650   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
24651   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
24652   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
24653   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24654   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
24655   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24656   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
24657   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
24658   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
24659   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24660   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
24661   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
24662   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
24663   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24664   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24665   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24666
24667   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24668   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24669   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24670   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24671
24672   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24673   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24674   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24675   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24676
24677   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24678
24679   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24680   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24681   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24682   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24683   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24684
24685   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
24686   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
24687   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
24688
24689   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
24690
24691   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
24692   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
24693   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
24694
24695   /* SSE MMX or 3Dnow!A */
24696   { 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 },
24697   { 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 },
24698   { 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 },
24699
24700   { 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 },
24701   { 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 },
24702   { 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 },
24703   { 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 },
24704
24705   { 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 },
24706   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
24707
24708   { 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 },
24709
24710   /* SSE2 */
24711   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24712
24713   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2df", IX86_BUILTIN_VEC_PERM_V2DF, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DI },
24714   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4sf", IX86_BUILTIN_VEC_PERM_V4SF, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SI },
24715   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2di", IX86_BUILTIN_VEC_PERM_V2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_V2DI },
24716   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4si", IX86_BUILTIN_VEC_PERM_V4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_V4SI },
24717   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8hi", IX86_BUILTIN_VEC_PERM_V8HI, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_V8HI },
24718   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v16qi", IX86_BUILTIN_VEC_PERM_V16QI, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
24719   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2di_u", IX86_BUILTIN_VEC_PERM_V2DI_U, UNKNOWN, (int) V2UDI_FTYPE_V2UDI_V2UDI_V2UDI },
24720   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4si_u", IX86_BUILTIN_VEC_PERM_V4SI_U, UNKNOWN, (int) V4USI_FTYPE_V4USI_V4USI_V4USI },
24721   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8hi_u", IX86_BUILTIN_VEC_PERM_V8HI_U, UNKNOWN, (int) V8UHI_FTYPE_V8UHI_V8UHI_V8UHI },
24722   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v16qi_u", IX86_BUILTIN_VEC_PERM_V16QI_U, UNKNOWN, (int) V16UQI_FTYPE_V16UQI_V16UQI_V16UQI },
24723   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4df", IX86_BUILTIN_VEC_PERM_V4DF, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DI },
24724   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8sf", IX86_BUILTIN_VEC_PERM_V8SF, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SI },
24725
24726   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
24727   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
24728   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
24729   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
24730   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2ps, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
24731   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtudq2ps, "__builtin_ia32_cvtudq2ps", IX86_BUILTIN_CVTUDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
24732
24733   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
24734   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
24735   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
24736   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
24737   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
24738
24739   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
24740
24741   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
24742   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
24743   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
24744   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
24745
24746   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
24747   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
24748   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttps2dq, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
24749
24750   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24751   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24752   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24753   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24754   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24755   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24756   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24757   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24758
24759   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
24760   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
24761   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
24762   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
24763   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
24764   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24765   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
24766   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
24767   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
24768   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
24769   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
24770   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24771   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
24772   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
24773   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
24774   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24775   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
24776   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
24777   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
24778   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24779
24780   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24781   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24782   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24783   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24784
24785   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24786   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24787   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24788   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24789
24790   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24791
24792   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24793   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24794   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24795
24796   { 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 },
24797
24798   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24799   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24800   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24801   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24802   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24803   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24804   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24805   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24806
24807   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24808   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24809   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24810   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24811   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24812   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24813   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24814   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24815
24816   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24817   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
24818
24819   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24820   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24821   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24822   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24823
24824   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24825   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24826
24827   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24828   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24829   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
24830   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24831   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24832   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
24833
24834   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24835   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24836   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24837   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24838
24839   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24840   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
24841   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
24842   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24843   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24844   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24845   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24846   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24847
24848   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
24849   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
24850   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
24851
24852   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24853   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
24854
24855   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
24856   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
24857
24858   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
24859
24860   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
24861   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
24862   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
24863   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
24864
24865   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
24866   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
24867   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
24868   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
24869   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
24870   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
24871   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
24872
24873   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
24874   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
24875   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
24876   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
24877   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
24878   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
24879   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
24880
24881   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
24882   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
24883   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
24884   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
24885
24886   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
24887   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
24888   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
24889
24890   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
24891
24892   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
24893   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
24894
24895   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
24896
24897   /* SSE2 MMX */
24898   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
24899   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
24900
24901   /* SSE3 */
24902   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
24903   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24904
24905   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24906   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24907   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24908   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24909   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24910   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24911
24912   /* SSSE3 */
24913   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
24914   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
24915   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
24916   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
24917   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
24918   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
24919
24920   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24921   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24922   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24923   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24924   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24925   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24926   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24927   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24928   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24929   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24930   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24931   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24932   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
24933   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
24934   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24935   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24936   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24937   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24938   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24939   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24940   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24941   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24942   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24943   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24944
24945   /* SSSE3.  */
24946   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
24947   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
24948
24949   /* SSE4.1 */
24950   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24951   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24952   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
24953   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
24954   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24955   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24956   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24957   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
24958   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
24959   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
24960
24961   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
24962   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
24963   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
24964   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
24965   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
24966   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
24967   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
24968   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
24969   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
24970   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
24971   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
24972   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
24973   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
24974
24975   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
24976   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24977   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24978   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24979   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24980   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24981   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24982   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24983   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24984   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24985   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
24986   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24987
24988   /* SSE4.1 */
24989   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
24990   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
24991   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24992   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24993
24994   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
24995   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
24996   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
24997
24998   /* SSE4.2 */
24999   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
25000   { 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 },
25001   { 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 },
25002   { 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 },
25003   { 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 },
25004
25005   /* SSE4A */
25006   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
25007   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
25008   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
25009   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
25010
25011   /* AES */
25012   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
25013   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
25014
25015   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
25016   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
25017   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
25018   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
25019
25020   /* PCLMUL */
25021   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
25022
25023   /* AVX */
25024   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25025   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25026   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25027   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25028   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25029   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25030   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25031   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25032   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25033   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25034   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25035   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25036   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25037   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25038   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25039   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25040   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25041   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25042   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25043   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25044   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25045   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25046   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25047   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25048   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25049   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25050
25051   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
25052   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
25053   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
25054   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
25055
25056   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
25057   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
25058   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
25059   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
25060   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
25061   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
25062   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
25063   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpsdv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
25064   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpssv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
25065   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
25066   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
25067   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
25068   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
25069   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
25070   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
25071   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
25072   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2pd256, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
25073   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2ps256, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
25074   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
25075   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
25076   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
25077   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttpd2dq256, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
25078   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
25079   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttps2dq256, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
25080   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
25081   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
25082   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
25083   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
25084   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
25085   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
25086   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
25087   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
25088   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
25089   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
25090
25091   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25092   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25093   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
25094
25095   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
25096   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25097   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25098   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25099   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25100
25101   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
25102
25103   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
25104   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
25105
25106   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25107   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25108   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25109   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25110
25111   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
25112   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
25113   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
25114   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8si, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
25115   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8sf, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
25116   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v4df, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
25117
25118   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
25119   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
25120   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
25121   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
25122   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
25123   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
25124   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
25125   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
25126   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
25127   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
25128   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
25129   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
25130   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
25131   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
25132   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
25133
25134   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
25135   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
25136
25137   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv8sf3,  "__builtin_ia32_copysignps256", IX86_BUILTIN_CPYSGNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
25138   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv4df3,  "__builtin_ia32_copysignpd256", IX86_BUILTIN_CPYSGNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
25139
25140   { OPTION_MASK_ISA_ABM, CODE_FOR_clzhi2_abm,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
25141
25142   /* BMI */
25143   { OPTION_MASK_ISA_BMI, CODE_FOR_bmi_bextr_si, "__builtin_ia32_bextr_u32", IX86_BUILTIN_BEXTR32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
25144   { OPTION_MASK_ISA_BMI, CODE_FOR_bmi_bextr_di, "__builtin_ia32_bextr_u64", IX86_BUILTIN_BEXTR64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
25145   { OPTION_MASK_ISA_BMI, CODE_FOR_ctzhi2,       "__builtin_ctzs",           IX86_BUILTIN_CTZS,    UNKNOWN, (int) UINT16_FTYPE_UINT16 },
25146
25147   /* TBM */
25148   { OPTION_MASK_ISA_TBM, CODE_FOR_tbm_bextri_si, "__builtin_ia32_bextri_u32", IX86_BUILTIN_BEXTRI32, UNKNOWN, (int) UINT_FTYPE_UINT_UINT },
25149   { OPTION_MASK_ISA_TBM, CODE_FOR_tbm_bextri_di, "__builtin_ia32_bextri_u64", IX86_BUILTIN_BEXTRI64, UNKNOWN, (int) UINT64_FTYPE_UINT64_UINT64 },
25150
25151   /* F16C */
25152   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps, "__builtin_ia32_vcvtph2ps", IX86_BUILTIN_CVTPH2PS, UNKNOWN, (int) V4SF_FTYPE_V8HI },
25153   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps256, "__builtin_ia32_vcvtph2ps256", IX86_BUILTIN_CVTPH2PS256, UNKNOWN, (int) V8SF_FTYPE_V8HI },
25154   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph, "__builtin_ia32_vcvtps2ph", IX86_BUILTIN_CVTPS2PH, UNKNOWN, (int) V8HI_FTYPE_V4SF_INT },
25155   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph256, "__builtin_ia32_vcvtps2ph256", IX86_BUILTIN_CVTPS2PH256, UNKNOWN, (int) V8HI_FTYPE_V8SF_INT },
25156 };
25157
25158 /* FMA4 and XOP.  */
25159 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
25160 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
25161 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
25162 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
25163 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
25164 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
25165 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
25166 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
25167 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
25168 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
25169 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
25170 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
25171 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
25172 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
25173 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
25174 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
25175 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
25176 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
25177 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
25178 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
25179 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
25180 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
25181 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
25182 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
25183 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
25184 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
25185 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
25186 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
25187 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
25188 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
25189 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
25190 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
25191 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
25192 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
25193 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
25194 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
25195 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
25196 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
25197 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
25198 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
25199 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
25200 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
25201 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
25202 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
25203 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
25204 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
25205 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
25206 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
25207 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
25208 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
25209 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
25210 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
25211
25212 static const struct builtin_description bdesc_multi_arg[] =
25213 {
25214   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v4sf,
25215     "__builtin_ia32_vfmaddss", IX86_BUILTIN_VFMADDSS,
25216     UNKNOWN, (int)MULTI_ARG_3_SF },
25217   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v2df,
25218     "__builtin_ia32_vfmaddsd", IX86_BUILTIN_VFMADDSD,
25219     UNKNOWN, (int)MULTI_ARG_3_DF },
25220
25221   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4sf,
25222     "__builtin_ia32_vfmaddps", IX86_BUILTIN_VFMADDPS,
25223     UNKNOWN, (int)MULTI_ARG_3_SF },
25224   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v2df,
25225     "__builtin_ia32_vfmaddpd", IX86_BUILTIN_VFMADDPD,
25226     UNKNOWN, (int)MULTI_ARG_3_DF },
25227   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v8sf,
25228     "__builtin_ia32_vfmaddps256", IX86_BUILTIN_VFMADDPS256,
25229     UNKNOWN, (int)MULTI_ARG_3_SF2 },
25230   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4df,
25231     "__builtin_ia32_vfmaddpd256", IX86_BUILTIN_VFMADDPD256,
25232     UNKNOWN, (int)MULTI_ARG_3_DF2 },
25233
25234   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4sf,
25235     "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,
25236     UNKNOWN, (int)MULTI_ARG_3_SF },
25237   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v2df,
25238     "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,
25239     UNKNOWN, (int)MULTI_ARG_3_DF },
25240   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v8sf,
25241     "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,
25242     UNKNOWN, (int)MULTI_ARG_3_SF2 },
25243   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4df,
25244     "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,
25245     UNKNOWN, (int)MULTI_ARG_3_DF2 },
25246
25247   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
25248   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
25249   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
25250   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
25251   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
25252   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
25253   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
25254
25255   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
25256   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
25257   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
25258   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
25259   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
25260   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
25261   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
25262
25263   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
25264
25265   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
25266   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
25267   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25268   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25269   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
25270   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
25271   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25272   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25273   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25274   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25275   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25276   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25277
25278   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
25279   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
25280   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
25281   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
25282   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
25283   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
25284   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
25285   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
25286   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
25287   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
25288   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
25289   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
25290   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
25291   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
25292   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
25293   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
25294
25295   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_2_SF },
25296   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_2_DF },
25297   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
25298   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
25299   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
25300   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
25301
25302   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
25303   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
25304   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
25305   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
25306   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
25307   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
25308   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
25309   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
25310   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
25311   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
25312   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
25313   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
25314   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
25315   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
25316   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
25317
25318   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
25319   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
25320   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
25321   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
25322   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
25323   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
25324   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
25325
25326   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
25327   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
25328   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
25329   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
25330   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
25331   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
25332   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
25333
25334   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
25335   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
25336   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
25337   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
25338   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
25339   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
25340   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
25341
25342   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
25343   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
25344   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
25345   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
25346   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
25347   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
25348   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
25349
25350   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
25351   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
25352   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
25353   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
25354   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
25355   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
25356   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
25357
25358   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
25359   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
25360   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
25361   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
25362   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
25363   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
25364   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
25365
25366   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
25367   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
25368   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
25369   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
25370   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
25371   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
25372   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
25373
25374   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
25375   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
25376   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
25377   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
25378   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
25379   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
25380   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
25381
25382   { 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 },
25383   { 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 },
25384   { 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 },
25385   { 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 },
25386   { 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 },
25387   { 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 },
25388   { 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 },
25389   { 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 },
25390
25391   { 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 },
25392   { 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 },
25393   { 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 },
25394   { 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 },
25395   { 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 },
25396   { 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 },
25397   { 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 },
25398   { 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 },
25399
25400   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
25401   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
25402   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
25403   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
25404
25405 };
25406
25407 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
25408    in the current target ISA to allow the user to compile particular modules
25409    with different target specific options that differ from the command line
25410    options.  */
25411 static void
25412 ix86_init_mmx_sse_builtins (void)
25413 {
25414   const struct builtin_description * d;
25415   enum ix86_builtin_func_type ftype;
25416   size_t i;
25417
25418   /* Add all special builtins with variable number of operands.  */
25419   for (i = 0, d = bdesc_special_args;
25420        i < ARRAY_SIZE (bdesc_special_args);
25421        i++, d++)
25422     {
25423       if (d->name == 0)
25424         continue;
25425
25426       ftype = (enum ix86_builtin_func_type) d->flag;
25427       def_builtin (d->mask, d->name, ftype, d->code);
25428     }
25429
25430   /* Add all builtins with variable number of operands.  */
25431   for (i = 0, d = bdesc_args;
25432        i < ARRAY_SIZE (bdesc_args);
25433        i++, d++)
25434     {
25435       if (d->name == 0)
25436         continue;
25437
25438       ftype = (enum ix86_builtin_func_type) d->flag;
25439       def_builtin_const (d->mask, d->name, ftype, d->code);
25440     }
25441
25442   /* pcmpestr[im] insns.  */
25443   for (i = 0, d = bdesc_pcmpestr;
25444        i < ARRAY_SIZE (bdesc_pcmpestr);
25445        i++, d++)
25446     {
25447       if (d->code == IX86_BUILTIN_PCMPESTRM128)
25448         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
25449       else
25450         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
25451       def_builtin_const (d->mask, d->name, ftype, d->code);
25452     }
25453
25454   /* pcmpistr[im] insns.  */
25455   for (i = 0, d = bdesc_pcmpistr;
25456        i < ARRAY_SIZE (bdesc_pcmpistr);
25457        i++, d++)
25458     {
25459       if (d->code == IX86_BUILTIN_PCMPISTRM128)
25460         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
25461       else
25462         ftype = INT_FTYPE_V16QI_V16QI_INT;
25463       def_builtin_const (d->mask, d->name, ftype, d->code);
25464     }
25465
25466   /* comi/ucomi insns.  */
25467   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
25468     {
25469       if (d->mask == OPTION_MASK_ISA_SSE2)
25470         ftype = INT_FTYPE_V2DF_V2DF;
25471       else
25472         ftype = INT_FTYPE_V4SF_V4SF;
25473       def_builtin_const (d->mask, d->name, ftype, d->code);
25474     }
25475
25476   /* SSE */
25477   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
25478                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
25479   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
25480                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
25481
25482   /* SSE or 3DNow!A */
25483   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
25484                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
25485                IX86_BUILTIN_MASKMOVQ);
25486
25487   /* SSE2 */
25488   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
25489                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
25490
25491   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
25492                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
25493   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
25494                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
25495
25496   /* SSE3.  */
25497   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
25498                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
25499   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
25500                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
25501
25502   /* AES */
25503   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
25504                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
25505   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
25506                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
25507   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
25508                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
25509   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
25510                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
25511   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
25512                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
25513   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
25514                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
25515
25516   /* PCLMUL */
25517   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
25518                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
25519
25520   /* RDRND */
25521   def_builtin (OPTION_MASK_ISA_RDRND, "__builtin_ia32_rdrand16_step",
25522                INT_FTYPE_PUSHORT, IX86_BUILTIN_RDRAND16_STEP);
25523   def_builtin (OPTION_MASK_ISA_RDRND, "__builtin_ia32_rdrand32_step",
25524                INT_FTYPE_PUNSIGNED, IX86_BUILTIN_RDRAND32_STEP);
25525   def_builtin (OPTION_MASK_ISA_RDRND | OPTION_MASK_ISA_64BIT,
25526                "__builtin_ia32_rdrand64_step", INT_FTYPE_PULONGLONG,
25527                IX86_BUILTIN_RDRAND64_STEP);
25528
25529   /* MMX access to the vec_init patterns.  */
25530   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
25531                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
25532
25533   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
25534                      V4HI_FTYPE_HI_HI_HI_HI,
25535                      IX86_BUILTIN_VEC_INIT_V4HI);
25536
25537   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
25538                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
25539                      IX86_BUILTIN_VEC_INIT_V8QI);
25540
25541   /* Access to the vec_extract patterns.  */
25542   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
25543                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
25544   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
25545                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
25546   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
25547                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
25548   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
25549                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
25550   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
25551                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
25552
25553   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
25554                      "__builtin_ia32_vec_ext_v4hi",
25555                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
25556
25557   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
25558                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
25559
25560   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
25561                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
25562
25563   /* Access to the vec_set patterns.  */
25564   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
25565                      "__builtin_ia32_vec_set_v2di",
25566                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
25567
25568   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
25569                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
25570
25571   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
25572                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
25573
25574   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
25575                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
25576
25577   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
25578                      "__builtin_ia32_vec_set_v4hi",
25579                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
25580
25581   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
25582                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
25583
25584   /* Add FMA4 multi-arg argument instructions */
25585   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
25586     {
25587       if (d->name == 0)
25588         continue;
25589
25590       ftype = (enum ix86_builtin_func_type) d->flag;
25591       def_builtin_const (d->mask, d->name, ftype, d->code);
25592     }
25593 }
25594
25595 /* Internal method for ix86_init_builtins.  */
25596
25597 static void
25598 ix86_init_builtins_va_builtins_abi (void)
25599 {
25600   tree ms_va_ref, sysv_va_ref;
25601   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
25602   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
25603   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
25604   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
25605
25606   if (!TARGET_64BIT)
25607     return;
25608   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
25609   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
25610   ms_va_ref = build_reference_type (ms_va_list_type_node);
25611   sysv_va_ref =
25612     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
25613
25614   fnvoid_va_end_ms =
25615     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
25616   fnvoid_va_start_ms =
25617     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
25618   fnvoid_va_end_sysv =
25619     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
25620   fnvoid_va_start_sysv =
25621     build_varargs_function_type_list (void_type_node, sysv_va_ref,
25622                                        NULL_TREE);
25623   fnvoid_va_copy_ms =
25624     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
25625                               NULL_TREE);
25626   fnvoid_va_copy_sysv =
25627     build_function_type_list (void_type_node, sysv_va_ref,
25628                               sysv_va_ref, NULL_TREE);
25629
25630   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
25631                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
25632   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
25633                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
25634   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
25635                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
25636   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
25637                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
25638   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
25639                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
25640   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
25641                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
25642 }
25643
25644 static void
25645 ix86_init_builtin_types (void)
25646 {
25647   tree float128_type_node, float80_type_node;
25648
25649   /* The __float80 type.  */
25650   float80_type_node = long_double_type_node;
25651   if (TYPE_MODE (float80_type_node) != XFmode)
25652     {
25653       /* The __float80 type.  */
25654       float80_type_node = make_node (REAL_TYPE);
25655
25656       TYPE_PRECISION (float80_type_node) = 80;
25657       layout_type (float80_type_node);
25658     }
25659   lang_hooks.types.register_builtin_type (float80_type_node, "__float80");
25660
25661   /* The __float128 type.  */
25662   float128_type_node = make_node (REAL_TYPE);
25663   TYPE_PRECISION (float128_type_node) = 128;
25664   layout_type (float128_type_node);
25665   lang_hooks.types.register_builtin_type (float128_type_node, "__float128");
25666
25667   /* This macro is built by i386-builtin-types.awk.  */
25668   DEFINE_BUILTIN_PRIMITIVE_TYPES;
25669 }
25670
25671 static void
25672 ix86_init_builtins (void)
25673 {
25674   tree t;
25675
25676   ix86_init_builtin_types ();
25677
25678   /* TFmode support builtins.  */
25679   def_builtin_const (0, "__builtin_infq",
25680                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
25681   def_builtin_const (0, "__builtin_huge_valq",
25682                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
25683
25684   /* We will expand them to normal call if SSE2 isn't available since
25685      they are used by libgcc. */
25686   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
25687   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
25688                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
25689   TREE_READONLY (t) = 1;
25690   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
25691
25692   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
25693   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
25694                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
25695   TREE_READONLY (t) = 1;
25696   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
25697
25698   ix86_init_mmx_sse_builtins ();
25699
25700   if (TARGET_64BIT)
25701     ix86_init_builtins_va_builtins_abi ();
25702
25703 #ifdef SUBTARGET_INIT_BUILTINS
25704   SUBTARGET_INIT_BUILTINS;
25705 #endif
25706 }
25707
25708 /* Return the ix86 builtin for CODE.  */
25709
25710 static tree
25711 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
25712 {
25713   if (code >= IX86_BUILTIN_MAX)
25714     return error_mark_node;
25715
25716   return ix86_builtins[code];
25717 }
25718
25719 /* Errors in the source file can cause expand_expr to return const0_rtx
25720    where we expect a vector.  To avoid crashing, use one of the vector
25721    clear instructions.  */
25722 static rtx
25723 safe_vector_operand (rtx x, enum machine_mode mode)
25724 {
25725   if (x == const0_rtx)
25726     x = CONST0_RTX (mode);
25727   return x;
25728 }
25729
25730 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
25731
25732 static rtx
25733 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
25734 {
25735   rtx pat;
25736   tree arg0 = CALL_EXPR_ARG (exp, 0);
25737   tree arg1 = CALL_EXPR_ARG (exp, 1);
25738   rtx op0 = expand_normal (arg0);
25739   rtx op1 = expand_normal (arg1);
25740   enum machine_mode tmode = insn_data[icode].operand[0].mode;
25741   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
25742   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
25743
25744   if (VECTOR_MODE_P (mode0))
25745     op0 = safe_vector_operand (op0, mode0);
25746   if (VECTOR_MODE_P (mode1))
25747     op1 = safe_vector_operand (op1, mode1);
25748
25749   if (optimize || !target
25750       || GET_MODE (target) != tmode
25751       || !insn_data[icode].operand[0].predicate (target, tmode))
25752     target = gen_reg_rtx (tmode);
25753
25754   if (GET_MODE (op1) == SImode && mode1 == TImode)
25755     {
25756       rtx x = gen_reg_rtx (V4SImode);
25757       emit_insn (gen_sse2_loadd (x, op1));
25758       op1 = gen_lowpart (TImode, x);
25759     }
25760
25761   if (!insn_data[icode].operand[1].predicate (op0, mode0))
25762     op0 = copy_to_mode_reg (mode0, op0);
25763   if (!insn_data[icode].operand[2].predicate (op1, mode1))
25764     op1 = copy_to_mode_reg (mode1, op1);
25765
25766   pat = GEN_FCN (icode) (target, op0, op1);
25767   if (! pat)
25768     return 0;
25769
25770   emit_insn (pat);
25771
25772   return target;
25773 }
25774
25775 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
25776
25777 static rtx
25778 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
25779                                enum ix86_builtin_func_type m_type,
25780                                enum rtx_code sub_code)
25781 {
25782   rtx pat;
25783   int i;
25784   int nargs;
25785   bool comparison_p = false;
25786   bool tf_p = false;
25787   bool last_arg_constant = false;
25788   int num_memory = 0;
25789   struct {
25790     rtx op;
25791     enum machine_mode mode;
25792   } args[4];
25793
25794   enum machine_mode tmode = insn_data[icode].operand[0].mode;
25795
25796   switch (m_type)
25797     {
25798     case MULTI_ARG_4_DF2_DI_I:
25799     case MULTI_ARG_4_DF2_DI_I1:
25800     case MULTI_ARG_4_SF2_SI_I:
25801     case MULTI_ARG_4_SF2_SI_I1:
25802       nargs = 4;
25803       last_arg_constant = true;
25804       break;
25805
25806     case MULTI_ARG_3_SF:
25807     case MULTI_ARG_3_DF:
25808     case MULTI_ARG_3_SF2:
25809     case MULTI_ARG_3_DF2:
25810     case MULTI_ARG_3_DI:
25811     case MULTI_ARG_3_SI:
25812     case MULTI_ARG_3_SI_DI:
25813     case MULTI_ARG_3_HI:
25814     case MULTI_ARG_3_HI_SI:
25815     case MULTI_ARG_3_QI:
25816     case MULTI_ARG_3_DI2:
25817     case MULTI_ARG_3_SI2:
25818     case MULTI_ARG_3_HI2:
25819     case MULTI_ARG_3_QI2:
25820       nargs = 3;
25821       break;
25822
25823     case MULTI_ARG_2_SF:
25824     case MULTI_ARG_2_DF:
25825     case MULTI_ARG_2_DI:
25826     case MULTI_ARG_2_SI:
25827     case MULTI_ARG_2_HI:
25828     case MULTI_ARG_2_QI:
25829       nargs = 2;
25830       break;
25831
25832     case MULTI_ARG_2_DI_IMM:
25833     case MULTI_ARG_2_SI_IMM:
25834     case MULTI_ARG_2_HI_IMM:
25835     case MULTI_ARG_2_QI_IMM:
25836       nargs = 2;
25837       last_arg_constant = true;
25838       break;
25839
25840     case MULTI_ARG_1_SF:
25841     case MULTI_ARG_1_DF:
25842     case MULTI_ARG_1_SF2:
25843     case MULTI_ARG_1_DF2:
25844     case MULTI_ARG_1_DI:
25845     case MULTI_ARG_1_SI:
25846     case MULTI_ARG_1_HI:
25847     case MULTI_ARG_1_QI:
25848     case MULTI_ARG_1_SI_DI:
25849     case MULTI_ARG_1_HI_DI:
25850     case MULTI_ARG_1_HI_SI:
25851     case MULTI_ARG_1_QI_DI:
25852     case MULTI_ARG_1_QI_SI:
25853     case MULTI_ARG_1_QI_HI:
25854       nargs = 1;
25855       break;
25856
25857     case MULTI_ARG_2_DI_CMP:
25858     case MULTI_ARG_2_SI_CMP:
25859     case MULTI_ARG_2_HI_CMP:
25860     case MULTI_ARG_2_QI_CMP:
25861       nargs = 2;
25862       comparison_p = true;
25863       break;
25864
25865     case MULTI_ARG_2_SF_TF:
25866     case MULTI_ARG_2_DF_TF:
25867     case MULTI_ARG_2_DI_TF:
25868     case MULTI_ARG_2_SI_TF:
25869     case MULTI_ARG_2_HI_TF:
25870     case MULTI_ARG_2_QI_TF:
25871       nargs = 2;
25872       tf_p = true;
25873       break;
25874
25875     default:
25876       gcc_unreachable ();
25877     }
25878
25879   if (optimize || !target
25880       || GET_MODE (target) != tmode
25881       || !insn_data[icode].operand[0].predicate (target, tmode))
25882     target = gen_reg_rtx (tmode);
25883
25884   gcc_assert (nargs <= 4);
25885
25886   for (i = 0; i < nargs; i++)
25887     {
25888       tree arg = CALL_EXPR_ARG (exp, i);
25889       rtx op = expand_normal (arg);
25890       int adjust = (comparison_p) ? 1 : 0;
25891       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
25892
25893       if (last_arg_constant && i == nargs-1)
25894         {
25895           if (!CONST_INT_P (op))
25896             {
25897               error ("last argument must be an immediate");
25898               return gen_reg_rtx (tmode);
25899             }
25900         }
25901       else
25902         {
25903           if (VECTOR_MODE_P (mode))
25904             op = safe_vector_operand (op, mode);
25905
25906           /* If we aren't optimizing, only allow one memory operand to be
25907              generated.  */
25908           if (memory_operand (op, mode))
25909             num_memory++;
25910
25911           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
25912
25913           if (optimize
25914               || !insn_data[icode].operand[i+adjust+1].predicate (op, mode)
25915               || num_memory > 1)
25916             op = force_reg (mode, op);
25917         }
25918
25919       args[i].op = op;
25920       args[i].mode = mode;
25921     }
25922
25923   switch (nargs)
25924     {
25925     case 1:
25926       pat = GEN_FCN (icode) (target, args[0].op);
25927       break;
25928
25929     case 2:
25930       if (tf_p)
25931         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
25932                                GEN_INT ((int)sub_code));
25933       else if (! comparison_p)
25934         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
25935       else
25936         {
25937           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
25938                                        args[0].op,
25939                                        args[1].op);
25940
25941           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
25942         }
25943       break;
25944
25945     case 3:
25946       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
25947       break;
25948
25949     case 4:
25950       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
25951       break;
25952
25953     default:
25954       gcc_unreachable ();
25955     }
25956
25957   if (! pat)
25958     return 0;
25959
25960   emit_insn (pat);
25961   return target;
25962 }
25963
25964 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
25965    insns with vec_merge.  */
25966
25967 static rtx
25968 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
25969                                     rtx target)
25970 {
25971   rtx pat;
25972   tree arg0 = CALL_EXPR_ARG (exp, 0);
25973   rtx op1, op0 = expand_normal (arg0);
25974   enum machine_mode tmode = insn_data[icode].operand[0].mode;
25975   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
25976
25977   if (optimize || !target
25978       || GET_MODE (target) != tmode
25979       || !insn_data[icode].operand[0].predicate (target, tmode))
25980     target = gen_reg_rtx (tmode);
25981
25982   if (VECTOR_MODE_P (mode0))
25983     op0 = safe_vector_operand (op0, mode0);
25984
25985   if ((optimize && !register_operand (op0, mode0))
25986       || !insn_data[icode].operand[1].predicate (op0, mode0))
25987     op0 = copy_to_mode_reg (mode0, op0);
25988
25989   op1 = op0;
25990   if (!insn_data[icode].operand[2].predicate (op1, mode0))
25991     op1 = copy_to_mode_reg (mode0, op1);
25992
25993   pat = GEN_FCN (icode) (target, op0, op1);
25994   if (! pat)
25995     return 0;
25996   emit_insn (pat);
25997   return target;
25998 }
25999
26000 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
26001
26002 static rtx
26003 ix86_expand_sse_compare (const struct builtin_description *d,
26004                          tree exp, rtx target, bool swap)
26005 {
26006   rtx pat;
26007   tree arg0 = CALL_EXPR_ARG (exp, 0);
26008   tree arg1 = CALL_EXPR_ARG (exp, 1);
26009   rtx op0 = expand_normal (arg0);
26010   rtx op1 = expand_normal (arg1);
26011   rtx op2;
26012   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
26013   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
26014   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
26015   enum rtx_code comparison = d->comparison;
26016
26017   if (VECTOR_MODE_P (mode0))
26018     op0 = safe_vector_operand (op0, mode0);
26019   if (VECTOR_MODE_P (mode1))
26020     op1 = safe_vector_operand (op1, mode1);
26021
26022   /* Swap operands if we have a comparison that isn't available in
26023      hardware.  */
26024   if (swap)
26025     {
26026       rtx tmp = gen_reg_rtx (mode1);
26027       emit_move_insn (tmp, op1);
26028       op1 = op0;
26029       op0 = tmp;
26030     }
26031
26032   if (optimize || !target
26033       || GET_MODE (target) != tmode
26034       || !insn_data[d->icode].operand[0].predicate (target, tmode))
26035     target = gen_reg_rtx (tmode);
26036
26037   if ((optimize && !register_operand (op0, mode0))
26038       || !insn_data[d->icode].operand[1].predicate (op0, mode0))
26039     op0 = copy_to_mode_reg (mode0, op0);
26040   if ((optimize && !register_operand (op1, mode1))
26041       || !insn_data[d->icode].operand[2].predicate (op1, mode1))
26042     op1 = copy_to_mode_reg (mode1, op1);
26043
26044   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
26045   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
26046   if (! pat)
26047     return 0;
26048   emit_insn (pat);
26049   return target;
26050 }
26051
26052 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
26053
26054 static rtx
26055 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
26056                       rtx target)
26057 {
26058   rtx pat;
26059   tree arg0 = CALL_EXPR_ARG (exp, 0);
26060   tree arg1 = CALL_EXPR_ARG (exp, 1);
26061   rtx op0 = expand_normal (arg0);
26062   rtx op1 = expand_normal (arg1);
26063   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
26064   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
26065   enum rtx_code comparison = d->comparison;
26066
26067   if (VECTOR_MODE_P (mode0))
26068     op0 = safe_vector_operand (op0, mode0);
26069   if (VECTOR_MODE_P (mode1))
26070     op1 = safe_vector_operand (op1, mode1);
26071
26072   /* Swap operands if we have a comparison that isn't available in
26073      hardware.  */
26074   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
26075     {
26076       rtx tmp = op1;
26077       op1 = op0;
26078       op0 = tmp;
26079     }
26080
26081   target = gen_reg_rtx (SImode);
26082   emit_move_insn (target, const0_rtx);
26083   target = gen_rtx_SUBREG (QImode, target, 0);
26084
26085   if ((optimize && !register_operand (op0, mode0))
26086       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
26087     op0 = copy_to_mode_reg (mode0, op0);
26088   if ((optimize && !register_operand (op1, mode1))
26089       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
26090     op1 = copy_to_mode_reg (mode1, op1);
26091
26092   pat = GEN_FCN (d->icode) (op0, op1);
26093   if (! pat)
26094     return 0;
26095   emit_insn (pat);
26096   emit_insn (gen_rtx_SET (VOIDmode,
26097                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
26098                           gen_rtx_fmt_ee (comparison, QImode,
26099                                           SET_DEST (pat),
26100                                           const0_rtx)));
26101
26102   return SUBREG_REG (target);
26103 }
26104
26105 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
26106
26107 static rtx
26108 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
26109                        rtx target)
26110 {
26111   rtx pat;
26112   tree arg0 = CALL_EXPR_ARG (exp, 0);
26113   tree arg1 = CALL_EXPR_ARG (exp, 1);
26114   rtx op0 = expand_normal (arg0);
26115   rtx op1 = expand_normal (arg1);
26116   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
26117   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
26118   enum rtx_code comparison = d->comparison;
26119
26120   if (VECTOR_MODE_P (mode0))
26121     op0 = safe_vector_operand (op0, mode0);
26122   if (VECTOR_MODE_P (mode1))
26123     op1 = safe_vector_operand (op1, mode1);
26124
26125   target = gen_reg_rtx (SImode);
26126   emit_move_insn (target, const0_rtx);
26127   target = gen_rtx_SUBREG (QImode, target, 0);
26128
26129   if ((optimize && !register_operand (op0, mode0))
26130       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
26131     op0 = copy_to_mode_reg (mode0, op0);
26132   if ((optimize && !register_operand (op1, mode1))
26133       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
26134     op1 = copy_to_mode_reg (mode1, op1);
26135
26136   pat = GEN_FCN (d->icode) (op0, op1);
26137   if (! pat)
26138     return 0;
26139   emit_insn (pat);
26140   emit_insn (gen_rtx_SET (VOIDmode,
26141                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
26142                           gen_rtx_fmt_ee (comparison, QImode,
26143                                           SET_DEST (pat),
26144                                           const0_rtx)));
26145
26146   return SUBREG_REG (target);
26147 }
26148
26149 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
26150
26151 static rtx
26152 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
26153                           tree exp, rtx target)
26154 {
26155   rtx pat;
26156   tree arg0 = CALL_EXPR_ARG (exp, 0);
26157   tree arg1 = CALL_EXPR_ARG (exp, 1);
26158   tree arg2 = CALL_EXPR_ARG (exp, 2);
26159   tree arg3 = CALL_EXPR_ARG (exp, 3);
26160   tree arg4 = CALL_EXPR_ARG (exp, 4);
26161   rtx scratch0, scratch1;
26162   rtx op0 = expand_normal (arg0);
26163   rtx op1 = expand_normal (arg1);
26164   rtx op2 = expand_normal (arg2);
26165   rtx op3 = expand_normal (arg3);
26166   rtx op4 = expand_normal (arg4);
26167   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
26168
26169   tmode0 = insn_data[d->icode].operand[0].mode;
26170   tmode1 = insn_data[d->icode].operand[1].mode;
26171   modev2 = insn_data[d->icode].operand[2].mode;
26172   modei3 = insn_data[d->icode].operand[3].mode;
26173   modev4 = insn_data[d->icode].operand[4].mode;
26174   modei5 = insn_data[d->icode].operand[5].mode;
26175   modeimm = insn_data[d->icode].operand[6].mode;
26176
26177   if (VECTOR_MODE_P (modev2))
26178     op0 = safe_vector_operand (op0, modev2);
26179   if (VECTOR_MODE_P (modev4))
26180     op2 = safe_vector_operand (op2, modev4);
26181
26182   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
26183     op0 = copy_to_mode_reg (modev2, op0);
26184   if (!insn_data[d->icode].operand[3].predicate (op1, modei3))
26185     op1 = copy_to_mode_reg (modei3, op1);
26186   if ((optimize && !register_operand (op2, modev4))
26187       || !insn_data[d->icode].operand[4].predicate (op2, modev4))
26188     op2 = copy_to_mode_reg (modev4, op2);
26189   if (!insn_data[d->icode].operand[5].predicate (op3, modei5))
26190     op3 = copy_to_mode_reg (modei5, op3);
26191
26192   if (!insn_data[d->icode].operand[6].predicate (op4, modeimm))
26193     {
26194       error ("the fifth argument must be a 8-bit immediate");
26195       return const0_rtx;
26196     }
26197
26198   if (d->code == IX86_BUILTIN_PCMPESTRI128)
26199     {
26200       if (optimize || !target
26201           || GET_MODE (target) != tmode0
26202           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
26203         target = gen_reg_rtx (tmode0);
26204
26205       scratch1 = gen_reg_rtx (tmode1);
26206
26207       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
26208     }
26209   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
26210     {
26211       if (optimize || !target
26212           || GET_MODE (target) != tmode1
26213           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
26214         target = gen_reg_rtx (tmode1);
26215
26216       scratch0 = gen_reg_rtx (tmode0);
26217
26218       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
26219     }
26220   else
26221     {
26222       gcc_assert (d->flag);
26223
26224       scratch0 = gen_reg_rtx (tmode0);
26225       scratch1 = gen_reg_rtx (tmode1);
26226
26227       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
26228     }
26229
26230   if (! pat)
26231     return 0;
26232
26233   emit_insn (pat);
26234
26235   if (d->flag)
26236     {
26237       target = gen_reg_rtx (SImode);
26238       emit_move_insn (target, const0_rtx);
26239       target = gen_rtx_SUBREG (QImode, target, 0);
26240
26241       emit_insn
26242         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
26243                       gen_rtx_fmt_ee (EQ, QImode,
26244                                       gen_rtx_REG ((enum machine_mode) d->flag,
26245                                                    FLAGS_REG),
26246                                       const0_rtx)));
26247       return SUBREG_REG (target);
26248     }
26249   else
26250     return target;
26251 }
26252
26253
26254 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
26255
26256 static rtx
26257 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
26258                           tree exp, rtx target)
26259 {
26260   rtx pat;
26261   tree arg0 = CALL_EXPR_ARG (exp, 0);
26262   tree arg1 = CALL_EXPR_ARG (exp, 1);
26263   tree arg2 = CALL_EXPR_ARG (exp, 2);
26264   rtx scratch0, scratch1;
26265   rtx op0 = expand_normal (arg0);
26266   rtx op1 = expand_normal (arg1);
26267   rtx op2 = expand_normal (arg2);
26268   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
26269
26270   tmode0 = insn_data[d->icode].operand[0].mode;
26271   tmode1 = insn_data[d->icode].operand[1].mode;
26272   modev2 = insn_data[d->icode].operand[2].mode;
26273   modev3 = insn_data[d->icode].operand[3].mode;
26274   modeimm = insn_data[d->icode].operand[4].mode;
26275
26276   if (VECTOR_MODE_P (modev2))
26277     op0 = safe_vector_operand (op0, modev2);
26278   if (VECTOR_MODE_P (modev3))
26279     op1 = safe_vector_operand (op1, modev3);
26280
26281   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
26282     op0 = copy_to_mode_reg (modev2, op0);
26283   if ((optimize && !register_operand (op1, modev3))
26284       || !insn_data[d->icode].operand[3].predicate (op1, modev3))
26285     op1 = copy_to_mode_reg (modev3, op1);
26286
26287   if (!insn_data[d->icode].operand[4].predicate (op2, modeimm))
26288     {
26289       error ("the third argument must be a 8-bit immediate");
26290       return const0_rtx;
26291     }
26292
26293   if (d->code == IX86_BUILTIN_PCMPISTRI128)
26294     {
26295       if (optimize || !target
26296           || GET_MODE (target) != tmode0
26297           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
26298         target = gen_reg_rtx (tmode0);
26299
26300       scratch1 = gen_reg_rtx (tmode1);
26301
26302       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
26303     }
26304   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
26305     {
26306       if (optimize || !target
26307           || GET_MODE (target) != tmode1
26308           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
26309         target = gen_reg_rtx (tmode1);
26310
26311       scratch0 = gen_reg_rtx (tmode0);
26312
26313       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
26314     }
26315   else
26316     {
26317       gcc_assert (d->flag);
26318
26319       scratch0 = gen_reg_rtx (tmode0);
26320       scratch1 = gen_reg_rtx (tmode1);
26321
26322       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
26323     }
26324
26325   if (! pat)
26326     return 0;
26327
26328   emit_insn (pat);
26329
26330   if (d->flag)
26331     {
26332       target = gen_reg_rtx (SImode);
26333       emit_move_insn (target, const0_rtx);
26334       target = gen_rtx_SUBREG (QImode, target, 0);
26335
26336       emit_insn
26337         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
26338                       gen_rtx_fmt_ee (EQ, QImode,
26339                                       gen_rtx_REG ((enum machine_mode) d->flag,
26340                                                    FLAGS_REG),
26341                                       const0_rtx)));
26342       return SUBREG_REG (target);
26343     }
26344   else
26345     return target;
26346 }
26347
26348 /* Subroutine of ix86_expand_builtin to take care of insns with
26349    variable number of operands.  */
26350
26351 static rtx
26352 ix86_expand_args_builtin (const struct builtin_description *d,
26353                           tree exp, rtx target)
26354 {
26355   rtx pat, real_target;
26356   unsigned int i, nargs;
26357   unsigned int nargs_constant = 0;
26358   int num_memory = 0;
26359   struct
26360     {
26361       rtx op;
26362       enum machine_mode mode;
26363     } args[4];
26364   bool last_arg_count = false;
26365   enum insn_code icode = d->icode;
26366   const struct insn_data_d *insn_p = &insn_data[icode];
26367   enum machine_mode tmode = insn_p->operand[0].mode;
26368   enum machine_mode rmode = VOIDmode;
26369   bool swap = false;
26370   enum rtx_code comparison = d->comparison;
26371
26372   switch ((enum ix86_builtin_func_type) d->flag)
26373     {
26374     case INT_FTYPE_V8SF_V8SF_PTEST:
26375     case INT_FTYPE_V4DI_V4DI_PTEST:
26376     case INT_FTYPE_V4DF_V4DF_PTEST:
26377     case INT_FTYPE_V4SF_V4SF_PTEST:
26378     case INT_FTYPE_V2DI_V2DI_PTEST:
26379     case INT_FTYPE_V2DF_V2DF_PTEST:
26380       return ix86_expand_sse_ptest (d, exp, target);
26381     case FLOAT128_FTYPE_FLOAT128:
26382     case FLOAT_FTYPE_FLOAT:
26383     case INT_FTYPE_INT:
26384     case UINT64_FTYPE_INT:
26385     case UINT16_FTYPE_UINT16:
26386     case INT64_FTYPE_INT64:
26387     case INT64_FTYPE_V4SF:
26388     case INT64_FTYPE_V2DF:
26389     case INT_FTYPE_V16QI:
26390     case INT_FTYPE_V8QI:
26391     case INT_FTYPE_V8SF:
26392     case INT_FTYPE_V4DF:
26393     case INT_FTYPE_V4SF:
26394     case INT_FTYPE_V2DF:
26395     case V16QI_FTYPE_V16QI:
26396     case V8SI_FTYPE_V8SF:
26397     case V8SI_FTYPE_V4SI:
26398     case V8HI_FTYPE_V8HI:
26399     case V8HI_FTYPE_V16QI:
26400     case V8QI_FTYPE_V8QI:
26401     case V8SF_FTYPE_V8SF:
26402     case V8SF_FTYPE_V8SI:
26403     case V8SF_FTYPE_V4SF:
26404     case V8SF_FTYPE_V8HI:
26405     case V4SI_FTYPE_V4SI:
26406     case V4SI_FTYPE_V16QI:
26407     case V4SI_FTYPE_V4SF:
26408     case V4SI_FTYPE_V8SI:
26409     case V4SI_FTYPE_V8HI:
26410     case V4SI_FTYPE_V4DF:
26411     case V4SI_FTYPE_V2DF:
26412     case V4HI_FTYPE_V4HI:
26413     case V4DF_FTYPE_V4DF:
26414     case V4DF_FTYPE_V4SI:
26415     case V4DF_FTYPE_V4SF:
26416     case V4DF_FTYPE_V2DF:
26417     case V4SF_FTYPE_V4SF:
26418     case V4SF_FTYPE_V4SI:
26419     case V4SF_FTYPE_V8SF:
26420     case V4SF_FTYPE_V4DF:
26421     case V4SF_FTYPE_V8HI:
26422     case V4SF_FTYPE_V2DF:
26423     case V2DI_FTYPE_V2DI:
26424     case V2DI_FTYPE_V16QI:
26425     case V2DI_FTYPE_V8HI:
26426     case V2DI_FTYPE_V4SI:
26427     case V2DF_FTYPE_V2DF:
26428     case V2DF_FTYPE_V4SI:
26429     case V2DF_FTYPE_V4DF:
26430     case V2DF_FTYPE_V4SF:
26431     case V2DF_FTYPE_V2SI:
26432     case V2SI_FTYPE_V2SI:
26433     case V2SI_FTYPE_V4SF:
26434     case V2SI_FTYPE_V2SF:
26435     case V2SI_FTYPE_V2DF:
26436     case V2SF_FTYPE_V2SF:
26437     case V2SF_FTYPE_V2SI:
26438       nargs = 1;
26439       break;
26440     case V4SF_FTYPE_V4SF_VEC_MERGE:
26441     case V2DF_FTYPE_V2DF_VEC_MERGE:
26442       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
26443     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
26444     case V16QI_FTYPE_V16QI_V16QI:
26445     case V16QI_FTYPE_V8HI_V8HI:
26446     case V8QI_FTYPE_V8QI_V8QI:
26447     case V8QI_FTYPE_V4HI_V4HI:
26448     case V8HI_FTYPE_V8HI_V8HI:
26449     case V8HI_FTYPE_V16QI_V16QI:
26450     case V8HI_FTYPE_V4SI_V4SI:
26451     case V8SF_FTYPE_V8SF_V8SF:
26452     case V8SF_FTYPE_V8SF_V8SI:
26453     case V4SI_FTYPE_V4SI_V4SI:
26454     case V4SI_FTYPE_V8HI_V8HI:
26455     case V4SI_FTYPE_V4SF_V4SF:
26456     case V4SI_FTYPE_V2DF_V2DF:
26457     case V4HI_FTYPE_V4HI_V4HI:
26458     case V4HI_FTYPE_V8QI_V8QI:
26459     case V4HI_FTYPE_V2SI_V2SI:
26460     case V4DF_FTYPE_V4DF_V4DF:
26461     case V4DF_FTYPE_V4DF_V4DI:
26462     case V4SF_FTYPE_V4SF_V4SF:
26463     case V4SF_FTYPE_V4SF_V4SI:
26464     case V4SF_FTYPE_V4SF_V2SI:
26465     case V4SF_FTYPE_V4SF_V2DF:
26466     case V4SF_FTYPE_V4SF_DI:
26467     case V4SF_FTYPE_V4SF_SI:
26468     case V2DI_FTYPE_V2DI_V2DI:
26469     case V2DI_FTYPE_V16QI_V16QI:
26470     case V2DI_FTYPE_V4SI_V4SI:
26471     case V2DI_FTYPE_V2DI_V16QI:
26472     case V2DI_FTYPE_V2DF_V2DF:
26473     case V2SI_FTYPE_V2SI_V2SI:
26474     case V2SI_FTYPE_V4HI_V4HI:
26475     case V2SI_FTYPE_V2SF_V2SF:
26476     case V2DF_FTYPE_V2DF_V2DF:
26477     case V2DF_FTYPE_V2DF_V4SF:
26478     case V2DF_FTYPE_V2DF_V2DI:
26479     case V2DF_FTYPE_V2DF_DI:
26480     case V2DF_FTYPE_V2DF_SI:
26481     case V2SF_FTYPE_V2SF_V2SF:
26482     case V1DI_FTYPE_V1DI_V1DI:
26483     case V1DI_FTYPE_V8QI_V8QI:
26484     case V1DI_FTYPE_V2SI_V2SI:
26485       if (comparison == UNKNOWN)
26486         return ix86_expand_binop_builtin (icode, exp, target);
26487       nargs = 2;
26488       break;
26489     case V4SF_FTYPE_V4SF_V4SF_SWAP:
26490     case V2DF_FTYPE_V2DF_V2DF_SWAP:
26491       gcc_assert (comparison != UNKNOWN);
26492       nargs = 2;
26493       swap = true;
26494       break;
26495     case V8HI_FTYPE_V8HI_V8HI_COUNT:
26496     case V8HI_FTYPE_V8HI_SI_COUNT:
26497     case V4SI_FTYPE_V4SI_V4SI_COUNT:
26498     case V4SI_FTYPE_V4SI_SI_COUNT:
26499     case V4HI_FTYPE_V4HI_V4HI_COUNT:
26500     case V4HI_FTYPE_V4HI_SI_COUNT:
26501     case V2DI_FTYPE_V2DI_V2DI_COUNT:
26502     case V2DI_FTYPE_V2DI_SI_COUNT:
26503     case V2SI_FTYPE_V2SI_V2SI_COUNT:
26504     case V2SI_FTYPE_V2SI_SI_COUNT:
26505     case V1DI_FTYPE_V1DI_V1DI_COUNT:
26506     case V1DI_FTYPE_V1DI_SI_COUNT:
26507       nargs = 2;
26508       last_arg_count = true;
26509       break;
26510     case UINT64_FTYPE_UINT64_UINT64:
26511     case UINT_FTYPE_UINT_UINT:
26512     case UINT_FTYPE_UINT_USHORT:
26513     case UINT_FTYPE_UINT_UCHAR:
26514     case UINT16_FTYPE_UINT16_INT:
26515     case UINT8_FTYPE_UINT8_INT:
26516       nargs = 2;
26517       break;
26518     case V2DI_FTYPE_V2DI_INT_CONVERT:
26519       nargs = 2;
26520       rmode = V1TImode;
26521       nargs_constant = 1;
26522       break;
26523     case V8HI_FTYPE_V8HI_INT:
26524     case V8HI_FTYPE_V8SF_INT:
26525     case V8HI_FTYPE_V4SF_INT:
26526     case V8SF_FTYPE_V8SF_INT:
26527     case V4SI_FTYPE_V4SI_INT:
26528     case V4SI_FTYPE_V8SI_INT:
26529     case V4HI_FTYPE_V4HI_INT:
26530     case V4DF_FTYPE_V4DF_INT:
26531     case V4SF_FTYPE_V4SF_INT:
26532     case V4SF_FTYPE_V8SF_INT:
26533     case V2DI_FTYPE_V2DI_INT:
26534     case V2DF_FTYPE_V2DF_INT:
26535     case V2DF_FTYPE_V4DF_INT:
26536       nargs = 2;
26537       nargs_constant = 1;
26538       break;
26539     case V16QI_FTYPE_V16QI_V16QI_V16QI:
26540     case V8SF_FTYPE_V8SF_V8SF_V8SF:
26541     case V4DF_FTYPE_V4DF_V4DF_V4DF:
26542     case V4SF_FTYPE_V4SF_V4SF_V4SF:
26543     case V2DF_FTYPE_V2DF_V2DF_V2DF:
26544       nargs = 3;
26545       break;
26546     case V16QI_FTYPE_V16QI_V16QI_INT:
26547     case V8HI_FTYPE_V8HI_V8HI_INT:
26548     case V8SI_FTYPE_V8SI_V8SI_INT:
26549     case V8SI_FTYPE_V8SI_V4SI_INT:
26550     case V8SF_FTYPE_V8SF_V8SF_INT:
26551     case V8SF_FTYPE_V8SF_V4SF_INT:
26552     case V4SI_FTYPE_V4SI_V4SI_INT:
26553     case V4DF_FTYPE_V4DF_V4DF_INT:
26554     case V4DF_FTYPE_V4DF_V2DF_INT:
26555     case V4SF_FTYPE_V4SF_V4SF_INT:
26556     case V2DI_FTYPE_V2DI_V2DI_INT:
26557     case V2DF_FTYPE_V2DF_V2DF_INT:
26558       nargs = 3;
26559       nargs_constant = 1;
26560       break;
26561     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
26562       nargs = 3;
26563       rmode = V2DImode;
26564       nargs_constant = 1;
26565       break;
26566     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
26567       nargs = 3;
26568       rmode = DImode;
26569       nargs_constant = 1;
26570       break;
26571     case V2DI_FTYPE_V2DI_UINT_UINT:
26572       nargs = 3;
26573       nargs_constant = 2;
26574       break;
26575     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
26576     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
26577     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
26578     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
26579       nargs = 4;
26580       nargs_constant = 1;
26581       break;
26582     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
26583       nargs = 4;
26584       nargs_constant = 2;
26585       break;
26586     default:
26587       gcc_unreachable ();
26588     }
26589
26590   gcc_assert (nargs <= ARRAY_SIZE (args));
26591
26592   if (comparison != UNKNOWN)
26593     {
26594       gcc_assert (nargs == 2);
26595       return ix86_expand_sse_compare (d, exp, target, swap);
26596     }
26597
26598   if (rmode == VOIDmode || rmode == tmode)
26599     {
26600       if (optimize
26601           || target == 0
26602           || GET_MODE (target) != tmode
26603           || !insn_p->operand[0].predicate (target, tmode))
26604         target = gen_reg_rtx (tmode);
26605       real_target = target;
26606     }
26607   else
26608     {
26609       target = gen_reg_rtx (rmode);
26610       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
26611     }
26612
26613   for (i = 0; i < nargs; i++)
26614     {
26615       tree arg = CALL_EXPR_ARG (exp, i);
26616       rtx op = expand_normal (arg);
26617       enum machine_mode mode = insn_p->operand[i + 1].mode;
26618       bool match = insn_p->operand[i + 1].predicate (op, mode);
26619
26620       if (last_arg_count && (i + 1) == nargs)
26621         {
26622           /* SIMD shift insns take either an 8-bit immediate or
26623              register as count.  But builtin functions take int as
26624              count.  If count doesn't match, we put it in register.  */
26625           if (!match)
26626             {
26627               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
26628               if (!insn_p->operand[i + 1].predicate (op, mode))
26629                 op = copy_to_reg (op);
26630             }
26631         }
26632       else if ((nargs - i) <= nargs_constant)
26633         {
26634           if (!match)
26635             switch (icode)
26636               {
26637               case CODE_FOR_sse4_1_roundpd:
26638               case CODE_FOR_sse4_1_roundps:
26639               case CODE_FOR_sse4_1_roundsd:
26640               case CODE_FOR_sse4_1_roundss:
26641               case CODE_FOR_sse4_1_blendps:
26642               case CODE_FOR_avx_blendpd256:
26643               case CODE_FOR_avx_vpermilv4df:
26644               case CODE_FOR_avx_roundpd256:
26645               case CODE_FOR_avx_roundps256:
26646                 error ("the last argument must be a 4-bit immediate");
26647                 return const0_rtx;
26648
26649               case CODE_FOR_sse4_1_blendpd:
26650               case CODE_FOR_avx_vpermilv2df:
26651               case CODE_FOR_xop_vpermil2v2df3:
26652               case CODE_FOR_xop_vpermil2v4sf3:
26653               case CODE_FOR_xop_vpermil2v4df3:
26654               case CODE_FOR_xop_vpermil2v8sf3:
26655                 error ("the last argument must be a 2-bit immediate");
26656                 return const0_rtx;
26657
26658               case CODE_FOR_avx_vextractf128v4df:
26659               case CODE_FOR_avx_vextractf128v8sf:
26660               case CODE_FOR_avx_vextractf128v8si:
26661               case CODE_FOR_avx_vinsertf128v4df:
26662               case CODE_FOR_avx_vinsertf128v8sf:
26663               case CODE_FOR_avx_vinsertf128v8si:
26664                 error ("the last argument must be a 1-bit immediate");
26665                 return const0_rtx;
26666
26667               case CODE_FOR_avx_cmpsdv2df3:
26668               case CODE_FOR_avx_cmpssv4sf3:
26669               case CODE_FOR_avx_cmppdv2df3:
26670               case CODE_FOR_avx_cmppsv4sf3:
26671               case CODE_FOR_avx_cmppdv4df3:
26672               case CODE_FOR_avx_cmppsv8sf3:
26673                 error ("the last argument must be a 5-bit immediate");
26674                 return const0_rtx;
26675
26676              default:
26677                 switch (nargs_constant)
26678                   {
26679                   case 2:
26680                     if ((nargs - i) == nargs_constant)
26681                       {
26682                         error ("the next to last argument must be an 8-bit immediate");
26683                         break;
26684                       }
26685                   case 1:
26686                     error ("the last argument must be an 8-bit immediate");
26687                     break;
26688                   default:
26689                     gcc_unreachable ();
26690                   }
26691                 return const0_rtx;
26692               }
26693         }
26694       else
26695         {
26696           if (VECTOR_MODE_P (mode))
26697             op = safe_vector_operand (op, mode);
26698
26699           /* If we aren't optimizing, only allow one memory operand to
26700              be generated.  */
26701           if (memory_operand (op, mode))
26702             num_memory++;
26703
26704           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
26705             {
26706               if (optimize || !match || num_memory > 1)
26707                 op = copy_to_mode_reg (mode, op);
26708             }
26709           else
26710             {
26711               op = copy_to_reg (op);
26712               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
26713             }
26714         }
26715
26716       args[i].op = op;
26717       args[i].mode = mode;
26718     }
26719
26720   switch (nargs)
26721     {
26722     case 1:
26723       pat = GEN_FCN (icode) (real_target, args[0].op);
26724       break;
26725     case 2:
26726       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
26727       break;
26728     case 3:
26729       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
26730                              args[2].op);
26731       break;
26732     case 4:
26733       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
26734                              args[2].op, args[3].op);
26735       break;
26736     default:
26737       gcc_unreachable ();
26738     }
26739
26740   if (! pat)
26741     return 0;
26742
26743   emit_insn (pat);
26744   return target;
26745 }
26746
26747 /* Subroutine of ix86_expand_builtin to take care of special insns
26748    with variable number of operands.  */
26749
26750 static rtx
26751 ix86_expand_special_args_builtin (const struct builtin_description *d,
26752                                     tree exp, rtx target)
26753 {
26754   tree arg;
26755   rtx pat, op;
26756   unsigned int i, nargs, arg_adjust, memory;
26757   struct
26758     {
26759       rtx op;
26760       enum machine_mode mode;
26761     } args[3];
26762   enum insn_code icode = d->icode;
26763   bool last_arg_constant = false;
26764   const struct insn_data_d *insn_p = &insn_data[icode];
26765   enum machine_mode tmode = insn_p->operand[0].mode;
26766   enum { load, store } klass;
26767
26768   switch ((enum ix86_builtin_func_type) d->flag)
26769     {
26770     case VOID_FTYPE_VOID:
26771       if (icode == CODE_FOR_avx_vzeroupper)
26772         target = GEN_INT (vzeroupper_intrinsic);
26773       emit_insn (GEN_FCN (icode) (target));
26774       return 0;
26775     case VOID_FTYPE_UINT64:
26776     case VOID_FTYPE_UNSIGNED:
26777       nargs = 0;
26778       klass = store;
26779       memory = 0;
26780       break;
26781       break;
26782     case UINT64_FTYPE_VOID:
26783     case UNSIGNED_FTYPE_VOID:
26784       nargs = 0;
26785       klass = load;
26786       memory = 0;
26787       break;
26788     case UINT64_FTYPE_PUNSIGNED:
26789     case V2DI_FTYPE_PV2DI:
26790     case V32QI_FTYPE_PCCHAR:
26791     case V16QI_FTYPE_PCCHAR:
26792     case V8SF_FTYPE_PCV4SF:
26793     case V8SF_FTYPE_PCFLOAT:
26794     case V4SF_FTYPE_PCFLOAT:
26795     case V4DF_FTYPE_PCV2DF:
26796     case V4DF_FTYPE_PCDOUBLE:
26797     case V2DF_FTYPE_PCDOUBLE:
26798     case VOID_FTYPE_PVOID:
26799       nargs = 1;
26800       klass = load;
26801       memory = 0;
26802       break;
26803     case VOID_FTYPE_PV2SF_V4SF:
26804     case VOID_FTYPE_PV4DI_V4DI:
26805     case VOID_FTYPE_PV2DI_V2DI:
26806     case VOID_FTYPE_PCHAR_V32QI:
26807     case VOID_FTYPE_PCHAR_V16QI:
26808     case VOID_FTYPE_PFLOAT_V8SF:
26809     case VOID_FTYPE_PFLOAT_V4SF:
26810     case VOID_FTYPE_PDOUBLE_V4DF:
26811     case VOID_FTYPE_PDOUBLE_V2DF:
26812     case VOID_FTYPE_PULONGLONG_ULONGLONG:
26813     case VOID_FTYPE_PINT_INT:
26814       nargs = 1;
26815       klass = store;
26816       /* Reserve memory operand for target.  */
26817       memory = ARRAY_SIZE (args);
26818       break;
26819     case V4SF_FTYPE_V4SF_PCV2SF:
26820     case V2DF_FTYPE_V2DF_PCDOUBLE:
26821       nargs = 2;
26822       klass = load;
26823       memory = 1;
26824       break;
26825     case V8SF_FTYPE_PCV8SF_V8SI:
26826     case V4DF_FTYPE_PCV4DF_V4DI:
26827     case V4SF_FTYPE_PCV4SF_V4SI:
26828     case V2DF_FTYPE_PCV2DF_V2DI:
26829       nargs = 2;
26830       klass = load;
26831       memory = 0;
26832       break;
26833     case VOID_FTYPE_PV8SF_V8SI_V8SF:
26834     case VOID_FTYPE_PV4DF_V4DI_V4DF:
26835     case VOID_FTYPE_PV4SF_V4SI_V4SF:
26836     case VOID_FTYPE_PV2DF_V2DI_V2DF:
26837       nargs = 2;
26838       klass = store;
26839       /* Reserve memory operand for target.  */
26840       memory = ARRAY_SIZE (args);
26841       break;
26842     case VOID_FTYPE_UINT_UINT_UINT:
26843     case VOID_FTYPE_UINT64_UINT_UINT:
26844     case UCHAR_FTYPE_UINT_UINT_UINT:
26845     case UCHAR_FTYPE_UINT64_UINT_UINT:
26846       nargs = 3;
26847       klass = load;
26848       memory = ARRAY_SIZE (args);
26849       last_arg_constant = true;
26850       break;
26851     default:
26852       gcc_unreachable ();
26853     }
26854
26855   gcc_assert (nargs <= ARRAY_SIZE (args));
26856
26857   if (klass == store)
26858     {
26859       arg = CALL_EXPR_ARG (exp, 0);
26860       op = expand_normal (arg);
26861       gcc_assert (target == 0);
26862       if (memory)
26863         target = gen_rtx_MEM (tmode, copy_to_mode_reg (Pmode, op));
26864       else
26865         target = force_reg (tmode, op);
26866       arg_adjust = 1;
26867     }
26868   else
26869     {
26870       arg_adjust = 0;
26871       if (optimize
26872           || target == 0
26873           || GET_MODE (target) != tmode
26874           || !insn_p->operand[0].predicate (target, tmode))
26875         target = gen_reg_rtx (tmode);
26876     }
26877
26878   for (i = 0; i < nargs; i++)
26879     {
26880       enum machine_mode mode = insn_p->operand[i + 1].mode;
26881       bool match;
26882
26883       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
26884       op = expand_normal (arg);
26885       match = insn_p->operand[i + 1].predicate (op, mode);
26886
26887       if (last_arg_constant && (i + 1) == nargs)
26888         {
26889           if (!match)
26890             {
26891               if (icode == CODE_FOR_lwp_lwpvalsi3
26892                   || icode == CODE_FOR_lwp_lwpinssi3
26893                   || icode == CODE_FOR_lwp_lwpvaldi3
26894                   || icode == CODE_FOR_lwp_lwpinsdi3)
26895                 error ("the last argument must be a 32-bit immediate");
26896               else
26897                 error ("the last argument must be an 8-bit immediate");
26898               return const0_rtx;
26899             }
26900         }
26901       else
26902         {
26903           if (i == memory)
26904             {
26905               /* This must be the memory operand.  */
26906               op = gen_rtx_MEM (mode, copy_to_mode_reg (Pmode, op));
26907               gcc_assert (GET_MODE (op) == mode
26908                           || GET_MODE (op) == VOIDmode);
26909             }
26910           else
26911             {
26912               /* This must be register.  */
26913               if (VECTOR_MODE_P (mode))
26914                 op = safe_vector_operand (op, mode);
26915
26916               gcc_assert (GET_MODE (op) == mode
26917                           || GET_MODE (op) == VOIDmode);
26918               op = copy_to_mode_reg (mode, op);
26919             }
26920         }
26921
26922       args[i].op = op;
26923       args[i].mode = mode;
26924     }
26925
26926   switch (nargs)
26927     {
26928     case 0:
26929       pat = GEN_FCN (icode) (target);
26930       break;
26931     case 1:
26932       pat = GEN_FCN (icode) (target, args[0].op);
26933       break;
26934     case 2:
26935       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
26936       break;
26937     case 3:
26938       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
26939       break;
26940     default:
26941       gcc_unreachable ();
26942     }
26943
26944   if (! pat)
26945     return 0;
26946   emit_insn (pat);
26947   return klass == store ? 0 : target;
26948 }
26949
26950 /* Return the integer constant in ARG.  Constrain it to be in the range
26951    of the subparts of VEC_TYPE; issue an error if not.  */
26952
26953 static int
26954 get_element_number (tree vec_type, tree arg)
26955 {
26956   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
26957
26958   if (!host_integerp (arg, 1)
26959       || (elt = tree_low_cst (arg, 1), elt > max))
26960     {
26961       error ("selector must be an integer constant in the range 0..%wi", max);
26962       return 0;
26963     }
26964
26965   return elt;
26966 }
26967
26968 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
26969    ix86_expand_vector_init.  We DO have language-level syntax for this, in
26970    the form of  (type){ init-list }.  Except that since we can't place emms
26971    instructions from inside the compiler, we can't allow the use of MMX
26972    registers unless the user explicitly asks for it.  So we do *not* define
26973    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
26974    we have builtins invoked by mmintrin.h that gives us license to emit
26975    these sorts of instructions.  */
26976
26977 static rtx
26978 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
26979 {
26980   enum machine_mode tmode = TYPE_MODE (type);
26981   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
26982   int i, n_elt = GET_MODE_NUNITS (tmode);
26983   rtvec v = rtvec_alloc (n_elt);
26984
26985   gcc_assert (VECTOR_MODE_P (tmode));
26986   gcc_assert (call_expr_nargs (exp) == n_elt);
26987
26988   for (i = 0; i < n_elt; ++i)
26989     {
26990       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
26991       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
26992     }
26993
26994   if (!target || !register_operand (target, tmode))
26995     target = gen_reg_rtx (tmode);
26996
26997   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
26998   return target;
26999 }
27000
27001 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
27002    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
27003    had a language-level syntax for referencing vector elements.  */
27004
27005 static rtx
27006 ix86_expand_vec_ext_builtin (tree exp, rtx target)
27007 {
27008   enum machine_mode tmode, mode0;
27009   tree arg0, arg1;
27010   int elt;
27011   rtx op0;
27012
27013   arg0 = CALL_EXPR_ARG (exp, 0);
27014   arg1 = CALL_EXPR_ARG (exp, 1);
27015
27016   op0 = expand_normal (arg0);
27017   elt = get_element_number (TREE_TYPE (arg0), arg1);
27018
27019   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
27020   mode0 = TYPE_MODE (TREE_TYPE (arg0));
27021   gcc_assert (VECTOR_MODE_P (mode0));
27022
27023   op0 = force_reg (mode0, op0);
27024
27025   if (optimize || !target || !register_operand (target, tmode))
27026     target = gen_reg_rtx (tmode);
27027
27028   ix86_expand_vector_extract (true, target, op0, elt);
27029
27030   return target;
27031 }
27032
27033 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
27034    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
27035    a language-level syntax for referencing vector elements.  */
27036
27037 static rtx
27038 ix86_expand_vec_set_builtin (tree exp)
27039 {
27040   enum machine_mode tmode, mode1;
27041   tree arg0, arg1, arg2;
27042   int elt;
27043   rtx op0, op1, target;
27044
27045   arg0 = CALL_EXPR_ARG (exp, 0);
27046   arg1 = CALL_EXPR_ARG (exp, 1);
27047   arg2 = CALL_EXPR_ARG (exp, 2);
27048
27049   tmode = TYPE_MODE (TREE_TYPE (arg0));
27050   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
27051   gcc_assert (VECTOR_MODE_P (tmode));
27052
27053   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
27054   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
27055   elt = get_element_number (TREE_TYPE (arg0), arg2);
27056
27057   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
27058     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
27059
27060   op0 = force_reg (tmode, op0);
27061   op1 = force_reg (mode1, op1);
27062
27063   /* OP0 is the source of these builtin functions and shouldn't be
27064      modified.  Create a copy, use it and return it as target.  */
27065   target = gen_reg_rtx (tmode);
27066   emit_move_insn (target, op0);
27067   ix86_expand_vector_set (true, target, op1, elt);
27068
27069   return target;
27070 }
27071
27072 /* Expand an expression EXP that calls a built-in function,
27073    with result going to TARGET if that's convenient
27074    (and in mode MODE if that's convenient).
27075    SUBTARGET may be used as the target for computing one of EXP's operands.
27076    IGNORE is nonzero if the value is to be ignored.  */
27077
27078 static rtx
27079 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
27080                      enum machine_mode mode ATTRIBUTE_UNUSED,
27081                      int ignore ATTRIBUTE_UNUSED)
27082 {
27083   const struct builtin_description *d;
27084   size_t i;
27085   enum insn_code icode;
27086   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
27087   tree arg0, arg1, arg2;
27088   rtx op0, op1, op2, pat;
27089   enum machine_mode mode0, mode1, mode2;
27090   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
27091
27092   /* Determine whether the builtin function is available under the current ISA.
27093      Originally the builtin was not created if it wasn't applicable to the
27094      current ISA based on the command line switches.  With function specific
27095      options, we need to check in the context of the function making the call
27096      whether it is supported.  */
27097   if (ix86_builtins_isa[fcode].isa
27098       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
27099     {
27100       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
27101                                        NULL, NULL, false);
27102
27103       if (!opts)
27104         error ("%qE needs unknown isa option", fndecl);
27105       else
27106         {
27107           gcc_assert (opts != NULL);
27108           error ("%qE needs isa option %s", fndecl, opts);
27109           free (opts);
27110         }
27111       return const0_rtx;
27112     }
27113
27114   switch (fcode)
27115     {
27116     case IX86_BUILTIN_MASKMOVQ:
27117     case IX86_BUILTIN_MASKMOVDQU:
27118       icode = (fcode == IX86_BUILTIN_MASKMOVQ
27119                ? CODE_FOR_mmx_maskmovq
27120                : CODE_FOR_sse2_maskmovdqu);
27121       /* Note the arg order is different from the operand order.  */
27122       arg1 = CALL_EXPR_ARG (exp, 0);
27123       arg2 = CALL_EXPR_ARG (exp, 1);
27124       arg0 = CALL_EXPR_ARG (exp, 2);
27125       op0 = expand_normal (arg0);
27126       op1 = expand_normal (arg1);
27127       op2 = expand_normal (arg2);
27128       mode0 = insn_data[icode].operand[0].mode;
27129       mode1 = insn_data[icode].operand[1].mode;
27130       mode2 = insn_data[icode].operand[2].mode;
27131
27132       op0 = force_reg (Pmode, op0);
27133       op0 = gen_rtx_MEM (mode1, op0);
27134
27135       if (!insn_data[icode].operand[0].predicate (op0, mode0))
27136         op0 = copy_to_mode_reg (mode0, op0);
27137       if (!insn_data[icode].operand[1].predicate (op1, mode1))
27138         op1 = copy_to_mode_reg (mode1, op1);
27139       if (!insn_data[icode].operand[2].predicate (op2, mode2))
27140         op2 = copy_to_mode_reg (mode2, op2);
27141       pat = GEN_FCN (icode) (op0, op1, op2);
27142       if (! pat)
27143         return 0;
27144       emit_insn (pat);
27145       return 0;
27146
27147     case IX86_BUILTIN_LDMXCSR:
27148       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
27149       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
27150       emit_move_insn (target, op0);
27151       emit_insn (gen_sse_ldmxcsr (target));
27152       return 0;
27153
27154     case IX86_BUILTIN_STMXCSR:
27155       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
27156       emit_insn (gen_sse_stmxcsr (target));
27157       return copy_to_mode_reg (SImode, target);
27158
27159     case IX86_BUILTIN_CLFLUSH:
27160         arg0 = CALL_EXPR_ARG (exp, 0);
27161         op0 = expand_normal (arg0);
27162         icode = CODE_FOR_sse2_clflush;
27163         if (!insn_data[icode].operand[0].predicate (op0, Pmode))
27164             op0 = copy_to_mode_reg (Pmode, op0);
27165
27166         emit_insn (gen_sse2_clflush (op0));
27167         return 0;
27168
27169     case IX86_BUILTIN_MONITOR:
27170       arg0 = CALL_EXPR_ARG (exp, 0);
27171       arg1 = CALL_EXPR_ARG (exp, 1);
27172       arg2 = CALL_EXPR_ARG (exp, 2);
27173       op0 = expand_normal (arg0);
27174       op1 = expand_normal (arg1);
27175       op2 = expand_normal (arg2);
27176       if (!REG_P (op0))
27177         op0 = copy_to_mode_reg (Pmode, op0);
27178       if (!REG_P (op1))
27179         op1 = copy_to_mode_reg (SImode, op1);
27180       if (!REG_P (op2))
27181         op2 = copy_to_mode_reg (SImode, op2);
27182       emit_insn (ix86_gen_monitor (op0, op1, op2));
27183       return 0;
27184
27185     case IX86_BUILTIN_MWAIT:
27186       arg0 = CALL_EXPR_ARG (exp, 0);
27187       arg1 = CALL_EXPR_ARG (exp, 1);
27188       op0 = expand_normal (arg0);
27189       op1 = expand_normal (arg1);
27190       if (!REG_P (op0))
27191         op0 = copy_to_mode_reg (SImode, op0);
27192       if (!REG_P (op1))
27193         op1 = copy_to_mode_reg (SImode, op1);
27194       emit_insn (gen_sse3_mwait (op0, op1));
27195       return 0;
27196
27197     case IX86_BUILTIN_VEC_INIT_V2SI:
27198     case IX86_BUILTIN_VEC_INIT_V4HI:
27199     case IX86_BUILTIN_VEC_INIT_V8QI:
27200       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
27201
27202     case IX86_BUILTIN_VEC_EXT_V2DF:
27203     case IX86_BUILTIN_VEC_EXT_V2DI:
27204     case IX86_BUILTIN_VEC_EXT_V4SF:
27205     case IX86_BUILTIN_VEC_EXT_V4SI:
27206     case IX86_BUILTIN_VEC_EXT_V8HI:
27207     case IX86_BUILTIN_VEC_EXT_V2SI:
27208     case IX86_BUILTIN_VEC_EXT_V4HI:
27209     case IX86_BUILTIN_VEC_EXT_V16QI:
27210       return ix86_expand_vec_ext_builtin (exp, target);
27211
27212     case IX86_BUILTIN_VEC_SET_V2DI:
27213     case IX86_BUILTIN_VEC_SET_V4SF:
27214     case IX86_BUILTIN_VEC_SET_V4SI:
27215     case IX86_BUILTIN_VEC_SET_V8HI:
27216     case IX86_BUILTIN_VEC_SET_V4HI:
27217     case IX86_BUILTIN_VEC_SET_V16QI:
27218       return ix86_expand_vec_set_builtin (exp);
27219
27220     case IX86_BUILTIN_VEC_PERM_V2DF:
27221     case IX86_BUILTIN_VEC_PERM_V4SF:
27222     case IX86_BUILTIN_VEC_PERM_V2DI:
27223     case IX86_BUILTIN_VEC_PERM_V4SI:
27224     case IX86_BUILTIN_VEC_PERM_V8HI:
27225     case IX86_BUILTIN_VEC_PERM_V16QI:
27226     case IX86_BUILTIN_VEC_PERM_V2DI_U:
27227     case IX86_BUILTIN_VEC_PERM_V4SI_U:
27228     case IX86_BUILTIN_VEC_PERM_V8HI_U:
27229     case IX86_BUILTIN_VEC_PERM_V16QI_U:
27230     case IX86_BUILTIN_VEC_PERM_V4DF:
27231     case IX86_BUILTIN_VEC_PERM_V8SF:
27232       return ix86_expand_vec_perm_builtin (exp);
27233
27234     case IX86_BUILTIN_INFQ:
27235     case IX86_BUILTIN_HUGE_VALQ:
27236       {
27237         REAL_VALUE_TYPE inf;
27238         rtx tmp;
27239
27240         real_inf (&inf);
27241         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
27242
27243         tmp = validize_mem (force_const_mem (mode, tmp));
27244
27245         if (target == 0)
27246           target = gen_reg_rtx (mode);
27247
27248         emit_move_insn (target, tmp);
27249         return target;
27250       }
27251
27252     case IX86_BUILTIN_LLWPCB:
27253       arg0 = CALL_EXPR_ARG (exp, 0);
27254       op0 = expand_normal (arg0);
27255       icode = CODE_FOR_lwp_llwpcb;
27256       if (!insn_data[icode].operand[0].predicate (op0, Pmode))
27257         op0 = copy_to_mode_reg (Pmode, op0);
27258       emit_insn (gen_lwp_llwpcb (op0));
27259       return 0;
27260
27261     case IX86_BUILTIN_SLWPCB:
27262       icode = CODE_FOR_lwp_slwpcb;
27263       if (!target
27264           || !insn_data[icode].operand[0].predicate (target, Pmode))
27265         target = gen_reg_rtx (Pmode);
27266       emit_insn (gen_lwp_slwpcb (target));
27267       return target;
27268
27269     case IX86_BUILTIN_BEXTRI32:
27270     case IX86_BUILTIN_BEXTRI64:
27271       arg0 = CALL_EXPR_ARG (exp, 0);
27272       arg1 = CALL_EXPR_ARG (exp, 1);
27273       op0 = expand_normal (arg0);
27274       op1 = expand_normal (arg1);
27275       icode = (fcode == IX86_BUILTIN_BEXTRI32
27276           ? CODE_FOR_tbm_bextri_si
27277           : CODE_FOR_tbm_bextri_di);
27278       if (!CONST_INT_P (op1))
27279         {
27280           error ("last argument must be an immediate");
27281           return const0_rtx;
27282         }
27283       else
27284         {
27285           unsigned char length = (INTVAL (op1) >> 8) & 0xFF;
27286           unsigned char lsb_index = INTVAL (op1) & 0xFF;
27287           op1 = GEN_INT (length);
27288           op2 = GEN_INT (lsb_index);
27289           pat = GEN_FCN (icode) (target, op0, op1, op2);
27290           if (pat)
27291             emit_insn (pat);
27292           return target;
27293         }
27294
27295     case IX86_BUILTIN_RDRAND16_STEP:
27296       icode = CODE_FOR_rdrandhi_1;
27297       mode0 = HImode;
27298       goto rdrand_step;
27299
27300     case IX86_BUILTIN_RDRAND32_STEP:
27301       icode = CODE_FOR_rdrandsi_1;
27302       mode0 = SImode;
27303       goto rdrand_step;
27304
27305     case IX86_BUILTIN_RDRAND64_STEP:
27306       icode = CODE_FOR_rdranddi_1;
27307       mode0 = DImode;
27308
27309 rdrand_step:
27310       op0 = gen_reg_rtx (mode0);
27311       emit_insn (GEN_FCN (icode) (op0));
27312
27313       op1 = gen_reg_rtx (SImode);
27314       emit_move_insn (op1, CONST1_RTX (SImode));
27315
27316       /* Emit SImode conditional move.  */
27317       if (mode0 == HImode)
27318         {
27319           op2 = gen_reg_rtx (SImode);
27320           emit_insn (gen_zero_extendhisi2 (op2, op0));
27321         }
27322       else if (mode0 == SImode)
27323         op2 = op0;
27324       else
27325         op2 = gen_rtx_SUBREG (SImode, op0, 0);
27326
27327       pat = gen_rtx_GEU (VOIDmode, gen_rtx_REG (CCCmode, FLAGS_REG),
27328                          const0_rtx);
27329       emit_insn (gen_rtx_SET (VOIDmode, op1,
27330                               gen_rtx_IF_THEN_ELSE (SImode, pat, op2, op1)));
27331       emit_move_insn (target, op1);
27332
27333       arg0 = CALL_EXPR_ARG (exp, 0);
27334       op1 = expand_normal (arg0);
27335       if (!address_operand (op1, VOIDmode))
27336         op1 = copy_addr_to_reg (op1);
27337       emit_move_insn (gen_rtx_MEM (mode0, op1), op0);
27338       return target;
27339
27340     default:
27341       break;
27342     }
27343
27344   for (i = 0, d = bdesc_special_args;
27345        i < ARRAY_SIZE (bdesc_special_args);
27346        i++, d++)
27347     if (d->code == fcode)
27348       return ix86_expand_special_args_builtin (d, exp, target);
27349
27350   for (i = 0, d = bdesc_args;
27351        i < ARRAY_SIZE (bdesc_args);
27352        i++, d++)
27353     if (d->code == fcode)
27354       switch (fcode)
27355         {
27356         case IX86_BUILTIN_FABSQ:
27357         case IX86_BUILTIN_COPYSIGNQ:
27358           if (!TARGET_SSE2)
27359             /* Emit a normal call if SSE2 isn't available.  */
27360             return expand_call (exp, target, ignore);
27361         default:
27362           return ix86_expand_args_builtin (d, exp, target);
27363         }
27364
27365   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
27366     if (d->code == fcode)
27367       return ix86_expand_sse_comi (d, exp, target);
27368
27369   for (i = 0, d = bdesc_pcmpestr;
27370        i < ARRAY_SIZE (bdesc_pcmpestr);
27371        i++, d++)
27372     if (d->code == fcode)
27373       return ix86_expand_sse_pcmpestr (d, exp, target);
27374
27375   for (i = 0, d = bdesc_pcmpistr;
27376        i < ARRAY_SIZE (bdesc_pcmpistr);
27377        i++, d++)
27378     if (d->code == fcode)
27379       return ix86_expand_sse_pcmpistr (d, exp, target);
27380
27381   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
27382     if (d->code == fcode)
27383       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
27384                                             (enum ix86_builtin_func_type)
27385                                             d->flag, d->comparison);
27386
27387   gcc_unreachable ();
27388 }
27389
27390 /* Returns a function decl for a vectorized version of the builtin function
27391    with builtin function code FN and the result vector type TYPE, or NULL_TREE
27392    if it is not available.  */
27393
27394 static tree
27395 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
27396                                   tree type_in)
27397 {
27398   enum machine_mode in_mode, out_mode;
27399   int in_n, out_n;
27400   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
27401
27402   if (TREE_CODE (type_out) != VECTOR_TYPE
27403       || TREE_CODE (type_in) != VECTOR_TYPE
27404       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
27405     return NULL_TREE;
27406
27407   out_mode = TYPE_MODE (TREE_TYPE (type_out));
27408   out_n = TYPE_VECTOR_SUBPARTS (type_out);
27409   in_mode = TYPE_MODE (TREE_TYPE (type_in));
27410   in_n = TYPE_VECTOR_SUBPARTS (type_in);
27411
27412   switch (fn)
27413     {
27414     case BUILT_IN_SQRT:
27415       if (out_mode == DFmode && in_mode == DFmode)
27416         {
27417           if (out_n == 2 && in_n == 2)
27418             return ix86_builtins[IX86_BUILTIN_SQRTPD];
27419           else if (out_n == 4 && in_n == 4)
27420             return ix86_builtins[IX86_BUILTIN_SQRTPD256];
27421         }
27422       break;
27423
27424     case BUILT_IN_SQRTF:
27425       if (out_mode == SFmode && in_mode == SFmode)
27426         {
27427           if (out_n == 4 && in_n == 4)
27428             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
27429           else if (out_n == 8 && in_n == 8)
27430             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR256];
27431         }
27432       break;
27433
27434     case BUILT_IN_LRINT:
27435       if (out_mode == SImode && out_n == 4
27436           && in_mode == DFmode && in_n == 2)
27437         return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
27438       break;
27439
27440     case BUILT_IN_LRINTF:
27441       if (out_mode == SImode && in_mode == SFmode)
27442         {
27443           if (out_n == 4 && in_n == 4)
27444             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
27445           else if (out_n == 8 && in_n == 8)
27446             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ256];
27447         }
27448       break;
27449
27450     case BUILT_IN_COPYSIGN:
27451       if (out_mode == DFmode && in_mode == DFmode)
27452         {
27453           if (out_n == 2 && in_n == 2)
27454             return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
27455           else if (out_n == 4 && in_n == 4)
27456             return ix86_builtins[IX86_BUILTIN_CPYSGNPD256];
27457         }
27458       break;
27459
27460     case BUILT_IN_COPYSIGNF:
27461       if (out_mode == SFmode && in_mode == SFmode)
27462         {
27463           if (out_n == 4 && in_n == 4)
27464             return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
27465           else if (out_n == 8 && in_n == 8)
27466             return ix86_builtins[IX86_BUILTIN_CPYSGNPS256];
27467         }
27468       break;
27469
27470     case BUILT_IN_FMA:
27471       if (out_mode == DFmode && in_mode == DFmode)
27472         {
27473           if (out_n == 2 && in_n == 2)
27474             return ix86_builtins[IX86_BUILTIN_VFMADDPD];
27475           if (out_n == 4 && in_n == 4)
27476             return ix86_builtins[IX86_BUILTIN_VFMADDPD256];
27477         }
27478       break;
27479
27480     case BUILT_IN_FMAF:
27481       if (out_mode == SFmode && in_mode == SFmode)
27482         {
27483           if (out_n == 4 && in_n == 4)
27484             return ix86_builtins[IX86_BUILTIN_VFMADDPS];
27485           if (out_n == 8 && in_n == 8)
27486             return ix86_builtins[IX86_BUILTIN_VFMADDPS256];
27487         }
27488       break;
27489
27490     default:
27491       break;
27492     }
27493
27494   /* Dispatch to a handler for a vectorization library.  */
27495   if (ix86_veclib_handler)
27496     return ix86_veclib_handler ((enum built_in_function) fn, type_out,
27497                                 type_in);
27498
27499   return NULL_TREE;
27500 }
27501
27502 /* Handler for an SVML-style interface to
27503    a library with vectorized intrinsics.  */
27504
27505 static tree
27506 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
27507 {
27508   char name[20];
27509   tree fntype, new_fndecl, args;
27510   unsigned arity;
27511   const char *bname;
27512   enum machine_mode el_mode, in_mode;
27513   int n, in_n;
27514
27515   /* The SVML is suitable for unsafe math only.  */
27516   if (!flag_unsafe_math_optimizations)
27517     return NULL_TREE;
27518
27519   el_mode = TYPE_MODE (TREE_TYPE (type_out));
27520   n = TYPE_VECTOR_SUBPARTS (type_out);
27521   in_mode = TYPE_MODE (TREE_TYPE (type_in));
27522   in_n = TYPE_VECTOR_SUBPARTS (type_in);
27523   if (el_mode != in_mode
27524       || n != in_n)
27525     return NULL_TREE;
27526
27527   switch (fn)
27528     {
27529     case BUILT_IN_EXP:
27530     case BUILT_IN_LOG:
27531     case BUILT_IN_LOG10:
27532     case BUILT_IN_POW:
27533     case BUILT_IN_TANH:
27534     case BUILT_IN_TAN:
27535     case BUILT_IN_ATAN:
27536     case BUILT_IN_ATAN2:
27537     case BUILT_IN_ATANH:
27538     case BUILT_IN_CBRT:
27539     case BUILT_IN_SINH:
27540     case BUILT_IN_SIN:
27541     case BUILT_IN_ASINH:
27542     case BUILT_IN_ASIN:
27543     case BUILT_IN_COSH:
27544     case BUILT_IN_COS:
27545     case BUILT_IN_ACOSH:
27546     case BUILT_IN_ACOS:
27547       if (el_mode != DFmode || n != 2)
27548         return NULL_TREE;
27549       break;
27550
27551     case BUILT_IN_EXPF:
27552     case BUILT_IN_LOGF:
27553     case BUILT_IN_LOG10F:
27554     case BUILT_IN_POWF:
27555     case BUILT_IN_TANHF:
27556     case BUILT_IN_TANF:
27557     case BUILT_IN_ATANF:
27558     case BUILT_IN_ATAN2F:
27559     case BUILT_IN_ATANHF:
27560     case BUILT_IN_CBRTF:
27561     case BUILT_IN_SINHF:
27562     case BUILT_IN_SINF:
27563     case BUILT_IN_ASINHF:
27564     case BUILT_IN_ASINF:
27565     case BUILT_IN_COSHF:
27566     case BUILT_IN_COSF:
27567     case BUILT_IN_ACOSHF:
27568     case BUILT_IN_ACOSF:
27569       if (el_mode != SFmode || n != 4)
27570         return NULL_TREE;
27571       break;
27572
27573     default:
27574       return NULL_TREE;
27575     }
27576
27577   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
27578
27579   if (fn == BUILT_IN_LOGF)
27580     strcpy (name, "vmlsLn4");
27581   else if (fn == BUILT_IN_LOG)
27582     strcpy (name, "vmldLn2");
27583   else if (n == 4)
27584     {
27585       sprintf (name, "vmls%s", bname+10);
27586       name[strlen (name)-1] = '4';
27587     }
27588   else
27589     sprintf (name, "vmld%s2", bname+10);
27590
27591   /* Convert to uppercase. */
27592   name[4] &= ~0x20;
27593
27594   arity = 0;
27595   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
27596        args = TREE_CHAIN (args))
27597     arity++;
27598
27599   if (arity == 1)
27600     fntype = build_function_type_list (type_out, type_in, NULL);
27601   else
27602     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
27603
27604   /* Build a function declaration for the vectorized function.  */
27605   new_fndecl = build_decl (BUILTINS_LOCATION,
27606                            FUNCTION_DECL, get_identifier (name), fntype);
27607   TREE_PUBLIC (new_fndecl) = 1;
27608   DECL_EXTERNAL (new_fndecl) = 1;
27609   DECL_IS_NOVOPS (new_fndecl) = 1;
27610   TREE_READONLY (new_fndecl) = 1;
27611
27612   return new_fndecl;
27613 }
27614
27615 /* Handler for an ACML-style interface to
27616    a library with vectorized intrinsics.  */
27617
27618 static tree
27619 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
27620 {
27621   char name[20] = "__vr.._";
27622   tree fntype, new_fndecl, args;
27623   unsigned arity;
27624   const char *bname;
27625   enum machine_mode el_mode, in_mode;
27626   int n, in_n;
27627
27628   /* The ACML is 64bits only and suitable for unsafe math only as
27629      it does not correctly support parts of IEEE with the required
27630      precision such as denormals.  */
27631   if (!TARGET_64BIT
27632       || !flag_unsafe_math_optimizations)
27633     return NULL_TREE;
27634
27635   el_mode = TYPE_MODE (TREE_TYPE (type_out));
27636   n = TYPE_VECTOR_SUBPARTS (type_out);
27637   in_mode = TYPE_MODE (TREE_TYPE (type_in));
27638   in_n = TYPE_VECTOR_SUBPARTS (type_in);
27639   if (el_mode != in_mode
27640       || n != in_n)
27641     return NULL_TREE;
27642
27643   switch (fn)
27644     {
27645     case BUILT_IN_SIN:
27646     case BUILT_IN_COS:
27647     case BUILT_IN_EXP:
27648     case BUILT_IN_LOG:
27649     case BUILT_IN_LOG2:
27650     case BUILT_IN_LOG10:
27651       name[4] = 'd';
27652       name[5] = '2';
27653       if (el_mode != DFmode
27654           || n != 2)
27655         return NULL_TREE;
27656       break;
27657
27658     case BUILT_IN_SINF:
27659     case BUILT_IN_COSF:
27660     case BUILT_IN_EXPF:
27661     case BUILT_IN_POWF:
27662     case BUILT_IN_LOGF:
27663     case BUILT_IN_LOG2F:
27664     case BUILT_IN_LOG10F:
27665       name[4] = 's';
27666       name[5] = '4';
27667       if (el_mode != SFmode
27668           || n != 4)
27669         return NULL_TREE;
27670       break;
27671
27672     default:
27673       return NULL_TREE;
27674     }
27675
27676   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
27677   sprintf (name + 7, "%s", bname+10);
27678
27679   arity = 0;
27680   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
27681        args = TREE_CHAIN (args))
27682     arity++;
27683
27684   if (arity == 1)
27685     fntype = build_function_type_list (type_out, type_in, NULL);
27686   else
27687     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
27688
27689   /* Build a function declaration for the vectorized function.  */
27690   new_fndecl = build_decl (BUILTINS_LOCATION,
27691                            FUNCTION_DECL, get_identifier (name), fntype);
27692   TREE_PUBLIC (new_fndecl) = 1;
27693   DECL_EXTERNAL (new_fndecl) = 1;
27694   DECL_IS_NOVOPS (new_fndecl) = 1;
27695   TREE_READONLY (new_fndecl) = 1;
27696
27697   return new_fndecl;
27698 }
27699
27700
27701 /* Returns a decl of a function that implements conversion of an integer vector
27702    into a floating-point vector, or vice-versa.  DEST_TYPE and SRC_TYPE
27703    are the types involved when converting according to CODE.
27704    Return NULL_TREE if it is not available.  */
27705
27706 static tree
27707 ix86_vectorize_builtin_conversion (unsigned int code,
27708                                    tree dest_type, tree src_type)
27709 {
27710   if (! TARGET_SSE2)
27711     return NULL_TREE;
27712
27713   switch (code)
27714     {
27715     case FLOAT_EXPR:
27716       switch (TYPE_MODE (src_type))
27717         {
27718         case V4SImode:
27719           switch (TYPE_MODE (dest_type))
27720             {
27721             case V4SFmode:
27722               return (TYPE_UNSIGNED (src_type)
27723                       ? ix86_builtins[IX86_BUILTIN_CVTUDQ2PS]
27724                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS]);
27725             case V4DFmode:
27726               return (TYPE_UNSIGNED (src_type)
27727                       ? NULL_TREE
27728                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PD256]);
27729             default:
27730               return NULL_TREE;
27731             }
27732           break;
27733         case V8SImode:
27734           switch (TYPE_MODE (dest_type))
27735             {
27736             case V8SFmode:
27737               return (TYPE_UNSIGNED (src_type)
27738                       ? NULL_TREE
27739                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS256]);
27740             default:
27741               return NULL_TREE;
27742             }
27743           break;
27744         default:
27745           return NULL_TREE;
27746         }
27747
27748     case FIX_TRUNC_EXPR:
27749       switch (TYPE_MODE (dest_type))
27750         {
27751         case V4SImode:
27752           switch (TYPE_MODE (src_type))
27753             {
27754             case V4SFmode:
27755               return (TYPE_UNSIGNED (dest_type)
27756                       ? NULL_TREE
27757                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ]);
27758             case V4DFmode:
27759               return (TYPE_UNSIGNED (dest_type)
27760                       ? NULL_TREE
27761                       : ix86_builtins[IX86_BUILTIN_CVTTPD2DQ256]);
27762             default:
27763               return NULL_TREE;
27764             }
27765           break;
27766
27767         case V8SImode:
27768           switch (TYPE_MODE (src_type))
27769             {
27770             case V8SFmode:
27771               return (TYPE_UNSIGNED (dest_type)
27772                       ? NULL_TREE
27773                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ256]);
27774             default:
27775               return NULL_TREE;
27776             }
27777           break;
27778
27779         default:
27780           return NULL_TREE;
27781         }
27782
27783     default:
27784       return NULL_TREE;
27785     }
27786
27787   return NULL_TREE;
27788 }
27789
27790 /* Returns a code for a target-specific builtin that implements
27791    reciprocal of the function, or NULL_TREE if not available.  */
27792
27793 static tree
27794 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
27795                          bool sqrt ATTRIBUTE_UNUSED)
27796 {
27797   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
27798          && flag_finite_math_only && !flag_trapping_math
27799          && flag_unsafe_math_optimizations))
27800     return NULL_TREE;
27801
27802   if (md_fn)
27803     /* Machine dependent builtins.  */
27804     switch (fn)
27805       {
27806         /* Vectorized version of sqrt to rsqrt conversion.  */
27807       case IX86_BUILTIN_SQRTPS_NR:
27808         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
27809
27810       case IX86_BUILTIN_SQRTPS_NR256:
27811         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR256];
27812
27813       default:
27814         return NULL_TREE;
27815       }
27816   else
27817     /* Normal builtins.  */
27818     switch (fn)
27819       {
27820         /* Sqrt to rsqrt conversion.  */
27821       case BUILT_IN_SQRTF:
27822         return ix86_builtins[IX86_BUILTIN_RSQRTF];
27823
27824       default:
27825         return NULL_TREE;
27826       }
27827 }
27828 \f
27829 /* Helper for avx_vpermilps256_operand et al.  This is also used by
27830    the expansion functions to turn the parallel back into a mask.
27831    The return value is 0 for no match and the imm8+1 for a match.  */
27832
27833 int
27834 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
27835 {
27836   unsigned i, nelt = GET_MODE_NUNITS (mode);
27837   unsigned mask = 0;
27838   unsigned char ipar[8];
27839
27840   if (XVECLEN (par, 0) != (int) nelt)
27841     return 0;
27842
27843   /* Validate that all of the elements are constants, and not totally
27844      out of range.  Copy the data into an integral array to make the
27845      subsequent checks easier.  */
27846   for (i = 0; i < nelt; ++i)
27847     {
27848       rtx er = XVECEXP (par, 0, i);
27849       unsigned HOST_WIDE_INT ei;
27850
27851       if (!CONST_INT_P (er))
27852         return 0;
27853       ei = INTVAL (er);
27854       if (ei >= nelt)
27855         return 0;
27856       ipar[i] = ei;
27857     }
27858
27859   switch (mode)
27860     {
27861     case V4DFmode:
27862       /* In the 256-bit DFmode case, we can only move elements within
27863          a 128-bit lane.  */
27864       for (i = 0; i < 2; ++i)
27865         {
27866           if (ipar[i] >= 2)
27867             return 0;
27868           mask |= ipar[i] << i;
27869         }
27870       for (i = 2; i < 4; ++i)
27871         {
27872           if (ipar[i] < 2)
27873             return 0;
27874           mask |= (ipar[i] - 2) << i;
27875         }
27876       break;
27877
27878     case V8SFmode:
27879       /* In the 256-bit SFmode case, we have full freedom of movement
27880          within the low 128-bit lane, but the high 128-bit lane must
27881          mirror the exact same pattern.  */
27882       for (i = 0; i < 4; ++i)
27883         if (ipar[i] + 4 != ipar[i + 4])
27884           return 0;
27885       nelt = 4;
27886       /* FALLTHRU */
27887
27888     case V2DFmode:
27889     case V4SFmode:
27890       /* In the 128-bit case, we've full freedom in the placement of
27891          the elements from the source operand.  */
27892       for (i = 0; i < nelt; ++i)
27893         mask |= ipar[i] << (i * (nelt / 2));
27894       break;
27895
27896     default:
27897       gcc_unreachable ();
27898     }
27899
27900   /* Make sure success has a non-zero value by adding one.  */
27901   return mask + 1;
27902 }
27903
27904 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
27905    the expansion functions to turn the parallel back into a mask.
27906    The return value is 0 for no match and the imm8+1 for a match.  */
27907
27908 int
27909 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
27910 {
27911   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
27912   unsigned mask = 0;
27913   unsigned char ipar[8];
27914
27915   if (XVECLEN (par, 0) != (int) nelt)
27916     return 0;
27917
27918   /* Validate that all of the elements are constants, and not totally
27919      out of range.  Copy the data into an integral array to make the
27920      subsequent checks easier.  */
27921   for (i = 0; i < nelt; ++i)
27922     {
27923       rtx er = XVECEXP (par, 0, i);
27924       unsigned HOST_WIDE_INT ei;
27925
27926       if (!CONST_INT_P (er))
27927         return 0;
27928       ei = INTVAL (er);
27929       if (ei >= 2 * nelt)
27930         return 0;
27931       ipar[i] = ei;
27932     }
27933
27934   /* Validate that the halves of the permute are halves.  */
27935   for (i = 0; i < nelt2 - 1; ++i)
27936     if (ipar[i] + 1 != ipar[i + 1])
27937       return 0;
27938   for (i = nelt2; i < nelt - 1; ++i)
27939     if (ipar[i] + 1 != ipar[i + 1])
27940       return 0;
27941
27942   /* Reconstruct the mask.  */
27943   for (i = 0; i < 2; ++i)
27944     {
27945       unsigned e = ipar[i * nelt2];
27946       if (e % nelt2)
27947         return 0;
27948       e /= nelt2;
27949       mask |= e << (i * 4);
27950     }
27951
27952   /* Make sure success has a non-zero value by adding one.  */
27953   return mask + 1;
27954 }
27955 \f
27956
27957 /* Store OPERAND to the memory after reload is completed.  This means
27958    that we can't easily use assign_stack_local.  */
27959 rtx
27960 ix86_force_to_memory (enum machine_mode mode, rtx operand)
27961 {
27962   rtx result;
27963
27964   gcc_assert (reload_completed);
27965   if (ix86_using_red_zone ())
27966     {
27967       result = gen_rtx_MEM (mode,
27968                             gen_rtx_PLUS (Pmode,
27969                                           stack_pointer_rtx,
27970                                           GEN_INT (-RED_ZONE_SIZE)));
27971       emit_move_insn (result, operand);
27972     }
27973   else if (TARGET_64BIT)
27974     {
27975       switch (mode)
27976         {
27977         case HImode:
27978         case SImode:
27979           operand = gen_lowpart (DImode, operand);
27980           /* FALLTHRU */
27981         case DImode:
27982           emit_insn (
27983                       gen_rtx_SET (VOIDmode,
27984                                    gen_rtx_MEM (DImode,
27985                                                 gen_rtx_PRE_DEC (DImode,
27986                                                         stack_pointer_rtx)),
27987                                    operand));
27988           break;
27989         default:
27990           gcc_unreachable ();
27991         }
27992       result = gen_rtx_MEM (mode, stack_pointer_rtx);
27993     }
27994   else
27995     {
27996       switch (mode)
27997         {
27998         case DImode:
27999           {
28000             rtx operands[2];
28001             split_double_mode (mode, &operand, 1, operands, operands + 1);
28002             emit_insn (
28003                         gen_rtx_SET (VOIDmode,
28004                                      gen_rtx_MEM (SImode,
28005                                                   gen_rtx_PRE_DEC (Pmode,
28006                                                         stack_pointer_rtx)),
28007                                      operands[1]));
28008             emit_insn (
28009                         gen_rtx_SET (VOIDmode,
28010                                      gen_rtx_MEM (SImode,
28011                                                   gen_rtx_PRE_DEC (Pmode,
28012                                                         stack_pointer_rtx)),
28013                                      operands[0]));
28014           }
28015           break;
28016         case HImode:
28017           /* Store HImodes as SImodes.  */
28018           operand = gen_lowpart (SImode, operand);
28019           /* FALLTHRU */
28020         case SImode:
28021           emit_insn (
28022                       gen_rtx_SET (VOIDmode,
28023                                    gen_rtx_MEM (GET_MODE (operand),
28024                                                 gen_rtx_PRE_DEC (SImode,
28025                                                         stack_pointer_rtx)),
28026                                    operand));
28027           break;
28028         default:
28029           gcc_unreachable ();
28030         }
28031       result = gen_rtx_MEM (mode, stack_pointer_rtx);
28032     }
28033   return result;
28034 }
28035
28036 /* Free operand from the memory.  */
28037 void
28038 ix86_free_from_memory (enum machine_mode mode)
28039 {
28040   if (!ix86_using_red_zone ())
28041     {
28042       int size;
28043
28044       if (mode == DImode || TARGET_64BIT)
28045         size = 8;
28046       else
28047         size = 4;
28048       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
28049          to pop or add instruction if registers are available.  */
28050       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
28051                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
28052                                             GEN_INT (size))));
28053     }
28054 }
28055
28056 /* Implement TARGET_IRA_COVER_CLASSES.  If -mfpmath=sse, we prefer
28057    SSE_REGS to FLOAT_REGS if their costs for a pseudo are the
28058    same.  */
28059 static const reg_class_t *
28060 i386_ira_cover_classes (void)
28061 {
28062   static const reg_class_t sse_fpmath_classes[] = {
28063     GENERAL_REGS, SSE_REGS, MMX_REGS, FLOAT_REGS, LIM_REG_CLASSES
28064   };
28065   static const reg_class_t no_sse_fpmath_classes[] = {
28066     GENERAL_REGS, FLOAT_REGS, MMX_REGS, SSE_REGS, LIM_REG_CLASSES
28067   };
28068
28069  return TARGET_SSE_MATH ? sse_fpmath_classes : no_sse_fpmath_classes;
28070 }
28071
28072 /* Implement TARGET_PREFERRED_RELOAD_CLASS.
28073
28074    Put float CONST_DOUBLE in the constant pool instead of fp regs.
28075    QImode must go into class Q_REGS.
28076    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
28077    movdf to do mem-to-mem moves through integer regs.  */
28078
28079 static reg_class_t
28080 ix86_preferred_reload_class (rtx x, reg_class_t regclass)
28081 {
28082   enum machine_mode mode = GET_MODE (x);
28083
28084   /* We're only allowed to return a subclass of CLASS.  Many of the
28085      following checks fail for NO_REGS, so eliminate that early.  */
28086   if (regclass == NO_REGS)
28087     return NO_REGS;
28088
28089   /* All classes can load zeros.  */
28090   if (x == CONST0_RTX (mode))
28091     return regclass;
28092
28093   /* Force constants into memory if we are loading a (nonzero) constant into
28094      an MMX or SSE register.  This is because there are no MMX/SSE instructions
28095      to load from a constant.  */
28096   if (CONSTANT_P (x)
28097       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
28098     return NO_REGS;
28099
28100   /* Prefer SSE regs only, if we can use them for math.  */
28101   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
28102     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
28103
28104   /* Floating-point constants need more complex checks.  */
28105   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
28106     {
28107       /* General regs can load everything.  */
28108       if (reg_class_subset_p (regclass, GENERAL_REGS))
28109         return regclass;
28110
28111       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
28112          zero above.  We only want to wind up preferring 80387 registers if
28113          we plan on doing computation with them.  */
28114       if (TARGET_80387
28115           && standard_80387_constant_p (x))
28116         {
28117           /* Limit class to non-sse.  */
28118           if (regclass == FLOAT_SSE_REGS)
28119             return FLOAT_REGS;
28120           if (regclass == FP_TOP_SSE_REGS)
28121             return FP_TOP_REG;
28122           if (regclass == FP_SECOND_SSE_REGS)
28123             return FP_SECOND_REG;
28124           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
28125             return regclass;
28126         }
28127
28128       return NO_REGS;
28129     }
28130
28131   /* Generally when we see PLUS here, it's the function invariant
28132      (plus soft-fp const_int).  Which can only be computed into general
28133      regs.  */
28134   if (GET_CODE (x) == PLUS)
28135     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
28136
28137   /* QImode constants are easy to load, but non-constant QImode data
28138      must go into Q_REGS.  */
28139   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
28140     {
28141       if (reg_class_subset_p (regclass, Q_REGS))
28142         return regclass;
28143       if (reg_class_subset_p (Q_REGS, regclass))
28144         return Q_REGS;
28145       return NO_REGS;
28146     }
28147
28148   return regclass;
28149 }
28150
28151 /* Discourage putting floating-point values in SSE registers unless
28152    SSE math is being used, and likewise for the 387 registers.  */
28153 static reg_class_t
28154 ix86_preferred_output_reload_class (rtx x, reg_class_t regclass)
28155 {
28156   enum machine_mode mode = GET_MODE (x);
28157
28158   /* Restrict the output reload class to the register bank that we are doing
28159      math on.  If we would like not to return a subset of CLASS, reject this
28160      alternative: if reload cannot do this, it will still use its choice.  */
28161   mode = GET_MODE (x);
28162   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
28163     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
28164
28165   if (X87_FLOAT_MODE_P (mode))
28166     {
28167       if (regclass == FP_TOP_SSE_REGS)
28168         return FP_TOP_REG;
28169       else if (regclass == FP_SECOND_SSE_REGS)
28170         return FP_SECOND_REG;
28171       else
28172         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
28173     }
28174
28175   return regclass;
28176 }
28177
28178 static reg_class_t
28179 ix86_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
28180                        enum machine_mode mode,
28181                        secondary_reload_info *sri ATTRIBUTE_UNUSED)
28182 {
28183   /* QImode spills from non-QI registers require
28184      intermediate register on 32bit targets.  */
28185   if (!in_p && mode == QImode && !TARGET_64BIT
28186       && (rclass == GENERAL_REGS
28187           || rclass == LEGACY_REGS
28188           || rclass == INDEX_REGS))
28189     {
28190       int regno;
28191
28192       if (REG_P (x))
28193         regno = REGNO (x);
28194       else
28195         regno = -1;
28196
28197       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
28198         regno = true_regnum (x);
28199
28200       /* Return Q_REGS if the operand is in memory.  */
28201       if (regno == -1)
28202         return Q_REGS;
28203     }
28204
28205   return NO_REGS;
28206 }
28207
28208 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.  */
28209
28210 static bool
28211 ix86_class_likely_spilled_p (reg_class_t rclass)
28212 {
28213   switch (rclass)
28214     {
28215       case AREG:
28216       case DREG:
28217       case CREG:
28218       case BREG:
28219       case AD_REGS:
28220       case SIREG:
28221       case DIREG:
28222       case SSE_FIRST_REG:
28223       case FP_TOP_REG:
28224       case FP_SECOND_REG:
28225         return true;
28226
28227       default:
28228         break;
28229     }
28230
28231   return false;
28232 }
28233
28234 /* If we are copying between general and FP registers, we need a memory
28235    location. The same is true for SSE and MMX registers.
28236
28237    To optimize register_move_cost performance, allow inline variant.
28238
28239    The macro can't work reliably when one of the CLASSES is class containing
28240    registers from multiple units (SSE, MMX, integer).  We avoid this by never
28241    combining those units in single alternative in the machine description.
28242    Ensure that this constraint holds to avoid unexpected surprises.
28243
28244    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
28245    enforce these sanity checks.  */
28246
28247 static inline bool
28248 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
28249                                 enum machine_mode mode, int strict)
28250 {
28251   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
28252       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
28253       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
28254       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
28255       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
28256       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
28257     {
28258       gcc_assert (!strict);
28259       return true;
28260     }
28261
28262   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
28263     return true;
28264
28265   /* ??? This is a lie.  We do have moves between mmx/general, and for
28266      mmx/sse2.  But by saying we need secondary memory we discourage the
28267      register allocator from using the mmx registers unless needed.  */
28268   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
28269     return true;
28270
28271   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
28272     {
28273       /* SSE1 doesn't have any direct moves from other classes.  */
28274       if (!TARGET_SSE2)
28275         return true;
28276
28277       /* If the target says that inter-unit moves are more expensive
28278          than moving through memory, then don't generate them.  */
28279       if (!TARGET_INTER_UNIT_MOVES)
28280         return true;
28281
28282       /* Between SSE and general, we have moves no larger than word size.  */
28283       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
28284         return true;
28285     }
28286
28287   return false;
28288 }
28289
28290 bool
28291 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
28292                               enum machine_mode mode, int strict)
28293 {
28294   return inline_secondary_memory_needed (class1, class2, mode, strict);
28295 }
28296
28297 /* Return true if the registers in CLASS cannot represent the change from
28298    modes FROM to TO.  */
28299
28300 bool
28301 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
28302                                enum reg_class regclass)
28303 {
28304   if (from == to)
28305     return false;
28306
28307   /* x87 registers can't do subreg at all, as all values are reformatted
28308      to extended precision.  */
28309   if (MAYBE_FLOAT_CLASS_P (regclass))
28310     return true;
28311
28312   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
28313     {
28314       /* Vector registers do not support QI or HImode loads.  If we don't
28315          disallow a change to these modes, reload will assume it's ok to
28316          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
28317          the vec_dupv4hi pattern.  */
28318       if (GET_MODE_SIZE (from) < 4)
28319         return true;
28320
28321       /* Vector registers do not support subreg with nonzero offsets, which
28322          are otherwise valid for integer registers.  Since we can't see
28323          whether we have a nonzero offset from here, prohibit all
28324          nonparadoxical subregs changing size.  */
28325       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
28326         return true;
28327     }
28328
28329   return false;
28330 }
28331
28332 /* Return the cost of moving data of mode M between a
28333    register and memory.  A value of 2 is the default; this cost is
28334    relative to those in `REGISTER_MOVE_COST'.
28335
28336    This function is used extensively by register_move_cost that is used to
28337    build tables at startup.  Make it inline in this case.
28338    When IN is 2, return maximum of in and out move cost.
28339
28340    If moving between registers and memory is more expensive than
28341    between two registers, you should define this macro to express the
28342    relative cost.
28343
28344    Model also increased moving costs of QImode registers in non
28345    Q_REGS classes.
28346  */
28347 static inline int
28348 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
28349                          int in)
28350 {
28351   int cost;
28352   if (FLOAT_CLASS_P (regclass))
28353     {
28354       int index;
28355       switch (mode)
28356         {
28357           case SFmode:
28358             index = 0;
28359             break;
28360           case DFmode:
28361             index = 1;
28362             break;
28363           case XFmode:
28364             index = 2;
28365             break;
28366           default:
28367             return 100;
28368         }
28369       if (in == 2)
28370         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
28371       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
28372     }
28373   if (SSE_CLASS_P (regclass))
28374     {
28375       int index;
28376       switch (GET_MODE_SIZE (mode))
28377         {
28378           case 4:
28379             index = 0;
28380             break;
28381           case 8:
28382             index = 1;
28383             break;
28384           case 16:
28385             index = 2;
28386             break;
28387           default:
28388             return 100;
28389         }
28390       if (in == 2)
28391         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
28392       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
28393     }
28394   if (MMX_CLASS_P (regclass))
28395     {
28396       int index;
28397       switch (GET_MODE_SIZE (mode))
28398         {
28399           case 4:
28400             index = 0;
28401             break;
28402           case 8:
28403             index = 1;
28404             break;
28405           default:
28406             return 100;
28407         }
28408       if (in)
28409         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
28410       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
28411     }
28412   switch (GET_MODE_SIZE (mode))
28413     {
28414       case 1:
28415         if (Q_CLASS_P (regclass) || TARGET_64BIT)
28416           {
28417             if (!in)
28418               return ix86_cost->int_store[0];
28419             if (TARGET_PARTIAL_REG_DEPENDENCY
28420                 && optimize_function_for_speed_p (cfun))
28421               cost = ix86_cost->movzbl_load;
28422             else
28423               cost = ix86_cost->int_load[0];
28424             if (in == 2)
28425               return MAX (cost, ix86_cost->int_store[0]);
28426             return cost;
28427           }
28428         else
28429           {
28430            if (in == 2)
28431              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
28432            if (in)
28433              return ix86_cost->movzbl_load;
28434            else
28435              return ix86_cost->int_store[0] + 4;
28436           }
28437         break;
28438       case 2:
28439         if (in == 2)
28440           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
28441         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
28442       default:
28443         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
28444         if (mode == TFmode)
28445           mode = XFmode;
28446         if (in == 2)
28447           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
28448         else if (in)
28449           cost = ix86_cost->int_load[2];
28450         else
28451           cost = ix86_cost->int_store[2];
28452         return (cost * (((int) GET_MODE_SIZE (mode)
28453                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
28454     }
28455 }
28456
28457 static int
28458 ix86_memory_move_cost (enum machine_mode mode, reg_class_t regclass,
28459                        bool in)
28460 {
28461   return inline_memory_move_cost (mode, (enum reg_class) regclass, in ? 1 : 0);
28462 }
28463
28464
28465 /* Return the cost of moving data from a register in class CLASS1 to
28466    one in class CLASS2.
28467
28468    It is not required that the cost always equal 2 when FROM is the same as TO;
28469    on some machines it is expensive to move between registers if they are not
28470    general registers.  */
28471
28472 static int
28473 ix86_register_move_cost (enum machine_mode mode, reg_class_t class1_i,
28474                          reg_class_t class2_i)
28475 {
28476   enum reg_class class1 = (enum reg_class) class1_i;
28477   enum reg_class class2 = (enum reg_class) class2_i;
28478
28479   /* In case we require secondary memory, compute cost of the store followed
28480      by load.  In order to avoid bad register allocation choices, we need
28481      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
28482
28483   if (inline_secondary_memory_needed (class1, class2, mode, 0))
28484     {
28485       int cost = 1;
28486
28487       cost += inline_memory_move_cost (mode, class1, 2);
28488       cost += inline_memory_move_cost (mode, class2, 2);
28489
28490       /* In case of copying from general_purpose_register we may emit multiple
28491          stores followed by single load causing memory size mismatch stall.
28492          Count this as arbitrarily high cost of 20.  */
28493       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
28494         cost += 20;
28495
28496       /* In the case of FP/MMX moves, the registers actually overlap, and we
28497          have to switch modes in order to treat them differently.  */
28498       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
28499           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
28500         cost += 20;
28501
28502       return cost;
28503     }
28504
28505   /* Moves between SSE/MMX and integer unit are expensive.  */
28506   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
28507       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
28508
28509     /* ??? By keeping returned value relatively high, we limit the number
28510        of moves between integer and MMX/SSE registers for all targets.
28511        Additionally, high value prevents problem with x86_modes_tieable_p(),
28512        where integer modes in MMX/SSE registers are not tieable
28513        because of missing QImode and HImode moves to, from or between
28514        MMX/SSE registers.  */
28515     return MAX (8, ix86_cost->mmxsse_to_integer);
28516
28517   if (MAYBE_FLOAT_CLASS_P (class1))
28518     return ix86_cost->fp_move;
28519   if (MAYBE_SSE_CLASS_P (class1))
28520     return ix86_cost->sse_move;
28521   if (MAYBE_MMX_CLASS_P (class1))
28522     return ix86_cost->mmx_move;
28523   return 2;
28524 }
28525
28526 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
28527
28528 bool
28529 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
28530 {
28531   /* Flags and only flags can only hold CCmode values.  */
28532   if (CC_REGNO_P (regno))
28533     return GET_MODE_CLASS (mode) == MODE_CC;
28534   if (GET_MODE_CLASS (mode) == MODE_CC
28535       || GET_MODE_CLASS (mode) == MODE_RANDOM
28536       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
28537     return 0;
28538   if (FP_REGNO_P (regno))
28539     return VALID_FP_MODE_P (mode);
28540   if (SSE_REGNO_P (regno))
28541     {
28542       /* We implement the move patterns for all vector modes into and
28543          out of SSE registers, even when no operation instructions
28544          are available.  OImode move is available only when AVX is
28545          enabled.  */
28546       return ((TARGET_AVX && mode == OImode)
28547               || VALID_AVX256_REG_MODE (mode)
28548               || VALID_SSE_REG_MODE (mode)
28549               || VALID_SSE2_REG_MODE (mode)
28550               || VALID_MMX_REG_MODE (mode)
28551               || VALID_MMX_REG_MODE_3DNOW (mode));
28552     }
28553   if (MMX_REGNO_P (regno))
28554     {
28555       /* We implement the move patterns for 3DNOW modes even in MMX mode,
28556          so if the register is available at all, then we can move data of
28557          the given mode into or out of it.  */
28558       return (VALID_MMX_REG_MODE (mode)
28559               || VALID_MMX_REG_MODE_3DNOW (mode));
28560     }
28561
28562   if (mode == QImode)
28563     {
28564       /* Take care for QImode values - they can be in non-QI regs,
28565          but then they do cause partial register stalls.  */
28566       if (regno <= BX_REG || TARGET_64BIT)
28567         return 1;
28568       if (!TARGET_PARTIAL_REG_STALL)
28569         return 1;
28570       return reload_in_progress || reload_completed;
28571     }
28572   /* We handle both integer and floats in the general purpose registers.  */
28573   else if (VALID_INT_MODE_P (mode))
28574     return 1;
28575   else if (VALID_FP_MODE_P (mode))
28576     return 1;
28577   else if (VALID_DFP_MODE_P (mode))
28578     return 1;
28579   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
28580      on to use that value in smaller contexts, this can easily force a
28581      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
28582      supporting DImode, allow it.  */
28583   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
28584     return 1;
28585
28586   return 0;
28587 }
28588
28589 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
28590    tieable integer mode.  */
28591
28592 static bool
28593 ix86_tieable_integer_mode_p (enum machine_mode mode)
28594 {
28595   switch (mode)
28596     {
28597     case HImode:
28598     case SImode:
28599       return true;
28600
28601     case QImode:
28602       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
28603
28604     case DImode:
28605       return TARGET_64BIT;
28606
28607     default:
28608       return false;
28609     }
28610 }
28611
28612 /* Return true if MODE1 is accessible in a register that can hold MODE2
28613    without copying.  That is, all register classes that can hold MODE2
28614    can also hold MODE1.  */
28615
28616 bool
28617 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
28618 {
28619   if (mode1 == mode2)
28620     return true;
28621
28622   if (ix86_tieable_integer_mode_p (mode1)
28623       && ix86_tieable_integer_mode_p (mode2))
28624     return true;
28625
28626   /* MODE2 being XFmode implies fp stack or general regs, which means we
28627      can tie any smaller floating point modes to it.  Note that we do not
28628      tie this with TFmode.  */
28629   if (mode2 == XFmode)
28630     return mode1 == SFmode || mode1 == DFmode;
28631
28632   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
28633      that we can tie it with SFmode.  */
28634   if (mode2 == DFmode)
28635     return mode1 == SFmode;
28636
28637   /* If MODE2 is only appropriate for an SSE register, then tie with
28638      any other mode acceptable to SSE registers.  */
28639   if (GET_MODE_SIZE (mode2) == 16
28640       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
28641     return (GET_MODE_SIZE (mode1) == 16
28642             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
28643
28644   /* If MODE2 is appropriate for an MMX register, then tie
28645      with any other mode acceptable to MMX registers.  */
28646   if (GET_MODE_SIZE (mode2) == 8
28647       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
28648     return (GET_MODE_SIZE (mode1) == 8
28649             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
28650
28651   return false;
28652 }
28653
28654 /* Compute a (partial) cost for rtx X.  Return true if the complete
28655    cost has been computed, and false if subexpressions should be
28656    scanned.  In either case, *TOTAL contains the cost result.  */
28657
28658 static bool
28659 ix86_rtx_costs (rtx x, int code, int outer_code_i, int *total, bool speed)
28660 {
28661   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
28662   enum machine_mode mode = GET_MODE (x);
28663   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
28664
28665   switch (code)
28666     {
28667     case CONST_INT:
28668     case CONST:
28669     case LABEL_REF:
28670     case SYMBOL_REF:
28671       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
28672         *total = 3;
28673       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
28674         *total = 2;
28675       else if (flag_pic && SYMBOLIC_CONST (x)
28676                && (!TARGET_64BIT
28677                    || (!GET_CODE (x) != LABEL_REF
28678                        && (GET_CODE (x) != SYMBOL_REF
28679                            || !SYMBOL_REF_LOCAL_P (x)))))
28680         *total = 1;
28681       else
28682         *total = 0;
28683       return true;
28684
28685     case CONST_DOUBLE:
28686       if (mode == VOIDmode)
28687         *total = 0;
28688       else
28689         switch (standard_80387_constant_p (x))
28690           {
28691           case 1: /* 0.0 */
28692             *total = 1;
28693             break;
28694           default: /* Other constants */
28695             *total = 2;
28696             break;
28697           case 0:
28698           case -1:
28699             /* Start with (MEM (SYMBOL_REF)), since that's where
28700                it'll probably end up.  Add a penalty for size.  */
28701             *total = (COSTS_N_INSNS (1)
28702                       + (flag_pic != 0 && !TARGET_64BIT)
28703                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
28704             break;
28705           }
28706       return true;
28707
28708     case ZERO_EXTEND:
28709       /* The zero extensions is often completely free on x86_64, so make
28710          it as cheap as possible.  */
28711       if (TARGET_64BIT && mode == DImode
28712           && GET_MODE (XEXP (x, 0)) == SImode)
28713         *total = 1;
28714       else if (TARGET_ZERO_EXTEND_WITH_AND)
28715         *total = cost->add;
28716       else
28717         *total = cost->movzx;
28718       return false;
28719
28720     case SIGN_EXTEND:
28721       *total = cost->movsx;
28722       return false;
28723
28724     case ASHIFT:
28725       if (CONST_INT_P (XEXP (x, 1))
28726           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
28727         {
28728           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
28729           if (value == 1)
28730             {
28731               *total = cost->add;
28732               return false;
28733             }
28734           if ((value == 2 || value == 3)
28735               && cost->lea <= cost->shift_const)
28736             {
28737               *total = cost->lea;
28738               return false;
28739             }
28740         }
28741       /* FALLTHRU */
28742
28743     case ROTATE:
28744     case ASHIFTRT:
28745     case LSHIFTRT:
28746     case ROTATERT:
28747       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
28748         {
28749           if (CONST_INT_P (XEXP (x, 1)))
28750             {
28751               if (INTVAL (XEXP (x, 1)) > 32)
28752                 *total = cost->shift_const + COSTS_N_INSNS (2);
28753               else
28754                 *total = cost->shift_const * 2;
28755             }
28756           else
28757             {
28758               if (GET_CODE (XEXP (x, 1)) == AND)
28759                 *total = cost->shift_var * 2;
28760               else
28761                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
28762             }
28763         }
28764       else
28765         {
28766           if (CONST_INT_P (XEXP (x, 1)))
28767             *total = cost->shift_const;
28768           else
28769             *total = cost->shift_var;
28770         }
28771       return false;
28772
28773     case FMA:
28774       {
28775         rtx sub;
28776
28777         gcc_assert (FLOAT_MODE_P (mode));
28778         gcc_assert (TARGET_FMA || TARGET_FMA4);
28779
28780         /* ??? SSE scalar/vector cost should be used here.  */
28781         /* ??? Bald assumption that fma has the same cost as fmul.  */
28782         *total = cost->fmul;
28783         *total += rtx_cost (XEXP (x, 1), FMA, speed);
28784
28785         /* Negate in op0 or op2 is free: FMS, FNMA, FNMS.  */
28786         sub = XEXP (x, 0);
28787         if (GET_CODE (sub) == NEG)
28788           sub = XEXP (x, 0);
28789         *total += rtx_cost (sub, FMA, speed);
28790
28791         sub = XEXP (x, 2);
28792         if (GET_CODE (sub) == NEG)
28793           sub = XEXP (x, 0);
28794         *total += rtx_cost (sub, FMA, speed);
28795         return true;
28796       }
28797
28798     case MULT:
28799       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28800         {
28801           /* ??? SSE scalar cost should be used here.  */
28802           *total = cost->fmul;
28803           return false;
28804         }
28805       else if (X87_FLOAT_MODE_P (mode))
28806         {
28807           *total = cost->fmul;
28808           return false;
28809         }
28810       else if (FLOAT_MODE_P (mode))
28811         {
28812           /* ??? SSE vector cost should be used here.  */
28813           *total = cost->fmul;
28814           return false;
28815         }
28816       else
28817         {
28818           rtx op0 = XEXP (x, 0);
28819           rtx op1 = XEXP (x, 1);
28820           int nbits;
28821           if (CONST_INT_P (XEXP (x, 1)))
28822             {
28823               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
28824               for (nbits = 0; value != 0; value &= value - 1)
28825                 nbits++;
28826             }
28827           else
28828             /* This is arbitrary.  */
28829             nbits = 7;
28830
28831           /* Compute costs correctly for widening multiplication.  */
28832           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
28833               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
28834                  == GET_MODE_SIZE (mode))
28835             {
28836               int is_mulwiden = 0;
28837               enum machine_mode inner_mode = GET_MODE (op0);
28838
28839               if (GET_CODE (op0) == GET_CODE (op1))
28840                 is_mulwiden = 1, op1 = XEXP (op1, 0);
28841               else if (CONST_INT_P (op1))
28842                 {
28843                   if (GET_CODE (op0) == SIGN_EXTEND)
28844                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
28845                                   == INTVAL (op1);
28846                   else
28847                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
28848                 }
28849
28850               if (is_mulwiden)
28851                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
28852             }
28853
28854           *total = (cost->mult_init[MODE_INDEX (mode)]
28855                     + nbits * cost->mult_bit
28856                     + rtx_cost (op0, outer_code, speed) + rtx_cost (op1, outer_code, speed));
28857
28858           return true;
28859         }
28860
28861     case DIV:
28862     case UDIV:
28863     case MOD:
28864     case UMOD:
28865       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28866         /* ??? SSE cost should be used here.  */
28867         *total = cost->fdiv;
28868       else if (X87_FLOAT_MODE_P (mode))
28869         *total = cost->fdiv;
28870       else if (FLOAT_MODE_P (mode))
28871         /* ??? SSE vector cost should be used here.  */
28872         *total = cost->fdiv;
28873       else
28874         *total = cost->divide[MODE_INDEX (mode)];
28875       return false;
28876
28877     case PLUS:
28878       if (GET_MODE_CLASS (mode) == MODE_INT
28879                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
28880         {
28881           if (GET_CODE (XEXP (x, 0)) == PLUS
28882               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
28883               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
28884               && CONSTANT_P (XEXP (x, 1)))
28885             {
28886               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
28887               if (val == 2 || val == 4 || val == 8)
28888                 {
28889                   *total = cost->lea;
28890                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
28891                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
28892                                       outer_code, speed);
28893                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
28894                   return true;
28895                 }
28896             }
28897           else if (GET_CODE (XEXP (x, 0)) == MULT
28898                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
28899             {
28900               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
28901               if (val == 2 || val == 4 || val == 8)
28902                 {
28903                   *total = cost->lea;
28904                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
28905                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
28906                   return true;
28907                 }
28908             }
28909           else if (GET_CODE (XEXP (x, 0)) == PLUS)
28910             {
28911               *total = cost->lea;
28912               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
28913               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
28914               *total += rtx_cost (XEXP (x, 1), outer_code, speed);
28915               return true;
28916             }
28917         }
28918       /* FALLTHRU */
28919
28920     case MINUS:
28921       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28922         {
28923           /* ??? SSE cost should be used here.  */
28924           *total = cost->fadd;
28925           return false;
28926         }
28927       else if (X87_FLOAT_MODE_P (mode))
28928         {
28929           *total = cost->fadd;
28930           return false;
28931         }
28932       else if (FLOAT_MODE_P (mode))
28933         {
28934           /* ??? SSE vector cost should be used here.  */
28935           *total = cost->fadd;
28936           return false;
28937         }
28938       /* FALLTHRU */
28939
28940     case AND:
28941     case IOR:
28942     case XOR:
28943       if (!TARGET_64BIT && mode == DImode)
28944         {
28945           *total = (cost->add * 2
28946                     + (rtx_cost (XEXP (x, 0), outer_code, speed)
28947                        << (GET_MODE (XEXP (x, 0)) != DImode))
28948                     + (rtx_cost (XEXP (x, 1), outer_code, speed)
28949                        << (GET_MODE (XEXP (x, 1)) != DImode)));
28950           return true;
28951         }
28952       /* FALLTHRU */
28953
28954     case NEG:
28955       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28956         {
28957           /* ??? SSE cost should be used here.  */
28958           *total = cost->fchs;
28959           return false;
28960         }
28961       else if (X87_FLOAT_MODE_P (mode))
28962         {
28963           *total = cost->fchs;
28964           return false;
28965         }
28966       else if (FLOAT_MODE_P (mode))
28967         {
28968           /* ??? SSE vector cost should be used here.  */
28969           *total = cost->fchs;
28970           return false;
28971         }
28972       /* FALLTHRU */
28973
28974     case NOT:
28975       if (!TARGET_64BIT && mode == DImode)
28976         *total = cost->add * 2;
28977       else
28978         *total = cost->add;
28979       return false;
28980
28981     case COMPARE:
28982       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
28983           && XEXP (XEXP (x, 0), 1) == const1_rtx
28984           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
28985           && XEXP (x, 1) == const0_rtx)
28986         {
28987           /* This kind of construct is implemented using test[bwl].
28988              Treat it as if we had an AND.  */
28989           *total = (cost->add
28990                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed)
28991                     + rtx_cost (const1_rtx, outer_code, speed));
28992           return true;
28993         }
28994       return false;
28995
28996     case FLOAT_EXTEND:
28997       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
28998         *total = 0;
28999       return false;
29000
29001     case ABS:
29002       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
29003         /* ??? SSE cost should be used here.  */
29004         *total = cost->fabs;
29005       else if (X87_FLOAT_MODE_P (mode))
29006         *total = cost->fabs;
29007       else if (FLOAT_MODE_P (mode))
29008         /* ??? SSE vector cost should be used here.  */
29009         *total = cost->fabs;
29010       return false;
29011
29012     case SQRT:
29013       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
29014         /* ??? SSE cost should be used here.  */
29015         *total = cost->fsqrt;
29016       else if (X87_FLOAT_MODE_P (mode))
29017         *total = cost->fsqrt;
29018       else if (FLOAT_MODE_P (mode))
29019         /* ??? SSE vector cost should be used here.  */
29020         *total = cost->fsqrt;
29021       return false;
29022
29023     case UNSPEC:
29024       if (XINT (x, 1) == UNSPEC_TP)
29025         *total = 0;
29026       return false;
29027
29028     case VEC_SELECT:
29029     case VEC_CONCAT:
29030     case VEC_MERGE:
29031     case VEC_DUPLICATE:
29032       /* ??? Assume all of these vector manipulation patterns are
29033          recognizable.  In which case they all pretty much have the
29034          same cost.  */
29035      *total = COSTS_N_INSNS (1);
29036      return true;
29037
29038     default:
29039       return false;
29040     }
29041 }
29042
29043 #if TARGET_MACHO
29044
29045 static int current_machopic_label_num;
29046
29047 /* Given a symbol name and its associated stub, write out the
29048    definition of the stub.  */
29049
29050 void
29051 machopic_output_stub (FILE *file, const char *symb, const char *stub)
29052 {
29053   unsigned int length;
29054   char *binder_name, *symbol_name, lazy_ptr_name[32];
29055   int label = ++current_machopic_label_num;
29056
29057   /* For 64-bit we shouldn't get here.  */
29058   gcc_assert (!TARGET_64BIT);
29059
29060   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
29061   symb = targetm.strip_name_encoding (symb);
29062
29063   length = strlen (stub);
29064   binder_name = XALLOCAVEC (char, length + 32);
29065   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
29066
29067   length = strlen (symb);
29068   symbol_name = XALLOCAVEC (char, length + 32);
29069   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
29070
29071   sprintf (lazy_ptr_name, "L%d$lz", label);
29072
29073   if (MACHOPIC_ATT_STUB)
29074     switch_to_section (darwin_sections[machopic_picsymbol_stub3_section]);
29075   else if (MACHOPIC_PURE)
29076     {
29077       if (TARGET_DEEP_BRANCH_PREDICTION)
29078         switch_to_section (darwin_sections[machopic_picsymbol_stub2_section]);
29079       else
29080     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
29081     }
29082   else
29083     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
29084
29085   fprintf (file, "%s:\n", stub);
29086   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29087
29088   if (MACHOPIC_ATT_STUB)
29089     {
29090       fprintf (file, "\thlt ; hlt ; hlt ; hlt ; hlt\n");
29091     }
29092   else if (MACHOPIC_PURE)
29093     {
29094       /* PIC stub.  */
29095       if (TARGET_DEEP_BRANCH_PREDICTION)
29096         {
29097           /* 25-byte PIC stub using "CALL get_pc_thunk".  */
29098           rtx tmp = gen_rtx_REG (SImode, 2 /* ECX */);
29099           output_set_got (tmp, NULL_RTX);       /* "CALL ___<cpu>.get_pc_thunk.cx".  */
29100           fprintf (file, "LPC$%d:\tmovl\t%s-LPC$%d(%%ecx),%%ecx\n", label, lazy_ptr_name, label);
29101         }
29102       else
29103         {
29104           /* 26-byte PIC stub using inline picbase: "CALL L42 ! L42: pop %eax".  */
29105           fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%ecx\n", label, label);
29106           fprintf (file, "\tmovl %s-LPC$%d(%%ecx),%%ecx\n", lazy_ptr_name, label);
29107         }
29108       fprintf (file, "\tjmp\t*%%ecx\n");
29109     }
29110   else
29111     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
29112
29113   /* The AT&T-style ("self-modifying") stub is not lazily bound, thus
29114      it needs no stub-binding-helper.  */
29115   if (MACHOPIC_ATT_STUB)
29116     return;
29117
29118   fprintf (file, "%s:\n", binder_name);
29119
29120   if (MACHOPIC_PURE)
29121     {
29122       fprintf (file, "\tlea\t%s-%s(%%ecx),%%ecx\n", lazy_ptr_name, binder_name);
29123       fprintf (file, "\tpushl\t%%ecx\n");
29124     }
29125   else
29126     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
29127
29128   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
29129
29130   /* N.B. Keep the correspondence of these
29131      'symbol_ptr/symbol_ptr2/symbol_ptr3' sections consistent with the
29132      old-pic/new-pic/non-pic stubs; altering this will break
29133      compatibility with existing dylibs.  */
29134   if (MACHOPIC_PURE)
29135     {
29136       /* PIC stubs.  */
29137       if (TARGET_DEEP_BRANCH_PREDICTION)
29138         /* 25-byte PIC stub using "CALL get_pc_thunk".  */
29139         switch_to_section (darwin_sections[machopic_lazy_symbol_ptr2_section]);
29140       else
29141         /* 26-byte PIC stub using inline picbase: "CALL L42 ! L42: pop %ebx".  */
29142   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
29143     }
29144   else
29145     /* 16-byte -mdynamic-no-pic stub.  */
29146     switch_to_section(darwin_sections[machopic_lazy_symbol_ptr3_section]);
29147
29148   fprintf (file, "%s:\n", lazy_ptr_name);
29149   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
29150   fprintf (file, ASM_LONG "%s\n", binder_name);
29151 }
29152 #endif /* TARGET_MACHO */
29153
29154 /* Order the registers for register allocator.  */
29155
29156 void
29157 x86_order_regs_for_local_alloc (void)
29158 {
29159    int pos = 0;
29160    int i;
29161
29162    /* First allocate the local general purpose registers.  */
29163    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
29164      if (GENERAL_REGNO_P (i) && call_used_regs[i])
29165         reg_alloc_order [pos++] = i;
29166
29167    /* Global general purpose registers.  */
29168    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
29169      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
29170         reg_alloc_order [pos++] = i;
29171
29172    /* x87 registers come first in case we are doing FP math
29173       using them.  */
29174    if (!TARGET_SSE_MATH)
29175      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
29176        reg_alloc_order [pos++] = i;
29177
29178    /* SSE registers.  */
29179    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
29180      reg_alloc_order [pos++] = i;
29181    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
29182      reg_alloc_order [pos++] = i;
29183
29184    /* x87 registers.  */
29185    if (TARGET_SSE_MATH)
29186      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
29187        reg_alloc_order [pos++] = i;
29188
29189    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
29190      reg_alloc_order [pos++] = i;
29191
29192    /* Initialize the rest of array as we do not allocate some registers
29193       at all.  */
29194    while (pos < FIRST_PSEUDO_REGISTER)
29195      reg_alloc_order [pos++] = 0;
29196 }
29197
29198 /* Handle a "callee_pop_aggregate_return" attribute; arguments as
29199    in struct attribute_spec handler.  */
29200 static tree
29201 ix86_handle_callee_pop_aggregate_return (tree *node, tree name,
29202                                               tree args,
29203                                               int flags ATTRIBUTE_UNUSED,
29204                                               bool *no_add_attrs)
29205 {
29206   if (TREE_CODE (*node) != FUNCTION_TYPE
29207       && TREE_CODE (*node) != METHOD_TYPE
29208       && TREE_CODE (*node) != FIELD_DECL
29209       && TREE_CODE (*node) != TYPE_DECL)
29210     {
29211       warning (OPT_Wattributes, "%qE attribute only applies to functions",
29212                name);
29213       *no_add_attrs = true;
29214       return NULL_TREE;
29215     }
29216   if (TARGET_64BIT)
29217     {
29218       warning (OPT_Wattributes, "%qE attribute only available for 32-bit",
29219                name);
29220       *no_add_attrs = true;
29221       return NULL_TREE;
29222     }
29223   if (is_attribute_p ("callee_pop_aggregate_return", name))
29224     {
29225       tree cst;
29226
29227       cst = TREE_VALUE (args);
29228       if (TREE_CODE (cst) != INTEGER_CST)
29229         {
29230           warning (OPT_Wattributes,
29231                    "%qE attribute requires an integer constant argument",
29232                    name);
29233           *no_add_attrs = true;
29234         }
29235       else if (compare_tree_int (cst, 0) != 0
29236                && compare_tree_int (cst, 1) != 0)
29237         {
29238           warning (OPT_Wattributes,
29239                    "argument to %qE attribute is neither zero, nor one",
29240                    name);
29241           *no_add_attrs = true;
29242         }
29243
29244       return NULL_TREE;
29245     }
29246
29247   return NULL_TREE;
29248 }
29249
29250 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
29251    struct attribute_spec.handler.  */
29252 static tree
29253 ix86_handle_abi_attribute (tree *node, tree name,
29254                               tree args ATTRIBUTE_UNUSED,
29255                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
29256 {
29257   if (TREE_CODE (*node) != FUNCTION_TYPE
29258       && TREE_CODE (*node) != METHOD_TYPE
29259       && TREE_CODE (*node) != FIELD_DECL
29260       && TREE_CODE (*node) != TYPE_DECL)
29261     {
29262       warning (OPT_Wattributes, "%qE attribute only applies to functions",
29263                name);
29264       *no_add_attrs = true;
29265       return NULL_TREE;
29266     }
29267   if (!TARGET_64BIT)
29268     {
29269       warning (OPT_Wattributes, "%qE attribute only available for 64-bit",
29270                name);
29271       *no_add_attrs = true;
29272       return NULL_TREE;
29273     }
29274
29275   /* Can combine regparm with all attributes but fastcall.  */
29276   if (is_attribute_p ("ms_abi", name))
29277     {
29278       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
29279         {
29280           error ("ms_abi and sysv_abi attributes are not compatible");
29281         }
29282
29283       return NULL_TREE;
29284     }
29285   else if (is_attribute_p ("sysv_abi", name))
29286     {
29287       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
29288         {
29289           error ("ms_abi and sysv_abi attributes are not compatible");
29290         }
29291
29292       return NULL_TREE;
29293     }
29294
29295   return NULL_TREE;
29296 }
29297
29298 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
29299    struct attribute_spec.handler.  */
29300 static tree
29301 ix86_handle_struct_attribute (tree *node, tree name,
29302                               tree args ATTRIBUTE_UNUSED,
29303                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
29304 {
29305   tree *type = NULL;
29306   if (DECL_P (*node))
29307     {
29308       if (TREE_CODE (*node) == TYPE_DECL)
29309         type = &TREE_TYPE (*node);
29310     }
29311   else
29312     type = node;
29313
29314   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
29315                  || TREE_CODE (*type) == UNION_TYPE)))
29316     {
29317       warning (OPT_Wattributes, "%qE attribute ignored",
29318                name);
29319       *no_add_attrs = true;
29320     }
29321
29322   else if ((is_attribute_p ("ms_struct", name)
29323             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
29324            || ((is_attribute_p ("gcc_struct", name)
29325                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
29326     {
29327       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
29328                name);
29329       *no_add_attrs = true;
29330     }
29331
29332   return NULL_TREE;
29333 }
29334
29335 static tree
29336 ix86_handle_fndecl_attribute (tree *node, tree name,
29337                               tree args ATTRIBUTE_UNUSED,
29338                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
29339 {
29340   if (TREE_CODE (*node) != FUNCTION_DECL)
29341     {
29342       warning (OPT_Wattributes, "%qE attribute only applies to functions",
29343                name);
29344       *no_add_attrs = true;
29345     }
29346   return NULL_TREE;
29347 }
29348
29349 static bool
29350 ix86_ms_bitfield_layout_p (const_tree record_type)
29351 {
29352   return ((TARGET_MS_BITFIELD_LAYOUT
29353            && !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
29354           || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type)));
29355 }
29356
29357 /* Returns an expression indicating where the this parameter is
29358    located on entry to the FUNCTION.  */
29359
29360 static rtx
29361 x86_this_parameter (tree function)
29362 {
29363   tree type = TREE_TYPE (function);
29364   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
29365   int nregs;
29366
29367   if (TARGET_64BIT)
29368     {
29369       const int *parm_regs;
29370
29371       if (ix86_function_type_abi (type) == MS_ABI)
29372         parm_regs = x86_64_ms_abi_int_parameter_registers;
29373       else
29374         parm_regs = x86_64_int_parameter_registers;
29375       return gen_rtx_REG (DImode, parm_regs[aggr]);
29376     }
29377
29378   nregs = ix86_function_regparm (type, function);
29379
29380   if (nregs > 0 && !stdarg_p (type))
29381     {
29382       int regno;
29383
29384       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
29385         regno = aggr ? DX_REG : CX_REG;
29386       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
29387         {
29388           regno = CX_REG;
29389           if (aggr)
29390             return gen_rtx_MEM (SImode,
29391                                 plus_constant (stack_pointer_rtx, 4));
29392         }
29393       else
29394         {
29395           regno = AX_REG;
29396           if (aggr)
29397             {
29398               regno = DX_REG;
29399               if (nregs == 1)
29400                 return gen_rtx_MEM (SImode,
29401                                     plus_constant (stack_pointer_rtx, 4));
29402             }
29403         }
29404       return gen_rtx_REG (SImode, regno);
29405     }
29406
29407   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
29408 }
29409
29410 /* Determine whether x86_output_mi_thunk can succeed.  */
29411
29412 static bool
29413 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
29414                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
29415                          HOST_WIDE_INT vcall_offset, const_tree function)
29416 {
29417   /* 64-bit can handle anything.  */
29418   if (TARGET_64BIT)
29419     return true;
29420
29421   /* For 32-bit, everything's fine if we have one free register.  */
29422   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
29423     return true;
29424
29425   /* Need a free register for vcall_offset.  */
29426   if (vcall_offset)
29427     return false;
29428
29429   /* Need a free register for GOT references.  */
29430   if (flag_pic && !targetm.binds_local_p (function))
29431     return false;
29432
29433   /* Otherwise ok.  */
29434   return true;
29435 }
29436
29437 /* Output the assembler code for a thunk function.  THUNK_DECL is the
29438    declaration for the thunk function itself, FUNCTION is the decl for
29439    the target function.  DELTA is an immediate constant offset to be
29440    added to THIS.  If VCALL_OFFSET is nonzero, the word at
29441    *(*this + vcall_offset) should be added to THIS.  */
29442
29443 static void
29444 x86_output_mi_thunk (FILE *file,
29445                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
29446                      HOST_WIDE_INT vcall_offset, tree function)
29447 {
29448   rtx xops[3];
29449   rtx this_param = x86_this_parameter (function);
29450   rtx this_reg, tmp;
29451
29452   /* Make sure unwind info is emitted for the thunk if needed.  */
29453   final_start_function (emit_barrier (), file, 1);
29454
29455   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
29456      pull it in now and let DELTA benefit.  */
29457   if (REG_P (this_param))
29458     this_reg = this_param;
29459   else if (vcall_offset)
29460     {
29461       /* Put the this parameter into %eax.  */
29462       xops[0] = this_param;
29463       xops[1] = this_reg = gen_rtx_REG (Pmode, AX_REG);
29464       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
29465     }
29466   else
29467     this_reg = NULL_RTX;
29468
29469   /* Adjust the this parameter by a fixed constant.  */
29470   if (delta)
29471     {
29472       xops[0] = GEN_INT (delta);
29473       xops[1] = this_reg ? this_reg : this_param;
29474       if (TARGET_64BIT)
29475         {
29476           if (!x86_64_general_operand (xops[0], DImode))
29477             {
29478               tmp = gen_rtx_REG (DImode, R10_REG);
29479               xops[1] = tmp;
29480               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
29481               xops[0] = tmp;
29482               xops[1] = this_param;
29483             }
29484           if (x86_maybe_negate_const_int (&xops[0], DImode))
29485             output_asm_insn ("sub{q}\t{%0, %1|%1, %0}", xops);
29486           else
29487             output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
29488         }
29489       else if (x86_maybe_negate_const_int (&xops[0], SImode))
29490         output_asm_insn ("sub{l}\t{%0, %1|%1, %0}", xops);
29491       else
29492         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
29493     }
29494
29495   /* Adjust the this parameter by a value stored in the vtable.  */
29496   if (vcall_offset)
29497     {
29498       if (TARGET_64BIT)
29499         tmp = gen_rtx_REG (DImode, R10_REG);
29500       else
29501         {
29502           int tmp_regno = CX_REG;
29503           if (lookup_attribute ("fastcall",
29504                                 TYPE_ATTRIBUTES (TREE_TYPE (function)))
29505               || lookup_attribute ("thiscall",
29506                                    TYPE_ATTRIBUTES (TREE_TYPE (function))))
29507             tmp_regno = AX_REG;
29508           tmp = gen_rtx_REG (SImode, tmp_regno);
29509         }
29510
29511       xops[0] = gen_rtx_MEM (Pmode, this_reg);
29512       xops[1] = tmp;
29513       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
29514
29515       /* Adjust the this parameter.  */
29516       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
29517       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
29518         {
29519           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
29520           xops[0] = GEN_INT (vcall_offset);
29521           xops[1] = tmp2;
29522           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
29523           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
29524         }
29525       xops[1] = this_reg;
29526       output_asm_insn ("add%z1\t{%0, %1|%1, %0}", xops);
29527     }
29528
29529   /* If necessary, drop THIS back to its stack slot.  */
29530   if (this_reg && this_reg != this_param)
29531     {
29532       xops[0] = this_reg;
29533       xops[1] = this_param;
29534       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
29535     }
29536
29537   xops[0] = XEXP (DECL_RTL (function), 0);
29538   if (TARGET_64BIT)
29539     {
29540       if (!flag_pic || targetm.binds_local_p (function)
29541           || DEFAULT_ABI == MS_ABI)
29542         output_asm_insn ("jmp\t%P0", xops);
29543       /* All thunks should be in the same object as their target,
29544          and thus binds_local_p should be true.  */
29545       else if (TARGET_64BIT && cfun->machine->call_abi == MS_ABI)
29546         gcc_unreachable ();
29547       else
29548         {
29549           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
29550           tmp = gen_rtx_CONST (Pmode, tmp);
29551           tmp = gen_rtx_MEM (QImode, tmp);
29552           xops[0] = tmp;
29553           output_asm_insn ("jmp\t%A0", xops);
29554         }
29555     }
29556   else
29557     {
29558       if (!flag_pic || targetm.binds_local_p (function))
29559         output_asm_insn ("jmp\t%P0", xops);
29560       else
29561 #if TARGET_MACHO
29562         if (TARGET_MACHO)
29563           {
29564             rtx sym_ref = XEXP (DECL_RTL (function), 0);
29565             if (TARGET_MACHO_BRANCH_ISLANDS)
29566               sym_ref = (gen_rtx_SYMBOL_REF
29567                    (Pmode,
29568                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
29569             tmp = gen_rtx_MEM (QImode, sym_ref);
29570             xops[0] = tmp;
29571             output_asm_insn ("jmp\t%0", xops);
29572           }
29573         else
29574 #endif /* TARGET_MACHO */
29575         {
29576           tmp = gen_rtx_REG (SImode, CX_REG);
29577           output_set_got (tmp, NULL_RTX);
29578
29579           xops[1] = tmp;
29580           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
29581           output_asm_insn ("jmp\t{*}%1", xops);
29582         }
29583     }
29584   final_end_function ();
29585 }
29586
29587 static void
29588 x86_file_start (void)
29589 {
29590   default_file_start ();
29591 #if TARGET_MACHO
29592   darwin_file_start ();
29593 #endif
29594   if (X86_FILE_START_VERSION_DIRECTIVE)
29595     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
29596   if (X86_FILE_START_FLTUSED)
29597     fputs ("\t.global\t__fltused\n", asm_out_file);
29598   if (ix86_asm_dialect == ASM_INTEL)
29599     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
29600 }
29601
29602 int
29603 x86_field_alignment (tree field, int computed)
29604 {
29605   enum machine_mode mode;
29606   tree type = TREE_TYPE (field);
29607
29608   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
29609     return computed;
29610   mode = TYPE_MODE (strip_array_types (type));
29611   if (mode == DFmode || mode == DCmode
29612       || GET_MODE_CLASS (mode) == MODE_INT
29613       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
29614     return MIN (32, computed);
29615   return computed;
29616 }
29617
29618 /* Output assembler code to FILE to increment profiler label # LABELNO
29619    for profiling a function entry.  */
29620 void
29621 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
29622 {
29623   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
29624                                          : MCOUNT_NAME);
29625
29626   if (TARGET_64BIT)
29627     {
29628 #ifndef NO_PROFILE_COUNTERS
29629       fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
29630 #endif
29631
29632       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
29633         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name);
29634       else
29635         fprintf (file, "\tcall\t%s\n", mcount_name);
29636     }
29637   else if (flag_pic)
29638     {
29639 #ifndef NO_PROFILE_COUNTERS
29640       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
29641                LPREFIX, labelno);
29642 #endif
29643       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
29644     }
29645   else
29646     {
29647 #ifndef NO_PROFILE_COUNTERS
29648       fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
29649                LPREFIX, labelno);
29650 #endif
29651       fprintf (file, "\tcall\t%s\n", mcount_name);
29652     }
29653 }
29654
29655 /* We don't have exact information about the insn sizes, but we may assume
29656    quite safely that we are informed about all 1 byte insns and memory
29657    address sizes.  This is enough to eliminate unnecessary padding in
29658    99% of cases.  */
29659
29660 static int
29661 min_insn_size (rtx insn)
29662 {
29663   int l = 0, len;
29664
29665   if (!INSN_P (insn) || !active_insn_p (insn))
29666     return 0;
29667
29668   /* Discard alignments we've emit and jump instructions.  */
29669   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
29670       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
29671     return 0;
29672   if (JUMP_TABLE_DATA_P (insn))
29673     return 0;
29674
29675   /* Important case - calls are always 5 bytes.
29676      It is common to have many calls in the row.  */
29677   if (CALL_P (insn)
29678       && symbolic_reference_mentioned_p (PATTERN (insn))
29679       && !SIBLING_CALL_P (insn))
29680     return 5;
29681   len = get_attr_length (insn);
29682   if (len <= 1)
29683     return 1;
29684
29685   /* For normal instructions we rely on get_attr_length being exact,
29686      with a few exceptions.  */
29687   if (!JUMP_P (insn))
29688     {
29689       enum attr_type type = get_attr_type (insn);
29690
29691       switch (type)
29692         {
29693         case TYPE_MULTI:
29694           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
29695               || asm_noperands (PATTERN (insn)) >= 0)
29696             return 0;
29697           break;
29698         case TYPE_OTHER:
29699         case TYPE_FCMP:
29700           break;
29701         default:
29702           /* Otherwise trust get_attr_length.  */
29703           return len;
29704         }
29705
29706       l = get_attr_length_address (insn);
29707       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
29708         l = 4;
29709     }
29710   if (l)
29711     return 1+l;
29712   else
29713     return 2;
29714 }
29715
29716 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
29717
29718 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
29719    window.  */
29720
29721 static void
29722 ix86_avoid_jump_mispredicts (void)
29723 {
29724   rtx insn, start = get_insns ();
29725   int nbytes = 0, njumps = 0;
29726   int isjump = 0;
29727
29728   /* Look for all minimal intervals of instructions containing 4 jumps.
29729      The intervals are bounded by START and INSN.  NBYTES is the total
29730      size of instructions in the interval including INSN and not including
29731      START.  When the NBYTES is smaller than 16 bytes, it is possible
29732      that the end of START and INSN ends up in the same 16byte page.
29733
29734      The smallest offset in the page INSN can start is the case where START
29735      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
29736      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
29737      */
29738   for (insn = start; insn; insn = NEXT_INSN (insn))
29739     {
29740       int min_size;
29741
29742       if (LABEL_P (insn))
29743         {
29744           int align = label_to_alignment (insn);
29745           int max_skip = label_to_max_skip (insn);
29746
29747           if (max_skip > 15)
29748             max_skip = 15;
29749           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
29750              already in the current 16 byte page, because otherwise
29751              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
29752              bytes to reach 16 byte boundary.  */
29753           if (align <= 0
29754               || (align <= 3 && max_skip != (1 << align) - 1))
29755             max_skip = 0;
29756           if (dump_file)
29757             fprintf (dump_file, "Label %i with max_skip %i\n",
29758                      INSN_UID (insn), max_skip);
29759           if (max_skip)
29760             {
29761               while (nbytes + max_skip >= 16)
29762                 {
29763                   start = NEXT_INSN (start);
29764                   if ((JUMP_P (start)
29765                        && GET_CODE (PATTERN (start)) != ADDR_VEC
29766                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
29767                       || CALL_P (start))
29768                     njumps--, isjump = 1;
29769                   else
29770                     isjump = 0;
29771                   nbytes -= min_insn_size (start);
29772                 }
29773             }
29774           continue;
29775         }
29776
29777       min_size = min_insn_size (insn);
29778       nbytes += min_size;
29779       if (dump_file)
29780         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
29781                  INSN_UID (insn), min_size);
29782       if ((JUMP_P (insn)
29783            && GET_CODE (PATTERN (insn)) != ADDR_VEC
29784            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
29785           || CALL_P (insn))
29786         njumps++;
29787       else
29788         continue;
29789
29790       while (njumps > 3)
29791         {
29792           start = NEXT_INSN (start);
29793           if ((JUMP_P (start)
29794                && GET_CODE (PATTERN (start)) != ADDR_VEC
29795                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
29796               || CALL_P (start))
29797             njumps--, isjump = 1;
29798           else
29799             isjump = 0;
29800           nbytes -= min_insn_size (start);
29801         }
29802       gcc_assert (njumps >= 0);
29803       if (dump_file)
29804         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
29805                  INSN_UID (start), INSN_UID (insn), nbytes);
29806
29807       if (njumps == 3 && isjump && nbytes < 16)
29808         {
29809           int padsize = 15 - nbytes + min_insn_size (insn);
29810
29811           if (dump_file)
29812             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
29813                      INSN_UID (insn), padsize);
29814           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
29815         }
29816     }
29817 }
29818 #endif
29819
29820 /* AMD Athlon works faster
29821    when RET is not destination of conditional jump or directly preceded
29822    by other jump instruction.  We avoid the penalty by inserting NOP just
29823    before the RET instructions in such cases.  */
29824 static void
29825 ix86_pad_returns (void)
29826 {
29827   edge e;
29828   edge_iterator ei;
29829
29830   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
29831     {
29832       basic_block bb = e->src;
29833       rtx ret = BB_END (bb);
29834       rtx prev;
29835       bool replace = false;
29836
29837       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
29838           || optimize_bb_for_size_p (bb))
29839         continue;
29840       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
29841         if (active_insn_p (prev) || LABEL_P (prev))
29842           break;
29843       if (prev && LABEL_P (prev))
29844         {
29845           edge e;
29846           edge_iterator ei;
29847
29848           FOR_EACH_EDGE (e, ei, bb->preds)
29849             if (EDGE_FREQUENCY (e) && e->src->index >= 0
29850                 && !(e->flags & EDGE_FALLTHRU))
29851               replace = true;
29852         }
29853       if (!replace)
29854         {
29855           prev = prev_active_insn (ret);
29856           if (prev
29857               && ((JUMP_P (prev) && any_condjump_p (prev))
29858                   || CALL_P (prev)))
29859             replace = true;
29860           /* Empty functions get branch mispredict even when
29861              the jump destination is not visible to us.  */
29862           if (!prev && !optimize_function_for_size_p (cfun))
29863             replace = true;
29864         }
29865       if (replace)
29866         {
29867           emit_jump_insn_before (gen_return_internal_long (), ret);
29868           delete_insn (ret);
29869         }
29870     }
29871 }
29872
29873 /* Count the minimum number of instructions in BB.  Return 4 if the
29874    number of instructions >= 4.  */
29875
29876 static int 
29877 ix86_count_insn_bb (basic_block bb)
29878 {
29879   rtx insn;
29880   int insn_count = 0;
29881
29882   /* Count number of instructions in this block.  Return 4 if the number
29883      of instructions >= 4.  */
29884   FOR_BB_INSNS (bb, insn)
29885     {
29886       /* Only happen in exit blocks.  */
29887       if (JUMP_P (insn)
29888           && GET_CODE (PATTERN (insn)) == RETURN)
29889         break;
29890
29891       if (NONDEBUG_INSN_P (insn)
29892           && GET_CODE (PATTERN (insn)) != USE
29893           && GET_CODE (PATTERN (insn)) != CLOBBER)
29894         {
29895           insn_count++;
29896           if (insn_count >= 4)
29897             return insn_count;
29898         }
29899     }
29900
29901   return insn_count;
29902 }
29903
29904
29905 /* Count the minimum number of instructions in code path in BB.  
29906    Return 4 if the number of instructions >= 4.  */
29907
29908 static int 
29909 ix86_count_insn (basic_block bb)
29910 {
29911   edge e;
29912   edge_iterator ei;
29913   int min_prev_count;
29914
29915   /* Only bother counting instructions along paths with no
29916      more than 2 basic blocks between entry and exit.  Given
29917      that BB has an edge to exit, determine if a predecessor
29918      of BB has an edge from entry.  If so, compute the number
29919      of instructions in the predecessor block.  If there
29920      happen to be multiple such blocks, compute the minimum.  */
29921   min_prev_count = 4;
29922   FOR_EACH_EDGE (e, ei, bb->preds)
29923     {
29924       edge prev_e;
29925       edge_iterator prev_ei;
29926
29927       if (e->src == ENTRY_BLOCK_PTR)
29928         {
29929           min_prev_count = 0;
29930           break;
29931         }
29932       FOR_EACH_EDGE (prev_e, prev_ei, e->src->preds)
29933         {
29934           if (prev_e->src == ENTRY_BLOCK_PTR)
29935             {
29936               int count = ix86_count_insn_bb (e->src);
29937               if (count < min_prev_count)
29938                 min_prev_count = count;
29939               break;
29940             }
29941         }
29942     }
29943
29944   if (min_prev_count < 4)
29945     min_prev_count += ix86_count_insn_bb (bb);
29946
29947   return min_prev_count;
29948 }
29949
29950 /* Pad short funtion to 4 instructions.   */
29951
29952 static void
29953 ix86_pad_short_function (void)
29954 {
29955   edge e;
29956   edge_iterator ei;
29957
29958   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
29959     {
29960       rtx ret = BB_END (e->src);
29961       if (JUMP_P (ret) && GET_CODE (PATTERN (ret)) == RETURN)
29962         {
29963           int insn_count = ix86_count_insn (e->src);
29964
29965           /* Pad short function.  */
29966           if (insn_count < 4)
29967             {
29968               rtx insn = ret;
29969
29970               /* Find epilogue.  */
29971               while (insn
29972                      && (!NOTE_P (insn)
29973                          || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG))
29974                 insn = PREV_INSN (insn);
29975
29976               if (!insn)
29977                 insn = ret;
29978
29979               /* Two NOPs count as one instruction.  */
29980               insn_count = 2 * (4 - insn_count);
29981               emit_insn_before (gen_nops (GEN_INT (insn_count)), insn);
29982             }
29983         }
29984     }
29985 }
29986
29987 /* Implement machine specific optimizations.  We implement padding of returns
29988    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
29989 static void
29990 ix86_reorg (void)
29991 {
29992   /* We are freeing block_for_insn in the toplev to keep compatibility
29993      with old MDEP_REORGS that are not CFG based.  Recompute it now.  */
29994   compute_bb_for_insn ();
29995
29996   if (optimize && optimize_function_for_speed_p (cfun))
29997     {
29998       if (TARGET_PAD_SHORT_FUNCTION)
29999         ix86_pad_short_function ();
30000       else if (TARGET_PAD_RETURNS)
30001         ix86_pad_returns ();
30002 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
30003       if (TARGET_FOUR_JUMP_LIMIT)
30004         ix86_avoid_jump_mispredicts ();
30005 #endif
30006     }
30007
30008   /* Run the vzeroupper optimization if needed.  */
30009   if (TARGET_VZEROUPPER)
30010     move_or_delete_vzeroupper ();
30011 }
30012
30013 /* Return nonzero when QImode register that must be represented via REX prefix
30014    is used.  */
30015 bool
30016 x86_extended_QIreg_mentioned_p (rtx insn)
30017 {
30018   int i;
30019   extract_insn_cached (insn);
30020   for (i = 0; i < recog_data.n_operands; i++)
30021     if (REG_P (recog_data.operand[i])
30022         && REGNO (recog_data.operand[i]) > BX_REG)
30023        return true;
30024   return false;
30025 }
30026
30027 /* Return nonzero when P points to register encoded via REX prefix.
30028    Called via for_each_rtx.  */
30029 static int
30030 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
30031 {
30032    unsigned int regno;
30033    if (!REG_P (*p))
30034      return 0;
30035    regno = REGNO (*p);
30036    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
30037 }
30038
30039 /* Return true when INSN mentions register that must be encoded using REX
30040    prefix.  */
30041 bool
30042 x86_extended_reg_mentioned_p (rtx insn)
30043 {
30044   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
30045                        extended_reg_mentioned_1, NULL);
30046 }
30047
30048 /* If profitable, negate (without causing overflow) integer constant
30049    of mode MODE at location LOC.  Return true in this case.  */
30050 bool
30051 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
30052 {
30053   HOST_WIDE_INT val;
30054
30055   if (!CONST_INT_P (*loc))
30056     return false;
30057
30058   switch (mode)
30059     {
30060     case DImode:
30061       /* DImode x86_64 constants must fit in 32 bits.  */
30062       gcc_assert (x86_64_immediate_operand (*loc, mode));
30063
30064       mode = SImode;
30065       break;
30066
30067     case SImode:
30068     case HImode:
30069     case QImode:
30070       break;
30071
30072     default:
30073       gcc_unreachable ();
30074     }
30075
30076   /* Avoid overflows.  */
30077   if (mode_signbit_p (mode, *loc))
30078     return false;
30079
30080   val = INTVAL (*loc);
30081
30082   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
30083      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
30084   if ((val < 0 && val != -128)
30085       || val == 128)
30086     {
30087       *loc = GEN_INT (-val);
30088       return true;
30089     }
30090
30091   return false;
30092 }
30093
30094 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
30095    optabs would emit if we didn't have TFmode patterns.  */
30096
30097 void
30098 x86_emit_floatuns (rtx operands[2])
30099 {
30100   rtx neglab, donelab, i0, i1, f0, in, out;
30101   enum machine_mode mode, inmode;
30102
30103   inmode = GET_MODE (operands[1]);
30104   gcc_assert (inmode == SImode || inmode == DImode);
30105
30106   out = operands[0];
30107   in = force_reg (inmode, operands[1]);
30108   mode = GET_MODE (out);
30109   neglab = gen_label_rtx ();
30110   donelab = gen_label_rtx ();
30111   f0 = gen_reg_rtx (mode);
30112
30113   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
30114
30115   expand_float (out, in, 0);
30116
30117   emit_jump_insn (gen_jump (donelab));
30118   emit_barrier ();
30119
30120   emit_label (neglab);
30121
30122   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
30123                             1, OPTAB_DIRECT);
30124   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
30125                             1, OPTAB_DIRECT);
30126   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
30127
30128   expand_float (f0, i0, 0);
30129
30130   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
30131
30132   emit_label (donelab);
30133 }
30134 \f
30135 /* AVX does not support 32-byte integer vector operations,
30136    thus the longest vector we are faced with is V16QImode.  */
30137 #define MAX_VECT_LEN    16
30138
30139 struct expand_vec_perm_d
30140 {
30141   rtx target, op0, op1;
30142   unsigned char perm[MAX_VECT_LEN];
30143   enum machine_mode vmode;
30144   unsigned char nelt;
30145   bool testing_p;
30146 };
30147
30148 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
30149 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
30150
30151 /* Get a vector mode of the same size as the original but with elements
30152    twice as wide.  This is only guaranteed to apply to integral vectors.  */
30153
30154 static inline enum machine_mode
30155 get_mode_wider_vector (enum machine_mode o)
30156 {
30157   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
30158   enum machine_mode n = GET_MODE_WIDER_MODE (o);
30159   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
30160   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
30161   return n;
30162 }
30163
30164 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
30165    with all elements equal to VAR.  Return true if successful.  */
30166
30167 static bool
30168 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
30169                                    rtx target, rtx val)
30170 {
30171   bool ok;
30172
30173   switch (mode)
30174     {
30175     case V2SImode:
30176     case V2SFmode:
30177       if (!mmx_ok)
30178         return false;
30179       /* FALLTHRU */
30180
30181     case V4DFmode:
30182     case V4DImode:
30183     case V8SFmode:
30184     case V8SImode:
30185     case V2DFmode:
30186     case V2DImode:
30187     case V4SFmode:
30188     case V4SImode:
30189       {
30190         rtx insn, dup;
30191
30192         /* First attempt to recognize VAL as-is.  */
30193         dup = gen_rtx_VEC_DUPLICATE (mode, val);
30194         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
30195         if (recog_memoized (insn) < 0)
30196           {
30197             rtx seq;
30198             /* If that fails, force VAL into a register.  */
30199
30200             start_sequence ();
30201             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
30202             seq = get_insns ();
30203             end_sequence ();
30204             if (seq)
30205               emit_insn_before (seq, insn);
30206
30207             ok = recog_memoized (insn) >= 0;
30208             gcc_assert (ok);
30209           }
30210       }
30211       return true;
30212
30213     case V4HImode:
30214       if (!mmx_ok)
30215         return false;
30216       if (TARGET_SSE || TARGET_3DNOW_A)
30217         {
30218           rtx x;
30219
30220           val = gen_lowpart (SImode, val);
30221           x = gen_rtx_TRUNCATE (HImode, val);
30222           x = gen_rtx_VEC_DUPLICATE (mode, x);
30223           emit_insn (gen_rtx_SET (VOIDmode, target, x));
30224           return true;
30225         }
30226       goto widen;
30227
30228     case V8QImode:
30229       if (!mmx_ok)
30230         return false;
30231       goto widen;
30232
30233     case V8HImode:
30234       if (TARGET_SSE2)
30235         {
30236           struct expand_vec_perm_d dperm;
30237           rtx tmp1, tmp2;
30238
30239         permute:
30240           memset (&dperm, 0, sizeof (dperm));
30241           dperm.target = target;
30242           dperm.vmode = mode;
30243           dperm.nelt = GET_MODE_NUNITS (mode);
30244           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
30245
30246           /* Extend to SImode using a paradoxical SUBREG.  */
30247           tmp1 = gen_reg_rtx (SImode);
30248           emit_move_insn (tmp1, gen_lowpart (SImode, val));
30249
30250           /* Insert the SImode value as low element of a V4SImode vector. */
30251           tmp2 = gen_lowpart (V4SImode, dperm.op0);
30252           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
30253
30254           ok = (expand_vec_perm_1 (&dperm)
30255                 || expand_vec_perm_broadcast_1 (&dperm));
30256           gcc_assert (ok);
30257           return ok;
30258         }
30259       goto widen;
30260
30261     case V16QImode:
30262       if (TARGET_SSE2)
30263         goto permute;
30264       goto widen;
30265
30266     widen:
30267       /* Replicate the value once into the next wider mode and recurse.  */
30268       {
30269         enum machine_mode smode, wsmode, wvmode;
30270         rtx x;
30271
30272         smode = GET_MODE_INNER (mode);
30273         wvmode = get_mode_wider_vector (mode);
30274         wsmode = GET_MODE_INNER (wvmode);
30275
30276         val = convert_modes (wsmode, smode, val, true);
30277         x = expand_simple_binop (wsmode, ASHIFT, val,
30278                                  GEN_INT (GET_MODE_BITSIZE (smode)),
30279                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
30280         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
30281
30282         x = gen_lowpart (wvmode, target);
30283         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
30284         gcc_assert (ok);
30285         return ok;
30286       }
30287
30288     case V16HImode:
30289     case V32QImode:
30290       {
30291         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
30292         rtx x = gen_reg_rtx (hvmode);
30293
30294         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
30295         gcc_assert (ok);
30296
30297         x = gen_rtx_VEC_CONCAT (mode, x, x);
30298         emit_insn (gen_rtx_SET (VOIDmode, target, x));
30299       }
30300       return true;
30301
30302     default:
30303       return false;
30304     }
30305 }
30306
30307 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
30308    whose ONE_VAR element is VAR, and other elements are zero.  Return true
30309    if successful.  */
30310
30311 static bool
30312 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
30313                                      rtx target, rtx var, int one_var)
30314 {
30315   enum machine_mode vsimode;
30316   rtx new_target;
30317   rtx x, tmp;
30318   bool use_vector_set = false;
30319
30320   switch (mode)
30321     {
30322     case V2DImode:
30323       /* For SSE4.1, we normally use vector set.  But if the second
30324          element is zero and inter-unit moves are OK, we use movq
30325          instead.  */
30326       use_vector_set = (TARGET_64BIT
30327                         && TARGET_SSE4_1
30328                         && !(TARGET_INTER_UNIT_MOVES
30329                              && one_var == 0));
30330       break;
30331     case V16QImode:
30332     case V4SImode:
30333     case V4SFmode:
30334       use_vector_set = TARGET_SSE4_1;
30335       break;
30336     case V8HImode:
30337       use_vector_set = TARGET_SSE2;
30338       break;
30339     case V4HImode:
30340       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
30341       break;
30342     case V32QImode:
30343     case V16HImode:
30344     case V8SImode:
30345     case V8SFmode:
30346     case V4DFmode:
30347       use_vector_set = TARGET_AVX;
30348       break;
30349     case V4DImode:
30350       /* Use ix86_expand_vector_set in 64bit mode only.  */
30351       use_vector_set = TARGET_AVX && TARGET_64BIT;
30352       break;
30353     default:
30354       break;
30355     }
30356
30357   if (use_vector_set)
30358     {
30359       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
30360       var = force_reg (GET_MODE_INNER (mode), var);
30361       ix86_expand_vector_set (mmx_ok, target, var, one_var);
30362       return true;
30363     }
30364
30365   switch (mode)
30366     {
30367     case V2SFmode:
30368     case V2SImode:
30369       if (!mmx_ok)
30370         return false;
30371       /* FALLTHRU */
30372
30373     case V2DFmode:
30374     case V2DImode:
30375       if (one_var != 0)
30376         return false;
30377       var = force_reg (GET_MODE_INNER (mode), var);
30378       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
30379       emit_insn (gen_rtx_SET (VOIDmode, target, x));
30380       return true;
30381
30382     case V4SFmode:
30383     case V4SImode:
30384       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
30385         new_target = gen_reg_rtx (mode);
30386       else
30387         new_target = target;
30388       var = force_reg (GET_MODE_INNER (mode), var);
30389       x = gen_rtx_VEC_DUPLICATE (mode, var);
30390       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
30391       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
30392       if (one_var != 0)
30393         {
30394           /* We need to shuffle the value to the correct position, so
30395              create a new pseudo to store the intermediate result.  */
30396
30397           /* With SSE2, we can use the integer shuffle insns.  */
30398           if (mode != V4SFmode && TARGET_SSE2)
30399             {
30400               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
30401                                             const1_rtx,
30402                                             GEN_INT (one_var == 1 ? 0 : 1),
30403                                             GEN_INT (one_var == 2 ? 0 : 1),
30404                                             GEN_INT (one_var == 3 ? 0 : 1)));
30405               if (target != new_target)
30406                 emit_move_insn (target, new_target);
30407               return true;
30408             }
30409
30410           /* Otherwise convert the intermediate result to V4SFmode and
30411              use the SSE1 shuffle instructions.  */
30412           if (mode != V4SFmode)
30413             {
30414               tmp = gen_reg_rtx (V4SFmode);
30415               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
30416             }
30417           else
30418             tmp = new_target;
30419
30420           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
30421                                        const1_rtx,
30422                                        GEN_INT (one_var == 1 ? 0 : 1),
30423                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
30424                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
30425
30426           if (mode != V4SFmode)
30427             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
30428           else if (tmp != target)
30429             emit_move_insn (target, tmp);
30430         }
30431       else if (target != new_target)
30432         emit_move_insn (target, new_target);
30433       return true;
30434
30435     case V8HImode:
30436     case V16QImode:
30437       vsimode = V4SImode;
30438       goto widen;
30439     case V4HImode:
30440     case V8QImode:
30441       if (!mmx_ok)
30442         return false;
30443       vsimode = V2SImode;
30444       goto widen;
30445     widen:
30446       if (one_var != 0)
30447         return false;
30448
30449       /* Zero extend the variable element to SImode and recurse.  */
30450       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
30451
30452       x = gen_reg_rtx (vsimode);
30453       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
30454                                                 var, one_var))
30455         gcc_unreachable ();
30456
30457       emit_move_insn (target, gen_lowpart (mode, x));
30458       return true;
30459
30460     default:
30461       return false;
30462     }
30463 }
30464
30465 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
30466    consisting of the values in VALS.  It is known that all elements
30467    except ONE_VAR are constants.  Return true if successful.  */
30468
30469 static bool
30470 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
30471                                  rtx target, rtx vals, int one_var)
30472 {
30473   rtx var = XVECEXP (vals, 0, one_var);
30474   enum machine_mode wmode;
30475   rtx const_vec, x;
30476
30477   const_vec = copy_rtx (vals);
30478   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
30479   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
30480
30481   switch (mode)
30482     {
30483     case V2DFmode:
30484     case V2DImode:
30485     case V2SFmode:
30486     case V2SImode:
30487       /* For the two element vectors, it's just as easy to use
30488          the general case.  */
30489       return false;
30490
30491     case V4DImode:
30492       /* Use ix86_expand_vector_set in 64bit mode only.  */
30493       if (!TARGET_64BIT)
30494         return false;
30495     case V4DFmode:
30496     case V8SFmode:
30497     case V8SImode:
30498     case V16HImode:
30499     case V32QImode:
30500     case V4SFmode:
30501     case V4SImode:
30502     case V8HImode:
30503     case V4HImode:
30504       break;
30505
30506     case V16QImode:
30507       if (TARGET_SSE4_1)
30508         break;
30509       wmode = V8HImode;
30510       goto widen;
30511     case V8QImode:
30512       wmode = V4HImode;
30513       goto widen;
30514     widen:
30515       /* There's no way to set one QImode entry easily.  Combine
30516          the variable value with its adjacent constant value, and
30517          promote to an HImode set.  */
30518       x = XVECEXP (vals, 0, one_var ^ 1);
30519       if (one_var & 1)
30520         {
30521           var = convert_modes (HImode, QImode, var, true);
30522           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
30523                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
30524           x = GEN_INT (INTVAL (x) & 0xff);
30525         }
30526       else
30527         {
30528           var = convert_modes (HImode, QImode, var, true);
30529           x = gen_int_mode (INTVAL (x) << 8, HImode);
30530         }
30531       if (x != const0_rtx)
30532         var = expand_simple_binop (HImode, IOR, var, x, var,
30533                                    1, OPTAB_LIB_WIDEN);
30534
30535       x = gen_reg_rtx (wmode);
30536       emit_move_insn (x, gen_lowpart (wmode, const_vec));
30537       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
30538
30539       emit_move_insn (target, gen_lowpart (mode, x));
30540       return true;
30541
30542     default:
30543       return false;
30544     }
30545
30546   emit_move_insn (target, const_vec);
30547   ix86_expand_vector_set (mmx_ok, target, var, one_var);
30548   return true;
30549 }
30550
30551 /* A subroutine of ix86_expand_vector_init_general.  Use vector
30552    concatenate to handle the most general case: all values variable,
30553    and none identical.  */
30554
30555 static void
30556 ix86_expand_vector_init_concat (enum machine_mode mode,
30557                                 rtx target, rtx *ops, int n)
30558 {
30559   enum machine_mode cmode, hmode = VOIDmode;
30560   rtx first[8], second[4];
30561   rtvec v;
30562   int i, j;
30563
30564   switch (n)
30565     {
30566     case 2:
30567       switch (mode)
30568         {
30569         case V8SImode:
30570           cmode = V4SImode;
30571           break;
30572         case V8SFmode:
30573           cmode = V4SFmode;
30574           break;
30575         case V4DImode:
30576           cmode = V2DImode;
30577           break;
30578         case V4DFmode:
30579           cmode = V2DFmode;
30580           break;
30581         case V4SImode:
30582           cmode = V2SImode;
30583           break;
30584         case V4SFmode:
30585           cmode = V2SFmode;
30586           break;
30587         case V2DImode:
30588           cmode = DImode;
30589           break;
30590         case V2SImode:
30591           cmode = SImode;
30592           break;
30593         case V2DFmode:
30594           cmode = DFmode;
30595           break;
30596         case V2SFmode:
30597           cmode = SFmode;
30598           break;
30599         default:
30600           gcc_unreachable ();
30601         }
30602
30603       if (!register_operand (ops[1], cmode))
30604         ops[1] = force_reg (cmode, ops[1]);
30605       if (!register_operand (ops[0], cmode))
30606         ops[0] = force_reg (cmode, ops[0]);
30607       emit_insn (gen_rtx_SET (VOIDmode, target,
30608                               gen_rtx_VEC_CONCAT (mode, ops[0],
30609                                                   ops[1])));
30610       break;
30611
30612     case 4:
30613       switch (mode)
30614         {
30615         case V4DImode:
30616           cmode = V2DImode;
30617           break;
30618         case V4DFmode:
30619           cmode = V2DFmode;
30620           break;
30621         case V4SImode:
30622           cmode = V2SImode;
30623           break;
30624         case V4SFmode:
30625           cmode = V2SFmode;
30626           break;
30627         default:
30628           gcc_unreachable ();
30629         }
30630       goto half;
30631
30632     case 8:
30633       switch (mode)
30634         {
30635         case V8SImode:
30636           cmode = V2SImode;
30637           hmode = V4SImode;
30638           break;
30639         case V8SFmode:
30640           cmode = V2SFmode;
30641           hmode = V4SFmode;
30642           break;
30643         default:
30644           gcc_unreachable ();
30645         }
30646       goto half;
30647
30648 half:
30649       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
30650       i = n - 1;
30651       j = (n >> 1) - 1;
30652       for (; i > 0; i -= 2, j--)
30653         {
30654           first[j] = gen_reg_rtx (cmode);
30655           v = gen_rtvec (2, ops[i - 1], ops[i]);
30656           ix86_expand_vector_init (false, first[j],
30657                                    gen_rtx_PARALLEL (cmode, v));
30658         }
30659
30660       n >>= 1;
30661       if (n > 2)
30662         {
30663           gcc_assert (hmode != VOIDmode);
30664           for (i = j = 0; i < n; i += 2, j++)
30665             {
30666               second[j] = gen_reg_rtx (hmode);
30667               ix86_expand_vector_init_concat (hmode, second [j],
30668                                               &first [i], 2);
30669             }
30670           n >>= 1;
30671           ix86_expand_vector_init_concat (mode, target, second, n);
30672         }
30673       else
30674         ix86_expand_vector_init_concat (mode, target, first, n);
30675       break;
30676
30677     default:
30678       gcc_unreachable ();
30679     }
30680 }
30681
30682 /* A subroutine of ix86_expand_vector_init_general.  Use vector
30683    interleave to handle the most general case: all values variable,
30684    and none identical.  */
30685
30686 static void
30687 ix86_expand_vector_init_interleave (enum machine_mode mode,
30688                                     rtx target, rtx *ops, int n)
30689 {
30690   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
30691   int i, j;
30692   rtx op0, op1;
30693   rtx (*gen_load_even) (rtx, rtx, rtx);
30694   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
30695   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
30696
30697   switch (mode)
30698     {
30699     case V8HImode:
30700       gen_load_even = gen_vec_setv8hi;
30701       gen_interleave_first_low = gen_vec_interleave_lowv4si;
30702       gen_interleave_second_low = gen_vec_interleave_lowv2di;
30703       inner_mode = HImode;
30704       first_imode = V4SImode;
30705       second_imode = V2DImode;
30706       third_imode = VOIDmode;
30707       break;
30708     case V16QImode:
30709       gen_load_even = gen_vec_setv16qi;
30710       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
30711       gen_interleave_second_low = gen_vec_interleave_lowv4si;
30712       inner_mode = QImode;
30713       first_imode = V8HImode;
30714       second_imode = V4SImode;
30715       third_imode = V2DImode;
30716       break;
30717     default:
30718       gcc_unreachable ();
30719     }
30720
30721   for (i = 0; i < n; i++)
30722     {
30723       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
30724       op0 = gen_reg_rtx (SImode);
30725       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
30726
30727       /* Insert the SImode value as low element of V4SImode vector. */
30728       op1 = gen_reg_rtx (V4SImode);
30729       op0 = gen_rtx_VEC_MERGE (V4SImode,
30730                                gen_rtx_VEC_DUPLICATE (V4SImode,
30731                                                       op0),
30732                                CONST0_RTX (V4SImode),
30733                                const1_rtx);
30734       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
30735
30736       /* Cast the V4SImode vector back to a vector in orignal mode.  */
30737       op0 = gen_reg_rtx (mode);
30738       emit_move_insn (op0, gen_lowpart (mode, op1));
30739
30740       /* Load even elements into the second positon.  */
30741       emit_insn (gen_load_even (op0,
30742                                 force_reg (inner_mode,
30743                                            ops [i + i + 1]),
30744                                 const1_rtx));
30745
30746       /* Cast vector to FIRST_IMODE vector.  */
30747       ops[i] = gen_reg_rtx (first_imode);
30748       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
30749     }
30750
30751   /* Interleave low FIRST_IMODE vectors.  */
30752   for (i = j = 0; i < n; i += 2, j++)
30753     {
30754       op0 = gen_reg_rtx (first_imode);
30755       emit_insn (gen_interleave_first_low (op0, ops[i], ops[i + 1]));
30756
30757       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
30758       ops[j] = gen_reg_rtx (second_imode);
30759       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
30760     }
30761
30762   /* Interleave low SECOND_IMODE vectors.  */
30763   switch (second_imode)
30764     {
30765     case V4SImode:
30766       for (i = j = 0; i < n / 2; i += 2, j++)
30767         {
30768           op0 = gen_reg_rtx (second_imode);
30769           emit_insn (gen_interleave_second_low (op0, ops[i],
30770                                                 ops[i + 1]));
30771
30772           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
30773              vector.  */
30774           ops[j] = gen_reg_rtx (third_imode);
30775           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
30776         }
30777       second_imode = V2DImode;
30778       gen_interleave_second_low = gen_vec_interleave_lowv2di;
30779       /* FALLTHRU */
30780
30781     case V2DImode:
30782       op0 = gen_reg_rtx (second_imode);
30783       emit_insn (gen_interleave_second_low (op0, ops[0],
30784                                             ops[1]));
30785
30786       /* Cast the SECOND_IMODE vector back to a vector on original
30787          mode.  */
30788       emit_insn (gen_rtx_SET (VOIDmode, target,
30789                               gen_lowpart (mode, op0)));
30790       break;
30791
30792     default:
30793       gcc_unreachable ();
30794     }
30795 }
30796
30797 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
30798    all values variable, and none identical.  */
30799
30800 static void
30801 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
30802                                  rtx target, rtx vals)
30803 {
30804   rtx ops[32], op0, op1;
30805   enum machine_mode half_mode = VOIDmode;
30806   int n, i;
30807
30808   switch (mode)
30809     {
30810     case V2SFmode:
30811     case V2SImode:
30812       if (!mmx_ok && !TARGET_SSE)
30813         break;
30814       /* FALLTHRU */
30815
30816     case V8SFmode:
30817     case V8SImode:
30818     case V4DFmode:
30819     case V4DImode:
30820     case V4SFmode:
30821     case V4SImode:
30822     case V2DFmode:
30823     case V2DImode:
30824       n = GET_MODE_NUNITS (mode);
30825       for (i = 0; i < n; i++)
30826         ops[i] = XVECEXP (vals, 0, i);
30827       ix86_expand_vector_init_concat (mode, target, ops, n);
30828       return;
30829
30830     case V32QImode:
30831       half_mode = V16QImode;
30832       goto half;
30833
30834     case V16HImode:
30835       half_mode = V8HImode;
30836       goto half;
30837
30838 half:
30839       n = GET_MODE_NUNITS (mode);
30840       for (i = 0; i < n; i++)
30841         ops[i] = XVECEXP (vals, 0, i);
30842       op0 = gen_reg_rtx (half_mode);
30843       op1 = gen_reg_rtx (half_mode);
30844       ix86_expand_vector_init_interleave (half_mode, op0, ops,
30845                                           n >> 2);
30846       ix86_expand_vector_init_interleave (half_mode, op1,
30847                                           &ops [n >> 1], n >> 2);
30848       emit_insn (gen_rtx_SET (VOIDmode, target,
30849                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
30850       return;
30851
30852     case V16QImode:
30853       if (!TARGET_SSE4_1)
30854         break;
30855       /* FALLTHRU */
30856
30857     case V8HImode:
30858       if (!TARGET_SSE2)
30859         break;
30860
30861       /* Don't use ix86_expand_vector_init_interleave if we can't
30862          move from GPR to SSE register directly.  */
30863       if (!TARGET_INTER_UNIT_MOVES)
30864         break;
30865
30866       n = GET_MODE_NUNITS (mode);
30867       for (i = 0; i < n; i++)
30868         ops[i] = XVECEXP (vals, 0, i);
30869       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
30870       return;
30871
30872     case V4HImode:
30873     case V8QImode:
30874       break;
30875
30876     default:
30877       gcc_unreachable ();
30878     }
30879
30880     {
30881       int i, j, n_elts, n_words, n_elt_per_word;
30882       enum machine_mode inner_mode;
30883       rtx words[4], shift;
30884
30885       inner_mode = GET_MODE_INNER (mode);
30886       n_elts = GET_MODE_NUNITS (mode);
30887       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
30888       n_elt_per_word = n_elts / n_words;
30889       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
30890
30891       for (i = 0; i < n_words; ++i)
30892         {
30893           rtx word = NULL_RTX;
30894
30895           for (j = 0; j < n_elt_per_word; ++j)
30896             {
30897               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
30898               elt = convert_modes (word_mode, inner_mode, elt, true);
30899
30900               if (j == 0)
30901                 word = elt;
30902               else
30903                 {
30904                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
30905                                               word, 1, OPTAB_LIB_WIDEN);
30906                   word = expand_simple_binop (word_mode, IOR, word, elt,
30907                                               word, 1, OPTAB_LIB_WIDEN);
30908                 }
30909             }
30910
30911           words[i] = word;
30912         }
30913
30914       if (n_words == 1)
30915         emit_move_insn (target, gen_lowpart (mode, words[0]));
30916       else if (n_words == 2)
30917         {
30918           rtx tmp = gen_reg_rtx (mode);
30919           emit_clobber (tmp);
30920           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
30921           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
30922           emit_move_insn (target, tmp);
30923         }
30924       else if (n_words == 4)
30925         {
30926           rtx tmp = gen_reg_rtx (V4SImode);
30927           gcc_assert (word_mode == SImode);
30928           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
30929           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
30930           emit_move_insn (target, gen_lowpart (mode, tmp));
30931         }
30932       else
30933         gcc_unreachable ();
30934     }
30935 }
30936
30937 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
30938    instructions unless MMX_OK is true.  */
30939
30940 void
30941 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
30942 {
30943   enum machine_mode mode = GET_MODE (target);
30944   enum machine_mode inner_mode = GET_MODE_INNER (mode);
30945   int n_elts = GET_MODE_NUNITS (mode);
30946   int n_var = 0, one_var = -1;
30947   bool all_same = true, all_const_zero = true;
30948   int i;
30949   rtx x;
30950
30951   for (i = 0; i < n_elts; ++i)
30952     {
30953       x = XVECEXP (vals, 0, i);
30954       if (!(CONST_INT_P (x)
30955             || GET_CODE (x) == CONST_DOUBLE
30956             || GET_CODE (x) == CONST_FIXED))
30957         n_var++, one_var = i;
30958       else if (x != CONST0_RTX (inner_mode))
30959         all_const_zero = false;
30960       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
30961         all_same = false;
30962     }
30963
30964   /* Constants are best loaded from the constant pool.  */
30965   if (n_var == 0)
30966     {
30967       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
30968       return;
30969     }
30970
30971   /* If all values are identical, broadcast the value.  */
30972   if (all_same
30973       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
30974                                             XVECEXP (vals, 0, 0)))
30975     return;
30976
30977   /* Values where only one field is non-constant are best loaded from
30978      the pool and overwritten via move later.  */
30979   if (n_var == 1)
30980     {
30981       if (all_const_zero
30982           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
30983                                                   XVECEXP (vals, 0, one_var),
30984                                                   one_var))
30985         return;
30986
30987       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
30988         return;
30989     }
30990
30991   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
30992 }
30993
30994 void
30995 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
30996 {
30997   enum machine_mode mode = GET_MODE (target);
30998   enum machine_mode inner_mode = GET_MODE_INNER (mode);
30999   enum machine_mode half_mode;
31000   bool use_vec_merge = false;
31001   rtx tmp;
31002   static rtx (*gen_extract[6][2]) (rtx, rtx)
31003     = {
31004         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
31005         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
31006         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
31007         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
31008         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
31009         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
31010       };
31011   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
31012     = {
31013         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
31014         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
31015         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
31016         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
31017         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
31018         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
31019       };
31020   int i, j, n;
31021
31022   switch (mode)
31023     {
31024     case V2SFmode:
31025     case V2SImode:
31026       if (mmx_ok)
31027         {
31028           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
31029           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
31030           if (elt == 0)
31031             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
31032           else
31033             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
31034           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
31035           return;
31036         }
31037       break;
31038
31039     case V2DImode:
31040       use_vec_merge = TARGET_SSE4_1;
31041       if (use_vec_merge)
31042         break;
31043
31044     case V2DFmode:
31045       {
31046         rtx op0, op1;
31047
31048         /* For the two element vectors, we implement a VEC_CONCAT with
31049            the extraction of the other element.  */
31050
31051         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
31052         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
31053
31054         if (elt == 0)
31055           op0 = val, op1 = tmp;
31056         else
31057           op0 = tmp, op1 = val;
31058
31059         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
31060         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
31061       }
31062       return;
31063
31064     case V4SFmode:
31065       use_vec_merge = TARGET_SSE4_1;
31066       if (use_vec_merge)
31067         break;
31068
31069       switch (elt)
31070         {
31071         case 0:
31072           use_vec_merge = true;
31073           break;
31074
31075         case 1:
31076           /* tmp = target = A B C D */
31077           tmp = copy_to_reg (target);
31078           /* target = A A B B */
31079           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
31080           /* target = X A B B */
31081           ix86_expand_vector_set (false, target, val, 0);
31082           /* target = A X C D  */
31083           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
31084                                           const1_rtx, const0_rtx,
31085                                           GEN_INT (2+4), GEN_INT (3+4)));
31086           return;
31087
31088         case 2:
31089           /* tmp = target = A B C D */
31090           tmp = copy_to_reg (target);
31091           /* tmp = X B C D */
31092           ix86_expand_vector_set (false, tmp, val, 0);
31093           /* target = A B X D */
31094           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
31095                                           const0_rtx, const1_rtx,
31096                                           GEN_INT (0+4), GEN_INT (3+4)));
31097           return;
31098
31099         case 3:
31100           /* tmp = target = A B C D */
31101           tmp = copy_to_reg (target);
31102           /* tmp = X B C D */
31103           ix86_expand_vector_set (false, tmp, val, 0);
31104           /* target = A B X D */
31105           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
31106                                           const0_rtx, const1_rtx,
31107                                           GEN_INT (2+4), GEN_INT (0+4)));
31108           return;
31109
31110         default:
31111           gcc_unreachable ();
31112         }
31113       break;
31114
31115     case V4SImode:
31116       use_vec_merge = TARGET_SSE4_1;
31117       if (use_vec_merge)
31118         break;
31119
31120       /* Element 0 handled by vec_merge below.  */
31121       if (elt == 0)
31122         {
31123           use_vec_merge = true;
31124           break;
31125         }
31126
31127       if (TARGET_SSE2)
31128         {
31129           /* With SSE2, use integer shuffles to swap element 0 and ELT,
31130              store into element 0, then shuffle them back.  */
31131
31132           rtx order[4];
31133
31134           order[0] = GEN_INT (elt);
31135           order[1] = const1_rtx;
31136           order[2] = const2_rtx;
31137           order[3] = GEN_INT (3);
31138           order[elt] = const0_rtx;
31139
31140           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
31141                                         order[1], order[2], order[3]));
31142
31143           ix86_expand_vector_set (false, target, val, 0);
31144
31145           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
31146                                         order[1], order[2], order[3]));
31147         }
31148       else
31149         {
31150           /* For SSE1, we have to reuse the V4SF code.  */
31151           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
31152                                   gen_lowpart (SFmode, val), elt);
31153         }
31154       return;
31155
31156     case V8HImode:
31157       use_vec_merge = TARGET_SSE2;
31158       break;
31159     case V4HImode:
31160       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
31161       break;
31162
31163     case V16QImode:
31164       use_vec_merge = TARGET_SSE4_1;
31165       break;
31166
31167     case V8QImode:
31168       break;
31169
31170     case V32QImode:
31171       half_mode = V16QImode;
31172       j = 0;
31173       n = 16;
31174       goto half;
31175
31176     case V16HImode:
31177       half_mode = V8HImode;
31178       j = 1;
31179       n = 8;
31180       goto half;
31181
31182     case V8SImode:
31183       half_mode = V4SImode;
31184       j = 2;
31185       n = 4;
31186       goto half;
31187
31188     case V4DImode:
31189       half_mode = V2DImode;
31190       j = 3;
31191       n = 2;
31192       goto half;
31193
31194     case V8SFmode:
31195       half_mode = V4SFmode;
31196       j = 4;
31197       n = 4;
31198       goto half;
31199
31200     case V4DFmode:
31201       half_mode = V2DFmode;
31202       j = 5;
31203       n = 2;
31204       goto half;
31205
31206 half:
31207       /* Compute offset.  */
31208       i = elt / n;
31209       elt %= n;
31210
31211       gcc_assert (i <= 1);
31212
31213       /* Extract the half.  */
31214       tmp = gen_reg_rtx (half_mode);
31215       emit_insn (gen_extract[j][i] (tmp, target));
31216
31217       /* Put val in tmp at elt.  */
31218       ix86_expand_vector_set (false, tmp, val, elt);
31219
31220       /* Put it back.  */
31221       emit_insn (gen_insert[j][i] (target, target, tmp));
31222       return;
31223
31224     default:
31225       break;
31226     }
31227
31228   if (use_vec_merge)
31229     {
31230       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
31231       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
31232       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
31233     }
31234   else
31235     {
31236       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
31237
31238       emit_move_insn (mem, target);
31239
31240       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
31241       emit_move_insn (tmp, val);
31242
31243       emit_move_insn (target, mem);
31244     }
31245 }
31246
31247 void
31248 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
31249 {
31250   enum machine_mode mode = GET_MODE (vec);
31251   enum machine_mode inner_mode = GET_MODE_INNER (mode);
31252   bool use_vec_extr = false;
31253   rtx tmp;
31254
31255   switch (mode)
31256     {
31257     case V2SImode:
31258     case V2SFmode:
31259       if (!mmx_ok)
31260         break;
31261       /* FALLTHRU */
31262
31263     case V2DFmode:
31264     case V2DImode:
31265       use_vec_extr = true;
31266       break;
31267
31268     case V4SFmode:
31269       use_vec_extr = TARGET_SSE4_1;
31270       if (use_vec_extr)
31271         break;
31272
31273       switch (elt)
31274         {
31275         case 0:
31276           tmp = vec;
31277           break;
31278
31279         case 1:
31280         case 3:
31281           tmp = gen_reg_rtx (mode);
31282           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
31283                                        GEN_INT (elt), GEN_INT (elt),
31284                                        GEN_INT (elt+4), GEN_INT (elt+4)));
31285           break;
31286
31287         case 2:
31288           tmp = gen_reg_rtx (mode);
31289           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
31290           break;
31291
31292         default:
31293           gcc_unreachable ();
31294         }
31295       vec = tmp;
31296       use_vec_extr = true;
31297       elt = 0;
31298       break;
31299
31300     case V4SImode:
31301       use_vec_extr = TARGET_SSE4_1;
31302       if (use_vec_extr)
31303         break;
31304
31305       if (TARGET_SSE2)
31306         {
31307           switch (elt)
31308             {
31309             case 0:
31310               tmp = vec;
31311               break;
31312
31313             case 1:
31314             case 3:
31315               tmp = gen_reg_rtx (mode);
31316               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
31317                                             GEN_INT (elt), GEN_INT (elt),
31318                                             GEN_INT (elt), GEN_INT (elt)));
31319               break;
31320
31321             case 2:
31322               tmp = gen_reg_rtx (mode);
31323               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
31324               break;
31325
31326             default:
31327               gcc_unreachable ();
31328             }
31329           vec = tmp;
31330           use_vec_extr = true;
31331           elt = 0;
31332         }
31333       else
31334         {
31335           /* For SSE1, we have to reuse the V4SF code.  */
31336           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
31337                                       gen_lowpart (V4SFmode, vec), elt);
31338           return;
31339         }
31340       break;
31341
31342     case V8HImode:
31343       use_vec_extr = TARGET_SSE2;
31344       break;
31345     case V4HImode:
31346       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
31347       break;
31348
31349     case V16QImode:
31350       use_vec_extr = TARGET_SSE4_1;
31351       break;
31352
31353     case V8QImode:
31354       /* ??? Could extract the appropriate HImode element and shift.  */
31355     default:
31356       break;
31357     }
31358
31359   if (use_vec_extr)
31360     {
31361       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
31362       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
31363
31364       /* Let the rtl optimizers know about the zero extension performed.  */
31365       if (inner_mode == QImode || inner_mode == HImode)
31366         {
31367           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
31368           target = gen_lowpart (SImode, target);
31369         }
31370
31371       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
31372     }
31373   else
31374     {
31375       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
31376
31377       emit_move_insn (mem, vec);
31378
31379       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
31380       emit_move_insn (target, tmp);
31381     }
31382 }
31383
31384 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
31385    pattern to reduce; DEST is the destination; IN is the input vector.  */
31386
31387 void
31388 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
31389 {
31390   rtx tmp1, tmp2, tmp3;
31391
31392   tmp1 = gen_reg_rtx (V4SFmode);
31393   tmp2 = gen_reg_rtx (V4SFmode);
31394   tmp3 = gen_reg_rtx (V4SFmode);
31395
31396   emit_insn (gen_sse_movhlps (tmp1, in, in));
31397   emit_insn (fn (tmp2, tmp1, in));
31398
31399   emit_insn (gen_sse_shufps_v4sf (tmp3, tmp2, tmp2,
31400                                   const1_rtx, const1_rtx,
31401                                   GEN_INT (1+4), GEN_INT (1+4)));
31402   emit_insn (fn (dest, tmp2, tmp3));
31403 }
31404 \f
31405 /* Target hook for scalar_mode_supported_p.  */
31406 static bool
31407 ix86_scalar_mode_supported_p (enum machine_mode mode)
31408 {
31409   if (DECIMAL_FLOAT_MODE_P (mode))
31410     return default_decimal_float_supported_p ();
31411   else if (mode == TFmode)
31412     return true;
31413   else
31414     return default_scalar_mode_supported_p (mode);
31415 }
31416
31417 /* Implements target hook vector_mode_supported_p.  */
31418 static bool
31419 ix86_vector_mode_supported_p (enum machine_mode mode)
31420 {
31421   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
31422     return true;
31423   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
31424     return true;
31425   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
31426     return true;
31427   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
31428     return true;
31429   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
31430     return true;
31431   return false;
31432 }
31433
31434 /* Target hook for c_mode_for_suffix.  */
31435 static enum machine_mode
31436 ix86_c_mode_for_suffix (char suffix)
31437 {
31438   if (suffix == 'q')
31439     return TFmode;
31440   if (suffix == 'w')
31441     return XFmode;
31442
31443   return VOIDmode;
31444 }
31445
31446 /* Worker function for TARGET_MD_ASM_CLOBBERS.
31447
31448    We do this in the new i386 backend to maintain source compatibility
31449    with the old cc0-based compiler.  */
31450
31451 static tree
31452 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
31453                       tree inputs ATTRIBUTE_UNUSED,
31454                       tree clobbers)
31455 {
31456   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
31457                         clobbers);
31458   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
31459                         clobbers);
31460   return clobbers;
31461 }
31462
31463 /* Implements target vector targetm.asm.encode_section_info.  This
31464    is not used by netware.  */
31465
31466 static void ATTRIBUTE_UNUSED
31467 ix86_encode_section_info (tree decl, rtx rtl, int first)
31468 {
31469   default_encode_section_info (decl, rtl, first);
31470
31471   if (TREE_CODE (decl) == VAR_DECL
31472       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
31473       && ix86_in_large_data_p (decl))
31474     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
31475 }
31476
31477 /* Worker function for REVERSE_CONDITION.  */
31478
31479 enum rtx_code
31480 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
31481 {
31482   return (mode != CCFPmode && mode != CCFPUmode
31483           ? reverse_condition (code)
31484           : reverse_condition_maybe_unordered (code));
31485 }
31486
31487 /* Output code to perform an x87 FP register move, from OPERANDS[1]
31488    to OPERANDS[0].  */
31489
31490 const char *
31491 output_387_reg_move (rtx insn, rtx *operands)
31492 {
31493   if (REG_P (operands[0]))
31494     {
31495       if (REG_P (operands[1])
31496           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
31497         {
31498           if (REGNO (operands[0]) == FIRST_STACK_REG)
31499             return output_387_ffreep (operands, 0);
31500           return "fstp\t%y0";
31501         }
31502       if (STACK_TOP_P (operands[0]))
31503         return "fld%Z1\t%y1";
31504       return "fst\t%y0";
31505     }
31506   else if (MEM_P (operands[0]))
31507     {
31508       gcc_assert (REG_P (operands[1]));
31509       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
31510         return "fstp%Z0\t%y0";
31511       else
31512         {
31513           /* There is no non-popping store to memory for XFmode.
31514              So if we need one, follow the store with a load.  */
31515           if (GET_MODE (operands[0]) == XFmode)
31516             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
31517           else
31518             return "fst%Z0\t%y0";
31519         }
31520     }
31521   else
31522     gcc_unreachable();
31523 }
31524
31525 /* Output code to perform a conditional jump to LABEL, if C2 flag in
31526    FP status register is set.  */
31527
31528 void
31529 ix86_emit_fp_unordered_jump (rtx label)
31530 {
31531   rtx reg = gen_reg_rtx (HImode);
31532   rtx temp;
31533
31534   emit_insn (gen_x86_fnstsw_1 (reg));
31535
31536   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
31537     {
31538       emit_insn (gen_x86_sahf_1 (reg));
31539
31540       temp = gen_rtx_REG (CCmode, FLAGS_REG);
31541       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
31542     }
31543   else
31544     {
31545       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
31546
31547       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
31548       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
31549     }
31550
31551   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
31552                               gen_rtx_LABEL_REF (VOIDmode, label),
31553                               pc_rtx);
31554   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
31555
31556   emit_jump_insn (temp);
31557   predict_jump (REG_BR_PROB_BASE * 10 / 100);
31558 }
31559
31560 /* Output code to perform a log1p XFmode calculation.  */
31561
31562 void ix86_emit_i387_log1p (rtx op0, rtx op1)
31563 {
31564   rtx label1 = gen_label_rtx ();
31565   rtx label2 = gen_label_rtx ();
31566
31567   rtx tmp = gen_reg_rtx (XFmode);
31568   rtx tmp2 = gen_reg_rtx (XFmode);
31569   rtx test;
31570
31571   emit_insn (gen_absxf2 (tmp, op1));
31572   test = gen_rtx_GE (VOIDmode, tmp,
31573     CONST_DOUBLE_FROM_REAL_VALUE (
31574        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
31575        XFmode));
31576   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
31577
31578   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
31579   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
31580   emit_jump (label2);
31581
31582   emit_label (label1);
31583   emit_move_insn (tmp, CONST1_RTX (XFmode));
31584   emit_insn (gen_addxf3 (tmp, op1, tmp));
31585   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
31586   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
31587
31588   emit_label (label2);
31589 }
31590
31591 /* Output code to perform a Newton-Rhapson approximation of a single precision
31592    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
31593
31594 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
31595 {
31596   rtx x0, x1, e0, e1, two;
31597
31598   x0 = gen_reg_rtx (mode);
31599   e0 = gen_reg_rtx (mode);
31600   e1 = gen_reg_rtx (mode);
31601   x1 = gen_reg_rtx (mode);
31602
31603   two = CONST_DOUBLE_FROM_REAL_VALUE (dconst2, SFmode);
31604
31605   if (VECTOR_MODE_P (mode))
31606     two = ix86_build_const_vector (mode, true, two);
31607
31608   two = force_reg (mode, two);
31609
31610   /* a / b = a * rcp(b) * (2.0 - b * rcp(b)) */
31611
31612   /* x0 = rcp(b) estimate */
31613   emit_insn (gen_rtx_SET (VOIDmode, x0,
31614                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
31615                                           UNSPEC_RCP)));
31616   /* e0 = x0 * a */
31617   emit_insn (gen_rtx_SET (VOIDmode, e0,
31618                           gen_rtx_MULT (mode, x0, a)));
31619   /* e1 = x0 * b */
31620   emit_insn (gen_rtx_SET (VOIDmode, e1,
31621                           gen_rtx_MULT (mode, x0, b)));
31622   /* x1 = 2. - e1 */
31623   emit_insn (gen_rtx_SET (VOIDmode, x1,
31624                           gen_rtx_MINUS (mode, two, e1)));
31625   /* res = e0 * x1 */
31626   emit_insn (gen_rtx_SET (VOIDmode, res,
31627                           gen_rtx_MULT (mode, e0, x1)));
31628 }
31629
31630 /* Output code to perform a Newton-Rhapson approximation of a
31631    single precision floating point [reciprocal] square root.  */
31632
31633 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
31634                          bool recip)
31635 {
31636   rtx x0, e0, e1, e2, e3, mthree, mhalf;
31637   REAL_VALUE_TYPE r;
31638
31639   x0 = gen_reg_rtx (mode);
31640   e0 = gen_reg_rtx (mode);
31641   e1 = gen_reg_rtx (mode);
31642   e2 = gen_reg_rtx (mode);
31643   e3 = gen_reg_rtx (mode);
31644
31645   real_from_integer (&r, VOIDmode, -3, -1, 0);
31646   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
31647
31648   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
31649   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
31650
31651   if (VECTOR_MODE_P (mode))
31652     {
31653       mthree = ix86_build_const_vector (mode, true, mthree);
31654       mhalf = ix86_build_const_vector (mode, true, mhalf);
31655     }
31656
31657   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
31658      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
31659
31660   /* x0 = rsqrt(a) estimate */
31661   emit_insn (gen_rtx_SET (VOIDmode, x0,
31662                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
31663                                           UNSPEC_RSQRT)));
31664
31665   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
31666   if (!recip)
31667     {
31668       rtx zero, mask;
31669
31670       zero = gen_reg_rtx (mode);
31671       mask = gen_reg_rtx (mode);
31672
31673       zero = force_reg (mode, CONST0_RTX(mode));
31674       emit_insn (gen_rtx_SET (VOIDmode, mask,
31675                               gen_rtx_NE (mode, zero, a)));
31676
31677       emit_insn (gen_rtx_SET (VOIDmode, x0,
31678                               gen_rtx_AND (mode, x0, mask)));
31679     }
31680
31681   /* e0 = x0 * a */
31682   emit_insn (gen_rtx_SET (VOIDmode, e0,
31683                           gen_rtx_MULT (mode, x0, a)));
31684   /* e1 = e0 * x0 */
31685   emit_insn (gen_rtx_SET (VOIDmode, e1,
31686                           gen_rtx_MULT (mode, e0, x0)));
31687
31688   /* e2 = e1 - 3. */
31689   mthree = force_reg (mode, mthree);
31690   emit_insn (gen_rtx_SET (VOIDmode, e2,
31691                           gen_rtx_PLUS (mode, e1, mthree)));
31692
31693   mhalf = force_reg (mode, mhalf);
31694   if (recip)
31695     /* e3 = -.5 * x0 */
31696     emit_insn (gen_rtx_SET (VOIDmode, e3,
31697                             gen_rtx_MULT (mode, x0, mhalf)));
31698   else
31699     /* e3 = -.5 * e0 */
31700     emit_insn (gen_rtx_SET (VOIDmode, e3,
31701                             gen_rtx_MULT (mode, e0, mhalf)));
31702   /* ret = e2 * e3 */
31703   emit_insn (gen_rtx_SET (VOIDmode, res,
31704                           gen_rtx_MULT (mode, e2, e3)));
31705 }
31706
31707 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
31708
31709 static void ATTRIBUTE_UNUSED
31710 i386_solaris_elf_named_section (const char *name, unsigned int flags,
31711                                 tree decl)
31712 {
31713   /* With Binutils 2.15, the "@unwind" marker must be specified on
31714      every occurrence of the ".eh_frame" section, not just the first
31715      one.  */
31716   if (TARGET_64BIT
31717       && strcmp (name, ".eh_frame") == 0)
31718     {
31719       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
31720                flags & SECTION_WRITE ? "aw" : "a");
31721       return;
31722     }
31723   default_elf_asm_named_section (name, flags, decl);
31724 }
31725
31726 /* Return the mangling of TYPE if it is an extended fundamental type.  */
31727
31728 static const char *
31729 ix86_mangle_type (const_tree type)
31730 {
31731   type = TYPE_MAIN_VARIANT (type);
31732
31733   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
31734       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
31735     return NULL;
31736
31737   switch (TYPE_MODE (type))
31738     {
31739     case TFmode:
31740       /* __float128 is "g".  */
31741       return "g";
31742     case XFmode:
31743       /* "long double" or __float80 is "e".  */
31744       return "e";
31745     default:
31746       return NULL;
31747     }
31748 }
31749
31750 /* For 32-bit code we can save PIC register setup by using
31751    __stack_chk_fail_local hidden function instead of calling
31752    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
31753    register, so it is better to call __stack_chk_fail directly.  */
31754
31755 static tree
31756 ix86_stack_protect_fail (void)
31757 {
31758   return TARGET_64BIT
31759          ? default_external_stack_protect_fail ()
31760          : default_hidden_stack_protect_fail ();
31761 }
31762
31763 /* Select a format to encode pointers in exception handling data.  CODE
31764    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
31765    true if the symbol may be affected by dynamic relocations.
31766
31767    ??? All x86 object file formats are capable of representing this.
31768    After all, the relocation needed is the same as for the call insn.
31769    Whether or not a particular assembler allows us to enter such, I
31770    guess we'll have to see.  */
31771 int
31772 asm_preferred_eh_data_format (int code, int global)
31773 {
31774   if (flag_pic)
31775     {
31776       int type = DW_EH_PE_sdata8;
31777       if (!TARGET_64BIT
31778           || ix86_cmodel == CM_SMALL_PIC
31779           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
31780         type = DW_EH_PE_sdata4;
31781       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
31782     }
31783   if (ix86_cmodel == CM_SMALL
31784       || (ix86_cmodel == CM_MEDIUM && code))
31785     return DW_EH_PE_udata4;
31786   return DW_EH_PE_absptr;
31787 }
31788 \f
31789 /* Expand copysign from SIGN to the positive value ABS_VALUE
31790    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
31791    the sign-bit.  */
31792 static void
31793 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
31794 {
31795   enum machine_mode mode = GET_MODE (sign);
31796   rtx sgn = gen_reg_rtx (mode);
31797   if (mask == NULL_RTX)
31798     {
31799       enum machine_mode vmode;
31800
31801       if (mode == SFmode)
31802         vmode = V4SFmode;
31803       else if (mode == DFmode)
31804         vmode = V2DFmode;
31805       else
31806         vmode = mode;
31807
31808       mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), false);
31809       if (!VECTOR_MODE_P (mode))
31810         {
31811           /* We need to generate a scalar mode mask in this case.  */
31812           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
31813           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
31814           mask = gen_reg_rtx (mode);
31815           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
31816         }
31817     }
31818   else
31819     mask = gen_rtx_NOT (mode, mask);
31820   emit_insn (gen_rtx_SET (VOIDmode, sgn,
31821                           gen_rtx_AND (mode, mask, sign)));
31822   emit_insn (gen_rtx_SET (VOIDmode, result,
31823                           gen_rtx_IOR (mode, abs_value, sgn)));
31824 }
31825
31826 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
31827    mask for masking out the sign-bit is stored in *SMASK, if that is
31828    non-null.  */
31829 static rtx
31830 ix86_expand_sse_fabs (rtx op0, rtx *smask)
31831 {
31832   enum machine_mode vmode, mode = GET_MODE (op0);
31833   rtx xa, mask;
31834
31835   xa = gen_reg_rtx (mode);
31836   if (mode == SFmode)
31837     vmode = V4SFmode;
31838   else if (mode == DFmode)
31839     vmode = V2DFmode;
31840   else
31841     vmode = mode;
31842   mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), true);
31843   if (!VECTOR_MODE_P (mode))
31844     {
31845       /* We need to generate a scalar mode mask in this case.  */
31846       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
31847       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
31848       mask = gen_reg_rtx (mode);
31849       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
31850     }
31851   emit_insn (gen_rtx_SET (VOIDmode, xa,
31852                           gen_rtx_AND (mode, op0, mask)));
31853
31854   if (smask)
31855     *smask = mask;
31856
31857   return xa;
31858 }
31859
31860 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
31861    swapping the operands if SWAP_OPERANDS is true.  The expanded
31862    code is a forward jump to a newly created label in case the
31863    comparison is true.  The generated label rtx is returned.  */
31864 static rtx
31865 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
31866                                   bool swap_operands)
31867 {
31868   rtx label, tmp;
31869
31870   if (swap_operands)
31871     {
31872       tmp = op0;
31873       op0 = op1;
31874       op1 = tmp;
31875     }
31876
31877   label = gen_label_rtx ();
31878   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
31879   emit_insn (gen_rtx_SET (VOIDmode, tmp,
31880                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
31881   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
31882   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
31883                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
31884   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
31885   JUMP_LABEL (tmp) = label;
31886
31887   return label;
31888 }
31889
31890 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
31891    using comparison code CODE.  Operands are swapped for the comparison if
31892    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
31893 static rtx
31894 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
31895                               bool swap_operands)
31896 {
31897   enum machine_mode mode = GET_MODE (op0);
31898   rtx mask = gen_reg_rtx (mode);
31899
31900   if (swap_operands)
31901     {
31902       rtx tmp = op0;
31903       op0 = op1;
31904       op1 = tmp;
31905     }
31906
31907   if (mode == DFmode)
31908     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
31909                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
31910   else
31911     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
31912                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
31913
31914   return mask;
31915 }
31916
31917 /* Generate and return a rtx of mode MODE for 2**n where n is the number
31918    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
31919 static rtx
31920 ix86_gen_TWO52 (enum machine_mode mode)
31921 {
31922   REAL_VALUE_TYPE TWO52r;
31923   rtx TWO52;
31924
31925   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
31926   TWO52 = const_double_from_real_value (TWO52r, mode);
31927   TWO52 = force_reg (mode, TWO52);
31928
31929   return TWO52;
31930 }
31931
31932 /* Expand SSE sequence for computing lround from OP1 storing
31933    into OP0.  */
31934 void
31935 ix86_expand_lround (rtx op0, rtx op1)
31936 {
31937   /* C code for the stuff we're doing below:
31938        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
31939        return (long)tmp;
31940    */
31941   enum machine_mode mode = GET_MODE (op1);
31942   const struct real_format *fmt;
31943   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
31944   rtx adj;
31945
31946   /* load nextafter (0.5, 0.0) */
31947   fmt = REAL_MODE_FORMAT (mode);
31948   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
31949   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
31950
31951   /* adj = copysign (0.5, op1) */
31952   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
31953   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
31954
31955   /* adj = op1 + adj */
31956   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
31957
31958   /* op0 = (imode)adj */
31959   expand_fix (op0, adj, 0);
31960 }
31961
31962 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
31963    into OPERAND0.  */
31964 void
31965 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
31966 {
31967   /* C code for the stuff we're doing below (for do_floor):
31968         xi = (long)op1;
31969         xi -= (double)xi > op1 ? 1 : 0;
31970         return xi;
31971    */
31972   enum machine_mode fmode = GET_MODE (op1);
31973   enum machine_mode imode = GET_MODE (op0);
31974   rtx ireg, freg, label, tmp;
31975
31976   /* reg = (long)op1 */
31977   ireg = gen_reg_rtx (imode);
31978   expand_fix (ireg, op1, 0);
31979
31980   /* freg = (double)reg */
31981   freg = gen_reg_rtx (fmode);
31982   expand_float (freg, ireg, 0);
31983
31984   /* ireg = (freg > op1) ? ireg - 1 : ireg */
31985   label = ix86_expand_sse_compare_and_jump (UNLE,
31986                                             freg, op1, !do_floor);
31987   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
31988                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
31989   emit_move_insn (ireg, tmp);
31990
31991   emit_label (label);
31992   LABEL_NUSES (label) = 1;
31993
31994   emit_move_insn (op0, ireg);
31995 }
31996
31997 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
31998    result in OPERAND0.  */
31999 void
32000 ix86_expand_rint (rtx operand0, rtx operand1)
32001 {
32002   /* C code for the stuff we're doing below:
32003         xa = fabs (operand1);
32004         if (!isless (xa, 2**52))
32005           return operand1;
32006         xa = xa + 2**52 - 2**52;
32007         return copysign (xa, operand1);
32008    */
32009   enum machine_mode mode = GET_MODE (operand0);
32010   rtx res, xa, label, TWO52, mask;
32011
32012   res = gen_reg_rtx (mode);
32013   emit_move_insn (res, operand1);
32014
32015   /* xa = abs (operand1) */
32016   xa = ix86_expand_sse_fabs (res, &mask);
32017
32018   /* if (!isless (xa, TWO52)) goto label; */
32019   TWO52 = ix86_gen_TWO52 (mode);
32020   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32021
32022   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
32023   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
32024
32025   ix86_sse_copysign_to_positive (res, xa, res, mask);
32026
32027   emit_label (label);
32028   LABEL_NUSES (label) = 1;
32029
32030   emit_move_insn (operand0, res);
32031 }
32032
32033 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
32034    into OPERAND0.  */
32035 void
32036 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
32037 {
32038   /* C code for the stuff we expand below.
32039         double xa = fabs (x), x2;
32040         if (!isless (xa, TWO52))
32041           return x;
32042         xa = xa + TWO52 - TWO52;
32043         x2 = copysign (xa, x);
32044      Compensate.  Floor:
32045         if (x2 > x)
32046           x2 -= 1;
32047      Compensate.  Ceil:
32048         if (x2 < x)
32049           x2 -= -1;
32050         return x2;
32051    */
32052   enum machine_mode mode = GET_MODE (operand0);
32053   rtx xa, TWO52, tmp, label, one, res, mask;
32054
32055   TWO52 = ix86_gen_TWO52 (mode);
32056
32057   /* Temporary for holding the result, initialized to the input
32058      operand to ease control flow.  */
32059   res = gen_reg_rtx (mode);
32060   emit_move_insn (res, operand1);
32061
32062   /* xa = abs (operand1) */
32063   xa = ix86_expand_sse_fabs (res, &mask);
32064
32065   /* if (!isless (xa, TWO52)) goto label; */
32066   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32067
32068   /* xa = xa + TWO52 - TWO52; */
32069   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
32070   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
32071
32072   /* xa = copysign (xa, operand1) */
32073   ix86_sse_copysign_to_positive (xa, xa, res, mask);
32074
32075   /* generate 1.0 or -1.0 */
32076   one = force_reg (mode,
32077                    const_double_from_real_value (do_floor
32078                                                  ? dconst1 : dconstm1, mode));
32079
32080   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
32081   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
32082   emit_insn (gen_rtx_SET (VOIDmode, tmp,
32083                           gen_rtx_AND (mode, one, tmp)));
32084   /* We always need to subtract here to preserve signed zero.  */
32085   tmp = expand_simple_binop (mode, MINUS,
32086                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
32087   emit_move_insn (res, tmp);
32088
32089   emit_label (label);
32090   LABEL_NUSES (label) = 1;
32091
32092   emit_move_insn (operand0, res);
32093 }
32094
32095 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
32096    into OPERAND0.  */
32097 void
32098 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
32099 {
32100   /* C code for the stuff we expand below.
32101         double xa = fabs (x), x2;
32102         if (!isless (xa, TWO52))
32103           return x;
32104         x2 = (double)(long)x;
32105      Compensate.  Floor:
32106         if (x2 > x)
32107           x2 -= 1;
32108      Compensate.  Ceil:
32109         if (x2 < x)
32110           x2 += 1;
32111         if (HONOR_SIGNED_ZEROS (mode))
32112           return copysign (x2, x);
32113         return x2;
32114    */
32115   enum machine_mode mode = GET_MODE (operand0);
32116   rtx xa, xi, TWO52, tmp, label, one, res, mask;
32117
32118   TWO52 = ix86_gen_TWO52 (mode);
32119
32120   /* Temporary for holding the result, initialized to the input
32121      operand to ease control flow.  */
32122   res = gen_reg_rtx (mode);
32123   emit_move_insn (res, operand1);
32124
32125   /* xa = abs (operand1) */
32126   xa = ix86_expand_sse_fabs (res, &mask);
32127
32128   /* if (!isless (xa, TWO52)) goto label; */
32129   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32130
32131   /* xa = (double)(long)x */
32132   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
32133   expand_fix (xi, res, 0);
32134   expand_float (xa, xi, 0);
32135
32136   /* generate 1.0 */
32137   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
32138
32139   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
32140   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
32141   emit_insn (gen_rtx_SET (VOIDmode, tmp,
32142                           gen_rtx_AND (mode, one, tmp)));
32143   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
32144                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
32145   emit_move_insn (res, tmp);
32146
32147   if (HONOR_SIGNED_ZEROS (mode))
32148     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
32149
32150   emit_label (label);
32151   LABEL_NUSES (label) = 1;
32152
32153   emit_move_insn (operand0, res);
32154 }
32155
32156 /* Expand SSE sequence for computing round from OPERAND1 storing
32157    into OPERAND0.  Sequence that works without relying on DImode truncation
32158    via cvttsd2siq that is only available on 64bit targets.  */
32159 void
32160 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
32161 {
32162   /* C code for the stuff we expand below.
32163         double xa = fabs (x), xa2, x2;
32164         if (!isless (xa, TWO52))
32165           return x;
32166      Using the absolute value and copying back sign makes
32167      -0.0 -> -0.0 correct.
32168         xa2 = xa + TWO52 - TWO52;
32169      Compensate.
32170         dxa = xa2 - xa;
32171         if (dxa <= -0.5)
32172           xa2 += 1;
32173         else if (dxa > 0.5)
32174           xa2 -= 1;
32175         x2 = copysign (xa2, x);
32176         return x2;
32177    */
32178   enum machine_mode mode = GET_MODE (operand0);
32179   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
32180
32181   TWO52 = ix86_gen_TWO52 (mode);
32182
32183   /* Temporary for holding the result, initialized to the input
32184      operand to ease control flow.  */
32185   res = gen_reg_rtx (mode);
32186   emit_move_insn (res, operand1);
32187
32188   /* xa = abs (operand1) */
32189   xa = ix86_expand_sse_fabs (res, &mask);
32190
32191   /* if (!isless (xa, TWO52)) goto label; */
32192   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32193
32194   /* xa2 = xa + TWO52 - TWO52; */
32195   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
32196   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
32197
32198   /* dxa = xa2 - xa; */
32199   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
32200
32201   /* generate 0.5, 1.0 and -0.5 */
32202   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
32203   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
32204   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
32205                                0, OPTAB_DIRECT);
32206
32207   /* Compensate.  */
32208   tmp = gen_reg_rtx (mode);
32209   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
32210   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
32211   emit_insn (gen_rtx_SET (VOIDmode, tmp,
32212                           gen_rtx_AND (mode, one, tmp)));
32213   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
32214   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
32215   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
32216   emit_insn (gen_rtx_SET (VOIDmode, tmp,
32217                           gen_rtx_AND (mode, one, tmp)));
32218   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
32219
32220   /* res = copysign (xa2, operand1) */
32221   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
32222
32223   emit_label (label);
32224   LABEL_NUSES (label) = 1;
32225
32226   emit_move_insn (operand0, res);
32227 }
32228
32229 /* Expand SSE sequence for computing trunc from OPERAND1 storing
32230    into OPERAND0.  */
32231 void
32232 ix86_expand_trunc (rtx operand0, rtx operand1)
32233 {
32234   /* C code for SSE variant we expand below.
32235         double xa = fabs (x), x2;
32236         if (!isless (xa, TWO52))
32237           return x;
32238         x2 = (double)(long)x;
32239         if (HONOR_SIGNED_ZEROS (mode))
32240           return copysign (x2, x);
32241         return x2;
32242    */
32243   enum machine_mode mode = GET_MODE (operand0);
32244   rtx xa, xi, TWO52, label, res, mask;
32245
32246   TWO52 = ix86_gen_TWO52 (mode);
32247
32248   /* Temporary for holding the result, initialized to the input
32249      operand to ease control flow.  */
32250   res = gen_reg_rtx (mode);
32251   emit_move_insn (res, operand1);
32252
32253   /* xa = abs (operand1) */
32254   xa = ix86_expand_sse_fabs (res, &mask);
32255
32256   /* if (!isless (xa, TWO52)) goto label; */
32257   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32258
32259   /* x = (double)(long)x */
32260   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
32261   expand_fix (xi, res, 0);
32262   expand_float (res, xi, 0);
32263
32264   if (HONOR_SIGNED_ZEROS (mode))
32265     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
32266
32267   emit_label (label);
32268   LABEL_NUSES (label) = 1;
32269
32270   emit_move_insn (operand0, res);
32271 }
32272
32273 /* Expand SSE sequence for computing trunc from OPERAND1 storing
32274    into OPERAND0.  */
32275 void
32276 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
32277 {
32278   enum machine_mode mode = GET_MODE (operand0);
32279   rtx xa, mask, TWO52, label, one, res, smask, tmp;
32280
32281   /* C code for SSE variant we expand below.
32282         double xa = fabs (x), x2;
32283         if (!isless (xa, TWO52))
32284           return x;
32285         xa2 = xa + TWO52 - TWO52;
32286      Compensate:
32287         if (xa2 > xa)
32288           xa2 -= 1.0;
32289         x2 = copysign (xa2, x);
32290         return x2;
32291    */
32292
32293   TWO52 = ix86_gen_TWO52 (mode);
32294
32295   /* Temporary for holding the result, initialized to the input
32296      operand to ease control flow.  */
32297   res = gen_reg_rtx (mode);
32298   emit_move_insn (res, operand1);
32299
32300   /* xa = abs (operand1) */
32301   xa = ix86_expand_sse_fabs (res, &smask);
32302
32303   /* if (!isless (xa, TWO52)) goto label; */
32304   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32305
32306   /* res = xa + TWO52 - TWO52; */
32307   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
32308   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
32309   emit_move_insn (res, tmp);
32310
32311   /* generate 1.0 */
32312   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
32313
32314   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
32315   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
32316   emit_insn (gen_rtx_SET (VOIDmode, mask,
32317                           gen_rtx_AND (mode, mask, one)));
32318   tmp = expand_simple_binop (mode, MINUS,
32319                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
32320   emit_move_insn (res, tmp);
32321
32322   /* res = copysign (res, operand1) */
32323   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
32324
32325   emit_label (label);
32326   LABEL_NUSES (label) = 1;
32327
32328   emit_move_insn (operand0, res);
32329 }
32330
32331 /* Expand SSE sequence for computing round from OPERAND1 storing
32332    into OPERAND0.  */
32333 void
32334 ix86_expand_round (rtx operand0, rtx operand1)
32335 {
32336   /* C code for the stuff we're doing below:
32337         double xa = fabs (x);
32338         if (!isless (xa, TWO52))
32339           return x;
32340         xa = (double)(long)(xa + nextafter (0.5, 0.0));
32341         return copysign (xa, x);
32342    */
32343   enum machine_mode mode = GET_MODE (operand0);
32344   rtx res, TWO52, xa, label, xi, half, mask;
32345   const struct real_format *fmt;
32346   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
32347
32348   /* Temporary for holding the result, initialized to the input
32349      operand to ease control flow.  */
32350   res = gen_reg_rtx (mode);
32351   emit_move_insn (res, operand1);
32352
32353   TWO52 = ix86_gen_TWO52 (mode);
32354   xa = ix86_expand_sse_fabs (res, &mask);
32355   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
32356
32357   /* load nextafter (0.5, 0.0) */
32358   fmt = REAL_MODE_FORMAT (mode);
32359   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
32360   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
32361
32362   /* xa = xa + 0.5 */
32363   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
32364   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
32365
32366   /* xa = (double)(int64_t)xa */
32367   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
32368   expand_fix (xi, xa, 0);
32369   expand_float (xa, xi, 0);
32370
32371   /* res = copysign (xa, operand1) */
32372   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
32373
32374   emit_label (label);
32375   LABEL_NUSES (label) = 1;
32376
32377   emit_move_insn (operand0, res);
32378 }
32379 \f
32380
32381 /* Table of valid machine attributes.  */
32382 static const struct attribute_spec ix86_attribute_table[] =
32383 {
32384   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
32385   /* Stdcall attribute says callee is responsible for popping arguments
32386      if they are not variable.  */
32387   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
32388   /* Fastcall attribute says callee is responsible for popping arguments
32389      if they are not variable.  */
32390   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
32391   /* Thiscall attribute says callee is responsible for popping arguments
32392      if they are not variable.  */
32393   { "thiscall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
32394   /* Cdecl attribute says the callee is a normal C declaration */
32395   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
32396   /* Regparm attribute specifies how many integer arguments are to be
32397      passed in registers.  */
32398   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
32399   /* Sseregparm attribute says we are using x86_64 calling conventions
32400      for FP arguments.  */
32401   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
32402   /* force_align_arg_pointer says this function realigns the stack at entry.  */
32403   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
32404     false, true,  true, ix86_handle_cconv_attribute },
32405 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
32406   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
32407   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
32408   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
32409 #endif
32410   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
32411   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
32412 #ifdef SUBTARGET_ATTRIBUTE_TABLE
32413   SUBTARGET_ATTRIBUTE_TABLE,
32414 #endif
32415   /* ms_abi and sysv_abi calling convention function attributes.  */
32416   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
32417   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
32418   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute },
32419   { "callee_pop_aggregate_return", 1, 1, false, true, true,
32420     ix86_handle_callee_pop_aggregate_return },
32421   /* End element.  */
32422   { NULL,        0, 0, false, false, false, NULL }
32423 };
32424
32425 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
32426 static int
32427 ix86_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
32428                                  tree vectype ATTRIBUTE_UNUSED,
32429                                  int misalign ATTRIBUTE_UNUSED)
32430 {
32431   switch (type_of_cost)
32432     {
32433       case scalar_stmt:
32434         return ix86_cost->scalar_stmt_cost;
32435
32436       case scalar_load:
32437         return ix86_cost->scalar_load_cost;
32438
32439       case scalar_store:
32440         return ix86_cost->scalar_store_cost;
32441
32442       case vector_stmt:
32443         return ix86_cost->vec_stmt_cost;
32444
32445       case vector_load:
32446         return ix86_cost->vec_align_load_cost;
32447
32448       case vector_store:
32449         return ix86_cost->vec_store_cost;
32450
32451       case vec_to_scalar:
32452         return ix86_cost->vec_to_scalar_cost;
32453
32454       case scalar_to_vec:
32455         return ix86_cost->scalar_to_vec_cost;
32456
32457       case unaligned_load:
32458       case unaligned_store:
32459         return ix86_cost->vec_unalign_load_cost;
32460
32461       case cond_branch_taken:
32462         return ix86_cost->cond_taken_branch_cost;
32463
32464       case cond_branch_not_taken:
32465         return ix86_cost->cond_not_taken_branch_cost;
32466
32467       case vec_perm:
32468         return 1;
32469
32470       default:
32471         gcc_unreachable ();
32472     }
32473 }
32474
32475
32476 /* Implement targetm.vectorize.builtin_vec_perm.  */
32477
32478 static tree
32479 ix86_vectorize_builtin_vec_perm (tree vec_type, tree *mask_type)
32480 {
32481   tree itype = TREE_TYPE (vec_type);
32482   bool u = TYPE_UNSIGNED (itype);
32483   enum machine_mode vmode = TYPE_MODE (vec_type);
32484   enum ix86_builtins fcode;
32485   bool ok = TARGET_SSE2;
32486
32487   switch (vmode)
32488     {
32489     case V4DFmode:
32490       ok = TARGET_AVX;
32491       fcode = IX86_BUILTIN_VEC_PERM_V4DF;
32492       goto get_di;
32493     case V2DFmode:
32494       fcode = IX86_BUILTIN_VEC_PERM_V2DF;
32495     get_di:
32496       itype = ix86_get_builtin_type (IX86_BT_DI);
32497       break;
32498
32499     case V8SFmode:
32500       ok = TARGET_AVX;
32501       fcode = IX86_BUILTIN_VEC_PERM_V8SF;
32502       goto get_si;
32503     case V4SFmode:
32504       ok = TARGET_SSE;
32505       fcode = IX86_BUILTIN_VEC_PERM_V4SF;
32506     get_si:
32507       itype = ix86_get_builtin_type (IX86_BT_SI);
32508       break;
32509
32510     case V2DImode:
32511       fcode = u ? IX86_BUILTIN_VEC_PERM_V2DI_U : IX86_BUILTIN_VEC_PERM_V2DI;
32512       break;
32513     case V4SImode:
32514       fcode = u ? IX86_BUILTIN_VEC_PERM_V4SI_U : IX86_BUILTIN_VEC_PERM_V4SI;
32515       break;
32516     case V8HImode:
32517       fcode = u ? IX86_BUILTIN_VEC_PERM_V8HI_U : IX86_BUILTIN_VEC_PERM_V8HI;
32518       break;
32519     case V16QImode:
32520       fcode = u ? IX86_BUILTIN_VEC_PERM_V16QI_U : IX86_BUILTIN_VEC_PERM_V16QI;
32521       break;
32522     default:
32523       ok = false;
32524       break;
32525     }
32526
32527   if (!ok)
32528     return NULL_TREE;
32529
32530   *mask_type = itype;
32531   return ix86_builtins[(int) fcode];
32532 }
32533
32534 /* Return a vector mode with twice as many elements as VMODE.  */
32535 /* ??? Consider moving this to a table generated by genmodes.c.  */
32536
32537 static enum machine_mode
32538 doublesize_vector_mode (enum machine_mode vmode)
32539 {
32540   switch (vmode)
32541     {
32542     case V2SFmode:      return V4SFmode;
32543     case V1DImode:      return V2DImode;
32544     case V2SImode:      return V4SImode;
32545     case V4HImode:      return V8HImode;
32546     case V8QImode:      return V16QImode;
32547
32548     case V2DFmode:      return V4DFmode;
32549     case V4SFmode:      return V8SFmode;
32550     case V2DImode:      return V4DImode;
32551     case V4SImode:      return V8SImode;
32552     case V8HImode:      return V16HImode;
32553     case V16QImode:     return V32QImode;
32554
32555     case V4DFmode:      return V8DFmode;
32556     case V8SFmode:      return V16SFmode;
32557     case V4DImode:      return V8DImode;
32558     case V8SImode:      return V16SImode;
32559     case V16HImode:     return V32HImode;
32560     case V32QImode:     return V64QImode;
32561
32562     default:
32563       gcc_unreachable ();
32564     }
32565 }
32566
32567 /* Construct (set target (vec_select op0 (parallel perm))) and
32568    return true if that's a valid instruction in the active ISA.  */
32569
32570 static bool
32571 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
32572 {
32573   rtx rperm[MAX_VECT_LEN], x;
32574   unsigned i;
32575
32576   for (i = 0; i < nelt; ++i)
32577     rperm[i] = GEN_INT (perm[i]);
32578
32579   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
32580   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
32581   x = gen_rtx_SET (VOIDmode, target, x);
32582
32583   x = emit_insn (x);
32584   if (recog_memoized (x) < 0)
32585     {
32586       remove_insn (x);
32587       return false;
32588     }
32589   return true;
32590 }
32591
32592 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
32593
32594 static bool
32595 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
32596                         const unsigned char *perm, unsigned nelt)
32597 {
32598   enum machine_mode v2mode;
32599   rtx x;
32600
32601   v2mode = doublesize_vector_mode (GET_MODE (op0));
32602   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
32603   return expand_vselect (target, x, perm, nelt);
32604 }
32605
32606 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32607    in terms of blendp[sd] / pblendw / pblendvb.  */
32608
32609 static bool
32610 expand_vec_perm_blend (struct expand_vec_perm_d *d)
32611 {
32612   enum machine_mode vmode = d->vmode;
32613   unsigned i, mask, nelt = d->nelt;
32614   rtx target, op0, op1, x;
32615
32616   if (!TARGET_SSE4_1 || d->op0 == d->op1)
32617     return false;
32618   if (!(GET_MODE_SIZE (vmode) == 16 || vmode == V4DFmode || vmode == V8SFmode))
32619     return false;
32620
32621   /* This is a blend, not a permute.  Elements must stay in their
32622      respective lanes.  */
32623   for (i = 0; i < nelt; ++i)
32624     {
32625       unsigned e = d->perm[i];
32626       if (!(e == i || e == i + nelt))
32627         return false;
32628     }
32629
32630   if (d->testing_p)
32631     return true;
32632
32633   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
32634      decision should be extracted elsewhere, so that we only try that
32635      sequence once all budget==3 options have been tried.  */
32636
32637   /* For bytes, see if bytes move in pairs so we can use pblendw with
32638      an immediate argument, rather than pblendvb with a vector argument.  */
32639   if (vmode == V16QImode)
32640     {
32641       bool pblendw_ok = true;
32642       for (i = 0; i < 16 && pblendw_ok; i += 2)
32643         pblendw_ok = (d->perm[i] + 1 == d->perm[i + 1]);
32644
32645       if (!pblendw_ok)
32646         {
32647           rtx rperm[16], vperm;
32648
32649           for (i = 0; i < nelt; ++i)
32650             rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
32651
32652           vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
32653           vperm = force_reg (V16QImode, vperm);
32654
32655           emit_insn (gen_sse4_1_pblendvb (d->target, d->op0, d->op1, vperm));
32656           return true;
32657         }
32658     }
32659
32660   target = d->target;
32661   op0 = d->op0;
32662   op1 = d->op1;
32663   mask = 0;
32664
32665   switch (vmode)
32666     {
32667     case V4DFmode:
32668     case V8SFmode:
32669     case V2DFmode:
32670     case V4SFmode:
32671     case V8HImode:
32672       for (i = 0; i < nelt; ++i)
32673         mask |= (d->perm[i] >= nelt) << i;
32674       break;
32675
32676     case V2DImode:
32677       for (i = 0; i < 2; ++i)
32678         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
32679       goto do_subreg;
32680
32681     case V4SImode:
32682       for (i = 0; i < 4; ++i)
32683         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
32684       goto do_subreg;
32685
32686     case V16QImode:
32687       for (i = 0; i < 8; ++i)
32688         mask |= (d->perm[i * 2] >= 16) << i;
32689
32690     do_subreg:
32691       vmode = V8HImode;
32692       target = gen_lowpart (vmode, target);
32693       op0 = gen_lowpart (vmode, op0);
32694       op1 = gen_lowpart (vmode, op1);
32695       break;
32696
32697     default:
32698       gcc_unreachable ();
32699     }
32700
32701   /* This matches five different patterns with the different modes.  */
32702   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
32703   x = gen_rtx_SET (VOIDmode, target, x);
32704   emit_insn (x);
32705
32706   return true;
32707 }
32708
32709 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32710    in terms of the variable form of vpermilps.
32711
32712    Note that we will have already failed the immediate input vpermilps,
32713    which requires that the high and low part shuffle be identical; the
32714    variable form doesn't require that.  */
32715
32716 static bool
32717 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
32718 {
32719   rtx rperm[8], vperm;
32720   unsigned i;
32721
32722   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
32723     return false;
32724
32725   /* We can only permute within the 128-bit lane.  */
32726   for (i = 0; i < 8; ++i)
32727     {
32728       unsigned e = d->perm[i];
32729       if (i < 4 ? e >= 4 : e < 4)
32730         return false;
32731     }
32732
32733   if (d->testing_p)
32734     return true;
32735
32736   for (i = 0; i < 8; ++i)
32737     {
32738       unsigned e = d->perm[i];
32739
32740       /* Within each 128-bit lane, the elements of op0 are numbered
32741          from 0 and the elements of op1 are numbered from 4.  */
32742       if (e >= 8 + 4)
32743         e -= 8;
32744       else if (e >= 4)
32745         e -= 4;
32746
32747       rperm[i] = GEN_INT (e);
32748     }
32749
32750   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
32751   vperm = force_reg (V8SImode, vperm);
32752   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
32753
32754   return true;
32755 }
32756
32757 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32758    in terms of pshufb or vpperm.  */
32759
32760 static bool
32761 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
32762 {
32763   unsigned i, nelt, eltsz;
32764   rtx rperm[16], vperm, target, op0, op1;
32765
32766   if (!(d->op0 == d->op1 ? TARGET_SSSE3 : TARGET_XOP))
32767     return false;
32768   if (GET_MODE_SIZE (d->vmode) != 16)
32769     return false;
32770
32771   if (d->testing_p)
32772     return true;
32773
32774   nelt = d->nelt;
32775   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
32776
32777   for (i = 0; i < nelt; ++i)
32778     {
32779       unsigned j, e = d->perm[i];
32780       for (j = 0; j < eltsz; ++j)
32781         rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
32782     }
32783
32784   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
32785   vperm = force_reg (V16QImode, vperm);
32786
32787   target = gen_lowpart (V16QImode, d->target);
32788   op0 = gen_lowpart (V16QImode, d->op0);
32789   if (d->op0 == d->op1)
32790     emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
32791   else
32792     {
32793       op1 = gen_lowpart (V16QImode, d->op1);
32794       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
32795     }
32796
32797   return true;
32798 }
32799
32800 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
32801    in a single instruction.  */
32802
32803 static bool
32804 expand_vec_perm_1 (struct expand_vec_perm_d *d)
32805 {
32806   unsigned i, nelt = d->nelt;
32807   unsigned char perm2[MAX_VECT_LEN];
32808
32809   /* Check plain VEC_SELECT first, because AVX has instructions that could
32810      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
32811      input where SEL+CONCAT may not.  */
32812   if (d->op0 == d->op1)
32813     {
32814       int mask = nelt - 1;
32815
32816       for (i = 0; i < nelt; i++)
32817         perm2[i] = d->perm[i] & mask;
32818
32819       if (expand_vselect (d->target, d->op0, perm2, nelt))
32820         return true;
32821
32822       /* There are plenty of patterns in sse.md that are written for
32823          SEL+CONCAT and are not replicated for a single op.  Perhaps
32824          that should be changed, to avoid the nastiness here.  */
32825
32826       /* Recognize interleave style patterns, which means incrementing
32827          every other permutation operand.  */
32828       for (i = 0; i < nelt; i += 2)
32829         {
32830           perm2[i] = d->perm[i] & mask;
32831           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
32832         }
32833       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
32834         return true;
32835
32836       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
32837       if (nelt >= 4)
32838         {
32839           for (i = 0; i < nelt; i += 4)
32840             {
32841               perm2[i + 0] = d->perm[i + 0] & mask;
32842               perm2[i + 1] = d->perm[i + 1] & mask;
32843               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
32844               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
32845             }
32846
32847           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
32848             return true;
32849         }
32850     }
32851
32852   /* Finally, try the fully general two operand permute.  */
32853   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
32854     return true;
32855
32856   /* Recognize interleave style patterns with reversed operands.  */
32857   if (d->op0 != d->op1)
32858     {
32859       for (i = 0; i < nelt; ++i)
32860         {
32861           unsigned e = d->perm[i];
32862           if (e >= nelt)
32863             e -= nelt;
32864           else
32865             e += nelt;
32866           perm2[i] = e;
32867         }
32868
32869       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
32870         return true;
32871     }
32872
32873   /* Try the SSE4.1 blend variable merge instructions.  */
32874   if (expand_vec_perm_blend (d))
32875     return true;
32876
32877   /* Try one of the AVX vpermil variable permutations.  */
32878   if (expand_vec_perm_vpermil (d))
32879     return true;
32880
32881   /* Try the SSSE3 pshufb or XOP vpperm variable permutation.  */
32882   if (expand_vec_perm_pshufb (d))
32883     return true;
32884
32885   return false;
32886 }
32887
32888 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32889    in terms of a pair of pshuflw + pshufhw instructions.  */
32890
32891 static bool
32892 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
32893 {
32894   unsigned char perm2[MAX_VECT_LEN];
32895   unsigned i;
32896   bool ok;
32897
32898   if (d->vmode != V8HImode || d->op0 != d->op1)
32899     return false;
32900
32901   /* The two permutations only operate in 64-bit lanes.  */
32902   for (i = 0; i < 4; ++i)
32903     if (d->perm[i] >= 4)
32904       return false;
32905   for (i = 4; i < 8; ++i)
32906     if (d->perm[i] < 4)
32907       return false;
32908
32909   if (d->testing_p)
32910     return true;
32911
32912   /* Emit the pshuflw.  */
32913   memcpy (perm2, d->perm, 4);
32914   for (i = 4; i < 8; ++i)
32915     perm2[i] = i;
32916   ok = expand_vselect (d->target, d->op0, perm2, 8);
32917   gcc_assert (ok);
32918
32919   /* Emit the pshufhw.  */
32920   memcpy (perm2 + 4, d->perm + 4, 4);
32921   for (i = 0; i < 4; ++i)
32922     perm2[i] = i;
32923   ok = expand_vselect (d->target, d->target, perm2, 8);
32924   gcc_assert (ok);
32925
32926   return true;
32927 }
32928
32929 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
32930    the permutation using the SSSE3 palignr instruction.  This succeeds
32931    when all of the elements in PERM fit within one vector and we merely
32932    need to shift them down so that a single vector permutation has a
32933    chance to succeed.  */
32934
32935 static bool
32936 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
32937 {
32938   unsigned i, nelt = d->nelt;
32939   unsigned min, max;
32940   bool in_order, ok;
32941   rtx shift;
32942
32943   /* Even with AVX, palignr only operates on 128-bit vectors.  */
32944   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
32945     return false;
32946
32947   min = nelt, max = 0;
32948   for (i = 0; i < nelt; ++i)
32949     {
32950       unsigned e = d->perm[i];
32951       if (e < min)
32952         min = e;
32953       if (e > max)
32954         max = e;
32955     }
32956   if (min == 0 || max - min >= nelt)
32957     return false;
32958
32959   /* Given that we have SSSE3, we know we'll be able to implement the
32960      single operand permutation after the palignr with pshufb.  */
32961   if (d->testing_p)
32962     return true;
32963
32964   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
32965   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
32966                                   gen_lowpart (TImode, d->op1),
32967                                   gen_lowpart (TImode, d->op0), shift));
32968
32969   d->op0 = d->op1 = d->target;
32970
32971   in_order = true;
32972   for (i = 0; i < nelt; ++i)
32973     {
32974       unsigned e = d->perm[i] - min;
32975       if (e != i)
32976         in_order = false;
32977       d->perm[i] = e;
32978     }
32979
32980   /* Test for the degenerate case where the alignment by itself
32981      produces the desired permutation.  */
32982   if (in_order)
32983     return true;
32984
32985   ok = expand_vec_perm_1 (d);
32986   gcc_assert (ok);
32987
32988   return ok;
32989 }
32990
32991 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
32992    a two vector permutation into a single vector permutation by using
32993    an interleave operation to merge the vectors.  */
32994
32995 static bool
32996 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
32997 {
32998   struct expand_vec_perm_d dremap, dfinal;
32999   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
33000   unsigned contents, h1, h2, h3, h4;
33001   unsigned char remap[2 * MAX_VECT_LEN];
33002   rtx seq;
33003   bool ok;
33004
33005   if (d->op0 == d->op1)
33006     return false;
33007
33008   /* The 256-bit unpck[lh]p[sd] instructions only operate within the 128-bit
33009      lanes.  We can use similar techniques with the vperm2f128 instruction,
33010      but it requires slightly different logic.  */
33011   if (GET_MODE_SIZE (d->vmode) != 16)
33012     return false;
33013
33014   /* Examine from whence the elements come.  */
33015   contents = 0;
33016   for (i = 0; i < nelt; ++i)
33017     contents |= 1u << d->perm[i];
33018
33019   /* Split the two input vectors into 4 halves.  */
33020   h1 = (1u << nelt2) - 1;
33021   h2 = h1 << nelt2;
33022   h3 = h2 << nelt2;
33023   h4 = h3 << nelt2;
33024
33025   memset (remap, 0xff, sizeof (remap));
33026   dremap = *d;
33027
33028   /* If the elements from the low halves use interleave low, and similarly
33029      for interleave high.  If the elements are from mis-matched halves, we
33030      can use shufps for V4SF/V4SI or do a DImode shuffle.  */
33031   if ((contents & (h1 | h3)) == contents)
33032     {
33033       for (i = 0; i < nelt2; ++i)
33034         {
33035           remap[i] = i * 2;
33036           remap[i + nelt] = i * 2 + 1;
33037           dremap.perm[i * 2] = i;
33038           dremap.perm[i * 2 + 1] = i + nelt;
33039         }
33040     }
33041   else if ((contents & (h2 | h4)) == contents)
33042     {
33043       for (i = 0; i < nelt2; ++i)
33044         {
33045           remap[i + nelt2] = i * 2;
33046           remap[i + nelt + nelt2] = i * 2 + 1;
33047           dremap.perm[i * 2] = i + nelt2;
33048           dremap.perm[i * 2 + 1] = i + nelt + nelt2;
33049         }
33050     }
33051   else if ((contents & (h1 | h4)) == contents)
33052     {
33053       for (i = 0; i < nelt2; ++i)
33054         {
33055           remap[i] = i;
33056           remap[i + nelt + nelt2] = i + nelt2;
33057           dremap.perm[i] = i;
33058           dremap.perm[i + nelt2] = i + nelt + nelt2;
33059         }
33060       if (nelt != 4)
33061         {
33062           dremap.vmode = V2DImode;
33063           dremap.nelt = 2;
33064           dremap.perm[0] = 0;
33065           dremap.perm[1] = 3;
33066         }
33067     }
33068   else if ((contents & (h2 | h3)) == contents)
33069     {
33070       for (i = 0; i < nelt2; ++i)
33071         {
33072           remap[i + nelt2] = i;
33073           remap[i + nelt] = i + nelt2;
33074           dremap.perm[i] = i + nelt2;
33075           dremap.perm[i + nelt2] = i + nelt;
33076         }
33077       if (nelt != 4)
33078         {
33079           dremap.vmode = V2DImode;
33080           dremap.nelt = 2;
33081           dremap.perm[0] = 1;
33082           dremap.perm[1] = 2;
33083         }
33084     }
33085   else
33086     return false;
33087
33088   /* Use the remapping array set up above to move the elements from their
33089      swizzled locations into their final destinations.  */
33090   dfinal = *d;
33091   for (i = 0; i < nelt; ++i)
33092     {
33093       unsigned e = remap[d->perm[i]];
33094       gcc_assert (e < nelt);
33095       dfinal.perm[i] = e;
33096     }
33097   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
33098   dfinal.op1 = dfinal.op0;
33099   dremap.target = dfinal.op0;
33100
33101   /* Test if the final remap can be done with a single insn.  For V4SFmode or
33102      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
33103   start_sequence ();
33104   ok = expand_vec_perm_1 (&dfinal);
33105   seq = get_insns ();
33106   end_sequence ();
33107
33108   if (!ok)
33109     return false;
33110
33111   if (dremap.vmode != dfinal.vmode)
33112     {
33113       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
33114       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
33115       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
33116     }
33117
33118   ok = expand_vec_perm_1 (&dremap);
33119   gcc_assert (ok);
33120
33121   emit_insn (seq);
33122   return true;
33123 }
33124
33125 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
33126    permutation with two pshufb insns and an ior.  We should have already
33127    failed all two instruction sequences.  */
33128
33129 static bool
33130 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
33131 {
33132   rtx rperm[2][16], vperm, l, h, op, m128;
33133   unsigned int i, nelt, eltsz;
33134
33135   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
33136     return false;
33137   gcc_assert (d->op0 != d->op1);
33138
33139   nelt = d->nelt;
33140   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
33141
33142   /* Generate two permutation masks.  If the required element is within
33143      the given vector it is shuffled into the proper lane.  If the required
33144      element is in the other vector, force a zero into the lane by setting
33145      bit 7 in the permutation mask.  */
33146   m128 = GEN_INT (-128);
33147   for (i = 0; i < nelt; ++i)
33148     {
33149       unsigned j, e = d->perm[i];
33150       unsigned which = (e >= nelt);
33151       if (e >= nelt)
33152         e -= nelt;
33153
33154       for (j = 0; j < eltsz; ++j)
33155         {
33156           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
33157           rperm[1-which][i*eltsz + j] = m128;
33158         }
33159     }
33160
33161   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
33162   vperm = force_reg (V16QImode, vperm);
33163
33164   l = gen_reg_rtx (V16QImode);
33165   op = gen_lowpart (V16QImode, d->op0);
33166   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
33167
33168   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
33169   vperm = force_reg (V16QImode, vperm);
33170
33171   h = gen_reg_rtx (V16QImode);
33172   op = gen_lowpart (V16QImode, d->op1);
33173   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
33174
33175   op = gen_lowpart (V16QImode, d->target);
33176   emit_insn (gen_iorv16qi3 (op, l, h));
33177
33178   return true;
33179 }
33180
33181 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
33182    and extract-odd permutations.  */
33183
33184 static bool
33185 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
33186 {
33187   rtx t1, t2, t3;
33188
33189   switch (d->vmode)
33190     {
33191     case V4DFmode:
33192       t1 = gen_reg_rtx (V4DFmode);
33193       t2 = gen_reg_rtx (V4DFmode);
33194
33195       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
33196       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
33197       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
33198
33199       /* Now an unpck[lh]pd will produce the result required.  */
33200       if (odd)
33201         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
33202       else
33203         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
33204       emit_insn (t3);
33205       break;
33206
33207     case V8SFmode:
33208       {
33209         int mask = odd ? 0xdd : 0x88;
33210
33211         t1 = gen_reg_rtx (V8SFmode);
33212         t2 = gen_reg_rtx (V8SFmode);
33213         t3 = gen_reg_rtx (V8SFmode);
33214
33215         /* Shuffle within the 128-bit lanes to produce:
33216            { 0 2 8 a 4 6 c e } | { 1 3 9 b 5 7 d f }.  */
33217         emit_insn (gen_avx_shufps256 (t1, d->op0, d->op1,
33218                                       GEN_INT (mask)));
33219
33220         /* Shuffle the lanes around to produce:
33221            { 4 6 c e 0 2 8 a } and { 5 7 d f 1 3 9 b }.  */
33222         emit_insn (gen_avx_vperm2f128v8sf3 (t2, t1, t1,
33223                                             GEN_INT (0x3)));
33224
33225         /* Shuffle within the 128-bit lanes to produce:
33226            { 0 2 4 6 4 6 0 2 } | { 1 3 5 7 5 7 1 3 }.  */
33227         emit_insn (gen_avx_shufps256 (t3, t1, t2, GEN_INT (0x44)));
33228
33229         /* Shuffle within the 128-bit lanes to produce:
33230            { 8 a c e c e 8 a } | { 9 b d f d f 9 b }.  */
33231         emit_insn (gen_avx_shufps256 (t2, t1, t2, GEN_INT (0xee)));
33232
33233         /* Shuffle the lanes around to produce:
33234            { 0 2 4 6 8 a c e } | { 1 3 5 7 9 b d f }.  */
33235         emit_insn (gen_avx_vperm2f128v8sf3 (d->target, t3, t2,
33236                                             GEN_INT (0x20)));
33237       }
33238       break;
33239
33240     case V2DFmode:
33241     case V4SFmode:
33242     case V2DImode:
33243     case V4SImode:
33244       /* These are always directly implementable by expand_vec_perm_1.  */
33245       gcc_unreachable ();
33246
33247     case V8HImode:
33248       if (TARGET_SSSE3)
33249         return expand_vec_perm_pshufb2 (d);
33250       else
33251         {
33252           /* We need 2*log2(N)-1 operations to achieve odd/even
33253              with interleave. */
33254           t1 = gen_reg_rtx (V8HImode);
33255           t2 = gen_reg_rtx (V8HImode);
33256           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
33257           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
33258           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
33259           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
33260           if (odd)
33261             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
33262           else
33263             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
33264           emit_insn (t3);
33265         }
33266       break;
33267
33268     case V16QImode:
33269       if (TARGET_SSSE3)
33270         return expand_vec_perm_pshufb2 (d);
33271       else
33272         {
33273           t1 = gen_reg_rtx (V16QImode);
33274           t2 = gen_reg_rtx (V16QImode);
33275           t3 = gen_reg_rtx (V16QImode);
33276           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
33277           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
33278           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
33279           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
33280           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
33281           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
33282           if (odd)
33283             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
33284           else
33285             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
33286           emit_insn (t3);
33287         }
33288       break;
33289
33290     default:
33291       gcc_unreachable ();
33292     }
33293
33294   return true;
33295 }
33296
33297 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
33298    extract-even and extract-odd permutations.  */
33299
33300 static bool
33301 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
33302 {
33303   unsigned i, odd, nelt = d->nelt;
33304
33305   odd = d->perm[0];
33306   if (odd != 0 && odd != 1)
33307     return false;
33308
33309   for (i = 1; i < nelt; ++i)
33310     if (d->perm[i] != 2 * i + odd)
33311       return false;
33312
33313   return expand_vec_perm_even_odd_1 (d, odd);
33314 }
33315
33316 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
33317    permutations.  We assume that expand_vec_perm_1 has already failed.  */
33318
33319 static bool
33320 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
33321 {
33322   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
33323   enum machine_mode vmode = d->vmode;
33324   unsigned char perm2[4];
33325   rtx op0 = d->op0;
33326   bool ok;
33327
33328   switch (vmode)
33329     {
33330     case V4DFmode:
33331     case V8SFmode:
33332       /* These are special-cased in sse.md so that we can optionally
33333          use the vbroadcast instruction.  They expand to two insns
33334          if the input happens to be in a register.  */
33335       gcc_unreachable ();
33336
33337     case V2DFmode:
33338     case V2DImode:
33339     case V4SFmode:
33340     case V4SImode:
33341       /* These are always implementable using standard shuffle patterns.  */
33342       gcc_unreachable ();
33343
33344     case V8HImode:
33345     case V16QImode:
33346       /* These can be implemented via interleave.  We save one insn by
33347          stopping once we have promoted to V4SImode and then use pshufd.  */
33348       do
33349         {
33350           optab otab = vec_interleave_low_optab;
33351
33352           if (elt >= nelt2)
33353             {
33354               otab = vec_interleave_high_optab;
33355               elt -= nelt2;
33356             }
33357           nelt2 /= 2;
33358
33359           op0 = expand_binop (vmode, otab, op0, op0, NULL, 0, OPTAB_DIRECT);
33360           vmode = get_mode_wider_vector (vmode);
33361           op0 = gen_lowpart (vmode, op0);
33362         }
33363       while (vmode != V4SImode);
33364
33365       memset (perm2, elt, 4);
33366       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
33367       gcc_assert (ok);
33368       return true;
33369
33370     default:
33371       gcc_unreachable ();
33372     }
33373 }
33374
33375 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
33376    broadcast permutations.  */
33377
33378 static bool
33379 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
33380 {
33381   unsigned i, elt, nelt = d->nelt;
33382
33383   if (d->op0 != d->op1)
33384     return false;
33385
33386   elt = d->perm[0];
33387   for (i = 1; i < nelt; ++i)
33388     if (d->perm[i] != elt)
33389       return false;
33390
33391   return expand_vec_perm_broadcast_1 (d);
33392 }
33393
33394 /* The guts of ix86_expand_vec_perm_builtin, also used by the ok hook.
33395    With all of the interface bits taken care of, perform the expansion
33396    in D and return true on success.  */
33397
33398 static bool
33399 ix86_expand_vec_perm_builtin_1 (struct expand_vec_perm_d *d)
33400 {
33401   /* Try a single instruction expansion.  */
33402   if (expand_vec_perm_1 (d))
33403     return true;
33404
33405   /* Try sequences of two instructions.  */
33406
33407   if (expand_vec_perm_pshuflw_pshufhw (d))
33408     return true;
33409
33410   if (expand_vec_perm_palignr (d))
33411     return true;
33412
33413   if (expand_vec_perm_interleave2 (d))
33414     return true;
33415
33416   if (expand_vec_perm_broadcast (d))
33417     return true;
33418
33419   /* Try sequences of three instructions.  */
33420
33421   if (expand_vec_perm_pshufb2 (d))
33422     return true;
33423
33424   /* ??? Look for narrow permutations whose element orderings would
33425      allow the promotion to a wider mode.  */
33426
33427   /* ??? Look for sequences of interleave or a wider permute that place
33428      the data into the correct lanes for a half-vector shuffle like
33429      pshuf[lh]w or vpermilps.  */
33430
33431   /* ??? Look for sequences of interleave that produce the desired results.
33432      The combinatorics of punpck[lh] get pretty ugly... */
33433
33434   if (expand_vec_perm_even_odd (d))
33435     return true;
33436
33437   return false;
33438 }
33439
33440 /* Extract the values from the vector CST into the permutation array in D.
33441    Return 0 on error, 1 if all values from the permutation come from the
33442    first vector, 2 if all values from the second vector, and 3 otherwise.  */
33443
33444 static int
33445 extract_vec_perm_cst (struct expand_vec_perm_d *d, tree cst)
33446 {
33447   tree list = TREE_VECTOR_CST_ELTS (cst);
33448   unsigned i, nelt = d->nelt;
33449   int ret = 0;
33450
33451   for (i = 0; i < nelt; ++i, list = TREE_CHAIN (list))
33452     {
33453       unsigned HOST_WIDE_INT e;
33454
33455       if (!host_integerp (TREE_VALUE (list), 1))
33456         return 0;
33457       e = tree_low_cst (TREE_VALUE (list), 1);
33458       if (e >= 2 * nelt)
33459         return 0;
33460
33461       ret |= (e < nelt ? 1 : 2);
33462       d->perm[i] = e;
33463     }
33464   gcc_assert (list == NULL);
33465
33466   /* For all elements from second vector, fold the elements to first.  */
33467   if (ret == 2)
33468     for (i = 0; i < nelt; ++i)
33469       d->perm[i] -= nelt;
33470
33471   return ret;
33472 }
33473
33474 static rtx
33475 ix86_expand_vec_perm_builtin (tree exp)
33476 {
33477   struct expand_vec_perm_d d;
33478   tree arg0, arg1, arg2;
33479
33480   arg0 = CALL_EXPR_ARG (exp, 0);
33481   arg1 = CALL_EXPR_ARG (exp, 1);
33482   arg2 = CALL_EXPR_ARG (exp, 2);
33483
33484   d.vmode = TYPE_MODE (TREE_TYPE (arg0));
33485   d.nelt = GET_MODE_NUNITS (d.vmode);
33486   d.testing_p = false;
33487   gcc_assert (VECTOR_MODE_P (d.vmode));
33488
33489   if (TREE_CODE (arg2) != VECTOR_CST)
33490     {
33491       error_at (EXPR_LOCATION (exp),
33492                 "vector permutation requires vector constant");
33493       goto exit_error;
33494     }
33495
33496   switch (extract_vec_perm_cst (&d, arg2))
33497     {
33498     default:
33499       gcc_unreachable();
33500
33501     case 0:
33502       error_at (EXPR_LOCATION (exp), "invalid vector permutation constant");
33503       goto exit_error;
33504
33505     case 3:
33506       if (!operand_equal_p (arg0, arg1, 0))
33507         {
33508           d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
33509           d.op0 = force_reg (d.vmode, d.op0);
33510           d.op1 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
33511           d.op1 = force_reg (d.vmode, d.op1);
33512           break;
33513         }
33514
33515       /* The elements of PERM do not suggest that only the first operand
33516          is used, but both operands are identical.  Allow easier matching
33517          of the permutation by folding the permutation into the single
33518          input vector.  */
33519       {
33520         unsigned i, nelt = d.nelt;
33521         for (i = 0; i < nelt; ++i)
33522           if (d.perm[i] >= nelt)
33523             d.perm[i] -= nelt;
33524       }
33525       /* FALLTHRU */
33526
33527     case 1:
33528       d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
33529       d.op0 = force_reg (d.vmode, d.op0);
33530       d.op1 = d.op0;
33531       break;
33532
33533     case 2:
33534       d.op0 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
33535       d.op0 = force_reg (d.vmode, d.op0);
33536       d.op1 = d.op0;
33537       break;
33538     }
33539
33540   d.target = gen_reg_rtx (d.vmode);
33541   if (ix86_expand_vec_perm_builtin_1 (&d))
33542     return d.target;
33543
33544   /* For compiler generated permutations, we should never got here, because
33545      the compiler should also be checking the ok hook.  But since this is a
33546      builtin the user has access too, so don't abort.  */
33547   switch (d.nelt)
33548     {
33549     case 2:
33550       sorry ("vector permutation (%d %d)", d.perm[0], d.perm[1]);
33551       break;
33552     case 4:
33553       sorry ("vector permutation (%d %d %d %d)",
33554              d.perm[0], d.perm[1], d.perm[2], d.perm[3]);
33555       break;
33556     case 8:
33557       sorry ("vector permutation (%d %d %d %d %d %d %d %d)",
33558              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
33559              d.perm[4], d.perm[5], d.perm[6], d.perm[7]);
33560       break;
33561     case 16:
33562       sorry ("vector permutation "
33563              "(%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d)",
33564              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
33565              d.perm[4], d.perm[5], d.perm[6], d.perm[7],
33566              d.perm[8], d.perm[9], d.perm[10], d.perm[11],
33567              d.perm[12], d.perm[13], d.perm[14], d.perm[15]);
33568       break;
33569     default:
33570       gcc_unreachable ();
33571     }
33572  exit_error:
33573   return CONST0_RTX (d.vmode);
33574 }
33575
33576 /* Implement targetm.vectorize.builtin_vec_perm_ok.  */
33577
33578 static bool
33579 ix86_vectorize_builtin_vec_perm_ok (tree vec_type, tree mask)
33580 {
33581   struct expand_vec_perm_d d;
33582   int vec_mask;
33583   bool ret, one_vec;
33584
33585   d.vmode = TYPE_MODE (vec_type);
33586   d.nelt = GET_MODE_NUNITS (d.vmode);
33587   d.testing_p = true;
33588
33589   /* Given sufficient ISA support we can just return true here
33590      for selected vector modes.  */
33591   if (GET_MODE_SIZE (d.vmode) == 16)
33592     {
33593       /* All implementable with a single vpperm insn.  */
33594       if (TARGET_XOP)
33595         return true;
33596       /* All implementable with 2 pshufb + 1 ior.  */
33597       if (TARGET_SSSE3)
33598         return true;
33599       /* All implementable with shufpd or unpck[lh]pd.  */
33600       if (d.nelt == 2)
33601         return true;
33602     }
33603
33604   vec_mask = extract_vec_perm_cst (&d, mask);
33605
33606   /* This hook is cannot be called in response to something that the
33607      user does (unlike the builtin expander) so we shouldn't ever see
33608      an error generated from the extract.  */
33609   gcc_assert (vec_mask > 0 && vec_mask <= 3);
33610   one_vec = (vec_mask != 3);
33611
33612   /* Implementable with shufps or pshufd.  */
33613   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
33614     return true;
33615
33616   /* Otherwise we have to go through the motions and see if we can
33617      figure out how to generate the requested permutation.  */
33618   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
33619   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
33620   if (!one_vec)
33621     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
33622
33623   start_sequence ();
33624   ret = ix86_expand_vec_perm_builtin_1 (&d);
33625   end_sequence ();
33626
33627   return ret;
33628 }
33629
33630 void
33631 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
33632 {
33633   struct expand_vec_perm_d d;
33634   unsigned i, nelt;
33635
33636   d.target = targ;
33637   d.op0 = op0;
33638   d.op1 = op1;
33639   d.vmode = GET_MODE (targ);
33640   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
33641   d.testing_p = false;
33642
33643   for (i = 0; i < nelt; ++i)
33644     d.perm[i] = i * 2 + odd;
33645
33646   /* We'll either be able to implement the permutation directly...  */
33647   if (expand_vec_perm_1 (&d))
33648     return;
33649
33650   /* ... or we use the special-case patterns.  */
33651   expand_vec_perm_even_odd_1 (&d, odd);
33652 }
33653 \f
33654 /* This function returns the calling abi specific va_list type node.
33655    It returns  the FNDECL specific va_list type.  */
33656
33657 static tree
33658 ix86_fn_abi_va_list (tree fndecl)
33659 {
33660   if (!TARGET_64BIT)
33661     return va_list_type_node;
33662   gcc_assert (fndecl != NULL_TREE);
33663
33664   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
33665     return ms_va_list_type_node;
33666   else
33667     return sysv_va_list_type_node;
33668 }
33669
33670 /* Returns the canonical va_list type specified by TYPE. If there
33671    is no valid TYPE provided, it return NULL_TREE.  */
33672
33673 static tree
33674 ix86_canonical_va_list_type (tree type)
33675 {
33676   tree wtype, htype;
33677
33678   /* Resolve references and pointers to va_list type.  */
33679   if (TREE_CODE (type) == MEM_REF)
33680     type = TREE_TYPE (type);
33681   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
33682     type = TREE_TYPE (type);
33683   else if (POINTER_TYPE_P (type) && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
33684     type = TREE_TYPE (type);
33685
33686   if (TARGET_64BIT && va_list_type_node != NULL_TREE)
33687     {
33688       wtype = va_list_type_node;
33689           gcc_assert (wtype != NULL_TREE);
33690       htype = type;
33691       if (TREE_CODE (wtype) == ARRAY_TYPE)
33692         {
33693           /* If va_list is an array type, the argument may have decayed
33694              to a pointer type, e.g. by being passed to another function.
33695              In that case, unwrap both types so that we can compare the
33696              underlying records.  */
33697           if (TREE_CODE (htype) == ARRAY_TYPE
33698               || POINTER_TYPE_P (htype))
33699             {
33700               wtype = TREE_TYPE (wtype);
33701               htype = TREE_TYPE (htype);
33702             }
33703         }
33704       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
33705         return va_list_type_node;
33706       wtype = sysv_va_list_type_node;
33707           gcc_assert (wtype != NULL_TREE);
33708       htype = type;
33709       if (TREE_CODE (wtype) == ARRAY_TYPE)
33710         {
33711           /* If va_list is an array type, the argument may have decayed
33712              to a pointer type, e.g. by being passed to another function.
33713              In that case, unwrap both types so that we can compare the
33714              underlying records.  */
33715           if (TREE_CODE (htype) == ARRAY_TYPE
33716               || POINTER_TYPE_P (htype))
33717             {
33718               wtype = TREE_TYPE (wtype);
33719               htype = TREE_TYPE (htype);
33720             }
33721         }
33722       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
33723         return sysv_va_list_type_node;
33724       wtype = ms_va_list_type_node;
33725           gcc_assert (wtype != NULL_TREE);
33726       htype = type;
33727       if (TREE_CODE (wtype) == ARRAY_TYPE)
33728         {
33729           /* If va_list is an array type, the argument may have decayed
33730              to a pointer type, e.g. by being passed to another function.
33731              In that case, unwrap both types so that we can compare the
33732              underlying records.  */
33733           if (TREE_CODE (htype) == ARRAY_TYPE
33734               || POINTER_TYPE_P (htype))
33735             {
33736               wtype = TREE_TYPE (wtype);
33737               htype = TREE_TYPE (htype);
33738             }
33739         }
33740       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
33741         return ms_va_list_type_node;
33742       return NULL_TREE;
33743     }
33744   return std_canonical_va_list_type (type);
33745 }
33746
33747 /* Iterate through the target-specific builtin types for va_list.
33748    IDX denotes the iterator, *PTREE is set to the result type of
33749    the va_list builtin, and *PNAME to its internal type.
33750    Returns zero if there is no element for this index, otherwise
33751    IDX should be increased upon the next call.
33752    Note, do not iterate a base builtin's name like __builtin_va_list.
33753    Used from c_common_nodes_and_builtins.  */
33754
33755 static int
33756 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
33757 {
33758   if (TARGET_64BIT)
33759     {
33760       switch (idx)
33761         {
33762         default:
33763           break;
33764
33765         case 0:
33766           *ptree = ms_va_list_type_node;
33767           *pname = "__builtin_ms_va_list";
33768           return 1;
33769
33770         case 1:
33771           *ptree = sysv_va_list_type_node;
33772           *pname = "__builtin_sysv_va_list";
33773           return 1;
33774         }
33775     }
33776
33777   return 0;
33778 }
33779
33780 #undef TARGET_SCHED_DISPATCH
33781 #define TARGET_SCHED_DISPATCH has_dispatch
33782 #undef TARGET_SCHED_DISPATCH_DO
33783 #define TARGET_SCHED_DISPATCH_DO do_dispatch
33784
33785 /* The size of the dispatch window is the total number of bytes of
33786    object code allowed in a window.  */
33787 #define DISPATCH_WINDOW_SIZE 16
33788
33789 /* Number of dispatch windows considered for scheduling.  */
33790 #define MAX_DISPATCH_WINDOWS 3
33791
33792 /* Maximum number of instructions in a window.  */
33793 #define MAX_INSN 4
33794
33795 /* Maximum number of immediate operands in a window.  */
33796 #define MAX_IMM 4
33797
33798 /* Maximum number of immediate bits allowed in a window.  */
33799 #define MAX_IMM_SIZE 128
33800
33801 /* Maximum number of 32 bit immediates allowed in a window.  */
33802 #define MAX_IMM_32 4
33803
33804 /* Maximum number of 64 bit immediates allowed in a window.  */
33805 #define MAX_IMM_64 2
33806
33807 /* Maximum total of loads or prefetches allowed in a window.  */
33808 #define MAX_LOAD 2
33809
33810 /* Maximum total of stores allowed in a window.  */
33811 #define MAX_STORE 1
33812
33813 #undef BIG
33814 #define BIG 100
33815
33816
33817 /* Dispatch groups.  Istructions that affect the mix in a dispatch window.  */
33818 enum dispatch_group {
33819   disp_no_group = 0,
33820   disp_load,
33821   disp_store,
33822   disp_load_store,
33823   disp_prefetch,
33824   disp_imm,
33825   disp_imm_32,
33826   disp_imm_64,
33827   disp_branch,
33828   disp_cmp,
33829   disp_jcc,
33830   disp_last
33831 };
33832
33833 /* Number of allowable groups in a dispatch window.  It is an array
33834    indexed by dispatch_group enum.  100 is used as a big number,
33835    because the number of these kind of operations does not have any
33836    effect in dispatch window, but we need them for other reasons in
33837    the table.  */
33838 static unsigned int num_allowable_groups[disp_last] = {
33839   0, 2, 1, 1, 2, 4, 4, 2, 1, BIG, BIG
33840 };
33841
33842 char group_name[disp_last + 1][16] = {
33843   "disp_no_group", "disp_load", "disp_store", "disp_load_store",
33844   "disp_prefetch", "disp_imm", "disp_imm_32", "disp_imm_64",
33845   "disp_branch", "disp_cmp", "disp_jcc", "disp_last"
33846 };
33847
33848 /* Instruction path.  */
33849 enum insn_path {
33850   no_path = 0,
33851   path_single, /* Single micro op.  */
33852   path_double, /* Double micro op.  */
33853   path_multi,  /* Instructions with more than 2 micro op..  */
33854   last_path
33855 };
33856
33857 /* sched_insn_info defines a window to the instructions scheduled in
33858    the basic block.  It contains a pointer to the insn_info table and
33859    the instruction scheduled.
33860
33861    Windows are allocated for each basic block and are linked
33862    together.  */
33863 typedef struct sched_insn_info_s {
33864   rtx insn;
33865   enum dispatch_group group;
33866   enum insn_path path;
33867   int byte_len;
33868   int imm_bytes;
33869 } sched_insn_info;
33870
33871 /* Linked list of dispatch windows.  This is a two way list of
33872    dispatch windows of a basic block.  It contains information about
33873    the number of uops in the window and the total number of
33874    instructions and of bytes in the object code for this dispatch
33875    window.  */
33876 typedef struct dispatch_windows_s {
33877   int num_insn;            /* Number of insn in the window.  */
33878   int num_uops;            /* Number of uops in the window.  */
33879   int window_size;         /* Number of bytes in the window.  */
33880   int window_num;          /* Window number between 0 or 1.  */
33881   int num_imm;             /* Number of immediates in an insn.  */
33882   int num_imm_32;          /* Number of 32 bit immediates in an insn.  */
33883   int num_imm_64;          /* Number of 64 bit immediates in an insn.  */
33884   int imm_size;            /* Total immediates in the window.  */
33885   int num_loads;           /* Total memory loads in the window.  */
33886   int num_stores;          /* Total memory stores in the window.  */
33887   int violation;          /* Violation exists in window.  */
33888   sched_insn_info *window; /* Pointer to the window.  */
33889   struct dispatch_windows_s *next;
33890   struct dispatch_windows_s *prev;
33891 } dispatch_windows;
33892
33893 /* Immediate valuse used in an insn.  */
33894 typedef struct imm_info_s
33895   {
33896     int imm;
33897     int imm32;
33898     int imm64;
33899   } imm_info;
33900
33901 static dispatch_windows *dispatch_window_list;
33902 static dispatch_windows *dispatch_window_list1;
33903
33904 /* Get dispatch group of insn.  */
33905
33906 static enum dispatch_group
33907 get_mem_group (rtx insn)
33908 {
33909   enum attr_memory memory;
33910
33911   if (INSN_CODE (insn) < 0)
33912     return disp_no_group;
33913   memory = get_attr_memory (insn);
33914   if (memory == MEMORY_STORE)
33915     return disp_store;
33916
33917   if (memory == MEMORY_LOAD)
33918     return disp_load;
33919
33920   if (memory == MEMORY_BOTH)
33921     return disp_load_store;
33922
33923   return disp_no_group;
33924 }
33925
33926 /* Return true if insn is a compare instruction.  */
33927
33928 static bool
33929 is_cmp (rtx insn)
33930 {
33931   enum attr_type type;
33932
33933   type = get_attr_type (insn);
33934   return (type == TYPE_TEST
33935           || type == TYPE_ICMP
33936           || type == TYPE_FCMP
33937           || GET_CODE (PATTERN (insn)) == COMPARE);
33938 }
33939
33940 /* Return true if a dispatch violation encountered.  */
33941
33942 static bool
33943 dispatch_violation (void)
33944 {
33945   if (dispatch_window_list->next)
33946     return dispatch_window_list->next->violation;
33947   return dispatch_window_list->violation;
33948 }
33949
33950 /* Return true if insn is a branch instruction.  */
33951
33952 static bool
33953 is_branch (rtx insn)
33954 {
33955   return (CALL_P (insn) || JUMP_P (insn));
33956 }
33957
33958 /* Return true if insn is a prefetch instruction.  */
33959
33960 static bool
33961 is_prefetch (rtx insn)
33962 {
33963   return NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == PREFETCH;
33964 }
33965
33966 /* This function initializes a dispatch window and the list container holding a
33967    pointer to the window.  */
33968
33969 static void
33970 init_window (int window_num)
33971 {
33972   int i;
33973   dispatch_windows *new_list;
33974
33975   if (window_num == 0)
33976     new_list = dispatch_window_list;
33977   else
33978     new_list = dispatch_window_list1;
33979
33980   new_list->num_insn = 0;
33981   new_list->num_uops = 0;
33982   new_list->window_size = 0;
33983   new_list->next = NULL;
33984   new_list->prev = NULL;
33985   new_list->window_num = window_num;
33986   new_list->num_imm = 0;
33987   new_list->num_imm_32 = 0;
33988   new_list->num_imm_64 = 0;
33989   new_list->imm_size = 0;
33990   new_list->num_loads = 0;
33991   new_list->num_stores = 0;
33992   new_list->violation = false;
33993
33994   for (i = 0; i < MAX_INSN; i++)
33995     {
33996       new_list->window[i].insn = NULL;
33997       new_list->window[i].group = disp_no_group;
33998       new_list->window[i].path = no_path;
33999       new_list->window[i].byte_len = 0;
34000       new_list->window[i].imm_bytes = 0;
34001     }
34002   return;
34003 }
34004
34005 /* This function allocates and initializes a dispatch window and the
34006    list container holding a pointer to the window.  */
34007
34008 static dispatch_windows *
34009 allocate_window (void)
34010 {
34011   dispatch_windows *new_list = XNEW (struct dispatch_windows_s);
34012   new_list->window = XNEWVEC (struct sched_insn_info_s, MAX_INSN + 1);
34013
34014   return new_list;
34015 }
34016
34017 /* This routine initializes the dispatch scheduling information.  It
34018    initiates building dispatch scheduler tables and constructs the
34019    first dispatch window.  */
34020
34021 static void
34022 init_dispatch_sched (void)
34023 {
34024   /* Allocate a dispatch list and a window.  */
34025   dispatch_window_list = allocate_window ();
34026   dispatch_window_list1 = allocate_window ();
34027   init_window (0);
34028   init_window (1);
34029 }
34030
34031 /* This function returns true if a branch is detected.  End of a basic block
34032    does not have to be a branch, but here we assume only branches end a
34033    window.  */
34034
34035 static bool
34036 is_end_basic_block (enum dispatch_group group)
34037 {
34038   return group == disp_branch;
34039 }
34040
34041 /* This function is called when the end of a window processing is reached.  */
34042
34043 static void
34044 process_end_window (void)
34045 {
34046   gcc_assert (dispatch_window_list->num_insn <= MAX_INSN);
34047   if (dispatch_window_list->next)
34048     {
34049       gcc_assert (dispatch_window_list1->num_insn <= MAX_INSN);
34050       gcc_assert (dispatch_window_list->window_size
34051                   + dispatch_window_list1->window_size <= 48);
34052       init_window (1);
34053     }
34054   init_window (0);
34055 }
34056
34057 /* Allocates a new dispatch window and adds it to WINDOW_LIST.
34058    WINDOW_NUM is either 0 or 1.  A maximum of two windows are generated
34059    for 48 bytes of instructions.  Note that these windows are not dispatch
34060    windows that their sizes are DISPATCH_WINDOW_SIZE.  */
34061
34062 static dispatch_windows *
34063 allocate_next_window (int window_num)
34064 {
34065   if (window_num == 0)
34066     {
34067       if (dispatch_window_list->next)
34068           init_window (1);
34069       init_window (0);
34070       return dispatch_window_list;
34071     }
34072
34073   dispatch_window_list->next = dispatch_window_list1;
34074   dispatch_window_list1->prev = dispatch_window_list;
34075
34076   return dispatch_window_list1;
34077 }
34078
34079 /* Increment the number of immediate operands of an instruction.  */
34080
34081 static int
34082 find_constant_1 (rtx *in_rtx, imm_info *imm_values)
34083 {
34084   if (*in_rtx == 0)
34085     return 0;
34086
34087     switch ( GET_CODE (*in_rtx))
34088     {
34089     case CONST:
34090     case SYMBOL_REF:
34091     case CONST_INT:
34092       (imm_values->imm)++;
34093       if (x86_64_immediate_operand (*in_rtx, SImode))
34094         (imm_values->imm32)++;
34095       else
34096         (imm_values->imm64)++;
34097       break;
34098
34099     case CONST_DOUBLE:
34100       (imm_values->imm)++;
34101       (imm_values->imm64)++;
34102       break;
34103
34104     case CODE_LABEL:
34105       if (LABEL_KIND (*in_rtx) == LABEL_NORMAL)
34106         {
34107           (imm_values->imm)++;
34108           (imm_values->imm32)++;
34109         }
34110       break;
34111
34112     default:
34113       break;
34114     }
34115
34116   return 0;
34117 }
34118
34119 /* Compute number of immediate operands of an instruction.  */
34120
34121 static void
34122 find_constant (rtx in_rtx, imm_info *imm_values)
34123 {
34124   for_each_rtx (INSN_P (in_rtx) ? &PATTERN (in_rtx) : &in_rtx,
34125                 (rtx_function) find_constant_1, (void *) imm_values);
34126 }
34127
34128 /* Return total size of immediate operands of an instruction along with number
34129    of corresponding immediate-operands.  It initializes its parameters to zero
34130    befor calling FIND_CONSTANT.
34131    INSN is the input instruction.  IMM is the total of immediates.
34132    IMM32 is the number of 32 bit immediates.  IMM64 is the number of 64
34133    bit immediates.  */
34134
34135 static int
34136 get_num_immediates (rtx insn, int *imm, int *imm32, int *imm64)
34137 {
34138   imm_info imm_values = {0, 0, 0};
34139
34140   find_constant (insn, &imm_values);
34141   *imm = imm_values.imm;
34142   *imm32 = imm_values.imm32;
34143   *imm64 = imm_values.imm64;
34144   return imm_values.imm32 * 4 + imm_values.imm64 * 8;
34145 }
34146
34147 /* This function indicates if an operand of an instruction is an
34148    immediate.  */
34149
34150 static bool
34151 has_immediate (rtx insn)
34152 {
34153   int num_imm_operand;
34154   int num_imm32_operand;
34155   int num_imm64_operand;
34156
34157   if (insn)
34158     return get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
34159                                &num_imm64_operand);
34160   return false;
34161 }
34162
34163 /* Return single or double path for instructions.  */
34164
34165 static enum insn_path
34166 get_insn_path (rtx insn)
34167 {
34168   enum attr_amdfam10_decode path = get_attr_amdfam10_decode (insn);
34169
34170   if ((int)path == 0)
34171     return path_single;
34172
34173   if ((int)path == 1)
34174     return path_double;
34175
34176   return path_multi;
34177 }
34178
34179 /* Return insn dispatch group.  */
34180
34181 static enum dispatch_group
34182 get_insn_group (rtx insn)
34183 {
34184   enum dispatch_group group = get_mem_group (insn);
34185   if (group)
34186     return group;
34187
34188   if (is_branch (insn))
34189     return disp_branch;
34190
34191   if (is_cmp (insn))
34192     return disp_cmp;
34193
34194   if (has_immediate (insn))
34195     return disp_imm;
34196
34197   if (is_prefetch (insn))
34198     return disp_prefetch;
34199
34200   return disp_no_group;
34201 }
34202
34203 /* Count number of GROUP restricted instructions in a dispatch
34204    window WINDOW_LIST.  */
34205
34206 static int
34207 count_num_restricted (rtx insn, dispatch_windows *window_list)
34208 {
34209   enum dispatch_group group = get_insn_group (insn);
34210   int imm_size;
34211   int num_imm_operand;
34212   int num_imm32_operand;
34213   int num_imm64_operand;
34214
34215   if (group == disp_no_group)
34216     return 0;
34217
34218   if (group == disp_imm)
34219     {
34220       imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
34221                               &num_imm64_operand);
34222       if (window_list->imm_size + imm_size > MAX_IMM_SIZE
34223           || num_imm_operand + window_list->num_imm > MAX_IMM
34224           || (num_imm32_operand > 0
34225               && (window_list->num_imm_32 + num_imm32_operand > MAX_IMM_32
34226                   || window_list->num_imm_64 * 2 + num_imm32_operand > MAX_IMM_32))
34227           || (num_imm64_operand > 0
34228               && (window_list->num_imm_64 + num_imm64_operand > MAX_IMM_64
34229                   || window_list->num_imm_32 + num_imm64_operand * 2 > MAX_IMM_32))
34230           || (window_list->imm_size + imm_size == MAX_IMM_SIZE
34231               && num_imm64_operand > 0
34232               && ((window_list->num_imm_64 > 0
34233                    && window_list->num_insn >= 2)
34234                   || window_list->num_insn >= 3)))
34235         return BIG;
34236
34237       return 1;
34238     }
34239
34240   if ((group == disp_load_store
34241        && (window_list->num_loads >= MAX_LOAD
34242            || window_list->num_stores >= MAX_STORE))
34243       || ((group == disp_load
34244            || group == disp_prefetch)
34245           && window_list->num_loads >= MAX_LOAD)
34246       || (group == disp_store
34247           && window_list->num_stores >= MAX_STORE))
34248     return BIG;
34249
34250   return 1;
34251 }
34252
34253 /* This function returns true if insn satisfies dispatch rules on the
34254    last window scheduled.  */
34255
34256 static bool
34257 fits_dispatch_window (rtx insn)
34258 {
34259   dispatch_windows *window_list = dispatch_window_list;
34260   dispatch_windows *window_list_next = dispatch_window_list->next;
34261   unsigned int num_restrict;
34262   enum dispatch_group group = get_insn_group (insn);
34263   enum insn_path path = get_insn_path (insn);
34264   int sum;
34265
34266   /* Make disp_cmp and disp_jcc get scheduled at the latest.  These
34267      instructions should be given the lowest priority in the
34268      scheduling process in Haifa scheduler to make sure they will be
34269      scheduled in the same dispatch window as the refrence to them.  */
34270   if (group == disp_jcc || group == disp_cmp)
34271     return false;
34272
34273   /* Check nonrestricted.  */
34274   if (group == disp_no_group || group == disp_branch)
34275     return true;
34276
34277   /* Get last dispatch window.  */
34278   if (window_list_next)
34279     window_list = window_list_next;
34280
34281   if (window_list->window_num == 1)
34282     {
34283       sum = window_list->prev->window_size + window_list->window_size;
34284
34285       if (sum == 32
34286           || (min_insn_size (insn) + sum) >= 48)
34287         /* Window 1 is full.  Go for next window.  */
34288         return true;
34289     }
34290
34291   num_restrict = count_num_restricted (insn, window_list);
34292
34293   if (num_restrict > num_allowable_groups[group])
34294     return false;
34295
34296   /* See if it fits in the first window.  */
34297   if (window_list->window_num == 0)
34298     {
34299       /* The first widow should have only single and double path
34300          uops.  */
34301       if (path == path_double
34302           && (window_list->num_uops + 2) > MAX_INSN)
34303         return false;
34304       else if (path != path_single)
34305         return false;
34306     }
34307   return true;
34308 }
34309
34310 /* Add an instruction INSN with NUM_UOPS micro-operations to the
34311    dispatch window WINDOW_LIST.  */
34312
34313 static void
34314 add_insn_window (rtx insn, dispatch_windows *window_list, int num_uops)
34315 {
34316   int byte_len = min_insn_size (insn);
34317   int num_insn = window_list->num_insn;
34318   int imm_size;
34319   sched_insn_info *window = window_list->window;
34320   enum dispatch_group group = get_insn_group (insn);
34321   enum insn_path path = get_insn_path (insn);
34322   int num_imm_operand;
34323   int num_imm32_operand;
34324   int num_imm64_operand;
34325
34326   if (!window_list->violation && group != disp_cmp
34327       && !fits_dispatch_window (insn))
34328     window_list->violation = true;
34329
34330   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
34331                                  &num_imm64_operand);
34332
34333   /* Initialize window with new instruction.  */
34334   window[num_insn].insn = insn;
34335   window[num_insn].byte_len = byte_len;
34336   window[num_insn].group = group;
34337   window[num_insn].path = path;
34338   window[num_insn].imm_bytes = imm_size;
34339
34340   window_list->window_size += byte_len;
34341   window_list->num_insn = num_insn + 1;
34342   window_list->num_uops = window_list->num_uops + num_uops;
34343   window_list->imm_size += imm_size;
34344   window_list->num_imm += num_imm_operand;
34345   window_list->num_imm_32 += num_imm32_operand;
34346   window_list->num_imm_64 += num_imm64_operand;
34347
34348   if (group == disp_store)
34349     window_list->num_stores += 1;
34350   else if (group == disp_load
34351            || group == disp_prefetch)
34352     window_list->num_loads += 1;
34353   else if (group == disp_load_store)
34354     {
34355       window_list->num_stores += 1;
34356       window_list->num_loads += 1;
34357     }
34358 }
34359
34360 /* Adds a scheduled instruction, INSN, to the current dispatch window.
34361    If the total bytes of instructions or the number of instructions in
34362    the window exceed allowable, it allocates a new window.  */
34363
34364 static void
34365 add_to_dispatch_window (rtx insn)
34366 {
34367   int byte_len;
34368   dispatch_windows *window_list;
34369   dispatch_windows *next_list;
34370   dispatch_windows *window0_list;
34371   enum insn_path path;
34372   enum dispatch_group insn_group;
34373   bool insn_fits;
34374   int num_insn;
34375   int num_uops;
34376   int window_num;
34377   int insn_num_uops;
34378   int sum;
34379
34380   if (INSN_CODE (insn) < 0)
34381     return;
34382
34383   byte_len = min_insn_size (insn);
34384   window_list = dispatch_window_list;
34385   next_list = window_list->next;
34386   path = get_insn_path (insn);
34387   insn_group = get_insn_group (insn);
34388
34389   /* Get the last dispatch window.  */
34390   if (next_list)
34391       window_list = dispatch_window_list->next;
34392
34393   if (path == path_single)
34394     insn_num_uops = 1;
34395   else if (path == path_double)
34396     insn_num_uops = 2;
34397   else
34398     insn_num_uops = (int) path;
34399
34400   /* If current window is full, get a new window.
34401      Window number zero is full, if MAX_INSN uops are scheduled in it.
34402      Window number one is full, if window zero's bytes plus window
34403      one's bytes is 32, or if the bytes of the new instruction added
34404      to the total makes it greater than 48, or it has already MAX_INSN
34405      instructions in it.  */
34406   num_insn = window_list->num_insn;
34407   num_uops = window_list->num_uops;
34408   window_num = window_list->window_num;
34409   insn_fits = fits_dispatch_window (insn);
34410
34411   if (num_insn >= MAX_INSN
34412       || num_uops + insn_num_uops > MAX_INSN
34413       || !(insn_fits))
34414     {
34415       window_num = ~window_num & 1;
34416       window_list = allocate_next_window (window_num);
34417     }
34418
34419   if (window_num == 0)
34420     {
34421       add_insn_window (insn, window_list, insn_num_uops);
34422       if (window_list->num_insn >= MAX_INSN
34423           && insn_group == disp_branch)
34424         {
34425           process_end_window ();
34426           return;
34427         }
34428     }
34429   else if (window_num == 1)
34430     {
34431       window0_list = window_list->prev;
34432       sum = window0_list->window_size + window_list->window_size;
34433       if (sum == 32
34434           || (byte_len + sum) >= 48)
34435         {
34436           process_end_window ();
34437           window_list = dispatch_window_list;
34438         }
34439
34440       add_insn_window (insn, window_list, insn_num_uops);
34441     }
34442   else
34443     gcc_unreachable ();
34444
34445   if (is_end_basic_block (insn_group))
34446     {
34447       /* End of basic block is reached do end-basic-block process.  */
34448       process_end_window ();
34449       return;
34450     }
34451 }
34452
34453 /* Print the dispatch window, WINDOW_NUM, to FILE.  */
34454
34455 DEBUG_FUNCTION static void
34456 debug_dispatch_window_file (FILE *file, int window_num)
34457 {
34458   dispatch_windows *list;
34459   int i;
34460
34461   if (window_num == 0)
34462     list = dispatch_window_list;
34463   else
34464     list = dispatch_window_list1;
34465
34466   fprintf (file, "Window #%d:\n", list->window_num);
34467   fprintf (file, "  num_insn = %d, num_uops = %d, window_size = %d\n",
34468           list->num_insn, list->num_uops, list->window_size);
34469   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
34470            list->num_imm, list->num_imm_32, list->num_imm_64, list->imm_size);
34471
34472   fprintf (file, "  num_loads = %d, num_stores = %d\n", list->num_loads,
34473           list->num_stores);
34474   fprintf (file, " insn info:\n");
34475
34476   for (i = 0; i < MAX_INSN; i++)
34477     {
34478       if (!list->window[i].insn)
34479         break;
34480       fprintf (file, "    group[%d] = %s, insn[%d] = %p, path[%d] = %d byte_len[%d] = %d, imm_bytes[%d] = %d\n",
34481               i, group_name[list->window[i].group],
34482               i, (void *)list->window[i].insn,
34483               i, list->window[i].path,
34484               i, list->window[i].byte_len,
34485               i, list->window[i].imm_bytes);
34486     }
34487 }
34488
34489 /* Print to stdout a dispatch window.  */
34490
34491 DEBUG_FUNCTION void
34492 debug_dispatch_window (int window_num)
34493 {
34494   debug_dispatch_window_file (stdout, window_num);
34495 }
34496
34497 /* Print INSN dispatch information to FILE.  */
34498
34499 DEBUG_FUNCTION static void
34500 debug_insn_dispatch_info_file (FILE *file, rtx insn)
34501 {
34502   int byte_len;
34503   enum insn_path path;
34504   enum dispatch_group group;
34505   int imm_size;
34506   int num_imm_operand;
34507   int num_imm32_operand;
34508   int num_imm64_operand;
34509
34510   if (INSN_CODE (insn) < 0)
34511     return;
34512
34513   byte_len = min_insn_size (insn);
34514   path = get_insn_path (insn);
34515   group = get_insn_group (insn);
34516   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
34517                                  &num_imm64_operand);
34518
34519   fprintf (file, " insn info:\n");
34520   fprintf (file, "  group = %s, path = %d, byte_len = %d\n",
34521            group_name[group], path, byte_len);
34522   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
34523            num_imm_operand, num_imm32_operand, num_imm64_operand, imm_size);
34524 }
34525
34526 /* Print to STDERR the status of the ready list with respect to
34527    dispatch windows.  */
34528
34529 DEBUG_FUNCTION void
34530 debug_ready_dispatch (void)
34531 {
34532   int i;
34533   int no_ready = number_in_ready ();
34534
34535   fprintf (stdout, "Number of ready: %d\n", no_ready);
34536
34537   for (i = 0; i < no_ready; i++)
34538     debug_insn_dispatch_info_file (stdout, get_ready_element (i));
34539 }
34540
34541 /* This routine is the driver of the dispatch scheduler.  */
34542
34543 static void
34544 do_dispatch (rtx insn, int mode)
34545 {
34546   if (mode == DISPATCH_INIT)
34547     init_dispatch_sched ();
34548   else if (mode == ADD_TO_DISPATCH_WINDOW)
34549     add_to_dispatch_window (insn);
34550 }
34551
34552 /* Return TRUE if Dispatch Scheduling is supported.  */
34553
34554 static bool
34555 has_dispatch (rtx insn, int action)
34556 {
34557   if (ix86_tune == PROCESSOR_BDVER1 && flag_dispatch_scheduler)
34558     switch (action)
34559       {
34560       default:
34561         return false;
34562
34563       case IS_DISPATCH_ON:
34564         return true;
34565         break;
34566
34567       case IS_CMP:
34568         return is_cmp (insn);
34569
34570       case DISPATCH_VIOLATION:
34571         return dispatch_violation ();
34572
34573       case FITS_DISPATCH_WINDOW:
34574         return fits_dispatch_window (insn);
34575       }
34576
34577   return false;
34578 }
34579
34580 /* ??? No autovectorization into MMX or 3DNOW until we can reliably
34581    place emms and femms instructions.  */
34582
34583 static enum machine_mode
34584 ix86_preferred_simd_mode (enum machine_mode mode)
34585 {
34586   /* Disable double precision vectorizer if needed.  */
34587   if (mode == DFmode && !TARGET_VECTORIZE_DOUBLE)
34588     return word_mode;
34589
34590   if (!TARGET_AVX && !TARGET_SSE)
34591     return word_mode;
34592
34593   switch (mode)
34594     {
34595     case SFmode:
34596       return TARGET_AVX ? V8SFmode : V4SFmode;
34597     case DFmode:
34598       return TARGET_AVX ? V4DFmode : V2DFmode;
34599     case DImode:
34600       return V2DImode;
34601     case SImode:
34602       return V4SImode;
34603     case HImode:
34604       return V8HImode;
34605     case QImode:
34606       return V16QImode;
34607
34608     default:;
34609     }
34610
34611   return word_mode;
34612 }
34613
34614 /* If AVX is enabled then try vectorizing with both 256bit and 128bit
34615    vectors.  */
34616
34617 static unsigned int
34618 ix86_autovectorize_vector_sizes (void)
34619 {
34620   return TARGET_AVX ? 32 | 16 : 0;
34621 }
34622
34623 /* Initialize the GCC target structure.  */
34624 #undef TARGET_RETURN_IN_MEMORY
34625 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
34626
34627 #undef TARGET_LEGITIMIZE_ADDRESS
34628 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
34629
34630 #undef TARGET_ATTRIBUTE_TABLE
34631 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
34632 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
34633 #  undef TARGET_MERGE_DECL_ATTRIBUTES
34634 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
34635 #endif
34636
34637 #undef TARGET_COMP_TYPE_ATTRIBUTES
34638 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
34639
34640 #undef TARGET_INIT_BUILTINS
34641 #define TARGET_INIT_BUILTINS ix86_init_builtins
34642 #undef TARGET_BUILTIN_DECL
34643 #define TARGET_BUILTIN_DECL ix86_builtin_decl
34644 #undef TARGET_EXPAND_BUILTIN
34645 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
34646
34647 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
34648 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
34649   ix86_builtin_vectorized_function
34650
34651 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
34652 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_vectorize_builtin_conversion
34653
34654 #undef TARGET_BUILTIN_RECIPROCAL
34655 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
34656
34657 #undef TARGET_ASM_FUNCTION_EPILOGUE
34658 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
34659
34660 #undef TARGET_ENCODE_SECTION_INFO
34661 #ifndef SUBTARGET_ENCODE_SECTION_INFO
34662 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
34663 #else
34664 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
34665 #endif
34666
34667 #undef TARGET_ASM_OPEN_PAREN
34668 #define TARGET_ASM_OPEN_PAREN ""
34669 #undef TARGET_ASM_CLOSE_PAREN
34670 #define TARGET_ASM_CLOSE_PAREN ""
34671
34672 #undef TARGET_ASM_BYTE_OP
34673 #define TARGET_ASM_BYTE_OP ASM_BYTE
34674
34675 #undef TARGET_ASM_ALIGNED_HI_OP
34676 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
34677 #undef TARGET_ASM_ALIGNED_SI_OP
34678 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
34679 #ifdef ASM_QUAD
34680 #undef TARGET_ASM_ALIGNED_DI_OP
34681 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
34682 #endif
34683
34684 #undef TARGET_PROFILE_BEFORE_PROLOGUE
34685 #define TARGET_PROFILE_BEFORE_PROLOGUE ix86_profile_before_prologue
34686
34687 #undef TARGET_ASM_UNALIGNED_HI_OP
34688 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
34689 #undef TARGET_ASM_UNALIGNED_SI_OP
34690 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
34691 #undef TARGET_ASM_UNALIGNED_DI_OP
34692 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
34693
34694 #undef TARGET_PRINT_OPERAND
34695 #define TARGET_PRINT_OPERAND ix86_print_operand
34696 #undef TARGET_PRINT_OPERAND_ADDRESS
34697 #define TARGET_PRINT_OPERAND_ADDRESS ix86_print_operand_address
34698 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
34699 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ix86_print_operand_punct_valid_p
34700 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
34701 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA i386_asm_output_addr_const_extra 
34702
34703 #undef TARGET_SCHED_INIT_GLOBAL
34704 #define TARGET_SCHED_INIT_GLOBAL ix86_sched_init_global
34705 #undef TARGET_SCHED_ADJUST_COST
34706 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
34707 #undef TARGET_SCHED_ISSUE_RATE
34708 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
34709 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
34710 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
34711   ia32_multipass_dfa_lookahead
34712
34713 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
34714 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
34715
34716 #ifdef HAVE_AS_TLS
34717 #undef TARGET_HAVE_TLS
34718 #define TARGET_HAVE_TLS true
34719 #endif
34720 #undef TARGET_CANNOT_FORCE_CONST_MEM
34721 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
34722 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
34723 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
34724
34725 #undef TARGET_DELEGITIMIZE_ADDRESS
34726 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
34727
34728 #undef TARGET_MS_BITFIELD_LAYOUT_P
34729 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
34730
34731 #if TARGET_MACHO
34732 #undef TARGET_BINDS_LOCAL_P
34733 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
34734 #endif
34735 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
34736 #undef TARGET_BINDS_LOCAL_P
34737 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
34738 #endif
34739
34740 #undef TARGET_ASM_OUTPUT_MI_THUNK
34741 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
34742 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
34743 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
34744
34745 #undef TARGET_ASM_FILE_START
34746 #define TARGET_ASM_FILE_START x86_file_start
34747
34748 #undef TARGET_DEFAULT_TARGET_FLAGS
34749 #define TARGET_DEFAULT_TARGET_FLAGS     \
34750   (TARGET_DEFAULT                       \
34751    | TARGET_SUBTARGET_DEFAULT           \
34752    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
34753
34754 #undef TARGET_HANDLE_OPTION
34755 #define TARGET_HANDLE_OPTION ix86_handle_option
34756
34757 #undef TARGET_OPTION_OVERRIDE
34758 #define TARGET_OPTION_OVERRIDE ix86_option_override
34759 #undef TARGET_OPTION_OPTIMIZATION_TABLE
34760 #define TARGET_OPTION_OPTIMIZATION_TABLE ix86_option_optimization_table
34761 #undef TARGET_OPTION_INIT_STRUCT
34762 #define TARGET_OPTION_INIT_STRUCT ix86_option_init_struct
34763
34764 #undef TARGET_REGISTER_MOVE_COST
34765 #define TARGET_REGISTER_MOVE_COST ix86_register_move_cost
34766 #undef TARGET_MEMORY_MOVE_COST
34767 #define TARGET_MEMORY_MOVE_COST ix86_memory_move_cost
34768 #undef TARGET_RTX_COSTS
34769 #define TARGET_RTX_COSTS ix86_rtx_costs
34770 #undef TARGET_ADDRESS_COST
34771 #define TARGET_ADDRESS_COST ix86_address_cost
34772
34773 #undef TARGET_FIXED_CONDITION_CODE_REGS
34774 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
34775 #undef TARGET_CC_MODES_COMPATIBLE
34776 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
34777
34778 #undef TARGET_MACHINE_DEPENDENT_REORG
34779 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
34780
34781 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
34782 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
34783
34784 #undef TARGET_BUILD_BUILTIN_VA_LIST
34785 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
34786
34787 #undef TARGET_ENUM_VA_LIST_P
34788 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
34789
34790 #undef TARGET_FN_ABI_VA_LIST
34791 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
34792
34793 #undef TARGET_CANONICAL_VA_LIST_TYPE
34794 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
34795
34796 #undef TARGET_EXPAND_BUILTIN_VA_START
34797 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
34798
34799 #undef TARGET_MD_ASM_CLOBBERS
34800 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
34801
34802 #undef TARGET_PROMOTE_PROTOTYPES
34803 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
34804 #undef TARGET_STRUCT_VALUE_RTX
34805 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
34806 #undef TARGET_SETUP_INCOMING_VARARGS
34807 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
34808 #undef TARGET_MUST_PASS_IN_STACK
34809 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
34810 #undef TARGET_FUNCTION_ARG_ADVANCE
34811 #define TARGET_FUNCTION_ARG_ADVANCE ix86_function_arg_advance
34812 #undef TARGET_FUNCTION_ARG
34813 #define TARGET_FUNCTION_ARG ix86_function_arg
34814 #undef TARGET_FUNCTION_ARG_BOUNDARY
34815 #define TARGET_FUNCTION_ARG_BOUNDARY ix86_function_arg_boundary
34816 #undef TARGET_PASS_BY_REFERENCE
34817 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
34818 #undef TARGET_INTERNAL_ARG_POINTER
34819 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
34820 #undef TARGET_UPDATE_STACK_BOUNDARY
34821 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
34822 #undef TARGET_GET_DRAP_RTX
34823 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
34824 #undef TARGET_STRICT_ARGUMENT_NAMING
34825 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
34826 #undef TARGET_STATIC_CHAIN
34827 #define TARGET_STATIC_CHAIN ix86_static_chain
34828 #undef TARGET_TRAMPOLINE_INIT
34829 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
34830 #undef TARGET_RETURN_POPS_ARGS
34831 #define TARGET_RETURN_POPS_ARGS ix86_return_pops_args
34832
34833 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
34834 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
34835
34836 #undef TARGET_SCALAR_MODE_SUPPORTED_P
34837 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
34838
34839 #undef TARGET_VECTOR_MODE_SUPPORTED_P
34840 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
34841
34842 #undef TARGET_C_MODE_FOR_SUFFIX
34843 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
34844
34845 #ifdef HAVE_AS_TLS
34846 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
34847 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
34848 #endif
34849
34850 #ifdef SUBTARGET_INSERT_ATTRIBUTES
34851 #undef TARGET_INSERT_ATTRIBUTES
34852 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
34853 #endif
34854
34855 #undef TARGET_MANGLE_TYPE
34856 #define TARGET_MANGLE_TYPE ix86_mangle_type
34857
34858 #undef TARGET_STACK_PROTECT_FAIL
34859 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
34860
34861 #undef TARGET_SUPPORTS_SPLIT_STACK
34862 #define TARGET_SUPPORTS_SPLIT_STACK ix86_supports_split_stack
34863
34864 #undef TARGET_FUNCTION_VALUE
34865 #define TARGET_FUNCTION_VALUE ix86_function_value
34866
34867 #undef TARGET_FUNCTION_VALUE_REGNO_P
34868 #define TARGET_FUNCTION_VALUE_REGNO_P ix86_function_value_regno_p
34869
34870 #undef TARGET_SECONDARY_RELOAD
34871 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
34872
34873 #undef TARGET_PREFERRED_RELOAD_CLASS
34874 #define TARGET_PREFERRED_RELOAD_CLASS ix86_preferred_reload_class
34875 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
34876 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS ix86_preferred_output_reload_class
34877 #undef TARGET_CLASS_LIKELY_SPILLED_P
34878 #define TARGET_CLASS_LIKELY_SPILLED_P ix86_class_likely_spilled_p
34879
34880 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
34881 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
34882   ix86_builtin_vectorization_cost
34883 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM
34884 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM \
34885   ix86_vectorize_builtin_vec_perm
34886 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK
34887 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK \
34888   ix86_vectorize_builtin_vec_perm_ok
34889 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
34890 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
34891   ix86_preferred_simd_mode
34892 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
34893 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
34894   ix86_autovectorize_vector_sizes
34895
34896 #undef TARGET_SET_CURRENT_FUNCTION
34897 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
34898
34899 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
34900 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
34901
34902 #undef TARGET_OPTION_SAVE
34903 #define TARGET_OPTION_SAVE ix86_function_specific_save
34904
34905 #undef TARGET_OPTION_RESTORE
34906 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
34907
34908 #undef TARGET_OPTION_PRINT
34909 #define TARGET_OPTION_PRINT ix86_function_specific_print
34910
34911 #undef TARGET_CAN_INLINE_P
34912 #define TARGET_CAN_INLINE_P ix86_can_inline_p
34913
34914 #undef TARGET_EXPAND_TO_RTL_HOOK
34915 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
34916
34917 #undef TARGET_LEGITIMATE_ADDRESS_P
34918 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
34919
34920 #undef TARGET_IRA_COVER_CLASSES
34921 #define TARGET_IRA_COVER_CLASSES i386_ira_cover_classes
34922
34923 #undef TARGET_FRAME_POINTER_REQUIRED
34924 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
34925
34926 #undef TARGET_CAN_ELIMINATE
34927 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
34928
34929 #undef TARGET_EXTRA_LIVE_ON_ENTRY
34930 #define TARGET_EXTRA_LIVE_ON_ENTRY ix86_live_on_entry
34931
34932 #undef TARGET_ASM_CODE_END
34933 #define TARGET_ASM_CODE_END ix86_code_end
34934
34935 #undef TARGET_CONDITIONAL_REGISTER_USAGE
34936 #define TARGET_CONDITIONAL_REGISTER_USAGE ix86_conditional_register_usage
34937
34938 struct gcc_target targetm = TARGET_INITIALIZER;
34939 \f
34940 #include "gt-i386.h"