OSDN Git Service

bda8ed3f9cd5abc4fb51c80cab68c04afcd85c8d
[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
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 typedef struct block_info_def
61 {
62   /* TRUE if the upper 128bits of any AVX registers are live at exit.  */
63   bool upper_128bits_set;
64   /* TRUE if block has been processed.  */
65   bool done;
66 } *block_info;
67
68 #define BLOCK_INFO(B)   ((block_info) (B)->aux)
69
70 enum call_avx256_state
71 {
72   /* Callee returns 256bit AVX register.  */
73   callee_return_avx256 = -1,
74   /* Callee returns and passes 256bit AVX register.  */
75   callee_return_pass_avx256,
76   /* Callee passes 256bit AVX register.  */
77   callee_pass_avx256,
78   /* Callee doesn't return nor passe 256bit AVX register, or no
79      256bit AVX register in function return.  */
80   call_no_avx256,
81   /* vzeroupper intrinsic.  */
82   vzeroupper_intrinsic
83 };
84
85 /* Check if a 256bit AVX register is referenced in stores.   */
86
87 static void
88 check_avx256_stores (rtx dest, const_rtx set, void *data)
89 {
90   if ((REG_P (dest)
91        && VALID_AVX256_REG_MODE (GET_MODE (dest)))
92       || (GET_CODE (set) == SET
93           && REG_P (SET_SRC (set))
94           && VALID_AVX256_REG_MODE (GET_MODE (SET_SRC (set)))))
95     {
96       bool *upper_128bits_set = (bool *) data;
97       *upper_128bits_set = true;
98     }
99 }
100
101 /* Helper function for move_or_delete_vzeroupper_1.  Look for vzeroupper
102    in basic block BB.  Delete it if upper 128bit AVX registers are
103    unused.  If it isn't deleted, move it to just before a jump insn.
104    
105    UPPER_128BITS_LIVE is TRUE if the upper 128bits of any AVX registers
106    are live at entry.  */
107
108 static void
109 move_or_delete_vzeroupper_2 (basic_block bb, bool upper_128bits_set)
110 {
111   rtx curr_insn, next_insn, prev_insn, insn;
112
113   if (dump_file)
114     fprintf (dump_file, " BB [%i] entry: upper 128bits: %d\n",
115              bb->index, upper_128bits_set);
116
117   for (curr_insn = BB_HEAD (bb);
118        curr_insn && curr_insn != NEXT_INSN (BB_END (bb));
119        curr_insn = next_insn)
120     {
121       int avx256;
122
123       next_insn = NEXT_INSN (curr_insn);
124
125       if (!NONDEBUG_INSN_P (curr_insn))
126         continue;
127
128       /* Search for vzeroupper.  */
129       insn = PATTERN (curr_insn);
130       if (GET_CODE (insn) == UNSPEC_VOLATILE
131           && XINT (insn, 1) == UNSPECV_VZEROUPPER)
132         {
133           /* Found vzeroupper.  */
134           if (dump_file)
135             {
136               fprintf (dump_file, "Found vzeroupper:\n");
137               print_rtl_single (dump_file, curr_insn);
138             }
139         }
140       else
141         {
142           /* Check vzeroall intrinsic.  */
143           if (GET_CODE (insn) == PARALLEL
144               && GET_CODE (XVECEXP (insn, 0, 0)) == UNSPEC_VOLATILE
145               && XINT (XVECEXP (insn, 0, 0), 1) == UNSPECV_VZEROALL)
146             upper_128bits_set = false;
147           else if (!upper_128bits_set)
148             {
149               /* Check if upper 128bits of AVX registers are used.  */
150               note_stores (insn, check_avx256_stores,
151                            &upper_128bits_set);
152             }
153           continue;
154         }
155
156       avx256 = INTVAL (XVECEXP (insn, 0, 0));
157
158       if (!upper_128bits_set)
159         {
160           /* Since the upper 128bits are cleared, callee must not pass
161              256bit AVX register.  We only need to check if callee
162              returns 256bit AVX register.  */
163           upper_128bits_set = avx256 == callee_return_avx256;
164
165           /* Remove unnecessary vzeroupper since upper 128bits are
166              cleared.  */
167           if (dump_file)
168             {
169               fprintf (dump_file, "Delete redundant vzeroupper:\n");
170               print_rtl_single (dump_file, curr_insn);
171             }
172           delete_insn (curr_insn);
173           continue;
174         }
175       else if (avx256 == callee_return_pass_avx256
176                || avx256 == callee_pass_avx256)
177         {
178           /* Callee passes 256bit AVX register.  Check if callee
179              returns 256bit AVX register.  */
180           upper_128bits_set = avx256 == callee_return_pass_avx256;
181
182           /* Must remove vzeroupper since callee passes 256bit AVX
183              register.  */
184           if (dump_file)
185             {
186               fprintf (dump_file, "Delete callee pass vzeroupper:\n");
187               print_rtl_single (dump_file, curr_insn);
188             }
189           delete_insn (curr_insn);
190           continue;
191         }
192
193       /* Find the jump after vzeroupper.  */
194       prev_insn = curr_insn;
195       if (avx256 == vzeroupper_intrinsic)
196         {
197           /* For vzeroupper intrinsic, check if there is another
198              vzeroupper.  */
199           insn = NEXT_INSN (curr_insn);
200           while (insn)
201             {
202               if (NONJUMP_INSN_P (insn)
203                   && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
204                   && XINT (PATTERN (insn), 1) == UNSPECV_VZEROUPPER)
205                 {
206                   if (dump_file)
207                     {
208                       fprintf (dump_file,
209                                "Delete redundant vzeroupper intrinsic:\n");
210                       print_rtl_single (dump_file, curr_insn);
211                     }
212                   delete_insn (curr_insn);
213                   insn = NULL;
214                   continue;
215                 }
216
217               if (JUMP_P (insn) || CALL_P (insn))
218                 break;
219               prev_insn = insn;
220               insn = NEXT_INSN (insn);
221               if (insn == NEXT_INSN (BB_END (bb)))
222                 break;
223             }
224
225           /* Continue if redundant vzeroupper intrinsic is deleted.  */
226           if (!insn)
227             continue;
228         }
229       else
230         {
231           /* Find the next jump/call.  */
232           insn = NEXT_INSN (curr_insn);
233           while (insn)
234             {
235               if (JUMP_P (insn) || CALL_P (insn))
236                 break;
237               prev_insn = insn;
238               insn = NEXT_INSN (insn);
239               if (insn == NEXT_INSN (BB_END (bb)))
240                 break;
241             }
242
243           if (!insn)
244             gcc_unreachable();
245         }
246
247       /* Keep vzeroupper.  */
248       upper_128bits_set = false;
249
250       /* Also allow label as the next instruction.  */
251       if (insn == NEXT_INSN (BB_END (bb)) && !LABEL_P (insn))
252         gcc_unreachable();
253
254       /* Move vzeroupper before jump/call if neeeded.  */
255       if (curr_insn != prev_insn)
256         {
257           reorder_insns_nobb (curr_insn, curr_insn, prev_insn);
258           if (dump_file)
259             {
260               fprintf (dump_file, "Move vzeroupper after:\n");
261               print_rtl_single (dump_file, prev_insn);
262               fprintf (dump_file, "before:\n");
263               print_rtl_single (dump_file, insn);
264             }
265         }
266
267       next_insn = NEXT_INSN (insn);
268     }
269
270   BLOCK_INFO (bb)->upper_128bits_set = upper_128bits_set;
271
272   if (dump_file)
273     fprintf (dump_file, " BB [%i] exit: upper 128bits: %d\n",
274              bb->index, upper_128bits_set);
275 }
276
277 /* Helper function for move_or_delete_vzeroupper.  Process vzeroupper
278    in BLOCK and its predecessor blocks recursively.  */
279
280 static void
281 move_or_delete_vzeroupper_1 (basic_block block)
282 {
283   edge e;
284   edge_iterator ei;
285   bool upper_128bits_set;
286
287   if (dump_file)
288     fprintf (dump_file, " Process BB [%i]: status: %d\n",
289              block->index, BLOCK_INFO (block)->done);
290
291   if (BLOCK_INFO (block)->done)
292     return;
293
294   BLOCK_INFO (block)->done = true;
295
296   upper_128bits_set = false;
297
298   /* Process all predecessor edges of this block.  */
299   FOR_EACH_EDGE (e, ei, block->preds)
300     {
301       if (e->src == block)
302         continue;
303       move_or_delete_vzeroupper_1 (e->src);
304       if (BLOCK_INFO (e->src)->upper_128bits_set)
305         upper_128bits_set = true;
306     }
307
308   /* Process this block.  */
309   move_or_delete_vzeroupper_2 (block, upper_128bits_set);
310 }
311
312 /* Go through the instruction stream looking for vzeroupper.  Delete
313    it if upper 128bit AVX registers are unused.  If it isn't deleted,
314    move it to just before a jump insn.  */
315
316 static void
317 move_or_delete_vzeroupper (void)
318 {
319   edge e;
320   edge_iterator ei;
321
322   /* Set up block info for each basic block.  */
323   alloc_aux_for_blocks (sizeof (struct block_info_def));
324
325   /* Process successor blocks of all entry points.  */
326   if (dump_file)
327     fprintf (dump_file, "Process all entry points\n");
328
329   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
330     {
331       move_or_delete_vzeroupper_2 (e->dest,
332                                    cfun->machine->caller_pass_avx256_p);
333       BLOCK_INFO (e->dest)->done = true;
334     }
335
336   /* Process predecessor blocks of all exit points.  */
337   if (dump_file)
338     fprintf (dump_file, "Process all exit points\n");
339
340   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
341     move_or_delete_vzeroupper_1 (e->src);
342
343   free_aux_for_blocks ();
344 }
345
346 static rtx legitimize_dllimport_symbol (rtx, bool);
347
348 #ifndef CHECK_STACK_LIMIT
349 #define CHECK_STACK_LIMIT (-1)
350 #endif
351
352 /* Return index of given mode in mult and division cost tables.  */
353 #define MODE_INDEX(mode)                                        \
354   ((mode) == QImode ? 0                                         \
355    : (mode) == HImode ? 1                                       \
356    : (mode) == SImode ? 2                                       \
357    : (mode) == DImode ? 3                                       \
358    : 4)
359
360 /* Processor costs (relative to an add) */
361 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
362 #define COSTS_N_BYTES(N) ((N) * 2)
363
364 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
365
366 const
367 struct processor_costs ix86_size_cost = {/* costs for tuning for size */
368   COSTS_N_BYTES (2),                    /* cost of an add instruction */
369   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
370   COSTS_N_BYTES (2),                    /* variable shift costs */
371   COSTS_N_BYTES (3),                    /* constant shift costs */
372   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
373    COSTS_N_BYTES (3),                   /*                               HI */
374    COSTS_N_BYTES (3),                   /*                               SI */
375    COSTS_N_BYTES (3),                   /*                               DI */
376    COSTS_N_BYTES (5)},                  /*                            other */
377   0,                                    /* cost of multiply per each bit set */
378   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
379    COSTS_N_BYTES (3),                   /*                          HI */
380    COSTS_N_BYTES (3),                   /*                          SI */
381    COSTS_N_BYTES (3),                   /*                          DI */
382    COSTS_N_BYTES (5)},                  /*                          other */
383   COSTS_N_BYTES (3),                    /* cost of movsx */
384   COSTS_N_BYTES (3),                    /* cost of movzx */
385   0,                                    /* "large" insn */
386   2,                                    /* MOVE_RATIO */
387   2,                                 /* cost for loading QImode using movzbl */
388   {2, 2, 2},                            /* cost of loading integer registers
389                                            in QImode, HImode and SImode.
390                                            Relative to reg-reg move (2).  */
391   {2, 2, 2},                            /* cost of storing integer registers */
392   2,                                    /* cost of reg,reg fld/fst */
393   {2, 2, 2},                            /* cost of loading fp registers
394                                            in SFmode, DFmode and XFmode */
395   {2, 2, 2},                            /* cost of storing fp registers
396                                            in SFmode, DFmode and XFmode */
397   3,                                    /* cost of moving MMX register */
398   {3, 3},                               /* cost of loading MMX registers
399                                            in SImode and DImode */
400   {3, 3},                               /* cost of storing MMX registers
401                                            in SImode and DImode */
402   3,                                    /* cost of moving SSE register */
403   {3, 3, 3},                            /* cost of loading SSE registers
404                                            in SImode, DImode and TImode */
405   {3, 3, 3},                            /* cost of storing SSE registers
406                                            in SImode, DImode and TImode */
407   3,                                    /* MMX or SSE register to integer */
408   0,                                    /* size of l1 cache  */
409   0,                                    /* size of l2 cache  */
410   0,                                    /* size of prefetch block */
411   0,                                    /* number of parallel prefetches */
412   2,                                    /* Branch cost */
413   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
414   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
415   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
416   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
417   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
418   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
419   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
420    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
421   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
422    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
423   1,                                    /* scalar_stmt_cost.  */
424   1,                                    /* scalar load_cost.  */
425   1,                                    /* scalar_store_cost.  */
426   1,                                    /* vec_stmt_cost.  */
427   1,                                    /* vec_to_scalar_cost.  */
428   1,                                    /* scalar_to_vec_cost.  */
429   1,                                    /* vec_align_load_cost.  */
430   1,                                    /* vec_unalign_load_cost.  */
431   1,                                    /* vec_store_cost.  */
432   1,                                    /* cond_taken_branch_cost.  */
433   1,                                    /* cond_not_taken_branch_cost.  */
434 };
435
436 /* Processor costs (relative to an add) */
437 static const
438 struct processor_costs i386_cost = {    /* 386 specific costs */
439   COSTS_N_INSNS (1),                    /* cost of an add instruction */
440   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
441   COSTS_N_INSNS (3),                    /* variable shift costs */
442   COSTS_N_INSNS (2),                    /* constant shift costs */
443   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
444    COSTS_N_INSNS (6),                   /*                               HI */
445    COSTS_N_INSNS (6),                   /*                               SI */
446    COSTS_N_INSNS (6),                   /*                               DI */
447    COSTS_N_INSNS (6)},                  /*                            other */
448   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
449   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
450    COSTS_N_INSNS (23),                  /*                          HI */
451    COSTS_N_INSNS (23),                  /*                          SI */
452    COSTS_N_INSNS (23),                  /*                          DI */
453    COSTS_N_INSNS (23)},                 /*                          other */
454   COSTS_N_INSNS (3),                    /* cost of movsx */
455   COSTS_N_INSNS (2),                    /* cost of movzx */
456   15,                                   /* "large" insn */
457   3,                                    /* MOVE_RATIO */
458   4,                                 /* cost for loading QImode using movzbl */
459   {2, 4, 2},                            /* cost of loading integer registers
460                                            in QImode, HImode and SImode.
461                                            Relative to reg-reg move (2).  */
462   {2, 4, 2},                            /* cost of storing integer registers */
463   2,                                    /* cost of reg,reg fld/fst */
464   {8, 8, 8},                            /* cost of loading fp registers
465                                            in SFmode, DFmode and XFmode */
466   {8, 8, 8},                            /* cost of storing fp registers
467                                            in SFmode, DFmode and XFmode */
468   2,                                    /* cost of moving MMX register */
469   {4, 8},                               /* cost of loading MMX registers
470                                            in SImode and DImode */
471   {4, 8},                               /* cost of storing MMX registers
472                                            in SImode and DImode */
473   2,                                    /* cost of moving SSE register */
474   {4, 8, 16},                           /* cost of loading SSE registers
475                                            in SImode, DImode and TImode */
476   {4, 8, 16},                           /* cost of storing SSE registers
477                                            in SImode, DImode and TImode */
478   3,                                    /* MMX or SSE register to integer */
479   0,                                    /* size of l1 cache  */
480   0,                                    /* size of l2 cache  */
481   0,                                    /* size of prefetch block */
482   0,                                    /* number of parallel prefetches */
483   1,                                    /* Branch cost */
484   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
485   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
486   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
487   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
488   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
489   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
490   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
491    DUMMY_STRINGOP_ALGS},
492   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
493    DUMMY_STRINGOP_ALGS},
494   1,                                    /* scalar_stmt_cost.  */
495   1,                                    /* scalar load_cost.  */
496   1,                                    /* scalar_store_cost.  */
497   1,                                    /* vec_stmt_cost.  */
498   1,                                    /* vec_to_scalar_cost.  */
499   1,                                    /* scalar_to_vec_cost.  */
500   1,                                    /* vec_align_load_cost.  */
501   2,                                    /* vec_unalign_load_cost.  */
502   1,                                    /* vec_store_cost.  */
503   3,                                    /* cond_taken_branch_cost.  */
504   1,                                    /* cond_not_taken_branch_cost.  */
505 };
506
507 static const
508 struct processor_costs i486_cost = {    /* 486 specific costs */
509   COSTS_N_INSNS (1),                    /* cost of an add instruction */
510   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
511   COSTS_N_INSNS (3),                    /* variable shift costs */
512   COSTS_N_INSNS (2),                    /* constant shift costs */
513   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
514    COSTS_N_INSNS (12),                  /*                               HI */
515    COSTS_N_INSNS (12),                  /*                               SI */
516    COSTS_N_INSNS (12),                  /*                               DI */
517    COSTS_N_INSNS (12)},                 /*                            other */
518   1,                                    /* cost of multiply per each bit set */
519   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
520    COSTS_N_INSNS (40),                  /*                          HI */
521    COSTS_N_INSNS (40),                  /*                          SI */
522    COSTS_N_INSNS (40),                  /*                          DI */
523    COSTS_N_INSNS (40)},                 /*                          other */
524   COSTS_N_INSNS (3),                    /* cost of movsx */
525   COSTS_N_INSNS (2),                    /* cost of movzx */
526   15,                                   /* "large" insn */
527   3,                                    /* MOVE_RATIO */
528   4,                                 /* cost for loading QImode using movzbl */
529   {2, 4, 2},                            /* cost of loading integer registers
530                                            in QImode, HImode and SImode.
531                                            Relative to reg-reg move (2).  */
532   {2, 4, 2},                            /* cost of storing integer registers */
533   2,                                    /* cost of reg,reg fld/fst */
534   {8, 8, 8},                            /* cost of loading fp registers
535                                            in SFmode, DFmode and XFmode */
536   {8, 8, 8},                            /* cost of storing fp registers
537                                            in SFmode, DFmode and XFmode */
538   2,                                    /* cost of moving MMX register */
539   {4, 8},                               /* cost of loading MMX registers
540                                            in SImode and DImode */
541   {4, 8},                               /* cost of storing MMX registers
542                                            in SImode and DImode */
543   2,                                    /* cost of moving SSE register */
544   {4, 8, 16},                           /* cost of loading SSE registers
545                                            in SImode, DImode and TImode */
546   {4, 8, 16},                           /* cost of storing SSE registers
547                                            in SImode, DImode and TImode */
548   3,                                    /* MMX or SSE register to integer */
549   4,                                    /* size of l1 cache.  486 has 8kB cache
550                                            shared for code and data, so 4kB is
551                                            not really precise.  */
552   4,                                    /* size of l2 cache  */
553   0,                                    /* size of prefetch block */
554   0,                                    /* number of parallel prefetches */
555   1,                                    /* Branch cost */
556   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
557   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
558   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
559   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
560   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
561   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
562   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
563    DUMMY_STRINGOP_ALGS},
564   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
565    DUMMY_STRINGOP_ALGS},
566   1,                                    /* scalar_stmt_cost.  */
567   1,                                    /* scalar load_cost.  */
568   1,                                    /* scalar_store_cost.  */
569   1,                                    /* vec_stmt_cost.  */
570   1,                                    /* vec_to_scalar_cost.  */
571   1,                                    /* scalar_to_vec_cost.  */
572   1,                                    /* vec_align_load_cost.  */
573   2,                                    /* vec_unalign_load_cost.  */
574   1,                                    /* vec_store_cost.  */
575   3,                                    /* cond_taken_branch_cost.  */
576   1,                                    /* cond_not_taken_branch_cost.  */
577 };
578
579 static const
580 struct processor_costs pentium_cost = {
581   COSTS_N_INSNS (1),                    /* cost of an add instruction */
582   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
583   COSTS_N_INSNS (4),                    /* variable shift costs */
584   COSTS_N_INSNS (1),                    /* constant shift costs */
585   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
586    COSTS_N_INSNS (11),                  /*                               HI */
587    COSTS_N_INSNS (11),                  /*                               SI */
588    COSTS_N_INSNS (11),                  /*                               DI */
589    COSTS_N_INSNS (11)},                 /*                            other */
590   0,                                    /* cost of multiply per each bit set */
591   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
592    COSTS_N_INSNS (25),                  /*                          HI */
593    COSTS_N_INSNS (25),                  /*                          SI */
594    COSTS_N_INSNS (25),                  /*                          DI */
595    COSTS_N_INSNS (25)},                 /*                          other */
596   COSTS_N_INSNS (3),                    /* cost of movsx */
597   COSTS_N_INSNS (2),                    /* cost of movzx */
598   8,                                    /* "large" insn */
599   6,                                    /* MOVE_RATIO */
600   6,                                 /* cost for loading QImode using movzbl */
601   {2, 4, 2},                            /* cost of loading integer registers
602                                            in QImode, HImode and SImode.
603                                            Relative to reg-reg move (2).  */
604   {2, 4, 2},                            /* cost of storing integer registers */
605   2,                                    /* cost of reg,reg fld/fst */
606   {2, 2, 6},                            /* cost of loading fp registers
607                                            in SFmode, DFmode and XFmode */
608   {4, 4, 6},                            /* cost of storing fp registers
609                                            in SFmode, DFmode and XFmode */
610   8,                                    /* cost of moving MMX register */
611   {8, 8},                               /* cost of loading MMX registers
612                                            in SImode and DImode */
613   {8, 8},                               /* cost of storing MMX registers
614                                            in SImode and DImode */
615   2,                                    /* cost of moving SSE register */
616   {4, 8, 16},                           /* cost of loading SSE registers
617                                            in SImode, DImode and TImode */
618   {4, 8, 16},                           /* cost of storing SSE registers
619                                            in SImode, DImode and TImode */
620   3,                                    /* MMX or SSE register to integer */
621   8,                                    /* size of l1 cache.  */
622   8,                                    /* size of l2 cache  */
623   0,                                    /* size of prefetch block */
624   0,                                    /* number of parallel prefetches */
625   2,                                    /* Branch cost */
626   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
627   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
628   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
629   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
630   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
631   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
632   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
633    DUMMY_STRINGOP_ALGS},
634   {{libcall, {{-1, rep_prefix_4_byte}}},
635    DUMMY_STRINGOP_ALGS},
636   1,                                    /* scalar_stmt_cost.  */
637   1,                                    /* scalar load_cost.  */
638   1,                                    /* scalar_store_cost.  */
639   1,                                    /* vec_stmt_cost.  */
640   1,                                    /* vec_to_scalar_cost.  */
641   1,                                    /* scalar_to_vec_cost.  */
642   1,                                    /* vec_align_load_cost.  */
643   2,                                    /* vec_unalign_load_cost.  */
644   1,                                    /* vec_store_cost.  */
645   3,                                    /* cond_taken_branch_cost.  */
646   1,                                    /* cond_not_taken_branch_cost.  */
647 };
648
649 static const
650 struct processor_costs pentiumpro_cost = {
651   COSTS_N_INSNS (1),                    /* cost of an add instruction */
652   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
653   COSTS_N_INSNS (1),                    /* variable shift costs */
654   COSTS_N_INSNS (1),                    /* constant shift costs */
655   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
656    COSTS_N_INSNS (4),                   /*                               HI */
657    COSTS_N_INSNS (4),                   /*                               SI */
658    COSTS_N_INSNS (4),                   /*                               DI */
659    COSTS_N_INSNS (4)},                  /*                            other */
660   0,                                    /* cost of multiply per each bit set */
661   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
662    COSTS_N_INSNS (17),                  /*                          HI */
663    COSTS_N_INSNS (17),                  /*                          SI */
664    COSTS_N_INSNS (17),                  /*                          DI */
665    COSTS_N_INSNS (17)},                 /*                          other */
666   COSTS_N_INSNS (1),                    /* cost of movsx */
667   COSTS_N_INSNS (1),                    /* cost of movzx */
668   8,                                    /* "large" insn */
669   6,                                    /* MOVE_RATIO */
670   2,                                 /* cost for loading QImode using movzbl */
671   {4, 4, 4},                            /* cost of loading integer registers
672                                            in QImode, HImode and SImode.
673                                            Relative to reg-reg move (2).  */
674   {2, 2, 2},                            /* cost of storing integer registers */
675   2,                                    /* cost of reg,reg fld/fst */
676   {2, 2, 6},                            /* cost of loading fp registers
677                                            in SFmode, DFmode and XFmode */
678   {4, 4, 6},                            /* cost of storing fp registers
679                                            in SFmode, DFmode and XFmode */
680   2,                                    /* cost of moving MMX register */
681   {2, 2},                               /* cost of loading MMX registers
682                                            in SImode and DImode */
683   {2, 2},                               /* cost of storing MMX registers
684                                            in SImode and DImode */
685   2,                                    /* cost of moving SSE register */
686   {2, 2, 8},                            /* cost of loading SSE registers
687                                            in SImode, DImode and TImode */
688   {2, 2, 8},                            /* cost of storing SSE registers
689                                            in SImode, DImode and TImode */
690   3,                                    /* MMX or SSE register to integer */
691   8,                                    /* size of l1 cache.  */
692   256,                                  /* size of l2 cache  */
693   32,                                   /* size of prefetch block */
694   6,                                    /* number of parallel prefetches */
695   2,                                    /* Branch cost */
696   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
697   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
698   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
699   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
700   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
701   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
702   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes
703      (we ensure the alignment).  For small blocks inline loop is still a
704      noticeable win, for bigger blocks either rep movsl or rep movsb is
705      way to go.  Rep movsb has apparently more expensive startup time in CPU,
706      but after 4K the difference is down in the noise.  */
707   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
708                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
709    DUMMY_STRINGOP_ALGS},
710   {{rep_prefix_4_byte, {{1024, unrolled_loop},
711                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
712    DUMMY_STRINGOP_ALGS},
713   1,                                    /* scalar_stmt_cost.  */
714   1,                                    /* scalar load_cost.  */
715   1,                                    /* scalar_store_cost.  */
716   1,                                    /* vec_stmt_cost.  */
717   1,                                    /* vec_to_scalar_cost.  */
718   1,                                    /* scalar_to_vec_cost.  */
719   1,                                    /* vec_align_load_cost.  */
720   2,                                    /* vec_unalign_load_cost.  */
721   1,                                    /* vec_store_cost.  */
722   3,                                    /* cond_taken_branch_cost.  */
723   1,                                    /* cond_not_taken_branch_cost.  */
724 };
725
726 static const
727 struct processor_costs geode_cost = {
728   COSTS_N_INSNS (1),                    /* cost of an add instruction */
729   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
730   COSTS_N_INSNS (2),                    /* variable shift costs */
731   COSTS_N_INSNS (1),                    /* constant shift costs */
732   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
733    COSTS_N_INSNS (4),                   /*                               HI */
734    COSTS_N_INSNS (7),                   /*                               SI */
735    COSTS_N_INSNS (7),                   /*                               DI */
736    COSTS_N_INSNS (7)},                  /*                            other */
737   0,                                    /* cost of multiply per each bit set */
738   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
739    COSTS_N_INSNS (23),                  /*                          HI */
740    COSTS_N_INSNS (39),                  /*                          SI */
741    COSTS_N_INSNS (39),                  /*                          DI */
742    COSTS_N_INSNS (39)},                 /*                          other */
743   COSTS_N_INSNS (1),                    /* cost of movsx */
744   COSTS_N_INSNS (1),                    /* cost of movzx */
745   8,                                    /* "large" insn */
746   4,                                    /* MOVE_RATIO */
747   1,                                 /* cost for loading QImode using movzbl */
748   {1, 1, 1},                            /* cost of loading integer registers
749                                            in QImode, HImode and SImode.
750                                            Relative to reg-reg move (2).  */
751   {1, 1, 1},                            /* cost of storing integer registers */
752   1,                                    /* cost of reg,reg fld/fst */
753   {1, 1, 1},                            /* cost of loading fp registers
754                                            in SFmode, DFmode and XFmode */
755   {4, 6, 6},                            /* cost of storing fp registers
756                                            in SFmode, DFmode and XFmode */
757
758   1,                                    /* cost of moving MMX register */
759   {1, 1},                               /* cost of loading MMX registers
760                                            in SImode and DImode */
761   {1, 1},                               /* cost of storing MMX registers
762                                            in SImode and DImode */
763   1,                                    /* cost of moving SSE register */
764   {1, 1, 1},                            /* cost of loading SSE registers
765                                            in SImode, DImode and TImode */
766   {1, 1, 1},                            /* cost of storing SSE registers
767                                            in SImode, DImode and TImode */
768   1,                                    /* MMX or SSE register to integer */
769   64,                                   /* size of l1 cache.  */
770   128,                                  /* size of l2 cache.  */
771   32,                                   /* size of prefetch block */
772   1,                                    /* number of parallel prefetches */
773   1,                                    /* Branch cost */
774   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
775   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
776   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
777   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
778   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
779   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
780   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
781    DUMMY_STRINGOP_ALGS},
782   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
783    DUMMY_STRINGOP_ALGS},
784   1,                                    /* scalar_stmt_cost.  */
785   1,                                    /* scalar load_cost.  */
786   1,                                    /* scalar_store_cost.  */
787   1,                                    /* vec_stmt_cost.  */
788   1,                                    /* vec_to_scalar_cost.  */
789   1,                                    /* scalar_to_vec_cost.  */
790   1,                                    /* vec_align_load_cost.  */
791   2,                                    /* vec_unalign_load_cost.  */
792   1,                                    /* vec_store_cost.  */
793   3,                                    /* cond_taken_branch_cost.  */
794   1,                                    /* cond_not_taken_branch_cost.  */
795 };
796
797 static const
798 struct processor_costs k6_cost = {
799   COSTS_N_INSNS (1),                    /* cost of an add instruction */
800   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
801   COSTS_N_INSNS (1),                    /* variable shift costs */
802   COSTS_N_INSNS (1),                    /* constant shift costs */
803   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
804    COSTS_N_INSNS (3),                   /*                               HI */
805    COSTS_N_INSNS (3),                   /*                               SI */
806    COSTS_N_INSNS (3),                   /*                               DI */
807    COSTS_N_INSNS (3)},                  /*                            other */
808   0,                                    /* cost of multiply per each bit set */
809   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
810    COSTS_N_INSNS (18),                  /*                          HI */
811    COSTS_N_INSNS (18),                  /*                          SI */
812    COSTS_N_INSNS (18),                  /*                          DI */
813    COSTS_N_INSNS (18)},                 /*                          other */
814   COSTS_N_INSNS (2),                    /* cost of movsx */
815   COSTS_N_INSNS (2),                    /* cost of movzx */
816   8,                                    /* "large" insn */
817   4,                                    /* MOVE_RATIO */
818   3,                                 /* cost for loading QImode using movzbl */
819   {4, 5, 4},                            /* cost of loading integer registers
820                                            in QImode, HImode and SImode.
821                                            Relative to reg-reg move (2).  */
822   {2, 3, 2},                            /* cost of storing integer registers */
823   4,                                    /* cost of reg,reg fld/fst */
824   {6, 6, 6},                            /* cost of loading fp registers
825                                            in SFmode, DFmode and XFmode */
826   {4, 4, 4},                            /* cost of storing fp registers
827                                            in SFmode, DFmode and XFmode */
828   2,                                    /* cost of moving MMX register */
829   {2, 2},                               /* cost of loading MMX registers
830                                            in SImode and DImode */
831   {2, 2},                               /* cost of storing MMX registers
832                                            in SImode and DImode */
833   2,                                    /* cost of moving SSE register */
834   {2, 2, 8},                            /* cost of loading SSE registers
835                                            in SImode, DImode and TImode */
836   {2, 2, 8},                            /* cost of storing SSE registers
837                                            in SImode, DImode and TImode */
838   6,                                    /* MMX or SSE register to integer */
839   32,                                   /* size of l1 cache.  */
840   32,                                   /* size of l2 cache.  Some models
841                                            have integrated l2 cache, but
842                                            optimizing for k6 is not important
843                                            enough to worry about that.  */
844   32,                                   /* size of prefetch block */
845   1,                                    /* number of parallel prefetches */
846   1,                                    /* Branch cost */
847   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
848   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
849   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
850   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
851   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
852   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
853   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
854    DUMMY_STRINGOP_ALGS},
855   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
856    DUMMY_STRINGOP_ALGS},
857   1,                                    /* scalar_stmt_cost.  */
858   1,                                    /* scalar load_cost.  */
859   1,                                    /* scalar_store_cost.  */
860   1,                                    /* vec_stmt_cost.  */
861   1,                                    /* vec_to_scalar_cost.  */
862   1,                                    /* scalar_to_vec_cost.  */
863   1,                                    /* vec_align_load_cost.  */
864   2,                                    /* vec_unalign_load_cost.  */
865   1,                                    /* vec_store_cost.  */
866   3,                                    /* cond_taken_branch_cost.  */
867   1,                                    /* cond_not_taken_branch_cost.  */
868 };
869
870 static const
871 struct processor_costs athlon_cost = {
872   COSTS_N_INSNS (1),                    /* cost of an add instruction */
873   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
874   COSTS_N_INSNS (1),                    /* variable shift costs */
875   COSTS_N_INSNS (1),                    /* constant shift costs */
876   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
877    COSTS_N_INSNS (5),                   /*                               HI */
878    COSTS_N_INSNS (5),                   /*                               SI */
879    COSTS_N_INSNS (5),                   /*                               DI */
880    COSTS_N_INSNS (5)},                  /*                            other */
881   0,                                    /* cost of multiply per each bit set */
882   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
883    COSTS_N_INSNS (26),                  /*                          HI */
884    COSTS_N_INSNS (42),                  /*                          SI */
885    COSTS_N_INSNS (74),                  /*                          DI */
886    COSTS_N_INSNS (74)},                 /*                          other */
887   COSTS_N_INSNS (1),                    /* cost of movsx */
888   COSTS_N_INSNS (1),                    /* cost of movzx */
889   8,                                    /* "large" insn */
890   9,                                    /* MOVE_RATIO */
891   4,                                 /* cost for loading QImode using movzbl */
892   {3, 4, 3},                            /* cost of loading integer registers
893                                            in QImode, HImode and SImode.
894                                            Relative to reg-reg move (2).  */
895   {3, 4, 3},                            /* cost of storing integer registers */
896   4,                                    /* cost of reg,reg fld/fst */
897   {4, 4, 12},                           /* cost of loading fp registers
898                                            in SFmode, DFmode and XFmode */
899   {6, 6, 8},                            /* cost of storing fp registers
900                                            in SFmode, DFmode and XFmode */
901   2,                                    /* cost of moving MMX register */
902   {4, 4},                               /* cost of loading MMX registers
903                                            in SImode and DImode */
904   {4, 4},                               /* cost of storing MMX registers
905                                            in SImode and DImode */
906   2,                                    /* cost of moving SSE register */
907   {4, 4, 6},                            /* cost of loading SSE registers
908                                            in SImode, DImode and TImode */
909   {4, 4, 5},                            /* cost of storing SSE registers
910                                            in SImode, DImode and TImode */
911   5,                                    /* MMX or SSE register to integer */
912   64,                                   /* size of l1 cache.  */
913   256,                                  /* size of l2 cache.  */
914   64,                                   /* size of prefetch block */
915   6,                                    /* number of parallel prefetches */
916   5,                                    /* Branch cost */
917   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
918   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
919   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
920   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
921   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
922   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
923   /* For some reason, Athlon deals better with REP prefix (relative to loops)
924      compared to K8. Alignment becomes important after 8 bytes for memcpy and
925      128 bytes for memset.  */
926   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
927    DUMMY_STRINGOP_ALGS},
928   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
929    DUMMY_STRINGOP_ALGS},
930   1,                                    /* scalar_stmt_cost.  */
931   1,                                    /* scalar load_cost.  */
932   1,                                    /* scalar_store_cost.  */
933   1,                                    /* vec_stmt_cost.  */
934   1,                                    /* vec_to_scalar_cost.  */
935   1,                                    /* scalar_to_vec_cost.  */
936   1,                                    /* vec_align_load_cost.  */
937   2,                                    /* vec_unalign_load_cost.  */
938   1,                                    /* vec_store_cost.  */
939   3,                                    /* cond_taken_branch_cost.  */
940   1,                                    /* cond_not_taken_branch_cost.  */
941 };
942
943 static const
944 struct processor_costs k8_cost = {
945   COSTS_N_INSNS (1),                    /* cost of an add instruction */
946   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
947   COSTS_N_INSNS (1),                    /* variable shift costs */
948   COSTS_N_INSNS (1),                    /* constant shift costs */
949   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
950    COSTS_N_INSNS (4),                   /*                               HI */
951    COSTS_N_INSNS (3),                   /*                               SI */
952    COSTS_N_INSNS (4),                   /*                               DI */
953    COSTS_N_INSNS (5)},                  /*                            other */
954   0,                                    /* cost of multiply per each bit set */
955   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
956    COSTS_N_INSNS (26),                  /*                          HI */
957    COSTS_N_INSNS (42),                  /*                          SI */
958    COSTS_N_INSNS (74),                  /*                          DI */
959    COSTS_N_INSNS (74)},                 /*                          other */
960   COSTS_N_INSNS (1),                    /* cost of movsx */
961   COSTS_N_INSNS (1),                    /* cost of movzx */
962   8,                                    /* "large" insn */
963   9,                                    /* MOVE_RATIO */
964   4,                                 /* cost for loading QImode using movzbl */
965   {3, 4, 3},                            /* cost of loading integer registers
966                                            in QImode, HImode and SImode.
967                                            Relative to reg-reg move (2).  */
968   {3, 4, 3},                            /* cost of storing integer registers */
969   4,                                    /* cost of reg,reg fld/fst */
970   {4, 4, 12},                           /* cost of loading fp registers
971                                            in SFmode, DFmode and XFmode */
972   {6, 6, 8},                            /* cost of storing fp registers
973                                            in SFmode, DFmode and XFmode */
974   2,                                    /* cost of moving MMX register */
975   {3, 3},                               /* cost of loading MMX registers
976                                            in SImode and DImode */
977   {4, 4},                               /* cost of storing MMX registers
978                                            in SImode and DImode */
979   2,                                    /* cost of moving SSE register */
980   {4, 3, 6},                            /* cost of loading SSE registers
981                                            in SImode, DImode and TImode */
982   {4, 4, 5},                            /* cost of storing SSE registers
983                                            in SImode, DImode and TImode */
984   5,                                    /* MMX or SSE register to integer */
985   64,                                   /* size of l1 cache.  */
986   512,                                  /* size of l2 cache.  */
987   64,                                   /* size of prefetch block */
988   /* New AMD processors never drop prefetches; if they cannot be performed
989      immediately, they are queued.  We set number of simultaneous prefetches
990      to a large constant to reflect this (it probably is not a good idea not
991      to limit number of prefetches at all, as their execution also takes some
992      time).  */
993   100,                                  /* number of parallel prefetches */
994   3,                                    /* Branch cost */
995   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
996   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
997   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
998   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
999   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1000   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1001   /* K8 has optimized REP instruction for medium sized blocks, but for very
1002      small blocks it is better to use loop. For large blocks, libcall can
1003      do nontemporary accesses and beat inline considerably.  */
1004   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1005    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1006   {{libcall, {{8, loop}, {24, unrolled_loop},
1007               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1008    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1009   4,                                    /* scalar_stmt_cost.  */
1010   2,                                    /* scalar load_cost.  */
1011   2,                                    /* scalar_store_cost.  */
1012   5,                                    /* vec_stmt_cost.  */
1013   0,                                    /* vec_to_scalar_cost.  */
1014   2,                                    /* scalar_to_vec_cost.  */
1015   2,                                    /* vec_align_load_cost.  */
1016   3,                                    /* vec_unalign_load_cost.  */
1017   3,                                    /* vec_store_cost.  */
1018   3,                                    /* cond_taken_branch_cost.  */
1019   2,                                    /* cond_not_taken_branch_cost.  */
1020 };
1021
1022 struct processor_costs amdfam10_cost = {
1023   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1024   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
1025   COSTS_N_INSNS (1),                    /* variable shift costs */
1026   COSTS_N_INSNS (1),                    /* constant shift costs */
1027   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1028    COSTS_N_INSNS (4),                   /*                               HI */
1029    COSTS_N_INSNS (3),                   /*                               SI */
1030    COSTS_N_INSNS (4),                   /*                               DI */
1031    COSTS_N_INSNS (5)},                  /*                            other */
1032   0,                                    /* cost of multiply per each bit set */
1033   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1034    COSTS_N_INSNS (35),                  /*                          HI */
1035    COSTS_N_INSNS (51),                  /*                          SI */
1036    COSTS_N_INSNS (83),                  /*                          DI */
1037    COSTS_N_INSNS (83)},                 /*                          other */
1038   COSTS_N_INSNS (1),                    /* cost of movsx */
1039   COSTS_N_INSNS (1),                    /* cost of movzx */
1040   8,                                    /* "large" insn */
1041   9,                                    /* MOVE_RATIO */
1042   4,                                 /* cost for loading QImode using movzbl */
1043   {3, 4, 3},                            /* cost of loading integer registers
1044                                            in QImode, HImode and SImode.
1045                                            Relative to reg-reg move (2).  */
1046   {3, 4, 3},                            /* cost of storing integer registers */
1047   4,                                    /* cost of reg,reg fld/fst */
1048   {4, 4, 12},                           /* cost of loading fp registers
1049                                            in SFmode, DFmode and XFmode */
1050   {6, 6, 8},                            /* cost of storing fp registers
1051                                            in SFmode, DFmode and XFmode */
1052   2,                                    /* cost of moving MMX register */
1053   {3, 3},                               /* cost of loading MMX registers
1054                                            in SImode and DImode */
1055   {4, 4},                               /* cost of storing MMX registers
1056                                            in SImode and DImode */
1057   2,                                    /* cost of moving SSE register */
1058   {4, 4, 3},                            /* cost of loading SSE registers
1059                                            in SImode, DImode and TImode */
1060   {4, 4, 5},                            /* cost of storing SSE registers
1061                                            in SImode, DImode and TImode */
1062   3,                                    /* MMX or SSE register to integer */
1063                                         /* On K8:
1064                                             MOVD reg64, xmmreg Double FSTORE 4
1065                                             MOVD reg32, xmmreg Double FSTORE 4
1066                                            On AMDFAM10:
1067                                             MOVD reg64, xmmreg Double FADD 3
1068                                                                1/1  1/1
1069                                             MOVD reg32, xmmreg Double FADD 3
1070                                                                1/1  1/1 */
1071   64,                                   /* size of l1 cache.  */
1072   512,                                  /* size of l2 cache.  */
1073   64,                                   /* size of prefetch block */
1074   /* New AMD processors never drop prefetches; if they cannot be performed
1075      immediately, they are queued.  We set number of simultaneous prefetches
1076      to a large constant to reflect this (it probably is not a good idea not
1077      to limit number of prefetches at all, as their execution also takes some
1078      time).  */
1079   100,                                  /* number of parallel prefetches */
1080   2,                                    /* Branch cost */
1081   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
1082   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
1083   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
1084   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1085   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1086   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
1087
1088   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
1089      very small blocks it is better to use loop. For large blocks, libcall can
1090      do nontemporary accesses and beat inline considerably.  */
1091   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1092    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1093   {{libcall, {{8, loop}, {24, unrolled_loop},
1094               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1095    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1096   4,                                    /* scalar_stmt_cost.  */
1097   2,                                    /* scalar load_cost.  */
1098   2,                                    /* scalar_store_cost.  */
1099   6,                                    /* vec_stmt_cost.  */
1100   0,                                    /* vec_to_scalar_cost.  */
1101   2,                                    /* scalar_to_vec_cost.  */
1102   2,                                    /* vec_align_load_cost.  */
1103   2,                                    /* vec_unalign_load_cost.  */
1104   2,                                    /* vec_store_cost.  */
1105   2,                                    /* cond_taken_branch_cost.  */
1106   1,                                    /* cond_not_taken_branch_cost.  */
1107 };
1108
1109 struct processor_costs bdver1_cost = {
1110   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1111   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1112   COSTS_N_INSNS (1),                    /* variable shift costs */
1113   COSTS_N_INSNS (1),                    /* constant shift costs */
1114   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
1115    COSTS_N_INSNS (4),                   /*                               HI */
1116    COSTS_N_INSNS (4),                   /*                               SI */
1117    COSTS_N_INSNS (6),                   /*                               DI */
1118    COSTS_N_INSNS (6)},                  /*                            other */
1119   0,                                    /* cost of multiply per each bit set */
1120   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
1121    COSTS_N_INSNS (35),                  /*                          HI */
1122    COSTS_N_INSNS (51),                  /*                          SI */
1123    COSTS_N_INSNS (83),                  /*                          DI */
1124    COSTS_N_INSNS (83)},                 /*                          other */
1125   COSTS_N_INSNS (1),                    /* cost of movsx */
1126   COSTS_N_INSNS (1),                    /* cost of movzx */
1127   8,                                    /* "large" insn */
1128   9,                                    /* MOVE_RATIO */
1129   4,                                 /* cost for loading QImode using movzbl */
1130   {5, 5, 4},                            /* cost of loading integer registers
1131                                            in QImode, HImode and SImode.
1132                                            Relative to reg-reg move (2).  */
1133   {4, 4, 4},                            /* cost of storing integer registers */
1134   2,                                    /* cost of reg,reg fld/fst */
1135   {5, 5, 12},                           /* cost of loading fp registers
1136                                            in SFmode, DFmode and XFmode */
1137   {4, 4, 8},                            /* cost of storing fp registers
1138                                            in SFmode, DFmode and XFmode */
1139   2,                                    /* cost of moving MMX register */
1140   {4, 4},                               /* cost of loading MMX registers
1141                                            in SImode and DImode */
1142   {4, 4},                               /* cost of storing MMX registers
1143                                            in SImode and DImode */
1144   2,                                    /* cost of moving SSE register */
1145   {4, 4, 4},                            /* cost of loading SSE registers
1146                                            in SImode, DImode and TImode */
1147   {4, 4, 4},                            /* cost of storing SSE registers
1148                                            in SImode, DImode and TImode */
1149   2,                                    /* MMX or SSE register to integer */
1150                                         /* On K8:
1151                                             MOVD reg64, xmmreg Double FSTORE 4
1152                                             MOVD reg32, xmmreg Double FSTORE 4
1153                                            On AMDFAM10:
1154                                             MOVD reg64, xmmreg Double FADD 3
1155                                                                1/1  1/1
1156                                             MOVD reg32, xmmreg Double FADD 3
1157                                                                1/1  1/1 */
1158   16,                                   /* size of l1 cache.  */
1159   2048,                                 /* size of l2 cache.  */
1160   64,                                   /* size of prefetch block */
1161   /* New AMD processors never drop prefetches; if they cannot be performed
1162      immediately, they are queued.  We set number of simultaneous prefetches
1163      to a large constant to reflect this (it probably is not a good idea not
1164      to limit number of prefetches at all, as their execution also takes some
1165      time).  */
1166   100,                                  /* number of parallel prefetches */
1167   2,                                    /* Branch cost */
1168   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1169   COSTS_N_INSNS (6),                    /* cost of FMUL instruction.  */
1170   COSTS_N_INSNS (42),                   /* cost of FDIV instruction.  */
1171   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1172   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1173   COSTS_N_INSNS (52),                   /* cost of FSQRT instruction.  */
1174
1175   /*  BDVER1 has optimized REP instruction for medium sized blocks, but for
1176       very small blocks it is better to use loop. For large blocks, libcall
1177       can do nontemporary accesses and beat inline considerably.  */
1178   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
1179    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1180   {{libcall, {{8, loop}, {24, unrolled_loop},
1181               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1182    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1183   6,                                    /* scalar_stmt_cost.  */
1184   4,                                    /* scalar load_cost.  */
1185   4,                                    /* scalar_store_cost.  */
1186   6,                                    /* vec_stmt_cost.  */
1187   0,                                    /* vec_to_scalar_cost.  */
1188   2,                                    /* scalar_to_vec_cost.  */
1189   4,                                    /* vec_align_load_cost.  */
1190   4,                                    /* vec_unalign_load_cost.  */
1191   4,                                    /* vec_store_cost.  */
1192   2,                                    /* cond_taken_branch_cost.  */
1193   1,                                    /* cond_not_taken_branch_cost.  */
1194 };
1195
1196 static const
1197 struct processor_costs pentium4_cost = {
1198   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1199   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
1200   COSTS_N_INSNS (4),                    /* variable shift costs */
1201   COSTS_N_INSNS (4),                    /* constant shift costs */
1202   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
1203    COSTS_N_INSNS (15),                  /*                               HI */
1204    COSTS_N_INSNS (15),                  /*                               SI */
1205    COSTS_N_INSNS (15),                  /*                               DI */
1206    COSTS_N_INSNS (15)},                 /*                            other */
1207   0,                                    /* cost of multiply per each bit set */
1208   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
1209    COSTS_N_INSNS (56),                  /*                          HI */
1210    COSTS_N_INSNS (56),                  /*                          SI */
1211    COSTS_N_INSNS (56),                  /*                          DI */
1212    COSTS_N_INSNS (56)},                 /*                          other */
1213   COSTS_N_INSNS (1),                    /* cost of movsx */
1214   COSTS_N_INSNS (1),                    /* cost of movzx */
1215   16,                                   /* "large" insn */
1216   6,                                    /* MOVE_RATIO */
1217   2,                                 /* cost for loading QImode using movzbl */
1218   {4, 5, 4},                            /* cost of loading integer registers
1219                                            in QImode, HImode and SImode.
1220                                            Relative to reg-reg move (2).  */
1221   {2, 3, 2},                            /* cost of storing integer registers */
1222   2,                                    /* cost of reg,reg fld/fst */
1223   {2, 2, 6},                            /* cost of loading fp registers
1224                                            in SFmode, DFmode and XFmode */
1225   {4, 4, 6},                            /* cost of storing fp registers
1226                                            in SFmode, DFmode and XFmode */
1227   2,                                    /* cost of moving MMX register */
1228   {2, 2},                               /* cost of loading MMX registers
1229                                            in SImode and DImode */
1230   {2, 2},                               /* cost of storing MMX registers
1231                                            in SImode and DImode */
1232   12,                                   /* cost of moving SSE register */
1233   {12, 12, 12},                         /* cost of loading SSE registers
1234                                            in SImode, DImode and TImode */
1235   {2, 2, 8},                            /* cost of storing SSE registers
1236                                            in SImode, DImode and TImode */
1237   10,                                   /* MMX or SSE register to integer */
1238   8,                                    /* size of l1 cache.  */
1239   256,                                  /* size of l2 cache.  */
1240   64,                                   /* size of prefetch block */
1241   6,                                    /* number of parallel prefetches */
1242   2,                                    /* Branch cost */
1243   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
1244   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
1245   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
1246   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
1247   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
1248   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
1249   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1250    DUMMY_STRINGOP_ALGS},
1251   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1252    {-1, libcall}}},
1253    DUMMY_STRINGOP_ALGS},
1254   1,                                    /* scalar_stmt_cost.  */
1255   1,                                    /* scalar load_cost.  */
1256   1,                                    /* scalar_store_cost.  */
1257   1,                                    /* vec_stmt_cost.  */
1258   1,                                    /* vec_to_scalar_cost.  */
1259   1,                                    /* scalar_to_vec_cost.  */
1260   1,                                    /* vec_align_load_cost.  */
1261   2,                                    /* vec_unalign_load_cost.  */
1262   1,                                    /* vec_store_cost.  */
1263   3,                                    /* cond_taken_branch_cost.  */
1264   1,                                    /* cond_not_taken_branch_cost.  */
1265 };
1266
1267 static const
1268 struct processor_costs nocona_cost = {
1269   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1270   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
1271   COSTS_N_INSNS (1),                    /* variable shift costs */
1272   COSTS_N_INSNS (1),                    /* constant shift costs */
1273   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
1274    COSTS_N_INSNS (10),                  /*                               HI */
1275    COSTS_N_INSNS (10),                  /*                               SI */
1276    COSTS_N_INSNS (10),                  /*                               DI */
1277    COSTS_N_INSNS (10)},                 /*                            other */
1278   0,                                    /* cost of multiply per each bit set */
1279   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
1280    COSTS_N_INSNS (66),                  /*                          HI */
1281    COSTS_N_INSNS (66),                  /*                          SI */
1282    COSTS_N_INSNS (66),                  /*                          DI */
1283    COSTS_N_INSNS (66)},                 /*                          other */
1284   COSTS_N_INSNS (1),                    /* cost of movsx */
1285   COSTS_N_INSNS (1),                    /* cost of movzx */
1286   16,                                   /* "large" insn */
1287   17,                                   /* MOVE_RATIO */
1288   4,                                 /* cost for loading QImode using movzbl */
1289   {4, 4, 4},                            /* cost of loading integer registers
1290                                            in QImode, HImode and SImode.
1291                                            Relative to reg-reg move (2).  */
1292   {4, 4, 4},                            /* cost of storing integer registers */
1293   3,                                    /* cost of reg,reg fld/fst */
1294   {12, 12, 12},                         /* cost of loading fp registers
1295                                            in SFmode, DFmode and XFmode */
1296   {4, 4, 4},                            /* cost of storing fp registers
1297                                            in SFmode, DFmode and XFmode */
1298   6,                                    /* cost of moving MMX register */
1299   {12, 12},                             /* cost of loading MMX registers
1300                                            in SImode and DImode */
1301   {12, 12},                             /* cost of storing MMX registers
1302                                            in SImode and DImode */
1303   6,                                    /* cost of moving SSE register */
1304   {12, 12, 12},                         /* cost of loading SSE registers
1305                                            in SImode, DImode and TImode */
1306   {12, 12, 12},                         /* cost of storing SSE registers
1307                                            in SImode, DImode and TImode */
1308   8,                                    /* MMX or SSE register to integer */
1309   8,                                    /* size of l1 cache.  */
1310   1024,                                 /* size of l2 cache.  */
1311   128,                                  /* size of prefetch block */
1312   8,                                    /* number of parallel prefetches */
1313   1,                                    /* Branch cost */
1314   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1315   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1316   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
1317   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
1318   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
1319   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
1320   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1321    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
1322               {100000, unrolled_loop}, {-1, libcall}}}},
1323   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1324    {-1, libcall}}},
1325    {libcall, {{24, loop}, {64, unrolled_loop},
1326               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1327   1,                                    /* scalar_stmt_cost.  */
1328   1,                                    /* scalar load_cost.  */
1329   1,                                    /* scalar_store_cost.  */
1330   1,                                    /* vec_stmt_cost.  */
1331   1,                                    /* vec_to_scalar_cost.  */
1332   1,                                    /* scalar_to_vec_cost.  */
1333   1,                                    /* vec_align_load_cost.  */
1334   2,                                    /* vec_unalign_load_cost.  */
1335   1,                                    /* vec_store_cost.  */
1336   3,                                    /* cond_taken_branch_cost.  */
1337   1,                                    /* cond_not_taken_branch_cost.  */
1338 };
1339
1340 static const
1341 struct processor_costs core2_cost = {
1342   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1343   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1344   COSTS_N_INSNS (1),                    /* variable shift costs */
1345   COSTS_N_INSNS (1),                    /* constant shift costs */
1346   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1347    COSTS_N_INSNS (3),                   /*                               HI */
1348    COSTS_N_INSNS (3),                   /*                               SI */
1349    COSTS_N_INSNS (3),                   /*                               DI */
1350    COSTS_N_INSNS (3)},                  /*                            other */
1351   0,                                    /* cost of multiply per each bit set */
1352   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
1353    COSTS_N_INSNS (22),                  /*                          HI */
1354    COSTS_N_INSNS (22),                  /*                          SI */
1355    COSTS_N_INSNS (22),                  /*                          DI */
1356    COSTS_N_INSNS (22)},                 /*                          other */
1357   COSTS_N_INSNS (1),                    /* cost of movsx */
1358   COSTS_N_INSNS (1),                    /* cost of movzx */
1359   8,                                    /* "large" insn */
1360   16,                                   /* MOVE_RATIO */
1361   2,                                 /* cost for loading QImode using movzbl */
1362   {6, 6, 6},                            /* cost of loading integer registers
1363                                            in QImode, HImode and SImode.
1364                                            Relative to reg-reg move (2).  */
1365   {4, 4, 4},                            /* cost of storing integer registers */
1366   2,                                    /* cost of reg,reg fld/fst */
1367   {6, 6, 6},                            /* cost of loading fp registers
1368                                            in SFmode, DFmode and XFmode */
1369   {4, 4, 4},                            /* cost of storing fp registers
1370                                            in SFmode, DFmode and XFmode */
1371   2,                                    /* cost of moving MMX register */
1372   {6, 6},                               /* cost of loading MMX registers
1373                                            in SImode and DImode */
1374   {4, 4},                               /* cost of storing MMX registers
1375                                            in SImode and DImode */
1376   2,                                    /* cost of moving SSE register */
1377   {6, 6, 6},                            /* cost of loading SSE registers
1378                                            in SImode, DImode and TImode */
1379   {4, 4, 4},                            /* cost of storing SSE registers
1380                                            in SImode, DImode and TImode */
1381   2,                                    /* MMX or SSE register to integer */
1382   32,                                   /* size of l1 cache.  */
1383   2048,                                 /* size of l2 cache.  */
1384   128,                                  /* size of prefetch block */
1385   8,                                    /* number of parallel prefetches */
1386   3,                                    /* Branch cost */
1387   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
1388   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
1389   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
1390   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
1391   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
1392   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
1393   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1394    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1395               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1396   {{libcall, {{8, loop}, {15, unrolled_loop},
1397               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1398    {libcall, {{24, loop}, {32, unrolled_loop},
1399               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1400   1,                                    /* scalar_stmt_cost.  */
1401   1,                                    /* scalar load_cost.  */
1402   1,                                    /* scalar_store_cost.  */
1403   1,                                    /* vec_stmt_cost.  */
1404   1,                                    /* vec_to_scalar_cost.  */
1405   1,                                    /* scalar_to_vec_cost.  */
1406   1,                                    /* vec_align_load_cost.  */
1407   2,                                    /* vec_unalign_load_cost.  */
1408   1,                                    /* vec_store_cost.  */
1409   3,                                    /* cond_taken_branch_cost.  */
1410   1,                                    /* cond_not_taken_branch_cost.  */
1411 };
1412
1413 static const
1414 struct processor_costs atom_cost = {
1415   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1416   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1417   COSTS_N_INSNS (1),                    /* variable shift costs */
1418   COSTS_N_INSNS (1),                    /* constant shift costs */
1419   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1420    COSTS_N_INSNS (4),                   /*                               HI */
1421    COSTS_N_INSNS (3),                   /*                               SI */
1422    COSTS_N_INSNS (4),                   /*                               DI */
1423    COSTS_N_INSNS (2)},                  /*                            other */
1424   0,                                    /* cost of multiply per each bit set */
1425   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1426    COSTS_N_INSNS (26),                  /*                          HI */
1427    COSTS_N_INSNS (42),                  /*                          SI */
1428    COSTS_N_INSNS (74),                  /*                          DI */
1429    COSTS_N_INSNS (74)},                 /*                          other */
1430   COSTS_N_INSNS (1),                    /* cost of movsx */
1431   COSTS_N_INSNS (1),                    /* cost of movzx */
1432   8,                                    /* "large" insn */
1433   17,                                   /* MOVE_RATIO */
1434   2,                                 /* cost for loading QImode using movzbl */
1435   {4, 4, 4},                            /* cost of loading integer registers
1436                                            in QImode, HImode and SImode.
1437                                            Relative to reg-reg move (2).  */
1438   {4, 4, 4},                            /* cost of storing integer registers */
1439   4,                                    /* cost of reg,reg fld/fst */
1440   {12, 12, 12},                         /* cost of loading fp registers
1441                                            in SFmode, DFmode and XFmode */
1442   {6, 6, 8},                            /* cost of storing fp registers
1443                                            in SFmode, DFmode and XFmode */
1444   2,                                    /* cost of moving MMX register */
1445   {8, 8},                               /* cost of loading MMX registers
1446                                            in SImode and DImode */
1447   {8, 8},                               /* cost of storing MMX registers
1448                                            in SImode and DImode */
1449   2,                                    /* cost of moving SSE register */
1450   {8, 8, 8},                            /* cost of loading SSE registers
1451                                            in SImode, DImode and TImode */
1452   {8, 8, 8},                            /* cost of storing SSE registers
1453                                            in SImode, DImode and TImode */
1454   5,                                    /* MMX or SSE register to integer */
1455   32,                                   /* size of l1 cache.  */
1456   256,                                  /* size of l2 cache.  */
1457   64,                                   /* size of prefetch block */
1458   6,                                    /* number of parallel prefetches */
1459   3,                                    /* Branch cost */
1460   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1461   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1462   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1463   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1464   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1465   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1466   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1467    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1468           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1469   {{libcall, {{8, loop}, {15, unrolled_loop},
1470           {2048, rep_prefix_4_byte}, {-1, libcall}}},
1471    {libcall, {{24, loop}, {32, unrolled_loop},
1472           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1473   1,                                    /* scalar_stmt_cost.  */
1474   1,                                    /* scalar load_cost.  */
1475   1,                                    /* scalar_store_cost.  */
1476   1,                                    /* vec_stmt_cost.  */
1477   1,                                    /* vec_to_scalar_cost.  */
1478   1,                                    /* scalar_to_vec_cost.  */
1479   1,                                    /* vec_align_load_cost.  */
1480   2,                                    /* vec_unalign_load_cost.  */
1481   1,                                    /* vec_store_cost.  */
1482   3,                                    /* cond_taken_branch_cost.  */
1483   1,                                    /* cond_not_taken_branch_cost.  */
1484 };
1485
1486 /* Generic64 should produce code tuned for Nocona and K8.  */
1487 static const
1488 struct processor_costs generic64_cost = {
1489   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1490   /* On all chips taken into consideration lea is 2 cycles and more.  With
1491      this cost however our current implementation of synth_mult results in
1492      use of unnecessary temporary registers causing regression on several
1493      SPECfp benchmarks.  */
1494   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1495   COSTS_N_INSNS (1),                    /* variable shift costs */
1496   COSTS_N_INSNS (1),                    /* constant shift costs */
1497   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1498    COSTS_N_INSNS (4),                   /*                               HI */
1499    COSTS_N_INSNS (3),                   /*                               SI */
1500    COSTS_N_INSNS (4),                   /*                               DI */
1501    COSTS_N_INSNS (2)},                  /*                            other */
1502   0,                                    /* cost of multiply per each bit set */
1503   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1504    COSTS_N_INSNS (26),                  /*                          HI */
1505    COSTS_N_INSNS (42),                  /*                          SI */
1506    COSTS_N_INSNS (74),                  /*                          DI */
1507    COSTS_N_INSNS (74)},                 /*                          other */
1508   COSTS_N_INSNS (1),                    /* cost of movsx */
1509   COSTS_N_INSNS (1),                    /* cost of movzx */
1510   8,                                    /* "large" insn */
1511   17,                                   /* MOVE_RATIO */
1512   4,                                 /* cost for loading QImode using movzbl */
1513   {4, 4, 4},                            /* cost of loading integer registers
1514                                            in QImode, HImode and SImode.
1515                                            Relative to reg-reg move (2).  */
1516   {4, 4, 4},                            /* cost of storing integer registers */
1517   4,                                    /* cost of reg,reg fld/fst */
1518   {12, 12, 12},                         /* cost of loading fp registers
1519                                            in SFmode, DFmode and XFmode */
1520   {6, 6, 8},                            /* cost of storing fp registers
1521                                            in SFmode, DFmode and XFmode */
1522   2,                                    /* cost of moving MMX register */
1523   {8, 8},                               /* cost of loading MMX registers
1524                                            in SImode and DImode */
1525   {8, 8},                               /* cost of storing MMX registers
1526                                            in SImode and DImode */
1527   2,                                    /* cost of moving SSE register */
1528   {8, 8, 8},                            /* cost of loading SSE registers
1529                                            in SImode, DImode and TImode */
1530   {8, 8, 8},                            /* cost of storing SSE registers
1531                                            in SImode, DImode and TImode */
1532   5,                                    /* MMX or SSE register to integer */
1533   32,                                   /* size of l1 cache.  */
1534   512,                                  /* size of l2 cache.  */
1535   64,                                   /* size of prefetch block */
1536   6,                                    /* number of parallel prefetches */
1537   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this
1538      value is increased to perhaps more appropriate value of 5.  */
1539   3,                                    /* Branch cost */
1540   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1541   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1542   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1543   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1544   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1545   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1546   {DUMMY_STRINGOP_ALGS,
1547    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1548   {DUMMY_STRINGOP_ALGS,
1549    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1550   1,                                    /* scalar_stmt_cost.  */
1551   1,                                    /* scalar load_cost.  */
1552   1,                                    /* scalar_store_cost.  */
1553   1,                                    /* vec_stmt_cost.  */
1554   1,                                    /* vec_to_scalar_cost.  */
1555   1,                                    /* scalar_to_vec_cost.  */
1556   1,                                    /* vec_align_load_cost.  */
1557   2,                                    /* vec_unalign_load_cost.  */
1558   1,                                    /* vec_store_cost.  */
1559   3,                                    /* cond_taken_branch_cost.  */
1560   1,                                    /* cond_not_taken_branch_cost.  */
1561 };
1562
1563 /* Generic32 should produce code tuned for PPro, Pentium4, Nocona,
1564    Athlon and K8.  */
1565 static const
1566 struct processor_costs generic32_cost = {
1567   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1568   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1569   COSTS_N_INSNS (1),                    /* variable shift costs */
1570   COSTS_N_INSNS (1),                    /* constant shift costs */
1571   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1572    COSTS_N_INSNS (4),                   /*                               HI */
1573    COSTS_N_INSNS (3),                   /*                               SI */
1574    COSTS_N_INSNS (4),                   /*                               DI */
1575    COSTS_N_INSNS (2)},                  /*                            other */
1576   0,                                    /* cost of multiply per each bit set */
1577   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1578    COSTS_N_INSNS (26),                  /*                          HI */
1579    COSTS_N_INSNS (42),                  /*                          SI */
1580    COSTS_N_INSNS (74),                  /*                          DI */
1581    COSTS_N_INSNS (74)},                 /*                          other */
1582   COSTS_N_INSNS (1),                    /* cost of movsx */
1583   COSTS_N_INSNS (1),                    /* cost of movzx */
1584   8,                                    /* "large" insn */
1585   17,                                   /* MOVE_RATIO */
1586   4,                                 /* cost for loading QImode using movzbl */
1587   {4, 4, 4},                            /* cost of loading integer registers
1588                                            in QImode, HImode and SImode.
1589                                            Relative to reg-reg move (2).  */
1590   {4, 4, 4},                            /* cost of storing integer registers */
1591   4,                                    /* cost of reg,reg fld/fst */
1592   {12, 12, 12},                         /* cost of loading fp registers
1593                                            in SFmode, DFmode and XFmode */
1594   {6, 6, 8},                            /* cost of storing fp registers
1595                                            in SFmode, DFmode and XFmode */
1596   2,                                    /* cost of moving MMX register */
1597   {8, 8},                               /* cost of loading MMX registers
1598                                            in SImode and DImode */
1599   {8, 8},                               /* cost of storing MMX registers
1600                                            in SImode and DImode */
1601   2,                                    /* cost of moving SSE register */
1602   {8, 8, 8},                            /* cost of loading SSE registers
1603                                            in SImode, DImode and TImode */
1604   {8, 8, 8},                            /* cost of storing SSE registers
1605                                            in SImode, DImode and TImode */
1606   5,                                    /* MMX or SSE register to integer */
1607   32,                                   /* size of l1 cache.  */
1608   256,                                  /* size of l2 cache.  */
1609   64,                                   /* size of prefetch block */
1610   6,                                    /* number of parallel prefetches */
1611   3,                                    /* Branch cost */
1612   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1613   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1614   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1615   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1616   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1617   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1618   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1619    DUMMY_STRINGOP_ALGS},
1620   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1621    DUMMY_STRINGOP_ALGS},
1622   1,                                    /* scalar_stmt_cost.  */
1623   1,                                    /* scalar load_cost.  */
1624   1,                                    /* scalar_store_cost.  */
1625   1,                                    /* vec_stmt_cost.  */
1626   1,                                    /* vec_to_scalar_cost.  */
1627   1,                                    /* scalar_to_vec_cost.  */
1628   1,                                    /* vec_align_load_cost.  */
1629   2,                                    /* vec_unalign_load_cost.  */
1630   1,                                    /* vec_store_cost.  */
1631   3,                                    /* cond_taken_branch_cost.  */
1632   1,                                    /* cond_not_taken_branch_cost.  */
1633 };
1634
1635 const struct processor_costs *ix86_cost = &pentium_cost;
1636
1637 /* Processor feature/optimization bitmasks.  */
1638 #define m_386 (1<<PROCESSOR_I386)
1639 #define m_486 (1<<PROCESSOR_I486)
1640 #define m_PENT (1<<PROCESSOR_PENTIUM)
1641 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
1642 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
1643 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
1644 #define m_CORE2  (1<<PROCESSOR_CORE2)
1645 #define m_COREI7_32  (1<<PROCESSOR_COREI7_32)
1646 #define m_COREI7_64  (1<<PROCESSOR_COREI7_64)
1647 #define m_ATOM  (1<<PROCESSOR_ATOM)
1648
1649 #define m_GEODE  (1<<PROCESSOR_GEODE)
1650 #define m_K6  (1<<PROCESSOR_K6)
1651 #define m_K6_GEODE  (m_K6 | m_GEODE)
1652 #define m_K8  (1<<PROCESSOR_K8)
1653 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
1654 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
1655 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
1656 #define m_BDVER1  (1<<PROCESSOR_BDVER1)
1657 #define m_AMD_MULTIPLE  (m_K8 | m_ATHLON | m_AMDFAM10 | m_BDVER1)
1658
1659 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32 | m_COREI7_32)
1660 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64 | m_COREI7_64)
1661
1662 /* Generic instruction choice should be common subset of supported CPUs
1663    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1664 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1665
1666 /* Feature tests against the various tunings.  */
1667 unsigned char ix86_tune_features[X86_TUNE_LAST];
1668
1669 /* Feature tests against the various tunings used to create ix86_tune_features
1670    based on the processor mask.  */
1671 static unsigned int initial_ix86_tune_features[X86_TUNE_LAST] = {
1672   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1673      negatively, so enabling for Generic64 seems like good code size
1674      tradeoff.  We can't enable it for 32bit generic because it does not
1675      work well with PPro base chips.  */
1676   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_CORE2 | m_GENERIC64,
1677
1678   /* X86_TUNE_PUSH_MEMORY */
1679   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4
1680   | m_NOCONA | m_CORE2 | m_GENERIC,
1681
1682   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1683   m_486 | m_PENT,
1684
1685   /* X86_TUNE_UNROLL_STRLEN */
1686   m_486 | m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_K6
1687   | m_CORE2 | m_GENERIC,
1688
1689   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1690   m_ATOM | m_PPRO | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4 | m_GENERIC,
1691
1692   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1693      on simulation result. But after P4 was made, no performance benefit
1694      was observed with branch hints.  It also increases the code size.
1695      As a result, icc never generates branch hints.  */
1696   0,
1697
1698   /* X86_TUNE_DOUBLE_WITH_ADD */
1699   ~m_386,
1700
1701   /* X86_TUNE_USE_SAHF */
1702   m_ATOM | m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_BDVER1 | m_PENT4
1703   | m_NOCONA | m_CORE2 | m_GENERIC,
1704
1705   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1706      partial dependencies.  */
1707   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_PENT4 | m_NOCONA
1708   | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1709
1710   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1711      register stalls on Generic32 compilation setting as well.  However
1712      in current implementation the partial register stalls are not eliminated
1713      very well - they can be introduced via subregs synthesized by combine
1714      and can happen in caller/callee saving sequences.  Because this option
1715      pays back little on PPro based chips and is in conflict with partial reg
1716      dependencies used by Athlon/P4 based chips, it is better to leave it off
1717      for generic32 for now.  */
1718   m_PPRO,
1719
1720   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1721   m_CORE2 | m_GENERIC,
1722
1723   /* X86_TUNE_USE_HIMODE_FIOP */
1724   m_386 | m_486 | m_K6_GEODE,
1725
1726   /* X86_TUNE_USE_SIMODE_FIOP */
1727   ~(m_PPRO | m_AMD_MULTIPLE | m_PENT | m_ATOM | m_CORE2 | m_GENERIC),
1728
1729   /* X86_TUNE_USE_MOV0 */
1730   m_K6,
1731
1732   /* X86_TUNE_USE_CLTD */
1733   ~(m_PENT | m_ATOM | m_K6 | m_CORE2 | m_GENERIC),
1734
1735   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1736   m_PENT4,
1737
1738   /* X86_TUNE_SPLIT_LONG_MOVES */
1739   m_PPRO,
1740
1741   /* X86_TUNE_READ_MODIFY_WRITE */
1742   ~m_PENT,
1743
1744   /* X86_TUNE_READ_MODIFY */
1745   ~(m_PENT | m_PPRO),
1746
1747   /* X86_TUNE_PROMOTE_QIMODE */
1748   m_K6_GEODE | m_PENT | m_ATOM | m_386 | m_486 | m_AMD_MULTIPLE
1749   | m_CORE2 | m_GENERIC /* | m_PENT4 ? */,
1750
1751   /* X86_TUNE_FAST_PREFIX */
1752   ~(m_PENT | m_486 | m_386),
1753
1754   /* X86_TUNE_SINGLE_STRINGOP */
1755   m_386 | m_PENT4 | m_NOCONA,
1756
1757   /* X86_TUNE_QIMODE_MATH */
1758   ~0,
1759
1760   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1761      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1762      might be considered for Generic32 if our scheme for avoiding partial
1763      stalls was more effective.  */
1764   ~m_PPRO,
1765
1766   /* X86_TUNE_PROMOTE_QI_REGS */
1767   0,
1768
1769   /* X86_TUNE_PROMOTE_HI_REGS */
1770   m_PPRO,
1771
1772   /* X86_TUNE_SINGLE_POP: Enable if single pop insn is preferred
1773      over esp addition.  */
1774   m_386 | m_486 | m_PENT | m_PPRO,
1775
1776   /* X86_TUNE_DOUBLE_POP: Enable if double pop insn is preferred
1777      over esp addition.  */
1778   m_PENT,
1779
1780   /* X86_TUNE_SINGLE_PUSH: Enable if single push insn is preferred
1781      over esp subtraction.  */
1782   m_386 | m_486 | m_PENT | m_K6_GEODE,
1783
1784   /* X86_TUNE_DOUBLE_PUSH. Enable if double push insn is preferred
1785      over esp subtraction.  */
1786   m_PENT | m_K6_GEODE,
1787
1788   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1789      for DFmode copies */
1790   ~(m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1791     | m_GENERIC | m_GEODE),
1792
1793   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1794   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1795
1796   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1797      conflict here in between PPro/Pentium4 based chips that thread 128bit
1798      SSE registers as single units versus K8 based chips that divide SSE
1799      registers to two 64bit halves.  This knob promotes all store destinations
1800      to be 128bit to allow register renaming on 128bit SSE units, but usually
1801      results in one extra microop on 64bit SSE units.  Experimental results
1802      shows that disabling this option on P4 brings over 20% SPECfp regression,
1803      while enabling it on K8 brings roughly 2.4% regression that can be partly
1804      masked by careful scheduling of moves.  */
1805   m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC
1806   | m_AMDFAM10 | m_BDVER1,
1807
1808   /* X86_TUNE_SSE_UNALIGNED_LOAD_OPTIMAL */
1809   m_AMDFAM10 | m_BDVER1,
1810
1811   /* X86_TUNE_SSE_UNALIGNED_STORE_OPTIMAL */
1812   m_BDVER1,
1813
1814   /* X86_TUNE_SSE_PACKED_SINGLE_INSN_OPTIMAL */
1815   m_BDVER1,
1816
1817   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1818      are resolved on SSE register parts instead of whole registers, so we may
1819      maintain just lower part of scalar values in proper format leaving the
1820      upper part undefined.  */
1821   m_ATHLON_K8,
1822
1823   /* X86_TUNE_SSE_TYPELESS_STORES */
1824   m_AMD_MULTIPLE,
1825
1826   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1827   m_PPRO | m_PENT4 | m_NOCONA,
1828
1829   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1830   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1831
1832   /* X86_TUNE_PROLOGUE_USING_MOVE */
1833   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2 | m_GENERIC,
1834
1835   /* X86_TUNE_EPILOGUE_USING_MOVE */
1836   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2 | m_GENERIC,
1837
1838   /* X86_TUNE_SHIFT1 */
1839   ~m_486,
1840
1841   /* X86_TUNE_USE_FFREEP */
1842   m_AMD_MULTIPLE,
1843
1844   /* X86_TUNE_INTER_UNIT_MOVES */
1845   ~(m_AMD_MULTIPLE | m_GENERIC),
1846
1847   /* X86_TUNE_INTER_UNIT_CONVERSIONS */
1848   ~(m_AMDFAM10 | m_BDVER1),
1849
1850   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1851      than 4 branch instructions in the 16 byte window.  */
1852   m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2
1853   | m_GENERIC,
1854
1855   /* X86_TUNE_SCHEDULE */
1856   m_PPRO | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT | m_ATOM | m_CORE2
1857   | m_GENERIC,
1858
1859   /* X86_TUNE_USE_BT */
1860   m_AMD_MULTIPLE | m_ATOM | m_CORE2 | m_GENERIC,
1861
1862   /* X86_TUNE_USE_INCDEC */
1863   ~(m_PENT4 | m_NOCONA | m_GENERIC | m_ATOM),
1864
1865   /* X86_TUNE_PAD_RETURNS */
1866   m_AMD_MULTIPLE | m_CORE2 | m_GENERIC,
1867
1868   /* X86_TUNE_PAD_SHORT_FUNCTION: Pad short funtion.  */
1869   m_ATOM,
1870
1871   /* X86_TUNE_EXT_80387_CONSTANTS */
1872   m_K6_GEODE | m_ATHLON_K8 | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO
1873   | m_CORE2 | m_GENERIC,
1874
1875   /* X86_TUNE_SHORTEN_X87_SSE */
1876   ~m_K8,
1877
1878   /* X86_TUNE_AVOID_VECTOR_DECODE */
1879   m_K8 | m_GENERIC64,
1880
1881   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1882      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1883   ~(m_386 | m_486),
1884
1885   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1886      vector path on AMD machines.  */
1887   m_K8 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1,
1888
1889   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1890      machines.  */
1891   m_K8 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1,
1892
1893   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1894      than a MOV.  */
1895   m_PENT,
1896
1897   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1898      but one byte longer.  */
1899   m_PENT,
1900
1901   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1902      operand that cannot be represented using a modRM byte.  The XOR
1903      replacement is long decoded, so this split helps here as well.  */
1904   m_K6,
1905
1906   /* X86_TUNE_USE_VECTOR_FP_CONVERTS: Prefer vector packed SSE conversion
1907      from FP to FP. */
1908   m_AMDFAM10 | m_GENERIC,
1909
1910   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
1911      from integer to FP. */
1912   m_AMDFAM10,
1913
1914   /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction
1915      with a subsequent conditional jump instruction into a single
1916      compare-and-branch uop.  */
1917   m_CORE2 | m_BDVER1,
1918
1919   /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag
1920      will impact LEA instruction selection. */
1921   m_ATOM,
1922
1923   /* X86_TUNE_VECTORIZE_DOUBLE: Enable double precision vector
1924      instructions.  */
1925   ~m_ATOM,
1926 };
1927
1928 /* Feature tests against the various architecture variations.  */
1929 unsigned char ix86_arch_features[X86_ARCH_LAST];
1930
1931 /* Feature tests against the various architecture variations, used to create
1932    ix86_arch_features based on the processor mask.  */
1933 static unsigned int initial_ix86_arch_features[X86_ARCH_LAST] = {
1934   /* X86_ARCH_CMOVE: Conditional move was added for pentiumpro.  */
1935   ~(m_386 | m_486 | m_PENT | m_K6),
1936
1937   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1938   ~m_386,
1939
1940   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1941   ~(m_386 | m_486),
1942
1943   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1944   ~m_386,
1945
1946   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1947   ~m_386,
1948 };
1949
1950 static const unsigned int x86_accumulate_outgoing_args
1951   = m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1952     | m_GENERIC;
1953
1954 static const unsigned int x86_arch_always_fancy_math_387
1955   = m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4
1956     | m_NOCONA | m_CORE2 | m_GENERIC;
1957
1958 static enum stringop_alg stringop_alg = no_stringop;
1959
1960 /* In case the average insn count for single function invocation is
1961    lower than this constant, emit fast (but longer) prologue and
1962    epilogue code.  */
1963 #define FAST_PROLOGUE_INSN_COUNT 20
1964
1965 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1966 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1967 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1968 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1969
1970 /* Array of the smallest class containing reg number REGNO, indexed by
1971    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1972
1973 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1974 {
1975   /* ax, dx, cx, bx */
1976   AREG, DREG, CREG, BREG,
1977   /* si, di, bp, sp */
1978   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1979   /* FP registers */
1980   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1981   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1982   /* arg pointer */
1983   NON_Q_REGS,
1984   /* flags, fpsr, fpcr, frame */
1985   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1986   /* SSE registers */
1987   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1988   SSE_REGS, SSE_REGS,
1989   /* MMX registers */
1990   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1991   MMX_REGS, MMX_REGS,
1992   /* REX registers */
1993   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1994   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1995   /* SSE REX registers */
1996   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1997   SSE_REGS, SSE_REGS,
1998 };
1999
2000 /* The "default" register map used in 32bit mode.  */
2001
2002 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
2003 {
2004   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
2005   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
2006   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
2007   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
2008   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
2009   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
2010   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
2011 };
2012
2013 /* The "default" register map used in 64bit mode.  */
2014
2015 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
2016 {
2017   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
2018   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
2019   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
2020   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
2021   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
2022   8,9,10,11,12,13,14,15,                /* extended integer registers */
2023   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
2024 };
2025
2026 /* Define the register numbers to be used in Dwarf debugging information.
2027    The SVR4 reference port C compiler uses the following register numbers
2028    in its Dwarf output code:
2029         0 for %eax (gcc regno = 0)
2030         1 for %ecx (gcc regno = 2)
2031         2 for %edx (gcc regno = 1)
2032         3 for %ebx (gcc regno = 3)
2033         4 for %esp (gcc regno = 7)
2034         5 for %ebp (gcc regno = 6)
2035         6 for %esi (gcc regno = 4)
2036         7 for %edi (gcc regno = 5)
2037    The following three DWARF register numbers are never generated by
2038    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
2039    believes these numbers have these meanings.
2040         8  for %eip    (no gcc equivalent)
2041         9  for %eflags (gcc regno = 17)
2042         10 for %trapno (no gcc equivalent)
2043    It is not at all clear how we should number the FP stack registers
2044    for the x86 architecture.  If the version of SDB on x86/svr4 were
2045    a bit less brain dead with respect to floating-point then we would
2046    have a precedent to follow with respect to DWARF register numbers
2047    for x86 FP registers, but the SDB on x86/svr4 is so completely
2048    broken with respect to FP registers that it is hardly worth thinking
2049    of it as something to strive for compatibility with.
2050    The version of x86/svr4 SDB I have at the moment does (partially)
2051    seem to believe that DWARF register number 11 is associated with
2052    the x86 register %st(0), but that's about all.  Higher DWARF
2053    register numbers don't seem to be associated with anything in
2054    particular, and even for DWARF regno 11, SDB only seems to under-
2055    stand that it should say that a variable lives in %st(0) (when
2056    asked via an `=' command) if we said it was in DWARF regno 11,
2057    but SDB still prints garbage when asked for the value of the
2058    variable in question (via a `/' command).
2059    (Also note that the labels SDB prints for various FP stack regs
2060    when doing an `x' command are all wrong.)
2061    Note that these problems generally don't affect the native SVR4
2062    C compiler because it doesn't allow the use of -O with -g and
2063    because when it is *not* optimizing, it allocates a memory
2064    location for each floating-point variable, and the memory
2065    location is what gets described in the DWARF AT_location
2066    attribute for the variable in question.
2067    Regardless of the severe mental illness of the x86/svr4 SDB, we
2068    do something sensible here and we use the following DWARF
2069    register numbers.  Note that these are all stack-top-relative
2070    numbers.
2071         11 for %st(0) (gcc regno = 8)
2072         12 for %st(1) (gcc regno = 9)
2073         13 for %st(2) (gcc regno = 10)
2074         14 for %st(3) (gcc regno = 11)
2075         15 for %st(4) (gcc regno = 12)
2076         16 for %st(5) (gcc regno = 13)
2077         17 for %st(6) (gcc regno = 14)
2078         18 for %st(7) (gcc regno = 15)
2079 */
2080 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
2081 {
2082   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
2083   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
2084   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
2085   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
2086   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
2087   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
2088   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
2089 };
2090
2091 /* Define parameter passing and return registers.  */
2092
2093 static int const x86_64_int_parameter_registers[6] =
2094 {
2095   DI_REG, SI_REG, DX_REG, CX_REG, R8_REG, R9_REG
2096 };
2097
2098 static int const x86_64_ms_abi_int_parameter_registers[4] =
2099 {
2100   CX_REG, DX_REG, R8_REG, R9_REG
2101 };
2102
2103 static int const x86_64_int_return_registers[4] =
2104 {
2105   AX_REG, DX_REG, DI_REG, SI_REG
2106 };
2107
2108 /* Define the structure for the machine field in struct function.  */
2109
2110 struct GTY(()) stack_local_entry {
2111   unsigned short mode;
2112   unsigned short n;
2113   rtx rtl;
2114   struct stack_local_entry *next;
2115 };
2116
2117 /* Structure describing stack frame layout.
2118    Stack grows downward:
2119
2120    [arguments]
2121                                         <- ARG_POINTER
2122    saved pc
2123
2124    saved static chain                   if ix86_static_chain_on_stack
2125
2126    saved frame pointer                  if frame_pointer_needed
2127                                         <- HARD_FRAME_POINTER
2128    [saved regs]
2129                                         <- regs_save_offset
2130    [padding0]
2131
2132    [saved SSE regs]
2133                                         <- sse_regs_save_offset
2134    [padding1]          |
2135                        |                <- FRAME_POINTER
2136    [va_arg registers]  |
2137                        |
2138    [frame]             |
2139                        |
2140    [padding2]          | = to_allocate
2141                                         <- STACK_POINTER
2142   */
2143 struct ix86_frame
2144 {
2145   int nsseregs;
2146   int nregs;
2147   int va_arg_size;
2148   int red_zone_size;
2149   int outgoing_arguments_size;
2150   HOST_WIDE_INT frame;
2151
2152   /* The offsets relative to ARG_POINTER.  */
2153   HOST_WIDE_INT frame_pointer_offset;
2154   HOST_WIDE_INT hard_frame_pointer_offset;
2155   HOST_WIDE_INT stack_pointer_offset;
2156   HOST_WIDE_INT hfp_save_offset;
2157   HOST_WIDE_INT reg_save_offset;
2158   HOST_WIDE_INT sse_reg_save_offset;
2159
2160   /* When save_regs_using_mov is set, emit prologue using
2161      move instead of push instructions.  */
2162   bool save_regs_using_mov;
2163 };
2164
2165 /* Code model option.  */
2166 enum cmodel ix86_cmodel;
2167 /* Asm dialect.  */
2168 enum asm_dialect ix86_asm_dialect = ASM_ATT;
2169 /* TLS dialects.  */
2170 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
2171
2172 /* Which unit we are generating floating point math for.  */
2173 enum fpmath_unit ix86_fpmath;
2174
2175 /* Which cpu are we scheduling for.  */
2176 enum attr_cpu ix86_schedule;
2177
2178 /* Which cpu are we optimizing for.  */
2179 enum processor_type ix86_tune;
2180
2181 /* Which instruction set architecture to use.  */
2182 enum processor_type ix86_arch;
2183
2184 /* true if sse prefetch instruction is not NOOP.  */
2185 int x86_prefetch_sse;
2186
2187 /* ix86_regparm_string as a number */
2188 static int ix86_regparm;
2189
2190 /* -mstackrealign option */
2191 static const char ix86_force_align_arg_pointer_string[]
2192   = "force_align_arg_pointer";
2193
2194 static rtx (*ix86_gen_leave) (void);
2195 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
2196 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
2197 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
2198 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
2199 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
2200 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
2201 static rtx (*ix86_gen_allocate_stack_worker) (rtx, rtx);
2202 static rtx (*ix86_gen_adjust_stack_and_probe) (rtx, rtx, rtx);
2203 static rtx (*ix86_gen_probe_stack_range) (rtx, rtx, rtx);
2204
2205 /* Preferred alignment for stack boundary in bits.  */
2206 unsigned int ix86_preferred_stack_boundary;
2207
2208 /* Alignment for incoming stack boundary in bits specified at
2209    command line.  */
2210 static unsigned int ix86_user_incoming_stack_boundary;
2211
2212 /* Default alignment for incoming stack boundary in bits.  */
2213 static unsigned int ix86_default_incoming_stack_boundary;
2214
2215 /* Alignment for incoming stack boundary in bits.  */
2216 unsigned int ix86_incoming_stack_boundary;
2217
2218 /* The abi used by target.  */
2219 enum calling_abi ix86_abi;
2220
2221 /* Values 1-5: see jump.c */
2222 int ix86_branch_cost;
2223
2224 /* Calling abi specific va_list type nodes.  */
2225 static GTY(()) tree sysv_va_list_type_node;
2226 static GTY(()) tree ms_va_list_type_node;
2227
2228 /* Variables which are this size or smaller are put in the data/bss
2229    or ldata/lbss sections.  */
2230
2231 int ix86_section_threshold = 65536;
2232
2233 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
2234 char internal_label_prefix[16];
2235 int internal_label_prefix_len;
2236
2237 /* Fence to use after loop using movnt.  */
2238 tree x86_mfence;
2239
2240 /* Register class used for passing given 64bit part of the argument.
2241    These represent classes as documented by the PS ABI, with the exception
2242    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
2243    use SF or DFmode move instead of DImode to avoid reformatting penalties.
2244
2245    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
2246    whenever possible (upper half does contain padding).  */
2247 enum x86_64_reg_class
2248   {
2249     X86_64_NO_CLASS,
2250     X86_64_INTEGER_CLASS,
2251     X86_64_INTEGERSI_CLASS,
2252     X86_64_SSE_CLASS,
2253     X86_64_SSESF_CLASS,
2254     X86_64_SSEDF_CLASS,
2255     X86_64_SSEUP_CLASS,
2256     X86_64_X87_CLASS,
2257     X86_64_X87UP_CLASS,
2258     X86_64_COMPLEX_X87_CLASS,
2259     X86_64_MEMORY_CLASS
2260   };
2261
2262 #define MAX_CLASSES 4
2263
2264 /* Table of constants used by fldpi, fldln2, etc....  */
2265 static REAL_VALUE_TYPE ext_80387_constants_table [5];
2266 static bool ext_80387_constants_init = 0;
2267
2268 \f
2269 static struct machine_function * ix86_init_machine_status (void);
2270 static rtx ix86_function_value (const_tree, const_tree, bool);
2271 static bool ix86_function_value_regno_p (const unsigned int);
2272 static rtx ix86_static_chain (const_tree, bool);
2273 static int ix86_function_regparm (const_tree, const_tree);
2274 static void ix86_compute_frame_layout (struct ix86_frame *);
2275 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
2276                                                  rtx, rtx, int);
2277 static void ix86_add_new_builtins (int);
2278 static rtx ix86_expand_vec_perm_builtin (tree);
2279 static tree ix86_canonical_va_list_type (tree);
2280 static void predict_jump (int);
2281 static unsigned int split_stack_prologue_scratch_regno (void);
2282 static bool i386_asm_output_addr_const_extra (FILE *, rtx);
2283
2284 enum ix86_function_specific_strings
2285 {
2286   IX86_FUNCTION_SPECIFIC_ARCH,
2287   IX86_FUNCTION_SPECIFIC_TUNE,
2288   IX86_FUNCTION_SPECIFIC_FPMATH,
2289   IX86_FUNCTION_SPECIFIC_MAX
2290 };
2291
2292 static char *ix86_target_string (int, int, const char *, const char *,
2293                                  const char *, bool);
2294 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
2295 static void ix86_function_specific_save (struct cl_target_option *);
2296 static void ix86_function_specific_restore (struct cl_target_option *);
2297 static void ix86_function_specific_print (FILE *, int,
2298                                           struct cl_target_option *);
2299 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
2300 static bool ix86_valid_target_attribute_inner_p (tree, char *[]);
2301 static bool ix86_can_inline_p (tree, tree);
2302 static void ix86_set_current_function (tree);
2303 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
2304
2305 static enum calling_abi ix86_function_abi (const_tree);
2306
2307 \f
2308 #ifndef SUBTARGET32_DEFAULT_CPU
2309 #define SUBTARGET32_DEFAULT_CPU "i386"
2310 #endif
2311
2312 /* The svr4 ABI for the i386 says that records and unions are returned
2313    in memory.  */
2314 #ifndef DEFAULT_PCC_STRUCT_RETURN
2315 #define DEFAULT_PCC_STRUCT_RETURN 1
2316 #endif
2317
2318 /* Whether -mtune= or -march= were specified */
2319 static int ix86_tune_defaulted;
2320 static int ix86_arch_specified;
2321
2322 /* A mask of ix86_isa_flags that includes bit X if X
2323    was set or cleared on the command line.  */
2324 static int ix86_isa_flags_explicit;
2325
2326 /* Define a set of ISAs which are available when a given ISA is
2327    enabled.  MMX and SSE ISAs are handled separately.  */
2328
2329 #define OPTION_MASK_ISA_MMX_SET OPTION_MASK_ISA_MMX
2330 #define OPTION_MASK_ISA_3DNOW_SET \
2331   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_MMX_SET)
2332
2333 #define OPTION_MASK_ISA_SSE_SET OPTION_MASK_ISA_SSE
2334 #define OPTION_MASK_ISA_SSE2_SET \
2335   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE_SET)
2336 #define OPTION_MASK_ISA_SSE3_SET \
2337   (OPTION_MASK_ISA_SSE3 | OPTION_MASK_ISA_SSE2_SET)
2338 #define OPTION_MASK_ISA_SSSE3_SET \
2339   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE3_SET)
2340 #define OPTION_MASK_ISA_SSE4_1_SET \
2341   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSSE3_SET)
2342 #define OPTION_MASK_ISA_SSE4_2_SET \
2343   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_SSE4_1_SET)
2344 #define OPTION_MASK_ISA_AVX_SET \
2345   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_SSE4_2_SET)
2346 #define OPTION_MASK_ISA_FMA_SET \
2347   (OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_AVX_SET)
2348
2349 /* SSE4 includes both SSE4.1 and SSE4.2. -msse4 should be the same
2350    as -msse4.2.  */
2351 #define OPTION_MASK_ISA_SSE4_SET OPTION_MASK_ISA_SSE4_2_SET
2352
2353 #define OPTION_MASK_ISA_SSE4A_SET \
2354   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE3_SET)
2355 #define OPTION_MASK_ISA_FMA4_SET \
2356   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_SSE4A_SET \
2357    | OPTION_MASK_ISA_AVX_SET)
2358 #define OPTION_MASK_ISA_XOP_SET \
2359   (OPTION_MASK_ISA_XOP | OPTION_MASK_ISA_FMA4_SET)
2360 #define OPTION_MASK_ISA_LWP_SET \
2361   OPTION_MASK_ISA_LWP
2362
2363 /* AES and PCLMUL need SSE2 because they use xmm registers */
2364 #define OPTION_MASK_ISA_AES_SET \
2365   (OPTION_MASK_ISA_AES | OPTION_MASK_ISA_SSE2_SET)
2366 #define OPTION_MASK_ISA_PCLMUL_SET \
2367   (OPTION_MASK_ISA_PCLMUL | OPTION_MASK_ISA_SSE2_SET)
2368
2369 #define OPTION_MASK_ISA_ABM_SET \
2370   (OPTION_MASK_ISA_ABM | OPTION_MASK_ISA_POPCNT)
2371
2372 #define OPTION_MASK_ISA_POPCNT_SET OPTION_MASK_ISA_POPCNT
2373 #define OPTION_MASK_ISA_CX16_SET OPTION_MASK_ISA_CX16
2374 #define OPTION_MASK_ISA_SAHF_SET OPTION_MASK_ISA_SAHF
2375 #define OPTION_MASK_ISA_MOVBE_SET OPTION_MASK_ISA_MOVBE
2376 #define OPTION_MASK_ISA_CRC32_SET OPTION_MASK_ISA_CRC32
2377
2378 #define OPTION_MASK_ISA_FSGSBASE_SET OPTION_MASK_ISA_FSGSBASE
2379 #define OPTION_MASK_ISA_RDRND_SET OPTION_MASK_ISA_RDRND
2380 #define OPTION_MASK_ISA_F16C_SET \
2381   (OPTION_MASK_ISA_F16C | OPTION_MASK_ISA_AVX_SET)
2382
2383 /* Define a set of ISAs which aren't available when a given ISA is
2384    disabled.  MMX and SSE ISAs are handled separately.  */
2385
2386 #define OPTION_MASK_ISA_MMX_UNSET \
2387   (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_3DNOW_UNSET)
2388 #define OPTION_MASK_ISA_3DNOW_UNSET \
2389   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_3DNOW_A_UNSET)
2390 #define OPTION_MASK_ISA_3DNOW_A_UNSET OPTION_MASK_ISA_3DNOW_A
2391
2392 #define OPTION_MASK_ISA_SSE_UNSET \
2393   (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_SSE2_UNSET)
2394 #define OPTION_MASK_ISA_SSE2_UNSET \
2395   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3_UNSET)
2396 #define OPTION_MASK_ISA_SSE3_UNSET \
2397   (OPTION_MASK_ISA_SSE3 \
2398    | OPTION_MASK_ISA_SSSE3_UNSET \
2399    | OPTION_MASK_ISA_SSE4A_UNSET )
2400 #define OPTION_MASK_ISA_SSSE3_UNSET \
2401   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE4_1_UNSET)
2402 #define OPTION_MASK_ISA_SSE4_1_UNSET \
2403   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSE4_2_UNSET)
2404 #define OPTION_MASK_ISA_SSE4_2_UNSET \
2405   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_AVX_UNSET )
2406 #define OPTION_MASK_ISA_AVX_UNSET \
2407   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_FMA_UNSET \
2408    | OPTION_MASK_ISA_FMA4_UNSET | OPTION_MASK_ISA_F16C_UNSET)
2409 #define OPTION_MASK_ISA_FMA_UNSET OPTION_MASK_ISA_FMA
2410
2411 /* SSE4 includes both SSE4.1 and SSE4.2.  -mno-sse4 should the same
2412    as -mno-sse4.1. */
2413 #define OPTION_MASK_ISA_SSE4_UNSET OPTION_MASK_ISA_SSE4_1_UNSET
2414
2415 #define OPTION_MASK_ISA_SSE4A_UNSET \
2416   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_FMA4_UNSET)
2417
2418 #define OPTION_MASK_ISA_FMA4_UNSET \
2419   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_XOP_UNSET)
2420 #define OPTION_MASK_ISA_XOP_UNSET OPTION_MASK_ISA_XOP
2421 #define OPTION_MASK_ISA_LWP_UNSET OPTION_MASK_ISA_LWP
2422
2423 #define OPTION_MASK_ISA_AES_UNSET OPTION_MASK_ISA_AES
2424 #define OPTION_MASK_ISA_PCLMUL_UNSET OPTION_MASK_ISA_PCLMUL
2425 #define OPTION_MASK_ISA_ABM_UNSET OPTION_MASK_ISA_ABM
2426 #define OPTION_MASK_ISA_POPCNT_UNSET OPTION_MASK_ISA_POPCNT
2427 #define OPTION_MASK_ISA_CX16_UNSET OPTION_MASK_ISA_CX16
2428 #define OPTION_MASK_ISA_SAHF_UNSET OPTION_MASK_ISA_SAHF
2429 #define OPTION_MASK_ISA_MOVBE_UNSET OPTION_MASK_ISA_MOVBE
2430 #define OPTION_MASK_ISA_CRC32_UNSET OPTION_MASK_ISA_CRC32
2431
2432 #define OPTION_MASK_ISA_FSGSBASE_UNSET OPTION_MASK_ISA_FSGSBASE
2433 #define OPTION_MASK_ISA_RDRND_UNSET OPTION_MASK_ISA_RDRND
2434 #define OPTION_MASK_ISA_F16C_UNSET OPTION_MASK_ISA_F16C
2435
2436 /* Vectorization library interface and handlers.  */
2437 static tree (*ix86_veclib_handler) (enum built_in_function, tree, tree);
2438
2439 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2440 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2441
2442 /* Processor target table, indexed by processor number */
2443 struct ptt
2444 {
2445   const struct processor_costs *cost;           /* Processor costs */
2446   const int align_loop;                         /* Default alignments.  */
2447   const int align_loop_max_skip;
2448   const int align_jump;
2449   const int align_jump_max_skip;
2450   const int align_func;
2451 };
2452
2453 static const struct ptt processor_target_table[PROCESSOR_max] =
2454 {
2455   {&i386_cost, 4, 3, 4, 3, 4},
2456   {&i486_cost, 16, 15, 16, 15, 16},
2457   {&pentium_cost, 16, 7, 16, 7, 16},
2458   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2459   {&geode_cost, 0, 0, 0, 0, 0},
2460   {&k6_cost, 32, 7, 32, 7, 32},
2461   {&athlon_cost, 16, 7, 16, 7, 16},
2462   {&pentium4_cost, 0, 0, 0, 0, 0},
2463   {&k8_cost, 16, 7, 16, 7, 16},
2464   {&nocona_cost, 0, 0, 0, 0, 0},
2465   {&core2_cost, 16, 10, 16, 10, 16},
2466   /* Core i7 32-bit.  */
2467   {&generic32_cost, 16, 10, 16, 10, 16},
2468   /* Core i7 64-bit.  */
2469   {&generic64_cost, 16, 10, 16, 10, 16},
2470   {&generic32_cost, 16, 7, 16, 7, 16},
2471   {&generic64_cost, 16, 10, 16, 10, 16},
2472   {&amdfam10_cost, 32, 24, 32, 7, 32},
2473   {&bdver1_cost, 32, 24, 32, 7, 32},
2474   {&atom_cost, 16, 7, 16, 7, 16}
2475 };
2476
2477 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2478 {
2479   "generic",
2480   "i386",
2481   "i486",
2482   "pentium",
2483   "pentium-mmx",
2484   "pentiumpro",
2485   "pentium2",
2486   "pentium3",
2487   "pentium4",
2488   "pentium-m",
2489   "prescott",
2490   "nocona",
2491   "core2",
2492   "corei7",
2493   "atom",
2494   "geode",
2495   "k6",
2496   "k6-2",
2497   "k6-3",
2498   "athlon",
2499   "athlon-4",
2500   "k8",
2501   "amdfam10",
2502   "bdver1"
2503 };
2504 \f
2505 /* Return true if a red-zone is in use.  */
2506
2507 static inline bool
2508 ix86_using_red_zone (void)
2509 {
2510   return TARGET_RED_ZONE && !TARGET_64BIT_MS_ABI;
2511 }
2512
2513 /* Implement TARGET_HANDLE_OPTION.  */
2514
2515 static bool
2516 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
2517 {
2518   switch (code)
2519     {
2520     case OPT_mmmx:
2521       if (value)
2522         {
2523           ix86_isa_flags |= OPTION_MASK_ISA_MMX_SET;
2524           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_SET;
2525         }
2526       else
2527         {
2528           ix86_isa_flags &= ~OPTION_MASK_ISA_MMX_UNSET;
2529           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_UNSET;
2530         }
2531       return true;
2532
2533     case OPT_m3dnow:
2534       if (value)
2535         {
2536           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_SET;
2537           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_SET;
2538         }
2539       else
2540         {
2541           ix86_isa_flags &= ~OPTION_MASK_ISA_3DNOW_UNSET;
2542           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_UNSET;
2543         }
2544       return true;
2545
2546     case OPT_m3dnowa:
2547       return false;
2548
2549     case OPT_msse:
2550       if (value)
2551         {
2552           ix86_isa_flags |= OPTION_MASK_ISA_SSE_SET;
2553           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_SET;
2554         }
2555       else
2556         {
2557           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE_UNSET;
2558           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_UNSET;
2559         }
2560       return true;
2561
2562     case OPT_msse2:
2563       if (value)
2564         {
2565           ix86_isa_flags |= OPTION_MASK_ISA_SSE2_SET;
2566           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_SET;
2567         }
2568       else
2569         {
2570           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE2_UNSET;
2571           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_UNSET;
2572         }
2573       return true;
2574
2575     case OPT_msse3:
2576       if (value)
2577         {
2578           ix86_isa_flags |= OPTION_MASK_ISA_SSE3_SET;
2579           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_SET;
2580         }
2581       else
2582         {
2583           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE3_UNSET;
2584           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_UNSET;
2585         }
2586       return true;
2587
2588     case OPT_mssse3:
2589       if (value)
2590         {
2591           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3_SET;
2592           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_SET;
2593         }
2594       else
2595         {
2596           ix86_isa_flags &= ~OPTION_MASK_ISA_SSSE3_UNSET;
2597           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_UNSET;
2598         }
2599       return true;
2600
2601     case OPT_msse4_1:
2602       if (value)
2603         {
2604           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1_SET;
2605           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_SET;
2606         }
2607       else
2608         {
2609           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_1_UNSET;
2610           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_UNSET;
2611         }
2612       return true;
2613
2614     case OPT_msse4_2:
2615       if (value)
2616         {
2617           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2_SET;
2618           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_SET;
2619         }
2620       else
2621         {
2622           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_2_UNSET;
2623           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_UNSET;
2624         }
2625       return true;
2626
2627     case OPT_mavx:
2628       if (value)
2629         {
2630           ix86_isa_flags |= OPTION_MASK_ISA_AVX_SET;
2631           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_SET;
2632         }
2633       else
2634         {
2635           ix86_isa_flags &= ~OPTION_MASK_ISA_AVX_UNSET;
2636           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_UNSET;
2637         }
2638       return true;
2639
2640     case OPT_mfma:
2641       if (value)
2642         {
2643           ix86_isa_flags |= OPTION_MASK_ISA_FMA_SET;
2644           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_SET;
2645         }
2646       else
2647         {
2648           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA_UNSET;
2649           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_UNSET;
2650         }
2651       return true;
2652
2653     case OPT_msse4:
2654       ix86_isa_flags |= OPTION_MASK_ISA_SSE4_SET;
2655       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_SET;
2656       return true;
2657
2658     case OPT_mno_sse4:
2659       ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_UNSET;
2660       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_UNSET;
2661       return true;
2662
2663     case OPT_msse4a:
2664       if (value)
2665         {
2666           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A_SET;
2667           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_SET;
2668         }
2669       else
2670         {
2671           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4A_UNSET;
2672           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_UNSET;
2673         }
2674       return true;
2675
2676     case OPT_mfma4:
2677       if (value)
2678         {
2679           ix86_isa_flags |= OPTION_MASK_ISA_FMA4_SET;
2680           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_SET;
2681         }
2682       else
2683         {
2684           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA4_UNSET;
2685           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_UNSET;
2686         }
2687       return true;
2688
2689    case OPT_mxop:
2690       if (value)
2691         {
2692           ix86_isa_flags |= OPTION_MASK_ISA_XOP_SET;
2693           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_SET;
2694         }
2695       else
2696         {
2697           ix86_isa_flags &= ~OPTION_MASK_ISA_XOP_UNSET;
2698           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_UNSET;
2699         }
2700       return true;
2701
2702    case OPT_mlwp:
2703       if (value)
2704         {
2705           ix86_isa_flags |= OPTION_MASK_ISA_LWP_SET;
2706           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_SET;
2707         }
2708       else
2709         {
2710           ix86_isa_flags &= ~OPTION_MASK_ISA_LWP_UNSET;
2711           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_UNSET;
2712         }
2713       return true;
2714
2715     case OPT_mabm:
2716       if (value)
2717         {
2718           ix86_isa_flags |= OPTION_MASK_ISA_ABM_SET;
2719           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_SET;
2720         }
2721       else
2722         {
2723           ix86_isa_flags &= ~OPTION_MASK_ISA_ABM_UNSET;
2724           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_UNSET;
2725         }
2726       return true;
2727
2728     case OPT_mpopcnt:
2729       if (value)
2730         {
2731           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT_SET;
2732           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_SET;
2733         }
2734       else
2735         {
2736           ix86_isa_flags &= ~OPTION_MASK_ISA_POPCNT_UNSET;
2737           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_UNSET;
2738         }
2739       return true;
2740
2741     case OPT_msahf:
2742       if (value)
2743         {
2744           ix86_isa_flags |= OPTION_MASK_ISA_SAHF_SET;
2745           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_SET;
2746         }
2747       else
2748         {
2749           ix86_isa_flags &= ~OPTION_MASK_ISA_SAHF_UNSET;
2750           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_UNSET;
2751         }
2752       return true;
2753
2754     case OPT_mcx16:
2755       if (value)
2756         {
2757           ix86_isa_flags |= OPTION_MASK_ISA_CX16_SET;
2758           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_SET;
2759         }
2760       else
2761         {
2762           ix86_isa_flags &= ~OPTION_MASK_ISA_CX16_UNSET;
2763           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_UNSET;
2764         }
2765       return true;
2766
2767     case OPT_mmovbe:
2768       if (value)
2769         {
2770           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE_SET;
2771           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_SET;
2772         }
2773       else
2774         {
2775           ix86_isa_flags &= ~OPTION_MASK_ISA_MOVBE_UNSET;
2776           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_UNSET;
2777         }
2778       return true;
2779
2780     case OPT_mcrc32:
2781       if (value)
2782         {
2783           ix86_isa_flags |= OPTION_MASK_ISA_CRC32_SET;
2784           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_SET;
2785         }
2786       else
2787         {
2788           ix86_isa_flags &= ~OPTION_MASK_ISA_CRC32_UNSET;
2789           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_UNSET;
2790         }
2791       return true;
2792
2793     case OPT_maes:
2794       if (value)
2795         {
2796           ix86_isa_flags |= OPTION_MASK_ISA_AES_SET;
2797           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_SET;
2798         }
2799       else
2800         {
2801           ix86_isa_flags &= ~OPTION_MASK_ISA_AES_UNSET;
2802           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_UNSET;
2803         }
2804       return true;
2805
2806     case OPT_mpclmul:
2807       if (value)
2808         {
2809           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL_SET;
2810           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_SET;
2811         }
2812       else
2813         {
2814           ix86_isa_flags &= ~OPTION_MASK_ISA_PCLMUL_UNSET;
2815           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_UNSET;
2816         }
2817       return true;
2818
2819     case OPT_mfsgsbase:
2820       if (value)
2821         {
2822           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE_SET;
2823           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_SET;
2824         }
2825       else
2826         {
2827           ix86_isa_flags &= ~OPTION_MASK_ISA_FSGSBASE_UNSET;
2828           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_UNSET;
2829         }
2830       return true;
2831
2832     case OPT_mrdrnd:
2833       if (value)
2834         {
2835           ix86_isa_flags |= OPTION_MASK_ISA_RDRND_SET;
2836           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_SET;
2837         }
2838       else
2839         {
2840           ix86_isa_flags &= ~OPTION_MASK_ISA_RDRND_UNSET;
2841           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_UNSET;
2842         }
2843       return true;
2844
2845     case OPT_mf16c:
2846       if (value)
2847         {
2848           ix86_isa_flags |= OPTION_MASK_ISA_F16C_SET;
2849           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_SET;
2850         }
2851       else
2852         {
2853           ix86_isa_flags &= ~OPTION_MASK_ISA_F16C_UNSET;
2854           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_UNSET;
2855         }
2856       return true;
2857
2858     default:
2859       return true;
2860     }
2861 }
2862 \f
2863 /* Return a string that documents the current -m options.  The caller is
2864    responsible for freeing the string.  */
2865
2866 static char *
2867 ix86_target_string (int isa, int flags, const char *arch, const char *tune,
2868                     const char *fpmath, bool add_nl_p)
2869 {
2870   struct ix86_target_opts
2871   {
2872     const char *option;         /* option string */
2873     int mask;                   /* isa mask options */
2874   };
2875
2876   /* This table is ordered so that options like -msse4.2 that imply
2877      preceding options while match those first.  */
2878   static struct ix86_target_opts isa_opts[] =
2879   {
2880     { "-m64",           OPTION_MASK_ISA_64BIT },
2881     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2882     { "-mfma",          OPTION_MASK_ISA_FMA },
2883     { "-mxop",          OPTION_MASK_ISA_XOP },
2884     { "-mlwp",          OPTION_MASK_ISA_LWP },
2885     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2886     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2887     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2888     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2889     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2890     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2891     { "-msse",          OPTION_MASK_ISA_SSE },
2892     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2893     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2894     { "-mmmx",          OPTION_MASK_ISA_MMX },
2895     { "-mabm",          OPTION_MASK_ISA_ABM },
2896     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2897     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2898     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2899     { "-maes",          OPTION_MASK_ISA_AES },
2900     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2901     { "-mfsgsbase",     OPTION_MASK_ISA_FSGSBASE },
2902     { "-mrdrnd",        OPTION_MASK_ISA_RDRND },
2903     { "-mf16c",         OPTION_MASK_ISA_F16C },
2904   };
2905
2906   /* Flag options.  */
2907   static struct ix86_target_opts flag_opts[] =
2908   {
2909     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
2910     { "-m80387",                        MASK_80387 },
2911     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
2912     { "-malign-double",                 MASK_ALIGN_DOUBLE },
2913     { "-mcld",                          MASK_CLD },
2914     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
2915     { "-mieee-fp",                      MASK_IEEE_FP },
2916     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
2917     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
2918     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
2919     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
2920     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
2921     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
2922     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
2923     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
2924     { "-mrecip",                        MASK_RECIP },
2925     { "-mrtd",                          MASK_RTD },
2926     { "-msseregparm",                   MASK_SSEREGPARM },
2927     { "-mstack-arg-probe",              MASK_STACK_PROBE },
2928     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
2929     { "-mvect8-ret-in-mem",             MASK_VECT8_RETURNS },
2930     { "-m8bit-idiv",                    MASK_USE_8BIT_IDIV },
2931     { "-mvzeroupper",                   MASK_VZEROUPPER },
2932   };
2933
2934   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
2935
2936   char isa_other[40];
2937   char target_other[40];
2938   unsigned num = 0;
2939   unsigned i, j;
2940   char *ret;
2941   char *ptr;
2942   size_t len;
2943   size_t line_len;
2944   size_t sep_len;
2945
2946   memset (opts, '\0', sizeof (opts));
2947
2948   /* Add -march= option.  */
2949   if (arch)
2950     {
2951       opts[num][0] = "-march=";
2952       opts[num++][1] = arch;
2953     }
2954
2955   /* Add -mtune= option.  */
2956   if (tune)
2957     {
2958       opts[num][0] = "-mtune=";
2959       opts[num++][1] = tune;
2960     }
2961
2962   /* Pick out the options in isa options.  */
2963   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
2964     {
2965       if ((isa & isa_opts[i].mask) != 0)
2966         {
2967           opts[num++][0] = isa_opts[i].option;
2968           isa &= ~ isa_opts[i].mask;
2969         }
2970     }
2971
2972   if (isa && add_nl_p)
2973     {
2974       opts[num++][0] = isa_other;
2975       sprintf (isa_other, "(other isa: %#x)", isa);
2976     }
2977
2978   /* Add flag options.  */
2979   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
2980     {
2981       if ((flags & flag_opts[i].mask) != 0)
2982         {
2983           opts[num++][0] = flag_opts[i].option;
2984           flags &= ~ flag_opts[i].mask;
2985         }
2986     }
2987
2988   if (flags && add_nl_p)
2989     {
2990       opts[num++][0] = target_other;
2991       sprintf (target_other, "(other flags: %#x)", flags);
2992     }
2993
2994   /* Add -fpmath= option.  */
2995   if (fpmath)
2996     {
2997       opts[num][0] = "-mfpmath=";
2998       opts[num++][1] = fpmath;
2999     }
3000
3001   /* Any options?  */
3002   if (num == 0)
3003     return NULL;
3004
3005   gcc_assert (num < ARRAY_SIZE (opts));
3006
3007   /* Size the string.  */
3008   len = 0;
3009   sep_len = (add_nl_p) ? 3 : 1;
3010   for (i = 0; i < num; i++)
3011     {
3012       len += sep_len;
3013       for (j = 0; j < 2; j++)
3014         if (opts[i][j])
3015           len += strlen (opts[i][j]);
3016     }
3017
3018   /* Build the string.  */
3019   ret = ptr = (char *) xmalloc (len);
3020   line_len = 0;
3021
3022   for (i = 0; i < num; i++)
3023     {
3024       size_t len2[2];
3025
3026       for (j = 0; j < 2; j++)
3027         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
3028
3029       if (i != 0)
3030         {
3031           *ptr++ = ' ';
3032           line_len++;
3033
3034           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
3035             {
3036               *ptr++ = '\\';
3037               *ptr++ = '\n';
3038               line_len = 0;
3039             }
3040         }
3041
3042       for (j = 0; j < 2; j++)
3043         if (opts[i][j])
3044           {
3045             memcpy (ptr, opts[i][j], len2[j]);
3046             ptr += len2[j];
3047             line_len += len2[j];
3048           }
3049     }
3050
3051   *ptr = '\0';
3052   gcc_assert (ret + len >= ptr);
3053
3054   return ret;
3055 }
3056
3057 /* Return TRUE if software prefetching is beneficial for the
3058    given CPU. */
3059
3060 static bool
3061 software_prefetching_beneficial_p (void)
3062 {
3063   switch (ix86_tune)
3064     {
3065     case PROCESSOR_GEODE:
3066     case PROCESSOR_K6:
3067     case PROCESSOR_ATHLON:
3068     case PROCESSOR_K8:
3069     case PROCESSOR_AMDFAM10:
3070       return true;
3071
3072     default:
3073       return false;
3074     }
3075 }
3076
3077 /* Return true, if profiling code should be emitted before
3078    prologue. Otherwise it returns false.
3079    Note: For x86 with "hotfix" it is sorried.  */
3080 static bool
3081 ix86_profile_before_prologue (void)
3082 {
3083   return flag_fentry != 0;
3084 }
3085
3086 /* Function that is callable from the debugger to print the current
3087    options.  */
3088 void
3089 ix86_debug_options (void)
3090 {
3091   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
3092                                    ix86_arch_string, ix86_tune_string,
3093                                    ix86_fpmath_string, true);
3094
3095   if (opts)
3096     {
3097       fprintf (stderr, "%s\n\n", opts);
3098       free (opts);
3099     }
3100   else
3101     fputs ("<no options>\n\n", stderr);
3102
3103   return;
3104 }
3105 \f
3106 /* Override various settings based on options.  If MAIN_ARGS_P, the
3107    options are from the command line, otherwise they are from
3108    attributes.  */
3109
3110 static void
3111 ix86_option_override_internal (bool main_args_p)
3112 {
3113   int i;
3114   unsigned int ix86_arch_mask, ix86_tune_mask;
3115   const bool ix86_tune_specified = (ix86_tune_string != NULL);
3116   const char *prefix;
3117   const char *suffix;
3118   const char *sw;
3119
3120   /* Comes from final.c -- no real reason to change it.  */
3121 #define MAX_CODE_ALIGN 16
3122
3123   enum pta_flags
3124     {
3125       PTA_SSE = 1 << 0,
3126       PTA_SSE2 = 1 << 1,
3127       PTA_SSE3 = 1 << 2,
3128       PTA_MMX = 1 << 3,
3129       PTA_PREFETCH_SSE = 1 << 4,
3130       PTA_3DNOW = 1 << 5,
3131       PTA_3DNOW_A = 1 << 6,
3132       PTA_64BIT = 1 << 7,
3133       PTA_SSSE3 = 1 << 8,
3134       PTA_CX16 = 1 << 9,
3135       PTA_POPCNT = 1 << 10,
3136       PTA_ABM = 1 << 11,
3137       PTA_SSE4A = 1 << 12,
3138       PTA_NO_SAHF = 1 << 13,
3139       PTA_SSE4_1 = 1 << 14,
3140       PTA_SSE4_2 = 1 << 15,
3141       PTA_AES = 1 << 16,
3142       PTA_PCLMUL = 1 << 17,
3143       PTA_AVX = 1 << 18,
3144       PTA_FMA = 1 << 19,
3145       PTA_MOVBE = 1 << 20,
3146       PTA_FMA4 = 1 << 21,
3147       PTA_XOP = 1 << 22,
3148       PTA_LWP = 1 << 23,
3149       PTA_FSGSBASE = 1 << 24,
3150       PTA_RDRND = 1 << 25,
3151       PTA_F16C = 1 << 26
3152     };
3153
3154   static struct pta
3155     {
3156       const char *const name;           /* processor name or nickname.  */
3157       const enum processor_type processor;
3158       const enum attr_cpu schedule;
3159       const unsigned /*enum pta_flags*/ flags;
3160     }
3161   const processor_alias_table[] =
3162     {
3163       {"i386", PROCESSOR_I386, CPU_NONE, 0},
3164       {"i486", PROCESSOR_I486, CPU_NONE, 0},
3165       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
3166       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
3167       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
3168       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
3169       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
3170       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
3171       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
3172       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
3173       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
3174       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
3175       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
3176         PTA_MMX | PTA_SSE},
3177       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
3178         PTA_MMX | PTA_SSE},
3179       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
3180         PTA_MMX | PTA_SSE | PTA_SSE2},
3181       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
3182         PTA_MMX |PTA_SSE | PTA_SSE2},
3183       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
3184         PTA_MMX | PTA_SSE | PTA_SSE2},
3185       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
3186         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
3187       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
3188         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3189         | PTA_CX16 | PTA_NO_SAHF},
3190       {"core2", PROCESSOR_CORE2, CPU_CORE2,
3191         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3192         | PTA_SSSE3 | PTA_CX16},
3193       {"corei7", PROCESSOR_COREI7_64, CPU_GENERIC64,
3194        PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3195        | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_CX16},
3196       {"atom", PROCESSOR_ATOM, CPU_ATOM,
3197         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
3198         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
3199       {"geode", PROCESSOR_GEODE, CPU_GEODE,
3200         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A |PTA_PREFETCH_SSE},
3201       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
3202       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3203       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
3204       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
3205         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3206       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
3207         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
3208       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
3209         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3210       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
3211         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3212       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
3213         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
3214       {"x86-64", PROCESSOR_K8, CPU_K8,
3215         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
3216       {"k8", PROCESSOR_K8, CPU_K8,
3217         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3218         | PTA_SSE2 | PTA_NO_SAHF},
3219       {"k8-sse3", PROCESSOR_K8, CPU_K8,
3220         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3221         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3222       {"opteron", PROCESSOR_K8, CPU_K8,
3223         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3224         | PTA_SSE2 | PTA_NO_SAHF},
3225       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
3226         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3227         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3228       {"athlon64", PROCESSOR_K8, CPU_K8,
3229         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3230         | PTA_SSE2 | PTA_NO_SAHF},
3231       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
3232         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3233         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
3234       {"athlon-fx", PROCESSOR_K8, CPU_K8,
3235         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3236         | PTA_SSE2 | PTA_NO_SAHF},
3237       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3238         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3239         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3240       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
3241         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3242         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
3243       {"bdver1", PROCESSOR_BDVER1, CPU_BDVER1,
3244         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
3245         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM
3246         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AES
3247         | PTA_PCLMUL | PTA_AVX | PTA_FMA4 | PTA_XOP | PTA_LWP},
3248       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
3249         0 /* flags are only used for -march switch.  */ },
3250       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
3251         PTA_64BIT /* flags are only used for -march switch.  */ },
3252     };
3253
3254   int const pta_size = ARRAY_SIZE (processor_alias_table);
3255
3256   /* Set up prefix/suffix so the error messages refer to either the command
3257      line argument, or the attribute(target).  */
3258   if (main_args_p)
3259     {
3260       prefix = "-m";
3261       suffix = "";
3262       sw = "switch";
3263     }
3264   else
3265     {
3266       prefix = "option(\"";
3267       suffix = "\")";
3268       sw = "attribute";
3269     }
3270
3271 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3272   SUBTARGET_OVERRIDE_OPTIONS;
3273 #endif
3274
3275 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
3276   SUBSUBTARGET_OVERRIDE_OPTIONS;
3277 #endif
3278
3279   /* -fPIC is the default for x86_64.  */
3280   if (TARGET_MACHO && TARGET_64BIT)
3281     flag_pic = 2;
3282
3283   /* Need to check -mtune=generic first.  */
3284   if (ix86_tune_string)
3285     {
3286       if (!strcmp (ix86_tune_string, "generic")
3287           || !strcmp (ix86_tune_string, "i686")
3288           /* As special support for cross compilers we read -mtune=native
3289              as -mtune=generic.  With native compilers we won't see the
3290              -mtune=native, as it was changed by the driver.  */
3291           || !strcmp (ix86_tune_string, "native"))
3292         {
3293           if (TARGET_64BIT)
3294             ix86_tune_string = "generic64";
3295           else
3296             ix86_tune_string = "generic32";
3297         }
3298       /* If this call is for setting the option attribute, allow the
3299          generic32/generic64 that was previously set.  */
3300       else if (!main_args_p
3301                && (!strcmp (ix86_tune_string, "generic32")
3302                    || !strcmp (ix86_tune_string, "generic64")))
3303         ;
3304       else if (!strncmp (ix86_tune_string, "generic", 7))
3305         error ("bad value (%s) for %stune=%s %s",
3306                ix86_tune_string, prefix, suffix, sw);
3307       else if (!strcmp (ix86_tune_string, "x86-64"))
3308         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated.  Use "
3309                  "%stune=k8%s or %stune=generic%s instead as appropriate.",
3310                  prefix, suffix, prefix, suffix, prefix, suffix);
3311     }
3312   else
3313     {
3314       if (ix86_arch_string)
3315         ix86_tune_string = ix86_arch_string;
3316       if (!ix86_tune_string)
3317         {
3318           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
3319           ix86_tune_defaulted = 1;
3320         }
3321
3322       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
3323          need to use a sensible tune option.  */
3324       if (!strcmp (ix86_tune_string, "generic")
3325           || !strcmp (ix86_tune_string, "x86-64")
3326           || !strcmp (ix86_tune_string, "i686"))
3327         {
3328           if (TARGET_64BIT)
3329             ix86_tune_string = "generic64";
3330           else
3331             ix86_tune_string = "generic32";
3332         }
3333     }
3334
3335   if (ix86_stringop_string)
3336     {
3337       if (!strcmp (ix86_stringop_string, "rep_byte"))
3338         stringop_alg = rep_prefix_1_byte;
3339       else if (!strcmp (ix86_stringop_string, "libcall"))
3340         stringop_alg = libcall;
3341       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
3342         stringop_alg = rep_prefix_4_byte;
3343       else if (!strcmp (ix86_stringop_string, "rep_8byte")
3344                && TARGET_64BIT)
3345         /* rep; movq isn't available in 32-bit code.  */
3346         stringop_alg = rep_prefix_8_byte;
3347       else if (!strcmp (ix86_stringop_string, "byte_loop"))
3348         stringop_alg = loop_1_byte;
3349       else if (!strcmp (ix86_stringop_string, "loop"))
3350         stringop_alg = loop;
3351       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
3352         stringop_alg = unrolled_loop;
3353       else
3354         error ("bad value (%s) for %sstringop-strategy=%s %s",
3355                ix86_stringop_string, prefix, suffix, sw);
3356     }
3357
3358   if (!ix86_arch_string)
3359     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
3360   else
3361     ix86_arch_specified = 1;
3362
3363   /* Validate -mabi= value.  */
3364   if (ix86_abi_string)
3365     {
3366       if (strcmp (ix86_abi_string, "sysv") == 0)
3367         ix86_abi = SYSV_ABI;
3368       else if (strcmp (ix86_abi_string, "ms") == 0)
3369         ix86_abi = MS_ABI;
3370       else
3371         error ("unknown ABI (%s) for %sabi=%s %s",
3372                ix86_abi_string, prefix, suffix, sw);
3373     }
3374   else
3375     ix86_abi = DEFAULT_ABI;
3376
3377   if (ix86_cmodel_string != 0)
3378     {
3379       if (!strcmp (ix86_cmodel_string, "small"))
3380         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3381       else if (!strcmp (ix86_cmodel_string, "medium"))
3382         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
3383       else if (!strcmp (ix86_cmodel_string, "large"))
3384         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
3385       else if (flag_pic)
3386         error ("code model %s does not support PIC mode", ix86_cmodel_string);
3387       else if (!strcmp (ix86_cmodel_string, "32"))
3388         ix86_cmodel = CM_32;
3389       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
3390         ix86_cmodel = CM_KERNEL;
3391       else
3392         error ("bad value (%s) for %scmodel=%s %s",
3393                ix86_cmodel_string, prefix, suffix, sw);
3394     }
3395   else
3396     {
3397       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
3398          use of rip-relative addressing.  This eliminates fixups that
3399          would otherwise be needed if this object is to be placed in a
3400          DLL, and is essentially just as efficient as direct addressing.  */
3401       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
3402         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
3403       else if (TARGET_64BIT)
3404         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3405       else
3406         ix86_cmodel = CM_32;
3407     }
3408   if (ix86_asm_string != 0)
3409     {
3410       if (! TARGET_MACHO
3411           && !strcmp (ix86_asm_string, "intel"))
3412         ix86_asm_dialect = ASM_INTEL;
3413       else if (!strcmp (ix86_asm_string, "att"))
3414         ix86_asm_dialect = ASM_ATT;
3415       else
3416         error ("bad value (%s) for %sasm=%s %s",
3417                ix86_asm_string, prefix, suffix, sw);
3418     }
3419   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
3420     error ("code model %qs not supported in the %s bit mode",
3421            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
3422   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
3423     sorry ("%i-bit mode not compiled in",
3424            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
3425
3426   for (i = 0; i < pta_size; i++)
3427     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
3428       {
3429         ix86_schedule = processor_alias_table[i].schedule;
3430         ix86_arch = processor_alias_table[i].processor;
3431         /* Default cpu tuning to the architecture.  */
3432         ix86_tune = ix86_arch;
3433
3434         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3435           error ("CPU you selected does not support x86-64 "
3436                  "instruction set");
3437
3438         if (processor_alias_table[i].flags & PTA_MMX
3439             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
3440           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
3441         if (processor_alias_table[i].flags & PTA_3DNOW
3442             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
3443           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
3444         if (processor_alias_table[i].flags & PTA_3DNOW_A
3445             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
3446           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
3447         if (processor_alias_table[i].flags & PTA_SSE
3448             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
3449           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
3450         if (processor_alias_table[i].flags & PTA_SSE2
3451             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
3452           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
3453         if (processor_alias_table[i].flags & PTA_SSE3
3454             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
3455           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
3456         if (processor_alias_table[i].flags & PTA_SSSE3
3457             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
3458           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
3459         if (processor_alias_table[i].flags & PTA_SSE4_1
3460             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
3461           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
3462         if (processor_alias_table[i].flags & PTA_SSE4_2
3463             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
3464           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
3465         if (processor_alias_table[i].flags & PTA_AVX
3466             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
3467           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
3468         if (processor_alias_table[i].flags & PTA_FMA
3469             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
3470           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
3471         if (processor_alias_table[i].flags & PTA_SSE4A
3472             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3473           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3474         if (processor_alias_table[i].flags & PTA_FMA4
3475             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3476           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3477         if (processor_alias_table[i].flags & PTA_XOP
3478             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3479           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3480         if (processor_alias_table[i].flags & PTA_LWP
3481             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3482           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3483         if (processor_alias_table[i].flags & PTA_ABM
3484             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3485           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3486         if (processor_alias_table[i].flags & PTA_CX16
3487             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3488           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3489         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3490             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3491           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3492         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3493             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3494           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3495         if (processor_alias_table[i].flags & PTA_MOVBE
3496             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3497           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3498         if (processor_alias_table[i].flags & PTA_AES
3499             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3500           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3501         if (processor_alias_table[i].flags & PTA_PCLMUL
3502             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3503           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3504         if (processor_alias_table[i].flags & PTA_FSGSBASE
3505             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FSGSBASE))
3506           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE;
3507         if (processor_alias_table[i].flags & PTA_RDRND
3508             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_RDRND))
3509           ix86_isa_flags |= OPTION_MASK_ISA_RDRND;
3510         if (processor_alias_table[i].flags & PTA_F16C
3511             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_F16C))
3512           ix86_isa_flags |= OPTION_MASK_ISA_F16C;
3513         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3514           x86_prefetch_sse = true;
3515
3516         break;
3517       }
3518
3519   if (!strcmp (ix86_arch_string, "generic"))
3520     error ("generic CPU can be used only for %stune=%s %s",
3521            prefix, suffix, sw);
3522   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3523     error ("bad value (%s) for %sarch=%s %s",
3524            ix86_arch_string, prefix, suffix, sw);
3525
3526   ix86_arch_mask = 1u << ix86_arch;
3527   for (i = 0; i < X86_ARCH_LAST; ++i)
3528     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3529
3530   for (i = 0; i < pta_size; i++)
3531     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3532       {
3533         ix86_schedule = processor_alias_table[i].schedule;
3534         ix86_tune = processor_alias_table[i].processor;
3535         if (TARGET_64BIT)
3536           {
3537             if (!(processor_alias_table[i].flags & PTA_64BIT))
3538               {
3539                 if (ix86_tune_defaulted)
3540                   {
3541                     ix86_tune_string = "x86-64";
3542                     for (i = 0; i < pta_size; i++)
3543                       if (! strcmp (ix86_tune_string,
3544                                     processor_alias_table[i].name))
3545                         break;
3546                     ix86_schedule = processor_alias_table[i].schedule;
3547                     ix86_tune = processor_alias_table[i].processor;
3548                   }
3549                 else
3550                   error ("CPU you selected does not support x86-64 "
3551                          "instruction set");
3552               }
3553           }
3554         else
3555           {
3556             /* Adjust tuning when compiling for 32-bit ABI.  */
3557             switch (ix86_tune)
3558               {
3559               case PROCESSOR_GENERIC64:
3560                 ix86_tune = PROCESSOR_GENERIC32;
3561                 ix86_schedule = CPU_PENTIUMPRO;
3562                 break;
3563
3564               case PROCESSOR_COREI7_64:
3565                 ix86_tune = PROCESSOR_COREI7_32;
3566                 ix86_schedule = CPU_PENTIUMPRO;
3567                 break;
3568
3569               default:
3570                 break;
3571               }
3572           }
3573         /* Intel CPUs have always interpreted SSE prefetch instructions as
3574            NOPs; so, we can enable SSE prefetch instructions even when
3575            -mtune (rather than -march) points us to a processor that has them.
3576            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3577            higher processors.  */
3578         if (TARGET_CMOVE
3579             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3580           x86_prefetch_sse = true;
3581         break;
3582       }
3583
3584   if (ix86_tune_specified && i == pta_size)
3585     error ("bad value (%s) for %stune=%s %s",
3586            ix86_tune_string, prefix, suffix, sw);
3587
3588   ix86_tune_mask = 1u << ix86_tune;
3589   for (i = 0; i < X86_TUNE_LAST; ++i)
3590     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3591
3592 #ifndef USE_IX86_FRAME_POINTER
3593 #define USE_IX86_FRAME_POINTER 0
3594 #endif
3595
3596 #ifndef USE_X86_64_FRAME_POINTER
3597 #define USE_X86_64_FRAME_POINTER 0
3598 #endif
3599
3600   /* Set the default values for switches whose default depends on TARGET_64BIT
3601      in case they weren't overwritten by command line options.  */
3602   if (TARGET_64BIT)
3603     {
3604       if (optimize > 1 && !global_options_set.x_flag_zee)
3605         flag_zee = 1;
3606       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3607         flag_omit_frame_pointer = !USE_X86_64_FRAME_POINTER;
3608       if (flag_asynchronous_unwind_tables == 2)
3609         flag_unwind_tables = flag_asynchronous_unwind_tables = 1;
3610       if (flag_pcc_struct_return == 2)
3611         flag_pcc_struct_return = 0;
3612     }
3613   else
3614     {
3615       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3616         flag_omit_frame_pointer = !(USE_IX86_FRAME_POINTER || optimize_size);
3617       if (flag_asynchronous_unwind_tables == 2)
3618         flag_asynchronous_unwind_tables = !USE_IX86_FRAME_POINTER;
3619       if (flag_pcc_struct_return == 2)
3620         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
3621     }
3622
3623   if (optimize_size)
3624     ix86_cost = &ix86_size_cost;
3625   else
3626     ix86_cost = processor_target_table[ix86_tune].cost;
3627
3628   /* Arrange to set up i386_stack_locals for all functions.  */
3629   init_machine_status = ix86_init_machine_status;
3630
3631   /* Validate -mregparm= value.  */
3632   if (ix86_regparm_string)
3633     {
3634       if (TARGET_64BIT)
3635         warning (0, "%sregparm%s is ignored in 64-bit mode", prefix, suffix);
3636       i = atoi (ix86_regparm_string);
3637       if (i < 0 || i > REGPARM_MAX)
3638         error ("%sregparm=%d%s is not between 0 and %d",
3639                prefix, i, suffix, REGPARM_MAX);
3640       else
3641         ix86_regparm = i;
3642     }
3643   if (TARGET_64BIT)
3644     ix86_regparm = REGPARM_MAX;
3645
3646   /* If the user has provided any of the -malign-* options,
3647      warn and use that value only if -falign-* is not set.
3648      Remove this code in GCC 3.2 or later.  */
3649   if (ix86_align_loops_string)
3650     {
3651       warning (0, "%salign-loops%s is obsolete, use -falign-loops%s",
3652                prefix, suffix, suffix);
3653       if (align_loops == 0)
3654         {
3655           i = atoi (ix86_align_loops_string);
3656           if (i < 0 || i > MAX_CODE_ALIGN)
3657             error ("%salign-loops=%d%s is not between 0 and %d",
3658                    prefix, i, suffix, MAX_CODE_ALIGN);
3659           else
3660             align_loops = 1 << i;
3661         }
3662     }
3663
3664   if (ix86_align_jumps_string)
3665     {
3666       warning (0, "%salign-jumps%s is obsolete, use -falign-jumps%s",
3667                prefix, suffix, suffix);
3668       if (align_jumps == 0)
3669         {
3670           i = atoi (ix86_align_jumps_string);
3671           if (i < 0 || i > MAX_CODE_ALIGN)
3672             error ("%salign-loops=%d%s is not between 0 and %d",
3673                    prefix, i, suffix, MAX_CODE_ALIGN);
3674           else
3675             align_jumps = 1 << i;
3676         }
3677     }
3678
3679   if (ix86_align_funcs_string)
3680     {
3681       warning (0, "%salign-functions%s is obsolete, use -falign-functions%s",
3682                prefix, suffix, suffix);
3683       if (align_functions == 0)
3684         {
3685           i = atoi (ix86_align_funcs_string);
3686           if (i < 0 || i > MAX_CODE_ALIGN)
3687             error ("%salign-loops=%d%s is not between 0 and %d",
3688                    prefix, i, suffix, MAX_CODE_ALIGN);
3689           else
3690             align_functions = 1 << i;
3691         }
3692     }
3693
3694   /* Default align_* from the processor table.  */
3695   if (align_loops == 0)
3696     {
3697       align_loops = processor_target_table[ix86_tune].align_loop;
3698       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3699     }
3700   if (align_jumps == 0)
3701     {
3702       align_jumps = processor_target_table[ix86_tune].align_jump;
3703       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3704     }
3705   if (align_functions == 0)
3706     {
3707       align_functions = processor_target_table[ix86_tune].align_func;
3708     }
3709
3710   /* Validate -mbranch-cost= value, or provide default.  */
3711   ix86_branch_cost = ix86_cost->branch_cost;
3712   if (ix86_branch_cost_string)
3713     {
3714       i = atoi (ix86_branch_cost_string);
3715       if (i < 0 || i > 5)
3716         error ("%sbranch-cost=%d%s is not between 0 and 5", prefix, i, suffix);
3717       else
3718         ix86_branch_cost = i;
3719     }
3720   if (ix86_section_threshold_string)
3721     {
3722       i = atoi (ix86_section_threshold_string);
3723       if (i < 0)
3724         error ("%slarge-data-threshold=%d%s is negative", prefix, i, suffix);
3725       else
3726         ix86_section_threshold = i;
3727     }
3728
3729   if (ix86_tls_dialect_string)
3730     {
3731       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
3732         ix86_tls_dialect = TLS_DIALECT_GNU;
3733       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
3734         ix86_tls_dialect = TLS_DIALECT_GNU2;
3735       else
3736         error ("bad value (%s) for %stls-dialect=%s %s",
3737                ix86_tls_dialect_string, prefix, suffix, sw);
3738     }
3739
3740   if (ix87_precision_string)
3741     {
3742       i = atoi (ix87_precision_string);
3743       if (i != 32 && i != 64 && i != 80)
3744         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
3745     }
3746
3747   if (TARGET_64BIT)
3748     {
3749       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3750
3751       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3752          explicitly disable any of these.  In particular, disabling SSE and
3753          MMX for kernel code is extremely useful.  */
3754       if (!ix86_arch_specified)
3755       ix86_isa_flags
3756         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3757              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3758
3759       if (TARGET_RTD)
3760         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3761     }
3762   else
3763     {
3764       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3765
3766       if (!ix86_arch_specified)
3767       ix86_isa_flags
3768         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3769
3770       /* i386 ABI does not specify red zone.  It still makes sense to use it
3771          when programmer takes care to stack from being destroyed.  */
3772       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3773         target_flags |= MASK_NO_RED_ZONE;
3774     }
3775
3776   /* Keep nonleaf frame pointers.  */
3777   if (flag_omit_frame_pointer)
3778     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3779   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3780     flag_omit_frame_pointer = 1;
3781
3782   /* If we're doing fast math, we don't care about comparison order
3783      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3784   if (flag_finite_math_only)
3785     target_flags &= ~MASK_IEEE_FP;
3786
3787   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3788      since the insns won't need emulation.  */
3789   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3790     target_flags &= ~MASK_NO_FANCY_MATH_387;
3791
3792   /* Likewise, if the target doesn't have a 387, or we've specified
3793      software floating point, don't use 387 inline intrinsics.  */
3794   if (!TARGET_80387)
3795     target_flags |= MASK_NO_FANCY_MATH_387;
3796
3797   /* Turn on MMX builtins for -msse.  */
3798   if (TARGET_SSE)
3799     {
3800       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3801       x86_prefetch_sse = true;
3802     }
3803
3804   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3805   if (TARGET_SSE4_2 || TARGET_ABM)
3806     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3807
3808   /* Validate -mpreferred-stack-boundary= value or default it to
3809      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3810   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3811   if (ix86_preferred_stack_boundary_string)
3812     {
3813       int min = (TARGET_64BIT ? 4 : 2);
3814       int max = (TARGET_SEH ? 4 : 12);
3815
3816       i = atoi (ix86_preferred_stack_boundary_string);
3817       if (i < min || i > max)
3818         {
3819           if (min == max)
3820             error ("%spreferred-stack-boundary%s is not supported "
3821                    "for this target", prefix, suffix);
3822           else
3823             error ("%spreferred-stack-boundary=%d%s is not between %d and %d",
3824                    prefix, i, suffix, min, max);
3825         }
3826       else
3827         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
3828     }
3829
3830   /* Set the default value for -mstackrealign.  */
3831   if (ix86_force_align_arg_pointer == -1)
3832     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3833
3834   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3835
3836   /* Validate -mincoming-stack-boundary= value or default it to
3837      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3838   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3839   if (ix86_incoming_stack_boundary_string)
3840     {
3841       i = atoi (ix86_incoming_stack_boundary_string);
3842       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3843         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3844                i, TARGET_64BIT ? 4 : 2);
3845       else
3846         {
3847           ix86_user_incoming_stack_boundary = (1 << i) * BITS_PER_UNIT;
3848           ix86_incoming_stack_boundary
3849             = ix86_user_incoming_stack_boundary;
3850         }
3851     }
3852
3853   /* Accept -msseregparm only if at least SSE support is enabled.  */
3854   if (TARGET_SSEREGPARM
3855       && ! TARGET_SSE)
3856     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3857
3858   ix86_fpmath = TARGET_FPMATH_DEFAULT;
3859   if (ix86_fpmath_string != 0)
3860     {
3861       if (! strcmp (ix86_fpmath_string, "387"))
3862         ix86_fpmath = FPMATH_387;
3863       else if (! strcmp (ix86_fpmath_string, "sse"))
3864         {
3865           if (!TARGET_SSE)
3866             {
3867               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3868               ix86_fpmath = FPMATH_387;
3869             }
3870           else
3871             ix86_fpmath = FPMATH_SSE;
3872         }
3873       else if (! strcmp (ix86_fpmath_string, "387,sse")
3874                || ! strcmp (ix86_fpmath_string, "387+sse")
3875                || ! strcmp (ix86_fpmath_string, "sse,387")
3876                || ! strcmp (ix86_fpmath_string, "sse+387")
3877                || ! strcmp (ix86_fpmath_string, "both"))
3878         {
3879           if (!TARGET_SSE)
3880             {
3881               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3882               ix86_fpmath = FPMATH_387;
3883             }
3884           else if (!TARGET_80387)
3885             {
3886               warning (0, "387 instruction set disabled, using SSE arithmetics");
3887               ix86_fpmath = FPMATH_SSE;
3888             }
3889           else
3890             ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
3891         }
3892       else
3893         error ("bad value (%s) for %sfpmath=%s %s",
3894                ix86_fpmath_string, prefix, suffix, sw);
3895     }
3896
3897   /* If the i387 is disabled, then do not return values in it. */
3898   if (!TARGET_80387)
3899     target_flags &= ~MASK_FLOAT_RETURNS;
3900
3901   /* Use external vectorized library in vectorizing intrinsics.  */
3902   if (ix86_veclibabi_string)
3903     {
3904       if (strcmp (ix86_veclibabi_string, "svml") == 0)
3905         ix86_veclib_handler = ix86_veclibabi_svml;
3906       else if (strcmp (ix86_veclibabi_string, "acml") == 0)
3907         ix86_veclib_handler = ix86_veclibabi_acml;
3908       else
3909         error ("unknown vectorization library ABI type (%s) for "
3910                "%sveclibabi=%s %s", ix86_veclibabi_string,
3911                prefix, suffix, sw);
3912     }
3913
3914   if ((!USE_IX86_FRAME_POINTER
3915        || (x86_accumulate_outgoing_args & ix86_tune_mask))
3916       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3917       && !optimize_size)
3918     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3919
3920   /* ??? Unwind info is not correct around the CFG unless either a frame
3921      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
3922      unwind info generation to be aware of the CFG and propagating states
3923      around edges.  */
3924   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
3925        || flag_exceptions || flag_non_call_exceptions)
3926       && flag_omit_frame_pointer
3927       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3928     {
3929       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3930         warning (0, "unwind tables currently require either a frame pointer "
3931                  "or %saccumulate-outgoing-args%s for correctness",
3932                  prefix, suffix);
3933       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3934     }
3935
3936   /* If stack probes are required, the space used for large function
3937      arguments on the stack must also be probed, so enable
3938      -maccumulate-outgoing-args so this happens in the prologue.  */
3939   if (TARGET_STACK_PROBE
3940       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3941     {
3942       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3943         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
3944                  "for correctness", prefix, suffix);
3945       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3946     }
3947
3948   /* For sane SSE instruction set generation we need fcomi instruction.
3949      It is safe to enable all CMOVE instructions.  */
3950   if (TARGET_SSE)
3951     TARGET_CMOVE = 1;
3952
3953   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
3954   {
3955     char *p;
3956     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
3957     p = strchr (internal_label_prefix, 'X');
3958     internal_label_prefix_len = p - internal_label_prefix;
3959     *p = '\0';
3960   }
3961
3962   /* When scheduling description is not available, disable scheduler pass
3963      so it won't slow down the compilation and make x87 code slower.  */
3964   if (!TARGET_SCHEDULE)
3965     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
3966
3967   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3968                          ix86_cost->simultaneous_prefetches,
3969                          global_options.x_param_values,
3970                          global_options_set.x_param_values);
3971   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, ix86_cost->prefetch_block,
3972                          global_options.x_param_values,
3973                          global_options_set.x_param_values);
3974   maybe_set_param_value (PARAM_L1_CACHE_SIZE, ix86_cost->l1_cache_size,
3975                          global_options.x_param_values,
3976                          global_options_set.x_param_values);
3977   maybe_set_param_value (PARAM_L2_CACHE_SIZE, ix86_cost->l2_cache_size,
3978                          global_options.x_param_values,
3979                          global_options_set.x_param_values);
3980
3981   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
3982   if (flag_prefetch_loop_arrays < 0
3983       && HAVE_prefetch
3984       && optimize >= 3
3985       && software_prefetching_beneficial_p ())
3986     flag_prefetch_loop_arrays = 1;
3987
3988   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3989      can be optimized to ap = __builtin_next_arg (0).  */
3990   if (!TARGET_64BIT && !flag_split_stack)
3991     targetm.expand_builtin_va_start = NULL;
3992
3993   if (TARGET_64BIT)
3994     {
3995       ix86_gen_leave = gen_leave_rex64;
3996       ix86_gen_add3 = gen_adddi3;
3997       ix86_gen_sub3 = gen_subdi3;
3998       ix86_gen_sub3_carry = gen_subdi3_carry;
3999       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
4000       ix86_gen_monitor = gen_sse3_monitor64;
4001       ix86_gen_andsp = gen_anddi3;
4002       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_di;
4003       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probedi;
4004       ix86_gen_probe_stack_range = gen_probe_stack_rangedi;
4005     }
4006   else
4007     {
4008       ix86_gen_leave = gen_leave;
4009       ix86_gen_add3 = gen_addsi3;
4010       ix86_gen_sub3 = gen_subsi3;
4011       ix86_gen_sub3_carry = gen_subsi3_carry;
4012       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
4013       ix86_gen_monitor = gen_sse3_monitor;
4014       ix86_gen_andsp = gen_andsi3;
4015       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_si;
4016       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probesi;
4017       ix86_gen_probe_stack_range = gen_probe_stack_rangesi;
4018     }
4019
4020 #ifdef USE_IX86_CLD
4021   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
4022   if (!TARGET_64BIT)
4023     target_flags |= MASK_CLD & ~target_flags_explicit;
4024 #endif
4025
4026   if (!TARGET_64BIT && flag_pic)
4027     {
4028       if (flag_fentry > 0)
4029         sorry ("-mfentry isn't supported for 32-bit in combination with -fpic");
4030       flag_fentry = 0;
4031     }
4032   else if (TARGET_SEH)
4033     {
4034       if (flag_fentry == 0)
4035         sorry ("-mno-fentry isn't compatible with SEH");
4036       flag_fentry = 1;
4037     }
4038   else if (flag_fentry < 0)
4039    {
4040 #if defined(PROFILE_BEFORE_PROLOGUE)
4041      flag_fentry = 1;
4042 #else
4043      flag_fentry = 0;
4044 #endif
4045    }
4046
4047   /* Save the initial options in case the user does function specific options */
4048   if (main_args_p)
4049     target_option_default_node = target_option_current_node
4050       = build_target_option_node ();
4051
4052   if (TARGET_AVX)
4053     {
4054       /* Enable vzeroupper pass by default for TARGET_AVX.  */
4055       if (!(target_flags_explicit & MASK_VZEROUPPER))
4056         target_flags |= MASK_VZEROUPPER;
4057     }
4058   else 
4059     {
4060       /* Disable vzeroupper pass if TARGET_AVX is disabled.  */
4061       target_flags &= ~MASK_VZEROUPPER;
4062     }
4063 }
4064
4065 /* Return TRUE if type TYPE and mode MODE use 256bit AVX modes.  */
4066
4067 static bool
4068 use_avx256_p (enum machine_mode mode, const_tree type)
4069 {
4070   return (VALID_AVX256_REG_MODE (mode)
4071           || (type
4072               && TREE_CODE (type) == VECTOR_TYPE
4073               && int_size_in_bytes (type) == 32));
4074 }
4075
4076 /* Return TRUE if VAL is passed in register with 256bit AVX modes.  */
4077
4078 static bool
4079 function_pass_avx256_p (const_rtx val)
4080 {
4081   if (!val)
4082     return false;
4083
4084   if (REG_P (val) && VALID_AVX256_REG_MODE (GET_MODE (val)))
4085     return true;
4086
4087   if (GET_CODE (val) == PARALLEL)
4088     {
4089       int i;
4090       rtx r;
4091
4092       for (i = XVECLEN (val, 0) - 1; i >= 0; i--)
4093         {
4094           r = XVECEXP (val, 0, i);
4095           if (GET_CODE (r) == EXPR_LIST
4096               && XEXP (r, 0)
4097               && REG_P (XEXP (r, 0))
4098               && (GET_MODE (XEXP (r, 0)) == OImode
4099                   || VALID_AVX256_REG_MODE (GET_MODE (XEXP (r, 0)))))
4100             return true;
4101         }
4102     }
4103
4104   return false;
4105 }
4106
4107 /* Implement the TARGET_OPTION_OVERRIDE hook.  */
4108
4109 static void
4110 ix86_option_override (void)
4111 {
4112   ix86_option_override_internal (true);
4113 }
4114
4115 /* Update register usage after having seen the compiler flags.  */
4116
4117 void
4118 ix86_conditional_register_usage (void)
4119 {
4120   int i;
4121   unsigned int j;
4122
4123   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4124     {
4125       if (fixed_regs[i] > 1)
4126         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
4127       if (call_used_regs[i] > 1)
4128         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
4129     }
4130
4131   /* The PIC register, if it exists, is fixed.  */
4132   j = PIC_OFFSET_TABLE_REGNUM;
4133   if (j != INVALID_REGNUM)
4134     fixed_regs[j] = call_used_regs[j] = 1;
4135
4136   /* The MS_ABI changes the set of call-used registers.  */
4137   if (TARGET_64BIT && ix86_cfun_abi () == MS_ABI)
4138     {
4139       call_used_regs[SI_REG] = 0;
4140       call_used_regs[DI_REG] = 0;
4141       call_used_regs[XMM6_REG] = 0;
4142       call_used_regs[XMM7_REG] = 0;
4143       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
4144         call_used_regs[i] = 0;
4145     }
4146
4147   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
4148      other call-clobbered regs for 64-bit.  */
4149   if (TARGET_64BIT)
4150     {
4151       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
4152
4153       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4154         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
4155             && call_used_regs[i])
4156           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
4157     }
4158
4159   /* If MMX is disabled, squash the registers.  */
4160   if (! TARGET_MMX)
4161     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4162       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
4163         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
4164
4165   /* If SSE is disabled, squash the registers.  */
4166   if (! TARGET_SSE)
4167     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4168       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
4169         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
4170
4171   /* If the FPU is disabled, squash the registers.  */
4172   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
4173     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4174       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
4175         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
4176
4177   /* If 32-bit, squash the 64-bit registers.  */
4178   if (! TARGET_64BIT)
4179     {
4180       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
4181         reg_names[i] = "";
4182       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
4183         reg_names[i] = "";
4184     }
4185 }
4186
4187 \f
4188 /* Save the current options */
4189
4190 static void
4191 ix86_function_specific_save (struct cl_target_option *ptr)
4192 {
4193   ptr->arch = ix86_arch;
4194   ptr->schedule = ix86_schedule;
4195   ptr->tune = ix86_tune;
4196   ptr->fpmath = ix86_fpmath;
4197   ptr->branch_cost = ix86_branch_cost;
4198   ptr->tune_defaulted = ix86_tune_defaulted;
4199   ptr->arch_specified = ix86_arch_specified;
4200   ptr->ix86_isa_flags_explicit = ix86_isa_flags_explicit;
4201   ptr->ix86_target_flags_explicit = target_flags_explicit;
4202
4203   /* The fields are char but the variables are not; make sure the
4204      values fit in the fields.  */
4205   gcc_assert (ptr->arch == ix86_arch);
4206   gcc_assert (ptr->schedule == ix86_schedule);
4207   gcc_assert (ptr->tune == ix86_tune);
4208   gcc_assert (ptr->fpmath == ix86_fpmath);
4209   gcc_assert (ptr->branch_cost == ix86_branch_cost);
4210 }
4211
4212 /* Restore the current options */
4213
4214 static void
4215 ix86_function_specific_restore (struct cl_target_option *ptr)
4216 {
4217   enum processor_type old_tune = ix86_tune;
4218   enum processor_type old_arch = ix86_arch;
4219   unsigned int ix86_arch_mask, ix86_tune_mask;
4220   int i;
4221
4222   ix86_arch = (enum processor_type) ptr->arch;
4223   ix86_schedule = (enum attr_cpu) ptr->schedule;
4224   ix86_tune = (enum processor_type) ptr->tune;
4225   ix86_fpmath = (enum fpmath_unit) ptr->fpmath;
4226   ix86_branch_cost = ptr->branch_cost;
4227   ix86_tune_defaulted = ptr->tune_defaulted;
4228   ix86_arch_specified = ptr->arch_specified;
4229   ix86_isa_flags_explicit = ptr->ix86_isa_flags_explicit;
4230   target_flags_explicit = ptr->ix86_target_flags_explicit;
4231
4232   /* Recreate the arch feature tests if the arch changed */
4233   if (old_arch != ix86_arch)
4234     {
4235       ix86_arch_mask = 1u << ix86_arch;
4236       for (i = 0; i < X86_ARCH_LAST; ++i)
4237         ix86_arch_features[i]
4238           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
4239     }
4240
4241   /* Recreate the tune optimization tests */
4242   if (old_tune != ix86_tune)
4243     {
4244       ix86_tune_mask = 1u << ix86_tune;
4245       for (i = 0; i < X86_TUNE_LAST; ++i)
4246         ix86_tune_features[i]
4247           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
4248     }
4249 }
4250
4251 /* Print the current options */
4252
4253 static void
4254 ix86_function_specific_print (FILE *file, int indent,
4255                               struct cl_target_option *ptr)
4256 {
4257   char *target_string
4258     = ix86_target_string (ptr->x_ix86_isa_flags, ptr->x_target_flags,
4259                           NULL, NULL, NULL, false);
4260
4261   fprintf (file, "%*sarch = %d (%s)\n",
4262            indent, "",
4263            ptr->arch,
4264            ((ptr->arch < TARGET_CPU_DEFAULT_max)
4265             ? cpu_names[ptr->arch]
4266             : "<unknown>"));
4267
4268   fprintf (file, "%*stune = %d (%s)\n",
4269            indent, "",
4270            ptr->tune,
4271            ((ptr->tune < TARGET_CPU_DEFAULT_max)
4272             ? cpu_names[ptr->tune]
4273             : "<unknown>"));
4274
4275   fprintf (file, "%*sfpmath = %d%s%s\n", indent, "", ptr->fpmath,
4276            (ptr->fpmath & FPMATH_387) ? ", 387" : "",
4277            (ptr->fpmath & FPMATH_SSE) ? ", sse" : "");
4278   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
4279
4280   if (target_string)
4281     {
4282       fprintf (file, "%*s%s\n", indent, "", target_string);
4283       free (target_string);
4284     }
4285 }
4286
4287 \f
4288 /* Inner function to process the attribute((target(...))), take an argument and
4289    set the current options from the argument. If we have a list, recursively go
4290    over the list.  */
4291
4292 static bool
4293 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[])
4294 {
4295   char *next_optstr;
4296   bool ret = true;
4297
4298 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
4299 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
4300 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
4301 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
4302
4303   enum ix86_opt_type
4304   {
4305     ix86_opt_unknown,
4306     ix86_opt_yes,
4307     ix86_opt_no,
4308     ix86_opt_str,
4309     ix86_opt_isa
4310   };
4311
4312   static const struct
4313   {
4314     const char *string;
4315     size_t len;
4316     enum ix86_opt_type type;
4317     int opt;
4318     int mask;
4319   } attrs[] = {
4320     /* isa options */
4321     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
4322     IX86_ATTR_ISA ("abm",       OPT_mabm),
4323     IX86_ATTR_ISA ("aes",       OPT_maes),
4324     IX86_ATTR_ISA ("avx",       OPT_mavx),
4325     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
4326     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
4327     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
4328     IX86_ATTR_ISA ("sse",       OPT_msse),
4329     IX86_ATTR_ISA ("sse2",      OPT_msse2),
4330     IX86_ATTR_ISA ("sse3",      OPT_msse3),
4331     IX86_ATTR_ISA ("sse4",      OPT_msse4),
4332     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
4333     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
4334     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
4335     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
4336     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
4337     IX86_ATTR_ISA ("xop",       OPT_mxop),
4338     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
4339     IX86_ATTR_ISA ("fsgsbase",  OPT_mfsgsbase),
4340     IX86_ATTR_ISA ("rdrnd",     OPT_mrdrnd),
4341     IX86_ATTR_ISA ("f16c",      OPT_mf16c),
4342
4343     /* string options */
4344     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
4345     IX86_ATTR_STR ("fpmath=",   IX86_FUNCTION_SPECIFIC_FPMATH),
4346     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
4347
4348     /* flag options */
4349     IX86_ATTR_YES ("cld",
4350                    OPT_mcld,
4351                    MASK_CLD),
4352
4353     IX86_ATTR_NO ("fancy-math-387",
4354                   OPT_mfancy_math_387,
4355                   MASK_NO_FANCY_MATH_387),
4356
4357     IX86_ATTR_YES ("ieee-fp",
4358                    OPT_mieee_fp,
4359                    MASK_IEEE_FP),
4360
4361     IX86_ATTR_YES ("inline-all-stringops",
4362                    OPT_minline_all_stringops,
4363                    MASK_INLINE_ALL_STRINGOPS),
4364
4365     IX86_ATTR_YES ("inline-stringops-dynamically",
4366                    OPT_minline_stringops_dynamically,
4367                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
4368
4369     IX86_ATTR_NO ("align-stringops",
4370                   OPT_mno_align_stringops,
4371                   MASK_NO_ALIGN_STRINGOPS),
4372
4373     IX86_ATTR_YES ("recip",
4374                    OPT_mrecip,
4375                    MASK_RECIP),
4376
4377   };
4378
4379   /* If this is a list, recurse to get the options.  */
4380   if (TREE_CODE (args) == TREE_LIST)
4381     {
4382       bool ret = true;
4383
4384       for (; args; args = TREE_CHAIN (args))
4385         if (TREE_VALUE (args)
4386             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args), p_strings))
4387           ret = false;
4388
4389       return ret;
4390     }
4391
4392   else if (TREE_CODE (args) != STRING_CST)
4393     gcc_unreachable ();
4394
4395   /* Handle multiple arguments separated by commas.  */
4396   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
4397
4398   while (next_optstr && *next_optstr != '\0')
4399     {
4400       char *p = next_optstr;
4401       char *orig_p = p;
4402       char *comma = strchr (next_optstr, ',');
4403       const char *opt_string;
4404       size_t len, opt_len;
4405       int opt;
4406       bool opt_set_p;
4407       char ch;
4408       unsigned i;
4409       enum ix86_opt_type type = ix86_opt_unknown;
4410       int mask = 0;
4411
4412       if (comma)
4413         {
4414           *comma = '\0';
4415           len = comma - next_optstr;
4416           next_optstr = comma + 1;
4417         }
4418       else
4419         {
4420           len = strlen (p);
4421           next_optstr = NULL;
4422         }
4423
4424       /* Recognize no-xxx.  */
4425       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
4426         {
4427           opt_set_p = false;
4428           p += 3;
4429           len -= 3;
4430         }
4431       else
4432         opt_set_p = true;
4433
4434       /* Find the option.  */
4435       ch = *p;
4436       opt = N_OPTS;
4437       for (i = 0; i < ARRAY_SIZE (attrs); i++)
4438         {
4439           type = attrs[i].type;
4440           opt_len = attrs[i].len;
4441           if (ch == attrs[i].string[0]
4442               && ((type != ix86_opt_str) ? len == opt_len : len > opt_len)
4443               && memcmp (p, attrs[i].string, opt_len) == 0)
4444             {
4445               opt = attrs[i].opt;
4446               mask = attrs[i].mask;
4447               opt_string = attrs[i].string;
4448               break;
4449             }
4450         }
4451
4452       /* Process the option.  */
4453       if (opt == N_OPTS)
4454         {
4455           error ("attribute(target(\"%s\")) is unknown", orig_p);
4456           ret = false;
4457         }
4458
4459       else if (type == ix86_opt_isa)
4460         ix86_handle_option (opt, p, opt_set_p);
4461
4462       else if (type == ix86_opt_yes || type == ix86_opt_no)
4463         {
4464           if (type == ix86_opt_no)
4465             opt_set_p = !opt_set_p;
4466
4467           if (opt_set_p)
4468             target_flags |= mask;
4469           else
4470             target_flags &= ~mask;
4471         }
4472
4473       else if (type == ix86_opt_str)
4474         {
4475           if (p_strings[opt])
4476             {
4477               error ("option(\"%s\") was already specified", opt_string);
4478               ret = false;
4479             }
4480           else
4481             p_strings[opt] = xstrdup (p + opt_len);
4482         }
4483
4484       else
4485         gcc_unreachable ();
4486     }
4487
4488   return ret;
4489 }
4490
4491 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
4492
4493 tree
4494 ix86_valid_target_attribute_tree (tree args)
4495 {
4496   const char *orig_arch_string = ix86_arch_string;
4497   const char *orig_tune_string = ix86_tune_string;
4498   const char *orig_fpmath_string = ix86_fpmath_string;
4499   int orig_tune_defaulted = ix86_tune_defaulted;
4500   int orig_arch_specified = ix86_arch_specified;
4501   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL, NULL };
4502   tree t = NULL_TREE;
4503   int i;
4504   struct cl_target_option *def
4505     = TREE_TARGET_OPTION (target_option_default_node);
4506
4507   /* Process each of the options on the chain.  */
4508   if (! ix86_valid_target_attribute_inner_p (args, option_strings))
4509     return NULL_TREE;
4510
4511   /* If the changed options are different from the default, rerun
4512      ix86_option_override_internal, and then save the options away.
4513      The string options are are attribute options, and will be undone
4514      when we copy the save structure.  */
4515   if (ix86_isa_flags != def->x_ix86_isa_flags
4516       || target_flags != def->x_target_flags
4517       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
4518       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
4519       || option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4520     {
4521       /* If we are using the default tune= or arch=, undo the string assigned,
4522          and use the default.  */
4523       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
4524         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
4525       else if (!orig_arch_specified)
4526         ix86_arch_string = NULL;
4527
4528       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
4529         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
4530       else if (orig_tune_defaulted)
4531         ix86_tune_string = NULL;
4532
4533       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
4534       if (option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4535         ix86_fpmath_string = option_strings[IX86_FUNCTION_SPECIFIC_FPMATH];
4536       else if (!TARGET_64BIT && TARGET_SSE)
4537         ix86_fpmath_string = "sse,387";
4538
4539       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
4540       ix86_option_override_internal (false);
4541
4542       /* Add any builtin functions with the new isa if any.  */
4543       ix86_add_new_builtins (ix86_isa_flags);
4544
4545       /* Save the current options unless we are validating options for
4546          #pragma.  */
4547       t = build_target_option_node ();
4548
4549       ix86_arch_string = orig_arch_string;
4550       ix86_tune_string = orig_tune_string;
4551       ix86_fpmath_string = orig_fpmath_string;
4552
4553       /* Free up memory allocated to hold the strings */
4554       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
4555         if (option_strings[i])
4556           free (option_strings[i]);
4557     }
4558
4559   return t;
4560 }
4561
4562 /* Hook to validate attribute((target("string"))).  */
4563
4564 static bool
4565 ix86_valid_target_attribute_p (tree fndecl,
4566                                tree ARG_UNUSED (name),
4567                                tree args,
4568                                int ARG_UNUSED (flags))
4569 {
4570   struct cl_target_option cur_target;
4571   bool ret = true;
4572   tree old_optimize = build_optimization_node ();
4573   tree new_target, new_optimize;
4574   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
4575
4576   /* If the function changed the optimization levels as well as setting target
4577      options, start with the optimizations specified.  */
4578   if (func_optimize && func_optimize != old_optimize)
4579     cl_optimization_restore (&global_options,
4580                              TREE_OPTIMIZATION (func_optimize));
4581
4582   /* The target attributes may also change some optimization flags, so update
4583      the optimization options if necessary.  */
4584   cl_target_option_save (&cur_target, &global_options);
4585   new_target = ix86_valid_target_attribute_tree (args);
4586   new_optimize = build_optimization_node ();
4587
4588   if (!new_target)
4589     ret = false;
4590
4591   else if (fndecl)
4592     {
4593       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
4594
4595       if (old_optimize != new_optimize)
4596         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
4597     }
4598
4599   cl_target_option_restore (&global_options, &cur_target);
4600
4601   if (old_optimize != new_optimize)
4602     cl_optimization_restore (&global_options,
4603                              TREE_OPTIMIZATION (old_optimize));
4604
4605   return ret;
4606 }
4607
4608 \f
4609 /* Hook to determine if one function can safely inline another.  */
4610
4611 static bool
4612 ix86_can_inline_p (tree caller, tree callee)
4613 {
4614   bool ret = false;
4615   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
4616   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
4617
4618   /* If callee has no option attributes, then it is ok to inline.  */
4619   if (!callee_tree)
4620     ret = true;
4621
4622   /* If caller has no option attributes, but callee does then it is not ok to
4623      inline.  */
4624   else if (!caller_tree)
4625     ret = false;
4626
4627   else
4628     {
4629       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
4630       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
4631
4632       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
4633          can inline a SSE2 function but a SSE2 function can't inline a SSE4
4634          function.  */
4635       if ((caller_opts->x_ix86_isa_flags & callee_opts->x_ix86_isa_flags)
4636           != callee_opts->x_ix86_isa_flags)
4637         ret = false;
4638
4639       /* See if we have the same non-isa options.  */
4640       else if (caller_opts->x_target_flags != callee_opts->x_target_flags)
4641         ret = false;
4642
4643       /* See if arch, tune, etc. are the same.  */
4644       else if (caller_opts->arch != callee_opts->arch)
4645         ret = false;
4646
4647       else if (caller_opts->tune != callee_opts->tune)
4648         ret = false;
4649
4650       else if (caller_opts->fpmath != callee_opts->fpmath)
4651         ret = false;
4652
4653       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4654         ret = false;
4655
4656       else
4657         ret = true;
4658     }
4659
4660   return ret;
4661 }
4662
4663 \f
4664 /* Remember the last target of ix86_set_current_function.  */
4665 static GTY(()) tree ix86_previous_fndecl;
4666
4667 /* Establish appropriate back-end context for processing the function
4668    FNDECL.  The argument might be NULL to indicate processing at top
4669    level, outside of any function scope.  */
4670 static void
4671 ix86_set_current_function (tree fndecl)
4672 {
4673   /* Only change the context if the function changes.  This hook is called
4674      several times in the course of compiling a function, and we don't want to
4675      slow things down too much or call target_reinit when it isn't safe.  */
4676   if (fndecl && fndecl != ix86_previous_fndecl)
4677     {
4678       tree old_tree = (ix86_previous_fndecl
4679                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4680                        : NULL_TREE);
4681
4682       tree new_tree = (fndecl
4683                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4684                        : NULL_TREE);
4685
4686       ix86_previous_fndecl = fndecl;
4687       if (old_tree == new_tree)
4688         ;
4689
4690       else if (new_tree)
4691         {
4692           cl_target_option_restore (&global_options,
4693                                     TREE_TARGET_OPTION (new_tree));
4694           target_reinit ();
4695         }
4696
4697       else if (old_tree)
4698         {
4699           struct cl_target_option *def
4700             = TREE_TARGET_OPTION (target_option_current_node);
4701
4702           cl_target_option_restore (&global_options, def);
4703           target_reinit ();
4704         }
4705     }
4706 }
4707
4708 \f
4709 /* Return true if this goes in large data/bss.  */
4710
4711 static bool
4712 ix86_in_large_data_p (tree exp)
4713 {
4714   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4715     return false;
4716
4717   /* Functions are never large data.  */
4718   if (TREE_CODE (exp) == FUNCTION_DECL)
4719     return false;
4720
4721   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4722     {
4723       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4724       if (strcmp (section, ".ldata") == 0
4725           || strcmp (section, ".lbss") == 0)
4726         return true;
4727       return false;
4728     }
4729   else
4730     {
4731       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4732
4733       /* If this is an incomplete type with size 0, then we can't put it
4734          in data because it might be too big when completed.  */
4735       if (!size || size > ix86_section_threshold)
4736         return true;
4737     }
4738
4739   return false;
4740 }
4741
4742 /* Switch to the appropriate section for output of DECL.
4743    DECL is either a `VAR_DECL' node or a constant of some sort.
4744    RELOC indicates whether forming the initial value of DECL requires
4745    link-time relocations.  */
4746
4747 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4748         ATTRIBUTE_UNUSED;
4749
4750 static section *
4751 x86_64_elf_select_section (tree decl, int reloc,
4752                            unsigned HOST_WIDE_INT align)
4753 {
4754   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4755       && ix86_in_large_data_p (decl))
4756     {
4757       const char *sname = NULL;
4758       unsigned int flags = SECTION_WRITE;
4759       switch (categorize_decl_for_section (decl, reloc))
4760         {
4761         case SECCAT_DATA:
4762           sname = ".ldata";
4763           break;
4764         case SECCAT_DATA_REL:
4765           sname = ".ldata.rel";
4766           break;
4767         case SECCAT_DATA_REL_LOCAL:
4768           sname = ".ldata.rel.local";
4769           break;
4770         case SECCAT_DATA_REL_RO:
4771           sname = ".ldata.rel.ro";
4772           break;
4773         case SECCAT_DATA_REL_RO_LOCAL:
4774           sname = ".ldata.rel.ro.local";
4775           break;
4776         case SECCAT_BSS:
4777           sname = ".lbss";
4778           flags |= SECTION_BSS;
4779           break;
4780         case SECCAT_RODATA:
4781         case SECCAT_RODATA_MERGE_STR:
4782         case SECCAT_RODATA_MERGE_STR_INIT:
4783         case SECCAT_RODATA_MERGE_CONST:
4784           sname = ".lrodata";
4785           flags = 0;
4786           break;
4787         case SECCAT_SRODATA:
4788         case SECCAT_SDATA:
4789         case SECCAT_SBSS:
4790           gcc_unreachable ();
4791         case SECCAT_TEXT:
4792         case SECCAT_TDATA:
4793         case SECCAT_TBSS:
4794           /* We don't split these for medium model.  Place them into
4795              default sections and hope for best.  */
4796           break;
4797         }
4798       if (sname)
4799         {
4800           /* We might get called with string constants, but get_named_section
4801              doesn't like them as they are not DECLs.  Also, we need to set
4802              flags in that case.  */
4803           if (!DECL_P (decl))
4804             return get_section (sname, flags, NULL);
4805           return get_named_section (decl, sname, reloc);
4806         }
4807     }
4808   return default_elf_select_section (decl, reloc, align);
4809 }
4810
4811 /* Build up a unique section name, expressed as a
4812    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4813    RELOC indicates whether the initial value of EXP requires
4814    link-time relocations.  */
4815
4816 static void ATTRIBUTE_UNUSED
4817 x86_64_elf_unique_section (tree decl, int reloc)
4818 {
4819   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4820       && ix86_in_large_data_p (decl))
4821     {
4822       const char *prefix = NULL;
4823       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4824       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4825
4826       switch (categorize_decl_for_section (decl, reloc))
4827         {
4828         case SECCAT_DATA:
4829         case SECCAT_DATA_REL:
4830         case SECCAT_DATA_REL_LOCAL:
4831         case SECCAT_DATA_REL_RO:
4832         case SECCAT_DATA_REL_RO_LOCAL:
4833           prefix = one_only ? ".ld" : ".ldata";
4834           break;
4835         case SECCAT_BSS:
4836           prefix = one_only ? ".lb" : ".lbss";
4837           break;
4838         case SECCAT_RODATA:
4839         case SECCAT_RODATA_MERGE_STR:
4840         case SECCAT_RODATA_MERGE_STR_INIT:
4841         case SECCAT_RODATA_MERGE_CONST:
4842           prefix = one_only ? ".lr" : ".lrodata";
4843           break;
4844         case SECCAT_SRODATA:
4845         case SECCAT_SDATA:
4846         case SECCAT_SBSS:
4847           gcc_unreachable ();
4848         case SECCAT_TEXT:
4849         case SECCAT_TDATA:
4850         case SECCAT_TBSS:
4851           /* We don't split these for medium model.  Place them into
4852              default sections and hope for best.  */
4853           break;
4854         }
4855       if (prefix)
4856         {
4857           const char *name, *linkonce;
4858           char *string;
4859
4860           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4861           name = targetm.strip_name_encoding (name);
4862
4863           /* If we're using one_only, then there needs to be a .gnu.linkonce
4864              prefix to the section name.  */
4865           linkonce = one_only ? ".gnu.linkonce" : "";
4866
4867           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4868
4869           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4870           return;
4871         }
4872     }
4873   default_unique_section (decl, reloc);
4874 }
4875
4876 #ifdef COMMON_ASM_OP
4877 /* This says how to output assembler code to declare an
4878    uninitialized external linkage data object.
4879
4880    For medium model x86-64 we need to use .largecomm opcode for
4881    large objects.  */
4882 void
4883 x86_elf_aligned_common (FILE *file,
4884                         const char *name, unsigned HOST_WIDE_INT size,
4885                         int align)
4886 {
4887   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4888       && size > (unsigned int)ix86_section_threshold)
4889     fputs (".largecomm\t", file);
4890   else
4891     fputs (COMMON_ASM_OP, file);
4892   assemble_name (file, name);
4893   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
4894            size, align / BITS_PER_UNIT);
4895 }
4896 #endif
4897
4898 /* Utility function for targets to use in implementing
4899    ASM_OUTPUT_ALIGNED_BSS.  */
4900
4901 void
4902 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
4903                         const char *name, unsigned HOST_WIDE_INT size,
4904                         int align)
4905 {
4906   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4907       && size > (unsigned int)ix86_section_threshold)
4908     switch_to_section (get_named_section (decl, ".lbss", 0));
4909   else
4910     switch_to_section (bss_section);
4911   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
4912 #ifdef ASM_DECLARE_OBJECT_NAME
4913   last_assemble_variable_decl = decl;
4914   ASM_DECLARE_OBJECT_NAME (file, name, decl);
4915 #else
4916   /* Standard thing is just output label for the object.  */
4917   ASM_OUTPUT_LABEL (file, name);
4918 #endif /* ASM_DECLARE_OBJECT_NAME */
4919   ASM_OUTPUT_SKIP (file, size ? size : 1);
4920 }
4921 \f
4922 static const struct default_options ix86_option_optimization_table[] =
4923   {
4924     /* Turn off -fschedule-insns by default.  It tends to make the
4925        problem with not enough registers even worse.  */
4926 #ifdef INSN_SCHEDULING
4927     { OPT_LEVELS_ALL, OPT_fschedule_insns, NULL, 0 },
4928 #endif
4929
4930 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
4931     SUBTARGET_OPTIMIZATION_OPTIONS,
4932 #endif
4933     { OPT_LEVELS_NONE, 0, NULL, 0 }
4934   };
4935
4936 /* Implement TARGET_OPTION_INIT_STRUCT.  */
4937
4938 static void
4939 ix86_option_init_struct (struct gcc_options *opts)
4940 {
4941   if (TARGET_MACHO)
4942     /* The Darwin libraries never set errno, so we might as well
4943        avoid calling them when that's the only reason we would.  */
4944     opts->x_flag_errno_math = 0;
4945
4946   opts->x_flag_pcc_struct_return = 2;
4947   opts->x_flag_asynchronous_unwind_tables = 2;
4948   opts->x_flag_vect_cost_model = 1;
4949 }
4950
4951 /* Decide whether we must probe the stack before any space allocation
4952    on this target.  It's essentially TARGET_STACK_PROBE except when
4953    -fstack-check causes the stack to be already probed differently.  */
4954
4955 bool
4956 ix86_target_stack_probe (void)
4957 {
4958   /* Do not probe the stack twice if static stack checking is enabled.  */
4959   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
4960     return false;
4961
4962   return TARGET_STACK_PROBE;
4963 }
4964 \f
4965 /* Decide whether we can make a sibling call to a function.  DECL is the
4966    declaration of the function being targeted by the call and EXP is the
4967    CALL_EXPR representing the call.  */
4968
4969 static bool
4970 ix86_function_ok_for_sibcall (tree decl, tree exp)
4971 {
4972   tree type, decl_or_type;
4973   rtx a, b;
4974
4975   /* If we are generating position-independent code, we cannot sibcall
4976      optimize any indirect call, or a direct call to a global function,
4977      as the PLT requires %ebx be live. (Darwin does not have a PLT.)  */
4978   if (!TARGET_MACHO
4979       && !TARGET_64BIT 
4980       && flag_pic 
4981       && (!decl || !targetm.binds_local_p (decl)))
4982     return false;
4983
4984   /* If we need to align the outgoing stack, then sibcalling would
4985      unalign the stack, which may break the called function.  */
4986   if (ix86_minimum_incoming_stack_boundary (true)
4987       < PREFERRED_STACK_BOUNDARY)
4988     return false;
4989
4990   if (decl)
4991     {
4992       decl_or_type = decl;
4993       type = TREE_TYPE (decl);
4994     }
4995   else
4996     {
4997       /* We're looking at the CALL_EXPR, we need the type of the function.  */
4998       type = CALL_EXPR_FN (exp);                /* pointer expression */
4999       type = TREE_TYPE (type);                  /* pointer type */
5000       type = TREE_TYPE (type);                  /* function type */
5001       decl_or_type = type;
5002     }
5003
5004   /* Check that the return value locations are the same.  Like
5005      if we are returning floats on the 80387 register stack, we cannot
5006      make a sibcall from a function that doesn't return a float to a
5007      function that does or, conversely, from a function that does return
5008      a float to a function that doesn't; the necessary stack adjustment
5009      would not be executed.  This is also the place we notice
5010      differences in the return value ABI.  Note that it is ok for one
5011      of the functions to have void return type as long as the return
5012      value of the other is passed in a register.  */
5013   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
5014   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
5015                            cfun->decl, false);
5016   if (STACK_REG_P (a) || STACK_REG_P (b))
5017     {
5018       if (!rtx_equal_p (a, b))
5019         return false;
5020     }
5021   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
5022     {
5023       /* Disable sibcall if we need to generate vzeroupper after
5024          callee returns.  */
5025       if (TARGET_VZEROUPPER
5026           && cfun->machine->callee_return_avx256_p
5027           && !cfun->machine->caller_return_avx256_p)
5028         return false;
5029     }
5030   else if (!rtx_equal_p (a, b))
5031     return false;
5032
5033   if (TARGET_64BIT)
5034     {
5035       /* The SYSV ABI has more call-clobbered registers;
5036          disallow sibcalls from MS to SYSV.  */
5037       if (cfun->machine->call_abi == MS_ABI
5038           && ix86_function_type_abi (type) == SYSV_ABI)
5039         return false;
5040     }
5041   else
5042     {
5043       /* If this call is indirect, we'll need to be able to use a
5044          call-clobbered register for the address of the target function.
5045          Make sure that all such registers are not used for passing
5046          parameters.  Note that DLLIMPORT functions are indirect.  */
5047       if (!decl
5048           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
5049         {
5050           if (ix86_function_regparm (type, NULL) >= 3)
5051             {
5052               /* ??? Need to count the actual number of registers to be used,
5053                  not the possible number of registers.  Fix later.  */
5054               return false;
5055             }
5056         }
5057     }
5058
5059   /* Otherwise okay.  That also includes certain types of indirect calls.  */
5060   return true;
5061 }
5062
5063 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
5064    and "sseregparm" calling convention attributes;
5065    arguments as in struct attribute_spec.handler.  */
5066
5067 static tree
5068 ix86_handle_cconv_attribute (tree *node, tree name,
5069                                    tree args,
5070                                    int flags ATTRIBUTE_UNUSED,
5071                                    bool *no_add_attrs)
5072 {
5073   if (TREE_CODE (*node) != FUNCTION_TYPE
5074       && TREE_CODE (*node) != METHOD_TYPE
5075       && TREE_CODE (*node) != FIELD_DECL
5076       && TREE_CODE (*node) != TYPE_DECL)
5077     {
5078       warning (OPT_Wattributes, "%qE attribute only applies to functions",
5079                name);
5080       *no_add_attrs = true;
5081       return NULL_TREE;
5082     }
5083
5084   /* Can combine regparm with all attributes but fastcall.  */
5085   if (is_attribute_p ("regparm", name))
5086     {
5087       tree cst;
5088
5089       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5090         {
5091           error ("fastcall and regparm attributes are not compatible");
5092         }
5093
5094       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5095         {
5096           error ("regparam and thiscall attributes are not compatible");
5097         }
5098
5099       cst = TREE_VALUE (args);
5100       if (TREE_CODE (cst) != INTEGER_CST)
5101         {
5102           warning (OPT_Wattributes,
5103                    "%qE attribute requires an integer constant argument",
5104                    name);
5105           *no_add_attrs = true;
5106         }
5107       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
5108         {
5109           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
5110                    name, REGPARM_MAX);
5111           *no_add_attrs = true;
5112         }
5113
5114       return NULL_TREE;
5115     }
5116
5117   if (TARGET_64BIT)
5118     {
5119       /* Do not warn when emulating the MS ABI.  */
5120       if ((TREE_CODE (*node) != FUNCTION_TYPE
5121            && TREE_CODE (*node) != METHOD_TYPE)
5122           || ix86_function_type_abi (*node) != MS_ABI)
5123         warning (OPT_Wattributes, "%qE attribute ignored",
5124                  name);
5125       *no_add_attrs = true;
5126       return NULL_TREE;
5127     }
5128
5129   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
5130   if (is_attribute_p ("fastcall", name))
5131     {
5132       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5133         {
5134           error ("fastcall and cdecl attributes are not compatible");
5135         }
5136       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5137         {
5138           error ("fastcall and stdcall attributes are not compatible");
5139         }
5140       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
5141         {
5142           error ("fastcall and regparm attributes are not compatible");
5143         }
5144       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5145         {
5146           error ("fastcall and thiscall attributes are not compatible");
5147         }
5148     }
5149
5150   /* Can combine stdcall with fastcall (redundant), regparm and
5151      sseregparm.  */
5152   else if (is_attribute_p ("stdcall", name))
5153     {
5154       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5155         {
5156           error ("stdcall and cdecl attributes are not compatible");
5157         }
5158       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5159         {
5160           error ("stdcall and fastcall attributes are not compatible");
5161         }
5162       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5163         {
5164           error ("stdcall and thiscall attributes are not compatible");
5165         }
5166     }
5167
5168   /* Can combine cdecl with regparm and sseregparm.  */
5169   else if (is_attribute_p ("cdecl", name))
5170     {
5171       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5172         {
5173           error ("stdcall and cdecl attributes are not compatible");
5174         }
5175       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5176         {
5177           error ("fastcall and cdecl attributes are not compatible");
5178         }
5179       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
5180         {
5181           error ("cdecl and thiscall attributes are not compatible");
5182         }
5183     }
5184   else if (is_attribute_p ("thiscall", name))
5185     {
5186       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
5187         warning (OPT_Wattributes, "%qE attribute is used for none class-method",
5188                  name);
5189       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
5190         {
5191           error ("stdcall and thiscall attributes are not compatible");
5192         }
5193       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
5194         {
5195           error ("fastcall and thiscall attributes are not compatible");
5196         }
5197       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
5198         {
5199           error ("cdecl and thiscall attributes are not compatible");
5200         }
5201     }
5202
5203   /* Can combine sseregparm with all attributes.  */
5204
5205   return NULL_TREE;
5206 }
5207
5208 /* Return 0 if the attributes for two types are incompatible, 1 if they
5209    are compatible, and 2 if they are nearly compatible (which causes a
5210    warning to be generated).  */
5211
5212 static int
5213 ix86_comp_type_attributes (const_tree type1, const_tree type2)
5214 {
5215   /* Check for mismatch of non-default calling convention.  */
5216   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
5217
5218   if (TREE_CODE (type1) != FUNCTION_TYPE
5219       && TREE_CODE (type1) != METHOD_TYPE)
5220     return 1;
5221
5222   /* Check for mismatched fastcall/regparm types.  */
5223   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
5224        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
5225       || (ix86_function_regparm (type1, NULL)
5226           != ix86_function_regparm (type2, NULL)))
5227     return 0;
5228
5229   /* Check for mismatched sseregparm types.  */
5230   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
5231       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
5232     return 0;
5233
5234   /* Check for mismatched thiscall types.  */
5235   if (!lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type1))
5236       != !lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type2)))
5237     return 0;
5238
5239   /* Check for mismatched return types (cdecl vs stdcall).  */
5240   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
5241       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
5242     return 0;
5243
5244   return 1;
5245 }
5246 \f
5247 /* Return the regparm value for a function with the indicated TYPE and DECL.
5248    DECL may be NULL when calling function indirectly
5249    or considering a libcall.  */
5250
5251 static int
5252 ix86_function_regparm (const_tree type, const_tree decl)
5253 {
5254   tree attr;
5255   int regparm;
5256
5257   if (TARGET_64BIT)
5258     return (ix86_function_type_abi (type) == SYSV_ABI
5259             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
5260
5261   regparm = ix86_regparm;
5262   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
5263   if (attr)
5264     {
5265       regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
5266       return regparm;
5267     }
5268
5269   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
5270     return 2;
5271
5272   if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
5273     return 1;
5274
5275   /* Use register calling convention for local functions when possible.  */
5276   if (decl
5277       && TREE_CODE (decl) == FUNCTION_DECL
5278       && optimize
5279       && !(profile_flag && !flag_fentry))
5280     {
5281       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5282       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
5283       if (i && i->local)
5284         {
5285           int local_regparm, globals = 0, regno;
5286
5287           /* Make sure no regparm register is taken by a
5288              fixed register variable.  */
5289           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
5290             if (fixed_regs[local_regparm])
5291               break;
5292
5293           /* We don't want to use regparm(3) for nested functions as
5294              these use a static chain pointer in the third argument.  */
5295           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
5296             local_regparm = 2;
5297
5298           /* In 32-bit mode save a register for the split stack.  */
5299           if (!TARGET_64BIT && local_regparm == 3 && flag_split_stack)
5300             local_regparm = 2;
5301
5302           /* Each fixed register usage increases register pressure,
5303              so less registers should be used for argument passing.
5304              This functionality can be overriden by an explicit
5305              regparm value.  */
5306           for (regno = 0; regno <= DI_REG; regno++)
5307             if (fixed_regs[regno])
5308               globals++;
5309
5310           local_regparm
5311             = globals < local_regparm ? local_regparm - globals : 0;
5312
5313           if (local_regparm > regparm)
5314             regparm = local_regparm;
5315         }
5316     }
5317
5318   return regparm;
5319 }
5320
5321 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
5322    DFmode (2) arguments in SSE registers for a function with the
5323    indicated TYPE and DECL.  DECL may be NULL when calling function
5324    indirectly or considering a libcall.  Otherwise return 0.  */
5325
5326 static int
5327 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
5328 {
5329   gcc_assert (!TARGET_64BIT);
5330
5331   /* Use SSE registers to pass SFmode and DFmode arguments if requested
5332      by the sseregparm attribute.  */
5333   if (TARGET_SSEREGPARM
5334       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
5335     {
5336       if (!TARGET_SSE)
5337         {
5338           if (warn)
5339             {
5340               if (decl)
5341                 error ("Calling %qD with attribute sseregparm without "
5342                        "SSE/SSE2 enabled", decl);
5343               else
5344                 error ("Calling %qT with attribute sseregparm without "
5345                        "SSE/SSE2 enabled", type);
5346             }
5347           return 0;
5348         }
5349
5350       return 2;
5351     }
5352
5353   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
5354      (and DFmode for SSE2) arguments in SSE registers.  */
5355   if (decl && TARGET_SSE_MATH && optimize
5356       && !(profile_flag && !flag_fentry))
5357     {
5358       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
5359       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
5360       if (i && i->local)
5361         return TARGET_SSE2 ? 2 : 1;
5362     }
5363
5364   return 0;
5365 }
5366
5367 /* Return true if EAX is live at the start of the function.  Used by
5368    ix86_expand_prologue to determine if we need special help before
5369    calling allocate_stack_worker.  */
5370
5371 static bool
5372 ix86_eax_live_at_start_p (void)
5373 {
5374   /* Cheat.  Don't bother working forward from ix86_function_regparm
5375      to the function type to whether an actual argument is located in
5376      eax.  Instead just look at cfg info, which is still close enough
5377      to correct at this point.  This gives false positives for broken
5378      functions that might use uninitialized data that happens to be
5379      allocated in eax, but who cares?  */
5380   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
5381 }
5382
5383 /* Value is the number of bytes of arguments automatically
5384    popped when returning from a subroutine call.
5385    FUNDECL is the declaration node of the function (as a tree),
5386    FUNTYPE is the data type of the function (as a tree),
5387    or for a library call it is an identifier node for the subroutine name.
5388    SIZE is the number of bytes of arguments passed on the stack.
5389
5390    On the 80386, the RTD insn may be used to pop them if the number
5391      of args is fixed, but if the number is variable then the caller
5392      must pop them all.  RTD can't be used for library calls now
5393      because the library is compiled with the Unix compiler.
5394    Use of RTD is a selectable option, since it is incompatible with
5395    standard Unix calling sequences.  If the option is not selected,
5396    the caller must always pop the args.
5397
5398    The attribute stdcall is equivalent to RTD on a per module basis.  */
5399
5400 static int
5401 ix86_return_pops_args (tree fundecl, tree funtype, int size)
5402 {
5403   int rtd;
5404
5405   /* None of the 64-bit ABIs pop arguments.  */
5406   if (TARGET_64BIT)
5407     return 0;
5408
5409   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
5410
5411   /* Cdecl functions override -mrtd, and never pop the stack.  */
5412   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
5413     {
5414       /* Stdcall and fastcall functions will pop the stack if not
5415          variable args.  */
5416       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
5417           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype))
5418           || lookup_attribute ("thiscall", TYPE_ATTRIBUTES (funtype)))
5419         rtd = 1;
5420
5421       if (rtd && ! stdarg_p (funtype))
5422         return size;
5423     }
5424
5425   /* Lose any fake structure return argument if it is passed on the stack.  */
5426   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
5427       && !KEEP_AGGREGATE_RETURN_POINTER)
5428     {
5429       int nregs = ix86_function_regparm (funtype, fundecl);
5430       if (nregs == 0)
5431         return GET_MODE_SIZE (Pmode);
5432     }
5433
5434   return 0;
5435 }
5436 \f
5437 /* Argument support functions.  */
5438
5439 /* Return true when register may be used to pass function parameters.  */
5440 bool
5441 ix86_function_arg_regno_p (int regno)
5442 {
5443   int i;
5444   const int *parm_regs;
5445
5446   if (!TARGET_64BIT)
5447     {
5448       if (TARGET_MACHO)
5449         return (regno < REGPARM_MAX
5450                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
5451       else
5452         return (regno < REGPARM_MAX
5453                 || (TARGET_MMX && MMX_REGNO_P (regno)
5454                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
5455                 || (TARGET_SSE && SSE_REGNO_P (regno)
5456                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
5457     }
5458
5459   if (TARGET_MACHO)
5460     {
5461       if (SSE_REGNO_P (regno) && TARGET_SSE)
5462         return true;
5463     }
5464   else
5465     {
5466       if (TARGET_SSE && SSE_REGNO_P (regno)
5467           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
5468         return true;
5469     }
5470
5471   /* TODO: The function should depend on current function ABI but
5472      builtins.c would need updating then. Therefore we use the
5473      default ABI.  */
5474
5475   /* RAX is used as hidden argument to va_arg functions.  */
5476   if (ix86_abi == SYSV_ABI && regno == AX_REG)
5477     return true;
5478
5479   if (ix86_abi == MS_ABI)
5480     parm_regs = x86_64_ms_abi_int_parameter_registers;
5481   else
5482     parm_regs = x86_64_int_parameter_registers;
5483   for (i = 0; i < (ix86_abi == MS_ABI
5484                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
5485     if (regno == parm_regs[i])
5486       return true;
5487   return false;
5488 }
5489
5490 /* Return if we do not know how to pass TYPE solely in registers.  */
5491
5492 static bool
5493 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
5494 {
5495   if (must_pass_in_stack_var_size_or_pad (mode, type))
5496     return true;
5497
5498   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
5499      The layout_type routine is crafty and tries to trick us into passing
5500      currently unsupported vector types on the stack by using TImode.  */
5501   return (!TARGET_64BIT && mode == TImode
5502           && type && TREE_CODE (type) != VECTOR_TYPE);
5503 }
5504
5505 /* It returns the size, in bytes, of the area reserved for arguments passed
5506    in registers for the function represented by fndecl dependent to the used
5507    abi format.  */
5508 int
5509 ix86_reg_parm_stack_space (const_tree fndecl)
5510 {
5511   enum calling_abi call_abi = SYSV_ABI;
5512   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
5513     call_abi = ix86_function_abi (fndecl);
5514   else
5515     call_abi = ix86_function_type_abi (fndecl);
5516   if (call_abi == MS_ABI)
5517     return 32;
5518   return 0;
5519 }
5520
5521 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
5522    call abi used.  */
5523 enum calling_abi
5524 ix86_function_type_abi (const_tree fntype)
5525 {
5526   if (TARGET_64BIT && fntype != NULL)
5527     {
5528       enum calling_abi abi = ix86_abi;
5529       if (abi == SYSV_ABI)
5530         {
5531           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
5532             abi = MS_ABI;
5533         }
5534       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
5535         abi = SYSV_ABI;
5536       return abi;
5537     }
5538   return ix86_abi;
5539 }
5540
5541 static bool
5542 ix86_function_ms_hook_prologue (const_tree fn)
5543 {
5544   if (fn && lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fn)))
5545     {
5546       if (decl_function_context (fn) != NULL_TREE)
5547         error_at (DECL_SOURCE_LOCATION (fn),
5548                   "ms_hook_prologue is not compatible with nested function");
5549       else
5550         return true;
5551     }
5552   return false;
5553 }
5554
5555 static enum calling_abi
5556 ix86_function_abi (const_tree fndecl)
5557 {
5558   if (! fndecl)
5559     return ix86_abi;
5560   return ix86_function_type_abi (TREE_TYPE (fndecl));
5561 }
5562
5563 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
5564    call abi used.  */
5565 enum calling_abi
5566 ix86_cfun_abi (void)
5567 {
5568   if (! cfun || ! TARGET_64BIT)
5569     return ix86_abi;
5570   return cfun->machine->call_abi;
5571 }
5572
5573 /* Write the extra assembler code needed to declare a function properly.  */
5574
5575 void
5576 ix86_asm_output_function_label (FILE *asm_out_file, const char *fname,
5577                                 tree decl)
5578 {
5579   bool is_ms_hook = ix86_function_ms_hook_prologue (decl);
5580
5581   if (is_ms_hook)
5582     {
5583       int i, filler_count = (TARGET_64BIT ? 32 : 16);
5584       unsigned int filler_cc = 0xcccccccc;
5585
5586       for (i = 0; i < filler_count; i += 4)
5587         fprintf (asm_out_file, ASM_LONG " %#x\n", filler_cc);
5588     }
5589
5590 #ifdef SUBTARGET_ASM_UNWIND_INIT
5591   SUBTARGET_ASM_UNWIND_INIT (asm_out_file);
5592 #endif
5593
5594   ASM_OUTPUT_LABEL (asm_out_file, fname);
5595
5596   /* Output magic byte marker, if hot-patch attribute is set.  */
5597   if (is_ms_hook)
5598     {
5599       if (TARGET_64BIT)
5600         {
5601           /* leaq [%rsp + 0], %rsp  */
5602           asm_fprintf (asm_out_file, ASM_BYTE
5603                        "0x48, 0x8d, 0xa4, 0x24, 0x00, 0x00, 0x00, 0x00\n");
5604         }
5605       else
5606         {
5607           /* movl.s %edi, %edi
5608              push   %ebp
5609              movl.s %esp, %ebp */
5610           asm_fprintf (asm_out_file, ASM_BYTE
5611                        "0x8b, 0xff, 0x55, 0x8b, 0xec\n");
5612         }
5613     }
5614 }
5615
5616 /* regclass.c  */
5617 extern void init_regs (void);
5618
5619 /* Implementation of call abi switching target hook. Specific to FNDECL
5620    the specific call register sets are set. See also CONDITIONAL_REGISTER_USAGE
5621    for more details.  */
5622 void
5623 ix86_call_abi_override (const_tree fndecl)
5624 {
5625   if (fndecl == NULL_TREE)
5626     cfun->machine->call_abi = ix86_abi;
5627   else
5628     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
5629 }
5630
5631 /* MS and SYSV ABI have different set of call used registers.  Avoid expensive
5632    re-initialization of init_regs each time we switch function context since
5633    this is needed only during RTL expansion.  */
5634 static void
5635 ix86_maybe_switch_abi (void)
5636 {
5637   if (TARGET_64BIT &&
5638       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
5639     reinit_regs ();
5640 }
5641
5642 /* Initialize a variable CUM of type CUMULATIVE_ARGS
5643    for a call to a function whose data type is FNTYPE.
5644    For a library call, FNTYPE is 0.  */
5645
5646 void
5647 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
5648                       tree fntype,      /* tree ptr for function decl */
5649                       rtx libname,      /* SYMBOL_REF of library name or 0 */
5650                       tree fndecl,
5651                       int caller)
5652 {
5653   struct cgraph_local_info *i;
5654   tree fnret_type;
5655
5656   memset (cum, 0, sizeof (*cum));
5657
5658   /* Initialize for the current callee.  */
5659   if (caller)
5660     {
5661       cfun->machine->callee_pass_avx256_p = false;
5662       cfun->machine->callee_return_avx256_p = false;
5663     }
5664
5665   if (fndecl)
5666     {
5667       i = cgraph_local_info (fndecl);
5668       cum->call_abi = ix86_function_abi (fndecl);
5669       fnret_type = TREE_TYPE (TREE_TYPE (fndecl));
5670     }
5671   else
5672     {
5673       i = NULL;
5674       cum->call_abi = ix86_function_type_abi (fntype);
5675       if (fntype)
5676         fnret_type = TREE_TYPE (fntype);
5677       else
5678         fnret_type = NULL;
5679     }
5680
5681   if (TARGET_VZEROUPPER && fnret_type)
5682     {
5683       rtx fnret_value = ix86_function_value (fnret_type, fntype,
5684                                              false);
5685       if (function_pass_avx256_p (fnret_value))
5686         {
5687           /* The return value of this function uses 256bit AVX modes.  */
5688           cfun->machine->use_avx256_p = true;
5689           if (caller)
5690             cfun->machine->callee_return_avx256_p = true;
5691           else
5692             cfun->machine->caller_return_avx256_p = true;
5693         }
5694     }
5695
5696   cum->caller = caller;
5697
5698   /* Set up the number of registers to use for passing arguments.  */
5699
5700   if (cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
5701     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
5702            "or subtarget optimization implying it");
5703   cum->nregs = ix86_regparm;
5704   if (TARGET_64BIT)
5705     {
5706       cum->nregs = (cum->call_abi == SYSV_ABI
5707                    ? X86_64_REGPARM_MAX
5708                    : X86_64_MS_REGPARM_MAX);
5709     }
5710   if (TARGET_SSE)
5711     {
5712       cum->sse_nregs = SSE_REGPARM_MAX;
5713       if (TARGET_64BIT)
5714         {
5715           cum->sse_nregs = (cum->call_abi == SYSV_ABI
5716                            ? X86_64_SSE_REGPARM_MAX
5717                            : X86_64_MS_SSE_REGPARM_MAX);
5718         }
5719     }
5720   if (TARGET_MMX)
5721     cum->mmx_nregs = MMX_REGPARM_MAX;
5722   cum->warn_avx = true;
5723   cum->warn_sse = true;
5724   cum->warn_mmx = true;
5725
5726   /* Because type might mismatch in between caller and callee, we need to
5727      use actual type of function for local calls.
5728      FIXME: cgraph_analyze can be told to actually record if function uses
5729      va_start so for local functions maybe_vaarg can be made aggressive
5730      helping K&R code.
5731      FIXME: once typesytem is fixed, we won't need this code anymore.  */
5732   if (i && i->local)
5733     fntype = TREE_TYPE (fndecl);
5734   cum->maybe_vaarg = (fntype
5735                       ? (!prototype_p (fntype) || stdarg_p (fntype))
5736                       : !libname);
5737
5738   if (!TARGET_64BIT)
5739     {
5740       /* If there are variable arguments, then we won't pass anything
5741          in registers in 32-bit mode. */
5742       if (stdarg_p (fntype))
5743         {
5744           cum->nregs = 0;
5745           cum->sse_nregs = 0;
5746           cum->mmx_nregs = 0;
5747           cum->warn_avx = 0;
5748           cum->warn_sse = 0;
5749           cum->warn_mmx = 0;
5750           return;
5751         }
5752
5753       /* Use ecx and edx registers if function has fastcall attribute,
5754          else look for regparm information.  */
5755       if (fntype)
5756         {
5757           if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
5758             {
5759               cum->nregs = 1;
5760               cum->fastcall = 1; /* Same first register as in fastcall.  */
5761             }
5762           else if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
5763             {
5764               cum->nregs = 2;
5765               cum->fastcall = 1;
5766             }
5767           else
5768             cum->nregs = ix86_function_regparm (fntype, fndecl);
5769         }
5770
5771       /* Set up the number of SSE registers used for passing SFmode
5772          and DFmode arguments.  Warn for mismatching ABI.  */
5773       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
5774     }
5775 }
5776
5777 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
5778    But in the case of vector types, it is some vector mode.
5779
5780    When we have only some of our vector isa extensions enabled, then there
5781    are some modes for which vector_mode_supported_p is false.  For these
5782    modes, the generic vector support in gcc will choose some non-vector mode
5783    in order to implement the type.  By computing the natural mode, we'll
5784    select the proper ABI location for the operand and not depend on whatever
5785    the middle-end decides to do with these vector types.
5786
5787    The midde-end can't deal with the vector types > 16 bytes.  In this
5788    case, we return the original mode and warn ABI change if CUM isn't
5789    NULL.  */
5790
5791 static enum machine_mode
5792 type_natural_mode (const_tree type, const CUMULATIVE_ARGS *cum)
5793 {
5794   enum machine_mode mode = TYPE_MODE (type);
5795
5796   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5797     {
5798       HOST_WIDE_INT size = int_size_in_bytes (type);
5799       if ((size == 8 || size == 16 || size == 32)
5800           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5801           && TYPE_VECTOR_SUBPARTS (type) > 1)
5802         {
5803           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5804
5805           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5806             mode = MIN_MODE_VECTOR_FLOAT;
5807           else
5808             mode = MIN_MODE_VECTOR_INT;
5809
5810           /* Get the mode which has this inner mode and number of units.  */
5811           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5812             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5813                 && GET_MODE_INNER (mode) == innermode)
5814               {
5815                 if (size == 32 && !TARGET_AVX)
5816                   {
5817                     static bool warnedavx;
5818
5819                     if (cum
5820                         && !warnedavx
5821                         && cum->warn_avx)
5822                       {
5823                         warnedavx = true;
5824                         warning (0, "AVX vector argument without AVX "
5825                                  "enabled changes the ABI");
5826                       }
5827                     return TYPE_MODE (type);
5828                   }
5829                 else
5830                   return mode;
5831               }
5832
5833           gcc_unreachable ();
5834         }
5835     }
5836
5837   return mode;
5838 }
5839
5840 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5841    this may not agree with the mode that the type system has chosen for the
5842    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5843    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5844
5845 static rtx
5846 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5847                      unsigned int regno)
5848 {
5849   rtx tmp;
5850
5851   if (orig_mode != BLKmode)
5852     tmp = gen_rtx_REG (orig_mode, regno);
5853   else
5854     {
5855       tmp = gen_rtx_REG (mode, regno);
5856       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5857       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5858     }
5859
5860   return tmp;
5861 }
5862
5863 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5864    of this code is to classify each 8bytes of incoming argument by the register
5865    class and assign registers accordingly.  */
5866
5867 /* Return the union class of CLASS1 and CLASS2.
5868    See the x86-64 PS ABI for details.  */
5869
5870 static enum x86_64_reg_class
5871 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5872 {
5873   /* Rule #1: If both classes are equal, this is the resulting class.  */
5874   if (class1 == class2)
5875     return class1;
5876
5877   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5878      the other class.  */
5879   if (class1 == X86_64_NO_CLASS)
5880     return class2;
5881   if (class2 == X86_64_NO_CLASS)
5882     return class1;
5883
5884   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
5885   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
5886     return X86_64_MEMORY_CLASS;
5887
5888   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
5889   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
5890       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
5891     return X86_64_INTEGERSI_CLASS;
5892   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
5893       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
5894     return X86_64_INTEGER_CLASS;
5895
5896   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
5897      MEMORY is used.  */
5898   if (class1 == X86_64_X87_CLASS
5899       || class1 == X86_64_X87UP_CLASS
5900       || class1 == X86_64_COMPLEX_X87_CLASS
5901       || class2 == X86_64_X87_CLASS
5902       || class2 == X86_64_X87UP_CLASS
5903       || class2 == X86_64_COMPLEX_X87_CLASS)
5904     return X86_64_MEMORY_CLASS;
5905
5906   /* Rule #6: Otherwise class SSE is used.  */
5907   return X86_64_SSE_CLASS;
5908 }
5909
5910 /* Classify the argument of type TYPE and mode MODE.
5911    CLASSES will be filled by the register class used to pass each word
5912    of the operand.  The number of words is returned.  In case the parameter
5913    should be passed in memory, 0 is returned. As a special case for zero
5914    sized containers, classes[0] will be NO_CLASS and 1 is returned.
5915
5916    BIT_OFFSET is used internally for handling records and specifies offset
5917    of the offset in bits modulo 256 to avoid overflow cases.
5918
5919    See the x86-64 PS ABI for details.
5920 */
5921
5922 static int
5923 classify_argument (enum machine_mode mode, const_tree type,
5924                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
5925 {
5926   HOST_WIDE_INT bytes =
5927     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5928   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5929
5930   /* Variable sized entities are always passed/returned in memory.  */
5931   if (bytes < 0)
5932     return 0;
5933
5934   if (mode != VOIDmode
5935       && targetm.calls.must_pass_in_stack (mode, type))
5936     return 0;
5937
5938   if (type && AGGREGATE_TYPE_P (type))
5939     {
5940       int i;
5941       tree field;
5942       enum x86_64_reg_class subclasses[MAX_CLASSES];
5943
5944       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
5945       if (bytes > 32)
5946         return 0;
5947
5948       for (i = 0; i < words; i++)
5949         classes[i] = X86_64_NO_CLASS;
5950
5951       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
5952          signalize memory class, so handle it as special case.  */
5953       if (!words)
5954         {
5955           classes[0] = X86_64_NO_CLASS;
5956           return 1;
5957         }
5958
5959       /* Classify each field of record and merge classes.  */
5960       switch (TREE_CODE (type))
5961         {
5962         case RECORD_TYPE:
5963           /* And now merge the fields of structure.  */
5964           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5965             {
5966               if (TREE_CODE (field) == FIELD_DECL)
5967                 {
5968                   int num;
5969
5970                   if (TREE_TYPE (field) == error_mark_node)
5971                     continue;
5972
5973                   /* Bitfields are always classified as integer.  Handle them
5974                      early, since later code would consider them to be
5975                      misaligned integers.  */
5976                   if (DECL_BIT_FIELD (field))
5977                     {
5978                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5979                            i < ((int_bit_position (field) + (bit_offset % 64))
5980                                 + tree_low_cst (DECL_SIZE (field), 0)
5981                                 + 63) / 8 / 8; i++)
5982                         classes[i] =
5983                           merge_classes (X86_64_INTEGER_CLASS,
5984                                          classes[i]);
5985                     }
5986                   else
5987                     {
5988                       int pos;
5989
5990                       type = TREE_TYPE (field);
5991
5992                       /* Flexible array member is ignored.  */
5993                       if (TYPE_MODE (type) == BLKmode
5994                           && TREE_CODE (type) == ARRAY_TYPE
5995                           && TYPE_SIZE (type) == NULL_TREE
5996                           && TYPE_DOMAIN (type) != NULL_TREE
5997                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5998                               == NULL_TREE))
5999                         {
6000                           static bool warned;
6001
6002                           if (!warned && warn_psabi)
6003                             {
6004                               warned = true;
6005                               inform (input_location,
6006                                       "The ABI of passing struct with"
6007                                       " a flexible array member has"
6008                                       " changed in GCC 4.4");
6009                             }
6010                           continue;
6011                         }
6012                       num = classify_argument (TYPE_MODE (type), type,
6013                                                subclasses,
6014                                                (int_bit_position (field)
6015                                                 + bit_offset) % 256);
6016                       if (!num)
6017                         return 0;
6018                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
6019                       for (i = 0; i < num && (i + pos) < words; i++)
6020                         classes[i + pos] =
6021                           merge_classes (subclasses[i], classes[i + pos]);
6022                     }
6023                 }
6024             }
6025           break;
6026
6027         case ARRAY_TYPE:
6028           /* Arrays are handled as small records.  */
6029           {
6030             int num;
6031             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
6032                                      TREE_TYPE (type), subclasses, bit_offset);
6033             if (!num)
6034               return 0;
6035
6036             /* The partial classes are now full classes.  */
6037             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
6038               subclasses[0] = X86_64_SSE_CLASS;
6039             if (subclasses[0] == X86_64_INTEGERSI_CLASS
6040                 && !((bit_offset % 64) == 0 && bytes == 4))
6041               subclasses[0] = X86_64_INTEGER_CLASS;
6042
6043             for (i = 0; i < words; i++)
6044               classes[i] = subclasses[i % num];
6045
6046             break;
6047           }
6048         case UNION_TYPE:
6049         case QUAL_UNION_TYPE:
6050           /* Unions are similar to RECORD_TYPE but offset is always 0.
6051              */
6052           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6053             {
6054               if (TREE_CODE (field) == FIELD_DECL)
6055                 {
6056                   int num;
6057
6058                   if (TREE_TYPE (field) == error_mark_node)
6059                     continue;
6060
6061                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
6062                                            TREE_TYPE (field), subclasses,
6063                                            bit_offset);
6064                   if (!num)
6065                     return 0;
6066                   for (i = 0; i < num; i++)
6067                     classes[i] = merge_classes (subclasses[i], classes[i]);
6068                 }
6069             }
6070           break;
6071
6072         default:
6073           gcc_unreachable ();
6074         }
6075
6076       if (words > 2)
6077         {
6078           /* When size > 16 bytes, if the first one isn't
6079              X86_64_SSE_CLASS or any other ones aren't
6080              X86_64_SSEUP_CLASS, everything should be passed in
6081              memory.  */
6082           if (classes[0] != X86_64_SSE_CLASS)
6083               return 0;
6084
6085           for (i = 1; i < words; i++)
6086             if (classes[i] != X86_64_SSEUP_CLASS)
6087               return 0;
6088         }
6089
6090       /* Final merger cleanup.  */
6091       for (i = 0; i < words; i++)
6092         {
6093           /* If one class is MEMORY, everything should be passed in
6094              memory.  */
6095           if (classes[i] == X86_64_MEMORY_CLASS)
6096             return 0;
6097
6098           /* The X86_64_SSEUP_CLASS should be always preceded by
6099              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
6100           if (classes[i] == X86_64_SSEUP_CLASS
6101               && classes[i - 1] != X86_64_SSE_CLASS
6102               && classes[i - 1] != X86_64_SSEUP_CLASS)
6103             {
6104               /* The first one should never be X86_64_SSEUP_CLASS.  */
6105               gcc_assert (i != 0);
6106               classes[i] = X86_64_SSE_CLASS;
6107             }
6108
6109           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
6110                everything should be passed in memory.  */
6111           if (classes[i] == X86_64_X87UP_CLASS
6112               && (classes[i - 1] != X86_64_X87_CLASS))
6113             {
6114               static bool warned;
6115
6116               /* The first one should never be X86_64_X87UP_CLASS.  */
6117               gcc_assert (i != 0);
6118               if (!warned && warn_psabi)
6119                 {
6120                   warned = true;
6121                   inform (input_location,
6122                           "The ABI of passing union with long double"
6123                           " has changed in GCC 4.4");
6124                 }
6125               return 0;
6126             }
6127         }
6128       return words;
6129     }
6130
6131   /* Compute alignment needed.  We align all types to natural boundaries with
6132      exception of XFmode that is aligned to 64bits.  */
6133   if (mode != VOIDmode && mode != BLKmode)
6134     {
6135       int mode_alignment = GET_MODE_BITSIZE (mode);
6136
6137       if (mode == XFmode)
6138         mode_alignment = 128;
6139       else if (mode == XCmode)
6140         mode_alignment = 256;
6141       if (COMPLEX_MODE_P (mode))
6142         mode_alignment /= 2;
6143       /* Misaligned fields are always returned in memory.  */
6144       if (bit_offset % mode_alignment)
6145         return 0;
6146     }
6147
6148   /* for V1xx modes, just use the base mode */
6149   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
6150       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
6151     mode = GET_MODE_INNER (mode);
6152
6153   /* Classification of atomic types.  */
6154   switch (mode)
6155     {
6156     case SDmode:
6157     case DDmode:
6158       classes[0] = X86_64_SSE_CLASS;
6159       return 1;
6160     case TDmode:
6161       classes[0] = X86_64_SSE_CLASS;
6162       classes[1] = X86_64_SSEUP_CLASS;
6163       return 2;
6164     case DImode:
6165     case SImode:
6166     case HImode:
6167     case QImode:
6168     case CSImode:
6169     case CHImode:
6170     case CQImode:
6171       {
6172         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
6173
6174         if (size <= 32)
6175           {
6176             classes[0] = X86_64_INTEGERSI_CLASS;
6177             return 1;
6178           }
6179         else if (size <= 64)
6180           {
6181             classes[0] = X86_64_INTEGER_CLASS;
6182             return 1;
6183           }
6184         else if (size <= 64+32)
6185           {
6186             classes[0] = X86_64_INTEGER_CLASS;
6187             classes[1] = X86_64_INTEGERSI_CLASS;
6188             return 2;
6189           }
6190         else if (size <= 64+64)
6191           {
6192             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6193             return 2;
6194           }
6195         else
6196           gcc_unreachable ();
6197       }
6198     case CDImode:
6199     case TImode:
6200       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
6201       return 2;
6202     case COImode:
6203     case OImode:
6204       /* OImode shouldn't be used directly.  */
6205       gcc_unreachable ();
6206     case CTImode:
6207       return 0;
6208     case SFmode:
6209       if (!(bit_offset % 64))
6210         classes[0] = X86_64_SSESF_CLASS;
6211       else
6212         classes[0] = X86_64_SSE_CLASS;
6213       return 1;
6214     case DFmode:
6215       classes[0] = X86_64_SSEDF_CLASS;
6216       return 1;
6217     case XFmode:
6218       classes[0] = X86_64_X87_CLASS;
6219       classes[1] = X86_64_X87UP_CLASS;
6220       return 2;
6221     case TFmode:
6222       classes[0] = X86_64_SSE_CLASS;
6223       classes[1] = X86_64_SSEUP_CLASS;
6224       return 2;
6225     case SCmode:
6226       classes[0] = X86_64_SSE_CLASS;
6227       if (!(bit_offset % 64))
6228         return 1;
6229       else
6230         {
6231           static bool warned;
6232
6233           if (!warned && warn_psabi)
6234             {
6235               warned = true;
6236               inform (input_location,
6237                       "The ABI of passing structure with complex float"
6238                       " member has changed in GCC 4.4");
6239             }
6240           classes[1] = X86_64_SSESF_CLASS;
6241           return 2;
6242         }
6243     case DCmode:
6244       classes[0] = X86_64_SSEDF_CLASS;
6245       classes[1] = X86_64_SSEDF_CLASS;
6246       return 2;
6247     case XCmode:
6248       classes[0] = X86_64_COMPLEX_X87_CLASS;
6249       return 1;
6250     case TCmode:
6251       /* This modes is larger than 16 bytes.  */
6252       return 0;
6253     case V8SFmode:
6254     case V8SImode:
6255     case V32QImode:
6256     case V16HImode:
6257     case V4DFmode:
6258     case V4DImode:
6259       classes[0] = X86_64_SSE_CLASS;
6260       classes[1] = X86_64_SSEUP_CLASS;
6261       classes[2] = X86_64_SSEUP_CLASS;
6262       classes[3] = X86_64_SSEUP_CLASS;
6263       return 4;
6264     case V4SFmode:
6265     case V4SImode:
6266     case V16QImode:
6267     case V8HImode:
6268     case V2DFmode:
6269     case V2DImode:
6270       classes[0] = X86_64_SSE_CLASS;
6271       classes[1] = X86_64_SSEUP_CLASS;
6272       return 2;
6273     case V1TImode:
6274     case V1DImode:
6275     case V2SFmode:
6276     case V2SImode:
6277     case V4HImode:
6278     case V8QImode:
6279       classes[0] = X86_64_SSE_CLASS;
6280       return 1;
6281     case BLKmode:
6282     case VOIDmode:
6283       return 0;
6284     default:
6285       gcc_assert (VECTOR_MODE_P (mode));
6286
6287       if (bytes > 16)
6288         return 0;
6289
6290       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
6291
6292       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
6293         classes[0] = X86_64_INTEGERSI_CLASS;
6294       else
6295         classes[0] = X86_64_INTEGER_CLASS;
6296       classes[1] = X86_64_INTEGER_CLASS;
6297       return 1 + (bytes > 8);
6298     }
6299 }
6300
6301 /* Examine the argument and return set number of register required in each
6302    class.  Return 0 iff parameter should be passed in memory.  */
6303 static int
6304 examine_argument (enum machine_mode mode, const_tree type, int in_return,
6305                   int *int_nregs, int *sse_nregs)
6306 {
6307   enum x86_64_reg_class regclass[MAX_CLASSES];
6308   int n = classify_argument (mode, type, regclass, 0);
6309
6310   *int_nregs = 0;
6311   *sse_nregs = 0;
6312   if (!n)
6313     return 0;
6314   for (n--; n >= 0; n--)
6315     switch (regclass[n])
6316       {
6317       case X86_64_INTEGER_CLASS:
6318       case X86_64_INTEGERSI_CLASS:
6319         (*int_nregs)++;
6320         break;
6321       case X86_64_SSE_CLASS:
6322       case X86_64_SSESF_CLASS:
6323       case X86_64_SSEDF_CLASS:
6324         (*sse_nregs)++;
6325         break;
6326       case X86_64_NO_CLASS:
6327       case X86_64_SSEUP_CLASS:
6328         break;
6329       case X86_64_X87_CLASS:
6330       case X86_64_X87UP_CLASS:
6331         if (!in_return)
6332           return 0;
6333         break;
6334       case X86_64_COMPLEX_X87_CLASS:
6335         return in_return ? 2 : 0;
6336       case X86_64_MEMORY_CLASS:
6337         gcc_unreachable ();
6338       }
6339   return 1;
6340 }
6341
6342 /* Construct container for the argument used by GCC interface.  See
6343    FUNCTION_ARG for the detailed description.  */
6344
6345 static rtx
6346 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
6347                      const_tree type, int in_return, int nintregs, int nsseregs,
6348                      const int *intreg, int sse_regno)
6349 {
6350   /* The following variables hold the static issued_error state.  */
6351   static bool issued_sse_arg_error;
6352   static bool issued_sse_ret_error;
6353   static bool issued_x87_ret_error;
6354
6355   enum machine_mode tmpmode;
6356   int bytes =
6357     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
6358   enum x86_64_reg_class regclass[MAX_CLASSES];
6359   int n;
6360   int i;
6361   int nexps = 0;
6362   int needed_sseregs, needed_intregs;
6363   rtx exp[MAX_CLASSES];
6364   rtx ret;
6365
6366   n = classify_argument (mode, type, regclass, 0);
6367   if (!n)
6368     return NULL;
6369   if (!examine_argument (mode, type, in_return, &needed_intregs,
6370                          &needed_sseregs))
6371     return NULL;
6372   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
6373     return NULL;
6374
6375   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
6376      some less clueful developer tries to use floating-point anyway.  */
6377   if (needed_sseregs && !TARGET_SSE)
6378     {
6379       if (in_return)
6380         {
6381           if (!issued_sse_ret_error)
6382             {
6383               error ("SSE register return with SSE disabled");
6384               issued_sse_ret_error = true;
6385             }
6386         }
6387       else if (!issued_sse_arg_error)
6388         {
6389           error ("SSE register argument with SSE disabled");
6390           issued_sse_arg_error = true;
6391         }
6392       return NULL;
6393     }
6394
6395   /* Likewise, error if the ABI requires us to return values in the
6396      x87 registers and the user specified -mno-80387.  */
6397   if (!TARGET_80387 && in_return)
6398     for (i = 0; i < n; i++)
6399       if (regclass[i] == X86_64_X87_CLASS
6400           || regclass[i] == X86_64_X87UP_CLASS
6401           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
6402         {
6403           if (!issued_x87_ret_error)
6404             {
6405               error ("x87 register return with x87 disabled");
6406               issued_x87_ret_error = true;
6407             }
6408           return NULL;
6409         }
6410
6411   /* First construct simple cases.  Avoid SCmode, since we want to use
6412      single register to pass this type.  */
6413   if (n == 1 && mode != SCmode)
6414     switch (regclass[0])
6415       {
6416       case X86_64_INTEGER_CLASS:
6417       case X86_64_INTEGERSI_CLASS:
6418         return gen_rtx_REG (mode, intreg[0]);
6419       case X86_64_SSE_CLASS:
6420       case X86_64_SSESF_CLASS:
6421       case X86_64_SSEDF_CLASS:
6422         if (mode != BLKmode)
6423           return gen_reg_or_parallel (mode, orig_mode,
6424                                       SSE_REGNO (sse_regno));
6425         break;
6426       case X86_64_X87_CLASS:
6427       case X86_64_COMPLEX_X87_CLASS:
6428         return gen_rtx_REG (mode, FIRST_STACK_REG);
6429       case X86_64_NO_CLASS:
6430         /* Zero sized array, struct or class.  */
6431         return NULL;
6432       default:
6433         gcc_unreachable ();
6434       }
6435   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
6436       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
6437     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6438   if (n == 4
6439       && regclass[0] == X86_64_SSE_CLASS
6440       && regclass[1] == X86_64_SSEUP_CLASS
6441       && regclass[2] == X86_64_SSEUP_CLASS
6442       && regclass[3] == X86_64_SSEUP_CLASS
6443       && mode != BLKmode)
6444     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6445
6446   if (n == 2
6447       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
6448     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
6449   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
6450       && regclass[1] == X86_64_INTEGER_CLASS
6451       && (mode == CDImode || mode == TImode || mode == TFmode)
6452       && intreg[0] + 1 == intreg[1])
6453     return gen_rtx_REG (mode, intreg[0]);
6454
6455   /* Otherwise figure out the entries of the PARALLEL.  */
6456   for (i = 0; i < n; i++)
6457     {
6458       int pos;
6459
6460       switch (regclass[i])
6461         {
6462           case X86_64_NO_CLASS:
6463             break;
6464           case X86_64_INTEGER_CLASS:
6465           case X86_64_INTEGERSI_CLASS:
6466             /* Merge TImodes on aligned occasions here too.  */
6467             if (i * 8 + 8 > bytes)
6468               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
6469             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
6470               tmpmode = SImode;
6471             else
6472               tmpmode = DImode;
6473             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
6474             if (tmpmode == BLKmode)
6475               tmpmode = DImode;
6476             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6477                                                gen_rtx_REG (tmpmode, *intreg),
6478                                                GEN_INT (i*8));
6479             intreg++;
6480             break;
6481           case X86_64_SSESF_CLASS:
6482             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6483                                                gen_rtx_REG (SFmode,
6484                                                             SSE_REGNO (sse_regno)),
6485                                                GEN_INT (i*8));
6486             sse_regno++;
6487             break;
6488           case X86_64_SSEDF_CLASS:
6489             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6490                                                gen_rtx_REG (DFmode,
6491                                                             SSE_REGNO (sse_regno)),
6492                                                GEN_INT (i*8));
6493             sse_regno++;
6494             break;
6495           case X86_64_SSE_CLASS:
6496             pos = i;
6497             switch (n)
6498               {
6499               case 1:
6500                 tmpmode = DImode;
6501                 break;
6502               case 2:
6503                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
6504                   {
6505                     tmpmode = TImode;
6506                     i++;
6507                   }
6508                 else
6509                   tmpmode = DImode;
6510                 break;
6511               case 4:
6512                 gcc_assert (i == 0
6513                             && regclass[1] == X86_64_SSEUP_CLASS
6514                             && regclass[2] == X86_64_SSEUP_CLASS
6515                             && regclass[3] == X86_64_SSEUP_CLASS);
6516                 tmpmode = OImode;
6517                 i += 3;
6518                 break;
6519               default:
6520                 gcc_unreachable ();
6521               }
6522             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6523                                                gen_rtx_REG (tmpmode,
6524                                                             SSE_REGNO (sse_regno)),
6525                                                GEN_INT (pos*8));
6526             sse_regno++;
6527             break;
6528           default:
6529             gcc_unreachable ();
6530         }
6531     }
6532
6533   /* Empty aligned struct, union or class.  */
6534   if (nexps == 0)
6535     return NULL;
6536
6537   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
6538   for (i = 0; i < nexps; i++)
6539     XVECEXP (ret, 0, i) = exp [i];
6540   return ret;
6541 }
6542
6543 /* Update the data in CUM to advance over an argument of mode MODE
6544    and data type TYPE.  (TYPE is null for libcalls where that information
6545    may not be available.)  */
6546
6547 static void
6548 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6549                          const_tree type, HOST_WIDE_INT bytes,
6550                          HOST_WIDE_INT words)
6551 {
6552   switch (mode)
6553     {
6554     default:
6555       break;
6556
6557     case BLKmode:
6558       if (bytes < 0)
6559         break;
6560       /* FALLTHRU */
6561
6562     case DImode:
6563     case SImode:
6564     case HImode:
6565     case QImode:
6566       cum->words += words;
6567       cum->nregs -= words;
6568       cum->regno += words;
6569
6570       if (cum->nregs <= 0)
6571         {
6572           cum->nregs = 0;
6573           cum->regno = 0;
6574         }
6575       break;
6576
6577     case OImode:
6578       /* OImode shouldn't be used directly.  */
6579       gcc_unreachable ();
6580
6581     case DFmode:
6582       if (cum->float_in_sse < 2)
6583         break;
6584     case SFmode:
6585       if (cum->float_in_sse < 1)
6586         break;
6587       /* FALLTHRU */
6588
6589     case V8SFmode:
6590     case V8SImode:
6591     case V32QImode:
6592     case V16HImode:
6593     case V4DFmode:
6594     case V4DImode:
6595     case TImode:
6596     case V16QImode:
6597     case V8HImode:
6598     case V4SImode:
6599     case V2DImode:
6600     case V4SFmode:
6601     case V2DFmode:
6602       if (!type || !AGGREGATE_TYPE_P (type))
6603         {
6604           cum->sse_words += words;
6605           cum->sse_nregs -= 1;
6606           cum->sse_regno += 1;
6607           if (cum->sse_nregs <= 0)
6608             {
6609               cum->sse_nregs = 0;
6610               cum->sse_regno = 0;
6611             }
6612         }
6613       break;
6614
6615     case V8QImode:
6616     case V4HImode:
6617     case V2SImode:
6618     case V2SFmode:
6619     case V1TImode:
6620     case V1DImode:
6621       if (!type || !AGGREGATE_TYPE_P (type))
6622         {
6623           cum->mmx_words += words;
6624           cum->mmx_nregs -= 1;
6625           cum->mmx_regno += 1;
6626           if (cum->mmx_nregs <= 0)
6627             {
6628               cum->mmx_nregs = 0;
6629               cum->mmx_regno = 0;
6630             }
6631         }
6632       break;
6633     }
6634 }
6635
6636 static void
6637 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6638                          const_tree type, HOST_WIDE_INT words, bool named)
6639 {
6640   int int_nregs, sse_nregs;
6641
6642   /* Unnamed 256bit vector mode parameters are passed on stack.  */
6643   if (!named && VALID_AVX256_REG_MODE (mode))
6644     return;
6645
6646   if (examine_argument (mode, type, 0, &int_nregs, &sse_nregs)
6647       && sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
6648     {
6649       cum->nregs -= int_nregs;
6650       cum->sse_nregs -= sse_nregs;
6651       cum->regno += int_nregs;
6652       cum->sse_regno += sse_nregs;
6653     }
6654   else
6655     {
6656       int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD;
6657       cum->words = (cum->words + align - 1) & ~(align - 1);
6658       cum->words += words;
6659     }
6660 }
6661
6662 static void
6663 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
6664                             HOST_WIDE_INT words)
6665 {
6666   /* Otherwise, this should be passed indirect.  */
6667   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
6668
6669   cum->words += words;
6670   if (cum->nregs > 0)
6671     {
6672       cum->nregs -= 1;
6673       cum->regno += 1;
6674     }
6675 }
6676
6677 /* Update the data in CUM to advance over an argument of mode MODE and
6678    data type TYPE.  (TYPE is null for libcalls where that information
6679    may not be available.)  */
6680
6681 static void
6682 ix86_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6683                            const_tree type, bool named)
6684 {
6685   HOST_WIDE_INT bytes, words;
6686
6687   if (mode == BLKmode)
6688     bytes = int_size_in_bytes (type);
6689   else
6690     bytes = GET_MODE_SIZE (mode);
6691   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6692
6693   if (type)
6694     mode = type_natural_mode (type, NULL);
6695
6696   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6697     function_arg_advance_ms_64 (cum, bytes, words);
6698   else if (TARGET_64BIT)
6699     function_arg_advance_64 (cum, mode, type, words, named);
6700   else
6701     function_arg_advance_32 (cum, mode, type, bytes, words);
6702 }
6703
6704 /* Define where to put the arguments to a function.
6705    Value is zero to push the argument on the stack,
6706    or a hard register in which to store the argument.
6707
6708    MODE is the argument's machine mode.
6709    TYPE is the data type of the argument (as a tree).
6710     This is null for libcalls where that information may
6711     not be available.
6712    CUM is a variable of type CUMULATIVE_ARGS which gives info about
6713     the preceding args and about the function being called.
6714    NAMED is nonzero if this argument is a named parameter
6715     (otherwise it is an extra parameter matching an ellipsis).  */
6716
6717 static rtx
6718 function_arg_32 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6719                  enum machine_mode orig_mode, const_tree type,
6720                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
6721 {
6722   static bool warnedsse, warnedmmx;
6723
6724   /* Avoid the AL settings for the Unix64 ABI.  */
6725   if (mode == VOIDmode)
6726     return constm1_rtx;
6727
6728   switch (mode)
6729     {
6730     default:
6731       break;
6732
6733     case BLKmode:
6734       if (bytes < 0)
6735         break;
6736       /* FALLTHRU */
6737     case DImode:
6738     case SImode:
6739     case HImode:
6740     case QImode:
6741       if (words <= cum->nregs)
6742         {
6743           int regno = cum->regno;
6744
6745           /* Fastcall allocates the first two DWORD (SImode) or
6746             smaller arguments to ECX and EDX if it isn't an
6747             aggregate type .  */
6748           if (cum->fastcall)
6749             {
6750               if (mode == BLKmode
6751                   || mode == DImode
6752                   || (type && AGGREGATE_TYPE_P (type)))
6753                 break;
6754
6755               /* ECX not EAX is the first allocated register.  */
6756               if (regno == AX_REG)
6757                 regno = CX_REG;
6758             }
6759           return gen_rtx_REG (mode, regno);
6760         }
6761       break;
6762
6763     case DFmode:
6764       if (cum->float_in_sse < 2)
6765         break;
6766     case SFmode:
6767       if (cum->float_in_sse < 1)
6768         break;
6769       /* FALLTHRU */
6770     case TImode:
6771       /* In 32bit, we pass TImode in xmm registers.  */
6772     case V16QImode:
6773     case V8HImode:
6774     case V4SImode:
6775     case V2DImode:
6776     case V4SFmode:
6777     case V2DFmode:
6778       if (!type || !AGGREGATE_TYPE_P (type))
6779         {
6780           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
6781             {
6782               warnedsse = true;
6783               warning (0, "SSE vector argument without SSE enabled "
6784                        "changes the ABI");
6785             }
6786           if (cum->sse_nregs)
6787             return gen_reg_or_parallel (mode, orig_mode,
6788                                         cum->sse_regno + FIRST_SSE_REG);
6789         }
6790       break;
6791
6792     case OImode:
6793       /* OImode shouldn't be used directly.  */
6794       gcc_unreachable ();
6795
6796     case V8SFmode:
6797     case V8SImode:
6798     case V32QImode:
6799     case V16HImode:
6800     case V4DFmode:
6801     case V4DImode:
6802       if (!type || !AGGREGATE_TYPE_P (type))
6803         {
6804           if (cum->sse_nregs)
6805             return gen_reg_or_parallel (mode, orig_mode,
6806                                         cum->sse_regno + FIRST_SSE_REG);
6807         }
6808       break;
6809
6810     case V8QImode:
6811     case V4HImode:
6812     case V2SImode:
6813     case V2SFmode:
6814     case V1TImode:
6815     case V1DImode:
6816       if (!type || !AGGREGATE_TYPE_P (type))
6817         {
6818           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6819             {
6820               warnedmmx = true;
6821               warning (0, "MMX vector argument without MMX enabled "
6822                        "changes the ABI");
6823             }
6824           if (cum->mmx_nregs)
6825             return gen_reg_or_parallel (mode, orig_mode,
6826                                         cum->mmx_regno + FIRST_MMX_REG);
6827         }
6828       break;
6829     }
6830
6831   return NULL_RTX;
6832 }
6833
6834 static rtx
6835 function_arg_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6836                  enum machine_mode orig_mode, const_tree type, bool named)
6837 {
6838   /* Handle a hidden AL argument containing number of registers
6839      for varargs x86-64 functions.  */
6840   if (mode == VOIDmode)
6841     return GEN_INT (cum->maybe_vaarg
6842                     ? (cum->sse_nregs < 0
6843                        ? X86_64_SSE_REGPARM_MAX
6844                        : cum->sse_regno)
6845                     : -1);
6846
6847   switch (mode)
6848     {
6849     default:
6850       break;
6851
6852     case V8SFmode:
6853     case V8SImode:
6854     case V32QImode:
6855     case V16HImode:
6856     case V4DFmode:
6857     case V4DImode:
6858       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6859       if (!named)
6860         return NULL;
6861       break;
6862     }
6863
6864   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6865                               cum->sse_nregs,
6866                               &x86_64_int_parameter_registers [cum->regno],
6867                               cum->sse_regno);
6868 }
6869
6870 static rtx
6871 function_arg_ms_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6872                     enum machine_mode orig_mode, bool named,
6873                     HOST_WIDE_INT bytes)
6874 {
6875   unsigned int regno;
6876
6877   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6878      We use value of -2 to specify that current function call is MSABI.  */
6879   if (mode == VOIDmode)
6880     return GEN_INT (-2);
6881
6882   /* If we've run out of registers, it goes on the stack.  */
6883   if (cum->nregs == 0)
6884     return NULL_RTX;
6885
6886   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
6887
6888   /* Only floating point modes are passed in anything but integer regs.  */
6889   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
6890     {
6891       if (named)
6892         regno = cum->regno + FIRST_SSE_REG;
6893       else
6894         {
6895           rtx t1, t2;
6896
6897           /* Unnamed floating parameters are passed in both the
6898              SSE and integer registers.  */
6899           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
6900           t2 = gen_rtx_REG (mode, regno);
6901           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
6902           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
6903           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
6904         }
6905     }
6906   /* Handle aggregated types passed in register.  */
6907   if (orig_mode == BLKmode)
6908     {
6909       if (bytes > 0 && bytes <= 8)
6910         mode = (bytes > 4 ? DImode : SImode);
6911       if (mode == BLKmode)
6912         mode = DImode;
6913     }
6914
6915   return gen_reg_or_parallel (mode, orig_mode, regno);
6916 }
6917
6918 /* Return where to put the arguments to a function.
6919    Return zero to push the argument on the stack, or a hard register in which to store the argument.
6920
6921    MODE is the argument's machine mode.  TYPE is the data type of the
6922    argument.  It is null for libcalls where that information may not be
6923    available.  CUM gives information about the preceding args and about
6924    the function being called.  NAMED is nonzero if this argument is a
6925    named parameter (otherwise it is an extra parameter matching an
6926    ellipsis).  */
6927
6928 static rtx
6929 ix86_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
6930                    const_tree type, bool named)
6931 {
6932   enum machine_mode mode = omode;
6933   HOST_WIDE_INT bytes, words;
6934   rtx arg;
6935
6936   if (mode == BLKmode)
6937     bytes = int_size_in_bytes (type);
6938   else
6939     bytes = GET_MODE_SIZE (mode);
6940   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6941
6942   /* To simplify the code below, represent vector types with a vector mode
6943      even if MMX/SSE are not active.  */
6944   if (type && TREE_CODE (type) == VECTOR_TYPE)
6945     mode = type_natural_mode (type, cum);
6946
6947   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6948     arg = function_arg_ms_64 (cum, mode, omode, named, bytes);
6949   else if (TARGET_64BIT)
6950     arg = function_arg_64 (cum, mode, omode, type, named);
6951   else
6952     arg = function_arg_32 (cum, mode, omode, type, bytes, words);
6953
6954   if (TARGET_VZEROUPPER && function_pass_avx256_p (arg))
6955     {
6956       /* This argument uses 256bit AVX modes.  */
6957       cfun->machine->use_avx256_p = true;
6958       if (cum->caller)
6959         cfun->machine->callee_pass_avx256_p = true;
6960       else
6961         cfun->machine->caller_pass_avx256_p = true;
6962     }
6963
6964   return arg;
6965 }
6966
6967 /* A C expression that indicates when an argument must be passed by
6968    reference.  If nonzero for an argument, a copy of that argument is
6969    made in memory and a pointer to the argument is passed instead of
6970    the argument itself.  The pointer is passed in whatever way is
6971    appropriate for passing a pointer to that type.  */
6972
6973 static bool
6974 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
6975                         enum machine_mode mode ATTRIBUTE_UNUSED,
6976                         const_tree type, bool named ATTRIBUTE_UNUSED)
6977 {
6978   /* See Windows x64 Software Convention.  */
6979   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6980     {
6981       int msize = (int) GET_MODE_SIZE (mode);
6982       if (type)
6983         {
6984           /* Arrays are passed by reference.  */
6985           if (TREE_CODE (type) == ARRAY_TYPE)
6986             return true;
6987
6988           if (AGGREGATE_TYPE_P (type))
6989             {
6990               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
6991                  are passed by reference.  */
6992               msize = int_size_in_bytes (type);
6993             }
6994         }
6995
6996       /* __m128 is passed by reference.  */
6997       switch (msize) {
6998       case 1: case 2: case 4: case 8:
6999         break;
7000       default:
7001         return true;
7002       }
7003     }
7004   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
7005     return 1;
7006
7007   return 0;
7008 }
7009
7010 /* Return true when TYPE should be 128bit aligned for 32bit argument
7011    passing ABI.  XXX: This function is obsolete and is only used for
7012    checking psABI compatibility with previous versions of GCC.  */
7013
7014 static bool
7015 ix86_compat_aligned_value_p (const_tree type)
7016 {
7017   enum machine_mode mode = TYPE_MODE (type);
7018   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
7019        || mode == TDmode
7020        || mode == TFmode
7021        || mode == TCmode)
7022       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
7023     return true;
7024   if (TYPE_ALIGN (type) < 128)
7025     return false;
7026
7027   if (AGGREGATE_TYPE_P (type))
7028     {
7029       /* Walk the aggregates recursively.  */
7030       switch (TREE_CODE (type))
7031         {
7032         case RECORD_TYPE:
7033         case UNION_TYPE:
7034         case QUAL_UNION_TYPE:
7035           {
7036             tree field;
7037
7038             /* Walk all the structure fields.  */
7039             for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7040               {
7041                 if (TREE_CODE (field) == FIELD_DECL
7042                     && ix86_compat_aligned_value_p (TREE_TYPE (field)))
7043                   return true;
7044               }
7045             break;
7046           }
7047
7048         case ARRAY_TYPE:
7049           /* Just for use if some languages passes arrays by value.  */
7050           if (ix86_compat_aligned_value_p (TREE_TYPE (type)))
7051             return true;
7052           break;
7053
7054         default:
7055           gcc_unreachable ();
7056         }
7057     }
7058   return false;
7059 }
7060
7061 /* Return the alignment boundary for MODE and TYPE with alignment ALIGN.
7062    XXX: This function is obsolete and is only used for checking psABI
7063    compatibility with previous versions of GCC.  */
7064
7065 static int
7066 ix86_compat_function_arg_boundary (enum machine_mode mode,
7067                                    const_tree type, int align)
7068 {
7069   /* In 32bit, only _Decimal128 and __float128 are aligned to their
7070      natural boundaries.  */
7071   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
7072     {
7073       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
7074          make an exception for SSE modes since these require 128bit
7075          alignment.
7076
7077          The handling here differs from field_alignment.  ICC aligns MMX
7078          arguments to 4 byte boundaries, while structure fields are aligned
7079          to 8 byte boundaries.  */
7080       if (!type)
7081         {
7082           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
7083             align = PARM_BOUNDARY;
7084         }
7085       else
7086         {
7087           if (!ix86_compat_aligned_value_p (type))
7088             align = PARM_BOUNDARY;
7089         }
7090     }
7091   if (align > BIGGEST_ALIGNMENT)
7092     align = BIGGEST_ALIGNMENT;
7093   return align;
7094 }
7095
7096 /* Return true when TYPE should be 128bit aligned for 32bit argument
7097    passing ABI.  */
7098
7099 static bool
7100 ix86_contains_aligned_value_p (const_tree type)
7101 {
7102   enum machine_mode mode = TYPE_MODE (type);
7103
7104   if (mode == XFmode || mode == XCmode)
7105     return false;
7106
7107   if (TYPE_ALIGN (type) < 128)
7108     return false;
7109
7110   if (AGGREGATE_TYPE_P (type))
7111     {
7112       /* Walk the aggregates recursively.  */
7113       switch (TREE_CODE (type))
7114         {
7115         case RECORD_TYPE:
7116         case UNION_TYPE:
7117         case QUAL_UNION_TYPE:
7118           {
7119             tree field;
7120
7121             /* Walk all the structure fields.  */
7122             for (field = TYPE_FIELDS (type);
7123                  field;
7124                  field = DECL_CHAIN (field))
7125               {
7126                 if (TREE_CODE (field) == FIELD_DECL
7127                     && ix86_contains_aligned_value_p (TREE_TYPE (field)))
7128                   return true;
7129               }
7130             break;
7131           }
7132
7133         case ARRAY_TYPE:
7134           /* Just for use if some languages passes arrays by value.  */
7135           if (ix86_contains_aligned_value_p (TREE_TYPE (type)))
7136             return true;
7137           break;
7138
7139         default:
7140           gcc_unreachable ();
7141         }
7142     }
7143   else
7144     return TYPE_ALIGN (type) >= 128;
7145
7146   return false;
7147 }
7148
7149 /* Gives the alignment boundary, in bits, of an argument with the
7150    specified mode and type.  */
7151
7152 int
7153 ix86_function_arg_boundary (enum machine_mode mode, const_tree type)
7154 {
7155   int align;
7156   if (type)
7157     {
7158       /* Since the main variant type is used for call, we convert it to
7159          the main variant type.  */
7160       type = TYPE_MAIN_VARIANT (type);
7161       align = TYPE_ALIGN (type);
7162     }
7163   else
7164     align = GET_MODE_ALIGNMENT (mode);
7165   if (align < PARM_BOUNDARY)
7166     align = PARM_BOUNDARY;
7167   else
7168     {
7169       static bool warned;
7170       int saved_align = align;
7171
7172       if (!TARGET_64BIT)
7173         {
7174           /* i386 ABI defines XFmode arguments to be 4 byte aligned.  */
7175           if (!type)
7176             {
7177               if (mode == XFmode || mode == XCmode)
7178                 align = PARM_BOUNDARY;
7179             }
7180           else if (!ix86_contains_aligned_value_p (type))
7181             align = PARM_BOUNDARY;
7182
7183           if (align < 128)
7184             align = PARM_BOUNDARY;
7185         }
7186
7187       if (warn_psabi
7188           && !warned
7189           && align != ix86_compat_function_arg_boundary (mode, type,
7190                                                          saved_align))
7191         {
7192           warned = true;
7193           inform (input_location,
7194                   "The ABI of passing parameter with %dbyte"
7195                   " alignment has changed in GCC 4.6",
7196                   align / BITS_PER_UNIT);
7197         }
7198     }
7199
7200   return align;
7201 }
7202
7203 /* Return true if N is a possible register number of function value.  */
7204
7205 static bool
7206 ix86_function_value_regno_p (const unsigned int regno)
7207 {
7208   switch (regno)
7209     {
7210     case 0:
7211       return true;
7212
7213     case FIRST_FLOAT_REG:
7214       /* TODO: The function should depend on current function ABI but
7215        builtins.c would need updating then. Therefore we use the
7216        default ABI.  */
7217       if (TARGET_64BIT && ix86_abi == MS_ABI)
7218         return false;
7219       return TARGET_FLOAT_RETURNS_IN_80387;
7220
7221     case FIRST_SSE_REG:
7222       return TARGET_SSE;
7223
7224     case FIRST_MMX_REG:
7225       if (TARGET_MACHO || TARGET_64BIT)
7226         return false;
7227       return TARGET_MMX;
7228     }
7229
7230   return false;
7231 }
7232
7233 /* Define how to find the value returned by a function.
7234    VALTYPE is the data type of the value (as a tree).
7235    If the precise function being called is known, FUNC is its FUNCTION_DECL;
7236    otherwise, FUNC is 0.  */
7237
7238 static rtx
7239 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
7240                    const_tree fntype, const_tree fn)
7241 {
7242   unsigned int regno;
7243
7244   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
7245      we normally prevent this case when mmx is not available.  However
7246      some ABIs may require the result to be returned like DImode.  */
7247   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7248     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
7249
7250   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
7251      we prevent this case when sse is not available.  However some ABIs
7252      may require the result to be returned like integer TImode.  */
7253   else if (mode == TImode
7254            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7255     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
7256
7257   /* 32-byte vector modes in %ymm0.   */
7258   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
7259     regno = TARGET_AVX ? FIRST_SSE_REG : 0;
7260
7261   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
7262   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
7263     regno = FIRST_FLOAT_REG;
7264   else
7265     /* Most things go in %eax.  */
7266     regno = AX_REG;
7267
7268   /* Override FP return register with %xmm0 for local functions when
7269      SSE math is enabled or for functions with sseregparm attribute.  */
7270   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
7271     {
7272       int sse_level = ix86_function_sseregparm (fntype, fn, false);
7273       if ((sse_level >= 1 && mode == SFmode)
7274           || (sse_level == 2 && mode == DFmode))
7275         regno = FIRST_SSE_REG;
7276     }
7277
7278   /* OImode shouldn't be used directly.  */
7279   gcc_assert (mode != OImode);
7280
7281   return gen_rtx_REG (orig_mode, regno);
7282 }
7283
7284 static rtx
7285 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
7286                    const_tree valtype)
7287 {
7288   rtx ret;
7289
7290   /* Handle libcalls, which don't provide a type node.  */
7291   if (valtype == NULL)
7292     {
7293       switch (mode)
7294         {
7295         case SFmode:
7296         case SCmode:
7297         case DFmode:
7298         case DCmode:
7299         case TFmode:
7300         case SDmode:
7301         case DDmode:
7302         case TDmode:
7303           return gen_rtx_REG (mode, FIRST_SSE_REG);
7304         case XFmode:
7305         case XCmode:
7306           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
7307         case TCmode:
7308           return NULL;
7309         default:
7310           return gen_rtx_REG (mode, AX_REG);
7311         }
7312     }
7313
7314   ret = construct_container (mode, orig_mode, valtype, 1,
7315                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
7316                              x86_64_int_return_registers, 0);
7317
7318   /* For zero sized structures, construct_container returns NULL, but we
7319      need to keep rest of compiler happy by returning meaningful value.  */
7320   if (!ret)
7321     ret = gen_rtx_REG (orig_mode, AX_REG);
7322
7323   return ret;
7324 }
7325
7326 static rtx
7327 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
7328 {
7329   unsigned int regno = AX_REG;
7330
7331   if (TARGET_SSE)
7332     {
7333       switch (GET_MODE_SIZE (mode))
7334         {
7335         case 16:
7336           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7337              && !COMPLEX_MODE_P (mode))
7338             regno = FIRST_SSE_REG;
7339           break;
7340         case 8:
7341         case 4:
7342           if (mode == SFmode || mode == DFmode)
7343             regno = FIRST_SSE_REG;
7344           break;
7345         default:
7346           break;
7347         }
7348     }
7349   return gen_rtx_REG (orig_mode, regno);
7350 }
7351
7352 static rtx
7353 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
7354                        enum machine_mode orig_mode, enum machine_mode mode)
7355 {
7356   const_tree fn, fntype;
7357
7358   fn = NULL_TREE;
7359   if (fntype_or_decl && DECL_P (fntype_or_decl))
7360     fn = fntype_or_decl;
7361   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
7362
7363   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
7364     return function_value_ms_64 (orig_mode, mode);
7365   else if (TARGET_64BIT)
7366     return function_value_64 (orig_mode, mode, valtype);
7367   else
7368     return function_value_32 (orig_mode, mode, fntype, fn);
7369 }
7370
7371 static rtx
7372 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
7373                      bool outgoing ATTRIBUTE_UNUSED)
7374 {
7375   enum machine_mode mode, orig_mode;
7376
7377   orig_mode = TYPE_MODE (valtype);
7378   mode = type_natural_mode (valtype, NULL);
7379   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
7380 }
7381
7382 rtx
7383 ix86_libcall_value (enum machine_mode mode)
7384 {
7385   return ix86_function_value_1 (NULL, NULL, mode, mode);
7386 }
7387
7388 /* Return true iff type is returned in memory.  */
7389
7390 static bool ATTRIBUTE_UNUSED
7391 return_in_memory_32 (const_tree type, enum machine_mode mode)
7392 {
7393   HOST_WIDE_INT size;
7394
7395   if (mode == BLKmode)
7396     return true;
7397
7398   size = int_size_in_bytes (type);
7399
7400   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
7401     return false;
7402
7403   if (VECTOR_MODE_P (mode) || mode == TImode)
7404     {
7405       /* User-created vectors small enough to fit in EAX.  */
7406       if (size < 8)
7407         return false;
7408
7409       /* MMX/3dNow values are returned in MM0,
7410          except when it doesn't exits or the ABI prescribes otherwise.  */
7411       if (size == 8)
7412         return !TARGET_MMX || TARGET_VECT8_RETURNS;
7413
7414       /* SSE values are returned in XMM0, except when it doesn't exist.  */
7415       if (size == 16)
7416         return !TARGET_SSE;
7417
7418       /* AVX values are returned in YMM0, except when it doesn't exist.  */
7419       if (size == 32)
7420         return !TARGET_AVX;
7421     }
7422
7423   if (mode == XFmode)
7424     return false;
7425
7426   if (size > 12)
7427     return true;
7428
7429   /* OImode shouldn't be used directly.  */
7430   gcc_assert (mode != OImode);
7431
7432   return false;
7433 }
7434
7435 static bool ATTRIBUTE_UNUSED
7436 return_in_memory_64 (const_tree type, enum machine_mode mode)
7437 {
7438   int needed_intregs, needed_sseregs;
7439   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
7440 }
7441
7442 static bool ATTRIBUTE_UNUSED
7443 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
7444 {
7445   HOST_WIDE_INT size = int_size_in_bytes (type);
7446
7447   /* __m128 is returned in xmm0.  */
7448   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
7449       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
7450     return false;
7451
7452   /* Otherwise, the size must be exactly in [1248]. */
7453   return size != 1 && size != 2 && size != 4 && size != 8;
7454 }
7455
7456 static bool
7457 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7458 {
7459 #ifdef SUBTARGET_RETURN_IN_MEMORY
7460   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
7461 #else
7462   const enum machine_mode mode = type_natural_mode (type, NULL);
7463
7464   if (TARGET_64BIT)
7465     {
7466       if (ix86_function_type_abi (fntype) == MS_ABI)
7467         return return_in_memory_ms_64 (type, mode);
7468       else
7469         return return_in_memory_64 (type, mode);
7470     }
7471   else
7472     return return_in_memory_32 (type, mode);
7473 #endif
7474 }
7475
7476 /* When returning SSE vector types, we have a choice of either
7477      (1) being abi incompatible with a -march switch, or
7478      (2) generating an error.
7479    Given no good solution, I think the safest thing is one warning.
7480    The user won't be able to use -Werror, but....
7481
7482    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
7483    called in response to actually generating a caller or callee that
7484    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
7485    via aggregate_value_p for general type probing from tree-ssa.  */
7486
7487 static rtx
7488 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
7489 {
7490   static bool warnedsse, warnedmmx;
7491
7492   if (!TARGET_64BIT && type)
7493     {
7494       /* Look at the return type of the function, not the function type.  */
7495       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
7496
7497       if (!TARGET_SSE && !warnedsse)
7498         {
7499           if (mode == TImode
7500               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
7501             {
7502               warnedsse = true;
7503               warning (0, "SSE vector return without SSE enabled "
7504                        "changes the ABI");
7505             }
7506         }
7507
7508       if (!TARGET_MMX && !warnedmmx)
7509         {
7510           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
7511             {
7512               warnedmmx = true;
7513               warning (0, "MMX vector return without MMX enabled "
7514                        "changes the ABI");
7515             }
7516         }
7517     }
7518
7519   return NULL;
7520 }
7521
7522 \f
7523 /* Create the va_list data type.  */
7524
7525 /* Returns the calling convention specific va_list date type.
7526    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
7527
7528 static tree
7529 ix86_build_builtin_va_list_abi (enum calling_abi abi)
7530 {
7531   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
7532
7533   /* For i386 we use plain pointer to argument area.  */
7534   if (!TARGET_64BIT || abi == MS_ABI)
7535     return build_pointer_type (char_type_node);
7536
7537   record = lang_hooks.types.make_type (RECORD_TYPE);
7538   type_decl = build_decl (BUILTINS_LOCATION,
7539                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
7540
7541   f_gpr = build_decl (BUILTINS_LOCATION,
7542                       FIELD_DECL, get_identifier ("gp_offset"),
7543                       unsigned_type_node);
7544   f_fpr = build_decl (BUILTINS_LOCATION,
7545                       FIELD_DECL, get_identifier ("fp_offset"),
7546                       unsigned_type_node);
7547   f_ovf = build_decl (BUILTINS_LOCATION,
7548                       FIELD_DECL, get_identifier ("overflow_arg_area"),
7549                       ptr_type_node);
7550   f_sav = build_decl (BUILTINS_LOCATION,
7551                       FIELD_DECL, get_identifier ("reg_save_area"),
7552                       ptr_type_node);
7553
7554   va_list_gpr_counter_field = f_gpr;
7555   va_list_fpr_counter_field = f_fpr;
7556
7557   DECL_FIELD_CONTEXT (f_gpr) = record;
7558   DECL_FIELD_CONTEXT (f_fpr) = record;
7559   DECL_FIELD_CONTEXT (f_ovf) = record;
7560   DECL_FIELD_CONTEXT (f_sav) = record;
7561
7562   TYPE_STUB_DECL (record) = type_decl;
7563   TYPE_NAME (record) = type_decl;
7564   TYPE_FIELDS (record) = f_gpr;
7565   DECL_CHAIN (f_gpr) = f_fpr;
7566   DECL_CHAIN (f_fpr) = f_ovf;
7567   DECL_CHAIN (f_ovf) = f_sav;
7568
7569   layout_type (record);
7570
7571   /* The correct type is an array type of one element.  */
7572   return build_array_type (record, build_index_type (size_zero_node));
7573 }
7574
7575 /* Setup the builtin va_list data type and for 64-bit the additional
7576    calling convention specific va_list data types.  */
7577
7578 static tree
7579 ix86_build_builtin_va_list (void)
7580 {
7581   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
7582
7583   /* Initialize abi specific va_list builtin types.  */
7584   if (TARGET_64BIT)
7585     {
7586       tree t;
7587       if (ix86_abi == MS_ABI)
7588         {
7589           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
7590           if (TREE_CODE (t) != RECORD_TYPE)
7591             t = build_variant_type_copy (t);
7592           sysv_va_list_type_node = t;
7593         }
7594       else
7595         {
7596           t = ret;
7597           if (TREE_CODE (t) != RECORD_TYPE)
7598             t = build_variant_type_copy (t);
7599           sysv_va_list_type_node = t;
7600         }
7601       if (ix86_abi != MS_ABI)
7602         {
7603           t = ix86_build_builtin_va_list_abi (MS_ABI);
7604           if (TREE_CODE (t) != RECORD_TYPE)
7605             t = build_variant_type_copy (t);
7606           ms_va_list_type_node = t;
7607         }
7608       else
7609         {
7610           t = ret;
7611           if (TREE_CODE (t) != RECORD_TYPE)
7612             t = build_variant_type_copy (t);
7613           ms_va_list_type_node = t;
7614         }
7615     }
7616
7617   return ret;
7618 }
7619
7620 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
7621
7622 static void
7623 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
7624 {
7625   rtx save_area, mem;
7626   alias_set_type set;
7627   int i, max;
7628
7629   /* GPR size of varargs save area.  */
7630   if (cfun->va_list_gpr_size)
7631     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
7632   else
7633     ix86_varargs_gpr_size = 0;
7634
7635   /* FPR size of varargs save area.  We don't need it if we don't pass
7636      anything in SSE registers.  */
7637   if (TARGET_SSE && cfun->va_list_fpr_size)
7638     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
7639   else
7640     ix86_varargs_fpr_size = 0;
7641
7642   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
7643     return;
7644
7645   save_area = frame_pointer_rtx;
7646   set = get_varargs_alias_set ();
7647
7648   max = cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
7649   if (max > X86_64_REGPARM_MAX)
7650     max = X86_64_REGPARM_MAX;
7651
7652   for (i = cum->regno; i < max; i++)
7653     {
7654       mem = gen_rtx_MEM (Pmode,
7655                          plus_constant (save_area, i * UNITS_PER_WORD));
7656       MEM_NOTRAP_P (mem) = 1;
7657       set_mem_alias_set (mem, set);
7658       emit_move_insn (mem, gen_rtx_REG (Pmode,
7659                                         x86_64_int_parameter_registers[i]));
7660     }
7661
7662   if (ix86_varargs_fpr_size)
7663     {
7664       enum machine_mode smode;
7665       rtx label, test;
7666
7667       /* Now emit code to save SSE registers.  The AX parameter contains number
7668          of SSE parameter registers used to call this function, though all we
7669          actually check here is the zero/non-zero status.  */
7670
7671       label = gen_label_rtx ();
7672       test = gen_rtx_EQ (VOIDmode, gen_rtx_REG (QImode, AX_REG), const0_rtx);
7673       emit_jump_insn (gen_cbranchqi4 (test, XEXP (test, 0), XEXP (test, 1),
7674                                       label));
7675
7676       /* ??? If !TARGET_SSE_TYPELESS_STORES, would we perform better if
7677          we used movdqa (i.e. TImode) instead?  Perhaps even better would
7678          be if we could determine the real mode of the data, via a hook
7679          into pass_stdarg.  Ignore all that for now.  */
7680       smode = V4SFmode;
7681       if (crtl->stack_alignment_needed < GET_MODE_ALIGNMENT (smode))
7682         crtl->stack_alignment_needed = GET_MODE_ALIGNMENT (smode);
7683
7684       max = cum->sse_regno + cfun->va_list_fpr_size / 16;
7685       if (max > X86_64_SSE_REGPARM_MAX)
7686         max = X86_64_SSE_REGPARM_MAX;
7687
7688       for (i = cum->sse_regno; i < max; ++i)
7689         {
7690           mem = plus_constant (save_area, i * 16 + ix86_varargs_gpr_size);
7691           mem = gen_rtx_MEM (smode, mem);
7692           MEM_NOTRAP_P (mem) = 1;
7693           set_mem_alias_set (mem, set);
7694           set_mem_align (mem, GET_MODE_ALIGNMENT (smode));
7695
7696           emit_move_insn (mem, gen_rtx_REG (smode, SSE_REGNO (i)));
7697         }
7698
7699       emit_label (label);
7700     }
7701 }
7702
7703 static void
7704 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
7705 {
7706   alias_set_type set = get_varargs_alias_set ();
7707   int i;
7708
7709   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
7710     {
7711       rtx reg, mem;
7712
7713       mem = gen_rtx_MEM (Pmode,
7714                          plus_constant (virtual_incoming_args_rtx,
7715                                         i * UNITS_PER_WORD));
7716       MEM_NOTRAP_P (mem) = 1;
7717       set_mem_alias_set (mem, set);
7718
7719       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
7720       emit_move_insn (mem, reg);
7721     }
7722 }
7723
7724 static void
7725 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7726                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
7727                              int no_rtl)
7728 {
7729   CUMULATIVE_ARGS next_cum;
7730   tree fntype;
7731
7732   /* This argument doesn't appear to be used anymore.  Which is good,
7733      because the old code here didn't suppress rtl generation.  */
7734   gcc_assert (!no_rtl);
7735
7736   if (!TARGET_64BIT)
7737     return;
7738
7739   fntype = TREE_TYPE (current_function_decl);
7740
7741   /* For varargs, we do not want to skip the dummy va_dcl argument.
7742      For stdargs, we do want to skip the last named argument.  */
7743   next_cum = *cum;
7744   if (stdarg_p (fntype))
7745     ix86_function_arg_advance (&next_cum, mode, type, true);
7746
7747   if (cum->call_abi == MS_ABI)
7748     setup_incoming_varargs_ms_64 (&next_cum);
7749   else
7750     setup_incoming_varargs_64 (&next_cum);
7751 }
7752
7753 /* Checks if TYPE is of kind va_list char *.  */
7754
7755 static bool
7756 is_va_list_char_pointer (tree type)
7757 {
7758   tree canonic;
7759
7760   /* For 32-bit it is always true.  */
7761   if (!TARGET_64BIT)
7762     return true;
7763   canonic = ix86_canonical_va_list_type (type);
7764   return (canonic == ms_va_list_type_node
7765           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
7766 }
7767
7768 /* Implement va_start.  */
7769
7770 static void
7771 ix86_va_start (tree valist, rtx nextarg)
7772 {
7773   HOST_WIDE_INT words, n_gpr, n_fpr;
7774   tree f_gpr, f_fpr, f_ovf, f_sav;
7775   tree gpr, fpr, ovf, sav, t;
7776   tree type;
7777   rtx ovf_rtx;
7778
7779   if (flag_split_stack
7780       && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7781     {
7782       unsigned int scratch_regno;
7783
7784       /* When we are splitting the stack, we can't refer to the stack
7785          arguments using internal_arg_pointer, because they may be on
7786          the old stack.  The split stack prologue will arrange to
7787          leave a pointer to the old stack arguments in a scratch
7788          register, which we here copy to a pseudo-register.  The split
7789          stack prologue can't set the pseudo-register directly because
7790          it (the prologue) runs before any registers have been saved.  */
7791
7792       scratch_regno = split_stack_prologue_scratch_regno ();
7793       if (scratch_regno != INVALID_REGNUM)
7794         {
7795           rtx reg, seq;
7796
7797           reg = gen_reg_rtx (Pmode);
7798           cfun->machine->split_stack_varargs_pointer = reg;
7799
7800           start_sequence ();
7801           emit_move_insn (reg, gen_rtx_REG (Pmode, scratch_regno));
7802           seq = get_insns ();
7803           end_sequence ();
7804
7805           push_topmost_sequence ();
7806           emit_insn_after (seq, entry_of_function ());
7807           pop_topmost_sequence ();
7808         }
7809     }
7810
7811   /* Only 64bit target needs something special.  */
7812   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7813     {
7814       if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7815         std_expand_builtin_va_start (valist, nextarg);
7816       else
7817         {
7818           rtx va_r, next;
7819
7820           va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
7821           next = expand_binop (ptr_mode, add_optab,
7822                                cfun->machine->split_stack_varargs_pointer,
7823                                crtl->args.arg_offset_rtx,
7824                                NULL_RTX, 0, OPTAB_LIB_WIDEN);
7825           convert_move (va_r, next, 0);
7826         }
7827       return;
7828     }
7829
7830   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7831   f_fpr = DECL_CHAIN (f_gpr);
7832   f_ovf = DECL_CHAIN (f_fpr);
7833   f_sav = DECL_CHAIN (f_ovf);
7834
7835   valist = build_simple_mem_ref (valist);
7836   TREE_TYPE (valist) = TREE_TYPE (sysv_va_list_type_node);
7837   /* The following should be folded into the MEM_REF offset.  */
7838   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), unshare_expr (valist),
7839                 f_gpr, NULL_TREE);
7840   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
7841                 f_fpr, NULL_TREE);
7842   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
7843                 f_ovf, NULL_TREE);
7844   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
7845                 f_sav, NULL_TREE);
7846
7847   /* Count number of gp and fp argument registers used.  */
7848   words = crtl->args.info.words;
7849   n_gpr = crtl->args.info.regno;
7850   n_fpr = crtl->args.info.sse_regno;
7851
7852   if (cfun->va_list_gpr_size)
7853     {
7854       type = TREE_TYPE (gpr);
7855       t = build2 (MODIFY_EXPR, type,
7856                   gpr, build_int_cst (type, n_gpr * 8));
7857       TREE_SIDE_EFFECTS (t) = 1;
7858       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7859     }
7860
7861   if (TARGET_SSE && cfun->va_list_fpr_size)
7862     {
7863       type = TREE_TYPE (fpr);
7864       t = build2 (MODIFY_EXPR, type, fpr,
7865                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
7866       TREE_SIDE_EFFECTS (t) = 1;
7867       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7868     }
7869
7870   /* Find the overflow area.  */
7871   type = TREE_TYPE (ovf);
7872   if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7873     ovf_rtx = crtl->args.internal_arg_pointer;
7874   else
7875     ovf_rtx = cfun->machine->split_stack_varargs_pointer;
7876   t = make_tree (type, ovf_rtx);
7877   if (words != 0)
7878     t = build2 (POINTER_PLUS_EXPR, type, t,
7879                 size_int (words * UNITS_PER_WORD));
7880   t = build2 (MODIFY_EXPR, type, ovf, t);
7881   TREE_SIDE_EFFECTS (t) = 1;
7882   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7883
7884   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
7885     {
7886       /* Find the register save area.
7887          Prologue of the function save it right above stack frame.  */
7888       type = TREE_TYPE (sav);
7889       t = make_tree (type, frame_pointer_rtx);
7890       if (!ix86_varargs_gpr_size)
7891         t = build2 (POINTER_PLUS_EXPR, type, t,
7892                     size_int (-8 * X86_64_REGPARM_MAX));
7893       t = build2 (MODIFY_EXPR, type, sav, t);
7894       TREE_SIDE_EFFECTS (t) = 1;
7895       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7896     }
7897 }
7898
7899 /* Implement va_arg.  */
7900
7901 static tree
7902 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7903                       gimple_seq *post_p)
7904 {
7905   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
7906   tree f_gpr, f_fpr, f_ovf, f_sav;
7907   tree gpr, fpr, ovf, sav, t;
7908   int size, rsize;
7909   tree lab_false, lab_over = NULL_TREE;
7910   tree addr, t2;
7911   rtx container;
7912   int indirect_p = 0;
7913   tree ptrtype;
7914   enum machine_mode nat_mode;
7915   unsigned int arg_boundary;
7916
7917   /* Only 64bit target needs something special.  */
7918   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7919     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7920
7921   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7922   f_fpr = DECL_CHAIN (f_gpr);
7923   f_ovf = DECL_CHAIN (f_fpr);
7924   f_sav = DECL_CHAIN (f_ovf);
7925
7926   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
7927                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
7928   valist = build_va_arg_indirect_ref (valist);
7929   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7930   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7931   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7932
7933   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
7934   if (indirect_p)
7935     type = build_pointer_type (type);
7936   size = int_size_in_bytes (type);
7937   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7938
7939   nat_mode = type_natural_mode (type, NULL);
7940   switch (nat_mode)
7941     {
7942     case V8SFmode:
7943     case V8SImode:
7944     case V32QImode:
7945     case V16HImode:
7946     case V4DFmode:
7947     case V4DImode:
7948       /* Unnamed 256bit vector mode parameters are passed on stack.  */
7949       if (ix86_cfun_abi () == SYSV_ABI)
7950         {
7951           container = NULL;
7952           break;
7953         }
7954
7955     default:
7956       container = construct_container (nat_mode, TYPE_MODE (type),
7957                                        type, 0, X86_64_REGPARM_MAX,
7958                                        X86_64_SSE_REGPARM_MAX, intreg,
7959                                        0);
7960       break;
7961     }
7962
7963   /* Pull the value out of the saved registers.  */
7964
7965   addr = create_tmp_var (ptr_type_node, "addr");
7966
7967   if (container)
7968     {
7969       int needed_intregs, needed_sseregs;
7970       bool need_temp;
7971       tree int_addr, sse_addr;
7972
7973       lab_false = create_artificial_label (UNKNOWN_LOCATION);
7974       lab_over = create_artificial_label (UNKNOWN_LOCATION);
7975
7976       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
7977
7978       need_temp = (!REG_P (container)
7979                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
7980                        || TYPE_ALIGN (type) > 128));
7981
7982       /* In case we are passing structure, verify that it is consecutive block
7983          on the register save area.  If not we need to do moves.  */
7984       if (!need_temp && !REG_P (container))
7985         {
7986           /* Verify that all registers are strictly consecutive  */
7987           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
7988             {
7989               int i;
7990
7991               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7992                 {
7993                   rtx slot = XVECEXP (container, 0, i);
7994                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
7995                       || INTVAL (XEXP (slot, 1)) != i * 16)
7996                     need_temp = 1;
7997                 }
7998             }
7999           else
8000             {
8001               int i;
8002
8003               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
8004                 {
8005                   rtx slot = XVECEXP (container, 0, i);
8006                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
8007                       || INTVAL (XEXP (slot, 1)) != i * 8)
8008                     need_temp = 1;
8009                 }
8010             }
8011         }
8012       if (!need_temp)
8013         {
8014           int_addr = addr;
8015           sse_addr = addr;
8016         }
8017       else
8018         {
8019           int_addr = create_tmp_var (ptr_type_node, "int_addr");
8020           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
8021         }
8022
8023       /* First ensure that we fit completely in registers.  */
8024       if (needed_intregs)
8025         {
8026           t = build_int_cst (TREE_TYPE (gpr),
8027                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
8028           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
8029           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
8030           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
8031           gimplify_and_add (t, pre_p);
8032         }
8033       if (needed_sseregs)
8034         {
8035           t = build_int_cst (TREE_TYPE (fpr),
8036                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
8037                              + X86_64_REGPARM_MAX * 8);
8038           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
8039           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
8040           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
8041           gimplify_and_add (t, pre_p);
8042         }
8043
8044       /* Compute index to start of area used for integer regs.  */
8045       if (needed_intregs)
8046         {
8047           /* int_addr = gpr + sav; */
8048           t = fold_convert (sizetype, gpr);
8049           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
8050           gimplify_assign (int_addr, t, pre_p);
8051         }
8052       if (needed_sseregs)
8053         {
8054           /* sse_addr = fpr + sav; */
8055           t = fold_convert (sizetype, fpr);
8056           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
8057           gimplify_assign (sse_addr, t, pre_p);
8058         }
8059       if (need_temp)
8060         {
8061           int i, prev_size = 0;
8062           tree temp = create_tmp_var (type, "va_arg_tmp");
8063
8064           /* addr = &temp; */
8065           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
8066           gimplify_assign (addr, t, pre_p);
8067
8068           for (i = 0; i < XVECLEN (container, 0); i++)
8069             {
8070               rtx slot = XVECEXP (container, 0, i);
8071               rtx reg = XEXP (slot, 0);
8072               enum machine_mode mode = GET_MODE (reg);
8073               tree piece_type;
8074               tree addr_type;
8075               tree daddr_type;
8076               tree src_addr, src;
8077               int src_offset;
8078               tree dest_addr, dest;
8079               int cur_size = GET_MODE_SIZE (mode);
8080
8081               gcc_assert (prev_size <= INTVAL (XEXP (slot, 1)));
8082               prev_size = INTVAL (XEXP (slot, 1));
8083               if (prev_size + cur_size > size)
8084                 {
8085                   cur_size = size - prev_size;
8086                   mode = mode_for_size (cur_size * BITS_PER_UNIT, MODE_INT, 1);
8087                   if (mode == BLKmode)
8088                     mode = QImode;
8089                 }
8090               piece_type = lang_hooks.types.type_for_mode (mode, 1);
8091               if (mode == GET_MODE (reg))
8092                 addr_type = build_pointer_type (piece_type);
8093               else
8094                 addr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8095                                                          true);
8096               daddr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
8097                                                         true);
8098
8099               if (SSE_REGNO_P (REGNO (reg)))
8100                 {
8101                   src_addr = sse_addr;
8102                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
8103                 }
8104               else
8105                 {
8106                   src_addr = int_addr;
8107                   src_offset = REGNO (reg) * 8;
8108                 }
8109               src_addr = fold_convert (addr_type, src_addr);
8110               src_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, src_addr,
8111                                       size_int (src_offset));
8112
8113               dest_addr = fold_convert (daddr_type, addr);
8114               dest_addr = fold_build2 (POINTER_PLUS_EXPR, daddr_type, dest_addr,
8115                                        size_int (prev_size));
8116               if (cur_size == GET_MODE_SIZE (mode))
8117                 {
8118                   src = build_va_arg_indirect_ref (src_addr);
8119                   dest = build_va_arg_indirect_ref (dest_addr);
8120
8121                   gimplify_assign (dest, src, pre_p);
8122                 }
8123               else
8124                 {
8125                   tree copy
8126                     = build_call_expr (implicit_built_in_decls[BUILT_IN_MEMCPY],
8127                                        3, dest_addr, src_addr,
8128                                        size_int (cur_size));
8129                   gimplify_and_add (copy, pre_p);
8130                 }
8131               prev_size += cur_size;
8132             }
8133         }
8134
8135       if (needed_intregs)
8136         {
8137           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
8138                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
8139           gimplify_assign (gpr, t, pre_p);
8140         }
8141
8142       if (needed_sseregs)
8143         {
8144           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
8145                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
8146           gimplify_assign (fpr, t, pre_p);
8147         }
8148
8149       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
8150
8151       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
8152     }
8153
8154   /* ... otherwise out of the overflow area.  */
8155
8156   /* When we align parameter on stack for caller, if the parameter
8157      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
8158      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
8159      here with caller.  */
8160   arg_boundary = FUNCTION_ARG_BOUNDARY (VOIDmode, type);
8161   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
8162     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
8163
8164   /* Care for on-stack alignment if needed.  */
8165   if (arg_boundary <= 64 || size == 0)
8166     t = ovf;
8167  else
8168     {
8169       HOST_WIDE_INT align = arg_boundary / 8;
8170       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), ovf,
8171                   size_int (align - 1));
8172       t = fold_convert (sizetype, t);
8173       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
8174                   size_int (-align));
8175       t = fold_convert (TREE_TYPE (ovf), t);
8176     }
8177
8178   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
8179   gimplify_assign (addr, t, pre_p);
8180
8181   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
8182               size_int (rsize * UNITS_PER_WORD));
8183   gimplify_assign (unshare_expr (ovf), t, pre_p);
8184
8185   if (container)
8186     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
8187
8188   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
8189   addr = fold_convert (ptrtype, addr);
8190
8191   if (indirect_p)
8192     addr = build_va_arg_indirect_ref (addr);
8193   return build_va_arg_indirect_ref (addr);
8194 }
8195 \f
8196 /* Return true if OPNUM's MEM should be matched
8197    in movabs* patterns.  */
8198
8199 bool
8200 ix86_check_movabs (rtx insn, int opnum)
8201 {
8202   rtx set, mem;
8203
8204   set = PATTERN (insn);
8205   if (GET_CODE (set) == PARALLEL)
8206     set = XVECEXP (set, 0, 0);
8207   gcc_assert (GET_CODE (set) == SET);
8208   mem = XEXP (set, opnum);
8209   while (GET_CODE (mem) == SUBREG)
8210     mem = SUBREG_REG (mem);
8211   gcc_assert (MEM_P (mem));
8212   return volatile_ok || !MEM_VOLATILE_P (mem);
8213 }
8214 \f
8215 /* Initialize the table of extra 80387 mathematical constants.  */
8216
8217 static void
8218 init_ext_80387_constants (void)
8219 {
8220   static const char * cst[5] =
8221   {
8222     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
8223     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
8224     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
8225     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
8226     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
8227   };
8228   int i;
8229
8230   for (i = 0; i < 5; i++)
8231     {
8232       real_from_string (&ext_80387_constants_table[i], cst[i]);
8233       /* Ensure each constant is rounded to XFmode precision.  */
8234       real_convert (&ext_80387_constants_table[i],
8235                     XFmode, &ext_80387_constants_table[i]);
8236     }
8237
8238   ext_80387_constants_init = 1;
8239 }
8240
8241 /* Return non-zero if the constant is something that
8242    can be loaded with a special instruction.  */
8243
8244 int
8245 standard_80387_constant_p (rtx x)
8246 {
8247   enum machine_mode mode = GET_MODE (x);
8248
8249   REAL_VALUE_TYPE r;
8250
8251   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
8252     return -1;
8253
8254   if (x == CONST0_RTX (mode))
8255     return 1;
8256   if (x == CONST1_RTX (mode))
8257     return 2;
8258
8259   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8260
8261   /* For XFmode constants, try to find a special 80387 instruction when
8262      optimizing for size or on those CPUs that benefit from them.  */
8263   if (mode == XFmode
8264       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
8265     {
8266       int i;
8267
8268       if (! ext_80387_constants_init)
8269         init_ext_80387_constants ();
8270
8271       for (i = 0; i < 5; i++)
8272         if (real_identical (&r, &ext_80387_constants_table[i]))
8273           return i + 3;
8274     }
8275
8276   /* Load of the constant -0.0 or -1.0 will be split as
8277      fldz;fchs or fld1;fchs sequence.  */
8278   if (real_isnegzero (&r))
8279     return 8;
8280   if (real_identical (&r, &dconstm1))
8281     return 9;
8282
8283   return 0;
8284 }
8285
8286 /* Return the opcode of the special instruction to be used to load
8287    the constant X.  */
8288
8289 const char *
8290 standard_80387_constant_opcode (rtx x)
8291 {
8292   switch (standard_80387_constant_p (x))
8293     {
8294     case 1:
8295       return "fldz";
8296     case 2:
8297       return "fld1";
8298     case 3:
8299       return "fldlg2";
8300     case 4:
8301       return "fldln2";
8302     case 5:
8303       return "fldl2e";
8304     case 6:
8305       return "fldl2t";
8306     case 7:
8307       return "fldpi";
8308     case 8:
8309     case 9:
8310       return "#";
8311     default:
8312       gcc_unreachable ();
8313     }
8314 }
8315
8316 /* Return the CONST_DOUBLE representing the 80387 constant that is
8317    loaded by the specified special instruction.  The argument IDX
8318    matches the return value from standard_80387_constant_p.  */
8319
8320 rtx
8321 standard_80387_constant_rtx (int idx)
8322 {
8323   int i;
8324
8325   if (! ext_80387_constants_init)
8326     init_ext_80387_constants ();
8327
8328   switch (idx)
8329     {
8330     case 3:
8331     case 4:
8332     case 5:
8333     case 6:
8334     case 7:
8335       i = idx - 3;
8336       break;
8337
8338     default:
8339       gcc_unreachable ();
8340     }
8341
8342   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
8343                                        XFmode);
8344 }
8345
8346 /* Return 1 if X is all 0s and 2 if x is all 1s
8347    in supported SSE vector mode.  */
8348
8349 int
8350 standard_sse_constant_p (rtx x)
8351 {
8352   enum machine_mode mode = GET_MODE (x);
8353
8354   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
8355     return 1;
8356   if (vector_all_ones_operand (x, mode))
8357     switch (mode)
8358       {
8359       case V16QImode:
8360       case V8HImode:
8361       case V4SImode:
8362       case V2DImode:
8363         if (TARGET_SSE2)
8364           return 2;
8365       default:
8366         break;
8367       }
8368
8369   return 0;
8370 }
8371
8372 /* Return the opcode of the special instruction to be used to load
8373    the constant X.  */
8374
8375 const char *
8376 standard_sse_constant_opcode (rtx insn, rtx x)
8377 {
8378   switch (standard_sse_constant_p (x))
8379     {
8380     case 1:
8381       switch (get_attr_mode (insn))
8382         {
8383         case MODE_V4SF:
8384           return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
8385         case MODE_V2DF:
8386           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8387             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
8388           else
8389             return TARGET_AVX ? "vxorpd\t%0, %0, %0" : "xorpd\t%0, %0";
8390         case MODE_TI:
8391           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8392             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
8393           else
8394             return TARGET_AVX ? "vpxor\t%0, %0, %0" : "pxor\t%0, %0";
8395         case MODE_V8SF:
8396           return "vxorps\t%x0, %x0, %x0";
8397         case MODE_V4DF:
8398           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8399             return "vxorps\t%x0, %x0, %x0";
8400           else
8401             return "vxorpd\t%x0, %x0, %x0";
8402         case MODE_OI:
8403           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
8404             return "vxorps\t%x0, %x0, %x0";
8405           else
8406             return "vpxor\t%x0, %x0, %x0";
8407         default:
8408           break;
8409         }
8410     case 2:
8411       return TARGET_AVX ? "vpcmpeqd\t%0, %0, %0" : "pcmpeqd\t%0, %0";
8412     default:
8413       break;
8414     }
8415   gcc_unreachable ();
8416 }
8417
8418 /* Returns true if OP contains a symbol reference */
8419
8420 bool
8421 symbolic_reference_mentioned_p (rtx op)
8422 {
8423   const char *fmt;
8424   int i;
8425
8426   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
8427     return true;
8428
8429   fmt = GET_RTX_FORMAT (GET_CODE (op));
8430   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
8431     {
8432       if (fmt[i] == 'E')
8433         {
8434           int j;
8435
8436           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
8437             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
8438               return true;
8439         }
8440
8441       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
8442         return true;
8443     }
8444
8445   return false;
8446 }
8447
8448 /* Return true if it is appropriate to emit `ret' instructions in the
8449    body of a function.  Do this only if the epilogue is simple, needing a
8450    couple of insns.  Prior to reloading, we can't tell how many registers
8451    must be saved, so return false then.  Return false if there is no frame
8452    marker to de-allocate.  */
8453
8454 bool
8455 ix86_can_use_return_insn_p (void)
8456 {
8457   struct ix86_frame frame;
8458
8459   if (! reload_completed || frame_pointer_needed)
8460     return 0;
8461
8462   /* Don't allow more than 32k pop, since that's all we can do
8463      with one instruction.  */
8464   if (crtl->args.pops_args && crtl->args.size >= 32768)
8465     return 0;
8466
8467   ix86_compute_frame_layout (&frame);
8468   return (frame.stack_pointer_offset == UNITS_PER_WORD
8469           && (frame.nregs + frame.nsseregs) == 0);
8470 }
8471 \f
8472 /* Value should be nonzero if functions must have frame pointers.
8473    Zero means the frame pointer need not be set up (and parms may
8474    be accessed via the stack pointer) in functions that seem suitable.  */
8475
8476 static bool
8477 ix86_frame_pointer_required (void)
8478 {
8479   /* If we accessed previous frames, then the generated code expects
8480      to be able to access the saved ebp value in our frame.  */
8481   if (cfun->machine->accesses_prev_frame)
8482     return true;
8483
8484   /* Several x86 os'es need a frame pointer for other reasons,
8485      usually pertaining to setjmp.  */
8486   if (SUBTARGET_FRAME_POINTER_REQUIRED)
8487     return true;
8488
8489   /* In ix86_option_override_internal, TARGET_OMIT_LEAF_FRAME_POINTER
8490      turns off the frame pointer by default.  Turn it back on now if
8491      we've not got a leaf function.  */
8492   if (TARGET_OMIT_LEAF_FRAME_POINTER
8493       && (!current_function_is_leaf
8494           || ix86_current_function_calls_tls_descriptor))
8495     return true;
8496
8497   if (crtl->profile && !flag_fentry)
8498     return true;
8499
8500   return false;
8501 }
8502
8503 /* Record that the current function accesses previous call frames.  */
8504
8505 void
8506 ix86_setup_frame_addresses (void)
8507 {
8508   cfun->machine->accesses_prev_frame = 1;
8509 }
8510 \f
8511 #ifndef USE_HIDDEN_LINKONCE
8512 # if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
8513 #  define USE_HIDDEN_LINKONCE 1
8514 # else
8515 #  define USE_HIDDEN_LINKONCE 0
8516 # endif
8517 #endif
8518
8519 static int pic_labels_used;
8520
8521 /* Fills in the label name that should be used for a pc thunk for
8522    the given register.  */
8523
8524 static void
8525 get_pc_thunk_name (char name[32], unsigned int regno)
8526 {
8527   gcc_assert (!TARGET_64BIT);
8528
8529   if (USE_HIDDEN_LINKONCE)
8530     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
8531   else
8532     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
8533 }
8534
8535
8536 /* This function generates code for -fpic that loads %ebx with
8537    the return address of the caller and then returns.  */
8538
8539 static void
8540 ix86_code_end (void)
8541 {
8542   rtx xops[2];
8543   int regno;
8544
8545   for (regno = AX_REG; regno <= SP_REG; regno++)
8546     {
8547       char name[32];
8548       tree decl;
8549
8550       if (!(pic_labels_used & (1 << regno)))
8551         continue;
8552
8553       get_pc_thunk_name (name, regno);
8554
8555       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
8556                          get_identifier (name),
8557                          build_function_type (void_type_node, void_list_node));
8558       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
8559                                        NULL_TREE, void_type_node);
8560       TREE_PUBLIC (decl) = 1;
8561       TREE_STATIC (decl) = 1;
8562
8563 #if TARGET_MACHO
8564       if (TARGET_MACHO)
8565         {
8566           switch_to_section (darwin_sections[text_coal_section]);
8567           fputs ("\t.weak_definition\t", asm_out_file);
8568           assemble_name (asm_out_file, name);
8569           fputs ("\n\t.private_extern\t", asm_out_file);
8570           assemble_name (asm_out_file, name);
8571           putc ('\n', asm_out_file);
8572           ASM_OUTPUT_LABEL (asm_out_file, name);
8573           DECL_WEAK (decl) = 1;
8574         }
8575       else
8576 #endif
8577       if (USE_HIDDEN_LINKONCE)
8578         {
8579           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
8580
8581           targetm.asm_out.unique_section (decl, 0);
8582           switch_to_section (get_named_section (decl, NULL, 0));
8583
8584           targetm.asm_out.globalize_label (asm_out_file, name);
8585           fputs ("\t.hidden\t", asm_out_file);
8586           assemble_name (asm_out_file, name);
8587           putc ('\n', asm_out_file);
8588           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
8589         }
8590       else
8591         {
8592           switch_to_section (text_section);
8593           ASM_OUTPUT_LABEL (asm_out_file, name);
8594         }
8595
8596       DECL_INITIAL (decl) = make_node (BLOCK);
8597       current_function_decl = decl;
8598       init_function_start (decl);
8599       first_function_block_is_cold = false;
8600       /* Make sure unwind info is emitted for the thunk if needed.  */
8601       final_start_function (emit_barrier (), asm_out_file, 1);
8602
8603       /* Pad stack IP move with 4 instructions (two NOPs count
8604          as one instruction).  */
8605       if (TARGET_PAD_SHORT_FUNCTION)
8606         {
8607           int i = 8;
8608
8609           while (i--)
8610             fputs ("\tnop\n", asm_out_file);
8611         }
8612
8613       xops[0] = gen_rtx_REG (Pmode, regno);
8614       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8615       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
8616       fputs ("\tret\n", asm_out_file);
8617       final_end_function ();
8618       init_insn_lengths ();
8619       free_after_compilation (cfun);
8620       set_cfun (NULL);
8621       current_function_decl = NULL;
8622     }
8623
8624   if (flag_split_stack)
8625     file_end_indicate_split_stack ();
8626 }
8627
8628 /* Emit code for the SET_GOT patterns.  */
8629
8630 const char *
8631 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
8632 {
8633   rtx xops[3];
8634
8635   xops[0] = dest;
8636
8637   if (TARGET_VXWORKS_RTP && flag_pic)
8638     {
8639       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
8640       xops[2] = gen_rtx_MEM (Pmode,
8641                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
8642       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
8643
8644       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
8645          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
8646          an unadorned address.  */
8647       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
8648       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
8649       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
8650       return "";
8651     }
8652
8653   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
8654
8655   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
8656     {
8657       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
8658
8659       if (!flag_pic)
8660         output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
8661       else
8662         {
8663           output_asm_insn ("call\t%a2", xops);
8664 #ifdef DWARF2_UNWIND_INFO
8665           /* The call to next label acts as a push.  */
8666           if (dwarf2out_do_frame ())
8667             {
8668               rtx insn;
8669               start_sequence ();
8670               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8671                                              gen_rtx_PLUS (Pmode,
8672                                                            stack_pointer_rtx,
8673                                                            GEN_INT (-4))));
8674               RTX_FRAME_RELATED_P (insn) = 1;
8675               dwarf2out_frame_debug (insn, true);
8676               end_sequence ();
8677             }
8678 #endif
8679         }
8680
8681 #if TARGET_MACHO
8682       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8683          is what will be referenced by the Mach-O PIC subsystem.  */
8684       if (!label)
8685         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8686 #endif
8687
8688       targetm.asm_out.internal_label (asm_out_file, "L",
8689                                       CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
8690
8691       if (flag_pic)
8692         {
8693           output_asm_insn ("pop%z0\t%0", xops);
8694 #ifdef DWARF2_UNWIND_INFO
8695           /* The pop is a pop and clobbers dest, but doesn't restore it
8696              for unwind info purposes.  */
8697           if (dwarf2out_do_frame ())
8698             {
8699               rtx insn;
8700               start_sequence ();
8701               insn = emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
8702               dwarf2out_frame_debug (insn, true);
8703               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8704                                              gen_rtx_PLUS (Pmode,
8705                                                            stack_pointer_rtx,
8706                                                            GEN_INT (4))));
8707               RTX_FRAME_RELATED_P (insn) = 1;
8708               dwarf2out_frame_debug (insn, true);
8709               end_sequence ();
8710             }
8711 #endif
8712         }
8713     }
8714   else
8715     {
8716       char name[32];
8717       get_pc_thunk_name (name, REGNO (dest));
8718       pic_labels_used |= 1 << REGNO (dest);
8719
8720 #ifdef DWARF2_UNWIND_INFO
8721       /* Ensure all queued register saves are flushed before the
8722          call.  */
8723       if (dwarf2out_do_frame ())
8724         dwarf2out_flush_queued_reg_saves ();
8725 #endif
8726       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
8727       xops[2] = gen_rtx_MEM (QImode, xops[2]);
8728       output_asm_insn ("call\t%X2", xops);
8729       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8730          is what will be referenced by the Mach-O PIC subsystem.  */
8731 #if TARGET_MACHO
8732       if (!label)
8733         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8734       else
8735         targetm.asm_out.internal_label (asm_out_file, "L",
8736                                            CODE_LABEL_NUMBER (label));
8737 #endif
8738     }
8739
8740   if (TARGET_MACHO)
8741     return "";
8742
8743   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
8744     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
8745   else
8746     output_asm_insn ("add%z0\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
8747
8748   return "";
8749 }
8750
8751 /* Generate an "push" pattern for input ARG.  */
8752
8753 static rtx
8754 gen_push (rtx arg)
8755 {
8756   struct machine_function *m = cfun->machine;
8757
8758   if (m->fs.cfa_reg == stack_pointer_rtx)
8759     m->fs.cfa_offset += UNITS_PER_WORD;
8760   m->fs.sp_offset += UNITS_PER_WORD;
8761
8762   return gen_rtx_SET (VOIDmode,
8763                       gen_rtx_MEM (Pmode,
8764                                    gen_rtx_PRE_DEC (Pmode,
8765                                                     stack_pointer_rtx)),
8766                       arg);
8767 }
8768
8769 /* Generate an "pop" pattern for input ARG.  */
8770
8771 static rtx
8772 gen_pop (rtx arg)
8773 {
8774   return gen_rtx_SET (VOIDmode,
8775                       arg,
8776                       gen_rtx_MEM (Pmode,
8777                                    gen_rtx_POST_INC (Pmode,
8778                                                      stack_pointer_rtx)));
8779 }
8780
8781 /* Return >= 0 if there is an unused call-clobbered register available
8782    for the entire function.  */
8783
8784 static unsigned int
8785 ix86_select_alt_pic_regnum (void)
8786 {
8787   if (current_function_is_leaf
8788       && !crtl->profile
8789       && !ix86_current_function_calls_tls_descriptor)
8790     {
8791       int i, drap;
8792       /* Can't use the same register for both PIC and DRAP.  */
8793       if (crtl->drap_reg)
8794         drap = REGNO (crtl->drap_reg);
8795       else
8796         drap = -1;
8797       for (i = 2; i >= 0; --i)
8798         if (i != drap && !df_regs_ever_live_p (i))
8799           return i;
8800     }
8801
8802   return INVALID_REGNUM;
8803 }
8804
8805 /* Return 1 if we need to save REGNO.  */
8806 static int
8807 ix86_save_reg (unsigned int regno, int maybe_eh_return)
8808 {
8809   if (pic_offset_table_rtx
8810       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
8811       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8812           || crtl->profile
8813           || crtl->calls_eh_return
8814           || crtl->uses_const_pool))
8815     {
8816       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
8817         return 0;
8818       return 1;
8819     }
8820
8821   if (crtl->calls_eh_return && maybe_eh_return)
8822     {
8823       unsigned i;
8824       for (i = 0; ; i++)
8825         {
8826           unsigned test = EH_RETURN_DATA_REGNO (i);
8827           if (test == INVALID_REGNUM)
8828             break;
8829           if (test == regno)
8830             return 1;
8831         }
8832     }
8833
8834   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
8835     return 1;
8836
8837   return (df_regs_ever_live_p (regno)
8838           && !call_used_regs[regno]
8839           && !fixed_regs[regno]
8840           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
8841 }
8842
8843 /* Return number of saved general prupose registers.  */
8844
8845 static int
8846 ix86_nsaved_regs (void)
8847 {
8848   int nregs = 0;
8849   int regno;
8850
8851   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8852     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8853       nregs ++;
8854   return nregs;
8855 }
8856
8857 /* Return number of saved SSE registrers.  */
8858
8859 static int
8860 ix86_nsaved_sseregs (void)
8861 {
8862   int nregs = 0;
8863   int regno;
8864
8865   if (ix86_cfun_abi () != MS_ABI)
8866     return 0;
8867   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8868     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8869       nregs ++;
8870   return nregs;
8871 }
8872
8873 /* Given FROM and TO register numbers, say whether this elimination is
8874    allowed.  If stack alignment is needed, we can only replace argument
8875    pointer with hard frame pointer, or replace frame pointer with stack
8876    pointer.  Otherwise, frame pointer elimination is automatically
8877    handled and all other eliminations are valid.  */
8878
8879 static bool
8880 ix86_can_eliminate (const int from, const int to)
8881 {
8882   if (stack_realign_fp)
8883     return ((from == ARG_POINTER_REGNUM
8884              && to == HARD_FRAME_POINTER_REGNUM)
8885             || (from == FRAME_POINTER_REGNUM
8886                 && to == STACK_POINTER_REGNUM));
8887   else
8888     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
8889 }
8890
8891 /* Return the offset between two registers, one to be eliminated, and the other
8892    its replacement, at the start of a routine.  */
8893
8894 HOST_WIDE_INT
8895 ix86_initial_elimination_offset (int from, int to)
8896 {
8897   struct ix86_frame frame;
8898   ix86_compute_frame_layout (&frame);
8899
8900   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
8901     return frame.hard_frame_pointer_offset;
8902   else if (from == FRAME_POINTER_REGNUM
8903            && to == HARD_FRAME_POINTER_REGNUM)
8904     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
8905   else
8906     {
8907       gcc_assert (to == STACK_POINTER_REGNUM);
8908
8909       if (from == ARG_POINTER_REGNUM)
8910         return frame.stack_pointer_offset;
8911
8912       gcc_assert (from == FRAME_POINTER_REGNUM);
8913       return frame.stack_pointer_offset - frame.frame_pointer_offset;
8914     }
8915 }
8916
8917 /* In a dynamically-aligned function, we can't know the offset from
8918    stack pointer to frame pointer, so we must ensure that setjmp
8919    eliminates fp against the hard fp (%ebp) rather than trying to
8920    index from %esp up to the top of the frame across a gap that is
8921    of unknown (at compile-time) size.  */
8922 static rtx
8923 ix86_builtin_setjmp_frame_value (void)
8924 {
8925   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
8926 }
8927
8928 /* On the x86 -fsplit-stack and -fstack-protector both use the same
8929    field in the TCB, so they can not be used together.  */
8930
8931 static bool
8932 ix86_supports_split_stack (bool report ATTRIBUTE_UNUSED)
8933 {
8934   bool ret = true;
8935
8936 #ifndef TARGET_THREAD_SPLIT_STACK_OFFSET
8937   if (report)
8938     error ("%<-fsplit-stack%> currently only supported on GNU/Linux");
8939   ret = false;
8940 #else
8941   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
8942     {
8943       if (report)
8944         error ("%<-fsplit-stack%> requires "
8945                "assembler support for CFI directives");
8946       ret = false;
8947     }
8948 #endif
8949
8950   return ret;
8951 }
8952
8953 /* When using -fsplit-stack, the allocation routines set a field in
8954    the TCB to the bottom of the stack plus this much space, measured
8955    in bytes.  */
8956
8957 #define SPLIT_STACK_AVAILABLE 256
8958
8959 /* Fill structure ix86_frame about frame of currently computed function.  */
8960
8961 static void
8962 ix86_compute_frame_layout (struct ix86_frame *frame)
8963 {
8964   unsigned int stack_alignment_needed;
8965   HOST_WIDE_INT offset;
8966   unsigned int preferred_alignment;
8967   HOST_WIDE_INT size = get_frame_size ();
8968   HOST_WIDE_INT to_allocate;
8969
8970   frame->nregs = ix86_nsaved_regs ();
8971   frame->nsseregs = ix86_nsaved_sseregs ();
8972
8973   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
8974   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
8975
8976   /* MS ABI seem to require stack alignment to be always 16 except for function
8977      prologues and leaf.  */
8978   if ((ix86_cfun_abi () == MS_ABI && preferred_alignment < 16)
8979       && (!current_function_is_leaf || cfun->calls_alloca != 0
8980           || ix86_current_function_calls_tls_descriptor))
8981     {
8982       preferred_alignment = 16;
8983       stack_alignment_needed = 16;
8984       crtl->preferred_stack_boundary = 128;
8985       crtl->stack_alignment_needed = 128;
8986     }
8987
8988   gcc_assert (!size || stack_alignment_needed);
8989   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
8990   gcc_assert (preferred_alignment <= stack_alignment_needed);
8991
8992   /* For SEH we have to limit the amount of code movement into the prologue.
8993      At present we do this via a BLOCKAGE, at which point there's very little
8994      scheduling that can be done, which means that there's very little point
8995      in doing anything except PUSHs.  */
8996   if (TARGET_SEH)
8997     cfun->machine->use_fast_prologue_epilogue = false;
8998
8999   /* During reload iteration the amount of registers saved can change.
9000      Recompute the value as needed.  Do not recompute when amount of registers
9001      didn't change as reload does multiple calls to the function and does not
9002      expect the decision to change within single iteration.  */
9003   else if (!optimize_function_for_size_p (cfun)
9004            && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
9005     {
9006       int count = frame->nregs;
9007       struct cgraph_node *node = cgraph_node (current_function_decl);
9008
9009       cfun->machine->use_fast_prologue_epilogue_nregs = count;
9010
9011       /* The fast prologue uses move instead of push to save registers.  This
9012          is significantly longer, but also executes faster as modern hardware
9013          can execute the moves in parallel, but can't do that for push/pop.
9014
9015          Be careful about choosing what prologue to emit:  When function takes
9016          many instructions to execute we may use slow version as well as in
9017          case function is known to be outside hot spot (this is known with
9018          feedback only).  Weight the size of function by number of registers
9019          to save as it is cheap to use one or two push instructions but very
9020          slow to use many of them.  */
9021       if (count)
9022         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
9023       if (node->frequency < NODE_FREQUENCY_NORMAL
9024           || (flag_branch_probabilities
9025               && node->frequency < NODE_FREQUENCY_HOT))
9026         cfun->machine->use_fast_prologue_epilogue = false;
9027       else
9028         cfun->machine->use_fast_prologue_epilogue
9029            = !expensive_function_p (count);
9030     }
9031   if (TARGET_PROLOGUE_USING_MOVE
9032       && cfun->machine->use_fast_prologue_epilogue)
9033     frame->save_regs_using_mov = true;
9034   else
9035     frame->save_regs_using_mov = false;
9036
9037   /* If static stack checking is enabled and done with probes, the registers
9038      need to be saved before allocating the frame.  */
9039   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
9040     frame->save_regs_using_mov = false;
9041
9042   /* Skip return address.  */
9043   offset = UNITS_PER_WORD;
9044
9045   /* Skip pushed static chain.  */
9046   if (ix86_static_chain_on_stack)
9047     offset += UNITS_PER_WORD;
9048
9049   /* Skip saved base pointer.  */
9050   if (frame_pointer_needed)
9051     offset += UNITS_PER_WORD;
9052   frame->hfp_save_offset = offset;
9053
9054   /* The traditional frame pointer location is at the top of the frame.  */
9055   frame->hard_frame_pointer_offset = offset;
9056
9057   /* Register save area */
9058   offset += frame->nregs * UNITS_PER_WORD;
9059   frame->reg_save_offset = offset;
9060
9061   /* Align and set SSE register save area.  */
9062   if (frame->nsseregs)
9063     {
9064       /* The only ABI that has saved SSE registers (Win64) also has a
9065          16-byte aligned default stack, and thus we don't need to be
9066          within the re-aligned local stack frame to save them.  */
9067       gcc_assert (INCOMING_STACK_BOUNDARY >= 128);
9068       offset = (offset + 16 - 1) & -16;
9069       offset += frame->nsseregs * 16;
9070     }
9071   frame->sse_reg_save_offset = offset;
9072
9073   /* The re-aligned stack starts here.  Values before this point are not
9074      directly comparable with values below this point.  In order to make
9075      sure that no value happens to be the same before and after, force
9076      the alignment computation below to add a non-zero value.  */
9077   if (stack_realign_fp)
9078     offset = (offset + stack_alignment_needed) & -stack_alignment_needed;
9079
9080   /* Va-arg area */
9081   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
9082   offset += frame->va_arg_size;
9083
9084   /* Align start of frame for local function.  */
9085   offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed;
9086
9087   /* Frame pointer points here.  */
9088   frame->frame_pointer_offset = offset;
9089
9090   offset += size;
9091
9092   /* Add outgoing arguments area.  Can be skipped if we eliminated
9093      all the function calls as dead code.
9094      Skipping is however impossible when function calls alloca.  Alloca
9095      expander assumes that last crtl->outgoing_args_size
9096      of stack frame are unused.  */
9097   if (ACCUMULATE_OUTGOING_ARGS
9098       && (!current_function_is_leaf || cfun->calls_alloca
9099           || ix86_current_function_calls_tls_descriptor))
9100     {
9101       offset += crtl->outgoing_args_size;
9102       frame->outgoing_arguments_size = crtl->outgoing_args_size;
9103     }
9104   else
9105     frame->outgoing_arguments_size = 0;
9106
9107   /* Align stack boundary.  Only needed if we're calling another function
9108      or using alloca.  */
9109   if (!current_function_is_leaf || cfun->calls_alloca
9110       || ix86_current_function_calls_tls_descriptor)
9111     offset = (offset + preferred_alignment - 1) & -preferred_alignment;
9112
9113   /* We've reached end of stack frame.  */
9114   frame->stack_pointer_offset = offset;
9115
9116   /* Size prologue needs to allocate.  */
9117   to_allocate = offset - frame->sse_reg_save_offset;
9118
9119   if ((!to_allocate && frame->nregs <= 1)
9120       || (TARGET_64BIT && to_allocate >= (HOST_WIDE_INT) 0x80000000))
9121     frame->save_regs_using_mov = false;
9122
9123   if (ix86_using_red_zone ()
9124       && current_function_sp_is_unchanging
9125       && current_function_is_leaf
9126       && !ix86_current_function_calls_tls_descriptor)
9127     {
9128       frame->red_zone_size = to_allocate;
9129       if (frame->save_regs_using_mov)
9130         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
9131       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
9132         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
9133     }
9134   else
9135     frame->red_zone_size = 0;
9136   frame->stack_pointer_offset -= frame->red_zone_size;
9137
9138   /* The SEH frame pointer location is near the bottom of the frame.
9139      This is enforced by the fact that the difference between the
9140      stack pointer and the frame pointer is limited to 240 bytes in
9141      the unwind data structure.  */
9142   if (TARGET_SEH)
9143     {
9144       HOST_WIDE_INT diff;
9145
9146       /* If we can leave the frame pointer where it is, do so.  */
9147       diff = frame->stack_pointer_offset - frame->hard_frame_pointer_offset;
9148       if (diff > 240 || (diff & 15) != 0)
9149         {
9150           /* Ideally we'd determine what portion of the local stack frame
9151              (within the constraint of the lowest 240) is most heavily used.
9152              But without that complication, simply bias the frame pointer
9153              by 128 bytes so as to maximize the amount of the local stack
9154              frame that is addressable with 8-bit offsets.  */
9155           frame->hard_frame_pointer_offset = frame->stack_pointer_offset - 128;
9156         }
9157     }
9158 }
9159
9160 /* This is semi-inlined memory_address_length, but simplified
9161    since we know that we're always dealing with reg+offset, and
9162    to avoid having to create and discard all that rtl.  */
9163
9164 static inline int
9165 choose_baseaddr_len (unsigned int regno, HOST_WIDE_INT offset)
9166 {
9167   int len = 4;
9168
9169   if (offset == 0)
9170     {
9171       /* EBP and R13 cannot be encoded without an offset.  */
9172       len = (regno == BP_REG || regno == R13_REG);
9173     }
9174   else if (IN_RANGE (offset, -128, 127))
9175     len = 1;
9176
9177   /* ESP and R12 must be encoded with a SIB byte.  */
9178   if (regno == SP_REG || regno == R12_REG)
9179     len++;
9180
9181   return len;
9182 }
9183   
9184 /* Return an RTX that points to CFA_OFFSET within the stack frame.
9185    The valid base registers are taken from CFUN->MACHINE->FS.  */
9186
9187 static rtx
9188 choose_baseaddr (HOST_WIDE_INT cfa_offset)
9189 {
9190   const struct machine_function *m = cfun->machine;
9191   rtx base_reg = NULL;
9192   HOST_WIDE_INT base_offset = 0;
9193
9194   if (m->use_fast_prologue_epilogue)
9195     {
9196       /* Choose the base register most likely to allow the most scheduling
9197          opportunities.  Generally FP is valid througout the function,
9198          while DRAP must be reloaded within the epilogue.  But choose either
9199          over the SP due to increased encoding size.  */
9200
9201       if (m->fs.fp_valid)
9202         {
9203           base_reg = hard_frame_pointer_rtx;
9204           base_offset = m->fs.fp_offset - cfa_offset;
9205         }
9206       else if (m->fs.drap_valid)
9207         {
9208           base_reg = crtl->drap_reg;
9209           base_offset = 0 - cfa_offset;
9210         }
9211       else if (m->fs.sp_valid)
9212         {
9213           base_reg = stack_pointer_rtx;
9214           base_offset = m->fs.sp_offset - cfa_offset;
9215         }
9216     }
9217   else
9218     {
9219       HOST_WIDE_INT toffset;
9220       int len = 16, tlen;
9221
9222       /* Choose the base register with the smallest address encoding.
9223          With a tie, choose FP > DRAP > SP.  */
9224       if (m->fs.sp_valid)
9225         {
9226           base_reg = stack_pointer_rtx;
9227           base_offset = m->fs.sp_offset - cfa_offset;
9228           len = choose_baseaddr_len (STACK_POINTER_REGNUM, base_offset);
9229         }
9230       if (m->fs.drap_valid)
9231         {
9232           toffset = 0 - cfa_offset;
9233           tlen = choose_baseaddr_len (REGNO (crtl->drap_reg), toffset);
9234           if (tlen <= len)
9235             {
9236               base_reg = crtl->drap_reg;
9237               base_offset = toffset;
9238               len = tlen;
9239             }
9240         }
9241       if (m->fs.fp_valid)
9242         {
9243           toffset = m->fs.fp_offset - cfa_offset;
9244           tlen = choose_baseaddr_len (HARD_FRAME_POINTER_REGNUM, toffset);
9245           if (tlen <= len)
9246             {
9247               base_reg = hard_frame_pointer_rtx;
9248               base_offset = toffset;
9249               len = tlen;
9250             }
9251         }
9252     }
9253   gcc_assert (base_reg != NULL);
9254
9255   return plus_constant (base_reg, base_offset);
9256 }
9257
9258 /* Emit code to save registers in the prologue.  */
9259
9260 static void
9261 ix86_emit_save_regs (void)
9262 {
9263   unsigned int regno;
9264   rtx insn;
9265
9266   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
9267     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9268       {
9269         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
9270         RTX_FRAME_RELATED_P (insn) = 1;
9271       }
9272 }
9273
9274 /* Emit a single register save at CFA - CFA_OFFSET.  */
9275
9276 static void
9277 ix86_emit_save_reg_using_mov (enum machine_mode mode, unsigned int regno,
9278                               HOST_WIDE_INT cfa_offset)
9279 {
9280   struct machine_function *m = cfun->machine;
9281   rtx reg = gen_rtx_REG (mode, regno);
9282   rtx mem, addr, base, insn;
9283
9284   addr = choose_baseaddr (cfa_offset);
9285   mem = gen_frame_mem (mode, addr);
9286
9287   /* For SSE saves, we need to indicate the 128-bit alignment.  */
9288   set_mem_align (mem, GET_MODE_ALIGNMENT (mode));
9289
9290   insn = emit_move_insn (mem, reg);
9291   RTX_FRAME_RELATED_P (insn) = 1;
9292
9293   base = addr;
9294   if (GET_CODE (base) == PLUS)
9295     base = XEXP (base, 0);
9296   gcc_checking_assert (REG_P (base));
9297
9298   /* When saving registers into a re-aligned local stack frame, avoid
9299      any tricky guessing by dwarf2out.  */
9300   if (m->fs.realigned)
9301     {
9302       gcc_checking_assert (stack_realign_drap);
9303
9304       if (regno == REGNO (crtl->drap_reg))
9305         {
9306           /* A bit of a hack.  We force the DRAP register to be saved in
9307              the re-aligned stack frame, which provides us with a copy
9308              of the CFA that will last past the prologue.  Install it.  */
9309           gcc_checking_assert (cfun->machine->fs.fp_valid);
9310           addr = plus_constant (hard_frame_pointer_rtx,
9311                                 cfun->machine->fs.fp_offset - cfa_offset);
9312           mem = gen_rtx_MEM (mode, addr);
9313           add_reg_note (insn, REG_CFA_DEF_CFA, mem);
9314         }
9315       else
9316         {
9317           /* The frame pointer is a stable reference within the
9318              aligned frame.  Use it.  */
9319           gcc_checking_assert (cfun->machine->fs.fp_valid);
9320           addr = plus_constant (hard_frame_pointer_rtx,
9321                                 cfun->machine->fs.fp_offset - cfa_offset);
9322           mem = gen_rtx_MEM (mode, addr);
9323           add_reg_note (insn, REG_CFA_EXPRESSION,
9324                         gen_rtx_SET (VOIDmode, mem, reg));
9325         }
9326     }
9327
9328   /* The memory may not be relative to the current CFA register,
9329      which means that we may need to generate a new pattern for
9330      use by the unwind info.  */
9331   else if (base != m->fs.cfa_reg)
9332     {
9333       addr = plus_constant (m->fs.cfa_reg, m->fs.cfa_offset - cfa_offset);
9334       mem = gen_rtx_MEM (mode, addr);
9335       add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (VOIDmode, mem, reg));
9336     }
9337 }
9338
9339 /* Emit code to save registers using MOV insns.
9340    First register is stored at CFA - CFA_OFFSET.  */
9341 static void
9342 ix86_emit_save_regs_using_mov (HOST_WIDE_INT cfa_offset)
9343 {
9344   unsigned int regno;
9345
9346   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9347     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9348       {
9349         ix86_emit_save_reg_using_mov (Pmode, regno, cfa_offset);
9350         cfa_offset -= UNITS_PER_WORD;
9351       }
9352 }
9353
9354 /* Emit code to save SSE registers using MOV insns.
9355    First register is stored at CFA - CFA_OFFSET.  */
9356 static void
9357 ix86_emit_save_sse_regs_using_mov (HOST_WIDE_INT cfa_offset)
9358 {
9359   unsigned int regno;
9360
9361   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9362     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
9363       {
9364         ix86_emit_save_reg_using_mov (V4SFmode, regno, cfa_offset);
9365         cfa_offset -= 16;
9366       }
9367 }
9368
9369 static GTY(()) rtx queued_cfa_restores;
9370
9371 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
9372    manipulation insn.  The value is on the stack at CFA - CFA_OFFSET.
9373    Don't add the note if the previously saved value will be left untouched
9374    within stack red-zone till return, as unwinders can find the same value
9375    in the register and on the stack.  */
9376
9377 static void
9378 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT cfa_offset)
9379 {
9380   if (cfa_offset <= cfun->machine->fs.red_zone_offset)
9381     return;
9382
9383   if (insn)
9384     {
9385       add_reg_note (insn, REG_CFA_RESTORE, reg);
9386       RTX_FRAME_RELATED_P (insn) = 1;
9387     }
9388   else
9389     queued_cfa_restores
9390       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
9391 }
9392
9393 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
9394
9395 static void
9396 ix86_add_queued_cfa_restore_notes (rtx insn)
9397 {
9398   rtx last;
9399   if (!queued_cfa_restores)
9400     return;
9401   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
9402     ;
9403   XEXP (last, 1) = REG_NOTES (insn);
9404   REG_NOTES (insn) = queued_cfa_restores;
9405   queued_cfa_restores = NULL_RTX;
9406   RTX_FRAME_RELATED_P (insn) = 1;
9407 }
9408
9409 /* Expand prologue or epilogue stack adjustment.
9410    The pattern exist to put a dependency on all ebp-based memory accesses.
9411    STYLE should be negative if instructions should be marked as frame related,
9412    zero if %r11 register is live and cannot be freely used and positive
9413    otherwise.  */
9414
9415 static void
9416 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
9417                            int style, bool set_cfa)
9418 {
9419   struct machine_function *m = cfun->machine;
9420   rtx insn;
9421
9422   if (! TARGET_64BIT)
9423     insn = gen_pro_epilogue_adjust_stack_si_add (dest, src, offset);
9424   else if (x86_64_immediate_operand (offset, DImode))
9425     insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, offset);
9426   else
9427     {
9428       rtx tmp;
9429       /* r11 is used by indirect sibcall return as well, set before the
9430          epilogue and used after the epilogue.  */
9431       if (style)
9432         tmp = gen_rtx_REG (DImode, R11_REG);
9433       else
9434         {
9435           gcc_assert (src != hard_frame_pointer_rtx
9436                       && dest != hard_frame_pointer_rtx);
9437           tmp = hard_frame_pointer_rtx;
9438         }
9439       insn = emit_insn (gen_rtx_SET (DImode, tmp, offset));
9440       if (style < 0)
9441         RTX_FRAME_RELATED_P (insn) = 1;
9442
9443       insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, tmp);
9444     }
9445
9446   insn = emit_insn (insn);
9447   if (style >= 0)
9448     ix86_add_queued_cfa_restore_notes (insn);
9449
9450   if (set_cfa)
9451     {
9452       rtx r;
9453
9454       gcc_assert (m->fs.cfa_reg == src);
9455       m->fs.cfa_offset += INTVAL (offset);
9456       m->fs.cfa_reg = dest;
9457
9458       r = gen_rtx_PLUS (Pmode, src, offset);
9459       r = gen_rtx_SET (VOIDmode, dest, r);
9460       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
9461       RTX_FRAME_RELATED_P (insn) = 1;
9462     }
9463   else if (style < 0)
9464     RTX_FRAME_RELATED_P (insn) = 1;
9465
9466   if (dest == stack_pointer_rtx)
9467     {
9468       HOST_WIDE_INT ooffset = m->fs.sp_offset;
9469       bool valid = m->fs.sp_valid;
9470
9471       if (src == hard_frame_pointer_rtx)
9472         {
9473           valid = m->fs.fp_valid;
9474           ooffset = m->fs.fp_offset;
9475         }
9476       else if (src == crtl->drap_reg)
9477         {
9478           valid = m->fs.drap_valid;
9479           ooffset = 0;
9480         }
9481       else
9482         {
9483           /* Else there are two possibilities: SP itself, which we set
9484              up as the default above.  Or EH_RETURN_STACKADJ_RTX, which is
9485              taken care of this by hand along the eh_return path.  */
9486           gcc_checking_assert (src == stack_pointer_rtx
9487                                || offset == const0_rtx);
9488         }
9489
9490       m->fs.sp_offset = ooffset - INTVAL (offset);
9491       m->fs.sp_valid = valid;
9492     }
9493 }
9494
9495 /* Find an available register to be used as dynamic realign argument
9496    pointer regsiter.  Such a register will be written in prologue and
9497    used in begin of body, so it must not be
9498         1. parameter passing register.
9499         2. GOT pointer.
9500    We reuse static-chain register if it is available.  Otherwise, we
9501    use DI for i386 and R13 for x86-64.  We chose R13 since it has
9502    shorter encoding.
9503
9504    Return: the regno of chosen register.  */
9505
9506 static unsigned int
9507 find_drap_reg (void)
9508 {
9509   tree decl = cfun->decl;
9510
9511   if (TARGET_64BIT)
9512     {
9513       /* Use R13 for nested function or function need static chain.
9514          Since function with tail call may use any caller-saved
9515          registers in epilogue, DRAP must not use caller-saved
9516          register in such case.  */
9517       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9518         return R13_REG;
9519
9520       return R10_REG;
9521     }
9522   else
9523     {
9524       /* Use DI for nested function or function need static chain.
9525          Since function with tail call may use any caller-saved
9526          registers in epilogue, DRAP must not use caller-saved
9527          register in such case.  */
9528       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
9529         return DI_REG;
9530
9531       /* Reuse static chain register if it isn't used for parameter
9532          passing.  */
9533       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2
9534           && !lookup_attribute ("fastcall",
9535                                 TYPE_ATTRIBUTES (TREE_TYPE (decl)))
9536           && !lookup_attribute ("thiscall",
9537                                 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
9538         return CX_REG;
9539       else
9540         return DI_REG;
9541     }
9542 }
9543
9544 /* Return minimum incoming stack alignment.  */
9545
9546 static unsigned int
9547 ix86_minimum_incoming_stack_boundary (bool sibcall)
9548 {
9549   unsigned int incoming_stack_boundary;
9550
9551   /* Prefer the one specified at command line. */
9552   if (ix86_user_incoming_stack_boundary)
9553     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
9554   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
9555      if -mstackrealign is used, it isn't used for sibcall check and
9556      estimated stack alignment is 128bit.  */
9557   else if (!sibcall
9558            && !TARGET_64BIT
9559            && ix86_force_align_arg_pointer
9560            && crtl->stack_alignment_estimated == 128)
9561     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9562   else
9563     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
9564
9565   /* Incoming stack alignment can be changed on individual functions
9566      via force_align_arg_pointer attribute.  We use the smallest
9567      incoming stack boundary.  */
9568   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
9569       && lookup_attribute (ix86_force_align_arg_pointer_string,
9570                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
9571     incoming_stack_boundary = MIN_STACK_BOUNDARY;
9572
9573   /* The incoming stack frame has to be aligned at least at
9574      parm_stack_boundary.  */
9575   if (incoming_stack_boundary < crtl->parm_stack_boundary)
9576     incoming_stack_boundary = crtl->parm_stack_boundary;
9577
9578   /* Stack at entrance of main is aligned by runtime.  We use the
9579      smallest incoming stack boundary. */
9580   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
9581       && DECL_NAME (current_function_decl)
9582       && MAIN_NAME_P (DECL_NAME (current_function_decl))
9583       && DECL_FILE_SCOPE_P (current_function_decl))
9584     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
9585
9586   return incoming_stack_boundary;
9587 }
9588
9589 /* Update incoming stack boundary and estimated stack alignment.  */
9590
9591 static void
9592 ix86_update_stack_boundary (void)
9593 {
9594   ix86_incoming_stack_boundary
9595     = ix86_minimum_incoming_stack_boundary (false);
9596
9597   /* x86_64 vararg needs 16byte stack alignment for register save
9598      area.  */
9599   if (TARGET_64BIT
9600       && cfun->stdarg
9601       && crtl->stack_alignment_estimated < 128)
9602     crtl->stack_alignment_estimated = 128;
9603 }
9604
9605 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
9606    needed or an rtx for DRAP otherwise.  */
9607
9608 static rtx
9609 ix86_get_drap_rtx (void)
9610 {
9611   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
9612     crtl->need_drap = true;
9613
9614   if (stack_realign_drap)
9615     {
9616       /* Assign DRAP to vDRAP and returns vDRAP */
9617       unsigned int regno = find_drap_reg ();
9618       rtx drap_vreg;
9619       rtx arg_ptr;
9620       rtx seq, insn;
9621
9622       arg_ptr = gen_rtx_REG (Pmode, regno);
9623       crtl->drap_reg = arg_ptr;
9624
9625       start_sequence ();
9626       drap_vreg = copy_to_reg (arg_ptr);
9627       seq = get_insns ();
9628       end_sequence ();
9629
9630       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
9631       if (!optimize)
9632         {
9633           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
9634           RTX_FRAME_RELATED_P (insn) = 1;
9635         }
9636       return drap_vreg;
9637     }
9638   else
9639     return NULL;
9640 }
9641
9642 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
9643
9644 static rtx
9645 ix86_internal_arg_pointer (void)
9646 {
9647   return virtual_incoming_args_rtx;
9648 }
9649
9650 struct scratch_reg {
9651   rtx reg;
9652   bool saved;
9653 };
9654
9655 /* Return a short-lived scratch register for use on function entry.
9656    In 32-bit mode, it is valid only after the registers are saved
9657    in the prologue.  This register must be released by means of
9658    release_scratch_register_on_entry once it is dead.  */
9659
9660 static void
9661 get_scratch_register_on_entry (struct scratch_reg *sr)
9662 {
9663   int regno;
9664
9665   sr->saved = false;
9666
9667   if (TARGET_64BIT)
9668     {
9669       /* We always use R11 in 64-bit mode.  */
9670       regno = R11_REG;
9671     }
9672   else
9673     {
9674       tree decl = current_function_decl, fntype = TREE_TYPE (decl);
9675       bool fastcall_p
9676         = lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
9677       bool static_chain_p = DECL_STATIC_CHAIN (decl);
9678       int regparm = ix86_function_regparm (fntype, decl);
9679       int drap_regno
9680         = crtl->drap_reg ? REGNO (crtl->drap_reg) : INVALID_REGNUM;
9681
9682       /* 'fastcall' sets regparm to 2, uses ecx/edx for arguments and eax
9683           for the static chain register.  */
9684       if ((regparm < 1 || (fastcall_p && !static_chain_p))
9685           && drap_regno != AX_REG)
9686         regno = AX_REG;
9687       else if (regparm < 2 && drap_regno != DX_REG)
9688         regno = DX_REG;
9689       /* ecx is the static chain register.  */
9690       else if (regparm < 3 && !fastcall_p && !static_chain_p
9691                && drap_regno != CX_REG)
9692         regno = CX_REG;
9693       else if (ix86_save_reg (BX_REG, true))
9694         regno = BX_REG;
9695       /* esi is the static chain register.  */
9696       else if (!(regparm == 3 && static_chain_p)
9697                && ix86_save_reg (SI_REG, true))
9698         regno = SI_REG;
9699       else if (ix86_save_reg (DI_REG, true))
9700         regno = DI_REG;
9701       else
9702         {
9703           regno = (drap_regno == AX_REG ? DX_REG : AX_REG);
9704           sr->saved = true;
9705         }
9706     }
9707
9708   sr->reg = gen_rtx_REG (Pmode, regno);
9709   if (sr->saved)
9710     {
9711       rtx insn = emit_insn (gen_push (sr->reg));
9712       RTX_FRAME_RELATED_P (insn) = 1;
9713     }
9714 }
9715
9716 /* Release a scratch register obtained from the preceding function.  */
9717
9718 static void
9719 release_scratch_register_on_entry (struct scratch_reg *sr)
9720 {
9721   if (sr->saved)
9722     {
9723       rtx x, insn = emit_insn (gen_pop (sr->reg));
9724
9725       /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
9726       RTX_FRAME_RELATED_P (insn) = 1;
9727       x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
9728       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
9729       add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
9730     }
9731 }
9732
9733 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
9734
9735 /* Emit code to adjust the stack pointer by SIZE bytes while probing it.  */
9736
9737 static void
9738 ix86_adjust_stack_and_probe (const HOST_WIDE_INT size)
9739 {
9740   /* We skip the probe for the first interval + a small dope of 4 words and
9741      probe that many bytes past the specified size to maintain a protection
9742      area at the botton of the stack.  */
9743   const int dope = 4 * UNITS_PER_WORD;
9744   rtx size_rtx = GEN_INT (size);
9745
9746   /* See if we have a constant small number of probes to generate.  If so,
9747      that's the easy case.  The run-time loop is made up of 11 insns in the
9748      generic case while the compile-time loop is made up of 3+2*(n-1) insns
9749      for n # of intervals.  */
9750   if (size <= 5 * PROBE_INTERVAL)
9751     {
9752       HOST_WIDE_INT i, adjust;
9753       bool first_probe = true;
9754
9755       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
9756          values of N from 1 until it exceeds SIZE.  If only one probe is
9757          needed, this will not generate any code.  Then adjust and probe
9758          to PROBE_INTERVAL + SIZE.  */
9759       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9760         {
9761           if (first_probe)
9762             {
9763               adjust = 2 * PROBE_INTERVAL + dope;
9764               first_probe = false;
9765             }
9766           else
9767             adjust = PROBE_INTERVAL;
9768
9769           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9770                                   plus_constant (stack_pointer_rtx, -adjust)));
9771           emit_stack_probe (stack_pointer_rtx);
9772         }
9773
9774       if (first_probe)
9775         adjust = size + PROBE_INTERVAL + dope;
9776       else
9777         adjust = size + PROBE_INTERVAL - i;
9778
9779       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9780                               plus_constant (stack_pointer_rtx, -adjust)));
9781       emit_stack_probe (stack_pointer_rtx);
9782
9783       /* Adjust back to account for the additional first interval.  */
9784       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9785                               plus_constant (stack_pointer_rtx,
9786                                              PROBE_INTERVAL + dope)));
9787     }
9788
9789   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9790      extra careful with variables wrapping around because we might be at
9791      the very top (or the very bottom) of the address space and we have
9792      to be able to handle this case properly; in particular, we use an
9793      equality test for the loop condition.  */
9794   else
9795     {
9796       HOST_WIDE_INT rounded_size;
9797       struct scratch_reg sr;
9798
9799       get_scratch_register_on_entry (&sr);
9800
9801
9802       /* Step 1: round SIZE to the previous multiple of the interval.  */
9803
9804       rounded_size = size & -PROBE_INTERVAL;
9805
9806
9807       /* Step 2: compute initial and final value of the loop counter.  */
9808
9809       /* SP = SP_0 + PROBE_INTERVAL.  */
9810       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9811                               plus_constant (stack_pointer_rtx,
9812                                              - (PROBE_INTERVAL + dope))));
9813
9814       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
9815       emit_move_insn (sr.reg, GEN_INT (-rounded_size));
9816       emit_insn (gen_rtx_SET (VOIDmode, sr.reg,
9817                               gen_rtx_PLUS (Pmode, sr.reg,
9818                                             stack_pointer_rtx)));
9819
9820
9821       /* Step 3: the loop
9822
9823          while (SP != LAST_ADDR)
9824            {
9825              SP = SP + PROBE_INTERVAL
9826              probe at SP
9827            }
9828
9829          adjusts SP and probes to PROBE_INTERVAL + N * PROBE_INTERVAL for
9830          values of N from 1 until it is equal to ROUNDED_SIZE.  */
9831
9832       emit_insn (ix86_gen_adjust_stack_and_probe (sr.reg, sr.reg, size_rtx));
9833
9834
9835       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
9836          assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
9837
9838       if (size != rounded_size)
9839         {
9840           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9841                                   plus_constant (stack_pointer_rtx,
9842                                                  rounded_size - size)));
9843           emit_stack_probe (stack_pointer_rtx);
9844         }
9845
9846       /* Adjust back to account for the additional first interval.  */
9847       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9848                               plus_constant (stack_pointer_rtx,
9849                                              PROBE_INTERVAL + dope)));
9850
9851       release_scratch_register_on_entry (&sr);
9852     }
9853
9854   gcc_assert (cfun->machine->fs.cfa_reg != stack_pointer_rtx);
9855   cfun->machine->fs.sp_offset += size;
9856
9857   /* Make sure nothing is scheduled before we are done.  */
9858   emit_insn (gen_blockage ());
9859 }
9860
9861 /* Adjust the stack pointer up to REG while probing it.  */
9862
9863 const char *
9864 output_adjust_stack_and_probe (rtx reg)
9865 {
9866   static int labelno = 0;
9867   char loop_lab[32], end_lab[32];
9868   rtx xops[2];
9869
9870   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9871   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9872
9873   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9874
9875   /* Jump to END_LAB if SP == LAST_ADDR.  */
9876   xops[0] = stack_pointer_rtx;
9877   xops[1] = reg;
9878   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9879   fputs ("\tje\t", asm_out_file);
9880   assemble_name_raw (asm_out_file, end_lab);
9881   fputc ('\n', asm_out_file);
9882
9883   /* SP = SP + PROBE_INTERVAL.  */
9884   xops[1] = GEN_INT (PROBE_INTERVAL);
9885   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9886
9887   /* Probe at SP.  */
9888   xops[1] = const0_rtx;
9889   output_asm_insn ("or%z0\t{%1, (%0)|DWORD PTR [%0], %1}", xops);
9890
9891   fprintf (asm_out_file, "\tjmp\t");
9892   assemble_name_raw (asm_out_file, loop_lab);
9893   fputc ('\n', asm_out_file);
9894
9895   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9896
9897   return "";
9898 }
9899
9900 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
9901    inclusive.  These are offsets from the current stack pointer.  */
9902
9903 static void
9904 ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
9905 {
9906   /* See if we have a constant small number of probes to generate.  If so,
9907      that's the easy case.  The run-time loop is made up of 7 insns in the
9908      generic case while the compile-time loop is made up of n insns for n #
9909      of intervals.  */
9910   if (size <= 7 * PROBE_INTERVAL)
9911     {
9912       HOST_WIDE_INT i;
9913
9914       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
9915          it exceeds SIZE.  If only one probe is needed, this will not
9916          generate any code.  Then probe at FIRST + SIZE.  */
9917       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9918         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
9919
9920       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
9921     }
9922
9923   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9924      extra careful with variables wrapping around because we might be at
9925      the very top (or the very bottom) of the address space and we have
9926      to be able to handle this case properly; in particular, we use an
9927      equality test for the loop condition.  */
9928   else
9929     {
9930       HOST_WIDE_INT rounded_size, last;
9931       struct scratch_reg sr;
9932
9933       get_scratch_register_on_entry (&sr);
9934
9935
9936       /* Step 1: round SIZE to the previous multiple of the interval.  */
9937
9938       rounded_size = size & -PROBE_INTERVAL;
9939
9940
9941       /* Step 2: compute initial and final value of the loop counter.  */
9942
9943       /* TEST_OFFSET = FIRST.  */
9944       emit_move_insn (sr.reg, GEN_INT (-first));
9945
9946       /* LAST_OFFSET = FIRST + ROUNDED_SIZE.  */
9947       last = first + rounded_size;
9948
9949
9950       /* Step 3: the loop
9951
9952          while (TEST_ADDR != LAST_ADDR)
9953            {
9954              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
9955              probe at TEST_ADDR
9956            }
9957
9958          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
9959          until it is equal to ROUNDED_SIZE.  */
9960
9961       emit_insn (ix86_gen_probe_stack_range (sr.reg, sr.reg, GEN_INT (-last)));
9962
9963
9964       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
9965          that SIZE is equal to ROUNDED_SIZE.  */
9966
9967       if (size != rounded_size)
9968         emit_stack_probe (plus_constant (gen_rtx_PLUS (Pmode,
9969                                                        stack_pointer_rtx,
9970                                                        sr.reg),
9971                                          rounded_size - size));
9972
9973       release_scratch_register_on_entry (&sr);
9974     }
9975
9976   /* Make sure nothing is scheduled before we are done.  */
9977   emit_insn (gen_blockage ());
9978 }
9979
9980 /* Probe a range of stack addresses from REG to END, inclusive.  These are
9981    offsets from the current stack pointer.  */
9982
9983 const char *
9984 output_probe_stack_range (rtx reg, rtx end)
9985 {
9986   static int labelno = 0;
9987   char loop_lab[32], end_lab[32];
9988   rtx xops[3];
9989
9990   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9991   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9992
9993   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9994
9995   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
9996   xops[0] = reg;
9997   xops[1] = end;
9998   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9999   fputs ("\tje\t", asm_out_file);
10000   assemble_name_raw (asm_out_file, end_lab);
10001   fputc ('\n', asm_out_file);
10002
10003   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
10004   xops[1] = GEN_INT (PROBE_INTERVAL);
10005   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
10006
10007   /* Probe at TEST_ADDR.  */
10008   xops[0] = stack_pointer_rtx;
10009   xops[1] = reg;
10010   xops[2] = const0_rtx;
10011   output_asm_insn ("or%z0\t{%2, (%0,%1)|DWORD PTR [%0+%1], %2}", xops);
10012
10013   fprintf (asm_out_file, "\tjmp\t");
10014   assemble_name_raw (asm_out_file, loop_lab);
10015   fputc ('\n', asm_out_file);
10016
10017   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
10018
10019   return "";
10020 }
10021
10022 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
10023    to be generated in correct form.  */
10024 static void
10025 ix86_finalize_stack_realign_flags (void)
10026 {
10027   /* Check if stack realign is really needed after reload, and
10028      stores result in cfun */
10029   unsigned int incoming_stack_boundary
10030     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
10031        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
10032   unsigned int stack_realign = (incoming_stack_boundary
10033                                 < (current_function_is_leaf
10034                                    ? crtl->max_used_stack_slot_alignment
10035                                    : crtl->stack_alignment_needed));
10036
10037   if (crtl->stack_realign_finalized)
10038     {
10039       /* After stack_realign_needed is finalized, we can't no longer
10040          change it.  */
10041       gcc_assert (crtl->stack_realign_needed == stack_realign);
10042     }
10043   else
10044     {
10045       crtl->stack_realign_needed = stack_realign;
10046       crtl->stack_realign_finalized = true;
10047     }
10048 }
10049
10050 /* Expand the prologue into a bunch of separate insns.  */
10051
10052 void
10053 ix86_expand_prologue (void)
10054 {
10055   struct machine_function *m = cfun->machine;
10056   rtx insn, t;
10057   bool pic_reg_used;
10058   struct ix86_frame frame;
10059   HOST_WIDE_INT allocate;
10060   bool int_registers_saved;
10061
10062   ix86_finalize_stack_realign_flags ();
10063
10064   /* DRAP should not coexist with stack_realign_fp */
10065   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
10066
10067   memset (&m->fs, 0, sizeof (m->fs));
10068
10069   /* Initialize CFA state for before the prologue.  */
10070   m->fs.cfa_reg = stack_pointer_rtx;
10071   m->fs.cfa_offset = INCOMING_FRAME_SP_OFFSET;
10072
10073   /* Track SP offset to the CFA.  We continue tracking this after we've
10074      swapped the CFA register away from SP.  In the case of re-alignment
10075      this is fudged; we're interested to offsets within the local frame.  */
10076   m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10077   m->fs.sp_valid = true;
10078
10079   ix86_compute_frame_layout (&frame);
10080
10081   if (!TARGET_64BIT && ix86_function_ms_hook_prologue (current_function_decl))
10082     {
10083       /* We should have already generated an error for any use of
10084          ms_hook on a nested function.  */
10085       gcc_checking_assert (!ix86_static_chain_on_stack);
10086
10087       /* Check if profiling is active and we shall use profiling before
10088          prologue variant. If so sorry.  */
10089       if (crtl->profile && flag_fentry != 0)
10090         sorry ("ms_hook_prologue attribute isn't compatible "
10091                "with -mfentry for 32-bit");
10092
10093       /* In ix86_asm_output_function_label we emitted:
10094          8b ff     movl.s %edi,%edi
10095          55        push   %ebp
10096          8b ec     movl.s %esp,%ebp
10097
10098          This matches the hookable function prologue in Win32 API
10099          functions in Microsoft Windows XP Service Pack 2 and newer.
10100          Wine uses this to enable Windows apps to hook the Win32 API
10101          functions provided by Wine.
10102
10103          What that means is that we've already set up the frame pointer.  */
10104
10105       if (frame_pointer_needed
10106           && !(crtl->drap_reg && crtl->stack_realign_needed))
10107         {
10108           rtx push, mov;
10109
10110           /* We've decided to use the frame pointer already set up.
10111              Describe this to the unwinder by pretending that both
10112              push and mov insns happen right here.
10113
10114              Putting the unwind info here at the end of the ms_hook
10115              is done so that we can make absolutely certain we get
10116              the required byte sequence at the start of the function,
10117              rather than relying on an assembler that can produce
10118              the exact encoding required.
10119
10120              However it does mean (in the unpatched case) that we have
10121              a 1 insn window where the asynchronous unwind info is
10122              incorrect.  However, if we placed the unwind info at
10123              its correct location we would have incorrect unwind info
10124              in the patched case.  Which is probably all moot since
10125              I don't expect Wine generates dwarf2 unwind info for the
10126              system libraries that use this feature.  */
10127
10128           insn = emit_insn (gen_blockage ());
10129
10130           push = gen_push (hard_frame_pointer_rtx);
10131           mov = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10132                              stack_pointer_rtx);
10133           RTX_FRAME_RELATED_P (push) = 1;
10134           RTX_FRAME_RELATED_P (mov) = 1;
10135
10136           RTX_FRAME_RELATED_P (insn) = 1;
10137           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10138                         gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, push, mov)));
10139
10140           /* Note that gen_push incremented m->fs.cfa_offset, even
10141              though we didn't emit the push insn here.  */
10142           m->fs.cfa_reg = hard_frame_pointer_rtx;
10143           m->fs.fp_offset = m->fs.cfa_offset;
10144           m->fs.fp_valid = true;
10145         }
10146       else
10147         {
10148           /* The frame pointer is not needed so pop %ebp again.
10149              This leaves us with a pristine state.  */
10150           emit_insn (gen_pop (hard_frame_pointer_rtx));
10151         }
10152     }
10153
10154   /* The first insn of a function that accepts its static chain on the
10155      stack is to push the register that would be filled in by a direct
10156      call.  This insn will be skipped by the trampoline.  */
10157   else if (ix86_static_chain_on_stack)
10158     {
10159       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
10160       emit_insn (gen_blockage ());
10161
10162       /* We don't want to interpret this push insn as a register save,
10163          only as a stack adjustment.  The real copy of the register as
10164          a save will be done later, if needed.  */
10165       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
10166       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
10167       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
10168       RTX_FRAME_RELATED_P (insn) = 1;
10169     }
10170
10171   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
10172      of DRAP is needed and stack realignment is really needed after reload */
10173   if (stack_realign_drap)
10174     {
10175       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10176
10177       /* Only need to push parameter pointer reg if it is caller saved.  */
10178       if (!call_used_regs[REGNO (crtl->drap_reg)])
10179         {
10180           /* Push arg pointer reg */
10181           insn = emit_insn (gen_push (crtl->drap_reg));
10182           RTX_FRAME_RELATED_P (insn) = 1;
10183         }
10184
10185       /* Grab the argument pointer.  */
10186       t = plus_constant (stack_pointer_rtx, m->fs.sp_offset);
10187       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10188       RTX_FRAME_RELATED_P (insn) = 1;
10189       m->fs.cfa_reg = crtl->drap_reg;
10190       m->fs.cfa_offset = 0;
10191
10192       /* Align the stack.  */
10193       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10194                                         stack_pointer_rtx,
10195                                         GEN_INT (-align_bytes)));
10196       RTX_FRAME_RELATED_P (insn) = 1;
10197
10198       /* Replicate the return address on the stack so that return
10199          address can be reached via (argp - 1) slot.  This is needed
10200          to implement macro RETURN_ADDR_RTX and intrinsic function
10201          expand_builtin_return_addr etc.  */
10202       t = plus_constant (crtl->drap_reg, -UNITS_PER_WORD);
10203       t = gen_frame_mem (Pmode, t);
10204       insn = emit_insn (gen_push (t));
10205       RTX_FRAME_RELATED_P (insn) = 1;
10206
10207       /* For the purposes of frame and register save area addressing,
10208          we've started over with a new frame.  */
10209       m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
10210       m->fs.realigned = true;
10211     }
10212
10213   if (frame_pointer_needed && !m->fs.fp_valid)
10214     {
10215       /* Note: AT&T enter does NOT have reversed args.  Enter is probably
10216          slower on all targets.  Also sdb doesn't like it.  */
10217       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
10218       RTX_FRAME_RELATED_P (insn) = 1;
10219
10220       if (m->fs.sp_offset == frame.hard_frame_pointer_offset)
10221         {
10222           insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
10223           RTX_FRAME_RELATED_P (insn) = 1;
10224
10225           if (m->fs.cfa_reg == stack_pointer_rtx)
10226             m->fs.cfa_reg = hard_frame_pointer_rtx;
10227           m->fs.fp_offset = m->fs.sp_offset;
10228           m->fs.fp_valid = true;
10229         }
10230     }
10231
10232   int_registers_saved = (frame.nregs == 0);
10233
10234   if (!int_registers_saved)
10235     {
10236       /* If saving registers via PUSH, do so now.  */
10237       if (!frame.save_regs_using_mov)
10238         {
10239           ix86_emit_save_regs ();
10240           int_registers_saved = true;
10241           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
10242         }
10243
10244       /* When using red zone we may start register saving before allocating
10245          the stack frame saving one cycle of the prologue.  However, avoid
10246          doing this if we have to probe the stack; at least on x86_64 the
10247          stack probe can turn into a call that clobbers a red zone location. */
10248       else if (ix86_using_red_zone ()
10249                && (! TARGET_STACK_PROBE
10250                    || frame.stack_pointer_offset < CHECK_STACK_LIMIT))
10251         {
10252           ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10253           int_registers_saved = true;
10254         }
10255     }
10256
10257   if (stack_realign_fp)
10258     {
10259       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
10260       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
10261
10262       /* The computation of the size of the re-aligned stack frame means
10263          that we must allocate the size of the register save area before
10264          performing the actual alignment.  Otherwise we cannot guarantee
10265          that there's enough storage above the realignment point.  */
10266       if (m->fs.sp_offset != frame.sse_reg_save_offset)
10267         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10268                                    GEN_INT (m->fs.sp_offset
10269                                             - frame.sse_reg_save_offset),
10270                                    -1, false);
10271
10272       /* Align the stack.  */
10273       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
10274                                         stack_pointer_rtx,
10275                                         GEN_INT (-align_bytes)));
10276
10277       /* For the purposes of register save area addressing, the stack
10278          pointer is no longer valid.  As for the value of sp_offset,
10279          see ix86_compute_frame_layout, which we need to match in order
10280          to pass verification of stack_pointer_offset at the end.  */
10281       m->fs.sp_offset = (m->fs.sp_offset + align_bytes) & -align_bytes;
10282       m->fs.sp_valid = false;
10283     }
10284
10285   allocate = frame.stack_pointer_offset - m->fs.sp_offset;
10286
10287   if (flag_stack_usage)
10288     {
10289       /* We start to count from ARG_POINTER.  */
10290       HOST_WIDE_INT stack_size = frame.stack_pointer_offset;
10291
10292       /* If it was realigned, take into account the fake frame.  */
10293       if (stack_realign_drap)
10294         {
10295           if (ix86_static_chain_on_stack)
10296             stack_size += UNITS_PER_WORD;
10297
10298           if (!call_used_regs[REGNO (crtl->drap_reg)])
10299             stack_size += UNITS_PER_WORD;
10300
10301           /* This over-estimates by 1 minimal-stack-alignment-unit but
10302              mitigates that by counting in the new return address slot.  */
10303           current_function_dynamic_stack_size
10304             += crtl->stack_alignment_needed / BITS_PER_UNIT;
10305         }
10306
10307       current_function_static_stack_size = stack_size;
10308     }
10309
10310   /* The stack has already been decremented by the instruction calling us
10311      so we need to probe unconditionally to preserve the protection area.  */
10312   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
10313     {
10314       /* We expect the registers to be saved when probes are used.  */
10315       gcc_assert (int_registers_saved);
10316
10317       if (STACK_CHECK_MOVING_SP)
10318         {
10319           ix86_adjust_stack_and_probe (allocate);
10320           allocate = 0;
10321         }
10322       else
10323         {
10324           HOST_WIDE_INT size = allocate;
10325
10326           if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
10327             size = 0x80000000 - STACK_CHECK_PROTECT - 1;
10328
10329           if (TARGET_STACK_PROBE)
10330             ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
10331           else
10332             ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
10333         }
10334     }
10335
10336   if (allocate == 0)
10337     ;
10338   else if (!ix86_target_stack_probe ()
10339            || frame.stack_pointer_offset < CHECK_STACK_LIMIT)
10340     {
10341       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10342                                  GEN_INT (-allocate), -1,
10343                                  m->fs.cfa_reg == stack_pointer_rtx);
10344     }
10345   else
10346     {
10347       rtx eax = gen_rtx_REG (Pmode, AX_REG);
10348       rtx r10 = NULL;
10349       rtx (*adjust_stack_insn)(rtx, rtx, rtx);
10350
10351       bool eax_live = false;
10352       bool r10_live = false;
10353
10354       if (TARGET_64BIT)
10355         r10_live = (DECL_STATIC_CHAIN (current_function_decl) != 0);
10356       if (!TARGET_64BIT_MS_ABI)
10357         eax_live = ix86_eax_live_at_start_p ();
10358
10359       if (eax_live)
10360         {
10361           emit_insn (gen_push (eax));
10362           allocate -= UNITS_PER_WORD;
10363         }
10364       if (r10_live)
10365         {
10366           r10 = gen_rtx_REG (Pmode, R10_REG);
10367           emit_insn (gen_push (r10));
10368           allocate -= UNITS_PER_WORD;
10369         }
10370
10371       emit_move_insn (eax, GEN_INT (allocate));
10372       emit_insn (ix86_gen_allocate_stack_worker (eax, eax));
10373
10374       /* Use the fact that AX still contains ALLOCATE.  */
10375       adjust_stack_insn = (TARGET_64BIT
10376                            ? gen_pro_epilogue_adjust_stack_di_sub
10377                            : gen_pro_epilogue_adjust_stack_si_sub);
10378
10379       insn = emit_insn (adjust_stack_insn (stack_pointer_rtx,
10380                                            stack_pointer_rtx, eax));
10381
10382       /* Note that SEH directives need to continue tracking the stack
10383          pointer even after the frame pointer has been set up.  */
10384       if (m->fs.cfa_reg == stack_pointer_rtx || TARGET_SEH)
10385         {
10386           if (m->fs.cfa_reg == stack_pointer_rtx)
10387             m->fs.cfa_offset += allocate;
10388
10389           RTX_FRAME_RELATED_P (insn) = 1;
10390           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
10391                         gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10392                                      plus_constant (stack_pointer_rtx,
10393                                                     -allocate)));
10394         }
10395       m->fs.sp_offset += allocate;
10396
10397       if (r10_live && eax_live)
10398         {
10399           t = choose_baseaddr (m->fs.sp_offset - allocate);
10400           emit_move_insn (r10, gen_frame_mem (Pmode, t));
10401           t = choose_baseaddr (m->fs.sp_offset - allocate - UNITS_PER_WORD);
10402           emit_move_insn (eax, gen_frame_mem (Pmode, t));
10403         }
10404       else if (eax_live || r10_live)
10405         {
10406           t = choose_baseaddr (m->fs.sp_offset - allocate);
10407           emit_move_insn ((eax_live ? eax : r10), gen_frame_mem (Pmode, t));
10408         }
10409     }
10410   gcc_assert (m->fs.sp_offset == frame.stack_pointer_offset);
10411
10412   /* If we havn't already set up the frame pointer, do so now.  */
10413   if (frame_pointer_needed && !m->fs.fp_valid)
10414     {
10415       insn = ix86_gen_add3 (hard_frame_pointer_rtx, stack_pointer_rtx,
10416                             GEN_INT (frame.stack_pointer_offset
10417                                      - frame.hard_frame_pointer_offset));
10418       insn = emit_insn (insn);
10419       RTX_FRAME_RELATED_P (insn) = 1;
10420       add_reg_note (insn, REG_CFA_ADJUST_CFA, NULL);
10421
10422       if (m->fs.cfa_reg == stack_pointer_rtx)
10423         m->fs.cfa_reg = hard_frame_pointer_rtx;
10424       m->fs.fp_offset = frame.hard_frame_pointer_offset;
10425       m->fs.fp_valid = true;
10426     }
10427
10428   if (!int_registers_saved)
10429     ix86_emit_save_regs_using_mov (frame.reg_save_offset);
10430   if (frame.nsseregs)
10431     ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
10432
10433   pic_reg_used = false;
10434   if (pic_offset_table_rtx
10435       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
10436           || crtl->profile))
10437     {
10438       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
10439
10440       if (alt_pic_reg_used != INVALID_REGNUM)
10441         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
10442
10443       pic_reg_used = true;
10444     }
10445
10446   if (pic_reg_used)
10447     {
10448       if (TARGET_64BIT)
10449         {
10450           if (ix86_cmodel == CM_LARGE_PIC)
10451             {
10452               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
10453               rtx label = gen_label_rtx ();
10454               emit_label (label);
10455               LABEL_PRESERVE_P (label) = 1;
10456               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
10457               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
10458               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
10459               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
10460                                             pic_offset_table_rtx, tmp_reg));
10461             }
10462           else
10463             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
10464         }
10465       else
10466         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
10467     }
10468
10469   /* In the pic_reg_used case, make sure that the got load isn't deleted
10470      when mcount needs it.  Blockage to avoid call movement across mcount
10471      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
10472      note.  */
10473   if (crtl->profile && !flag_fentry && pic_reg_used)
10474     emit_insn (gen_prologue_use (pic_offset_table_rtx));
10475
10476   if (crtl->drap_reg && !crtl->stack_realign_needed)
10477     {
10478       /* vDRAP is setup but after reload it turns out stack realign
10479          isn't necessary, here we will emit prologue to setup DRAP
10480          without stack realign adjustment */
10481       t = choose_baseaddr (0);
10482       emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
10483     }
10484
10485   /* Prevent instructions from being scheduled into register save push
10486      sequence when access to the redzone area is done through frame pointer.
10487      The offset between the frame pointer and the stack pointer is calculated
10488      relative to the value of the stack pointer at the end of the function
10489      prologue, and moving instructions that access redzone area via frame
10490      pointer inside push sequence violates this assumption.  */
10491   if (frame_pointer_needed && frame.red_zone_size)
10492     emit_insn (gen_memory_blockage ());
10493
10494   /* Emit cld instruction if stringops are used in the function.  */
10495   if (TARGET_CLD && ix86_current_function_needs_cld)
10496     emit_insn (gen_cld ());
10497
10498   /* SEH requires that the prologue end within 256 bytes of the start of
10499      the function.  Prevent instruction schedules that would extend that.  */
10500   if (TARGET_SEH)
10501     emit_insn (gen_blockage ());
10502 }
10503
10504 /* Emit code to restore REG using a POP insn.  */
10505
10506 static void
10507 ix86_emit_restore_reg_using_pop (rtx reg)
10508 {
10509   struct machine_function *m = cfun->machine;
10510   rtx insn = emit_insn (gen_pop (reg));
10511
10512   ix86_add_cfa_restore_note (insn, reg, m->fs.sp_offset);
10513   m->fs.sp_offset -= UNITS_PER_WORD;
10514
10515   if (m->fs.cfa_reg == crtl->drap_reg
10516       && REGNO (reg) == REGNO (crtl->drap_reg))
10517     {
10518       /* Previously we'd represented the CFA as an expression
10519          like *(%ebp - 8).  We've just popped that value from
10520          the stack, which means we need to reset the CFA to
10521          the drap register.  This will remain until we restore
10522          the stack pointer.  */
10523       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10524       RTX_FRAME_RELATED_P (insn) = 1;
10525
10526       /* This means that the DRAP register is valid for addressing too.  */
10527       m->fs.drap_valid = true;
10528       return;
10529     }
10530
10531   if (m->fs.cfa_reg == stack_pointer_rtx)
10532     {
10533       rtx x = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
10534       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
10535       add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
10536       RTX_FRAME_RELATED_P (insn) = 1;
10537
10538       m->fs.cfa_offset -= UNITS_PER_WORD;
10539     }
10540
10541   /* When the frame pointer is the CFA, and we pop it, we are
10542      swapping back to the stack pointer as the CFA.  This happens
10543      for stack frames that don't allocate other data, so we assume
10544      the stack pointer is now pointing at the return address, i.e.
10545      the function entry state, which makes the offset be 1 word.  */
10546   if (reg == hard_frame_pointer_rtx)
10547     {
10548       m->fs.fp_valid = false;
10549       if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10550         {
10551           m->fs.cfa_reg = stack_pointer_rtx;
10552           m->fs.cfa_offset -= UNITS_PER_WORD;
10553
10554           add_reg_note (insn, REG_CFA_DEF_CFA,
10555                         gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10556                                       GEN_INT (m->fs.cfa_offset)));
10557           RTX_FRAME_RELATED_P (insn) = 1;
10558         }
10559     }
10560 }
10561
10562 /* Emit code to restore saved registers using POP insns.  */
10563
10564 static void
10565 ix86_emit_restore_regs_using_pop (void)
10566 {
10567   unsigned int regno;
10568
10569   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10570     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
10571       ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno));
10572 }
10573
10574 /* Emit code and notes for the LEAVE instruction.  */
10575
10576 static void
10577 ix86_emit_leave (void)
10578 {
10579   struct machine_function *m = cfun->machine;
10580   rtx insn = emit_insn (ix86_gen_leave ());
10581
10582   ix86_add_queued_cfa_restore_notes (insn);
10583
10584   gcc_assert (m->fs.fp_valid);
10585   m->fs.sp_valid = true;
10586   m->fs.sp_offset = m->fs.fp_offset - UNITS_PER_WORD;
10587   m->fs.fp_valid = false;
10588
10589   if (m->fs.cfa_reg == hard_frame_pointer_rtx)
10590     {
10591       m->fs.cfa_reg = stack_pointer_rtx;
10592       m->fs.cfa_offset = m->fs.sp_offset;
10593
10594       add_reg_note (insn, REG_CFA_DEF_CFA,
10595                     plus_constant (stack_pointer_rtx, m->fs.sp_offset));
10596       RTX_FRAME_RELATED_P (insn) = 1;
10597       ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx,
10598                                  m->fs.fp_offset);
10599     }
10600 }
10601
10602 /* Emit code to restore saved registers using MOV insns.
10603    First register is restored from CFA - CFA_OFFSET.  */
10604 static void
10605 ix86_emit_restore_regs_using_mov (HOST_WIDE_INT cfa_offset,
10606                                   int maybe_eh_return)
10607 {
10608   struct machine_function *m = cfun->machine;
10609   unsigned int regno;
10610
10611   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10612     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10613       {
10614         rtx reg = gen_rtx_REG (Pmode, regno);
10615         rtx insn, mem;
10616         
10617         mem = choose_baseaddr (cfa_offset);
10618         mem = gen_frame_mem (Pmode, mem);
10619         insn = emit_move_insn (reg, mem);
10620
10621         if (m->fs.cfa_reg == crtl->drap_reg && regno == REGNO (crtl->drap_reg))
10622           {
10623             /* Previously we'd represented the CFA as an expression
10624                like *(%ebp - 8).  We've just popped that value from
10625                the stack, which means we need to reset the CFA to
10626                the drap register.  This will remain until we restore
10627                the stack pointer.  */
10628             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10629             RTX_FRAME_RELATED_P (insn) = 1;
10630
10631             /* This means that the DRAP register is valid for addressing.  */
10632             m->fs.drap_valid = true;
10633           }
10634         else
10635           ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10636
10637         cfa_offset -= UNITS_PER_WORD;
10638       }
10639 }
10640
10641 /* Emit code to restore saved registers using MOV insns.
10642    First register is restored from CFA - CFA_OFFSET.  */
10643 static void
10644 ix86_emit_restore_sse_regs_using_mov (HOST_WIDE_INT cfa_offset,
10645                                       int maybe_eh_return)
10646 {
10647   unsigned int regno;
10648
10649   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10650     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10651       {
10652         rtx reg = gen_rtx_REG (V4SFmode, regno);
10653         rtx mem;
10654
10655         mem = choose_baseaddr (cfa_offset);
10656         mem = gen_rtx_MEM (V4SFmode, mem);
10657         set_mem_align (mem, 128);
10658         emit_move_insn (reg, mem);
10659
10660         ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10661
10662         cfa_offset -= 16;
10663       }
10664 }
10665
10666 /* Restore function stack, frame, and registers.  */
10667
10668 void
10669 ix86_expand_epilogue (int style)
10670 {
10671   struct machine_function *m = cfun->machine;
10672   struct machine_frame_state frame_state_save = m->fs;
10673   struct ix86_frame frame;
10674   bool restore_regs_via_mov;
10675   bool using_drap;
10676
10677   ix86_finalize_stack_realign_flags ();
10678   ix86_compute_frame_layout (&frame);
10679
10680   m->fs.sp_valid = (!frame_pointer_needed
10681                     || (current_function_sp_is_unchanging
10682                         && !stack_realign_fp));
10683   gcc_assert (!m->fs.sp_valid
10684               || m->fs.sp_offset == frame.stack_pointer_offset);
10685
10686   /* The FP must be valid if the frame pointer is present.  */
10687   gcc_assert (frame_pointer_needed == m->fs.fp_valid);
10688   gcc_assert (!m->fs.fp_valid
10689               || m->fs.fp_offset == frame.hard_frame_pointer_offset);
10690
10691   /* We must have *some* valid pointer to the stack frame.  */
10692   gcc_assert (m->fs.sp_valid || m->fs.fp_valid);
10693
10694   /* The DRAP is never valid at this point.  */
10695   gcc_assert (!m->fs.drap_valid);
10696
10697   /* See the comment about red zone and frame
10698      pointer usage in ix86_expand_prologue.  */
10699   if (frame_pointer_needed && frame.red_zone_size)
10700     emit_insn (gen_memory_blockage ());
10701
10702   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
10703   gcc_assert (!using_drap || m->fs.cfa_reg == crtl->drap_reg);
10704
10705   /* Determine the CFA offset of the end of the red-zone.  */
10706   m->fs.red_zone_offset = 0;
10707   if (ix86_using_red_zone () && crtl->args.pops_args < 65536)
10708     {
10709       /* The red-zone begins below the return address.  */
10710       m->fs.red_zone_offset = RED_ZONE_SIZE + UNITS_PER_WORD;
10711
10712       /* When the register save area is in the aligned portion of
10713          the stack, determine the maximum runtime displacement that
10714          matches up with the aligned frame.  */
10715       if (stack_realign_drap)
10716         m->fs.red_zone_offset -= (crtl->stack_alignment_needed / BITS_PER_UNIT
10717                                   + UNITS_PER_WORD);
10718     }
10719
10720   /* Special care must be taken for the normal return case of a function
10721      using eh_return: the eax and edx registers are marked as saved, but
10722      not restored along this path.  Adjust the save location to match.  */
10723   if (crtl->calls_eh_return && style != 2)
10724     frame.reg_save_offset -= 2 * UNITS_PER_WORD;
10725
10726   /* EH_RETURN requires the use of moves to function properly.  */
10727   if (crtl->calls_eh_return)
10728     restore_regs_via_mov = true;
10729   /* SEH requires the use of pops to identify the epilogue.  */
10730   else if (TARGET_SEH)
10731     restore_regs_via_mov = false;
10732   /* If we're only restoring one register and sp is not valid then
10733      using a move instruction to restore the register since it's
10734      less work than reloading sp and popping the register.  */
10735   else if (!m->fs.sp_valid && frame.nregs <= 1)
10736     restore_regs_via_mov = true;
10737   else if (TARGET_EPILOGUE_USING_MOVE
10738            && cfun->machine->use_fast_prologue_epilogue
10739            && (frame.nregs > 1
10740                || m->fs.sp_offset != frame.reg_save_offset))
10741     restore_regs_via_mov = true;
10742   else if (frame_pointer_needed
10743            && !frame.nregs
10744            && m->fs.sp_offset != frame.reg_save_offset)
10745     restore_regs_via_mov = true;
10746   else if (frame_pointer_needed
10747            && TARGET_USE_LEAVE
10748            && cfun->machine->use_fast_prologue_epilogue
10749            && frame.nregs == 1)
10750     restore_regs_via_mov = true;
10751   else
10752     restore_regs_via_mov = false;
10753
10754   if (restore_regs_via_mov || frame.nsseregs)
10755     {
10756       /* Ensure that the entire register save area is addressable via
10757          the stack pointer, if we will restore via sp.  */
10758       if (TARGET_64BIT
10759           && m->fs.sp_offset > 0x7fffffff
10760           && !(m->fs.fp_valid || m->fs.drap_valid)
10761           && (frame.nsseregs + frame.nregs) != 0)
10762         {
10763           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10764                                      GEN_INT (m->fs.sp_offset
10765                                               - frame.sse_reg_save_offset),
10766                                      style,
10767                                      m->fs.cfa_reg == stack_pointer_rtx);
10768         }
10769     }
10770
10771   /* If there are any SSE registers to restore, then we have to do it
10772      via moves, since there's obviously no pop for SSE regs.  */
10773   if (frame.nsseregs)
10774     ix86_emit_restore_sse_regs_using_mov (frame.sse_reg_save_offset,
10775                                           style == 2);
10776
10777   if (restore_regs_via_mov)
10778     {
10779       rtx t;
10780
10781       if (frame.nregs)
10782         ix86_emit_restore_regs_using_mov (frame.reg_save_offset, style == 2);
10783
10784       /* eh_return epilogues need %ecx added to the stack pointer.  */
10785       if (style == 2)
10786         {
10787           rtx insn, sa = EH_RETURN_STACKADJ_RTX;
10788
10789           /* Stack align doesn't work with eh_return.  */
10790           gcc_assert (!stack_realign_drap);
10791           /* Neither does regparm nested functions.  */
10792           gcc_assert (!ix86_static_chain_on_stack);
10793
10794           if (frame_pointer_needed)
10795             {
10796               t = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
10797               t = plus_constant (t, m->fs.fp_offset - UNITS_PER_WORD);
10798               emit_insn (gen_rtx_SET (VOIDmode, sa, t));
10799
10800               t = gen_frame_mem (Pmode, hard_frame_pointer_rtx);
10801               insn = emit_move_insn (hard_frame_pointer_rtx, t);
10802
10803               /* Note that we use SA as a temporary CFA, as the return
10804                  address is at the proper place relative to it.  We
10805                  pretend this happens at the FP restore insn because
10806                  prior to this insn the FP would be stored at the wrong
10807                  offset relative to SA, and after this insn we have no
10808                  other reasonable register to use for the CFA.  We don't
10809                  bother resetting the CFA to the SP for the duration of
10810                  the return insn.  */
10811               add_reg_note (insn, REG_CFA_DEF_CFA,
10812                             plus_constant (sa, UNITS_PER_WORD));
10813               ix86_add_queued_cfa_restore_notes (insn);
10814               add_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx);
10815               RTX_FRAME_RELATED_P (insn) = 1;
10816
10817               m->fs.cfa_reg = sa;
10818               m->fs.cfa_offset = UNITS_PER_WORD;
10819               m->fs.fp_valid = false;
10820
10821               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
10822                                          const0_rtx, style, false);
10823             }
10824           else
10825             {
10826               t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
10827               t = plus_constant (t, m->fs.sp_offset - UNITS_PER_WORD);
10828               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, t));
10829               ix86_add_queued_cfa_restore_notes (insn);
10830
10831               gcc_assert (m->fs.cfa_reg == stack_pointer_rtx);
10832               if (m->fs.cfa_offset != UNITS_PER_WORD)
10833                 {
10834                   m->fs.cfa_offset = UNITS_PER_WORD;
10835                   add_reg_note (insn, REG_CFA_DEF_CFA,
10836                                 plus_constant (stack_pointer_rtx,
10837                                                UNITS_PER_WORD));
10838                   RTX_FRAME_RELATED_P (insn) = 1;
10839                 }
10840             }
10841           m->fs.sp_offset = UNITS_PER_WORD;
10842           m->fs.sp_valid = true;
10843         }
10844     }
10845   else
10846     {
10847       /* SEH requires that the function end with (1) a stack adjustment
10848          if necessary, (2) a sequence of pops, and (3) a return or
10849          jump instruction.  Prevent insns from the function body from
10850          being scheduled into this sequence.  */
10851       if (TARGET_SEH)
10852         {
10853           /* Prevent a catch region from being adjacent to the standard
10854              epilogue sequence.  Unfortuantely crtl->uses_eh_lsda nor
10855              several other flags that would be interesting to test are
10856              not yet set up.  */
10857           if (flag_non_call_exceptions)
10858             emit_insn (gen_nops (const1_rtx));
10859           else
10860             emit_insn (gen_blockage ());
10861         }
10862
10863       /* First step is to deallocate the stack frame so that we can
10864          pop the registers.  */
10865       if (!m->fs.sp_valid)
10866         {
10867           pro_epilogue_adjust_stack (stack_pointer_rtx, hard_frame_pointer_rtx,
10868                                      GEN_INT (m->fs.fp_offset
10869                                               - frame.reg_save_offset),
10870                                      style, false);
10871         }
10872       else if (m->fs.sp_offset != frame.reg_save_offset)
10873         {
10874           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10875                                      GEN_INT (m->fs.sp_offset
10876                                               - frame.reg_save_offset),
10877                                      style,
10878                                      m->fs.cfa_reg == stack_pointer_rtx);
10879         }
10880
10881       ix86_emit_restore_regs_using_pop ();
10882     }
10883
10884   /* If we used a stack pointer and haven't already got rid of it,
10885      then do so now.  */
10886   if (m->fs.fp_valid)
10887     {
10888       /* If the stack pointer is valid and pointing at the frame
10889          pointer store address, then we only need a pop.  */
10890       if (m->fs.sp_valid && m->fs.sp_offset == frame.hfp_save_offset)
10891         ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10892       /* Leave results in shorter dependency chains on CPUs that are
10893          able to grok it fast.  */
10894       else if (TARGET_USE_LEAVE
10895                || optimize_function_for_size_p (cfun)
10896                || !cfun->machine->use_fast_prologue_epilogue)
10897         ix86_emit_leave ();
10898       else
10899         {
10900           pro_epilogue_adjust_stack (stack_pointer_rtx,
10901                                      hard_frame_pointer_rtx,
10902                                      const0_rtx, style, !using_drap);
10903           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10904         }
10905     }
10906
10907   if (using_drap)
10908     {
10909       int param_ptr_offset = UNITS_PER_WORD;
10910       rtx insn;
10911
10912       gcc_assert (stack_realign_drap);
10913
10914       if (ix86_static_chain_on_stack)
10915         param_ptr_offset += UNITS_PER_WORD;
10916       if (!call_used_regs[REGNO (crtl->drap_reg)])
10917         param_ptr_offset += UNITS_PER_WORD;
10918
10919       insn = emit_insn (gen_rtx_SET
10920                         (VOIDmode, stack_pointer_rtx,
10921                          gen_rtx_PLUS (Pmode,
10922                                        crtl->drap_reg,
10923                                        GEN_INT (-param_ptr_offset))));
10924       m->fs.cfa_reg = stack_pointer_rtx;
10925       m->fs.cfa_offset = param_ptr_offset;
10926       m->fs.sp_offset = param_ptr_offset;
10927       m->fs.realigned = false;
10928
10929       add_reg_note (insn, REG_CFA_DEF_CFA,
10930                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10931                                   GEN_INT (param_ptr_offset)));
10932       RTX_FRAME_RELATED_P (insn) = 1;
10933
10934       if (!call_used_regs[REGNO (crtl->drap_reg)])
10935         ix86_emit_restore_reg_using_pop (crtl->drap_reg);
10936     }
10937
10938   /* At this point the stack pointer must be valid, and we must have
10939      restored all of the registers.  We may not have deallocated the
10940      entire stack frame.  We've delayed this until now because it may
10941      be possible to merge the local stack deallocation with the
10942      deallocation forced by ix86_static_chain_on_stack.   */
10943   gcc_assert (m->fs.sp_valid);
10944   gcc_assert (!m->fs.fp_valid);
10945   gcc_assert (!m->fs.realigned);
10946   if (m->fs.sp_offset != UNITS_PER_WORD)
10947     {
10948       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10949                                  GEN_INT (m->fs.sp_offset - UNITS_PER_WORD),
10950                                  style, true);
10951     }
10952
10953   /* Sibcall epilogues don't want a return instruction.  */
10954   if (style == 0)
10955     {
10956       m->fs = frame_state_save;
10957       return;
10958     }
10959
10960   /* Emit vzeroupper if needed.  */
10961   if (TARGET_VZEROUPPER
10962       && cfun->machine->use_avx256_p
10963       && !cfun->machine->caller_return_avx256_p)
10964     {
10965       cfun->machine->use_vzeroupper_p = 1;
10966       emit_insn (gen_avx_vzeroupper (GEN_INT (call_no_avx256))); 
10967     }
10968
10969   if (crtl->args.pops_args && crtl->args.size)
10970     {
10971       rtx popc = GEN_INT (crtl->args.pops_args);
10972
10973       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
10974          address, do explicit add, and jump indirectly to the caller.  */
10975
10976       if (crtl->args.pops_args >= 65536)
10977         {
10978           rtx ecx = gen_rtx_REG (SImode, CX_REG);
10979           rtx insn;
10980
10981           /* There is no "pascal" calling convention in any 64bit ABI.  */
10982           gcc_assert (!TARGET_64BIT);
10983
10984           insn = emit_insn (gen_pop (ecx));
10985           m->fs.cfa_offset -= UNITS_PER_WORD;
10986           m->fs.sp_offset -= UNITS_PER_WORD;
10987
10988           add_reg_note (insn, REG_CFA_ADJUST_CFA,
10989                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
10990           add_reg_note (insn, REG_CFA_REGISTER,
10991                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
10992           RTX_FRAME_RELATED_P (insn) = 1;
10993
10994           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10995                                      popc, -1, true);
10996           emit_jump_insn (gen_return_indirect_internal (ecx));
10997         }
10998       else
10999         emit_jump_insn (gen_return_pop_internal (popc));
11000     }
11001   else
11002     emit_jump_insn (gen_return_internal ());
11003
11004   /* Restore the state back to the state from the prologue,
11005      so that it's correct for the next epilogue.  */
11006   m->fs = frame_state_save;
11007 }
11008
11009 /* Reset from the function's potential modifications.  */
11010
11011 static void
11012 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11013                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11014 {
11015   if (pic_offset_table_rtx)
11016     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
11017 #if TARGET_MACHO
11018   /* Mach-O doesn't support labels at the end of objects, so if
11019      it looks like we might want one, insert a NOP.  */
11020   {
11021     rtx insn = get_last_insn ();
11022     while (insn
11023            && NOTE_P (insn)
11024            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
11025       insn = PREV_INSN (insn);
11026     if (insn
11027         && (LABEL_P (insn)
11028             || (NOTE_P (insn)
11029                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
11030       fputs ("\tnop\n", file);
11031   }
11032 #endif
11033
11034 }
11035
11036 /* Return a scratch register to use in the split stack prologue.  The
11037    split stack prologue is used for -fsplit-stack.  It is the first
11038    instructions in the function, even before the regular prologue.
11039    The scratch register can be any caller-saved register which is not
11040    used for parameters or for the static chain.  */
11041
11042 static unsigned int
11043 split_stack_prologue_scratch_regno (void)
11044 {
11045   if (TARGET_64BIT)
11046     return R11_REG;
11047   else
11048     {
11049       bool is_fastcall;
11050       int regparm;
11051
11052       is_fastcall = (lookup_attribute ("fastcall",
11053                                        TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
11054                      != NULL);
11055       regparm = ix86_function_regparm (TREE_TYPE (cfun->decl), cfun->decl);
11056
11057       if (is_fastcall)
11058         {
11059           if (DECL_STATIC_CHAIN (cfun->decl))
11060             {
11061               sorry ("-fsplit-stack does not support fastcall with "
11062                      "nested function");
11063               return INVALID_REGNUM;
11064             }
11065           return AX_REG;
11066         }
11067       else if (regparm < 3)
11068         {
11069           if (!DECL_STATIC_CHAIN (cfun->decl))
11070             return CX_REG;
11071           else
11072             {
11073               if (regparm >= 2)
11074                 {
11075                   sorry ("-fsplit-stack does not support 2 register "
11076                          " parameters for a nested function");
11077                   return INVALID_REGNUM;
11078                 }
11079               return DX_REG;
11080             }
11081         }
11082       else
11083         {
11084           /* FIXME: We could make this work by pushing a register
11085              around the addition and comparison.  */
11086           sorry ("-fsplit-stack does not support 3 register parameters");
11087           return INVALID_REGNUM;
11088         }
11089     }
11090 }
11091
11092 /* A SYMBOL_REF for the function which allocates new stackspace for
11093    -fsplit-stack.  */
11094
11095 static GTY(()) rtx split_stack_fn;
11096
11097 /* Handle -fsplit-stack.  These are the first instructions in the
11098    function, even before the regular prologue.  */
11099
11100 void
11101 ix86_expand_split_stack_prologue (void)
11102 {
11103   struct ix86_frame frame;
11104   HOST_WIDE_INT allocate;
11105   int args_size;
11106   rtx label, limit, current, jump_insn, allocate_rtx, call_insn, call_fusage;
11107   rtx scratch_reg = NULL_RTX;
11108   rtx varargs_label = NULL_RTX;
11109
11110   gcc_assert (flag_split_stack && reload_completed);
11111
11112   ix86_finalize_stack_realign_flags ();
11113   ix86_compute_frame_layout (&frame);
11114   allocate = frame.stack_pointer_offset - INCOMING_FRAME_SP_OFFSET;
11115
11116   /* This is the label we will branch to if we have enough stack
11117      space.  We expect the basic block reordering pass to reverse this
11118      branch if optimizing, so that we branch in the unlikely case.  */
11119   label = gen_label_rtx ();
11120
11121   /* We need to compare the stack pointer minus the frame size with
11122      the stack boundary in the TCB.  The stack boundary always gives
11123      us SPLIT_STACK_AVAILABLE bytes, so if we need less than that we
11124      can compare directly.  Otherwise we need to do an addition.  */
11125
11126   limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
11127                           UNSPEC_STACK_CHECK);
11128   limit = gen_rtx_CONST (Pmode, limit);
11129   limit = gen_rtx_MEM (Pmode, limit);
11130   if (allocate < SPLIT_STACK_AVAILABLE)
11131     current = stack_pointer_rtx;
11132   else
11133     {
11134       unsigned int scratch_regno;
11135       rtx offset;
11136
11137       /* We need a scratch register to hold the stack pointer minus
11138          the required frame size.  Since this is the very start of the
11139          function, the scratch register can be any caller-saved
11140          register which is not used for parameters.  */
11141       offset = GEN_INT (- allocate);
11142       scratch_regno = split_stack_prologue_scratch_regno ();
11143       if (scratch_regno == INVALID_REGNUM)
11144         return;
11145       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11146       if (!TARGET_64BIT || x86_64_immediate_operand (offset, Pmode))
11147         {
11148           /* We don't use ix86_gen_add3 in this case because it will
11149              want to split to lea, but when not optimizing the insn
11150              will not be split after this point.  */
11151           emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11152                                   gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11153                                                 offset)));
11154         }
11155       else
11156         {
11157           emit_move_insn (scratch_reg, offset);
11158           emit_insn (gen_adddi3 (scratch_reg, scratch_reg,
11159                                  stack_pointer_rtx));
11160         }
11161       current = scratch_reg;
11162     }
11163
11164   ix86_expand_branch (GEU, current, limit, label);
11165   jump_insn = get_last_insn ();
11166   JUMP_LABEL (jump_insn) = label;
11167
11168   /* Mark the jump as very likely to be taken.  */
11169   add_reg_note (jump_insn, REG_BR_PROB,
11170                 GEN_INT (REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100));
11171
11172   /* Get more stack space.  We pass in the desired stack space and the
11173      size of the arguments to copy to the new stack.  In 32-bit mode
11174      we push the parameters; __morestack will return on a new stack
11175      anyhow.  In 64-bit mode we pass the parameters in r10 and
11176      r11.  */
11177   allocate_rtx = GEN_INT (allocate);
11178   args_size = crtl->args.size >= 0 ? crtl->args.size : 0;
11179   call_fusage = NULL_RTX;
11180   if (TARGET_64BIT)
11181     {
11182       rtx reg;
11183
11184       reg = gen_rtx_REG (Pmode, R10_REG);
11185
11186       /* If this function uses a static chain, it will be in %r10.
11187          Preserve it across the call to __morestack.  */
11188       if (DECL_STATIC_CHAIN (cfun->decl))
11189         {
11190           rtx rax;
11191
11192           rax = gen_rtx_REG (Pmode, AX_REG);
11193           emit_move_insn (rax, reg);
11194           use_reg (&call_fusage, rax);
11195         }
11196
11197       emit_move_insn (reg, allocate_rtx);
11198       use_reg (&call_fusage, reg);
11199       reg = gen_rtx_REG (Pmode, R11_REG);
11200       emit_move_insn (reg, GEN_INT (args_size));
11201       use_reg (&call_fusage, reg);
11202     }
11203   else
11204     {
11205       emit_insn (gen_push (GEN_INT (args_size)));
11206       emit_insn (gen_push (allocate_rtx));
11207     }
11208   if (split_stack_fn == NULL_RTX)
11209     split_stack_fn = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
11210   call_insn = ix86_expand_call (NULL_RTX, gen_rtx_MEM (QImode, split_stack_fn),
11211                                 GEN_INT (UNITS_PER_WORD), constm1_rtx,
11212                                 NULL_RTX, 0);
11213   add_function_usage_to (call_insn, call_fusage);
11214
11215   /* In order to make call/return prediction work right, we now need
11216      to execute a return instruction.  See
11217      libgcc/config/i386/morestack.S for the details on how this works.
11218
11219      For flow purposes gcc must not see this as a return
11220      instruction--we need control flow to continue at the subsequent
11221      label.  Therefore, we use an unspec.  */
11222   gcc_assert (crtl->args.pops_args < 65536);
11223   emit_insn (gen_split_stack_return (GEN_INT (crtl->args.pops_args)));
11224
11225   /* If we are in 64-bit mode and this function uses a static chain,
11226      we saved %r10 in %rax before calling _morestack.  */
11227   if (TARGET_64BIT && DECL_STATIC_CHAIN (cfun->decl))
11228     emit_move_insn (gen_rtx_REG (Pmode, R10_REG),
11229                     gen_rtx_REG (Pmode, AX_REG));
11230
11231   /* If this function calls va_start, we need to store a pointer to
11232      the arguments on the old stack, because they may not have been
11233      all copied to the new stack.  At this point the old stack can be
11234      found at the frame pointer value used by __morestack, because
11235      __morestack has set that up before calling back to us.  Here we
11236      store that pointer in a scratch register, and in
11237      ix86_expand_prologue we store the scratch register in a stack
11238      slot.  */
11239   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11240     {
11241       unsigned int scratch_regno;
11242       rtx frame_reg;
11243       int words;
11244
11245       scratch_regno = split_stack_prologue_scratch_regno ();
11246       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
11247       frame_reg = gen_rtx_REG (Pmode, BP_REG);
11248
11249       /* 64-bit:
11250          fp -> old fp value
11251                return address within this function
11252                return address of caller of this function
11253                stack arguments
11254          So we add three words to get to the stack arguments.
11255
11256          32-bit:
11257          fp -> old fp value
11258                return address within this function
11259                first argument to __morestack
11260                second argument to __morestack
11261                return address of caller of this function
11262                stack arguments
11263          So we add five words to get to the stack arguments.
11264       */
11265       words = TARGET_64BIT ? 3 : 5;
11266       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11267                               gen_rtx_PLUS (Pmode, frame_reg,
11268                                             GEN_INT (words * UNITS_PER_WORD))));
11269
11270       varargs_label = gen_label_rtx ();
11271       emit_jump_insn (gen_jump (varargs_label));
11272       JUMP_LABEL (get_last_insn ()) = varargs_label;
11273
11274       emit_barrier ();
11275     }
11276
11277   emit_label (label);
11278   LABEL_NUSES (label) = 1;
11279
11280   /* If this function calls va_start, we now have to set the scratch
11281      register for the case where we do not call __morestack.  In this
11282      case we need to set it based on the stack pointer.  */
11283   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11284     {
11285       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
11286                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
11287                                             GEN_INT (UNITS_PER_WORD))));
11288
11289       emit_label (varargs_label);
11290       LABEL_NUSES (varargs_label) = 1;
11291     }
11292 }
11293
11294 /* We may have to tell the dataflow pass that the split stack prologue
11295    is initializing a scratch register.  */
11296
11297 static void
11298 ix86_live_on_entry (bitmap regs)
11299 {
11300   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
11301     {
11302       gcc_assert (flag_split_stack);
11303       bitmap_set_bit (regs, split_stack_prologue_scratch_regno ());
11304     }
11305 }
11306 \f
11307 /* Extract the parts of an RTL expression that is a valid memory address
11308    for an instruction.  Return 0 if the structure of the address is
11309    grossly off.  Return -1 if the address contains ASHIFT, so it is not
11310    strictly valid, but still used for computing length of lea instruction.  */
11311
11312 int
11313 ix86_decompose_address (rtx addr, struct ix86_address *out)
11314 {
11315   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
11316   rtx base_reg, index_reg;
11317   HOST_WIDE_INT scale = 1;
11318   rtx scale_rtx = NULL_RTX;
11319   rtx tmp;
11320   int retval = 1;
11321   enum ix86_address_seg seg = SEG_DEFAULT;
11322
11323   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
11324     base = addr;
11325   else if (GET_CODE (addr) == PLUS)
11326     {
11327       rtx addends[4], op;
11328       int n = 0, i;
11329
11330       op = addr;
11331       do
11332         {
11333           if (n >= 4)
11334             return 0;
11335           addends[n++] = XEXP (op, 1);
11336           op = XEXP (op, 0);
11337         }
11338       while (GET_CODE (op) == PLUS);
11339       if (n >= 4)
11340         return 0;
11341       addends[n] = op;
11342
11343       for (i = n; i >= 0; --i)
11344         {
11345           op = addends[i];
11346           switch (GET_CODE (op))
11347             {
11348             case MULT:
11349               if (index)
11350                 return 0;
11351               index = XEXP (op, 0);
11352               scale_rtx = XEXP (op, 1);
11353               break;
11354
11355             case ASHIFT:
11356               if (index)
11357                 return 0;
11358               index = XEXP (op, 0);
11359               tmp = XEXP (op, 1);
11360               if (!CONST_INT_P (tmp))
11361                 return 0;
11362               scale = INTVAL (tmp);
11363               if ((unsigned HOST_WIDE_INT) scale > 3)
11364                 return 0;
11365               scale = 1 << scale;
11366               break;
11367
11368             case UNSPEC:
11369               if (XINT (op, 1) == UNSPEC_TP
11370                   && TARGET_TLS_DIRECT_SEG_REFS
11371                   && seg == SEG_DEFAULT)
11372                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
11373               else
11374                 return 0;
11375               break;
11376
11377             case REG:
11378             case SUBREG:
11379               if (!base)
11380                 base = op;
11381               else if (!index)
11382                 index = op;
11383               else
11384                 return 0;
11385               break;
11386
11387             case CONST:
11388             case CONST_INT:
11389             case SYMBOL_REF:
11390             case LABEL_REF:
11391               if (disp)
11392                 return 0;
11393               disp = op;
11394               break;
11395
11396             default:
11397               return 0;
11398             }
11399         }
11400     }
11401   else if (GET_CODE (addr) == MULT)
11402     {
11403       index = XEXP (addr, 0);           /* index*scale */
11404       scale_rtx = XEXP (addr, 1);
11405     }
11406   else if (GET_CODE (addr) == ASHIFT)
11407     {
11408       /* We're called for lea too, which implements ashift on occasion.  */
11409       index = XEXP (addr, 0);
11410       tmp = XEXP (addr, 1);
11411       if (!CONST_INT_P (tmp))
11412         return 0;
11413       scale = INTVAL (tmp);
11414       if ((unsigned HOST_WIDE_INT) scale > 3)
11415         return 0;
11416       scale = 1 << scale;
11417       retval = -1;
11418     }
11419   else
11420     disp = addr;                        /* displacement */
11421
11422   /* Extract the integral value of scale.  */
11423   if (scale_rtx)
11424     {
11425       if (!CONST_INT_P (scale_rtx))
11426         return 0;
11427       scale = INTVAL (scale_rtx);
11428     }
11429
11430   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
11431   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
11432
11433   /* Avoid useless 0 displacement.  */
11434   if (disp == const0_rtx && (base || index))
11435     disp = NULL_RTX;
11436
11437   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
11438   if (base_reg && index_reg && scale == 1
11439       && (index_reg == arg_pointer_rtx
11440           || index_reg == frame_pointer_rtx
11441           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
11442     {
11443       rtx tmp;
11444       tmp = base, base = index, index = tmp;
11445       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
11446     }
11447
11448   /* Special case: %ebp cannot be encoded as a base without a displacement.
11449      Similarly %r13.  */
11450   if (!disp
11451       && base_reg
11452       && (base_reg == hard_frame_pointer_rtx
11453           || base_reg == frame_pointer_rtx
11454           || base_reg == arg_pointer_rtx
11455           || (REG_P (base_reg)
11456               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
11457                   || REGNO (base_reg) == R13_REG))))
11458     disp = const0_rtx;
11459
11460   /* Special case: on K6, [%esi] makes the instruction vector decoded.
11461      Avoid this by transforming to [%esi+0].
11462      Reload calls address legitimization without cfun defined, so we need
11463      to test cfun for being non-NULL. */
11464   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
11465       && base_reg && !index_reg && !disp
11466       && REG_P (base_reg) && REGNO (base_reg) == SI_REG)
11467     disp = const0_rtx;
11468
11469   /* Special case: encode reg+reg instead of reg*2.  */
11470   if (!base && index && scale == 2)
11471     base = index, base_reg = index_reg, scale = 1;
11472
11473   /* Special case: scaling cannot be encoded without base or displacement.  */
11474   if (!base && !disp && index && scale != 1)
11475     disp = const0_rtx;
11476
11477   out->base = base;
11478   out->index = index;
11479   out->disp = disp;
11480   out->scale = scale;
11481   out->seg = seg;
11482
11483   return retval;
11484 }
11485 \f
11486 /* Return cost of the memory address x.
11487    For i386, it is better to use a complex address than let gcc copy
11488    the address into a reg and make a new pseudo.  But not if the address
11489    requires to two regs - that would mean more pseudos with longer
11490    lifetimes.  */
11491 static int
11492 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
11493 {
11494   struct ix86_address parts;
11495   int cost = 1;
11496   int ok = ix86_decompose_address (x, &parts);
11497
11498   gcc_assert (ok);
11499
11500   if (parts.base && GET_CODE (parts.base) == SUBREG)
11501     parts.base = SUBREG_REG (parts.base);
11502   if (parts.index && GET_CODE (parts.index) == SUBREG)
11503     parts.index = SUBREG_REG (parts.index);
11504
11505   /* Attempt to minimize number of registers in the address.  */
11506   if ((parts.base
11507        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
11508       || (parts.index
11509           && (!REG_P (parts.index)
11510               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
11511     cost++;
11512
11513   if (parts.base
11514       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
11515       && parts.index
11516       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
11517       && parts.base != parts.index)
11518     cost++;
11519
11520   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
11521      since it's predecode logic can't detect the length of instructions
11522      and it degenerates to vector decoded.  Increase cost of such
11523      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
11524      to split such addresses or even refuse such addresses at all.
11525
11526      Following addressing modes are affected:
11527       [base+scale*index]
11528       [scale*index+disp]
11529       [base+index]
11530
11531      The first and last case  may be avoidable by explicitly coding the zero in
11532      memory address, but I don't have AMD-K6 machine handy to check this
11533      theory.  */
11534
11535   if (TARGET_K6
11536       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
11537           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
11538           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
11539     cost += 10;
11540
11541   return cost;
11542 }
11543 \f
11544 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
11545    this is used for to form addresses to local data when -fPIC is in
11546    use.  */
11547
11548 static bool
11549 darwin_local_data_pic (rtx disp)
11550 {
11551   return (GET_CODE (disp) == UNSPEC
11552           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
11553 }
11554
11555 /* Determine if a given RTX is a valid constant.  We already know this
11556    satisfies CONSTANT_P.  */
11557
11558 bool
11559 legitimate_constant_p (rtx x)
11560 {
11561   switch (GET_CODE (x))
11562     {
11563     case CONST:
11564       x = XEXP (x, 0);
11565
11566       if (GET_CODE (x) == PLUS)
11567         {
11568           if (!CONST_INT_P (XEXP (x, 1)))
11569             return false;
11570           x = XEXP (x, 0);
11571         }
11572
11573       if (TARGET_MACHO && darwin_local_data_pic (x))
11574         return true;
11575
11576       /* Only some unspecs are valid as "constants".  */
11577       if (GET_CODE (x) == UNSPEC)
11578         switch (XINT (x, 1))
11579           {
11580           case UNSPEC_GOT:
11581           case UNSPEC_GOTOFF:
11582           case UNSPEC_PLTOFF:
11583             return TARGET_64BIT;
11584           case UNSPEC_TPOFF:
11585           case UNSPEC_NTPOFF:
11586             x = XVECEXP (x, 0, 0);
11587             return (GET_CODE (x) == SYMBOL_REF
11588                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11589           case UNSPEC_DTPOFF:
11590             x = XVECEXP (x, 0, 0);
11591             return (GET_CODE (x) == SYMBOL_REF
11592                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
11593           default:
11594             return false;
11595           }
11596
11597       /* We must have drilled down to a symbol.  */
11598       if (GET_CODE (x) == LABEL_REF)
11599         return true;
11600       if (GET_CODE (x) != SYMBOL_REF)
11601         return false;
11602       /* FALLTHRU */
11603
11604     case SYMBOL_REF:
11605       /* TLS symbols are never valid.  */
11606       if (SYMBOL_REF_TLS_MODEL (x))
11607         return false;
11608
11609       /* DLLIMPORT symbols are never valid.  */
11610       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
11611           && SYMBOL_REF_DLLIMPORT_P (x))
11612         return false;
11613
11614 #if TARGET_MACHO
11615       /* mdynamic-no-pic */
11616       if (MACHO_DYNAMIC_NO_PIC_P)
11617         return machopic_symbol_defined_p (x);
11618 #endif
11619       break;
11620
11621     case CONST_DOUBLE:
11622       if (GET_MODE (x) == TImode
11623           && x != CONST0_RTX (TImode)
11624           && !TARGET_64BIT)
11625         return false;
11626       break;
11627
11628     case CONST_VECTOR:
11629       if (!standard_sse_constant_p (x))
11630         return false;
11631
11632     default:
11633       break;
11634     }
11635
11636   /* Otherwise we handle everything else in the move patterns.  */
11637   return true;
11638 }
11639
11640 /* Determine if it's legal to put X into the constant pool.  This
11641    is not possible for the address of thread-local symbols, which
11642    is checked above.  */
11643
11644 static bool
11645 ix86_cannot_force_const_mem (rtx x)
11646 {
11647   /* We can always put integral constants and vectors in memory.  */
11648   switch (GET_CODE (x))
11649     {
11650     case CONST_INT:
11651     case CONST_DOUBLE:
11652     case CONST_VECTOR:
11653       return false;
11654
11655     default:
11656       break;
11657     }
11658   return !legitimate_constant_p (x);
11659 }
11660
11661
11662 /* Nonzero if the constant value X is a legitimate general operand
11663    when generating PIC code.  It is given that flag_pic is on and
11664    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
11665
11666 bool
11667 legitimate_pic_operand_p (rtx x)
11668 {
11669   rtx inner;
11670
11671   switch (GET_CODE (x))
11672     {
11673     case CONST:
11674       inner = XEXP (x, 0);
11675       if (GET_CODE (inner) == PLUS
11676           && CONST_INT_P (XEXP (inner, 1)))
11677         inner = XEXP (inner, 0);
11678
11679       /* Only some unspecs are valid as "constants".  */
11680       if (GET_CODE (inner) == UNSPEC)
11681         switch (XINT (inner, 1))
11682           {
11683           case UNSPEC_GOT:
11684           case UNSPEC_GOTOFF:
11685           case UNSPEC_PLTOFF:
11686             return TARGET_64BIT;
11687           case UNSPEC_TPOFF:
11688             x = XVECEXP (inner, 0, 0);
11689             return (GET_CODE (x) == SYMBOL_REF
11690                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11691           case UNSPEC_MACHOPIC_OFFSET:
11692             return legitimate_pic_address_disp_p (x);
11693           default:
11694             return false;
11695           }
11696       /* FALLTHRU */
11697
11698     case SYMBOL_REF:
11699     case LABEL_REF:
11700       return legitimate_pic_address_disp_p (x);
11701
11702     default:
11703       return true;
11704     }
11705 }
11706
11707 /* Determine if a given CONST RTX is a valid memory displacement
11708    in PIC mode.  */
11709
11710 bool
11711 legitimate_pic_address_disp_p (rtx disp)
11712 {
11713   bool saw_plus;
11714
11715   /* In 64bit mode we can allow direct addresses of symbols and labels
11716      when they are not dynamic symbols.  */
11717   if (TARGET_64BIT)
11718     {
11719       rtx op0 = disp, op1;
11720
11721       switch (GET_CODE (disp))
11722         {
11723         case LABEL_REF:
11724           return true;
11725
11726         case CONST:
11727           if (GET_CODE (XEXP (disp, 0)) != PLUS)
11728             break;
11729           op0 = XEXP (XEXP (disp, 0), 0);
11730           op1 = XEXP (XEXP (disp, 0), 1);
11731           if (!CONST_INT_P (op1)
11732               || INTVAL (op1) >= 16*1024*1024
11733               || INTVAL (op1) < -16*1024*1024)
11734             break;
11735           if (GET_CODE (op0) == LABEL_REF)
11736             return true;
11737           if (GET_CODE (op0) != SYMBOL_REF)
11738             break;
11739           /* FALLTHRU */
11740
11741         case SYMBOL_REF:
11742           /* TLS references should always be enclosed in UNSPEC.  */
11743           if (SYMBOL_REF_TLS_MODEL (op0))
11744             return false;
11745           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
11746               && ix86_cmodel != CM_LARGE_PIC)
11747             return true;
11748           break;
11749
11750         default:
11751           break;
11752         }
11753     }
11754   if (GET_CODE (disp) != CONST)
11755     return false;
11756   disp = XEXP (disp, 0);
11757
11758   if (TARGET_64BIT)
11759     {
11760       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
11761          of GOT tables.  We should not need these anyway.  */
11762       if (GET_CODE (disp) != UNSPEC
11763           || (XINT (disp, 1) != UNSPEC_GOTPCREL
11764               && XINT (disp, 1) != UNSPEC_GOTOFF
11765               && XINT (disp, 1) != UNSPEC_PLTOFF))
11766         return false;
11767
11768       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
11769           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
11770         return false;
11771       return true;
11772     }
11773
11774   saw_plus = false;
11775   if (GET_CODE (disp) == PLUS)
11776     {
11777       if (!CONST_INT_P (XEXP (disp, 1)))
11778         return false;
11779       disp = XEXP (disp, 0);
11780       saw_plus = true;
11781     }
11782
11783   if (TARGET_MACHO && darwin_local_data_pic (disp))
11784     return true;
11785
11786   if (GET_CODE (disp) != UNSPEC)
11787     return false;
11788
11789   switch (XINT (disp, 1))
11790     {
11791     case UNSPEC_GOT:
11792       if (saw_plus)
11793         return false;
11794       /* We need to check for both symbols and labels because VxWorks loads
11795          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
11796          details.  */
11797       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11798               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
11799     case UNSPEC_GOTOFF:
11800       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
11801          While ABI specify also 32bit relocation but we don't produce it in
11802          small PIC model at all.  */
11803       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11804            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
11805           && !TARGET_64BIT)
11806         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
11807       return false;
11808     case UNSPEC_GOTTPOFF:
11809     case UNSPEC_GOTNTPOFF:
11810     case UNSPEC_INDNTPOFF:
11811       if (saw_plus)
11812         return false;
11813       disp = XVECEXP (disp, 0, 0);
11814       return (GET_CODE (disp) == SYMBOL_REF
11815               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
11816     case UNSPEC_NTPOFF:
11817       disp = XVECEXP (disp, 0, 0);
11818       return (GET_CODE (disp) == SYMBOL_REF
11819               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
11820     case UNSPEC_DTPOFF:
11821       disp = XVECEXP (disp, 0, 0);
11822       return (GET_CODE (disp) == SYMBOL_REF
11823               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
11824     }
11825
11826   return false;
11827 }
11828
11829 /* Recognizes RTL expressions that are valid memory addresses for an
11830    instruction.  The MODE argument is the machine mode for the MEM
11831    expression that wants to use this address.
11832
11833    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
11834    convert common non-canonical forms to canonical form so that they will
11835    be recognized.  */
11836
11837 static bool
11838 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
11839                            rtx addr, bool strict)
11840 {
11841   struct ix86_address parts;
11842   rtx base, index, disp;
11843   HOST_WIDE_INT scale;
11844
11845   if (ix86_decompose_address (addr, &parts) <= 0)
11846     /* Decomposition failed.  */
11847     return false;
11848
11849   base = parts.base;
11850   index = parts.index;
11851   disp = parts.disp;
11852   scale = parts.scale;
11853
11854   /* Validate base register.
11855
11856      Don't allow SUBREG's that span more than a word here.  It can lead to spill
11857      failures when the base is one word out of a two word structure, which is
11858      represented internally as a DImode int.  */
11859
11860   if (base)
11861     {
11862       rtx reg;
11863
11864       if (REG_P (base))
11865         reg = base;
11866       else if (GET_CODE (base) == SUBREG
11867                && REG_P (SUBREG_REG (base))
11868                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
11869                   <= UNITS_PER_WORD)
11870         reg = SUBREG_REG (base);
11871       else
11872         /* Base is not a register.  */
11873         return false;
11874
11875       if (GET_MODE (base) != Pmode)
11876         /* Base is not in Pmode.  */
11877         return false;
11878
11879       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
11880           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
11881         /* Base is not valid.  */
11882         return false;
11883     }
11884
11885   /* Validate index register.
11886
11887      Don't allow SUBREG's that span more than a word here -- same as above.  */
11888
11889   if (index)
11890     {
11891       rtx reg;
11892
11893       if (REG_P (index))
11894         reg = index;
11895       else if (GET_CODE (index) == SUBREG
11896                && REG_P (SUBREG_REG (index))
11897                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
11898                   <= UNITS_PER_WORD)
11899         reg = SUBREG_REG (index);
11900       else
11901         /* Index is not a register.  */
11902         return false;
11903
11904       if (GET_MODE (index) != Pmode)
11905         /* Index is not in Pmode.  */
11906         return false;
11907
11908       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
11909           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
11910         /* Index is not valid.  */
11911         return false;
11912     }
11913
11914   /* Validate scale factor.  */
11915   if (scale != 1)
11916     {
11917       if (!index)
11918         /* Scale without index.  */
11919         return false;
11920
11921       if (scale != 2 && scale != 4 && scale != 8)
11922         /* Scale is not a valid multiplier.  */
11923         return false;
11924     }
11925
11926   /* Validate displacement.  */
11927   if (disp)
11928     {
11929       if (GET_CODE (disp) == CONST
11930           && GET_CODE (XEXP (disp, 0)) == UNSPEC
11931           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
11932         switch (XINT (XEXP (disp, 0), 1))
11933           {
11934           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
11935              used.  While ABI specify also 32bit relocations, we don't produce
11936              them at all and use IP relative instead.  */
11937           case UNSPEC_GOT:
11938           case UNSPEC_GOTOFF:
11939             gcc_assert (flag_pic);
11940             if (!TARGET_64BIT)
11941               goto is_legitimate_pic;
11942
11943             /* 64bit address unspec.  */
11944             return false;
11945
11946           case UNSPEC_GOTPCREL:
11947             gcc_assert (flag_pic);
11948             goto is_legitimate_pic;
11949
11950           case UNSPEC_GOTTPOFF:
11951           case UNSPEC_GOTNTPOFF:
11952           case UNSPEC_INDNTPOFF:
11953           case UNSPEC_NTPOFF:
11954           case UNSPEC_DTPOFF:
11955             break;
11956
11957           case UNSPEC_STACK_CHECK:
11958             gcc_assert (flag_split_stack);
11959             break;
11960
11961           default:
11962             /* Invalid address unspec.  */
11963             return false;
11964           }
11965
11966       else if (SYMBOLIC_CONST (disp)
11967                && (flag_pic
11968                    || (TARGET_MACHO
11969 #if TARGET_MACHO
11970                        && MACHOPIC_INDIRECT
11971                        && !machopic_operand_p (disp)
11972 #endif
11973                )))
11974         {
11975
11976         is_legitimate_pic:
11977           if (TARGET_64BIT && (index || base))
11978             {
11979               /* foo@dtpoff(%rX) is ok.  */
11980               if (GET_CODE (disp) != CONST
11981                   || GET_CODE (XEXP (disp, 0)) != PLUS
11982                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
11983                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
11984                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
11985                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
11986                 /* Non-constant pic memory reference.  */
11987                 return false;
11988             }
11989           else if ((!TARGET_MACHO || flag_pic)
11990                     && ! legitimate_pic_address_disp_p (disp))
11991             /* Displacement is an invalid pic construct.  */
11992             return false;
11993 #if TARGET_MACHO
11994           else if (MACHO_DYNAMIC_NO_PIC_P && !legitimate_constant_p (disp))
11995             /* displacment must be referenced via non_lazy_pointer */
11996             return false;
11997 #endif
11998
11999           /* This code used to verify that a symbolic pic displacement
12000              includes the pic_offset_table_rtx register.
12001
12002              While this is good idea, unfortunately these constructs may
12003              be created by "adds using lea" optimization for incorrect
12004              code like:
12005
12006              int a;
12007              int foo(int i)
12008                {
12009                  return *(&a+i);
12010                }
12011
12012              This code is nonsensical, but results in addressing
12013              GOT table with pic_offset_table_rtx base.  We can't
12014              just refuse it easily, since it gets matched by
12015              "addsi3" pattern, that later gets split to lea in the
12016              case output register differs from input.  While this
12017              can be handled by separate addsi pattern for this case
12018              that never results in lea, this seems to be easier and
12019              correct fix for crash to disable this test.  */
12020         }
12021       else if (GET_CODE (disp) != LABEL_REF
12022                && !CONST_INT_P (disp)
12023                && (GET_CODE (disp) != CONST
12024                    || !legitimate_constant_p (disp))
12025                && (GET_CODE (disp) != SYMBOL_REF
12026                    || !legitimate_constant_p (disp)))
12027         /* Displacement is not constant.  */
12028         return false;
12029       else if (TARGET_64BIT
12030                && !x86_64_immediate_operand (disp, VOIDmode))
12031         /* Displacement is out of range.  */
12032         return false;
12033     }
12034
12035   /* Everything looks valid.  */
12036   return true;
12037 }
12038
12039 /* Determine if a given RTX is a valid constant address.  */
12040
12041 bool
12042 constant_address_p (rtx x)
12043 {
12044   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
12045 }
12046 \f
12047 /* Return a unique alias set for the GOT.  */
12048
12049 static alias_set_type
12050 ix86_GOT_alias_set (void)
12051 {
12052   static alias_set_type set = -1;
12053   if (set == -1)
12054     set = new_alias_set ();
12055   return set;
12056 }
12057
12058 /* Return a legitimate reference for ORIG (an address) using the
12059    register REG.  If REG is 0, a new pseudo is generated.
12060
12061    There are two types of references that must be handled:
12062
12063    1. Global data references must load the address from the GOT, via
12064       the PIC reg.  An insn is emitted to do this load, and the reg is
12065       returned.
12066
12067    2. Static data references, constant pool addresses, and code labels
12068       compute the address as an offset from the GOT, whose base is in
12069       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
12070       differentiate them from global data objects.  The returned
12071       address is the PIC reg + an unspec constant.
12072
12073    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
12074    reg also appears in the address.  */
12075
12076 static rtx
12077 legitimize_pic_address (rtx orig, rtx reg)
12078 {
12079   rtx addr = orig;
12080   rtx new_rtx = orig;
12081   rtx base;
12082
12083 #if TARGET_MACHO
12084   if (TARGET_MACHO && !TARGET_64BIT)
12085     {
12086       if (reg == 0)
12087         reg = gen_reg_rtx (Pmode);
12088       /* Use the generic Mach-O PIC machinery.  */
12089       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
12090     }
12091 #endif
12092
12093   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
12094     new_rtx = addr;
12095   else if (TARGET_64BIT
12096            && ix86_cmodel != CM_SMALL_PIC
12097            && gotoff_operand (addr, Pmode))
12098     {
12099       rtx tmpreg;
12100       /* This symbol may be referenced via a displacement from the PIC
12101          base address (@GOTOFF).  */
12102
12103       if (reload_in_progress)
12104         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12105       if (GET_CODE (addr) == CONST)
12106         addr = XEXP (addr, 0);
12107       if (GET_CODE (addr) == PLUS)
12108           {
12109             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12110                                       UNSPEC_GOTOFF);
12111             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12112           }
12113         else
12114           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12115       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12116       if (!reg)
12117         tmpreg = gen_reg_rtx (Pmode);
12118       else
12119         tmpreg = reg;
12120       emit_move_insn (tmpreg, new_rtx);
12121
12122       if (reg != 0)
12123         {
12124           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
12125                                          tmpreg, 1, OPTAB_DIRECT);
12126           new_rtx = reg;
12127         }
12128       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
12129     }
12130   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
12131     {
12132       /* This symbol may be referenced via a displacement from the PIC
12133          base address (@GOTOFF).  */
12134
12135       if (reload_in_progress)
12136         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12137       if (GET_CODE (addr) == CONST)
12138         addr = XEXP (addr, 0);
12139       if (GET_CODE (addr) == PLUS)
12140           {
12141             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
12142                                       UNSPEC_GOTOFF);
12143             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
12144           }
12145         else
12146           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
12147       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12148       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12149
12150       if (reg != 0)
12151         {
12152           emit_move_insn (reg, new_rtx);
12153           new_rtx = reg;
12154         }
12155     }
12156   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
12157            /* We can't use @GOTOFF for text labels on VxWorks;
12158               see gotoff_operand.  */
12159            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
12160     {
12161       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12162         {
12163           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
12164             return legitimize_dllimport_symbol (addr, true);
12165           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
12166               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
12167               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
12168             {
12169               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
12170               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
12171             }
12172         }
12173
12174       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
12175         {
12176           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
12177           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12178           new_rtx = gen_const_mem (Pmode, new_rtx);
12179           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12180
12181           if (reg == 0)
12182             reg = gen_reg_rtx (Pmode);
12183           /* Use directly gen_movsi, otherwise the address is loaded
12184              into register for CSE.  We don't want to CSE this addresses,
12185              instead we CSE addresses from the GOT table, so skip this.  */
12186           emit_insn (gen_movsi (reg, new_rtx));
12187           new_rtx = reg;
12188         }
12189       else
12190         {
12191           /* This symbol must be referenced via a load from the
12192              Global Offset Table (@GOT).  */
12193
12194           if (reload_in_progress)
12195             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12196           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
12197           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12198           if (TARGET_64BIT)
12199             new_rtx = force_reg (Pmode, new_rtx);
12200           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12201           new_rtx = gen_const_mem (Pmode, new_rtx);
12202           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
12203
12204           if (reg == 0)
12205             reg = gen_reg_rtx (Pmode);
12206           emit_move_insn (reg, new_rtx);
12207           new_rtx = reg;
12208         }
12209     }
12210   else
12211     {
12212       if (CONST_INT_P (addr)
12213           && !x86_64_immediate_operand (addr, VOIDmode))
12214         {
12215           if (reg)
12216             {
12217               emit_move_insn (reg, addr);
12218               new_rtx = reg;
12219             }
12220           else
12221             new_rtx = force_reg (Pmode, addr);
12222         }
12223       else if (GET_CODE (addr) == CONST)
12224         {
12225           addr = XEXP (addr, 0);
12226
12227           /* We must match stuff we generate before.  Assume the only
12228              unspecs that can get here are ours.  Not that we could do
12229              anything with them anyway....  */
12230           if (GET_CODE (addr) == UNSPEC
12231               || (GET_CODE (addr) == PLUS
12232                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
12233             return orig;
12234           gcc_assert (GET_CODE (addr) == PLUS);
12235         }
12236       if (GET_CODE (addr) == PLUS)
12237         {
12238           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
12239
12240           /* Check first to see if this is a constant offset from a @GOTOFF
12241              symbol reference.  */
12242           if (gotoff_operand (op0, Pmode)
12243               && CONST_INT_P (op1))
12244             {
12245               if (!TARGET_64BIT)
12246                 {
12247                   if (reload_in_progress)
12248                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12249                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
12250                                             UNSPEC_GOTOFF);
12251                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
12252                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
12253                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
12254
12255                   if (reg != 0)
12256                     {
12257                       emit_move_insn (reg, new_rtx);
12258                       new_rtx = reg;
12259                     }
12260                 }
12261               else
12262                 {
12263                   if (INTVAL (op1) < -16*1024*1024
12264                       || INTVAL (op1) >= 16*1024*1024)
12265                     {
12266                       if (!x86_64_immediate_operand (op1, Pmode))
12267                         op1 = force_reg (Pmode, op1);
12268                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
12269                     }
12270                 }
12271             }
12272           else
12273             {
12274               base = legitimize_pic_address (XEXP (addr, 0), reg);
12275               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
12276                                                  base == reg ? NULL_RTX : reg);
12277
12278               if (CONST_INT_P (new_rtx))
12279                 new_rtx = plus_constant (base, INTVAL (new_rtx));
12280               else
12281                 {
12282                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
12283                     {
12284                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
12285                       new_rtx = XEXP (new_rtx, 1);
12286                     }
12287                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
12288                 }
12289             }
12290         }
12291     }
12292   return new_rtx;
12293 }
12294 \f
12295 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
12296
12297 static rtx
12298 get_thread_pointer (int to_reg)
12299 {
12300   rtx tp, reg, insn;
12301
12302   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
12303   if (!to_reg)
12304     return tp;
12305
12306   reg = gen_reg_rtx (Pmode);
12307   insn = gen_rtx_SET (VOIDmode, reg, tp);
12308   insn = emit_insn (insn);
12309
12310   return reg;
12311 }
12312
12313 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
12314    false if we expect this to be used for a memory address and true if
12315    we expect to load the address into a register.  */
12316
12317 static rtx
12318 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
12319 {
12320   rtx dest, base, off, pic, tp;
12321   int type;
12322
12323   switch (model)
12324     {
12325     case TLS_MODEL_GLOBAL_DYNAMIC:
12326       dest = gen_reg_rtx (Pmode);
12327       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
12328
12329       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
12330         {
12331           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
12332
12333           start_sequence ();
12334           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
12335           insns = get_insns ();
12336           end_sequence ();
12337
12338           RTL_CONST_CALL_P (insns) = 1;
12339           emit_libcall_block (insns, dest, rax, x);
12340         }
12341       else if (TARGET_64BIT && TARGET_GNU2_TLS)
12342         emit_insn (gen_tls_global_dynamic_64 (dest, x));
12343       else
12344         emit_insn (gen_tls_global_dynamic_32 (dest, x));
12345
12346       if (TARGET_GNU2_TLS)
12347         {
12348           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
12349
12350           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
12351         }
12352       break;
12353
12354     case TLS_MODEL_LOCAL_DYNAMIC:
12355       base = gen_reg_rtx (Pmode);
12356       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
12357
12358       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
12359         {
12360           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, note;
12361
12362           start_sequence ();
12363           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
12364           insns = get_insns ();
12365           end_sequence ();
12366
12367           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
12368           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
12369           RTL_CONST_CALL_P (insns) = 1;
12370           emit_libcall_block (insns, base, rax, note);
12371         }
12372       else if (TARGET_64BIT && TARGET_GNU2_TLS)
12373         emit_insn (gen_tls_local_dynamic_base_64 (base));
12374       else
12375         emit_insn (gen_tls_local_dynamic_base_32 (base));
12376
12377       if (TARGET_GNU2_TLS)
12378         {
12379           rtx x = ix86_tls_module_base ();
12380
12381           set_unique_reg_note (get_last_insn (), REG_EQUIV,
12382                                gen_rtx_MINUS (Pmode, x, tp));
12383         }
12384
12385       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
12386       off = gen_rtx_CONST (Pmode, off);
12387
12388       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
12389
12390       if (TARGET_GNU2_TLS)
12391         {
12392           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
12393
12394           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
12395         }
12396
12397       break;
12398
12399     case TLS_MODEL_INITIAL_EXEC:
12400       if (TARGET_64BIT)
12401         {
12402           pic = NULL;
12403           type = UNSPEC_GOTNTPOFF;
12404         }
12405       else if (flag_pic)
12406         {
12407           if (reload_in_progress)
12408             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
12409           pic = pic_offset_table_rtx;
12410           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
12411         }
12412       else if (!TARGET_ANY_GNU_TLS)
12413         {
12414           pic = gen_reg_rtx (Pmode);
12415           emit_insn (gen_set_got (pic));
12416           type = UNSPEC_GOTTPOFF;
12417         }
12418       else
12419         {
12420           pic = NULL;
12421           type = UNSPEC_INDNTPOFF;
12422         }
12423
12424       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
12425       off = gen_rtx_CONST (Pmode, off);
12426       if (pic)
12427         off = gen_rtx_PLUS (Pmode, pic, off);
12428       off = gen_const_mem (Pmode, off);
12429       set_mem_alias_set (off, ix86_GOT_alias_set ());
12430
12431       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12432         {
12433           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12434           off = force_reg (Pmode, off);
12435           return gen_rtx_PLUS (Pmode, base, off);
12436         }
12437       else
12438         {
12439           base = get_thread_pointer (true);
12440           dest = gen_reg_rtx (Pmode);
12441           emit_insn (gen_subsi3 (dest, base, off));
12442         }
12443       break;
12444
12445     case TLS_MODEL_LOCAL_EXEC:
12446       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
12447                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12448                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
12449       off = gen_rtx_CONST (Pmode, off);
12450
12451       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
12452         {
12453           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
12454           return gen_rtx_PLUS (Pmode, base, off);
12455         }
12456       else
12457         {
12458           base = get_thread_pointer (true);
12459           dest = gen_reg_rtx (Pmode);
12460           emit_insn (gen_subsi3 (dest, base, off));
12461         }
12462       break;
12463
12464     default:
12465       gcc_unreachable ();
12466     }
12467
12468   return dest;
12469 }
12470
12471 /* Create or return the unique __imp_DECL dllimport symbol corresponding
12472    to symbol DECL.  */
12473
12474 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
12475   htab_t dllimport_map;
12476
12477 static tree
12478 get_dllimport_decl (tree decl)
12479 {
12480   struct tree_map *h, in;
12481   void **loc;
12482   const char *name;
12483   const char *prefix;
12484   size_t namelen, prefixlen;
12485   char *imp_name;
12486   tree to;
12487   rtx rtl;
12488
12489   if (!dllimport_map)
12490     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
12491
12492   in.hash = htab_hash_pointer (decl);
12493   in.base.from = decl;
12494   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
12495   h = (struct tree_map *) *loc;
12496   if (h)
12497     return h->to;
12498
12499   *loc = h = ggc_alloc_tree_map ();
12500   h->hash = in.hash;
12501   h->base.from = decl;
12502   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
12503                            VAR_DECL, NULL, ptr_type_node);
12504   DECL_ARTIFICIAL (to) = 1;
12505   DECL_IGNORED_P (to) = 1;
12506   DECL_EXTERNAL (to) = 1;
12507   TREE_READONLY (to) = 1;
12508
12509   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
12510   name = targetm.strip_name_encoding (name);
12511   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
12512     ? "*__imp_" : "*__imp__";
12513   namelen = strlen (name);
12514   prefixlen = strlen (prefix);
12515   imp_name = (char *) alloca (namelen + prefixlen + 1);
12516   memcpy (imp_name, prefix, prefixlen);
12517   memcpy (imp_name + prefixlen, name, namelen + 1);
12518
12519   name = ggc_alloc_string (imp_name, namelen + prefixlen);
12520   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
12521   SET_SYMBOL_REF_DECL (rtl, to);
12522   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
12523
12524   rtl = gen_const_mem (Pmode, rtl);
12525   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
12526
12527   SET_DECL_RTL (to, rtl);
12528   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
12529
12530   return to;
12531 }
12532
12533 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
12534    true if we require the result be a register.  */
12535
12536 static rtx
12537 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
12538 {
12539   tree imp_decl;
12540   rtx x;
12541
12542   gcc_assert (SYMBOL_REF_DECL (symbol));
12543   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
12544
12545   x = DECL_RTL (imp_decl);
12546   if (want_reg)
12547     x = force_reg (Pmode, x);
12548   return x;
12549 }
12550
12551 /* Try machine-dependent ways of modifying an illegitimate address
12552    to be legitimate.  If we find one, return the new, valid address.
12553    This macro is used in only one place: `memory_address' in explow.c.
12554
12555    OLDX is the address as it was before break_out_memory_refs was called.
12556    In some cases it is useful to look at this to decide what needs to be done.
12557
12558    It is always safe for this macro to do nothing.  It exists to recognize
12559    opportunities to optimize the output.
12560
12561    For the 80386, we handle X+REG by loading X into a register R and
12562    using R+REG.  R will go in a general reg and indexing will be used.
12563    However, if REG is a broken-out memory address or multiplication,
12564    nothing needs to be done because REG can certainly go in a general reg.
12565
12566    When -fpic is used, special handling is needed for symbolic references.
12567    See comments by legitimize_pic_address in i386.c for details.  */
12568
12569 static rtx
12570 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
12571                          enum machine_mode mode)
12572 {
12573   int changed = 0;
12574   unsigned log;
12575
12576   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
12577   if (log)
12578     return legitimize_tls_address (x, (enum tls_model) log, false);
12579   if (GET_CODE (x) == CONST
12580       && GET_CODE (XEXP (x, 0)) == PLUS
12581       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12582       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
12583     {
12584       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
12585                                       (enum tls_model) log, false);
12586       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12587     }
12588
12589   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
12590     {
12591       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
12592         return legitimize_dllimport_symbol (x, true);
12593       if (GET_CODE (x) == CONST
12594           && GET_CODE (XEXP (x, 0)) == PLUS
12595           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
12596           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
12597         {
12598           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
12599           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
12600         }
12601     }
12602
12603   if (flag_pic && SYMBOLIC_CONST (x))
12604     return legitimize_pic_address (x, 0);
12605
12606 #if TARGET_MACHO
12607   if (MACHO_DYNAMIC_NO_PIC_P && SYMBOLIC_CONST (x))
12608     return machopic_indirect_data_reference (x, 0);
12609 #endif
12610
12611   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
12612   if (GET_CODE (x) == ASHIFT
12613       && CONST_INT_P (XEXP (x, 1))
12614       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
12615     {
12616       changed = 1;
12617       log = INTVAL (XEXP (x, 1));
12618       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
12619                         GEN_INT (1 << log));
12620     }
12621
12622   if (GET_CODE (x) == PLUS)
12623     {
12624       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
12625
12626       if (GET_CODE (XEXP (x, 0)) == ASHIFT
12627           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
12628           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
12629         {
12630           changed = 1;
12631           log = INTVAL (XEXP (XEXP (x, 0), 1));
12632           XEXP (x, 0) = gen_rtx_MULT (Pmode,
12633                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
12634                                       GEN_INT (1 << log));
12635         }
12636
12637       if (GET_CODE (XEXP (x, 1)) == ASHIFT
12638           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
12639           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
12640         {
12641           changed = 1;
12642           log = INTVAL (XEXP (XEXP (x, 1), 1));
12643           XEXP (x, 1) = gen_rtx_MULT (Pmode,
12644                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
12645                                       GEN_INT (1 << log));
12646         }
12647
12648       /* Put multiply first if it isn't already.  */
12649       if (GET_CODE (XEXP (x, 1)) == MULT)
12650         {
12651           rtx tmp = XEXP (x, 0);
12652           XEXP (x, 0) = XEXP (x, 1);
12653           XEXP (x, 1) = tmp;
12654           changed = 1;
12655         }
12656
12657       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
12658          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
12659          created by virtual register instantiation, register elimination, and
12660          similar optimizations.  */
12661       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
12662         {
12663           changed = 1;
12664           x = gen_rtx_PLUS (Pmode,
12665                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
12666                                           XEXP (XEXP (x, 1), 0)),
12667                             XEXP (XEXP (x, 1), 1));
12668         }
12669
12670       /* Canonicalize
12671          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
12672          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
12673       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
12674                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
12675                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
12676                && CONSTANT_P (XEXP (x, 1)))
12677         {
12678           rtx constant;
12679           rtx other = NULL_RTX;
12680
12681           if (CONST_INT_P (XEXP (x, 1)))
12682             {
12683               constant = XEXP (x, 1);
12684               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
12685             }
12686           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
12687             {
12688               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
12689               other = XEXP (x, 1);
12690             }
12691           else
12692             constant = 0;
12693
12694           if (constant)
12695             {
12696               changed = 1;
12697               x = gen_rtx_PLUS (Pmode,
12698                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
12699                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
12700                                 plus_constant (other, INTVAL (constant)));
12701             }
12702         }
12703
12704       if (changed && ix86_legitimate_address_p (mode, x, false))
12705         return x;
12706
12707       if (GET_CODE (XEXP (x, 0)) == MULT)
12708         {
12709           changed = 1;
12710           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
12711         }
12712
12713       if (GET_CODE (XEXP (x, 1)) == MULT)
12714         {
12715           changed = 1;
12716           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
12717         }
12718
12719       if (changed
12720           && REG_P (XEXP (x, 1))
12721           && REG_P (XEXP (x, 0)))
12722         return x;
12723
12724       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
12725         {
12726           changed = 1;
12727           x = legitimize_pic_address (x, 0);
12728         }
12729
12730       if (changed && ix86_legitimate_address_p (mode, x, false))
12731         return x;
12732
12733       if (REG_P (XEXP (x, 0)))
12734         {
12735           rtx temp = gen_reg_rtx (Pmode);
12736           rtx val  = force_operand (XEXP (x, 1), temp);
12737           if (val != temp)
12738             emit_move_insn (temp, val);
12739
12740           XEXP (x, 1) = temp;
12741           return x;
12742         }
12743
12744       else if (REG_P (XEXP (x, 1)))
12745         {
12746           rtx temp = gen_reg_rtx (Pmode);
12747           rtx val  = force_operand (XEXP (x, 0), temp);
12748           if (val != temp)
12749             emit_move_insn (temp, val);
12750
12751           XEXP (x, 0) = temp;
12752           return x;
12753         }
12754     }
12755
12756   return x;
12757 }
12758 \f
12759 /* Print an integer constant expression in assembler syntax.  Addition
12760    and subtraction are the only arithmetic that may appear in these
12761    expressions.  FILE is the stdio stream to write to, X is the rtx, and
12762    CODE is the operand print code from the output string.  */
12763
12764 static void
12765 output_pic_addr_const (FILE *file, rtx x, int code)
12766 {
12767   char buf[256];
12768
12769   switch (GET_CODE (x))
12770     {
12771     case PC:
12772       gcc_assert (flag_pic);
12773       putc ('.', file);
12774       break;
12775
12776     case SYMBOL_REF:
12777       if (TARGET_64BIT || ! TARGET_MACHO_BRANCH_ISLANDS)
12778         output_addr_const (file, x);
12779       else
12780         {
12781           const char *name = XSTR (x, 0);
12782
12783           /* Mark the decl as referenced so that cgraph will
12784              output the function.  */
12785           if (SYMBOL_REF_DECL (x))
12786             mark_decl_referenced (SYMBOL_REF_DECL (x));
12787
12788 #if TARGET_MACHO
12789           if (MACHOPIC_INDIRECT
12790               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
12791             name = machopic_indirection_name (x, /*stub_p=*/true);
12792 #endif
12793           assemble_name (file, name);
12794         }
12795       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
12796           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
12797         fputs ("@PLT", file);
12798       break;
12799
12800     case LABEL_REF:
12801       x = XEXP (x, 0);
12802       /* FALLTHRU */
12803     case CODE_LABEL:
12804       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
12805       assemble_name (asm_out_file, buf);
12806       break;
12807
12808     case CONST_INT:
12809       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12810       break;
12811
12812     case CONST:
12813       /* This used to output parentheses around the expression,
12814          but that does not work on the 386 (either ATT or BSD assembler).  */
12815       output_pic_addr_const (file, XEXP (x, 0), code);
12816       break;
12817
12818     case CONST_DOUBLE:
12819       if (GET_MODE (x) == VOIDmode)
12820         {
12821           /* We can use %d if the number is <32 bits and positive.  */
12822           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
12823             fprintf (file, "0x%lx%08lx",
12824                      (unsigned long) CONST_DOUBLE_HIGH (x),
12825                      (unsigned long) CONST_DOUBLE_LOW (x));
12826           else
12827             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
12828         }
12829       else
12830         /* We can't handle floating point constants;
12831            TARGET_PRINT_OPERAND must handle them.  */
12832         output_operand_lossage ("floating constant misused");
12833       break;
12834
12835     case PLUS:
12836       /* Some assemblers need integer constants to appear first.  */
12837       if (CONST_INT_P (XEXP (x, 0)))
12838         {
12839           output_pic_addr_const (file, XEXP (x, 0), code);
12840           putc ('+', file);
12841           output_pic_addr_const (file, XEXP (x, 1), code);
12842         }
12843       else
12844         {
12845           gcc_assert (CONST_INT_P (XEXP (x, 1)));
12846           output_pic_addr_const (file, XEXP (x, 1), code);
12847           putc ('+', file);
12848           output_pic_addr_const (file, XEXP (x, 0), code);
12849         }
12850       break;
12851
12852     case MINUS:
12853       if (!TARGET_MACHO)
12854         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
12855       output_pic_addr_const (file, XEXP (x, 0), code);
12856       putc ('-', file);
12857       output_pic_addr_const (file, XEXP (x, 1), code);
12858       if (!TARGET_MACHO)
12859         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
12860       break;
12861
12862      case UNSPEC:
12863        if (XINT (x, 1) == UNSPEC_STACK_CHECK)
12864          {
12865            bool f = i386_asm_output_addr_const_extra (file, x);
12866            gcc_assert (f);
12867            break;
12868          }
12869
12870        gcc_assert (XVECLEN (x, 0) == 1);
12871        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
12872        switch (XINT (x, 1))
12873         {
12874         case UNSPEC_GOT:
12875           fputs ("@GOT", file);
12876           break;
12877         case UNSPEC_GOTOFF:
12878           fputs ("@GOTOFF", file);
12879           break;
12880         case UNSPEC_PLTOFF:
12881           fputs ("@PLTOFF", file);
12882           break;
12883         case UNSPEC_GOTPCREL:
12884           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
12885                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
12886           break;
12887         case UNSPEC_GOTTPOFF:
12888           /* FIXME: This might be @TPOFF in Sun ld too.  */
12889           fputs ("@gottpoff", file);
12890           break;
12891         case UNSPEC_TPOFF:
12892           fputs ("@tpoff", file);
12893           break;
12894         case UNSPEC_NTPOFF:
12895           if (TARGET_64BIT)
12896             fputs ("@tpoff", file);
12897           else
12898             fputs ("@ntpoff", file);
12899           break;
12900         case UNSPEC_DTPOFF:
12901           fputs ("@dtpoff", file);
12902           break;
12903         case UNSPEC_GOTNTPOFF:
12904           if (TARGET_64BIT)
12905             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
12906                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
12907           else
12908             fputs ("@gotntpoff", file);
12909           break;
12910         case UNSPEC_INDNTPOFF:
12911           fputs ("@indntpoff", file);
12912           break;
12913 #if TARGET_MACHO
12914         case UNSPEC_MACHOPIC_OFFSET:
12915           putc ('-', file);
12916           machopic_output_function_base_name (file);
12917           break;
12918 #endif
12919         default:
12920           output_operand_lossage ("invalid UNSPEC as operand");
12921           break;
12922         }
12923        break;
12924
12925     default:
12926       output_operand_lossage ("invalid expression as operand");
12927     }
12928 }
12929
12930 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
12931    We need to emit DTP-relative relocations.  */
12932
12933 static void ATTRIBUTE_UNUSED
12934 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
12935 {
12936   fputs (ASM_LONG, file);
12937   output_addr_const (file, x);
12938   fputs ("@dtpoff", file);
12939   switch (size)
12940     {
12941     case 4:
12942       break;
12943     case 8:
12944       fputs (", 0", file);
12945       break;
12946     default:
12947       gcc_unreachable ();
12948    }
12949 }
12950
12951 /* Return true if X is a representation of the PIC register.  This copes
12952    with calls from ix86_find_base_term, where the register might have
12953    been replaced by a cselib value.  */
12954
12955 static bool
12956 ix86_pic_register_p (rtx x)
12957 {
12958   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
12959     return (pic_offset_table_rtx
12960             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
12961   else
12962     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
12963 }
12964
12965 /* Helper function for ix86_delegitimize_address.
12966    Attempt to delegitimize TLS local-exec accesses.  */
12967
12968 static rtx
12969 ix86_delegitimize_tls_address (rtx orig_x)
12970 {
12971   rtx x = orig_x, unspec;
12972   struct ix86_address addr;
12973
12974   if (!TARGET_TLS_DIRECT_SEG_REFS)
12975     return orig_x;
12976   if (MEM_P (x))
12977     x = XEXP (x, 0);
12978   if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
12979     return orig_x;
12980   if (ix86_decompose_address (x, &addr) == 0
12981       || addr.seg != (TARGET_64BIT ? SEG_FS : SEG_GS)
12982       || addr.disp == NULL_RTX
12983       || GET_CODE (addr.disp) != CONST)
12984     return orig_x;
12985   unspec = XEXP (addr.disp, 0);
12986   if (GET_CODE (unspec) == PLUS && CONST_INT_P (XEXP (unspec, 1)))
12987     unspec = XEXP (unspec, 0);
12988   if (GET_CODE (unspec) != UNSPEC || XINT (unspec, 1) != UNSPEC_NTPOFF)
12989     return orig_x;
12990   x = XVECEXP (unspec, 0, 0);
12991   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12992   if (unspec != XEXP (addr.disp, 0))
12993     x = gen_rtx_PLUS (Pmode, x, XEXP (XEXP (addr.disp, 0), 1));
12994   if (addr.index)
12995     {
12996       rtx idx = addr.index;
12997       if (addr.scale != 1)
12998         idx = gen_rtx_MULT (Pmode, idx, GEN_INT (addr.scale));
12999       x = gen_rtx_PLUS (Pmode, idx, x);
13000     }
13001   if (addr.base)
13002     x = gen_rtx_PLUS (Pmode, addr.base, x);
13003   if (MEM_P (orig_x))
13004     x = replace_equiv_address_nv (orig_x, x);
13005   return x;
13006 }
13007
13008 /* In the name of slightly smaller debug output, and to cater to
13009    general assembler lossage, recognize PIC+GOTOFF and turn it back
13010    into a direct symbol reference.
13011
13012    On Darwin, this is necessary to avoid a crash, because Darwin
13013    has a different PIC label for each routine but the DWARF debugging
13014    information is not associated with any particular routine, so it's
13015    necessary to remove references to the PIC label from RTL stored by
13016    the DWARF output code.  */
13017
13018 static rtx
13019 ix86_delegitimize_address (rtx x)
13020 {
13021   rtx orig_x = delegitimize_mem_from_attrs (x);
13022   /* addend is NULL or some rtx if x is something+GOTOFF where
13023      something doesn't include the PIC register.  */
13024   rtx addend = NULL_RTX;
13025   /* reg_addend is NULL or a multiple of some register.  */
13026   rtx reg_addend = NULL_RTX;
13027   /* const_addend is NULL or a const_int.  */
13028   rtx const_addend = NULL_RTX;
13029   /* This is the result, or NULL.  */
13030   rtx result = NULL_RTX;
13031
13032   x = orig_x;
13033
13034   if (MEM_P (x))
13035     x = XEXP (x, 0);
13036
13037   if (TARGET_64BIT)
13038     {
13039       if (GET_CODE (x) != CONST
13040           || GET_CODE (XEXP (x, 0)) != UNSPEC
13041           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
13042           || !MEM_P (orig_x))
13043         return ix86_delegitimize_tls_address (orig_x);
13044       x = XVECEXP (XEXP (x, 0), 0, 0);
13045       if (GET_MODE (orig_x) != Pmode)
13046         return simplify_gen_subreg (GET_MODE (orig_x), x, Pmode, 0);
13047       return x;
13048     }
13049
13050   if (GET_CODE (x) != PLUS
13051       || GET_CODE (XEXP (x, 1)) != CONST)
13052     return ix86_delegitimize_tls_address (orig_x);
13053
13054   if (ix86_pic_register_p (XEXP (x, 0)))
13055     /* %ebx + GOT/GOTOFF */
13056     ;
13057   else if (GET_CODE (XEXP (x, 0)) == PLUS)
13058     {
13059       /* %ebx + %reg * scale + GOT/GOTOFF */
13060       reg_addend = XEXP (x, 0);
13061       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
13062         reg_addend = XEXP (reg_addend, 1);
13063       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
13064         reg_addend = XEXP (reg_addend, 0);
13065       else
13066         {
13067           reg_addend = NULL_RTX;
13068           addend = XEXP (x, 0);
13069         }
13070     }
13071   else
13072     addend = XEXP (x, 0);
13073
13074   x = XEXP (XEXP (x, 1), 0);
13075   if (GET_CODE (x) == PLUS
13076       && CONST_INT_P (XEXP (x, 1)))
13077     {
13078       const_addend = XEXP (x, 1);
13079       x = XEXP (x, 0);
13080     }
13081
13082   if (GET_CODE (x) == UNSPEC
13083       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
13084           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
13085     result = XVECEXP (x, 0, 0);
13086
13087   if (TARGET_MACHO && darwin_local_data_pic (x)
13088       && !MEM_P (orig_x))
13089     result = XVECEXP (x, 0, 0);
13090
13091   if (! result)
13092     return ix86_delegitimize_tls_address (orig_x);
13093
13094   if (const_addend)
13095     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
13096   if (reg_addend)
13097     result = gen_rtx_PLUS (Pmode, reg_addend, result);
13098   if (addend)
13099     {
13100       /* If the rest of original X doesn't involve the PIC register, add
13101          addend and subtract pic_offset_table_rtx.  This can happen e.g.
13102          for code like:
13103          leal (%ebx, %ecx, 4), %ecx
13104          ...
13105          movl foo@GOTOFF(%ecx), %edx
13106          in which case we return (%ecx - %ebx) + foo.  */
13107       if (pic_offset_table_rtx)
13108         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
13109                                                      pic_offset_table_rtx),
13110                                result);
13111       else
13112         return orig_x;
13113     }
13114   if (GET_MODE (orig_x) != Pmode && MEM_P (orig_x))
13115     return simplify_gen_subreg (GET_MODE (orig_x), result, Pmode, 0);
13116   return result;
13117 }
13118
13119 /* If X is a machine specific address (i.e. a symbol or label being
13120    referenced as a displacement from the GOT implemented using an
13121    UNSPEC), then return the base term.  Otherwise return X.  */
13122
13123 rtx
13124 ix86_find_base_term (rtx x)
13125 {
13126   rtx term;
13127
13128   if (TARGET_64BIT)
13129     {
13130       if (GET_CODE (x) != CONST)
13131         return x;
13132       term = XEXP (x, 0);
13133       if (GET_CODE (term) == PLUS
13134           && (CONST_INT_P (XEXP (term, 1))
13135               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
13136         term = XEXP (term, 0);
13137       if (GET_CODE (term) != UNSPEC
13138           || XINT (term, 1) != UNSPEC_GOTPCREL)
13139         return x;
13140
13141       return XVECEXP (term, 0, 0);
13142     }
13143
13144   return ix86_delegitimize_address (x);
13145 }
13146 \f
13147 static void
13148 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
13149                     int fp, FILE *file)
13150 {
13151   const char *suffix;
13152
13153   if (mode == CCFPmode || mode == CCFPUmode)
13154     {
13155       code = ix86_fp_compare_code_to_integer (code);
13156       mode = CCmode;
13157     }
13158   if (reverse)
13159     code = reverse_condition (code);
13160
13161   switch (code)
13162     {
13163     case EQ:
13164       switch (mode)
13165         {
13166         case CCAmode:
13167           suffix = "a";
13168           break;
13169
13170         case CCCmode:
13171           suffix = "c";
13172           break;
13173
13174         case CCOmode:
13175           suffix = "o";
13176           break;
13177
13178         case CCSmode:
13179           suffix = "s";
13180           break;
13181
13182         default:
13183           suffix = "e";
13184         }
13185       break;
13186     case NE:
13187       switch (mode)
13188         {
13189         case CCAmode:
13190           suffix = "na";
13191           break;
13192
13193         case CCCmode:
13194           suffix = "nc";
13195           break;
13196
13197         case CCOmode:
13198           suffix = "no";
13199           break;
13200
13201         case CCSmode:
13202           suffix = "ns";
13203           break;
13204
13205         default:
13206           suffix = "ne";
13207         }
13208       break;
13209     case GT:
13210       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
13211       suffix = "g";
13212       break;
13213     case GTU:
13214       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
13215          Those same assemblers have the same but opposite lossage on cmov.  */
13216       if (mode == CCmode)
13217         suffix = fp ? "nbe" : "a";
13218       else if (mode == CCCmode)
13219         suffix = "b";
13220       else
13221         gcc_unreachable ();
13222       break;
13223     case LT:
13224       switch (mode)
13225         {
13226         case CCNOmode:
13227         case CCGOCmode:
13228           suffix = "s";
13229           break;
13230
13231         case CCmode:
13232         case CCGCmode:
13233           suffix = "l";
13234           break;
13235
13236         default:
13237           gcc_unreachable ();
13238         }
13239       break;
13240     case LTU:
13241       gcc_assert (mode == CCmode || mode == CCCmode);
13242       suffix = "b";
13243       break;
13244     case GE:
13245       switch (mode)
13246         {
13247         case CCNOmode:
13248         case CCGOCmode:
13249           suffix = "ns";
13250           break;
13251
13252         case CCmode:
13253         case CCGCmode:
13254           suffix = "ge";
13255           break;
13256
13257         default:
13258           gcc_unreachable ();
13259         }
13260       break;
13261     case GEU:
13262       /* ??? As above.  */
13263       gcc_assert (mode == CCmode || mode == CCCmode);
13264       suffix = fp ? "nb" : "ae";
13265       break;
13266     case LE:
13267       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
13268       suffix = "le";
13269       break;
13270     case LEU:
13271       /* ??? As above.  */
13272       if (mode == CCmode)
13273         suffix = "be";
13274       else if (mode == CCCmode)
13275         suffix = fp ? "nb" : "ae";
13276       else
13277         gcc_unreachable ();
13278       break;
13279     case UNORDERED:
13280       suffix = fp ? "u" : "p";
13281       break;
13282     case ORDERED:
13283       suffix = fp ? "nu" : "np";
13284       break;
13285     default:
13286       gcc_unreachable ();
13287     }
13288   fputs (suffix, file);
13289 }
13290
13291 /* Print the name of register X to FILE based on its machine mode and number.
13292    If CODE is 'w', pretend the mode is HImode.
13293    If CODE is 'b', pretend the mode is QImode.
13294    If CODE is 'k', pretend the mode is SImode.
13295    If CODE is 'q', pretend the mode is DImode.
13296    If CODE is 'x', pretend the mode is V4SFmode.
13297    If CODE is 't', pretend the mode is V8SFmode.
13298    If CODE is 'h', pretend the reg is the 'high' byte register.
13299    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
13300    If CODE is 'd', duplicate the operand for AVX instruction.
13301  */
13302
13303 void
13304 print_reg (rtx x, int code, FILE *file)
13305 {
13306   const char *reg;
13307   bool duplicated = code == 'd' && TARGET_AVX;
13308
13309   gcc_assert (x == pc_rtx
13310               || (REGNO (x) != ARG_POINTER_REGNUM
13311                   && REGNO (x) != FRAME_POINTER_REGNUM
13312                   && REGNO (x) != FLAGS_REG
13313                   && REGNO (x) != FPSR_REG
13314                   && REGNO (x) != FPCR_REG));
13315
13316   if (ASSEMBLER_DIALECT == ASM_ATT)
13317     putc ('%', file);
13318
13319   if (x == pc_rtx)
13320     {
13321       gcc_assert (TARGET_64BIT);
13322       fputs ("rip", file);
13323       return;
13324     }
13325
13326   if (code == 'w' || MMX_REG_P (x))
13327     code = 2;
13328   else if (code == 'b')
13329     code = 1;
13330   else if (code == 'k')
13331     code = 4;
13332   else if (code == 'q')
13333     code = 8;
13334   else if (code == 'y')
13335     code = 3;
13336   else if (code == 'h')
13337     code = 0;
13338   else if (code == 'x')
13339     code = 16;
13340   else if (code == 't')
13341     code = 32;
13342   else
13343     code = GET_MODE_SIZE (GET_MODE (x));
13344
13345   /* Irritatingly, AMD extended registers use different naming convention
13346      from the normal registers.  */
13347   if (REX_INT_REG_P (x))
13348     {
13349       gcc_assert (TARGET_64BIT);
13350       switch (code)
13351         {
13352           case 0:
13353             error ("extended registers have no high halves");
13354             break;
13355           case 1:
13356             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
13357             break;
13358           case 2:
13359             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
13360             break;
13361           case 4:
13362             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
13363             break;
13364           case 8:
13365             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
13366             break;
13367           default:
13368             error ("unsupported operand size for extended register");
13369             break;
13370         }
13371       return;
13372     }
13373
13374   reg = NULL;
13375   switch (code)
13376     {
13377     case 3:
13378       if (STACK_TOP_P (x))
13379         {
13380           reg = "st(0)";
13381           break;
13382         }
13383       /* FALLTHRU */
13384     case 8:
13385     case 4:
13386     case 12:
13387       if (! ANY_FP_REG_P (x))
13388         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
13389       /* FALLTHRU */
13390     case 16:
13391     case 2:
13392     normal:
13393       reg = hi_reg_name[REGNO (x)];
13394       break;
13395     case 1:
13396       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
13397         goto normal;
13398       reg = qi_reg_name[REGNO (x)];
13399       break;
13400     case 0:
13401       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
13402         goto normal;
13403       reg = qi_high_reg_name[REGNO (x)];
13404       break;
13405     case 32:
13406       if (SSE_REG_P (x))
13407         {
13408           gcc_assert (!duplicated);
13409           putc ('y', file);
13410           fputs (hi_reg_name[REGNO (x)] + 1, file);
13411           return;
13412         }
13413       break;
13414     default:
13415       gcc_unreachable ();
13416     }
13417
13418   fputs (reg, file);
13419   if (duplicated)
13420     {
13421       if (ASSEMBLER_DIALECT == ASM_ATT)
13422         fprintf (file, ", %%%s", reg);
13423       else
13424         fprintf (file, ", %s", reg);
13425     }
13426 }
13427
13428 /* Locate some local-dynamic symbol still in use by this function
13429    so that we can print its name in some tls_local_dynamic_base
13430    pattern.  */
13431
13432 static int
13433 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
13434 {
13435   rtx x = *px;
13436
13437   if (GET_CODE (x) == SYMBOL_REF
13438       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
13439     {
13440       cfun->machine->some_ld_name = XSTR (x, 0);
13441       return 1;
13442     }
13443
13444   return 0;
13445 }
13446
13447 static const char *
13448 get_some_local_dynamic_name (void)
13449 {
13450   rtx insn;
13451
13452   if (cfun->machine->some_ld_name)
13453     return cfun->machine->some_ld_name;
13454
13455   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
13456     if (NONDEBUG_INSN_P (insn)
13457         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
13458       return cfun->machine->some_ld_name;
13459
13460   return NULL;
13461 }
13462
13463 /* Meaning of CODE:
13464    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
13465    C -- print opcode suffix for set/cmov insn.
13466    c -- like C, but print reversed condition
13467    F,f -- likewise, but for floating-point.
13468    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
13469         otherwise nothing
13470    R -- print the prefix for register names.
13471    z -- print the opcode suffix for the size of the current operand.
13472    Z -- likewise, with special suffixes for x87 instructions.
13473    * -- print a star (in certain assembler syntax)
13474    A -- print an absolute memory reference.
13475    w -- print the operand as if it's a "word" (HImode) even if it isn't.
13476    s -- print a shift double count, followed by the assemblers argument
13477         delimiter.
13478    b -- print the QImode name of the register for the indicated operand.
13479         %b0 would print %al if operands[0] is reg 0.
13480    w --  likewise, print the HImode name of the register.
13481    k --  likewise, print the SImode name of the register.
13482    q --  likewise, print the DImode name of the register.
13483    x --  likewise, print the V4SFmode name of the register.
13484    t --  likewise, print the V8SFmode name of the register.
13485    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
13486    y -- print "st(0)" instead of "st" as a register.
13487    d -- print duplicated register operand for AVX instruction.
13488    D -- print condition for SSE cmp instruction.
13489    P -- if PIC, print an @PLT suffix.
13490    X -- don't print any sort of PIC '@' suffix for a symbol.
13491    & -- print some in-use local-dynamic symbol name.
13492    H -- print a memory address offset by 8; used for sse high-parts
13493    Y -- print condition for XOP pcom* instruction.
13494    + -- print a branch hint as 'cs' or 'ds' prefix
13495    ; -- print a semicolon (after prefixes due to bug in older gas).
13496    @ -- print a segment register of thread base pointer load
13497  */
13498
13499 void
13500 ix86_print_operand (FILE *file, rtx x, int code)
13501 {
13502   if (code)
13503     {
13504       switch (code)
13505         {
13506         case '*':
13507           if (ASSEMBLER_DIALECT == ASM_ATT)
13508             putc ('*', file);
13509           return;
13510
13511         case '&':
13512           {
13513             const char *name = get_some_local_dynamic_name ();
13514             if (name == NULL)
13515               output_operand_lossage ("'%%&' used without any "
13516                                       "local dynamic TLS references");
13517             else
13518               assemble_name (file, name);
13519             return;
13520           }
13521
13522         case 'A':
13523           switch (ASSEMBLER_DIALECT)
13524             {
13525             case ASM_ATT:
13526               putc ('*', file);
13527               break;
13528
13529             case ASM_INTEL:
13530               /* Intel syntax. For absolute addresses, registers should not
13531                  be surrounded by braces.  */
13532               if (!REG_P (x))
13533                 {
13534                   putc ('[', file);
13535                   ix86_print_operand (file, x, 0);
13536                   putc (']', file);
13537                   return;
13538                 }
13539               break;
13540
13541             default:
13542               gcc_unreachable ();
13543             }
13544
13545           ix86_print_operand (file, x, 0);
13546           return;
13547
13548
13549         case 'L':
13550           if (ASSEMBLER_DIALECT == ASM_ATT)
13551             putc ('l', file);
13552           return;
13553
13554         case 'W':
13555           if (ASSEMBLER_DIALECT == ASM_ATT)
13556             putc ('w', file);
13557           return;
13558
13559         case 'B':
13560           if (ASSEMBLER_DIALECT == ASM_ATT)
13561             putc ('b', file);
13562           return;
13563
13564         case 'Q':
13565           if (ASSEMBLER_DIALECT == ASM_ATT)
13566             putc ('l', file);
13567           return;
13568
13569         case 'S':
13570           if (ASSEMBLER_DIALECT == ASM_ATT)
13571             putc ('s', file);
13572           return;
13573
13574         case 'T':
13575           if (ASSEMBLER_DIALECT == ASM_ATT)
13576             putc ('t', file);
13577           return;
13578
13579         case 'z':
13580           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
13581             {
13582               /* Opcodes don't get size suffixes if using Intel opcodes.  */
13583               if (ASSEMBLER_DIALECT == ASM_INTEL)
13584                 return;
13585
13586               switch (GET_MODE_SIZE (GET_MODE (x)))
13587                 {
13588                 case 1:
13589                   putc ('b', file);
13590                   return;
13591
13592                 case 2:
13593                   putc ('w', file);
13594                   return;
13595
13596                 case 4:
13597                   putc ('l', file);
13598                   return;
13599
13600                 case 8:
13601                   putc ('q', file);
13602                   return;
13603
13604                 default:
13605                   output_operand_lossage
13606                     ("invalid operand size for operand code '%c'", code);
13607                   return;
13608                 }
13609             }
13610
13611           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
13612             warning
13613               (0, "non-integer operand used with operand code '%c'", code);
13614           /* FALLTHRU */
13615
13616         case 'Z':
13617           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
13618           if (ASSEMBLER_DIALECT == ASM_INTEL)
13619             return;
13620
13621           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
13622             {
13623               switch (GET_MODE_SIZE (GET_MODE (x)))
13624                 {
13625                 case 2:
13626 #ifdef HAVE_AS_IX86_FILDS
13627                   putc ('s', file);
13628 #endif
13629                   return;
13630
13631                 case 4:
13632                   putc ('l', file);
13633                   return;
13634
13635                 case 8:
13636 #ifdef HAVE_AS_IX86_FILDQ
13637                   putc ('q', file);
13638 #else
13639                   fputs ("ll", file);
13640 #endif
13641                   return;
13642
13643                 default:
13644                   break;
13645                 }
13646             }
13647           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
13648             {
13649               /* 387 opcodes don't get size suffixes
13650                  if the operands are registers.  */
13651               if (STACK_REG_P (x))
13652                 return;
13653
13654               switch (GET_MODE_SIZE (GET_MODE (x)))
13655                 {
13656                 case 4:
13657                   putc ('s', file);
13658                   return;
13659
13660                 case 8:
13661                   putc ('l', file);
13662                   return;
13663
13664                 case 12:
13665                 case 16:
13666                   putc ('t', file);
13667                   return;
13668
13669                 default:
13670                   break;
13671                 }
13672             }
13673           else
13674             {
13675               output_operand_lossage
13676                 ("invalid operand type used with operand code '%c'", code);
13677               return;
13678             }
13679
13680           output_operand_lossage
13681             ("invalid operand size for operand code '%c'", code);
13682           return;
13683
13684         case 'd':
13685         case 'b':
13686         case 'w':
13687         case 'k':
13688         case 'q':
13689         case 'h':
13690         case 't':
13691         case 'y':
13692         case 'x':
13693         case 'X':
13694         case 'P':
13695           break;
13696
13697         case 's':
13698           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
13699             {
13700               ix86_print_operand (file, x, 0);
13701               fputs (", ", file);
13702             }
13703           return;
13704
13705         case 'D':
13706           /* Little bit of braindamage here.  The SSE compare instructions
13707              does use completely different names for the comparisons that the
13708              fp conditional moves.  */
13709           if (TARGET_AVX)
13710             {
13711               switch (GET_CODE (x))
13712                 {
13713                 case EQ:
13714                   fputs ("eq", file);
13715                   break;
13716                 case UNEQ:
13717                   fputs ("eq_us", file);
13718                   break;
13719                 case LT:
13720                   fputs ("lt", file);
13721                   break;
13722                 case UNLT:
13723                   fputs ("nge", file);
13724                   break;
13725                 case LE:
13726                   fputs ("le", file);
13727                   break;
13728                 case UNLE:
13729                   fputs ("ngt", file);
13730                   break;
13731                 case UNORDERED:
13732                   fputs ("unord", file);
13733                   break;
13734                 case NE:
13735                   fputs ("neq", file);
13736                   break;
13737                 case LTGT:
13738                   fputs ("neq_oq", file);
13739                   break;
13740                 case GE:
13741                   fputs ("ge", file);
13742                   break;
13743                 case UNGE:
13744                   fputs ("nlt", file);
13745                   break;
13746                 case GT:
13747                   fputs ("gt", file);
13748                   break;
13749                 case UNGT:
13750                   fputs ("nle", file);
13751                   break;
13752                 case ORDERED:
13753                   fputs ("ord", file);
13754                   break;
13755                 default:
13756                   output_operand_lossage ("operand is not a condition code, "
13757                                           "invalid operand code 'D'");
13758                   return;
13759                 }
13760             }
13761           else
13762             {
13763               switch (GET_CODE (x))
13764                 {
13765                 case EQ:
13766                 case UNEQ:
13767                   fputs ("eq", file);
13768                   break;
13769                 case LT:
13770                 case UNLT:
13771                   fputs ("lt", file);
13772                   break;
13773                 case LE:
13774                 case UNLE:
13775                   fputs ("le", file);
13776                   break;
13777                 case UNORDERED:
13778                   fputs ("unord", file);
13779                   break;
13780                 case NE:
13781                 case LTGT:
13782                   fputs ("neq", file);
13783                   break;
13784                 case UNGE:
13785                 case GE:
13786                   fputs ("nlt", file);
13787                   break;
13788                 case UNGT:
13789                 case GT:
13790                   fputs ("nle", file);
13791                   break;
13792                 case ORDERED:
13793                   fputs ("ord", file);
13794                   break;
13795                 default:
13796                   output_operand_lossage ("operand is not a condition code, "
13797                                           "invalid operand code 'D'");
13798                   return;
13799                 }
13800             }
13801           return;
13802         case 'O':
13803 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
13804           if (ASSEMBLER_DIALECT == ASM_ATT)
13805             {
13806               switch (GET_MODE (x))
13807                 {
13808                 case HImode: putc ('w', file); break;
13809                 case SImode:
13810                 case SFmode: putc ('l', file); break;
13811                 case DImode:
13812                 case DFmode: putc ('q', file); break;
13813                 default: gcc_unreachable ();
13814                 }
13815               putc ('.', file);
13816             }
13817 #endif
13818           return;
13819         case 'C':
13820           if (!COMPARISON_P (x))
13821             {
13822               output_operand_lossage ("operand is neither a constant nor a "
13823                                       "condition code, invalid operand code "
13824                                       "'C'");
13825               return;
13826             }
13827           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
13828           return;
13829         case 'F':
13830           if (!COMPARISON_P (x))
13831             {
13832               output_operand_lossage ("operand is neither a constant nor a "
13833                                       "condition code, invalid operand code "
13834                                       "'F'");
13835               return;
13836             }
13837 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
13838           if (ASSEMBLER_DIALECT == ASM_ATT)
13839             putc ('.', file);
13840 #endif
13841           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
13842           return;
13843
13844           /* Like above, but reverse condition */
13845         case 'c':
13846           /* Check to see if argument to %c is really a constant
13847              and not a condition code which needs to be reversed.  */
13848           if (!COMPARISON_P (x))
13849             {
13850               output_operand_lossage ("operand is neither a constant nor a "
13851                                       "condition code, invalid operand "
13852                                       "code 'c'");
13853               return;
13854             }
13855           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
13856           return;
13857         case 'f':
13858           if (!COMPARISON_P (x))
13859             {
13860               output_operand_lossage ("operand is neither a constant nor a "
13861                                       "condition code, invalid operand "
13862                                       "code 'f'");
13863               return;
13864             }
13865 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
13866           if (ASSEMBLER_DIALECT == ASM_ATT)
13867             putc ('.', file);
13868 #endif
13869           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
13870           return;
13871
13872         case 'H':
13873           /* It doesn't actually matter what mode we use here, as we're
13874              only going to use this for printing.  */
13875           x = adjust_address_nv (x, DImode, 8);
13876           break;
13877
13878         case '+':
13879           {
13880             rtx x;
13881
13882             if (!optimize
13883                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
13884               return;
13885
13886             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
13887             if (x)
13888               {
13889                 int pred_val = INTVAL (XEXP (x, 0));
13890
13891                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
13892                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
13893                   {
13894                     int taken = pred_val > REG_BR_PROB_BASE / 2;
13895                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
13896
13897                     /* Emit hints only in the case default branch prediction
13898                        heuristics would fail.  */
13899                     if (taken != cputaken)
13900                       {
13901                         /* We use 3e (DS) prefix for taken branches and
13902                            2e (CS) prefix for not taken branches.  */
13903                         if (taken)
13904                           fputs ("ds ; ", file);
13905                         else
13906                           fputs ("cs ; ", file);
13907                       }
13908                   }
13909               }
13910             return;
13911           }
13912
13913         case 'Y':
13914           switch (GET_CODE (x))
13915             {
13916             case NE:
13917               fputs ("neq", file);
13918               break;
13919             case EQ:
13920               fputs ("eq", file);
13921               break;
13922             case GE:
13923             case GEU:
13924               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
13925               break;
13926             case GT:
13927             case GTU:
13928               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
13929               break;
13930             case LE:
13931             case LEU:
13932               fputs ("le", file);
13933               break;
13934             case LT:
13935             case LTU:
13936               fputs ("lt", file);
13937               break;
13938             case UNORDERED:
13939               fputs ("unord", file);
13940               break;
13941             case ORDERED:
13942               fputs ("ord", file);
13943               break;
13944             case UNEQ:
13945               fputs ("ueq", file);
13946               break;
13947             case UNGE:
13948               fputs ("nlt", file);
13949               break;
13950             case UNGT:
13951               fputs ("nle", file);
13952               break;
13953             case UNLE:
13954               fputs ("ule", file);
13955               break;
13956             case UNLT:
13957               fputs ("ult", file);
13958               break;
13959             case LTGT:
13960               fputs ("une", file);
13961               break;
13962             default:
13963               output_operand_lossage ("operand is not a condition code, "
13964                                       "invalid operand code 'Y'");
13965               return;
13966             }
13967           return;
13968
13969         case ';':
13970 #ifndef HAVE_AS_IX86_REP_LOCK_PREFIX
13971           putc (';', file);
13972 #endif
13973           return;
13974
13975         case '@':
13976           if (ASSEMBLER_DIALECT == ASM_ATT)
13977             putc ('%', file);
13978
13979           /* The kernel uses a different segment register for performance
13980              reasons; a system call would not have to trash the userspace
13981              segment register, which would be expensive.  */
13982           if (TARGET_64BIT && ix86_cmodel != CM_KERNEL)
13983             fputs ("fs", file);
13984           else
13985             fputs ("gs", file);
13986           return;
13987
13988         default:
13989             output_operand_lossage ("invalid operand code '%c'", code);
13990         }
13991     }
13992
13993   if (REG_P (x))
13994     print_reg (x, code, file);
13995
13996   else if (MEM_P (x))
13997     {
13998       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
13999       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
14000           && GET_MODE (x) != BLKmode)
14001         {
14002           const char * size;
14003           switch (GET_MODE_SIZE (GET_MODE (x)))
14004             {
14005             case 1: size = "BYTE"; break;
14006             case 2: size = "WORD"; break;
14007             case 4: size = "DWORD"; break;
14008             case 8: size = "QWORD"; break;
14009             case 12: size = "TBYTE"; break;
14010             case 16:
14011               if (GET_MODE (x) == XFmode)
14012                 size = "TBYTE";
14013               else
14014                 size = "XMMWORD";
14015               break;
14016             case 32: size = "YMMWORD"; break;
14017             default:
14018               gcc_unreachable ();
14019             }
14020
14021           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
14022           if (code == 'b')
14023             size = "BYTE";
14024           else if (code == 'w')
14025             size = "WORD";
14026           else if (code == 'k')
14027             size = "DWORD";
14028
14029           fputs (size, file);
14030           fputs (" PTR ", file);
14031         }
14032
14033       x = XEXP (x, 0);
14034       /* Avoid (%rip) for call operands.  */
14035       if (CONSTANT_ADDRESS_P (x) && code == 'P'
14036           && !CONST_INT_P (x))
14037         output_addr_const (file, x);
14038       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
14039         output_operand_lossage ("invalid constraints for operand");
14040       else
14041         output_address (x);
14042     }
14043
14044   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
14045     {
14046       REAL_VALUE_TYPE r;
14047       long l;
14048
14049       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
14050       REAL_VALUE_TO_TARGET_SINGLE (r, l);
14051
14052       if (ASSEMBLER_DIALECT == ASM_ATT)
14053         putc ('$', file);
14054       /* Sign extend 32bit SFmode immediate to 8 bytes.  */
14055       if (code == 'q')
14056         fprintf (file, "0x%08llx", (unsigned long long) (int) l);
14057       else
14058         fprintf (file, "0x%08x", (unsigned int) l);
14059     }
14060
14061   /* These float cases don't actually occur as immediate operands.  */
14062   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
14063     {
14064       char dstr[30];
14065
14066       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
14067       fputs (dstr, file);
14068     }
14069
14070   else if (GET_CODE (x) == CONST_DOUBLE
14071            && GET_MODE (x) == XFmode)
14072     {
14073       char dstr[30];
14074
14075       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
14076       fputs (dstr, file);
14077     }
14078
14079   else
14080     {
14081       /* We have patterns that allow zero sets of memory, for instance.
14082          In 64-bit mode, we should probably support all 8-byte vectors,
14083          since we can in fact encode that into an immediate.  */
14084       if (GET_CODE (x) == CONST_VECTOR)
14085         {
14086           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
14087           x = const0_rtx;
14088         }
14089
14090       if (code != 'P')
14091         {
14092           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
14093             {
14094               if (ASSEMBLER_DIALECT == ASM_ATT)
14095                 putc ('$', file);
14096             }
14097           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
14098                    || GET_CODE (x) == LABEL_REF)
14099             {
14100               if (ASSEMBLER_DIALECT == ASM_ATT)
14101                 putc ('$', file);
14102               else
14103                 fputs ("OFFSET FLAT:", file);
14104             }
14105         }
14106       if (CONST_INT_P (x))
14107         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
14108       else if (flag_pic || MACHOPIC_INDIRECT)
14109         output_pic_addr_const (file, x, code);
14110       else
14111         output_addr_const (file, x);
14112     }
14113 }
14114
14115 static bool
14116 ix86_print_operand_punct_valid_p (unsigned char code)
14117 {
14118   return (code == '@' || code == '*' || code == '+'
14119           || code == '&' || code == ';');
14120 }
14121 \f
14122 /* Print a memory operand whose address is ADDR.  */
14123
14124 static void
14125 ix86_print_operand_address (FILE *file, rtx addr)
14126 {
14127   struct ix86_address parts;
14128   rtx base, index, disp;
14129   int scale;
14130   int ok = ix86_decompose_address (addr, &parts);
14131
14132   gcc_assert (ok);
14133
14134   base = parts.base;
14135   index = parts.index;
14136   disp = parts.disp;
14137   scale = parts.scale;
14138
14139   switch (parts.seg)
14140     {
14141     case SEG_DEFAULT:
14142       break;
14143     case SEG_FS:
14144     case SEG_GS:
14145       if (ASSEMBLER_DIALECT == ASM_ATT)
14146         putc ('%', file);
14147       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
14148       break;
14149     default:
14150       gcc_unreachable ();
14151     }
14152
14153   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
14154   if (TARGET_64BIT && !base && !index)
14155     {
14156       rtx symbol = disp;
14157
14158       if (GET_CODE (disp) == CONST
14159           && GET_CODE (XEXP (disp, 0)) == PLUS
14160           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14161         symbol = XEXP (XEXP (disp, 0), 0);
14162
14163       if (GET_CODE (symbol) == LABEL_REF
14164           || (GET_CODE (symbol) == SYMBOL_REF
14165               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
14166         base = pc_rtx;
14167     }
14168   if (!base && !index)
14169     {
14170       /* Displacement only requires special attention.  */
14171
14172       if (CONST_INT_P (disp))
14173         {
14174           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
14175             fputs ("ds:", file);
14176           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
14177         }
14178       else if (flag_pic)
14179         output_pic_addr_const (file, disp, 0);
14180       else
14181         output_addr_const (file, disp);
14182     }
14183   else
14184     {
14185       if (ASSEMBLER_DIALECT == ASM_ATT)
14186         {
14187           if (disp)
14188             {
14189               if (flag_pic)
14190                 output_pic_addr_const (file, disp, 0);
14191               else if (GET_CODE (disp) == LABEL_REF)
14192                 output_asm_label (disp);
14193               else
14194                 output_addr_const (file, disp);
14195             }
14196
14197           putc ('(', file);
14198           if (base)
14199             print_reg (base, 0, file);
14200           if (index)
14201             {
14202               putc (',', file);
14203               print_reg (index, 0, file);
14204               if (scale != 1)
14205                 fprintf (file, ",%d", scale);
14206             }
14207           putc (')', file);
14208         }
14209       else
14210         {
14211           rtx offset = NULL_RTX;
14212
14213           if (disp)
14214             {
14215               /* Pull out the offset of a symbol; print any symbol itself.  */
14216               if (GET_CODE (disp) == CONST
14217                   && GET_CODE (XEXP (disp, 0)) == PLUS
14218                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
14219                 {
14220                   offset = XEXP (XEXP (disp, 0), 1);
14221                   disp = gen_rtx_CONST (VOIDmode,
14222                                         XEXP (XEXP (disp, 0), 0));
14223                 }
14224
14225               if (flag_pic)
14226                 output_pic_addr_const (file, disp, 0);
14227               else if (GET_CODE (disp) == LABEL_REF)
14228                 output_asm_label (disp);
14229               else if (CONST_INT_P (disp))
14230                 offset = disp;
14231               else
14232                 output_addr_const (file, disp);
14233             }
14234
14235           putc ('[', file);
14236           if (base)
14237             {
14238               print_reg (base, 0, file);
14239               if (offset)
14240                 {
14241                   if (INTVAL (offset) >= 0)
14242                     putc ('+', file);
14243                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14244                 }
14245             }
14246           else if (offset)
14247             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
14248           else
14249             putc ('0', file);
14250
14251           if (index)
14252             {
14253               putc ('+', file);
14254               print_reg (index, 0, file);
14255               if (scale != 1)
14256                 fprintf (file, "*%d", scale);
14257             }
14258           putc (']', file);
14259         }
14260     }
14261 }
14262
14263 /* Implementation of TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
14264
14265 static bool
14266 i386_asm_output_addr_const_extra (FILE *file, rtx x)
14267 {
14268   rtx op;
14269
14270   if (GET_CODE (x) != UNSPEC)
14271     return false;
14272
14273   op = XVECEXP (x, 0, 0);
14274   switch (XINT (x, 1))
14275     {
14276     case UNSPEC_GOTTPOFF:
14277       output_addr_const (file, op);
14278       /* FIXME: This might be @TPOFF in Sun ld.  */
14279       fputs ("@gottpoff", file);
14280       break;
14281     case UNSPEC_TPOFF:
14282       output_addr_const (file, op);
14283       fputs ("@tpoff", file);
14284       break;
14285     case UNSPEC_NTPOFF:
14286       output_addr_const (file, op);
14287       if (TARGET_64BIT)
14288         fputs ("@tpoff", file);
14289       else
14290         fputs ("@ntpoff", file);
14291       break;
14292     case UNSPEC_DTPOFF:
14293       output_addr_const (file, op);
14294       fputs ("@dtpoff", file);
14295       break;
14296     case UNSPEC_GOTNTPOFF:
14297       output_addr_const (file, op);
14298       if (TARGET_64BIT)
14299         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
14300                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
14301       else
14302         fputs ("@gotntpoff", file);
14303       break;
14304     case UNSPEC_INDNTPOFF:
14305       output_addr_const (file, op);
14306       fputs ("@indntpoff", file);
14307       break;
14308 #if TARGET_MACHO
14309     case UNSPEC_MACHOPIC_OFFSET:
14310       output_addr_const (file, op);
14311       putc ('-', file);
14312       machopic_output_function_base_name (file);
14313       break;
14314 #endif
14315
14316     case UNSPEC_STACK_CHECK:
14317       {
14318         int offset;
14319
14320         gcc_assert (flag_split_stack);
14321
14322 #ifdef TARGET_THREAD_SPLIT_STACK_OFFSET
14323         offset = TARGET_THREAD_SPLIT_STACK_OFFSET;
14324 #else
14325         gcc_unreachable ();
14326 #endif
14327
14328         fprintf (file, "%s:%d", TARGET_64BIT ? "%fs" : "%gs", offset);
14329       }
14330       break;
14331
14332     default:
14333       return false;
14334     }
14335
14336   return true;
14337 }
14338 \f
14339 /* Split one or more double-mode RTL references into pairs of half-mode
14340    references.  The RTL can be REG, offsettable MEM, integer constant, or
14341    CONST_DOUBLE.  "operands" is a pointer to an array of double-mode RTLs to
14342    split and "num" is its length.  lo_half and hi_half are output arrays
14343    that parallel "operands".  */
14344
14345 void
14346 split_double_mode (enum machine_mode mode, rtx operands[],
14347                    int num, rtx lo_half[], rtx hi_half[])
14348 {
14349   enum machine_mode half_mode;
14350   unsigned int byte;
14351
14352   switch (mode)
14353     {
14354     case TImode:
14355       half_mode = DImode;
14356       break;
14357     case DImode:
14358       half_mode = SImode;
14359       break;
14360     default:
14361       gcc_unreachable ();
14362     }
14363
14364   byte = GET_MODE_SIZE (half_mode);
14365
14366   while (num--)
14367     {
14368       rtx op = operands[num];
14369
14370       /* simplify_subreg refuse to split volatile memory addresses,
14371          but we still have to handle it.  */
14372       if (MEM_P (op))
14373         {
14374           lo_half[num] = adjust_address (op, half_mode, 0);
14375           hi_half[num] = adjust_address (op, half_mode, byte);
14376         }
14377       else
14378         {
14379           lo_half[num] = simplify_gen_subreg (half_mode, op,
14380                                               GET_MODE (op) == VOIDmode
14381                                               ? mode : GET_MODE (op), 0);
14382           hi_half[num] = simplify_gen_subreg (half_mode, op,
14383                                               GET_MODE (op) == VOIDmode
14384                                               ? mode : GET_MODE (op), byte);
14385         }
14386     }
14387 }
14388 \f
14389 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
14390    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
14391    is the expression of the binary operation.  The output may either be
14392    emitted here, or returned to the caller, like all output_* functions.
14393
14394    There is no guarantee that the operands are the same mode, as they
14395    might be within FLOAT or FLOAT_EXTEND expressions.  */
14396
14397 #ifndef SYSV386_COMPAT
14398 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
14399    wants to fix the assemblers because that causes incompatibility
14400    with gcc.  No-one wants to fix gcc because that causes
14401    incompatibility with assemblers...  You can use the option of
14402    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
14403 #define SYSV386_COMPAT 1
14404 #endif
14405
14406 const char *
14407 output_387_binary_op (rtx insn, rtx *operands)
14408 {
14409   static char buf[40];
14410   const char *p;
14411   const char *ssep;
14412   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
14413
14414 #ifdef ENABLE_CHECKING
14415   /* Even if we do not want to check the inputs, this documents input
14416      constraints.  Which helps in understanding the following code.  */
14417   if (STACK_REG_P (operands[0])
14418       && ((REG_P (operands[1])
14419            && REGNO (operands[0]) == REGNO (operands[1])
14420            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
14421           || (REG_P (operands[2])
14422               && REGNO (operands[0]) == REGNO (operands[2])
14423               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
14424       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
14425     ; /* ok */
14426   else
14427     gcc_assert (is_sse);
14428 #endif
14429
14430   switch (GET_CODE (operands[3]))
14431     {
14432     case PLUS:
14433       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14434           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14435         p = "fiadd";
14436       else
14437         p = "fadd";
14438       ssep = "vadd";
14439       break;
14440
14441     case MINUS:
14442       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14443           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14444         p = "fisub";
14445       else
14446         p = "fsub";
14447       ssep = "vsub";
14448       break;
14449
14450     case MULT:
14451       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14452           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14453         p = "fimul";
14454       else
14455         p = "fmul";
14456       ssep = "vmul";
14457       break;
14458
14459     case DIV:
14460       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
14461           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
14462         p = "fidiv";
14463       else
14464         p = "fdiv";
14465       ssep = "vdiv";
14466       break;
14467
14468     default:
14469       gcc_unreachable ();
14470     }
14471
14472   if (is_sse)
14473    {
14474      if (TARGET_AVX)
14475        {
14476          strcpy (buf, ssep);
14477          if (GET_MODE (operands[0]) == SFmode)
14478            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
14479          else
14480            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
14481        }
14482      else
14483        {
14484          strcpy (buf, ssep + 1);
14485          if (GET_MODE (operands[0]) == SFmode)
14486            strcat (buf, "ss\t{%2, %0|%0, %2}");
14487          else
14488            strcat (buf, "sd\t{%2, %0|%0, %2}");
14489        }
14490       return buf;
14491    }
14492   strcpy (buf, p);
14493
14494   switch (GET_CODE (operands[3]))
14495     {
14496     case MULT:
14497     case PLUS:
14498       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
14499         {
14500           rtx temp = operands[2];
14501           operands[2] = operands[1];
14502           operands[1] = temp;
14503         }
14504
14505       /* know operands[0] == operands[1].  */
14506
14507       if (MEM_P (operands[2]))
14508         {
14509           p = "%Z2\t%2";
14510           break;
14511         }
14512
14513       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
14514         {
14515           if (STACK_TOP_P (operands[0]))
14516             /* How is it that we are storing to a dead operand[2]?
14517                Well, presumably operands[1] is dead too.  We can't
14518                store the result to st(0) as st(0) gets popped on this
14519                instruction.  Instead store to operands[2] (which I
14520                think has to be st(1)).  st(1) will be popped later.
14521                gcc <= 2.8.1 didn't have this check and generated
14522                assembly code that the Unixware assembler rejected.  */
14523             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
14524           else
14525             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
14526           break;
14527         }
14528
14529       if (STACK_TOP_P (operands[0]))
14530         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
14531       else
14532         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
14533       break;
14534
14535     case MINUS:
14536     case DIV:
14537       if (MEM_P (operands[1]))
14538         {
14539           p = "r%Z1\t%1";
14540           break;
14541         }
14542
14543       if (MEM_P (operands[2]))
14544         {
14545           p = "%Z2\t%2";
14546           break;
14547         }
14548
14549       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
14550         {
14551 #if SYSV386_COMPAT
14552           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
14553              derived assemblers, confusingly reverse the direction of
14554              the operation for fsub{r} and fdiv{r} when the
14555              destination register is not st(0).  The Intel assembler
14556              doesn't have this brain damage.  Read !SYSV386_COMPAT to
14557              figure out what the hardware really does.  */
14558           if (STACK_TOP_P (operands[0]))
14559             p = "{p\t%0, %2|rp\t%2, %0}";
14560           else
14561             p = "{rp\t%2, %0|p\t%0, %2}";
14562 #else
14563           if (STACK_TOP_P (operands[0]))
14564             /* As above for fmul/fadd, we can't store to st(0).  */
14565             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
14566           else
14567             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
14568 #endif
14569           break;
14570         }
14571
14572       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
14573         {
14574 #if SYSV386_COMPAT
14575           if (STACK_TOP_P (operands[0]))
14576             p = "{rp\t%0, %1|p\t%1, %0}";
14577           else
14578             p = "{p\t%1, %0|rp\t%0, %1}";
14579 #else
14580           if (STACK_TOP_P (operands[0]))
14581             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
14582           else
14583             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
14584 #endif
14585           break;
14586         }
14587
14588       if (STACK_TOP_P (operands[0]))
14589         {
14590           if (STACK_TOP_P (operands[1]))
14591             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
14592           else
14593             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
14594           break;
14595         }
14596       else if (STACK_TOP_P (operands[1]))
14597         {
14598 #if SYSV386_COMPAT
14599           p = "{\t%1, %0|r\t%0, %1}";
14600 #else
14601           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
14602 #endif
14603         }
14604       else
14605         {
14606 #if SYSV386_COMPAT
14607           p = "{r\t%2, %0|\t%0, %2}";
14608 #else
14609           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
14610 #endif
14611         }
14612       break;
14613
14614     default:
14615       gcc_unreachable ();
14616     }
14617
14618   strcat (buf, p);
14619   return buf;
14620 }
14621
14622 /* Return needed mode for entity in optimize_mode_switching pass.  */
14623
14624 int
14625 ix86_mode_needed (int entity, rtx insn)
14626 {
14627   enum attr_i387_cw mode;
14628
14629   /* The mode UNINITIALIZED is used to store control word after a
14630      function call or ASM pattern.  The mode ANY specify that function
14631      has no requirements on the control word and make no changes in the
14632      bits we are interested in.  */
14633
14634   if (CALL_P (insn)
14635       || (NONJUMP_INSN_P (insn)
14636           && (asm_noperands (PATTERN (insn)) >= 0
14637               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
14638     return I387_CW_UNINITIALIZED;
14639
14640   if (recog_memoized (insn) < 0)
14641     return I387_CW_ANY;
14642
14643   mode = get_attr_i387_cw (insn);
14644
14645   switch (entity)
14646     {
14647     case I387_TRUNC:
14648       if (mode == I387_CW_TRUNC)
14649         return mode;
14650       break;
14651
14652     case I387_FLOOR:
14653       if (mode == I387_CW_FLOOR)
14654         return mode;
14655       break;
14656
14657     case I387_CEIL:
14658       if (mode == I387_CW_CEIL)
14659         return mode;
14660       break;
14661
14662     case I387_MASK_PM:
14663       if (mode == I387_CW_MASK_PM)
14664         return mode;
14665       break;
14666
14667     default:
14668       gcc_unreachable ();
14669     }
14670
14671   return I387_CW_ANY;
14672 }
14673
14674 /* Output code to initialize control word copies used by trunc?f?i and
14675    rounding patterns.  CURRENT_MODE is set to current control word,
14676    while NEW_MODE is set to new control word.  */
14677
14678 void
14679 emit_i387_cw_initialization (int mode)
14680 {
14681   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
14682   rtx new_mode;
14683
14684   enum ix86_stack_slot slot;
14685
14686   rtx reg = gen_reg_rtx (HImode);
14687
14688   emit_insn (gen_x86_fnstcw_1 (stored_mode));
14689   emit_move_insn (reg, copy_rtx (stored_mode));
14690
14691   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
14692       || optimize_function_for_size_p (cfun))
14693     {
14694       switch (mode)
14695         {
14696         case I387_CW_TRUNC:
14697           /* round toward zero (truncate) */
14698           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
14699           slot = SLOT_CW_TRUNC;
14700           break;
14701
14702         case I387_CW_FLOOR:
14703           /* round down toward -oo */
14704           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
14705           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
14706           slot = SLOT_CW_FLOOR;
14707           break;
14708
14709         case I387_CW_CEIL:
14710           /* round up toward +oo */
14711           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
14712           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
14713           slot = SLOT_CW_CEIL;
14714           break;
14715
14716         case I387_CW_MASK_PM:
14717           /* mask precision exception for nearbyint() */
14718           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
14719           slot = SLOT_CW_MASK_PM;
14720           break;
14721
14722         default:
14723           gcc_unreachable ();
14724         }
14725     }
14726   else
14727     {
14728       switch (mode)
14729         {
14730         case I387_CW_TRUNC:
14731           /* round toward zero (truncate) */
14732           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
14733           slot = SLOT_CW_TRUNC;
14734           break;
14735
14736         case I387_CW_FLOOR:
14737           /* round down toward -oo */
14738           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
14739           slot = SLOT_CW_FLOOR;
14740           break;
14741
14742         case I387_CW_CEIL:
14743           /* round up toward +oo */
14744           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
14745           slot = SLOT_CW_CEIL;
14746           break;
14747
14748         case I387_CW_MASK_PM:
14749           /* mask precision exception for nearbyint() */
14750           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
14751           slot = SLOT_CW_MASK_PM;
14752           break;
14753
14754         default:
14755           gcc_unreachable ();
14756         }
14757     }
14758
14759   gcc_assert (slot < MAX_386_STACK_LOCALS);
14760
14761   new_mode = assign_386_stack_local (HImode, slot);
14762   emit_move_insn (new_mode, reg);
14763 }
14764
14765 /* Output code for INSN to convert a float to a signed int.  OPERANDS
14766    are the insn operands.  The output may be [HSD]Imode and the input
14767    operand may be [SDX]Fmode.  */
14768
14769 const char *
14770 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
14771 {
14772   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
14773   int dimode_p = GET_MODE (operands[0]) == DImode;
14774   int round_mode = get_attr_i387_cw (insn);
14775
14776   /* Jump through a hoop or two for DImode, since the hardware has no
14777      non-popping instruction.  We used to do this a different way, but
14778      that was somewhat fragile and broke with post-reload splitters.  */
14779   if ((dimode_p || fisttp) && !stack_top_dies)
14780     output_asm_insn ("fld\t%y1", operands);
14781
14782   gcc_assert (STACK_TOP_P (operands[1]));
14783   gcc_assert (MEM_P (operands[0]));
14784   gcc_assert (GET_MODE (operands[1]) != TFmode);
14785
14786   if (fisttp)
14787       output_asm_insn ("fisttp%Z0\t%0", operands);
14788   else
14789     {
14790       if (round_mode != I387_CW_ANY)
14791         output_asm_insn ("fldcw\t%3", operands);
14792       if (stack_top_dies || dimode_p)
14793         output_asm_insn ("fistp%Z0\t%0", operands);
14794       else
14795         output_asm_insn ("fist%Z0\t%0", operands);
14796       if (round_mode != I387_CW_ANY)
14797         output_asm_insn ("fldcw\t%2", operands);
14798     }
14799
14800   return "";
14801 }
14802
14803 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
14804    have the values zero or one, indicates the ffreep insn's operand
14805    from the OPERANDS array.  */
14806
14807 static const char *
14808 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
14809 {
14810   if (TARGET_USE_FFREEP)
14811 #ifdef HAVE_AS_IX86_FFREEP
14812     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
14813 #else
14814     {
14815       static char retval[32];
14816       int regno = REGNO (operands[opno]);
14817
14818       gcc_assert (FP_REGNO_P (regno));
14819
14820       regno -= FIRST_STACK_REG;
14821
14822       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
14823       return retval;
14824     }
14825 #endif
14826
14827   return opno ? "fstp\t%y1" : "fstp\t%y0";
14828 }
14829
14830
14831 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
14832    should be used.  UNORDERED_P is true when fucom should be used.  */
14833
14834 const char *
14835 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
14836 {
14837   int stack_top_dies;
14838   rtx cmp_op0, cmp_op1;
14839   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
14840
14841   if (eflags_p)
14842     {
14843       cmp_op0 = operands[0];
14844       cmp_op1 = operands[1];
14845     }
14846   else
14847     {
14848       cmp_op0 = operands[1];
14849       cmp_op1 = operands[2];
14850     }
14851
14852   if (is_sse)
14853     {
14854       static const char ucomiss[] = "vucomiss\t{%1, %0|%0, %1}";
14855       static const char ucomisd[] = "vucomisd\t{%1, %0|%0, %1}";
14856       static const char comiss[] = "vcomiss\t{%1, %0|%0, %1}";
14857       static const char comisd[] = "vcomisd\t{%1, %0|%0, %1}";
14858
14859       if (GET_MODE (operands[0]) == SFmode)
14860         if (unordered_p)
14861           return &ucomiss[TARGET_AVX ? 0 : 1];
14862         else
14863           return &comiss[TARGET_AVX ? 0 : 1];
14864       else
14865         if (unordered_p)
14866           return &ucomisd[TARGET_AVX ? 0 : 1];
14867         else
14868           return &comisd[TARGET_AVX ? 0 : 1];
14869     }
14870
14871   gcc_assert (STACK_TOP_P (cmp_op0));
14872
14873   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
14874
14875   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
14876     {
14877       if (stack_top_dies)
14878         {
14879           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
14880           return output_387_ffreep (operands, 1);
14881         }
14882       else
14883         return "ftst\n\tfnstsw\t%0";
14884     }
14885
14886   if (STACK_REG_P (cmp_op1)
14887       && stack_top_dies
14888       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
14889       && REGNO (cmp_op1) != FIRST_STACK_REG)
14890     {
14891       /* If both the top of the 387 stack dies, and the other operand
14892          is also a stack register that dies, then this must be a
14893          `fcompp' float compare */
14894
14895       if (eflags_p)
14896         {
14897           /* There is no double popping fcomi variant.  Fortunately,
14898              eflags is immune from the fstp's cc clobbering.  */
14899           if (unordered_p)
14900             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
14901           else
14902             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
14903           return output_387_ffreep (operands, 0);
14904         }
14905       else
14906         {
14907           if (unordered_p)
14908             return "fucompp\n\tfnstsw\t%0";
14909           else
14910             return "fcompp\n\tfnstsw\t%0";
14911         }
14912     }
14913   else
14914     {
14915       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
14916
14917       static const char * const alt[16] =
14918       {
14919         "fcom%Z2\t%y2\n\tfnstsw\t%0",
14920         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
14921         "fucom%Z2\t%y2\n\tfnstsw\t%0",
14922         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
14923
14924         "ficom%Z2\t%y2\n\tfnstsw\t%0",
14925         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
14926         NULL,
14927         NULL,
14928
14929         "fcomi\t{%y1, %0|%0, %y1}",
14930         "fcomip\t{%y1, %0|%0, %y1}",
14931         "fucomi\t{%y1, %0|%0, %y1}",
14932         "fucomip\t{%y1, %0|%0, %y1}",
14933
14934         NULL,
14935         NULL,
14936         NULL,
14937         NULL
14938       };
14939
14940       int mask;
14941       const char *ret;
14942
14943       mask  = eflags_p << 3;
14944       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
14945       mask |= unordered_p << 1;
14946       mask |= stack_top_dies;
14947
14948       gcc_assert (mask < 16);
14949       ret = alt[mask];
14950       gcc_assert (ret);
14951
14952       return ret;
14953     }
14954 }
14955
14956 void
14957 ix86_output_addr_vec_elt (FILE *file, int value)
14958 {
14959   const char *directive = ASM_LONG;
14960
14961 #ifdef ASM_QUAD
14962   if (TARGET_64BIT)
14963     directive = ASM_QUAD;
14964 #else
14965   gcc_assert (!TARGET_64BIT);
14966 #endif
14967
14968   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
14969 }
14970
14971 void
14972 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
14973 {
14974   const char *directive = ASM_LONG;
14975
14976 #ifdef ASM_QUAD
14977   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
14978     directive = ASM_QUAD;
14979 #else
14980   gcc_assert (!TARGET_64BIT);
14981 #endif
14982   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
14983   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
14984     fprintf (file, "%s%s%d-%s%d\n",
14985              directive, LPREFIX, value, LPREFIX, rel);
14986   else if (HAVE_AS_GOTOFF_IN_DATA)
14987     fprintf (file, ASM_LONG "%s%d@GOTOFF\n", LPREFIX, value);
14988 #if TARGET_MACHO
14989   else if (TARGET_MACHO)
14990     {
14991       fprintf (file, ASM_LONG "%s%d-", LPREFIX, value);
14992       machopic_output_function_base_name (file);
14993       putc ('\n', file);
14994     }
14995 #endif
14996   else
14997     asm_fprintf (file, ASM_LONG "%U%s+[.-%s%d]\n",
14998                  GOT_SYMBOL_NAME, LPREFIX, value);
14999 }
15000 \f
15001 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
15002    for the target.  */
15003
15004 void
15005 ix86_expand_clear (rtx dest)
15006 {
15007   rtx tmp;
15008
15009   /* We play register width games, which are only valid after reload.  */
15010   gcc_assert (reload_completed);
15011
15012   /* Avoid HImode and its attendant prefix byte.  */
15013   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
15014     dest = gen_rtx_REG (SImode, REGNO (dest));
15015   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
15016
15017   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
15018   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
15019     {
15020       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15021       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
15022     }
15023
15024   emit_insn (tmp);
15025 }
15026
15027 /* X is an unchanging MEM.  If it is a constant pool reference, return
15028    the constant pool rtx, else NULL.  */
15029
15030 rtx
15031 maybe_get_pool_constant (rtx x)
15032 {
15033   x = ix86_delegitimize_address (XEXP (x, 0));
15034
15035   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
15036     return get_pool_constant (x);
15037
15038   return NULL_RTX;
15039 }
15040
15041 void
15042 ix86_expand_move (enum machine_mode mode, rtx operands[])
15043 {
15044   rtx op0, op1;
15045   enum tls_model model;
15046
15047   op0 = operands[0];
15048   op1 = operands[1];
15049
15050   if (GET_CODE (op1) == SYMBOL_REF)
15051     {
15052       model = SYMBOL_REF_TLS_MODEL (op1);
15053       if (model)
15054         {
15055           op1 = legitimize_tls_address (op1, model, true);
15056           op1 = force_operand (op1, op0);
15057           if (op1 == op0)
15058             return;
15059         }
15060       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15061                && SYMBOL_REF_DLLIMPORT_P (op1))
15062         op1 = legitimize_dllimport_symbol (op1, false);
15063     }
15064   else if (GET_CODE (op1) == CONST
15065            && GET_CODE (XEXP (op1, 0)) == PLUS
15066            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
15067     {
15068       rtx addend = XEXP (XEXP (op1, 0), 1);
15069       rtx symbol = XEXP (XEXP (op1, 0), 0);
15070       rtx tmp = NULL;
15071
15072       model = SYMBOL_REF_TLS_MODEL (symbol);
15073       if (model)
15074         tmp = legitimize_tls_address (symbol, model, true);
15075       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
15076                && SYMBOL_REF_DLLIMPORT_P (symbol))
15077         tmp = legitimize_dllimport_symbol (symbol, true);
15078
15079       if (tmp)
15080         {
15081           tmp = force_operand (tmp, NULL);
15082           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
15083                                      op0, 1, OPTAB_DIRECT);
15084           if (tmp == op0)
15085             return;
15086         }
15087     }
15088
15089   if ((flag_pic || MACHOPIC_INDIRECT) 
15090        && mode == Pmode && symbolic_operand (op1, Pmode))
15091     {
15092       if (TARGET_MACHO && !TARGET_64BIT)
15093         {
15094 #if TARGET_MACHO
15095           /* dynamic-no-pic */
15096           if (MACHOPIC_INDIRECT)
15097             {
15098               rtx temp = ((reload_in_progress
15099                            || ((op0 && REG_P (op0))
15100                                && mode == Pmode))
15101                           ? op0 : gen_reg_rtx (Pmode));
15102               op1 = machopic_indirect_data_reference (op1, temp);
15103               if (MACHOPIC_PURE)
15104                 op1 = machopic_legitimize_pic_address (op1, mode,
15105                                                        temp == op1 ? 0 : temp);
15106             }
15107           if (op0 != op1 && GET_CODE (op0) != MEM)
15108             {
15109               rtx insn = gen_rtx_SET (VOIDmode, op0, op1);
15110               emit_insn (insn);
15111               return;
15112             }
15113           if (GET_CODE (op0) == MEM)
15114             op1 = force_reg (Pmode, op1);
15115           else
15116             {
15117               rtx temp = op0;
15118               if (GET_CODE (temp) != REG)
15119                 temp = gen_reg_rtx (Pmode);
15120               temp = legitimize_pic_address (op1, temp);
15121               if (temp == op0)
15122             return;
15123               op1 = temp;
15124             }
15125       /* dynamic-no-pic */
15126 #endif
15127         }
15128       else
15129         {
15130           if (MEM_P (op0))
15131             op1 = force_reg (Pmode, op1);
15132           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
15133             {
15134               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
15135               op1 = legitimize_pic_address (op1, reg);
15136               if (op0 == op1)
15137                 return;
15138             }
15139         }
15140     }
15141   else
15142     {
15143       if (MEM_P (op0)
15144           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
15145               || !push_operand (op0, mode))
15146           && MEM_P (op1))
15147         op1 = force_reg (mode, op1);
15148
15149       if (push_operand (op0, mode)
15150           && ! general_no_elim_operand (op1, mode))
15151         op1 = copy_to_mode_reg (mode, op1);
15152
15153       /* Force large constants in 64bit compilation into register
15154          to get them CSEed.  */
15155       if (can_create_pseudo_p ()
15156           && (mode == DImode) && TARGET_64BIT
15157           && immediate_operand (op1, mode)
15158           && !x86_64_zext_immediate_operand (op1, VOIDmode)
15159           && !register_operand (op0, mode)
15160           && optimize)
15161         op1 = copy_to_mode_reg (mode, op1);
15162
15163       if (can_create_pseudo_p ()
15164           && FLOAT_MODE_P (mode)
15165           && GET_CODE (op1) == CONST_DOUBLE)
15166         {
15167           /* If we are loading a floating point constant to a register,
15168              force the value to memory now, since we'll get better code
15169              out the back end.  */
15170
15171           op1 = validize_mem (force_const_mem (mode, op1));
15172           if (!register_operand (op0, mode))
15173             {
15174               rtx temp = gen_reg_rtx (mode);
15175               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
15176               emit_move_insn (op0, temp);
15177               return;
15178             }
15179         }
15180     }
15181
15182   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15183 }
15184
15185 void
15186 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
15187 {
15188   rtx op0 = operands[0], op1 = operands[1];
15189   unsigned int align = GET_MODE_ALIGNMENT (mode);
15190
15191   /* Force constants other than zero into memory.  We do not know how
15192      the instructions used to build constants modify the upper 64 bits
15193      of the register, once we have that information we may be able
15194      to handle some of them more efficiently.  */
15195   if (can_create_pseudo_p ()
15196       && register_operand (op0, mode)
15197       && (CONSTANT_P (op1)
15198           || (GET_CODE (op1) == SUBREG
15199               && CONSTANT_P (SUBREG_REG (op1))))
15200       && !standard_sse_constant_p (op1))
15201     op1 = validize_mem (force_const_mem (mode, op1));
15202
15203   /* We need to check memory alignment for SSE mode since attribute
15204      can make operands unaligned.  */
15205   if (can_create_pseudo_p ()
15206       && SSE_REG_MODE_P (mode)
15207       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
15208           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
15209     {
15210       rtx tmp[2];
15211
15212       /* ix86_expand_vector_move_misalign() does not like constants ... */
15213       if (CONSTANT_P (op1)
15214           || (GET_CODE (op1) == SUBREG
15215               && CONSTANT_P (SUBREG_REG (op1))))
15216         op1 = validize_mem (force_const_mem (mode, op1));
15217
15218       /* ... nor both arguments in memory.  */
15219       if (!register_operand (op0, mode)
15220           && !register_operand (op1, mode))
15221         op1 = force_reg (mode, op1);
15222
15223       tmp[0] = op0; tmp[1] = op1;
15224       ix86_expand_vector_move_misalign (mode, tmp);
15225       return;
15226     }
15227
15228   /* Make operand1 a register if it isn't already.  */
15229   if (can_create_pseudo_p ()
15230       && !register_operand (op0, mode)
15231       && !register_operand (op1, mode))
15232     {
15233       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
15234       return;
15235     }
15236
15237   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
15238 }
15239
15240 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
15241    straight to ix86_expand_vector_move.  */
15242 /* Code generation for scalar reg-reg moves of single and double precision data:
15243      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
15244        movaps reg, reg
15245      else
15246        movss reg, reg
15247      if (x86_sse_partial_reg_dependency == true)
15248        movapd reg, reg
15249      else
15250        movsd reg, reg
15251
15252    Code generation for scalar loads of double precision data:
15253      if (x86_sse_split_regs == true)
15254        movlpd mem, reg      (gas syntax)
15255      else
15256        movsd mem, reg
15257
15258    Code generation for unaligned packed loads of single precision data
15259    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
15260      if (x86_sse_unaligned_move_optimal)
15261        movups mem, reg
15262
15263      if (x86_sse_partial_reg_dependency == true)
15264        {
15265          xorps  reg, reg
15266          movlps mem, reg
15267          movhps mem+8, reg
15268        }
15269      else
15270        {
15271          movlps mem, reg
15272          movhps mem+8, reg
15273        }
15274
15275    Code generation for unaligned packed loads of double precision data
15276    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
15277      if (x86_sse_unaligned_move_optimal)
15278        movupd mem, reg
15279
15280      if (x86_sse_split_regs == true)
15281        {
15282          movlpd mem, reg
15283          movhpd mem+8, reg
15284        }
15285      else
15286        {
15287          movsd  mem, reg
15288          movhpd mem+8, reg
15289        }
15290  */
15291
15292 void
15293 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
15294 {
15295   rtx op0, op1, m;
15296
15297   op0 = operands[0];
15298   op1 = operands[1];
15299
15300   if (TARGET_AVX)
15301     {
15302       switch (GET_MODE_CLASS (mode))
15303         {
15304         case MODE_VECTOR_INT:
15305         case MODE_INT:
15306           switch (GET_MODE_SIZE (mode))
15307             {
15308             case 16:
15309               /*  If we're optimizing for size, movups is the smallest.  */
15310               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15311                 {
15312                   op0 = gen_lowpart (V4SFmode, op0);
15313                   op1 = gen_lowpart (V4SFmode, op1);
15314                   emit_insn (gen_avx_movups (op0, op1));
15315                   return;
15316                 }
15317               op0 = gen_lowpart (V16QImode, op0);
15318               op1 = gen_lowpart (V16QImode, op1);
15319               emit_insn (gen_avx_movdqu (op0, op1));
15320               break;
15321             case 32:
15322               op0 = gen_lowpart (V32QImode, op0);
15323               op1 = gen_lowpart (V32QImode, op1);
15324               emit_insn (gen_avx_movdqu256 (op0, op1));
15325               break;
15326             default:
15327               gcc_unreachable ();
15328             }
15329           break;
15330         case MODE_VECTOR_FLOAT:
15331           op0 = gen_lowpart (mode, op0);
15332           op1 = gen_lowpart (mode, op1);
15333
15334           switch (mode)
15335             {
15336             case V4SFmode:
15337               emit_insn (gen_avx_movups (op0, op1));
15338               break;
15339             case V8SFmode:
15340               emit_insn (gen_avx_movups256 (op0, op1));
15341               break;
15342             case V2DFmode:
15343               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15344                 {
15345                   op0 = gen_lowpart (V4SFmode, op0);
15346                   op1 = gen_lowpart (V4SFmode, op1);
15347                   emit_insn (gen_avx_movups (op0, op1));
15348                   return;
15349                 }
15350               emit_insn (gen_avx_movupd (op0, op1));
15351               break;
15352             case V4DFmode:
15353               emit_insn (gen_avx_movupd256 (op0, op1));
15354               break;
15355             default:
15356               gcc_unreachable ();
15357             }
15358           break;
15359
15360         default:
15361           gcc_unreachable ();
15362         }
15363
15364       return;
15365     }
15366
15367   if (MEM_P (op1))
15368     {
15369       /* If we're optimizing for size, movups is the smallest.  */
15370       if (optimize_insn_for_size_p ()
15371           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15372         {
15373           op0 = gen_lowpart (V4SFmode, op0);
15374           op1 = gen_lowpart (V4SFmode, op1);
15375           emit_insn (gen_sse_movups (op0, op1));
15376           return;
15377         }
15378
15379       /* ??? If we have typed data, then it would appear that using
15380          movdqu is the only way to get unaligned data loaded with
15381          integer type.  */
15382       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15383         {
15384           op0 = gen_lowpart (V16QImode, op0);
15385           op1 = gen_lowpart (V16QImode, op1);
15386           emit_insn (gen_sse2_movdqu (op0, op1));
15387           return;
15388         }
15389
15390       if (TARGET_SSE2 && mode == V2DFmode)
15391         {
15392           rtx zero;
15393
15394           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
15395             {
15396               op0 = gen_lowpart (V2DFmode, op0);
15397               op1 = gen_lowpart (V2DFmode, op1);
15398               emit_insn (gen_sse2_movupd (op0, op1));
15399               return;
15400             }
15401
15402           /* When SSE registers are split into halves, we can avoid
15403              writing to the top half twice.  */
15404           if (TARGET_SSE_SPLIT_REGS)
15405             {
15406               emit_clobber (op0);
15407               zero = op0;
15408             }
15409           else
15410             {
15411               /* ??? Not sure about the best option for the Intel chips.
15412                  The following would seem to satisfy; the register is
15413                  entirely cleared, breaking the dependency chain.  We
15414                  then store to the upper half, with a dependency depth
15415                  of one.  A rumor has it that Intel recommends two movsd
15416                  followed by an unpacklpd, but this is unconfirmed.  And
15417                  given that the dependency depth of the unpacklpd would
15418                  still be one, I'm not sure why this would be better.  */
15419               zero = CONST0_RTX (V2DFmode);
15420             }
15421
15422           m = adjust_address (op1, DFmode, 0);
15423           emit_insn (gen_sse2_loadlpd (op0, zero, m));
15424           m = adjust_address (op1, DFmode, 8);
15425           emit_insn (gen_sse2_loadhpd (op0, op0, m));
15426         }
15427       else
15428         {
15429           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
15430             {
15431               op0 = gen_lowpart (V4SFmode, op0);
15432               op1 = gen_lowpart (V4SFmode, op1);
15433               emit_insn (gen_sse_movups (op0, op1));
15434               return;
15435             }
15436
15437           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
15438             emit_move_insn (op0, CONST0_RTX (mode));
15439           else
15440             emit_clobber (op0);
15441
15442           if (mode != V4SFmode)
15443             op0 = gen_lowpart (V4SFmode, op0);
15444           m = adjust_address (op1, V2SFmode, 0);
15445           emit_insn (gen_sse_loadlps (op0, op0, m));
15446           m = adjust_address (op1, V2SFmode, 8);
15447           emit_insn (gen_sse_loadhps (op0, op0, m));
15448         }
15449     }
15450   else if (MEM_P (op0))
15451     {
15452       /* If we're optimizing for size, movups is the smallest.  */
15453       if (optimize_insn_for_size_p ()
15454           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
15455         {
15456           op0 = gen_lowpart (V4SFmode, op0);
15457           op1 = gen_lowpart (V4SFmode, op1);
15458           emit_insn (gen_sse_movups (op0, op1));
15459           return;
15460         }
15461
15462       /* ??? Similar to above, only less clear because of quote
15463          typeless stores unquote.  */
15464       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
15465           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
15466         {
15467           op0 = gen_lowpart (V16QImode, op0);
15468           op1 = gen_lowpart (V16QImode, op1);
15469           emit_insn (gen_sse2_movdqu (op0, op1));
15470           return;
15471         }
15472
15473       if (TARGET_SSE2 && mode == V2DFmode)
15474         {
15475           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
15476             {
15477               op0 = gen_lowpart (V2DFmode, op0);
15478               op1 = gen_lowpart (V2DFmode, op1);
15479               emit_insn (gen_sse2_movupd (op0, op1));
15480             }
15481           else
15482             {
15483               m = adjust_address (op0, DFmode, 0);
15484               emit_insn (gen_sse2_storelpd (m, op1));
15485               m = adjust_address (op0, DFmode, 8);
15486               emit_insn (gen_sse2_storehpd (m, op1));
15487             }
15488         }
15489       else
15490         {
15491           if (mode != V4SFmode)
15492             op1 = gen_lowpart (V4SFmode, op1);
15493
15494           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
15495             {
15496               op0 = gen_lowpart (V4SFmode, op0);
15497               emit_insn (gen_sse_movups (op0, op1));
15498             }
15499           else
15500             {
15501               m = adjust_address (op0, V2SFmode, 0);
15502               emit_insn (gen_sse_storelps (m, op1));
15503               m = adjust_address (op0, V2SFmode, 8);
15504               emit_insn (gen_sse_storehps (m, op1));
15505             }
15506         }
15507     }
15508   else
15509     gcc_unreachable ();
15510 }
15511
15512 /* Expand a push in MODE.  This is some mode for which we do not support
15513    proper push instructions, at least from the registers that we expect
15514    the value to live in.  */
15515
15516 void
15517 ix86_expand_push (enum machine_mode mode, rtx x)
15518 {
15519   rtx tmp;
15520
15521   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
15522                              GEN_INT (-GET_MODE_SIZE (mode)),
15523                              stack_pointer_rtx, 1, OPTAB_DIRECT);
15524   if (tmp != stack_pointer_rtx)
15525     emit_move_insn (stack_pointer_rtx, tmp);
15526
15527   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
15528
15529   /* When we push an operand onto stack, it has to be aligned at least
15530      at the function argument boundary.  However since we don't have
15531      the argument type, we can't determine the actual argument
15532      boundary.  */
15533   emit_move_insn (tmp, x);
15534 }
15535
15536 /* Helper function of ix86_fixup_binary_operands to canonicalize
15537    operand order.  Returns true if the operands should be swapped.  */
15538
15539 static bool
15540 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
15541                              rtx operands[])
15542 {
15543   rtx dst = operands[0];
15544   rtx src1 = operands[1];
15545   rtx src2 = operands[2];
15546
15547   /* If the operation is not commutative, we can't do anything.  */
15548   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
15549     return false;
15550
15551   /* Highest priority is that src1 should match dst.  */
15552   if (rtx_equal_p (dst, src1))
15553     return false;
15554   if (rtx_equal_p (dst, src2))
15555     return true;
15556
15557   /* Next highest priority is that immediate constants come second.  */
15558   if (immediate_operand (src2, mode))
15559     return false;
15560   if (immediate_operand (src1, mode))
15561     return true;
15562
15563   /* Lowest priority is that memory references should come second.  */
15564   if (MEM_P (src2))
15565     return false;
15566   if (MEM_P (src1))
15567     return true;
15568
15569   return false;
15570 }
15571
15572
15573 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
15574    destination to use for the operation.  If different from the true
15575    destination in operands[0], a copy operation will be required.  */
15576
15577 rtx
15578 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
15579                             rtx operands[])
15580 {
15581   rtx dst = operands[0];
15582   rtx src1 = operands[1];
15583   rtx src2 = operands[2];
15584
15585   /* Canonicalize operand order.  */
15586   if (ix86_swap_binary_operands_p (code, mode, operands))
15587     {
15588       rtx temp;
15589
15590       /* It is invalid to swap operands of different modes.  */
15591       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
15592
15593       temp = src1;
15594       src1 = src2;
15595       src2 = temp;
15596     }
15597
15598   /* Both source operands cannot be in memory.  */
15599   if (MEM_P (src1) && MEM_P (src2))
15600     {
15601       /* Optimization: Only read from memory once.  */
15602       if (rtx_equal_p (src1, src2))
15603         {
15604           src2 = force_reg (mode, src2);
15605           src1 = src2;
15606         }
15607       else
15608         src2 = force_reg (mode, src2);
15609     }
15610
15611   /* If the destination is memory, and we do not have matching source
15612      operands, do things in registers.  */
15613   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
15614     dst = gen_reg_rtx (mode);
15615
15616   /* Source 1 cannot be a constant.  */
15617   if (CONSTANT_P (src1))
15618     src1 = force_reg (mode, src1);
15619
15620   /* Source 1 cannot be a non-matching memory.  */
15621   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
15622     src1 = force_reg (mode, src1);
15623
15624   operands[1] = src1;
15625   operands[2] = src2;
15626   return dst;
15627 }
15628
15629 /* Similarly, but assume that the destination has already been
15630    set up properly.  */
15631
15632 void
15633 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
15634                                     enum machine_mode mode, rtx operands[])
15635 {
15636   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
15637   gcc_assert (dst == operands[0]);
15638 }
15639
15640 /* Attempt to expand a binary operator.  Make the expansion closer to the
15641    actual machine, then just general_operand, which will allow 3 separate
15642    memory references (one output, two input) in a single insn.  */
15643
15644 void
15645 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
15646                              rtx operands[])
15647 {
15648   rtx src1, src2, dst, op, clob;
15649
15650   dst = ix86_fixup_binary_operands (code, mode, operands);
15651   src1 = operands[1];
15652   src2 = operands[2];
15653
15654  /* Emit the instruction.  */
15655
15656   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
15657   if (reload_in_progress)
15658     {
15659       /* Reload doesn't know about the flags register, and doesn't know that
15660          it doesn't want to clobber it.  We can only do this with PLUS.  */
15661       gcc_assert (code == PLUS);
15662       emit_insn (op);
15663     }
15664   else if (reload_completed
15665            && code == PLUS
15666            && !rtx_equal_p (dst, src1))
15667     {
15668       /* This is going to be an LEA; avoid splitting it later.  */
15669       emit_insn (op);
15670     }
15671   else
15672     {
15673       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15674       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
15675     }
15676
15677   /* Fix up the destination if needed.  */
15678   if (dst != operands[0])
15679     emit_move_insn (operands[0], dst);
15680 }
15681
15682 /* Return TRUE or FALSE depending on whether the binary operator meets the
15683    appropriate constraints.  */
15684
15685 bool
15686 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
15687                          rtx operands[3])
15688 {
15689   rtx dst = operands[0];
15690   rtx src1 = operands[1];
15691   rtx src2 = operands[2];
15692
15693   /* Both source operands cannot be in memory.  */
15694   if (MEM_P (src1) && MEM_P (src2))
15695     return false;
15696
15697   /* Canonicalize operand order for commutative operators.  */
15698   if (ix86_swap_binary_operands_p (code, mode, operands))
15699     {
15700       rtx temp = src1;
15701       src1 = src2;
15702       src2 = temp;
15703     }
15704
15705   /* If the destination is memory, we must have a matching source operand.  */
15706   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
15707       return false;
15708
15709   /* Source 1 cannot be a constant.  */
15710   if (CONSTANT_P (src1))
15711     return false;
15712
15713   /* Source 1 cannot be a non-matching memory.  */
15714   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
15715     {
15716       /* Support "andhi/andsi/anddi" as a zero-extending move.  */
15717       return (code == AND
15718               && (mode == HImode
15719                   || mode == SImode
15720                   || (TARGET_64BIT && mode == DImode))
15721               && CONST_INT_P (src2)
15722               && (INTVAL (src2) == 0xff
15723                   || INTVAL (src2) == 0xffff));
15724     }
15725
15726   return true;
15727 }
15728
15729 /* Attempt to expand a unary operator.  Make the expansion closer to the
15730    actual machine, then just general_operand, which will allow 2 separate
15731    memory references (one output, one input) in a single insn.  */
15732
15733 void
15734 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
15735                             rtx operands[])
15736 {
15737   int matching_memory;
15738   rtx src, dst, op, clob;
15739
15740   dst = operands[0];
15741   src = operands[1];
15742
15743   /* If the destination is memory, and we do not have matching source
15744      operands, do things in registers.  */
15745   matching_memory = 0;
15746   if (MEM_P (dst))
15747     {
15748       if (rtx_equal_p (dst, src))
15749         matching_memory = 1;
15750       else
15751         dst = gen_reg_rtx (mode);
15752     }
15753
15754   /* When source operand is memory, destination must match.  */
15755   if (MEM_P (src) && !matching_memory)
15756     src = force_reg (mode, src);
15757
15758   /* Emit the instruction.  */
15759
15760   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
15761   if (reload_in_progress || code == NOT)
15762     {
15763       /* Reload doesn't know about the flags register, and doesn't know that
15764          it doesn't want to clobber it.  */
15765       gcc_assert (code == NOT);
15766       emit_insn (op);
15767     }
15768   else
15769     {
15770       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15771       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
15772     }
15773
15774   /* Fix up the destination if needed.  */
15775   if (dst != operands[0])
15776     emit_move_insn (operands[0], dst);
15777 }
15778
15779 /* Split 32bit/64bit divmod with 8bit unsigned divmod if dividend and
15780    divisor are within the the range [0-255].  */
15781
15782 void
15783 ix86_split_idivmod (enum machine_mode mode, rtx operands[],
15784                     bool signed_p)
15785 {
15786   rtx end_label, qimode_label;
15787   rtx insn, div, mod;
15788   rtx scratch, tmp0, tmp1, tmp2;
15789   rtx (*gen_divmod4_1) (rtx, rtx, rtx, rtx);
15790   rtx (*gen_zero_extend) (rtx, rtx);
15791   rtx (*gen_test_ccno_1) (rtx, rtx);
15792
15793   switch (mode)
15794     {
15795     case SImode:
15796       gen_divmod4_1 = signed_p ? gen_divmodsi4_1 : gen_udivmodsi4_1;
15797       gen_test_ccno_1 = gen_testsi_ccno_1;
15798       gen_zero_extend = gen_zero_extendqisi2;
15799       break;
15800     case DImode:
15801       gen_divmod4_1 = signed_p ? gen_divmoddi4_1 : gen_udivmoddi4_1;
15802       gen_test_ccno_1 = gen_testdi_ccno_1;
15803       gen_zero_extend = gen_zero_extendqidi2;
15804       break;
15805     default:
15806       gcc_unreachable ();
15807     }
15808
15809   end_label = gen_label_rtx ();
15810   qimode_label = gen_label_rtx ();
15811
15812   scratch = gen_reg_rtx (mode);
15813
15814   /* Use 8bit unsigned divimod if dividend and divisor are within the
15815      the range [0-255].  */
15816   emit_move_insn (scratch, operands[2]);
15817   scratch = expand_simple_binop (mode, IOR, scratch, operands[3],
15818                                  scratch, 1, OPTAB_DIRECT);
15819   emit_insn (gen_test_ccno_1 (scratch, GEN_INT (-0x100)));
15820   tmp0 = gen_rtx_REG (CCNOmode, FLAGS_REG);
15821   tmp0 = gen_rtx_EQ (VOIDmode, tmp0, const0_rtx);
15822   tmp0 = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp0,
15823                                gen_rtx_LABEL_REF (VOIDmode, qimode_label),
15824                                pc_rtx);
15825   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp0));
15826   predict_jump (REG_BR_PROB_BASE * 50 / 100);
15827   JUMP_LABEL (insn) = qimode_label;
15828
15829   /* Generate original signed/unsigned divimod.  */
15830   div = gen_divmod4_1 (operands[0], operands[1],
15831                        operands[2], operands[3]);
15832   emit_insn (div);
15833
15834   /* Branch to the end.  */
15835   emit_jump_insn (gen_jump (end_label));
15836   emit_barrier ();
15837
15838   /* Generate 8bit unsigned divide.  */
15839   emit_label (qimode_label);
15840   /* Don't use operands[0] for result of 8bit divide since not all
15841      registers support QImode ZERO_EXTRACT.  */
15842   tmp0 = simplify_gen_subreg (HImode, scratch, mode, 0);
15843   tmp1 = simplify_gen_subreg (HImode, operands[2], mode, 0);
15844   tmp2 = simplify_gen_subreg (QImode, operands[3], mode, 0);
15845   emit_insn (gen_udivmodhiqi3 (tmp0, tmp1, tmp2));
15846
15847   if (signed_p)
15848     {
15849       div = gen_rtx_DIV (SImode, operands[2], operands[3]);
15850       mod = gen_rtx_MOD (SImode, operands[2], operands[3]);
15851     }
15852   else
15853     {
15854       div = gen_rtx_UDIV (SImode, operands[2], operands[3]);
15855       mod = gen_rtx_UMOD (SImode, operands[2], operands[3]);
15856     }
15857
15858   /* Extract remainder from AH.  */
15859   tmp1 = gen_rtx_ZERO_EXTRACT (mode, tmp0, GEN_INT (8), GEN_INT (8));
15860   if (REG_P (operands[1]))
15861     insn = emit_move_insn (operands[1], tmp1);
15862   else
15863     {
15864       /* Need a new scratch register since the old one has result 
15865          of 8bit divide.  */
15866       scratch = gen_reg_rtx (mode);
15867       emit_move_insn (scratch, tmp1);
15868       insn = emit_move_insn (operands[1], scratch);
15869     }
15870   set_unique_reg_note (insn, REG_EQUAL, mod);
15871
15872   /* Zero extend quotient from AL.  */
15873   tmp1 = gen_lowpart (QImode, tmp0);
15874   insn = emit_insn (gen_zero_extend (operands[0], tmp1));
15875   set_unique_reg_note (insn, REG_EQUAL, div);
15876
15877   emit_label (end_label);
15878 }
15879
15880 #define LEA_SEARCH_THRESHOLD 12
15881
15882 /* Search backward for non-agu definition of register number REGNO1
15883    or register number REGNO2 in INSN's basic block until
15884    1. Pass LEA_SEARCH_THRESHOLD instructions, or
15885    2. Reach BB boundary, or
15886    3. Reach agu definition.
15887    Returns the distance between the non-agu definition point and INSN.
15888    If no definition point, returns -1.  */
15889
15890 static int
15891 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
15892                          rtx insn)
15893 {
15894   basic_block bb = BLOCK_FOR_INSN (insn);
15895   int distance = 0;
15896   df_ref *def_rec;
15897   enum attr_type insn_type;
15898
15899   if (insn != BB_HEAD (bb))
15900     {
15901       rtx prev = PREV_INSN (insn);
15902       while (prev && distance < LEA_SEARCH_THRESHOLD)
15903         {
15904           if (NONDEBUG_INSN_P (prev))
15905             {
15906               distance++;
15907               for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
15908                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
15909                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
15910                     && (regno1 == DF_REF_REGNO (*def_rec)
15911                         || regno2 == DF_REF_REGNO (*def_rec)))
15912                   {
15913                     insn_type = get_attr_type (prev);
15914                     if (insn_type != TYPE_LEA)
15915                       goto done;
15916                   }
15917             }
15918           if (prev == BB_HEAD (bb))
15919             break;
15920           prev = PREV_INSN (prev);
15921         }
15922     }
15923
15924   if (distance < LEA_SEARCH_THRESHOLD)
15925     {
15926       edge e;
15927       edge_iterator ei;
15928       bool simple_loop = false;
15929
15930       FOR_EACH_EDGE (e, ei, bb->preds)
15931         if (e->src == bb)
15932           {
15933             simple_loop = true;
15934             break;
15935           }
15936
15937       if (simple_loop)
15938         {
15939           rtx prev = BB_END (bb);
15940           while (prev
15941                  && prev != insn
15942                  && distance < LEA_SEARCH_THRESHOLD)
15943             {
15944               if (NONDEBUG_INSN_P (prev))
15945                 {
15946                   distance++;
15947                   for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
15948                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
15949                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
15950                         && (regno1 == DF_REF_REGNO (*def_rec)
15951                             || regno2 == DF_REF_REGNO (*def_rec)))
15952                       {
15953                         insn_type = get_attr_type (prev);
15954                         if (insn_type != TYPE_LEA)
15955                           goto done;
15956                       }
15957                 }
15958               prev = PREV_INSN (prev);
15959             }
15960         }
15961     }
15962
15963   distance = -1;
15964
15965 done:
15966   /* get_attr_type may modify recog data.  We want to make sure
15967      that recog data is valid for instruction INSN, on which
15968      distance_non_agu_define is called.  INSN is unchanged here.  */
15969   extract_insn_cached (insn);
15970   return distance;
15971 }
15972
15973 /* Return the distance between INSN and the next insn that uses
15974    register number REGNO0 in memory address.  Return -1 if no such
15975    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
15976
15977 static int
15978 distance_agu_use (unsigned int regno0, rtx insn)
15979 {
15980   basic_block bb = BLOCK_FOR_INSN (insn);
15981   int distance = 0;
15982   df_ref *def_rec;
15983   df_ref *use_rec;
15984
15985   if (insn != BB_END (bb))
15986     {
15987       rtx next = NEXT_INSN (insn);
15988       while (next && distance < LEA_SEARCH_THRESHOLD)
15989         {
15990           if (NONDEBUG_INSN_P (next))
15991             {
15992               distance++;
15993
15994               for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
15995                 if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
15996                      || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
15997                     && regno0 == DF_REF_REGNO (*use_rec))
15998                   {
15999                     /* Return DISTANCE if OP0 is used in memory
16000                        address in NEXT.  */
16001                     return distance;
16002                   }
16003
16004               for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
16005                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
16006                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
16007                     && regno0 == DF_REF_REGNO (*def_rec))
16008                   {
16009                     /* Return -1 if OP0 is set in NEXT.  */
16010                     return -1;
16011                   }
16012             }
16013           if (next == BB_END (bb))
16014             break;
16015           next = NEXT_INSN (next);
16016         }
16017     }
16018
16019   if (distance < LEA_SEARCH_THRESHOLD)
16020     {
16021       edge e;
16022       edge_iterator ei;
16023       bool simple_loop = false;
16024
16025       FOR_EACH_EDGE (e, ei, bb->succs)
16026         if (e->dest == bb)
16027           {
16028             simple_loop = true;
16029             break;
16030           }
16031
16032       if (simple_loop)
16033         {
16034           rtx next = BB_HEAD (bb);
16035           while (next
16036                  && next != insn
16037                  && distance < LEA_SEARCH_THRESHOLD)
16038             {
16039               if (NONDEBUG_INSN_P (next))
16040                 {
16041                   distance++;
16042
16043                   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
16044                     if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
16045                          || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
16046                         && regno0 == DF_REF_REGNO (*use_rec))
16047                       {
16048                         /* Return DISTANCE if OP0 is used in memory
16049                            address in NEXT.  */
16050                         return distance;
16051                       }
16052
16053                   for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
16054                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
16055                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
16056                         && regno0 == DF_REF_REGNO (*def_rec))
16057                       {
16058                         /* Return -1 if OP0 is set in NEXT.  */
16059                         return -1;
16060                       }
16061
16062                 }
16063               next = NEXT_INSN (next);
16064             }
16065         }
16066     }
16067
16068   return -1;
16069 }
16070
16071 /* Define this macro to tune LEA priority vs ADD, it take effect when
16072    there is a dilemma of choicing LEA or ADD
16073    Negative value: ADD is more preferred than LEA
16074    Zero: Netrual
16075    Positive value: LEA is more preferred than ADD*/
16076 #define IX86_LEA_PRIORITY 2
16077
16078 /* Return true if it is ok to optimize an ADD operation to LEA
16079    operation to avoid flag register consumation.  For most processors,
16080    ADD is faster than LEA.  For the processors like ATOM, if the
16081    destination register of LEA holds an actual address which will be
16082    used soon, LEA is better and otherwise ADD is better.  */
16083
16084 bool
16085 ix86_lea_for_add_ok (rtx insn, rtx operands[])
16086 {
16087   unsigned int regno0 = true_regnum (operands[0]);
16088   unsigned int regno1 = true_regnum (operands[1]);
16089   unsigned int regno2 = true_regnum (operands[2]);
16090
16091   /* If a = b + c, (a!=b && a!=c), must use lea form. */
16092   if (regno0 != regno1 && regno0 != regno2)
16093     return true;
16094
16095   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
16096     return false;
16097   else
16098     {
16099       int dist_define, dist_use;
16100
16101       /* Return false if REGNO0 isn't used in memory address. */
16102       dist_use = distance_agu_use (regno0, insn);
16103       if (dist_use <= 0)
16104         return false;
16105
16106       dist_define = distance_non_agu_define (regno1, regno2, insn);
16107       if (dist_define <= 0)
16108         return true;
16109
16110       /* If this insn has both backward non-agu dependence and forward
16111          agu dependence, the one with short distance take effect. */
16112       if ((dist_define + IX86_LEA_PRIORITY) < dist_use)
16113         return false;
16114
16115       return true;
16116     }
16117 }
16118
16119 /* Return true if destination reg of SET_BODY is shift count of
16120    USE_BODY.  */
16121
16122 static bool
16123 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
16124 {
16125   rtx set_dest;
16126   rtx shift_rtx;
16127   int i;
16128
16129   /* Retrieve destination of SET_BODY.  */
16130   switch (GET_CODE (set_body))
16131     {
16132     case SET:
16133       set_dest = SET_DEST (set_body);
16134       if (!set_dest || !REG_P (set_dest))
16135         return false;
16136       break;
16137     case PARALLEL:
16138       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
16139         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
16140                                           use_body))
16141           return true;
16142     default:
16143       return false;
16144       break;
16145     }
16146
16147   /* Retrieve shift count of USE_BODY.  */
16148   switch (GET_CODE (use_body))
16149     {
16150     case SET:
16151       shift_rtx = XEXP (use_body, 1);
16152       break;
16153     case PARALLEL:
16154       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
16155         if (ix86_dep_by_shift_count_body (set_body,
16156                                           XVECEXP (use_body, 0, i)))
16157           return true;
16158     default:
16159       return false;
16160       break;
16161     }
16162
16163   if (shift_rtx
16164       && (GET_CODE (shift_rtx) == ASHIFT
16165           || GET_CODE (shift_rtx) == LSHIFTRT
16166           || GET_CODE (shift_rtx) == ASHIFTRT
16167           || GET_CODE (shift_rtx) == ROTATE
16168           || GET_CODE (shift_rtx) == ROTATERT))
16169     {
16170       rtx shift_count = XEXP (shift_rtx, 1);
16171
16172       /* Return true if shift count is dest of SET_BODY.  */
16173       if (REG_P (shift_count)
16174           && true_regnum (set_dest) == true_regnum (shift_count))
16175         return true;
16176     }
16177
16178   return false;
16179 }
16180
16181 /* Return true if destination reg of SET_INSN is shift count of
16182    USE_INSN.  */
16183
16184 bool
16185 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
16186 {
16187   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
16188                                        PATTERN (use_insn));
16189 }
16190
16191 /* Return TRUE or FALSE depending on whether the unary operator meets the
16192    appropriate constraints.  */
16193
16194 bool
16195 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
16196                         enum machine_mode mode ATTRIBUTE_UNUSED,
16197                         rtx operands[2] ATTRIBUTE_UNUSED)
16198 {
16199   /* If one of operands is memory, source and destination must match.  */
16200   if ((MEM_P (operands[0])
16201        || MEM_P (operands[1]))
16202       && ! rtx_equal_p (operands[0], operands[1]))
16203     return false;
16204   return true;
16205 }
16206
16207 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
16208    are ok, keeping in mind the possible movddup alternative.  */
16209
16210 bool
16211 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
16212 {
16213   if (MEM_P (operands[0]))
16214     return rtx_equal_p (operands[0], operands[1 + high]);
16215   if (MEM_P (operands[1]) && MEM_P (operands[2]))
16216     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
16217   return true;
16218 }
16219
16220 /* Post-reload splitter for converting an SF or DFmode value in an
16221    SSE register into an unsigned SImode.  */
16222
16223 void
16224 ix86_split_convert_uns_si_sse (rtx operands[])
16225 {
16226   enum machine_mode vecmode;
16227   rtx value, large, zero_or_two31, input, two31, x;
16228
16229   large = operands[1];
16230   zero_or_two31 = operands[2];
16231   input = operands[3];
16232   two31 = operands[4];
16233   vecmode = GET_MODE (large);
16234   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
16235
16236   /* Load up the value into the low element.  We must ensure that the other
16237      elements are valid floats -- zero is the easiest such value.  */
16238   if (MEM_P (input))
16239     {
16240       if (vecmode == V4SFmode)
16241         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
16242       else
16243         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
16244     }
16245   else
16246     {
16247       input = gen_rtx_REG (vecmode, REGNO (input));
16248       emit_move_insn (value, CONST0_RTX (vecmode));
16249       if (vecmode == V4SFmode)
16250         emit_insn (gen_sse_movss (value, value, input));
16251       else
16252         emit_insn (gen_sse2_movsd (value, value, input));
16253     }
16254
16255   emit_move_insn (large, two31);
16256   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
16257
16258   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
16259   emit_insn (gen_rtx_SET (VOIDmode, large, x));
16260
16261   x = gen_rtx_AND (vecmode, zero_or_two31, large);
16262   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
16263
16264   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
16265   emit_insn (gen_rtx_SET (VOIDmode, value, x));
16266
16267   large = gen_rtx_REG (V4SImode, REGNO (large));
16268   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
16269
16270   x = gen_rtx_REG (V4SImode, REGNO (value));
16271   if (vecmode == V4SFmode)
16272     emit_insn (gen_sse2_cvttps2dq (x, value));
16273   else
16274     emit_insn (gen_sse2_cvttpd2dq (x, value));
16275   value = x;
16276
16277   emit_insn (gen_xorv4si3 (value, value, large));
16278 }
16279
16280 /* Convert an unsigned DImode value into a DFmode, using only SSE.
16281    Expects the 64-bit DImode to be supplied in a pair of integral
16282    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
16283    -mfpmath=sse, !optimize_size only.  */
16284
16285 void
16286 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
16287 {
16288   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
16289   rtx int_xmm, fp_xmm;
16290   rtx biases, exponents;
16291   rtx x;
16292
16293   int_xmm = gen_reg_rtx (V4SImode);
16294   if (TARGET_INTER_UNIT_MOVES)
16295     emit_insn (gen_movdi_to_sse (int_xmm, input));
16296   else if (TARGET_SSE_SPLIT_REGS)
16297     {
16298       emit_clobber (int_xmm);
16299       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
16300     }
16301   else
16302     {
16303       x = gen_reg_rtx (V2DImode);
16304       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
16305       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
16306     }
16307
16308   x = gen_rtx_CONST_VECTOR (V4SImode,
16309                             gen_rtvec (4, GEN_INT (0x43300000UL),
16310                                        GEN_INT (0x45300000UL),
16311                                        const0_rtx, const0_rtx));
16312   exponents = validize_mem (force_const_mem (V4SImode, x));
16313
16314   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
16315   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
16316
16317   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
16318      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
16319      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
16320      (0x1.0p84 + double(fp_value_hi_xmm)).
16321      Note these exponents differ by 32.  */
16322
16323   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
16324
16325   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
16326      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
16327   real_ldexp (&bias_lo_rvt, &dconst1, 52);
16328   real_ldexp (&bias_hi_rvt, &dconst1, 84);
16329   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
16330   x = const_double_from_real_value (bias_hi_rvt, DFmode);
16331   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
16332   biases = validize_mem (force_const_mem (V2DFmode, biases));
16333   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
16334
16335   /* Add the upper and lower DFmode values together.  */
16336   if (TARGET_SSE3)
16337     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
16338   else
16339     {
16340       x = copy_to_mode_reg (V2DFmode, fp_xmm);
16341       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
16342       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
16343     }
16344
16345   ix86_expand_vector_extract (false, target, fp_xmm, 0);
16346 }
16347
16348 /* Not used, but eases macroization of patterns.  */
16349 void
16350 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
16351                                   rtx input ATTRIBUTE_UNUSED)
16352 {
16353   gcc_unreachable ();
16354 }
16355
16356 /* Convert an unsigned SImode value into a DFmode.  Only currently used
16357    for SSE, but applicable anywhere.  */
16358
16359 void
16360 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
16361 {
16362   REAL_VALUE_TYPE TWO31r;
16363   rtx x, fp;
16364
16365   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
16366                            NULL, 1, OPTAB_DIRECT);
16367
16368   fp = gen_reg_rtx (DFmode);
16369   emit_insn (gen_floatsidf2 (fp, x));
16370
16371   real_ldexp (&TWO31r, &dconst1, 31);
16372   x = const_double_from_real_value (TWO31r, DFmode);
16373
16374   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
16375   if (x != target)
16376     emit_move_insn (target, x);
16377 }
16378
16379 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
16380    32-bit mode; otherwise we have a direct convert instruction.  */
16381
16382 void
16383 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
16384 {
16385   REAL_VALUE_TYPE TWO32r;
16386   rtx fp_lo, fp_hi, x;
16387
16388   fp_lo = gen_reg_rtx (DFmode);
16389   fp_hi = gen_reg_rtx (DFmode);
16390
16391   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
16392
16393   real_ldexp (&TWO32r, &dconst1, 32);
16394   x = const_double_from_real_value (TWO32r, DFmode);
16395   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
16396
16397   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
16398
16399   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
16400                            0, OPTAB_DIRECT);
16401   if (x != target)
16402     emit_move_insn (target, x);
16403 }
16404
16405 /* Convert an unsigned SImode value into a SFmode, using only SSE.
16406    For x86_32, -mfpmath=sse, !optimize_size only.  */
16407 void
16408 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
16409 {
16410   REAL_VALUE_TYPE ONE16r;
16411   rtx fp_hi, fp_lo, int_hi, int_lo, x;
16412
16413   real_ldexp (&ONE16r, &dconst1, 16);
16414   x = const_double_from_real_value (ONE16r, SFmode);
16415   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
16416                                       NULL, 0, OPTAB_DIRECT);
16417   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
16418                                       NULL, 0, OPTAB_DIRECT);
16419   fp_hi = gen_reg_rtx (SFmode);
16420   fp_lo = gen_reg_rtx (SFmode);
16421   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
16422   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
16423   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
16424                                0, OPTAB_DIRECT);
16425   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
16426                                0, OPTAB_DIRECT);
16427   if (!rtx_equal_p (target, fp_hi))
16428     emit_move_insn (target, fp_hi);
16429 }
16430
16431 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
16432    then replicate the value for all elements of the vector
16433    register.  */
16434
16435 rtx
16436 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
16437 {
16438   rtvec v;
16439   switch (mode)
16440     {
16441     case V4SImode:
16442       gcc_assert (vect);
16443       v = gen_rtvec (4, value, value, value, value);
16444       return gen_rtx_CONST_VECTOR (V4SImode, v);
16445
16446     case V2DImode:
16447       gcc_assert (vect);
16448       v = gen_rtvec (2, value, value);
16449       return gen_rtx_CONST_VECTOR (V2DImode, v);
16450
16451     case V8SFmode:
16452       if (vect)
16453         v = gen_rtvec (8, value, value, value, value,
16454                        value, value, value, value);
16455       else
16456         v = gen_rtvec (8, value, CONST0_RTX (SFmode),
16457                        CONST0_RTX (SFmode), CONST0_RTX (SFmode),
16458                        CONST0_RTX (SFmode), CONST0_RTX (SFmode),
16459                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
16460       return gen_rtx_CONST_VECTOR (V8SFmode, v);
16461
16462     case V4SFmode:
16463       if (vect)
16464         v = gen_rtvec (4, value, value, value, value);
16465       else
16466         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
16467                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
16468       return gen_rtx_CONST_VECTOR (V4SFmode, v);
16469
16470     case V4DFmode:
16471       if (vect)
16472         v = gen_rtvec (4, value, value, value, value);
16473       else
16474         v = gen_rtvec (4, value, CONST0_RTX (DFmode),
16475                        CONST0_RTX (DFmode), CONST0_RTX (DFmode));
16476       return gen_rtx_CONST_VECTOR (V4DFmode, v);
16477
16478     case V2DFmode:
16479       if (vect)
16480         v = gen_rtvec (2, value, value);
16481       else
16482         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
16483       return gen_rtx_CONST_VECTOR (V2DFmode, v);
16484
16485     default:
16486       gcc_unreachable ();
16487     }
16488 }
16489
16490 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
16491    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
16492    for an SSE register.  If VECT is true, then replicate the mask for
16493    all elements of the vector register.  If INVERT is true, then create
16494    a mask excluding the sign bit.  */
16495
16496 rtx
16497 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
16498 {
16499   enum machine_mode vec_mode, imode;
16500   HOST_WIDE_INT hi, lo;
16501   int shift = 63;
16502   rtx v;
16503   rtx mask;
16504
16505   /* Find the sign bit, sign extended to 2*HWI.  */
16506   switch (mode)
16507     {
16508     case V4SImode:
16509     case V8SFmode:
16510     case V4SFmode:
16511       vec_mode = mode;
16512       mode = GET_MODE_INNER (mode);
16513       imode = SImode;
16514       lo = 0x80000000, hi = lo < 0;
16515       break;
16516
16517     case V2DImode:
16518     case V4DFmode:
16519     case V2DFmode:
16520       vec_mode = mode;
16521       mode = GET_MODE_INNER (mode);
16522       imode = DImode;
16523       if (HOST_BITS_PER_WIDE_INT >= 64)
16524         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
16525       else
16526         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
16527       break;
16528
16529     case TImode:
16530     case TFmode:
16531       vec_mode = VOIDmode;
16532       if (HOST_BITS_PER_WIDE_INT >= 64)
16533         {
16534           imode = TImode;
16535           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
16536         }
16537       else
16538         {
16539           rtvec vec;
16540
16541           imode = DImode;
16542           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
16543
16544           if (invert)
16545             {
16546               lo = ~lo, hi = ~hi;
16547               v = constm1_rtx;
16548             }
16549           else
16550             v = const0_rtx;
16551
16552           mask = immed_double_const (lo, hi, imode);
16553
16554           vec = gen_rtvec (2, v, mask);
16555           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
16556           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
16557
16558           return v;
16559         }
16560      break;
16561
16562     default:
16563       gcc_unreachable ();
16564     }
16565
16566   if (invert)
16567     lo = ~lo, hi = ~hi;
16568
16569   /* Force this value into the low part of a fp vector constant.  */
16570   mask = immed_double_const (lo, hi, imode);
16571   mask = gen_lowpart (mode, mask);
16572
16573   if (vec_mode == VOIDmode)
16574     return force_reg (mode, mask);
16575
16576   v = ix86_build_const_vector (vec_mode, vect, mask);
16577   return force_reg (vec_mode, v);
16578 }
16579
16580 /* Generate code for floating point ABS or NEG.  */
16581
16582 void
16583 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
16584                                 rtx operands[])
16585 {
16586   rtx mask, set, dst, src;
16587   bool use_sse = false;
16588   bool vector_mode = VECTOR_MODE_P (mode);
16589   enum machine_mode vmode = mode;
16590
16591   if (vector_mode)
16592     use_sse = true;
16593   else if (mode == TFmode)
16594     use_sse = true;
16595   else if (TARGET_SSE_MATH)
16596     {
16597       use_sse = SSE_FLOAT_MODE_P (mode);
16598       if (mode == SFmode)
16599         vmode = V4SFmode;
16600       else if (mode == DFmode)
16601         vmode = V2DFmode;
16602     }
16603
16604   /* NEG and ABS performed with SSE use bitwise mask operations.
16605      Create the appropriate mask now.  */
16606   if (use_sse)
16607     mask = ix86_build_signbit_mask (vmode, vector_mode, code == ABS);
16608   else
16609     mask = NULL_RTX;
16610
16611   dst = operands[0];
16612   src = operands[1];
16613
16614   set = gen_rtx_fmt_e (code, mode, src);
16615   set = gen_rtx_SET (VOIDmode, dst, set);
16616
16617   if (mask)
16618     {
16619       rtx use, clob;
16620       rtvec par;
16621
16622       use = gen_rtx_USE (VOIDmode, mask);
16623       if (vector_mode)
16624         par = gen_rtvec (2, set, use);
16625       else
16626         {
16627           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
16628           par = gen_rtvec (3, set, use, clob);
16629         }
16630       emit_insn (gen_rtx_PARALLEL (VOIDmode, par));
16631     }
16632   else
16633     emit_insn (set);
16634 }
16635
16636 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
16637
16638 void
16639 ix86_expand_copysign (rtx operands[])
16640 {
16641   enum machine_mode mode, vmode;
16642   rtx dest, op0, op1, mask, nmask;
16643
16644   dest = operands[0];
16645   op0 = operands[1];
16646   op1 = operands[2];
16647
16648   mode = GET_MODE (dest);
16649
16650   if (mode == SFmode)
16651     vmode = V4SFmode;
16652   else if (mode == DFmode)
16653     vmode = V2DFmode;
16654   else
16655     vmode = mode;
16656
16657   if (GET_CODE (op0) == CONST_DOUBLE)
16658     {
16659       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
16660
16661       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
16662         op0 = simplify_unary_operation (ABS, mode, op0, mode);
16663
16664       if (mode == SFmode || mode == DFmode)
16665         {
16666           if (op0 == CONST0_RTX (mode))
16667             op0 = CONST0_RTX (vmode);
16668           else
16669             {
16670               rtx v = ix86_build_const_vector (vmode, false, op0);
16671
16672               op0 = force_reg (vmode, v);
16673             }
16674         }
16675       else if (op0 != CONST0_RTX (mode))
16676         op0 = force_reg (mode, op0);
16677
16678       mask = ix86_build_signbit_mask (vmode, 0, 0);
16679
16680       if (mode == SFmode)
16681         copysign_insn = gen_copysignsf3_const;
16682       else if (mode == DFmode)
16683         copysign_insn = gen_copysigndf3_const;
16684       else
16685         copysign_insn = gen_copysigntf3_const;
16686
16687         emit_insn (copysign_insn (dest, op0, op1, mask));
16688     }
16689   else
16690     {
16691       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
16692
16693       nmask = ix86_build_signbit_mask (vmode, 0, 1);
16694       mask = ix86_build_signbit_mask (vmode, 0, 0);
16695
16696       if (mode == SFmode)
16697         copysign_insn = gen_copysignsf3_var;
16698       else if (mode == DFmode)
16699         copysign_insn = gen_copysigndf3_var;
16700       else
16701         copysign_insn = gen_copysigntf3_var;
16702
16703       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
16704     }
16705 }
16706
16707 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
16708    be a constant, and so has already been expanded into a vector constant.  */
16709
16710 void
16711 ix86_split_copysign_const (rtx operands[])
16712 {
16713   enum machine_mode mode, vmode;
16714   rtx dest, op0, mask, x;
16715
16716   dest = operands[0];
16717   op0 = operands[1];
16718   mask = operands[3];
16719
16720   mode = GET_MODE (dest);
16721   vmode = GET_MODE (mask);
16722
16723   dest = simplify_gen_subreg (vmode, dest, mode, 0);
16724   x = gen_rtx_AND (vmode, dest, mask);
16725   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16726
16727   if (op0 != CONST0_RTX (vmode))
16728     {
16729       x = gen_rtx_IOR (vmode, dest, op0);
16730       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16731     }
16732 }
16733
16734 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
16735    so we have to do two masks.  */
16736
16737 void
16738 ix86_split_copysign_var (rtx operands[])
16739 {
16740   enum machine_mode mode, vmode;
16741   rtx dest, scratch, op0, op1, mask, nmask, x;
16742
16743   dest = operands[0];
16744   scratch = operands[1];
16745   op0 = operands[2];
16746   op1 = operands[3];
16747   nmask = operands[4];
16748   mask = operands[5];
16749
16750   mode = GET_MODE (dest);
16751   vmode = GET_MODE (mask);
16752
16753   if (rtx_equal_p (op0, op1))
16754     {
16755       /* Shouldn't happen often (it's useless, obviously), but when it does
16756          we'd generate incorrect code if we continue below.  */
16757       emit_move_insn (dest, op0);
16758       return;
16759     }
16760
16761   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
16762     {
16763       gcc_assert (REGNO (op1) == REGNO (scratch));
16764
16765       x = gen_rtx_AND (vmode, scratch, mask);
16766       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
16767
16768       dest = mask;
16769       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
16770       x = gen_rtx_NOT (vmode, dest);
16771       x = gen_rtx_AND (vmode, x, op0);
16772       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16773     }
16774   else
16775     {
16776       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
16777         {
16778           x = gen_rtx_AND (vmode, scratch, mask);
16779         }
16780       else                                              /* alternative 2,4 */
16781         {
16782           gcc_assert (REGNO (mask) == REGNO (scratch));
16783           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
16784           x = gen_rtx_AND (vmode, scratch, op1);
16785         }
16786       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
16787
16788       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
16789         {
16790           dest = simplify_gen_subreg (vmode, op0, mode, 0);
16791           x = gen_rtx_AND (vmode, dest, nmask);
16792         }
16793       else                                              /* alternative 3,4 */
16794         {
16795           gcc_assert (REGNO (nmask) == REGNO (dest));
16796           dest = nmask;
16797           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
16798           x = gen_rtx_AND (vmode, dest, op0);
16799         }
16800       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16801     }
16802
16803   x = gen_rtx_IOR (vmode, dest, scratch);
16804   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16805 }
16806
16807 /* Return TRUE or FALSE depending on whether the first SET in INSN
16808    has source and destination with matching CC modes, and that the
16809    CC mode is at least as constrained as REQ_MODE.  */
16810
16811 bool
16812 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
16813 {
16814   rtx set;
16815   enum machine_mode set_mode;
16816
16817   set = PATTERN (insn);
16818   if (GET_CODE (set) == PARALLEL)
16819     set = XVECEXP (set, 0, 0);
16820   gcc_assert (GET_CODE (set) == SET);
16821   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
16822
16823   set_mode = GET_MODE (SET_DEST (set));
16824   switch (set_mode)
16825     {
16826     case CCNOmode:
16827       if (req_mode != CCNOmode
16828           && (req_mode != CCmode
16829               || XEXP (SET_SRC (set), 1) != const0_rtx))
16830         return false;
16831       break;
16832     case CCmode:
16833       if (req_mode == CCGCmode)
16834         return false;
16835       /* FALLTHRU */
16836     case CCGCmode:
16837       if (req_mode == CCGOCmode || req_mode == CCNOmode)
16838         return false;
16839       /* FALLTHRU */
16840     case CCGOCmode:
16841       if (req_mode == CCZmode)
16842         return false;
16843       /* FALLTHRU */
16844     case CCAmode:
16845     case CCCmode:
16846     case CCOmode:
16847     case CCSmode:
16848     case CCZmode:
16849       break;
16850
16851     default:
16852       gcc_unreachable ();
16853     }
16854
16855   return GET_MODE (SET_SRC (set)) == set_mode;
16856 }
16857
16858 /* Generate insn patterns to do an integer compare of OPERANDS.  */
16859
16860 static rtx
16861 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
16862 {
16863   enum machine_mode cmpmode;
16864   rtx tmp, flags;
16865
16866   cmpmode = SELECT_CC_MODE (code, op0, op1);
16867   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
16868
16869   /* This is very simple, but making the interface the same as in the
16870      FP case makes the rest of the code easier.  */
16871   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
16872   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
16873
16874   /* Return the test that should be put into the flags user, i.e.
16875      the bcc, scc, or cmov instruction.  */
16876   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
16877 }
16878
16879 /* Figure out whether to use ordered or unordered fp comparisons.
16880    Return the appropriate mode to use.  */
16881
16882 enum machine_mode
16883 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
16884 {
16885   /* ??? In order to make all comparisons reversible, we do all comparisons
16886      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
16887      all forms trapping and nontrapping comparisons, we can make inequality
16888      comparisons trapping again, since it results in better code when using
16889      FCOM based compares.  */
16890   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
16891 }
16892
16893 enum machine_mode
16894 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
16895 {
16896   enum machine_mode mode = GET_MODE (op0);
16897
16898   if (SCALAR_FLOAT_MODE_P (mode))
16899     {
16900       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
16901       return ix86_fp_compare_mode (code);
16902     }
16903
16904   switch (code)
16905     {
16906       /* Only zero flag is needed.  */
16907     case EQ:                    /* ZF=0 */
16908     case NE:                    /* ZF!=0 */
16909       return CCZmode;
16910       /* Codes needing carry flag.  */
16911     case GEU:                   /* CF=0 */
16912     case LTU:                   /* CF=1 */
16913       /* Detect overflow checks.  They need just the carry flag.  */
16914       if (GET_CODE (op0) == PLUS
16915           && rtx_equal_p (op1, XEXP (op0, 0)))
16916         return CCCmode;
16917       else
16918         return CCmode;
16919     case GTU:                   /* CF=0 & ZF=0 */
16920     case LEU:                   /* CF=1 | ZF=1 */
16921       /* Detect overflow checks.  They need just the carry flag.  */
16922       if (GET_CODE (op0) == MINUS
16923           && rtx_equal_p (op1, XEXP (op0, 0)))
16924         return CCCmode;
16925       else
16926         return CCmode;
16927       /* Codes possibly doable only with sign flag when
16928          comparing against zero.  */
16929     case GE:                    /* SF=OF   or   SF=0 */
16930     case LT:                    /* SF<>OF  or   SF=1 */
16931       if (op1 == const0_rtx)
16932         return CCGOCmode;
16933       else
16934         /* For other cases Carry flag is not required.  */
16935         return CCGCmode;
16936       /* Codes doable only with sign flag when comparing
16937          against zero, but we miss jump instruction for it
16938          so we need to use relational tests against overflow
16939          that thus needs to be zero.  */
16940     case GT:                    /* ZF=0 & SF=OF */
16941     case LE:                    /* ZF=1 | SF<>OF */
16942       if (op1 == const0_rtx)
16943         return CCNOmode;
16944       else
16945         return CCGCmode;
16946       /* strcmp pattern do (use flags) and combine may ask us for proper
16947          mode.  */
16948     case USE:
16949       return CCmode;
16950     default:
16951       gcc_unreachable ();
16952     }
16953 }
16954
16955 /* Return the fixed registers used for condition codes.  */
16956
16957 static bool
16958 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
16959 {
16960   *p1 = FLAGS_REG;
16961   *p2 = FPSR_REG;
16962   return true;
16963 }
16964
16965 /* If two condition code modes are compatible, return a condition code
16966    mode which is compatible with both.  Otherwise, return
16967    VOIDmode.  */
16968
16969 static enum machine_mode
16970 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
16971 {
16972   if (m1 == m2)
16973     return m1;
16974
16975   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
16976     return VOIDmode;
16977
16978   if ((m1 == CCGCmode && m2 == CCGOCmode)
16979       || (m1 == CCGOCmode && m2 == CCGCmode))
16980     return CCGCmode;
16981
16982   switch (m1)
16983     {
16984     default:
16985       gcc_unreachable ();
16986
16987     case CCmode:
16988     case CCGCmode:
16989     case CCGOCmode:
16990     case CCNOmode:
16991     case CCAmode:
16992     case CCCmode:
16993     case CCOmode:
16994     case CCSmode:
16995     case CCZmode:
16996       switch (m2)
16997         {
16998         default:
16999           return VOIDmode;
17000
17001         case CCmode:
17002         case CCGCmode:
17003         case CCGOCmode:
17004         case CCNOmode:
17005         case CCAmode:
17006         case CCCmode:
17007         case CCOmode:
17008         case CCSmode:
17009         case CCZmode:
17010           return CCmode;
17011         }
17012
17013     case CCFPmode:
17014     case CCFPUmode:
17015       /* These are only compatible with themselves, which we already
17016          checked above.  */
17017       return VOIDmode;
17018     }
17019 }
17020
17021
17022 /* Return a comparison we can do and that it is equivalent to
17023    swap_condition (code) apart possibly from orderedness.
17024    But, never change orderedness if TARGET_IEEE_FP, returning
17025    UNKNOWN in that case if necessary.  */
17026
17027 static enum rtx_code
17028 ix86_fp_swap_condition (enum rtx_code code)
17029 {
17030   switch (code)
17031     {
17032     case GT:                   /* GTU - CF=0 & ZF=0 */
17033       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
17034     case GE:                   /* GEU - CF=0 */
17035       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
17036     case UNLT:                 /* LTU - CF=1 */
17037       return TARGET_IEEE_FP ? UNKNOWN : GT;
17038     case UNLE:                 /* LEU - CF=1 | ZF=1 */
17039       return TARGET_IEEE_FP ? UNKNOWN : GE;
17040     default:
17041       return swap_condition (code);
17042     }
17043 }
17044
17045 /* Return cost of comparison CODE using the best strategy for performance.
17046    All following functions do use number of instructions as a cost metrics.
17047    In future this should be tweaked to compute bytes for optimize_size and
17048    take into account performance of various instructions on various CPUs.  */
17049
17050 static int
17051 ix86_fp_comparison_cost (enum rtx_code code)
17052 {
17053   int arith_cost;
17054
17055   /* The cost of code using bit-twiddling on %ah.  */
17056   switch (code)
17057     {
17058     case UNLE:
17059     case UNLT:
17060     case LTGT:
17061     case GT:
17062     case GE:
17063     case UNORDERED:
17064     case ORDERED:
17065     case UNEQ:
17066       arith_cost = 4;
17067       break;
17068     case LT:
17069     case NE:
17070     case EQ:
17071     case UNGE:
17072       arith_cost = TARGET_IEEE_FP ? 5 : 4;
17073       break;
17074     case LE:
17075     case UNGT:
17076       arith_cost = TARGET_IEEE_FP ? 6 : 4;
17077       break;
17078     default:
17079       gcc_unreachable ();
17080     }
17081
17082   switch (ix86_fp_comparison_strategy (code))
17083     {
17084     case IX86_FPCMP_COMI:
17085       return arith_cost > 4 ? 3 : 2;
17086     case IX86_FPCMP_SAHF:
17087       return arith_cost > 4 ? 4 : 3;
17088     default:
17089       return arith_cost;
17090     }
17091 }
17092
17093 /* Return strategy to use for floating-point.  We assume that fcomi is always
17094    preferrable where available, since that is also true when looking at size
17095    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
17096
17097 enum ix86_fpcmp_strategy
17098 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
17099 {
17100   /* Do fcomi/sahf based test when profitable.  */
17101
17102   if (TARGET_CMOVE)
17103     return IX86_FPCMP_COMI;
17104
17105   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
17106     return IX86_FPCMP_SAHF;
17107
17108   return IX86_FPCMP_ARITH;
17109 }
17110
17111 /* Swap, force into registers, or otherwise massage the two operands
17112    to a fp comparison.  The operands are updated in place; the new
17113    comparison code is returned.  */
17114
17115 static enum rtx_code
17116 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
17117 {
17118   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
17119   rtx op0 = *pop0, op1 = *pop1;
17120   enum machine_mode op_mode = GET_MODE (op0);
17121   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
17122
17123   /* All of the unordered compare instructions only work on registers.
17124      The same is true of the fcomi compare instructions.  The XFmode
17125      compare instructions require registers except when comparing
17126      against zero or when converting operand 1 from fixed point to
17127      floating point.  */
17128
17129   if (!is_sse
17130       && (fpcmp_mode == CCFPUmode
17131           || (op_mode == XFmode
17132               && ! (standard_80387_constant_p (op0) == 1
17133                     || standard_80387_constant_p (op1) == 1)
17134               && GET_CODE (op1) != FLOAT)
17135           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
17136     {
17137       op0 = force_reg (op_mode, op0);
17138       op1 = force_reg (op_mode, op1);
17139     }
17140   else
17141     {
17142       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
17143          things around if they appear profitable, otherwise force op0
17144          into a register.  */
17145
17146       if (standard_80387_constant_p (op0) == 0
17147           || (MEM_P (op0)
17148               && ! (standard_80387_constant_p (op1) == 0
17149                     || MEM_P (op1))))
17150         {
17151           enum rtx_code new_code = ix86_fp_swap_condition (code);
17152           if (new_code != UNKNOWN)
17153             {
17154               rtx tmp;
17155               tmp = op0, op0 = op1, op1 = tmp;
17156               code = new_code;
17157             }
17158         }
17159
17160       if (!REG_P (op0))
17161         op0 = force_reg (op_mode, op0);
17162
17163       if (CONSTANT_P (op1))
17164         {
17165           int tmp = standard_80387_constant_p (op1);
17166           if (tmp == 0)
17167             op1 = validize_mem (force_const_mem (op_mode, op1));
17168           else if (tmp == 1)
17169             {
17170               if (TARGET_CMOVE)
17171                 op1 = force_reg (op_mode, op1);
17172             }
17173           else
17174             op1 = force_reg (op_mode, op1);
17175         }
17176     }
17177
17178   /* Try to rearrange the comparison to make it cheaper.  */
17179   if (ix86_fp_comparison_cost (code)
17180       > ix86_fp_comparison_cost (swap_condition (code))
17181       && (REG_P (op1) || can_create_pseudo_p ()))
17182     {
17183       rtx tmp;
17184       tmp = op0, op0 = op1, op1 = tmp;
17185       code = swap_condition (code);
17186       if (!REG_P (op0))
17187         op0 = force_reg (op_mode, op0);
17188     }
17189
17190   *pop0 = op0;
17191   *pop1 = op1;
17192   return code;
17193 }
17194
17195 /* Convert comparison codes we use to represent FP comparison to integer
17196    code that will result in proper branch.  Return UNKNOWN if no such code
17197    is available.  */
17198
17199 enum rtx_code
17200 ix86_fp_compare_code_to_integer (enum rtx_code code)
17201 {
17202   switch (code)
17203     {
17204     case GT:
17205       return GTU;
17206     case GE:
17207       return GEU;
17208     case ORDERED:
17209     case UNORDERED:
17210       return code;
17211       break;
17212     case UNEQ:
17213       return EQ;
17214       break;
17215     case UNLT:
17216       return LTU;
17217       break;
17218     case UNLE:
17219       return LEU;
17220       break;
17221     case LTGT:
17222       return NE;
17223       break;
17224     default:
17225       return UNKNOWN;
17226     }
17227 }
17228
17229 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
17230
17231 static rtx
17232 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
17233 {
17234   enum machine_mode fpcmp_mode, intcmp_mode;
17235   rtx tmp, tmp2;
17236
17237   fpcmp_mode = ix86_fp_compare_mode (code);
17238   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
17239
17240   /* Do fcomi/sahf based test when profitable.  */
17241   switch (ix86_fp_comparison_strategy (code))
17242     {
17243     case IX86_FPCMP_COMI:
17244       intcmp_mode = fpcmp_mode;
17245       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
17246       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
17247                          tmp);
17248       emit_insn (tmp);
17249       break;
17250
17251     case IX86_FPCMP_SAHF:
17252       intcmp_mode = fpcmp_mode;
17253       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
17254       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
17255                          tmp);
17256
17257       if (!scratch)
17258         scratch = gen_reg_rtx (HImode);
17259       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
17260       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
17261       break;
17262
17263     case IX86_FPCMP_ARITH:
17264       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
17265       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
17266       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
17267       if (!scratch)
17268         scratch = gen_reg_rtx (HImode);
17269       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
17270
17271       /* In the unordered case, we have to check C2 for NaN's, which
17272          doesn't happen to work out to anything nice combination-wise.
17273          So do some bit twiddling on the value we've got in AH to come
17274          up with an appropriate set of condition codes.  */
17275
17276       intcmp_mode = CCNOmode;
17277       switch (code)
17278         {
17279         case GT:
17280         case UNGT:
17281           if (code == GT || !TARGET_IEEE_FP)
17282             {
17283               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
17284               code = EQ;
17285             }
17286           else
17287             {
17288               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17289               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
17290               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
17291               intcmp_mode = CCmode;
17292               code = GEU;
17293             }
17294           break;
17295         case LT:
17296         case UNLT:
17297           if (code == LT && TARGET_IEEE_FP)
17298             {
17299               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17300               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
17301               intcmp_mode = CCmode;
17302               code = EQ;
17303             }
17304           else
17305             {
17306               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
17307               code = NE;
17308             }
17309           break;
17310         case GE:
17311         case UNGE:
17312           if (code == GE || !TARGET_IEEE_FP)
17313             {
17314               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
17315               code = EQ;
17316             }
17317           else
17318             {
17319               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17320               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
17321               code = NE;
17322             }
17323           break;
17324         case LE:
17325         case UNLE:
17326           if (code == LE && TARGET_IEEE_FP)
17327             {
17328               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17329               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
17330               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
17331               intcmp_mode = CCmode;
17332               code = LTU;
17333             }
17334           else
17335             {
17336               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
17337               code = NE;
17338             }
17339           break;
17340         case EQ:
17341         case UNEQ:
17342           if (code == EQ && TARGET_IEEE_FP)
17343             {
17344               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17345               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
17346               intcmp_mode = CCmode;
17347               code = EQ;
17348             }
17349           else
17350             {
17351               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
17352               code = NE;
17353             }
17354           break;
17355         case NE:
17356         case LTGT:
17357           if (code == NE && TARGET_IEEE_FP)
17358             {
17359               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
17360               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
17361                                              GEN_INT (0x40)));
17362               code = NE;
17363             }
17364           else
17365             {
17366               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
17367               code = EQ;
17368             }
17369           break;
17370
17371         case UNORDERED:
17372           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
17373           code = NE;
17374           break;
17375         case ORDERED:
17376           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
17377           code = EQ;
17378           break;
17379
17380         default:
17381           gcc_unreachable ();
17382         }
17383         break;
17384
17385     default:
17386       gcc_unreachable();
17387     }
17388
17389   /* Return the test that should be put into the flags user, i.e.
17390      the bcc, scc, or cmov instruction.  */
17391   return gen_rtx_fmt_ee (code, VOIDmode,
17392                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
17393                          const0_rtx);
17394 }
17395
17396 static rtx
17397 ix86_expand_compare (enum rtx_code code, rtx op0, rtx op1)
17398 {
17399   rtx ret;
17400
17401   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
17402     ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
17403
17404   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
17405     {
17406       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
17407       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
17408     }
17409   else
17410     ret = ix86_expand_int_compare (code, op0, op1);
17411
17412   return ret;
17413 }
17414
17415 void
17416 ix86_expand_branch (enum rtx_code code, rtx op0, rtx op1, rtx label)
17417 {
17418   enum machine_mode mode = GET_MODE (op0);
17419   rtx tmp;
17420
17421   switch (mode)
17422     {
17423     case SFmode:
17424     case DFmode:
17425     case XFmode:
17426     case QImode:
17427     case HImode:
17428     case SImode:
17429       simple:
17430       tmp = ix86_expand_compare (code, op0, op1);
17431       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
17432                                   gen_rtx_LABEL_REF (VOIDmode, label),
17433                                   pc_rtx);
17434       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
17435       return;
17436
17437     case DImode:
17438       if (TARGET_64BIT)
17439         goto simple;
17440     case TImode:
17441       /* Expand DImode branch into multiple compare+branch.  */
17442       {
17443         rtx lo[2], hi[2], label2;
17444         enum rtx_code code1, code2, code3;
17445         enum machine_mode submode;
17446
17447         if (CONSTANT_P (op0) && !CONSTANT_P (op1))
17448           {
17449             tmp = op0, op0 = op1, op1 = tmp;
17450             code = swap_condition (code);
17451           }
17452
17453         split_double_mode (mode, &op0, 1, lo+0, hi+0);
17454         split_double_mode (mode, &op1, 1, lo+1, hi+1);
17455
17456         submode = mode == DImode ? SImode : DImode;
17457
17458         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
17459            avoid two branches.  This costs one extra insn, so disable when
17460            optimizing for size.  */
17461
17462         if ((code == EQ || code == NE)
17463             && (!optimize_insn_for_size_p ()
17464                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
17465           {
17466             rtx xor0, xor1;
17467
17468             xor1 = hi[0];
17469             if (hi[1] != const0_rtx)
17470               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
17471                                    NULL_RTX, 0, OPTAB_WIDEN);
17472
17473             xor0 = lo[0];
17474             if (lo[1] != const0_rtx)
17475               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
17476                                    NULL_RTX, 0, OPTAB_WIDEN);
17477
17478             tmp = expand_binop (submode, ior_optab, xor1, xor0,
17479                                 NULL_RTX, 0, OPTAB_WIDEN);
17480
17481             ix86_expand_branch (code, tmp, const0_rtx, label);
17482             return;
17483           }
17484
17485         /* Otherwise, if we are doing less-than or greater-or-equal-than,
17486            op1 is a constant and the low word is zero, then we can just
17487            examine the high word.  Similarly for low word -1 and
17488            less-or-equal-than or greater-than.  */
17489
17490         if (CONST_INT_P (hi[1]))
17491           switch (code)
17492             {
17493             case LT: case LTU: case GE: case GEU:
17494               if (lo[1] == const0_rtx)
17495                 {
17496                   ix86_expand_branch (code, hi[0], hi[1], label);
17497                   return;
17498                 }
17499               break;
17500             case LE: case LEU: case GT: case GTU:
17501               if (lo[1] == constm1_rtx)
17502                 {
17503                   ix86_expand_branch (code, hi[0], hi[1], label);
17504                   return;
17505                 }
17506               break;
17507             default:
17508               break;
17509             }
17510
17511         /* Otherwise, we need two or three jumps.  */
17512
17513         label2 = gen_label_rtx ();
17514
17515         code1 = code;
17516         code2 = swap_condition (code);
17517         code3 = unsigned_condition (code);
17518
17519         switch (code)
17520           {
17521           case LT: case GT: case LTU: case GTU:
17522             break;
17523
17524           case LE:   code1 = LT;  code2 = GT;  break;
17525           case GE:   code1 = GT;  code2 = LT;  break;
17526           case LEU:  code1 = LTU; code2 = GTU; break;
17527           case GEU:  code1 = GTU; code2 = LTU; break;
17528
17529           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
17530           case NE:   code2 = UNKNOWN; break;
17531
17532           default:
17533             gcc_unreachable ();
17534           }
17535
17536         /*
17537          * a < b =>
17538          *    if (hi(a) < hi(b)) goto true;
17539          *    if (hi(a) > hi(b)) goto false;
17540          *    if (lo(a) < lo(b)) goto true;
17541          *  false:
17542          */
17543
17544         if (code1 != UNKNOWN)
17545           ix86_expand_branch (code1, hi[0], hi[1], label);
17546         if (code2 != UNKNOWN)
17547           ix86_expand_branch (code2, hi[0], hi[1], label2);
17548
17549         ix86_expand_branch (code3, lo[0], lo[1], label);
17550
17551         if (code2 != UNKNOWN)
17552           emit_label (label2);
17553         return;
17554       }
17555
17556     default:
17557       gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC);
17558       goto simple;
17559     }
17560 }
17561
17562 /* Split branch based on floating point condition.  */
17563 void
17564 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
17565                       rtx target1, rtx target2, rtx tmp, rtx pushed)
17566 {
17567   rtx condition;
17568   rtx i;
17569
17570   if (target2 != pc_rtx)
17571     {
17572       rtx tmp = target2;
17573       code = reverse_condition_maybe_unordered (code);
17574       target2 = target1;
17575       target1 = tmp;
17576     }
17577
17578   condition = ix86_expand_fp_compare (code, op1, op2,
17579                                       tmp);
17580
17581   /* Remove pushed operand from stack.  */
17582   if (pushed)
17583     ix86_free_from_memory (GET_MODE (pushed));
17584
17585   i = emit_jump_insn (gen_rtx_SET
17586                       (VOIDmode, pc_rtx,
17587                        gen_rtx_IF_THEN_ELSE (VOIDmode,
17588                                              condition, target1, target2)));
17589   if (split_branch_probability >= 0)
17590     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
17591 }
17592
17593 void
17594 ix86_expand_setcc (rtx dest, enum rtx_code code, rtx op0, rtx op1)
17595 {
17596   rtx ret;
17597
17598   gcc_assert (GET_MODE (dest) == QImode);
17599
17600   ret = ix86_expand_compare (code, op0, op1);
17601   PUT_MODE (ret, QImode);
17602   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
17603 }
17604
17605 /* Expand comparison setting or clearing carry flag.  Return true when
17606    successful and set pop for the operation.  */
17607 static bool
17608 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
17609 {
17610   enum machine_mode mode =
17611     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
17612
17613   /* Do not handle double-mode compares that go through special path.  */
17614   if (mode == (TARGET_64BIT ? TImode : DImode))
17615     return false;
17616
17617   if (SCALAR_FLOAT_MODE_P (mode))
17618     {
17619       rtx compare_op, compare_seq;
17620
17621       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
17622
17623       /* Shortcut:  following common codes never translate
17624          into carry flag compares.  */
17625       if (code == EQ || code == NE || code == UNEQ || code == LTGT
17626           || code == ORDERED || code == UNORDERED)
17627         return false;
17628
17629       /* These comparisons require zero flag; swap operands so they won't.  */
17630       if ((code == GT || code == UNLE || code == LE || code == UNGT)
17631           && !TARGET_IEEE_FP)
17632         {
17633           rtx tmp = op0;
17634           op0 = op1;
17635           op1 = tmp;
17636           code = swap_condition (code);
17637         }
17638
17639       /* Try to expand the comparison and verify that we end up with
17640          carry flag based comparison.  This fails to be true only when
17641          we decide to expand comparison using arithmetic that is not
17642          too common scenario.  */
17643       start_sequence ();
17644       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
17645       compare_seq = get_insns ();
17646       end_sequence ();
17647
17648       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
17649           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
17650         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
17651       else
17652         code = GET_CODE (compare_op);
17653
17654       if (code != LTU && code != GEU)
17655         return false;
17656
17657       emit_insn (compare_seq);
17658       *pop = compare_op;
17659       return true;
17660     }
17661
17662   if (!INTEGRAL_MODE_P (mode))
17663     return false;
17664
17665   switch (code)
17666     {
17667     case LTU:
17668     case GEU:
17669       break;
17670
17671     /* Convert a==0 into (unsigned)a<1.  */
17672     case EQ:
17673     case NE:
17674       if (op1 != const0_rtx)
17675         return false;
17676       op1 = const1_rtx;
17677       code = (code == EQ ? LTU : GEU);
17678       break;
17679
17680     /* Convert a>b into b<a or a>=b-1.  */
17681     case GTU:
17682     case LEU:
17683       if (CONST_INT_P (op1))
17684         {
17685           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
17686           /* Bail out on overflow.  We still can swap operands but that
17687              would force loading of the constant into register.  */
17688           if (op1 == const0_rtx
17689               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
17690             return false;
17691           code = (code == GTU ? GEU : LTU);
17692         }
17693       else
17694         {
17695           rtx tmp = op1;
17696           op1 = op0;
17697           op0 = tmp;
17698           code = (code == GTU ? LTU : GEU);
17699         }
17700       break;
17701
17702     /* Convert a>=0 into (unsigned)a<0x80000000.  */
17703     case LT:
17704     case GE:
17705       if (mode == DImode || op1 != const0_rtx)
17706         return false;
17707       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
17708       code = (code == LT ? GEU : LTU);
17709       break;
17710     case LE:
17711     case GT:
17712       if (mode == DImode || op1 != constm1_rtx)
17713         return false;
17714       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
17715       code = (code == LE ? GEU : LTU);
17716       break;
17717
17718     default:
17719       return false;
17720     }
17721   /* Swapping operands may cause constant to appear as first operand.  */
17722   if (!nonimmediate_operand (op0, VOIDmode))
17723     {
17724       if (!can_create_pseudo_p ())
17725         return false;
17726       op0 = force_reg (mode, op0);
17727     }
17728   *pop = ix86_expand_compare (code, op0, op1);
17729   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
17730   return true;
17731 }
17732
17733 bool
17734 ix86_expand_int_movcc (rtx operands[])
17735 {
17736   enum rtx_code code = GET_CODE (operands[1]), compare_code;
17737   rtx compare_seq, compare_op;
17738   enum machine_mode mode = GET_MODE (operands[0]);
17739   bool sign_bit_compare_p = false;
17740   rtx op0 = XEXP (operands[1], 0);
17741   rtx op1 = XEXP (operands[1], 1);
17742
17743   start_sequence ();
17744   compare_op = ix86_expand_compare (code, op0, op1);
17745   compare_seq = get_insns ();
17746   end_sequence ();
17747
17748   compare_code = GET_CODE (compare_op);
17749
17750   if ((op1 == const0_rtx && (code == GE || code == LT))
17751       || (op1 == constm1_rtx && (code == GT || code == LE)))
17752     sign_bit_compare_p = true;
17753
17754   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
17755      HImode insns, we'd be swallowed in word prefix ops.  */
17756
17757   if ((mode != HImode || TARGET_FAST_PREFIX)
17758       && (mode != (TARGET_64BIT ? TImode : DImode))
17759       && CONST_INT_P (operands[2])
17760       && CONST_INT_P (operands[3]))
17761     {
17762       rtx out = operands[0];
17763       HOST_WIDE_INT ct = INTVAL (operands[2]);
17764       HOST_WIDE_INT cf = INTVAL (operands[3]);
17765       HOST_WIDE_INT diff;
17766
17767       diff = ct - cf;
17768       /*  Sign bit compares are better done using shifts than we do by using
17769           sbb.  */
17770       if (sign_bit_compare_p
17771           || ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
17772         {
17773           /* Detect overlap between destination and compare sources.  */
17774           rtx tmp = out;
17775
17776           if (!sign_bit_compare_p)
17777             {
17778               rtx flags;
17779               bool fpcmp = false;
17780
17781               compare_code = GET_CODE (compare_op);
17782
17783               flags = XEXP (compare_op, 0);
17784
17785               if (GET_MODE (flags) == CCFPmode
17786                   || GET_MODE (flags) == CCFPUmode)
17787                 {
17788                   fpcmp = true;
17789                   compare_code
17790                     = ix86_fp_compare_code_to_integer (compare_code);
17791                 }
17792
17793               /* To simplify rest of code, restrict to the GEU case.  */
17794               if (compare_code == LTU)
17795                 {
17796                   HOST_WIDE_INT tmp = ct;
17797                   ct = cf;
17798                   cf = tmp;
17799                   compare_code = reverse_condition (compare_code);
17800                   code = reverse_condition (code);
17801                 }
17802               else
17803                 {
17804                   if (fpcmp)
17805                     PUT_CODE (compare_op,
17806                               reverse_condition_maybe_unordered
17807                                 (GET_CODE (compare_op)));
17808                   else
17809                     PUT_CODE (compare_op,
17810                               reverse_condition (GET_CODE (compare_op)));
17811                 }
17812               diff = ct - cf;
17813
17814               if (reg_overlap_mentioned_p (out, op0)
17815                   || reg_overlap_mentioned_p (out, op1))
17816                 tmp = gen_reg_rtx (mode);
17817
17818               if (mode == DImode)
17819                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
17820               else
17821                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
17822                                                  flags, compare_op));
17823             }
17824           else
17825             {
17826               if (code == GT || code == GE)
17827                 code = reverse_condition (code);
17828               else
17829                 {
17830                   HOST_WIDE_INT tmp = ct;
17831                   ct = cf;
17832                   cf = tmp;
17833                   diff = ct - cf;
17834                 }
17835               tmp = emit_store_flag (tmp, code, op0, op1, VOIDmode, 0, -1);
17836             }
17837
17838           if (diff == 1)
17839             {
17840               /*
17841                * cmpl op0,op1
17842                * sbbl dest,dest
17843                * [addl dest, ct]
17844                *
17845                * Size 5 - 8.
17846                */
17847               if (ct)
17848                 tmp = expand_simple_binop (mode, PLUS,
17849                                            tmp, GEN_INT (ct),
17850                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
17851             }
17852           else if (cf == -1)
17853             {
17854               /*
17855                * cmpl op0,op1
17856                * sbbl dest,dest
17857                * orl $ct, dest
17858                *
17859                * Size 8.
17860                */
17861               tmp = expand_simple_binop (mode, IOR,
17862                                          tmp, GEN_INT (ct),
17863                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
17864             }
17865           else if (diff == -1 && ct)
17866             {
17867               /*
17868                * cmpl op0,op1
17869                * sbbl dest,dest
17870                * notl dest
17871                * [addl dest, cf]
17872                *
17873                * Size 8 - 11.
17874                */
17875               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
17876               if (cf)
17877                 tmp = expand_simple_binop (mode, PLUS,
17878                                            copy_rtx (tmp), GEN_INT (cf),
17879                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
17880             }
17881           else
17882             {
17883               /*
17884                * cmpl op0,op1
17885                * sbbl dest,dest
17886                * [notl dest]
17887                * andl cf - ct, dest
17888                * [addl dest, ct]
17889                *
17890                * Size 8 - 11.
17891                */
17892
17893               if (cf == 0)
17894                 {
17895                   cf = ct;
17896                   ct = 0;
17897                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
17898                 }
17899
17900               tmp = expand_simple_binop (mode, AND,
17901                                          copy_rtx (tmp),
17902                                          gen_int_mode (cf - ct, mode),
17903                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
17904               if (ct)
17905                 tmp = expand_simple_binop (mode, PLUS,
17906                                            copy_rtx (tmp), GEN_INT (ct),
17907                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
17908             }
17909
17910           if (!rtx_equal_p (tmp, out))
17911             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
17912
17913           return true;
17914         }
17915
17916       if (diff < 0)
17917         {
17918           enum machine_mode cmp_mode = GET_MODE (op0);
17919
17920           HOST_WIDE_INT tmp;
17921           tmp = ct, ct = cf, cf = tmp;
17922           diff = -diff;
17923
17924           if (SCALAR_FLOAT_MODE_P (cmp_mode))
17925             {
17926               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
17927
17928               /* We may be reversing unordered compare to normal compare, that
17929                  is not valid in general (we may convert non-trapping condition
17930                  to trapping one), however on i386 we currently emit all
17931                  comparisons unordered.  */
17932               compare_code = reverse_condition_maybe_unordered (compare_code);
17933               code = reverse_condition_maybe_unordered (code);
17934             }
17935           else
17936             {
17937               compare_code = reverse_condition (compare_code);
17938               code = reverse_condition (code);
17939             }
17940         }
17941
17942       compare_code = UNKNOWN;
17943       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
17944           && CONST_INT_P (op1))
17945         {
17946           if (op1 == const0_rtx
17947               && (code == LT || code == GE))
17948             compare_code = code;
17949           else if (op1 == constm1_rtx)
17950             {
17951               if (code == LE)
17952                 compare_code = LT;
17953               else if (code == GT)
17954                 compare_code = GE;
17955             }
17956         }
17957
17958       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
17959       if (compare_code != UNKNOWN
17960           && GET_MODE (op0) == GET_MODE (out)
17961           && (cf == -1 || ct == -1))
17962         {
17963           /* If lea code below could be used, only optimize
17964              if it results in a 2 insn sequence.  */
17965
17966           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
17967                  || diff == 3 || diff == 5 || diff == 9)
17968               || (compare_code == LT && ct == -1)
17969               || (compare_code == GE && cf == -1))
17970             {
17971               /*
17972                * notl op1       (if necessary)
17973                * sarl $31, op1
17974                * orl cf, op1
17975                */
17976               if (ct != -1)
17977                 {
17978                   cf = ct;
17979                   ct = -1;
17980                   code = reverse_condition (code);
17981                 }
17982
17983               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
17984
17985               out = expand_simple_binop (mode, IOR,
17986                                          out, GEN_INT (cf),
17987                                          out, 1, OPTAB_DIRECT);
17988               if (out != operands[0])
17989                 emit_move_insn (operands[0], out);
17990
17991               return true;
17992             }
17993         }
17994
17995
17996       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
17997            || diff == 3 || diff == 5 || diff == 9)
17998           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
17999           && (mode != DImode
18000               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
18001         {
18002           /*
18003            * xorl dest,dest
18004            * cmpl op1,op2
18005            * setcc dest
18006            * lea cf(dest*(ct-cf)),dest
18007            *
18008            * Size 14.
18009            *
18010            * This also catches the degenerate setcc-only case.
18011            */
18012
18013           rtx tmp;
18014           int nops;
18015
18016           out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
18017
18018           nops = 0;
18019           /* On x86_64 the lea instruction operates on Pmode, so we need
18020              to get arithmetics done in proper mode to match.  */
18021           if (diff == 1)
18022             tmp = copy_rtx (out);
18023           else
18024             {
18025               rtx out1;
18026               out1 = copy_rtx (out);
18027               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
18028               nops++;
18029               if (diff & 1)
18030                 {
18031                   tmp = gen_rtx_PLUS (mode, tmp, out1);
18032                   nops++;
18033                 }
18034             }
18035           if (cf != 0)
18036             {
18037               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
18038               nops++;
18039             }
18040           if (!rtx_equal_p (tmp, out))
18041             {
18042               if (nops == 1)
18043                 out = force_operand (tmp, copy_rtx (out));
18044               else
18045                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
18046             }
18047           if (!rtx_equal_p (out, operands[0]))
18048             emit_move_insn (operands[0], copy_rtx (out));
18049
18050           return true;
18051         }
18052
18053       /*
18054        * General case:                  Jumpful:
18055        *   xorl dest,dest               cmpl op1, op2
18056        *   cmpl op1, op2                movl ct, dest
18057        *   setcc dest                   jcc 1f
18058        *   decl dest                    movl cf, dest
18059        *   andl (cf-ct),dest            1:
18060        *   addl ct,dest
18061        *
18062        * Size 20.                       Size 14.
18063        *
18064        * This is reasonably steep, but branch mispredict costs are
18065        * high on modern cpus, so consider failing only if optimizing
18066        * for space.
18067        */
18068
18069       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
18070           && BRANCH_COST (optimize_insn_for_speed_p (),
18071                           false) >= 2)
18072         {
18073           if (cf == 0)
18074             {
18075               enum machine_mode cmp_mode = GET_MODE (op0);
18076
18077               cf = ct;
18078               ct = 0;
18079
18080               if (SCALAR_FLOAT_MODE_P (cmp_mode))
18081                 {
18082                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
18083
18084                   /* We may be reversing unordered compare to normal compare,
18085                      that is not valid in general (we may convert non-trapping
18086                      condition to trapping one), however on i386 we currently
18087                      emit all comparisons unordered.  */
18088                   code = reverse_condition_maybe_unordered (code);
18089                 }
18090               else
18091                 {
18092                   code = reverse_condition (code);
18093                   if (compare_code != UNKNOWN)
18094                     compare_code = reverse_condition (compare_code);
18095                 }
18096             }
18097
18098           if (compare_code != UNKNOWN)
18099             {
18100               /* notl op1       (if needed)
18101                  sarl $31, op1
18102                  andl (cf-ct), op1
18103                  addl ct, op1
18104
18105                  For x < 0 (resp. x <= -1) there will be no notl,
18106                  so if possible swap the constants to get rid of the
18107                  complement.
18108                  True/false will be -1/0 while code below (store flag
18109                  followed by decrement) is 0/-1, so the constants need
18110                  to be exchanged once more.  */
18111
18112               if (compare_code == GE || !cf)
18113                 {
18114                   code = reverse_condition (code);
18115                   compare_code = LT;
18116                 }
18117               else
18118                 {
18119                   HOST_WIDE_INT tmp = cf;
18120                   cf = ct;
18121                   ct = tmp;
18122                 }
18123
18124               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
18125             }
18126           else
18127             {
18128               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
18129
18130               out = expand_simple_binop (mode, PLUS, copy_rtx (out),
18131                                          constm1_rtx,
18132                                          copy_rtx (out), 1, OPTAB_DIRECT);
18133             }
18134
18135           out = expand_simple_binop (mode, AND, copy_rtx (out),
18136                                      gen_int_mode (cf - ct, mode),
18137                                      copy_rtx (out), 1, OPTAB_DIRECT);
18138           if (ct)
18139             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
18140                                        copy_rtx (out), 1, OPTAB_DIRECT);
18141           if (!rtx_equal_p (out, operands[0]))
18142             emit_move_insn (operands[0], copy_rtx (out));
18143
18144           return true;
18145         }
18146     }
18147
18148   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
18149     {
18150       /* Try a few things more with specific constants and a variable.  */
18151
18152       optab op;
18153       rtx var, orig_out, out, tmp;
18154
18155       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
18156         return false;
18157
18158       /* If one of the two operands is an interesting constant, load a
18159          constant with the above and mask it in with a logical operation.  */
18160
18161       if (CONST_INT_P (operands[2]))
18162         {
18163           var = operands[3];
18164           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
18165             operands[3] = constm1_rtx, op = and_optab;
18166           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
18167             operands[3] = const0_rtx, op = ior_optab;
18168           else
18169             return false;
18170         }
18171       else if (CONST_INT_P (operands[3]))
18172         {
18173           var = operands[2];
18174           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
18175             operands[2] = constm1_rtx, op = and_optab;
18176           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
18177             operands[2] = const0_rtx, op = ior_optab;
18178           else
18179             return false;
18180         }
18181       else
18182         return false;
18183
18184       orig_out = operands[0];
18185       tmp = gen_reg_rtx (mode);
18186       operands[0] = tmp;
18187
18188       /* Recurse to get the constant loaded.  */
18189       if (ix86_expand_int_movcc (operands) == 0)
18190         return false;
18191
18192       /* Mask in the interesting variable.  */
18193       out = expand_binop (mode, op, var, tmp, orig_out, 0,
18194                           OPTAB_WIDEN);
18195       if (!rtx_equal_p (out, orig_out))
18196         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
18197
18198       return true;
18199     }
18200
18201   /*
18202    * For comparison with above,
18203    *
18204    * movl cf,dest
18205    * movl ct,tmp
18206    * cmpl op1,op2
18207    * cmovcc tmp,dest
18208    *
18209    * Size 15.
18210    */
18211
18212   if (! nonimmediate_operand (operands[2], mode))
18213     operands[2] = force_reg (mode, operands[2]);
18214   if (! nonimmediate_operand (operands[3], mode))
18215     operands[3] = force_reg (mode, operands[3]);
18216
18217   if (! register_operand (operands[2], VOIDmode)
18218       && (mode == QImode
18219           || ! register_operand (operands[3], VOIDmode)))
18220     operands[2] = force_reg (mode, operands[2]);
18221
18222   if (mode == QImode
18223       && ! register_operand (operands[3], VOIDmode))
18224     operands[3] = force_reg (mode, operands[3]);
18225
18226   emit_insn (compare_seq);
18227   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
18228                           gen_rtx_IF_THEN_ELSE (mode,
18229                                                 compare_op, operands[2],
18230                                                 operands[3])));
18231   return true;
18232 }
18233
18234 /* Swap, force into registers, or otherwise massage the two operands
18235    to an sse comparison with a mask result.  Thus we differ a bit from
18236    ix86_prepare_fp_compare_args which expects to produce a flags result.
18237
18238    The DEST operand exists to help determine whether to commute commutative
18239    operators.  The POP0/POP1 operands are updated in place.  The new
18240    comparison code is returned, or UNKNOWN if not implementable.  */
18241
18242 static enum rtx_code
18243 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
18244                                   rtx *pop0, rtx *pop1)
18245 {
18246   rtx tmp;
18247
18248   switch (code)
18249     {
18250     case LTGT:
18251     case UNEQ:
18252       /* We have no LTGT as an operator.  We could implement it with
18253          NE & ORDERED, but this requires an extra temporary.  It's
18254          not clear that it's worth it.  */
18255       return UNKNOWN;
18256
18257     case LT:
18258     case LE:
18259     case UNGT:
18260     case UNGE:
18261       /* These are supported directly.  */
18262       break;
18263
18264     case EQ:
18265     case NE:
18266     case UNORDERED:
18267     case ORDERED:
18268       /* For commutative operators, try to canonicalize the destination
18269          operand to be first in the comparison - this helps reload to
18270          avoid extra moves.  */
18271       if (!dest || !rtx_equal_p (dest, *pop1))
18272         break;
18273       /* FALLTHRU */
18274
18275     case GE:
18276     case GT:
18277     case UNLE:
18278     case UNLT:
18279       /* These are not supported directly.  Swap the comparison operands
18280          to transform into something that is supported.  */
18281       tmp = *pop0;
18282       *pop0 = *pop1;
18283       *pop1 = tmp;
18284       code = swap_condition (code);
18285       break;
18286
18287     default:
18288       gcc_unreachable ();
18289     }
18290
18291   return code;
18292 }
18293
18294 /* Detect conditional moves that exactly match min/max operational
18295    semantics.  Note that this is IEEE safe, as long as we don't
18296    interchange the operands.
18297
18298    Returns FALSE if this conditional move doesn't match a MIN/MAX,
18299    and TRUE if the operation is successful and instructions are emitted.  */
18300
18301 static bool
18302 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
18303                            rtx cmp_op1, rtx if_true, rtx if_false)
18304 {
18305   enum machine_mode mode;
18306   bool is_min;
18307   rtx tmp;
18308
18309   if (code == LT)
18310     ;
18311   else if (code == UNGE)
18312     {
18313       tmp = if_true;
18314       if_true = if_false;
18315       if_false = tmp;
18316     }
18317   else
18318     return false;
18319
18320   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
18321     is_min = true;
18322   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
18323     is_min = false;
18324   else
18325     return false;
18326
18327   mode = GET_MODE (dest);
18328
18329   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
18330      but MODE may be a vector mode and thus not appropriate.  */
18331   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
18332     {
18333       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
18334       rtvec v;
18335
18336       if_true = force_reg (mode, if_true);
18337       v = gen_rtvec (2, if_true, if_false);
18338       tmp = gen_rtx_UNSPEC (mode, v, u);
18339     }
18340   else
18341     {
18342       code = is_min ? SMIN : SMAX;
18343       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
18344     }
18345
18346   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
18347   return true;
18348 }
18349
18350 /* Expand an sse vector comparison.  Return the register with the result.  */
18351
18352 static rtx
18353 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
18354                      rtx op_true, rtx op_false)
18355 {
18356   enum machine_mode mode = GET_MODE (dest);
18357   rtx x;
18358
18359   cmp_op0 = force_reg (mode, cmp_op0);
18360   if (!nonimmediate_operand (cmp_op1, mode))
18361     cmp_op1 = force_reg (mode, cmp_op1);
18362
18363   if (optimize
18364       || reg_overlap_mentioned_p (dest, op_true)
18365       || reg_overlap_mentioned_p (dest, op_false))
18366     dest = gen_reg_rtx (mode);
18367
18368   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
18369   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18370
18371   return dest;
18372 }
18373
18374 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
18375    operations.  This is used for both scalar and vector conditional moves.  */
18376
18377 static void
18378 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
18379 {
18380   enum machine_mode mode = GET_MODE (dest);
18381   rtx t2, t3, x;
18382
18383   if (op_false == CONST0_RTX (mode))
18384     {
18385       op_true = force_reg (mode, op_true);
18386       x = gen_rtx_AND (mode, cmp, op_true);
18387       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18388     }
18389   else if (op_true == CONST0_RTX (mode))
18390     {
18391       op_false = force_reg (mode, op_false);
18392       x = gen_rtx_NOT (mode, cmp);
18393       x = gen_rtx_AND (mode, x, op_false);
18394       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18395     }
18396   else if (TARGET_XOP)
18397     {
18398       rtx pcmov = gen_rtx_SET (mode, dest,
18399                                gen_rtx_IF_THEN_ELSE (mode, cmp,
18400                                                      op_true,
18401                                                      op_false));
18402       emit_insn (pcmov);
18403     }
18404   else
18405     {
18406       op_true = force_reg (mode, op_true);
18407       op_false = force_reg (mode, op_false);
18408
18409       t2 = gen_reg_rtx (mode);
18410       if (optimize)
18411         t3 = gen_reg_rtx (mode);
18412       else
18413         t3 = dest;
18414
18415       x = gen_rtx_AND (mode, op_true, cmp);
18416       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
18417
18418       x = gen_rtx_NOT (mode, cmp);
18419       x = gen_rtx_AND (mode, x, op_false);
18420       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
18421
18422       x = gen_rtx_IOR (mode, t3, t2);
18423       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
18424     }
18425 }
18426
18427 /* Expand a floating-point conditional move.  Return true if successful.  */
18428
18429 bool
18430 ix86_expand_fp_movcc (rtx operands[])
18431 {
18432   enum machine_mode mode = GET_MODE (operands[0]);
18433   enum rtx_code code = GET_CODE (operands[1]);
18434   rtx tmp, compare_op;
18435   rtx op0 = XEXP (operands[1], 0);
18436   rtx op1 = XEXP (operands[1], 1);
18437
18438   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
18439     {
18440       enum machine_mode cmode;
18441
18442       /* Since we've no cmove for sse registers, don't force bad register
18443          allocation just to gain access to it.  Deny movcc when the
18444          comparison mode doesn't match the move mode.  */
18445       cmode = GET_MODE (op0);
18446       if (cmode == VOIDmode)
18447         cmode = GET_MODE (op1);
18448       if (cmode != mode)
18449         return false;
18450
18451       code = ix86_prepare_sse_fp_compare_args (operands[0], code, &op0, &op1);
18452       if (code == UNKNOWN)
18453         return false;
18454
18455       if (ix86_expand_sse_fp_minmax (operands[0], code, op0, op1,
18456                                      operands[2], operands[3]))
18457         return true;
18458
18459       tmp = ix86_expand_sse_cmp (operands[0], code, op0, op1,
18460                                  operands[2], operands[3]);
18461       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
18462       return true;
18463     }
18464
18465   /* The floating point conditional move instructions don't directly
18466      support conditions resulting from a signed integer comparison.  */
18467
18468   compare_op = ix86_expand_compare (code, op0, op1);
18469   if (!fcmov_comparison_operator (compare_op, VOIDmode))
18470     {
18471       tmp = gen_reg_rtx (QImode);
18472       ix86_expand_setcc (tmp, code, op0, op1);
18473
18474       compare_op = ix86_expand_compare (NE, tmp, const0_rtx);
18475     }
18476
18477   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
18478                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
18479                                                 operands[2], operands[3])));
18480
18481   return true;
18482 }
18483
18484 /* Expand a floating-point vector conditional move; a vcond operation
18485    rather than a movcc operation.  */
18486
18487 bool
18488 ix86_expand_fp_vcond (rtx operands[])
18489 {
18490   enum rtx_code code = GET_CODE (operands[3]);
18491   rtx cmp;
18492
18493   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
18494                                            &operands[4], &operands[5]);
18495   if (code == UNKNOWN)
18496     return false;
18497
18498   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
18499                                  operands[5], operands[1], operands[2]))
18500     return true;
18501
18502   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
18503                              operands[1], operands[2]);
18504   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
18505   return true;
18506 }
18507
18508 /* Expand a signed/unsigned integral vector conditional move.  */
18509
18510 bool
18511 ix86_expand_int_vcond (rtx operands[])
18512 {
18513   enum machine_mode mode = GET_MODE (operands[0]);
18514   enum rtx_code code = GET_CODE (operands[3]);
18515   bool negate = false;
18516   rtx x, cop0, cop1;
18517
18518   cop0 = operands[4];
18519   cop1 = operands[5];
18520
18521   /* XOP supports all of the comparisons on all vector int types.  */
18522   if (!TARGET_XOP)
18523     {
18524       /* Canonicalize the comparison to EQ, GT, GTU.  */
18525       switch (code)
18526         {
18527         case EQ:
18528         case GT:
18529         case GTU:
18530           break;
18531
18532         case NE:
18533         case LE:
18534         case LEU:
18535           code = reverse_condition (code);
18536           negate = true;
18537           break;
18538
18539         case GE:
18540         case GEU:
18541           code = reverse_condition (code);
18542           negate = true;
18543           /* FALLTHRU */
18544
18545         case LT:
18546         case LTU:
18547           code = swap_condition (code);
18548           x = cop0, cop0 = cop1, cop1 = x;
18549           break;
18550
18551         default:
18552           gcc_unreachable ();
18553         }
18554
18555       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
18556       if (mode == V2DImode)
18557         {
18558           switch (code)
18559             {
18560             case EQ:
18561               /* SSE4.1 supports EQ.  */
18562               if (!TARGET_SSE4_1)
18563                 return false;
18564               break;
18565
18566             case GT:
18567             case GTU:
18568               /* SSE4.2 supports GT/GTU.  */
18569               if (!TARGET_SSE4_2)
18570                 return false;
18571               break;
18572
18573             default:
18574               gcc_unreachable ();
18575             }
18576         }
18577
18578       /* Unsigned parallel compare is not supported by the hardware.
18579          Play some tricks to turn this into a signed comparison
18580          against 0.  */
18581       if (code == GTU)
18582         {
18583           cop0 = force_reg (mode, cop0);
18584
18585           switch (mode)
18586             {
18587             case V4SImode:
18588             case V2DImode:
18589                 {
18590                   rtx t1, t2, mask;
18591                   rtx (*gen_sub3) (rtx, rtx, rtx);
18592
18593                   /* Subtract (-(INT MAX) - 1) from both operands to make
18594                      them signed.  */
18595                   mask = ix86_build_signbit_mask (mode, true, false);
18596                   gen_sub3 = (mode == V4SImode
18597                               ? gen_subv4si3 : gen_subv2di3);
18598                   t1 = gen_reg_rtx (mode);
18599                   emit_insn (gen_sub3 (t1, cop0, mask));
18600
18601                   t2 = gen_reg_rtx (mode);
18602                   emit_insn (gen_sub3 (t2, cop1, mask));
18603
18604                   cop0 = t1;
18605                   cop1 = t2;
18606                   code = GT;
18607                 }
18608               break;
18609
18610             case V16QImode:
18611             case V8HImode:
18612               /* Perform a parallel unsigned saturating subtraction.  */
18613               x = gen_reg_rtx (mode);
18614               emit_insn (gen_rtx_SET (VOIDmode, x,
18615                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
18616
18617               cop0 = x;
18618               cop1 = CONST0_RTX (mode);
18619               code = EQ;
18620               negate = !negate;
18621               break;
18622
18623             default:
18624               gcc_unreachable ();
18625             }
18626         }
18627     }
18628
18629   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
18630                            operands[1+negate], operands[2-negate]);
18631
18632   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
18633                          operands[2-negate]);
18634   return true;
18635 }
18636
18637 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
18638    true if we should do zero extension, else sign extension.  HIGH_P is
18639    true if we want the N/2 high elements, else the low elements.  */
18640
18641 void
18642 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18643 {
18644   enum machine_mode imode = GET_MODE (operands[1]);
18645   rtx (*unpack)(rtx, rtx, rtx);
18646   rtx se, dest;
18647
18648   switch (imode)
18649     {
18650     case V16QImode:
18651       if (high_p)
18652         unpack = gen_vec_interleave_highv16qi;
18653       else
18654         unpack = gen_vec_interleave_lowv16qi;
18655       break;
18656     case V8HImode:
18657       if (high_p)
18658         unpack = gen_vec_interleave_highv8hi;
18659       else
18660         unpack = gen_vec_interleave_lowv8hi;
18661       break;
18662     case V4SImode:
18663       if (high_p)
18664         unpack = gen_vec_interleave_highv4si;
18665       else
18666         unpack = gen_vec_interleave_lowv4si;
18667       break;
18668     default:
18669       gcc_unreachable ();
18670     }
18671
18672   dest = gen_lowpart (imode, operands[0]);
18673
18674   if (unsigned_p)
18675     se = force_reg (imode, CONST0_RTX (imode));
18676   else
18677     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
18678                               operands[1], pc_rtx, pc_rtx);
18679
18680   emit_insn (unpack (dest, operands[1], se));
18681 }
18682
18683 /* This function performs the same task as ix86_expand_sse_unpack,
18684    but with SSE4.1 instructions.  */
18685
18686 void
18687 ix86_expand_sse4_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18688 {
18689   enum machine_mode imode = GET_MODE (operands[1]);
18690   rtx (*unpack)(rtx, rtx);
18691   rtx src, dest;
18692
18693   switch (imode)
18694     {
18695     case V16QImode:
18696       if (unsigned_p)
18697         unpack = gen_sse4_1_zero_extendv8qiv8hi2;
18698       else
18699         unpack = gen_sse4_1_sign_extendv8qiv8hi2;
18700       break;
18701     case V8HImode:
18702       if (unsigned_p)
18703         unpack = gen_sse4_1_zero_extendv4hiv4si2;
18704       else
18705         unpack = gen_sse4_1_sign_extendv4hiv4si2;
18706       break;
18707     case V4SImode:
18708       if (unsigned_p)
18709         unpack = gen_sse4_1_zero_extendv2siv2di2;
18710       else
18711         unpack = gen_sse4_1_sign_extendv2siv2di2;
18712       break;
18713     default:
18714       gcc_unreachable ();
18715     }
18716
18717   dest = operands[0];
18718   if (high_p)
18719     {
18720       /* Shift higher 8 bytes to lower 8 bytes.  */
18721       src = gen_reg_rtx (imode);
18722       emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, src),
18723                                      gen_lowpart (V1TImode, operands[1]),
18724                                      GEN_INT (64)));
18725     }
18726   else
18727     src = operands[1];
18728
18729   emit_insn (unpack (dest, src));
18730 }
18731
18732 /* Expand conditional increment or decrement using adb/sbb instructions.
18733    The default case using setcc followed by the conditional move can be
18734    done by generic code.  */
18735 bool
18736 ix86_expand_int_addcc (rtx operands[])
18737 {
18738   enum rtx_code code = GET_CODE (operands[1]);
18739   rtx flags;
18740   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
18741   rtx compare_op;
18742   rtx val = const0_rtx;
18743   bool fpcmp = false;
18744   enum machine_mode mode;
18745   rtx op0 = XEXP (operands[1], 0);
18746   rtx op1 = XEXP (operands[1], 1);
18747
18748   if (operands[3] != const1_rtx
18749       && operands[3] != constm1_rtx)
18750     return false;
18751   if (!ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
18752      return false;
18753   code = GET_CODE (compare_op);
18754
18755   flags = XEXP (compare_op, 0);
18756
18757   if (GET_MODE (flags) == CCFPmode
18758       || GET_MODE (flags) == CCFPUmode)
18759     {
18760       fpcmp = true;
18761       code = ix86_fp_compare_code_to_integer (code);
18762     }
18763
18764   if (code != LTU)
18765     {
18766       val = constm1_rtx;
18767       if (fpcmp)
18768         PUT_CODE (compare_op,
18769                   reverse_condition_maybe_unordered
18770                     (GET_CODE (compare_op)));
18771       else
18772         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
18773     }
18774
18775   mode = GET_MODE (operands[0]);
18776
18777   /* Construct either adc or sbb insn.  */
18778   if ((code == LTU) == (operands[3] == constm1_rtx))
18779     {
18780       switch (mode)
18781         {
18782           case QImode:
18783             insn = gen_subqi3_carry;
18784             break;
18785           case HImode:
18786             insn = gen_subhi3_carry;
18787             break;
18788           case SImode:
18789             insn = gen_subsi3_carry;
18790             break;
18791           case DImode:
18792             insn = gen_subdi3_carry;
18793             break;
18794           default:
18795             gcc_unreachable ();
18796         }
18797     }
18798   else
18799     {
18800       switch (mode)
18801         {
18802           case QImode:
18803             insn = gen_addqi3_carry;
18804             break;
18805           case HImode:
18806             insn = gen_addhi3_carry;
18807             break;
18808           case SImode:
18809             insn = gen_addsi3_carry;
18810             break;
18811           case DImode:
18812             insn = gen_adddi3_carry;
18813             break;
18814           default:
18815             gcc_unreachable ();
18816         }
18817     }
18818   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
18819
18820   return true;
18821 }
18822
18823
18824 /* Split operands 0 and 1 into half-mode parts.  Similar to split_double_mode,
18825    but works for floating pointer parameters and nonoffsetable memories.
18826    For pushes, it returns just stack offsets; the values will be saved
18827    in the right order.  Maximally three parts are generated.  */
18828
18829 static int
18830 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
18831 {
18832   int size;
18833
18834   if (!TARGET_64BIT)
18835     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
18836   else
18837     size = (GET_MODE_SIZE (mode) + 4) / 8;
18838
18839   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
18840   gcc_assert (size >= 2 && size <= 4);
18841
18842   /* Optimize constant pool reference to immediates.  This is used by fp
18843      moves, that force all constants to memory to allow combining.  */
18844   if (MEM_P (operand) && MEM_READONLY_P (operand))
18845     {
18846       rtx tmp = maybe_get_pool_constant (operand);
18847       if (tmp)
18848         operand = tmp;
18849     }
18850
18851   if (MEM_P (operand) && !offsettable_memref_p (operand))
18852     {
18853       /* The only non-offsetable memories we handle are pushes.  */
18854       int ok = push_operand (operand, VOIDmode);
18855
18856       gcc_assert (ok);
18857
18858       operand = copy_rtx (operand);
18859       PUT_MODE (operand, Pmode);
18860       parts[0] = parts[1] = parts[2] = parts[3] = operand;
18861       return size;
18862     }
18863
18864   if (GET_CODE (operand) == CONST_VECTOR)
18865     {
18866       enum machine_mode imode = int_mode_for_mode (mode);
18867       /* Caution: if we looked through a constant pool memory above,
18868          the operand may actually have a different mode now.  That's
18869          ok, since we want to pun this all the way back to an integer.  */
18870       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
18871       gcc_assert (operand != NULL);
18872       mode = imode;
18873     }
18874
18875   if (!TARGET_64BIT)
18876     {
18877       if (mode == DImode)
18878         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
18879       else
18880         {
18881           int i;
18882
18883           if (REG_P (operand))
18884             {
18885               gcc_assert (reload_completed);
18886               for (i = 0; i < size; i++)
18887                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
18888             }
18889           else if (offsettable_memref_p (operand))
18890             {
18891               operand = adjust_address (operand, SImode, 0);
18892               parts[0] = operand;
18893               for (i = 1; i < size; i++)
18894                 parts[i] = adjust_address (operand, SImode, 4 * i);
18895             }
18896           else if (GET_CODE (operand) == CONST_DOUBLE)
18897             {
18898               REAL_VALUE_TYPE r;
18899               long l[4];
18900
18901               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
18902               switch (mode)
18903                 {
18904                 case TFmode:
18905                   real_to_target (l, &r, mode);
18906                   parts[3] = gen_int_mode (l[3], SImode);
18907                   parts[2] = gen_int_mode (l[2], SImode);
18908                   break;
18909                 case XFmode:
18910                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
18911                   parts[2] = gen_int_mode (l[2], SImode);
18912                   break;
18913                 case DFmode:
18914                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
18915                   break;
18916                 default:
18917                   gcc_unreachable ();
18918                 }
18919               parts[1] = gen_int_mode (l[1], SImode);
18920               parts[0] = gen_int_mode (l[0], SImode);
18921             }
18922           else
18923             gcc_unreachable ();
18924         }
18925     }
18926   else
18927     {
18928       if (mode == TImode)
18929         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
18930       if (mode == XFmode || mode == TFmode)
18931         {
18932           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
18933           if (REG_P (operand))
18934             {
18935               gcc_assert (reload_completed);
18936               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
18937               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
18938             }
18939           else if (offsettable_memref_p (operand))
18940             {
18941               operand = adjust_address (operand, DImode, 0);
18942               parts[0] = operand;
18943               parts[1] = adjust_address (operand, upper_mode, 8);
18944             }
18945           else if (GET_CODE (operand) == CONST_DOUBLE)
18946             {
18947               REAL_VALUE_TYPE r;
18948               long l[4];
18949
18950               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
18951               real_to_target (l, &r, mode);
18952
18953               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
18954               if (HOST_BITS_PER_WIDE_INT >= 64)
18955                 parts[0]
18956                   = gen_int_mode
18957                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
18958                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
18959                        DImode);
18960               else
18961                 parts[0] = immed_double_const (l[0], l[1], DImode);
18962
18963               if (upper_mode == SImode)
18964                 parts[1] = gen_int_mode (l[2], SImode);
18965               else if (HOST_BITS_PER_WIDE_INT >= 64)
18966                 parts[1]
18967                   = gen_int_mode
18968                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
18969                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
18970                        DImode);
18971               else
18972                 parts[1] = immed_double_const (l[2], l[3], DImode);
18973             }
18974           else
18975             gcc_unreachable ();
18976         }
18977     }
18978
18979   return size;
18980 }
18981
18982 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
18983    Return false when normal moves are needed; true when all required
18984    insns have been emitted.  Operands 2-4 contain the input values
18985    int the correct order; operands 5-7 contain the output values.  */
18986
18987 void
18988 ix86_split_long_move (rtx operands[])
18989 {
18990   rtx part[2][4];
18991   int nparts, i, j;
18992   int push = 0;
18993   int collisions = 0;
18994   enum machine_mode mode = GET_MODE (operands[0]);
18995   bool collisionparts[4];
18996
18997   /* The DFmode expanders may ask us to move double.
18998      For 64bit target this is single move.  By hiding the fact
18999      here we simplify i386.md splitters.  */
19000   if (TARGET_64BIT && GET_MODE_SIZE (GET_MODE (operands[0])) == 8)
19001     {
19002       /* Optimize constant pool reference to immediates.  This is used by
19003          fp moves, that force all constants to memory to allow combining.  */
19004
19005       if (MEM_P (operands[1])
19006           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
19007           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
19008         operands[1] = get_pool_constant (XEXP (operands[1], 0));
19009       if (push_operand (operands[0], VOIDmode))
19010         {
19011           operands[0] = copy_rtx (operands[0]);
19012           PUT_MODE (operands[0], Pmode);
19013         }
19014       else
19015         operands[0] = gen_lowpart (DImode, operands[0]);
19016       operands[1] = gen_lowpart (DImode, operands[1]);
19017       emit_move_insn (operands[0], operands[1]);
19018       return;
19019     }
19020
19021   /* The only non-offsettable memory we handle is push.  */
19022   if (push_operand (operands[0], VOIDmode))
19023     push = 1;
19024   else
19025     gcc_assert (!MEM_P (operands[0])
19026                 || offsettable_memref_p (operands[0]));
19027
19028   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
19029   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
19030
19031   /* When emitting push, take care for source operands on the stack.  */
19032   if (push && MEM_P (operands[1])
19033       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
19034     {
19035       rtx src_base = XEXP (part[1][nparts - 1], 0);
19036
19037       /* Compensate for the stack decrement by 4.  */
19038       if (!TARGET_64BIT && nparts == 3
19039           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
19040         src_base = plus_constant (src_base, 4);
19041
19042       /* src_base refers to the stack pointer and is
19043          automatically decreased by emitted push.  */
19044       for (i = 0; i < nparts; i++)
19045         part[1][i] = change_address (part[1][i],
19046                                      GET_MODE (part[1][i]), src_base);
19047     }
19048
19049   /* We need to do copy in the right order in case an address register
19050      of the source overlaps the destination.  */
19051   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
19052     {
19053       rtx tmp;
19054
19055       for (i = 0; i < nparts; i++)
19056         {
19057           collisionparts[i]
19058             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
19059           if (collisionparts[i])
19060             collisions++;
19061         }
19062
19063       /* Collision in the middle part can be handled by reordering.  */
19064       if (collisions == 1 && nparts == 3 && collisionparts [1])
19065         {
19066           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
19067           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
19068         }
19069       else if (collisions == 1
19070                && nparts == 4
19071                && (collisionparts [1] || collisionparts [2]))
19072         {
19073           if (collisionparts [1])
19074             {
19075               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
19076               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
19077             }
19078           else
19079             {
19080               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
19081               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
19082             }
19083         }
19084
19085       /* If there are more collisions, we can't handle it by reordering.
19086          Do an lea to the last part and use only one colliding move.  */
19087       else if (collisions > 1)
19088         {
19089           rtx base;
19090
19091           collisions = 1;
19092
19093           base = part[0][nparts - 1];
19094
19095           /* Handle the case when the last part isn't valid for lea.
19096              Happens in 64-bit mode storing the 12-byte XFmode.  */
19097           if (GET_MODE (base) != Pmode)
19098             base = gen_rtx_REG (Pmode, REGNO (base));
19099
19100           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
19101           part[1][0] = replace_equiv_address (part[1][0], base);
19102           for (i = 1; i < nparts; i++)
19103             {
19104               tmp = plus_constant (base, UNITS_PER_WORD * i);
19105               part[1][i] = replace_equiv_address (part[1][i], tmp);
19106             }
19107         }
19108     }
19109
19110   if (push)
19111     {
19112       if (!TARGET_64BIT)
19113         {
19114           if (nparts == 3)
19115             {
19116               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
19117                 emit_insn (gen_addsi3 (stack_pointer_rtx,
19118                                        stack_pointer_rtx, GEN_INT (-4)));
19119               emit_move_insn (part[0][2], part[1][2]);
19120             }
19121           else if (nparts == 4)
19122             {
19123               emit_move_insn (part[0][3], part[1][3]);
19124               emit_move_insn (part[0][2], part[1][2]);
19125             }
19126         }
19127       else
19128         {
19129           /* In 64bit mode we don't have 32bit push available.  In case this is
19130              register, it is OK - we will just use larger counterpart.  We also
19131              retype memory - these comes from attempt to avoid REX prefix on
19132              moving of second half of TFmode value.  */
19133           if (GET_MODE (part[1][1]) == SImode)
19134             {
19135               switch (GET_CODE (part[1][1]))
19136                 {
19137                 case MEM:
19138                   part[1][1] = adjust_address (part[1][1], DImode, 0);
19139                   break;
19140
19141                 case REG:
19142                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
19143                   break;
19144
19145                 default:
19146                   gcc_unreachable ();
19147                 }
19148
19149               if (GET_MODE (part[1][0]) == SImode)
19150                 part[1][0] = part[1][1];
19151             }
19152         }
19153       emit_move_insn (part[0][1], part[1][1]);
19154       emit_move_insn (part[0][0], part[1][0]);
19155       return;
19156     }
19157
19158   /* Choose correct order to not overwrite the source before it is copied.  */
19159   if ((REG_P (part[0][0])
19160        && REG_P (part[1][1])
19161        && (REGNO (part[0][0]) == REGNO (part[1][1])
19162            || (nparts == 3
19163                && REGNO (part[0][0]) == REGNO (part[1][2]))
19164            || (nparts == 4
19165                && REGNO (part[0][0]) == REGNO (part[1][3]))))
19166       || (collisions > 0
19167           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
19168     {
19169       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
19170         {
19171           operands[2 + i] = part[0][j];
19172           operands[6 + i] = part[1][j];
19173         }
19174     }
19175   else
19176     {
19177       for (i = 0; i < nparts; i++)
19178         {
19179           operands[2 + i] = part[0][i];
19180           operands[6 + i] = part[1][i];
19181         }
19182     }
19183
19184   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
19185   if (optimize_insn_for_size_p ())
19186     {
19187       for (j = 0; j < nparts - 1; j++)
19188         if (CONST_INT_P (operands[6 + j])
19189             && operands[6 + j] != const0_rtx
19190             && REG_P (operands[2 + j]))
19191           for (i = j; i < nparts - 1; i++)
19192             if (CONST_INT_P (operands[7 + i])
19193                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
19194               operands[7 + i] = operands[2 + j];
19195     }
19196
19197   for (i = 0; i < nparts; i++)
19198     emit_move_insn (operands[2 + i], operands[6 + i]);
19199
19200   return;
19201 }
19202
19203 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
19204    left shift by a constant, either using a single shift or
19205    a sequence of add instructions.  */
19206
19207 static void
19208 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
19209 {
19210   rtx (*insn)(rtx, rtx, rtx);
19211
19212   if (count == 1
19213       || (count * ix86_cost->add <= ix86_cost->shift_const
19214           && !optimize_insn_for_size_p ()))
19215     {
19216       insn = mode == DImode ? gen_addsi3 : gen_adddi3;
19217       while (count-- > 0)
19218         emit_insn (insn (operand, operand, operand));
19219     }
19220   else
19221     {
19222       insn = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
19223       emit_insn (insn (operand, operand, GEN_INT (count)));
19224     }
19225 }
19226
19227 void
19228 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
19229 {
19230   rtx (*gen_ashl3)(rtx, rtx, rtx);
19231   rtx (*gen_shld)(rtx, rtx, rtx);
19232   int half_width = GET_MODE_BITSIZE (mode) >> 1;
19233
19234   rtx low[2], high[2];
19235   int count;
19236
19237   if (CONST_INT_P (operands[2]))
19238     {
19239       split_double_mode (mode, operands, 2, low, high);
19240       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
19241
19242       if (count >= half_width)
19243         {
19244           emit_move_insn (high[0], low[1]);
19245           emit_move_insn (low[0], const0_rtx);
19246
19247           if (count > half_width)
19248             ix86_expand_ashl_const (high[0], count - half_width, mode);
19249         }
19250       else
19251         {
19252           gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
19253
19254           if (!rtx_equal_p (operands[0], operands[1]))
19255             emit_move_insn (operands[0], operands[1]);
19256
19257           emit_insn (gen_shld (high[0], low[0], GEN_INT (count)));
19258           ix86_expand_ashl_const (low[0], count, mode);
19259         }
19260       return;
19261     }
19262
19263   split_double_mode (mode, operands, 1, low, high);
19264
19265   gen_ashl3 = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
19266
19267   if (operands[1] == const1_rtx)
19268     {
19269       /* Assuming we've chosen a QImode capable registers, then 1 << N
19270          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
19271       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
19272         {
19273           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
19274
19275           ix86_expand_clear (low[0]);
19276           ix86_expand_clear (high[0]);
19277           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (half_width)));
19278
19279           d = gen_lowpart (QImode, low[0]);
19280           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
19281           s = gen_rtx_EQ (QImode, flags, const0_rtx);
19282           emit_insn (gen_rtx_SET (VOIDmode, d, s));
19283
19284           d = gen_lowpart (QImode, high[0]);
19285           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
19286           s = gen_rtx_NE (QImode, flags, const0_rtx);
19287           emit_insn (gen_rtx_SET (VOIDmode, d, s));
19288         }
19289
19290       /* Otherwise, we can get the same results by manually performing
19291          a bit extract operation on bit 5/6, and then performing the two
19292          shifts.  The two methods of getting 0/1 into low/high are exactly
19293          the same size.  Avoiding the shift in the bit extract case helps
19294          pentium4 a bit; no one else seems to care much either way.  */
19295       else
19296         {
19297           enum machine_mode half_mode;
19298           rtx (*gen_lshr3)(rtx, rtx, rtx);
19299           rtx (*gen_and3)(rtx, rtx, rtx);
19300           rtx (*gen_xor3)(rtx, rtx, rtx);
19301           HOST_WIDE_INT bits;
19302           rtx x;
19303
19304           if (mode == DImode)
19305             {
19306               half_mode = SImode;
19307               gen_lshr3 = gen_lshrsi3;
19308               gen_and3 = gen_andsi3;
19309               gen_xor3 = gen_xorsi3;
19310               bits = 5;
19311             }
19312           else
19313             {
19314               half_mode = DImode;
19315               gen_lshr3 = gen_lshrdi3;
19316               gen_and3 = gen_anddi3;
19317               gen_xor3 = gen_xordi3;
19318               bits = 6;
19319             }
19320
19321           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
19322             x = gen_rtx_ZERO_EXTEND (half_mode, operands[2]);
19323           else
19324             x = gen_lowpart (half_mode, operands[2]);
19325           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
19326
19327           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (bits)));
19328           emit_insn (gen_and3 (high[0], high[0], const1_rtx));
19329           emit_move_insn (low[0], high[0]);
19330           emit_insn (gen_xor3 (low[0], low[0], const1_rtx));
19331         }
19332
19333       emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
19334       emit_insn (gen_ashl3 (high[0], high[0], operands[2]));
19335       return;
19336     }
19337
19338   if (operands[1] == constm1_rtx)
19339     {
19340       /* For -1 << N, we can avoid the shld instruction, because we
19341          know that we're shifting 0...31/63 ones into a -1.  */
19342       emit_move_insn (low[0], constm1_rtx);
19343       if (optimize_insn_for_size_p ())
19344         emit_move_insn (high[0], low[0]);
19345       else
19346         emit_move_insn (high[0], constm1_rtx);
19347     }
19348   else
19349     {
19350       gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
19351
19352       if (!rtx_equal_p (operands[0], operands[1]))
19353         emit_move_insn (operands[0], operands[1]);
19354
19355       split_double_mode (mode, operands, 1, low, high);
19356       emit_insn (gen_shld (high[0], low[0], operands[2]));
19357     }
19358
19359   emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
19360
19361   if (TARGET_CMOVE && scratch)
19362     {
19363       rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
19364         = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
19365
19366       ix86_expand_clear (scratch);
19367       emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2], scratch));
19368     }
19369   else
19370     {
19371       rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
19372         = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
19373
19374       emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
19375     }
19376 }
19377
19378 void
19379 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
19380 {
19381   rtx (*gen_ashr3)(rtx, rtx, rtx)
19382     = mode == DImode ? gen_ashrsi3 : gen_ashrdi3;
19383   rtx (*gen_shrd)(rtx, rtx, rtx);
19384   int half_width = GET_MODE_BITSIZE (mode) >> 1;
19385
19386   rtx low[2], high[2];
19387   int count;
19388
19389   if (CONST_INT_P (operands[2]))
19390     {
19391       split_double_mode (mode, operands, 2, low, high);
19392       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
19393
19394       if (count == GET_MODE_BITSIZE (mode) - 1)
19395         {
19396           emit_move_insn (high[0], high[1]);
19397           emit_insn (gen_ashr3 (high[0], high[0],
19398                                 GEN_INT (half_width - 1)));
19399           emit_move_insn (low[0], high[0]);
19400
19401         }
19402       else if (count >= half_width)
19403         {
19404           emit_move_insn (low[0], high[1]);
19405           emit_move_insn (high[0], low[0]);
19406           emit_insn (gen_ashr3 (high[0], high[0],
19407                                 GEN_INT (half_width - 1)));
19408
19409           if (count > half_width)
19410             emit_insn (gen_ashr3 (low[0], low[0],
19411                                   GEN_INT (count - half_width)));
19412         }
19413       else
19414         {
19415           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19416
19417           if (!rtx_equal_p (operands[0], operands[1]))
19418             emit_move_insn (operands[0], operands[1]);
19419
19420           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
19421           emit_insn (gen_ashr3 (high[0], high[0], GEN_INT (count)));
19422         }
19423     }
19424   else
19425     {
19426       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19427
19428      if (!rtx_equal_p (operands[0], operands[1]))
19429         emit_move_insn (operands[0], operands[1]);
19430
19431       split_double_mode (mode, operands, 1, low, high);
19432
19433       emit_insn (gen_shrd (low[0], high[0], operands[2]));
19434       emit_insn (gen_ashr3 (high[0], high[0], operands[2]));
19435
19436       if (TARGET_CMOVE && scratch)
19437         {
19438           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
19439             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
19440
19441           emit_move_insn (scratch, high[0]);
19442           emit_insn (gen_ashr3 (scratch, scratch,
19443                                 GEN_INT (half_width - 1)));
19444           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
19445                                           scratch));
19446         }
19447       else
19448         {
19449           rtx (*gen_x86_shift_adj_3)(rtx, rtx, rtx)
19450             = mode == DImode ? gen_x86_shiftsi_adj_3 : gen_x86_shiftdi_adj_3;
19451
19452           emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
19453         }
19454     }
19455 }
19456
19457 void
19458 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
19459 {
19460   rtx (*gen_lshr3)(rtx, rtx, rtx)
19461     = mode == DImode ? gen_lshrsi3 : gen_lshrdi3;
19462   rtx (*gen_shrd)(rtx, rtx, rtx);
19463   int half_width = GET_MODE_BITSIZE (mode) >> 1;
19464
19465   rtx low[2], high[2];
19466   int count;
19467
19468   if (CONST_INT_P (operands[2]))
19469     {
19470       split_double_mode (mode, operands, 2, low, high);
19471       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
19472
19473       if (count >= half_width)
19474         {
19475           emit_move_insn (low[0], high[1]);
19476           ix86_expand_clear (high[0]);
19477
19478           if (count > half_width)
19479             emit_insn (gen_lshr3 (low[0], low[0],
19480                                   GEN_INT (count - half_width)));
19481         }
19482       else
19483         {
19484           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19485
19486           if (!rtx_equal_p (operands[0], operands[1]))
19487             emit_move_insn (operands[0], operands[1]);
19488
19489           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
19490           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (count)));
19491         }
19492     }
19493   else
19494     {
19495       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
19496
19497       if (!rtx_equal_p (operands[0], operands[1]))
19498         emit_move_insn (operands[0], operands[1]);
19499
19500       split_double_mode (mode, operands, 1, low, high);
19501
19502       emit_insn (gen_shrd (low[0], high[0], operands[2]));
19503       emit_insn (gen_lshr3 (high[0], high[0], operands[2]));
19504
19505       if (TARGET_CMOVE && scratch)
19506         {
19507           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
19508             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
19509
19510           ix86_expand_clear (scratch);
19511           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
19512                                           scratch));
19513         }
19514       else
19515         {
19516           rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
19517             = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
19518
19519           emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
19520         }
19521     }
19522 }
19523
19524 /* Predict just emitted jump instruction to be taken with probability PROB.  */
19525 static void
19526 predict_jump (int prob)
19527 {
19528   rtx insn = get_last_insn ();
19529   gcc_assert (JUMP_P (insn));
19530   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
19531 }
19532
19533 /* Helper function for the string operations below.  Dest VARIABLE whether
19534    it is aligned to VALUE bytes.  If true, jump to the label.  */
19535 static rtx
19536 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
19537 {
19538   rtx label = gen_label_rtx ();
19539   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
19540   if (GET_MODE (variable) == DImode)
19541     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
19542   else
19543     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
19544   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
19545                            1, label);
19546   if (epilogue)
19547     predict_jump (REG_BR_PROB_BASE * 50 / 100);
19548   else
19549     predict_jump (REG_BR_PROB_BASE * 90 / 100);
19550   return label;
19551 }
19552
19553 /* Adjust COUNTER by the VALUE.  */
19554 static void
19555 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
19556 {
19557   rtx (*gen_add)(rtx, rtx, rtx)
19558     = GET_MODE (countreg) == DImode ? gen_adddi3 : gen_addsi3;
19559
19560   emit_insn (gen_add (countreg, countreg, GEN_INT (-value)));
19561 }
19562
19563 /* Zero extend possibly SImode EXP to Pmode register.  */
19564 rtx
19565 ix86_zero_extend_to_Pmode (rtx exp)
19566 {
19567   rtx r;
19568   if (GET_MODE (exp) == VOIDmode)
19569     return force_reg (Pmode, exp);
19570   if (GET_MODE (exp) == Pmode)
19571     return copy_to_mode_reg (Pmode, exp);
19572   r = gen_reg_rtx (Pmode);
19573   emit_insn (gen_zero_extendsidi2 (r, exp));
19574   return r;
19575 }
19576
19577 /* Divide COUNTREG by SCALE.  */
19578 static rtx
19579 scale_counter (rtx countreg, int scale)
19580 {
19581   rtx sc;
19582
19583   if (scale == 1)
19584     return countreg;
19585   if (CONST_INT_P (countreg))
19586     return GEN_INT (INTVAL (countreg) / scale);
19587   gcc_assert (REG_P (countreg));
19588
19589   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
19590                             GEN_INT (exact_log2 (scale)),
19591                             NULL, 1, OPTAB_DIRECT);
19592   return sc;
19593 }
19594
19595 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
19596    DImode for constant loop counts.  */
19597
19598 static enum machine_mode
19599 counter_mode (rtx count_exp)
19600 {
19601   if (GET_MODE (count_exp) != VOIDmode)
19602     return GET_MODE (count_exp);
19603   if (!CONST_INT_P (count_exp))
19604     return Pmode;
19605   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
19606     return DImode;
19607   return SImode;
19608 }
19609
19610 /* When SRCPTR is non-NULL, output simple loop to move memory
19611    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
19612    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
19613    equivalent loop to set memory by VALUE (supposed to be in MODE).
19614
19615    The size is rounded down to whole number of chunk size moved at once.
19616    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
19617
19618
19619 static void
19620 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
19621                                rtx destptr, rtx srcptr, rtx value,
19622                                rtx count, enum machine_mode mode, int unroll,
19623                                int expected_size)
19624 {
19625   rtx out_label, top_label, iter, tmp;
19626   enum machine_mode iter_mode = counter_mode (count);
19627   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
19628   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
19629   rtx size;
19630   rtx x_addr;
19631   rtx y_addr;
19632   int i;
19633
19634   top_label = gen_label_rtx ();
19635   out_label = gen_label_rtx ();
19636   iter = gen_reg_rtx (iter_mode);
19637
19638   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
19639                               NULL, 1, OPTAB_DIRECT);
19640   /* Those two should combine.  */
19641   if (piece_size == const1_rtx)
19642     {
19643       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
19644                                true, out_label);
19645       predict_jump (REG_BR_PROB_BASE * 10 / 100);
19646     }
19647   emit_move_insn (iter, const0_rtx);
19648
19649   emit_label (top_label);
19650
19651   tmp = convert_modes (Pmode, iter_mode, iter, true);
19652   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
19653   destmem = change_address (destmem, mode, x_addr);
19654
19655   if (srcmem)
19656     {
19657       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
19658       srcmem = change_address (srcmem, mode, y_addr);
19659
19660       /* When unrolling for chips that reorder memory reads and writes,
19661          we can save registers by using single temporary.
19662          Also using 4 temporaries is overkill in 32bit mode.  */
19663       if (!TARGET_64BIT && 0)
19664         {
19665           for (i = 0; i < unroll; i++)
19666             {
19667               if (i)
19668                 {
19669                   destmem =
19670                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19671                   srcmem =
19672                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
19673                 }
19674               emit_move_insn (destmem, srcmem);
19675             }
19676         }
19677       else
19678         {
19679           rtx tmpreg[4];
19680           gcc_assert (unroll <= 4);
19681           for (i = 0; i < unroll; i++)
19682             {
19683               tmpreg[i] = gen_reg_rtx (mode);
19684               if (i)
19685                 {
19686                   srcmem =
19687                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
19688                 }
19689               emit_move_insn (tmpreg[i], srcmem);
19690             }
19691           for (i = 0; i < unroll; i++)
19692             {
19693               if (i)
19694                 {
19695                   destmem =
19696                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19697                 }
19698               emit_move_insn (destmem, tmpreg[i]);
19699             }
19700         }
19701     }
19702   else
19703     for (i = 0; i < unroll; i++)
19704       {
19705         if (i)
19706           destmem =
19707             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19708         emit_move_insn (destmem, value);
19709       }
19710
19711   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
19712                              true, OPTAB_LIB_WIDEN);
19713   if (tmp != iter)
19714     emit_move_insn (iter, tmp);
19715
19716   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
19717                            true, top_label);
19718   if (expected_size != -1)
19719     {
19720       expected_size /= GET_MODE_SIZE (mode) * unroll;
19721       if (expected_size == 0)
19722         predict_jump (0);
19723       else if (expected_size > REG_BR_PROB_BASE)
19724         predict_jump (REG_BR_PROB_BASE - 1);
19725       else
19726         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
19727     }
19728   else
19729     predict_jump (REG_BR_PROB_BASE * 80 / 100);
19730   iter = ix86_zero_extend_to_Pmode (iter);
19731   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
19732                              true, OPTAB_LIB_WIDEN);
19733   if (tmp != destptr)
19734     emit_move_insn (destptr, tmp);
19735   if (srcptr)
19736     {
19737       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
19738                                  true, OPTAB_LIB_WIDEN);
19739       if (tmp != srcptr)
19740         emit_move_insn (srcptr, tmp);
19741     }
19742   emit_label (out_label);
19743 }
19744
19745 /* Output "rep; mov" instruction.
19746    Arguments have same meaning as for previous function */
19747 static void
19748 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
19749                            rtx destptr, rtx srcptr,
19750                            rtx count,
19751                            enum machine_mode mode)
19752 {
19753   rtx destexp;
19754   rtx srcexp;
19755   rtx countreg;
19756
19757   /* If the size is known, it is shorter to use rep movs.  */
19758   if (mode == QImode && CONST_INT_P (count)
19759       && !(INTVAL (count) & 3))
19760     mode = SImode;
19761
19762   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
19763     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
19764   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
19765     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
19766   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
19767   if (mode != QImode)
19768     {
19769       destexp = gen_rtx_ASHIFT (Pmode, countreg,
19770                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19771       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
19772       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
19773                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19774       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
19775     }
19776   else
19777     {
19778       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
19779       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
19780     }
19781   if (CONST_INT_P (count))
19782     {
19783       count = GEN_INT (INTVAL (count)
19784                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
19785       destmem = shallow_copy_rtx (destmem);
19786       srcmem = shallow_copy_rtx (srcmem);
19787       set_mem_size (destmem, count);
19788       set_mem_size (srcmem, count);
19789     }
19790   else
19791     {
19792       if (MEM_SIZE (destmem))
19793         set_mem_size (destmem, NULL_RTX);
19794       if (MEM_SIZE (srcmem))
19795         set_mem_size (srcmem, NULL_RTX);
19796     }
19797   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
19798                           destexp, srcexp));
19799 }
19800
19801 /* Output "rep; stos" instruction.
19802    Arguments have same meaning as for previous function */
19803 static void
19804 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
19805                             rtx count, enum machine_mode mode,
19806                             rtx orig_value)
19807 {
19808   rtx destexp;
19809   rtx countreg;
19810
19811   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
19812     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
19813   value = force_reg (mode, gen_lowpart (mode, value));
19814   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
19815   if (mode != QImode)
19816     {
19817       destexp = gen_rtx_ASHIFT (Pmode, countreg,
19818                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19819       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
19820     }
19821   else
19822     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
19823   if (orig_value == const0_rtx && CONST_INT_P (count))
19824     {
19825       count = GEN_INT (INTVAL (count)
19826                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
19827       destmem = shallow_copy_rtx (destmem);
19828       set_mem_size (destmem, count);
19829     }
19830   else if (MEM_SIZE (destmem))
19831     set_mem_size (destmem, NULL_RTX);
19832   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
19833 }
19834
19835 static void
19836 emit_strmov (rtx destmem, rtx srcmem,
19837              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
19838 {
19839   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
19840   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
19841   emit_insn (gen_strmov (destptr, dest, srcptr, src));
19842 }
19843
19844 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
19845 static void
19846 expand_movmem_epilogue (rtx destmem, rtx srcmem,
19847                         rtx destptr, rtx srcptr, rtx count, int max_size)
19848 {
19849   rtx src, dest;
19850   if (CONST_INT_P (count))
19851     {
19852       HOST_WIDE_INT countval = INTVAL (count);
19853       int offset = 0;
19854
19855       if ((countval & 0x10) && max_size > 16)
19856         {
19857           if (TARGET_64BIT)
19858             {
19859               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
19860               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
19861             }
19862           else
19863             gcc_unreachable ();
19864           offset += 16;
19865         }
19866       if ((countval & 0x08) && max_size > 8)
19867         {
19868           if (TARGET_64BIT)
19869             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
19870           else
19871             {
19872               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
19873               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
19874             }
19875           offset += 8;
19876         }
19877       if ((countval & 0x04) && max_size > 4)
19878         {
19879           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
19880           offset += 4;
19881         }
19882       if ((countval & 0x02) && max_size > 2)
19883         {
19884           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
19885           offset += 2;
19886         }
19887       if ((countval & 0x01) && max_size > 1)
19888         {
19889           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
19890           offset += 1;
19891         }
19892       return;
19893     }
19894   if (max_size > 8)
19895     {
19896       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
19897                                     count, 1, OPTAB_DIRECT);
19898       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
19899                                      count, QImode, 1, 4);
19900       return;
19901     }
19902
19903   /* When there are stringops, we can cheaply increase dest and src pointers.
19904      Otherwise we save code size by maintaining offset (zero is readily
19905      available from preceding rep operation) and using x86 addressing modes.
19906    */
19907   if (TARGET_SINGLE_STRINGOP)
19908     {
19909       if (max_size > 4)
19910         {
19911           rtx label = ix86_expand_aligntest (count, 4, true);
19912           src = change_address (srcmem, SImode, srcptr);
19913           dest = change_address (destmem, SImode, destptr);
19914           emit_insn (gen_strmov (destptr, dest, srcptr, src));
19915           emit_label (label);
19916           LABEL_NUSES (label) = 1;
19917         }
19918       if (max_size > 2)
19919         {
19920           rtx label = ix86_expand_aligntest (count, 2, true);
19921           src = change_address (srcmem, HImode, srcptr);
19922           dest = change_address (destmem, HImode, destptr);
19923           emit_insn (gen_strmov (destptr, dest, srcptr, src));
19924           emit_label (label);
19925           LABEL_NUSES (label) = 1;
19926         }
19927       if (max_size > 1)
19928         {
19929           rtx label = ix86_expand_aligntest (count, 1, true);
19930           src = change_address (srcmem, QImode, srcptr);
19931           dest = change_address (destmem, QImode, destptr);
19932           emit_insn (gen_strmov (destptr, dest, srcptr, src));
19933           emit_label (label);
19934           LABEL_NUSES (label) = 1;
19935         }
19936     }
19937   else
19938     {
19939       rtx offset = force_reg (Pmode, const0_rtx);
19940       rtx tmp;
19941
19942       if (max_size > 4)
19943         {
19944           rtx label = ix86_expand_aligntest (count, 4, true);
19945           src = change_address (srcmem, SImode, srcptr);
19946           dest = change_address (destmem, SImode, destptr);
19947           emit_move_insn (dest, src);
19948           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
19949                                      true, OPTAB_LIB_WIDEN);
19950           if (tmp != offset)
19951             emit_move_insn (offset, tmp);
19952           emit_label (label);
19953           LABEL_NUSES (label) = 1;
19954         }
19955       if (max_size > 2)
19956         {
19957           rtx label = ix86_expand_aligntest (count, 2, true);
19958           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
19959           src = change_address (srcmem, HImode, tmp);
19960           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
19961           dest = change_address (destmem, HImode, tmp);
19962           emit_move_insn (dest, src);
19963           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
19964                                      true, OPTAB_LIB_WIDEN);
19965           if (tmp != offset)
19966             emit_move_insn (offset, tmp);
19967           emit_label (label);
19968           LABEL_NUSES (label) = 1;
19969         }
19970       if (max_size > 1)
19971         {
19972           rtx label = ix86_expand_aligntest (count, 1, true);
19973           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
19974           src = change_address (srcmem, QImode, tmp);
19975           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
19976           dest = change_address (destmem, QImode, tmp);
19977           emit_move_insn (dest, src);
19978           emit_label (label);
19979           LABEL_NUSES (label) = 1;
19980         }
19981     }
19982 }
19983
19984 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
19985 static void
19986 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
19987                                  rtx count, int max_size)
19988 {
19989   count =
19990     expand_simple_binop (counter_mode (count), AND, count,
19991                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
19992   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
19993                                  gen_lowpart (QImode, value), count, QImode,
19994                                  1, max_size / 2);
19995 }
19996
19997 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
19998 static void
19999 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
20000 {
20001   rtx dest;
20002
20003   if (CONST_INT_P (count))
20004     {
20005       HOST_WIDE_INT countval = INTVAL (count);
20006       int offset = 0;
20007
20008       if ((countval & 0x10) && max_size > 16)
20009         {
20010           if (TARGET_64BIT)
20011             {
20012               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
20013               emit_insn (gen_strset (destptr, dest, value));
20014               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
20015               emit_insn (gen_strset (destptr, dest, value));
20016             }
20017           else
20018             gcc_unreachable ();
20019           offset += 16;
20020         }
20021       if ((countval & 0x08) && max_size > 8)
20022         {
20023           if (TARGET_64BIT)
20024             {
20025               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
20026               emit_insn (gen_strset (destptr, dest, value));
20027             }
20028           else
20029             {
20030               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
20031               emit_insn (gen_strset (destptr, dest, value));
20032               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
20033               emit_insn (gen_strset (destptr, dest, value));
20034             }
20035           offset += 8;
20036         }
20037       if ((countval & 0x04) && max_size > 4)
20038         {
20039           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
20040           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
20041           offset += 4;
20042         }
20043       if ((countval & 0x02) && max_size > 2)
20044         {
20045           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
20046           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
20047           offset += 2;
20048         }
20049       if ((countval & 0x01) && max_size > 1)
20050         {
20051           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
20052           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
20053           offset += 1;
20054         }
20055       return;
20056     }
20057   if (max_size > 32)
20058     {
20059       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
20060       return;
20061     }
20062   if (max_size > 16)
20063     {
20064       rtx label = ix86_expand_aligntest (count, 16, true);
20065       if (TARGET_64BIT)
20066         {
20067           dest = change_address (destmem, DImode, destptr);
20068           emit_insn (gen_strset (destptr, dest, value));
20069           emit_insn (gen_strset (destptr, dest, value));
20070         }
20071       else
20072         {
20073           dest = change_address (destmem, SImode, destptr);
20074           emit_insn (gen_strset (destptr, dest, value));
20075           emit_insn (gen_strset (destptr, dest, value));
20076           emit_insn (gen_strset (destptr, dest, value));
20077           emit_insn (gen_strset (destptr, dest, value));
20078         }
20079       emit_label (label);
20080       LABEL_NUSES (label) = 1;
20081     }
20082   if (max_size > 8)
20083     {
20084       rtx label = ix86_expand_aligntest (count, 8, true);
20085       if (TARGET_64BIT)
20086         {
20087           dest = change_address (destmem, DImode, destptr);
20088           emit_insn (gen_strset (destptr, dest, value));
20089         }
20090       else
20091         {
20092           dest = change_address (destmem, SImode, destptr);
20093           emit_insn (gen_strset (destptr, dest, value));
20094           emit_insn (gen_strset (destptr, dest, value));
20095         }
20096       emit_label (label);
20097       LABEL_NUSES (label) = 1;
20098     }
20099   if (max_size > 4)
20100     {
20101       rtx label = ix86_expand_aligntest (count, 4, true);
20102       dest = change_address (destmem, SImode, destptr);
20103       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
20104       emit_label (label);
20105       LABEL_NUSES (label) = 1;
20106     }
20107   if (max_size > 2)
20108     {
20109       rtx label = ix86_expand_aligntest (count, 2, true);
20110       dest = change_address (destmem, HImode, destptr);
20111       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
20112       emit_label (label);
20113       LABEL_NUSES (label) = 1;
20114     }
20115   if (max_size > 1)
20116     {
20117       rtx label = ix86_expand_aligntest (count, 1, true);
20118       dest = change_address (destmem, QImode, destptr);
20119       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
20120       emit_label (label);
20121       LABEL_NUSES (label) = 1;
20122     }
20123 }
20124
20125 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
20126    DESIRED_ALIGNMENT.  */
20127 static void
20128 expand_movmem_prologue (rtx destmem, rtx srcmem,
20129                         rtx destptr, rtx srcptr, rtx count,
20130                         int align, int desired_alignment)
20131 {
20132   if (align <= 1 && desired_alignment > 1)
20133     {
20134       rtx label = ix86_expand_aligntest (destptr, 1, false);
20135       srcmem = change_address (srcmem, QImode, srcptr);
20136       destmem = change_address (destmem, QImode, destptr);
20137       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
20138       ix86_adjust_counter (count, 1);
20139       emit_label (label);
20140       LABEL_NUSES (label) = 1;
20141     }
20142   if (align <= 2 && desired_alignment > 2)
20143     {
20144       rtx label = ix86_expand_aligntest (destptr, 2, false);
20145       srcmem = change_address (srcmem, HImode, srcptr);
20146       destmem = change_address (destmem, HImode, destptr);
20147       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
20148       ix86_adjust_counter (count, 2);
20149       emit_label (label);
20150       LABEL_NUSES (label) = 1;
20151     }
20152   if (align <= 4 && desired_alignment > 4)
20153     {
20154       rtx label = ix86_expand_aligntest (destptr, 4, false);
20155       srcmem = change_address (srcmem, SImode, srcptr);
20156       destmem = change_address (destmem, SImode, destptr);
20157       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
20158       ix86_adjust_counter (count, 4);
20159       emit_label (label);
20160       LABEL_NUSES (label) = 1;
20161     }
20162   gcc_assert (desired_alignment <= 8);
20163 }
20164
20165 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
20166    ALIGN_BYTES is how many bytes need to be copied.  */
20167 static rtx
20168 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
20169                                  int desired_align, int align_bytes)
20170 {
20171   rtx src = *srcp;
20172   rtx src_size, dst_size;
20173   int off = 0;
20174   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
20175   if (src_align_bytes >= 0)
20176     src_align_bytes = desired_align - src_align_bytes;
20177   src_size = MEM_SIZE (src);
20178   dst_size = MEM_SIZE (dst);
20179   if (align_bytes & 1)
20180     {
20181       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
20182       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
20183       off = 1;
20184       emit_insn (gen_strmov (destreg, dst, srcreg, src));
20185     }
20186   if (align_bytes & 2)
20187     {
20188       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
20189       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
20190       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
20191         set_mem_align (dst, 2 * BITS_PER_UNIT);
20192       if (src_align_bytes >= 0
20193           && (src_align_bytes & 1) == (align_bytes & 1)
20194           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
20195         set_mem_align (src, 2 * BITS_PER_UNIT);
20196       off = 2;
20197       emit_insn (gen_strmov (destreg, dst, srcreg, src));
20198     }
20199   if (align_bytes & 4)
20200     {
20201       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
20202       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
20203       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
20204         set_mem_align (dst, 4 * BITS_PER_UNIT);
20205       if (src_align_bytes >= 0)
20206         {
20207           unsigned int src_align = 0;
20208           if ((src_align_bytes & 3) == (align_bytes & 3))
20209             src_align = 4;
20210           else if ((src_align_bytes & 1) == (align_bytes & 1))
20211             src_align = 2;
20212           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
20213             set_mem_align (src, src_align * BITS_PER_UNIT);
20214         }
20215       off = 4;
20216       emit_insn (gen_strmov (destreg, dst, srcreg, src));
20217     }
20218   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
20219   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
20220   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
20221     set_mem_align (dst, desired_align * BITS_PER_UNIT);
20222   if (src_align_bytes >= 0)
20223     {
20224       unsigned int src_align = 0;
20225       if ((src_align_bytes & 7) == (align_bytes & 7))
20226         src_align = 8;
20227       else if ((src_align_bytes & 3) == (align_bytes & 3))
20228         src_align = 4;
20229       else if ((src_align_bytes & 1) == (align_bytes & 1))
20230         src_align = 2;
20231       if (src_align > (unsigned int) desired_align)
20232         src_align = desired_align;
20233       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
20234         set_mem_align (src, src_align * BITS_PER_UNIT);
20235     }
20236   if (dst_size)
20237     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
20238   if (src_size)
20239     set_mem_size (dst, GEN_INT (INTVAL (src_size) - align_bytes));
20240   *srcp = src;
20241   return dst;
20242 }
20243
20244 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
20245    DESIRED_ALIGNMENT.  */
20246 static void
20247 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
20248                         int align, int desired_alignment)
20249 {
20250   if (align <= 1 && desired_alignment > 1)
20251     {
20252       rtx label = ix86_expand_aligntest (destptr, 1, false);
20253       destmem = change_address (destmem, QImode, destptr);
20254       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
20255       ix86_adjust_counter (count, 1);
20256       emit_label (label);
20257       LABEL_NUSES (label) = 1;
20258     }
20259   if (align <= 2 && desired_alignment > 2)
20260     {
20261       rtx label = ix86_expand_aligntest (destptr, 2, false);
20262       destmem = change_address (destmem, HImode, destptr);
20263       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
20264       ix86_adjust_counter (count, 2);
20265       emit_label (label);
20266       LABEL_NUSES (label) = 1;
20267     }
20268   if (align <= 4 && desired_alignment > 4)
20269     {
20270       rtx label = ix86_expand_aligntest (destptr, 4, false);
20271       destmem = change_address (destmem, SImode, destptr);
20272       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
20273       ix86_adjust_counter (count, 4);
20274       emit_label (label);
20275       LABEL_NUSES (label) = 1;
20276     }
20277   gcc_assert (desired_alignment <= 8);
20278 }
20279
20280 /* Set enough from DST to align DST known to by aligned by ALIGN to
20281    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
20282 static rtx
20283 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
20284                                  int desired_align, int align_bytes)
20285 {
20286   int off = 0;
20287   rtx dst_size = MEM_SIZE (dst);
20288   if (align_bytes & 1)
20289     {
20290       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
20291       off = 1;
20292       emit_insn (gen_strset (destreg, dst,
20293                              gen_lowpart (QImode, value)));
20294     }
20295   if (align_bytes & 2)
20296     {
20297       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
20298       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
20299         set_mem_align (dst, 2 * BITS_PER_UNIT);
20300       off = 2;
20301       emit_insn (gen_strset (destreg, dst,
20302                              gen_lowpart (HImode, value)));
20303     }
20304   if (align_bytes & 4)
20305     {
20306       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
20307       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
20308         set_mem_align (dst, 4 * BITS_PER_UNIT);
20309       off = 4;
20310       emit_insn (gen_strset (destreg, dst,
20311                              gen_lowpart (SImode, value)));
20312     }
20313   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
20314   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
20315     set_mem_align (dst, desired_align * BITS_PER_UNIT);
20316   if (dst_size)
20317     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
20318   return dst;
20319 }
20320
20321 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
20322 static enum stringop_alg
20323 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
20324             int *dynamic_check)
20325 {
20326   const struct stringop_algs * algs;
20327   bool optimize_for_speed;
20328   /* Algorithms using the rep prefix want at least edi and ecx;
20329      additionally, memset wants eax and memcpy wants esi.  Don't
20330      consider such algorithms if the user has appropriated those
20331      registers for their own purposes.  */
20332   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
20333                              || (memset
20334                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
20335
20336 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
20337                            || (alg != rep_prefix_1_byte         \
20338                                && alg != rep_prefix_4_byte      \
20339                                && alg != rep_prefix_8_byte))
20340   const struct processor_costs *cost;
20341
20342   /* Even if the string operation call is cold, we still might spend a lot
20343      of time processing large blocks.  */
20344   if (optimize_function_for_size_p (cfun)
20345       || (optimize_insn_for_size_p ()
20346           && expected_size != -1 && expected_size < 256))
20347     optimize_for_speed = false;
20348   else
20349     optimize_for_speed = true;
20350
20351   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
20352
20353   *dynamic_check = -1;
20354   if (memset)
20355     algs = &cost->memset[TARGET_64BIT != 0];
20356   else
20357     algs = &cost->memcpy[TARGET_64BIT != 0];
20358   if (stringop_alg != no_stringop && ALG_USABLE_P (stringop_alg))
20359     return stringop_alg;
20360   /* rep; movq or rep; movl is the smallest variant.  */
20361   else if (!optimize_for_speed)
20362     {
20363       if (!count || (count & 3))
20364         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
20365       else
20366         return rep_prefix_usable ? rep_prefix_4_byte : loop;
20367     }
20368   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
20369    */
20370   else if (expected_size != -1 && expected_size < 4)
20371     return loop_1_byte;
20372   else if (expected_size != -1)
20373     {
20374       unsigned int i;
20375       enum stringop_alg alg = libcall;
20376       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
20377         {
20378           /* We get here if the algorithms that were not libcall-based
20379              were rep-prefix based and we are unable to use rep prefixes
20380              based on global register usage.  Break out of the loop and
20381              use the heuristic below.  */
20382           if (algs->size[i].max == 0)
20383             break;
20384           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
20385             {
20386               enum stringop_alg candidate = algs->size[i].alg;
20387
20388               if (candidate != libcall && ALG_USABLE_P (candidate))
20389                 alg = candidate;
20390               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
20391                  last non-libcall inline algorithm.  */
20392               if (TARGET_INLINE_ALL_STRINGOPS)
20393                 {
20394                   /* When the current size is best to be copied by a libcall,
20395                      but we are still forced to inline, run the heuristic below
20396                      that will pick code for medium sized blocks.  */
20397                   if (alg != libcall)
20398                     return alg;
20399                   break;
20400                 }
20401               else if (ALG_USABLE_P (candidate))
20402                 return candidate;
20403             }
20404         }
20405       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
20406     }
20407   /* When asked to inline the call anyway, try to pick meaningful choice.
20408      We look for maximal size of block that is faster to copy by hand and
20409      take blocks of at most of that size guessing that average size will
20410      be roughly half of the block.
20411
20412      If this turns out to be bad, we might simply specify the preferred
20413      choice in ix86_costs.  */
20414   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
20415       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
20416     {
20417       int max = -1;
20418       enum stringop_alg alg;
20419       int i;
20420       bool any_alg_usable_p = true;
20421
20422       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
20423         {
20424           enum stringop_alg candidate = algs->size[i].alg;
20425           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
20426
20427           if (candidate != libcall && candidate
20428               && ALG_USABLE_P (candidate))
20429               max = algs->size[i].max;
20430         }
20431       /* If there aren't any usable algorithms, then recursing on
20432          smaller sizes isn't going to find anything.  Just return the
20433          simple byte-at-a-time copy loop.  */
20434       if (!any_alg_usable_p)
20435         {
20436           /* Pick something reasonable.  */
20437           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
20438             *dynamic_check = 128;
20439           return loop_1_byte;
20440         }
20441       if (max == -1)
20442         max = 4096;
20443       alg = decide_alg (count, max / 2, memset, dynamic_check);
20444       gcc_assert (*dynamic_check == -1);
20445       gcc_assert (alg != libcall);
20446       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
20447         *dynamic_check = max;
20448       return alg;
20449     }
20450   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
20451 #undef ALG_USABLE_P
20452 }
20453
20454 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
20455    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
20456 static int
20457 decide_alignment (int align,
20458                   enum stringop_alg alg,
20459                   int expected_size)
20460 {
20461   int desired_align = 0;
20462   switch (alg)
20463     {
20464       case no_stringop:
20465         gcc_unreachable ();
20466       case loop:
20467       case unrolled_loop:
20468         desired_align = GET_MODE_SIZE (Pmode);
20469         break;
20470       case rep_prefix_8_byte:
20471         desired_align = 8;
20472         break;
20473       case rep_prefix_4_byte:
20474         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
20475            copying whole cacheline at once.  */
20476         if (TARGET_PENTIUMPRO)
20477           desired_align = 8;
20478         else
20479           desired_align = 4;
20480         break;
20481       case rep_prefix_1_byte:
20482         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
20483            copying whole cacheline at once.  */
20484         if (TARGET_PENTIUMPRO)
20485           desired_align = 8;
20486         else
20487           desired_align = 1;
20488         break;
20489       case loop_1_byte:
20490         desired_align = 1;
20491         break;
20492       case libcall:
20493         return 0;
20494     }
20495
20496   if (optimize_size)
20497     desired_align = 1;
20498   if (desired_align < align)
20499     desired_align = align;
20500   if (expected_size != -1 && expected_size < 4)
20501     desired_align = align;
20502   return desired_align;
20503 }
20504
20505 /* Return the smallest power of 2 greater than VAL.  */
20506 static int
20507 smallest_pow2_greater_than (int val)
20508 {
20509   int ret = 1;
20510   while (ret <= val)
20511     ret <<= 1;
20512   return ret;
20513 }
20514
20515 /* Expand string move (memcpy) operation.  Use i386 string operations when
20516    profitable.  expand_setmem contains similar code.  The code depends upon
20517    architecture, block size and alignment, but always has the same
20518    overall structure:
20519
20520    1) Prologue guard: Conditional that jumps up to epilogues for small
20521       blocks that can be handled by epilogue alone.  This is faster but
20522       also needed for correctness, since prologue assume the block is larger
20523       than the desired alignment.
20524
20525       Optional dynamic check for size and libcall for large
20526       blocks is emitted here too, with -minline-stringops-dynamically.
20527
20528    2) Prologue: copy first few bytes in order to get destination aligned
20529       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
20530       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
20531       We emit either a jump tree on power of two sized blocks, or a byte loop.
20532
20533    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
20534       with specified algorithm.
20535
20536    4) Epilogue: code copying tail of the block that is too small to be
20537       handled by main body (or up to size guarded by prologue guard).  */
20538
20539 bool
20540 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
20541                     rtx expected_align_exp, rtx expected_size_exp)
20542 {
20543   rtx destreg;
20544   rtx srcreg;
20545   rtx label = NULL;
20546   rtx tmp;
20547   rtx jump_around_label = NULL;
20548   HOST_WIDE_INT align = 1;
20549   unsigned HOST_WIDE_INT count = 0;
20550   HOST_WIDE_INT expected_size = -1;
20551   int size_needed = 0, epilogue_size_needed;
20552   int desired_align = 0, align_bytes = 0;
20553   enum stringop_alg alg;
20554   int dynamic_check;
20555   bool need_zero_guard = false;
20556
20557   if (CONST_INT_P (align_exp))
20558     align = INTVAL (align_exp);
20559   /* i386 can do misaligned access on reasonably increased cost.  */
20560   if (CONST_INT_P (expected_align_exp)
20561       && INTVAL (expected_align_exp) > align)
20562     align = INTVAL (expected_align_exp);
20563   /* ALIGN is the minimum of destination and source alignment, but we care here
20564      just about destination alignment.  */
20565   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
20566     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
20567
20568   if (CONST_INT_P (count_exp))
20569     count = expected_size = INTVAL (count_exp);
20570   if (CONST_INT_P (expected_size_exp) && count == 0)
20571     expected_size = INTVAL (expected_size_exp);
20572
20573   /* Make sure we don't need to care about overflow later on.  */
20574   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
20575     return false;
20576
20577   /* Step 0: Decide on preferred algorithm, desired alignment and
20578      size of chunks to be copied by main loop.  */
20579
20580   alg = decide_alg (count, expected_size, false, &dynamic_check);
20581   desired_align = decide_alignment (align, alg, expected_size);
20582
20583   if (!TARGET_ALIGN_STRINGOPS)
20584     align = desired_align;
20585
20586   if (alg == libcall)
20587     return false;
20588   gcc_assert (alg != no_stringop);
20589   if (!count)
20590     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
20591   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
20592   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
20593   switch (alg)
20594     {
20595     case libcall:
20596     case no_stringop:
20597       gcc_unreachable ();
20598     case loop:
20599       need_zero_guard = true;
20600       size_needed = GET_MODE_SIZE (Pmode);
20601       break;
20602     case unrolled_loop:
20603       need_zero_guard = true;
20604       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
20605       break;
20606     case rep_prefix_8_byte:
20607       size_needed = 8;
20608       break;
20609     case rep_prefix_4_byte:
20610       size_needed = 4;
20611       break;
20612     case rep_prefix_1_byte:
20613       size_needed = 1;
20614       break;
20615     case loop_1_byte:
20616       need_zero_guard = true;
20617       size_needed = 1;
20618       break;
20619     }
20620
20621   epilogue_size_needed = size_needed;
20622
20623   /* Step 1: Prologue guard.  */
20624
20625   /* Alignment code needs count to be in register.  */
20626   if (CONST_INT_P (count_exp) && desired_align > align)
20627     {
20628       if (INTVAL (count_exp) > desired_align
20629           && INTVAL (count_exp) > size_needed)
20630         {
20631           align_bytes
20632             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
20633           if (align_bytes <= 0)
20634             align_bytes = 0;
20635           else
20636             align_bytes = desired_align - align_bytes;
20637         }
20638       if (align_bytes == 0)
20639         count_exp = force_reg (counter_mode (count_exp), count_exp);
20640     }
20641   gcc_assert (desired_align >= 1 && align >= 1);
20642
20643   /* Ensure that alignment prologue won't copy past end of block.  */
20644   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
20645     {
20646       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
20647       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
20648          Make sure it is power of 2.  */
20649       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
20650
20651       if (count)
20652         {
20653           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
20654             {
20655               /* If main algorithm works on QImode, no epilogue is needed.
20656                  For small sizes just don't align anything.  */
20657               if (size_needed == 1)
20658                 desired_align = align;
20659               else
20660                 goto epilogue;
20661             }
20662         }
20663       else
20664         {
20665           label = gen_label_rtx ();
20666           emit_cmp_and_jump_insns (count_exp,
20667                                    GEN_INT (epilogue_size_needed),
20668                                    LTU, 0, counter_mode (count_exp), 1, label);
20669           if (expected_size == -1 || expected_size < epilogue_size_needed)
20670             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20671           else
20672             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20673         }
20674     }
20675
20676   /* Emit code to decide on runtime whether library call or inline should be
20677      used.  */
20678   if (dynamic_check != -1)
20679     {
20680       if (CONST_INT_P (count_exp))
20681         {
20682           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
20683             {
20684               emit_block_move_via_libcall (dst, src, count_exp, false);
20685               count_exp = const0_rtx;
20686               goto epilogue;
20687             }
20688         }
20689       else
20690         {
20691           rtx hot_label = gen_label_rtx ();
20692           jump_around_label = gen_label_rtx ();
20693           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
20694                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
20695           predict_jump (REG_BR_PROB_BASE * 90 / 100);
20696           emit_block_move_via_libcall (dst, src, count_exp, false);
20697           emit_jump (jump_around_label);
20698           emit_label (hot_label);
20699         }
20700     }
20701
20702   /* Step 2: Alignment prologue.  */
20703
20704   if (desired_align > align)
20705     {
20706       if (align_bytes == 0)
20707         {
20708           /* Except for the first move in epilogue, we no longer know
20709              constant offset in aliasing info.  It don't seems to worth
20710              the pain to maintain it for the first move, so throw away
20711              the info early.  */
20712           src = change_address (src, BLKmode, srcreg);
20713           dst = change_address (dst, BLKmode, destreg);
20714           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
20715                                   desired_align);
20716         }
20717       else
20718         {
20719           /* If we know how many bytes need to be stored before dst is
20720              sufficiently aligned, maintain aliasing info accurately.  */
20721           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
20722                                                  desired_align, align_bytes);
20723           count_exp = plus_constant (count_exp, -align_bytes);
20724           count -= align_bytes;
20725         }
20726       if (need_zero_guard
20727           && (count < (unsigned HOST_WIDE_INT) size_needed
20728               || (align_bytes == 0
20729                   && count < ((unsigned HOST_WIDE_INT) size_needed
20730                               + desired_align - align))))
20731         {
20732           /* It is possible that we copied enough so the main loop will not
20733              execute.  */
20734           gcc_assert (size_needed > 1);
20735           if (label == NULL_RTX)
20736             label = gen_label_rtx ();
20737           emit_cmp_and_jump_insns (count_exp,
20738                                    GEN_INT (size_needed),
20739                                    LTU, 0, counter_mode (count_exp), 1, label);
20740           if (expected_size == -1
20741               || expected_size < (desired_align - align) / 2 + size_needed)
20742             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20743           else
20744             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20745         }
20746     }
20747   if (label && size_needed == 1)
20748     {
20749       emit_label (label);
20750       LABEL_NUSES (label) = 1;
20751       label = NULL;
20752       epilogue_size_needed = 1;
20753     }
20754   else if (label == NULL_RTX)
20755     epilogue_size_needed = size_needed;
20756
20757   /* Step 3: Main loop.  */
20758
20759   switch (alg)
20760     {
20761     case libcall:
20762     case no_stringop:
20763       gcc_unreachable ();
20764     case loop_1_byte:
20765       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20766                                      count_exp, QImode, 1, expected_size);
20767       break;
20768     case loop:
20769       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20770                                      count_exp, Pmode, 1, expected_size);
20771       break;
20772     case unrolled_loop:
20773       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
20774          registers for 4 temporaries anyway.  */
20775       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20776                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
20777                                      expected_size);
20778       break;
20779     case rep_prefix_8_byte:
20780       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
20781                                  DImode);
20782       break;
20783     case rep_prefix_4_byte:
20784       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
20785                                  SImode);
20786       break;
20787     case rep_prefix_1_byte:
20788       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
20789                                  QImode);
20790       break;
20791     }
20792   /* Adjust properly the offset of src and dest memory for aliasing.  */
20793   if (CONST_INT_P (count_exp))
20794     {
20795       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
20796                                           (count / size_needed) * size_needed);
20797       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
20798                                           (count / size_needed) * size_needed);
20799     }
20800   else
20801     {
20802       src = change_address (src, BLKmode, srcreg);
20803       dst = change_address (dst, BLKmode, destreg);
20804     }
20805
20806   /* Step 4: Epilogue to copy the remaining bytes.  */
20807  epilogue:
20808   if (label)
20809     {
20810       /* When the main loop is done, COUNT_EXP might hold original count,
20811          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
20812          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
20813          bytes. Compensate if needed.  */
20814
20815       if (size_needed < epilogue_size_needed)
20816         {
20817           tmp =
20818             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
20819                                  GEN_INT (size_needed - 1), count_exp, 1,
20820                                  OPTAB_DIRECT);
20821           if (tmp != count_exp)
20822             emit_move_insn (count_exp, tmp);
20823         }
20824       emit_label (label);
20825       LABEL_NUSES (label) = 1;
20826     }
20827
20828   if (count_exp != const0_rtx && epilogue_size_needed > 1)
20829     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
20830                             epilogue_size_needed);
20831   if (jump_around_label)
20832     emit_label (jump_around_label);
20833   return true;
20834 }
20835
20836 /* Helper function for memcpy.  For QImode value 0xXY produce
20837    0xXYXYXYXY of wide specified by MODE.  This is essentially
20838    a * 0x10101010, but we can do slightly better than
20839    synth_mult by unwinding the sequence by hand on CPUs with
20840    slow multiply.  */
20841 static rtx
20842 promote_duplicated_reg (enum machine_mode mode, rtx val)
20843 {
20844   enum machine_mode valmode = GET_MODE (val);
20845   rtx tmp;
20846   int nops = mode == DImode ? 3 : 2;
20847
20848   gcc_assert (mode == SImode || mode == DImode);
20849   if (val == const0_rtx)
20850     return copy_to_mode_reg (mode, const0_rtx);
20851   if (CONST_INT_P (val))
20852     {
20853       HOST_WIDE_INT v = INTVAL (val) & 255;
20854
20855       v |= v << 8;
20856       v |= v << 16;
20857       if (mode == DImode)
20858         v |= (v << 16) << 16;
20859       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
20860     }
20861
20862   if (valmode == VOIDmode)
20863     valmode = QImode;
20864   if (valmode != QImode)
20865     val = gen_lowpart (QImode, val);
20866   if (mode == QImode)
20867     return val;
20868   if (!TARGET_PARTIAL_REG_STALL)
20869     nops--;
20870   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
20871       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
20872       <= (ix86_cost->shift_const + ix86_cost->add) * nops
20873           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
20874     {
20875       rtx reg = convert_modes (mode, QImode, val, true);
20876       tmp = promote_duplicated_reg (mode, const1_rtx);
20877       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
20878                                   OPTAB_DIRECT);
20879     }
20880   else
20881     {
20882       rtx reg = convert_modes (mode, QImode, val, true);
20883
20884       if (!TARGET_PARTIAL_REG_STALL)
20885         if (mode == SImode)
20886           emit_insn (gen_movsi_insv_1 (reg, reg));
20887         else
20888           emit_insn (gen_movdi_insv_1 (reg, reg));
20889       else
20890         {
20891           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
20892                                      NULL, 1, OPTAB_DIRECT);
20893           reg =
20894             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
20895         }
20896       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
20897                                  NULL, 1, OPTAB_DIRECT);
20898       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
20899       if (mode == SImode)
20900         return reg;
20901       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
20902                                  NULL, 1, OPTAB_DIRECT);
20903       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
20904       return reg;
20905     }
20906 }
20907
20908 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
20909    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
20910    alignment from ALIGN to DESIRED_ALIGN.  */
20911 static rtx
20912 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
20913 {
20914   rtx promoted_val;
20915
20916   if (TARGET_64BIT
20917       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
20918     promoted_val = promote_duplicated_reg (DImode, val);
20919   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
20920     promoted_val = promote_duplicated_reg (SImode, val);
20921   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
20922     promoted_val = promote_duplicated_reg (HImode, val);
20923   else
20924     promoted_val = val;
20925
20926   return promoted_val;
20927 }
20928
20929 /* Expand string clear operation (bzero).  Use i386 string operations when
20930    profitable.  See expand_movmem comment for explanation of individual
20931    steps performed.  */
20932 bool
20933 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
20934                     rtx expected_align_exp, rtx expected_size_exp)
20935 {
20936   rtx destreg;
20937   rtx label = NULL;
20938   rtx tmp;
20939   rtx jump_around_label = NULL;
20940   HOST_WIDE_INT align = 1;
20941   unsigned HOST_WIDE_INT count = 0;
20942   HOST_WIDE_INT expected_size = -1;
20943   int size_needed = 0, epilogue_size_needed;
20944   int desired_align = 0, align_bytes = 0;
20945   enum stringop_alg alg;
20946   rtx promoted_val = NULL;
20947   bool force_loopy_epilogue = false;
20948   int dynamic_check;
20949   bool need_zero_guard = false;
20950
20951   if (CONST_INT_P (align_exp))
20952     align = INTVAL (align_exp);
20953   /* i386 can do misaligned access on reasonably increased cost.  */
20954   if (CONST_INT_P (expected_align_exp)
20955       && INTVAL (expected_align_exp) > align)
20956     align = INTVAL (expected_align_exp);
20957   if (CONST_INT_P (count_exp))
20958     count = expected_size = INTVAL (count_exp);
20959   if (CONST_INT_P (expected_size_exp) && count == 0)
20960     expected_size = INTVAL (expected_size_exp);
20961
20962   /* Make sure we don't need to care about overflow later on.  */
20963   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
20964     return false;
20965
20966   /* Step 0: Decide on preferred algorithm, desired alignment and
20967      size of chunks to be copied by main loop.  */
20968
20969   alg = decide_alg (count, expected_size, true, &dynamic_check);
20970   desired_align = decide_alignment (align, alg, expected_size);
20971
20972   if (!TARGET_ALIGN_STRINGOPS)
20973     align = desired_align;
20974
20975   if (alg == libcall)
20976     return false;
20977   gcc_assert (alg != no_stringop);
20978   if (!count)
20979     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
20980   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
20981   switch (alg)
20982     {
20983     case libcall:
20984     case no_stringop:
20985       gcc_unreachable ();
20986     case loop:
20987       need_zero_guard = true;
20988       size_needed = GET_MODE_SIZE (Pmode);
20989       break;
20990     case unrolled_loop:
20991       need_zero_guard = true;
20992       size_needed = GET_MODE_SIZE (Pmode) * 4;
20993       break;
20994     case rep_prefix_8_byte:
20995       size_needed = 8;
20996       break;
20997     case rep_prefix_4_byte:
20998       size_needed = 4;
20999       break;
21000     case rep_prefix_1_byte:
21001       size_needed = 1;
21002       break;
21003     case loop_1_byte:
21004       need_zero_guard = true;
21005       size_needed = 1;
21006       break;
21007     }
21008   epilogue_size_needed = size_needed;
21009
21010   /* Step 1: Prologue guard.  */
21011
21012   /* Alignment code needs count to be in register.  */
21013   if (CONST_INT_P (count_exp) && desired_align > align)
21014     {
21015       if (INTVAL (count_exp) > desired_align
21016           && INTVAL (count_exp) > size_needed)
21017         {
21018           align_bytes
21019             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
21020           if (align_bytes <= 0)
21021             align_bytes = 0;
21022           else
21023             align_bytes = desired_align - align_bytes;
21024         }
21025       if (align_bytes == 0)
21026         {
21027           enum machine_mode mode = SImode;
21028           if (TARGET_64BIT && (count & ~0xffffffff))
21029             mode = DImode;
21030           count_exp = force_reg (mode, count_exp);
21031         }
21032     }
21033   /* Do the cheap promotion to allow better CSE across the
21034      main loop and epilogue (ie one load of the big constant in the
21035      front of all code.  */
21036   if (CONST_INT_P (val_exp))
21037     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
21038                                                    desired_align, align);
21039   /* Ensure that alignment prologue won't copy past end of block.  */
21040   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
21041     {
21042       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
21043       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
21044          Make sure it is power of 2.  */
21045       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
21046
21047       /* To improve performance of small blocks, we jump around the VAL
21048          promoting mode.  This mean that if the promoted VAL is not constant,
21049          we might not use it in the epilogue and have to use byte
21050          loop variant.  */
21051       if (epilogue_size_needed > 2 && !promoted_val)
21052         force_loopy_epilogue = true;
21053       if (count)
21054         {
21055           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
21056             {
21057               /* If main algorithm works on QImode, no epilogue is needed.
21058                  For small sizes just don't align anything.  */
21059               if (size_needed == 1)
21060                 desired_align = align;
21061               else
21062                 goto epilogue;
21063             }
21064         }
21065       else
21066         {
21067           label = gen_label_rtx ();
21068           emit_cmp_and_jump_insns (count_exp,
21069                                    GEN_INT (epilogue_size_needed),
21070                                    LTU, 0, counter_mode (count_exp), 1, label);
21071           if (expected_size == -1 || expected_size <= epilogue_size_needed)
21072             predict_jump (REG_BR_PROB_BASE * 60 / 100);
21073           else
21074             predict_jump (REG_BR_PROB_BASE * 20 / 100);
21075         }
21076     }
21077   if (dynamic_check != -1)
21078     {
21079       rtx hot_label = gen_label_rtx ();
21080       jump_around_label = gen_label_rtx ();
21081       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
21082                                LEU, 0, counter_mode (count_exp), 1, hot_label);
21083       predict_jump (REG_BR_PROB_BASE * 90 / 100);
21084       set_storage_via_libcall (dst, count_exp, val_exp, false);
21085       emit_jump (jump_around_label);
21086       emit_label (hot_label);
21087     }
21088
21089   /* Step 2: Alignment prologue.  */
21090
21091   /* Do the expensive promotion once we branched off the small blocks.  */
21092   if (!promoted_val)
21093     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
21094                                                    desired_align, align);
21095   gcc_assert (desired_align >= 1 && align >= 1);
21096
21097   if (desired_align > align)
21098     {
21099       if (align_bytes == 0)
21100         {
21101           /* Except for the first move in epilogue, we no longer know
21102              constant offset in aliasing info.  It don't seems to worth
21103              the pain to maintain it for the first move, so throw away
21104              the info early.  */
21105           dst = change_address (dst, BLKmode, destreg);
21106           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
21107                                   desired_align);
21108         }
21109       else
21110         {
21111           /* If we know how many bytes need to be stored before dst is
21112              sufficiently aligned, maintain aliasing info accurately.  */
21113           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
21114                                                  desired_align, align_bytes);
21115           count_exp = plus_constant (count_exp, -align_bytes);
21116           count -= align_bytes;
21117         }
21118       if (need_zero_guard
21119           && (count < (unsigned HOST_WIDE_INT) size_needed
21120               || (align_bytes == 0
21121                   && count < ((unsigned HOST_WIDE_INT) size_needed
21122                               + desired_align - align))))
21123         {
21124           /* It is possible that we copied enough so the main loop will not
21125              execute.  */
21126           gcc_assert (size_needed > 1);
21127           if (label == NULL_RTX)
21128             label = gen_label_rtx ();
21129           emit_cmp_and_jump_insns (count_exp,
21130                                    GEN_INT (size_needed),
21131                                    LTU, 0, counter_mode (count_exp), 1, label);
21132           if (expected_size == -1
21133               || expected_size < (desired_align - align) / 2 + size_needed)
21134             predict_jump (REG_BR_PROB_BASE * 20 / 100);
21135           else
21136             predict_jump (REG_BR_PROB_BASE * 60 / 100);
21137         }
21138     }
21139   if (label && size_needed == 1)
21140     {
21141       emit_label (label);
21142       LABEL_NUSES (label) = 1;
21143       label = NULL;
21144       promoted_val = val_exp;
21145       epilogue_size_needed = 1;
21146     }
21147   else if (label == NULL_RTX)
21148     epilogue_size_needed = size_needed;
21149
21150   /* Step 3: Main loop.  */
21151
21152   switch (alg)
21153     {
21154     case libcall:
21155     case no_stringop:
21156       gcc_unreachable ();
21157     case loop_1_byte:
21158       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
21159                                      count_exp, QImode, 1, expected_size);
21160       break;
21161     case loop:
21162       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
21163                                      count_exp, Pmode, 1, expected_size);
21164       break;
21165     case unrolled_loop:
21166       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
21167                                      count_exp, Pmode, 4, expected_size);
21168       break;
21169     case rep_prefix_8_byte:
21170       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
21171                                   DImode, val_exp);
21172       break;
21173     case rep_prefix_4_byte:
21174       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
21175                                   SImode, val_exp);
21176       break;
21177     case rep_prefix_1_byte:
21178       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
21179                                   QImode, val_exp);
21180       break;
21181     }
21182   /* Adjust properly the offset of src and dest memory for aliasing.  */
21183   if (CONST_INT_P (count_exp))
21184     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
21185                                         (count / size_needed) * size_needed);
21186   else
21187     dst = change_address (dst, BLKmode, destreg);
21188
21189   /* Step 4: Epilogue to copy the remaining bytes.  */
21190
21191   if (label)
21192     {
21193       /* When the main loop is done, COUNT_EXP might hold original count,
21194          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
21195          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
21196          bytes. Compensate if needed.  */
21197
21198       if (size_needed < epilogue_size_needed)
21199         {
21200           tmp =
21201             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
21202                                  GEN_INT (size_needed - 1), count_exp, 1,
21203                                  OPTAB_DIRECT);
21204           if (tmp != count_exp)
21205             emit_move_insn (count_exp, tmp);
21206         }
21207       emit_label (label);
21208       LABEL_NUSES (label) = 1;
21209     }
21210  epilogue:
21211   if (count_exp != const0_rtx && epilogue_size_needed > 1)
21212     {
21213       if (force_loopy_epilogue)
21214         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
21215                                          epilogue_size_needed);
21216       else
21217         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
21218                                 epilogue_size_needed);
21219     }
21220   if (jump_around_label)
21221     emit_label (jump_around_label);
21222   return true;
21223 }
21224
21225 /* Expand the appropriate insns for doing strlen if not just doing
21226    repnz; scasb
21227
21228    out = result, initialized with the start address
21229    align_rtx = alignment of the address.
21230    scratch = scratch register, initialized with the startaddress when
21231         not aligned, otherwise undefined
21232
21233    This is just the body. It needs the initializations mentioned above and
21234    some address computing at the end.  These things are done in i386.md.  */
21235
21236 static void
21237 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
21238 {
21239   int align;
21240   rtx tmp;
21241   rtx align_2_label = NULL_RTX;
21242   rtx align_3_label = NULL_RTX;
21243   rtx align_4_label = gen_label_rtx ();
21244   rtx end_0_label = gen_label_rtx ();
21245   rtx mem;
21246   rtx tmpreg = gen_reg_rtx (SImode);
21247   rtx scratch = gen_reg_rtx (SImode);
21248   rtx cmp;
21249
21250   align = 0;
21251   if (CONST_INT_P (align_rtx))
21252     align = INTVAL (align_rtx);
21253
21254   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
21255
21256   /* Is there a known alignment and is it less than 4?  */
21257   if (align < 4)
21258     {
21259       rtx scratch1 = gen_reg_rtx (Pmode);
21260       emit_move_insn (scratch1, out);
21261       /* Is there a known alignment and is it not 2? */
21262       if (align != 2)
21263         {
21264           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
21265           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
21266
21267           /* Leave just the 3 lower bits.  */
21268           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
21269                                     NULL_RTX, 0, OPTAB_WIDEN);
21270
21271           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
21272                                    Pmode, 1, align_4_label);
21273           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
21274                                    Pmode, 1, align_2_label);
21275           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
21276                                    Pmode, 1, align_3_label);
21277         }
21278       else
21279         {
21280           /* Since the alignment is 2, we have to check 2 or 0 bytes;
21281              check if is aligned to 4 - byte.  */
21282
21283           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
21284                                     NULL_RTX, 0, OPTAB_WIDEN);
21285
21286           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
21287                                    Pmode, 1, align_4_label);
21288         }
21289
21290       mem = change_address (src, QImode, out);
21291
21292       /* Now compare the bytes.  */
21293
21294       /* Compare the first n unaligned byte on a byte per byte basis.  */
21295       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
21296                                QImode, 1, end_0_label);
21297
21298       /* Increment the address.  */
21299       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
21300
21301       /* Not needed with an alignment of 2 */
21302       if (align != 2)
21303         {
21304           emit_label (align_2_label);
21305
21306           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
21307                                    end_0_label);
21308
21309           emit_insn (ix86_gen_add3 (out, out, const1_rtx));
21310
21311           emit_label (align_3_label);
21312         }
21313
21314       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
21315                                end_0_label);
21316
21317       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
21318     }
21319
21320   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
21321      align this loop.  It gives only huge programs, but does not help to
21322      speed up.  */
21323   emit_label (align_4_label);
21324
21325   mem = change_address (src, SImode, out);
21326   emit_move_insn (scratch, mem);
21327   emit_insn (ix86_gen_add3 (out, out, GEN_INT (4)));
21328
21329   /* This formula yields a nonzero result iff one of the bytes is zero.
21330      This saves three branches inside loop and many cycles.  */
21331
21332   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
21333   emit_insn (gen_one_cmplsi2 (scratch, scratch));
21334   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
21335   emit_insn (gen_andsi3 (tmpreg, tmpreg,
21336                          gen_int_mode (0x80808080, SImode)));
21337   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
21338                            align_4_label);
21339
21340   if (TARGET_CMOVE)
21341     {
21342        rtx reg = gen_reg_rtx (SImode);
21343        rtx reg2 = gen_reg_rtx (Pmode);
21344        emit_move_insn (reg, tmpreg);
21345        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
21346
21347        /* If zero is not in the first two bytes, move two bytes forward.  */
21348        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
21349        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21350        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
21351        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
21352                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
21353                                                      reg,
21354                                                      tmpreg)));
21355        /* Emit lea manually to avoid clobbering of flags.  */
21356        emit_insn (gen_rtx_SET (SImode, reg2,
21357                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
21358
21359        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21360        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
21361        emit_insn (gen_rtx_SET (VOIDmode, out,
21362                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
21363                                                      reg2,
21364                                                      out)));
21365     }
21366   else
21367     {
21368        rtx end_2_label = gen_label_rtx ();
21369        /* Is zero in the first two bytes? */
21370
21371        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
21372        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21373        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
21374        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
21375                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
21376                             pc_rtx);
21377        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
21378        JUMP_LABEL (tmp) = end_2_label;
21379
21380        /* Not in the first two.  Move two bytes forward.  */
21381        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
21382        emit_insn (ix86_gen_add3 (out, out, const2_rtx));
21383
21384        emit_label (end_2_label);
21385
21386     }
21387
21388   /* Avoid branch in fixing the byte.  */
21389   tmpreg = gen_lowpart (QImode, tmpreg);
21390   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
21391   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
21392   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
21393   emit_insn (ix86_gen_sub3_carry (out, out, GEN_INT (3), tmp, cmp));
21394
21395   emit_label (end_0_label);
21396 }
21397
21398 /* Expand strlen.  */
21399
21400 bool
21401 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
21402 {
21403   rtx addr, scratch1, scratch2, scratch3, scratch4;
21404
21405   /* The generic case of strlen expander is long.  Avoid it's
21406      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
21407
21408   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
21409       && !TARGET_INLINE_ALL_STRINGOPS
21410       && !optimize_insn_for_size_p ()
21411       && (!CONST_INT_P (align) || INTVAL (align) < 4))
21412     return false;
21413
21414   addr = force_reg (Pmode, XEXP (src, 0));
21415   scratch1 = gen_reg_rtx (Pmode);
21416
21417   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
21418       && !optimize_insn_for_size_p ())
21419     {
21420       /* Well it seems that some optimizer does not combine a call like
21421          foo(strlen(bar), strlen(bar));
21422          when the move and the subtraction is done here.  It does calculate
21423          the length just once when these instructions are done inside of
21424          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
21425          often used and I use one fewer register for the lifetime of
21426          output_strlen_unroll() this is better.  */
21427
21428       emit_move_insn (out, addr);
21429
21430       ix86_expand_strlensi_unroll_1 (out, src, align);
21431
21432       /* strlensi_unroll_1 returns the address of the zero at the end of
21433          the string, like memchr(), so compute the length by subtracting
21434          the start address.  */
21435       emit_insn (ix86_gen_sub3 (out, out, addr));
21436     }
21437   else
21438     {
21439       rtx unspec;
21440
21441       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
21442       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
21443         return false;
21444
21445       scratch2 = gen_reg_rtx (Pmode);
21446       scratch3 = gen_reg_rtx (Pmode);
21447       scratch4 = force_reg (Pmode, constm1_rtx);
21448
21449       emit_move_insn (scratch3, addr);
21450       eoschar = force_reg (QImode, eoschar);
21451
21452       src = replace_equiv_address_nv (src, scratch3);
21453
21454       /* If .md starts supporting :P, this can be done in .md.  */
21455       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
21456                                                  scratch4), UNSPEC_SCAS);
21457       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
21458       emit_insn (ix86_gen_one_cmpl2 (scratch2, scratch1));
21459       emit_insn (ix86_gen_add3 (out, scratch2, constm1_rtx));
21460     }
21461   return true;
21462 }
21463
21464 /* For given symbol (function) construct code to compute address of it's PLT
21465    entry in large x86-64 PIC model.  */
21466 rtx
21467 construct_plt_address (rtx symbol)
21468 {
21469   rtx tmp = gen_reg_rtx (Pmode);
21470   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
21471
21472   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
21473   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
21474
21475   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
21476   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
21477   return tmp;
21478 }
21479
21480 rtx
21481 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
21482                   rtx callarg2,
21483                   rtx pop, int sibcall)
21484 {
21485   rtx use = NULL, call;
21486
21487   if (pop == const0_rtx)
21488     pop = NULL;
21489   gcc_assert (!TARGET_64BIT || !pop);
21490
21491   if (TARGET_MACHO && !TARGET_64BIT)
21492     {
21493 #if TARGET_MACHO
21494       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
21495         fnaddr = machopic_indirect_call_target (fnaddr);
21496 #endif
21497     }
21498   else
21499     {
21500       /* Static functions and indirect calls don't need the pic register.  */
21501       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
21502           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
21503           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
21504         use_reg (&use, pic_offset_table_rtx);
21505     }
21506
21507   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
21508     {
21509       rtx al = gen_rtx_REG (QImode, AX_REG);
21510       emit_move_insn (al, callarg2);
21511       use_reg (&use, al);
21512     }
21513
21514   if (ix86_cmodel == CM_LARGE_PIC
21515       && MEM_P (fnaddr)
21516       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
21517       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
21518     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
21519   else if (sibcall
21520            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
21521            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
21522     {
21523       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
21524       fnaddr = gen_rtx_MEM (QImode, fnaddr);
21525     }
21526
21527   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
21528   if (retval)
21529     call = gen_rtx_SET (VOIDmode, retval, call);
21530   if (pop)
21531     {
21532       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
21533       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
21534       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
21535     }
21536   if (TARGET_64BIT
21537       && ix86_cfun_abi () == MS_ABI
21538       && (!callarg2 || INTVAL (callarg2) != -2))
21539     {
21540       /* We need to represent that SI and DI registers are clobbered
21541          by SYSV calls.  */
21542       static int clobbered_registers[] = {
21543         XMM6_REG, XMM7_REG, XMM8_REG,
21544         XMM9_REG, XMM10_REG, XMM11_REG,
21545         XMM12_REG, XMM13_REG, XMM14_REG,
21546         XMM15_REG, SI_REG, DI_REG
21547       };
21548       unsigned int i;
21549       rtx vec[ARRAY_SIZE (clobbered_registers) + 2];
21550       rtx unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
21551                                    UNSPEC_MS_TO_SYSV_CALL);
21552
21553       vec[0] = call;
21554       vec[1] = unspec;
21555       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
21556         vec[i + 2] = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
21557                                       ? TImode : DImode,
21558                                       gen_rtx_REG
21559                                         (SSE_REGNO_P (clobbered_registers[i])
21560                                                       ? TImode : DImode,
21561                                          clobbered_registers[i]));
21562
21563       call = gen_rtx_PARALLEL (VOIDmode,
21564                                gen_rtvec_v (ARRAY_SIZE (clobbered_registers)
21565                                + 2, vec));
21566     }
21567
21568   /* Emit vzeroupper if needed.  */
21569   if (TARGET_VZEROUPPER && cfun->machine->use_avx256_p)
21570     {
21571       int avx256;
21572       cfun->machine->use_vzeroupper_p = 1;
21573       if (cfun->machine->callee_pass_avx256_p)
21574         {
21575           if (cfun->machine->callee_return_avx256_p)
21576             avx256 = callee_return_pass_avx256;
21577           else
21578             avx256 = callee_pass_avx256;
21579         }
21580       else if (cfun->machine->callee_return_avx256_p)
21581         avx256 = callee_return_avx256;
21582       else
21583         avx256 = call_no_avx256;
21584       emit_insn (gen_avx_vzeroupper (GEN_INT (avx256))); 
21585     }
21586
21587   call = emit_call_insn (call);
21588   if (use)
21589     CALL_INSN_FUNCTION_USAGE (call) = use;
21590
21591   return call;
21592 }
21593
21594 /* Output the assembly for a call instruction.  */
21595
21596 const char *
21597 ix86_output_call_insn (rtx insn, rtx call_op, int addr_op)
21598 {
21599   bool direct_p = constant_call_address_operand (call_op, Pmode);
21600   bool seh_nop_p = false;
21601
21602   gcc_assert (addr_op == 0 || addr_op == 1);
21603
21604   if (SIBLING_CALL_P (insn))
21605     {
21606       if (direct_p)
21607         return addr_op ? "jmp\t%P1" : "jmp\t%P0";
21608       /* SEH epilogue detection requires the indirect branch case
21609          to include REX.W.  */
21610       else if (TARGET_SEH)
21611         return addr_op ? "rex.W jmp %A1" : "rex.W jmp %A0";
21612       else
21613         return addr_op ? "jmp\t%A1" : "jmp\t%A0";
21614     }
21615
21616   /* SEH unwinding can require an extra nop to be emitted in several
21617      circumstances.  Determine if we have one of those.  */
21618   if (TARGET_SEH)
21619     {
21620       rtx i;
21621
21622       for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
21623         {
21624           /* If we get to another real insn, we don't need the nop.  */
21625           if (INSN_P (i))
21626             break;
21627
21628           /* If we get to the epilogue note, prevent a catch region from
21629              being adjacent to the standard epilogue sequence.  If non-
21630              call-exceptions, we'll have done this during epilogue emission. */
21631           if (NOTE_P (i) && NOTE_KIND (i) == NOTE_INSN_EPILOGUE_BEG
21632               && !flag_non_call_exceptions
21633               && !can_throw_internal (insn))
21634             {
21635               seh_nop_p = true;
21636               break;
21637             }
21638         }
21639
21640       /* If we didn't find a real insn following the call, prevent the
21641          unwinder from looking into the next function.  */
21642       if (i == NULL)
21643         seh_nop_p = true;
21644     }
21645
21646   if (direct_p)
21647     {
21648       if (seh_nop_p)
21649         return addr_op ? "call\t%P1\n\tnop" : "call\t%P0\n\tnop";
21650       else
21651         return addr_op ? "call\t%P1" : "call\t%P0";
21652     }
21653   else
21654     {
21655       if (seh_nop_p)
21656         return addr_op ? "call\t%A1\n\tnop" : "call\t%A0\n\tnop";
21657       else
21658         return addr_op ? "call\t%A1" : "call\t%A0";
21659     }
21660 }
21661 \f
21662 /* Clear stack slot assignments remembered from previous functions.
21663    This is called from INIT_EXPANDERS once before RTL is emitted for each
21664    function.  */
21665
21666 static struct machine_function *
21667 ix86_init_machine_status (void)
21668 {
21669   struct machine_function *f;
21670
21671   f = ggc_alloc_cleared_machine_function ();
21672   f->use_fast_prologue_epilogue_nregs = -1;
21673   f->tls_descriptor_call_expanded_p = 0;
21674   f->call_abi = ix86_abi;
21675
21676   return f;
21677 }
21678
21679 /* Return a MEM corresponding to a stack slot with mode MODE.
21680    Allocate a new slot if necessary.
21681
21682    The RTL for a function can have several slots available: N is
21683    which slot to use.  */
21684
21685 rtx
21686 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
21687 {
21688   struct stack_local_entry *s;
21689
21690   gcc_assert (n < MAX_386_STACK_LOCALS);
21691
21692   /* Virtual slot is valid only before vregs are instantiated.  */
21693   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
21694
21695   for (s = ix86_stack_locals; s; s = s->next)
21696     if (s->mode == mode && s->n == n)
21697       return copy_rtx (s->rtl);
21698
21699   s = ggc_alloc_stack_local_entry ();
21700   s->n = n;
21701   s->mode = mode;
21702   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
21703
21704   s->next = ix86_stack_locals;
21705   ix86_stack_locals = s;
21706   return s->rtl;
21707 }
21708
21709 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
21710
21711 static GTY(()) rtx ix86_tls_symbol;
21712 rtx
21713 ix86_tls_get_addr (void)
21714 {
21715
21716   if (!ix86_tls_symbol)
21717     {
21718       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
21719                                             (TARGET_ANY_GNU_TLS
21720                                              && !TARGET_64BIT)
21721                                             ? "___tls_get_addr"
21722                                             : "__tls_get_addr");
21723     }
21724
21725   return ix86_tls_symbol;
21726 }
21727
21728 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
21729
21730 static GTY(()) rtx ix86_tls_module_base_symbol;
21731 rtx
21732 ix86_tls_module_base (void)
21733 {
21734
21735   if (!ix86_tls_module_base_symbol)
21736     {
21737       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
21738                                                         "_TLS_MODULE_BASE_");
21739       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
21740         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
21741     }
21742
21743   return ix86_tls_module_base_symbol;
21744 }
21745 \f
21746 /* Calculate the length of the memory address in the instruction
21747    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
21748
21749 int
21750 memory_address_length (rtx addr)
21751 {
21752   struct ix86_address parts;
21753   rtx base, index, disp;
21754   int len;
21755   int ok;
21756
21757   if (GET_CODE (addr) == PRE_DEC
21758       || GET_CODE (addr) == POST_INC
21759       || GET_CODE (addr) == PRE_MODIFY
21760       || GET_CODE (addr) == POST_MODIFY)
21761     return 0;
21762
21763   ok = ix86_decompose_address (addr, &parts);
21764   gcc_assert (ok);
21765
21766   if (parts.base && GET_CODE (parts.base) == SUBREG)
21767     parts.base = SUBREG_REG (parts.base);
21768   if (parts.index && GET_CODE (parts.index) == SUBREG)
21769     parts.index = SUBREG_REG (parts.index);
21770
21771   base = parts.base;
21772   index = parts.index;
21773   disp = parts.disp;
21774   len = 0;
21775
21776   /* Rule of thumb:
21777        - esp as the base always wants an index,
21778        - ebp as the base always wants a displacement,
21779        - r12 as the base always wants an index,
21780        - r13 as the base always wants a displacement.  */
21781
21782   /* Register Indirect.  */
21783   if (base && !index && !disp)
21784     {
21785       /* esp (for its index) and ebp (for its displacement) need
21786          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
21787          code.  */
21788       if (REG_P (addr)
21789           && (addr == arg_pointer_rtx
21790               || addr == frame_pointer_rtx
21791               || REGNO (addr) == SP_REG
21792               || REGNO (addr) == BP_REG
21793               || REGNO (addr) == R12_REG
21794               || REGNO (addr) == R13_REG))
21795         len = 1;
21796     }
21797
21798   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
21799      is not disp32, but disp32(%rip), so for disp32
21800      SIB byte is needed, unless print_operand_address
21801      optimizes it into disp32(%rip) or (%rip) is implied
21802      by UNSPEC.  */
21803   else if (disp && !base && !index)
21804     {
21805       len = 4;
21806       if (TARGET_64BIT)
21807         {
21808           rtx symbol = disp;
21809
21810           if (GET_CODE (disp) == CONST)
21811             symbol = XEXP (disp, 0);
21812           if (GET_CODE (symbol) == PLUS
21813               && CONST_INT_P (XEXP (symbol, 1)))
21814             symbol = XEXP (symbol, 0);
21815
21816           if (GET_CODE (symbol) != LABEL_REF
21817               && (GET_CODE (symbol) != SYMBOL_REF
21818                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
21819               && (GET_CODE (symbol) != UNSPEC
21820                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
21821                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
21822             len += 1;
21823         }
21824     }
21825
21826   else
21827     {
21828       /* Find the length of the displacement constant.  */
21829       if (disp)
21830         {
21831           if (base && satisfies_constraint_K (disp))
21832             len = 1;
21833           else
21834             len = 4;
21835         }
21836       /* ebp always wants a displacement.  Similarly r13.  */
21837       else if (base && REG_P (base)
21838                && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
21839         len = 1;
21840
21841       /* An index requires the two-byte modrm form....  */
21842       if (index
21843           /* ...like esp (or r12), which always wants an index.  */
21844           || base == arg_pointer_rtx
21845           || base == frame_pointer_rtx
21846           || (base && REG_P (base)
21847               && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
21848         len += 1;
21849     }
21850
21851   switch (parts.seg)
21852     {
21853     case SEG_FS:
21854     case SEG_GS:
21855       len += 1;
21856       break;
21857     default:
21858       break;
21859     }
21860
21861   return len;
21862 }
21863
21864 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
21865    is set, expect that insn have 8bit immediate alternative.  */
21866 int
21867 ix86_attr_length_immediate_default (rtx insn, int shortform)
21868 {
21869   int len = 0;
21870   int i;
21871   extract_insn_cached (insn);
21872   for (i = recog_data.n_operands - 1; i >= 0; --i)
21873     if (CONSTANT_P (recog_data.operand[i]))
21874       {
21875         enum attr_mode mode = get_attr_mode (insn);
21876
21877         gcc_assert (!len);
21878         if (shortform && CONST_INT_P (recog_data.operand[i]))
21879           {
21880             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
21881             switch (mode)
21882               {
21883               case MODE_QI:
21884                 len = 1;
21885                 continue;
21886               case MODE_HI:
21887                 ival = trunc_int_for_mode (ival, HImode);
21888                 break;
21889               case MODE_SI:
21890                 ival = trunc_int_for_mode (ival, SImode);
21891                 break;
21892               default:
21893                 break;
21894               }
21895             if (IN_RANGE (ival, -128, 127))
21896               {
21897                 len = 1;
21898                 continue;
21899               }
21900           }
21901         switch (mode)
21902           {
21903           case MODE_QI:
21904             len = 1;
21905             break;
21906           case MODE_HI:
21907             len = 2;
21908             break;
21909           case MODE_SI:
21910             len = 4;
21911             break;
21912           /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
21913           case MODE_DI:
21914             len = 4;
21915             break;
21916           default:
21917             fatal_insn ("unknown insn mode", insn);
21918         }
21919       }
21920   return len;
21921 }
21922 /* Compute default value for "length_address" attribute.  */
21923 int
21924 ix86_attr_length_address_default (rtx insn)
21925 {
21926   int i;
21927
21928   if (get_attr_type (insn) == TYPE_LEA)
21929     {
21930       rtx set = PATTERN (insn), addr;
21931
21932       if (GET_CODE (set) == PARALLEL)
21933         set = XVECEXP (set, 0, 0);
21934
21935       gcc_assert (GET_CODE (set) == SET);
21936
21937       addr = SET_SRC (set);
21938       if (TARGET_64BIT && get_attr_mode (insn) == MODE_SI)
21939         {
21940           if (GET_CODE (addr) == ZERO_EXTEND)
21941             addr = XEXP (addr, 0);
21942           if (GET_CODE (addr) == SUBREG)
21943             addr = SUBREG_REG (addr);
21944         }
21945
21946       return memory_address_length (addr);
21947     }
21948
21949   extract_insn_cached (insn);
21950   for (i = recog_data.n_operands - 1; i >= 0; --i)
21951     if (MEM_P (recog_data.operand[i]))
21952       {
21953         constrain_operands_cached (reload_completed);
21954         if (which_alternative != -1)
21955           {
21956             const char *constraints = recog_data.constraints[i];
21957             int alt = which_alternative;
21958
21959             while (*constraints == '=' || *constraints == '+')
21960               constraints++;
21961             while (alt-- > 0)
21962               while (*constraints++ != ',')
21963                 ;
21964             /* Skip ignored operands.  */
21965             if (*constraints == 'X')
21966               continue;
21967           }
21968         return memory_address_length (XEXP (recog_data.operand[i], 0));
21969       }
21970   return 0;
21971 }
21972
21973 /* Compute default value for "length_vex" attribute. It includes
21974    2 or 3 byte VEX prefix and 1 opcode byte.  */
21975
21976 int
21977 ix86_attr_length_vex_default (rtx insn, int has_0f_opcode,
21978                               int has_vex_w)
21979 {
21980   int i;
21981
21982   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
21983      byte VEX prefix.  */
21984   if (!has_0f_opcode || has_vex_w)
21985     return 3 + 1;
21986
21987  /* We can always use 2 byte VEX prefix in 32bit.  */
21988   if (!TARGET_64BIT)
21989     return 2 + 1;
21990
21991   extract_insn_cached (insn);
21992
21993   for (i = recog_data.n_operands - 1; i >= 0; --i)
21994     if (REG_P (recog_data.operand[i]))
21995       {
21996         /* REX.W bit uses 3 byte VEX prefix.  */
21997         if (GET_MODE (recog_data.operand[i]) == DImode
21998             && GENERAL_REG_P (recog_data.operand[i]))
21999           return 3 + 1;
22000       }
22001     else
22002       {
22003         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
22004         if (MEM_P (recog_data.operand[i])
22005             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
22006           return 3 + 1;
22007       }
22008
22009   return 2 + 1;
22010 }
22011 \f
22012 /* Return the maximum number of instructions a cpu can issue.  */
22013
22014 static int
22015 ix86_issue_rate (void)
22016 {
22017   switch (ix86_tune)
22018     {
22019     case PROCESSOR_PENTIUM:
22020     case PROCESSOR_ATOM:
22021     case PROCESSOR_K6:
22022       return 2;
22023
22024     case PROCESSOR_PENTIUMPRO:
22025     case PROCESSOR_PENTIUM4:
22026     case PROCESSOR_ATHLON:
22027     case PROCESSOR_K8:
22028     case PROCESSOR_AMDFAM10:
22029     case PROCESSOR_NOCONA:
22030     case PROCESSOR_GENERIC32:
22031     case PROCESSOR_GENERIC64:
22032     case PROCESSOR_BDVER1:
22033       return 3;
22034
22035     case PROCESSOR_CORE2:
22036       return 4;
22037
22038     default:
22039       return 1;
22040     }
22041 }
22042
22043 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
22044    by DEP_INSN and nothing set by DEP_INSN.  */
22045
22046 static int
22047 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
22048 {
22049   rtx set, set2;
22050
22051   /* Simplify the test for uninteresting insns.  */
22052   if (insn_type != TYPE_SETCC
22053       && insn_type != TYPE_ICMOV
22054       && insn_type != TYPE_FCMOV
22055       && insn_type != TYPE_IBR)
22056     return 0;
22057
22058   if ((set = single_set (dep_insn)) != 0)
22059     {
22060       set = SET_DEST (set);
22061       set2 = NULL_RTX;
22062     }
22063   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
22064            && XVECLEN (PATTERN (dep_insn), 0) == 2
22065            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
22066            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
22067     {
22068       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
22069       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
22070     }
22071   else
22072     return 0;
22073
22074   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
22075     return 0;
22076
22077   /* This test is true if the dependent insn reads the flags but
22078      not any other potentially set register.  */
22079   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
22080     return 0;
22081
22082   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
22083     return 0;
22084
22085   return 1;
22086 }
22087
22088 /* Return true iff USE_INSN has a memory address with operands set by
22089    SET_INSN.  */
22090
22091 bool
22092 ix86_agi_dependent (rtx set_insn, rtx use_insn)
22093 {
22094   int i;
22095   extract_insn_cached (use_insn);
22096   for (i = recog_data.n_operands - 1; i >= 0; --i)
22097     if (MEM_P (recog_data.operand[i]))
22098       {
22099         rtx addr = XEXP (recog_data.operand[i], 0);
22100         return modified_in_p (addr, set_insn) != 0;
22101       }
22102   return false;
22103 }
22104
22105 static int
22106 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
22107 {
22108   enum attr_type insn_type, dep_insn_type;
22109   enum attr_memory memory;
22110   rtx set, set2;
22111   int dep_insn_code_number;
22112
22113   /* Anti and output dependencies have zero cost on all CPUs.  */
22114   if (REG_NOTE_KIND (link) != 0)
22115     return 0;
22116
22117   dep_insn_code_number = recog_memoized (dep_insn);
22118
22119   /* If we can't recognize the insns, we can't really do anything.  */
22120   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
22121     return cost;
22122
22123   insn_type = get_attr_type (insn);
22124   dep_insn_type = get_attr_type (dep_insn);
22125
22126   switch (ix86_tune)
22127     {
22128     case PROCESSOR_PENTIUM:
22129       /* Address Generation Interlock adds a cycle of latency.  */
22130       if (insn_type == TYPE_LEA)
22131         {
22132           rtx addr = PATTERN (insn);
22133
22134           if (GET_CODE (addr) == PARALLEL)
22135             addr = XVECEXP (addr, 0, 0);
22136
22137           gcc_assert (GET_CODE (addr) == SET);
22138
22139           addr = SET_SRC (addr);
22140           if (modified_in_p (addr, dep_insn))
22141             cost += 1;
22142         }
22143       else if (ix86_agi_dependent (dep_insn, insn))
22144         cost += 1;
22145
22146       /* ??? Compares pair with jump/setcc.  */
22147       if (ix86_flags_dependent (insn, dep_insn, insn_type))
22148         cost = 0;
22149
22150       /* Floating point stores require value to be ready one cycle earlier.  */
22151       if (insn_type == TYPE_FMOV
22152           && get_attr_memory (insn) == MEMORY_STORE
22153           && !ix86_agi_dependent (dep_insn, insn))
22154         cost += 1;
22155       break;
22156
22157     case PROCESSOR_PENTIUMPRO:
22158       memory = get_attr_memory (insn);
22159
22160       /* INT->FP conversion is expensive.  */
22161       if (get_attr_fp_int_src (dep_insn))
22162         cost += 5;
22163
22164       /* There is one cycle extra latency between an FP op and a store.  */
22165       if (insn_type == TYPE_FMOV
22166           && (set = single_set (dep_insn)) != NULL_RTX
22167           && (set2 = single_set (insn)) != NULL_RTX
22168           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
22169           && MEM_P (SET_DEST (set2)))
22170         cost += 1;
22171
22172       /* Show ability of reorder buffer to hide latency of load by executing
22173          in parallel with previous instruction in case
22174          previous instruction is not needed to compute the address.  */
22175       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
22176           && !ix86_agi_dependent (dep_insn, insn))
22177         {
22178           /* Claim moves to take one cycle, as core can issue one load
22179              at time and the next load can start cycle later.  */
22180           if (dep_insn_type == TYPE_IMOV
22181               || dep_insn_type == TYPE_FMOV)
22182             cost = 1;
22183           else if (cost > 1)
22184             cost--;
22185         }
22186       break;
22187
22188     case PROCESSOR_K6:
22189       memory = get_attr_memory (insn);
22190
22191       /* The esp dependency is resolved before the instruction is really
22192          finished.  */
22193       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
22194           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
22195         return 1;
22196
22197       /* INT->FP conversion is expensive.  */
22198       if (get_attr_fp_int_src (dep_insn))
22199         cost += 5;
22200
22201       /* Show ability of reorder buffer to hide latency of load by executing
22202          in parallel with previous instruction in case
22203          previous instruction is not needed to compute the address.  */
22204       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
22205           && !ix86_agi_dependent (dep_insn, insn))
22206         {
22207           /* Claim moves to take one cycle, as core can issue one load
22208              at time and the next load can start cycle later.  */
22209           if (dep_insn_type == TYPE_IMOV
22210               || dep_insn_type == TYPE_FMOV)
22211             cost = 1;
22212           else if (cost > 2)
22213             cost -= 2;
22214           else
22215             cost = 1;
22216         }
22217       break;
22218
22219     case PROCESSOR_ATHLON:
22220     case PROCESSOR_K8:
22221     case PROCESSOR_AMDFAM10:
22222     case PROCESSOR_BDVER1:
22223     case PROCESSOR_ATOM:
22224     case PROCESSOR_GENERIC32:
22225     case PROCESSOR_GENERIC64:
22226       memory = get_attr_memory (insn);
22227
22228       /* Show ability of reorder buffer to hide latency of load by executing
22229          in parallel with previous instruction in case
22230          previous instruction is not needed to compute the address.  */
22231       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
22232           && !ix86_agi_dependent (dep_insn, insn))
22233         {
22234           enum attr_unit unit = get_attr_unit (insn);
22235           int loadcost = 3;
22236
22237           /* Because of the difference between the length of integer and
22238              floating unit pipeline preparation stages, the memory operands
22239              for floating point are cheaper.
22240
22241              ??? For Athlon it the difference is most probably 2.  */
22242           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
22243             loadcost = 3;
22244           else
22245             loadcost = TARGET_ATHLON ? 2 : 0;
22246
22247           if (cost >= loadcost)
22248             cost -= loadcost;
22249           else
22250             cost = 0;
22251         }
22252
22253     default:
22254       break;
22255     }
22256
22257   return cost;
22258 }
22259
22260 /* How many alternative schedules to try.  This should be as wide as the
22261    scheduling freedom in the DFA, but no wider.  Making this value too
22262    large results extra work for the scheduler.  */
22263
22264 static int
22265 ia32_multipass_dfa_lookahead (void)
22266 {
22267   switch (ix86_tune)
22268     {
22269     case PROCESSOR_PENTIUM:
22270       return 2;
22271
22272     case PROCESSOR_PENTIUMPRO:
22273     case PROCESSOR_K6:
22274       return 1;
22275
22276     case PROCESSOR_CORE2:
22277     case PROCESSOR_COREI7_32:
22278     case PROCESSOR_COREI7_64:
22279       /* Generally, we want haifa-sched:max_issue() to look ahead as far
22280          as many instructions can be executed on a cycle, i.e.,
22281          issue_rate.  I wonder why tuning for many CPUs does not do this.  */
22282       return ix86_issue_rate ();
22283
22284     default:
22285       return 0;
22286     }
22287 }
22288
22289 \f
22290
22291 /* Model decoder of Core 2/i7.
22292    Below hooks for multipass scheduling (see haifa-sched.c:max_issue)
22293    track the instruction fetch block boundaries and make sure that long
22294    (9+ bytes) instructions are assigned to D0.  */
22295
22296 /* Maximum length of an insn that can be handled by
22297    a secondary decoder unit.  '8' for Core 2/i7.  */
22298 static int core2i7_secondary_decoder_max_insn_size;
22299
22300 /* Ifetch block size, i.e., number of bytes decoder reads per cycle.
22301    '16' for Core 2/i7.  */
22302 static int core2i7_ifetch_block_size;
22303
22304 /* Maximum number of instructions decoder can handle per cycle.
22305    '6' for Core 2/i7.  */
22306 static int core2i7_ifetch_block_max_insns;
22307
22308 typedef struct ix86_first_cycle_multipass_data_ *
22309   ix86_first_cycle_multipass_data_t;
22310 typedef const struct ix86_first_cycle_multipass_data_ *
22311   const_ix86_first_cycle_multipass_data_t;
22312
22313 /* A variable to store target state across calls to max_issue within
22314    one cycle.  */
22315 static struct ix86_first_cycle_multipass_data_ _ix86_first_cycle_multipass_data,
22316   *ix86_first_cycle_multipass_data = &_ix86_first_cycle_multipass_data;
22317
22318 /* Initialize DATA.  */
22319 static void
22320 core2i7_first_cycle_multipass_init (void *_data)
22321 {
22322   ix86_first_cycle_multipass_data_t data
22323     = (ix86_first_cycle_multipass_data_t) _data;
22324
22325   data->ifetch_block_len = 0;
22326   data->ifetch_block_n_insns = 0;
22327   data->ready_try_change = NULL;
22328   data->ready_try_change_size = 0;
22329 }
22330
22331 /* Advancing the cycle; reset ifetch block counts.  */
22332 static void
22333 core2i7_dfa_post_advance_cycle (void)
22334 {
22335   ix86_first_cycle_multipass_data_t data = ix86_first_cycle_multipass_data;
22336
22337   gcc_assert (data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
22338
22339   data->ifetch_block_len = 0;
22340   data->ifetch_block_n_insns = 0;
22341 }
22342
22343 static int min_insn_size (rtx);
22344
22345 /* Filter out insns from ready_try that the core will not be able to issue
22346    on current cycle due to decoder.  */
22347 static void
22348 core2i7_first_cycle_multipass_filter_ready_try
22349 (const_ix86_first_cycle_multipass_data_t data,
22350  char *ready_try, int n_ready, bool first_cycle_insn_p)
22351 {
22352   while (n_ready--)
22353     {
22354       rtx insn;
22355       int insn_size;
22356
22357       if (ready_try[n_ready])
22358         continue;
22359
22360       insn = get_ready_element (n_ready);
22361       insn_size = min_insn_size (insn);
22362
22363       if (/* If this is a too long an insn for a secondary decoder ...  */
22364           (!first_cycle_insn_p
22365            && insn_size > core2i7_secondary_decoder_max_insn_size)
22366           /* ... or it would not fit into the ifetch block ...  */
22367           || data->ifetch_block_len + insn_size > core2i7_ifetch_block_size
22368           /* ... or the decoder is full already ...  */
22369           || data->ifetch_block_n_insns + 1 > core2i7_ifetch_block_max_insns)
22370         /* ... mask the insn out.  */
22371         {
22372           ready_try[n_ready] = 1;
22373
22374           if (data->ready_try_change)
22375             SET_BIT (data->ready_try_change, n_ready);
22376         }
22377     }
22378 }
22379
22380 /* Prepare for a new round of multipass lookahead scheduling.  */
22381 static void
22382 core2i7_first_cycle_multipass_begin (void *_data, char *ready_try, int n_ready,
22383                                      bool first_cycle_insn_p)
22384 {
22385   ix86_first_cycle_multipass_data_t data
22386     = (ix86_first_cycle_multipass_data_t) _data;
22387   const_ix86_first_cycle_multipass_data_t prev_data
22388     = ix86_first_cycle_multipass_data;
22389
22390   /* Restore the state from the end of the previous round.  */
22391   data->ifetch_block_len = prev_data->ifetch_block_len;
22392   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns;
22393
22394   /* Filter instructions that cannot be issued on current cycle due to
22395      decoder restrictions.  */
22396   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
22397                                                   first_cycle_insn_p);
22398 }
22399
22400 /* INSN is being issued in current solution.  Account for its impact on
22401    the decoder model.  */
22402 static void
22403 core2i7_first_cycle_multipass_issue (void *_data, char *ready_try, int n_ready,
22404                                      rtx insn, const void *_prev_data)
22405 {
22406   ix86_first_cycle_multipass_data_t data
22407     = (ix86_first_cycle_multipass_data_t) _data;
22408   const_ix86_first_cycle_multipass_data_t prev_data
22409     = (const_ix86_first_cycle_multipass_data_t) _prev_data;
22410
22411   int insn_size = min_insn_size (insn);
22412
22413   data->ifetch_block_len = prev_data->ifetch_block_len + insn_size;
22414   data->ifetch_block_n_insns = prev_data->ifetch_block_n_insns + 1;
22415   gcc_assert (data->ifetch_block_len <= core2i7_ifetch_block_size
22416               && data->ifetch_block_n_insns <= core2i7_ifetch_block_max_insns);
22417
22418   /* Allocate or resize the bitmap for storing INSN's effect on ready_try.  */
22419   if (!data->ready_try_change)
22420     {
22421       data->ready_try_change = sbitmap_alloc (n_ready);
22422       data->ready_try_change_size = n_ready;
22423     }
22424   else if (data->ready_try_change_size < n_ready)
22425     {
22426       data->ready_try_change = sbitmap_resize (data->ready_try_change,
22427                                                n_ready, 0);
22428       data->ready_try_change_size = n_ready;
22429     }
22430   sbitmap_zero (data->ready_try_change);
22431
22432   /* Filter out insns from ready_try that the core will not be able to issue
22433      on current cycle due to decoder.  */
22434   core2i7_first_cycle_multipass_filter_ready_try (data, ready_try, n_ready,
22435                                                   false);
22436 }
22437
22438 /* Revert the effect on ready_try.  */
22439 static void
22440 core2i7_first_cycle_multipass_backtrack (const void *_data,
22441                                          char *ready_try,
22442                                          int n_ready ATTRIBUTE_UNUSED)
22443 {
22444   const_ix86_first_cycle_multipass_data_t data
22445     = (const_ix86_first_cycle_multipass_data_t) _data;
22446   unsigned int i = 0;
22447   sbitmap_iterator sbi;
22448
22449   gcc_assert (sbitmap_last_set_bit (data->ready_try_change) < n_ready);
22450   EXECUTE_IF_SET_IN_SBITMAP (data->ready_try_change, 0, i, sbi)
22451     {
22452       ready_try[i] = 0;
22453     }
22454 }
22455
22456 /* Save the result of multipass lookahead scheduling for the next round.  */
22457 static void
22458 core2i7_first_cycle_multipass_end (const void *_data)
22459 {
22460   const_ix86_first_cycle_multipass_data_t data
22461     = (const_ix86_first_cycle_multipass_data_t) _data;
22462   ix86_first_cycle_multipass_data_t next_data
22463     = ix86_first_cycle_multipass_data;
22464
22465   if (data != NULL)
22466     {
22467       next_data->ifetch_block_len = data->ifetch_block_len;
22468       next_data->ifetch_block_n_insns = data->ifetch_block_n_insns;
22469     }
22470 }
22471
22472 /* Deallocate target data.  */
22473 static void
22474 core2i7_first_cycle_multipass_fini (void *_data)
22475 {
22476   ix86_first_cycle_multipass_data_t data
22477     = (ix86_first_cycle_multipass_data_t) _data;
22478
22479   if (data->ready_try_change)
22480     {
22481       sbitmap_free (data->ready_try_change);
22482       data->ready_try_change = NULL;
22483       data->ready_try_change_size = 0;
22484     }
22485 }
22486
22487 /* Prepare for scheduling pass.  */
22488 static void
22489 ix86_sched_init_global (FILE *dump ATTRIBUTE_UNUSED,
22490                         int verbose ATTRIBUTE_UNUSED,
22491                         int max_uid ATTRIBUTE_UNUSED)
22492 {
22493   /* Install scheduling hooks for current CPU.  Some of these hooks are used
22494      in time-critical parts of the scheduler, so we only set them up when
22495      they are actually used.  */
22496   switch (ix86_tune)
22497     {
22498     case PROCESSOR_CORE2:
22499     case PROCESSOR_COREI7_32:
22500     case PROCESSOR_COREI7_64:
22501       targetm.sched.dfa_post_advance_cycle
22502         = core2i7_dfa_post_advance_cycle;
22503       targetm.sched.first_cycle_multipass_init
22504         = core2i7_first_cycle_multipass_init;
22505       targetm.sched.first_cycle_multipass_begin
22506         = core2i7_first_cycle_multipass_begin;
22507       targetm.sched.first_cycle_multipass_issue
22508         = core2i7_first_cycle_multipass_issue;
22509       targetm.sched.first_cycle_multipass_backtrack
22510         = core2i7_first_cycle_multipass_backtrack;
22511       targetm.sched.first_cycle_multipass_end
22512         = core2i7_first_cycle_multipass_end;
22513       targetm.sched.first_cycle_multipass_fini
22514         = core2i7_first_cycle_multipass_fini;
22515
22516       /* Set decoder parameters.  */
22517       core2i7_secondary_decoder_max_insn_size = 8;
22518       core2i7_ifetch_block_size = 16;
22519       core2i7_ifetch_block_max_insns = 6;
22520       break;
22521
22522     default:
22523       targetm.sched.dfa_post_advance_cycle = NULL;
22524       targetm.sched.first_cycle_multipass_init = NULL;
22525       targetm.sched.first_cycle_multipass_begin = NULL;
22526       targetm.sched.first_cycle_multipass_issue = NULL;
22527       targetm.sched.first_cycle_multipass_backtrack = NULL;
22528       targetm.sched.first_cycle_multipass_end = NULL;
22529       targetm.sched.first_cycle_multipass_fini = NULL;
22530       break;
22531     }
22532 }
22533
22534 \f
22535 /* Compute the alignment given to a constant that is being placed in memory.
22536    EXP is the constant and ALIGN is the alignment that the object would
22537    ordinarily have.
22538    The value of this function is used instead of that alignment to align
22539    the object.  */
22540
22541 int
22542 ix86_constant_alignment (tree exp, int align)
22543 {
22544   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
22545       || TREE_CODE (exp) == INTEGER_CST)
22546     {
22547       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
22548         return 64;
22549       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
22550         return 128;
22551     }
22552   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
22553            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
22554     return BITS_PER_WORD;
22555
22556   return align;
22557 }
22558
22559 /* Compute the alignment for a static variable.
22560    TYPE is the data type, and ALIGN is the alignment that
22561    the object would ordinarily have.  The value of this function is used
22562    instead of that alignment to align the object.  */
22563
22564 int
22565 ix86_data_alignment (tree type, int align)
22566 {
22567   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
22568
22569   if (AGGREGATE_TYPE_P (type)
22570       && TYPE_SIZE (type)
22571       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
22572       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
22573           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
22574       && align < max_align)
22575     align = max_align;
22576
22577   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
22578      to 16byte boundary.  */
22579   if (TARGET_64BIT)
22580     {
22581       if (AGGREGATE_TYPE_P (type)
22582            && TYPE_SIZE (type)
22583            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
22584            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
22585                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
22586         return 128;
22587     }
22588
22589   if (TREE_CODE (type) == ARRAY_TYPE)
22590     {
22591       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
22592         return 64;
22593       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
22594         return 128;
22595     }
22596   else if (TREE_CODE (type) == COMPLEX_TYPE)
22597     {
22598
22599       if (TYPE_MODE (type) == DCmode && align < 64)
22600         return 64;
22601       if ((TYPE_MODE (type) == XCmode
22602            || TYPE_MODE (type) == TCmode) && align < 128)
22603         return 128;
22604     }
22605   else if ((TREE_CODE (type) == RECORD_TYPE
22606             || TREE_CODE (type) == UNION_TYPE
22607             || TREE_CODE (type) == QUAL_UNION_TYPE)
22608            && TYPE_FIELDS (type))
22609     {
22610       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
22611         return 64;
22612       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
22613         return 128;
22614     }
22615   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
22616            || TREE_CODE (type) == INTEGER_TYPE)
22617     {
22618       if (TYPE_MODE (type) == DFmode && align < 64)
22619         return 64;
22620       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
22621         return 128;
22622     }
22623
22624   return align;
22625 }
22626
22627 /* Compute the alignment for a local variable or a stack slot.  EXP is
22628    the data type or decl itself, MODE is the widest mode available and
22629    ALIGN is the alignment that the object would ordinarily have.  The
22630    value of this macro is used instead of that alignment to align the
22631    object.  */
22632
22633 unsigned int
22634 ix86_local_alignment (tree exp, enum machine_mode mode,
22635                       unsigned int align)
22636 {
22637   tree type, decl;
22638
22639   if (exp && DECL_P (exp))
22640     {
22641       type = TREE_TYPE (exp);
22642       decl = exp;
22643     }
22644   else
22645     {
22646       type = exp;
22647       decl = NULL;
22648     }
22649
22650   if (use_avx256_p (mode, type))
22651     cfun->machine->use_avx256_p = true;
22652
22653   /* Don't do dynamic stack realignment for long long objects with
22654      -mpreferred-stack-boundary=2.  */
22655   if (!TARGET_64BIT
22656       && align == 64
22657       && ix86_preferred_stack_boundary < 64
22658       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
22659       && (!type || !TYPE_USER_ALIGN (type))
22660       && (!decl || !DECL_USER_ALIGN (decl)))
22661     align = 32;
22662
22663   /* If TYPE is NULL, we are allocating a stack slot for caller-save
22664      register in MODE.  We will return the largest alignment of XF
22665      and DF.  */
22666   if (!type)
22667     {
22668       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
22669         align = GET_MODE_ALIGNMENT (DFmode);
22670       return align;
22671     }
22672
22673   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
22674      to 16byte boundary.  Exact wording is:
22675
22676      An array uses the same alignment as its elements, except that a local or
22677      global array variable of length at least 16 bytes or
22678      a C99 variable-length array variable always has alignment of at least 16 bytes.
22679
22680      This was added to allow use of aligned SSE instructions at arrays.  This
22681      rule is meant for static storage (where compiler can not do the analysis
22682      by itself).  We follow it for automatic variables only when convenient.
22683      We fully control everything in the function compiled and functions from
22684      other unit can not rely on the alignment.
22685
22686      Exclude va_list type.  It is the common case of local array where
22687      we can not benefit from the alignment.  */
22688   if (TARGET_64BIT && optimize_function_for_speed_p (cfun)
22689       && TARGET_SSE)
22690     {
22691       if (AGGREGATE_TYPE_P (type)
22692            && (TYPE_MAIN_VARIANT (type)
22693                != TYPE_MAIN_VARIANT (va_list_type_node))
22694            && TYPE_SIZE (type)
22695            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
22696            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
22697                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
22698         return 128;
22699     }
22700   if (TREE_CODE (type) == ARRAY_TYPE)
22701     {
22702       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
22703         return 64;
22704       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
22705         return 128;
22706     }
22707   else if (TREE_CODE (type) == COMPLEX_TYPE)
22708     {
22709       if (TYPE_MODE (type) == DCmode && align < 64)
22710         return 64;
22711       if ((TYPE_MODE (type) == XCmode
22712            || TYPE_MODE (type) == TCmode) && align < 128)
22713         return 128;
22714     }
22715   else if ((TREE_CODE (type) == RECORD_TYPE
22716             || TREE_CODE (type) == UNION_TYPE
22717             || TREE_CODE (type) == QUAL_UNION_TYPE)
22718            && TYPE_FIELDS (type))
22719     {
22720       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
22721         return 64;
22722       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
22723         return 128;
22724     }
22725   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
22726            || TREE_CODE (type) == INTEGER_TYPE)
22727     {
22728
22729       if (TYPE_MODE (type) == DFmode && align < 64)
22730         return 64;
22731       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
22732         return 128;
22733     }
22734   return align;
22735 }
22736
22737 /* Compute the minimum required alignment for dynamic stack realignment
22738    purposes for a local variable, parameter or a stack slot.  EXP is
22739    the data type or decl itself, MODE is its mode and ALIGN is the
22740    alignment that the object would ordinarily have.  */
22741
22742 unsigned int
22743 ix86_minimum_alignment (tree exp, enum machine_mode mode,
22744                         unsigned int align)
22745 {
22746   tree type, decl;
22747
22748   if (exp && DECL_P (exp))
22749     {
22750       type = TREE_TYPE (exp);
22751       decl = exp;
22752     }
22753   else
22754     {
22755       type = exp;
22756       decl = NULL;
22757     }
22758
22759   if (use_avx256_p (mode, type))
22760     cfun->machine->use_avx256_p = true;
22761
22762   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
22763     return align;
22764
22765   /* Don't do dynamic stack realignment for long long objects with
22766      -mpreferred-stack-boundary=2.  */
22767   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
22768       && (!type || !TYPE_USER_ALIGN (type))
22769       && (!decl || !DECL_USER_ALIGN (decl)))
22770     return 32;
22771
22772   return align;
22773 }
22774 \f
22775 /* Find a location for the static chain incoming to a nested function.
22776    This is a register, unless all free registers are used by arguments.  */
22777
22778 static rtx
22779 ix86_static_chain (const_tree fndecl, bool incoming_p)
22780 {
22781   unsigned regno;
22782
22783   if (!DECL_STATIC_CHAIN (fndecl))
22784     return NULL;
22785
22786   if (TARGET_64BIT)
22787     {
22788       /* We always use R10 in 64-bit mode.  */
22789       regno = R10_REG;
22790     }
22791   else
22792     {
22793       tree fntype;
22794       /* By default in 32-bit mode we use ECX to pass the static chain.  */
22795       regno = CX_REG;
22796
22797       fntype = TREE_TYPE (fndecl);
22798       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
22799         {
22800           /* Fastcall functions use ecx/edx for arguments, which leaves
22801              us with EAX for the static chain.  */
22802           regno = AX_REG;
22803         }
22804       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
22805         {
22806           /* Thiscall functions use ecx for arguments, which leaves
22807              us with EAX for the static chain.  */
22808           regno = AX_REG;
22809         }
22810       else if (ix86_function_regparm (fntype, fndecl) == 3)
22811         {
22812           /* For regparm 3, we have no free call-clobbered registers in
22813              which to store the static chain.  In order to implement this,
22814              we have the trampoline push the static chain to the stack.
22815              However, we can't push a value below the return address when
22816              we call the nested function directly, so we have to use an
22817              alternate entry point.  For this we use ESI, and have the
22818              alternate entry point push ESI, so that things appear the
22819              same once we're executing the nested function.  */
22820           if (incoming_p)
22821             {
22822               if (fndecl == current_function_decl)
22823                 ix86_static_chain_on_stack = true;
22824               return gen_frame_mem (SImode,
22825                                     plus_constant (arg_pointer_rtx, -8));
22826             }
22827           regno = SI_REG;
22828         }
22829     }
22830
22831   return gen_rtx_REG (Pmode, regno);
22832 }
22833
22834 /* Emit RTL insns to initialize the variable parts of a trampoline.
22835    FNDECL is the decl of the target address; M_TRAMP is a MEM for
22836    the trampoline, and CHAIN_VALUE is an RTX for the static chain
22837    to be passed to the target function.  */
22838
22839 static void
22840 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
22841 {
22842   rtx mem, fnaddr;
22843
22844   fnaddr = XEXP (DECL_RTL (fndecl), 0);
22845
22846   if (!TARGET_64BIT)
22847     {
22848       rtx disp, chain;
22849       int opcode;
22850
22851       /* Depending on the static chain location, either load a register
22852          with a constant, or push the constant to the stack.  All of the
22853          instructions are the same size.  */
22854       chain = ix86_static_chain (fndecl, true);
22855       if (REG_P (chain))
22856         {
22857           if (REGNO (chain) == CX_REG)
22858             opcode = 0xb9;
22859           else if (REGNO (chain) == AX_REG)
22860             opcode = 0xb8;
22861           else
22862             gcc_unreachable ();
22863         }
22864       else
22865         opcode = 0x68;
22866
22867       mem = adjust_address (m_tramp, QImode, 0);
22868       emit_move_insn (mem, gen_int_mode (opcode, QImode));
22869
22870       mem = adjust_address (m_tramp, SImode, 1);
22871       emit_move_insn (mem, chain_value);
22872
22873       /* Compute offset from the end of the jmp to the target function.
22874          In the case in which the trampoline stores the static chain on
22875          the stack, we need to skip the first insn which pushes the
22876          (call-saved) register static chain; this push is 1 byte.  */
22877       disp = expand_binop (SImode, sub_optab, fnaddr,
22878                            plus_constant (XEXP (m_tramp, 0),
22879                                           MEM_P (chain) ? 9 : 10),
22880                            NULL_RTX, 1, OPTAB_DIRECT);
22881
22882       mem = adjust_address (m_tramp, QImode, 5);
22883       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
22884
22885       mem = adjust_address (m_tramp, SImode, 6);
22886       emit_move_insn (mem, disp);
22887     }
22888   else
22889     {
22890       int offset = 0;
22891
22892       /* Load the function address to r11.  Try to load address using
22893          the shorter movl instead of movabs.  We may want to support
22894          movq for kernel mode, but kernel does not use trampolines at
22895          the moment.  */
22896       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
22897         {
22898           fnaddr = copy_to_mode_reg (DImode, fnaddr);
22899
22900           mem = adjust_address (m_tramp, HImode, offset);
22901           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
22902
22903           mem = adjust_address (m_tramp, SImode, offset + 2);
22904           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
22905           offset += 6;
22906         }
22907       else
22908         {
22909           mem = adjust_address (m_tramp, HImode, offset);
22910           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
22911
22912           mem = adjust_address (m_tramp, DImode, offset + 2);
22913           emit_move_insn (mem, fnaddr);
22914           offset += 10;
22915         }
22916
22917       /* Load static chain using movabs to r10.  */
22918       mem = adjust_address (m_tramp, HImode, offset);
22919       emit_move_insn (mem, gen_int_mode (0xba49, HImode));
22920
22921       mem = adjust_address (m_tramp, DImode, offset + 2);
22922       emit_move_insn (mem, chain_value);
22923       offset += 10;
22924
22925       /* Jump to r11; the last (unused) byte is a nop, only there to
22926          pad the write out to a single 32-bit store.  */
22927       mem = adjust_address (m_tramp, SImode, offset);
22928       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
22929       offset += 4;
22930
22931       gcc_assert (offset <= TRAMPOLINE_SIZE);
22932     }
22933
22934 #ifdef ENABLE_EXECUTE_STACK
22935 #ifdef CHECK_EXECUTE_STACK_ENABLED
22936   if (CHECK_EXECUTE_STACK_ENABLED)
22937 #endif
22938   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
22939                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
22940 #endif
22941 }
22942 \f
22943 /* The following file contains several enumerations and data structures
22944    built from the definitions in i386-builtin-types.def.  */
22945
22946 #include "i386-builtin-types.inc"
22947
22948 /* Table for the ix86 builtin non-function types.  */
22949 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
22950
22951 /* Retrieve an element from the above table, building some of
22952    the types lazily.  */
22953
22954 static tree
22955 ix86_get_builtin_type (enum ix86_builtin_type tcode)
22956 {
22957   unsigned int index;
22958   tree type, itype;
22959
22960   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
22961
22962   type = ix86_builtin_type_tab[(int) tcode];
22963   if (type != NULL)
22964     return type;
22965
22966   gcc_assert (tcode > IX86_BT_LAST_PRIM);
22967   if (tcode <= IX86_BT_LAST_VECT)
22968     {
22969       enum machine_mode mode;
22970
22971       index = tcode - IX86_BT_LAST_PRIM - 1;
22972       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
22973       mode = ix86_builtin_type_vect_mode[index];
22974
22975       type = build_vector_type_for_mode (itype, mode);
22976     }
22977   else
22978     {
22979       int quals;
22980
22981       index = tcode - IX86_BT_LAST_VECT - 1;
22982       if (tcode <= IX86_BT_LAST_PTR)
22983         quals = TYPE_UNQUALIFIED;
22984       else
22985         quals = TYPE_QUAL_CONST;
22986
22987       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
22988       if (quals != TYPE_UNQUALIFIED)
22989         itype = build_qualified_type (itype, quals);
22990
22991       type = build_pointer_type (itype);
22992     }
22993
22994   ix86_builtin_type_tab[(int) tcode] = type;
22995   return type;
22996 }
22997
22998 /* Table for the ix86 builtin function types.  */
22999 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
23000
23001 /* Retrieve an element from the above table, building some of
23002    the types lazily.  */
23003
23004 static tree
23005 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
23006 {
23007   tree type;
23008
23009   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
23010
23011   type = ix86_builtin_func_type_tab[(int) tcode];
23012   if (type != NULL)
23013     return type;
23014
23015   if (tcode <= IX86_BT_LAST_FUNC)
23016     {
23017       unsigned start = ix86_builtin_func_start[(int) tcode];
23018       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
23019       tree rtype, atype, args = void_list_node;
23020       unsigned i;
23021
23022       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
23023       for (i = after - 1; i > start; --i)
23024         {
23025           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
23026           args = tree_cons (NULL, atype, args);
23027         }
23028
23029       type = build_function_type (rtype, args);
23030     }
23031   else
23032     {
23033       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
23034       enum ix86_builtin_func_type icode;
23035
23036       icode = ix86_builtin_func_alias_base[index];
23037       type = ix86_get_builtin_func_type (icode);
23038     }
23039
23040   ix86_builtin_func_type_tab[(int) tcode] = type;
23041   return type;
23042 }
23043
23044
23045 /* Codes for all the SSE/MMX builtins.  */
23046 enum ix86_builtins
23047 {
23048   IX86_BUILTIN_ADDPS,
23049   IX86_BUILTIN_ADDSS,
23050   IX86_BUILTIN_DIVPS,
23051   IX86_BUILTIN_DIVSS,
23052   IX86_BUILTIN_MULPS,
23053   IX86_BUILTIN_MULSS,
23054   IX86_BUILTIN_SUBPS,
23055   IX86_BUILTIN_SUBSS,
23056
23057   IX86_BUILTIN_CMPEQPS,
23058   IX86_BUILTIN_CMPLTPS,
23059   IX86_BUILTIN_CMPLEPS,
23060   IX86_BUILTIN_CMPGTPS,
23061   IX86_BUILTIN_CMPGEPS,
23062   IX86_BUILTIN_CMPNEQPS,
23063   IX86_BUILTIN_CMPNLTPS,
23064   IX86_BUILTIN_CMPNLEPS,
23065   IX86_BUILTIN_CMPNGTPS,
23066   IX86_BUILTIN_CMPNGEPS,
23067   IX86_BUILTIN_CMPORDPS,
23068   IX86_BUILTIN_CMPUNORDPS,
23069   IX86_BUILTIN_CMPEQSS,
23070   IX86_BUILTIN_CMPLTSS,
23071   IX86_BUILTIN_CMPLESS,
23072   IX86_BUILTIN_CMPNEQSS,
23073   IX86_BUILTIN_CMPNLTSS,
23074   IX86_BUILTIN_CMPNLESS,
23075   IX86_BUILTIN_CMPNGTSS,
23076   IX86_BUILTIN_CMPNGESS,
23077   IX86_BUILTIN_CMPORDSS,
23078   IX86_BUILTIN_CMPUNORDSS,
23079
23080   IX86_BUILTIN_COMIEQSS,
23081   IX86_BUILTIN_COMILTSS,
23082   IX86_BUILTIN_COMILESS,
23083   IX86_BUILTIN_COMIGTSS,
23084   IX86_BUILTIN_COMIGESS,
23085   IX86_BUILTIN_COMINEQSS,
23086   IX86_BUILTIN_UCOMIEQSS,
23087   IX86_BUILTIN_UCOMILTSS,
23088   IX86_BUILTIN_UCOMILESS,
23089   IX86_BUILTIN_UCOMIGTSS,
23090   IX86_BUILTIN_UCOMIGESS,
23091   IX86_BUILTIN_UCOMINEQSS,
23092
23093   IX86_BUILTIN_CVTPI2PS,
23094   IX86_BUILTIN_CVTPS2PI,
23095   IX86_BUILTIN_CVTSI2SS,
23096   IX86_BUILTIN_CVTSI642SS,
23097   IX86_BUILTIN_CVTSS2SI,
23098   IX86_BUILTIN_CVTSS2SI64,
23099   IX86_BUILTIN_CVTTPS2PI,
23100   IX86_BUILTIN_CVTTSS2SI,
23101   IX86_BUILTIN_CVTTSS2SI64,
23102
23103   IX86_BUILTIN_MAXPS,
23104   IX86_BUILTIN_MAXSS,
23105   IX86_BUILTIN_MINPS,
23106   IX86_BUILTIN_MINSS,
23107
23108   IX86_BUILTIN_LOADUPS,
23109   IX86_BUILTIN_STOREUPS,
23110   IX86_BUILTIN_MOVSS,
23111
23112   IX86_BUILTIN_MOVHLPS,
23113   IX86_BUILTIN_MOVLHPS,
23114   IX86_BUILTIN_LOADHPS,
23115   IX86_BUILTIN_LOADLPS,
23116   IX86_BUILTIN_STOREHPS,
23117   IX86_BUILTIN_STORELPS,
23118
23119   IX86_BUILTIN_MASKMOVQ,
23120   IX86_BUILTIN_MOVMSKPS,
23121   IX86_BUILTIN_PMOVMSKB,
23122
23123   IX86_BUILTIN_MOVNTPS,
23124   IX86_BUILTIN_MOVNTQ,
23125
23126   IX86_BUILTIN_LOADDQU,
23127   IX86_BUILTIN_STOREDQU,
23128
23129   IX86_BUILTIN_PACKSSWB,
23130   IX86_BUILTIN_PACKSSDW,
23131   IX86_BUILTIN_PACKUSWB,
23132
23133   IX86_BUILTIN_PADDB,
23134   IX86_BUILTIN_PADDW,
23135   IX86_BUILTIN_PADDD,
23136   IX86_BUILTIN_PADDQ,
23137   IX86_BUILTIN_PADDSB,
23138   IX86_BUILTIN_PADDSW,
23139   IX86_BUILTIN_PADDUSB,
23140   IX86_BUILTIN_PADDUSW,
23141   IX86_BUILTIN_PSUBB,
23142   IX86_BUILTIN_PSUBW,
23143   IX86_BUILTIN_PSUBD,
23144   IX86_BUILTIN_PSUBQ,
23145   IX86_BUILTIN_PSUBSB,
23146   IX86_BUILTIN_PSUBSW,
23147   IX86_BUILTIN_PSUBUSB,
23148   IX86_BUILTIN_PSUBUSW,
23149
23150   IX86_BUILTIN_PAND,
23151   IX86_BUILTIN_PANDN,
23152   IX86_BUILTIN_POR,
23153   IX86_BUILTIN_PXOR,
23154
23155   IX86_BUILTIN_PAVGB,
23156   IX86_BUILTIN_PAVGW,
23157
23158   IX86_BUILTIN_PCMPEQB,
23159   IX86_BUILTIN_PCMPEQW,
23160   IX86_BUILTIN_PCMPEQD,
23161   IX86_BUILTIN_PCMPGTB,
23162   IX86_BUILTIN_PCMPGTW,
23163   IX86_BUILTIN_PCMPGTD,
23164
23165   IX86_BUILTIN_PMADDWD,
23166
23167   IX86_BUILTIN_PMAXSW,
23168   IX86_BUILTIN_PMAXUB,
23169   IX86_BUILTIN_PMINSW,
23170   IX86_BUILTIN_PMINUB,
23171
23172   IX86_BUILTIN_PMULHUW,
23173   IX86_BUILTIN_PMULHW,
23174   IX86_BUILTIN_PMULLW,
23175
23176   IX86_BUILTIN_PSADBW,
23177   IX86_BUILTIN_PSHUFW,
23178
23179   IX86_BUILTIN_PSLLW,
23180   IX86_BUILTIN_PSLLD,
23181   IX86_BUILTIN_PSLLQ,
23182   IX86_BUILTIN_PSRAW,
23183   IX86_BUILTIN_PSRAD,
23184   IX86_BUILTIN_PSRLW,
23185   IX86_BUILTIN_PSRLD,
23186   IX86_BUILTIN_PSRLQ,
23187   IX86_BUILTIN_PSLLWI,
23188   IX86_BUILTIN_PSLLDI,
23189   IX86_BUILTIN_PSLLQI,
23190   IX86_BUILTIN_PSRAWI,
23191   IX86_BUILTIN_PSRADI,
23192   IX86_BUILTIN_PSRLWI,
23193   IX86_BUILTIN_PSRLDI,
23194   IX86_BUILTIN_PSRLQI,
23195
23196   IX86_BUILTIN_PUNPCKHBW,
23197   IX86_BUILTIN_PUNPCKHWD,
23198   IX86_BUILTIN_PUNPCKHDQ,
23199   IX86_BUILTIN_PUNPCKLBW,
23200   IX86_BUILTIN_PUNPCKLWD,
23201   IX86_BUILTIN_PUNPCKLDQ,
23202
23203   IX86_BUILTIN_SHUFPS,
23204
23205   IX86_BUILTIN_RCPPS,
23206   IX86_BUILTIN_RCPSS,
23207   IX86_BUILTIN_RSQRTPS,
23208   IX86_BUILTIN_RSQRTPS_NR,
23209   IX86_BUILTIN_RSQRTSS,
23210   IX86_BUILTIN_RSQRTF,
23211   IX86_BUILTIN_SQRTPS,
23212   IX86_BUILTIN_SQRTPS_NR,
23213   IX86_BUILTIN_SQRTSS,
23214
23215   IX86_BUILTIN_UNPCKHPS,
23216   IX86_BUILTIN_UNPCKLPS,
23217
23218   IX86_BUILTIN_ANDPS,
23219   IX86_BUILTIN_ANDNPS,
23220   IX86_BUILTIN_ORPS,
23221   IX86_BUILTIN_XORPS,
23222
23223   IX86_BUILTIN_EMMS,
23224   IX86_BUILTIN_LDMXCSR,
23225   IX86_BUILTIN_STMXCSR,
23226   IX86_BUILTIN_SFENCE,
23227
23228   /* 3DNow! Original */
23229   IX86_BUILTIN_FEMMS,
23230   IX86_BUILTIN_PAVGUSB,
23231   IX86_BUILTIN_PF2ID,
23232   IX86_BUILTIN_PFACC,
23233   IX86_BUILTIN_PFADD,
23234   IX86_BUILTIN_PFCMPEQ,
23235   IX86_BUILTIN_PFCMPGE,
23236   IX86_BUILTIN_PFCMPGT,
23237   IX86_BUILTIN_PFMAX,
23238   IX86_BUILTIN_PFMIN,
23239   IX86_BUILTIN_PFMUL,
23240   IX86_BUILTIN_PFRCP,
23241   IX86_BUILTIN_PFRCPIT1,
23242   IX86_BUILTIN_PFRCPIT2,
23243   IX86_BUILTIN_PFRSQIT1,
23244   IX86_BUILTIN_PFRSQRT,
23245   IX86_BUILTIN_PFSUB,
23246   IX86_BUILTIN_PFSUBR,
23247   IX86_BUILTIN_PI2FD,
23248   IX86_BUILTIN_PMULHRW,
23249
23250   /* 3DNow! Athlon Extensions */
23251   IX86_BUILTIN_PF2IW,
23252   IX86_BUILTIN_PFNACC,
23253   IX86_BUILTIN_PFPNACC,
23254   IX86_BUILTIN_PI2FW,
23255   IX86_BUILTIN_PSWAPDSI,
23256   IX86_BUILTIN_PSWAPDSF,
23257
23258   /* SSE2 */
23259   IX86_BUILTIN_ADDPD,
23260   IX86_BUILTIN_ADDSD,
23261   IX86_BUILTIN_DIVPD,
23262   IX86_BUILTIN_DIVSD,
23263   IX86_BUILTIN_MULPD,
23264   IX86_BUILTIN_MULSD,
23265   IX86_BUILTIN_SUBPD,
23266   IX86_BUILTIN_SUBSD,
23267
23268   IX86_BUILTIN_CMPEQPD,
23269   IX86_BUILTIN_CMPLTPD,
23270   IX86_BUILTIN_CMPLEPD,
23271   IX86_BUILTIN_CMPGTPD,
23272   IX86_BUILTIN_CMPGEPD,
23273   IX86_BUILTIN_CMPNEQPD,
23274   IX86_BUILTIN_CMPNLTPD,
23275   IX86_BUILTIN_CMPNLEPD,
23276   IX86_BUILTIN_CMPNGTPD,
23277   IX86_BUILTIN_CMPNGEPD,
23278   IX86_BUILTIN_CMPORDPD,
23279   IX86_BUILTIN_CMPUNORDPD,
23280   IX86_BUILTIN_CMPEQSD,
23281   IX86_BUILTIN_CMPLTSD,
23282   IX86_BUILTIN_CMPLESD,
23283   IX86_BUILTIN_CMPNEQSD,
23284   IX86_BUILTIN_CMPNLTSD,
23285   IX86_BUILTIN_CMPNLESD,
23286   IX86_BUILTIN_CMPORDSD,
23287   IX86_BUILTIN_CMPUNORDSD,
23288
23289   IX86_BUILTIN_COMIEQSD,
23290   IX86_BUILTIN_COMILTSD,
23291   IX86_BUILTIN_COMILESD,
23292   IX86_BUILTIN_COMIGTSD,
23293   IX86_BUILTIN_COMIGESD,
23294   IX86_BUILTIN_COMINEQSD,
23295   IX86_BUILTIN_UCOMIEQSD,
23296   IX86_BUILTIN_UCOMILTSD,
23297   IX86_BUILTIN_UCOMILESD,
23298   IX86_BUILTIN_UCOMIGTSD,
23299   IX86_BUILTIN_UCOMIGESD,
23300   IX86_BUILTIN_UCOMINEQSD,
23301
23302   IX86_BUILTIN_MAXPD,
23303   IX86_BUILTIN_MAXSD,
23304   IX86_BUILTIN_MINPD,
23305   IX86_BUILTIN_MINSD,
23306
23307   IX86_BUILTIN_ANDPD,
23308   IX86_BUILTIN_ANDNPD,
23309   IX86_BUILTIN_ORPD,
23310   IX86_BUILTIN_XORPD,
23311
23312   IX86_BUILTIN_SQRTPD,
23313   IX86_BUILTIN_SQRTSD,
23314
23315   IX86_BUILTIN_UNPCKHPD,
23316   IX86_BUILTIN_UNPCKLPD,
23317
23318   IX86_BUILTIN_SHUFPD,
23319
23320   IX86_BUILTIN_LOADUPD,
23321   IX86_BUILTIN_STOREUPD,
23322   IX86_BUILTIN_MOVSD,
23323
23324   IX86_BUILTIN_LOADHPD,
23325   IX86_BUILTIN_LOADLPD,
23326
23327   IX86_BUILTIN_CVTDQ2PD,
23328   IX86_BUILTIN_CVTDQ2PS,
23329
23330   IX86_BUILTIN_CVTPD2DQ,
23331   IX86_BUILTIN_CVTPD2PI,
23332   IX86_BUILTIN_CVTPD2PS,
23333   IX86_BUILTIN_CVTTPD2DQ,
23334   IX86_BUILTIN_CVTTPD2PI,
23335
23336   IX86_BUILTIN_CVTPI2PD,
23337   IX86_BUILTIN_CVTSI2SD,
23338   IX86_BUILTIN_CVTSI642SD,
23339
23340   IX86_BUILTIN_CVTSD2SI,
23341   IX86_BUILTIN_CVTSD2SI64,
23342   IX86_BUILTIN_CVTSD2SS,
23343   IX86_BUILTIN_CVTSS2SD,
23344   IX86_BUILTIN_CVTTSD2SI,
23345   IX86_BUILTIN_CVTTSD2SI64,
23346
23347   IX86_BUILTIN_CVTPS2DQ,
23348   IX86_BUILTIN_CVTPS2PD,
23349   IX86_BUILTIN_CVTTPS2DQ,
23350
23351   IX86_BUILTIN_MOVNTI,
23352   IX86_BUILTIN_MOVNTPD,
23353   IX86_BUILTIN_MOVNTDQ,
23354
23355   IX86_BUILTIN_MOVQ128,
23356
23357   /* SSE2 MMX */
23358   IX86_BUILTIN_MASKMOVDQU,
23359   IX86_BUILTIN_MOVMSKPD,
23360   IX86_BUILTIN_PMOVMSKB128,
23361
23362   IX86_BUILTIN_PACKSSWB128,
23363   IX86_BUILTIN_PACKSSDW128,
23364   IX86_BUILTIN_PACKUSWB128,
23365
23366   IX86_BUILTIN_PADDB128,
23367   IX86_BUILTIN_PADDW128,
23368   IX86_BUILTIN_PADDD128,
23369   IX86_BUILTIN_PADDQ128,
23370   IX86_BUILTIN_PADDSB128,
23371   IX86_BUILTIN_PADDSW128,
23372   IX86_BUILTIN_PADDUSB128,
23373   IX86_BUILTIN_PADDUSW128,
23374   IX86_BUILTIN_PSUBB128,
23375   IX86_BUILTIN_PSUBW128,
23376   IX86_BUILTIN_PSUBD128,
23377   IX86_BUILTIN_PSUBQ128,
23378   IX86_BUILTIN_PSUBSB128,
23379   IX86_BUILTIN_PSUBSW128,
23380   IX86_BUILTIN_PSUBUSB128,
23381   IX86_BUILTIN_PSUBUSW128,
23382
23383   IX86_BUILTIN_PAND128,
23384   IX86_BUILTIN_PANDN128,
23385   IX86_BUILTIN_POR128,
23386   IX86_BUILTIN_PXOR128,
23387
23388   IX86_BUILTIN_PAVGB128,
23389   IX86_BUILTIN_PAVGW128,
23390
23391   IX86_BUILTIN_PCMPEQB128,
23392   IX86_BUILTIN_PCMPEQW128,
23393   IX86_BUILTIN_PCMPEQD128,
23394   IX86_BUILTIN_PCMPGTB128,
23395   IX86_BUILTIN_PCMPGTW128,
23396   IX86_BUILTIN_PCMPGTD128,
23397
23398   IX86_BUILTIN_PMADDWD128,
23399
23400   IX86_BUILTIN_PMAXSW128,
23401   IX86_BUILTIN_PMAXUB128,
23402   IX86_BUILTIN_PMINSW128,
23403   IX86_BUILTIN_PMINUB128,
23404
23405   IX86_BUILTIN_PMULUDQ,
23406   IX86_BUILTIN_PMULUDQ128,
23407   IX86_BUILTIN_PMULHUW128,
23408   IX86_BUILTIN_PMULHW128,
23409   IX86_BUILTIN_PMULLW128,
23410
23411   IX86_BUILTIN_PSADBW128,
23412   IX86_BUILTIN_PSHUFHW,
23413   IX86_BUILTIN_PSHUFLW,
23414   IX86_BUILTIN_PSHUFD,
23415
23416   IX86_BUILTIN_PSLLDQI128,
23417   IX86_BUILTIN_PSLLWI128,
23418   IX86_BUILTIN_PSLLDI128,
23419   IX86_BUILTIN_PSLLQI128,
23420   IX86_BUILTIN_PSRAWI128,
23421   IX86_BUILTIN_PSRADI128,
23422   IX86_BUILTIN_PSRLDQI128,
23423   IX86_BUILTIN_PSRLWI128,
23424   IX86_BUILTIN_PSRLDI128,
23425   IX86_BUILTIN_PSRLQI128,
23426
23427   IX86_BUILTIN_PSLLDQ128,
23428   IX86_BUILTIN_PSLLW128,
23429   IX86_BUILTIN_PSLLD128,
23430   IX86_BUILTIN_PSLLQ128,
23431   IX86_BUILTIN_PSRAW128,
23432   IX86_BUILTIN_PSRAD128,
23433   IX86_BUILTIN_PSRLW128,
23434   IX86_BUILTIN_PSRLD128,
23435   IX86_BUILTIN_PSRLQ128,
23436
23437   IX86_BUILTIN_PUNPCKHBW128,
23438   IX86_BUILTIN_PUNPCKHWD128,
23439   IX86_BUILTIN_PUNPCKHDQ128,
23440   IX86_BUILTIN_PUNPCKHQDQ128,
23441   IX86_BUILTIN_PUNPCKLBW128,
23442   IX86_BUILTIN_PUNPCKLWD128,
23443   IX86_BUILTIN_PUNPCKLDQ128,
23444   IX86_BUILTIN_PUNPCKLQDQ128,
23445
23446   IX86_BUILTIN_CLFLUSH,
23447   IX86_BUILTIN_MFENCE,
23448   IX86_BUILTIN_LFENCE,
23449
23450   IX86_BUILTIN_BSRSI,
23451   IX86_BUILTIN_BSRDI,
23452   IX86_BUILTIN_RDPMC,
23453   IX86_BUILTIN_RDTSC,
23454   IX86_BUILTIN_RDTSCP,
23455   IX86_BUILTIN_ROLQI,
23456   IX86_BUILTIN_ROLHI,
23457   IX86_BUILTIN_RORQI,
23458   IX86_BUILTIN_RORHI,
23459
23460   /* SSE3.  */
23461   IX86_BUILTIN_ADDSUBPS,
23462   IX86_BUILTIN_HADDPS,
23463   IX86_BUILTIN_HSUBPS,
23464   IX86_BUILTIN_MOVSHDUP,
23465   IX86_BUILTIN_MOVSLDUP,
23466   IX86_BUILTIN_ADDSUBPD,
23467   IX86_BUILTIN_HADDPD,
23468   IX86_BUILTIN_HSUBPD,
23469   IX86_BUILTIN_LDDQU,
23470
23471   IX86_BUILTIN_MONITOR,
23472   IX86_BUILTIN_MWAIT,
23473
23474   /* SSSE3.  */
23475   IX86_BUILTIN_PHADDW,
23476   IX86_BUILTIN_PHADDD,
23477   IX86_BUILTIN_PHADDSW,
23478   IX86_BUILTIN_PHSUBW,
23479   IX86_BUILTIN_PHSUBD,
23480   IX86_BUILTIN_PHSUBSW,
23481   IX86_BUILTIN_PMADDUBSW,
23482   IX86_BUILTIN_PMULHRSW,
23483   IX86_BUILTIN_PSHUFB,
23484   IX86_BUILTIN_PSIGNB,
23485   IX86_BUILTIN_PSIGNW,
23486   IX86_BUILTIN_PSIGND,
23487   IX86_BUILTIN_PALIGNR,
23488   IX86_BUILTIN_PABSB,
23489   IX86_BUILTIN_PABSW,
23490   IX86_BUILTIN_PABSD,
23491
23492   IX86_BUILTIN_PHADDW128,
23493   IX86_BUILTIN_PHADDD128,
23494   IX86_BUILTIN_PHADDSW128,
23495   IX86_BUILTIN_PHSUBW128,
23496   IX86_BUILTIN_PHSUBD128,
23497   IX86_BUILTIN_PHSUBSW128,
23498   IX86_BUILTIN_PMADDUBSW128,
23499   IX86_BUILTIN_PMULHRSW128,
23500   IX86_BUILTIN_PSHUFB128,
23501   IX86_BUILTIN_PSIGNB128,
23502   IX86_BUILTIN_PSIGNW128,
23503   IX86_BUILTIN_PSIGND128,
23504   IX86_BUILTIN_PALIGNR128,
23505   IX86_BUILTIN_PABSB128,
23506   IX86_BUILTIN_PABSW128,
23507   IX86_BUILTIN_PABSD128,
23508
23509   /* AMDFAM10 - SSE4A New Instructions.  */
23510   IX86_BUILTIN_MOVNTSD,
23511   IX86_BUILTIN_MOVNTSS,
23512   IX86_BUILTIN_EXTRQI,
23513   IX86_BUILTIN_EXTRQ,
23514   IX86_BUILTIN_INSERTQI,
23515   IX86_BUILTIN_INSERTQ,
23516
23517   /* SSE4.1.  */
23518   IX86_BUILTIN_BLENDPD,
23519   IX86_BUILTIN_BLENDPS,
23520   IX86_BUILTIN_BLENDVPD,
23521   IX86_BUILTIN_BLENDVPS,
23522   IX86_BUILTIN_PBLENDVB128,
23523   IX86_BUILTIN_PBLENDW128,
23524
23525   IX86_BUILTIN_DPPD,
23526   IX86_BUILTIN_DPPS,
23527
23528   IX86_BUILTIN_INSERTPS128,
23529
23530   IX86_BUILTIN_MOVNTDQA,
23531   IX86_BUILTIN_MPSADBW128,
23532   IX86_BUILTIN_PACKUSDW128,
23533   IX86_BUILTIN_PCMPEQQ,
23534   IX86_BUILTIN_PHMINPOSUW128,
23535
23536   IX86_BUILTIN_PMAXSB128,
23537   IX86_BUILTIN_PMAXSD128,
23538   IX86_BUILTIN_PMAXUD128,
23539   IX86_BUILTIN_PMAXUW128,
23540
23541   IX86_BUILTIN_PMINSB128,
23542   IX86_BUILTIN_PMINSD128,
23543   IX86_BUILTIN_PMINUD128,
23544   IX86_BUILTIN_PMINUW128,
23545
23546   IX86_BUILTIN_PMOVSXBW128,
23547   IX86_BUILTIN_PMOVSXBD128,
23548   IX86_BUILTIN_PMOVSXBQ128,
23549   IX86_BUILTIN_PMOVSXWD128,
23550   IX86_BUILTIN_PMOVSXWQ128,
23551   IX86_BUILTIN_PMOVSXDQ128,
23552
23553   IX86_BUILTIN_PMOVZXBW128,
23554   IX86_BUILTIN_PMOVZXBD128,
23555   IX86_BUILTIN_PMOVZXBQ128,
23556   IX86_BUILTIN_PMOVZXWD128,
23557   IX86_BUILTIN_PMOVZXWQ128,
23558   IX86_BUILTIN_PMOVZXDQ128,
23559
23560   IX86_BUILTIN_PMULDQ128,
23561   IX86_BUILTIN_PMULLD128,
23562
23563   IX86_BUILTIN_ROUNDPD,
23564   IX86_BUILTIN_ROUNDPS,
23565   IX86_BUILTIN_ROUNDSD,
23566   IX86_BUILTIN_ROUNDSS,
23567
23568   IX86_BUILTIN_PTESTZ,
23569   IX86_BUILTIN_PTESTC,
23570   IX86_BUILTIN_PTESTNZC,
23571
23572   IX86_BUILTIN_VEC_INIT_V2SI,
23573   IX86_BUILTIN_VEC_INIT_V4HI,
23574   IX86_BUILTIN_VEC_INIT_V8QI,
23575   IX86_BUILTIN_VEC_EXT_V2DF,
23576   IX86_BUILTIN_VEC_EXT_V2DI,
23577   IX86_BUILTIN_VEC_EXT_V4SF,
23578   IX86_BUILTIN_VEC_EXT_V4SI,
23579   IX86_BUILTIN_VEC_EXT_V8HI,
23580   IX86_BUILTIN_VEC_EXT_V2SI,
23581   IX86_BUILTIN_VEC_EXT_V4HI,
23582   IX86_BUILTIN_VEC_EXT_V16QI,
23583   IX86_BUILTIN_VEC_SET_V2DI,
23584   IX86_BUILTIN_VEC_SET_V4SF,
23585   IX86_BUILTIN_VEC_SET_V4SI,
23586   IX86_BUILTIN_VEC_SET_V8HI,
23587   IX86_BUILTIN_VEC_SET_V4HI,
23588   IX86_BUILTIN_VEC_SET_V16QI,
23589
23590   IX86_BUILTIN_VEC_PACK_SFIX,
23591
23592   /* SSE4.2.  */
23593   IX86_BUILTIN_CRC32QI,
23594   IX86_BUILTIN_CRC32HI,
23595   IX86_BUILTIN_CRC32SI,
23596   IX86_BUILTIN_CRC32DI,
23597
23598   IX86_BUILTIN_PCMPESTRI128,
23599   IX86_BUILTIN_PCMPESTRM128,
23600   IX86_BUILTIN_PCMPESTRA128,
23601   IX86_BUILTIN_PCMPESTRC128,
23602   IX86_BUILTIN_PCMPESTRO128,
23603   IX86_BUILTIN_PCMPESTRS128,
23604   IX86_BUILTIN_PCMPESTRZ128,
23605   IX86_BUILTIN_PCMPISTRI128,
23606   IX86_BUILTIN_PCMPISTRM128,
23607   IX86_BUILTIN_PCMPISTRA128,
23608   IX86_BUILTIN_PCMPISTRC128,
23609   IX86_BUILTIN_PCMPISTRO128,
23610   IX86_BUILTIN_PCMPISTRS128,
23611   IX86_BUILTIN_PCMPISTRZ128,
23612
23613   IX86_BUILTIN_PCMPGTQ,
23614
23615   /* AES instructions */
23616   IX86_BUILTIN_AESENC128,
23617   IX86_BUILTIN_AESENCLAST128,
23618   IX86_BUILTIN_AESDEC128,
23619   IX86_BUILTIN_AESDECLAST128,
23620   IX86_BUILTIN_AESIMC128,
23621   IX86_BUILTIN_AESKEYGENASSIST128,
23622
23623   /* PCLMUL instruction */
23624   IX86_BUILTIN_PCLMULQDQ128,
23625
23626   /* AVX */
23627   IX86_BUILTIN_ADDPD256,
23628   IX86_BUILTIN_ADDPS256,
23629   IX86_BUILTIN_ADDSUBPD256,
23630   IX86_BUILTIN_ADDSUBPS256,
23631   IX86_BUILTIN_ANDPD256,
23632   IX86_BUILTIN_ANDPS256,
23633   IX86_BUILTIN_ANDNPD256,
23634   IX86_BUILTIN_ANDNPS256,
23635   IX86_BUILTIN_BLENDPD256,
23636   IX86_BUILTIN_BLENDPS256,
23637   IX86_BUILTIN_BLENDVPD256,
23638   IX86_BUILTIN_BLENDVPS256,
23639   IX86_BUILTIN_DIVPD256,
23640   IX86_BUILTIN_DIVPS256,
23641   IX86_BUILTIN_DPPS256,
23642   IX86_BUILTIN_HADDPD256,
23643   IX86_BUILTIN_HADDPS256,
23644   IX86_BUILTIN_HSUBPD256,
23645   IX86_BUILTIN_HSUBPS256,
23646   IX86_BUILTIN_MAXPD256,
23647   IX86_BUILTIN_MAXPS256,
23648   IX86_BUILTIN_MINPD256,
23649   IX86_BUILTIN_MINPS256,
23650   IX86_BUILTIN_MULPD256,
23651   IX86_BUILTIN_MULPS256,
23652   IX86_BUILTIN_ORPD256,
23653   IX86_BUILTIN_ORPS256,
23654   IX86_BUILTIN_SHUFPD256,
23655   IX86_BUILTIN_SHUFPS256,
23656   IX86_BUILTIN_SUBPD256,
23657   IX86_BUILTIN_SUBPS256,
23658   IX86_BUILTIN_XORPD256,
23659   IX86_BUILTIN_XORPS256,
23660   IX86_BUILTIN_CMPSD,
23661   IX86_BUILTIN_CMPSS,
23662   IX86_BUILTIN_CMPPD,
23663   IX86_BUILTIN_CMPPS,
23664   IX86_BUILTIN_CMPPD256,
23665   IX86_BUILTIN_CMPPS256,
23666   IX86_BUILTIN_CVTDQ2PD256,
23667   IX86_BUILTIN_CVTDQ2PS256,
23668   IX86_BUILTIN_CVTPD2PS256,
23669   IX86_BUILTIN_CVTPS2DQ256,
23670   IX86_BUILTIN_CVTPS2PD256,
23671   IX86_BUILTIN_CVTTPD2DQ256,
23672   IX86_BUILTIN_CVTPD2DQ256,
23673   IX86_BUILTIN_CVTTPS2DQ256,
23674   IX86_BUILTIN_EXTRACTF128PD256,
23675   IX86_BUILTIN_EXTRACTF128PS256,
23676   IX86_BUILTIN_EXTRACTF128SI256,
23677   IX86_BUILTIN_VZEROALL,
23678   IX86_BUILTIN_VZEROUPPER,
23679   IX86_BUILTIN_VPERMILVARPD,
23680   IX86_BUILTIN_VPERMILVARPS,
23681   IX86_BUILTIN_VPERMILVARPD256,
23682   IX86_BUILTIN_VPERMILVARPS256,
23683   IX86_BUILTIN_VPERMILPD,
23684   IX86_BUILTIN_VPERMILPS,
23685   IX86_BUILTIN_VPERMILPD256,
23686   IX86_BUILTIN_VPERMILPS256,
23687   IX86_BUILTIN_VPERMIL2PD,
23688   IX86_BUILTIN_VPERMIL2PS,
23689   IX86_BUILTIN_VPERMIL2PD256,
23690   IX86_BUILTIN_VPERMIL2PS256,
23691   IX86_BUILTIN_VPERM2F128PD256,
23692   IX86_BUILTIN_VPERM2F128PS256,
23693   IX86_BUILTIN_VPERM2F128SI256,
23694   IX86_BUILTIN_VBROADCASTSS,
23695   IX86_BUILTIN_VBROADCASTSD256,
23696   IX86_BUILTIN_VBROADCASTSS256,
23697   IX86_BUILTIN_VBROADCASTPD256,
23698   IX86_BUILTIN_VBROADCASTPS256,
23699   IX86_BUILTIN_VINSERTF128PD256,
23700   IX86_BUILTIN_VINSERTF128PS256,
23701   IX86_BUILTIN_VINSERTF128SI256,
23702   IX86_BUILTIN_LOADUPD256,
23703   IX86_BUILTIN_LOADUPS256,
23704   IX86_BUILTIN_STOREUPD256,
23705   IX86_BUILTIN_STOREUPS256,
23706   IX86_BUILTIN_LDDQU256,
23707   IX86_BUILTIN_MOVNTDQ256,
23708   IX86_BUILTIN_MOVNTPD256,
23709   IX86_BUILTIN_MOVNTPS256,
23710   IX86_BUILTIN_LOADDQU256,
23711   IX86_BUILTIN_STOREDQU256,
23712   IX86_BUILTIN_MASKLOADPD,
23713   IX86_BUILTIN_MASKLOADPS,
23714   IX86_BUILTIN_MASKSTOREPD,
23715   IX86_BUILTIN_MASKSTOREPS,
23716   IX86_BUILTIN_MASKLOADPD256,
23717   IX86_BUILTIN_MASKLOADPS256,
23718   IX86_BUILTIN_MASKSTOREPD256,
23719   IX86_BUILTIN_MASKSTOREPS256,
23720   IX86_BUILTIN_MOVSHDUP256,
23721   IX86_BUILTIN_MOVSLDUP256,
23722   IX86_BUILTIN_MOVDDUP256,
23723
23724   IX86_BUILTIN_SQRTPD256,
23725   IX86_BUILTIN_SQRTPS256,
23726   IX86_BUILTIN_SQRTPS_NR256,
23727   IX86_BUILTIN_RSQRTPS256,
23728   IX86_BUILTIN_RSQRTPS_NR256,
23729
23730   IX86_BUILTIN_RCPPS256,
23731
23732   IX86_BUILTIN_ROUNDPD256,
23733   IX86_BUILTIN_ROUNDPS256,
23734
23735   IX86_BUILTIN_UNPCKHPD256,
23736   IX86_BUILTIN_UNPCKLPD256,
23737   IX86_BUILTIN_UNPCKHPS256,
23738   IX86_BUILTIN_UNPCKLPS256,
23739
23740   IX86_BUILTIN_SI256_SI,
23741   IX86_BUILTIN_PS256_PS,
23742   IX86_BUILTIN_PD256_PD,
23743   IX86_BUILTIN_SI_SI256,
23744   IX86_BUILTIN_PS_PS256,
23745   IX86_BUILTIN_PD_PD256,
23746
23747   IX86_BUILTIN_VTESTZPD,
23748   IX86_BUILTIN_VTESTCPD,
23749   IX86_BUILTIN_VTESTNZCPD,
23750   IX86_BUILTIN_VTESTZPS,
23751   IX86_BUILTIN_VTESTCPS,
23752   IX86_BUILTIN_VTESTNZCPS,
23753   IX86_BUILTIN_VTESTZPD256,
23754   IX86_BUILTIN_VTESTCPD256,
23755   IX86_BUILTIN_VTESTNZCPD256,
23756   IX86_BUILTIN_VTESTZPS256,
23757   IX86_BUILTIN_VTESTCPS256,
23758   IX86_BUILTIN_VTESTNZCPS256,
23759   IX86_BUILTIN_PTESTZ256,
23760   IX86_BUILTIN_PTESTC256,
23761   IX86_BUILTIN_PTESTNZC256,
23762
23763   IX86_BUILTIN_MOVMSKPD256,
23764   IX86_BUILTIN_MOVMSKPS256,
23765
23766   /* TFmode support builtins.  */
23767   IX86_BUILTIN_INFQ,
23768   IX86_BUILTIN_HUGE_VALQ,
23769   IX86_BUILTIN_FABSQ,
23770   IX86_BUILTIN_COPYSIGNQ,
23771
23772   /* Vectorizer support builtins.  */
23773   IX86_BUILTIN_CPYSGNPS,
23774   IX86_BUILTIN_CPYSGNPD,
23775   IX86_BUILTIN_CPYSGNPS256,
23776   IX86_BUILTIN_CPYSGNPD256,
23777
23778   IX86_BUILTIN_CVTUDQ2PS,
23779
23780   IX86_BUILTIN_VEC_PERM_V2DF,
23781   IX86_BUILTIN_VEC_PERM_V4SF,
23782   IX86_BUILTIN_VEC_PERM_V2DI,
23783   IX86_BUILTIN_VEC_PERM_V4SI,
23784   IX86_BUILTIN_VEC_PERM_V8HI,
23785   IX86_BUILTIN_VEC_PERM_V16QI,
23786   IX86_BUILTIN_VEC_PERM_V2DI_U,
23787   IX86_BUILTIN_VEC_PERM_V4SI_U,
23788   IX86_BUILTIN_VEC_PERM_V8HI_U,
23789   IX86_BUILTIN_VEC_PERM_V16QI_U,
23790   IX86_BUILTIN_VEC_PERM_V4DF,
23791   IX86_BUILTIN_VEC_PERM_V8SF,
23792
23793   /* FMA4 and XOP instructions.  */
23794   IX86_BUILTIN_VFMADDSS,
23795   IX86_BUILTIN_VFMADDSD,
23796   IX86_BUILTIN_VFMADDPS,
23797   IX86_BUILTIN_VFMADDPD,
23798   IX86_BUILTIN_VFMADDPS256,
23799   IX86_BUILTIN_VFMADDPD256,
23800   IX86_BUILTIN_VFMADDSUBPS,
23801   IX86_BUILTIN_VFMADDSUBPD,
23802   IX86_BUILTIN_VFMADDSUBPS256,
23803   IX86_BUILTIN_VFMADDSUBPD256,
23804
23805   IX86_BUILTIN_VPCMOV,
23806   IX86_BUILTIN_VPCMOV_V2DI,
23807   IX86_BUILTIN_VPCMOV_V4SI,
23808   IX86_BUILTIN_VPCMOV_V8HI,
23809   IX86_BUILTIN_VPCMOV_V16QI,
23810   IX86_BUILTIN_VPCMOV_V4SF,
23811   IX86_BUILTIN_VPCMOV_V2DF,
23812   IX86_BUILTIN_VPCMOV256,
23813   IX86_BUILTIN_VPCMOV_V4DI256,
23814   IX86_BUILTIN_VPCMOV_V8SI256,
23815   IX86_BUILTIN_VPCMOV_V16HI256,
23816   IX86_BUILTIN_VPCMOV_V32QI256,
23817   IX86_BUILTIN_VPCMOV_V8SF256,
23818   IX86_BUILTIN_VPCMOV_V4DF256,
23819
23820   IX86_BUILTIN_VPPERM,
23821
23822   IX86_BUILTIN_VPMACSSWW,
23823   IX86_BUILTIN_VPMACSWW,
23824   IX86_BUILTIN_VPMACSSWD,
23825   IX86_BUILTIN_VPMACSWD,
23826   IX86_BUILTIN_VPMACSSDD,
23827   IX86_BUILTIN_VPMACSDD,
23828   IX86_BUILTIN_VPMACSSDQL,
23829   IX86_BUILTIN_VPMACSSDQH,
23830   IX86_BUILTIN_VPMACSDQL,
23831   IX86_BUILTIN_VPMACSDQH,
23832   IX86_BUILTIN_VPMADCSSWD,
23833   IX86_BUILTIN_VPMADCSWD,
23834
23835   IX86_BUILTIN_VPHADDBW,
23836   IX86_BUILTIN_VPHADDBD,
23837   IX86_BUILTIN_VPHADDBQ,
23838   IX86_BUILTIN_VPHADDWD,
23839   IX86_BUILTIN_VPHADDWQ,
23840   IX86_BUILTIN_VPHADDDQ,
23841   IX86_BUILTIN_VPHADDUBW,
23842   IX86_BUILTIN_VPHADDUBD,
23843   IX86_BUILTIN_VPHADDUBQ,
23844   IX86_BUILTIN_VPHADDUWD,
23845   IX86_BUILTIN_VPHADDUWQ,
23846   IX86_BUILTIN_VPHADDUDQ,
23847   IX86_BUILTIN_VPHSUBBW,
23848   IX86_BUILTIN_VPHSUBWD,
23849   IX86_BUILTIN_VPHSUBDQ,
23850
23851   IX86_BUILTIN_VPROTB,
23852   IX86_BUILTIN_VPROTW,
23853   IX86_BUILTIN_VPROTD,
23854   IX86_BUILTIN_VPROTQ,
23855   IX86_BUILTIN_VPROTB_IMM,
23856   IX86_BUILTIN_VPROTW_IMM,
23857   IX86_BUILTIN_VPROTD_IMM,
23858   IX86_BUILTIN_VPROTQ_IMM,
23859
23860   IX86_BUILTIN_VPSHLB,
23861   IX86_BUILTIN_VPSHLW,
23862   IX86_BUILTIN_VPSHLD,
23863   IX86_BUILTIN_VPSHLQ,
23864   IX86_BUILTIN_VPSHAB,
23865   IX86_BUILTIN_VPSHAW,
23866   IX86_BUILTIN_VPSHAD,
23867   IX86_BUILTIN_VPSHAQ,
23868
23869   IX86_BUILTIN_VFRCZSS,
23870   IX86_BUILTIN_VFRCZSD,
23871   IX86_BUILTIN_VFRCZPS,
23872   IX86_BUILTIN_VFRCZPD,
23873   IX86_BUILTIN_VFRCZPS256,
23874   IX86_BUILTIN_VFRCZPD256,
23875
23876   IX86_BUILTIN_VPCOMEQUB,
23877   IX86_BUILTIN_VPCOMNEUB,
23878   IX86_BUILTIN_VPCOMLTUB,
23879   IX86_BUILTIN_VPCOMLEUB,
23880   IX86_BUILTIN_VPCOMGTUB,
23881   IX86_BUILTIN_VPCOMGEUB,
23882   IX86_BUILTIN_VPCOMFALSEUB,
23883   IX86_BUILTIN_VPCOMTRUEUB,
23884
23885   IX86_BUILTIN_VPCOMEQUW,
23886   IX86_BUILTIN_VPCOMNEUW,
23887   IX86_BUILTIN_VPCOMLTUW,
23888   IX86_BUILTIN_VPCOMLEUW,
23889   IX86_BUILTIN_VPCOMGTUW,
23890   IX86_BUILTIN_VPCOMGEUW,
23891   IX86_BUILTIN_VPCOMFALSEUW,
23892   IX86_BUILTIN_VPCOMTRUEUW,
23893
23894   IX86_BUILTIN_VPCOMEQUD,
23895   IX86_BUILTIN_VPCOMNEUD,
23896   IX86_BUILTIN_VPCOMLTUD,
23897   IX86_BUILTIN_VPCOMLEUD,
23898   IX86_BUILTIN_VPCOMGTUD,
23899   IX86_BUILTIN_VPCOMGEUD,
23900   IX86_BUILTIN_VPCOMFALSEUD,
23901   IX86_BUILTIN_VPCOMTRUEUD,
23902
23903   IX86_BUILTIN_VPCOMEQUQ,
23904   IX86_BUILTIN_VPCOMNEUQ,
23905   IX86_BUILTIN_VPCOMLTUQ,
23906   IX86_BUILTIN_VPCOMLEUQ,
23907   IX86_BUILTIN_VPCOMGTUQ,
23908   IX86_BUILTIN_VPCOMGEUQ,
23909   IX86_BUILTIN_VPCOMFALSEUQ,
23910   IX86_BUILTIN_VPCOMTRUEUQ,
23911
23912   IX86_BUILTIN_VPCOMEQB,
23913   IX86_BUILTIN_VPCOMNEB,
23914   IX86_BUILTIN_VPCOMLTB,
23915   IX86_BUILTIN_VPCOMLEB,
23916   IX86_BUILTIN_VPCOMGTB,
23917   IX86_BUILTIN_VPCOMGEB,
23918   IX86_BUILTIN_VPCOMFALSEB,
23919   IX86_BUILTIN_VPCOMTRUEB,
23920
23921   IX86_BUILTIN_VPCOMEQW,
23922   IX86_BUILTIN_VPCOMNEW,
23923   IX86_BUILTIN_VPCOMLTW,
23924   IX86_BUILTIN_VPCOMLEW,
23925   IX86_BUILTIN_VPCOMGTW,
23926   IX86_BUILTIN_VPCOMGEW,
23927   IX86_BUILTIN_VPCOMFALSEW,
23928   IX86_BUILTIN_VPCOMTRUEW,
23929
23930   IX86_BUILTIN_VPCOMEQD,
23931   IX86_BUILTIN_VPCOMNED,
23932   IX86_BUILTIN_VPCOMLTD,
23933   IX86_BUILTIN_VPCOMLED,
23934   IX86_BUILTIN_VPCOMGTD,
23935   IX86_BUILTIN_VPCOMGED,
23936   IX86_BUILTIN_VPCOMFALSED,
23937   IX86_BUILTIN_VPCOMTRUED,
23938
23939   IX86_BUILTIN_VPCOMEQQ,
23940   IX86_BUILTIN_VPCOMNEQ,
23941   IX86_BUILTIN_VPCOMLTQ,
23942   IX86_BUILTIN_VPCOMLEQ,
23943   IX86_BUILTIN_VPCOMGTQ,
23944   IX86_BUILTIN_VPCOMGEQ,
23945   IX86_BUILTIN_VPCOMFALSEQ,
23946   IX86_BUILTIN_VPCOMTRUEQ,
23947
23948   /* LWP instructions.  */
23949   IX86_BUILTIN_LLWPCB,
23950   IX86_BUILTIN_SLWPCB,
23951   IX86_BUILTIN_LWPVAL32,
23952   IX86_BUILTIN_LWPVAL64,
23953   IX86_BUILTIN_LWPINS32,
23954   IX86_BUILTIN_LWPINS64,
23955
23956   IX86_BUILTIN_CLZS,
23957
23958   /* FSGSBASE instructions.  */
23959   IX86_BUILTIN_RDFSBASE32,
23960   IX86_BUILTIN_RDFSBASE64,
23961   IX86_BUILTIN_RDGSBASE32,
23962   IX86_BUILTIN_RDGSBASE64,
23963   IX86_BUILTIN_WRFSBASE32,
23964   IX86_BUILTIN_WRFSBASE64,
23965   IX86_BUILTIN_WRGSBASE32,
23966   IX86_BUILTIN_WRGSBASE64,
23967
23968   /* RDRND instructions.  */
23969   IX86_BUILTIN_RDRAND16,
23970   IX86_BUILTIN_RDRAND32,
23971   IX86_BUILTIN_RDRAND64,
23972
23973   /* F16C instructions.  */
23974   IX86_BUILTIN_CVTPH2PS,
23975   IX86_BUILTIN_CVTPH2PS256,
23976   IX86_BUILTIN_CVTPS2PH,
23977   IX86_BUILTIN_CVTPS2PH256,
23978
23979   IX86_BUILTIN_MAX
23980 };
23981
23982 /* Table for the ix86 builtin decls.  */
23983 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
23984
23985 /* Table of all of the builtin functions that are possible with different ISA's
23986    but are waiting to be built until a function is declared to use that
23987    ISA.  */
23988 struct builtin_isa {
23989   const char *name;             /* function name */
23990   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
23991   int isa;                      /* isa_flags this builtin is defined for */
23992   bool const_p;                 /* true if the declaration is constant */
23993   bool set_and_not_built_p;
23994 };
23995
23996 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
23997
23998
23999 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
24000    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
24001    function decl in the ix86_builtins array.  Returns the function decl or
24002    NULL_TREE, if the builtin was not added.
24003
24004    If the front end has a special hook for builtin functions, delay adding
24005    builtin functions that aren't in the current ISA until the ISA is changed
24006    with function specific optimization.  Doing so, can save about 300K for the
24007    default compiler.  When the builtin is expanded, check at that time whether
24008    it is valid.
24009
24010    If the front end doesn't have a special hook, record all builtins, even if
24011    it isn't an instruction set in the current ISA in case the user uses
24012    function specific options for a different ISA, so that we don't get scope
24013    errors if a builtin is added in the middle of a function scope.  */
24014
24015 static inline tree
24016 def_builtin (int mask, const char *name, enum ix86_builtin_func_type tcode,
24017              enum ix86_builtins code)
24018 {
24019   tree decl = NULL_TREE;
24020
24021   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
24022     {
24023       ix86_builtins_isa[(int) code].isa = mask;
24024
24025       mask &= ~OPTION_MASK_ISA_64BIT;
24026       if (mask == 0
24027           || (mask & ix86_isa_flags) != 0
24028           || (lang_hooks.builtin_function
24029               == lang_hooks.builtin_function_ext_scope))
24030
24031         {
24032           tree type = ix86_get_builtin_func_type (tcode);
24033           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
24034                                        NULL, NULL_TREE);
24035           ix86_builtins[(int) code] = decl;
24036           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
24037         }
24038       else
24039         {
24040           ix86_builtins[(int) code] = NULL_TREE;
24041           ix86_builtins_isa[(int) code].tcode = tcode;
24042           ix86_builtins_isa[(int) code].name = name;
24043           ix86_builtins_isa[(int) code].const_p = false;
24044           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
24045         }
24046     }
24047
24048   return decl;
24049 }
24050
24051 /* Like def_builtin, but also marks the function decl "const".  */
24052
24053 static inline tree
24054 def_builtin_const (int mask, const char *name,
24055                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
24056 {
24057   tree decl = def_builtin (mask, name, tcode, code);
24058   if (decl)
24059     TREE_READONLY (decl) = 1;
24060   else
24061     ix86_builtins_isa[(int) code].const_p = true;
24062
24063   return decl;
24064 }
24065
24066 /* Add any new builtin functions for a given ISA that may not have been
24067    declared.  This saves a bit of space compared to adding all of the
24068    declarations to the tree, even if we didn't use them.  */
24069
24070 static void
24071 ix86_add_new_builtins (int isa)
24072 {
24073   int i;
24074
24075   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
24076     {
24077       if ((ix86_builtins_isa[i].isa & isa) != 0
24078           && ix86_builtins_isa[i].set_and_not_built_p)
24079         {
24080           tree decl, type;
24081
24082           /* Don't define the builtin again.  */
24083           ix86_builtins_isa[i].set_and_not_built_p = false;
24084
24085           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
24086           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
24087                                                  type, i, BUILT_IN_MD, NULL,
24088                                                  NULL_TREE);
24089
24090           ix86_builtins[i] = decl;
24091           if (ix86_builtins_isa[i].const_p)
24092             TREE_READONLY (decl) = 1;
24093         }
24094     }
24095 }
24096
24097 /* Bits for builtin_description.flag.  */
24098
24099 /* Set when we don't support the comparison natively, and should
24100    swap_comparison in order to support it.  */
24101 #define BUILTIN_DESC_SWAP_OPERANDS      1
24102
24103 struct builtin_description
24104 {
24105   const unsigned int mask;
24106   const enum insn_code icode;
24107   const char *const name;
24108   const enum ix86_builtins code;
24109   const enum rtx_code comparison;
24110   const int flag;
24111 };
24112
24113 static const struct builtin_description bdesc_comi[] =
24114 {
24115   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
24116   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
24117   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
24118   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
24119   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
24120   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
24121   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
24122   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
24123   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
24124   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
24125   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
24126   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
24127   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
24128   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
24129   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
24130   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
24131   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
24132   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
24133   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
24134   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
24135   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
24136   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
24137   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
24138   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
24139 };
24140
24141 static const struct builtin_description bdesc_pcmpestr[] =
24142 {
24143   /* SSE4.2 */
24144   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
24145   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
24146   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
24147   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
24148   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
24149   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
24150   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
24151 };
24152
24153 static const struct builtin_description bdesc_pcmpistr[] =
24154 {
24155   /* SSE4.2 */
24156   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
24157   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
24158   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
24159   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
24160   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
24161   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
24162   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
24163 };
24164
24165 /* Special builtins with variable number of arguments.  */
24166 static const struct builtin_description bdesc_special_args[] =
24167 {
24168   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
24169   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
24170
24171   /* MMX */
24172   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
24173
24174   /* 3DNow! */
24175   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
24176
24177   /* SSE */
24178   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
24179   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
24180   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
24181
24182   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
24183   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
24184   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
24185   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
24186
24187   /* SSE or 3DNow!A  */
24188   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
24189   { 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 },
24190
24191   /* SSE2 */
24192   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
24193   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
24194   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
24195   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
24196   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
24197   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
24198   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntsi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
24199   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
24200   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
24201
24202   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
24203   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
24204
24205   /* SSE3 */
24206   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
24207
24208   /* SSE4.1 */
24209   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
24210
24211   /* SSE4A */
24212   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
24213   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
24214
24215   /* AVX */
24216   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
24217   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
24218
24219   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
24220   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
24221   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
24222   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
24223   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
24224
24225   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
24226   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
24227   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
24228   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
24229   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
24230   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
24231   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
24232
24233   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
24234   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
24235   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
24236
24237   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DF },
24238   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SF },
24239   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DF },
24240   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SF },
24241   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DF_V2DF },
24242   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SF_V4SF },
24243   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DF_V4DF },
24244   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SF_V8SF },
24245
24246   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
24247   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
24248   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
24249   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
24250   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
24251   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
24252
24253   /* FSGSBASE */
24254   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasesi, "__builtin_ia32_rdfsbase32", IX86_BUILTIN_RDFSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
24255   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasedi, "__builtin_ia32_rdfsbase64", IX86_BUILTIN_RDFSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
24256   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasesi, "__builtin_ia32_rdgsbase32", IX86_BUILTIN_RDGSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
24257   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasedi, "__builtin_ia32_rdgsbase64", IX86_BUILTIN_RDGSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
24258   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasesi, "__builtin_ia32_wrfsbase32", IX86_BUILTIN_WRFSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
24259   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasedi, "__builtin_ia32_wrfsbase64", IX86_BUILTIN_WRFSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
24260   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasesi, "__builtin_ia32_wrgsbase32", IX86_BUILTIN_WRGSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
24261   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasedi, "__builtin_ia32_wrgsbase64", IX86_BUILTIN_WRGSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
24262
24263   /* RDRND */
24264   { OPTION_MASK_ISA_RDRND, CODE_FOR_rdrandhi, "__builtin_ia32_rdrand16", IX86_BUILTIN_RDRAND16, UNKNOWN, (int) UINT16_FTYPE_VOID },
24265   { OPTION_MASK_ISA_RDRND, CODE_FOR_rdrandsi, "__builtin_ia32_rdrand32", IX86_BUILTIN_RDRAND32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
24266   { OPTION_MASK_ISA_RDRND | OPTION_MASK_ISA_64BIT, CODE_FOR_rdranddi, "__builtin_ia32_rdrand64", IX86_BUILTIN_RDRAND64, UNKNOWN, (int) UINT64_FTYPE_VOID },
24267 };
24268
24269 /* Builtins with variable number of arguments.  */
24270 static const struct builtin_description bdesc_args[] =
24271 {
24272   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
24273   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
24274   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
24275   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
24276   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
24277   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
24278   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
24279
24280   /* MMX */
24281   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24282   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24283   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24284   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24285   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24286   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24287
24288   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24289   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24290   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24291   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24292   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24293   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24294   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24295   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24296
24297   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24298   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24299
24300   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24301   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24302   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24303   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24304
24305   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24306   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24307   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24308   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24309   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24310   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24311
24312   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24313   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24314   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24315   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24316   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
24317   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
24318
24319   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
24320   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
24321   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
24322
24323   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
24324
24325   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
24326   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
24327   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
24328   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
24329   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
24330   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
24331
24332   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
24333   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
24334   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
24335   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
24336   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
24337   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
24338
24339   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
24340   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
24341   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
24342   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
24343
24344   /* 3DNow! */
24345   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
24346   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
24347   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
24348   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
24349
24350   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24351   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24352   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24353   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
24354   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
24355   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
24356   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24357   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24358   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24359   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24360   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24361   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24362   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24363   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24364   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24365
24366   /* 3DNow!A */
24367   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
24368   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
24369   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
24370   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
24371   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24372   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
24373
24374   /* SSE */
24375   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
24376   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24377   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24378   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24379   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24380   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24381   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
24382   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
24383   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
24384   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
24385   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
24386   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
24387
24388   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24389
24390   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24391   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24392   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24393   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24394   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24395   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24396   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24397   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24398
24399   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
24400   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
24401   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
24402   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24403   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24404   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24405   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
24406   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
24407   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
24408   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24409   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
24410   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24411   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
24412   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
24413   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
24414   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24415   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
24416   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
24417   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
24418   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24419   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
24420   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
24421
24422   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24423   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24424   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24425   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24426
24427   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24428   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24429   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24430   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24431
24432   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24433
24434   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24435   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24436   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24437   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24438   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24439
24440   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
24441   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
24442   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
24443
24444   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
24445
24446   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
24447   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
24448   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
24449
24450   /* SSE MMX or 3Dnow!A */
24451   { 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 },
24452   { 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 },
24453   { 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 },
24454
24455   { 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 },
24456   { 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 },
24457   { 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 },
24458   { 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 },
24459
24460   { 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 },
24461   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
24462
24463   { 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 },
24464
24465   /* SSE2 */
24466   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24467
24468   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2df", IX86_BUILTIN_VEC_PERM_V2DF, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DI },
24469   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4sf", IX86_BUILTIN_VEC_PERM_V4SF, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SI },
24470   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2di", IX86_BUILTIN_VEC_PERM_V2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_V2DI },
24471   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4si", IX86_BUILTIN_VEC_PERM_V4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_V4SI },
24472   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8hi", IX86_BUILTIN_VEC_PERM_V8HI, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_V8HI },
24473   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v16qi", IX86_BUILTIN_VEC_PERM_V16QI, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
24474   { 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 },
24475   { 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 },
24476   { 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 },
24477   { 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 },
24478   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4df", IX86_BUILTIN_VEC_PERM_V4DF, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DI },
24479   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8sf", IX86_BUILTIN_VEC_PERM_V8SF, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SI },
24480
24481   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
24482   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
24483   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
24484   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
24485   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2ps, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
24486   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtudq2ps, "__builtin_ia32_cvtudq2ps", IX86_BUILTIN_CVTUDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
24487
24488   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
24489   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
24490   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
24491   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
24492   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
24493
24494   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
24495
24496   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
24497   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
24498   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
24499   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
24500
24501   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
24502   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
24503   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttps2dq, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
24504
24505   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24506   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24507   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24508   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24509   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24510   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24511   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24512   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24513
24514   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
24515   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
24516   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
24517   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
24518   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
24519   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24520   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
24521   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
24522   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
24523   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
24524   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
24525   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24526   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
24527   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
24528   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
24529   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24530   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
24531   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
24532   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
24533   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
24534
24535   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24536   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24537   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24538   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24539
24540   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24541   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24542   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24543   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24544
24545   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24546
24547   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24548   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24549   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24550
24551   { 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 },
24552
24553   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24554   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24555   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24556   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24557   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24558   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24559   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24560   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24561
24562   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24563   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24564   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24565   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24566   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24567   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24568   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24569   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24570
24571   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24572   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
24573
24574   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24575   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24576   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24577   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24578
24579   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24580   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24581
24582   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24583   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24584   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
24585   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24586   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24587   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
24588
24589   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24590   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24591   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24592   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24593
24594   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24595   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
24596   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
24597   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24598   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24599   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24600   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24601   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24602
24603   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
24604   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
24605   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
24606
24607   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24608   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
24609
24610   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
24611   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
24612
24613   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
24614
24615   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
24616   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
24617   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
24618   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
24619
24620   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
24621   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
24622   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
24623   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
24624   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
24625   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
24626   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
24627
24628   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
24629   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
24630   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
24631   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
24632   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
24633   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
24634   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
24635
24636   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
24637   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
24638   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
24639   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
24640
24641   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
24642   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
24643   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
24644
24645   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
24646
24647   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
24648   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
24649
24650   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
24651
24652   /* SSE2 MMX */
24653   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
24654   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
24655
24656   /* SSE3 */
24657   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
24658   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
24659
24660   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24661   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24662   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24663   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24664   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
24665   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
24666
24667   /* SSSE3 */
24668   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
24669   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
24670   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
24671   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
24672   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
24673   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
24674
24675   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24676   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24677   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24678   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24679   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24680   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24681   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24682   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24683   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24684   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24685   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24686   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24687   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
24688   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
24689   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24690   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24691   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24692   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24693   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24694   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
24695   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24696   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
24697   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24698   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
24699
24700   /* SSSE3.  */
24701   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
24702   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
24703
24704   /* SSE4.1 */
24705   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24706   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24707   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
24708   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
24709   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24710   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24711   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24712   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
24713   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
24714   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
24715
24716   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
24717   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
24718   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
24719   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
24720   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
24721   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
24722   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
24723   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
24724   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
24725   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
24726   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
24727   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
24728   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
24729
24730   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
24731   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24732   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24733   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24734   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24735   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24736   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
24737   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24738   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24739   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
24740   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
24741   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
24742
24743   /* SSE4.1 */
24744   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
24745   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
24746   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24747   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24748
24749   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
24750   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
24751   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
24752
24753   /* SSE4.2 */
24754   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24755   { 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 },
24756   { 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 },
24757   { 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 },
24758   { 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 },
24759
24760   /* SSE4A */
24761   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
24762   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
24763   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
24764   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24765
24766   /* AES */
24767   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
24768   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
24769
24770   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24771   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24772   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24773   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
24774
24775   /* PCLMUL */
24776   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
24777
24778   /* AVX */
24779   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24780   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24781   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24782   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24783   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24784   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24785   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24786   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24787   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24788   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24789   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24790   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24791   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24792   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24793   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24794   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24795   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24796   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24797   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24798   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24799   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24800   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24801   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24802   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24803   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24804   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24805
24806   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
24807   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
24808   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
24809   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
24810
24811   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
24812   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
24813   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
24814   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
24815   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
24816   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
24817   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
24818   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpsdv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24819   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpssv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24820   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
24821   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
24822   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
24823   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
24824   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
24825   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
24826   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
24827   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2pd256, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
24828   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2ps256, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
24829   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
24830   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
24831   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
24832   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttpd2dq256, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
24833   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
24834   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttps2dq256, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
24835   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
24836   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
24837   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
24838   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
24839   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
24840   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
24841   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
24842   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
24843   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
24844   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
24845
24846   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24847   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24848   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
24849
24850   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
24851   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24852   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24853   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24854   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24855
24856   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
24857
24858   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
24859   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
24860
24861   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24862   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24863   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24864   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24865
24866   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
24867   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
24868   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
24869   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8si, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
24870   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8sf, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
24871   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v4df, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
24872
24873   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
24874   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
24875   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
24876   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
24877   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
24878   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
24879   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
24880   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
24881   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
24882   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
24883   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
24884   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
24885   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
24886   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
24887   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
24888
24889   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
24890   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
24891
24892   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv8sf3,  "__builtin_ia32_copysignps256", IX86_BUILTIN_CPYSGNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
24893   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv4df3,  "__builtin_ia32_copysignpd256", IX86_BUILTIN_CPYSGNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
24894
24895   { OPTION_MASK_ISA_ABM, CODE_FOR_clzhi2_abm,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
24896
24897   /* F16C */
24898   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps, "__builtin_ia32_vcvtph2ps", IX86_BUILTIN_CVTPH2PS, UNKNOWN, (int) V4SF_FTYPE_V8HI },
24899   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps256, "__builtin_ia32_vcvtph2ps256", IX86_BUILTIN_CVTPH2PS256, UNKNOWN, (int) V8SF_FTYPE_V8HI },
24900   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph, "__builtin_ia32_vcvtps2ph", IX86_BUILTIN_CVTPS2PH, UNKNOWN, (int) V8HI_FTYPE_V4SF_INT },
24901   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph256, "__builtin_ia32_vcvtps2ph256", IX86_BUILTIN_CVTPS2PH256, UNKNOWN, (int) V8HI_FTYPE_V8SF_INT },
24902 };
24903
24904 /* FMA4 and XOP.  */
24905 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
24906 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
24907 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
24908 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
24909 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
24910 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
24911 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
24912 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
24913 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
24914 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
24915 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
24916 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
24917 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
24918 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
24919 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
24920 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
24921 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
24922 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
24923 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
24924 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
24925 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
24926 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
24927 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
24928 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
24929 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
24930 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
24931 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
24932 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
24933 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
24934 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
24935 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
24936 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
24937 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
24938 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
24939 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
24940 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
24941 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
24942 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
24943 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
24944 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
24945 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
24946 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
24947 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
24948 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
24949 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
24950 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
24951 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
24952 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
24953 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
24954 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
24955 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
24956 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
24957
24958 static const struct builtin_description bdesc_multi_arg[] =
24959 {
24960   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v4sf,
24961     "__builtin_ia32_vfmaddss", IX86_BUILTIN_VFMADDSS,
24962     UNKNOWN, (int)MULTI_ARG_3_SF },
24963   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v2df,
24964     "__builtin_ia32_vfmaddsd", IX86_BUILTIN_VFMADDSD,
24965     UNKNOWN, (int)MULTI_ARG_3_DF },
24966
24967   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4sf,
24968     "__builtin_ia32_vfmaddps", IX86_BUILTIN_VFMADDPS,
24969     UNKNOWN, (int)MULTI_ARG_3_SF },
24970   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v2df,
24971     "__builtin_ia32_vfmaddpd", IX86_BUILTIN_VFMADDPD,
24972     UNKNOWN, (int)MULTI_ARG_3_DF },
24973   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v8sf,
24974     "__builtin_ia32_vfmaddps256", IX86_BUILTIN_VFMADDPS256,
24975     UNKNOWN, (int)MULTI_ARG_3_SF2 },
24976   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4df,
24977     "__builtin_ia32_vfmaddpd256", IX86_BUILTIN_VFMADDPD256,
24978     UNKNOWN, (int)MULTI_ARG_3_DF2 },
24979
24980   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4sf,
24981     "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,
24982     UNKNOWN, (int)MULTI_ARG_3_SF },
24983   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v2df,
24984     "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,
24985     UNKNOWN, (int)MULTI_ARG_3_DF },
24986   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v8sf,
24987     "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,
24988     UNKNOWN, (int)MULTI_ARG_3_SF2 },
24989   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4df,
24990     "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,
24991     UNKNOWN, (int)MULTI_ARG_3_DF2 },
24992
24993   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
24994   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
24995   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
24996   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
24997   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
24998   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
24999   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
25000
25001   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
25002   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
25003   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
25004   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
25005   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
25006   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
25007   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
25008
25009   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
25010
25011   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
25012   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
25013   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25014   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25015   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
25016   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
25017   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25018   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25019   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25020   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
25021   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25022   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
25023
25024   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
25025   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
25026   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
25027   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
25028   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
25029   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
25030   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
25031   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
25032   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
25033   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
25034   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
25035   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
25036   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
25037   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
25038   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
25039   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
25040
25041   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_2_SF },
25042   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_2_DF },
25043   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
25044   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
25045   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
25046   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
25047
25048   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
25049   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
25050   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
25051   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
25052   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
25053   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
25054   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
25055   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
25056   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
25057   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
25058   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
25059   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
25060   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
25061   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
25062   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
25063
25064   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
25065   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
25066   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
25067   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
25068   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
25069   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
25070   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
25071
25072   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
25073   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
25074   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
25075   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
25076   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
25077   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
25078   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
25079
25080   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
25081   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
25082   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
25083   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
25084   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
25085   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
25086   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
25087
25088   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
25089   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
25090   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
25091   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
25092   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
25093   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
25094   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
25095
25096   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
25097   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
25098   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
25099   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
25100   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
25101   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
25102   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
25103
25104   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
25105   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
25106   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
25107   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
25108   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
25109   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
25110   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
25111
25112   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
25113   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
25114   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
25115   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
25116   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
25117   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
25118   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
25119
25120   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
25121   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
25122   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
25123   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
25124   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
25125   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
25126   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
25127
25128   { 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 },
25129   { 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 },
25130   { 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 },
25131   { 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 },
25132   { 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 },
25133   { 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 },
25134   { 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 },
25135   { 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 },
25136
25137   { 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 },
25138   { 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 },
25139   { 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 },
25140   { 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 },
25141   { 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 },
25142   { 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 },
25143   { 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 },
25144   { 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 },
25145
25146   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
25147   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
25148   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
25149   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
25150
25151 };
25152
25153 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
25154    in the current target ISA to allow the user to compile particular modules
25155    with different target specific options that differ from the command line
25156    options.  */
25157 static void
25158 ix86_init_mmx_sse_builtins (void)
25159 {
25160   const struct builtin_description * d;
25161   enum ix86_builtin_func_type ftype;
25162   size_t i;
25163
25164   /* Add all special builtins with variable number of operands.  */
25165   for (i = 0, d = bdesc_special_args;
25166        i < ARRAY_SIZE (bdesc_special_args);
25167        i++, d++)
25168     {
25169       if (d->name == 0)
25170         continue;
25171
25172       ftype = (enum ix86_builtin_func_type) d->flag;
25173       def_builtin (d->mask, d->name, ftype, d->code);
25174     }
25175
25176   /* Add all builtins with variable number of operands.  */
25177   for (i = 0, d = bdesc_args;
25178        i < ARRAY_SIZE (bdesc_args);
25179        i++, d++)
25180     {
25181       if (d->name == 0)
25182         continue;
25183
25184       ftype = (enum ix86_builtin_func_type) d->flag;
25185       def_builtin_const (d->mask, d->name, ftype, d->code);
25186     }
25187
25188   /* pcmpestr[im] insns.  */
25189   for (i = 0, d = bdesc_pcmpestr;
25190        i < ARRAY_SIZE (bdesc_pcmpestr);
25191        i++, d++)
25192     {
25193       if (d->code == IX86_BUILTIN_PCMPESTRM128)
25194         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
25195       else
25196         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
25197       def_builtin_const (d->mask, d->name, ftype, d->code);
25198     }
25199
25200   /* pcmpistr[im] insns.  */
25201   for (i = 0, d = bdesc_pcmpistr;
25202        i < ARRAY_SIZE (bdesc_pcmpistr);
25203        i++, d++)
25204     {
25205       if (d->code == IX86_BUILTIN_PCMPISTRM128)
25206         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
25207       else
25208         ftype = INT_FTYPE_V16QI_V16QI_INT;
25209       def_builtin_const (d->mask, d->name, ftype, d->code);
25210     }
25211
25212   /* comi/ucomi insns.  */
25213   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
25214     {
25215       if (d->mask == OPTION_MASK_ISA_SSE2)
25216         ftype = INT_FTYPE_V2DF_V2DF;
25217       else
25218         ftype = INT_FTYPE_V4SF_V4SF;
25219       def_builtin_const (d->mask, d->name, ftype, d->code);
25220     }
25221
25222   /* SSE */
25223   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
25224                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
25225   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
25226                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
25227
25228   /* SSE or 3DNow!A */
25229   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
25230                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
25231                IX86_BUILTIN_MASKMOVQ);
25232
25233   /* SSE2 */
25234   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
25235                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
25236
25237   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
25238                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
25239   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
25240                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
25241
25242   /* SSE3.  */
25243   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
25244                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
25245   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
25246                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
25247
25248   /* AES */
25249   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
25250                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
25251   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
25252                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
25253   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
25254                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
25255   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
25256                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
25257   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
25258                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
25259   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
25260                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
25261
25262   /* PCLMUL */
25263   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
25264                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
25265
25266   /* MMX access to the vec_init patterns.  */
25267   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
25268                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
25269
25270   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
25271                      V4HI_FTYPE_HI_HI_HI_HI,
25272                      IX86_BUILTIN_VEC_INIT_V4HI);
25273
25274   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
25275                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
25276                      IX86_BUILTIN_VEC_INIT_V8QI);
25277
25278   /* Access to the vec_extract patterns.  */
25279   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
25280                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
25281   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
25282                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
25283   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
25284                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
25285   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
25286                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
25287   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
25288                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
25289
25290   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
25291                      "__builtin_ia32_vec_ext_v4hi",
25292                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
25293
25294   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
25295                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
25296
25297   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
25298                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
25299
25300   /* Access to the vec_set patterns.  */
25301   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
25302                      "__builtin_ia32_vec_set_v2di",
25303                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
25304
25305   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
25306                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
25307
25308   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
25309                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
25310
25311   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
25312                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
25313
25314   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
25315                      "__builtin_ia32_vec_set_v4hi",
25316                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
25317
25318   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
25319                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
25320
25321   /* Add FMA4 multi-arg argument instructions */
25322   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
25323     {
25324       if (d->name == 0)
25325         continue;
25326
25327       ftype = (enum ix86_builtin_func_type) d->flag;
25328       def_builtin_const (d->mask, d->name, ftype, d->code);
25329     }
25330 }
25331
25332 /* Internal method for ix86_init_builtins.  */
25333
25334 static void
25335 ix86_init_builtins_va_builtins_abi (void)
25336 {
25337   tree ms_va_ref, sysv_va_ref;
25338   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
25339   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
25340   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
25341   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
25342
25343   if (!TARGET_64BIT)
25344     return;
25345   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
25346   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
25347   ms_va_ref = build_reference_type (ms_va_list_type_node);
25348   sysv_va_ref =
25349     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
25350
25351   fnvoid_va_end_ms =
25352     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
25353   fnvoid_va_start_ms =
25354     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
25355   fnvoid_va_end_sysv =
25356     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
25357   fnvoid_va_start_sysv =
25358     build_varargs_function_type_list (void_type_node, sysv_va_ref,
25359                                        NULL_TREE);
25360   fnvoid_va_copy_ms =
25361     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
25362                               NULL_TREE);
25363   fnvoid_va_copy_sysv =
25364     build_function_type_list (void_type_node, sysv_va_ref,
25365                               sysv_va_ref, NULL_TREE);
25366
25367   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
25368                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
25369   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
25370                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
25371   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
25372                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
25373   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
25374                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
25375   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
25376                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
25377   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
25378                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
25379 }
25380
25381 static void
25382 ix86_init_builtin_types (void)
25383 {
25384   tree float128_type_node, float80_type_node;
25385
25386   /* The __float80 type.  */
25387   float80_type_node = long_double_type_node;
25388   if (TYPE_MODE (float80_type_node) != XFmode)
25389     {
25390       /* The __float80 type.  */
25391       float80_type_node = make_node (REAL_TYPE);
25392
25393       TYPE_PRECISION (float80_type_node) = 80;
25394       layout_type (float80_type_node);
25395     }
25396   lang_hooks.types.register_builtin_type (float80_type_node, "__float80");
25397
25398   /* The __float128 type.  */
25399   float128_type_node = make_node (REAL_TYPE);
25400   TYPE_PRECISION (float128_type_node) = 128;
25401   layout_type (float128_type_node);
25402   lang_hooks.types.register_builtin_type (float128_type_node, "__float128");
25403
25404   /* This macro is built by i386-builtin-types.awk.  */
25405   DEFINE_BUILTIN_PRIMITIVE_TYPES;
25406 }
25407
25408 static void
25409 ix86_init_builtins (void)
25410 {
25411   tree t;
25412
25413   ix86_init_builtin_types ();
25414
25415   /* TFmode support builtins.  */
25416   def_builtin_const (0, "__builtin_infq",
25417                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
25418   def_builtin_const (0, "__builtin_huge_valq",
25419                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
25420
25421   /* We will expand them to normal call if SSE2 isn't available since
25422      they are used by libgcc. */
25423   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
25424   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
25425                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
25426   TREE_READONLY (t) = 1;
25427   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
25428
25429   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
25430   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
25431                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
25432   TREE_READONLY (t) = 1;
25433   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
25434
25435   ix86_init_mmx_sse_builtins ();
25436
25437   if (TARGET_64BIT)
25438     ix86_init_builtins_va_builtins_abi ();
25439
25440 #ifdef SUBTARGET_INIT_BUILTINS
25441   SUBTARGET_INIT_BUILTINS;
25442 #endif
25443 }
25444
25445 /* Return the ix86 builtin for CODE.  */
25446
25447 static tree
25448 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
25449 {
25450   if (code >= IX86_BUILTIN_MAX)
25451     return error_mark_node;
25452
25453   return ix86_builtins[code];
25454 }
25455
25456 /* Errors in the source file can cause expand_expr to return const0_rtx
25457    where we expect a vector.  To avoid crashing, use one of the vector
25458    clear instructions.  */
25459 static rtx
25460 safe_vector_operand (rtx x, enum machine_mode mode)
25461 {
25462   if (x == const0_rtx)
25463     x = CONST0_RTX (mode);
25464   return x;
25465 }
25466
25467 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
25468
25469 static rtx
25470 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
25471 {
25472   rtx pat;
25473   tree arg0 = CALL_EXPR_ARG (exp, 0);
25474   tree arg1 = CALL_EXPR_ARG (exp, 1);
25475   rtx op0 = expand_normal (arg0);
25476   rtx op1 = expand_normal (arg1);
25477   enum machine_mode tmode = insn_data[icode].operand[0].mode;
25478   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
25479   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
25480
25481   if (VECTOR_MODE_P (mode0))
25482     op0 = safe_vector_operand (op0, mode0);
25483   if (VECTOR_MODE_P (mode1))
25484     op1 = safe_vector_operand (op1, mode1);
25485
25486   if (optimize || !target
25487       || GET_MODE (target) != tmode
25488       || !insn_data[icode].operand[0].predicate (target, tmode))
25489     target = gen_reg_rtx (tmode);
25490
25491   if (GET_MODE (op1) == SImode && mode1 == TImode)
25492     {
25493       rtx x = gen_reg_rtx (V4SImode);
25494       emit_insn (gen_sse2_loadd (x, op1));
25495       op1 = gen_lowpart (TImode, x);
25496     }
25497
25498   if (!insn_data[icode].operand[1].predicate (op0, mode0))
25499     op0 = copy_to_mode_reg (mode0, op0);
25500   if (!insn_data[icode].operand[2].predicate (op1, mode1))
25501     op1 = copy_to_mode_reg (mode1, op1);
25502
25503   pat = GEN_FCN (icode) (target, op0, op1);
25504   if (! pat)
25505     return 0;
25506
25507   emit_insn (pat);
25508
25509   return target;
25510 }
25511
25512 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
25513
25514 static rtx
25515 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
25516                                enum ix86_builtin_func_type m_type,
25517                                enum rtx_code sub_code)
25518 {
25519   rtx pat;
25520   int i;
25521   int nargs;
25522   bool comparison_p = false;
25523   bool tf_p = false;
25524   bool last_arg_constant = false;
25525   int num_memory = 0;
25526   struct {
25527     rtx op;
25528     enum machine_mode mode;
25529   } args[4];
25530
25531   enum machine_mode tmode = insn_data[icode].operand[0].mode;
25532
25533   switch (m_type)
25534     {
25535     case MULTI_ARG_4_DF2_DI_I:
25536     case MULTI_ARG_4_DF2_DI_I1:
25537     case MULTI_ARG_4_SF2_SI_I:
25538     case MULTI_ARG_4_SF2_SI_I1:
25539       nargs = 4;
25540       last_arg_constant = true;
25541       break;
25542
25543     case MULTI_ARG_3_SF:
25544     case MULTI_ARG_3_DF:
25545     case MULTI_ARG_3_SF2:
25546     case MULTI_ARG_3_DF2:
25547     case MULTI_ARG_3_DI:
25548     case MULTI_ARG_3_SI:
25549     case MULTI_ARG_3_SI_DI:
25550     case MULTI_ARG_3_HI:
25551     case MULTI_ARG_3_HI_SI:
25552     case MULTI_ARG_3_QI:
25553     case MULTI_ARG_3_DI2:
25554     case MULTI_ARG_3_SI2:
25555     case MULTI_ARG_3_HI2:
25556     case MULTI_ARG_3_QI2:
25557       nargs = 3;
25558       break;
25559
25560     case MULTI_ARG_2_SF:
25561     case MULTI_ARG_2_DF:
25562     case MULTI_ARG_2_DI:
25563     case MULTI_ARG_2_SI:
25564     case MULTI_ARG_2_HI:
25565     case MULTI_ARG_2_QI:
25566       nargs = 2;
25567       break;
25568
25569     case MULTI_ARG_2_DI_IMM:
25570     case MULTI_ARG_2_SI_IMM:
25571     case MULTI_ARG_2_HI_IMM:
25572     case MULTI_ARG_2_QI_IMM:
25573       nargs = 2;
25574       last_arg_constant = true;
25575       break;
25576
25577     case MULTI_ARG_1_SF:
25578     case MULTI_ARG_1_DF:
25579     case MULTI_ARG_1_SF2:
25580     case MULTI_ARG_1_DF2:
25581     case MULTI_ARG_1_DI:
25582     case MULTI_ARG_1_SI:
25583     case MULTI_ARG_1_HI:
25584     case MULTI_ARG_1_QI:
25585     case MULTI_ARG_1_SI_DI:
25586     case MULTI_ARG_1_HI_DI:
25587     case MULTI_ARG_1_HI_SI:
25588     case MULTI_ARG_1_QI_DI:
25589     case MULTI_ARG_1_QI_SI:
25590     case MULTI_ARG_1_QI_HI:
25591       nargs = 1;
25592       break;
25593
25594     case MULTI_ARG_2_DI_CMP:
25595     case MULTI_ARG_2_SI_CMP:
25596     case MULTI_ARG_2_HI_CMP:
25597     case MULTI_ARG_2_QI_CMP:
25598       nargs = 2;
25599       comparison_p = true;
25600       break;
25601
25602     case MULTI_ARG_2_SF_TF:
25603     case MULTI_ARG_2_DF_TF:
25604     case MULTI_ARG_2_DI_TF:
25605     case MULTI_ARG_2_SI_TF:
25606     case MULTI_ARG_2_HI_TF:
25607     case MULTI_ARG_2_QI_TF:
25608       nargs = 2;
25609       tf_p = true;
25610       break;
25611
25612     default:
25613       gcc_unreachable ();
25614     }
25615
25616   if (optimize || !target
25617       || GET_MODE (target) != tmode
25618       || !insn_data[icode].operand[0].predicate (target, tmode))
25619     target = gen_reg_rtx (tmode);
25620
25621   gcc_assert (nargs <= 4);
25622
25623   for (i = 0; i < nargs; i++)
25624     {
25625       tree arg = CALL_EXPR_ARG (exp, i);
25626       rtx op = expand_normal (arg);
25627       int adjust = (comparison_p) ? 1 : 0;
25628       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
25629
25630       if (last_arg_constant && i == nargs-1)
25631         {
25632           if (!CONST_INT_P (op))
25633             {
25634               error ("last argument must be an immediate");
25635               return gen_reg_rtx (tmode);
25636             }
25637         }
25638       else
25639         {
25640           if (VECTOR_MODE_P (mode))
25641             op = safe_vector_operand (op, mode);
25642
25643           /* If we aren't optimizing, only allow one memory operand to be
25644              generated.  */
25645           if (memory_operand (op, mode))
25646             num_memory++;
25647
25648           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
25649
25650           if (optimize
25651               || !insn_data[icode].operand[i+adjust+1].predicate (op, mode)
25652               || num_memory > 1)
25653             op = force_reg (mode, op);
25654         }
25655
25656       args[i].op = op;
25657       args[i].mode = mode;
25658     }
25659
25660   switch (nargs)
25661     {
25662     case 1:
25663       pat = GEN_FCN (icode) (target, args[0].op);
25664       break;
25665
25666     case 2:
25667       if (tf_p)
25668         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
25669                                GEN_INT ((int)sub_code));
25670       else if (! comparison_p)
25671         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
25672       else
25673         {
25674           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
25675                                        args[0].op,
25676                                        args[1].op);
25677
25678           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
25679         }
25680       break;
25681
25682     case 3:
25683       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
25684       break;
25685
25686     case 4:
25687       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
25688       break;
25689
25690     default:
25691       gcc_unreachable ();
25692     }
25693
25694   if (! pat)
25695     return 0;
25696
25697   emit_insn (pat);
25698   return target;
25699 }
25700
25701 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
25702    insns with vec_merge.  */
25703
25704 static rtx
25705 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
25706                                     rtx target)
25707 {
25708   rtx pat;
25709   tree arg0 = CALL_EXPR_ARG (exp, 0);
25710   rtx op1, op0 = expand_normal (arg0);
25711   enum machine_mode tmode = insn_data[icode].operand[0].mode;
25712   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
25713
25714   if (optimize || !target
25715       || GET_MODE (target) != tmode
25716       || !insn_data[icode].operand[0].predicate (target, tmode))
25717     target = gen_reg_rtx (tmode);
25718
25719   if (VECTOR_MODE_P (mode0))
25720     op0 = safe_vector_operand (op0, mode0);
25721
25722   if ((optimize && !register_operand (op0, mode0))
25723       || !insn_data[icode].operand[1].predicate (op0, mode0))
25724     op0 = copy_to_mode_reg (mode0, op0);
25725
25726   op1 = op0;
25727   if (!insn_data[icode].operand[2].predicate (op1, mode0))
25728     op1 = copy_to_mode_reg (mode0, op1);
25729
25730   pat = GEN_FCN (icode) (target, op0, op1);
25731   if (! pat)
25732     return 0;
25733   emit_insn (pat);
25734   return target;
25735 }
25736
25737 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
25738
25739 static rtx
25740 ix86_expand_sse_compare (const struct builtin_description *d,
25741                          tree exp, rtx target, bool swap)
25742 {
25743   rtx pat;
25744   tree arg0 = CALL_EXPR_ARG (exp, 0);
25745   tree arg1 = CALL_EXPR_ARG (exp, 1);
25746   rtx op0 = expand_normal (arg0);
25747   rtx op1 = expand_normal (arg1);
25748   rtx op2;
25749   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
25750   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
25751   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
25752   enum rtx_code comparison = d->comparison;
25753
25754   if (VECTOR_MODE_P (mode0))
25755     op0 = safe_vector_operand (op0, mode0);
25756   if (VECTOR_MODE_P (mode1))
25757     op1 = safe_vector_operand (op1, mode1);
25758
25759   /* Swap operands if we have a comparison that isn't available in
25760      hardware.  */
25761   if (swap)
25762     {
25763       rtx tmp = gen_reg_rtx (mode1);
25764       emit_move_insn (tmp, op1);
25765       op1 = op0;
25766       op0 = tmp;
25767     }
25768
25769   if (optimize || !target
25770       || GET_MODE (target) != tmode
25771       || !insn_data[d->icode].operand[0].predicate (target, tmode))
25772     target = gen_reg_rtx (tmode);
25773
25774   if ((optimize && !register_operand (op0, mode0))
25775       || !insn_data[d->icode].operand[1].predicate (op0, mode0))
25776     op0 = copy_to_mode_reg (mode0, op0);
25777   if ((optimize && !register_operand (op1, mode1))
25778       || !insn_data[d->icode].operand[2].predicate (op1, mode1))
25779     op1 = copy_to_mode_reg (mode1, op1);
25780
25781   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
25782   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
25783   if (! pat)
25784     return 0;
25785   emit_insn (pat);
25786   return target;
25787 }
25788
25789 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
25790
25791 static rtx
25792 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
25793                       rtx target)
25794 {
25795   rtx pat;
25796   tree arg0 = CALL_EXPR_ARG (exp, 0);
25797   tree arg1 = CALL_EXPR_ARG (exp, 1);
25798   rtx op0 = expand_normal (arg0);
25799   rtx op1 = expand_normal (arg1);
25800   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
25801   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
25802   enum rtx_code comparison = d->comparison;
25803
25804   if (VECTOR_MODE_P (mode0))
25805     op0 = safe_vector_operand (op0, mode0);
25806   if (VECTOR_MODE_P (mode1))
25807     op1 = safe_vector_operand (op1, mode1);
25808
25809   /* Swap operands if we have a comparison that isn't available in
25810      hardware.  */
25811   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
25812     {
25813       rtx tmp = op1;
25814       op1 = op0;
25815       op0 = tmp;
25816     }
25817
25818   target = gen_reg_rtx (SImode);
25819   emit_move_insn (target, const0_rtx);
25820   target = gen_rtx_SUBREG (QImode, target, 0);
25821
25822   if ((optimize && !register_operand (op0, mode0))
25823       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
25824     op0 = copy_to_mode_reg (mode0, op0);
25825   if ((optimize && !register_operand (op1, mode1))
25826       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
25827     op1 = copy_to_mode_reg (mode1, op1);
25828
25829   pat = GEN_FCN (d->icode) (op0, op1);
25830   if (! pat)
25831     return 0;
25832   emit_insn (pat);
25833   emit_insn (gen_rtx_SET (VOIDmode,
25834                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
25835                           gen_rtx_fmt_ee (comparison, QImode,
25836                                           SET_DEST (pat),
25837                                           const0_rtx)));
25838
25839   return SUBREG_REG (target);
25840 }
25841
25842 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
25843
25844 static rtx
25845 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
25846                        rtx target)
25847 {
25848   rtx pat;
25849   tree arg0 = CALL_EXPR_ARG (exp, 0);
25850   tree arg1 = CALL_EXPR_ARG (exp, 1);
25851   rtx op0 = expand_normal (arg0);
25852   rtx op1 = expand_normal (arg1);
25853   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
25854   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
25855   enum rtx_code comparison = d->comparison;
25856
25857   if (VECTOR_MODE_P (mode0))
25858     op0 = safe_vector_operand (op0, mode0);
25859   if (VECTOR_MODE_P (mode1))
25860     op1 = safe_vector_operand (op1, mode1);
25861
25862   target = gen_reg_rtx (SImode);
25863   emit_move_insn (target, const0_rtx);
25864   target = gen_rtx_SUBREG (QImode, target, 0);
25865
25866   if ((optimize && !register_operand (op0, mode0))
25867       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
25868     op0 = copy_to_mode_reg (mode0, op0);
25869   if ((optimize && !register_operand (op1, mode1))
25870       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
25871     op1 = copy_to_mode_reg (mode1, op1);
25872
25873   pat = GEN_FCN (d->icode) (op0, op1);
25874   if (! pat)
25875     return 0;
25876   emit_insn (pat);
25877   emit_insn (gen_rtx_SET (VOIDmode,
25878                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
25879                           gen_rtx_fmt_ee (comparison, QImode,
25880                                           SET_DEST (pat),
25881                                           const0_rtx)));
25882
25883   return SUBREG_REG (target);
25884 }
25885
25886 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
25887
25888 static rtx
25889 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
25890                           tree exp, rtx target)
25891 {
25892   rtx pat;
25893   tree arg0 = CALL_EXPR_ARG (exp, 0);
25894   tree arg1 = CALL_EXPR_ARG (exp, 1);
25895   tree arg2 = CALL_EXPR_ARG (exp, 2);
25896   tree arg3 = CALL_EXPR_ARG (exp, 3);
25897   tree arg4 = CALL_EXPR_ARG (exp, 4);
25898   rtx scratch0, scratch1;
25899   rtx op0 = expand_normal (arg0);
25900   rtx op1 = expand_normal (arg1);
25901   rtx op2 = expand_normal (arg2);
25902   rtx op3 = expand_normal (arg3);
25903   rtx op4 = expand_normal (arg4);
25904   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
25905
25906   tmode0 = insn_data[d->icode].operand[0].mode;
25907   tmode1 = insn_data[d->icode].operand[1].mode;
25908   modev2 = insn_data[d->icode].operand[2].mode;
25909   modei3 = insn_data[d->icode].operand[3].mode;
25910   modev4 = insn_data[d->icode].operand[4].mode;
25911   modei5 = insn_data[d->icode].operand[5].mode;
25912   modeimm = insn_data[d->icode].operand[6].mode;
25913
25914   if (VECTOR_MODE_P (modev2))
25915     op0 = safe_vector_operand (op0, modev2);
25916   if (VECTOR_MODE_P (modev4))
25917     op2 = safe_vector_operand (op2, modev4);
25918
25919   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
25920     op0 = copy_to_mode_reg (modev2, op0);
25921   if (!insn_data[d->icode].operand[3].predicate (op1, modei3))
25922     op1 = copy_to_mode_reg (modei3, op1);
25923   if ((optimize && !register_operand (op2, modev4))
25924       || !insn_data[d->icode].operand[4].predicate (op2, modev4))
25925     op2 = copy_to_mode_reg (modev4, op2);
25926   if (!insn_data[d->icode].operand[5].predicate (op3, modei5))
25927     op3 = copy_to_mode_reg (modei5, op3);
25928
25929   if (!insn_data[d->icode].operand[6].predicate (op4, modeimm))
25930     {
25931       error ("the fifth argument must be a 8-bit immediate");
25932       return const0_rtx;
25933     }
25934
25935   if (d->code == IX86_BUILTIN_PCMPESTRI128)
25936     {
25937       if (optimize || !target
25938           || GET_MODE (target) != tmode0
25939           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
25940         target = gen_reg_rtx (tmode0);
25941
25942       scratch1 = gen_reg_rtx (tmode1);
25943
25944       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
25945     }
25946   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
25947     {
25948       if (optimize || !target
25949           || GET_MODE (target) != tmode1
25950           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
25951         target = gen_reg_rtx (tmode1);
25952
25953       scratch0 = gen_reg_rtx (tmode0);
25954
25955       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
25956     }
25957   else
25958     {
25959       gcc_assert (d->flag);
25960
25961       scratch0 = gen_reg_rtx (tmode0);
25962       scratch1 = gen_reg_rtx (tmode1);
25963
25964       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
25965     }
25966
25967   if (! pat)
25968     return 0;
25969
25970   emit_insn (pat);
25971
25972   if (d->flag)
25973     {
25974       target = gen_reg_rtx (SImode);
25975       emit_move_insn (target, const0_rtx);
25976       target = gen_rtx_SUBREG (QImode, target, 0);
25977
25978       emit_insn
25979         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
25980                       gen_rtx_fmt_ee (EQ, QImode,
25981                                       gen_rtx_REG ((enum machine_mode) d->flag,
25982                                                    FLAGS_REG),
25983                                       const0_rtx)));
25984       return SUBREG_REG (target);
25985     }
25986   else
25987     return target;
25988 }
25989
25990
25991 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
25992
25993 static rtx
25994 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
25995                           tree exp, rtx target)
25996 {
25997   rtx pat;
25998   tree arg0 = CALL_EXPR_ARG (exp, 0);
25999   tree arg1 = CALL_EXPR_ARG (exp, 1);
26000   tree arg2 = CALL_EXPR_ARG (exp, 2);
26001   rtx scratch0, scratch1;
26002   rtx op0 = expand_normal (arg0);
26003   rtx op1 = expand_normal (arg1);
26004   rtx op2 = expand_normal (arg2);
26005   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
26006
26007   tmode0 = insn_data[d->icode].operand[0].mode;
26008   tmode1 = insn_data[d->icode].operand[1].mode;
26009   modev2 = insn_data[d->icode].operand[2].mode;
26010   modev3 = insn_data[d->icode].operand[3].mode;
26011   modeimm = insn_data[d->icode].operand[4].mode;
26012
26013   if (VECTOR_MODE_P (modev2))
26014     op0 = safe_vector_operand (op0, modev2);
26015   if (VECTOR_MODE_P (modev3))
26016     op1 = safe_vector_operand (op1, modev3);
26017
26018   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
26019     op0 = copy_to_mode_reg (modev2, op0);
26020   if ((optimize && !register_operand (op1, modev3))
26021       || !insn_data[d->icode].operand[3].predicate (op1, modev3))
26022     op1 = copy_to_mode_reg (modev3, op1);
26023
26024   if (!insn_data[d->icode].operand[4].predicate (op2, modeimm))
26025     {
26026       error ("the third argument must be a 8-bit immediate");
26027       return const0_rtx;
26028     }
26029
26030   if (d->code == IX86_BUILTIN_PCMPISTRI128)
26031     {
26032       if (optimize || !target
26033           || GET_MODE (target) != tmode0
26034           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
26035         target = gen_reg_rtx (tmode0);
26036
26037       scratch1 = gen_reg_rtx (tmode1);
26038
26039       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
26040     }
26041   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
26042     {
26043       if (optimize || !target
26044           || GET_MODE (target) != tmode1
26045           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
26046         target = gen_reg_rtx (tmode1);
26047
26048       scratch0 = gen_reg_rtx (tmode0);
26049
26050       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
26051     }
26052   else
26053     {
26054       gcc_assert (d->flag);
26055
26056       scratch0 = gen_reg_rtx (tmode0);
26057       scratch1 = gen_reg_rtx (tmode1);
26058
26059       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
26060     }
26061
26062   if (! pat)
26063     return 0;
26064
26065   emit_insn (pat);
26066
26067   if (d->flag)
26068     {
26069       target = gen_reg_rtx (SImode);
26070       emit_move_insn (target, const0_rtx);
26071       target = gen_rtx_SUBREG (QImode, target, 0);
26072
26073       emit_insn
26074         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
26075                       gen_rtx_fmt_ee (EQ, QImode,
26076                                       gen_rtx_REG ((enum machine_mode) d->flag,
26077                                                    FLAGS_REG),
26078                                       const0_rtx)));
26079       return SUBREG_REG (target);
26080     }
26081   else
26082     return target;
26083 }
26084
26085 /* Subroutine of ix86_expand_builtin to take care of insns with
26086    variable number of operands.  */
26087
26088 static rtx
26089 ix86_expand_args_builtin (const struct builtin_description *d,
26090                           tree exp, rtx target)
26091 {
26092   rtx pat, real_target;
26093   unsigned int i, nargs;
26094   unsigned int nargs_constant = 0;
26095   int num_memory = 0;
26096   struct
26097     {
26098       rtx op;
26099       enum machine_mode mode;
26100     } args[4];
26101   bool last_arg_count = false;
26102   enum insn_code icode = d->icode;
26103   const struct insn_data_d *insn_p = &insn_data[icode];
26104   enum machine_mode tmode = insn_p->operand[0].mode;
26105   enum machine_mode rmode = VOIDmode;
26106   bool swap = false;
26107   enum rtx_code comparison = d->comparison;
26108
26109   switch ((enum ix86_builtin_func_type) d->flag)
26110     {
26111     case INT_FTYPE_V8SF_V8SF_PTEST:
26112     case INT_FTYPE_V4DI_V4DI_PTEST:
26113     case INT_FTYPE_V4DF_V4DF_PTEST:
26114     case INT_FTYPE_V4SF_V4SF_PTEST:
26115     case INT_FTYPE_V2DI_V2DI_PTEST:
26116     case INT_FTYPE_V2DF_V2DF_PTEST:
26117       return ix86_expand_sse_ptest (d, exp, target);
26118     case FLOAT128_FTYPE_FLOAT128:
26119     case FLOAT_FTYPE_FLOAT:
26120     case INT_FTYPE_INT:
26121     case UINT64_FTYPE_INT:
26122     case UINT16_FTYPE_UINT16:
26123     case INT64_FTYPE_INT64:
26124     case INT64_FTYPE_V4SF:
26125     case INT64_FTYPE_V2DF:
26126     case INT_FTYPE_V16QI:
26127     case INT_FTYPE_V8QI:
26128     case INT_FTYPE_V8SF:
26129     case INT_FTYPE_V4DF:
26130     case INT_FTYPE_V4SF:
26131     case INT_FTYPE_V2DF:
26132     case V16QI_FTYPE_V16QI:
26133     case V8SI_FTYPE_V8SF:
26134     case V8SI_FTYPE_V4SI:
26135     case V8HI_FTYPE_V8HI:
26136     case V8HI_FTYPE_V16QI:
26137     case V8QI_FTYPE_V8QI:
26138     case V8SF_FTYPE_V8SF:
26139     case V8SF_FTYPE_V8SI:
26140     case V8SF_FTYPE_V4SF:
26141     case V8SF_FTYPE_V8HI:
26142     case V4SI_FTYPE_V4SI:
26143     case V4SI_FTYPE_V16QI:
26144     case V4SI_FTYPE_V4SF:
26145     case V4SI_FTYPE_V8SI:
26146     case V4SI_FTYPE_V8HI:
26147     case V4SI_FTYPE_V4DF:
26148     case V4SI_FTYPE_V2DF:
26149     case V4HI_FTYPE_V4HI:
26150     case V4DF_FTYPE_V4DF:
26151     case V4DF_FTYPE_V4SI:
26152     case V4DF_FTYPE_V4SF:
26153     case V4DF_FTYPE_V2DF:
26154     case V4SF_FTYPE_V4SF:
26155     case V4SF_FTYPE_V4SI:
26156     case V4SF_FTYPE_V8SF:
26157     case V4SF_FTYPE_V4DF:
26158     case V4SF_FTYPE_V8HI:
26159     case V4SF_FTYPE_V2DF:
26160     case V2DI_FTYPE_V2DI:
26161     case V2DI_FTYPE_V16QI:
26162     case V2DI_FTYPE_V8HI:
26163     case V2DI_FTYPE_V4SI:
26164     case V2DF_FTYPE_V2DF:
26165     case V2DF_FTYPE_V4SI:
26166     case V2DF_FTYPE_V4DF:
26167     case V2DF_FTYPE_V4SF:
26168     case V2DF_FTYPE_V2SI:
26169     case V2SI_FTYPE_V2SI:
26170     case V2SI_FTYPE_V4SF:
26171     case V2SI_FTYPE_V2SF:
26172     case V2SI_FTYPE_V2DF:
26173     case V2SF_FTYPE_V2SF:
26174     case V2SF_FTYPE_V2SI:
26175       nargs = 1;
26176       break;
26177     case V4SF_FTYPE_V4SF_VEC_MERGE:
26178     case V2DF_FTYPE_V2DF_VEC_MERGE:
26179       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
26180     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
26181     case V16QI_FTYPE_V16QI_V16QI:
26182     case V16QI_FTYPE_V8HI_V8HI:
26183     case V8QI_FTYPE_V8QI_V8QI:
26184     case V8QI_FTYPE_V4HI_V4HI:
26185     case V8HI_FTYPE_V8HI_V8HI:
26186     case V8HI_FTYPE_V16QI_V16QI:
26187     case V8HI_FTYPE_V4SI_V4SI:
26188     case V8SF_FTYPE_V8SF_V8SF:
26189     case V8SF_FTYPE_V8SF_V8SI:
26190     case V4SI_FTYPE_V4SI_V4SI:
26191     case V4SI_FTYPE_V8HI_V8HI:
26192     case V4SI_FTYPE_V4SF_V4SF:
26193     case V4SI_FTYPE_V2DF_V2DF:
26194     case V4HI_FTYPE_V4HI_V4HI:
26195     case V4HI_FTYPE_V8QI_V8QI:
26196     case V4HI_FTYPE_V2SI_V2SI:
26197     case V4DF_FTYPE_V4DF_V4DF:
26198     case V4DF_FTYPE_V4DF_V4DI:
26199     case V4SF_FTYPE_V4SF_V4SF:
26200     case V4SF_FTYPE_V4SF_V4SI:
26201     case V4SF_FTYPE_V4SF_V2SI:
26202     case V4SF_FTYPE_V4SF_V2DF:
26203     case V4SF_FTYPE_V4SF_DI:
26204     case V4SF_FTYPE_V4SF_SI:
26205     case V2DI_FTYPE_V2DI_V2DI:
26206     case V2DI_FTYPE_V16QI_V16QI:
26207     case V2DI_FTYPE_V4SI_V4SI:
26208     case V2DI_FTYPE_V2DI_V16QI:
26209     case V2DI_FTYPE_V2DF_V2DF:
26210     case V2SI_FTYPE_V2SI_V2SI:
26211     case V2SI_FTYPE_V4HI_V4HI:
26212     case V2SI_FTYPE_V2SF_V2SF:
26213     case V2DF_FTYPE_V2DF_V2DF:
26214     case V2DF_FTYPE_V2DF_V4SF:
26215     case V2DF_FTYPE_V2DF_V2DI:
26216     case V2DF_FTYPE_V2DF_DI:
26217     case V2DF_FTYPE_V2DF_SI:
26218     case V2SF_FTYPE_V2SF_V2SF:
26219     case V1DI_FTYPE_V1DI_V1DI:
26220     case V1DI_FTYPE_V8QI_V8QI:
26221     case V1DI_FTYPE_V2SI_V2SI:
26222       if (comparison == UNKNOWN)
26223         return ix86_expand_binop_builtin (icode, exp, target);
26224       nargs = 2;
26225       break;
26226     case V4SF_FTYPE_V4SF_V4SF_SWAP:
26227     case V2DF_FTYPE_V2DF_V2DF_SWAP:
26228       gcc_assert (comparison != UNKNOWN);
26229       nargs = 2;
26230       swap = true;
26231       break;
26232     case V8HI_FTYPE_V8HI_V8HI_COUNT:
26233     case V8HI_FTYPE_V8HI_SI_COUNT:
26234     case V4SI_FTYPE_V4SI_V4SI_COUNT:
26235     case V4SI_FTYPE_V4SI_SI_COUNT:
26236     case V4HI_FTYPE_V4HI_V4HI_COUNT:
26237     case V4HI_FTYPE_V4HI_SI_COUNT:
26238     case V2DI_FTYPE_V2DI_V2DI_COUNT:
26239     case V2DI_FTYPE_V2DI_SI_COUNT:
26240     case V2SI_FTYPE_V2SI_V2SI_COUNT:
26241     case V2SI_FTYPE_V2SI_SI_COUNT:
26242     case V1DI_FTYPE_V1DI_V1DI_COUNT:
26243     case V1DI_FTYPE_V1DI_SI_COUNT:
26244       nargs = 2;
26245       last_arg_count = true;
26246       break;
26247     case UINT64_FTYPE_UINT64_UINT64:
26248     case UINT_FTYPE_UINT_UINT:
26249     case UINT_FTYPE_UINT_USHORT:
26250     case UINT_FTYPE_UINT_UCHAR:
26251     case UINT16_FTYPE_UINT16_INT:
26252     case UINT8_FTYPE_UINT8_INT:
26253       nargs = 2;
26254       break;
26255     case V2DI_FTYPE_V2DI_INT_CONVERT:
26256       nargs = 2;
26257       rmode = V1TImode;
26258       nargs_constant = 1;
26259       break;
26260     case V8HI_FTYPE_V8HI_INT:
26261     case V8HI_FTYPE_V8SF_INT:
26262     case V8HI_FTYPE_V4SF_INT:
26263     case V8SF_FTYPE_V8SF_INT:
26264     case V4SI_FTYPE_V4SI_INT:
26265     case V4SI_FTYPE_V8SI_INT:
26266     case V4HI_FTYPE_V4HI_INT:
26267     case V4DF_FTYPE_V4DF_INT:
26268     case V4SF_FTYPE_V4SF_INT:
26269     case V4SF_FTYPE_V8SF_INT:
26270     case V2DI_FTYPE_V2DI_INT:
26271     case V2DF_FTYPE_V2DF_INT:
26272     case V2DF_FTYPE_V4DF_INT:
26273       nargs = 2;
26274       nargs_constant = 1;
26275       break;
26276     case V16QI_FTYPE_V16QI_V16QI_V16QI:
26277     case V8SF_FTYPE_V8SF_V8SF_V8SF:
26278     case V4DF_FTYPE_V4DF_V4DF_V4DF:
26279     case V4SF_FTYPE_V4SF_V4SF_V4SF:
26280     case V2DF_FTYPE_V2DF_V2DF_V2DF:
26281       nargs = 3;
26282       break;
26283     case V16QI_FTYPE_V16QI_V16QI_INT:
26284     case V8HI_FTYPE_V8HI_V8HI_INT:
26285     case V8SI_FTYPE_V8SI_V8SI_INT:
26286     case V8SI_FTYPE_V8SI_V4SI_INT:
26287     case V8SF_FTYPE_V8SF_V8SF_INT:
26288     case V8SF_FTYPE_V8SF_V4SF_INT:
26289     case V4SI_FTYPE_V4SI_V4SI_INT:
26290     case V4DF_FTYPE_V4DF_V4DF_INT:
26291     case V4DF_FTYPE_V4DF_V2DF_INT:
26292     case V4SF_FTYPE_V4SF_V4SF_INT:
26293     case V2DI_FTYPE_V2DI_V2DI_INT:
26294     case V2DF_FTYPE_V2DF_V2DF_INT:
26295       nargs = 3;
26296       nargs_constant = 1;
26297       break;
26298     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
26299       nargs = 3;
26300       rmode = V2DImode;
26301       nargs_constant = 1;
26302       break;
26303     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
26304       nargs = 3;
26305       rmode = DImode;
26306       nargs_constant = 1;
26307       break;
26308     case V2DI_FTYPE_V2DI_UINT_UINT:
26309       nargs = 3;
26310       nargs_constant = 2;
26311       break;
26312     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
26313     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
26314     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
26315     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
26316       nargs = 4;
26317       nargs_constant = 1;
26318       break;
26319     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
26320       nargs = 4;
26321       nargs_constant = 2;
26322       break;
26323     default:
26324       gcc_unreachable ();
26325     }
26326
26327   gcc_assert (nargs <= ARRAY_SIZE (args));
26328
26329   if (comparison != UNKNOWN)
26330     {
26331       gcc_assert (nargs == 2);
26332       return ix86_expand_sse_compare (d, exp, target, swap);
26333     }
26334
26335   if (rmode == VOIDmode || rmode == tmode)
26336     {
26337       if (optimize
26338           || target == 0
26339           || GET_MODE (target) != tmode
26340           || !insn_p->operand[0].predicate (target, tmode))
26341         target = gen_reg_rtx (tmode);
26342       real_target = target;
26343     }
26344   else
26345     {
26346       target = gen_reg_rtx (rmode);
26347       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
26348     }
26349
26350   for (i = 0; i < nargs; i++)
26351     {
26352       tree arg = CALL_EXPR_ARG (exp, i);
26353       rtx op = expand_normal (arg);
26354       enum machine_mode mode = insn_p->operand[i + 1].mode;
26355       bool match = insn_p->operand[i + 1].predicate (op, mode);
26356
26357       if (last_arg_count && (i + 1) == nargs)
26358         {
26359           /* SIMD shift insns take either an 8-bit immediate or
26360              register as count.  But builtin functions take int as
26361              count.  If count doesn't match, we put it in register.  */
26362           if (!match)
26363             {
26364               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
26365               if (!insn_p->operand[i + 1].predicate (op, mode))
26366                 op = copy_to_reg (op);
26367             }
26368         }
26369       else if ((nargs - i) <= nargs_constant)
26370         {
26371           if (!match)
26372             switch (icode)
26373               {
26374               case CODE_FOR_sse4_1_roundpd:
26375               case CODE_FOR_sse4_1_roundps:
26376               case CODE_FOR_sse4_1_roundsd:
26377               case CODE_FOR_sse4_1_roundss:
26378               case CODE_FOR_sse4_1_blendps:
26379               case CODE_FOR_avx_blendpd256:
26380               case CODE_FOR_avx_vpermilv4df:
26381               case CODE_FOR_avx_roundpd256:
26382               case CODE_FOR_avx_roundps256:
26383                 error ("the last argument must be a 4-bit immediate");
26384                 return const0_rtx;
26385
26386               case CODE_FOR_sse4_1_blendpd:
26387               case CODE_FOR_avx_vpermilv2df:
26388               case CODE_FOR_xop_vpermil2v2df3:
26389               case CODE_FOR_xop_vpermil2v4sf3:
26390               case CODE_FOR_xop_vpermil2v4df3:
26391               case CODE_FOR_xop_vpermil2v8sf3:
26392                 error ("the last argument must be a 2-bit immediate");
26393                 return const0_rtx;
26394
26395               case CODE_FOR_avx_vextractf128v4df:
26396               case CODE_FOR_avx_vextractf128v8sf:
26397               case CODE_FOR_avx_vextractf128v8si:
26398               case CODE_FOR_avx_vinsertf128v4df:
26399               case CODE_FOR_avx_vinsertf128v8sf:
26400               case CODE_FOR_avx_vinsertf128v8si:
26401                 error ("the last argument must be a 1-bit immediate");
26402                 return const0_rtx;
26403
26404               case CODE_FOR_avx_cmpsdv2df3:
26405               case CODE_FOR_avx_cmpssv4sf3:
26406               case CODE_FOR_avx_cmppdv2df3:
26407               case CODE_FOR_avx_cmppsv4sf3:
26408               case CODE_FOR_avx_cmppdv4df3:
26409               case CODE_FOR_avx_cmppsv8sf3:
26410                 error ("the last argument must be a 5-bit immediate");
26411                 return const0_rtx;
26412
26413              default:
26414                 switch (nargs_constant)
26415                   {
26416                   case 2:
26417                     if ((nargs - i) == nargs_constant)
26418                       {
26419                         error ("the next to last argument must be an 8-bit immediate");
26420                         break;
26421                       }
26422                   case 1:
26423                     error ("the last argument must be an 8-bit immediate");
26424                     break;
26425                   default:
26426                     gcc_unreachable ();
26427                   }
26428                 return const0_rtx;
26429               }
26430         }
26431       else
26432         {
26433           if (VECTOR_MODE_P (mode))
26434             op = safe_vector_operand (op, mode);
26435
26436           /* If we aren't optimizing, only allow one memory operand to
26437              be generated.  */
26438           if (memory_operand (op, mode))
26439             num_memory++;
26440
26441           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
26442             {
26443               if (optimize || !match || num_memory > 1)
26444                 op = copy_to_mode_reg (mode, op);
26445             }
26446           else
26447             {
26448               op = copy_to_reg (op);
26449               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
26450             }
26451         }
26452
26453       args[i].op = op;
26454       args[i].mode = mode;
26455     }
26456
26457   switch (nargs)
26458     {
26459     case 1:
26460       pat = GEN_FCN (icode) (real_target, args[0].op);
26461       break;
26462     case 2:
26463       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
26464       break;
26465     case 3:
26466       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
26467                              args[2].op);
26468       break;
26469     case 4:
26470       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
26471                              args[2].op, args[3].op);
26472       break;
26473     default:
26474       gcc_unreachable ();
26475     }
26476
26477   if (! pat)
26478     return 0;
26479
26480   emit_insn (pat);
26481   return target;
26482 }
26483
26484 /* Subroutine of ix86_expand_builtin to take care of special insns
26485    with variable number of operands.  */
26486
26487 static rtx
26488 ix86_expand_special_args_builtin (const struct builtin_description *d,
26489                                     tree exp, rtx target)
26490 {
26491   tree arg;
26492   rtx pat, op;
26493   unsigned int i, nargs, arg_adjust, memory;
26494   struct
26495     {
26496       rtx op;
26497       enum machine_mode mode;
26498     } args[3];
26499   enum insn_code icode = d->icode;
26500   bool last_arg_constant = false;
26501   const struct insn_data_d *insn_p = &insn_data[icode];
26502   enum machine_mode tmode = insn_p->operand[0].mode;
26503   enum { load, store } klass;
26504
26505   switch ((enum ix86_builtin_func_type) d->flag)
26506     {
26507     case VOID_FTYPE_VOID:
26508       if (icode == CODE_FOR_avx_vzeroupper)
26509         target = GEN_INT (vzeroupper_intrinsic);
26510       emit_insn (GEN_FCN (icode) (target));
26511       return 0;
26512     case VOID_FTYPE_UINT64:
26513     case VOID_FTYPE_UNSIGNED:
26514       nargs = 0;
26515       klass = store;
26516       memory = 0;
26517       break;
26518       break;
26519     case UINT64_FTYPE_VOID:
26520     case UNSIGNED_FTYPE_VOID:
26521     case UINT16_FTYPE_VOID:
26522       nargs = 0;
26523       klass = load;
26524       memory = 0;
26525       break;
26526     case UINT64_FTYPE_PUNSIGNED:
26527     case V2DI_FTYPE_PV2DI:
26528     case V32QI_FTYPE_PCCHAR:
26529     case V16QI_FTYPE_PCCHAR:
26530     case V8SF_FTYPE_PCV4SF:
26531     case V8SF_FTYPE_PCFLOAT:
26532     case V4SF_FTYPE_PCFLOAT:
26533     case V4DF_FTYPE_PCV2DF:
26534     case V4DF_FTYPE_PCDOUBLE:
26535     case V2DF_FTYPE_PCDOUBLE:
26536     case VOID_FTYPE_PVOID:
26537       nargs = 1;
26538       klass = load;
26539       memory = 0;
26540       break;
26541     case VOID_FTYPE_PV2SF_V4SF:
26542     case VOID_FTYPE_PV4DI_V4DI:
26543     case VOID_FTYPE_PV2DI_V2DI:
26544     case VOID_FTYPE_PCHAR_V32QI:
26545     case VOID_FTYPE_PCHAR_V16QI:
26546     case VOID_FTYPE_PFLOAT_V8SF:
26547     case VOID_FTYPE_PFLOAT_V4SF:
26548     case VOID_FTYPE_PDOUBLE_V4DF:
26549     case VOID_FTYPE_PDOUBLE_V2DF:
26550     case VOID_FTYPE_PULONGLONG_ULONGLONG:
26551     case VOID_FTYPE_PINT_INT:
26552       nargs = 1;
26553       klass = store;
26554       /* Reserve memory operand for target.  */
26555       memory = ARRAY_SIZE (args);
26556       break;
26557     case V4SF_FTYPE_V4SF_PCV2SF:
26558     case V2DF_FTYPE_V2DF_PCDOUBLE:
26559       nargs = 2;
26560       klass = load;
26561       memory = 1;
26562       break;
26563     case V8SF_FTYPE_PCV8SF_V8SF:
26564     case V4DF_FTYPE_PCV4DF_V4DF:
26565     case V4SF_FTYPE_PCV4SF_V4SF:
26566     case V2DF_FTYPE_PCV2DF_V2DF:
26567       nargs = 2;
26568       klass = load;
26569       memory = 0;
26570       break;
26571     case VOID_FTYPE_PV8SF_V8SF_V8SF:
26572     case VOID_FTYPE_PV4DF_V4DF_V4DF:
26573     case VOID_FTYPE_PV4SF_V4SF_V4SF:
26574     case VOID_FTYPE_PV2DF_V2DF_V2DF:
26575       nargs = 2;
26576       klass = store;
26577       /* Reserve memory operand for target.  */
26578       memory = ARRAY_SIZE (args);
26579       break;
26580     case VOID_FTYPE_UINT_UINT_UINT:
26581     case VOID_FTYPE_UINT64_UINT_UINT:
26582     case UCHAR_FTYPE_UINT_UINT_UINT:
26583     case UCHAR_FTYPE_UINT64_UINT_UINT:
26584       nargs = 3;
26585       klass = load;
26586       memory = ARRAY_SIZE (args);
26587       last_arg_constant = true;
26588       break;
26589     default:
26590       gcc_unreachable ();
26591     }
26592
26593   gcc_assert (nargs <= ARRAY_SIZE (args));
26594
26595   if (klass == store)
26596     {
26597       arg = CALL_EXPR_ARG (exp, 0);
26598       op = expand_normal (arg);
26599       gcc_assert (target == 0);
26600       if (memory)
26601         target = gen_rtx_MEM (tmode, copy_to_mode_reg (Pmode, op));
26602       else
26603         target = force_reg (tmode, op);
26604       arg_adjust = 1;
26605     }
26606   else
26607     {
26608       arg_adjust = 0;
26609       if (optimize
26610           || target == 0
26611           || GET_MODE (target) != tmode
26612           || !insn_p->operand[0].predicate (target, tmode))
26613         target = gen_reg_rtx (tmode);
26614     }
26615
26616   for (i = 0; i < nargs; i++)
26617     {
26618       enum machine_mode mode = insn_p->operand[i + 1].mode;
26619       bool match;
26620
26621       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
26622       op = expand_normal (arg);
26623       match = insn_p->operand[i + 1].predicate (op, mode);
26624
26625       if (last_arg_constant && (i + 1) == nargs)
26626         {
26627           if (!match)
26628             {
26629               if (icode == CODE_FOR_lwp_lwpvalsi3
26630                   || icode == CODE_FOR_lwp_lwpinssi3
26631                   || icode == CODE_FOR_lwp_lwpvaldi3
26632                   || icode == CODE_FOR_lwp_lwpinsdi3)
26633                 error ("the last argument must be a 32-bit immediate");
26634               else
26635                 error ("the last argument must be an 8-bit immediate");
26636               return const0_rtx;
26637             }
26638         }
26639       else
26640         {
26641           if (i == memory)
26642             {
26643               /* This must be the memory operand.  */
26644               op = gen_rtx_MEM (mode, copy_to_mode_reg (Pmode, op));
26645               gcc_assert (GET_MODE (op) == mode
26646                           || GET_MODE (op) == VOIDmode);
26647             }
26648           else
26649             {
26650               /* This must be register.  */
26651               if (VECTOR_MODE_P (mode))
26652                 op = safe_vector_operand (op, mode);
26653
26654               gcc_assert (GET_MODE (op) == mode
26655                           || GET_MODE (op) == VOIDmode);
26656               op = copy_to_mode_reg (mode, op);
26657             }
26658         }
26659
26660       args[i].op = op;
26661       args[i].mode = mode;
26662     }
26663
26664   switch (nargs)
26665     {
26666     case 0:
26667       pat = GEN_FCN (icode) (target);
26668       break;
26669     case 1:
26670       pat = GEN_FCN (icode) (target, args[0].op);
26671       break;
26672     case 2:
26673       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
26674       break;
26675     case 3:
26676       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
26677       break;
26678     default:
26679       gcc_unreachable ();
26680     }
26681
26682   if (! pat)
26683     return 0;
26684   emit_insn (pat);
26685   return klass == store ? 0 : target;
26686 }
26687
26688 /* Return the integer constant in ARG.  Constrain it to be in the range
26689    of the subparts of VEC_TYPE; issue an error if not.  */
26690
26691 static int
26692 get_element_number (tree vec_type, tree arg)
26693 {
26694   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
26695
26696   if (!host_integerp (arg, 1)
26697       || (elt = tree_low_cst (arg, 1), elt > max))
26698     {
26699       error ("selector must be an integer constant in the range 0..%wi", max);
26700       return 0;
26701     }
26702
26703   return elt;
26704 }
26705
26706 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
26707    ix86_expand_vector_init.  We DO have language-level syntax for this, in
26708    the form of  (type){ init-list }.  Except that since we can't place emms
26709    instructions from inside the compiler, we can't allow the use of MMX
26710    registers unless the user explicitly asks for it.  So we do *not* define
26711    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
26712    we have builtins invoked by mmintrin.h that gives us license to emit
26713    these sorts of instructions.  */
26714
26715 static rtx
26716 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
26717 {
26718   enum machine_mode tmode = TYPE_MODE (type);
26719   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
26720   int i, n_elt = GET_MODE_NUNITS (tmode);
26721   rtvec v = rtvec_alloc (n_elt);
26722
26723   gcc_assert (VECTOR_MODE_P (tmode));
26724   gcc_assert (call_expr_nargs (exp) == n_elt);
26725
26726   for (i = 0; i < n_elt; ++i)
26727     {
26728       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
26729       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
26730     }
26731
26732   if (!target || !register_operand (target, tmode))
26733     target = gen_reg_rtx (tmode);
26734
26735   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
26736   return target;
26737 }
26738
26739 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
26740    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
26741    had a language-level syntax for referencing vector elements.  */
26742
26743 static rtx
26744 ix86_expand_vec_ext_builtin (tree exp, rtx target)
26745 {
26746   enum machine_mode tmode, mode0;
26747   tree arg0, arg1;
26748   int elt;
26749   rtx op0;
26750
26751   arg0 = CALL_EXPR_ARG (exp, 0);
26752   arg1 = CALL_EXPR_ARG (exp, 1);
26753
26754   op0 = expand_normal (arg0);
26755   elt = get_element_number (TREE_TYPE (arg0), arg1);
26756
26757   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
26758   mode0 = TYPE_MODE (TREE_TYPE (arg0));
26759   gcc_assert (VECTOR_MODE_P (mode0));
26760
26761   op0 = force_reg (mode0, op0);
26762
26763   if (optimize || !target || !register_operand (target, tmode))
26764     target = gen_reg_rtx (tmode);
26765
26766   ix86_expand_vector_extract (true, target, op0, elt);
26767
26768   return target;
26769 }
26770
26771 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
26772    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
26773    a language-level syntax for referencing vector elements.  */
26774
26775 static rtx
26776 ix86_expand_vec_set_builtin (tree exp)
26777 {
26778   enum machine_mode tmode, mode1;
26779   tree arg0, arg1, arg2;
26780   int elt;
26781   rtx op0, op1, target;
26782
26783   arg0 = CALL_EXPR_ARG (exp, 0);
26784   arg1 = CALL_EXPR_ARG (exp, 1);
26785   arg2 = CALL_EXPR_ARG (exp, 2);
26786
26787   tmode = TYPE_MODE (TREE_TYPE (arg0));
26788   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
26789   gcc_assert (VECTOR_MODE_P (tmode));
26790
26791   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
26792   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
26793   elt = get_element_number (TREE_TYPE (arg0), arg2);
26794
26795   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
26796     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
26797
26798   op0 = force_reg (tmode, op0);
26799   op1 = force_reg (mode1, op1);
26800
26801   /* OP0 is the source of these builtin functions and shouldn't be
26802      modified.  Create a copy, use it and return it as target.  */
26803   target = gen_reg_rtx (tmode);
26804   emit_move_insn (target, op0);
26805   ix86_expand_vector_set (true, target, op1, elt);
26806
26807   return target;
26808 }
26809
26810 /* Expand an expression EXP that calls a built-in function,
26811    with result going to TARGET if that's convenient
26812    (and in mode MODE if that's convenient).
26813    SUBTARGET may be used as the target for computing one of EXP's operands.
26814    IGNORE is nonzero if the value is to be ignored.  */
26815
26816 static rtx
26817 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
26818                      enum machine_mode mode ATTRIBUTE_UNUSED,
26819                      int ignore ATTRIBUTE_UNUSED)
26820 {
26821   const struct builtin_description *d;
26822   size_t i;
26823   enum insn_code icode;
26824   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
26825   tree arg0, arg1, arg2;
26826   rtx op0, op1, op2, pat;
26827   enum machine_mode mode0, mode1, mode2;
26828   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
26829
26830   /* Determine whether the builtin function is available under the current ISA.
26831      Originally the builtin was not created if it wasn't applicable to the
26832      current ISA based on the command line switches.  With function specific
26833      options, we need to check in the context of the function making the call
26834      whether it is supported.  */
26835   if (ix86_builtins_isa[fcode].isa
26836       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
26837     {
26838       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
26839                                        NULL, NULL, false);
26840
26841       if (!opts)
26842         error ("%qE needs unknown isa option", fndecl);
26843       else
26844         {
26845           gcc_assert (opts != NULL);
26846           error ("%qE needs isa option %s", fndecl, opts);
26847           free (opts);
26848         }
26849       return const0_rtx;
26850     }
26851
26852   switch (fcode)
26853     {
26854     case IX86_BUILTIN_MASKMOVQ:
26855     case IX86_BUILTIN_MASKMOVDQU:
26856       icode = (fcode == IX86_BUILTIN_MASKMOVQ
26857                ? CODE_FOR_mmx_maskmovq
26858                : CODE_FOR_sse2_maskmovdqu);
26859       /* Note the arg order is different from the operand order.  */
26860       arg1 = CALL_EXPR_ARG (exp, 0);
26861       arg2 = CALL_EXPR_ARG (exp, 1);
26862       arg0 = CALL_EXPR_ARG (exp, 2);
26863       op0 = expand_normal (arg0);
26864       op1 = expand_normal (arg1);
26865       op2 = expand_normal (arg2);
26866       mode0 = insn_data[icode].operand[0].mode;
26867       mode1 = insn_data[icode].operand[1].mode;
26868       mode2 = insn_data[icode].operand[2].mode;
26869
26870       op0 = force_reg (Pmode, op0);
26871       op0 = gen_rtx_MEM (mode1, op0);
26872
26873       if (!insn_data[icode].operand[0].predicate (op0, mode0))
26874         op0 = copy_to_mode_reg (mode0, op0);
26875       if (!insn_data[icode].operand[1].predicate (op1, mode1))
26876         op1 = copy_to_mode_reg (mode1, op1);
26877       if (!insn_data[icode].operand[2].predicate (op2, mode2))
26878         op2 = copy_to_mode_reg (mode2, op2);
26879       pat = GEN_FCN (icode) (op0, op1, op2);
26880       if (! pat)
26881         return 0;
26882       emit_insn (pat);
26883       return 0;
26884
26885     case IX86_BUILTIN_LDMXCSR:
26886       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
26887       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
26888       emit_move_insn (target, op0);
26889       emit_insn (gen_sse_ldmxcsr (target));
26890       return 0;
26891
26892     case IX86_BUILTIN_STMXCSR:
26893       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
26894       emit_insn (gen_sse_stmxcsr (target));
26895       return copy_to_mode_reg (SImode, target);
26896
26897     case IX86_BUILTIN_CLFLUSH:
26898         arg0 = CALL_EXPR_ARG (exp, 0);
26899         op0 = expand_normal (arg0);
26900         icode = CODE_FOR_sse2_clflush;
26901         if (!insn_data[icode].operand[0].predicate (op0, Pmode))
26902             op0 = copy_to_mode_reg (Pmode, op0);
26903
26904         emit_insn (gen_sse2_clflush (op0));
26905         return 0;
26906
26907     case IX86_BUILTIN_MONITOR:
26908       arg0 = CALL_EXPR_ARG (exp, 0);
26909       arg1 = CALL_EXPR_ARG (exp, 1);
26910       arg2 = CALL_EXPR_ARG (exp, 2);
26911       op0 = expand_normal (arg0);
26912       op1 = expand_normal (arg1);
26913       op2 = expand_normal (arg2);
26914       if (!REG_P (op0))
26915         op0 = copy_to_mode_reg (Pmode, op0);
26916       if (!REG_P (op1))
26917         op1 = copy_to_mode_reg (SImode, op1);
26918       if (!REG_P (op2))
26919         op2 = copy_to_mode_reg (SImode, op2);
26920       emit_insn (ix86_gen_monitor (op0, op1, op2));
26921       return 0;
26922
26923     case IX86_BUILTIN_MWAIT:
26924       arg0 = CALL_EXPR_ARG (exp, 0);
26925       arg1 = CALL_EXPR_ARG (exp, 1);
26926       op0 = expand_normal (arg0);
26927       op1 = expand_normal (arg1);
26928       if (!REG_P (op0))
26929         op0 = copy_to_mode_reg (SImode, op0);
26930       if (!REG_P (op1))
26931         op1 = copy_to_mode_reg (SImode, op1);
26932       emit_insn (gen_sse3_mwait (op0, op1));
26933       return 0;
26934
26935     case IX86_BUILTIN_VEC_INIT_V2SI:
26936     case IX86_BUILTIN_VEC_INIT_V4HI:
26937     case IX86_BUILTIN_VEC_INIT_V8QI:
26938       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
26939
26940     case IX86_BUILTIN_VEC_EXT_V2DF:
26941     case IX86_BUILTIN_VEC_EXT_V2DI:
26942     case IX86_BUILTIN_VEC_EXT_V4SF:
26943     case IX86_BUILTIN_VEC_EXT_V4SI:
26944     case IX86_BUILTIN_VEC_EXT_V8HI:
26945     case IX86_BUILTIN_VEC_EXT_V2SI:
26946     case IX86_BUILTIN_VEC_EXT_V4HI:
26947     case IX86_BUILTIN_VEC_EXT_V16QI:
26948       return ix86_expand_vec_ext_builtin (exp, target);
26949
26950     case IX86_BUILTIN_VEC_SET_V2DI:
26951     case IX86_BUILTIN_VEC_SET_V4SF:
26952     case IX86_BUILTIN_VEC_SET_V4SI:
26953     case IX86_BUILTIN_VEC_SET_V8HI:
26954     case IX86_BUILTIN_VEC_SET_V4HI:
26955     case IX86_BUILTIN_VEC_SET_V16QI:
26956       return ix86_expand_vec_set_builtin (exp);
26957
26958     case IX86_BUILTIN_VEC_PERM_V2DF:
26959     case IX86_BUILTIN_VEC_PERM_V4SF:
26960     case IX86_BUILTIN_VEC_PERM_V2DI:
26961     case IX86_BUILTIN_VEC_PERM_V4SI:
26962     case IX86_BUILTIN_VEC_PERM_V8HI:
26963     case IX86_BUILTIN_VEC_PERM_V16QI:
26964     case IX86_BUILTIN_VEC_PERM_V2DI_U:
26965     case IX86_BUILTIN_VEC_PERM_V4SI_U:
26966     case IX86_BUILTIN_VEC_PERM_V8HI_U:
26967     case IX86_BUILTIN_VEC_PERM_V16QI_U:
26968     case IX86_BUILTIN_VEC_PERM_V4DF:
26969     case IX86_BUILTIN_VEC_PERM_V8SF:
26970       return ix86_expand_vec_perm_builtin (exp);
26971
26972     case IX86_BUILTIN_INFQ:
26973     case IX86_BUILTIN_HUGE_VALQ:
26974       {
26975         REAL_VALUE_TYPE inf;
26976         rtx tmp;
26977
26978         real_inf (&inf);
26979         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
26980
26981         tmp = validize_mem (force_const_mem (mode, tmp));
26982
26983         if (target == 0)
26984           target = gen_reg_rtx (mode);
26985
26986         emit_move_insn (target, tmp);
26987         return target;
26988       }
26989
26990     case IX86_BUILTIN_LLWPCB:
26991       arg0 = CALL_EXPR_ARG (exp, 0);
26992       op0 = expand_normal (arg0);
26993       icode = CODE_FOR_lwp_llwpcb;
26994       if (!insn_data[icode].operand[0].predicate (op0, Pmode))
26995         op0 = copy_to_mode_reg (Pmode, op0);
26996       emit_insn (gen_lwp_llwpcb (op0));
26997       return 0;
26998
26999     case IX86_BUILTIN_SLWPCB:
27000       icode = CODE_FOR_lwp_slwpcb;
27001       if (!target
27002           || !insn_data[icode].operand[0].predicate (target, Pmode))
27003         target = gen_reg_rtx (Pmode);
27004       emit_insn (gen_lwp_slwpcb (target));
27005       return target;
27006
27007     default:
27008       break;
27009     }
27010
27011   for (i = 0, d = bdesc_special_args;
27012        i < ARRAY_SIZE (bdesc_special_args);
27013        i++, d++)
27014     if (d->code == fcode)
27015       return ix86_expand_special_args_builtin (d, exp, target);
27016
27017   for (i = 0, d = bdesc_args;
27018        i < ARRAY_SIZE (bdesc_args);
27019        i++, d++)
27020     if (d->code == fcode)
27021       switch (fcode)
27022         {
27023         case IX86_BUILTIN_FABSQ:
27024         case IX86_BUILTIN_COPYSIGNQ:
27025           if (!TARGET_SSE2)
27026             /* Emit a normal call if SSE2 isn't available.  */
27027             return expand_call (exp, target, ignore);
27028         default:
27029           return ix86_expand_args_builtin (d, exp, target);
27030         }
27031
27032   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
27033     if (d->code == fcode)
27034       return ix86_expand_sse_comi (d, exp, target);
27035
27036   for (i = 0, d = bdesc_pcmpestr;
27037        i < ARRAY_SIZE (bdesc_pcmpestr);
27038        i++, d++)
27039     if (d->code == fcode)
27040       return ix86_expand_sse_pcmpestr (d, exp, target);
27041
27042   for (i = 0, d = bdesc_pcmpistr;
27043        i < ARRAY_SIZE (bdesc_pcmpistr);
27044        i++, d++)
27045     if (d->code == fcode)
27046       return ix86_expand_sse_pcmpistr (d, exp, target);
27047
27048   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
27049     if (d->code == fcode)
27050       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
27051                                             (enum ix86_builtin_func_type)
27052                                             d->flag, d->comparison);
27053
27054   gcc_unreachable ();
27055 }
27056
27057 /* Returns a function decl for a vectorized version of the builtin function
27058    with builtin function code FN and the result vector type TYPE, or NULL_TREE
27059    if it is not available.  */
27060
27061 static tree
27062 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
27063                                   tree type_in)
27064 {
27065   enum machine_mode in_mode, out_mode;
27066   int in_n, out_n;
27067   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
27068
27069   if (TREE_CODE (type_out) != VECTOR_TYPE
27070       || TREE_CODE (type_in) != VECTOR_TYPE
27071       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
27072     return NULL_TREE;
27073
27074   out_mode = TYPE_MODE (TREE_TYPE (type_out));
27075   out_n = TYPE_VECTOR_SUBPARTS (type_out);
27076   in_mode = TYPE_MODE (TREE_TYPE (type_in));
27077   in_n = TYPE_VECTOR_SUBPARTS (type_in);
27078
27079   switch (fn)
27080     {
27081     case BUILT_IN_SQRT:
27082       if (out_mode == DFmode && in_mode == DFmode)
27083         {
27084           if (out_n == 2 && in_n == 2)
27085             return ix86_builtins[IX86_BUILTIN_SQRTPD];
27086           else if (out_n == 4 && in_n == 4)
27087             return ix86_builtins[IX86_BUILTIN_SQRTPD256];
27088         }
27089       break;
27090
27091     case BUILT_IN_SQRTF:
27092       if (out_mode == SFmode && in_mode == SFmode)
27093         {
27094           if (out_n == 4 && in_n == 4)
27095             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
27096           else if (out_n == 8 && in_n == 8)
27097             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR256];
27098         }
27099       break;
27100
27101     case BUILT_IN_LRINT:
27102       if (out_mode == SImode && out_n == 4
27103           && in_mode == DFmode && in_n == 2)
27104         return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
27105       break;
27106
27107     case BUILT_IN_LRINTF:
27108       if (out_mode == SImode && in_mode == SFmode)
27109         {
27110           if (out_n == 4 && in_n == 4)
27111             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
27112           else if (out_n == 8 && in_n == 8)
27113             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ256];
27114         }
27115       break;
27116
27117     case BUILT_IN_COPYSIGN:
27118       if (out_mode == DFmode && in_mode == DFmode)
27119         {
27120           if (out_n == 2 && in_n == 2)
27121             return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
27122           else if (out_n == 4 && in_n == 4)
27123             return ix86_builtins[IX86_BUILTIN_CPYSGNPD256];
27124         }
27125       break;
27126
27127     case BUILT_IN_COPYSIGNF:
27128       if (out_mode == SFmode && in_mode == SFmode)
27129         {
27130           if (out_n == 4 && in_n == 4)
27131             return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
27132           else if (out_n == 8 && in_n == 8)
27133             return ix86_builtins[IX86_BUILTIN_CPYSGNPS256];
27134         }
27135       break;
27136
27137     case BUILT_IN_FMA:
27138       if (out_mode == DFmode && in_mode == DFmode)
27139         {
27140           if (out_n == 2 && in_n == 2)
27141             return ix86_builtins[IX86_BUILTIN_VFMADDPD];
27142           if (out_n == 4 && in_n == 4)
27143             return ix86_builtins[IX86_BUILTIN_VFMADDPD256];
27144         }
27145       break;
27146
27147     case BUILT_IN_FMAF:
27148       if (out_mode == SFmode && in_mode == SFmode)
27149         {
27150           if (out_n == 4 && in_n == 4)
27151             return ix86_builtins[IX86_BUILTIN_VFMADDPS];
27152           if (out_n == 8 && in_n == 8)
27153             return ix86_builtins[IX86_BUILTIN_VFMADDPS256];
27154         }
27155       break;
27156
27157     default:
27158       break;
27159     }
27160
27161   /* Dispatch to a handler for a vectorization library.  */
27162   if (ix86_veclib_handler)
27163     return ix86_veclib_handler ((enum built_in_function) fn, type_out,
27164                                 type_in);
27165
27166   return NULL_TREE;
27167 }
27168
27169 /* Handler for an SVML-style interface to
27170    a library with vectorized intrinsics.  */
27171
27172 static tree
27173 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
27174 {
27175   char name[20];
27176   tree fntype, new_fndecl, args;
27177   unsigned arity;
27178   const char *bname;
27179   enum machine_mode el_mode, in_mode;
27180   int n, in_n;
27181
27182   /* The SVML is suitable for unsafe math only.  */
27183   if (!flag_unsafe_math_optimizations)
27184     return NULL_TREE;
27185
27186   el_mode = TYPE_MODE (TREE_TYPE (type_out));
27187   n = TYPE_VECTOR_SUBPARTS (type_out);
27188   in_mode = TYPE_MODE (TREE_TYPE (type_in));
27189   in_n = TYPE_VECTOR_SUBPARTS (type_in);
27190   if (el_mode != in_mode
27191       || n != in_n)
27192     return NULL_TREE;
27193
27194   switch (fn)
27195     {
27196     case BUILT_IN_EXP:
27197     case BUILT_IN_LOG:
27198     case BUILT_IN_LOG10:
27199     case BUILT_IN_POW:
27200     case BUILT_IN_TANH:
27201     case BUILT_IN_TAN:
27202     case BUILT_IN_ATAN:
27203     case BUILT_IN_ATAN2:
27204     case BUILT_IN_ATANH:
27205     case BUILT_IN_CBRT:
27206     case BUILT_IN_SINH:
27207     case BUILT_IN_SIN:
27208     case BUILT_IN_ASINH:
27209     case BUILT_IN_ASIN:
27210     case BUILT_IN_COSH:
27211     case BUILT_IN_COS:
27212     case BUILT_IN_ACOSH:
27213     case BUILT_IN_ACOS:
27214       if (el_mode != DFmode || n != 2)
27215         return NULL_TREE;
27216       break;
27217
27218     case BUILT_IN_EXPF:
27219     case BUILT_IN_LOGF:
27220     case BUILT_IN_LOG10F:
27221     case BUILT_IN_POWF:
27222     case BUILT_IN_TANHF:
27223     case BUILT_IN_TANF:
27224     case BUILT_IN_ATANF:
27225     case BUILT_IN_ATAN2F:
27226     case BUILT_IN_ATANHF:
27227     case BUILT_IN_CBRTF:
27228     case BUILT_IN_SINHF:
27229     case BUILT_IN_SINF:
27230     case BUILT_IN_ASINHF:
27231     case BUILT_IN_ASINF:
27232     case BUILT_IN_COSHF:
27233     case BUILT_IN_COSF:
27234     case BUILT_IN_ACOSHF:
27235     case BUILT_IN_ACOSF:
27236       if (el_mode != SFmode || n != 4)
27237         return NULL_TREE;
27238       break;
27239
27240     default:
27241       return NULL_TREE;
27242     }
27243
27244   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
27245
27246   if (fn == BUILT_IN_LOGF)
27247     strcpy (name, "vmlsLn4");
27248   else if (fn == BUILT_IN_LOG)
27249     strcpy (name, "vmldLn2");
27250   else if (n == 4)
27251     {
27252       sprintf (name, "vmls%s", bname+10);
27253       name[strlen (name)-1] = '4';
27254     }
27255   else
27256     sprintf (name, "vmld%s2", bname+10);
27257
27258   /* Convert to uppercase. */
27259   name[4] &= ~0x20;
27260
27261   arity = 0;
27262   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
27263        args = TREE_CHAIN (args))
27264     arity++;
27265
27266   if (arity == 1)
27267     fntype = build_function_type_list (type_out, type_in, NULL);
27268   else
27269     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
27270
27271   /* Build a function declaration for the vectorized function.  */
27272   new_fndecl = build_decl (BUILTINS_LOCATION,
27273                            FUNCTION_DECL, get_identifier (name), fntype);
27274   TREE_PUBLIC (new_fndecl) = 1;
27275   DECL_EXTERNAL (new_fndecl) = 1;
27276   DECL_IS_NOVOPS (new_fndecl) = 1;
27277   TREE_READONLY (new_fndecl) = 1;
27278
27279   return new_fndecl;
27280 }
27281
27282 /* Handler for an ACML-style interface to
27283    a library with vectorized intrinsics.  */
27284
27285 static tree
27286 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
27287 {
27288   char name[20] = "__vr.._";
27289   tree fntype, new_fndecl, args;
27290   unsigned arity;
27291   const char *bname;
27292   enum machine_mode el_mode, in_mode;
27293   int n, in_n;
27294
27295   /* The ACML is 64bits only and suitable for unsafe math only as
27296      it does not correctly support parts of IEEE with the required
27297      precision such as denormals.  */
27298   if (!TARGET_64BIT
27299       || !flag_unsafe_math_optimizations)
27300     return NULL_TREE;
27301
27302   el_mode = TYPE_MODE (TREE_TYPE (type_out));
27303   n = TYPE_VECTOR_SUBPARTS (type_out);
27304   in_mode = TYPE_MODE (TREE_TYPE (type_in));
27305   in_n = TYPE_VECTOR_SUBPARTS (type_in);
27306   if (el_mode != in_mode
27307       || n != in_n)
27308     return NULL_TREE;
27309
27310   switch (fn)
27311     {
27312     case BUILT_IN_SIN:
27313     case BUILT_IN_COS:
27314     case BUILT_IN_EXP:
27315     case BUILT_IN_LOG:
27316     case BUILT_IN_LOG2:
27317     case BUILT_IN_LOG10:
27318       name[4] = 'd';
27319       name[5] = '2';
27320       if (el_mode != DFmode
27321           || n != 2)
27322         return NULL_TREE;
27323       break;
27324
27325     case BUILT_IN_SINF:
27326     case BUILT_IN_COSF:
27327     case BUILT_IN_EXPF:
27328     case BUILT_IN_POWF:
27329     case BUILT_IN_LOGF:
27330     case BUILT_IN_LOG2F:
27331     case BUILT_IN_LOG10F:
27332       name[4] = 's';
27333       name[5] = '4';
27334       if (el_mode != SFmode
27335           || n != 4)
27336         return NULL_TREE;
27337       break;
27338
27339     default:
27340       return NULL_TREE;
27341     }
27342
27343   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
27344   sprintf (name + 7, "%s", bname+10);
27345
27346   arity = 0;
27347   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
27348        args = TREE_CHAIN (args))
27349     arity++;
27350
27351   if (arity == 1)
27352     fntype = build_function_type_list (type_out, type_in, NULL);
27353   else
27354     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
27355
27356   /* Build a function declaration for the vectorized function.  */
27357   new_fndecl = build_decl (BUILTINS_LOCATION,
27358                            FUNCTION_DECL, get_identifier (name), fntype);
27359   TREE_PUBLIC (new_fndecl) = 1;
27360   DECL_EXTERNAL (new_fndecl) = 1;
27361   DECL_IS_NOVOPS (new_fndecl) = 1;
27362   TREE_READONLY (new_fndecl) = 1;
27363
27364   return new_fndecl;
27365 }
27366
27367
27368 /* Returns a decl of a function that implements conversion of an integer vector
27369    into a floating-point vector, or vice-versa.  DEST_TYPE and SRC_TYPE
27370    are the types involved when converting according to CODE.
27371    Return NULL_TREE if it is not available.  */
27372
27373 static tree
27374 ix86_vectorize_builtin_conversion (unsigned int code,
27375                                    tree dest_type, tree src_type)
27376 {
27377   if (! TARGET_SSE2)
27378     return NULL_TREE;
27379
27380   switch (code)
27381     {
27382     case FLOAT_EXPR:
27383       switch (TYPE_MODE (src_type))
27384         {
27385         case V4SImode:
27386           switch (TYPE_MODE (dest_type))
27387             {
27388             case V4SFmode:
27389               return (TYPE_UNSIGNED (src_type)
27390                       ? ix86_builtins[IX86_BUILTIN_CVTUDQ2PS]
27391                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS]);
27392             case V4DFmode:
27393               return (TYPE_UNSIGNED (src_type)
27394                       ? NULL_TREE
27395                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PD256]);
27396             default:
27397               return NULL_TREE;
27398             }
27399           break;
27400         case V8SImode:
27401           switch (TYPE_MODE (dest_type))
27402             {
27403             case V8SFmode:
27404               return (TYPE_UNSIGNED (src_type)
27405                       ? NULL_TREE
27406                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS256]);
27407             default:
27408               return NULL_TREE;
27409             }
27410           break;
27411         default:
27412           return NULL_TREE;
27413         }
27414
27415     case FIX_TRUNC_EXPR:
27416       switch (TYPE_MODE (dest_type))
27417         {
27418         case V4SImode:
27419           switch (TYPE_MODE (src_type))
27420             {
27421             case V4SFmode:
27422               return (TYPE_UNSIGNED (dest_type)
27423                       ? NULL_TREE
27424                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ]);
27425             case V4DFmode:
27426               return (TYPE_UNSIGNED (dest_type)
27427                       ? NULL_TREE
27428                       : ix86_builtins[IX86_BUILTIN_CVTTPD2DQ256]);
27429             default:
27430               return NULL_TREE;
27431             }
27432           break;
27433
27434         case V8SImode:
27435           switch (TYPE_MODE (src_type))
27436             {
27437             case V8SFmode:
27438               return (TYPE_UNSIGNED (dest_type)
27439                       ? NULL_TREE
27440                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ256]);
27441             default:
27442               return NULL_TREE;
27443             }
27444           break;
27445
27446         default:
27447           return NULL_TREE;
27448         }
27449
27450     default:
27451       return NULL_TREE;
27452     }
27453
27454   return NULL_TREE;
27455 }
27456
27457 /* Returns a code for a target-specific builtin that implements
27458    reciprocal of the function, or NULL_TREE if not available.  */
27459
27460 static tree
27461 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
27462                          bool sqrt ATTRIBUTE_UNUSED)
27463 {
27464   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
27465          && flag_finite_math_only && !flag_trapping_math
27466          && flag_unsafe_math_optimizations))
27467     return NULL_TREE;
27468
27469   if (md_fn)
27470     /* Machine dependent builtins.  */
27471     switch (fn)
27472       {
27473         /* Vectorized version of sqrt to rsqrt conversion.  */
27474       case IX86_BUILTIN_SQRTPS_NR:
27475         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
27476
27477       case IX86_BUILTIN_SQRTPS_NR256:
27478         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR256];
27479
27480       default:
27481         return NULL_TREE;
27482       }
27483   else
27484     /* Normal builtins.  */
27485     switch (fn)
27486       {
27487         /* Sqrt to rsqrt conversion.  */
27488       case BUILT_IN_SQRTF:
27489         return ix86_builtins[IX86_BUILTIN_RSQRTF];
27490
27491       default:
27492         return NULL_TREE;
27493       }
27494 }
27495 \f
27496 /* Helper for avx_vpermilps256_operand et al.  This is also used by
27497    the expansion functions to turn the parallel back into a mask.
27498    The return value is 0 for no match and the imm8+1 for a match.  */
27499
27500 int
27501 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
27502 {
27503   unsigned i, nelt = GET_MODE_NUNITS (mode);
27504   unsigned mask = 0;
27505   unsigned char ipar[8];
27506
27507   if (XVECLEN (par, 0) != (int) nelt)
27508     return 0;
27509
27510   /* Validate that all of the elements are constants, and not totally
27511      out of range.  Copy the data into an integral array to make the
27512      subsequent checks easier.  */
27513   for (i = 0; i < nelt; ++i)
27514     {
27515       rtx er = XVECEXP (par, 0, i);
27516       unsigned HOST_WIDE_INT ei;
27517
27518       if (!CONST_INT_P (er))
27519         return 0;
27520       ei = INTVAL (er);
27521       if (ei >= nelt)
27522         return 0;
27523       ipar[i] = ei;
27524     }
27525
27526   switch (mode)
27527     {
27528     case V4DFmode:
27529       /* In the 256-bit DFmode case, we can only move elements within
27530          a 128-bit lane.  */
27531       for (i = 0; i < 2; ++i)
27532         {
27533           if (ipar[i] >= 2)
27534             return 0;
27535           mask |= ipar[i] << i;
27536         }
27537       for (i = 2; i < 4; ++i)
27538         {
27539           if (ipar[i] < 2)
27540             return 0;
27541           mask |= (ipar[i] - 2) << i;
27542         }
27543       break;
27544
27545     case V8SFmode:
27546       /* In the 256-bit SFmode case, we have full freedom of movement
27547          within the low 128-bit lane, but the high 128-bit lane must
27548          mirror the exact same pattern.  */
27549       for (i = 0; i < 4; ++i)
27550         if (ipar[i] + 4 != ipar[i + 4])
27551           return 0;
27552       nelt = 4;
27553       /* FALLTHRU */
27554
27555     case V2DFmode:
27556     case V4SFmode:
27557       /* In the 128-bit case, we've full freedom in the placement of
27558          the elements from the source operand.  */
27559       for (i = 0; i < nelt; ++i)
27560         mask |= ipar[i] << (i * (nelt / 2));
27561       break;
27562
27563     default:
27564       gcc_unreachable ();
27565     }
27566
27567   /* Make sure success has a non-zero value by adding one.  */
27568   return mask + 1;
27569 }
27570
27571 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
27572    the expansion functions to turn the parallel back into a mask.
27573    The return value is 0 for no match and the imm8+1 for a match.  */
27574
27575 int
27576 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
27577 {
27578   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
27579   unsigned mask = 0;
27580   unsigned char ipar[8];
27581
27582   if (XVECLEN (par, 0) != (int) nelt)
27583     return 0;
27584
27585   /* Validate that all of the elements are constants, and not totally
27586      out of range.  Copy the data into an integral array to make the
27587      subsequent checks easier.  */
27588   for (i = 0; i < nelt; ++i)
27589     {
27590       rtx er = XVECEXP (par, 0, i);
27591       unsigned HOST_WIDE_INT ei;
27592
27593       if (!CONST_INT_P (er))
27594         return 0;
27595       ei = INTVAL (er);
27596       if (ei >= 2 * nelt)
27597         return 0;
27598       ipar[i] = ei;
27599     }
27600
27601   /* Validate that the halves of the permute are halves.  */
27602   for (i = 0; i < nelt2 - 1; ++i)
27603     if (ipar[i] + 1 != ipar[i + 1])
27604       return 0;
27605   for (i = nelt2; i < nelt - 1; ++i)
27606     if (ipar[i] + 1 != ipar[i + 1])
27607       return 0;
27608
27609   /* Reconstruct the mask.  */
27610   for (i = 0; i < 2; ++i)
27611     {
27612       unsigned e = ipar[i * nelt2];
27613       if (e % nelt2)
27614         return 0;
27615       e /= nelt2;
27616       mask |= e << (i * 4);
27617     }
27618
27619   /* Make sure success has a non-zero value by adding one.  */
27620   return mask + 1;
27621 }
27622 \f
27623
27624 /* Store OPERAND to the memory after reload is completed.  This means
27625    that we can't easily use assign_stack_local.  */
27626 rtx
27627 ix86_force_to_memory (enum machine_mode mode, rtx operand)
27628 {
27629   rtx result;
27630
27631   gcc_assert (reload_completed);
27632   if (ix86_using_red_zone ())
27633     {
27634       result = gen_rtx_MEM (mode,
27635                             gen_rtx_PLUS (Pmode,
27636                                           stack_pointer_rtx,
27637                                           GEN_INT (-RED_ZONE_SIZE)));
27638       emit_move_insn (result, operand);
27639     }
27640   else if (TARGET_64BIT)
27641     {
27642       switch (mode)
27643         {
27644         case HImode:
27645         case SImode:
27646           operand = gen_lowpart (DImode, operand);
27647           /* FALLTHRU */
27648         case DImode:
27649           emit_insn (
27650                       gen_rtx_SET (VOIDmode,
27651                                    gen_rtx_MEM (DImode,
27652                                                 gen_rtx_PRE_DEC (DImode,
27653                                                         stack_pointer_rtx)),
27654                                    operand));
27655           break;
27656         default:
27657           gcc_unreachable ();
27658         }
27659       result = gen_rtx_MEM (mode, stack_pointer_rtx);
27660     }
27661   else
27662     {
27663       switch (mode)
27664         {
27665         case DImode:
27666           {
27667             rtx operands[2];
27668             split_double_mode (mode, &operand, 1, operands, operands + 1);
27669             emit_insn (
27670                         gen_rtx_SET (VOIDmode,
27671                                      gen_rtx_MEM (SImode,
27672                                                   gen_rtx_PRE_DEC (Pmode,
27673                                                         stack_pointer_rtx)),
27674                                      operands[1]));
27675             emit_insn (
27676                         gen_rtx_SET (VOIDmode,
27677                                      gen_rtx_MEM (SImode,
27678                                                   gen_rtx_PRE_DEC (Pmode,
27679                                                         stack_pointer_rtx)),
27680                                      operands[0]));
27681           }
27682           break;
27683         case HImode:
27684           /* Store HImodes as SImodes.  */
27685           operand = gen_lowpart (SImode, operand);
27686           /* FALLTHRU */
27687         case SImode:
27688           emit_insn (
27689                       gen_rtx_SET (VOIDmode,
27690                                    gen_rtx_MEM (GET_MODE (operand),
27691                                                 gen_rtx_PRE_DEC (SImode,
27692                                                         stack_pointer_rtx)),
27693                                    operand));
27694           break;
27695         default:
27696           gcc_unreachable ();
27697         }
27698       result = gen_rtx_MEM (mode, stack_pointer_rtx);
27699     }
27700   return result;
27701 }
27702
27703 /* Free operand from the memory.  */
27704 void
27705 ix86_free_from_memory (enum machine_mode mode)
27706 {
27707   if (!ix86_using_red_zone ())
27708     {
27709       int size;
27710
27711       if (mode == DImode || TARGET_64BIT)
27712         size = 8;
27713       else
27714         size = 4;
27715       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
27716          to pop or add instruction if registers are available.  */
27717       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
27718                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
27719                                             GEN_INT (size))));
27720     }
27721 }
27722
27723 /* Implement TARGET_IRA_COVER_CLASSES.  If -mfpmath=sse, we prefer
27724    SSE_REGS to FLOAT_REGS if their costs for a pseudo are the
27725    same.  */
27726 static const reg_class_t *
27727 i386_ira_cover_classes (void)
27728 {
27729   static const reg_class_t sse_fpmath_classes[] = {
27730     GENERAL_REGS, SSE_REGS, MMX_REGS, FLOAT_REGS, LIM_REG_CLASSES
27731   };
27732   static const reg_class_t no_sse_fpmath_classes[] = {
27733     GENERAL_REGS, FLOAT_REGS, MMX_REGS, SSE_REGS, LIM_REG_CLASSES
27734   };
27735
27736  return TARGET_SSE_MATH ? sse_fpmath_classes : no_sse_fpmath_classes;
27737 }
27738
27739 /* Implement TARGET_PREFERRED_RELOAD_CLASS.
27740
27741    Put float CONST_DOUBLE in the constant pool instead of fp regs.
27742    QImode must go into class Q_REGS.
27743    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
27744    movdf to do mem-to-mem moves through integer regs.  */
27745
27746 static reg_class_t
27747 ix86_preferred_reload_class (rtx x, reg_class_t regclass)
27748 {
27749   enum machine_mode mode = GET_MODE (x);
27750
27751   /* We're only allowed to return a subclass of CLASS.  Many of the
27752      following checks fail for NO_REGS, so eliminate that early.  */
27753   if (regclass == NO_REGS)
27754     return NO_REGS;
27755
27756   /* All classes can load zeros.  */
27757   if (x == CONST0_RTX (mode))
27758     return regclass;
27759
27760   /* Force constants into memory if we are loading a (nonzero) constant into
27761      an MMX or SSE register.  This is because there are no MMX/SSE instructions
27762      to load from a constant.  */
27763   if (CONSTANT_P (x)
27764       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
27765     return NO_REGS;
27766
27767   /* Prefer SSE regs only, if we can use them for math.  */
27768   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
27769     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
27770
27771   /* Floating-point constants need more complex checks.  */
27772   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
27773     {
27774       /* General regs can load everything.  */
27775       if (reg_class_subset_p (regclass, GENERAL_REGS))
27776         return regclass;
27777
27778       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
27779          zero above.  We only want to wind up preferring 80387 registers if
27780          we plan on doing computation with them.  */
27781       if (TARGET_80387
27782           && standard_80387_constant_p (x))
27783         {
27784           /* Limit class to non-sse.  */
27785           if (regclass == FLOAT_SSE_REGS)
27786             return FLOAT_REGS;
27787           if (regclass == FP_TOP_SSE_REGS)
27788             return FP_TOP_REG;
27789           if (regclass == FP_SECOND_SSE_REGS)
27790             return FP_SECOND_REG;
27791           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
27792             return regclass;
27793         }
27794
27795       return NO_REGS;
27796     }
27797
27798   /* Generally when we see PLUS here, it's the function invariant
27799      (plus soft-fp const_int).  Which can only be computed into general
27800      regs.  */
27801   if (GET_CODE (x) == PLUS)
27802     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
27803
27804   /* QImode constants are easy to load, but non-constant QImode data
27805      must go into Q_REGS.  */
27806   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
27807     {
27808       if (reg_class_subset_p (regclass, Q_REGS))
27809         return regclass;
27810       if (reg_class_subset_p (Q_REGS, regclass))
27811         return Q_REGS;
27812       return NO_REGS;
27813     }
27814
27815   return regclass;
27816 }
27817
27818 /* Discourage putting floating-point values in SSE registers unless
27819    SSE math is being used, and likewise for the 387 registers.  */
27820 static reg_class_t
27821 ix86_preferred_output_reload_class (rtx x, reg_class_t regclass)
27822 {
27823   enum machine_mode mode = GET_MODE (x);
27824
27825   /* Restrict the output reload class to the register bank that we are doing
27826      math on.  If we would like not to return a subset of CLASS, reject this
27827      alternative: if reload cannot do this, it will still use its choice.  */
27828   mode = GET_MODE (x);
27829   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
27830     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
27831
27832   if (X87_FLOAT_MODE_P (mode))
27833     {
27834       if (regclass == FP_TOP_SSE_REGS)
27835         return FP_TOP_REG;
27836       else if (regclass == FP_SECOND_SSE_REGS)
27837         return FP_SECOND_REG;
27838       else
27839         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
27840     }
27841
27842   return regclass;
27843 }
27844
27845 static reg_class_t
27846 ix86_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
27847                        enum machine_mode mode,
27848                        secondary_reload_info *sri ATTRIBUTE_UNUSED)
27849 {
27850   /* QImode spills from non-QI registers require
27851      intermediate register on 32bit targets.  */
27852   if (!in_p && mode == QImode && !TARGET_64BIT
27853       && (rclass == GENERAL_REGS
27854           || rclass == LEGACY_REGS
27855           || rclass == INDEX_REGS))
27856     {
27857       int regno;
27858
27859       if (REG_P (x))
27860         regno = REGNO (x);
27861       else
27862         regno = -1;
27863
27864       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
27865         regno = true_regnum (x);
27866
27867       /* Return Q_REGS if the operand is in memory.  */
27868       if (regno == -1)
27869         return Q_REGS;
27870     }
27871
27872   return NO_REGS;
27873 }
27874
27875 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.  */
27876
27877 static bool
27878 ix86_class_likely_spilled_p (reg_class_t rclass)
27879 {
27880   switch (rclass)
27881     {
27882       case AREG:
27883       case DREG:
27884       case CREG:
27885       case BREG:
27886       case AD_REGS:
27887       case SIREG:
27888       case DIREG:
27889       case SSE_FIRST_REG:
27890       case FP_TOP_REG:
27891       case FP_SECOND_REG:
27892         return true;
27893
27894       default:
27895         break;
27896     }
27897
27898   return false;
27899 }
27900
27901 /* If we are copying between general and FP registers, we need a memory
27902    location. The same is true for SSE and MMX registers.
27903
27904    To optimize register_move_cost performance, allow inline variant.
27905
27906    The macro can't work reliably when one of the CLASSES is class containing
27907    registers from multiple units (SSE, MMX, integer).  We avoid this by never
27908    combining those units in single alternative in the machine description.
27909    Ensure that this constraint holds to avoid unexpected surprises.
27910
27911    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
27912    enforce these sanity checks.  */
27913
27914 static inline bool
27915 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
27916                                 enum machine_mode mode, int strict)
27917 {
27918   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
27919       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
27920       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
27921       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
27922       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
27923       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
27924     {
27925       gcc_assert (!strict);
27926       return true;
27927     }
27928
27929   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
27930     return true;
27931
27932   /* ??? This is a lie.  We do have moves between mmx/general, and for
27933      mmx/sse2.  But by saying we need secondary memory we discourage the
27934      register allocator from using the mmx registers unless needed.  */
27935   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
27936     return true;
27937
27938   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
27939     {
27940       /* SSE1 doesn't have any direct moves from other classes.  */
27941       if (!TARGET_SSE2)
27942         return true;
27943
27944       /* If the target says that inter-unit moves are more expensive
27945          than moving through memory, then don't generate them.  */
27946       if (!TARGET_INTER_UNIT_MOVES)
27947         return true;
27948
27949       /* Between SSE and general, we have moves no larger than word size.  */
27950       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
27951         return true;
27952     }
27953
27954   return false;
27955 }
27956
27957 bool
27958 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
27959                               enum machine_mode mode, int strict)
27960 {
27961   return inline_secondary_memory_needed (class1, class2, mode, strict);
27962 }
27963
27964 /* Return true if the registers in CLASS cannot represent the change from
27965    modes FROM to TO.  */
27966
27967 bool
27968 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
27969                                enum reg_class regclass)
27970 {
27971   if (from == to)
27972     return false;
27973
27974   /* x87 registers can't do subreg at all, as all values are reformatted
27975      to extended precision.  */
27976   if (MAYBE_FLOAT_CLASS_P (regclass))
27977     return true;
27978
27979   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
27980     {
27981       /* Vector registers do not support QI or HImode loads.  If we don't
27982          disallow a change to these modes, reload will assume it's ok to
27983          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
27984          the vec_dupv4hi pattern.  */
27985       if (GET_MODE_SIZE (from) < 4)
27986         return true;
27987
27988       /* Vector registers do not support subreg with nonzero offsets, which
27989          are otherwise valid for integer registers.  Since we can't see
27990          whether we have a nonzero offset from here, prohibit all
27991          nonparadoxical subregs changing size.  */
27992       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
27993         return true;
27994     }
27995
27996   return false;
27997 }
27998
27999 /* Return the cost of moving data of mode M between a
28000    register and memory.  A value of 2 is the default; this cost is
28001    relative to those in `REGISTER_MOVE_COST'.
28002
28003    This function is used extensively by register_move_cost that is used to
28004    build tables at startup.  Make it inline in this case.
28005    When IN is 2, return maximum of in and out move cost.
28006
28007    If moving between registers and memory is more expensive than
28008    between two registers, you should define this macro to express the
28009    relative cost.
28010
28011    Model also increased moving costs of QImode registers in non
28012    Q_REGS classes.
28013  */
28014 static inline int
28015 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
28016                          int in)
28017 {
28018   int cost;
28019   if (FLOAT_CLASS_P (regclass))
28020     {
28021       int index;
28022       switch (mode)
28023         {
28024           case SFmode:
28025             index = 0;
28026             break;
28027           case DFmode:
28028             index = 1;
28029             break;
28030           case XFmode:
28031             index = 2;
28032             break;
28033           default:
28034             return 100;
28035         }
28036       if (in == 2)
28037         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
28038       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
28039     }
28040   if (SSE_CLASS_P (regclass))
28041     {
28042       int index;
28043       switch (GET_MODE_SIZE (mode))
28044         {
28045           case 4:
28046             index = 0;
28047             break;
28048           case 8:
28049             index = 1;
28050             break;
28051           case 16:
28052             index = 2;
28053             break;
28054           default:
28055             return 100;
28056         }
28057       if (in == 2)
28058         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
28059       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
28060     }
28061   if (MMX_CLASS_P (regclass))
28062     {
28063       int index;
28064       switch (GET_MODE_SIZE (mode))
28065         {
28066           case 4:
28067             index = 0;
28068             break;
28069           case 8:
28070             index = 1;
28071             break;
28072           default:
28073             return 100;
28074         }
28075       if (in)
28076         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
28077       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
28078     }
28079   switch (GET_MODE_SIZE (mode))
28080     {
28081       case 1:
28082         if (Q_CLASS_P (regclass) || TARGET_64BIT)
28083           {
28084             if (!in)
28085               return ix86_cost->int_store[0];
28086             if (TARGET_PARTIAL_REG_DEPENDENCY
28087                 && optimize_function_for_speed_p (cfun))
28088               cost = ix86_cost->movzbl_load;
28089             else
28090               cost = ix86_cost->int_load[0];
28091             if (in == 2)
28092               return MAX (cost, ix86_cost->int_store[0]);
28093             return cost;
28094           }
28095         else
28096           {
28097            if (in == 2)
28098              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
28099            if (in)
28100              return ix86_cost->movzbl_load;
28101            else
28102              return ix86_cost->int_store[0] + 4;
28103           }
28104         break;
28105       case 2:
28106         if (in == 2)
28107           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
28108         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
28109       default:
28110         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
28111         if (mode == TFmode)
28112           mode = XFmode;
28113         if (in == 2)
28114           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
28115         else if (in)
28116           cost = ix86_cost->int_load[2];
28117         else
28118           cost = ix86_cost->int_store[2];
28119         return (cost * (((int) GET_MODE_SIZE (mode)
28120                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
28121     }
28122 }
28123
28124 static int
28125 ix86_memory_move_cost (enum machine_mode mode, reg_class_t regclass,
28126                        bool in)
28127 {
28128   return inline_memory_move_cost (mode, (enum reg_class) regclass, in ? 1 : 0);
28129 }
28130
28131
28132 /* Return the cost of moving data from a register in class CLASS1 to
28133    one in class CLASS2.
28134
28135    It is not required that the cost always equal 2 when FROM is the same as TO;
28136    on some machines it is expensive to move between registers if they are not
28137    general registers.  */
28138
28139 static int
28140 ix86_register_move_cost (enum machine_mode mode, reg_class_t class1_i,
28141                          reg_class_t class2_i)
28142 {
28143   enum reg_class class1 = (enum reg_class) class1_i;
28144   enum reg_class class2 = (enum reg_class) class2_i;
28145
28146   /* In case we require secondary memory, compute cost of the store followed
28147      by load.  In order to avoid bad register allocation choices, we need
28148      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
28149
28150   if (inline_secondary_memory_needed (class1, class2, mode, 0))
28151     {
28152       int cost = 1;
28153
28154       cost += inline_memory_move_cost (mode, class1, 2);
28155       cost += inline_memory_move_cost (mode, class2, 2);
28156
28157       /* In case of copying from general_purpose_register we may emit multiple
28158          stores followed by single load causing memory size mismatch stall.
28159          Count this as arbitrarily high cost of 20.  */
28160       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
28161         cost += 20;
28162
28163       /* In the case of FP/MMX moves, the registers actually overlap, and we
28164          have to switch modes in order to treat them differently.  */
28165       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
28166           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
28167         cost += 20;
28168
28169       return cost;
28170     }
28171
28172   /* Moves between SSE/MMX and integer unit are expensive.  */
28173   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
28174       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
28175
28176     /* ??? By keeping returned value relatively high, we limit the number
28177        of moves between integer and MMX/SSE registers for all targets.
28178        Additionally, high value prevents problem with x86_modes_tieable_p(),
28179        where integer modes in MMX/SSE registers are not tieable
28180        because of missing QImode and HImode moves to, from or between
28181        MMX/SSE registers.  */
28182     return MAX (8, ix86_cost->mmxsse_to_integer);
28183
28184   if (MAYBE_FLOAT_CLASS_P (class1))
28185     return ix86_cost->fp_move;
28186   if (MAYBE_SSE_CLASS_P (class1))
28187     return ix86_cost->sse_move;
28188   if (MAYBE_MMX_CLASS_P (class1))
28189     return ix86_cost->mmx_move;
28190   return 2;
28191 }
28192
28193 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
28194
28195 bool
28196 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
28197 {
28198   /* Flags and only flags can only hold CCmode values.  */
28199   if (CC_REGNO_P (regno))
28200     return GET_MODE_CLASS (mode) == MODE_CC;
28201   if (GET_MODE_CLASS (mode) == MODE_CC
28202       || GET_MODE_CLASS (mode) == MODE_RANDOM
28203       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
28204     return 0;
28205   if (FP_REGNO_P (regno))
28206     return VALID_FP_MODE_P (mode);
28207   if (SSE_REGNO_P (regno))
28208     {
28209       /* We implement the move patterns for all vector modes into and
28210          out of SSE registers, even when no operation instructions
28211          are available.  OImode move is available only when AVX is
28212          enabled.  */
28213       return ((TARGET_AVX && mode == OImode)
28214               || VALID_AVX256_REG_MODE (mode)
28215               || VALID_SSE_REG_MODE (mode)
28216               || VALID_SSE2_REG_MODE (mode)
28217               || VALID_MMX_REG_MODE (mode)
28218               || VALID_MMX_REG_MODE_3DNOW (mode));
28219     }
28220   if (MMX_REGNO_P (regno))
28221     {
28222       /* We implement the move patterns for 3DNOW modes even in MMX mode,
28223          so if the register is available at all, then we can move data of
28224          the given mode into or out of it.  */
28225       return (VALID_MMX_REG_MODE (mode)
28226               || VALID_MMX_REG_MODE_3DNOW (mode));
28227     }
28228
28229   if (mode == QImode)
28230     {
28231       /* Take care for QImode values - they can be in non-QI regs,
28232          but then they do cause partial register stalls.  */
28233       if (regno <= BX_REG || TARGET_64BIT)
28234         return 1;
28235       if (!TARGET_PARTIAL_REG_STALL)
28236         return 1;
28237       return reload_in_progress || reload_completed;
28238     }
28239   /* We handle both integer and floats in the general purpose registers.  */
28240   else if (VALID_INT_MODE_P (mode))
28241     return 1;
28242   else if (VALID_FP_MODE_P (mode))
28243     return 1;
28244   else if (VALID_DFP_MODE_P (mode))
28245     return 1;
28246   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
28247      on to use that value in smaller contexts, this can easily force a
28248      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
28249      supporting DImode, allow it.  */
28250   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
28251     return 1;
28252
28253   return 0;
28254 }
28255
28256 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
28257    tieable integer mode.  */
28258
28259 static bool
28260 ix86_tieable_integer_mode_p (enum machine_mode mode)
28261 {
28262   switch (mode)
28263     {
28264     case HImode:
28265     case SImode:
28266       return true;
28267
28268     case QImode:
28269       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
28270
28271     case DImode:
28272       return TARGET_64BIT;
28273
28274     default:
28275       return false;
28276     }
28277 }
28278
28279 /* Return true if MODE1 is accessible in a register that can hold MODE2
28280    without copying.  That is, all register classes that can hold MODE2
28281    can also hold MODE1.  */
28282
28283 bool
28284 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
28285 {
28286   if (mode1 == mode2)
28287     return true;
28288
28289   if (ix86_tieable_integer_mode_p (mode1)
28290       && ix86_tieable_integer_mode_p (mode2))
28291     return true;
28292
28293   /* MODE2 being XFmode implies fp stack or general regs, which means we
28294      can tie any smaller floating point modes to it.  Note that we do not
28295      tie this with TFmode.  */
28296   if (mode2 == XFmode)
28297     return mode1 == SFmode || mode1 == DFmode;
28298
28299   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
28300      that we can tie it with SFmode.  */
28301   if (mode2 == DFmode)
28302     return mode1 == SFmode;
28303
28304   /* If MODE2 is only appropriate for an SSE register, then tie with
28305      any other mode acceptable to SSE registers.  */
28306   if (GET_MODE_SIZE (mode2) == 16
28307       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
28308     return (GET_MODE_SIZE (mode1) == 16
28309             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
28310
28311   /* If MODE2 is appropriate for an MMX register, then tie
28312      with any other mode acceptable to MMX registers.  */
28313   if (GET_MODE_SIZE (mode2) == 8
28314       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
28315     return (GET_MODE_SIZE (mode1) == 8
28316             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
28317
28318   return false;
28319 }
28320
28321 /* Compute a (partial) cost for rtx X.  Return true if the complete
28322    cost has been computed, and false if subexpressions should be
28323    scanned.  In either case, *TOTAL contains the cost result.  */
28324
28325 static bool
28326 ix86_rtx_costs (rtx x, int code, int outer_code_i, int *total, bool speed)
28327 {
28328   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
28329   enum machine_mode mode = GET_MODE (x);
28330   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
28331
28332   switch (code)
28333     {
28334     case CONST_INT:
28335     case CONST:
28336     case LABEL_REF:
28337     case SYMBOL_REF:
28338       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
28339         *total = 3;
28340       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
28341         *total = 2;
28342       else if (flag_pic && SYMBOLIC_CONST (x)
28343                && (!TARGET_64BIT
28344                    || (!GET_CODE (x) != LABEL_REF
28345                        && (GET_CODE (x) != SYMBOL_REF
28346                            || !SYMBOL_REF_LOCAL_P (x)))))
28347         *total = 1;
28348       else
28349         *total = 0;
28350       return true;
28351
28352     case CONST_DOUBLE:
28353       if (mode == VOIDmode)
28354         *total = 0;
28355       else
28356         switch (standard_80387_constant_p (x))
28357           {
28358           case 1: /* 0.0 */
28359             *total = 1;
28360             break;
28361           default: /* Other constants */
28362             *total = 2;
28363             break;
28364           case 0:
28365           case -1:
28366             /* Start with (MEM (SYMBOL_REF)), since that's where
28367                it'll probably end up.  Add a penalty for size.  */
28368             *total = (COSTS_N_INSNS (1)
28369                       + (flag_pic != 0 && !TARGET_64BIT)
28370                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
28371             break;
28372           }
28373       return true;
28374
28375     case ZERO_EXTEND:
28376       /* The zero extensions is often completely free on x86_64, so make
28377          it as cheap as possible.  */
28378       if (TARGET_64BIT && mode == DImode
28379           && GET_MODE (XEXP (x, 0)) == SImode)
28380         *total = 1;
28381       else if (TARGET_ZERO_EXTEND_WITH_AND)
28382         *total = cost->add;
28383       else
28384         *total = cost->movzx;
28385       return false;
28386
28387     case SIGN_EXTEND:
28388       *total = cost->movsx;
28389       return false;
28390
28391     case ASHIFT:
28392       if (CONST_INT_P (XEXP (x, 1))
28393           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
28394         {
28395           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
28396           if (value == 1)
28397             {
28398               *total = cost->add;
28399               return false;
28400             }
28401           if ((value == 2 || value == 3)
28402               && cost->lea <= cost->shift_const)
28403             {
28404               *total = cost->lea;
28405               return false;
28406             }
28407         }
28408       /* FALLTHRU */
28409
28410     case ROTATE:
28411     case ASHIFTRT:
28412     case LSHIFTRT:
28413     case ROTATERT:
28414       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
28415         {
28416           if (CONST_INT_P (XEXP (x, 1)))
28417             {
28418               if (INTVAL (XEXP (x, 1)) > 32)
28419                 *total = cost->shift_const + COSTS_N_INSNS (2);
28420               else
28421                 *total = cost->shift_const * 2;
28422             }
28423           else
28424             {
28425               if (GET_CODE (XEXP (x, 1)) == AND)
28426                 *total = cost->shift_var * 2;
28427               else
28428                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
28429             }
28430         }
28431       else
28432         {
28433           if (CONST_INT_P (XEXP (x, 1)))
28434             *total = cost->shift_const;
28435           else
28436             *total = cost->shift_var;
28437         }
28438       return false;
28439
28440     case MULT:
28441       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28442         {
28443           /* ??? SSE scalar cost should be used here.  */
28444           *total = cost->fmul;
28445           return false;
28446         }
28447       else if (X87_FLOAT_MODE_P (mode))
28448         {
28449           *total = cost->fmul;
28450           return false;
28451         }
28452       else if (FLOAT_MODE_P (mode))
28453         {
28454           /* ??? SSE vector cost should be used here.  */
28455           *total = cost->fmul;
28456           return false;
28457         }
28458       else
28459         {
28460           rtx op0 = XEXP (x, 0);
28461           rtx op1 = XEXP (x, 1);
28462           int nbits;
28463           if (CONST_INT_P (XEXP (x, 1)))
28464             {
28465               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
28466               for (nbits = 0; value != 0; value &= value - 1)
28467                 nbits++;
28468             }
28469           else
28470             /* This is arbitrary.  */
28471             nbits = 7;
28472
28473           /* Compute costs correctly for widening multiplication.  */
28474           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
28475               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
28476                  == GET_MODE_SIZE (mode))
28477             {
28478               int is_mulwiden = 0;
28479               enum machine_mode inner_mode = GET_MODE (op0);
28480
28481               if (GET_CODE (op0) == GET_CODE (op1))
28482                 is_mulwiden = 1, op1 = XEXP (op1, 0);
28483               else if (CONST_INT_P (op1))
28484                 {
28485                   if (GET_CODE (op0) == SIGN_EXTEND)
28486                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
28487                                   == INTVAL (op1);
28488                   else
28489                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
28490                 }
28491
28492               if (is_mulwiden)
28493                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
28494             }
28495
28496           *total = (cost->mult_init[MODE_INDEX (mode)]
28497                     + nbits * cost->mult_bit
28498                     + rtx_cost (op0, outer_code, speed) + rtx_cost (op1, outer_code, speed));
28499
28500           return true;
28501         }
28502
28503     case DIV:
28504     case UDIV:
28505     case MOD:
28506     case UMOD:
28507       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28508         /* ??? SSE cost should be used here.  */
28509         *total = cost->fdiv;
28510       else if (X87_FLOAT_MODE_P (mode))
28511         *total = cost->fdiv;
28512       else if (FLOAT_MODE_P (mode))
28513         /* ??? SSE vector cost should be used here.  */
28514         *total = cost->fdiv;
28515       else
28516         *total = cost->divide[MODE_INDEX (mode)];
28517       return false;
28518
28519     case PLUS:
28520       if (GET_MODE_CLASS (mode) == MODE_INT
28521                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
28522         {
28523           if (GET_CODE (XEXP (x, 0)) == PLUS
28524               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
28525               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
28526               && CONSTANT_P (XEXP (x, 1)))
28527             {
28528               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
28529               if (val == 2 || val == 4 || val == 8)
28530                 {
28531                   *total = cost->lea;
28532                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
28533                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
28534                                       outer_code, speed);
28535                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
28536                   return true;
28537                 }
28538             }
28539           else if (GET_CODE (XEXP (x, 0)) == MULT
28540                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
28541             {
28542               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
28543               if (val == 2 || val == 4 || val == 8)
28544                 {
28545                   *total = cost->lea;
28546                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
28547                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
28548                   return true;
28549                 }
28550             }
28551           else if (GET_CODE (XEXP (x, 0)) == PLUS)
28552             {
28553               *total = cost->lea;
28554               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
28555               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
28556               *total += rtx_cost (XEXP (x, 1), outer_code, speed);
28557               return true;
28558             }
28559         }
28560       /* FALLTHRU */
28561
28562     case MINUS:
28563       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28564         {
28565           /* ??? SSE cost should be used here.  */
28566           *total = cost->fadd;
28567           return false;
28568         }
28569       else if (X87_FLOAT_MODE_P (mode))
28570         {
28571           *total = cost->fadd;
28572           return false;
28573         }
28574       else if (FLOAT_MODE_P (mode))
28575         {
28576           /* ??? SSE vector cost should be used here.  */
28577           *total = cost->fadd;
28578           return false;
28579         }
28580       /* FALLTHRU */
28581
28582     case AND:
28583     case IOR:
28584     case XOR:
28585       if (!TARGET_64BIT && mode == DImode)
28586         {
28587           *total = (cost->add * 2
28588                     + (rtx_cost (XEXP (x, 0), outer_code, speed)
28589                        << (GET_MODE (XEXP (x, 0)) != DImode))
28590                     + (rtx_cost (XEXP (x, 1), outer_code, speed)
28591                        << (GET_MODE (XEXP (x, 1)) != DImode)));
28592           return true;
28593         }
28594       /* FALLTHRU */
28595
28596     case NEG:
28597       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28598         {
28599           /* ??? SSE cost should be used here.  */
28600           *total = cost->fchs;
28601           return false;
28602         }
28603       else if (X87_FLOAT_MODE_P (mode))
28604         {
28605           *total = cost->fchs;
28606           return false;
28607         }
28608       else if (FLOAT_MODE_P (mode))
28609         {
28610           /* ??? SSE vector cost should be used here.  */
28611           *total = cost->fchs;
28612           return false;
28613         }
28614       /* FALLTHRU */
28615
28616     case NOT:
28617       if (!TARGET_64BIT && mode == DImode)
28618         *total = cost->add * 2;
28619       else
28620         *total = cost->add;
28621       return false;
28622
28623     case COMPARE:
28624       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
28625           && XEXP (XEXP (x, 0), 1) == const1_rtx
28626           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
28627           && XEXP (x, 1) == const0_rtx)
28628         {
28629           /* This kind of construct is implemented using test[bwl].
28630              Treat it as if we had an AND.  */
28631           *total = (cost->add
28632                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed)
28633                     + rtx_cost (const1_rtx, outer_code, speed));
28634           return true;
28635         }
28636       return false;
28637
28638     case FLOAT_EXTEND:
28639       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
28640         *total = 0;
28641       return false;
28642
28643     case ABS:
28644       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28645         /* ??? SSE cost should be used here.  */
28646         *total = cost->fabs;
28647       else if (X87_FLOAT_MODE_P (mode))
28648         *total = cost->fabs;
28649       else if (FLOAT_MODE_P (mode))
28650         /* ??? SSE vector cost should be used here.  */
28651         *total = cost->fabs;
28652       return false;
28653
28654     case SQRT:
28655       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
28656         /* ??? SSE cost should be used here.  */
28657         *total = cost->fsqrt;
28658       else if (X87_FLOAT_MODE_P (mode))
28659         *total = cost->fsqrt;
28660       else if (FLOAT_MODE_P (mode))
28661         /* ??? SSE vector cost should be used here.  */
28662         *total = cost->fsqrt;
28663       return false;
28664
28665     case UNSPEC:
28666       if (XINT (x, 1) == UNSPEC_TP)
28667         *total = 0;
28668       return false;
28669
28670     case VEC_SELECT:
28671     case VEC_CONCAT:
28672     case VEC_MERGE:
28673     case VEC_DUPLICATE:
28674       /* ??? Assume all of these vector manipulation patterns are
28675          recognizable.  In which case they all pretty much have the
28676          same cost.  */
28677      *total = COSTS_N_INSNS (1);
28678      return true;
28679
28680     default:
28681       return false;
28682     }
28683 }
28684
28685 #if TARGET_MACHO
28686
28687 static int current_machopic_label_num;
28688
28689 /* Given a symbol name and its associated stub, write out the
28690    definition of the stub.  */
28691
28692 void
28693 machopic_output_stub (FILE *file, const char *symb, const char *stub)
28694 {
28695   unsigned int length;
28696   char *binder_name, *symbol_name, lazy_ptr_name[32];
28697   int label = ++current_machopic_label_num;
28698
28699   /* For 64-bit we shouldn't get here.  */
28700   gcc_assert (!TARGET_64BIT);
28701
28702   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
28703   symb = targetm.strip_name_encoding (symb);
28704
28705   length = strlen (stub);
28706   binder_name = XALLOCAVEC (char, length + 32);
28707   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
28708
28709   length = strlen (symb);
28710   symbol_name = XALLOCAVEC (char, length + 32);
28711   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
28712
28713   sprintf (lazy_ptr_name, "L%d$lz", label);
28714
28715   if (MACHOPIC_ATT_STUB)
28716     switch_to_section (darwin_sections[machopic_picsymbol_stub3_section]);
28717   else if (MACHOPIC_PURE)
28718     {
28719       if (TARGET_DEEP_BRANCH_PREDICTION)
28720         switch_to_section (darwin_sections[machopic_picsymbol_stub2_section]);
28721       else
28722     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
28723     }
28724   else
28725     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
28726
28727   fprintf (file, "%s:\n", stub);
28728   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28729
28730   if (MACHOPIC_ATT_STUB)
28731     {
28732       fprintf (file, "\thlt ; hlt ; hlt ; hlt ; hlt\n");
28733     }
28734   else if (MACHOPIC_PURE)
28735     {
28736       /* PIC stub.  */
28737       if (TARGET_DEEP_BRANCH_PREDICTION)
28738         {
28739           /* 25-byte PIC stub using "CALL get_pc_thunk".  */
28740           rtx tmp = gen_rtx_REG (SImode, 2 /* ECX */);
28741           output_set_got (tmp, NULL_RTX);       /* "CALL ___<cpu>.get_pc_thunk.cx".  */
28742           fprintf (file, "LPC$%d:\tmovl\t%s-LPC$%d(%%ecx),%%ecx\n", label, lazy_ptr_name, label);
28743         }
28744       else
28745         {
28746           /* 26-byte PIC stub using inline picbase: "CALL L42 ! L42: pop %eax".  */
28747           fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%ecx\n", label, label);
28748           fprintf (file, "\tmovl %s-LPC$%d(%%ecx),%%ecx\n", lazy_ptr_name, label);
28749         }
28750       fprintf (file, "\tjmp\t*%%ecx\n");
28751     }
28752   else
28753     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
28754
28755   /* The AT&T-style ("self-modifying") stub is not lazily bound, thus
28756      it needs no stub-binding-helper.  */
28757   if (MACHOPIC_ATT_STUB)
28758     return;
28759
28760   fprintf (file, "%s:\n", binder_name);
28761
28762   if (MACHOPIC_PURE)
28763     {
28764       fprintf (file, "\tlea\t%s-%s(%%ecx),%%ecx\n", lazy_ptr_name, binder_name);
28765       fprintf (file, "\tpushl\t%%ecx\n");
28766     }
28767   else
28768     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
28769
28770   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
28771
28772   /* N.B. Keep the correspondence of these
28773      'symbol_ptr/symbol_ptr2/symbol_ptr3' sections consistent with the
28774      old-pic/new-pic/non-pic stubs; altering this will break
28775      compatibility with existing dylibs.  */
28776   if (MACHOPIC_PURE)
28777     {
28778       /* PIC stubs.  */
28779       if (TARGET_DEEP_BRANCH_PREDICTION)
28780         /* 25-byte PIC stub using "CALL get_pc_thunk".  */
28781         switch_to_section (darwin_sections[machopic_lazy_symbol_ptr2_section]);
28782       else
28783         /* 26-byte PIC stub using inline picbase: "CALL L42 ! L42: pop %ebx".  */
28784   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
28785     }
28786   else
28787     /* 16-byte -mdynamic-no-pic stub.  */
28788     switch_to_section(darwin_sections[machopic_lazy_symbol_ptr3_section]);
28789
28790   fprintf (file, "%s:\n", lazy_ptr_name);
28791   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
28792   fprintf (file, ASM_LONG "%s\n", binder_name);
28793 }
28794 #endif /* TARGET_MACHO */
28795
28796 /* Order the registers for register allocator.  */
28797
28798 void
28799 x86_order_regs_for_local_alloc (void)
28800 {
28801    int pos = 0;
28802    int i;
28803
28804    /* First allocate the local general purpose registers.  */
28805    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
28806      if (GENERAL_REGNO_P (i) && call_used_regs[i])
28807         reg_alloc_order [pos++] = i;
28808
28809    /* Global general purpose registers.  */
28810    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
28811      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
28812         reg_alloc_order [pos++] = i;
28813
28814    /* x87 registers come first in case we are doing FP math
28815       using them.  */
28816    if (!TARGET_SSE_MATH)
28817      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
28818        reg_alloc_order [pos++] = i;
28819
28820    /* SSE registers.  */
28821    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
28822      reg_alloc_order [pos++] = i;
28823    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
28824      reg_alloc_order [pos++] = i;
28825
28826    /* x87 registers.  */
28827    if (TARGET_SSE_MATH)
28828      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
28829        reg_alloc_order [pos++] = i;
28830
28831    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
28832      reg_alloc_order [pos++] = i;
28833
28834    /* Initialize the rest of array as we do not allocate some registers
28835       at all.  */
28836    while (pos < FIRST_PSEUDO_REGISTER)
28837      reg_alloc_order [pos++] = 0;
28838 }
28839
28840 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
28841    struct attribute_spec.handler.  */
28842 static tree
28843 ix86_handle_abi_attribute (tree *node, tree name,
28844                               tree args ATTRIBUTE_UNUSED,
28845                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28846 {
28847   if (TREE_CODE (*node) != FUNCTION_TYPE
28848       && TREE_CODE (*node) != METHOD_TYPE
28849       && TREE_CODE (*node) != FIELD_DECL
28850       && TREE_CODE (*node) != TYPE_DECL)
28851     {
28852       warning (OPT_Wattributes, "%qE attribute only applies to functions",
28853                name);
28854       *no_add_attrs = true;
28855       return NULL_TREE;
28856     }
28857   if (!TARGET_64BIT)
28858     {
28859       warning (OPT_Wattributes, "%qE attribute only available for 64-bit",
28860                name);
28861       *no_add_attrs = true;
28862       return NULL_TREE;
28863     }
28864
28865   /* Can combine regparm with all attributes but fastcall.  */
28866   if (is_attribute_p ("ms_abi", name))
28867     {
28868       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
28869         {
28870           error ("ms_abi and sysv_abi attributes are not compatible");
28871         }
28872
28873       return NULL_TREE;
28874     }
28875   else if (is_attribute_p ("sysv_abi", name))
28876     {
28877       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
28878         {
28879           error ("ms_abi and sysv_abi attributes are not compatible");
28880         }
28881
28882       return NULL_TREE;
28883     }
28884
28885   return NULL_TREE;
28886 }
28887
28888 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
28889    struct attribute_spec.handler.  */
28890 static tree
28891 ix86_handle_struct_attribute (tree *node, tree name,
28892                               tree args ATTRIBUTE_UNUSED,
28893                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28894 {
28895   tree *type = NULL;
28896   if (DECL_P (*node))
28897     {
28898       if (TREE_CODE (*node) == TYPE_DECL)
28899         type = &TREE_TYPE (*node);
28900     }
28901   else
28902     type = node;
28903
28904   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
28905                  || TREE_CODE (*type) == UNION_TYPE)))
28906     {
28907       warning (OPT_Wattributes, "%qE attribute ignored",
28908                name);
28909       *no_add_attrs = true;
28910     }
28911
28912   else if ((is_attribute_p ("ms_struct", name)
28913             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
28914            || ((is_attribute_p ("gcc_struct", name)
28915                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
28916     {
28917       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
28918                name);
28919       *no_add_attrs = true;
28920     }
28921
28922   return NULL_TREE;
28923 }
28924
28925 static tree
28926 ix86_handle_fndecl_attribute (tree *node, tree name,
28927                               tree args ATTRIBUTE_UNUSED,
28928                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
28929 {
28930   if (TREE_CODE (*node) != FUNCTION_DECL)
28931     {
28932       warning (OPT_Wattributes, "%qE attribute only applies to functions",
28933                name);
28934       *no_add_attrs = true;
28935     }
28936   return NULL_TREE;
28937 }
28938
28939 static bool
28940 ix86_ms_bitfield_layout_p (const_tree record_type)
28941 {
28942   return ((TARGET_MS_BITFIELD_LAYOUT
28943            && !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
28944           || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type)));
28945 }
28946
28947 /* Returns an expression indicating where the this parameter is
28948    located on entry to the FUNCTION.  */
28949
28950 static rtx
28951 x86_this_parameter (tree function)
28952 {
28953   tree type = TREE_TYPE (function);
28954   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
28955   int nregs;
28956
28957   if (TARGET_64BIT)
28958     {
28959       const int *parm_regs;
28960
28961       if (ix86_function_type_abi (type) == MS_ABI)
28962         parm_regs = x86_64_ms_abi_int_parameter_registers;
28963       else
28964         parm_regs = x86_64_int_parameter_registers;
28965       return gen_rtx_REG (DImode, parm_regs[aggr]);
28966     }
28967
28968   nregs = ix86_function_regparm (type, function);
28969
28970   if (nregs > 0 && !stdarg_p (type))
28971     {
28972       int regno;
28973
28974       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
28975         regno = aggr ? DX_REG : CX_REG;
28976       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
28977         {
28978           regno = CX_REG;
28979           if (aggr)
28980             return gen_rtx_MEM (SImode,
28981                                 plus_constant (stack_pointer_rtx, 4));
28982         }
28983       else
28984         {
28985           regno = AX_REG;
28986           if (aggr)
28987             {
28988               regno = DX_REG;
28989               if (nregs == 1)
28990                 return gen_rtx_MEM (SImode,
28991                                     plus_constant (stack_pointer_rtx, 4));
28992             }
28993         }
28994       return gen_rtx_REG (SImode, regno);
28995     }
28996
28997   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
28998 }
28999
29000 /* Determine whether x86_output_mi_thunk can succeed.  */
29001
29002 static bool
29003 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
29004                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
29005                          HOST_WIDE_INT vcall_offset, const_tree function)
29006 {
29007   /* 64-bit can handle anything.  */
29008   if (TARGET_64BIT)
29009     return true;
29010
29011   /* For 32-bit, everything's fine if we have one free register.  */
29012   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
29013     return true;
29014
29015   /* Need a free register for vcall_offset.  */
29016   if (vcall_offset)
29017     return false;
29018
29019   /* Need a free register for GOT references.  */
29020   if (flag_pic && !targetm.binds_local_p (function))
29021     return false;
29022
29023   /* Otherwise ok.  */
29024   return true;
29025 }
29026
29027 /* Output the assembler code for a thunk function.  THUNK_DECL is the
29028    declaration for the thunk function itself, FUNCTION is the decl for
29029    the target function.  DELTA is an immediate constant offset to be
29030    added to THIS.  If VCALL_OFFSET is nonzero, the word at
29031    *(*this + vcall_offset) should be added to THIS.  */
29032
29033 static void
29034 x86_output_mi_thunk (FILE *file,
29035                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
29036                      HOST_WIDE_INT vcall_offset, tree function)
29037 {
29038   rtx xops[3];
29039   rtx this_param = x86_this_parameter (function);
29040   rtx this_reg, tmp;
29041
29042   /* Make sure unwind info is emitted for the thunk if needed.  */
29043   final_start_function (emit_barrier (), file, 1);
29044
29045   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
29046      pull it in now and let DELTA benefit.  */
29047   if (REG_P (this_param))
29048     this_reg = this_param;
29049   else if (vcall_offset)
29050     {
29051       /* Put the this parameter into %eax.  */
29052       xops[0] = this_param;
29053       xops[1] = this_reg = gen_rtx_REG (Pmode, AX_REG);
29054       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
29055     }
29056   else
29057     this_reg = NULL_RTX;
29058
29059   /* Adjust the this parameter by a fixed constant.  */
29060   if (delta)
29061     {
29062       xops[0] = GEN_INT (delta);
29063       xops[1] = this_reg ? this_reg : this_param;
29064       if (TARGET_64BIT)
29065         {
29066           if (!x86_64_general_operand (xops[0], DImode))
29067             {
29068               tmp = gen_rtx_REG (DImode, R10_REG);
29069               xops[1] = tmp;
29070               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
29071               xops[0] = tmp;
29072               xops[1] = this_param;
29073             }
29074           if (x86_maybe_negate_const_int (&xops[0], DImode))
29075             output_asm_insn ("sub{q}\t{%0, %1|%1, %0}", xops);
29076           else
29077             output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
29078         }
29079       else if (x86_maybe_negate_const_int (&xops[0], SImode))
29080         output_asm_insn ("sub{l}\t{%0, %1|%1, %0}", xops);
29081       else
29082         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
29083     }
29084
29085   /* Adjust the this parameter by a value stored in the vtable.  */
29086   if (vcall_offset)
29087     {
29088       if (TARGET_64BIT)
29089         tmp = gen_rtx_REG (DImode, R10_REG);
29090       else
29091         {
29092           int tmp_regno = CX_REG;
29093           if (lookup_attribute ("fastcall",
29094                                 TYPE_ATTRIBUTES (TREE_TYPE (function)))
29095               || lookup_attribute ("thiscall",
29096                                    TYPE_ATTRIBUTES (TREE_TYPE (function))))
29097             tmp_regno = AX_REG;
29098           tmp = gen_rtx_REG (SImode, tmp_regno);
29099         }
29100
29101       xops[0] = gen_rtx_MEM (Pmode, this_reg);
29102       xops[1] = tmp;
29103       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
29104
29105       /* Adjust the this parameter.  */
29106       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
29107       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
29108         {
29109           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
29110           xops[0] = GEN_INT (vcall_offset);
29111           xops[1] = tmp2;
29112           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
29113           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
29114         }
29115       xops[1] = this_reg;
29116       output_asm_insn ("add%z1\t{%0, %1|%1, %0}", xops);
29117     }
29118
29119   /* If necessary, drop THIS back to its stack slot.  */
29120   if (this_reg && this_reg != this_param)
29121     {
29122       xops[0] = this_reg;
29123       xops[1] = this_param;
29124       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
29125     }
29126
29127   xops[0] = XEXP (DECL_RTL (function), 0);
29128   if (TARGET_64BIT)
29129     {
29130       if (!flag_pic || targetm.binds_local_p (function))
29131         output_asm_insn ("jmp\t%P0", xops);
29132       /* All thunks should be in the same object as their target,
29133          and thus binds_local_p should be true.  */
29134       else if (TARGET_64BIT && cfun->machine->call_abi == MS_ABI)
29135         gcc_unreachable ();
29136       else
29137         {
29138           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
29139           tmp = gen_rtx_CONST (Pmode, tmp);
29140           tmp = gen_rtx_MEM (QImode, tmp);
29141           xops[0] = tmp;
29142           output_asm_insn ("jmp\t%A0", xops);
29143         }
29144     }
29145   else
29146     {
29147       if (!flag_pic || targetm.binds_local_p (function))
29148         output_asm_insn ("jmp\t%P0", xops);
29149       else
29150 #if TARGET_MACHO
29151         if (TARGET_MACHO)
29152           {
29153             rtx sym_ref = XEXP (DECL_RTL (function), 0);
29154             if (TARGET_MACHO_BRANCH_ISLANDS)
29155               sym_ref = (gen_rtx_SYMBOL_REF
29156                    (Pmode,
29157                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
29158             tmp = gen_rtx_MEM (QImode, sym_ref);
29159             xops[0] = tmp;
29160             output_asm_insn ("jmp\t%0", xops);
29161           }
29162         else
29163 #endif /* TARGET_MACHO */
29164         {
29165           tmp = gen_rtx_REG (SImode, CX_REG);
29166           output_set_got (tmp, NULL_RTX);
29167
29168           xops[1] = tmp;
29169           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
29170           output_asm_insn ("jmp\t{*}%1", xops);
29171         }
29172     }
29173   final_end_function ();
29174 }
29175
29176 static void
29177 x86_file_start (void)
29178 {
29179   default_file_start ();
29180 #if TARGET_MACHO
29181   darwin_file_start ();
29182 #endif
29183   if (X86_FILE_START_VERSION_DIRECTIVE)
29184     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
29185   if (X86_FILE_START_FLTUSED)
29186     fputs ("\t.global\t__fltused\n", asm_out_file);
29187   if (ix86_asm_dialect == ASM_INTEL)
29188     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
29189 }
29190
29191 int
29192 x86_field_alignment (tree field, int computed)
29193 {
29194   enum machine_mode mode;
29195   tree type = TREE_TYPE (field);
29196
29197   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
29198     return computed;
29199   mode = TYPE_MODE (strip_array_types (type));
29200   if (mode == DFmode || mode == DCmode
29201       || GET_MODE_CLASS (mode) == MODE_INT
29202       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
29203     return MIN (32, computed);
29204   return computed;
29205 }
29206
29207 /* Output assembler code to FILE to increment profiler label # LABELNO
29208    for profiling a function entry.  */
29209 void
29210 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
29211 {
29212   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
29213                                          : MCOUNT_NAME);
29214
29215   if (TARGET_64BIT)
29216     {
29217 #ifndef NO_PROFILE_COUNTERS
29218       fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
29219 #endif
29220
29221       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
29222         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name);
29223       else
29224         fprintf (file, "\tcall\t%s\n", mcount_name);
29225     }
29226   else if (flag_pic)
29227     {
29228 #ifndef NO_PROFILE_COUNTERS
29229       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
29230                LPREFIX, labelno);
29231 #endif
29232       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
29233     }
29234   else
29235     {
29236 #ifndef NO_PROFILE_COUNTERS
29237       fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
29238                LPREFIX, labelno);
29239 #endif
29240       fprintf (file, "\tcall\t%s\n", mcount_name);
29241     }
29242 }
29243
29244 /* We don't have exact information about the insn sizes, but we may assume
29245    quite safely that we are informed about all 1 byte insns and memory
29246    address sizes.  This is enough to eliminate unnecessary padding in
29247    99% of cases.  */
29248
29249 static int
29250 min_insn_size (rtx insn)
29251 {
29252   int l = 0, len;
29253
29254   if (!INSN_P (insn) || !active_insn_p (insn))
29255     return 0;
29256
29257   /* Discard alignments we've emit and jump instructions.  */
29258   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
29259       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
29260     return 0;
29261   if (JUMP_TABLE_DATA_P (insn))
29262     return 0;
29263
29264   /* Important case - calls are always 5 bytes.
29265      It is common to have many calls in the row.  */
29266   if (CALL_P (insn)
29267       && symbolic_reference_mentioned_p (PATTERN (insn))
29268       && !SIBLING_CALL_P (insn))
29269     return 5;
29270   len = get_attr_length (insn);
29271   if (len <= 1)
29272     return 1;
29273
29274   /* For normal instructions we rely on get_attr_length being exact,
29275      with a few exceptions.  */
29276   if (!JUMP_P (insn))
29277     {
29278       enum attr_type type = get_attr_type (insn);
29279
29280       switch (type)
29281         {
29282         case TYPE_MULTI:
29283           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
29284               || asm_noperands (PATTERN (insn)) >= 0)
29285             return 0;
29286           break;
29287         case TYPE_OTHER:
29288         case TYPE_FCMP:
29289           break;
29290         default:
29291           /* Otherwise trust get_attr_length.  */
29292           return len;
29293         }
29294
29295       l = get_attr_length_address (insn);
29296       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
29297         l = 4;
29298     }
29299   if (l)
29300     return 1+l;
29301   else
29302     return 2;
29303 }
29304
29305 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
29306
29307 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
29308    window.  */
29309
29310 static void
29311 ix86_avoid_jump_mispredicts (void)
29312 {
29313   rtx insn, start = get_insns ();
29314   int nbytes = 0, njumps = 0;
29315   int isjump = 0;
29316
29317   /* Look for all minimal intervals of instructions containing 4 jumps.
29318      The intervals are bounded by START and INSN.  NBYTES is the total
29319      size of instructions in the interval including INSN and not including
29320      START.  When the NBYTES is smaller than 16 bytes, it is possible
29321      that the end of START and INSN ends up in the same 16byte page.
29322
29323      The smallest offset in the page INSN can start is the case where START
29324      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
29325      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
29326      */
29327   for (insn = start; insn; insn = NEXT_INSN (insn))
29328     {
29329       int min_size;
29330
29331       if (LABEL_P (insn))
29332         {
29333           int align = label_to_alignment (insn);
29334           int max_skip = label_to_max_skip (insn);
29335
29336           if (max_skip > 15)
29337             max_skip = 15;
29338           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
29339              already in the current 16 byte page, because otherwise
29340              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
29341              bytes to reach 16 byte boundary.  */
29342           if (align <= 0
29343               || (align <= 3 && max_skip != (1 << align) - 1))
29344             max_skip = 0;
29345           if (dump_file)
29346             fprintf (dump_file, "Label %i with max_skip %i\n",
29347                      INSN_UID (insn), max_skip);
29348           if (max_skip)
29349             {
29350               while (nbytes + max_skip >= 16)
29351                 {
29352                   start = NEXT_INSN (start);
29353                   if ((JUMP_P (start)
29354                        && GET_CODE (PATTERN (start)) != ADDR_VEC
29355                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
29356                       || CALL_P (start))
29357                     njumps--, isjump = 1;
29358                   else
29359                     isjump = 0;
29360                   nbytes -= min_insn_size (start);
29361                 }
29362             }
29363           continue;
29364         }
29365
29366       min_size = min_insn_size (insn);
29367       nbytes += min_size;
29368       if (dump_file)
29369         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
29370                  INSN_UID (insn), min_size);
29371       if ((JUMP_P (insn)
29372            && GET_CODE (PATTERN (insn)) != ADDR_VEC
29373            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
29374           || CALL_P (insn))
29375         njumps++;
29376       else
29377         continue;
29378
29379       while (njumps > 3)
29380         {
29381           start = NEXT_INSN (start);
29382           if ((JUMP_P (start)
29383                && GET_CODE (PATTERN (start)) != ADDR_VEC
29384                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
29385               || CALL_P (start))
29386             njumps--, isjump = 1;
29387           else
29388             isjump = 0;
29389           nbytes -= min_insn_size (start);
29390         }
29391       gcc_assert (njumps >= 0);
29392       if (dump_file)
29393         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
29394                  INSN_UID (start), INSN_UID (insn), nbytes);
29395
29396       if (njumps == 3 && isjump && nbytes < 16)
29397         {
29398           int padsize = 15 - nbytes + min_insn_size (insn);
29399
29400           if (dump_file)
29401             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
29402                      INSN_UID (insn), padsize);
29403           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
29404         }
29405     }
29406 }
29407 #endif
29408
29409 /* AMD Athlon works faster
29410    when RET is not destination of conditional jump or directly preceded
29411    by other jump instruction.  We avoid the penalty by inserting NOP just
29412    before the RET instructions in such cases.  */
29413 static void
29414 ix86_pad_returns (void)
29415 {
29416   edge e;
29417   edge_iterator ei;
29418
29419   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
29420     {
29421       basic_block bb = e->src;
29422       rtx ret = BB_END (bb);
29423       rtx prev;
29424       bool replace = false;
29425
29426       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
29427           || optimize_bb_for_size_p (bb))
29428         continue;
29429       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
29430         if (active_insn_p (prev) || LABEL_P (prev))
29431           break;
29432       if (prev && LABEL_P (prev))
29433         {
29434           edge e;
29435           edge_iterator ei;
29436
29437           FOR_EACH_EDGE (e, ei, bb->preds)
29438             if (EDGE_FREQUENCY (e) && e->src->index >= 0
29439                 && !(e->flags & EDGE_FALLTHRU))
29440               replace = true;
29441         }
29442       if (!replace)
29443         {
29444           prev = prev_active_insn (ret);
29445           if (prev
29446               && ((JUMP_P (prev) && any_condjump_p (prev))
29447                   || CALL_P (prev)))
29448             replace = true;
29449           /* Empty functions get branch mispredict even when the jump destination
29450              is not visible to us.  */
29451           if (!prev && !optimize_function_for_size_p (cfun))
29452             replace = true;
29453         }
29454       if (replace)
29455         {
29456           emit_jump_insn_before (gen_return_internal_long (), ret);
29457           delete_insn (ret);
29458         }
29459     }
29460 }
29461
29462 /* Count the minimum number of instructions in BB.  Return 4 if the
29463    number of instructions >= 4.  */
29464
29465 static int 
29466 ix86_count_insn_bb (basic_block bb)
29467 {
29468   rtx insn;
29469   int insn_count = 0;
29470
29471   /* Count number of instructions in this block.  Return 4 if the number
29472      of instructions >= 4.  */
29473   FOR_BB_INSNS (bb, insn)
29474     {
29475       /* Only happen in exit blocks.  */
29476       if (JUMP_P (insn)
29477           && GET_CODE (PATTERN (insn)) == RETURN)
29478         break;
29479
29480       if (NONDEBUG_INSN_P (insn)
29481           && GET_CODE (PATTERN (insn)) != USE
29482           && GET_CODE (PATTERN (insn)) != CLOBBER)
29483         {
29484           insn_count++;
29485           if (insn_count >= 4)
29486             return insn_count;
29487         }
29488     }
29489
29490   return insn_count;
29491 }
29492
29493
29494 /* Count the minimum number of instructions in code path in BB.  
29495    Return 4 if the number of instructions >= 4.  */
29496
29497 static int 
29498 ix86_count_insn (basic_block bb)
29499 {
29500   edge e;
29501   edge_iterator ei;
29502   int min_prev_count;
29503
29504   /* Only bother counting instructions along paths with no
29505      more than 2 basic blocks between entry and exit.  Given
29506      that BB has an edge to exit, determine if a predecessor
29507      of BB has an edge from entry.  If so, compute the number
29508      of instructions in the predecessor block.  If there
29509      happen to be multiple such blocks, compute the minimum.  */
29510   min_prev_count = 4;
29511   FOR_EACH_EDGE (e, ei, bb->preds)
29512     {
29513       edge prev_e;
29514       edge_iterator prev_ei;
29515
29516       if (e->src == ENTRY_BLOCK_PTR)
29517         {
29518           min_prev_count = 0;
29519           break;
29520         }
29521       FOR_EACH_EDGE (prev_e, prev_ei, e->src->preds)
29522         {
29523           if (prev_e->src == ENTRY_BLOCK_PTR)
29524             {
29525               int count = ix86_count_insn_bb (e->src);
29526               if (count < min_prev_count)
29527                 min_prev_count = count;
29528               break;
29529             }
29530         }
29531     }
29532
29533   if (min_prev_count < 4)
29534     min_prev_count += ix86_count_insn_bb (bb);
29535
29536   return min_prev_count;
29537 }
29538
29539 /* Pad short funtion to 4 instructions.   */
29540
29541 static void
29542 ix86_pad_short_function (void)
29543 {
29544   edge e;
29545   edge_iterator ei;
29546
29547   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
29548     {
29549       rtx ret = BB_END (e->src);
29550       if (JUMP_P (ret) && GET_CODE (PATTERN (ret)) == RETURN)
29551         {
29552           int insn_count = ix86_count_insn (e->src);
29553
29554           /* Pad short function.  */
29555           if (insn_count < 4)
29556             {
29557               rtx insn = ret;
29558
29559               /* Find epilogue.  */
29560               while (insn
29561                      && (!NOTE_P (insn)
29562                          || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG))
29563                 insn = PREV_INSN (insn);
29564
29565               if (!insn)
29566                 insn = ret;
29567
29568               /* Two NOPs are counted as one instruction.  */
29569               insn_count = 2 * (4  - insn_count);
29570               emit_insn_before (gen_nops (GEN_INT (insn_count)), insn);
29571             }
29572         }
29573     }
29574 }
29575
29576 /* Implement machine specific optimizations.  We implement padding of returns
29577    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
29578 static void
29579 ix86_reorg (void)
29580 {
29581   if (optimize && optimize_function_for_speed_p (cfun))
29582     {
29583       if (TARGET_PAD_SHORT_FUNCTION)
29584         ix86_pad_short_function ();
29585       else if (TARGET_PAD_RETURNS)
29586         ix86_pad_returns ();
29587 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
29588       if (TARGET_FOUR_JUMP_LIMIT)
29589         ix86_avoid_jump_mispredicts ();
29590 #endif
29591     }
29592
29593   /* Run the vzeroupper optimization if needed.  */
29594   if (cfun->machine->use_vzeroupper_p)
29595     move_or_delete_vzeroupper ();
29596 }
29597
29598 /* Return nonzero when QImode register that must be represented via REX prefix
29599    is used.  */
29600 bool
29601 x86_extended_QIreg_mentioned_p (rtx insn)
29602 {
29603   int i;
29604   extract_insn_cached (insn);
29605   for (i = 0; i < recog_data.n_operands; i++)
29606     if (REG_P (recog_data.operand[i])
29607         && REGNO (recog_data.operand[i]) > BX_REG)
29608        return true;
29609   return false;
29610 }
29611
29612 /* Return nonzero when P points to register encoded via REX prefix.
29613    Called via for_each_rtx.  */
29614 static int
29615 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
29616 {
29617    unsigned int regno;
29618    if (!REG_P (*p))
29619      return 0;
29620    regno = REGNO (*p);
29621    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
29622 }
29623
29624 /* Return true when INSN mentions register that must be encoded using REX
29625    prefix.  */
29626 bool
29627 x86_extended_reg_mentioned_p (rtx insn)
29628 {
29629   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
29630                        extended_reg_mentioned_1, NULL);
29631 }
29632
29633 /* If profitable, negate (without causing overflow) integer constant
29634    of mode MODE at location LOC.  Return true in this case.  */
29635 bool
29636 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
29637 {
29638   HOST_WIDE_INT val;
29639
29640   if (!CONST_INT_P (*loc))
29641     return false;
29642
29643   switch (mode)
29644     {
29645     case DImode:
29646       /* DImode x86_64 constants must fit in 32 bits.  */
29647       gcc_assert (x86_64_immediate_operand (*loc, mode));
29648
29649       mode = SImode;
29650       break;
29651
29652     case SImode:
29653     case HImode:
29654     case QImode:
29655       break;
29656
29657     default:
29658       gcc_unreachable ();
29659     }
29660
29661   /* Avoid overflows.  */
29662   if (mode_signbit_p (mode, *loc))
29663     return false;
29664
29665   val = INTVAL (*loc);
29666
29667   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
29668      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
29669   if ((val < 0 && val != -128)
29670       || val == 128)
29671     {
29672       *loc = GEN_INT (-val);
29673       return true;
29674     }
29675
29676   return false;
29677 }
29678
29679 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
29680    optabs would emit if we didn't have TFmode patterns.  */
29681
29682 void
29683 x86_emit_floatuns (rtx operands[2])
29684 {
29685   rtx neglab, donelab, i0, i1, f0, in, out;
29686   enum machine_mode mode, inmode;
29687
29688   inmode = GET_MODE (operands[1]);
29689   gcc_assert (inmode == SImode || inmode == DImode);
29690
29691   out = operands[0];
29692   in = force_reg (inmode, operands[1]);
29693   mode = GET_MODE (out);
29694   neglab = gen_label_rtx ();
29695   donelab = gen_label_rtx ();
29696   f0 = gen_reg_rtx (mode);
29697
29698   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
29699
29700   expand_float (out, in, 0);
29701
29702   emit_jump_insn (gen_jump (donelab));
29703   emit_barrier ();
29704
29705   emit_label (neglab);
29706
29707   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
29708                             1, OPTAB_DIRECT);
29709   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
29710                             1, OPTAB_DIRECT);
29711   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
29712
29713   expand_float (f0, i0, 0);
29714
29715   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
29716
29717   emit_label (donelab);
29718 }
29719 \f
29720 /* AVX does not support 32-byte integer vector operations,
29721    thus the longest vector we are faced with is V16QImode.  */
29722 #define MAX_VECT_LEN    16
29723
29724 struct expand_vec_perm_d
29725 {
29726   rtx target, op0, op1;
29727   unsigned char perm[MAX_VECT_LEN];
29728   enum machine_mode vmode;
29729   unsigned char nelt;
29730   bool testing_p;
29731 };
29732
29733 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
29734 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
29735
29736 /* Get a vector mode of the same size as the original but with elements
29737    twice as wide.  This is only guaranteed to apply to integral vectors.  */
29738
29739 static inline enum machine_mode
29740 get_mode_wider_vector (enum machine_mode o)
29741 {
29742   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
29743   enum machine_mode n = GET_MODE_WIDER_MODE (o);
29744   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
29745   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
29746   return n;
29747 }
29748
29749 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
29750    with all elements equal to VAR.  Return true if successful.  */
29751
29752 static bool
29753 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
29754                                    rtx target, rtx val)
29755 {
29756   bool ok;
29757
29758   switch (mode)
29759     {
29760     case V2SImode:
29761     case V2SFmode:
29762       if (!mmx_ok)
29763         return false;
29764       /* FALLTHRU */
29765
29766     case V4DFmode:
29767     case V4DImode:
29768     case V8SFmode:
29769     case V8SImode:
29770     case V2DFmode:
29771     case V2DImode:
29772     case V4SFmode:
29773     case V4SImode:
29774       {
29775         rtx insn, dup;
29776
29777         /* First attempt to recognize VAL as-is.  */
29778         dup = gen_rtx_VEC_DUPLICATE (mode, val);
29779         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
29780         if (recog_memoized (insn) < 0)
29781           {
29782             rtx seq;
29783             /* If that fails, force VAL into a register.  */
29784
29785             start_sequence ();
29786             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
29787             seq = get_insns ();
29788             end_sequence ();
29789             if (seq)
29790               emit_insn_before (seq, insn);
29791
29792             ok = recog_memoized (insn) >= 0;
29793             gcc_assert (ok);
29794           }
29795       }
29796       return true;
29797
29798     case V4HImode:
29799       if (!mmx_ok)
29800         return false;
29801       if (TARGET_SSE || TARGET_3DNOW_A)
29802         {
29803           rtx x;
29804
29805           val = gen_lowpart (SImode, val);
29806           x = gen_rtx_TRUNCATE (HImode, val);
29807           x = gen_rtx_VEC_DUPLICATE (mode, x);
29808           emit_insn (gen_rtx_SET (VOIDmode, target, x));
29809           return true;
29810         }
29811       goto widen;
29812
29813     case V8QImode:
29814       if (!mmx_ok)
29815         return false;
29816       goto widen;
29817
29818     case V8HImode:
29819       if (TARGET_SSE2)
29820         {
29821           struct expand_vec_perm_d dperm;
29822           rtx tmp1, tmp2;
29823
29824         permute:
29825           memset (&dperm, 0, sizeof (dperm));
29826           dperm.target = target;
29827           dperm.vmode = mode;
29828           dperm.nelt = GET_MODE_NUNITS (mode);
29829           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
29830
29831           /* Extend to SImode using a paradoxical SUBREG.  */
29832           tmp1 = gen_reg_rtx (SImode);
29833           emit_move_insn (tmp1, gen_lowpart (SImode, val));
29834
29835           /* Insert the SImode value as low element of a V4SImode vector. */
29836           tmp2 = gen_lowpart (V4SImode, dperm.op0);
29837           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
29838
29839           ok = (expand_vec_perm_1 (&dperm)
29840                 || expand_vec_perm_broadcast_1 (&dperm));
29841           gcc_assert (ok);
29842           return ok;
29843         }
29844       goto widen;
29845
29846     case V16QImode:
29847       if (TARGET_SSE2)
29848         goto permute;
29849       goto widen;
29850
29851     widen:
29852       /* Replicate the value once into the next wider mode and recurse.  */
29853       {
29854         enum machine_mode smode, wsmode, wvmode;
29855         rtx x;
29856
29857         smode = GET_MODE_INNER (mode);
29858         wvmode = get_mode_wider_vector (mode);
29859         wsmode = GET_MODE_INNER (wvmode);
29860
29861         val = convert_modes (wsmode, smode, val, true);
29862         x = expand_simple_binop (wsmode, ASHIFT, val,
29863                                  GEN_INT (GET_MODE_BITSIZE (smode)),
29864                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
29865         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
29866
29867         x = gen_lowpart (wvmode, target);
29868         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
29869         gcc_assert (ok);
29870         return ok;
29871       }
29872
29873     case V16HImode:
29874     case V32QImode:
29875       {
29876         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
29877         rtx x = gen_reg_rtx (hvmode);
29878
29879         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
29880         gcc_assert (ok);
29881
29882         x = gen_rtx_VEC_CONCAT (mode, x, x);
29883         emit_insn (gen_rtx_SET (VOIDmode, target, x));
29884       }
29885       return true;
29886
29887     default:
29888       return false;
29889     }
29890 }
29891
29892 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
29893    whose ONE_VAR element is VAR, and other elements are zero.  Return true
29894    if successful.  */
29895
29896 static bool
29897 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
29898                                      rtx target, rtx var, int one_var)
29899 {
29900   enum machine_mode vsimode;
29901   rtx new_target;
29902   rtx x, tmp;
29903   bool use_vector_set = false;
29904
29905   switch (mode)
29906     {
29907     case V2DImode:
29908       /* For SSE4.1, we normally use vector set.  But if the second
29909          element is zero and inter-unit moves are OK, we use movq
29910          instead.  */
29911       use_vector_set = (TARGET_64BIT
29912                         && TARGET_SSE4_1
29913                         && !(TARGET_INTER_UNIT_MOVES
29914                              && one_var == 0));
29915       break;
29916     case V16QImode:
29917     case V4SImode:
29918     case V4SFmode:
29919       use_vector_set = TARGET_SSE4_1;
29920       break;
29921     case V8HImode:
29922       use_vector_set = TARGET_SSE2;
29923       break;
29924     case V4HImode:
29925       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
29926       break;
29927     case V32QImode:
29928     case V16HImode:
29929     case V8SImode:
29930     case V8SFmode:
29931     case V4DFmode:
29932       use_vector_set = TARGET_AVX;
29933       break;
29934     case V4DImode:
29935       /* Use ix86_expand_vector_set in 64bit mode only.  */
29936       use_vector_set = TARGET_AVX && TARGET_64BIT;
29937       break;
29938     default:
29939       break;
29940     }
29941
29942   if (use_vector_set)
29943     {
29944       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
29945       var = force_reg (GET_MODE_INNER (mode), var);
29946       ix86_expand_vector_set (mmx_ok, target, var, one_var);
29947       return true;
29948     }
29949
29950   switch (mode)
29951     {
29952     case V2SFmode:
29953     case V2SImode:
29954       if (!mmx_ok)
29955         return false;
29956       /* FALLTHRU */
29957
29958     case V2DFmode:
29959     case V2DImode:
29960       if (one_var != 0)
29961         return false;
29962       var = force_reg (GET_MODE_INNER (mode), var);
29963       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
29964       emit_insn (gen_rtx_SET (VOIDmode, target, x));
29965       return true;
29966
29967     case V4SFmode:
29968     case V4SImode:
29969       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
29970         new_target = gen_reg_rtx (mode);
29971       else
29972         new_target = target;
29973       var = force_reg (GET_MODE_INNER (mode), var);
29974       x = gen_rtx_VEC_DUPLICATE (mode, var);
29975       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
29976       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
29977       if (one_var != 0)
29978         {
29979           /* We need to shuffle the value to the correct position, so
29980              create a new pseudo to store the intermediate result.  */
29981
29982           /* With SSE2, we can use the integer shuffle insns.  */
29983           if (mode != V4SFmode && TARGET_SSE2)
29984             {
29985               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
29986                                             const1_rtx,
29987                                             GEN_INT (one_var == 1 ? 0 : 1),
29988                                             GEN_INT (one_var == 2 ? 0 : 1),
29989                                             GEN_INT (one_var == 3 ? 0 : 1)));
29990               if (target != new_target)
29991                 emit_move_insn (target, new_target);
29992               return true;
29993             }
29994
29995           /* Otherwise convert the intermediate result to V4SFmode and
29996              use the SSE1 shuffle instructions.  */
29997           if (mode != V4SFmode)
29998             {
29999               tmp = gen_reg_rtx (V4SFmode);
30000               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
30001             }
30002           else
30003             tmp = new_target;
30004
30005           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
30006                                        const1_rtx,
30007                                        GEN_INT (one_var == 1 ? 0 : 1),
30008                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
30009                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
30010
30011           if (mode != V4SFmode)
30012             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
30013           else if (tmp != target)
30014             emit_move_insn (target, tmp);
30015         }
30016       else if (target != new_target)
30017         emit_move_insn (target, new_target);
30018       return true;
30019
30020     case V8HImode:
30021     case V16QImode:
30022       vsimode = V4SImode;
30023       goto widen;
30024     case V4HImode:
30025     case V8QImode:
30026       if (!mmx_ok)
30027         return false;
30028       vsimode = V2SImode;
30029       goto widen;
30030     widen:
30031       if (one_var != 0)
30032         return false;
30033
30034       /* Zero extend the variable element to SImode and recurse.  */
30035       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
30036
30037       x = gen_reg_rtx (vsimode);
30038       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
30039                                                 var, one_var))
30040         gcc_unreachable ();
30041
30042       emit_move_insn (target, gen_lowpart (mode, x));
30043       return true;
30044
30045     default:
30046       return false;
30047     }
30048 }
30049
30050 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
30051    consisting of the values in VALS.  It is known that all elements
30052    except ONE_VAR are constants.  Return true if successful.  */
30053
30054 static bool
30055 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
30056                                  rtx target, rtx vals, int one_var)
30057 {
30058   rtx var = XVECEXP (vals, 0, one_var);
30059   enum machine_mode wmode;
30060   rtx const_vec, x;
30061
30062   const_vec = copy_rtx (vals);
30063   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
30064   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
30065
30066   switch (mode)
30067     {
30068     case V2DFmode:
30069     case V2DImode:
30070     case V2SFmode:
30071     case V2SImode:
30072       /* For the two element vectors, it's just as easy to use
30073          the general case.  */
30074       return false;
30075
30076     case V4DImode:
30077       /* Use ix86_expand_vector_set in 64bit mode only.  */
30078       if (!TARGET_64BIT)
30079         return false;
30080     case V4DFmode:
30081     case V8SFmode:
30082     case V8SImode:
30083     case V16HImode:
30084     case V32QImode:
30085     case V4SFmode:
30086     case V4SImode:
30087     case V8HImode:
30088     case V4HImode:
30089       break;
30090
30091     case V16QImode:
30092       if (TARGET_SSE4_1)
30093         break;
30094       wmode = V8HImode;
30095       goto widen;
30096     case V8QImode:
30097       wmode = V4HImode;
30098       goto widen;
30099     widen:
30100       /* There's no way to set one QImode entry easily.  Combine
30101          the variable value with its adjacent constant value, and
30102          promote to an HImode set.  */
30103       x = XVECEXP (vals, 0, one_var ^ 1);
30104       if (one_var & 1)
30105         {
30106           var = convert_modes (HImode, QImode, var, true);
30107           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
30108                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
30109           x = GEN_INT (INTVAL (x) & 0xff);
30110         }
30111       else
30112         {
30113           var = convert_modes (HImode, QImode, var, true);
30114           x = gen_int_mode (INTVAL (x) << 8, HImode);
30115         }
30116       if (x != const0_rtx)
30117         var = expand_simple_binop (HImode, IOR, var, x, var,
30118                                    1, OPTAB_LIB_WIDEN);
30119
30120       x = gen_reg_rtx (wmode);
30121       emit_move_insn (x, gen_lowpart (wmode, const_vec));
30122       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
30123
30124       emit_move_insn (target, gen_lowpart (mode, x));
30125       return true;
30126
30127     default:
30128       return false;
30129     }
30130
30131   emit_move_insn (target, const_vec);
30132   ix86_expand_vector_set (mmx_ok, target, var, one_var);
30133   return true;
30134 }
30135
30136 /* A subroutine of ix86_expand_vector_init_general.  Use vector
30137    concatenate to handle the most general case: all values variable,
30138    and none identical.  */
30139
30140 static void
30141 ix86_expand_vector_init_concat (enum machine_mode mode,
30142                                 rtx target, rtx *ops, int n)
30143 {
30144   enum machine_mode cmode, hmode = VOIDmode;
30145   rtx first[8], second[4];
30146   rtvec v;
30147   int i, j;
30148
30149   switch (n)
30150     {
30151     case 2:
30152       switch (mode)
30153         {
30154         case V8SImode:
30155           cmode = V4SImode;
30156           break;
30157         case V8SFmode:
30158           cmode = V4SFmode;
30159           break;
30160         case V4DImode:
30161           cmode = V2DImode;
30162           break;
30163         case V4DFmode:
30164           cmode = V2DFmode;
30165           break;
30166         case V4SImode:
30167           cmode = V2SImode;
30168           break;
30169         case V4SFmode:
30170           cmode = V2SFmode;
30171           break;
30172         case V2DImode:
30173           cmode = DImode;
30174           break;
30175         case V2SImode:
30176           cmode = SImode;
30177           break;
30178         case V2DFmode:
30179           cmode = DFmode;
30180           break;
30181         case V2SFmode:
30182           cmode = SFmode;
30183           break;
30184         default:
30185           gcc_unreachable ();
30186         }
30187
30188       if (!register_operand (ops[1], cmode))
30189         ops[1] = force_reg (cmode, ops[1]);
30190       if (!register_operand (ops[0], cmode))
30191         ops[0] = force_reg (cmode, ops[0]);
30192       emit_insn (gen_rtx_SET (VOIDmode, target,
30193                               gen_rtx_VEC_CONCAT (mode, ops[0],
30194                                                   ops[1])));
30195       break;
30196
30197     case 4:
30198       switch (mode)
30199         {
30200         case V4DImode:
30201           cmode = V2DImode;
30202           break;
30203         case V4DFmode:
30204           cmode = V2DFmode;
30205           break;
30206         case V4SImode:
30207           cmode = V2SImode;
30208           break;
30209         case V4SFmode:
30210           cmode = V2SFmode;
30211           break;
30212         default:
30213           gcc_unreachable ();
30214         }
30215       goto half;
30216
30217     case 8:
30218       switch (mode)
30219         {
30220         case V8SImode:
30221           cmode = V2SImode;
30222           hmode = V4SImode;
30223           break;
30224         case V8SFmode:
30225           cmode = V2SFmode;
30226           hmode = V4SFmode;
30227           break;
30228         default:
30229           gcc_unreachable ();
30230         }
30231       goto half;
30232
30233 half:
30234       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
30235       i = n - 1;
30236       j = (n >> 1) - 1;
30237       for (; i > 0; i -= 2, j--)
30238         {
30239           first[j] = gen_reg_rtx (cmode);
30240           v = gen_rtvec (2, ops[i - 1], ops[i]);
30241           ix86_expand_vector_init (false, first[j],
30242                                    gen_rtx_PARALLEL (cmode, v));
30243         }
30244
30245       n >>= 1;
30246       if (n > 2)
30247         {
30248           gcc_assert (hmode != VOIDmode);
30249           for (i = j = 0; i < n; i += 2, j++)
30250             {
30251               second[j] = gen_reg_rtx (hmode);
30252               ix86_expand_vector_init_concat (hmode, second [j],
30253                                               &first [i], 2);
30254             }
30255           n >>= 1;
30256           ix86_expand_vector_init_concat (mode, target, second, n);
30257         }
30258       else
30259         ix86_expand_vector_init_concat (mode, target, first, n);
30260       break;
30261
30262     default:
30263       gcc_unreachable ();
30264     }
30265 }
30266
30267 /* A subroutine of ix86_expand_vector_init_general.  Use vector
30268    interleave to handle the most general case: all values variable,
30269    and none identical.  */
30270
30271 static void
30272 ix86_expand_vector_init_interleave (enum machine_mode mode,
30273                                     rtx target, rtx *ops, int n)
30274 {
30275   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
30276   int i, j;
30277   rtx op0, op1;
30278   rtx (*gen_load_even) (rtx, rtx, rtx);
30279   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
30280   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
30281
30282   switch (mode)
30283     {
30284     case V8HImode:
30285       gen_load_even = gen_vec_setv8hi;
30286       gen_interleave_first_low = gen_vec_interleave_lowv4si;
30287       gen_interleave_second_low = gen_vec_interleave_lowv2di;
30288       inner_mode = HImode;
30289       first_imode = V4SImode;
30290       second_imode = V2DImode;
30291       third_imode = VOIDmode;
30292       break;
30293     case V16QImode:
30294       gen_load_even = gen_vec_setv16qi;
30295       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
30296       gen_interleave_second_low = gen_vec_interleave_lowv4si;
30297       inner_mode = QImode;
30298       first_imode = V8HImode;
30299       second_imode = V4SImode;
30300       third_imode = V2DImode;
30301       break;
30302     default:
30303       gcc_unreachable ();
30304     }
30305
30306   for (i = 0; i < n; i++)
30307     {
30308       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
30309       op0 = gen_reg_rtx (SImode);
30310       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
30311
30312       /* Insert the SImode value as low element of V4SImode vector. */
30313       op1 = gen_reg_rtx (V4SImode);
30314       op0 = gen_rtx_VEC_MERGE (V4SImode,
30315                                gen_rtx_VEC_DUPLICATE (V4SImode,
30316                                                       op0),
30317                                CONST0_RTX (V4SImode),
30318                                const1_rtx);
30319       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
30320
30321       /* Cast the V4SImode vector back to a vector in orignal mode.  */
30322       op0 = gen_reg_rtx (mode);
30323       emit_move_insn (op0, gen_lowpart (mode, op1));
30324
30325       /* Load even elements into the second positon.  */
30326       emit_insn (gen_load_even (op0,
30327                                 force_reg (inner_mode,
30328                                            ops [i + i + 1]),
30329                                 const1_rtx));
30330
30331       /* Cast vector to FIRST_IMODE vector.  */
30332       ops[i] = gen_reg_rtx (first_imode);
30333       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
30334     }
30335
30336   /* Interleave low FIRST_IMODE vectors.  */
30337   for (i = j = 0; i < n; i += 2, j++)
30338     {
30339       op0 = gen_reg_rtx (first_imode);
30340       emit_insn (gen_interleave_first_low (op0, ops[i], ops[i + 1]));
30341
30342       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
30343       ops[j] = gen_reg_rtx (second_imode);
30344       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
30345     }
30346
30347   /* Interleave low SECOND_IMODE vectors.  */
30348   switch (second_imode)
30349     {
30350     case V4SImode:
30351       for (i = j = 0; i < n / 2; i += 2, j++)
30352         {
30353           op0 = gen_reg_rtx (second_imode);
30354           emit_insn (gen_interleave_second_low (op0, ops[i],
30355                                                 ops[i + 1]));
30356
30357           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
30358              vector.  */
30359           ops[j] = gen_reg_rtx (third_imode);
30360           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
30361         }
30362       second_imode = V2DImode;
30363       gen_interleave_second_low = gen_vec_interleave_lowv2di;
30364       /* FALLTHRU */
30365
30366     case V2DImode:
30367       op0 = gen_reg_rtx (second_imode);
30368       emit_insn (gen_interleave_second_low (op0, ops[0],
30369                                             ops[1]));
30370
30371       /* Cast the SECOND_IMODE vector back to a vector on original
30372          mode.  */
30373       emit_insn (gen_rtx_SET (VOIDmode, target,
30374                               gen_lowpart (mode, op0)));
30375       break;
30376
30377     default:
30378       gcc_unreachable ();
30379     }
30380 }
30381
30382 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
30383    all values variable, and none identical.  */
30384
30385 static void
30386 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
30387                                  rtx target, rtx vals)
30388 {
30389   rtx ops[32], op0, op1;
30390   enum machine_mode half_mode = VOIDmode;
30391   int n, i;
30392
30393   switch (mode)
30394     {
30395     case V2SFmode:
30396     case V2SImode:
30397       if (!mmx_ok && !TARGET_SSE)
30398         break;
30399       /* FALLTHRU */
30400
30401     case V8SFmode:
30402     case V8SImode:
30403     case V4DFmode:
30404     case V4DImode:
30405     case V4SFmode:
30406     case V4SImode:
30407     case V2DFmode:
30408     case V2DImode:
30409       n = GET_MODE_NUNITS (mode);
30410       for (i = 0; i < n; i++)
30411         ops[i] = XVECEXP (vals, 0, i);
30412       ix86_expand_vector_init_concat (mode, target, ops, n);
30413       return;
30414
30415     case V32QImode:
30416       half_mode = V16QImode;
30417       goto half;
30418
30419     case V16HImode:
30420       half_mode = V8HImode;
30421       goto half;
30422
30423 half:
30424       n = GET_MODE_NUNITS (mode);
30425       for (i = 0; i < n; i++)
30426         ops[i] = XVECEXP (vals, 0, i);
30427       op0 = gen_reg_rtx (half_mode);
30428       op1 = gen_reg_rtx (half_mode);
30429       ix86_expand_vector_init_interleave (half_mode, op0, ops,
30430                                           n >> 2);
30431       ix86_expand_vector_init_interleave (half_mode, op1,
30432                                           &ops [n >> 1], n >> 2);
30433       emit_insn (gen_rtx_SET (VOIDmode, target,
30434                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
30435       return;
30436
30437     case V16QImode:
30438       if (!TARGET_SSE4_1)
30439         break;
30440       /* FALLTHRU */
30441
30442     case V8HImode:
30443       if (!TARGET_SSE2)
30444         break;
30445
30446       /* Don't use ix86_expand_vector_init_interleave if we can't
30447          move from GPR to SSE register directly.  */
30448       if (!TARGET_INTER_UNIT_MOVES)
30449         break;
30450
30451       n = GET_MODE_NUNITS (mode);
30452       for (i = 0; i < n; i++)
30453         ops[i] = XVECEXP (vals, 0, i);
30454       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
30455       return;
30456
30457     case V4HImode:
30458     case V8QImode:
30459       break;
30460
30461     default:
30462       gcc_unreachable ();
30463     }
30464
30465     {
30466       int i, j, n_elts, n_words, n_elt_per_word;
30467       enum machine_mode inner_mode;
30468       rtx words[4], shift;
30469
30470       inner_mode = GET_MODE_INNER (mode);
30471       n_elts = GET_MODE_NUNITS (mode);
30472       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
30473       n_elt_per_word = n_elts / n_words;
30474       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
30475
30476       for (i = 0; i < n_words; ++i)
30477         {
30478           rtx word = NULL_RTX;
30479
30480           for (j = 0; j < n_elt_per_word; ++j)
30481             {
30482               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
30483               elt = convert_modes (word_mode, inner_mode, elt, true);
30484
30485               if (j == 0)
30486                 word = elt;
30487               else
30488                 {
30489                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
30490                                               word, 1, OPTAB_LIB_WIDEN);
30491                   word = expand_simple_binop (word_mode, IOR, word, elt,
30492                                               word, 1, OPTAB_LIB_WIDEN);
30493                 }
30494             }
30495
30496           words[i] = word;
30497         }
30498
30499       if (n_words == 1)
30500         emit_move_insn (target, gen_lowpart (mode, words[0]));
30501       else if (n_words == 2)
30502         {
30503           rtx tmp = gen_reg_rtx (mode);
30504           emit_clobber (tmp);
30505           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
30506           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
30507           emit_move_insn (target, tmp);
30508         }
30509       else if (n_words == 4)
30510         {
30511           rtx tmp = gen_reg_rtx (V4SImode);
30512           gcc_assert (word_mode == SImode);
30513           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
30514           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
30515           emit_move_insn (target, gen_lowpart (mode, tmp));
30516         }
30517       else
30518         gcc_unreachable ();
30519     }
30520 }
30521
30522 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
30523    instructions unless MMX_OK is true.  */
30524
30525 void
30526 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
30527 {
30528   enum machine_mode mode = GET_MODE (target);
30529   enum machine_mode inner_mode = GET_MODE_INNER (mode);
30530   int n_elts = GET_MODE_NUNITS (mode);
30531   int n_var = 0, one_var = -1;
30532   bool all_same = true, all_const_zero = true;
30533   int i;
30534   rtx x;
30535
30536   for (i = 0; i < n_elts; ++i)
30537     {
30538       x = XVECEXP (vals, 0, i);
30539       if (!(CONST_INT_P (x)
30540             || GET_CODE (x) == CONST_DOUBLE
30541             || GET_CODE (x) == CONST_FIXED))
30542         n_var++, one_var = i;
30543       else if (x != CONST0_RTX (inner_mode))
30544         all_const_zero = false;
30545       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
30546         all_same = false;
30547     }
30548
30549   /* Constants are best loaded from the constant pool.  */
30550   if (n_var == 0)
30551     {
30552       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
30553       return;
30554     }
30555
30556   /* If all values are identical, broadcast the value.  */
30557   if (all_same
30558       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
30559                                             XVECEXP (vals, 0, 0)))
30560     return;
30561
30562   /* Values where only one field is non-constant are best loaded from
30563      the pool and overwritten via move later.  */
30564   if (n_var == 1)
30565     {
30566       if (all_const_zero
30567           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
30568                                                   XVECEXP (vals, 0, one_var),
30569                                                   one_var))
30570         return;
30571
30572       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
30573         return;
30574     }
30575
30576   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
30577 }
30578
30579 void
30580 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
30581 {
30582   enum machine_mode mode = GET_MODE (target);
30583   enum machine_mode inner_mode = GET_MODE_INNER (mode);
30584   enum machine_mode half_mode;
30585   bool use_vec_merge = false;
30586   rtx tmp;
30587   static rtx (*gen_extract[6][2]) (rtx, rtx)
30588     = {
30589         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
30590         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
30591         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
30592         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
30593         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
30594         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
30595       };
30596   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
30597     = {
30598         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
30599         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
30600         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
30601         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
30602         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
30603         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
30604       };
30605   int i, j, n;
30606
30607   switch (mode)
30608     {
30609     case V2SFmode:
30610     case V2SImode:
30611       if (mmx_ok)
30612         {
30613           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
30614           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
30615           if (elt == 0)
30616             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
30617           else
30618             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
30619           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
30620           return;
30621         }
30622       break;
30623
30624     case V2DImode:
30625       use_vec_merge = TARGET_SSE4_1;
30626       if (use_vec_merge)
30627         break;
30628
30629     case V2DFmode:
30630       {
30631         rtx op0, op1;
30632
30633         /* For the two element vectors, we implement a VEC_CONCAT with
30634            the extraction of the other element.  */
30635
30636         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
30637         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
30638
30639         if (elt == 0)
30640           op0 = val, op1 = tmp;
30641         else
30642           op0 = tmp, op1 = val;
30643
30644         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
30645         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
30646       }
30647       return;
30648
30649     case V4SFmode:
30650       use_vec_merge = TARGET_SSE4_1;
30651       if (use_vec_merge)
30652         break;
30653
30654       switch (elt)
30655         {
30656         case 0:
30657           use_vec_merge = true;
30658           break;
30659
30660         case 1:
30661           /* tmp = target = A B C D */
30662           tmp = copy_to_reg (target);
30663           /* target = A A B B */
30664           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
30665           /* target = X A B B */
30666           ix86_expand_vector_set (false, target, val, 0);
30667           /* target = A X C D  */
30668           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
30669                                           const1_rtx, const0_rtx,
30670                                           GEN_INT (2+4), GEN_INT (3+4)));
30671           return;
30672
30673         case 2:
30674           /* tmp = target = A B C D */
30675           tmp = copy_to_reg (target);
30676           /* tmp = X B C D */
30677           ix86_expand_vector_set (false, tmp, val, 0);
30678           /* target = A B X D */
30679           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
30680                                           const0_rtx, const1_rtx,
30681                                           GEN_INT (0+4), GEN_INT (3+4)));
30682           return;
30683
30684         case 3:
30685           /* tmp = target = A B C D */
30686           tmp = copy_to_reg (target);
30687           /* tmp = X B C D */
30688           ix86_expand_vector_set (false, tmp, val, 0);
30689           /* target = A B X D */
30690           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
30691                                           const0_rtx, const1_rtx,
30692                                           GEN_INT (2+4), GEN_INT (0+4)));
30693           return;
30694
30695         default:
30696           gcc_unreachable ();
30697         }
30698       break;
30699
30700     case V4SImode:
30701       use_vec_merge = TARGET_SSE4_1;
30702       if (use_vec_merge)
30703         break;
30704
30705       /* Element 0 handled by vec_merge below.  */
30706       if (elt == 0)
30707         {
30708           use_vec_merge = true;
30709           break;
30710         }
30711
30712       if (TARGET_SSE2)
30713         {
30714           /* With SSE2, use integer shuffles to swap element 0 and ELT,
30715              store into element 0, then shuffle them back.  */
30716
30717           rtx order[4];
30718
30719           order[0] = GEN_INT (elt);
30720           order[1] = const1_rtx;
30721           order[2] = const2_rtx;
30722           order[3] = GEN_INT (3);
30723           order[elt] = const0_rtx;
30724
30725           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
30726                                         order[1], order[2], order[3]));
30727
30728           ix86_expand_vector_set (false, target, val, 0);
30729
30730           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
30731                                         order[1], order[2], order[3]));
30732         }
30733       else
30734         {
30735           /* For SSE1, we have to reuse the V4SF code.  */
30736           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
30737                                   gen_lowpart (SFmode, val), elt);
30738         }
30739       return;
30740
30741     case V8HImode:
30742       use_vec_merge = TARGET_SSE2;
30743       break;
30744     case V4HImode:
30745       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
30746       break;
30747
30748     case V16QImode:
30749       use_vec_merge = TARGET_SSE4_1;
30750       break;
30751
30752     case V8QImode:
30753       break;
30754
30755     case V32QImode:
30756       half_mode = V16QImode;
30757       j = 0;
30758       n = 16;
30759       goto half;
30760
30761     case V16HImode:
30762       half_mode = V8HImode;
30763       j = 1;
30764       n = 8;
30765       goto half;
30766
30767     case V8SImode:
30768       half_mode = V4SImode;
30769       j = 2;
30770       n = 4;
30771       goto half;
30772
30773     case V4DImode:
30774       half_mode = V2DImode;
30775       j = 3;
30776       n = 2;
30777       goto half;
30778
30779     case V8SFmode:
30780       half_mode = V4SFmode;
30781       j = 4;
30782       n = 4;
30783       goto half;
30784
30785     case V4DFmode:
30786       half_mode = V2DFmode;
30787       j = 5;
30788       n = 2;
30789       goto half;
30790
30791 half:
30792       /* Compute offset.  */
30793       i = elt / n;
30794       elt %= n;
30795
30796       gcc_assert (i <= 1);
30797
30798       /* Extract the half.  */
30799       tmp = gen_reg_rtx (half_mode);
30800       emit_insn (gen_extract[j][i] (tmp, target));
30801
30802       /* Put val in tmp at elt.  */
30803       ix86_expand_vector_set (false, tmp, val, elt);
30804
30805       /* Put it back.  */
30806       emit_insn (gen_insert[j][i] (target, target, tmp));
30807       return;
30808
30809     default:
30810       break;
30811     }
30812
30813   if (use_vec_merge)
30814     {
30815       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
30816       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
30817       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
30818     }
30819   else
30820     {
30821       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
30822
30823       emit_move_insn (mem, target);
30824
30825       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
30826       emit_move_insn (tmp, val);
30827
30828       emit_move_insn (target, mem);
30829     }
30830 }
30831
30832 void
30833 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
30834 {
30835   enum machine_mode mode = GET_MODE (vec);
30836   enum machine_mode inner_mode = GET_MODE_INNER (mode);
30837   bool use_vec_extr = false;
30838   rtx tmp;
30839
30840   switch (mode)
30841     {
30842     case V2SImode:
30843     case V2SFmode:
30844       if (!mmx_ok)
30845         break;
30846       /* FALLTHRU */
30847
30848     case V2DFmode:
30849     case V2DImode:
30850       use_vec_extr = true;
30851       break;
30852
30853     case V4SFmode:
30854       use_vec_extr = TARGET_SSE4_1;
30855       if (use_vec_extr)
30856         break;
30857
30858       switch (elt)
30859         {
30860         case 0:
30861           tmp = vec;
30862           break;
30863
30864         case 1:
30865         case 3:
30866           tmp = gen_reg_rtx (mode);
30867           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
30868                                        GEN_INT (elt), GEN_INT (elt),
30869                                        GEN_INT (elt+4), GEN_INT (elt+4)));
30870           break;
30871
30872         case 2:
30873           tmp = gen_reg_rtx (mode);
30874           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
30875           break;
30876
30877         default:
30878           gcc_unreachable ();
30879         }
30880       vec = tmp;
30881       use_vec_extr = true;
30882       elt = 0;
30883       break;
30884
30885     case V4SImode:
30886       use_vec_extr = TARGET_SSE4_1;
30887       if (use_vec_extr)
30888         break;
30889
30890       if (TARGET_SSE2)
30891         {
30892           switch (elt)
30893             {
30894             case 0:
30895               tmp = vec;
30896               break;
30897
30898             case 1:
30899             case 3:
30900               tmp = gen_reg_rtx (mode);
30901               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
30902                                             GEN_INT (elt), GEN_INT (elt),
30903                                             GEN_INT (elt), GEN_INT (elt)));
30904               break;
30905
30906             case 2:
30907               tmp = gen_reg_rtx (mode);
30908               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
30909               break;
30910
30911             default:
30912               gcc_unreachable ();
30913             }
30914           vec = tmp;
30915           use_vec_extr = true;
30916           elt = 0;
30917         }
30918       else
30919         {
30920           /* For SSE1, we have to reuse the V4SF code.  */
30921           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
30922                                       gen_lowpart (V4SFmode, vec), elt);
30923           return;
30924         }
30925       break;
30926
30927     case V8HImode:
30928       use_vec_extr = TARGET_SSE2;
30929       break;
30930     case V4HImode:
30931       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
30932       break;
30933
30934     case V16QImode:
30935       use_vec_extr = TARGET_SSE4_1;
30936       break;
30937
30938     case V8QImode:
30939       /* ??? Could extract the appropriate HImode element and shift.  */
30940     default:
30941       break;
30942     }
30943
30944   if (use_vec_extr)
30945     {
30946       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
30947       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
30948
30949       /* Let the rtl optimizers know about the zero extension performed.  */
30950       if (inner_mode == QImode || inner_mode == HImode)
30951         {
30952           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
30953           target = gen_lowpart (SImode, target);
30954         }
30955
30956       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
30957     }
30958   else
30959     {
30960       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
30961
30962       emit_move_insn (mem, vec);
30963
30964       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
30965       emit_move_insn (target, tmp);
30966     }
30967 }
30968
30969 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
30970    pattern to reduce; DEST is the destination; IN is the input vector.  */
30971
30972 void
30973 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
30974 {
30975   rtx tmp1, tmp2, tmp3;
30976
30977   tmp1 = gen_reg_rtx (V4SFmode);
30978   tmp2 = gen_reg_rtx (V4SFmode);
30979   tmp3 = gen_reg_rtx (V4SFmode);
30980
30981   emit_insn (gen_sse_movhlps (tmp1, in, in));
30982   emit_insn (fn (tmp2, tmp1, in));
30983
30984   emit_insn (gen_sse_shufps_v4sf (tmp3, tmp2, tmp2,
30985                                   const1_rtx, const1_rtx,
30986                                   GEN_INT (1+4), GEN_INT (1+4)));
30987   emit_insn (fn (dest, tmp2, tmp3));
30988 }
30989 \f
30990 /* Target hook for scalar_mode_supported_p.  */
30991 static bool
30992 ix86_scalar_mode_supported_p (enum machine_mode mode)
30993 {
30994   if (DECIMAL_FLOAT_MODE_P (mode))
30995     return default_decimal_float_supported_p ();
30996   else if (mode == TFmode)
30997     return true;
30998   else
30999     return default_scalar_mode_supported_p (mode);
31000 }
31001
31002 /* Implements target hook vector_mode_supported_p.  */
31003 static bool
31004 ix86_vector_mode_supported_p (enum machine_mode mode)
31005 {
31006   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
31007     return true;
31008   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
31009     return true;
31010   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
31011     return true;
31012   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
31013     return true;
31014   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
31015     return true;
31016   return false;
31017 }
31018
31019 /* Target hook for c_mode_for_suffix.  */
31020 static enum machine_mode
31021 ix86_c_mode_for_suffix (char suffix)
31022 {
31023   if (suffix == 'q')
31024     return TFmode;
31025   if (suffix == 'w')
31026     return XFmode;
31027
31028   return VOIDmode;
31029 }
31030
31031 /* Worker function for TARGET_MD_ASM_CLOBBERS.
31032
31033    We do this in the new i386 backend to maintain source compatibility
31034    with the old cc0-based compiler.  */
31035
31036 static tree
31037 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
31038                       tree inputs ATTRIBUTE_UNUSED,
31039                       tree clobbers)
31040 {
31041   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
31042                         clobbers);
31043   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
31044                         clobbers);
31045   return clobbers;
31046 }
31047
31048 /* Implements target vector targetm.asm.encode_section_info.  This
31049    is not used by netware.  */
31050
31051 static void ATTRIBUTE_UNUSED
31052 ix86_encode_section_info (tree decl, rtx rtl, int first)
31053 {
31054   default_encode_section_info (decl, rtl, first);
31055
31056   if (TREE_CODE (decl) == VAR_DECL
31057       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
31058       && ix86_in_large_data_p (decl))
31059     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
31060 }
31061
31062 /* Worker function for REVERSE_CONDITION.  */
31063
31064 enum rtx_code
31065 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
31066 {
31067   return (mode != CCFPmode && mode != CCFPUmode
31068           ? reverse_condition (code)
31069           : reverse_condition_maybe_unordered (code));
31070 }
31071
31072 /* Output code to perform an x87 FP register move, from OPERANDS[1]
31073    to OPERANDS[0].  */
31074
31075 const char *
31076 output_387_reg_move (rtx insn, rtx *operands)
31077 {
31078   if (REG_P (operands[0]))
31079     {
31080       if (REG_P (operands[1])
31081           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
31082         {
31083           if (REGNO (operands[0]) == FIRST_STACK_REG)
31084             return output_387_ffreep (operands, 0);
31085           return "fstp\t%y0";
31086         }
31087       if (STACK_TOP_P (operands[0]))
31088         return "fld%Z1\t%y1";
31089       return "fst\t%y0";
31090     }
31091   else if (MEM_P (operands[0]))
31092     {
31093       gcc_assert (REG_P (operands[1]));
31094       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
31095         return "fstp%Z0\t%y0";
31096       else
31097         {
31098           /* There is no non-popping store to memory for XFmode.
31099              So if we need one, follow the store with a load.  */
31100           if (GET_MODE (operands[0]) == XFmode)
31101             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
31102           else
31103             return "fst%Z0\t%y0";
31104         }
31105     }
31106   else
31107     gcc_unreachable();
31108 }
31109
31110 /* Output code to perform a conditional jump to LABEL, if C2 flag in
31111    FP status register is set.  */
31112
31113 void
31114 ix86_emit_fp_unordered_jump (rtx label)
31115 {
31116   rtx reg = gen_reg_rtx (HImode);
31117   rtx temp;
31118
31119   emit_insn (gen_x86_fnstsw_1 (reg));
31120
31121   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
31122     {
31123       emit_insn (gen_x86_sahf_1 (reg));
31124
31125       temp = gen_rtx_REG (CCmode, FLAGS_REG);
31126       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
31127     }
31128   else
31129     {
31130       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
31131
31132       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
31133       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
31134     }
31135
31136   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
31137                               gen_rtx_LABEL_REF (VOIDmode, label),
31138                               pc_rtx);
31139   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
31140
31141   emit_jump_insn (temp);
31142   predict_jump (REG_BR_PROB_BASE * 10 / 100);
31143 }
31144
31145 /* Output code to perform a log1p XFmode calculation.  */
31146
31147 void ix86_emit_i387_log1p (rtx op0, rtx op1)
31148 {
31149   rtx label1 = gen_label_rtx ();
31150   rtx label2 = gen_label_rtx ();
31151
31152   rtx tmp = gen_reg_rtx (XFmode);
31153   rtx tmp2 = gen_reg_rtx (XFmode);
31154   rtx test;
31155
31156   emit_insn (gen_absxf2 (tmp, op1));
31157   test = gen_rtx_GE (VOIDmode, tmp,
31158     CONST_DOUBLE_FROM_REAL_VALUE (
31159        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
31160        XFmode));
31161   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
31162
31163   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
31164   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
31165   emit_jump (label2);
31166
31167   emit_label (label1);
31168   emit_move_insn (tmp, CONST1_RTX (XFmode));
31169   emit_insn (gen_addxf3 (tmp, op1, tmp));
31170   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
31171   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
31172
31173   emit_label (label2);
31174 }
31175
31176 /* Output code to perform a Newton-Rhapson approximation of a single precision
31177    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
31178
31179 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
31180 {
31181   rtx x0, x1, e0, e1, two;
31182
31183   x0 = gen_reg_rtx (mode);
31184   e0 = gen_reg_rtx (mode);
31185   e1 = gen_reg_rtx (mode);
31186   x1 = gen_reg_rtx (mode);
31187
31188   two = CONST_DOUBLE_FROM_REAL_VALUE (dconst2, SFmode);
31189
31190   if (VECTOR_MODE_P (mode))
31191     two = ix86_build_const_vector (mode, true, two);
31192
31193   two = force_reg (mode, two);
31194
31195   /* a / b = a * rcp(b) * (2.0 - b * rcp(b)) */
31196
31197   /* x0 = rcp(b) estimate */
31198   emit_insn (gen_rtx_SET (VOIDmode, x0,
31199                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
31200                                           UNSPEC_RCP)));
31201   /* e0 = x0 * a */
31202   emit_insn (gen_rtx_SET (VOIDmode, e0,
31203                           gen_rtx_MULT (mode, x0, a)));
31204   /* e1 = x0 * b */
31205   emit_insn (gen_rtx_SET (VOIDmode, e1,
31206                           gen_rtx_MULT (mode, x0, b)));
31207   /* x1 = 2. - e1 */
31208   emit_insn (gen_rtx_SET (VOIDmode, x1,
31209                           gen_rtx_MINUS (mode, two, e1)));
31210   /* res = e0 * x1 */
31211   emit_insn (gen_rtx_SET (VOIDmode, res,
31212                           gen_rtx_MULT (mode, e0, x1)));
31213 }
31214
31215 /* Output code to perform a Newton-Rhapson approximation of a
31216    single precision floating point [reciprocal] square root.  */
31217
31218 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
31219                          bool recip)
31220 {
31221   rtx x0, e0, e1, e2, e3, mthree, mhalf;
31222   REAL_VALUE_TYPE r;
31223
31224   x0 = gen_reg_rtx (mode);
31225   e0 = gen_reg_rtx (mode);
31226   e1 = gen_reg_rtx (mode);
31227   e2 = gen_reg_rtx (mode);
31228   e3 = gen_reg_rtx (mode);
31229
31230   real_from_integer (&r, VOIDmode, -3, -1, 0);
31231   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
31232
31233   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
31234   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
31235
31236   if (VECTOR_MODE_P (mode))
31237     {
31238       mthree = ix86_build_const_vector (mode, true, mthree);
31239       mhalf = ix86_build_const_vector (mode, true, mhalf);
31240     }
31241
31242   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
31243      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
31244
31245   /* x0 = rsqrt(a) estimate */
31246   emit_insn (gen_rtx_SET (VOIDmode, x0,
31247                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
31248                                           UNSPEC_RSQRT)));
31249
31250   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
31251   if (!recip)
31252     {
31253       rtx zero, mask;
31254
31255       zero = gen_reg_rtx (mode);
31256       mask = gen_reg_rtx (mode);
31257
31258       zero = force_reg (mode, CONST0_RTX(mode));
31259       emit_insn (gen_rtx_SET (VOIDmode, mask,
31260                               gen_rtx_NE (mode, zero, a)));
31261
31262       emit_insn (gen_rtx_SET (VOIDmode, x0,
31263                               gen_rtx_AND (mode, x0, mask)));
31264     }
31265
31266   /* e0 = x0 * a */
31267   emit_insn (gen_rtx_SET (VOIDmode, e0,
31268                           gen_rtx_MULT (mode, x0, a)));
31269   /* e1 = e0 * x0 */
31270   emit_insn (gen_rtx_SET (VOIDmode, e1,
31271                           gen_rtx_MULT (mode, e0, x0)));
31272
31273   /* e2 = e1 - 3. */
31274   mthree = force_reg (mode, mthree);
31275   emit_insn (gen_rtx_SET (VOIDmode, e2,
31276                           gen_rtx_PLUS (mode, e1, mthree)));
31277
31278   mhalf = force_reg (mode, mhalf);
31279   if (recip)
31280     /* e3 = -.5 * x0 */
31281     emit_insn (gen_rtx_SET (VOIDmode, e3,
31282                             gen_rtx_MULT (mode, x0, mhalf)));
31283   else
31284     /* e3 = -.5 * e0 */
31285     emit_insn (gen_rtx_SET (VOIDmode, e3,
31286                             gen_rtx_MULT (mode, e0, mhalf)));
31287   /* ret = e2 * e3 */
31288   emit_insn (gen_rtx_SET (VOIDmode, res,
31289                           gen_rtx_MULT (mode, e2, e3)));
31290 }
31291
31292 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
31293
31294 static void ATTRIBUTE_UNUSED
31295 i386_solaris_elf_named_section (const char *name, unsigned int flags,
31296                                 tree decl)
31297 {
31298   /* With Binutils 2.15, the "@unwind" marker must be specified on
31299      every occurrence of the ".eh_frame" section, not just the first
31300      one.  */
31301   if (TARGET_64BIT
31302       && strcmp (name, ".eh_frame") == 0)
31303     {
31304       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
31305                flags & SECTION_WRITE ? "aw" : "a");
31306       return;
31307     }
31308   default_elf_asm_named_section (name, flags, decl);
31309 }
31310
31311 /* Return the mangling of TYPE if it is an extended fundamental type.  */
31312
31313 static const char *
31314 ix86_mangle_type (const_tree type)
31315 {
31316   type = TYPE_MAIN_VARIANT (type);
31317
31318   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
31319       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
31320     return NULL;
31321
31322   switch (TYPE_MODE (type))
31323     {
31324     case TFmode:
31325       /* __float128 is "g".  */
31326       return "g";
31327     case XFmode:
31328       /* "long double" or __float80 is "e".  */
31329       return "e";
31330     default:
31331       return NULL;
31332     }
31333 }
31334
31335 /* For 32-bit code we can save PIC register setup by using
31336    __stack_chk_fail_local hidden function instead of calling
31337    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
31338    register, so it is better to call __stack_chk_fail directly.  */
31339
31340 static tree
31341 ix86_stack_protect_fail (void)
31342 {
31343   return TARGET_64BIT
31344          ? default_external_stack_protect_fail ()
31345          : default_hidden_stack_protect_fail ();
31346 }
31347
31348 /* Select a format to encode pointers in exception handling data.  CODE
31349    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
31350    true if the symbol may be affected by dynamic relocations.
31351
31352    ??? All x86 object file formats are capable of representing this.
31353    After all, the relocation needed is the same as for the call insn.
31354    Whether or not a particular assembler allows us to enter such, I
31355    guess we'll have to see.  */
31356 int
31357 asm_preferred_eh_data_format (int code, int global)
31358 {
31359   if (flag_pic)
31360     {
31361       int type = DW_EH_PE_sdata8;
31362       if (!TARGET_64BIT
31363           || ix86_cmodel == CM_SMALL_PIC
31364           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
31365         type = DW_EH_PE_sdata4;
31366       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
31367     }
31368   if (ix86_cmodel == CM_SMALL
31369       || (ix86_cmodel == CM_MEDIUM && code))
31370     return DW_EH_PE_udata4;
31371   return DW_EH_PE_absptr;
31372 }
31373 \f
31374 /* Expand copysign from SIGN to the positive value ABS_VALUE
31375    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
31376    the sign-bit.  */
31377 static void
31378 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
31379 {
31380   enum machine_mode mode = GET_MODE (sign);
31381   rtx sgn = gen_reg_rtx (mode);
31382   if (mask == NULL_RTX)
31383     {
31384       enum machine_mode vmode;
31385
31386       if (mode == SFmode)
31387         vmode = V4SFmode;
31388       else if (mode == DFmode)
31389         vmode = V2DFmode;
31390       else
31391         vmode = mode;
31392
31393       mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), false);
31394       if (!VECTOR_MODE_P (mode))
31395         {
31396           /* We need to generate a scalar mode mask in this case.  */
31397           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
31398           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
31399           mask = gen_reg_rtx (mode);
31400           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
31401         }
31402     }
31403   else
31404     mask = gen_rtx_NOT (mode, mask);
31405   emit_insn (gen_rtx_SET (VOIDmode, sgn,
31406                           gen_rtx_AND (mode, mask, sign)));
31407   emit_insn (gen_rtx_SET (VOIDmode, result,
31408                           gen_rtx_IOR (mode, abs_value, sgn)));
31409 }
31410
31411 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
31412    mask for masking out the sign-bit is stored in *SMASK, if that is
31413    non-null.  */
31414 static rtx
31415 ix86_expand_sse_fabs (rtx op0, rtx *smask)
31416 {
31417   enum machine_mode vmode, mode = GET_MODE (op0);
31418   rtx xa, mask;
31419
31420   xa = gen_reg_rtx (mode);
31421   if (mode == SFmode)
31422     vmode = V4SFmode;
31423   else if (mode == DFmode)
31424     vmode = V2DFmode;
31425   else
31426     vmode = mode;
31427   mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), true);
31428   if (!VECTOR_MODE_P (mode))
31429     {
31430       /* We need to generate a scalar mode mask in this case.  */
31431       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
31432       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
31433       mask = gen_reg_rtx (mode);
31434       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
31435     }
31436   emit_insn (gen_rtx_SET (VOIDmode, xa,
31437                           gen_rtx_AND (mode, op0, mask)));
31438
31439   if (smask)
31440     *smask = mask;
31441
31442   return xa;
31443 }
31444
31445 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
31446    swapping the operands if SWAP_OPERANDS is true.  The expanded
31447    code is a forward jump to a newly created label in case the
31448    comparison is true.  The generated label rtx is returned.  */
31449 static rtx
31450 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
31451                                   bool swap_operands)
31452 {
31453   rtx label, tmp;
31454
31455   if (swap_operands)
31456     {
31457       tmp = op0;
31458       op0 = op1;
31459       op1 = tmp;
31460     }
31461
31462   label = gen_label_rtx ();
31463   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
31464   emit_insn (gen_rtx_SET (VOIDmode, tmp,
31465                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
31466   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
31467   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
31468                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
31469   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
31470   JUMP_LABEL (tmp) = label;
31471
31472   return label;
31473 }
31474
31475 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
31476    using comparison code CODE.  Operands are swapped for the comparison if
31477    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
31478 static rtx
31479 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
31480                               bool swap_operands)
31481 {
31482   enum machine_mode mode = GET_MODE (op0);
31483   rtx mask = gen_reg_rtx (mode);
31484
31485   if (swap_operands)
31486     {
31487       rtx tmp = op0;
31488       op0 = op1;
31489       op1 = tmp;
31490     }
31491
31492   if (mode == DFmode)
31493     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
31494                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
31495   else
31496     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
31497                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
31498
31499   return mask;
31500 }
31501
31502 /* Generate and return a rtx of mode MODE for 2**n where n is the number
31503    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
31504 static rtx
31505 ix86_gen_TWO52 (enum machine_mode mode)
31506 {
31507   REAL_VALUE_TYPE TWO52r;
31508   rtx TWO52;
31509
31510   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
31511   TWO52 = const_double_from_real_value (TWO52r, mode);
31512   TWO52 = force_reg (mode, TWO52);
31513
31514   return TWO52;
31515 }
31516
31517 /* Expand SSE sequence for computing lround from OP1 storing
31518    into OP0.  */
31519 void
31520 ix86_expand_lround (rtx op0, rtx op1)
31521 {
31522   /* C code for the stuff we're doing below:
31523        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
31524        return (long)tmp;
31525    */
31526   enum machine_mode mode = GET_MODE (op1);
31527   const struct real_format *fmt;
31528   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
31529   rtx adj;
31530
31531   /* load nextafter (0.5, 0.0) */
31532   fmt = REAL_MODE_FORMAT (mode);
31533   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
31534   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
31535
31536   /* adj = copysign (0.5, op1) */
31537   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
31538   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
31539
31540   /* adj = op1 + adj */
31541   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
31542
31543   /* op0 = (imode)adj */
31544   expand_fix (op0, adj, 0);
31545 }
31546
31547 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
31548    into OPERAND0.  */
31549 void
31550 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
31551 {
31552   /* C code for the stuff we're doing below (for do_floor):
31553         xi = (long)op1;
31554         xi -= (double)xi > op1 ? 1 : 0;
31555         return xi;
31556    */
31557   enum machine_mode fmode = GET_MODE (op1);
31558   enum machine_mode imode = GET_MODE (op0);
31559   rtx ireg, freg, label, tmp;
31560
31561   /* reg = (long)op1 */
31562   ireg = gen_reg_rtx (imode);
31563   expand_fix (ireg, op1, 0);
31564
31565   /* freg = (double)reg */
31566   freg = gen_reg_rtx (fmode);
31567   expand_float (freg, ireg, 0);
31568
31569   /* ireg = (freg > op1) ? ireg - 1 : ireg */
31570   label = ix86_expand_sse_compare_and_jump (UNLE,
31571                                             freg, op1, !do_floor);
31572   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
31573                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
31574   emit_move_insn (ireg, tmp);
31575
31576   emit_label (label);
31577   LABEL_NUSES (label) = 1;
31578
31579   emit_move_insn (op0, ireg);
31580 }
31581
31582 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
31583    result in OPERAND0.  */
31584 void
31585 ix86_expand_rint (rtx operand0, rtx operand1)
31586 {
31587   /* C code for the stuff we're doing below:
31588         xa = fabs (operand1);
31589         if (!isless (xa, 2**52))
31590           return operand1;
31591         xa = xa + 2**52 - 2**52;
31592         return copysign (xa, operand1);
31593    */
31594   enum machine_mode mode = GET_MODE (operand0);
31595   rtx res, xa, label, TWO52, mask;
31596
31597   res = gen_reg_rtx (mode);
31598   emit_move_insn (res, operand1);
31599
31600   /* xa = abs (operand1) */
31601   xa = ix86_expand_sse_fabs (res, &mask);
31602
31603   /* if (!isless (xa, TWO52)) goto label; */
31604   TWO52 = ix86_gen_TWO52 (mode);
31605   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31606
31607   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
31608   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
31609
31610   ix86_sse_copysign_to_positive (res, xa, res, mask);
31611
31612   emit_label (label);
31613   LABEL_NUSES (label) = 1;
31614
31615   emit_move_insn (operand0, res);
31616 }
31617
31618 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
31619    into OPERAND0.  */
31620 void
31621 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
31622 {
31623   /* C code for the stuff we expand below.
31624         double xa = fabs (x), x2;
31625         if (!isless (xa, TWO52))
31626           return x;
31627         xa = xa + TWO52 - TWO52;
31628         x2 = copysign (xa, x);
31629      Compensate.  Floor:
31630         if (x2 > x)
31631           x2 -= 1;
31632      Compensate.  Ceil:
31633         if (x2 < x)
31634           x2 -= -1;
31635         return x2;
31636    */
31637   enum machine_mode mode = GET_MODE (operand0);
31638   rtx xa, TWO52, tmp, label, one, res, mask;
31639
31640   TWO52 = ix86_gen_TWO52 (mode);
31641
31642   /* Temporary for holding the result, initialized to the input
31643      operand to ease control flow.  */
31644   res = gen_reg_rtx (mode);
31645   emit_move_insn (res, operand1);
31646
31647   /* xa = abs (operand1) */
31648   xa = ix86_expand_sse_fabs (res, &mask);
31649
31650   /* if (!isless (xa, TWO52)) goto label; */
31651   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31652
31653   /* xa = xa + TWO52 - TWO52; */
31654   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
31655   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
31656
31657   /* xa = copysign (xa, operand1) */
31658   ix86_sse_copysign_to_positive (xa, xa, res, mask);
31659
31660   /* generate 1.0 or -1.0 */
31661   one = force_reg (mode,
31662                    const_double_from_real_value (do_floor
31663                                                  ? dconst1 : dconstm1, mode));
31664
31665   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
31666   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
31667   emit_insn (gen_rtx_SET (VOIDmode, tmp,
31668                           gen_rtx_AND (mode, one, tmp)));
31669   /* We always need to subtract here to preserve signed zero.  */
31670   tmp = expand_simple_binop (mode, MINUS,
31671                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
31672   emit_move_insn (res, tmp);
31673
31674   emit_label (label);
31675   LABEL_NUSES (label) = 1;
31676
31677   emit_move_insn (operand0, res);
31678 }
31679
31680 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
31681    into OPERAND0.  */
31682 void
31683 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
31684 {
31685   /* C code for the stuff we expand below.
31686         double xa = fabs (x), x2;
31687         if (!isless (xa, TWO52))
31688           return x;
31689         x2 = (double)(long)x;
31690      Compensate.  Floor:
31691         if (x2 > x)
31692           x2 -= 1;
31693      Compensate.  Ceil:
31694         if (x2 < x)
31695           x2 += 1;
31696         if (HONOR_SIGNED_ZEROS (mode))
31697           return copysign (x2, x);
31698         return x2;
31699    */
31700   enum machine_mode mode = GET_MODE (operand0);
31701   rtx xa, xi, TWO52, tmp, label, one, res, mask;
31702
31703   TWO52 = ix86_gen_TWO52 (mode);
31704
31705   /* Temporary for holding the result, initialized to the input
31706      operand to ease control flow.  */
31707   res = gen_reg_rtx (mode);
31708   emit_move_insn (res, operand1);
31709
31710   /* xa = abs (operand1) */
31711   xa = ix86_expand_sse_fabs (res, &mask);
31712
31713   /* if (!isless (xa, TWO52)) goto label; */
31714   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31715
31716   /* xa = (double)(long)x */
31717   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
31718   expand_fix (xi, res, 0);
31719   expand_float (xa, xi, 0);
31720
31721   /* generate 1.0 */
31722   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
31723
31724   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
31725   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
31726   emit_insn (gen_rtx_SET (VOIDmode, tmp,
31727                           gen_rtx_AND (mode, one, tmp)));
31728   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
31729                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
31730   emit_move_insn (res, tmp);
31731
31732   if (HONOR_SIGNED_ZEROS (mode))
31733     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
31734
31735   emit_label (label);
31736   LABEL_NUSES (label) = 1;
31737
31738   emit_move_insn (operand0, res);
31739 }
31740
31741 /* Expand SSE sequence for computing round from OPERAND1 storing
31742    into OPERAND0.  Sequence that works without relying on DImode truncation
31743    via cvttsd2siq that is only available on 64bit targets.  */
31744 void
31745 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
31746 {
31747   /* C code for the stuff we expand below.
31748         double xa = fabs (x), xa2, x2;
31749         if (!isless (xa, TWO52))
31750           return x;
31751      Using the absolute value and copying back sign makes
31752      -0.0 -> -0.0 correct.
31753         xa2 = xa + TWO52 - TWO52;
31754      Compensate.
31755         dxa = xa2 - xa;
31756         if (dxa <= -0.5)
31757           xa2 += 1;
31758         else if (dxa > 0.5)
31759           xa2 -= 1;
31760         x2 = copysign (xa2, x);
31761         return x2;
31762    */
31763   enum machine_mode mode = GET_MODE (operand0);
31764   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
31765
31766   TWO52 = ix86_gen_TWO52 (mode);
31767
31768   /* Temporary for holding the result, initialized to the input
31769      operand to ease control flow.  */
31770   res = gen_reg_rtx (mode);
31771   emit_move_insn (res, operand1);
31772
31773   /* xa = abs (operand1) */
31774   xa = ix86_expand_sse_fabs (res, &mask);
31775
31776   /* if (!isless (xa, TWO52)) goto label; */
31777   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31778
31779   /* xa2 = xa + TWO52 - TWO52; */
31780   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
31781   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
31782
31783   /* dxa = xa2 - xa; */
31784   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
31785
31786   /* generate 0.5, 1.0 and -0.5 */
31787   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
31788   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
31789   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
31790                                0, OPTAB_DIRECT);
31791
31792   /* Compensate.  */
31793   tmp = gen_reg_rtx (mode);
31794   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
31795   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
31796   emit_insn (gen_rtx_SET (VOIDmode, tmp,
31797                           gen_rtx_AND (mode, one, tmp)));
31798   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
31799   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
31800   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
31801   emit_insn (gen_rtx_SET (VOIDmode, tmp,
31802                           gen_rtx_AND (mode, one, tmp)));
31803   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
31804
31805   /* res = copysign (xa2, operand1) */
31806   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
31807
31808   emit_label (label);
31809   LABEL_NUSES (label) = 1;
31810
31811   emit_move_insn (operand0, res);
31812 }
31813
31814 /* Expand SSE sequence for computing trunc from OPERAND1 storing
31815    into OPERAND0.  */
31816 void
31817 ix86_expand_trunc (rtx operand0, rtx operand1)
31818 {
31819   /* C code for SSE variant we expand below.
31820         double xa = fabs (x), x2;
31821         if (!isless (xa, TWO52))
31822           return x;
31823         x2 = (double)(long)x;
31824         if (HONOR_SIGNED_ZEROS (mode))
31825           return copysign (x2, x);
31826         return x2;
31827    */
31828   enum machine_mode mode = GET_MODE (operand0);
31829   rtx xa, xi, TWO52, label, res, mask;
31830
31831   TWO52 = ix86_gen_TWO52 (mode);
31832
31833   /* Temporary for holding the result, initialized to the input
31834      operand to ease control flow.  */
31835   res = gen_reg_rtx (mode);
31836   emit_move_insn (res, operand1);
31837
31838   /* xa = abs (operand1) */
31839   xa = ix86_expand_sse_fabs (res, &mask);
31840
31841   /* if (!isless (xa, TWO52)) goto label; */
31842   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31843
31844   /* x = (double)(long)x */
31845   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
31846   expand_fix (xi, res, 0);
31847   expand_float (res, xi, 0);
31848
31849   if (HONOR_SIGNED_ZEROS (mode))
31850     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
31851
31852   emit_label (label);
31853   LABEL_NUSES (label) = 1;
31854
31855   emit_move_insn (operand0, res);
31856 }
31857
31858 /* Expand SSE sequence for computing trunc from OPERAND1 storing
31859    into OPERAND0.  */
31860 void
31861 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
31862 {
31863   enum machine_mode mode = GET_MODE (operand0);
31864   rtx xa, mask, TWO52, label, one, res, smask, tmp;
31865
31866   /* C code for SSE variant we expand below.
31867         double xa = fabs (x), x2;
31868         if (!isless (xa, TWO52))
31869           return x;
31870         xa2 = xa + TWO52 - TWO52;
31871      Compensate:
31872         if (xa2 > xa)
31873           xa2 -= 1.0;
31874         x2 = copysign (xa2, x);
31875         return x2;
31876    */
31877
31878   TWO52 = ix86_gen_TWO52 (mode);
31879
31880   /* Temporary for holding the result, initialized to the input
31881      operand to ease control flow.  */
31882   res = gen_reg_rtx (mode);
31883   emit_move_insn (res, operand1);
31884
31885   /* xa = abs (operand1) */
31886   xa = ix86_expand_sse_fabs (res, &smask);
31887
31888   /* if (!isless (xa, TWO52)) goto label; */
31889   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31890
31891   /* res = xa + TWO52 - TWO52; */
31892   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
31893   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
31894   emit_move_insn (res, tmp);
31895
31896   /* generate 1.0 */
31897   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
31898
31899   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
31900   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
31901   emit_insn (gen_rtx_SET (VOIDmode, mask,
31902                           gen_rtx_AND (mode, mask, one)));
31903   tmp = expand_simple_binop (mode, MINUS,
31904                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
31905   emit_move_insn (res, tmp);
31906
31907   /* res = copysign (res, operand1) */
31908   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
31909
31910   emit_label (label);
31911   LABEL_NUSES (label) = 1;
31912
31913   emit_move_insn (operand0, res);
31914 }
31915
31916 /* Expand SSE sequence for computing round from OPERAND1 storing
31917    into OPERAND0.  */
31918 void
31919 ix86_expand_round (rtx operand0, rtx operand1)
31920 {
31921   /* C code for the stuff we're doing below:
31922         double xa = fabs (x);
31923         if (!isless (xa, TWO52))
31924           return x;
31925         xa = (double)(long)(xa + nextafter (0.5, 0.0));
31926         return copysign (xa, x);
31927    */
31928   enum machine_mode mode = GET_MODE (operand0);
31929   rtx res, TWO52, xa, label, xi, half, mask;
31930   const struct real_format *fmt;
31931   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
31932
31933   /* Temporary for holding the result, initialized to the input
31934      operand to ease control flow.  */
31935   res = gen_reg_rtx (mode);
31936   emit_move_insn (res, operand1);
31937
31938   TWO52 = ix86_gen_TWO52 (mode);
31939   xa = ix86_expand_sse_fabs (res, &mask);
31940   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
31941
31942   /* load nextafter (0.5, 0.0) */
31943   fmt = REAL_MODE_FORMAT (mode);
31944   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
31945   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
31946
31947   /* xa = xa + 0.5 */
31948   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
31949   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
31950
31951   /* xa = (double)(int64_t)xa */
31952   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
31953   expand_fix (xi, xa, 0);
31954   expand_float (xa, xi, 0);
31955
31956   /* res = copysign (xa, operand1) */
31957   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
31958
31959   emit_label (label);
31960   LABEL_NUSES (label) = 1;
31961
31962   emit_move_insn (operand0, res);
31963 }
31964 \f
31965
31966 /* Table of valid machine attributes.  */
31967 static const struct attribute_spec ix86_attribute_table[] =
31968 {
31969   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
31970   /* Stdcall attribute says callee is responsible for popping arguments
31971      if they are not variable.  */
31972   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
31973   /* Fastcall attribute says callee is responsible for popping arguments
31974      if they are not variable.  */
31975   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
31976   /* Thiscall attribute says callee is responsible for popping arguments
31977      if they are not variable.  */
31978   { "thiscall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
31979   /* Cdecl attribute says the callee is a normal C declaration */
31980   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
31981   /* Regparm attribute specifies how many integer arguments are to be
31982      passed in registers.  */
31983   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
31984   /* Sseregparm attribute says we are using x86_64 calling conventions
31985      for FP arguments.  */
31986   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
31987   /* force_align_arg_pointer says this function realigns the stack at entry.  */
31988   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
31989     false, true,  true, ix86_handle_cconv_attribute },
31990 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
31991   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
31992   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
31993   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
31994 #endif
31995   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
31996   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
31997 #ifdef SUBTARGET_ATTRIBUTE_TABLE
31998   SUBTARGET_ATTRIBUTE_TABLE,
31999 #endif
32000   /* ms_abi and sysv_abi calling convention function attributes.  */
32001   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
32002   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
32003   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute },
32004   /* End element.  */
32005   { NULL,        0, 0, false, false, false, NULL }
32006 };
32007
32008 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
32009 static int
32010 ix86_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
32011                                  tree vectype ATTRIBUTE_UNUSED,
32012                                  int misalign ATTRIBUTE_UNUSED)
32013 {
32014   switch (type_of_cost)
32015     {
32016       case scalar_stmt:
32017         return ix86_cost->scalar_stmt_cost;
32018
32019       case scalar_load:
32020         return ix86_cost->scalar_load_cost;
32021
32022       case scalar_store:
32023         return ix86_cost->scalar_store_cost;
32024
32025       case vector_stmt:
32026         return ix86_cost->vec_stmt_cost;
32027
32028       case vector_load:
32029         return ix86_cost->vec_align_load_cost;
32030
32031       case vector_store:
32032         return ix86_cost->vec_store_cost;
32033
32034       case vec_to_scalar:
32035         return ix86_cost->vec_to_scalar_cost;
32036
32037       case scalar_to_vec:
32038         return ix86_cost->scalar_to_vec_cost;
32039
32040       case unaligned_load:
32041       case unaligned_store:
32042         return ix86_cost->vec_unalign_load_cost;
32043
32044       case cond_branch_taken:
32045         return ix86_cost->cond_taken_branch_cost;
32046
32047       case cond_branch_not_taken:
32048         return ix86_cost->cond_not_taken_branch_cost;
32049
32050       case vec_perm:
32051         return 1;
32052
32053       default:
32054         gcc_unreachable ();
32055     }
32056 }
32057
32058
32059 /* Implement targetm.vectorize.builtin_vec_perm.  */
32060
32061 static tree
32062 ix86_vectorize_builtin_vec_perm (tree vec_type, tree *mask_type)
32063 {
32064   tree itype = TREE_TYPE (vec_type);
32065   bool u = TYPE_UNSIGNED (itype);
32066   enum machine_mode vmode = TYPE_MODE (vec_type);
32067   enum ix86_builtins fcode;
32068   bool ok = TARGET_SSE2;
32069
32070   switch (vmode)
32071     {
32072     case V4DFmode:
32073       ok = TARGET_AVX;
32074       fcode = IX86_BUILTIN_VEC_PERM_V4DF;
32075       goto get_di;
32076     case V2DFmode:
32077       fcode = IX86_BUILTIN_VEC_PERM_V2DF;
32078     get_di:
32079       itype = ix86_get_builtin_type (IX86_BT_DI);
32080       break;
32081
32082     case V8SFmode:
32083       ok = TARGET_AVX;
32084       fcode = IX86_BUILTIN_VEC_PERM_V8SF;
32085       goto get_si;
32086     case V4SFmode:
32087       ok = TARGET_SSE;
32088       fcode = IX86_BUILTIN_VEC_PERM_V4SF;
32089     get_si:
32090       itype = ix86_get_builtin_type (IX86_BT_SI);
32091       break;
32092
32093     case V2DImode:
32094       fcode = u ? IX86_BUILTIN_VEC_PERM_V2DI_U : IX86_BUILTIN_VEC_PERM_V2DI;
32095       break;
32096     case V4SImode:
32097       fcode = u ? IX86_BUILTIN_VEC_PERM_V4SI_U : IX86_BUILTIN_VEC_PERM_V4SI;
32098       break;
32099     case V8HImode:
32100       fcode = u ? IX86_BUILTIN_VEC_PERM_V8HI_U : IX86_BUILTIN_VEC_PERM_V8HI;
32101       break;
32102     case V16QImode:
32103       fcode = u ? IX86_BUILTIN_VEC_PERM_V16QI_U : IX86_BUILTIN_VEC_PERM_V16QI;
32104       break;
32105     default:
32106       ok = false;
32107       break;
32108     }
32109
32110   if (!ok)
32111     return NULL_TREE;
32112
32113   *mask_type = itype;
32114   return ix86_builtins[(int) fcode];
32115 }
32116
32117 /* Return a vector mode with twice as many elements as VMODE.  */
32118 /* ??? Consider moving this to a table generated by genmodes.c.  */
32119
32120 static enum machine_mode
32121 doublesize_vector_mode (enum machine_mode vmode)
32122 {
32123   switch (vmode)
32124     {
32125     case V2SFmode:      return V4SFmode;
32126     case V1DImode:      return V2DImode;
32127     case V2SImode:      return V4SImode;
32128     case V4HImode:      return V8HImode;
32129     case V8QImode:      return V16QImode;
32130
32131     case V2DFmode:      return V4DFmode;
32132     case V4SFmode:      return V8SFmode;
32133     case V2DImode:      return V4DImode;
32134     case V4SImode:      return V8SImode;
32135     case V8HImode:      return V16HImode;
32136     case V16QImode:     return V32QImode;
32137
32138     case V4DFmode:      return V8DFmode;
32139     case V8SFmode:      return V16SFmode;
32140     case V4DImode:      return V8DImode;
32141     case V8SImode:      return V16SImode;
32142     case V16HImode:     return V32HImode;
32143     case V32QImode:     return V64QImode;
32144
32145     default:
32146       gcc_unreachable ();
32147     }
32148 }
32149
32150 /* Construct (set target (vec_select op0 (parallel perm))) and
32151    return true if that's a valid instruction in the active ISA.  */
32152
32153 static bool
32154 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
32155 {
32156   rtx rperm[MAX_VECT_LEN], x;
32157   unsigned i;
32158
32159   for (i = 0; i < nelt; ++i)
32160     rperm[i] = GEN_INT (perm[i]);
32161
32162   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
32163   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
32164   x = gen_rtx_SET (VOIDmode, target, x);
32165
32166   x = emit_insn (x);
32167   if (recog_memoized (x) < 0)
32168     {
32169       remove_insn (x);
32170       return false;
32171     }
32172   return true;
32173 }
32174
32175 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
32176
32177 static bool
32178 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
32179                         const unsigned char *perm, unsigned nelt)
32180 {
32181   enum machine_mode v2mode;
32182   rtx x;
32183
32184   v2mode = doublesize_vector_mode (GET_MODE (op0));
32185   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
32186   return expand_vselect (target, x, perm, nelt);
32187 }
32188
32189 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32190    in terms of blendp[sd] / pblendw / pblendvb.  */
32191
32192 static bool
32193 expand_vec_perm_blend (struct expand_vec_perm_d *d)
32194 {
32195   enum machine_mode vmode = d->vmode;
32196   unsigned i, mask, nelt = d->nelt;
32197   rtx target, op0, op1, x;
32198
32199   if (!TARGET_SSE4_1 || d->op0 == d->op1)
32200     return false;
32201   if (!(GET_MODE_SIZE (vmode) == 16 || vmode == V4DFmode || vmode == V8SFmode))
32202     return false;
32203
32204   /* This is a blend, not a permute.  Elements must stay in their
32205      respective lanes.  */
32206   for (i = 0; i < nelt; ++i)
32207     {
32208       unsigned e = d->perm[i];
32209       if (!(e == i || e == i + nelt))
32210         return false;
32211     }
32212
32213   if (d->testing_p)
32214     return true;
32215
32216   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
32217      decision should be extracted elsewhere, so that we only try that
32218      sequence once all budget==3 options have been tried.  */
32219
32220   /* For bytes, see if bytes move in pairs so we can use pblendw with
32221      an immediate argument, rather than pblendvb with a vector argument.  */
32222   if (vmode == V16QImode)
32223     {
32224       bool pblendw_ok = true;
32225       for (i = 0; i < 16 && pblendw_ok; i += 2)
32226         pblendw_ok = (d->perm[i] + 1 == d->perm[i + 1]);
32227
32228       if (!pblendw_ok)
32229         {
32230           rtx rperm[16], vperm;
32231
32232           for (i = 0; i < nelt; ++i)
32233             rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
32234
32235           vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
32236           vperm = force_reg (V16QImode, vperm);
32237
32238           emit_insn (gen_sse4_1_pblendvb (d->target, d->op0, d->op1, vperm));
32239           return true;
32240         }
32241     }
32242
32243   target = d->target;
32244   op0 = d->op0;
32245   op1 = d->op1;
32246   mask = 0;
32247
32248   switch (vmode)
32249     {
32250     case V4DFmode:
32251     case V8SFmode:
32252     case V2DFmode:
32253     case V4SFmode:
32254     case V8HImode:
32255       for (i = 0; i < nelt; ++i)
32256         mask |= (d->perm[i] >= nelt) << i;
32257       break;
32258
32259     case V2DImode:
32260       for (i = 0; i < 2; ++i)
32261         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
32262       goto do_subreg;
32263
32264     case V4SImode:
32265       for (i = 0; i < 4; ++i)
32266         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
32267       goto do_subreg;
32268
32269     case V16QImode:
32270       for (i = 0; i < 8; ++i)
32271         mask |= (d->perm[i * 2] >= 16) << i;
32272
32273     do_subreg:
32274       vmode = V8HImode;
32275       target = gen_lowpart (vmode, target);
32276       op0 = gen_lowpart (vmode, op0);
32277       op1 = gen_lowpart (vmode, op1);
32278       break;
32279
32280     default:
32281       gcc_unreachable ();
32282     }
32283
32284   /* This matches five different patterns with the different modes.  */
32285   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
32286   x = gen_rtx_SET (VOIDmode, target, x);
32287   emit_insn (x);
32288
32289   return true;
32290 }
32291
32292 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32293    in terms of the variable form of vpermilps.
32294
32295    Note that we will have already failed the immediate input vpermilps,
32296    which requires that the high and low part shuffle be identical; the
32297    variable form doesn't require that.  */
32298
32299 static bool
32300 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
32301 {
32302   rtx rperm[8], vperm;
32303   unsigned i;
32304
32305   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
32306     return false;
32307
32308   /* We can only permute within the 128-bit lane.  */
32309   for (i = 0; i < 8; ++i)
32310     {
32311       unsigned e = d->perm[i];
32312       if (i < 4 ? e >= 4 : e < 4)
32313         return false;
32314     }
32315
32316   if (d->testing_p)
32317     return true;
32318
32319   for (i = 0; i < 8; ++i)
32320     {
32321       unsigned e = d->perm[i];
32322
32323       /* Within each 128-bit lane, the elements of op0 are numbered
32324          from 0 and the elements of op1 are numbered from 4.  */
32325       if (e >= 8 + 4)
32326         e -= 8;
32327       else if (e >= 4)
32328         e -= 4;
32329
32330       rperm[i] = GEN_INT (e);
32331     }
32332
32333   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
32334   vperm = force_reg (V8SImode, vperm);
32335   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
32336
32337   return true;
32338 }
32339
32340 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32341    in terms of pshufb or vpperm.  */
32342
32343 static bool
32344 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
32345 {
32346   unsigned i, nelt, eltsz;
32347   rtx rperm[16], vperm, target, op0, op1;
32348
32349   if (!(d->op0 == d->op1 ? TARGET_SSSE3 : TARGET_XOP))
32350     return false;
32351   if (GET_MODE_SIZE (d->vmode) != 16)
32352     return false;
32353
32354   if (d->testing_p)
32355     return true;
32356
32357   nelt = d->nelt;
32358   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
32359
32360   for (i = 0; i < nelt; ++i)
32361     {
32362       unsigned j, e = d->perm[i];
32363       for (j = 0; j < eltsz; ++j)
32364         rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
32365     }
32366
32367   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
32368   vperm = force_reg (V16QImode, vperm);
32369
32370   target = gen_lowpart (V16QImode, d->target);
32371   op0 = gen_lowpart (V16QImode, d->op0);
32372   if (d->op0 == d->op1)
32373     emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
32374   else
32375     {
32376       op1 = gen_lowpart (V16QImode, d->op1);
32377       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
32378     }
32379
32380   return true;
32381 }
32382
32383 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
32384    in a single instruction.  */
32385
32386 static bool
32387 expand_vec_perm_1 (struct expand_vec_perm_d *d)
32388 {
32389   unsigned i, nelt = d->nelt;
32390   unsigned char perm2[MAX_VECT_LEN];
32391
32392   /* Check plain VEC_SELECT first, because AVX has instructions that could
32393      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
32394      input where SEL+CONCAT may not.  */
32395   if (d->op0 == d->op1)
32396     {
32397       int mask = nelt - 1;
32398
32399       for (i = 0; i < nelt; i++)
32400         perm2[i] = d->perm[i] & mask;
32401
32402       if (expand_vselect (d->target, d->op0, perm2, nelt))
32403         return true;
32404
32405       /* There are plenty of patterns in sse.md that are written for
32406          SEL+CONCAT and are not replicated for a single op.  Perhaps
32407          that should be changed, to avoid the nastiness here.  */
32408
32409       /* Recognize interleave style patterns, which means incrementing
32410          every other permutation operand.  */
32411       for (i = 0; i < nelt; i += 2)
32412         {
32413           perm2[i] = d->perm[i] & mask;
32414           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
32415         }
32416       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
32417         return true;
32418
32419       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
32420       if (nelt >= 4)
32421         {
32422           for (i = 0; i < nelt; i += 4)
32423             {
32424               perm2[i + 0] = d->perm[i + 0] & mask;
32425               perm2[i + 1] = d->perm[i + 1] & mask;
32426               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
32427               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
32428             }
32429
32430           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
32431             return true;
32432         }
32433     }
32434
32435   /* Finally, try the fully general two operand permute.  */
32436   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
32437     return true;
32438
32439   /* Recognize interleave style patterns with reversed operands.  */
32440   if (d->op0 != d->op1)
32441     {
32442       for (i = 0; i < nelt; ++i)
32443         {
32444           unsigned e = d->perm[i];
32445           if (e >= nelt)
32446             e -= nelt;
32447           else
32448             e += nelt;
32449           perm2[i] = e;
32450         }
32451
32452       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
32453         return true;
32454     }
32455
32456   /* Try the SSE4.1 blend variable merge instructions.  */
32457   if (expand_vec_perm_blend (d))
32458     return true;
32459
32460   /* Try one of the AVX vpermil variable permutations.  */
32461   if (expand_vec_perm_vpermil (d))
32462     return true;
32463
32464   /* Try the SSSE3 pshufb or XOP vpperm variable permutation.  */
32465   if (expand_vec_perm_pshufb (d))
32466     return true;
32467
32468   return false;
32469 }
32470
32471 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
32472    in terms of a pair of pshuflw + pshufhw instructions.  */
32473
32474 static bool
32475 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
32476 {
32477   unsigned char perm2[MAX_VECT_LEN];
32478   unsigned i;
32479   bool ok;
32480
32481   if (d->vmode != V8HImode || d->op0 != d->op1)
32482     return false;
32483
32484   /* The two permutations only operate in 64-bit lanes.  */
32485   for (i = 0; i < 4; ++i)
32486     if (d->perm[i] >= 4)
32487       return false;
32488   for (i = 4; i < 8; ++i)
32489     if (d->perm[i] < 4)
32490       return false;
32491
32492   if (d->testing_p)
32493     return true;
32494
32495   /* Emit the pshuflw.  */
32496   memcpy (perm2, d->perm, 4);
32497   for (i = 4; i < 8; ++i)
32498     perm2[i] = i;
32499   ok = expand_vselect (d->target, d->op0, perm2, 8);
32500   gcc_assert (ok);
32501
32502   /* Emit the pshufhw.  */
32503   memcpy (perm2 + 4, d->perm + 4, 4);
32504   for (i = 0; i < 4; ++i)
32505     perm2[i] = i;
32506   ok = expand_vselect (d->target, d->target, perm2, 8);
32507   gcc_assert (ok);
32508
32509   return true;
32510 }
32511
32512 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
32513    the permutation using the SSSE3 palignr instruction.  This succeeds
32514    when all of the elements in PERM fit within one vector and we merely
32515    need to shift them down so that a single vector permutation has a
32516    chance to succeed.  */
32517
32518 static bool
32519 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
32520 {
32521   unsigned i, nelt = d->nelt;
32522   unsigned min, max;
32523   bool in_order, ok;
32524   rtx shift;
32525
32526   /* Even with AVX, palignr only operates on 128-bit vectors.  */
32527   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
32528     return false;
32529
32530   min = nelt, max = 0;
32531   for (i = 0; i < nelt; ++i)
32532     {
32533       unsigned e = d->perm[i];
32534       if (e < min)
32535         min = e;
32536       if (e > max)
32537         max = e;
32538     }
32539   if (min == 0 || max - min >= nelt)
32540     return false;
32541
32542   /* Given that we have SSSE3, we know we'll be able to implement the
32543      single operand permutation after the palignr with pshufb.  */
32544   if (d->testing_p)
32545     return true;
32546
32547   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
32548   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
32549                                   gen_lowpart (TImode, d->op1),
32550                                   gen_lowpart (TImode, d->op0), shift));
32551
32552   d->op0 = d->op1 = d->target;
32553
32554   in_order = true;
32555   for (i = 0; i < nelt; ++i)
32556     {
32557       unsigned e = d->perm[i] - min;
32558       if (e != i)
32559         in_order = false;
32560       d->perm[i] = e;
32561     }
32562
32563   /* Test for the degenerate case where the alignment by itself
32564      produces the desired permutation.  */
32565   if (in_order)
32566     return true;
32567
32568   ok = expand_vec_perm_1 (d);
32569   gcc_assert (ok);
32570
32571   return ok;
32572 }
32573
32574 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
32575    a two vector permutation into a single vector permutation by using
32576    an interleave operation to merge the vectors.  */
32577
32578 static bool
32579 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
32580 {
32581   struct expand_vec_perm_d dremap, dfinal;
32582   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
32583   unsigned contents, h1, h2, h3, h4;
32584   unsigned char remap[2 * MAX_VECT_LEN];
32585   rtx seq;
32586   bool ok;
32587
32588   if (d->op0 == d->op1)
32589     return false;
32590
32591   /* The 256-bit unpck[lh]p[sd] instructions only operate within the 128-bit
32592      lanes.  We can use similar techniques with the vperm2f128 instruction,
32593      but it requires slightly different logic.  */
32594   if (GET_MODE_SIZE (d->vmode) != 16)
32595     return false;
32596
32597   /* Examine from whence the elements come.  */
32598   contents = 0;
32599   for (i = 0; i < nelt; ++i)
32600     contents |= 1u << d->perm[i];
32601
32602   /* Split the two input vectors into 4 halves.  */
32603   h1 = (1u << nelt2) - 1;
32604   h2 = h1 << nelt2;
32605   h3 = h2 << nelt2;
32606   h4 = h3 << nelt2;
32607
32608   memset (remap, 0xff, sizeof (remap));
32609   dremap = *d;
32610
32611   /* If the elements from the low halves use interleave low, and similarly
32612      for interleave high.  If the elements are from mis-matched halves, we
32613      can use shufps for V4SF/V4SI or do a DImode shuffle.  */
32614   if ((contents & (h1 | h3)) == contents)
32615     {
32616       for (i = 0; i < nelt2; ++i)
32617         {
32618           remap[i] = i * 2;
32619           remap[i + nelt] = i * 2 + 1;
32620           dremap.perm[i * 2] = i;
32621           dremap.perm[i * 2 + 1] = i + nelt;
32622         }
32623     }
32624   else if ((contents & (h2 | h4)) == contents)
32625     {
32626       for (i = 0; i < nelt2; ++i)
32627         {
32628           remap[i + nelt2] = i * 2;
32629           remap[i + nelt + nelt2] = i * 2 + 1;
32630           dremap.perm[i * 2] = i + nelt2;
32631           dremap.perm[i * 2 + 1] = i + nelt + nelt2;
32632         }
32633     }
32634   else if ((contents & (h1 | h4)) == contents)
32635     {
32636       for (i = 0; i < nelt2; ++i)
32637         {
32638           remap[i] = i;
32639           remap[i + nelt + nelt2] = i + nelt2;
32640           dremap.perm[i] = i;
32641           dremap.perm[i + nelt2] = i + nelt + nelt2;
32642         }
32643       if (nelt != 4)
32644         {
32645           dremap.vmode = V2DImode;
32646           dremap.nelt = 2;
32647           dremap.perm[0] = 0;
32648           dremap.perm[1] = 3;
32649         }
32650     }
32651   else if ((contents & (h2 | h3)) == contents)
32652     {
32653       for (i = 0; i < nelt2; ++i)
32654         {
32655           remap[i + nelt2] = i;
32656           remap[i + nelt] = i + nelt2;
32657           dremap.perm[i] = i + nelt2;
32658           dremap.perm[i + nelt2] = i + nelt;
32659         }
32660       if (nelt != 4)
32661         {
32662           dremap.vmode = V2DImode;
32663           dremap.nelt = 2;
32664           dremap.perm[0] = 1;
32665           dremap.perm[1] = 2;
32666         }
32667     }
32668   else
32669     return false;
32670
32671   /* Use the remapping array set up above to move the elements from their
32672      swizzled locations into their final destinations.  */
32673   dfinal = *d;
32674   for (i = 0; i < nelt; ++i)
32675     {
32676       unsigned e = remap[d->perm[i]];
32677       gcc_assert (e < nelt);
32678       dfinal.perm[i] = e;
32679     }
32680   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
32681   dfinal.op1 = dfinal.op0;
32682   dremap.target = dfinal.op0;
32683
32684   /* Test if the final remap can be done with a single insn.  For V4SFmode or
32685      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
32686   start_sequence ();
32687   ok = expand_vec_perm_1 (&dfinal);
32688   seq = get_insns ();
32689   end_sequence ();
32690
32691   if (!ok)
32692     return false;
32693
32694   if (dremap.vmode != dfinal.vmode)
32695     {
32696       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
32697       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
32698       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
32699     }
32700
32701   ok = expand_vec_perm_1 (&dremap);
32702   gcc_assert (ok);
32703
32704   emit_insn (seq);
32705   return true;
32706 }
32707
32708 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
32709    permutation with two pshufb insns and an ior.  We should have already
32710    failed all two instruction sequences.  */
32711
32712 static bool
32713 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
32714 {
32715   rtx rperm[2][16], vperm, l, h, op, m128;
32716   unsigned int i, nelt, eltsz;
32717
32718   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
32719     return false;
32720   gcc_assert (d->op0 != d->op1);
32721
32722   nelt = d->nelt;
32723   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
32724
32725   /* Generate two permutation masks.  If the required element is within
32726      the given vector it is shuffled into the proper lane.  If the required
32727      element is in the other vector, force a zero into the lane by setting
32728      bit 7 in the permutation mask.  */
32729   m128 = GEN_INT (-128);
32730   for (i = 0; i < nelt; ++i)
32731     {
32732       unsigned j, e = d->perm[i];
32733       unsigned which = (e >= nelt);
32734       if (e >= nelt)
32735         e -= nelt;
32736
32737       for (j = 0; j < eltsz; ++j)
32738         {
32739           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
32740           rperm[1-which][i*eltsz + j] = m128;
32741         }
32742     }
32743
32744   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
32745   vperm = force_reg (V16QImode, vperm);
32746
32747   l = gen_reg_rtx (V16QImode);
32748   op = gen_lowpart (V16QImode, d->op0);
32749   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
32750
32751   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
32752   vperm = force_reg (V16QImode, vperm);
32753
32754   h = gen_reg_rtx (V16QImode);
32755   op = gen_lowpart (V16QImode, d->op1);
32756   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
32757
32758   op = gen_lowpart (V16QImode, d->target);
32759   emit_insn (gen_iorv16qi3 (op, l, h));
32760
32761   return true;
32762 }
32763
32764 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
32765    and extract-odd permutations.  */
32766
32767 static bool
32768 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
32769 {
32770   rtx t1, t2, t3;
32771
32772   switch (d->vmode)
32773     {
32774     case V4DFmode:
32775       t1 = gen_reg_rtx (V4DFmode);
32776       t2 = gen_reg_rtx (V4DFmode);
32777
32778       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
32779       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
32780       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
32781
32782       /* Now an unpck[lh]pd will produce the result required.  */
32783       if (odd)
32784         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
32785       else
32786         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
32787       emit_insn (t3);
32788       break;
32789
32790     case V8SFmode:
32791       {
32792         int mask = odd ? 0xdd : 0x88;
32793
32794         t1 = gen_reg_rtx (V8SFmode);
32795         t2 = gen_reg_rtx (V8SFmode);
32796         t3 = gen_reg_rtx (V8SFmode);
32797
32798         /* Shuffle within the 128-bit lanes to produce:
32799            { 0 2 8 a 4 6 c e } | { 1 3 9 b 5 7 d f }.  */
32800         emit_insn (gen_avx_shufps256 (t1, d->op0, d->op1,
32801                                       GEN_INT (mask)));
32802
32803         /* Shuffle the lanes around to produce:
32804            { 4 6 c e 0 2 8 a } and { 5 7 d f 1 3 9 b }.  */
32805         emit_insn (gen_avx_vperm2f128v8sf3 (t2, t1, t1,
32806                                             GEN_INT (0x3)));
32807
32808         /* Shuffle within the 128-bit lanes to produce:
32809            { 0 2 4 6 4 6 0 2 } | { 1 3 5 7 5 7 1 3 }.  */
32810         emit_insn (gen_avx_shufps256 (t3, t1, t2, GEN_INT (0x44)));
32811
32812         /* Shuffle within the 128-bit lanes to produce:
32813            { 8 a c e c e 8 a } | { 9 b d f d f 9 b }.  */
32814         emit_insn (gen_avx_shufps256 (t2, t1, t2, GEN_INT (0xee)));
32815
32816         /* Shuffle the lanes around to produce:
32817            { 0 2 4 6 8 a c e } | { 1 3 5 7 9 b d f }.  */
32818         emit_insn (gen_avx_vperm2f128v8sf3 (d->target, t3, t2,
32819                                             GEN_INT (0x20)));
32820       }
32821       break;
32822
32823     case V2DFmode:
32824     case V4SFmode:
32825     case V2DImode:
32826     case V4SImode:
32827       /* These are always directly implementable by expand_vec_perm_1.  */
32828       gcc_unreachable ();
32829
32830     case V8HImode:
32831       if (TARGET_SSSE3)
32832         return expand_vec_perm_pshufb2 (d);
32833       else
32834         {
32835           /* We need 2*log2(N)-1 operations to achieve odd/even
32836              with interleave. */
32837           t1 = gen_reg_rtx (V8HImode);
32838           t2 = gen_reg_rtx (V8HImode);
32839           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
32840           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
32841           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
32842           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
32843           if (odd)
32844             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
32845           else
32846             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
32847           emit_insn (t3);
32848         }
32849       break;
32850
32851     case V16QImode:
32852       if (TARGET_SSSE3)
32853         return expand_vec_perm_pshufb2 (d);
32854       else
32855         {
32856           t1 = gen_reg_rtx (V16QImode);
32857           t2 = gen_reg_rtx (V16QImode);
32858           t3 = gen_reg_rtx (V16QImode);
32859           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
32860           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
32861           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
32862           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
32863           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
32864           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
32865           if (odd)
32866             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
32867           else
32868             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
32869           emit_insn (t3);
32870         }
32871       break;
32872
32873     default:
32874       gcc_unreachable ();
32875     }
32876
32877   return true;
32878 }
32879
32880 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
32881    extract-even and extract-odd permutations.  */
32882
32883 static bool
32884 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
32885 {
32886   unsigned i, odd, nelt = d->nelt;
32887
32888   odd = d->perm[0];
32889   if (odd != 0 && odd != 1)
32890     return false;
32891
32892   for (i = 1; i < nelt; ++i)
32893     if (d->perm[i] != 2 * i + odd)
32894       return false;
32895
32896   return expand_vec_perm_even_odd_1 (d, odd);
32897 }
32898
32899 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
32900    permutations.  We assume that expand_vec_perm_1 has already failed.  */
32901
32902 static bool
32903 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
32904 {
32905   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
32906   enum machine_mode vmode = d->vmode;
32907   unsigned char perm2[4];
32908   rtx op0 = d->op0;
32909   bool ok;
32910
32911   switch (vmode)
32912     {
32913     case V4DFmode:
32914     case V8SFmode:
32915       /* These are special-cased in sse.md so that we can optionally
32916          use the vbroadcast instruction.  They expand to two insns
32917          if the input happens to be in a register.  */
32918       gcc_unreachable ();
32919
32920     case V2DFmode:
32921     case V2DImode:
32922     case V4SFmode:
32923     case V4SImode:
32924       /* These are always implementable using standard shuffle patterns.  */
32925       gcc_unreachable ();
32926
32927     case V8HImode:
32928     case V16QImode:
32929       /* These can be implemented via interleave.  We save one insn by
32930          stopping once we have promoted to V4SImode and then use pshufd.  */
32931       do
32932         {
32933           optab otab = vec_interleave_low_optab;
32934
32935           if (elt >= nelt2)
32936             {
32937               otab = vec_interleave_high_optab;
32938               elt -= nelt2;
32939             }
32940           nelt2 /= 2;
32941
32942           op0 = expand_binop (vmode, otab, op0, op0, NULL, 0, OPTAB_DIRECT);
32943           vmode = get_mode_wider_vector (vmode);
32944           op0 = gen_lowpart (vmode, op0);
32945         }
32946       while (vmode != V4SImode);
32947
32948       memset (perm2, elt, 4);
32949       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
32950       gcc_assert (ok);
32951       return true;
32952
32953     default:
32954       gcc_unreachable ();
32955     }
32956 }
32957
32958 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
32959    broadcast permutations.  */
32960
32961 static bool
32962 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
32963 {
32964   unsigned i, elt, nelt = d->nelt;
32965
32966   if (d->op0 != d->op1)
32967     return false;
32968
32969   elt = d->perm[0];
32970   for (i = 1; i < nelt; ++i)
32971     if (d->perm[i] != elt)
32972       return false;
32973
32974   return expand_vec_perm_broadcast_1 (d);
32975 }
32976
32977 /* The guts of ix86_expand_vec_perm_builtin, also used by the ok hook.
32978    With all of the interface bits taken care of, perform the expansion
32979    in D and return true on success.  */
32980
32981 static bool
32982 ix86_expand_vec_perm_builtin_1 (struct expand_vec_perm_d *d)
32983 {
32984   /* Try a single instruction expansion.  */
32985   if (expand_vec_perm_1 (d))
32986     return true;
32987
32988   /* Try sequences of two instructions.  */
32989
32990   if (expand_vec_perm_pshuflw_pshufhw (d))
32991     return true;
32992
32993   if (expand_vec_perm_palignr (d))
32994     return true;
32995
32996   if (expand_vec_perm_interleave2 (d))
32997     return true;
32998
32999   if (expand_vec_perm_broadcast (d))
33000     return true;
33001
33002   /* Try sequences of three instructions.  */
33003
33004   if (expand_vec_perm_pshufb2 (d))
33005     return true;
33006
33007   /* ??? Look for narrow permutations whose element orderings would
33008      allow the promotion to a wider mode.  */
33009
33010   /* ??? Look for sequences of interleave or a wider permute that place
33011      the data into the correct lanes for a half-vector shuffle like
33012      pshuf[lh]w or vpermilps.  */
33013
33014   /* ??? Look for sequences of interleave that produce the desired results.
33015      The combinatorics of punpck[lh] get pretty ugly... */
33016
33017   if (expand_vec_perm_even_odd (d))
33018     return true;
33019
33020   return false;
33021 }
33022
33023 /* Extract the values from the vector CST into the permutation array in D.
33024    Return 0 on error, 1 if all values from the permutation come from the
33025    first vector, 2 if all values from the second vector, and 3 otherwise.  */
33026
33027 static int
33028 extract_vec_perm_cst (struct expand_vec_perm_d *d, tree cst)
33029 {
33030   tree list = TREE_VECTOR_CST_ELTS (cst);
33031   unsigned i, nelt = d->nelt;
33032   int ret = 0;
33033
33034   for (i = 0; i < nelt; ++i, list = TREE_CHAIN (list))
33035     {
33036       unsigned HOST_WIDE_INT e;
33037
33038       if (!host_integerp (TREE_VALUE (list), 1))
33039         return 0;
33040       e = tree_low_cst (TREE_VALUE (list), 1);
33041       if (e >= 2 * nelt)
33042         return 0;
33043
33044       ret |= (e < nelt ? 1 : 2);
33045       d->perm[i] = e;
33046     }
33047   gcc_assert (list == NULL);
33048
33049   /* For all elements from second vector, fold the elements to first.  */
33050   if (ret == 2)
33051     for (i = 0; i < nelt; ++i)
33052       d->perm[i] -= nelt;
33053
33054   return ret;
33055 }
33056
33057 static rtx
33058 ix86_expand_vec_perm_builtin (tree exp)
33059 {
33060   struct expand_vec_perm_d d;
33061   tree arg0, arg1, arg2;
33062
33063   arg0 = CALL_EXPR_ARG (exp, 0);
33064   arg1 = CALL_EXPR_ARG (exp, 1);
33065   arg2 = CALL_EXPR_ARG (exp, 2);
33066
33067   d.vmode = TYPE_MODE (TREE_TYPE (arg0));
33068   d.nelt = GET_MODE_NUNITS (d.vmode);
33069   d.testing_p = false;
33070   gcc_assert (VECTOR_MODE_P (d.vmode));
33071
33072   if (TREE_CODE (arg2) != VECTOR_CST)
33073     {
33074       error_at (EXPR_LOCATION (exp),
33075                 "vector permutation requires vector constant");
33076       goto exit_error;
33077     }
33078
33079   switch (extract_vec_perm_cst (&d, arg2))
33080     {
33081     default:
33082       gcc_unreachable();
33083
33084     case 0:
33085       error_at (EXPR_LOCATION (exp), "invalid vector permutation constant");
33086       goto exit_error;
33087
33088     case 3:
33089       if (!operand_equal_p (arg0, arg1, 0))
33090         {
33091           d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
33092           d.op0 = force_reg (d.vmode, d.op0);
33093           d.op1 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
33094           d.op1 = force_reg (d.vmode, d.op1);
33095           break;
33096         }
33097
33098       /* The elements of PERM do not suggest that only the first operand
33099          is used, but both operands are identical.  Allow easier matching
33100          of the permutation by folding the permutation into the single
33101          input vector.  */
33102       {
33103         unsigned i, nelt = d.nelt;
33104         for (i = 0; i < nelt; ++i)
33105           if (d.perm[i] >= nelt)
33106             d.perm[i] -= nelt;
33107       }
33108       /* FALLTHRU */
33109
33110     case 1:
33111       d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
33112       d.op0 = force_reg (d.vmode, d.op0);
33113       d.op1 = d.op0;
33114       break;
33115
33116     case 2:
33117       d.op0 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
33118       d.op0 = force_reg (d.vmode, d.op0);
33119       d.op1 = d.op0;
33120       break;
33121     }
33122
33123   d.target = gen_reg_rtx (d.vmode);
33124   if (ix86_expand_vec_perm_builtin_1 (&d))
33125     return d.target;
33126
33127   /* For compiler generated permutations, we should never got here, because
33128      the compiler should also be checking the ok hook.  But since this is a
33129      builtin the user has access too, so don't abort.  */
33130   switch (d.nelt)
33131     {
33132     case 2:
33133       sorry ("vector permutation (%d %d)", d.perm[0], d.perm[1]);
33134       break;
33135     case 4:
33136       sorry ("vector permutation (%d %d %d %d)",
33137              d.perm[0], d.perm[1], d.perm[2], d.perm[3]);
33138       break;
33139     case 8:
33140       sorry ("vector permutation (%d %d %d %d %d %d %d %d)",
33141              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
33142              d.perm[4], d.perm[5], d.perm[6], d.perm[7]);
33143       break;
33144     case 16:
33145       sorry ("vector permutation "
33146              "(%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d)",
33147              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
33148              d.perm[4], d.perm[5], d.perm[6], d.perm[7],
33149              d.perm[8], d.perm[9], d.perm[10], d.perm[11],
33150              d.perm[12], d.perm[13], d.perm[14], d.perm[15]);
33151       break;
33152     default:
33153       gcc_unreachable ();
33154     }
33155  exit_error:
33156   return CONST0_RTX (d.vmode);
33157 }
33158
33159 /* Implement targetm.vectorize.builtin_vec_perm_ok.  */
33160
33161 static bool
33162 ix86_vectorize_builtin_vec_perm_ok (tree vec_type, tree mask)
33163 {
33164   struct expand_vec_perm_d d;
33165   int vec_mask;
33166   bool ret, one_vec;
33167
33168   d.vmode = TYPE_MODE (vec_type);
33169   d.nelt = GET_MODE_NUNITS (d.vmode);
33170   d.testing_p = true;
33171
33172   /* Given sufficient ISA support we can just return true here
33173      for selected vector modes.  */
33174   if (GET_MODE_SIZE (d.vmode) == 16)
33175     {
33176       /* All implementable with a single vpperm insn.  */
33177       if (TARGET_XOP)
33178         return true;
33179       /* All implementable with 2 pshufb + 1 ior.  */
33180       if (TARGET_SSSE3)
33181         return true;
33182       /* All implementable with shufpd or unpck[lh]pd.  */
33183       if (d.nelt == 2)
33184         return true;
33185     }
33186
33187   vec_mask = extract_vec_perm_cst (&d, mask);
33188
33189   /* This hook is cannot be called in response to something that the
33190      user does (unlike the builtin expander) so we shouldn't ever see
33191      an error generated from the extract.  */
33192   gcc_assert (vec_mask > 0 && vec_mask <= 3);
33193   one_vec = (vec_mask != 3);
33194
33195   /* Implementable with shufps or pshufd.  */
33196   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
33197     return true;
33198
33199   /* Otherwise we have to go through the motions and see if we can
33200      figure out how to generate the requested permutation.  */
33201   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
33202   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
33203   if (!one_vec)
33204     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
33205
33206   start_sequence ();
33207   ret = ix86_expand_vec_perm_builtin_1 (&d);
33208   end_sequence ();
33209
33210   return ret;
33211 }
33212
33213 void
33214 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
33215 {
33216   struct expand_vec_perm_d d;
33217   unsigned i, nelt;
33218
33219   d.target = targ;
33220   d.op0 = op0;
33221   d.op1 = op1;
33222   d.vmode = GET_MODE (targ);
33223   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
33224   d.testing_p = false;
33225
33226   for (i = 0; i < nelt; ++i)
33227     d.perm[i] = i * 2 + odd;
33228
33229   /* We'll either be able to implement the permutation directly...  */
33230   if (expand_vec_perm_1 (&d))
33231     return;
33232
33233   /* ... or we use the special-case patterns.  */
33234   expand_vec_perm_even_odd_1 (&d, odd);
33235 }
33236 \f
33237 /* This function returns the calling abi specific va_list type node.
33238    It returns  the FNDECL specific va_list type.  */
33239
33240 static tree
33241 ix86_fn_abi_va_list (tree fndecl)
33242 {
33243   if (!TARGET_64BIT)
33244     return va_list_type_node;
33245   gcc_assert (fndecl != NULL_TREE);
33246
33247   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
33248     return ms_va_list_type_node;
33249   else
33250     return sysv_va_list_type_node;
33251 }
33252
33253 /* Returns the canonical va_list type specified by TYPE. If there
33254    is no valid TYPE provided, it return NULL_TREE.  */
33255
33256 static tree
33257 ix86_canonical_va_list_type (tree type)
33258 {
33259   tree wtype, htype;
33260
33261   /* Resolve references and pointers to va_list type.  */
33262   if (TREE_CODE (type) == MEM_REF)
33263     type = TREE_TYPE (type);
33264   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
33265     type = TREE_TYPE (type);
33266   else if (POINTER_TYPE_P (type) && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
33267     type = TREE_TYPE (type);
33268
33269   if (TARGET_64BIT)
33270     {
33271       wtype = va_list_type_node;
33272           gcc_assert (wtype != NULL_TREE);
33273       htype = type;
33274       if (TREE_CODE (wtype) == ARRAY_TYPE)
33275         {
33276           /* If va_list is an array type, the argument may have decayed
33277              to a pointer type, e.g. by being passed to another function.
33278              In that case, unwrap both types so that we can compare the
33279              underlying records.  */
33280           if (TREE_CODE (htype) == ARRAY_TYPE
33281               || POINTER_TYPE_P (htype))
33282             {
33283               wtype = TREE_TYPE (wtype);
33284               htype = TREE_TYPE (htype);
33285             }
33286         }
33287       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
33288         return va_list_type_node;
33289       wtype = sysv_va_list_type_node;
33290           gcc_assert (wtype != NULL_TREE);
33291       htype = type;
33292       if (TREE_CODE (wtype) == ARRAY_TYPE)
33293         {
33294           /* If va_list is an array type, the argument may have decayed
33295              to a pointer type, e.g. by being passed to another function.
33296              In that case, unwrap both types so that we can compare the
33297              underlying records.  */
33298           if (TREE_CODE (htype) == ARRAY_TYPE
33299               || POINTER_TYPE_P (htype))
33300             {
33301               wtype = TREE_TYPE (wtype);
33302               htype = TREE_TYPE (htype);
33303             }
33304         }
33305       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
33306         return sysv_va_list_type_node;
33307       wtype = ms_va_list_type_node;
33308           gcc_assert (wtype != NULL_TREE);
33309       htype = type;
33310       if (TREE_CODE (wtype) == ARRAY_TYPE)
33311         {
33312           /* If va_list is an array type, the argument may have decayed
33313              to a pointer type, e.g. by being passed to another function.
33314              In that case, unwrap both types so that we can compare the
33315              underlying records.  */
33316           if (TREE_CODE (htype) == ARRAY_TYPE
33317               || POINTER_TYPE_P (htype))
33318             {
33319               wtype = TREE_TYPE (wtype);
33320               htype = TREE_TYPE (htype);
33321             }
33322         }
33323       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
33324         return ms_va_list_type_node;
33325       return NULL_TREE;
33326     }
33327   return std_canonical_va_list_type (type);
33328 }
33329
33330 /* Iterate through the target-specific builtin types for va_list.
33331    IDX denotes the iterator, *PTREE is set to the result type of
33332    the va_list builtin, and *PNAME to its internal type.
33333    Returns zero if there is no element for this index, otherwise
33334    IDX should be increased upon the next call.
33335    Note, do not iterate a base builtin's name like __builtin_va_list.
33336    Used from c_common_nodes_and_builtins.  */
33337
33338 static int
33339 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
33340 {
33341   if (TARGET_64BIT)
33342     {
33343       switch (idx)
33344         {
33345         default:
33346           break;
33347
33348         case 0:
33349           *ptree = ms_va_list_type_node;
33350           *pname = "__builtin_ms_va_list";
33351           return 1;
33352
33353         case 1:
33354           *ptree = sysv_va_list_type_node;
33355           *pname = "__builtin_sysv_va_list";
33356           return 1;
33357         }
33358     }
33359
33360   return 0;
33361 }
33362
33363 #undef TARGET_SCHED_DISPATCH
33364 #define TARGET_SCHED_DISPATCH has_dispatch
33365 #undef TARGET_SCHED_DISPATCH_DO
33366 #define TARGET_SCHED_DISPATCH_DO do_dispatch
33367
33368 /* The size of the dispatch window is the total number of bytes of
33369    object code allowed in a window.  */
33370 #define DISPATCH_WINDOW_SIZE 16
33371
33372 /* Number of dispatch windows considered for scheduling.  */
33373 #define MAX_DISPATCH_WINDOWS 3
33374
33375 /* Maximum number of instructions in a window.  */
33376 #define MAX_INSN 4
33377
33378 /* Maximum number of immediate operands in a window.  */
33379 #define MAX_IMM 4
33380
33381 /* Maximum number of immediate bits allowed in a window.  */
33382 #define MAX_IMM_SIZE 128
33383
33384 /* Maximum number of 32 bit immediates allowed in a window.  */
33385 #define MAX_IMM_32 4
33386
33387 /* Maximum number of 64 bit immediates allowed in a window.  */
33388 #define MAX_IMM_64 2
33389
33390 /* Maximum total of loads or prefetches allowed in a window.  */
33391 #define MAX_LOAD 2
33392
33393 /* Maximum total of stores allowed in a window.  */
33394 #define MAX_STORE 1
33395
33396 #undef BIG
33397 #define BIG 100
33398
33399
33400 /* Dispatch groups.  Istructions that affect the mix in a dispatch window.  */
33401 enum dispatch_group {
33402   disp_no_group = 0,
33403   disp_load,
33404   disp_store,
33405   disp_load_store,
33406   disp_prefetch,
33407   disp_imm,
33408   disp_imm_32,
33409   disp_imm_64,
33410   disp_branch,
33411   disp_cmp,
33412   disp_jcc,
33413   disp_last
33414 };
33415
33416 /* Number of allowable groups in a dispatch window.  It is an array
33417    indexed by dispatch_group enum.  100 is used as a big number,
33418    because the number of these kind of operations does not have any
33419    effect in dispatch window, but we need them for other reasons in
33420    the table.  */
33421 static unsigned int num_allowable_groups[disp_last] = {
33422   0, 2, 1, 1, 2, 4, 4, 2, 1, BIG, BIG
33423 };
33424
33425 char group_name[disp_last + 1][16] = {
33426   "disp_no_group", "disp_load", "disp_store", "disp_load_store",
33427   "disp_prefetch", "disp_imm", "disp_imm_32", "disp_imm_64",
33428   "disp_branch", "disp_cmp", "disp_jcc", "disp_last"
33429 };
33430
33431 /* Instruction path.  */
33432 enum insn_path {
33433   no_path = 0,
33434   path_single, /* Single micro op.  */
33435   path_double, /* Double micro op.  */
33436   path_multi,  /* Instructions with more than 2 micro op..  */
33437   last_path
33438 };
33439
33440 /* sched_insn_info defines a window to the instructions scheduled in
33441    the basic block.  It contains a pointer to the insn_info table and
33442    the instruction scheduled.
33443
33444    Windows are allocated for each basic block and are linked
33445    together.  */
33446 typedef struct sched_insn_info_s {
33447   rtx insn;
33448   enum dispatch_group group;
33449   enum insn_path path;
33450   int byte_len;
33451   int imm_bytes;
33452 } sched_insn_info;
33453
33454 /* Linked list of dispatch windows.  This is a two way list of
33455    dispatch windows of a basic block.  It contains information about
33456    the number of uops in the window and the total number of
33457    instructions and of bytes in the object code for this dispatch
33458    window.  */
33459 typedef struct dispatch_windows_s {
33460   int num_insn;            /* Number of insn in the window.  */
33461   int num_uops;            /* Number of uops in the window.  */
33462   int window_size;         /* Number of bytes in the window.  */
33463   int window_num;          /* Window number between 0 or 1.  */
33464   int num_imm;             /* Number of immediates in an insn.  */
33465   int num_imm_32;          /* Number of 32 bit immediates in an insn.  */
33466   int num_imm_64;          /* Number of 64 bit immediates in an insn.  */
33467   int imm_size;            /* Total immediates in the window.  */
33468   int num_loads;           /* Total memory loads in the window.  */
33469   int num_stores;          /* Total memory stores in the window.  */
33470   int violation;          /* Violation exists in window.  */
33471   sched_insn_info *window; /* Pointer to the window.  */
33472   struct dispatch_windows_s *next;
33473   struct dispatch_windows_s *prev;
33474 } dispatch_windows;
33475
33476 /* Immediate valuse used in an insn.  */
33477 typedef struct imm_info_s
33478   {
33479     int imm;
33480     int imm32;
33481     int imm64;
33482   } imm_info;
33483
33484 static dispatch_windows *dispatch_window_list;
33485 static dispatch_windows *dispatch_window_list1;
33486
33487 /* Get dispatch group of insn.  */
33488
33489 static enum dispatch_group
33490 get_mem_group (rtx insn)
33491 {
33492   enum attr_memory memory;
33493
33494   if (INSN_CODE (insn) < 0)
33495     return disp_no_group;
33496   memory = get_attr_memory (insn);
33497   if (memory == MEMORY_STORE)
33498     return disp_store;
33499
33500   if (memory == MEMORY_LOAD)
33501     return disp_load;
33502
33503   if (memory == MEMORY_BOTH)
33504     return disp_load_store;
33505
33506   return disp_no_group;
33507 }
33508
33509 /* Return true if insn is a compare instruction.  */
33510
33511 static bool
33512 is_cmp (rtx insn)
33513 {
33514   enum attr_type type;
33515
33516   type = get_attr_type (insn);
33517   return (type == TYPE_TEST
33518           || type == TYPE_ICMP
33519           || type == TYPE_FCMP
33520           || GET_CODE (PATTERN (insn)) == COMPARE);
33521 }
33522
33523 /* Return true if a dispatch violation encountered.  */
33524
33525 static bool
33526 dispatch_violation (void)
33527 {
33528   if (dispatch_window_list->next)
33529     return dispatch_window_list->next->violation;
33530   return dispatch_window_list->violation;
33531 }
33532
33533 /* Return true if insn is a branch instruction.  */
33534
33535 static bool
33536 is_branch (rtx insn)
33537 {
33538   return (CALL_P (insn) || JUMP_P (insn));
33539 }
33540
33541 /* Return true if insn is a prefetch instruction.  */
33542
33543 static bool
33544 is_prefetch (rtx insn)
33545 {
33546   return NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == PREFETCH;
33547 }
33548
33549 /* This function initializes a dispatch window and the list container holding a
33550    pointer to the window.  */
33551
33552 static void
33553 init_window (int window_num)
33554 {
33555   int i;
33556   dispatch_windows *new_list;
33557
33558   if (window_num == 0)
33559     new_list = dispatch_window_list;
33560   else
33561     new_list = dispatch_window_list1;
33562
33563   new_list->num_insn = 0;
33564   new_list->num_uops = 0;
33565   new_list->window_size = 0;
33566   new_list->next = NULL;
33567   new_list->prev = NULL;
33568   new_list->window_num = window_num;
33569   new_list->num_imm = 0;
33570   new_list->num_imm_32 = 0;
33571   new_list->num_imm_64 = 0;
33572   new_list->imm_size = 0;
33573   new_list->num_loads = 0;
33574   new_list->num_stores = 0;
33575   new_list->violation = false;
33576
33577   for (i = 0; i < MAX_INSN; i++)
33578     {
33579       new_list->window[i].insn = NULL;
33580       new_list->window[i].group = disp_no_group;
33581       new_list->window[i].path = no_path;
33582       new_list->window[i].byte_len = 0;
33583       new_list->window[i].imm_bytes = 0;
33584     }
33585   return;
33586 }
33587
33588 /* This function allocates and initializes a dispatch window and the
33589    list container holding a pointer to the window.  */
33590
33591 static dispatch_windows *
33592 allocate_window (void)
33593 {
33594   dispatch_windows *new_list = XNEW (struct dispatch_windows_s);
33595   new_list->window = XNEWVEC (struct sched_insn_info_s, MAX_INSN + 1);
33596
33597   return new_list;
33598 }
33599
33600 /* This routine initializes the dispatch scheduling information.  It
33601    initiates building dispatch scheduler tables and constructs the
33602    first dispatch window.  */
33603
33604 static void
33605 init_dispatch_sched (void)
33606 {
33607   /* Allocate a dispatch list and a window.  */
33608   dispatch_window_list = allocate_window ();
33609   dispatch_window_list1 = allocate_window ();
33610   init_window (0);
33611   init_window (1);
33612 }
33613
33614 /* This function returns true if a branch is detected.  End of a basic block
33615    does not have to be a branch, but here we assume only branches end a
33616    window.  */
33617
33618 static bool
33619 is_end_basic_block (enum dispatch_group group)
33620 {
33621   return group == disp_branch;
33622 }
33623
33624 /* This function is called when the end of a window processing is reached.  */
33625
33626 static void
33627 process_end_window (void)
33628 {
33629   gcc_assert (dispatch_window_list->num_insn <= MAX_INSN);
33630   if (dispatch_window_list->next)
33631     {
33632       gcc_assert (dispatch_window_list1->num_insn <= MAX_INSN);
33633       gcc_assert (dispatch_window_list->window_size
33634                   + dispatch_window_list1->window_size <= 48);
33635       init_window (1);
33636     }
33637   init_window (0);
33638 }
33639
33640 /* Allocates a new dispatch window and adds it to WINDOW_LIST.
33641    WINDOW_NUM is either 0 or 1.  A maximum of two windows are generated
33642    for 48 bytes of instructions.  Note that these windows are not dispatch
33643    windows that their sizes are DISPATCH_WINDOW_SIZE.  */
33644
33645 static dispatch_windows *
33646 allocate_next_window (int window_num)
33647 {
33648   if (window_num == 0)
33649     {
33650       if (dispatch_window_list->next)
33651           init_window (1);
33652       init_window (0);
33653       return dispatch_window_list;
33654     }
33655
33656   dispatch_window_list->next = dispatch_window_list1;
33657   dispatch_window_list1->prev = dispatch_window_list;
33658
33659   return dispatch_window_list1;
33660 }
33661
33662 /* Increment the number of immediate operands of an instruction.  */
33663
33664 static int
33665 find_constant_1 (rtx *in_rtx, imm_info *imm_values)
33666 {
33667   if (*in_rtx == 0)
33668     return 0;
33669
33670     switch ( GET_CODE (*in_rtx))
33671     {
33672     case CONST:
33673     case SYMBOL_REF:
33674     case CONST_INT:
33675       (imm_values->imm)++;
33676       if (x86_64_immediate_operand (*in_rtx, SImode))
33677         (imm_values->imm32)++;
33678       else
33679         (imm_values->imm64)++;
33680       break;
33681
33682     case CONST_DOUBLE:
33683       (imm_values->imm)++;
33684       (imm_values->imm64)++;
33685       break;
33686
33687     case CODE_LABEL:
33688       if (LABEL_KIND (*in_rtx) == LABEL_NORMAL)
33689         {
33690           (imm_values->imm)++;
33691           (imm_values->imm32)++;
33692         }
33693       break;
33694
33695     default:
33696       break;
33697     }
33698
33699   return 0;
33700 }
33701
33702 /* Compute number of immediate operands of an instruction.  */
33703
33704 static void
33705 find_constant (rtx in_rtx, imm_info *imm_values)
33706 {
33707   for_each_rtx (INSN_P (in_rtx) ? &PATTERN (in_rtx) : &in_rtx,
33708                 (rtx_function) find_constant_1, (void *) imm_values);
33709 }
33710
33711 /* Return total size of immediate operands of an instruction along with number
33712    of corresponding immediate-operands.  It initializes its parameters to zero
33713    befor calling FIND_CONSTANT.
33714    INSN is the input instruction.  IMM is the total of immediates.
33715    IMM32 is the number of 32 bit immediates.  IMM64 is the number of 64
33716    bit immediates.  */
33717
33718 static int
33719 get_num_immediates (rtx insn, int *imm, int *imm32, int *imm64)
33720 {
33721   imm_info imm_values = {0, 0, 0};
33722
33723   find_constant (insn, &imm_values);
33724   *imm = imm_values.imm;
33725   *imm32 = imm_values.imm32;
33726   *imm64 = imm_values.imm64;
33727   return imm_values.imm32 * 4 + imm_values.imm64 * 8;
33728 }
33729
33730 /* This function indicates if an operand of an instruction is an
33731    immediate.  */
33732
33733 static bool
33734 has_immediate (rtx insn)
33735 {
33736   int num_imm_operand;
33737   int num_imm32_operand;
33738   int num_imm64_operand;
33739
33740   if (insn)
33741     return get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
33742                                &num_imm64_operand);
33743   return false;
33744 }
33745
33746 /* Return single or double path for instructions.  */
33747
33748 static enum insn_path
33749 get_insn_path (rtx insn)
33750 {
33751   enum attr_amdfam10_decode path = get_attr_amdfam10_decode (insn);
33752
33753   if ((int)path == 0)
33754     return path_single;
33755
33756   if ((int)path == 1)
33757     return path_double;
33758
33759   return path_multi;
33760 }
33761
33762 /* Return insn dispatch group.  */
33763
33764 static enum dispatch_group
33765 get_insn_group (rtx insn)
33766 {
33767   enum dispatch_group group = get_mem_group (insn);
33768   if (group)
33769     return group;
33770
33771   if (is_branch (insn))
33772     return disp_branch;
33773
33774   if (is_cmp (insn))
33775     return disp_cmp;
33776
33777   if (has_immediate (insn))
33778     return disp_imm;
33779
33780   if (is_prefetch (insn))
33781     return disp_prefetch;
33782
33783   return disp_no_group;
33784 }
33785
33786 /* Count number of GROUP restricted instructions in a dispatch
33787    window WINDOW_LIST.  */
33788
33789 static int
33790 count_num_restricted (rtx insn, dispatch_windows *window_list)
33791 {
33792   enum dispatch_group group = get_insn_group (insn);
33793   int imm_size;
33794   int num_imm_operand;
33795   int num_imm32_operand;
33796   int num_imm64_operand;
33797
33798   if (group == disp_no_group)
33799     return 0;
33800
33801   if (group == disp_imm)
33802     {
33803       imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
33804                               &num_imm64_operand);
33805       if (window_list->imm_size + imm_size > MAX_IMM_SIZE
33806           || num_imm_operand + window_list->num_imm > MAX_IMM
33807           || (num_imm32_operand > 0
33808               && (window_list->num_imm_32 + num_imm32_operand > MAX_IMM_32
33809                   || window_list->num_imm_64 * 2 + num_imm32_operand > MAX_IMM_32))
33810           || (num_imm64_operand > 0
33811               && (window_list->num_imm_64 + num_imm64_operand > MAX_IMM_64
33812                   || window_list->num_imm_32 + num_imm64_operand * 2 > MAX_IMM_32))
33813           || (window_list->imm_size + imm_size == MAX_IMM_SIZE
33814               && num_imm64_operand > 0
33815               && ((window_list->num_imm_64 > 0
33816                    && window_list->num_insn >= 2)
33817                   || window_list->num_insn >= 3)))
33818         return BIG;
33819
33820       return 1;
33821     }
33822
33823   if ((group == disp_load_store
33824        && (window_list->num_loads >= MAX_LOAD
33825            || window_list->num_stores >= MAX_STORE))
33826       || ((group == disp_load
33827            || group == disp_prefetch)
33828           && window_list->num_loads >= MAX_LOAD)
33829       || (group == disp_store
33830           && window_list->num_stores >= MAX_STORE))
33831     return BIG;
33832
33833   return 1;
33834 }
33835
33836 /* This function returns true if insn satisfies dispatch rules on the
33837    last window scheduled.  */
33838
33839 static bool
33840 fits_dispatch_window (rtx insn)
33841 {
33842   dispatch_windows *window_list = dispatch_window_list;
33843   dispatch_windows *window_list_next = dispatch_window_list->next;
33844   unsigned int num_restrict;
33845   enum dispatch_group group = get_insn_group (insn);
33846   enum insn_path path = get_insn_path (insn);
33847   int sum;
33848
33849   /* Make disp_cmp and disp_jcc get scheduled at the latest.  These
33850      instructions should be given the lowest priority in the
33851      scheduling process in Haifa scheduler to make sure they will be
33852      scheduled in the same dispatch window as the refrence to them.  */
33853   if (group == disp_jcc || group == disp_cmp)
33854     return false;
33855
33856   /* Check nonrestricted.  */
33857   if (group == disp_no_group || group == disp_branch)
33858     return true;
33859
33860   /* Get last dispatch window.  */
33861   if (window_list_next)
33862     window_list = window_list_next;
33863
33864   if (window_list->window_num == 1)
33865     {
33866       sum = window_list->prev->window_size + window_list->window_size;
33867
33868       if (sum == 32
33869           || (min_insn_size (insn) + sum) >= 48)
33870         /* Window 1 is full.  Go for next window.  */
33871         return true;
33872     }
33873
33874   num_restrict = count_num_restricted (insn, window_list);
33875
33876   if (num_restrict > num_allowable_groups[group])
33877     return false;
33878
33879   /* See if it fits in the first window.  */
33880   if (window_list->window_num == 0)
33881     {
33882       /* The first widow should have only single and double path
33883          uops.  */
33884       if (path == path_double
33885           && (window_list->num_uops + 2) > MAX_INSN)
33886         return false;
33887       else if (path != path_single)
33888         return false;
33889     }
33890   return true;
33891 }
33892
33893 /* Add an instruction INSN with NUM_UOPS micro-operations to the
33894    dispatch window WINDOW_LIST.  */
33895
33896 static void
33897 add_insn_window (rtx insn, dispatch_windows *window_list, int num_uops)
33898 {
33899   int byte_len = min_insn_size (insn);
33900   int num_insn = window_list->num_insn;
33901   int imm_size;
33902   sched_insn_info *window = window_list->window;
33903   enum dispatch_group group = get_insn_group (insn);
33904   enum insn_path path = get_insn_path (insn);
33905   int num_imm_operand;
33906   int num_imm32_operand;
33907   int num_imm64_operand;
33908
33909   if (!window_list->violation && group != disp_cmp
33910       && !fits_dispatch_window (insn))
33911     window_list->violation = true;
33912
33913   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
33914                                  &num_imm64_operand);
33915
33916   /* Initialize window with new instruction.  */
33917   window[num_insn].insn = insn;
33918   window[num_insn].byte_len = byte_len;
33919   window[num_insn].group = group;
33920   window[num_insn].path = path;
33921   window[num_insn].imm_bytes = imm_size;
33922
33923   window_list->window_size += byte_len;
33924   window_list->num_insn = num_insn + 1;
33925   window_list->num_uops = window_list->num_uops + num_uops;
33926   window_list->imm_size += imm_size;
33927   window_list->num_imm += num_imm_operand;
33928   window_list->num_imm_32 += num_imm32_operand;
33929   window_list->num_imm_64 += num_imm64_operand;
33930
33931   if (group == disp_store)
33932     window_list->num_stores += 1;
33933   else if (group == disp_load
33934            || group == disp_prefetch)
33935     window_list->num_loads += 1;
33936   else if (group == disp_load_store)
33937     {
33938       window_list->num_stores += 1;
33939       window_list->num_loads += 1;
33940     }
33941 }
33942
33943 /* Adds a scheduled instruction, INSN, to the current dispatch window.
33944    If the total bytes of instructions or the number of instructions in
33945    the window exceed allowable, it allocates a new window.  */
33946
33947 static void
33948 add_to_dispatch_window (rtx insn)
33949 {
33950   int byte_len;
33951   dispatch_windows *window_list;
33952   dispatch_windows *next_list;
33953   dispatch_windows *window0_list;
33954   enum insn_path path;
33955   enum dispatch_group insn_group;
33956   bool insn_fits;
33957   int num_insn;
33958   int num_uops;
33959   int window_num;
33960   int insn_num_uops;
33961   int sum;
33962
33963   if (INSN_CODE (insn) < 0)
33964     return;
33965
33966   byte_len = min_insn_size (insn);
33967   window_list = dispatch_window_list;
33968   next_list = window_list->next;
33969   path = get_insn_path (insn);
33970   insn_group = get_insn_group (insn);
33971
33972   /* Get the last dispatch window.  */
33973   if (next_list)
33974       window_list = dispatch_window_list->next;
33975
33976   if (path == path_single)
33977     insn_num_uops = 1;
33978   else if (path == path_double)
33979     insn_num_uops = 2;
33980   else
33981     insn_num_uops = (int) path;
33982
33983   /* If current window is full, get a new window.
33984      Window number zero is full, if MAX_INSN uops are scheduled in it.
33985      Window number one is full, if window zero's bytes plus window
33986      one's bytes is 32, or if the bytes of the new instruction added
33987      to the total makes it greater than 48, or it has already MAX_INSN
33988      instructions in it.  */
33989   num_insn = window_list->num_insn;
33990   num_uops = window_list->num_uops;
33991   window_num = window_list->window_num;
33992   insn_fits = fits_dispatch_window (insn);
33993
33994   if (num_insn >= MAX_INSN
33995       || num_uops + insn_num_uops > MAX_INSN
33996       || !(insn_fits))
33997     {
33998       window_num = ~window_num & 1;
33999       window_list = allocate_next_window (window_num);
34000     }
34001
34002   if (window_num == 0)
34003     {
34004       add_insn_window (insn, window_list, insn_num_uops);
34005       if (window_list->num_insn >= MAX_INSN
34006           && insn_group == disp_branch)
34007         {
34008           process_end_window ();
34009           return;
34010         }
34011     }
34012   else if (window_num == 1)
34013     {
34014       window0_list = window_list->prev;
34015       sum = window0_list->window_size + window_list->window_size;
34016       if (sum == 32
34017           || (byte_len + sum) >= 48)
34018         {
34019           process_end_window ();
34020           window_list = dispatch_window_list;
34021         }
34022
34023       add_insn_window (insn, window_list, insn_num_uops);
34024     }
34025   else
34026     gcc_unreachable ();
34027
34028   if (is_end_basic_block (insn_group))
34029     {
34030       /* End of basic block is reached do end-basic-block process.  */
34031       process_end_window ();
34032       return;
34033     }
34034 }
34035
34036 /* Print the dispatch window, WINDOW_NUM, to FILE.  */
34037
34038 DEBUG_FUNCTION static void
34039 debug_dispatch_window_file (FILE *file, int window_num)
34040 {
34041   dispatch_windows *list;
34042   int i;
34043
34044   if (window_num == 0)
34045     list = dispatch_window_list;
34046   else
34047     list = dispatch_window_list1;
34048
34049   fprintf (file, "Window #%d:\n", list->window_num);
34050   fprintf (file, "  num_insn = %d, num_uops = %d, window_size = %d\n",
34051           list->num_insn, list->num_uops, list->window_size);
34052   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
34053            list->num_imm, list->num_imm_32, list->num_imm_64, list->imm_size);
34054
34055   fprintf (file, "  num_loads = %d, num_stores = %d\n", list->num_loads,
34056           list->num_stores);
34057   fprintf (file, " insn info:\n");
34058
34059   for (i = 0; i < MAX_INSN; i++)
34060     {
34061       if (!list->window[i].insn)
34062         break;
34063       fprintf (file, "    group[%d] = %s, insn[%d] = %p, path[%d] = %d byte_len[%d] = %d, imm_bytes[%d] = %d\n",
34064               i, group_name[list->window[i].group],
34065               i, (void *)list->window[i].insn,
34066               i, list->window[i].path,
34067               i, list->window[i].byte_len,
34068               i, list->window[i].imm_bytes);
34069     }
34070 }
34071
34072 /* Print to stdout a dispatch window.  */
34073
34074 DEBUG_FUNCTION void
34075 debug_dispatch_window (int window_num)
34076 {
34077   debug_dispatch_window_file (stdout, window_num);
34078 }
34079
34080 /* Print INSN dispatch information to FILE.  */
34081
34082 DEBUG_FUNCTION static void
34083 debug_insn_dispatch_info_file (FILE *file, rtx insn)
34084 {
34085   int byte_len;
34086   enum insn_path path;
34087   enum dispatch_group group;
34088   int imm_size;
34089   int num_imm_operand;
34090   int num_imm32_operand;
34091   int num_imm64_operand;
34092
34093   if (INSN_CODE (insn) < 0)
34094     return;
34095
34096   byte_len = min_insn_size (insn);
34097   path = get_insn_path (insn);
34098   group = get_insn_group (insn);
34099   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
34100                                  &num_imm64_operand);
34101
34102   fprintf (file, " insn info:\n");
34103   fprintf (file, "  group = %s, path = %d, byte_len = %d\n",
34104            group_name[group], path, byte_len);
34105   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
34106            num_imm_operand, num_imm32_operand, num_imm64_operand, imm_size);
34107 }
34108
34109 /* Print to STDERR the status of the ready list with respect to
34110    dispatch windows.  */
34111
34112 DEBUG_FUNCTION void
34113 debug_ready_dispatch (void)
34114 {
34115   int i;
34116   int no_ready = number_in_ready ();
34117
34118   fprintf (stdout, "Number of ready: %d\n", no_ready);
34119
34120   for (i = 0; i < no_ready; i++)
34121     debug_insn_dispatch_info_file (stdout, get_ready_element (i));
34122 }
34123
34124 /* This routine is the driver of the dispatch scheduler.  */
34125
34126 static void
34127 do_dispatch (rtx insn, int mode)
34128 {
34129   if (mode == DISPATCH_INIT)
34130     init_dispatch_sched ();
34131   else if (mode == ADD_TO_DISPATCH_WINDOW)
34132     add_to_dispatch_window (insn);
34133 }
34134
34135 /* Return TRUE if Dispatch Scheduling is supported.  */
34136
34137 static bool
34138 has_dispatch (rtx insn, int action)
34139 {
34140   if (ix86_tune == PROCESSOR_BDVER1 && flag_dispatch_scheduler)
34141     switch (action)
34142       {
34143       default:
34144         return false;
34145
34146       case IS_DISPATCH_ON:
34147         return true;
34148         break;
34149
34150       case IS_CMP:
34151         return is_cmp (insn);
34152
34153       case DISPATCH_VIOLATION:
34154         return dispatch_violation ();
34155
34156       case FITS_DISPATCH_WINDOW:
34157         return fits_dispatch_window (insn);
34158       }
34159
34160   return false;
34161 }
34162
34163 /* ??? No autovectorization into MMX or 3DNOW until we can reliably
34164    place emms and femms instructions.  */
34165
34166 static enum machine_mode
34167 ix86_preferred_simd_mode (enum machine_mode mode)
34168 {
34169   /* Disable double precision vectorizer if needed.  */
34170   if (mode == DFmode && !TARGET_VECTORIZE_DOUBLE)
34171     return word_mode;
34172
34173   if (!TARGET_AVX && !TARGET_SSE)
34174     return word_mode;
34175
34176   switch (mode)
34177     {
34178     case SFmode:
34179       return TARGET_AVX ? V8SFmode : V4SFmode;
34180     case DFmode:
34181       return TARGET_AVX ? V4DFmode : V2DFmode;
34182     case DImode:
34183       return V2DImode;
34184     case SImode:
34185       return V4SImode;
34186     case HImode:
34187       return V8HImode;
34188     case QImode:
34189       return V16QImode;
34190
34191     default:;
34192     }
34193
34194   return word_mode;
34195 }
34196
34197 /* If AVX is enabled then try vectorizing with both 256bit and 128bit
34198    vectors.  */
34199
34200 static unsigned int
34201 ix86_autovectorize_vector_sizes (void)
34202 {
34203   return TARGET_AVX ? 32 | 16 : 0;
34204 }
34205
34206 /* Initialize the GCC target structure.  */
34207 #undef TARGET_RETURN_IN_MEMORY
34208 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
34209
34210 #undef TARGET_LEGITIMIZE_ADDRESS
34211 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
34212
34213 #undef TARGET_ATTRIBUTE_TABLE
34214 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
34215 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
34216 #  undef TARGET_MERGE_DECL_ATTRIBUTES
34217 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
34218 #endif
34219
34220 #undef TARGET_COMP_TYPE_ATTRIBUTES
34221 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
34222
34223 #undef TARGET_INIT_BUILTINS
34224 #define TARGET_INIT_BUILTINS ix86_init_builtins
34225 #undef TARGET_BUILTIN_DECL
34226 #define TARGET_BUILTIN_DECL ix86_builtin_decl
34227 #undef TARGET_EXPAND_BUILTIN
34228 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
34229
34230 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
34231 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
34232   ix86_builtin_vectorized_function
34233
34234 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
34235 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_vectorize_builtin_conversion
34236
34237 #undef TARGET_BUILTIN_RECIPROCAL
34238 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
34239
34240 #undef TARGET_ASM_FUNCTION_EPILOGUE
34241 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
34242
34243 #undef TARGET_ENCODE_SECTION_INFO
34244 #ifndef SUBTARGET_ENCODE_SECTION_INFO
34245 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
34246 #else
34247 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
34248 #endif
34249
34250 #undef TARGET_ASM_OPEN_PAREN
34251 #define TARGET_ASM_OPEN_PAREN ""
34252 #undef TARGET_ASM_CLOSE_PAREN
34253 #define TARGET_ASM_CLOSE_PAREN ""
34254
34255 #undef TARGET_ASM_BYTE_OP
34256 #define TARGET_ASM_BYTE_OP ASM_BYTE
34257
34258 #undef TARGET_ASM_ALIGNED_HI_OP
34259 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
34260 #undef TARGET_ASM_ALIGNED_SI_OP
34261 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
34262 #ifdef ASM_QUAD
34263 #undef TARGET_ASM_ALIGNED_DI_OP
34264 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
34265 #endif
34266
34267 #undef TARGET_PROFILE_BEFORE_PROLOGUE
34268 #define TARGET_PROFILE_BEFORE_PROLOGUE ix86_profile_before_prologue
34269
34270 #undef TARGET_ASM_UNALIGNED_HI_OP
34271 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
34272 #undef TARGET_ASM_UNALIGNED_SI_OP
34273 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
34274 #undef TARGET_ASM_UNALIGNED_DI_OP
34275 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
34276
34277 #undef TARGET_PRINT_OPERAND
34278 #define TARGET_PRINT_OPERAND ix86_print_operand
34279 #undef TARGET_PRINT_OPERAND_ADDRESS
34280 #define TARGET_PRINT_OPERAND_ADDRESS ix86_print_operand_address
34281 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
34282 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ix86_print_operand_punct_valid_p
34283 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
34284 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA i386_asm_output_addr_const_extra 
34285
34286 #undef TARGET_SCHED_INIT_GLOBAL
34287 #define TARGET_SCHED_INIT_GLOBAL ix86_sched_init_global
34288 #undef TARGET_SCHED_ADJUST_COST
34289 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
34290 #undef TARGET_SCHED_ISSUE_RATE
34291 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
34292 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
34293 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
34294   ia32_multipass_dfa_lookahead
34295
34296 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
34297 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
34298
34299 #ifdef HAVE_AS_TLS
34300 #undef TARGET_HAVE_TLS
34301 #define TARGET_HAVE_TLS true
34302 #endif
34303 #undef TARGET_CANNOT_FORCE_CONST_MEM
34304 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
34305 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
34306 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
34307
34308 #undef TARGET_DELEGITIMIZE_ADDRESS
34309 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
34310
34311 #undef TARGET_MS_BITFIELD_LAYOUT_P
34312 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
34313
34314 #if TARGET_MACHO
34315 #undef TARGET_BINDS_LOCAL_P
34316 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
34317 #endif
34318 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
34319 #undef TARGET_BINDS_LOCAL_P
34320 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
34321 #endif
34322
34323 #undef TARGET_ASM_OUTPUT_MI_THUNK
34324 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
34325 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
34326 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
34327
34328 #undef TARGET_ASM_FILE_START
34329 #define TARGET_ASM_FILE_START x86_file_start
34330
34331 #undef TARGET_DEFAULT_TARGET_FLAGS
34332 #define TARGET_DEFAULT_TARGET_FLAGS     \
34333   (TARGET_DEFAULT                       \
34334    | TARGET_SUBTARGET_DEFAULT           \
34335    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT \
34336    | MASK_FUSED_MADD)
34337
34338 #undef TARGET_HANDLE_OPTION
34339 #define TARGET_HANDLE_OPTION ix86_handle_option
34340
34341 #undef TARGET_OPTION_OVERRIDE
34342 #define TARGET_OPTION_OVERRIDE ix86_option_override
34343 #undef TARGET_OPTION_OPTIMIZATION_TABLE
34344 #define TARGET_OPTION_OPTIMIZATION_TABLE ix86_option_optimization_table
34345 #undef TARGET_OPTION_INIT_STRUCT
34346 #define TARGET_OPTION_INIT_STRUCT ix86_option_init_struct
34347
34348 #undef TARGET_REGISTER_MOVE_COST
34349 #define TARGET_REGISTER_MOVE_COST ix86_register_move_cost
34350 #undef TARGET_MEMORY_MOVE_COST
34351 #define TARGET_MEMORY_MOVE_COST ix86_memory_move_cost
34352 #undef TARGET_RTX_COSTS
34353 #define TARGET_RTX_COSTS ix86_rtx_costs
34354 #undef TARGET_ADDRESS_COST
34355 #define TARGET_ADDRESS_COST ix86_address_cost
34356
34357 #undef TARGET_FIXED_CONDITION_CODE_REGS
34358 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
34359 #undef TARGET_CC_MODES_COMPATIBLE
34360 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
34361
34362 #undef TARGET_MACHINE_DEPENDENT_REORG
34363 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
34364
34365 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
34366 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
34367
34368 #undef TARGET_BUILD_BUILTIN_VA_LIST
34369 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
34370
34371 #undef TARGET_ENUM_VA_LIST_P
34372 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
34373
34374 #undef TARGET_FN_ABI_VA_LIST
34375 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
34376
34377 #undef TARGET_CANONICAL_VA_LIST_TYPE
34378 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
34379
34380 #undef TARGET_EXPAND_BUILTIN_VA_START
34381 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
34382
34383 #undef TARGET_MD_ASM_CLOBBERS
34384 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
34385
34386 #undef TARGET_PROMOTE_PROTOTYPES
34387 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
34388 #undef TARGET_STRUCT_VALUE_RTX
34389 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
34390 #undef TARGET_SETUP_INCOMING_VARARGS
34391 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
34392 #undef TARGET_MUST_PASS_IN_STACK
34393 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
34394 #undef TARGET_FUNCTION_ARG_ADVANCE
34395 #define TARGET_FUNCTION_ARG_ADVANCE ix86_function_arg_advance
34396 #undef TARGET_FUNCTION_ARG
34397 #define TARGET_FUNCTION_ARG ix86_function_arg
34398 #undef TARGET_PASS_BY_REFERENCE
34399 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
34400 #undef TARGET_INTERNAL_ARG_POINTER
34401 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
34402 #undef TARGET_UPDATE_STACK_BOUNDARY
34403 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
34404 #undef TARGET_GET_DRAP_RTX
34405 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
34406 #undef TARGET_STRICT_ARGUMENT_NAMING
34407 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
34408 #undef TARGET_STATIC_CHAIN
34409 #define TARGET_STATIC_CHAIN ix86_static_chain
34410 #undef TARGET_TRAMPOLINE_INIT
34411 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
34412 #undef TARGET_RETURN_POPS_ARGS
34413 #define TARGET_RETURN_POPS_ARGS ix86_return_pops_args
34414
34415 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
34416 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
34417
34418 #undef TARGET_SCALAR_MODE_SUPPORTED_P
34419 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
34420
34421 #undef TARGET_VECTOR_MODE_SUPPORTED_P
34422 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
34423
34424 #undef TARGET_C_MODE_FOR_SUFFIX
34425 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
34426
34427 #ifdef HAVE_AS_TLS
34428 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
34429 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
34430 #endif
34431
34432 #ifdef SUBTARGET_INSERT_ATTRIBUTES
34433 #undef TARGET_INSERT_ATTRIBUTES
34434 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
34435 #endif
34436
34437 #undef TARGET_MANGLE_TYPE
34438 #define TARGET_MANGLE_TYPE ix86_mangle_type
34439
34440 #undef TARGET_STACK_PROTECT_FAIL
34441 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
34442
34443 #undef TARGET_SUPPORTS_SPLIT_STACK
34444 #define TARGET_SUPPORTS_SPLIT_STACK ix86_supports_split_stack
34445
34446 #undef TARGET_FUNCTION_VALUE
34447 #define TARGET_FUNCTION_VALUE ix86_function_value
34448
34449 #undef TARGET_FUNCTION_VALUE_REGNO_P
34450 #define TARGET_FUNCTION_VALUE_REGNO_P ix86_function_value_regno_p
34451
34452 #undef TARGET_SECONDARY_RELOAD
34453 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
34454
34455 #undef TARGET_PREFERRED_RELOAD_CLASS
34456 #define TARGET_PREFERRED_RELOAD_CLASS ix86_preferred_reload_class
34457 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
34458 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS ix86_preferred_output_reload_class
34459 #undef TARGET_CLASS_LIKELY_SPILLED_P
34460 #define TARGET_CLASS_LIKELY_SPILLED_P ix86_class_likely_spilled_p
34461
34462 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
34463 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
34464   ix86_builtin_vectorization_cost
34465 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM
34466 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM \
34467   ix86_vectorize_builtin_vec_perm
34468 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK
34469 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK \
34470   ix86_vectorize_builtin_vec_perm_ok
34471 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
34472 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
34473   ix86_preferred_simd_mode
34474 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
34475 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
34476   ix86_autovectorize_vector_sizes
34477
34478 #undef TARGET_SET_CURRENT_FUNCTION
34479 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
34480
34481 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
34482 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
34483
34484 #undef TARGET_OPTION_SAVE
34485 #define TARGET_OPTION_SAVE ix86_function_specific_save
34486
34487 #undef TARGET_OPTION_RESTORE
34488 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
34489
34490 #undef TARGET_OPTION_PRINT
34491 #define TARGET_OPTION_PRINT ix86_function_specific_print
34492
34493 #undef TARGET_CAN_INLINE_P
34494 #define TARGET_CAN_INLINE_P ix86_can_inline_p
34495
34496 #undef TARGET_EXPAND_TO_RTL_HOOK
34497 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
34498
34499 #undef TARGET_LEGITIMATE_ADDRESS_P
34500 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
34501
34502 #undef TARGET_IRA_COVER_CLASSES
34503 #define TARGET_IRA_COVER_CLASSES i386_ira_cover_classes
34504
34505 #undef TARGET_FRAME_POINTER_REQUIRED
34506 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
34507
34508 #undef TARGET_CAN_ELIMINATE
34509 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
34510
34511 #undef TARGET_EXTRA_LIVE_ON_ENTRY
34512 #define TARGET_EXTRA_LIVE_ON_ENTRY ix86_live_on_entry
34513
34514 #undef TARGET_ASM_CODE_END
34515 #define TARGET_ASM_CODE_END ix86_code_end
34516
34517 struct gcc_target targetm = TARGET_INITIALIZER;
34518 \f
34519 #include "gt-i386.h"