OSDN Git Service

PR target/43667
[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 "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-codes.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "optabs.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
59 static rtx legitimize_dllimport_symbol (rtx, bool);
60
61 #ifndef CHECK_STACK_LIMIT
62 #define CHECK_STACK_LIMIT (-1)
63 #endif
64
65 /* Return index of given mode in mult and division cost tables.  */
66 #define MODE_INDEX(mode)                                        \
67   ((mode) == QImode ? 0                                         \
68    : (mode) == HImode ? 1                                       \
69    : (mode) == SImode ? 2                                       \
70    : (mode) == DImode ? 3                                       \
71    : 4)
72
73 /* Processor costs (relative to an add) */
74 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
75 #define COSTS_N_BYTES(N) ((N) * 2)
76
77 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
78
79 const
80 struct processor_costs ix86_size_cost = {/* costs for tuning for size */
81   COSTS_N_BYTES (2),                    /* cost of an add instruction */
82   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
83   COSTS_N_BYTES (2),                    /* variable shift costs */
84   COSTS_N_BYTES (3),                    /* constant shift costs */
85   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
86    COSTS_N_BYTES (3),                   /*                               HI */
87    COSTS_N_BYTES (3),                   /*                               SI */
88    COSTS_N_BYTES (3),                   /*                               DI */
89    COSTS_N_BYTES (5)},                  /*                            other */
90   0,                                    /* cost of multiply per each bit set */
91   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
92    COSTS_N_BYTES (3),                   /*                          HI */
93    COSTS_N_BYTES (3),                   /*                          SI */
94    COSTS_N_BYTES (3),                   /*                          DI */
95    COSTS_N_BYTES (5)},                  /*                       other */
96   COSTS_N_BYTES (3),                    /* cost of movsx */
97   COSTS_N_BYTES (3),                    /* cost of movzx */
98   0,                                    /* "large" insn */
99   2,                                    /* MOVE_RATIO */
100   2,                                    /* cost for loading QImode using movzbl */
101   {2, 2, 2},                            /* cost of loading integer registers
102                                            in QImode, HImode and SImode.
103                                            Relative to reg-reg move (2).  */
104   {2, 2, 2},                            /* cost of storing integer registers */
105   2,                                    /* cost of reg,reg fld/fst */
106   {2, 2, 2},                            /* cost of loading fp registers
107                                            in SFmode, DFmode and XFmode */
108   {2, 2, 2},                            /* cost of storing fp registers
109                                            in SFmode, DFmode and XFmode */
110   3,                                    /* cost of moving MMX register */
111   {3, 3},                               /* cost of loading MMX registers
112                                            in SImode and DImode */
113   {3, 3},                               /* cost of storing MMX registers
114                                            in SImode and DImode */
115   3,                                    /* cost of moving SSE register */
116   {3, 3, 3},                            /* cost of loading SSE registers
117                                            in SImode, DImode and TImode */
118   {3, 3, 3},                            /* cost of storing SSE registers
119                                            in SImode, DImode and TImode */
120   3,                                    /* MMX or SSE register to integer */
121   0,                                    /* size of l1 cache  */
122   0,                                    /* size of l2 cache  */
123   0,                                    /* size of prefetch block */
124   0,                                    /* number of parallel prefetches */
125   2,                                    /* Branch cost */
126   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
127   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
128   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
129   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
130   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
131   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
132   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
133    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
134   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
135    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
136   1,                                    /* scalar_stmt_cost.  */
137   1,                                    /* scalar load_cost.  */
138   1,                                    /* scalar_store_cost.  */
139   1,                                    /* vec_stmt_cost.  */
140   1,                                    /* vec_to_scalar_cost.  */
141   1,                                    /* scalar_to_vec_cost.  */
142   1,                                    /* vec_align_load_cost.  */
143   1,                                    /* vec_unalign_load_cost.  */
144   1,                                    /* vec_store_cost.  */
145   1,                                    /* cond_taken_branch_cost.  */
146   1,                                    /* cond_not_taken_branch_cost.  */
147 };
148
149 /* Processor costs (relative to an add) */
150 static const
151 struct processor_costs i386_cost = {    /* 386 specific costs */
152   COSTS_N_INSNS (1),                    /* cost of an add instruction */
153   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
154   COSTS_N_INSNS (3),                    /* variable shift costs */
155   COSTS_N_INSNS (2),                    /* constant shift costs */
156   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
157    COSTS_N_INSNS (6),                   /*                               HI */
158    COSTS_N_INSNS (6),                   /*                               SI */
159    COSTS_N_INSNS (6),                   /*                               DI */
160    COSTS_N_INSNS (6)},                  /*                               other */
161   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
162   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
163    COSTS_N_INSNS (23),                  /*                          HI */
164    COSTS_N_INSNS (23),                  /*                          SI */
165    COSTS_N_INSNS (23),                  /*                          DI */
166    COSTS_N_INSNS (23)},                 /*                          other */
167   COSTS_N_INSNS (3),                    /* cost of movsx */
168   COSTS_N_INSNS (2),                    /* cost of movzx */
169   15,                                   /* "large" insn */
170   3,                                    /* MOVE_RATIO */
171   4,                                    /* cost for loading QImode using movzbl */
172   {2, 4, 2},                            /* cost of loading integer registers
173                                            in QImode, HImode and SImode.
174                                            Relative to reg-reg move (2).  */
175   {2, 4, 2},                            /* cost of storing integer registers */
176   2,                                    /* cost of reg,reg fld/fst */
177   {8, 8, 8},                            /* cost of loading fp registers
178                                            in SFmode, DFmode and XFmode */
179   {8, 8, 8},                            /* cost of storing fp registers
180                                            in SFmode, DFmode and XFmode */
181   2,                                    /* cost of moving MMX register */
182   {4, 8},                               /* cost of loading MMX registers
183                                            in SImode and DImode */
184   {4, 8},                               /* cost of storing MMX registers
185                                            in SImode and DImode */
186   2,                                    /* cost of moving SSE register */
187   {4, 8, 16},                           /* cost of loading SSE registers
188                                            in SImode, DImode and TImode */
189   {4, 8, 16},                           /* cost of storing SSE registers
190                                            in SImode, DImode and TImode */
191   3,                                    /* MMX or SSE register to integer */
192   0,                                    /* size of l1 cache  */
193   0,                                    /* size of l2 cache  */
194   0,                                    /* size of prefetch block */
195   0,                                    /* number of parallel prefetches */
196   1,                                    /* Branch cost */
197   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
198   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
199   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
200   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
201   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
202   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
203   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
204    DUMMY_STRINGOP_ALGS},
205   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
206    DUMMY_STRINGOP_ALGS},
207   1,                                    /* scalar_stmt_cost.  */
208   1,                                    /* scalar load_cost.  */
209   1,                                    /* scalar_store_cost.  */
210   1,                                    /* vec_stmt_cost.  */
211   1,                                    /* vec_to_scalar_cost.  */
212   1,                                    /* scalar_to_vec_cost.  */
213   1,                                    /* vec_align_load_cost.  */
214   2,                                    /* vec_unalign_load_cost.  */
215   1,                                    /* vec_store_cost.  */
216   3,                                    /* cond_taken_branch_cost.  */
217   1,                                    /* cond_not_taken_branch_cost.  */
218 };
219
220 static const
221 struct processor_costs i486_cost = {    /* 486 specific costs */
222   COSTS_N_INSNS (1),                    /* cost of an add instruction */
223   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
224   COSTS_N_INSNS (3),                    /* variable shift costs */
225   COSTS_N_INSNS (2),                    /* constant shift costs */
226   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
227    COSTS_N_INSNS (12),                  /*                               HI */
228    COSTS_N_INSNS (12),                  /*                               SI */
229    COSTS_N_INSNS (12),                  /*                               DI */
230    COSTS_N_INSNS (12)},                 /*                               other */
231   1,                                    /* cost of multiply per each bit set */
232   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
233    COSTS_N_INSNS (40),                  /*                          HI */
234    COSTS_N_INSNS (40),                  /*                          SI */
235    COSTS_N_INSNS (40),                  /*                          DI */
236    COSTS_N_INSNS (40)},                 /*                          other */
237   COSTS_N_INSNS (3),                    /* cost of movsx */
238   COSTS_N_INSNS (2),                    /* cost of movzx */
239   15,                                   /* "large" insn */
240   3,                                    /* MOVE_RATIO */
241   4,                                    /* cost for loading QImode using movzbl */
242   {2, 4, 2},                            /* cost of loading integer registers
243                                            in QImode, HImode and SImode.
244                                            Relative to reg-reg move (2).  */
245   {2, 4, 2},                            /* cost of storing integer registers */
246   2,                                    /* cost of reg,reg fld/fst */
247   {8, 8, 8},                            /* cost of loading fp registers
248                                            in SFmode, DFmode and XFmode */
249   {8, 8, 8},                            /* cost of storing fp registers
250                                            in SFmode, DFmode and XFmode */
251   2,                                    /* cost of moving MMX register */
252   {4, 8},                               /* cost of loading MMX registers
253                                            in SImode and DImode */
254   {4, 8},                               /* cost of storing MMX registers
255                                            in SImode and DImode */
256   2,                                    /* cost of moving SSE register */
257   {4, 8, 16},                           /* cost of loading SSE registers
258                                            in SImode, DImode and TImode */
259   {4, 8, 16},                           /* cost of storing SSE registers
260                                            in SImode, DImode and TImode */
261   3,                                    /* MMX or SSE register to integer */
262   4,                                    /* size of l1 cache.  486 has 8kB cache
263                                            shared for code and data, so 4kB is
264                                            not really precise.  */
265   4,                                    /* size of l2 cache  */
266   0,                                    /* size of prefetch block */
267   0,                                    /* number of parallel prefetches */
268   1,                                    /* Branch cost */
269   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
270   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
271   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
272   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
273   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
274   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
275   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
276    DUMMY_STRINGOP_ALGS},
277   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
278    DUMMY_STRINGOP_ALGS},
279   1,                                    /* scalar_stmt_cost.  */
280   1,                                    /* scalar load_cost.  */
281   1,                                    /* scalar_store_cost.  */
282   1,                                    /* vec_stmt_cost.  */
283   1,                                    /* vec_to_scalar_cost.  */
284   1,                                    /* scalar_to_vec_cost.  */
285   1,                                    /* vec_align_load_cost.  */
286   2,                                    /* vec_unalign_load_cost.  */
287   1,                                    /* vec_store_cost.  */
288   3,                                    /* cond_taken_branch_cost.  */
289   1,                                    /* cond_not_taken_branch_cost.  */
290 };
291
292 static const
293 struct processor_costs pentium_cost = {
294   COSTS_N_INSNS (1),                    /* cost of an add instruction */
295   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
296   COSTS_N_INSNS (4),                    /* variable shift costs */
297   COSTS_N_INSNS (1),                    /* constant shift costs */
298   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
299    COSTS_N_INSNS (11),                  /*                               HI */
300    COSTS_N_INSNS (11),                  /*                               SI */
301    COSTS_N_INSNS (11),                  /*                               DI */
302    COSTS_N_INSNS (11)},                 /*                               other */
303   0,                                    /* cost of multiply per each bit set */
304   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
305    COSTS_N_INSNS (25),                  /*                          HI */
306    COSTS_N_INSNS (25),                  /*                          SI */
307    COSTS_N_INSNS (25),                  /*                          DI */
308    COSTS_N_INSNS (25)},                 /*                          other */
309   COSTS_N_INSNS (3),                    /* cost of movsx */
310   COSTS_N_INSNS (2),                    /* cost of movzx */
311   8,                                    /* "large" insn */
312   6,                                    /* MOVE_RATIO */
313   6,                                    /* cost for loading QImode using movzbl */
314   {2, 4, 2},                            /* cost of loading integer registers
315                                            in QImode, HImode and SImode.
316                                            Relative to reg-reg move (2).  */
317   {2, 4, 2},                            /* cost of storing integer registers */
318   2,                                    /* cost of reg,reg fld/fst */
319   {2, 2, 6},                            /* cost of loading fp registers
320                                            in SFmode, DFmode and XFmode */
321   {4, 4, 6},                            /* cost of storing fp registers
322                                            in SFmode, DFmode and XFmode */
323   8,                                    /* cost of moving MMX register */
324   {8, 8},                               /* cost of loading MMX registers
325                                            in SImode and DImode */
326   {8, 8},                               /* cost of storing MMX registers
327                                            in SImode and DImode */
328   2,                                    /* cost of moving SSE register */
329   {4, 8, 16},                           /* cost of loading SSE registers
330                                            in SImode, DImode and TImode */
331   {4, 8, 16},                           /* cost of storing SSE registers
332                                            in SImode, DImode and TImode */
333   3,                                    /* MMX or SSE register to integer */
334   8,                                    /* size of l1 cache.  */
335   8,                                    /* size of l2 cache  */
336   0,                                    /* size of prefetch block */
337   0,                                    /* number of parallel prefetches */
338   2,                                    /* Branch cost */
339   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
340   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
341   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
342   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
343   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
344   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
345   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
346    DUMMY_STRINGOP_ALGS},
347   {{libcall, {{-1, rep_prefix_4_byte}}},
348    DUMMY_STRINGOP_ALGS},
349   1,                                    /* scalar_stmt_cost.  */
350   1,                                    /* scalar load_cost.  */
351   1,                                    /* scalar_store_cost.  */
352   1,                                    /* vec_stmt_cost.  */
353   1,                                    /* vec_to_scalar_cost.  */
354   1,                                    /* scalar_to_vec_cost.  */
355   1,                                    /* vec_align_load_cost.  */
356   2,                                    /* vec_unalign_load_cost.  */
357   1,                                    /* vec_store_cost.  */
358   3,                                    /* cond_taken_branch_cost.  */
359   1,                                    /* cond_not_taken_branch_cost.  */
360 };
361
362 static const
363 struct processor_costs pentiumpro_cost = {
364   COSTS_N_INSNS (1),                    /* cost of an add instruction */
365   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
366   COSTS_N_INSNS (1),                    /* variable shift costs */
367   COSTS_N_INSNS (1),                    /* constant shift costs */
368   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
369    COSTS_N_INSNS (4),                   /*                               HI */
370    COSTS_N_INSNS (4),                   /*                               SI */
371    COSTS_N_INSNS (4),                   /*                               DI */
372    COSTS_N_INSNS (4)},                  /*                               other */
373   0,                                    /* cost of multiply per each bit set */
374   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
375    COSTS_N_INSNS (17),                  /*                          HI */
376    COSTS_N_INSNS (17),                  /*                          SI */
377    COSTS_N_INSNS (17),                  /*                          DI */
378    COSTS_N_INSNS (17)},                 /*                          other */
379   COSTS_N_INSNS (1),                    /* cost of movsx */
380   COSTS_N_INSNS (1),                    /* cost of movzx */
381   8,                                    /* "large" insn */
382   6,                                    /* MOVE_RATIO */
383   2,                                    /* cost for loading QImode using movzbl */
384   {4, 4, 4},                            /* cost of loading integer registers
385                                            in QImode, HImode and SImode.
386                                            Relative to reg-reg move (2).  */
387   {2, 2, 2},                            /* cost of storing integer registers */
388   2,                                    /* cost of reg,reg fld/fst */
389   {2, 2, 6},                            /* cost of loading fp registers
390                                            in SFmode, DFmode and XFmode */
391   {4, 4, 6},                            /* cost of storing fp registers
392                                            in SFmode, DFmode and XFmode */
393   2,                                    /* cost of moving MMX register */
394   {2, 2},                               /* cost of loading MMX registers
395                                            in SImode and DImode */
396   {2, 2},                               /* cost of storing MMX registers
397                                            in SImode and DImode */
398   2,                                    /* cost of moving SSE register */
399   {2, 2, 8},                            /* cost of loading SSE registers
400                                            in SImode, DImode and TImode */
401   {2, 2, 8},                            /* cost of storing SSE registers
402                                            in SImode, DImode and TImode */
403   3,                                    /* MMX or SSE register to integer */
404   8,                                    /* size of l1 cache.  */
405   256,                                  /* size of l2 cache  */
406   32,                                   /* size of prefetch block */
407   6,                                    /* number of parallel prefetches */
408   2,                                    /* Branch cost */
409   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
410   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
411   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
412   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
413   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
414   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
415   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes (we ensure
416      the alignment).  For small blocks inline loop is still a noticeable win, for bigger
417      blocks either rep movsl or rep movsb is way to go.  Rep movsb has apparently
418      more expensive startup time in CPU, but after 4K the difference is down in the noise.
419    */
420   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
421                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
422    DUMMY_STRINGOP_ALGS},
423   {{rep_prefix_4_byte, {{1024, unrolled_loop},
424                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
425    DUMMY_STRINGOP_ALGS},
426   1,                                    /* scalar_stmt_cost.  */
427   1,                                    /* scalar load_cost.  */
428   1,                                    /* scalar_store_cost.  */
429   1,                                    /* vec_stmt_cost.  */
430   1,                                    /* vec_to_scalar_cost.  */
431   1,                                    /* scalar_to_vec_cost.  */
432   1,                                    /* vec_align_load_cost.  */
433   2,                                    /* vec_unalign_load_cost.  */
434   1,                                    /* vec_store_cost.  */
435   3,                                    /* cond_taken_branch_cost.  */
436   1,                                    /* cond_not_taken_branch_cost.  */
437 };
438
439 static const
440 struct processor_costs geode_cost = {
441   COSTS_N_INSNS (1),                    /* cost of an add instruction */
442   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
443   COSTS_N_INSNS (2),                    /* variable shift costs */
444   COSTS_N_INSNS (1),                    /* constant shift costs */
445   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
446    COSTS_N_INSNS (4),                   /*                               HI */
447    COSTS_N_INSNS (7),                   /*                               SI */
448    COSTS_N_INSNS (7),                   /*                               DI */
449    COSTS_N_INSNS (7)},                  /*                               other */
450   0,                                    /* cost of multiply per each bit set */
451   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
452    COSTS_N_INSNS (23),                  /*                          HI */
453    COSTS_N_INSNS (39),                  /*                          SI */
454    COSTS_N_INSNS (39),                  /*                          DI */
455    COSTS_N_INSNS (39)},                 /*                          other */
456   COSTS_N_INSNS (1),                    /* cost of movsx */
457   COSTS_N_INSNS (1),                    /* cost of movzx */
458   8,                                    /* "large" insn */
459   4,                                    /* MOVE_RATIO */
460   1,                                    /* cost for loading QImode using movzbl */
461   {1, 1, 1},                            /* cost of loading integer registers
462                                            in QImode, HImode and SImode.
463                                            Relative to reg-reg move (2).  */
464   {1, 1, 1},                            /* cost of storing integer registers */
465   1,                                    /* cost of reg,reg fld/fst */
466   {1, 1, 1},                            /* cost of loading fp registers
467                                            in SFmode, DFmode and XFmode */
468   {4, 6, 6},                            /* cost of storing fp registers
469                                            in SFmode, DFmode and XFmode */
470
471   1,                                    /* cost of moving MMX register */
472   {1, 1},                               /* cost of loading MMX registers
473                                            in SImode and DImode */
474   {1, 1},                               /* cost of storing MMX registers
475                                            in SImode and DImode */
476   1,                                    /* cost of moving SSE register */
477   {1, 1, 1},                            /* cost of loading SSE registers
478                                            in SImode, DImode and TImode */
479   {1, 1, 1},                            /* cost of storing SSE registers
480                                            in SImode, DImode and TImode */
481   1,                                    /* MMX or SSE register to integer */
482   64,                                   /* size of l1 cache.  */
483   128,                                  /* size of l2 cache.  */
484   32,                                   /* size of prefetch block */
485   1,                                    /* number of parallel prefetches */
486   1,                                    /* Branch cost */
487   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
488   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
489   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
490   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
491   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
492   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
493   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
494    DUMMY_STRINGOP_ALGS},
495   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
496    DUMMY_STRINGOP_ALGS},
497   1,                                    /* scalar_stmt_cost.  */
498   1,                                    /* scalar load_cost.  */
499   1,                                    /* scalar_store_cost.  */
500   1,                                    /* vec_stmt_cost.  */
501   1,                                    /* vec_to_scalar_cost.  */
502   1,                                    /* scalar_to_vec_cost.  */
503   1,                                    /* vec_align_load_cost.  */
504   2,                                    /* vec_unalign_load_cost.  */
505   1,                                    /* vec_store_cost.  */
506   3,                                    /* cond_taken_branch_cost.  */
507   1,                                    /* cond_not_taken_branch_cost.  */
508 };
509
510 static const
511 struct processor_costs k6_cost = {
512   COSTS_N_INSNS (1),                    /* cost of an add instruction */
513   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
514   COSTS_N_INSNS (1),                    /* variable shift costs */
515   COSTS_N_INSNS (1),                    /* constant shift costs */
516   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
517    COSTS_N_INSNS (3),                   /*                               HI */
518    COSTS_N_INSNS (3),                   /*                               SI */
519    COSTS_N_INSNS (3),                   /*                               DI */
520    COSTS_N_INSNS (3)},                  /*                               other */
521   0,                                    /* cost of multiply per each bit set */
522   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
523    COSTS_N_INSNS (18),                  /*                          HI */
524    COSTS_N_INSNS (18),                  /*                          SI */
525    COSTS_N_INSNS (18),                  /*                          DI */
526    COSTS_N_INSNS (18)},                 /*                          other */
527   COSTS_N_INSNS (2),                    /* cost of movsx */
528   COSTS_N_INSNS (2),                    /* cost of movzx */
529   8,                                    /* "large" insn */
530   4,                                    /* MOVE_RATIO */
531   3,                                    /* cost for loading QImode using movzbl */
532   {4, 5, 4},                            /* cost of loading integer registers
533                                            in QImode, HImode and SImode.
534                                            Relative to reg-reg move (2).  */
535   {2, 3, 2},                            /* cost of storing integer registers */
536   4,                                    /* cost of reg,reg fld/fst */
537   {6, 6, 6},                            /* cost of loading fp registers
538                                            in SFmode, DFmode and XFmode */
539   {4, 4, 4},                            /* cost of storing fp registers
540                                            in SFmode, DFmode and XFmode */
541   2,                                    /* cost of moving MMX register */
542   {2, 2},                               /* cost of loading MMX registers
543                                            in SImode and DImode */
544   {2, 2},                               /* cost of storing MMX registers
545                                            in SImode and DImode */
546   2,                                    /* cost of moving SSE register */
547   {2, 2, 8},                            /* cost of loading SSE registers
548                                            in SImode, DImode and TImode */
549   {2, 2, 8},                            /* cost of storing SSE registers
550                                            in SImode, DImode and TImode */
551   6,                                    /* MMX or SSE register to integer */
552   32,                                   /* size of l1 cache.  */
553   32,                                   /* size of l2 cache.  Some models
554                                            have integrated l2 cache, but
555                                            optimizing for k6 is not important
556                                            enough to worry about that.  */
557   32,                                   /* size of prefetch block */
558   1,                                    /* number of parallel prefetches */
559   1,                                    /* Branch cost */
560   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
561   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
562   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
563   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
564   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
565   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
566   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
567    DUMMY_STRINGOP_ALGS},
568   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
569    DUMMY_STRINGOP_ALGS},
570   1,                                    /* scalar_stmt_cost.  */
571   1,                                    /* scalar load_cost.  */
572   1,                                    /* scalar_store_cost.  */
573   1,                                    /* vec_stmt_cost.  */
574   1,                                    /* vec_to_scalar_cost.  */
575   1,                                    /* scalar_to_vec_cost.  */
576   1,                                    /* vec_align_load_cost.  */
577   2,                                    /* vec_unalign_load_cost.  */
578   1,                                    /* vec_store_cost.  */
579   3,                                    /* cond_taken_branch_cost.  */
580   1,                                    /* cond_not_taken_branch_cost.  */
581 };
582
583 static const
584 struct processor_costs athlon_cost = {
585   COSTS_N_INSNS (1),                    /* cost of an add instruction */
586   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
587   COSTS_N_INSNS (1),                    /* variable shift costs */
588   COSTS_N_INSNS (1),                    /* constant shift costs */
589   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
590    COSTS_N_INSNS (5),                   /*                               HI */
591    COSTS_N_INSNS (5),                   /*                               SI */
592    COSTS_N_INSNS (5),                   /*                               DI */
593    COSTS_N_INSNS (5)},                  /*                               other */
594   0,                                    /* cost of multiply per each bit set */
595   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
596    COSTS_N_INSNS (26),                  /*                          HI */
597    COSTS_N_INSNS (42),                  /*                          SI */
598    COSTS_N_INSNS (74),                  /*                          DI */
599    COSTS_N_INSNS (74)},                 /*                          other */
600   COSTS_N_INSNS (1),                    /* cost of movsx */
601   COSTS_N_INSNS (1),                    /* cost of movzx */
602   8,                                    /* "large" insn */
603   9,                                    /* MOVE_RATIO */
604   4,                                    /* cost for loading QImode using movzbl */
605   {3, 4, 3},                            /* cost of loading integer registers
606                                            in QImode, HImode and SImode.
607                                            Relative to reg-reg move (2).  */
608   {3, 4, 3},                            /* cost of storing integer registers */
609   4,                                    /* cost of reg,reg fld/fst */
610   {4, 4, 12},                           /* cost of loading fp registers
611                                            in SFmode, DFmode and XFmode */
612   {6, 6, 8},                            /* cost of storing fp registers
613                                            in SFmode, DFmode and XFmode */
614   2,                                    /* cost of moving MMX register */
615   {4, 4},                               /* cost of loading MMX registers
616                                            in SImode and DImode */
617   {4, 4},                               /* cost of storing MMX registers
618                                            in SImode and DImode */
619   2,                                    /* cost of moving SSE register */
620   {4, 4, 6},                            /* cost of loading SSE registers
621                                            in SImode, DImode and TImode */
622   {4, 4, 5},                            /* cost of storing SSE registers
623                                            in SImode, DImode and TImode */
624   5,                                    /* MMX or SSE register to integer */
625   64,                                   /* size of l1 cache.  */
626   256,                                  /* size of l2 cache.  */
627   64,                                   /* size of prefetch block */
628   6,                                    /* number of parallel prefetches */
629   5,                                    /* Branch cost */
630   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
631   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
632   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
633   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
634   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
635   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
636   /* For some reason, Athlon deals better with REP prefix (relative to loops)
637      compared to K8. Alignment becomes important after 8 bytes for memcpy and
638      128 bytes for memset.  */
639   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
640    DUMMY_STRINGOP_ALGS},
641   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
642    DUMMY_STRINGOP_ALGS},
643   1,                                    /* scalar_stmt_cost.  */
644   1,                                    /* scalar load_cost.  */
645   1,                                    /* scalar_store_cost.  */
646   1,                                    /* vec_stmt_cost.  */
647   1,                                    /* vec_to_scalar_cost.  */
648   1,                                    /* scalar_to_vec_cost.  */
649   1,                                    /* vec_align_load_cost.  */
650   2,                                    /* vec_unalign_load_cost.  */
651   1,                                    /* vec_store_cost.  */
652   3,                                    /* cond_taken_branch_cost.  */
653   1,                                    /* cond_not_taken_branch_cost.  */
654 };
655
656 static const
657 struct processor_costs k8_cost = {
658   COSTS_N_INSNS (1),                    /* cost of an add instruction */
659   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
660   COSTS_N_INSNS (1),                    /* variable shift costs */
661   COSTS_N_INSNS (1),                    /* constant shift costs */
662   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
663    COSTS_N_INSNS (4),                   /*                               HI */
664    COSTS_N_INSNS (3),                   /*                               SI */
665    COSTS_N_INSNS (4),                   /*                               DI */
666    COSTS_N_INSNS (5)},                  /*                               other */
667   0,                                    /* cost of multiply per each bit set */
668   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
669    COSTS_N_INSNS (26),                  /*                          HI */
670    COSTS_N_INSNS (42),                  /*                          SI */
671    COSTS_N_INSNS (74),                  /*                          DI */
672    COSTS_N_INSNS (74)},                 /*                          other */
673   COSTS_N_INSNS (1),                    /* cost of movsx */
674   COSTS_N_INSNS (1),                    /* cost of movzx */
675   8,                                    /* "large" insn */
676   9,                                    /* MOVE_RATIO */
677   4,                                    /* cost for loading QImode using movzbl */
678   {3, 4, 3},                            /* cost of loading integer registers
679                                            in QImode, HImode and SImode.
680                                            Relative to reg-reg move (2).  */
681   {3, 4, 3},                            /* cost of storing integer registers */
682   4,                                    /* cost of reg,reg fld/fst */
683   {4, 4, 12},                           /* cost of loading fp registers
684                                            in SFmode, DFmode and XFmode */
685   {6, 6, 8},                            /* cost of storing fp registers
686                                            in SFmode, DFmode and XFmode */
687   2,                                    /* cost of moving MMX register */
688   {3, 3},                               /* cost of loading MMX registers
689                                            in SImode and DImode */
690   {4, 4},                               /* cost of storing MMX registers
691                                            in SImode and DImode */
692   2,                                    /* cost of moving SSE register */
693   {4, 3, 6},                            /* cost of loading SSE registers
694                                            in SImode, DImode and TImode */
695   {4, 4, 5},                            /* cost of storing SSE registers
696                                            in SImode, DImode and TImode */
697   5,                                    /* MMX or SSE register to integer */
698   64,                                   /* size of l1 cache.  */
699   512,                                  /* size of l2 cache.  */
700   64,                                   /* size of prefetch block */
701   /* New AMD processors never drop prefetches; if they cannot be performed
702      immediately, they are queued.  We set number of simultaneous prefetches
703      to a large constant to reflect this (it probably is not a good idea not
704      to limit number of prefetches at all, as their execution also takes some
705      time).  */
706   100,                                  /* number of parallel prefetches */
707   3,                                    /* Branch cost */
708   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
709   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
710   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
711   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
712   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
713   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
714   /* K8 has optimized REP instruction for medium sized blocks, but for very small
715      blocks it is better to use loop. For large blocks, libcall can do
716      nontemporary accesses and beat inline considerably.  */
717   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
718    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
719   {{libcall, {{8, loop}, {24, unrolled_loop},
720               {2048, rep_prefix_4_byte}, {-1, libcall}}},
721    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
722   4,                                    /* scalar_stmt_cost.  */
723   2,                                    /* scalar load_cost.  */
724   2,                                    /* scalar_store_cost.  */
725   5,                                    /* vec_stmt_cost.  */
726   0,                                    /* vec_to_scalar_cost.  */
727   2,                                    /* scalar_to_vec_cost.  */
728   2,                                    /* vec_align_load_cost.  */
729   3,                                    /* vec_unalign_load_cost.  */
730   3,                                    /* vec_store_cost.  */
731   3,                                    /* cond_taken_branch_cost.  */
732   2,                                    /* cond_not_taken_branch_cost.  */
733 };
734
735 struct processor_costs amdfam10_cost = {
736   COSTS_N_INSNS (1),                    /* cost of an add instruction */
737   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
738   COSTS_N_INSNS (1),                    /* variable shift costs */
739   COSTS_N_INSNS (1),                    /* constant shift costs */
740   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
741    COSTS_N_INSNS (4),                   /*                               HI */
742    COSTS_N_INSNS (3),                   /*                               SI */
743    COSTS_N_INSNS (4),                   /*                               DI */
744    COSTS_N_INSNS (5)},                  /*                               other */
745   0,                                    /* cost of multiply per each bit set */
746   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
747    COSTS_N_INSNS (35),                  /*                          HI */
748    COSTS_N_INSNS (51),                  /*                          SI */
749    COSTS_N_INSNS (83),                  /*                          DI */
750    COSTS_N_INSNS (83)},                 /*                          other */
751   COSTS_N_INSNS (1),                    /* cost of movsx */
752   COSTS_N_INSNS (1),                    /* cost of movzx */
753   8,                                    /* "large" insn */
754   9,                                    /* MOVE_RATIO */
755   4,                                    /* cost for loading QImode using movzbl */
756   {3, 4, 3},                            /* cost of loading integer registers
757                                            in QImode, HImode and SImode.
758                                            Relative to reg-reg move (2).  */
759   {3, 4, 3},                            /* cost of storing integer registers */
760   4,                                    /* cost of reg,reg fld/fst */
761   {4, 4, 12},                           /* cost of loading fp registers
762                                            in SFmode, DFmode and XFmode */
763   {6, 6, 8},                            /* cost of storing fp registers
764                                            in SFmode, DFmode and XFmode */
765   2,                                    /* cost of moving MMX register */
766   {3, 3},                               /* cost of loading MMX registers
767                                            in SImode and DImode */
768   {4, 4},                               /* cost of storing MMX registers
769                                            in SImode and DImode */
770   2,                                    /* cost of moving SSE register */
771   {4, 4, 3},                            /* cost of loading SSE registers
772                                            in SImode, DImode and TImode */
773   {4, 4, 5},                            /* cost of storing SSE registers
774                                            in SImode, DImode and TImode */
775   3,                                    /* MMX or SSE register to integer */
776                                         /* On K8
777                                             MOVD reg64, xmmreg  Double  FSTORE 4
778                                             MOVD reg32, xmmreg  Double  FSTORE 4
779                                            On AMDFAM10
780                                             MOVD reg64, xmmreg  Double  FADD 3
781                                                                 1/1  1/1
782                                             MOVD reg32, xmmreg  Double  FADD 3
783                                                                 1/1  1/1 */
784   64,                                   /* size of l1 cache.  */
785   512,                                  /* size of l2 cache.  */
786   64,                                   /* size of prefetch block */
787   /* New AMD processors never drop prefetches; if they cannot be performed
788      immediately, they are queued.  We set number of simultaneous prefetches
789      to a large constant to reflect this (it probably is not a good idea not
790      to limit number of prefetches at all, as their execution also takes some
791      time).  */
792   100,                                  /* number of parallel prefetches */
793   2,                                    /* Branch cost */
794   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
795   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
796   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
797   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
798   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
799   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
800
801   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
802      very small blocks it is better to use loop. For large blocks, libcall can
803      do nontemporary accesses and beat inline considerably.  */
804   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
805    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
806   {{libcall, {{8, loop}, {24, unrolled_loop},
807               {2048, rep_prefix_4_byte}, {-1, libcall}}},
808    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
809   4,                                    /* scalar_stmt_cost.  */
810   2,                                    /* scalar load_cost.  */
811   2,                                    /* scalar_store_cost.  */
812   6,                                    /* vec_stmt_cost.  */
813   0,                                    /* vec_to_scalar_cost.  */
814   2,                                    /* scalar_to_vec_cost.  */
815   2,                                    /* vec_align_load_cost.  */
816   2,                                    /* vec_unalign_load_cost.  */
817   2,                                    /* vec_store_cost.  */
818   2,                                    /* cond_taken_branch_cost.  */
819   1,                                    /* cond_not_taken_branch_cost.  */
820 };
821
822 static const
823 struct processor_costs pentium4_cost = {
824   COSTS_N_INSNS (1),                    /* cost of an add instruction */
825   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
826   COSTS_N_INSNS (4),                    /* variable shift costs */
827   COSTS_N_INSNS (4),                    /* constant shift costs */
828   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
829    COSTS_N_INSNS (15),                  /*                               HI */
830    COSTS_N_INSNS (15),                  /*                               SI */
831    COSTS_N_INSNS (15),                  /*                               DI */
832    COSTS_N_INSNS (15)},                 /*                               other */
833   0,                                    /* cost of multiply per each bit set */
834   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
835    COSTS_N_INSNS (56),                  /*                          HI */
836    COSTS_N_INSNS (56),                  /*                          SI */
837    COSTS_N_INSNS (56),                  /*                          DI */
838    COSTS_N_INSNS (56)},                 /*                          other */
839   COSTS_N_INSNS (1),                    /* cost of movsx */
840   COSTS_N_INSNS (1),                    /* cost of movzx */
841   16,                                   /* "large" insn */
842   6,                                    /* MOVE_RATIO */
843   2,                                    /* cost for loading QImode using movzbl */
844   {4, 5, 4},                            /* cost of loading integer registers
845                                            in QImode, HImode and SImode.
846                                            Relative to reg-reg move (2).  */
847   {2, 3, 2},                            /* cost of storing integer registers */
848   2,                                    /* cost of reg,reg fld/fst */
849   {2, 2, 6},                            /* cost of loading fp registers
850                                            in SFmode, DFmode and XFmode */
851   {4, 4, 6},                            /* cost of storing fp registers
852                                            in SFmode, DFmode and XFmode */
853   2,                                    /* cost of moving MMX register */
854   {2, 2},                               /* cost of loading MMX registers
855                                            in SImode and DImode */
856   {2, 2},                               /* cost of storing MMX registers
857                                            in SImode and DImode */
858   12,                                   /* cost of moving SSE register */
859   {12, 12, 12},                         /* cost of loading SSE registers
860                                            in SImode, DImode and TImode */
861   {2, 2, 8},                            /* cost of storing SSE registers
862                                            in SImode, DImode and TImode */
863   10,                                   /* MMX or SSE register to integer */
864   8,                                    /* size of l1 cache.  */
865   256,                                  /* size of l2 cache.  */
866   64,                                   /* size of prefetch block */
867   6,                                    /* number of parallel prefetches */
868   2,                                    /* Branch cost */
869   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
870   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
871   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
872   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
873   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
874   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
875   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
876    DUMMY_STRINGOP_ALGS},
877   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
878    {-1, libcall}}},
879    DUMMY_STRINGOP_ALGS},
880   1,                                    /* scalar_stmt_cost.  */
881   1,                                    /* scalar load_cost.  */
882   1,                                    /* scalar_store_cost.  */
883   1,                                    /* vec_stmt_cost.  */
884   1,                                    /* vec_to_scalar_cost.  */
885   1,                                    /* scalar_to_vec_cost.  */
886   1,                                    /* vec_align_load_cost.  */
887   2,                                    /* vec_unalign_load_cost.  */
888   1,                                    /* vec_store_cost.  */
889   3,                                    /* cond_taken_branch_cost.  */
890   1,                                    /* cond_not_taken_branch_cost.  */
891 };
892
893 static const
894 struct processor_costs nocona_cost = {
895   COSTS_N_INSNS (1),                    /* cost of an add instruction */
896   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
897   COSTS_N_INSNS (1),                    /* variable shift costs */
898   COSTS_N_INSNS (1),                    /* constant shift costs */
899   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
900    COSTS_N_INSNS (10),                  /*                               HI */
901    COSTS_N_INSNS (10),                  /*                               SI */
902    COSTS_N_INSNS (10),                  /*                               DI */
903    COSTS_N_INSNS (10)},                 /*                               other */
904   0,                                    /* cost of multiply per each bit set */
905   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
906    COSTS_N_INSNS (66),                  /*                          HI */
907    COSTS_N_INSNS (66),                  /*                          SI */
908    COSTS_N_INSNS (66),                  /*                          DI */
909    COSTS_N_INSNS (66)},                 /*                          other */
910   COSTS_N_INSNS (1),                    /* cost of movsx */
911   COSTS_N_INSNS (1),                    /* cost of movzx */
912   16,                                   /* "large" insn */
913   17,                                   /* MOVE_RATIO */
914   4,                                    /* cost for loading QImode using movzbl */
915   {4, 4, 4},                            /* cost of loading integer registers
916                                            in QImode, HImode and SImode.
917                                            Relative to reg-reg move (2).  */
918   {4, 4, 4},                            /* cost of storing integer registers */
919   3,                                    /* cost of reg,reg fld/fst */
920   {12, 12, 12},                         /* cost of loading fp registers
921                                            in SFmode, DFmode and XFmode */
922   {4, 4, 4},                            /* cost of storing fp registers
923                                            in SFmode, DFmode and XFmode */
924   6,                                    /* cost of moving MMX register */
925   {12, 12},                             /* cost of loading MMX registers
926                                            in SImode and DImode */
927   {12, 12},                             /* cost of storing MMX registers
928                                            in SImode and DImode */
929   6,                                    /* cost of moving SSE register */
930   {12, 12, 12},                         /* cost of loading SSE registers
931                                            in SImode, DImode and TImode */
932   {12, 12, 12},                         /* cost of storing SSE registers
933                                            in SImode, DImode and TImode */
934   8,                                    /* MMX or SSE register to integer */
935   8,                                    /* size of l1 cache.  */
936   1024,                                 /* size of l2 cache.  */
937   128,                                  /* size of prefetch block */
938   8,                                    /* number of parallel prefetches */
939   1,                                    /* Branch cost */
940   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
941   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
942   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
943   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
944   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
945   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
946   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
947    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
948               {100000, unrolled_loop}, {-1, libcall}}}},
949   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
950    {-1, libcall}}},
951    {libcall, {{24, loop}, {64, unrolled_loop},
952               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
953   1,                                    /* scalar_stmt_cost.  */
954   1,                                    /* scalar load_cost.  */
955   1,                                    /* scalar_store_cost.  */
956   1,                                    /* vec_stmt_cost.  */
957   1,                                    /* vec_to_scalar_cost.  */
958   1,                                    /* scalar_to_vec_cost.  */
959   1,                                    /* vec_align_load_cost.  */
960   2,                                    /* vec_unalign_load_cost.  */
961   1,                                    /* vec_store_cost.  */
962   3,                                    /* cond_taken_branch_cost.  */
963   1,                                    /* cond_not_taken_branch_cost.  */
964 };
965
966 static const
967 struct processor_costs core2_cost = {
968   COSTS_N_INSNS (1),                    /* cost of an add instruction */
969   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
970   COSTS_N_INSNS (1),                    /* variable shift costs */
971   COSTS_N_INSNS (1),                    /* constant shift costs */
972   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
973    COSTS_N_INSNS (3),                   /*                               HI */
974    COSTS_N_INSNS (3),                   /*                               SI */
975    COSTS_N_INSNS (3),                   /*                               DI */
976    COSTS_N_INSNS (3)},                  /*                               other */
977   0,                                    /* cost of multiply per each bit set */
978   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
979    COSTS_N_INSNS (22),                  /*                          HI */
980    COSTS_N_INSNS (22),                  /*                          SI */
981    COSTS_N_INSNS (22),                  /*                          DI */
982    COSTS_N_INSNS (22)},                 /*                          other */
983   COSTS_N_INSNS (1),                    /* cost of movsx */
984   COSTS_N_INSNS (1),                    /* cost of movzx */
985   8,                                    /* "large" insn */
986   16,                                   /* MOVE_RATIO */
987   2,                                    /* cost for loading QImode using movzbl */
988   {6, 6, 6},                            /* cost of loading integer registers
989                                            in QImode, HImode and SImode.
990                                            Relative to reg-reg move (2).  */
991   {4, 4, 4},                            /* cost of storing integer registers */
992   2,                                    /* cost of reg,reg fld/fst */
993   {6, 6, 6},                            /* cost of loading fp registers
994                                            in SFmode, DFmode and XFmode */
995   {4, 4, 4},                            /* cost of storing fp registers
996                                            in SFmode, DFmode and XFmode */
997   2,                                    /* cost of moving MMX register */
998   {6, 6},                               /* cost of loading MMX registers
999                                            in SImode and DImode */
1000   {4, 4},                               /* cost of storing MMX registers
1001                                            in SImode and DImode */
1002   2,                                    /* cost of moving SSE register */
1003   {6, 6, 6},                            /* cost of loading SSE registers
1004                                            in SImode, DImode and TImode */
1005   {4, 4, 4},                            /* cost of storing SSE registers
1006                                            in SImode, DImode and TImode */
1007   2,                                    /* MMX or SSE register to integer */
1008   32,                                   /* size of l1 cache.  */
1009   2048,                                 /* size of l2 cache.  */
1010   128,                                  /* size of prefetch block */
1011   8,                                    /* number of parallel prefetches */
1012   3,                                    /* Branch cost */
1013   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
1014   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
1015   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
1016   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
1017   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
1018   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
1019   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1020    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1021               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1022   {{libcall, {{8, loop}, {15, unrolled_loop},
1023               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1024    {libcall, {{24, loop}, {32, unrolled_loop},
1025               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1026   1,                                    /* scalar_stmt_cost.  */
1027   1,                                    /* scalar load_cost.  */
1028   1,                                    /* scalar_store_cost.  */
1029   1,                                    /* vec_stmt_cost.  */
1030   1,                                    /* vec_to_scalar_cost.  */
1031   1,                                    /* scalar_to_vec_cost.  */
1032   1,                                    /* vec_align_load_cost.  */
1033   2,                                    /* vec_unalign_load_cost.  */
1034   1,                                    /* vec_store_cost.  */
1035   3,                                    /* cond_taken_branch_cost.  */
1036   1,                                    /* cond_not_taken_branch_cost.  */
1037 };
1038
1039 static const
1040 struct processor_costs atom_cost = {
1041   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1042   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1043   COSTS_N_INSNS (1),                    /* variable shift costs */
1044   COSTS_N_INSNS (1),                    /* constant shift costs */
1045   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1046    COSTS_N_INSNS (4),                   /*                               HI */
1047    COSTS_N_INSNS (3),                   /*                               SI */
1048    COSTS_N_INSNS (4),                   /*                               DI */
1049    COSTS_N_INSNS (2)},                  /*                               other */
1050   0,                                    /* cost of multiply per each bit set */
1051   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1052    COSTS_N_INSNS (26),                  /*                          HI */
1053    COSTS_N_INSNS (42),                  /*                          SI */
1054    COSTS_N_INSNS (74),                  /*                          DI */
1055    COSTS_N_INSNS (74)},                 /*                          other */
1056   COSTS_N_INSNS (1),                    /* cost of movsx */
1057   COSTS_N_INSNS (1),                    /* cost of movzx */
1058   8,                                    /* "large" insn */
1059   17,                                   /* MOVE_RATIO */
1060   2,                                    /* cost for loading QImode using movzbl */
1061   {4, 4, 4},                            /* cost of loading integer registers
1062                                            in QImode, HImode and SImode.
1063                                            Relative to reg-reg move (2).  */
1064   {4, 4, 4},                            /* cost of storing integer registers */
1065   4,                                    /* cost of reg,reg fld/fst */
1066   {12, 12, 12},                         /* cost of loading fp registers
1067                                            in SFmode, DFmode and XFmode */
1068   {6, 6, 8},                            /* cost of storing fp registers
1069                                            in SFmode, DFmode and XFmode */
1070   2,                                    /* cost of moving MMX register */
1071   {8, 8},                               /* cost of loading MMX registers
1072                                            in SImode and DImode */
1073   {8, 8},                               /* cost of storing MMX registers
1074                                            in SImode and DImode */
1075   2,                                    /* cost of moving SSE register */
1076   {8, 8, 8},                            /* cost of loading SSE registers
1077                                            in SImode, DImode and TImode */
1078   {8, 8, 8},                            /* cost of storing SSE registers
1079                                            in SImode, DImode and TImode */
1080   5,                                    /* MMX or SSE register to integer */
1081   32,                                   /* size of l1 cache.  */
1082   256,                                  /* size of l2 cache.  */
1083   64,                                   /* size of prefetch block */
1084   6,                                    /* number of parallel prefetches */
1085   3,                                    /* Branch cost */
1086   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1087   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1088   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1089   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1090   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1091   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1092   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1093    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1094           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1095   {{libcall, {{8, loop}, {15, unrolled_loop},
1096           {2048, rep_prefix_4_byte}, {-1, libcall}}},
1097    {libcall, {{24, loop}, {32, unrolled_loop},
1098           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1099   1,                                    /* scalar_stmt_cost.  */
1100   1,                                    /* scalar load_cost.  */
1101   1,                                    /* scalar_store_cost.  */
1102   1,                                    /* vec_stmt_cost.  */
1103   1,                                    /* vec_to_scalar_cost.  */
1104   1,                                    /* scalar_to_vec_cost.  */
1105   1,                                    /* vec_align_load_cost.  */
1106   2,                                    /* vec_unalign_load_cost.  */
1107   1,                                    /* vec_store_cost.  */
1108   3,                                    /* cond_taken_branch_cost.  */
1109   1,                                    /* cond_not_taken_branch_cost.  */
1110 };
1111
1112 /* Generic64 should produce code tuned for Nocona and K8.  */
1113 static const
1114 struct processor_costs generic64_cost = {
1115   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1116   /* On all chips taken into consideration lea is 2 cycles and more.  With
1117      this cost however our current implementation of synth_mult results in
1118      use of unnecessary temporary registers causing regression on several
1119      SPECfp benchmarks.  */
1120   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1121   COSTS_N_INSNS (1),                    /* variable shift costs */
1122   COSTS_N_INSNS (1),                    /* constant shift costs */
1123   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1124    COSTS_N_INSNS (4),                   /*                               HI */
1125    COSTS_N_INSNS (3),                   /*                               SI */
1126    COSTS_N_INSNS (4),                   /*                               DI */
1127    COSTS_N_INSNS (2)},                  /*                               other */
1128   0,                                    /* cost of multiply per each bit set */
1129   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1130    COSTS_N_INSNS (26),                  /*                          HI */
1131    COSTS_N_INSNS (42),                  /*                          SI */
1132    COSTS_N_INSNS (74),                  /*                          DI */
1133    COSTS_N_INSNS (74)},                 /*                          other */
1134   COSTS_N_INSNS (1),                    /* cost of movsx */
1135   COSTS_N_INSNS (1),                    /* cost of movzx */
1136   8,                                    /* "large" insn */
1137   17,                                   /* MOVE_RATIO */
1138   4,                                    /* cost for loading QImode using movzbl */
1139   {4, 4, 4},                            /* cost of loading integer registers
1140                                            in QImode, HImode and SImode.
1141                                            Relative to reg-reg move (2).  */
1142   {4, 4, 4},                            /* cost of storing integer registers */
1143   4,                                    /* cost of reg,reg fld/fst */
1144   {12, 12, 12},                         /* cost of loading fp registers
1145                                            in SFmode, DFmode and XFmode */
1146   {6, 6, 8},                            /* cost of storing fp registers
1147                                            in SFmode, DFmode and XFmode */
1148   2,                                    /* cost of moving MMX register */
1149   {8, 8},                               /* cost of loading MMX registers
1150                                            in SImode and DImode */
1151   {8, 8},                               /* cost of storing MMX registers
1152                                            in SImode and DImode */
1153   2,                                    /* cost of moving SSE register */
1154   {8, 8, 8},                            /* cost of loading SSE registers
1155                                            in SImode, DImode and TImode */
1156   {8, 8, 8},                            /* cost of storing SSE registers
1157                                            in SImode, DImode and TImode */
1158   5,                                    /* MMX or SSE register to integer */
1159   32,                                   /* size of l1 cache.  */
1160   512,                                  /* size of l2 cache.  */
1161   64,                                   /* size of prefetch block */
1162   6,                                    /* number of parallel prefetches */
1163   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
1164      is increased to perhaps more appropriate value of 5.  */
1165   3,                                    /* Branch cost */
1166   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1167   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1168   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1169   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1170   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1171   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1172   {DUMMY_STRINGOP_ALGS,
1173    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1174   {DUMMY_STRINGOP_ALGS,
1175    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1176   1,                                    /* scalar_stmt_cost.  */
1177   1,                                    /* scalar load_cost.  */
1178   1,                                    /* scalar_store_cost.  */
1179   1,                                    /* vec_stmt_cost.  */
1180   1,                                    /* vec_to_scalar_cost.  */
1181   1,                                    /* scalar_to_vec_cost.  */
1182   1,                                    /* vec_align_load_cost.  */
1183   2,                                    /* vec_unalign_load_cost.  */
1184   1,                                    /* vec_store_cost.  */
1185   3,                                    /* cond_taken_branch_cost.  */
1186   1,                                    /* cond_not_taken_branch_cost.  */
1187 };
1188
1189 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
1190 static const
1191 struct processor_costs generic32_cost = {
1192   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1193   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1194   COSTS_N_INSNS (1),                    /* variable shift costs */
1195   COSTS_N_INSNS (1),                    /* constant shift costs */
1196   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1197    COSTS_N_INSNS (4),                   /*                               HI */
1198    COSTS_N_INSNS (3),                   /*                               SI */
1199    COSTS_N_INSNS (4),                   /*                               DI */
1200    COSTS_N_INSNS (2)},                  /*                               other */
1201   0,                                    /* cost of multiply per each bit set */
1202   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1203    COSTS_N_INSNS (26),                  /*                          HI */
1204    COSTS_N_INSNS (42),                  /*                          SI */
1205    COSTS_N_INSNS (74),                  /*                          DI */
1206    COSTS_N_INSNS (74)},                 /*                          other */
1207   COSTS_N_INSNS (1),                    /* cost of movsx */
1208   COSTS_N_INSNS (1),                    /* cost of movzx */
1209   8,                                    /* "large" insn */
1210   17,                                   /* MOVE_RATIO */
1211   4,                                    /* cost for loading QImode using movzbl */
1212   {4, 4, 4},                            /* cost of loading integer registers
1213                                            in QImode, HImode and SImode.
1214                                            Relative to reg-reg move (2).  */
1215   {4, 4, 4},                            /* cost of storing integer registers */
1216   4,                                    /* cost of reg,reg fld/fst */
1217   {12, 12, 12},                         /* cost of loading fp registers
1218                                            in SFmode, DFmode and XFmode */
1219   {6, 6, 8},                            /* cost of storing fp registers
1220                                            in SFmode, DFmode and XFmode */
1221   2,                                    /* cost of moving MMX register */
1222   {8, 8},                               /* cost of loading MMX registers
1223                                            in SImode and DImode */
1224   {8, 8},                               /* cost of storing MMX registers
1225                                            in SImode and DImode */
1226   2,                                    /* cost of moving SSE register */
1227   {8, 8, 8},                            /* cost of loading SSE registers
1228                                            in SImode, DImode and TImode */
1229   {8, 8, 8},                            /* cost of storing SSE registers
1230                                            in SImode, DImode and TImode */
1231   5,                                    /* MMX or SSE register to integer */
1232   32,                                   /* size of l1 cache.  */
1233   256,                                  /* size of l2 cache.  */
1234   64,                                   /* size of prefetch block */
1235   6,                                    /* number of parallel prefetches */
1236   3,                                    /* Branch cost */
1237   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1238   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1239   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1240   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1241   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1242   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1243   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1244    DUMMY_STRINGOP_ALGS},
1245   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1246    DUMMY_STRINGOP_ALGS},
1247   1,                                    /* scalar_stmt_cost.  */
1248   1,                                    /* scalar load_cost.  */
1249   1,                                    /* scalar_store_cost.  */
1250   1,                                    /* vec_stmt_cost.  */
1251   1,                                    /* vec_to_scalar_cost.  */
1252   1,                                    /* scalar_to_vec_cost.  */
1253   1,                                    /* vec_align_load_cost.  */
1254   2,                                    /* vec_unalign_load_cost.  */
1255   1,                                    /* vec_store_cost.  */
1256   3,                                    /* cond_taken_branch_cost.  */
1257   1,                                    /* cond_not_taken_branch_cost.  */
1258 };
1259
1260 const struct processor_costs *ix86_cost = &pentium_cost;
1261
1262 /* Processor feature/optimization bitmasks.  */
1263 #define m_386 (1<<PROCESSOR_I386)
1264 #define m_486 (1<<PROCESSOR_I486)
1265 #define m_PENT (1<<PROCESSOR_PENTIUM)
1266 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
1267 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
1268 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
1269 #define m_CORE2  (1<<PROCESSOR_CORE2)
1270 #define m_ATOM  (1<<PROCESSOR_ATOM)
1271
1272 #define m_GEODE  (1<<PROCESSOR_GEODE)
1273 #define m_K6  (1<<PROCESSOR_K6)
1274 #define m_K6_GEODE  (m_K6 | m_GEODE)
1275 #define m_K8  (1<<PROCESSOR_K8)
1276 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
1277 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
1278 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
1279 #define m_AMD_MULTIPLE  (m_K8 | m_ATHLON | m_AMDFAM10)
1280
1281 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1282 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1283
1284 /* Generic instruction choice should be common subset of supported CPUs
1285    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1286 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1287
1288 /* Feature tests against the various tunings.  */
1289 unsigned char ix86_tune_features[X86_TUNE_LAST];
1290
1291 /* Feature tests against the various tunings used to create ix86_tune_features
1292    based on the processor mask.  */
1293 static unsigned int initial_ix86_tune_features[X86_TUNE_LAST] = {
1294   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1295      negatively, so enabling for Generic64 seems like good code size
1296      tradeoff.  We can't enable it for 32bit generic because it does not
1297      work well with PPro base chips.  */
1298   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_CORE2 | m_GENERIC64,
1299
1300   /* X86_TUNE_PUSH_MEMORY */
1301   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4
1302   | m_NOCONA | m_CORE2 | m_GENERIC,
1303
1304   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1305   m_486 | m_PENT,
1306
1307   /* X86_TUNE_UNROLL_STRLEN */
1308   m_486 | m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_K6
1309   | m_CORE2 | m_GENERIC,
1310
1311   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1312   m_ATOM | m_PPRO | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4 | m_GENERIC,
1313
1314   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1315      on simulation result. But after P4 was made, no performance benefit
1316      was observed with branch hints.  It also increases the code size.
1317      As a result, icc never generates branch hints.  */
1318   0,
1319
1320   /* X86_TUNE_DOUBLE_WITH_ADD */
1321   ~m_386,
1322
1323   /* X86_TUNE_USE_SAHF */
1324   m_ATOM | m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_PENT4
1325   | m_NOCONA | m_CORE2 | m_GENERIC,
1326
1327   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1328      partial dependencies.  */
1329   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_PENT4 | m_NOCONA
1330   | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1331
1332   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1333      register stalls on Generic32 compilation setting as well.  However
1334      in current implementation the partial register stalls are not eliminated
1335      very well - they can be introduced via subregs synthesized by combine
1336      and can happen in caller/callee saving sequences.  Because this option
1337      pays back little on PPro based chips and is in conflict with partial reg
1338      dependencies used by Athlon/P4 based chips, it is better to leave it off
1339      for generic32 for now.  */
1340   m_PPRO,
1341
1342   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1343   m_CORE2 | m_GENERIC,
1344
1345   /* X86_TUNE_USE_HIMODE_FIOP */
1346   m_386 | m_486 | m_K6_GEODE,
1347
1348   /* X86_TUNE_USE_SIMODE_FIOP */
1349   ~(m_PPRO | m_AMD_MULTIPLE | m_PENT | m_ATOM | m_CORE2 | m_GENERIC),
1350
1351   /* X86_TUNE_USE_MOV0 */
1352   m_K6,
1353
1354   /* X86_TUNE_USE_CLTD */
1355   ~(m_PENT | m_ATOM | m_K6 | m_CORE2 | m_GENERIC),
1356
1357   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1358   m_PENT4,
1359
1360   /* X86_TUNE_SPLIT_LONG_MOVES */
1361   m_PPRO,
1362
1363   /* X86_TUNE_READ_MODIFY_WRITE */
1364   ~m_PENT,
1365
1366   /* X86_TUNE_READ_MODIFY */
1367   ~(m_PENT | m_PPRO),
1368
1369   /* X86_TUNE_PROMOTE_QIMODE */
1370   m_K6_GEODE | m_PENT | m_ATOM | m_386 | m_486 | m_AMD_MULTIPLE
1371   | m_CORE2 | m_GENERIC /* | m_PENT4 ? */,
1372
1373   /* X86_TUNE_FAST_PREFIX */
1374   ~(m_PENT | m_486 | m_386),
1375
1376   /* X86_TUNE_SINGLE_STRINGOP */
1377   m_386 | m_PENT4 | m_NOCONA,
1378
1379   /* X86_TUNE_QIMODE_MATH */
1380   ~0,
1381
1382   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1383      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1384      might be considered for Generic32 if our scheme for avoiding partial
1385      stalls was more effective.  */
1386   ~m_PPRO,
1387
1388   /* X86_TUNE_PROMOTE_QI_REGS */
1389   0,
1390
1391   /* X86_TUNE_PROMOTE_HI_REGS */
1392   m_PPRO,
1393
1394   /* X86_TUNE_ADD_ESP_4: Enable if add/sub is preferred over 1/2 push/pop.  */
1395   m_ATOM | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT4 | m_NOCONA
1396   | m_CORE2 | m_GENERIC,
1397
1398   /* X86_TUNE_ADD_ESP_8 */
1399   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_K6_GEODE | m_386
1400   | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1401
1402   /* X86_TUNE_SUB_ESP_4 */
1403   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2
1404   | m_GENERIC,
1405
1406   /* X86_TUNE_SUB_ESP_8 */
1407   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_386 | m_486
1408   | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1409
1410   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1411      for DFmode copies */
1412   ~(m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1413     | m_GENERIC | m_GEODE),
1414
1415   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1416   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1417
1418   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1419      conflict here in between PPro/Pentium4 based chips that thread 128bit
1420      SSE registers as single units versus K8 based chips that divide SSE
1421      registers to two 64bit halves.  This knob promotes all store destinations
1422      to be 128bit to allow register renaming on 128bit SSE units, but usually
1423      results in one extra microop on 64bit SSE units.  Experimental results
1424      shows that disabling this option on P4 brings over 20% SPECfp regression,
1425      while enabling it on K8 brings roughly 2.4% regression that can be partly
1426      masked by careful scheduling of moves.  */
1427   m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC
1428   | m_AMDFAM10,
1429
1430   /* X86_TUNE_SSE_UNALIGNED_MOVE_OPTIMAL */
1431   m_AMDFAM10,
1432
1433   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1434      are resolved on SSE register parts instead of whole registers, so we may
1435      maintain just lower part of scalar values in proper format leaving the
1436      upper part undefined.  */
1437   m_ATHLON_K8,
1438
1439   /* X86_TUNE_SSE_TYPELESS_STORES */
1440   m_AMD_MULTIPLE,
1441
1442   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1443   m_PPRO | m_PENT4 | m_NOCONA,
1444
1445   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1446   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1447
1448   /* X86_TUNE_PROLOGUE_USING_MOVE */
1449   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2 | m_GENERIC,
1450
1451   /* X86_TUNE_EPILOGUE_USING_MOVE */
1452   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2 | m_GENERIC,
1453
1454   /* X86_TUNE_SHIFT1 */
1455   ~m_486,
1456
1457   /* X86_TUNE_USE_FFREEP */
1458   m_AMD_MULTIPLE,
1459
1460   /* X86_TUNE_INTER_UNIT_MOVES */
1461   ~(m_AMD_MULTIPLE | m_GENERIC),
1462
1463   /* X86_TUNE_INTER_UNIT_CONVERSIONS */
1464   ~(m_AMDFAM10),
1465
1466   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1467      than 4 branch instructions in the 16 byte window.  */
1468   m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2
1469   | m_GENERIC,
1470
1471   /* X86_TUNE_SCHEDULE */
1472   m_PPRO | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT | m_ATOM | m_CORE2
1473   | m_GENERIC,
1474
1475   /* X86_TUNE_USE_BT */
1476   m_AMD_MULTIPLE | m_ATOM | m_CORE2 | m_GENERIC,
1477
1478   /* X86_TUNE_USE_INCDEC */
1479   ~(m_PENT4 | m_NOCONA | m_GENERIC | m_ATOM),
1480
1481   /* X86_TUNE_PAD_RETURNS */
1482   m_AMD_MULTIPLE | m_CORE2 | m_GENERIC,
1483
1484   /* X86_TUNE_EXT_80387_CONSTANTS */
1485   m_K6_GEODE | m_ATHLON_K8 | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO
1486   | m_CORE2 | m_GENERIC,
1487
1488   /* X86_TUNE_SHORTEN_X87_SSE */
1489   ~m_K8,
1490
1491   /* X86_TUNE_AVOID_VECTOR_DECODE */
1492   m_K8 | m_GENERIC64,
1493
1494   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1495      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1496   ~(m_386 | m_486),
1497
1498   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1499      vector path on AMD machines.  */
1500   m_K8 | m_GENERIC64 | m_AMDFAM10,
1501
1502   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1503      machines.  */
1504   m_K8 | m_GENERIC64 | m_AMDFAM10,
1505
1506   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1507      than a MOV.  */
1508   m_PENT,
1509
1510   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1511      but one byte longer.  */
1512   m_PENT,
1513
1514   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1515      operand that cannot be represented using a modRM byte.  The XOR
1516      replacement is long decoded, so this split helps here as well.  */
1517   m_K6,
1518
1519   /* X86_TUNE_USE_VECTOR_FP_CONVERTS: Prefer vector packed SSE conversion
1520      from FP to FP. */
1521   m_AMDFAM10 | m_GENERIC,
1522
1523   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
1524      from integer to FP. */
1525   m_AMDFAM10,
1526
1527   /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction
1528      with a subsequent conditional jump instruction into a single
1529      compare-and-branch uop.  */
1530   m_CORE2,
1531
1532   /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag
1533      will impact LEA instruction selection. */
1534   m_ATOM,
1535 };
1536
1537 /* Feature tests against the various architecture variations.  */
1538 unsigned char ix86_arch_features[X86_ARCH_LAST];
1539
1540 /* Feature tests against the various architecture variations, used to create
1541    ix86_arch_features based on the processor mask.  */
1542 static unsigned int initial_ix86_arch_features[X86_ARCH_LAST] = {
1543   /* X86_ARCH_CMOVE: Conditional move was added for pentiumpro.  */
1544   ~(m_386 | m_486 | m_PENT | m_K6),
1545
1546   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1547   ~m_386,
1548
1549   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1550   ~(m_386 | m_486),
1551
1552   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1553   ~m_386,
1554
1555   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1556   ~m_386,
1557 };
1558
1559 static const unsigned int x86_accumulate_outgoing_args
1560   = m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1561     | m_GENERIC;
1562
1563 static const unsigned int x86_arch_always_fancy_math_387
1564   = m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4
1565     | m_NOCONA | m_CORE2 | m_GENERIC;
1566
1567 static enum stringop_alg stringop_alg = no_stringop;
1568
1569 /* In case the average insn count for single function invocation is
1570    lower than this constant, emit fast (but longer) prologue and
1571    epilogue code.  */
1572 #define FAST_PROLOGUE_INSN_COUNT 20
1573
1574 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1575 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1576 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1577 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1578
1579 /* Array of the smallest class containing reg number REGNO, indexed by
1580    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1581
1582 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1583 {
1584   /* ax, dx, cx, bx */
1585   AREG, DREG, CREG, BREG,
1586   /* si, di, bp, sp */
1587   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1588   /* FP registers */
1589   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1590   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1591   /* arg pointer */
1592   NON_Q_REGS,
1593   /* flags, fpsr, fpcr, frame */
1594   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1595   /* SSE registers */
1596   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1597   SSE_REGS, SSE_REGS,
1598   /* MMX registers */
1599   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1600   MMX_REGS, MMX_REGS,
1601   /* REX registers */
1602   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1603   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1604   /* SSE REX registers */
1605   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1606   SSE_REGS, SSE_REGS,
1607 };
1608
1609 /* The "default" register map used in 32bit mode.  */
1610
1611 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1612 {
1613   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1614   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1615   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1616   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1617   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1618   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1619   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1620 };
1621
1622 /* The "default" register map used in 64bit mode.  */
1623
1624 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1625 {
1626   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1627   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1628   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1629   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1630   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1631   8,9,10,11,12,13,14,15,                /* extended integer registers */
1632   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1633 };
1634
1635 /* Define the register numbers to be used in Dwarf debugging information.
1636    The SVR4 reference port C compiler uses the following register numbers
1637    in its Dwarf output code:
1638         0 for %eax (gcc regno = 0)
1639         1 for %ecx (gcc regno = 2)
1640         2 for %edx (gcc regno = 1)
1641         3 for %ebx (gcc regno = 3)
1642         4 for %esp (gcc regno = 7)
1643         5 for %ebp (gcc regno = 6)
1644         6 for %esi (gcc regno = 4)
1645         7 for %edi (gcc regno = 5)
1646    The following three DWARF register numbers are never generated by
1647    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1648    believes these numbers have these meanings.
1649         8  for %eip    (no gcc equivalent)
1650         9  for %eflags (gcc regno = 17)
1651         10 for %trapno (no gcc equivalent)
1652    It is not at all clear how we should number the FP stack registers
1653    for the x86 architecture.  If the version of SDB on x86/svr4 were
1654    a bit less brain dead with respect to floating-point then we would
1655    have a precedent to follow with respect to DWARF register numbers
1656    for x86 FP registers, but the SDB on x86/svr4 is so completely
1657    broken with respect to FP registers that it is hardly worth thinking
1658    of it as something to strive for compatibility with.
1659    The version of x86/svr4 SDB I have at the moment does (partially)
1660    seem to believe that DWARF register number 11 is associated with
1661    the x86 register %st(0), but that's about all.  Higher DWARF
1662    register numbers don't seem to be associated with anything in
1663    particular, and even for DWARF regno 11, SDB only seems to under-
1664    stand that it should say that a variable lives in %st(0) (when
1665    asked via an `=' command) if we said it was in DWARF regno 11,
1666    but SDB still prints garbage when asked for the value of the
1667    variable in question (via a `/' command).
1668    (Also note that the labels SDB prints for various FP stack regs
1669    when doing an `x' command are all wrong.)
1670    Note that these problems generally don't affect the native SVR4
1671    C compiler because it doesn't allow the use of -O with -g and
1672    because when it is *not* optimizing, it allocates a memory
1673    location for each floating-point variable, and the memory
1674    location is what gets described in the DWARF AT_location
1675    attribute for the variable in question.
1676    Regardless of the severe mental illness of the x86/svr4 SDB, we
1677    do something sensible here and we use the following DWARF
1678    register numbers.  Note that these are all stack-top-relative
1679    numbers.
1680         11 for %st(0) (gcc regno = 8)
1681         12 for %st(1) (gcc regno = 9)
1682         13 for %st(2) (gcc regno = 10)
1683         14 for %st(3) (gcc regno = 11)
1684         15 for %st(4) (gcc regno = 12)
1685         16 for %st(5) (gcc regno = 13)
1686         17 for %st(6) (gcc regno = 14)
1687         18 for %st(7) (gcc regno = 15)
1688 */
1689 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1690 {
1691   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1692   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1693   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1694   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1695   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1696   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1697   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1698 };
1699
1700 /* Test and compare insns in i386.md store the information needed to
1701    generate branch and scc insns here.  */
1702
1703 rtx ix86_compare_op0 = NULL_RTX;
1704 rtx ix86_compare_op1 = NULL_RTX;
1705
1706 /* Define parameter passing and return registers.  */
1707
1708 static int const x86_64_int_parameter_registers[6] =
1709 {
1710   DI_REG, SI_REG, DX_REG, CX_REG, R8_REG, R9_REG
1711 };
1712
1713 static int const x86_64_ms_abi_int_parameter_registers[4] =
1714 {
1715   CX_REG, DX_REG, R8_REG, R9_REG
1716 };
1717
1718 static int const x86_64_int_return_registers[4] =
1719 {
1720   AX_REG, DX_REG, DI_REG, SI_REG
1721 };
1722
1723 /* Define the structure for the machine field in struct function.  */
1724
1725 struct GTY(()) stack_local_entry {
1726   unsigned short mode;
1727   unsigned short n;
1728   rtx rtl;
1729   struct stack_local_entry *next;
1730 };
1731
1732 /* Structure describing stack frame layout.
1733    Stack grows downward:
1734
1735    [arguments]
1736                                               <- ARG_POINTER
1737    saved pc
1738
1739    saved frame pointer if frame_pointer_needed
1740                                               <- HARD_FRAME_POINTER
1741    [saved regs]
1742
1743    [padding0]
1744
1745    [saved SSE regs]
1746
1747    [padding1]          \
1748                         )
1749    [va_arg registers]  (
1750                         > to_allocate         <- FRAME_POINTER
1751    [frame]             (
1752                         )
1753    [padding2]          /
1754   */
1755 struct ix86_frame
1756 {
1757   int padding0;
1758   int nsseregs;
1759   int nregs;
1760   int padding1;
1761   int va_arg_size;
1762   HOST_WIDE_INT frame;
1763   int padding2;
1764   int outgoing_arguments_size;
1765   int red_zone_size;
1766
1767   HOST_WIDE_INT to_allocate;
1768   /* The offsets relative to ARG_POINTER.  */
1769   HOST_WIDE_INT frame_pointer_offset;
1770   HOST_WIDE_INT hard_frame_pointer_offset;
1771   HOST_WIDE_INT stack_pointer_offset;
1772
1773   /* When save_regs_using_mov is set, emit prologue using
1774      move instead of push instructions.  */
1775   bool save_regs_using_mov;
1776 };
1777
1778 /* Code model option.  */
1779 enum cmodel ix86_cmodel;
1780 /* Asm dialect.  */
1781 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1782 /* TLS dialects.  */
1783 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1784
1785 /* Which unit we are generating floating point math for.  */
1786 enum fpmath_unit ix86_fpmath;
1787
1788 /* Which cpu are we scheduling for.  */
1789 enum attr_cpu ix86_schedule;
1790
1791 /* Which cpu are we optimizing for.  */
1792 enum processor_type ix86_tune;
1793
1794 /* Which instruction set architecture to use.  */
1795 enum processor_type ix86_arch;
1796
1797 /* true if sse prefetch instruction is not NOOP.  */
1798 int x86_prefetch_sse;
1799
1800 /* ix86_regparm_string as a number */
1801 static int ix86_regparm;
1802
1803 /* -mstackrealign option */
1804 extern int ix86_force_align_arg_pointer;
1805 static const char ix86_force_align_arg_pointer_string[]
1806   = "force_align_arg_pointer";
1807
1808 static rtx (*ix86_gen_leave) (void);
1809 static rtx (*ix86_gen_pop1) (rtx);
1810 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
1811 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
1812 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
1813 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
1814 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
1815 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
1816
1817 /* Preferred alignment for stack boundary in bits.  */
1818 unsigned int ix86_preferred_stack_boundary;
1819
1820 /* Alignment for incoming stack boundary in bits specified at
1821    command line.  */
1822 static unsigned int ix86_user_incoming_stack_boundary;
1823
1824 /* Default alignment for incoming stack boundary in bits.  */
1825 static unsigned int ix86_default_incoming_stack_boundary;
1826
1827 /* Alignment for incoming stack boundary in bits.  */
1828 unsigned int ix86_incoming_stack_boundary;
1829
1830 /* The abi used by target.  */
1831 enum calling_abi ix86_abi;
1832
1833 /* Values 1-5: see jump.c */
1834 int ix86_branch_cost;
1835
1836 /* Calling abi specific va_list type nodes.  */
1837 static GTY(()) tree sysv_va_list_type_node;
1838 static GTY(()) tree ms_va_list_type_node;
1839
1840 /* Variables which are this size or smaller are put in the data/bss
1841    or ldata/lbss sections.  */
1842
1843 int ix86_section_threshold = 65536;
1844
1845 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1846 char internal_label_prefix[16];
1847 int internal_label_prefix_len;
1848
1849 /* Fence to use after loop using movnt.  */
1850 tree x86_mfence;
1851
1852 /* Register class used for passing given 64bit part of the argument.
1853    These represent classes as documented by the PS ABI, with the exception
1854    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1855    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1856
1857    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1858    whenever possible (upper half does contain padding).  */
1859 enum x86_64_reg_class
1860   {
1861     X86_64_NO_CLASS,
1862     X86_64_INTEGER_CLASS,
1863     X86_64_INTEGERSI_CLASS,
1864     X86_64_SSE_CLASS,
1865     X86_64_SSESF_CLASS,
1866     X86_64_SSEDF_CLASS,
1867     X86_64_SSEUP_CLASS,
1868     X86_64_X87_CLASS,
1869     X86_64_X87UP_CLASS,
1870     X86_64_COMPLEX_X87_CLASS,
1871     X86_64_MEMORY_CLASS
1872   };
1873
1874 #define MAX_CLASSES 4
1875
1876 /* Table of constants used by fldpi, fldln2, etc....  */
1877 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1878 static bool ext_80387_constants_init = 0;
1879
1880 \f
1881 static struct machine_function * ix86_init_machine_status (void);
1882 static rtx ix86_function_value (const_tree, const_tree, bool);
1883 static rtx ix86_static_chain (const_tree, bool);
1884 static int ix86_function_regparm (const_tree, const_tree);
1885 static void ix86_compute_frame_layout (struct ix86_frame *);
1886 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1887                                                  rtx, rtx, int);
1888 static void ix86_add_new_builtins (int);
1889 static rtx ix86_expand_vec_perm_builtin (tree);
1890
1891 enum ix86_function_specific_strings
1892 {
1893   IX86_FUNCTION_SPECIFIC_ARCH,
1894   IX86_FUNCTION_SPECIFIC_TUNE,
1895   IX86_FUNCTION_SPECIFIC_FPMATH,
1896   IX86_FUNCTION_SPECIFIC_MAX
1897 };
1898
1899 static char *ix86_target_string (int, int, const char *, const char *,
1900                                  const char *, bool);
1901 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
1902 static void ix86_function_specific_save (struct cl_target_option *);
1903 static void ix86_function_specific_restore (struct cl_target_option *);
1904 static void ix86_function_specific_print (FILE *, int,
1905                                           struct cl_target_option *);
1906 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
1907 static bool ix86_valid_target_attribute_inner_p (tree, char *[]);
1908 static bool ix86_can_inline_p (tree, tree);
1909 static void ix86_set_current_function (tree);
1910 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
1911
1912 static enum calling_abi ix86_function_abi (const_tree);
1913
1914 \f
1915 #ifndef SUBTARGET32_DEFAULT_CPU
1916 #define SUBTARGET32_DEFAULT_CPU "i386"
1917 #endif
1918
1919 /* The svr4 ABI for the i386 says that records and unions are returned
1920    in memory.  */
1921 #ifndef DEFAULT_PCC_STRUCT_RETURN
1922 #define DEFAULT_PCC_STRUCT_RETURN 1
1923 #endif
1924
1925 /* Whether -mtune= or -march= were specified */
1926 static int ix86_tune_defaulted;
1927 static int ix86_arch_specified;
1928
1929 /* Bit flags that specify the ISA we are compiling for.  */
1930 int ix86_isa_flags = TARGET_64BIT_DEFAULT | TARGET_SUBTARGET_ISA_DEFAULT;
1931
1932 /* A mask of ix86_isa_flags that includes bit X if X
1933    was set or cleared on the command line.  */
1934 static int ix86_isa_flags_explicit;
1935
1936 /* Define a set of ISAs which are available when a given ISA is
1937    enabled.  MMX and SSE ISAs are handled separately.  */
1938
1939 #define OPTION_MASK_ISA_MMX_SET OPTION_MASK_ISA_MMX
1940 #define OPTION_MASK_ISA_3DNOW_SET \
1941   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_MMX_SET)
1942
1943 #define OPTION_MASK_ISA_SSE_SET OPTION_MASK_ISA_SSE
1944 #define OPTION_MASK_ISA_SSE2_SET \
1945   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE_SET)
1946 #define OPTION_MASK_ISA_SSE3_SET \
1947   (OPTION_MASK_ISA_SSE3 | OPTION_MASK_ISA_SSE2_SET)
1948 #define OPTION_MASK_ISA_SSSE3_SET \
1949   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE3_SET)
1950 #define OPTION_MASK_ISA_SSE4_1_SET \
1951   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSSE3_SET)
1952 #define OPTION_MASK_ISA_SSE4_2_SET \
1953   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_SSE4_1_SET)
1954 #define OPTION_MASK_ISA_AVX_SET \
1955   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_SSE4_2_SET)
1956 #define OPTION_MASK_ISA_FMA_SET \
1957   (OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_AVX_SET)
1958
1959 /* SSE4 includes both SSE4.1 and SSE4.2. -msse4 should be the same
1960    as -msse4.2.  */
1961 #define OPTION_MASK_ISA_SSE4_SET OPTION_MASK_ISA_SSE4_2_SET
1962
1963 #define OPTION_MASK_ISA_SSE4A_SET \
1964   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE3_SET)
1965 #define OPTION_MASK_ISA_FMA4_SET \
1966   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_SSE4A_SET \
1967    | OPTION_MASK_ISA_AVX_SET)
1968 #define OPTION_MASK_ISA_XOP_SET \
1969   (OPTION_MASK_ISA_XOP | OPTION_MASK_ISA_FMA4_SET)
1970 #define OPTION_MASK_ISA_LWP_SET \
1971   OPTION_MASK_ISA_LWP
1972
1973 /* AES and PCLMUL need SSE2 because they use xmm registers */
1974 #define OPTION_MASK_ISA_AES_SET \
1975   (OPTION_MASK_ISA_AES | OPTION_MASK_ISA_SSE2_SET)
1976 #define OPTION_MASK_ISA_PCLMUL_SET \
1977   (OPTION_MASK_ISA_PCLMUL | OPTION_MASK_ISA_SSE2_SET)
1978
1979 #define OPTION_MASK_ISA_ABM_SET \
1980   (OPTION_MASK_ISA_ABM | OPTION_MASK_ISA_POPCNT)
1981
1982 #define OPTION_MASK_ISA_POPCNT_SET OPTION_MASK_ISA_POPCNT
1983 #define OPTION_MASK_ISA_CX16_SET OPTION_MASK_ISA_CX16
1984 #define OPTION_MASK_ISA_SAHF_SET OPTION_MASK_ISA_SAHF
1985 #define OPTION_MASK_ISA_MOVBE_SET OPTION_MASK_ISA_MOVBE
1986 #define OPTION_MASK_ISA_CRC32_SET OPTION_MASK_ISA_CRC32
1987
1988 /* Define a set of ISAs which aren't available when a given ISA is
1989    disabled.  MMX and SSE ISAs are handled separately.  */
1990
1991 #define OPTION_MASK_ISA_MMX_UNSET \
1992   (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_3DNOW_UNSET)
1993 #define OPTION_MASK_ISA_3DNOW_UNSET \
1994   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_3DNOW_A_UNSET)
1995 #define OPTION_MASK_ISA_3DNOW_A_UNSET OPTION_MASK_ISA_3DNOW_A
1996
1997 #define OPTION_MASK_ISA_SSE_UNSET \
1998   (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_SSE2_UNSET)
1999 #define OPTION_MASK_ISA_SSE2_UNSET \
2000   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3_UNSET)
2001 #define OPTION_MASK_ISA_SSE3_UNSET \
2002   (OPTION_MASK_ISA_SSE3 \
2003    | OPTION_MASK_ISA_SSSE3_UNSET \
2004    | OPTION_MASK_ISA_SSE4A_UNSET )
2005 #define OPTION_MASK_ISA_SSSE3_UNSET \
2006   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE4_1_UNSET)
2007 #define OPTION_MASK_ISA_SSE4_1_UNSET \
2008   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSE4_2_UNSET)
2009 #define OPTION_MASK_ISA_SSE4_2_UNSET \
2010   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_AVX_UNSET )
2011 #define OPTION_MASK_ISA_AVX_UNSET \
2012   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_FMA_UNSET \
2013    | OPTION_MASK_ISA_FMA4_UNSET)
2014 #define OPTION_MASK_ISA_FMA_UNSET OPTION_MASK_ISA_FMA
2015
2016 /* SSE4 includes both SSE4.1 and SSE4.2.  -mno-sse4 should the same
2017    as -mno-sse4.1. */
2018 #define OPTION_MASK_ISA_SSE4_UNSET OPTION_MASK_ISA_SSE4_1_UNSET
2019
2020 #define OPTION_MASK_ISA_SSE4A_UNSET \
2021   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_FMA4_UNSET)
2022
2023 #define OPTION_MASK_ISA_FMA4_UNSET \
2024   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_XOP_UNSET)
2025 #define OPTION_MASK_ISA_XOP_UNSET OPTION_MASK_ISA_XOP
2026 #define OPTION_MASK_ISA_LWP_UNSET OPTION_MASK_ISA_LWP
2027
2028 #define OPTION_MASK_ISA_AES_UNSET OPTION_MASK_ISA_AES
2029 #define OPTION_MASK_ISA_PCLMUL_UNSET OPTION_MASK_ISA_PCLMUL
2030 #define OPTION_MASK_ISA_ABM_UNSET OPTION_MASK_ISA_ABM
2031 #define OPTION_MASK_ISA_POPCNT_UNSET OPTION_MASK_ISA_POPCNT
2032 #define OPTION_MASK_ISA_CX16_UNSET OPTION_MASK_ISA_CX16
2033 #define OPTION_MASK_ISA_SAHF_UNSET OPTION_MASK_ISA_SAHF
2034 #define OPTION_MASK_ISA_MOVBE_UNSET OPTION_MASK_ISA_MOVBE
2035 #define OPTION_MASK_ISA_CRC32_UNSET OPTION_MASK_ISA_CRC32
2036
2037 /* Vectorization library interface and handlers.  */
2038 tree (*ix86_veclib_handler)(enum built_in_function, tree, tree) = NULL;
2039 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2040 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2041
2042 /* Processor target table, indexed by processor number */
2043 struct ptt
2044 {
2045   const struct processor_costs *cost;           /* Processor costs */
2046   const int align_loop;                         /* Default alignments.  */
2047   const int align_loop_max_skip;
2048   const int align_jump;
2049   const int align_jump_max_skip;
2050   const int align_func;
2051 };
2052
2053 static const struct ptt processor_target_table[PROCESSOR_max] =
2054 {
2055   {&i386_cost, 4, 3, 4, 3, 4},
2056   {&i486_cost, 16, 15, 16, 15, 16},
2057   {&pentium_cost, 16, 7, 16, 7, 16},
2058   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2059   {&geode_cost, 0, 0, 0, 0, 0},
2060   {&k6_cost, 32, 7, 32, 7, 32},
2061   {&athlon_cost, 16, 7, 16, 7, 16},
2062   {&pentium4_cost, 0, 0, 0, 0, 0},
2063   {&k8_cost, 16, 7, 16, 7, 16},
2064   {&nocona_cost, 0, 0, 0, 0, 0},
2065   {&core2_cost, 16, 10, 16, 10, 16},
2066   {&generic32_cost, 16, 7, 16, 7, 16},
2067   {&generic64_cost, 16, 10, 16, 10, 16},
2068   {&amdfam10_cost, 32, 24, 32, 7, 32},
2069   {&atom_cost, 16, 7, 16, 7, 16}
2070 };
2071
2072 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2073 {
2074   "generic",
2075   "i386",
2076   "i486",
2077   "pentium",
2078   "pentium-mmx",
2079   "pentiumpro",
2080   "pentium2",
2081   "pentium3",
2082   "pentium4",
2083   "pentium-m",
2084   "prescott",
2085   "nocona",
2086   "core2",
2087   "atom",
2088   "geode",
2089   "k6",
2090   "k6-2",
2091   "k6-3",
2092   "athlon",
2093   "athlon-4",
2094   "k8",
2095   "amdfam10"
2096 };
2097 \f
2098 /* Implement TARGET_HANDLE_OPTION.  */
2099
2100 static bool
2101 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
2102 {
2103   switch (code)
2104     {
2105     case OPT_mmmx:
2106       if (value)
2107         {
2108           ix86_isa_flags |= OPTION_MASK_ISA_MMX_SET;
2109           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_SET;
2110         }
2111       else
2112         {
2113           ix86_isa_flags &= ~OPTION_MASK_ISA_MMX_UNSET;
2114           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_UNSET;
2115         }
2116       return true;
2117
2118     case OPT_m3dnow:
2119       if (value)
2120         {
2121           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_SET;
2122           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_SET;
2123         }
2124       else
2125         {
2126           ix86_isa_flags &= ~OPTION_MASK_ISA_3DNOW_UNSET;
2127           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_UNSET;
2128         }
2129       return true;
2130
2131     case OPT_m3dnowa:
2132       return false;
2133
2134     case OPT_msse:
2135       if (value)
2136         {
2137           ix86_isa_flags |= OPTION_MASK_ISA_SSE_SET;
2138           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_SET;
2139         }
2140       else
2141         {
2142           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE_UNSET;
2143           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_UNSET;
2144         }
2145       return true;
2146
2147     case OPT_msse2:
2148       if (value)
2149         {
2150           ix86_isa_flags |= OPTION_MASK_ISA_SSE2_SET;
2151           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_SET;
2152         }
2153       else
2154         {
2155           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE2_UNSET;
2156           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_UNSET;
2157         }
2158       return true;
2159
2160     case OPT_msse3:
2161       if (value)
2162         {
2163           ix86_isa_flags |= OPTION_MASK_ISA_SSE3_SET;
2164           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_SET;
2165         }
2166       else
2167         {
2168           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE3_UNSET;
2169           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_UNSET;
2170         }
2171       return true;
2172
2173     case OPT_mssse3:
2174       if (value)
2175         {
2176           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3_SET;
2177           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_SET;
2178         }
2179       else
2180         {
2181           ix86_isa_flags &= ~OPTION_MASK_ISA_SSSE3_UNSET;
2182           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_UNSET;
2183         }
2184       return true;
2185
2186     case OPT_msse4_1:
2187       if (value)
2188         {
2189           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1_SET;
2190           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_SET;
2191         }
2192       else
2193         {
2194           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_1_UNSET;
2195           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_UNSET;
2196         }
2197       return true;
2198
2199     case OPT_msse4_2:
2200       if (value)
2201         {
2202           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2_SET;
2203           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_SET;
2204         }
2205       else
2206         {
2207           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_2_UNSET;
2208           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_UNSET;
2209         }
2210       return true;
2211
2212     case OPT_mavx:
2213       if (value)
2214         {
2215           ix86_isa_flags |= OPTION_MASK_ISA_AVX_SET;
2216           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_SET;
2217         }
2218       else
2219         {
2220           ix86_isa_flags &= ~OPTION_MASK_ISA_AVX_UNSET;
2221           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_UNSET;
2222         }
2223       return true;
2224
2225     case OPT_mfma:
2226       if (value)
2227         {
2228           ix86_isa_flags |= OPTION_MASK_ISA_FMA_SET;
2229           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_SET;
2230         }
2231       else
2232         {
2233           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA_UNSET;
2234           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_UNSET;
2235         }
2236       return true;
2237
2238     case OPT_msse4:
2239       ix86_isa_flags |= OPTION_MASK_ISA_SSE4_SET;
2240       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_SET;
2241       return true;
2242
2243     case OPT_mno_sse4:
2244       ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_UNSET;
2245       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_UNSET;
2246       return true;
2247
2248     case OPT_msse4a:
2249       if (value)
2250         {
2251           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A_SET;
2252           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_SET;
2253         }
2254       else
2255         {
2256           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4A_UNSET;
2257           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_UNSET;
2258         }
2259       return true;
2260
2261     case OPT_mfma4:
2262       if (value)
2263         {
2264           ix86_isa_flags |= OPTION_MASK_ISA_FMA4_SET;
2265           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_SET;
2266         }
2267       else
2268         {
2269           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA4_UNSET;
2270           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_UNSET;
2271         }
2272       return true;
2273
2274    case OPT_mxop:
2275       if (value)
2276         {
2277           ix86_isa_flags |= OPTION_MASK_ISA_XOP_SET;
2278           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_SET;
2279         }
2280       else
2281         {
2282           ix86_isa_flags &= ~OPTION_MASK_ISA_XOP_UNSET;
2283           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_UNSET;
2284         }
2285       return true;
2286
2287    case OPT_mlwp:
2288       if (value)
2289         {
2290           ix86_isa_flags |= OPTION_MASK_ISA_LWP_SET;
2291           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_SET;
2292         }
2293       else
2294         {
2295           ix86_isa_flags &= ~OPTION_MASK_ISA_LWP_UNSET;
2296           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_UNSET;
2297         }
2298       return true;
2299
2300     case OPT_mabm:
2301       if (value)
2302         {
2303           ix86_isa_flags |= OPTION_MASK_ISA_ABM_SET;
2304           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_SET;
2305         }
2306       else
2307         {
2308           ix86_isa_flags &= ~OPTION_MASK_ISA_ABM_UNSET;
2309           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_UNSET;
2310         }
2311       return true;
2312
2313     case OPT_mpopcnt:
2314       if (value)
2315         {
2316           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT_SET;
2317           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_SET;
2318         }
2319       else
2320         {
2321           ix86_isa_flags &= ~OPTION_MASK_ISA_POPCNT_UNSET;
2322           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_UNSET;
2323         }
2324       return true;
2325
2326     case OPT_msahf:
2327       if (value)
2328         {
2329           ix86_isa_flags |= OPTION_MASK_ISA_SAHF_SET;
2330           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_SET;
2331         }
2332       else
2333         {
2334           ix86_isa_flags &= ~OPTION_MASK_ISA_SAHF_UNSET;
2335           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_UNSET;
2336         }
2337       return true;
2338
2339     case OPT_mcx16:
2340       if (value)
2341         {
2342           ix86_isa_flags |= OPTION_MASK_ISA_CX16_SET;
2343           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_SET;
2344         }
2345       else
2346         {
2347           ix86_isa_flags &= ~OPTION_MASK_ISA_CX16_UNSET;
2348           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_UNSET;
2349         }
2350       return true;
2351
2352     case OPT_mmovbe:
2353       if (value)
2354         {
2355           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE_SET;
2356           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_SET;
2357         }
2358       else
2359         {
2360           ix86_isa_flags &= ~OPTION_MASK_ISA_MOVBE_UNSET;
2361           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_UNSET;
2362         }
2363       return true;
2364
2365     case OPT_mcrc32:
2366       if (value)
2367         {
2368           ix86_isa_flags |= OPTION_MASK_ISA_CRC32_SET;
2369           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_SET;
2370         }
2371       else
2372         {
2373           ix86_isa_flags &= ~OPTION_MASK_ISA_CRC32_UNSET;
2374           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_UNSET;
2375         }
2376       return true;
2377
2378     case OPT_maes:
2379       if (value)
2380         {
2381           ix86_isa_flags |= OPTION_MASK_ISA_AES_SET;
2382           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_SET;
2383         }
2384       else
2385         {
2386           ix86_isa_flags &= ~OPTION_MASK_ISA_AES_UNSET;
2387           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_UNSET;
2388         }
2389       return true;
2390
2391     case OPT_mpclmul:
2392       if (value)
2393         {
2394           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL_SET;
2395           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_SET;
2396         }
2397       else
2398         {
2399           ix86_isa_flags &= ~OPTION_MASK_ISA_PCLMUL_UNSET;
2400           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_UNSET;
2401         }
2402       return true;
2403
2404     default:
2405       return true;
2406     }
2407 }
2408 \f
2409 /* Return a string that documents the current -m options.  The caller is
2410    responsible for freeing the string.  */
2411
2412 static char *
2413 ix86_target_string (int isa, int flags, const char *arch, const char *tune,
2414                     const char *fpmath, bool add_nl_p)
2415 {
2416   struct ix86_target_opts
2417   {
2418     const char *option;         /* option string */
2419     int mask;                   /* isa mask options */
2420   };
2421
2422   /* This table is ordered so that options like -msse4.2 that imply
2423      preceding options while match those first.  */
2424   static struct ix86_target_opts isa_opts[] =
2425   {
2426     { "-m64",           OPTION_MASK_ISA_64BIT },
2427     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2428     { "-mfma",          OPTION_MASK_ISA_FMA },
2429     { "-mxop",          OPTION_MASK_ISA_XOP },
2430     { "-mlwp",          OPTION_MASK_ISA_LWP },
2431     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2432     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2433     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2434     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2435     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2436     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2437     { "-msse",          OPTION_MASK_ISA_SSE },
2438     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2439     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2440     { "-mmmx",          OPTION_MASK_ISA_MMX },
2441     { "-mabm",          OPTION_MASK_ISA_ABM },
2442     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2443     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2444     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2445     { "-maes",          OPTION_MASK_ISA_AES },
2446     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2447   };
2448
2449   /* Flag options.  */
2450   static struct ix86_target_opts flag_opts[] =
2451   {
2452     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
2453     { "-m80387",                        MASK_80387 },
2454     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
2455     { "-malign-double",                 MASK_ALIGN_DOUBLE },
2456     { "-mcld",                          MASK_CLD },
2457     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
2458     { "-mieee-fp",                      MASK_IEEE_FP },
2459     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
2460     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
2461     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
2462     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
2463     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
2464     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
2465     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
2466     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
2467     { "-mrecip",                        MASK_RECIP },
2468     { "-mrtd",                          MASK_RTD },
2469     { "-msseregparm",                   MASK_SSEREGPARM },
2470     { "-mstack-arg-probe",              MASK_STACK_PROBE },
2471     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
2472   };
2473
2474   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
2475
2476   char isa_other[40];
2477   char target_other[40];
2478   unsigned num = 0;
2479   unsigned i, j;
2480   char *ret;
2481   char *ptr;
2482   size_t len;
2483   size_t line_len;
2484   size_t sep_len;
2485
2486   memset (opts, '\0', sizeof (opts));
2487
2488   /* Add -march= option.  */
2489   if (arch)
2490     {
2491       opts[num][0] = "-march=";
2492       opts[num++][1] = arch;
2493     }
2494
2495   /* Add -mtune= option.  */
2496   if (tune)
2497     {
2498       opts[num][0] = "-mtune=";
2499       opts[num++][1] = tune;
2500     }
2501
2502   /* Pick out the options in isa options.  */
2503   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
2504     {
2505       if ((isa & isa_opts[i].mask) != 0)
2506         {
2507           opts[num++][0] = isa_opts[i].option;
2508           isa &= ~ isa_opts[i].mask;
2509         }
2510     }
2511
2512   if (isa && add_nl_p)
2513     {
2514       opts[num++][0] = isa_other;
2515       sprintf (isa_other, "(other isa: 0x%x)", isa);
2516     }
2517
2518   /* Add flag options.  */
2519   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
2520     {
2521       if ((flags & flag_opts[i].mask) != 0)
2522         {
2523           opts[num++][0] = flag_opts[i].option;
2524           flags &= ~ flag_opts[i].mask;
2525         }
2526     }
2527
2528   if (flags && add_nl_p)
2529     {
2530       opts[num++][0] = target_other;
2531       sprintf (target_other, "(other flags: 0x%x)", isa);
2532     }
2533
2534   /* Add -fpmath= option.  */
2535   if (fpmath)
2536     {
2537       opts[num][0] = "-mfpmath=";
2538       opts[num++][1] = fpmath;
2539     }
2540
2541   /* Any options?  */
2542   if (num == 0)
2543     return NULL;
2544
2545   gcc_assert (num < ARRAY_SIZE (opts));
2546
2547   /* Size the string.  */
2548   len = 0;
2549   sep_len = (add_nl_p) ? 3 : 1;
2550   for (i = 0; i < num; i++)
2551     {
2552       len += sep_len;
2553       for (j = 0; j < 2; j++)
2554         if (opts[i][j])
2555           len += strlen (opts[i][j]);
2556     }
2557
2558   /* Build the string.  */
2559   ret = ptr = (char *) xmalloc (len);
2560   line_len = 0;
2561
2562   for (i = 0; i < num; i++)
2563     {
2564       size_t len2[2];
2565
2566       for (j = 0; j < 2; j++)
2567         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
2568
2569       if (i != 0)
2570         {
2571           *ptr++ = ' ';
2572           line_len++;
2573
2574           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
2575             {
2576               *ptr++ = '\\';
2577               *ptr++ = '\n';
2578               line_len = 0;
2579             }
2580         }
2581
2582       for (j = 0; j < 2; j++)
2583         if (opts[i][j])
2584           {
2585             memcpy (ptr, opts[i][j], len2[j]);
2586             ptr += len2[j];
2587             line_len += len2[j];
2588           }
2589     }
2590
2591   *ptr = '\0';
2592   gcc_assert (ret + len >= ptr);
2593
2594   return ret;
2595 }
2596
2597 /* Function that is callable from the debugger to print the current
2598    options.  */
2599 void
2600 ix86_debug_options (void)
2601 {
2602   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
2603                                    ix86_arch_string, ix86_tune_string,
2604                                    ix86_fpmath_string, true);
2605
2606   if (opts)
2607     {
2608       fprintf (stderr, "%s\n\n", opts);
2609       free (opts);
2610     }
2611   else
2612     fputs ("<no options>\n\n", stderr);
2613
2614   return;
2615 }
2616 \f
2617 /* Sometimes certain combinations of command options do not make
2618    sense on a particular target machine.  You can define a macro
2619    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
2620    defined, is executed once just after all the command options have
2621    been parsed.
2622
2623    Don't use this macro to turn on various extra optimizations for
2624    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
2625
2626 void
2627 override_options (bool main_args_p)
2628 {
2629   int i;
2630   unsigned int ix86_arch_mask, ix86_tune_mask;
2631   const bool ix86_tune_specified = (ix86_tune_string != NULL); 
2632   const char *prefix;
2633   const char *suffix;
2634   const char *sw;
2635
2636   /* Comes from final.c -- no real reason to change it.  */
2637 #define MAX_CODE_ALIGN 16
2638
2639   enum pta_flags
2640     {
2641       PTA_SSE = 1 << 0,
2642       PTA_SSE2 = 1 << 1,
2643       PTA_SSE3 = 1 << 2,
2644       PTA_MMX = 1 << 3,
2645       PTA_PREFETCH_SSE = 1 << 4,
2646       PTA_3DNOW = 1 << 5,
2647       PTA_3DNOW_A = 1 << 6,
2648       PTA_64BIT = 1 << 7,
2649       PTA_SSSE3 = 1 << 8,
2650       PTA_CX16 = 1 << 9,
2651       PTA_POPCNT = 1 << 10,
2652       PTA_ABM = 1 << 11,
2653       PTA_SSE4A = 1 << 12,
2654       PTA_NO_SAHF = 1 << 13,
2655       PTA_SSE4_1 = 1 << 14,
2656       PTA_SSE4_2 = 1 << 15,
2657       PTA_AES = 1 << 16,
2658       PTA_PCLMUL = 1 << 17,
2659       PTA_AVX = 1 << 18,
2660       PTA_FMA = 1 << 19,
2661       PTA_MOVBE = 1 << 20,
2662       PTA_FMA4 = 1 << 21,
2663       PTA_XOP = 1 << 22,
2664       PTA_LWP = 1 << 23
2665     };
2666
2667   static struct pta
2668     {
2669       const char *const name;           /* processor name or nickname.  */
2670       const enum processor_type processor;
2671       const enum attr_cpu schedule;
2672       const unsigned /*enum pta_flags*/ flags;
2673     }
2674   const processor_alias_table[] =
2675     {
2676       {"i386", PROCESSOR_I386, CPU_NONE, 0},
2677       {"i486", PROCESSOR_I486, CPU_NONE, 0},
2678       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2679       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2680       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
2681       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
2682       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2683       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2684       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
2685       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2686       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2687       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
2688       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2689         PTA_MMX | PTA_SSE},
2690       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2691         PTA_MMX | PTA_SSE},
2692       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2693         PTA_MMX | PTA_SSE | PTA_SSE2},
2694       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
2695         PTA_MMX |PTA_SSE | PTA_SSE2},
2696       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
2697         PTA_MMX | PTA_SSE | PTA_SSE2},
2698       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
2699         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
2700       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
2701         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2702         | PTA_CX16 | PTA_NO_SAHF},
2703       {"core2", PROCESSOR_CORE2, CPU_CORE2,
2704         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2705         | PTA_SSSE3 | PTA_CX16},
2706       {"atom", PROCESSOR_ATOM, CPU_ATOM,
2707         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2708         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
2709       {"geode", PROCESSOR_GEODE, CPU_GEODE,
2710         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A |PTA_PREFETCH_SSE},
2711       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
2712       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
2713       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
2714       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
2715         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
2716       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
2717         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
2718       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
2719         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2720       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
2721         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2722       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
2723         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2724       {"x86-64", PROCESSOR_K8, CPU_K8,
2725         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
2726       {"k8", PROCESSOR_K8, CPU_K8,
2727         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2728         | PTA_SSE2 | PTA_NO_SAHF},
2729       {"k8-sse3", PROCESSOR_K8, CPU_K8,
2730         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2731         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2732       {"opteron", PROCESSOR_K8, CPU_K8,
2733         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2734         | PTA_SSE2 | PTA_NO_SAHF},
2735       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
2736         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2737         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2738       {"athlon64", PROCESSOR_K8, CPU_K8,
2739         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2740         | PTA_SSE2 | PTA_NO_SAHF},
2741       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
2742         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2743         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2744       {"athlon-fx", PROCESSOR_K8, CPU_K8,
2745         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2746         | PTA_SSE2 | PTA_NO_SAHF},
2747       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
2748         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2749         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
2750       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
2751         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2752         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
2753       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
2754         0 /* flags are only used for -march switch.  */ },
2755       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
2756         PTA_64BIT /* flags are only used for -march switch.  */ },
2757     };
2758
2759   int const pta_size = ARRAY_SIZE (processor_alias_table);
2760
2761   /* Set up prefix/suffix so the error messages refer to either the command
2762      line argument, or the attribute(target).  */
2763   if (main_args_p)
2764     {
2765       prefix = "-m";
2766       suffix = "";
2767       sw = "switch";
2768     }
2769   else
2770     {
2771       prefix = "option(\"";
2772       suffix = "\")";
2773       sw = "attribute";
2774     }
2775
2776 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2777   SUBTARGET_OVERRIDE_OPTIONS;
2778 #endif
2779
2780 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2781   SUBSUBTARGET_OVERRIDE_OPTIONS;
2782 #endif
2783
2784   /* -fPIC is the default for x86_64.  */
2785   if (TARGET_MACHO && TARGET_64BIT)
2786     flag_pic = 2;
2787
2788   /* Set the default values for switches whose default depends on TARGET_64BIT
2789      in case they weren't overwritten by command line options.  */
2790   if (TARGET_64BIT)
2791     {
2792       /* Mach-O doesn't support omitting the frame pointer for now.  */
2793       if (flag_omit_frame_pointer == 2)
2794         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
2795       if (flag_asynchronous_unwind_tables == 2)
2796         flag_asynchronous_unwind_tables = 1;
2797       if (flag_pcc_struct_return == 2)
2798         flag_pcc_struct_return = 0;
2799     }
2800   else
2801     {
2802       if (flag_omit_frame_pointer == 2)
2803         flag_omit_frame_pointer = 0;
2804       if (flag_asynchronous_unwind_tables == 2)
2805         flag_asynchronous_unwind_tables = 0;
2806       if (flag_pcc_struct_return == 2)
2807         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
2808     }
2809
2810   /* Need to check -mtune=generic first.  */
2811   if (ix86_tune_string)
2812     {
2813       if (!strcmp (ix86_tune_string, "generic")
2814           || !strcmp (ix86_tune_string, "i686")
2815           /* As special support for cross compilers we read -mtune=native
2816              as -mtune=generic.  With native compilers we won't see the
2817              -mtune=native, as it was changed by the driver.  */
2818           || !strcmp (ix86_tune_string, "native"))
2819         {
2820           if (TARGET_64BIT)
2821             ix86_tune_string = "generic64";
2822           else
2823             ix86_tune_string = "generic32";
2824         }
2825       /* If this call is for setting the option attribute, allow the
2826          generic32/generic64 that was previously set.  */
2827       else if (!main_args_p
2828                && (!strcmp (ix86_tune_string, "generic32")
2829                    || !strcmp (ix86_tune_string, "generic64")))
2830         ;
2831       else if (!strncmp (ix86_tune_string, "generic", 7))
2832         error ("bad value (%s) for %stune=%s %s",
2833                ix86_tune_string, prefix, suffix, sw);
2834       else if (!strcmp (ix86_tune_string, "x86-64"))
2835         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated.  Use "
2836                  "%stune=k8%s or %stune=generic%s instead as appropriate.",
2837                  prefix, suffix, prefix, suffix, prefix, suffix);
2838     }
2839   else
2840     {
2841       if (ix86_arch_string)
2842         ix86_tune_string = ix86_arch_string;
2843       if (!ix86_tune_string)
2844         {
2845           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
2846           ix86_tune_defaulted = 1;
2847         }
2848
2849       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
2850          need to use a sensible tune option.  */
2851       if (!strcmp (ix86_tune_string, "generic")
2852           || !strcmp (ix86_tune_string, "x86-64")
2853           || !strcmp (ix86_tune_string, "i686"))
2854         {
2855           if (TARGET_64BIT)
2856             ix86_tune_string = "generic64";
2857           else
2858             ix86_tune_string = "generic32";
2859         }
2860     }
2861
2862   if (ix86_stringop_string)
2863     {
2864       if (!strcmp (ix86_stringop_string, "rep_byte"))
2865         stringop_alg = rep_prefix_1_byte;
2866       else if (!strcmp (ix86_stringop_string, "libcall"))
2867         stringop_alg = libcall;
2868       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
2869         stringop_alg = rep_prefix_4_byte;
2870       else if (!strcmp (ix86_stringop_string, "rep_8byte")
2871                && TARGET_64BIT)
2872         /* rep; movq isn't available in 32-bit code.  */
2873         stringop_alg = rep_prefix_8_byte;
2874       else if (!strcmp (ix86_stringop_string, "byte_loop"))
2875         stringop_alg = loop_1_byte;
2876       else if (!strcmp (ix86_stringop_string, "loop"))
2877         stringop_alg = loop;
2878       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
2879         stringop_alg = unrolled_loop;
2880       else
2881         error ("bad value (%s) for %sstringop-strategy=%s %s",
2882                ix86_stringop_string, prefix, suffix, sw);
2883     }
2884
2885   if (!ix86_arch_string)
2886     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
2887   else
2888     ix86_arch_specified = 1;
2889
2890   /* Validate -mabi= value.  */
2891   if (ix86_abi_string)
2892     {
2893       if (strcmp (ix86_abi_string, "sysv") == 0)
2894         ix86_abi = SYSV_ABI;
2895       else if (strcmp (ix86_abi_string, "ms") == 0)
2896         ix86_abi = MS_ABI;
2897       else
2898         error ("unknown ABI (%s) for %sabi=%s %s",
2899                ix86_abi_string, prefix, suffix, sw);
2900     }
2901   else
2902     ix86_abi = DEFAULT_ABI;
2903
2904   if (ix86_cmodel_string != 0)
2905     {
2906       if (!strcmp (ix86_cmodel_string, "small"))
2907         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
2908       else if (!strcmp (ix86_cmodel_string, "medium"))
2909         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
2910       else if (!strcmp (ix86_cmodel_string, "large"))
2911         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
2912       else if (flag_pic)
2913         error ("code model %s does not support PIC mode", ix86_cmodel_string);
2914       else if (!strcmp (ix86_cmodel_string, "32"))
2915         ix86_cmodel = CM_32;
2916       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
2917         ix86_cmodel = CM_KERNEL;
2918       else
2919         error ("bad value (%s) for %scmodel=%s %s",
2920                ix86_cmodel_string, prefix, suffix, sw);
2921     }
2922   else
2923     {
2924       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
2925          use of rip-relative addressing.  This eliminates fixups that
2926          would otherwise be needed if this object is to be placed in a
2927          DLL, and is essentially just as efficient as direct addressing.  */
2928       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
2929         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
2930       else if (TARGET_64BIT)
2931         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
2932       else
2933         ix86_cmodel = CM_32;
2934     }
2935   if (ix86_asm_string != 0)
2936     {
2937       if (! TARGET_MACHO
2938           && !strcmp (ix86_asm_string, "intel"))
2939         ix86_asm_dialect = ASM_INTEL;
2940       else if (!strcmp (ix86_asm_string, "att"))
2941         ix86_asm_dialect = ASM_ATT;
2942       else
2943         error ("bad value (%s) for %sasm=%s %s",
2944                ix86_asm_string, prefix, suffix, sw);
2945     }
2946   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
2947     error ("code model %qs not supported in the %s bit mode",
2948            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
2949   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
2950     sorry ("%i-bit mode not compiled in",
2951            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
2952
2953   for (i = 0; i < pta_size; i++)
2954     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
2955       {
2956         ix86_schedule = processor_alias_table[i].schedule;
2957         ix86_arch = processor_alias_table[i].processor;
2958         /* Default cpu tuning to the architecture.  */
2959         ix86_tune = ix86_arch;
2960
2961         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
2962           error ("CPU you selected does not support x86-64 "
2963                  "instruction set");
2964
2965         if (processor_alias_table[i].flags & PTA_MMX
2966             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
2967           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
2968         if (processor_alias_table[i].flags & PTA_3DNOW
2969             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
2970           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
2971         if (processor_alias_table[i].flags & PTA_3DNOW_A
2972             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
2973           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
2974         if (processor_alias_table[i].flags & PTA_SSE
2975             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
2976           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
2977         if (processor_alias_table[i].flags & PTA_SSE2
2978             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
2979           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
2980         if (processor_alias_table[i].flags & PTA_SSE3
2981             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
2982           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
2983         if (processor_alias_table[i].flags & PTA_SSSE3
2984             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
2985           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
2986         if (processor_alias_table[i].flags & PTA_SSE4_1
2987             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
2988           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
2989         if (processor_alias_table[i].flags & PTA_SSE4_2
2990             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
2991           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
2992         if (processor_alias_table[i].flags & PTA_AVX
2993             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
2994           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
2995         if (processor_alias_table[i].flags & PTA_FMA
2996             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
2997           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
2998         if (processor_alias_table[i].flags & PTA_SSE4A
2999             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3000           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3001         if (processor_alias_table[i].flags & PTA_FMA4
3002             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3003           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3004         if (processor_alias_table[i].flags & PTA_XOP
3005             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3006           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3007         if (processor_alias_table[i].flags & PTA_LWP
3008             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3009           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3010         if (processor_alias_table[i].flags & PTA_ABM
3011             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3012           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3013         if (processor_alias_table[i].flags & PTA_CX16
3014             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3015           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3016         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3017             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3018           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3019         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3020             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3021           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3022         if (processor_alias_table[i].flags & PTA_MOVBE
3023             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3024           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3025         if (processor_alias_table[i].flags & PTA_AES
3026             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3027           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3028         if (processor_alias_table[i].flags & PTA_PCLMUL
3029             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3030           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3031         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3032           x86_prefetch_sse = true;
3033
3034         break;
3035       }
3036
3037   if (!strcmp (ix86_arch_string, "generic"))
3038     error ("generic CPU can be used only for %stune=%s %s",
3039            prefix, suffix, sw);
3040   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3041     error ("bad value (%s) for %sarch=%s %s",
3042            ix86_arch_string, prefix, suffix, sw);
3043
3044   ix86_arch_mask = 1u << ix86_arch;
3045   for (i = 0; i < X86_ARCH_LAST; ++i)
3046     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3047
3048   for (i = 0; i < pta_size; i++)
3049     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3050       {
3051         ix86_schedule = processor_alias_table[i].schedule;
3052         ix86_tune = processor_alias_table[i].processor;
3053         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3054           {
3055             if (ix86_tune_defaulted)
3056               {
3057                 ix86_tune_string = "x86-64";
3058                 for (i = 0; i < pta_size; i++)
3059                   if (! strcmp (ix86_tune_string,
3060                                 processor_alias_table[i].name))
3061                     break;
3062                 ix86_schedule = processor_alias_table[i].schedule;
3063                 ix86_tune = processor_alias_table[i].processor;
3064               }
3065             else
3066               error ("CPU you selected does not support x86-64 "
3067                      "instruction set");
3068           }
3069         /* Intel CPUs have always interpreted SSE prefetch instructions as
3070            NOPs; so, we can enable SSE prefetch instructions even when
3071            -mtune (rather than -march) points us to a processor that has them.
3072            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3073            higher processors.  */
3074         if (TARGET_CMOVE
3075             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3076           x86_prefetch_sse = true;
3077         break;
3078       }
3079
3080   if (ix86_tune_specified && i == pta_size)
3081     error ("bad value (%s) for %stune=%s %s",
3082            ix86_tune_string, prefix, suffix, sw);
3083
3084   ix86_tune_mask = 1u << ix86_tune;
3085   for (i = 0; i < X86_TUNE_LAST; ++i)
3086     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3087
3088   if (optimize_size)
3089     ix86_cost = &ix86_size_cost;
3090   else
3091     ix86_cost = processor_target_table[ix86_tune].cost;
3092
3093   /* Arrange to set up i386_stack_locals for all functions.  */
3094   init_machine_status = ix86_init_machine_status;
3095
3096   /* Validate -mregparm= value.  */
3097   if (ix86_regparm_string)
3098     {
3099       if (TARGET_64BIT)
3100         warning (0, "%sregparm%s is ignored in 64-bit mode", prefix, suffix);
3101       i = atoi (ix86_regparm_string);
3102       if (i < 0 || i > REGPARM_MAX)
3103         error ("%sregparm=%d%s is not between 0 and %d",
3104                prefix, i, suffix, REGPARM_MAX);
3105       else
3106         ix86_regparm = i;
3107     }
3108   if (TARGET_64BIT)
3109     ix86_regparm = REGPARM_MAX;
3110
3111   /* If the user has provided any of the -malign-* options,
3112      warn and use that value only if -falign-* is not set.
3113      Remove this code in GCC 3.2 or later.  */
3114   if (ix86_align_loops_string)
3115     {
3116       warning (0, "%salign-loops%s is obsolete, use -falign-loops%s",
3117                prefix, suffix, suffix);
3118       if (align_loops == 0)
3119         {
3120           i = atoi (ix86_align_loops_string);
3121           if (i < 0 || i > MAX_CODE_ALIGN)
3122             error ("%salign-loops=%d%s is not between 0 and %d",
3123                    prefix, i, suffix, MAX_CODE_ALIGN);
3124           else
3125             align_loops = 1 << i;
3126         }
3127     }
3128
3129   if (ix86_align_jumps_string)
3130     {
3131       warning (0, "%salign-jumps%s is obsolete, use -falign-jumps%s",
3132                prefix, suffix, suffix);
3133       if (align_jumps == 0)
3134         {
3135           i = atoi (ix86_align_jumps_string);
3136           if (i < 0 || i > MAX_CODE_ALIGN)
3137             error ("%salign-loops=%d%s is not between 0 and %d",
3138                    prefix, i, suffix, MAX_CODE_ALIGN);
3139           else
3140             align_jumps = 1 << i;
3141         }
3142     }
3143
3144   if (ix86_align_funcs_string)
3145     {
3146       warning (0, "%salign-functions%s is obsolete, use -falign-functions%s",
3147                prefix, suffix, suffix);
3148       if (align_functions == 0)
3149         {
3150           i = atoi (ix86_align_funcs_string);
3151           if (i < 0 || i > MAX_CODE_ALIGN)
3152             error ("%salign-loops=%d%s is not between 0 and %d",
3153                    prefix, i, suffix, MAX_CODE_ALIGN);
3154           else
3155             align_functions = 1 << i;
3156         }
3157     }
3158
3159   /* Default align_* from the processor table.  */
3160   if (align_loops == 0)
3161     {
3162       align_loops = processor_target_table[ix86_tune].align_loop;
3163       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3164     }
3165   if (align_jumps == 0)
3166     {
3167       align_jumps = processor_target_table[ix86_tune].align_jump;
3168       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3169     }
3170   if (align_functions == 0)
3171     {
3172       align_functions = processor_target_table[ix86_tune].align_func;
3173     }
3174
3175   /* Validate -mbranch-cost= value, or provide default.  */
3176   ix86_branch_cost = ix86_cost->branch_cost;
3177   if (ix86_branch_cost_string)
3178     {
3179       i = atoi (ix86_branch_cost_string);
3180       if (i < 0 || i > 5)
3181         error ("%sbranch-cost=%d%s is not between 0 and 5", prefix, i, suffix);
3182       else
3183         ix86_branch_cost = i;
3184     }
3185   if (ix86_section_threshold_string)
3186     {
3187       i = atoi (ix86_section_threshold_string);
3188       if (i < 0)
3189         error ("%slarge-data-threshold=%d%s is negative", prefix, i, suffix);
3190       else
3191         ix86_section_threshold = i;
3192     }
3193
3194   if (ix86_tls_dialect_string)
3195     {
3196       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
3197         ix86_tls_dialect = TLS_DIALECT_GNU;
3198       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
3199         ix86_tls_dialect = TLS_DIALECT_GNU2;
3200       else
3201         error ("bad value (%s) for %stls-dialect=%s %s",
3202                ix86_tls_dialect_string, prefix, suffix, sw);
3203     }
3204
3205   if (ix87_precision_string)
3206     {
3207       i = atoi (ix87_precision_string);
3208       if (i != 32 && i != 64 && i != 80)
3209         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
3210     }
3211
3212   if (TARGET_64BIT)
3213     {
3214       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3215
3216       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3217          explicitly disable any of these.  In particular, disabling SSE and
3218          MMX for kernel code is extremely useful.  */
3219       if (!ix86_arch_specified)
3220       ix86_isa_flags
3221         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3222              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3223
3224       if (TARGET_RTD)
3225         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3226     }
3227   else
3228     {
3229       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3230
3231       if (!ix86_arch_specified)
3232       ix86_isa_flags
3233         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3234
3235       /* i386 ABI does not specify red zone.  It still makes sense to use it
3236          when programmer takes care to stack from being destroyed.  */
3237       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3238         target_flags |= MASK_NO_RED_ZONE;
3239     }
3240
3241   /* Keep nonleaf frame pointers.  */
3242   if (flag_omit_frame_pointer)
3243     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3244   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3245     flag_omit_frame_pointer = 1;
3246
3247   /* If we're doing fast math, we don't care about comparison order
3248      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3249   if (flag_finite_math_only)
3250     target_flags &= ~MASK_IEEE_FP;
3251
3252   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3253      since the insns won't need emulation.  */
3254   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3255     target_flags &= ~MASK_NO_FANCY_MATH_387;
3256
3257   /* Likewise, if the target doesn't have a 387, or we've specified
3258      software floating point, don't use 387 inline intrinsics.  */
3259   if (!TARGET_80387)
3260     target_flags |= MASK_NO_FANCY_MATH_387;
3261
3262   /* Turn on MMX builtins for -msse.  */
3263   if (TARGET_SSE)
3264     {
3265       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3266       x86_prefetch_sse = true;
3267     }
3268
3269   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3270   if (TARGET_SSE4_2 || TARGET_ABM)
3271     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3272
3273   /* Validate -mpreferred-stack-boundary= value or default it to
3274      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3275   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3276   if (ix86_preferred_stack_boundary_string)
3277     {
3278       i = atoi (ix86_preferred_stack_boundary_string);
3279       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3280         error ("%spreferred-stack-boundary=%d%s is not between %d and 12",
3281                prefix, i, suffix, TARGET_64BIT ? 4 : 2);
3282       else
3283         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
3284     }
3285
3286   /* Set the default value for -mstackrealign.  */
3287   if (ix86_force_align_arg_pointer == -1)
3288     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3289
3290   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3291
3292   /* Validate -mincoming-stack-boundary= value or default it to
3293      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3294   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3295   if (ix86_incoming_stack_boundary_string)
3296     {
3297       i = atoi (ix86_incoming_stack_boundary_string);
3298       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3299         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3300                i, TARGET_64BIT ? 4 : 2);
3301       else
3302         {
3303           ix86_user_incoming_stack_boundary = (1 << i) * BITS_PER_UNIT;
3304           ix86_incoming_stack_boundary
3305             = ix86_user_incoming_stack_boundary;
3306         }
3307     }
3308
3309   /* Accept -msseregparm only if at least SSE support is enabled.  */
3310   if (TARGET_SSEREGPARM
3311       && ! TARGET_SSE)
3312     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3313
3314   ix86_fpmath = TARGET_FPMATH_DEFAULT;
3315   if (ix86_fpmath_string != 0)
3316     {
3317       if (! strcmp (ix86_fpmath_string, "387"))
3318         ix86_fpmath = FPMATH_387;
3319       else if (! strcmp (ix86_fpmath_string, "sse"))
3320         {
3321           if (!TARGET_SSE)
3322             {
3323               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3324               ix86_fpmath = FPMATH_387;
3325             }
3326           else
3327             ix86_fpmath = FPMATH_SSE;
3328         }
3329       else if (! strcmp (ix86_fpmath_string, "387,sse")
3330                || ! strcmp (ix86_fpmath_string, "387+sse")
3331                || ! strcmp (ix86_fpmath_string, "sse,387")
3332                || ! strcmp (ix86_fpmath_string, "sse+387")
3333                || ! strcmp (ix86_fpmath_string, "both"))
3334         {
3335           if (!TARGET_SSE)
3336             {
3337               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3338               ix86_fpmath = FPMATH_387;
3339             }
3340           else if (!TARGET_80387)
3341             {
3342               warning (0, "387 instruction set disabled, using SSE arithmetics");
3343               ix86_fpmath = FPMATH_SSE;
3344             }
3345           else
3346             ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
3347         }
3348       else
3349         error ("bad value (%s) for %sfpmath=%s %s",
3350                ix86_fpmath_string, prefix, suffix, sw);
3351     }
3352
3353   /* If the i387 is disabled, then do not return values in it. */
3354   if (!TARGET_80387)
3355     target_flags &= ~MASK_FLOAT_RETURNS;
3356
3357   /* Use external vectorized library in vectorizing intrinsics.  */
3358   if (ix86_veclibabi_string)
3359     {
3360       if (strcmp (ix86_veclibabi_string, "svml") == 0)
3361         ix86_veclib_handler = ix86_veclibabi_svml;
3362       else if (strcmp (ix86_veclibabi_string, "acml") == 0)
3363         ix86_veclib_handler = ix86_veclibabi_acml;
3364       else
3365         error ("unknown vectorization library ABI type (%s) for "
3366                "%sveclibabi=%s %s", ix86_veclibabi_string,
3367                prefix, suffix, sw);
3368     }
3369
3370   if ((x86_accumulate_outgoing_args & ix86_tune_mask)
3371       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3372       && !optimize_size)
3373     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3374
3375   /* ??? Unwind info is not correct around the CFG unless either a frame
3376      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
3377      unwind info generation to be aware of the CFG and propagating states
3378      around edges.  */
3379   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
3380        || flag_exceptions || flag_non_call_exceptions)
3381       && flag_omit_frame_pointer
3382       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3383     {
3384       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3385         warning (0, "unwind tables currently require either a frame pointer "
3386                  "or %saccumulate-outgoing-args%s for correctness",
3387                  prefix, suffix);
3388       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3389     }
3390
3391   /* If stack probes are required, the space used for large function
3392      arguments on the stack must also be probed, so enable
3393      -maccumulate-outgoing-args so this happens in the prologue.  */
3394   if (TARGET_STACK_PROBE
3395       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3396     {
3397       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3398         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
3399                  "for correctness", prefix, suffix);
3400       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3401     }
3402
3403   /* For sane SSE instruction set generation we need fcomi instruction.
3404      It is safe to enable all CMOVE instructions.  */
3405   if (TARGET_SSE)
3406     TARGET_CMOVE = 1;
3407
3408   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
3409   {
3410     char *p;
3411     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
3412     p = strchr (internal_label_prefix, 'X');
3413     internal_label_prefix_len = p - internal_label_prefix;
3414     *p = '\0';
3415   }
3416
3417   /* When scheduling description is not available, disable scheduler pass
3418      so it won't slow down the compilation and make x87 code slower.  */
3419   if (!TARGET_SCHEDULE)
3420     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
3421
3422   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
3423     set_param_value ("simultaneous-prefetches",
3424                      ix86_cost->simultaneous_prefetches);
3425   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
3426     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
3427   if (!PARAM_SET_P (PARAM_L1_CACHE_SIZE))
3428     set_param_value ("l1-cache-size", ix86_cost->l1_cache_size);
3429   if (!PARAM_SET_P (PARAM_L2_CACHE_SIZE))
3430     set_param_value ("l2-cache-size", ix86_cost->l2_cache_size);
3431
3432   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3433      can be optimized to ap = __builtin_next_arg (0).  */
3434   if (!TARGET_64BIT)
3435     targetm.expand_builtin_va_start = NULL;
3436
3437   if (TARGET_64BIT)
3438     {
3439       ix86_gen_leave = gen_leave_rex64;
3440       ix86_gen_pop1 = gen_popdi1;
3441       ix86_gen_add3 = gen_adddi3;
3442       ix86_gen_sub3 = gen_subdi3;
3443       ix86_gen_sub3_carry = gen_subdi3_carry;
3444       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
3445       ix86_gen_monitor = gen_sse3_monitor64;
3446       ix86_gen_andsp = gen_anddi3;
3447     }
3448   else
3449     {
3450       ix86_gen_leave = gen_leave;
3451       ix86_gen_pop1 = gen_popsi1;
3452       ix86_gen_add3 = gen_addsi3;
3453       ix86_gen_sub3 = gen_subsi3;
3454       ix86_gen_sub3_carry = gen_subsi3_carry;
3455       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
3456       ix86_gen_monitor = gen_sse3_monitor;
3457       ix86_gen_andsp = gen_andsi3;
3458     }
3459
3460 #ifdef USE_IX86_CLD
3461   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
3462   if (!TARGET_64BIT)
3463     target_flags |= MASK_CLD & ~target_flags_explicit;
3464 #endif
3465
3466   /* Save the initial options in case the user does function specific options */
3467   if (main_args_p)
3468     target_option_default_node = target_option_current_node
3469       = build_target_option_node ();
3470 }
3471
3472 /* Update register usage after having seen the compiler flags.  */
3473
3474 void
3475 ix86_conditional_register_usage (void)
3476 {
3477   int i;
3478   unsigned int j;
3479
3480   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3481     {
3482       if (fixed_regs[i] > 1)
3483         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
3484       if (call_used_regs[i] > 1)
3485         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
3486     }
3487
3488   /* The PIC register, if it exists, is fixed.  */
3489   j = PIC_OFFSET_TABLE_REGNUM;
3490   if (j != INVALID_REGNUM)
3491     fixed_regs[j] = call_used_regs[j] = 1;
3492
3493   /* The MS_ABI changes the set of call-used registers.  */
3494   if (TARGET_64BIT && ix86_cfun_abi () == MS_ABI)
3495     {
3496       call_used_regs[SI_REG] = 0;
3497       call_used_regs[DI_REG] = 0;
3498       call_used_regs[XMM6_REG] = 0;
3499       call_used_regs[XMM7_REG] = 0;
3500       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3501         call_used_regs[i] = 0;
3502     }
3503
3504   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
3505      other call-clobbered regs for 64-bit.  */
3506   if (TARGET_64BIT)
3507     {
3508       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
3509
3510       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3511         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
3512             && call_used_regs[i])
3513           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
3514     }
3515
3516   /* If MMX is disabled, squash the registers.  */
3517   if (! TARGET_MMX)
3518     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3519       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
3520         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3521
3522   /* If SSE is disabled, squash the registers.  */
3523   if (! TARGET_SSE)
3524     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3525       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
3526         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3527
3528   /* If the FPU is disabled, squash the registers.  */
3529   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
3530     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3531       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
3532         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3533
3534   /* If 32-bit, squash the 64-bit registers.  */
3535   if (! TARGET_64BIT)
3536     {
3537       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
3538         reg_names[i] = "";
3539       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3540         reg_names[i] = "";
3541     }
3542 }
3543
3544 \f
3545 /* Save the current options */
3546
3547 static void
3548 ix86_function_specific_save (struct cl_target_option *ptr)
3549 {
3550   ptr->arch = ix86_arch;
3551   ptr->schedule = ix86_schedule;
3552   ptr->tune = ix86_tune;
3553   ptr->fpmath = ix86_fpmath;
3554   ptr->branch_cost = ix86_branch_cost;
3555   ptr->tune_defaulted = ix86_tune_defaulted;
3556   ptr->arch_specified = ix86_arch_specified;
3557   ptr->ix86_isa_flags_explicit = ix86_isa_flags_explicit;
3558   ptr->target_flags_explicit = target_flags_explicit;
3559
3560   /* The fields are char but the variables are not; make sure the
3561      values fit in the fields.  */
3562   gcc_assert (ptr->arch == ix86_arch);
3563   gcc_assert (ptr->schedule == ix86_schedule);
3564   gcc_assert (ptr->tune == ix86_tune);
3565   gcc_assert (ptr->fpmath == ix86_fpmath);
3566   gcc_assert (ptr->branch_cost == ix86_branch_cost);
3567 }
3568
3569 /* Restore the current options */
3570
3571 static void
3572 ix86_function_specific_restore (struct cl_target_option *ptr)
3573 {
3574   enum processor_type old_tune = ix86_tune;
3575   enum processor_type old_arch = ix86_arch;
3576   unsigned int ix86_arch_mask, ix86_tune_mask;
3577   int i;
3578
3579   ix86_arch = (enum processor_type) ptr->arch;
3580   ix86_schedule = (enum attr_cpu) ptr->schedule;
3581   ix86_tune = (enum processor_type) ptr->tune;
3582   ix86_fpmath = (enum fpmath_unit) ptr->fpmath;
3583   ix86_branch_cost = ptr->branch_cost;
3584   ix86_tune_defaulted = ptr->tune_defaulted;
3585   ix86_arch_specified = ptr->arch_specified;
3586   ix86_isa_flags_explicit = ptr->ix86_isa_flags_explicit;
3587   target_flags_explicit = ptr->target_flags_explicit;
3588
3589   /* Recreate the arch feature tests if the arch changed */
3590   if (old_arch != ix86_arch)
3591     {
3592       ix86_arch_mask = 1u << ix86_arch;
3593       for (i = 0; i < X86_ARCH_LAST; ++i)
3594         ix86_arch_features[i]
3595           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3596     }
3597
3598   /* Recreate the tune optimization tests */
3599   if (old_tune != ix86_tune)
3600     {
3601       ix86_tune_mask = 1u << ix86_tune;
3602       for (i = 0; i < X86_TUNE_LAST; ++i)
3603         ix86_tune_features[i]
3604           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3605     }
3606 }
3607
3608 /* Print the current options */
3609
3610 static void
3611 ix86_function_specific_print (FILE *file, int indent,
3612                               struct cl_target_option *ptr)
3613 {
3614   char *target_string
3615     = ix86_target_string (ptr->ix86_isa_flags, ptr->target_flags,
3616                           NULL, NULL, NULL, false);
3617
3618   fprintf (file, "%*sarch = %d (%s)\n",
3619            indent, "",
3620            ptr->arch,
3621            ((ptr->arch < TARGET_CPU_DEFAULT_max)
3622             ? cpu_names[ptr->arch]
3623             : "<unknown>"));
3624
3625   fprintf (file, "%*stune = %d (%s)\n",
3626            indent, "",
3627            ptr->tune,
3628            ((ptr->tune < TARGET_CPU_DEFAULT_max)
3629             ? cpu_names[ptr->tune]
3630             : "<unknown>"));
3631
3632   fprintf (file, "%*sfpmath = %d%s%s\n", indent, "", ptr->fpmath,
3633            (ptr->fpmath & FPMATH_387) ? ", 387" : "",
3634            (ptr->fpmath & FPMATH_SSE) ? ", sse" : "");
3635   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
3636
3637   if (target_string)
3638     {
3639       fprintf (file, "%*s%s\n", indent, "", target_string);
3640       free (target_string);
3641     }
3642 }
3643
3644 \f
3645 /* Inner function to process the attribute((target(...))), take an argument and
3646    set the current options from the argument. If we have a list, recursively go
3647    over the list.  */
3648
3649 static bool
3650 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[])
3651 {
3652   char *next_optstr;
3653   bool ret = true;
3654
3655 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
3656 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
3657 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
3658 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
3659
3660   enum ix86_opt_type
3661   {
3662     ix86_opt_unknown,
3663     ix86_opt_yes,
3664     ix86_opt_no,
3665     ix86_opt_str,
3666     ix86_opt_isa
3667   };
3668
3669   static const struct
3670   {
3671     const char *string;
3672     size_t len;
3673     enum ix86_opt_type type;
3674     int opt;
3675     int mask;
3676   } attrs[] = {
3677     /* isa options */
3678     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
3679     IX86_ATTR_ISA ("abm",       OPT_mabm),
3680     IX86_ATTR_ISA ("aes",       OPT_maes),
3681     IX86_ATTR_ISA ("avx",       OPT_mavx),
3682     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
3683     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
3684     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
3685     IX86_ATTR_ISA ("sse",       OPT_msse),
3686     IX86_ATTR_ISA ("sse2",      OPT_msse2),
3687     IX86_ATTR_ISA ("sse3",      OPT_msse3),
3688     IX86_ATTR_ISA ("sse4",      OPT_msse4),
3689     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
3690     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
3691     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
3692     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
3693     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
3694     IX86_ATTR_ISA ("xop",       OPT_mxop),
3695     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
3696
3697     /* string options */
3698     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
3699     IX86_ATTR_STR ("fpmath=",   IX86_FUNCTION_SPECIFIC_FPMATH),
3700     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
3701
3702     /* flag options */
3703     IX86_ATTR_YES ("cld",
3704                    OPT_mcld,
3705                    MASK_CLD),
3706
3707     IX86_ATTR_NO ("fancy-math-387",
3708                   OPT_mfancy_math_387,
3709                   MASK_NO_FANCY_MATH_387),
3710
3711     IX86_ATTR_YES ("ieee-fp",
3712                    OPT_mieee_fp,
3713                    MASK_IEEE_FP),
3714
3715     IX86_ATTR_YES ("inline-all-stringops",
3716                    OPT_minline_all_stringops,
3717                    MASK_INLINE_ALL_STRINGOPS),
3718
3719     IX86_ATTR_YES ("inline-stringops-dynamically",
3720                    OPT_minline_stringops_dynamically,
3721                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
3722
3723     IX86_ATTR_NO ("align-stringops",
3724                   OPT_mno_align_stringops,
3725                   MASK_NO_ALIGN_STRINGOPS),
3726
3727     IX86_ATTR_YES ("recip",
3728                    OPT_mrecip,
3729                    MASK_RECIP),
3730
3731   };
3732
3733   /* If this is a list, recurse to get the options.  */
3734   if (TREE_CODE (args) == TREE_LIST)
3735     {
3736       bool ret = true;
3737
3738       for (; args; args = TREE_CHAIN (args))
3739         if (TREE_VALUE (args)
3740             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args), p_strings))
3741           ret = false;
3742
3743       return ret;
3744     }
3745
3746   else if (TREE_CODE (args) != STRING_CST)
3747     gcc_unreachable ();
3748
3749   /* Handle multiple arguments separated by commas.  */
3750   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
3751
3752   while (next_optstr && *next_optstr != '\0')
3753     {
3754       char *p = next_optstr;
3755       char *orig_p = p;
3756       char *comma = strchr (next_optstr, ',');
3757       const char *opt_string;
3758       size_t len, opt_len;
3759       int opt;
3760       bool opt_set_p;
3761       char ch;
3762       unsigned i;
3763       enum ix86_opt_type type = ix86_opt_unknown;
3764       int mask = 0;
3765
3766       if (comma)
3767         {
3768           *comma = '\0';
3769           len = comma - next_optstr;
3770           next_optstr = comma + 1;
3771         }
3772       else
3773         {
3774           len = strlen (p);
3775           next_optstr = NULL;
3776         }
3777
3778       /* Recognize no-xxx.  */
3779       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
3780         {
3781           opt_set_p = false;
3782           p += 3;
3783           len -= 3;
3784         }
3785       else
3786         opt_set_p = true;
3787
3788       /* Find the option.  */
3789       ch = *p;
3790       opt = N_OPTS;
3791       for (i = 0; i < ARRAY_SIZE (attrs); i++)
3792         {
3793           type = attrs[i].type;
3794           opt_len = attrs[i].len;
3795           if (ch == attrs[i].string[0]
3796               && ((type != ix86_opt_str) ? len == opt_len : len > opt_len)
3797               && memcmp (p, attrs[i].string, opt_len) == 0)
3798             {
3799               opt = attrs[i].opt;
3800               mask = attrs[i].mask;
3801               opt_string = attrs[i].string;
3802               break;
3803             }
3804         }
3805
3806       /* Process the option.  */
3807       if (opt == N_OPTS)
3808         {
3809           error ("attribute(target(\"%s\")) is unknown", orig_p);
3810           ret = false;
3811         }
3812
3813       else if (type == ix86_opt_isa)
3814         ix86_handle_option (opt, p, opt_set_p);
3815
3816       else if (type == ix86_opt_yes || type == ix86_opt_no)
3817         {
3818           if (type == ix86_opt_no)
3819             opt_set_p = !opt_set_p;
3820
3821           if (opt_set_p)
3822             target_flags |= mask;
3823           else
3824             target_flags &= ~mask;
3825         }
3826
3827       else if (type == ix86_opt_str)
3828         {
3829           if (p_strings[opt])
3830             {
3831               error ("option(\"%s\") was already specified", opt_string);
3832               ret = false;
3833             }
3834           else
3835             p_strings[opt] = xstrdup (p + opt_len);
3836         }
3837
3838       else
3839         gcc_unreachable ();
3840     }
3841
3842   return ret;
3843 }
3844
3845 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
3846
3847 tree
3848 ix86_valid_target_attribute_tree (tree args)
3849 {
3850   const char *orig_arch_string = ix86_arch_string;
3851   const char *orig_tune_string = ix86_tune_string;
3852   const char *orig_fpmath_string = ix86_fpmath_string;
3853   int orig_tune_defaulted = ix86_tune_defaulted;
3854   int orig_arch_specified = ix86_arch_specified;
3855   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL, NULL };
3856   tree t = NULL_TREE;
3857   int i;
3858   struct cl_target_option *def
3859     = TREE_TARGET_OPTION (target_option_default_node);
3860
3861   /* Process each of the options on the chain.  */
3862   if (! ix86_valid_target_attribute_inner_p (args, option_strings))
3863     return NULL_TREE;
3864
3865   /* If the changed options are different from the default, rerun override_options,
3866      and then save the options away.  The string options are are attribute options,
3867      and will be undone when we copy the save structure.  */
3868   if (ix86_isa_flags != def->ix86_isa_flags
3869       || target_flags != def->target_flags
3870       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
3871       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
3872       || option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
3873     {
3874       /* If we are using the default tune= or arch=, undo the string assigned,
3875          and use the default.  */
3876       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
3877         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
3878       else if (!orig_arch_specified)
3879         ix86_arch_string = NULL;
3880
3881       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
3882         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
3883       else if (orig_tune_defaulted)
3884         ix86_tune_string = NULL;
3885
3886       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
3887       if (option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
3888         ix86_fpmath_string = option_strings[IX86_FUNCTION_SPECIFIC_FPMATH];
3889       else if (!TARGET_64BIT && TARGET_SSE)
3890         ix86_fpmath_string = "sse,387";
3891
3892       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
3893       override_options (false);
3894
3895       /* Add any builtin functions with the new isa if any.  */
3896       ix86_add_new_builtins (ix86_isa_flags);
3897
3898       /* Save the current options unless we are validating options for
3899          #pragma.  */
3900       t = build_target_option_node ();
3901
3902       ix86_arch_string = orig_arch_string;
3903       ix86_tune_string = orig_tune_string;
3904       ix86_fpmath_string = orig_fpmath_string;
3905
3906       /* Free up memory allocated to hold the strings */
3907       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
3908         if (option_strings[i])
3909           free (option_strings[i]);
3910     }
3911
3912   return t;
3913 }
3914
3915 /* Hook to validate attribute((target("string"))).  */
3916
3917 static bool
3918 ix86_valid_target_attribute_p (tree fndecl,
3919                                tree ARG_UNUSED (name),
3920                                tree args,
3921                                int ARG_UNUSED (flags))
3922 {
3923   struct cl_target_option cur_target;
3924   bool ret = true;
3925   tree old_optimize = build_optimization_node ();
3926   tree new_target, new_optimize;
3927   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
3928
3929   /* If the function changed the optimization levels as well as setting target
3930      options, start with the optimizations specified.  */
3931   if (func_optimize && func_optimize != old_optimize)
3932     cl_optimization_restore (TREE_OPTIMIZATION (func_optimize));
3933
3934   /* The target attributes may also change some optimization flags, so update
3935      the optimization options if necessary.  */
3936   cl_target_option_save (&cur_target);
3937   new_target = ix86_valid_target_attribute_tree (args);
3938   new_optimize = build_optimization_node ();
3939
3940   if (!new_target)
3941     ret = false;
3942
3943   else if (fndecl)
3944     {
3945       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
3946
3947       if (old_optimize != new_optimize)
3948         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
3949     }
3950
3951   cl_target_option_restore (&cur_target);
3952
3953   if (old_optimize != new_optimize)
3954     cl_optimization_restore (TREE_OPTIMIZATION (old_optimize));
3955
3956   return ret;
3957 }
3958
3959 \f
3960 /* Hook to determine if one function can safely inline another.  */
3961
3962 static bool
3963 ix86_can_inline_p (tree caller, tree callee)
3964 {
3965   bool ret = false;
3966   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
3967   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
3968
3969   /* If callee has no option attributes, then it is ok to inline.  */
3970   if (!callee_tree)
3971     ret = true;
3972
3973   /* If caller has no option attributes, but callee does then it is not ok to
3974      inline.  */
3975   else if (!caller_tree)
3976     ret = false;
3977
3978   else
3979     {
3980       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
3981       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
3982
3983       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
3984          can inline a SSE2 function but a SSE2 function can't inline a SSE4
3985          function.  */
3986       if ((caller_opts->ix86_isa_flags & callee_opts->ix86_isa_flags)
3987           != callee_opts->ix86_isa_flags)
3988         ret = false;
3989
3990       /* See if we have the same non-isa options.  */
3991       else if (caller_opts->target_flags != callee_opts->target_flags)
3992         ret = false;
3993
3994       /* See if arch, tune, etc. are the same.  */
3995       else if (caller_opts->arch != callee_opts->arch)
3996         ret = false;
3997
3998       else if (caller_opts->tune != callee_opts->tune)
3999         ret = false;
4000
4001       else if (caller_opts->fpmath != callee_opts->fpmath)
4002         ret = false;
4003
4004       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4005         ret = false;
4006
4007       else
4008         ret = true;
4009     }
4010
4011   return ret;
4012 }
4013
4014 \f
4015 /* Remember the last target of ix86_set_current_function.  */
4016 static GTY(()) tree ix86_previous_fndecl;
4017
4018 /* Establish appropriate back-end context for processing the function
4019    FNDECL.  The argument might be NULL to indicate processing at top
4020    level, outside of any function scope.  */
4021 static void
4022 ix86_set_current_function (tree fndecl)
4023 {
4024   /* Only change the context if the function changes.  This hook is called
4025      several times in the course of compiling a function, and we don't want to
4026      slow things down too much or call target_reinit when it isn't safe.  */
4027   if (fndecl && fndecl != ix86_previous_fndecl)
4028     {
4029       tree old_tree = (ix86_previous_fndecl
4030                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4031                        : NULL_TREE);
4032
4033       tree new_tree = (fndecl
4034                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4035                        : NULL_TREE);
4036
4037       ix86_previous_fndecl = fndecl;
4038       if (old_tree == new_tree)
4039         ;
4040
4041       else if (new_tree)
4042         {
4043           cl_target_option_restore (TREE_TARGET_OPTION (new_tree));
4044           target_reinit ();
4045         }
4046
4047       else if (old_tree)
4048         {
4049           struct cl_target_option *def
4050             = TREE_TARGET_OPTION (target_option_current_node);
4051
4052           cl_target_option_restore (def);
4053           target_reinit ();
4054         }
4055     }
4056 }
4057
4058 \f
4059 /* Return true if this goes in large data/bss.  */
4060
4061 static bool
4062 ix86_in_large_data_p (tree exp)
4063 {
4064   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4065     return false;
4066
4067   /* Functions are never large data.  */
4068   if (TREE_CODE (exp) == FUNCTION_DECL)
4069     return false;
4070
4071   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4072     {
4073       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4074       if (strcmp (section, ".ldata") == 0
4075           || strcmp (section, ".lbss") == 0)
4076         return true;
4077       return false;
4078     }
4079   else
4080     {
4081       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4082
4083       /* If this is an incomplete type with size 0, then we can't put it
4084          in data because it might be too big when completed.  */
4085       if (!size || size > ix86_section_threshold)
4086         return true;
4087     }
4088
4089   return false;
4090 }
4091
4092 /* Switch to the appropriate section for output of DECL.
4093    DECL is either a `VAR_DECL' node or a constant of some sort.
4094    RELOC indicates whether forming the initial value of DECL requires
4095    link-time relocations.  */
4096
4097 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4098         ATTRIBUTE_UNUSED;
4099
4100 static section *
4101 x86_64_elf_select_section (tree decl, int reloc,
4102                            unsigned HOST_WIDE_INT align)
4103 {
4104   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4105       && ix86_in_large_data_p (decl))
4106     {
4107       const char *sname = NULL;
4108       unsigned int flags = SECTION_WRITE;
4109       switch (categorize_decl_for_section (decl, reloc))
4110         {
4111         case SECCAT_DATA:
4112           sname = ".ldata";
4113           break;
4114         case SECCAT_DATA_REL:
4115           sname = ".ldata.rel";
4116           break;
4117         case SECCAT_DATA_REL_LOCAL:
4118           sname = ".ldata.rel.local";
4119           break;
4120         case SECCAT_DATA_REL_RO:
4121           sname = ".ldata.rel.ro";
4122           break;
4123         case SECCAT_DATA_REL_RO_LOCAL:
4124           sname = ".ldata.rel.ro.local";
4125           break;
4126         case SECCAT_BSS:
4127           sname = ".lbss";
4128           flags |= SECTION_BSS;
4129           break;
4130         case SECCAT_RODATA:
4131         case SECCAT_RODATA_MERGE_STR:
4132         case SECCAT_RODATA_MERGE_STR_INIT:
4133         case SECCAT_RODATA_MERGE_CONST:
4134           sname = ".lrodata";
4135           flags = 0;
4136           break;
4137         case SECCAT_SRODATA:
4138         case SECCAT_SDATA:
4139         case SECCAT_SBSS:
4140           gcc_unreachable ();
4141         case SECCAT_TEXT:
4142         case SECCAT_TDATA:
4143         case SECCAT_TBSS:
4144           /* We don't split these for medium model.  Place them into
4145              default sections and hope for best.  */
4146           break;
4147         case SECCAT_EMUTLS_VAR:
4148         case SECCAT_EMUTLS_TMPL:
4149           gcc_unreachable ();
4150         }
4151       if (sname)
4152         {
4153           /* We might get called with string constants, but get_named_section
4154              doesn't like them as they are not DECLs.  Also, we need to set
4155              flags in that case.  */
4156           if (!DECL_P (decl))
4157             return get_section (sname, flags, NULL);
4158           return get_named_section (decl, sname, reloc);
4159         }
4160     }
4161   return default_elf_select_section (decl, reloc, align);
4162 }
4163
4164 /* Build up a unique section name, expressed as a
4165    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4166    RELOC indicates whether the initial value of EXP requires
4167    link-time relocations.  */
4168
4169 static void ATTRIBUTE_UNUSED
4170 x86_64_elf_unique_section (tree decl, int reloc)
4171 {
4172   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4173       && ix86_in_large_data_p (decl))
4174     {
4175       const char *prefix = NULL;
4176       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4177       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4178
4179       switch (categorize_decl_for_section (decl, reloc))
4180         {
4181         case SECCAT_DATA:
4182         case SECCAT_DATA_REL:
4183         case SECCAT_DATA_REL_LOCAL:
4184         case SECCAT_DATA_REL_RO:
4185         case SECCAT_DATA_REL_RO_LOCAL:
4186           prefix = one_only ? ".ld" : ".ldata";
4187           break;
4188         case SECCAT_BSS:
4189           prefix = one_only ? ".lb" : ".lbss";
4190           break;
4191         case SECCAT_RODATA:
4192         case SECCAT_RODATA_MERGE_STR:
4193         case SECCAT_RODATA_MERGE_STR_INIT:
4194         case SECCAT_RODATA_MERGE_CONST:
4195           prefix = one_only ? ".lr" : ".lrodata";
4196           break;
4197         case SECCAT_SRODATA:
4198         case SECCAT_SDATA:
4199         case SECCAT_SBSS:
4200           gcc_unreachable ();
4201         case SECCAT_TEXT:
4202         case SECCAT_TDATA:
4203         case SECCAT_TBSS:
4204           /* We don't split these for medium model.  Place them into
4205              default sections and hope for best.  */
4206           break;
4207         case SECCAT_EMUTLS_VAR:
4208           prefix = targetm.emutls.var_section;
4209           break;
4210         case SECCAT_EMUTLS_TMPL:
4211           prefix = targetm.emutls.tmpl_section;
4212           break;
4213         }
4214       if (prefix)
4215         {
4216           const char *name, *linkonce;
4217           char *string;
4218
4219           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4220           name = targetm.strip_name_encoding (name);
4221           
4222           /* If we're using one_only, then there needs to be a .gnu.linkonce
4223              prefix to the section name.  */
4224           linkonce = one_only ? ".gnu.linkonce" : "";
4225   
4226           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4227           
4228           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4229           return;
4230         }
4231     }
4232   default_unique_section (decl, reloc);
4233 }
4234
4235 #ifdef COMMON_ASM_OP
4236 /* This says how to output assembler code to declare an
4237    uninitialized external linkage data object.
4238
4239    For medium model x86-64 we need to use .largecomm opcode for
4240    large objects.  */
4241 void
4242 x86_elf_aligned_common (FILE *file,
4243                         const char *name, unsigned HOST_WIDE_INT size,
4244                         int align)
4245 {
4246   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4247       && size > (unsigned int)ix86_section_threshold)
4248     fputs (".largecomm\t", file);
4249   else
4250     fputs (COMMON_ASM_OP, file);
4251   assemble_name (file, name);
4252   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
4253            size, align / BITS_PER_UNIT);
4254 }
4255 #endif
4256
4257 /* Utility function for targets to use in implementing
4258    ASM_OUTPUT_ALIGNED_BSS.  */
4259
4260 void
4261 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
4262                         const char *name, unsigned HOST_WIDE_INT size,
4263                         int align)
4264 {
4265   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4266       && size > (unsigned int)ix86_section_threshold)
4267     switch_to_section (get_named_section (decl, ".lbss", 0));
4268   else
4269     switch_to_section (bss_section);
4270   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
4271 #ifdef ASM_DECLARE_OBJECT_NAME
4272   last_assemble_variable_decl = decl;
4273   ASM_DECLARE_OBJECT_NAME (file, name, decl);
4274 #else
4275   /* Standard thing is just output label for the object.  */
4276   ASM_OUTPUT_LABEL (file, name);
4277 #endif /* ASM_DECLARE_OBJECT_NAME */
4278   ASM_OUTPUT_SKIP (file, size ? size : 1);
4279 }
4280 \f
4281 void
4282 optimization_options (int level, int size ATTRIBUTE_UNUSED)
4283 {
4284   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
4285      make the problem with not enough registers even worse.  */
4286 #ifdef INSN_SCHEDULING
4287   if (level > 1)
4288     flag_schedule_insns = 0;
4289 #endif
4290
4291   if (TARGET_MACHO)
4292     /* The Darwin libraries never set errno, so we might as well
4293        avoid calling them when that's the only reason we would.  */
4294     flag_errno_math = 0;
4295
4296   /* The default values of these switches depend on the TARGET_64BIT
4297      that is not known at this moment.  Mark these values with 2 and
4298      let user the to override these.  In case there is no command line option
4299      specifying them, we will set the defaults in override_options.  */
4300   if (optimize >= 1)
4301     flag_omit_frame_pointer = 2;
4302   flag_pcc_struct_return = 2;
4303   flag_asynchronous_unwind_tables = 2;
4304   flag_vect_cost_model = 1;
4305 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
4306   SUBTARGET_OPTIMIZATION_OPTIONS;
4307 #endif
4308 }
4309 \f
4310 /* Decide whether we can make a sibling call to a function.  DECL is the
4311    declaration of the function being targeted by the call and EXP is the
4312    CALL_EXPR representing the call.  */
4313
4314 static bool
4315 ix86_function_ok_for_sibcall (tree decl, tree exp)
4316 {
4317   tree type, decl_or_type;
4318   rtx a, b;
4319
4320   /* If we are generating position-independent code, we cannot sibcall
4321      optimize any indirect call, or a direct call to a global function,
4322      as the PLT requires %ebx be live.  */
4323   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
4324     return false;
4325
4326   /* If we need to align the outgoing stack, then sibcalling would
4327      unalign the stack, which may break the called function.  */
4328   if (ix86_minimum_incoming_stack_boundary (true)
4329       < PREFERRED_STACK_BOUNDARY)
4330     return false;
4331
4332   if (decl)
4333     {
4334       decl_or_type = decl;
4335       type = TREE_TYPE (decl);
4336     }
4337   else
4338     {
4339       /* We're looking at the CALL_EXPR, we need the type of the function.  */
4340       type = CALL_EXPR_FN (exp);                /* pointer expression */
4341       type = TREE_TYPE (type);                  /* pointer type */
4342       type = TREE_TYPE (type);                  /* function type */
4343       decl_or_type = type;
4344     }
4345
4346   /* Check that the return value locations are the same.  Like
4347      if we are returning floats on the 80387 register stack, we cannot
4348      make a sibcall from a function that doesn't return a float to a
4349      function that does or, conversely, from a function that does return
4350      a float to a function that doesn't; the necessary stack adjustment
4351      would not be executed.  This is also the place we notice
4352      differences in the return value ABI.  Note that it is ok for one
4353      of the functions to have void return type as long as the return
4354      value of the other is passed in a register.  */
4355   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
4356   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4357                            cfun->decl, false);
4358   if (STACK_REG_P (a) || STACK_REG_P (b))
4359     {
4360       if (!rtx_equal_p (a, b))
4361         return false;
4362     }
4363   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4364     ;
4365   else if (!rtx_equal_p (a, b))
4366     return false;
4367
4368   if (TARGET_64BIT)
4369     {
4370       /* The SYSV ABI has more call-clobbered registers;
4371          disallow sibcalls from MS to SYSV.  */
4372       if (cfun->machine->call_abi == MS_ABI
4373           && ix86_function_type_abi (type) == SYSV_ABI)
4374         return false;
4375     }
4376   else
4377     {
4378       /* If this call is indirect, we'll need to be able to use a
4379          call-clobbered register for the address of the target function.
4380          Make sure that all such registers are not used for passing
4381          parameters.  Note that DLLIMPORT functions are indirect.  */
4382       if (!decl
4383           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
4384         {
4385           if (ix86_function_regparm (type, NULL) >= 3)
4386             {
4387               /* ??? Need to count the actual number of registers to be used,
4388                  not the possible number of registers.  Fix later.  */
4389               return false;
4390             }
4391         }
4392     }
4393
4394   /* Otherwise okay.  That also includes certain types of indirect calls.  */
4395   return true;
4396 }
4397
4398 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
4399    calling convention attributes;
4400    arguments as in struct attribute_spec.handler.  */
4401
4402 static tree
4403 ix86_handle_cconv_attribute (tree *node, tree name,
4404                                    tree args,
4405                                    int flags ATTRIBUTE_UNUSED,
4406                                    bool *no_add_attrs)
4407 {
4408   if (TREE_CODE (*node) != FUNCTION_TYPE
4409       && TREE_CODE (*node) != METHOD_TYPE
4410       && TREE_CODE (*node) != FIELD_DECL
4411       && TREE_CODE (*node) != TYPE_DECL)
4412     {
4413       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4414                name);
4415       *no_add_attrs = true;
4416       return NULL_TREE;
4417     }
4418
4419   /* Can combine regparm with all attributes but fastcall.  */
4420   if (is_attribute_p ("regparm", name))
4421     {
4422       tree cst;
4423
4424       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4425         {
4426           error ("fastcall and regparm attributes are not compatible");
4427         }
4428
4429       cst = TREE_VALUE (args);
4430       if (TREE_CODE (cst) != INTEGER_CST)
4431         {
4432           warning (OPT_Wattributes,
4433                    "%qE attribute requires an integer constant argument",
4434                    name);
4435           *no_add_attrs = true;
4436         }
4437       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
4438         {
4439           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
4440                    name, REGPARM_MAX);
4441           *no_add_attrs = true;
4442         }
4443
4444       return NULL_TREE;
4445     }
4446
4447   if (TARGET_64BIT)
4448     {
4449       /* Do not warn when emulating the MS ABI.  */
4450       if (TREE_CODE (*node) != FUNCTION_TYPE
4451           || ix86_function_type_abi (*node) != MS_ABI)
4452         warning (OPT_Wattributes, "%qE attribute ignored",
4453                  name);
4454       *no_add_attrs = true;
4455       return NULL_TREE;
4456     }
4457
4458   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
4459   if (is_attribute_p ("fastcall", name))
4460     {
4461       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4462         {
4463           error ("fastcall and cdecl attributes are not compatible");
4464         }
4465       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4466         {
4467           error ("fastcall and stdcall attributes are not compatible");
4468         }
4469       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
4470         {
4471           error ("fastcall and regparm attributes are not compatible");
4472         }
4473     }
4474
4475   /* Can combine stdcall with fastcall (redundant), regparm and
4476      sseregparm.  */
4477   else if (is_attribute_p ("stdcall", name))
4478     {
4479       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4480         {
4481           error ("stdcall and cdecl attributes are not compatible");
4482         }
4483       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4484         {
4485           error ("stdcall and fastcall attributes are not compatible");
4486         }
4487     }
4488
4489   /* Can combine cdecl with regparm and sseregparm.  */
4490   else if (is_attribute_p ("cdecl", name))
4491     {
4492       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4493         {
4494           error ("stdcall and cdecl attributes are not compatible");
4495         }
4496       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4497         {
4498           error ("fastcall and cdecl attributes are not compatible");
4499         }
4500     }
4501
4502   /* Can combine sseregparm with all attributes.  */
4503
4504   return NULL_TREE;
4505 }
4506
4507 /* Return 0 if the attributes for two types are incompatible, 1 if they
4508    are compatible, and 2 if they are nearly compatible (which causes a
4509    warning to be generated).  */
4510
4511 static int
4512 ix86_comp_type_attributes (const_tree type1, const_tree type2)
4513 {
4514   /* Check for mismatch of non-default calling convention.  */
4515   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
4516
4517   if (TREE_CODE (type1) != FUNCTION_TYPE
4518       && TREE_CODE (type1) != METHOD_TYPE)
4519     return 1;
4520
4521   /* Check for mismatched fastcall/regparm types.  */
4522   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
4523        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
4524       || (ix86_function_regparm (type1, NULL)
4525           != ix86_function_regparm (type2, NULL)))
4526     return 0;
4527
4528   /* Check for mismatched sseregparm types.  */
4529   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
4530       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
4531     return 0;
4532
4533   /* Check for mismatched return types (cdecl vs stdcall).  */
4534   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
4535       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
4536     return 0;
4537
4538   return 1;
4539 }
4540 \f
4541 /* Return the regparm value for a function with the indicated TYPE and DECL.
4542    DECL may be NULL when calling function indirectly
4543    or considering a libcall.  */
4544
4545 static int
4546 ix86_function_regparm (const_tree type, const_tree decl)
4547 {
4548   tree attr;
4549   int regparm;
4550
4551   if (TARGET_64BIT)
4552     return (ix86_function_type_abi (type) == SYSV_ABI
4553             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
4554
4555   regparm = ix86_regparm;
4556   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
4557   if (attr)
4558     {
4559       regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
4560       return regparm;
4561     }
4562
4563   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
4564     return 2;
4565
4566   /* Use register calling convention for local functions when possible.  */
4567   if (decl
4568       && TREE_CODE (decl) == FUNCTION_DECL
4569       && optimize
4570       && !profile_flag)
4571     {
4572       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
4573       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
4574       if (i && i->local)
4575         {
4576           int local_regparm, globals = 0, regno;
4577
4578           /* Make sure no regparm register is taken by a
4579              fixed register variable.  */
4580           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
4581             if (fixed_regs[local_regparm])
4582               break;
4583
4584           /* We don't want to use regparm(3) for nested functions as
4585              these use a static chain pointer in the third argument.  */
4586           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
4587             local_regparm = 2;
4588
4589           /* Each fixed register usage increases register pressure,
4590              so less registers should be used for argument passing.
4591              This functionality can be overriden by an explicit
4592              regparm value.  */
4593           for (regno = 0; regno <= DI_REG; regno++)
4594             if (fixed_regs[regno])
4595               globals++;
4596
4597           local_regparm
4598             = globals < local_regparm ? local_regparm - globals : 0;
4599
4600           if (local_regparm > regparm)
4601             regparm = local_regparm;
4602         }
4603     }
4604
4605   return regparm;
4606 }
4607
4608 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
4609    DFmode (2) arguments in SSE registers for a function with the
4610    indicated TYPE and DECL.  DECL may be NULL when calling function
4611    indirectly or considering a libcall.  Otherwise return 0.  */
4612
4613 static int
4614 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
4615 {
4616   gcc_assert (!TARGET_64BIT);
4617
4618   /* Use SSE registers to pass SFmode and DFmode arguments if requested
4619      by the sseregparm attribute.  */
4620   if (TARGET_SSEREGPARM
4621       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
4622     {
4623       if (!TARGET_SSE)
4624         {
4625           if (warn)
4626             {
4627               if (decl)
4628                 error ("Calling %qD with attribute sseregparm without "
4629                        "SSE/SSE2 enabled", decl);
4630               else
4631                 error ("Calling %qT with attribute sseregparm without "
4632                        "SSE/SSE2 enabled", type);
4633             }
4634           return 0;
4635         }
4636
4637       return 2;
4638     }
4639
4640   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
4641      (and DFmode for SSE2) arguments in SSE registers.  */
4642   if (decl && TARGET_SSE_MATH && optimize && !profile_flag)
4643     {
4644       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
4645       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
4646       if (i && i->local)
4647         return TARGET_SSE2 ? 2 : 1;
4648     }
4649
4650   return 0;
4651 }
4652
4653 /* Return true if EAX is live at the start of the function.  Used by
4654    ix86_expand_prologue to determine if we need special help before
4655    calling allocate_stack_worker.  */
4656
4657 static bool
4658 ix86_eax_live_at_start_p (void)
4659 {
4660   /* Cheat.  Don't bother working forward from ix86_function_regparm
4661      to the function type to whether an actual argument is located in
4662      eax.  Instead just look at cfg info, which is still close enough
4663      to correct at this point.  This gives false positives for broken
4664      functions that might use uninitialized data that happens to be
4665      allocated in eax, but who cares?  */
4666   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
4667 }
4668
4669 /* Value is the number of bytes of arguments automatically
4670    popped when returning from a subroutine call.
4671    FUNDECL is the declaration node of the function (as a tree),
4672    FUNTYPE is the data type of the function (as a tree),
4673    or for a library call it is an identifier node for the subroutine name.
4674    SIZE is the number of bytes of arguments passed on the stack.
4675
4676    On the 80386, the RTD insn may be used to pop them if the number
4677      of args is fixed, but if the number is variable then the caller
4678      must pop them all.  RTD can't be used for library calls now
4679      because the library is compiled with the Unix compiler.
4680    Use of RTD is a selectable option, since it is incompatible with
4681    standard Unix calling sequences.  If the option is not selected,
4682    the caller must always pop the args.
4683
4684    The attribute stdcall is equivalent to RTD on a per module basis.  */
4685
4686 int
4687 ix86_return_pops_args (tree fundecl, tree funtype, int size)
4688 {
4689   int rtd;
4690
4691   /* None of the 64-bit ABIs pop arguments.  */
4692   if (TARGET_64BIT)
4693     return 0;
4694
4695   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
4696
4697   /* Cdecl functions override -mrtd, and never pop the stack.  */
4698   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
4699     {
4700       /* Stdcall and fastcall functions will pop the stack if not
4701          variable args.  */
4702       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
4703           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
4704         rtd = 1;
4705
4706       if (rtd && ! stdarg_p (funtype))
4707         return size;
4708     }
4709
4710   /* Lose any fake structure return argument if it is passed on the stack.  */
4711   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
4712       && !KEEP_AGGREGATE_RETURN_POINTER)
4713     {
4714       int nregs = ix86_function_regparm (funtype, fundecl);
4715       if (nregs == 0)
4716         return GET_MODE_SIZE (Pmode);
4717     }
4718
4719   return 0;
4720 }
4721 \f
4722 /* Argument support functions.  */
4723
4724 /* Return true when register may be used to pass function parameters.  */
4725 bool
4726 ix86_function_arg_regno_p (int regno)
4727 {
4728   int i;
4729   const int *parm_regs;
4730
4731   if (!TARGET_64BIT)
4732     {
4733       if (TARGET_MACHO)
4734         return (regno < REGPARM_MAX
4735                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
4736       else
4737         return (regno < REGPARM_MAX
4738                 || (TARGET_MMX && MMX_REGNO_P (regno)
4739                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
4740                 || (TARGET_SSE && SSE_REGNO_P (regno)
4741                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
4742     }
4743
4744   if (TARGET_MACHO)
4745     {
4746       if (SSE_REGNO_P (regno) && TARGET_SSE)
4747         return true;
4748     }
4749   else
4750     {
4751       if (TARGET_SSE && SSE_REGNO_P (regno)
4752           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
4753         return true;
4754     }
4755
4756   /* TODO: The function should depend on current function ABI but
4757      builtins.c would need updating then. Therefore we use the
4758      default ABI.  */
4759
4760   /* RAX is used as hidden argument to va_arg functions.  */
4761   if (ix86_abi == SYSV_ABI && regno == AX_REG)
4762     return true;
4763
4764   if (ix86_abi == MS_ABI)
4765     parm_regs = x86_64_ms_abi_int_parameter_registers;
4766   else
4767     parm_regs = x86_64_int_parameter_registers;
4768   for (i = 0; i < (ix86_abi == MS_ABI
4769                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
4770     if (regno == parm_regs[i])
4771       return true;
4772   return false;
4773 }
4774
4775 /* Return if we do not know how to pass TYPE solely in registers.  */
4776
4777 static bool
4778 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
4779 {
4780   if (must_pass_in_stack_var_size_or_pad (mode, type))
4781     return true;
4782
4783   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
4784      The layout_type routine is crafty and tries to trick us into passing
4785      currently unsupported vector types on the stack by using TImode.  */
4786   return (!TARGET_64BIT && mode == TImode
4787           && type && TREE_CODE (type) != VECTOR_TYPE);
4788 }
4789
4790 /* It returns the size, in bytes, of the area reserved for arguments passed
4791    in registers for the function represented by fndecl dependent to the used
4792    abi format.  */
4793 int
4794 ix86_reg_parm_stack_space (const_tree fndecl)
4795 {
4796   enum calling_abi call_abi = SYSV_ABI;
4797   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
4798     call_abi = ix86_function_abi (fndecl);
4799   else
4800     call_abi = ix86_function_type_abi (fndecl);
4801   if (call_abi == MS_ABI)
4802     return 32;
4803   return 0;
4804 }
4805
4806 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
4807    call abi used.  */
4808 enum calling_abi
4809 ix86_function_type_abi (const_tree fntype)
4810 {
4811   if (TARGET_64BIT && fntype != NULL)
4812     {
4813       enum calling_abi abi = ix86_abi;
4814       if (abi == SYSV_ABI)
4815         {
4816           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
4817             abi = MS_ABI;
4818         }
4819       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
4820         abi = SYSV_ABI;
4821       return abi;
4822     }
4823   return ix86_abi;
4824 }
4825
4826 static bool
4827 ix86_function_ms_hook_prologue (const_tree fntype)
4828 {
4829   if (!TARGET_64BIT)
4830     {
4831       if (lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fntype)))
4832         {
4833           if (decl_function_context (fntype) != NULL_TREE)
4834           {
4835             error_at (DECL_SOURCE_LOCATION (fntype),
4836                 "ms_hook_prologue is not compatible with nested function");
4837           }
4838
4839           return true;
4840         }
4841     }
4842   return false;
4843 }
4844
4845 static enum calling_abi
4846 ix86_function_abi (const_tree fndecl)
4847 {
4848   if (! fndecl)
4849     return ix86_abi;
4850   return ix86_function_type_abi (TREE_TYPE (fndecl));
4851 }
4852
4853 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
4854    call abi used.  */
4855 enum calling_abi
4856 ix86_cfun_abi (void)
4857 {
4858   if (! cfun || ! TARGET_64BIT)
4859     return ix86_abi;
4860   return cfun->machine->call_abi;
4861 }
4862
4863 /* regclass.c  */
4864 extern void init_regs (void);
4865
4866 /* Implementation of call abi switching target hook. Specific to FNDECL
4867    the specific call register sets are set. See also CONDITIONAL_REGISTER_USAGE
4868    for more details.  */
4869 void
4870 ix86_call_abi_override (const_tree fndecl)
4871 {
4872   if (fndecl == NULL_TREE)
4873     cfun->machine->call_abi = ix86_abi;
4874   else
4875     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
4876 }
4877
4878 /* MS and SYSV ABI have different set of call used registers.  Avoid expensive
4879    re-initialization of init_regs each time we switch function context since
4880    this is needed only during RTL expansion.  */
4881 static void
4882 ix86_maybe_switch_abi (void)
4883 {
4884   if (TARGET_64BIT &&
4885       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
4886     reinit_regs ();
4887 }
4888
4889 /* Initialize a variable CUM of type CUMULATIVE_ARGS
4890    for a call to a function whose data type is FNTYPE.
4891    For a library call, FNTYPE is 0.  */
4892
4893 void
4894 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
4895                       tree fntype,      /* tree ptr for function decl */
4896                       rtx libname,      /* SYMBOL_REF of library name or 0 */
4897                       tree fndecl)
4898 {
4899   struct cgraph_local_info *i = fndecl ? cgraph_local_info (fndecl) : NULL;
4900   memset (cum, 0, sizeof (*cum));
4901
4902   if (fndecl)
4903    cum->call_abi = ix86_function_abi (fndecl);
4904   else
4905    cum->call_abi = ix86_function_type_abi (fntype);
4906   /* Set up the number of registers to use for passing arguments.  */
4907
4908   if (cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
4909     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
4910            "or subtarget optimization implying it");
4911   cum->nregs = ix86_regparm;
4912   if (TARGET_64BIT)
4913     {
4914       if (cum->call_abi != ix86_abi)
4915         cum->nregs = (ix86_abi != SYSV_ABI
4916                       ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
4917     }
4918   if (TARGET_SSE)
4919     {
4920       cum->sse_nregs = SSE_REGPARM_MAX;
4921       if (TARGET_64BIT)
4922         {
4923           if (cum->call_abi != ix86_abi)
4924             cum->sse_nregs = (ix86_abi != SYSV_ABI
4925                               ? X86_64_SSE_REGPARM_MAX
4926                               : X86_64_MS_SSE_REGPARM_MAX);
4927         }
4928     }
4929   if (TARGET_MMX)
4930     cum->mmx_nregs = MMX_REGPARM_MAX;
4931   cum->warn_avx = true;
4932   cum->warn_sse = true;
4933   cum->warn_mmx = true;
4934
4935   /* Because type might mismatch in between caller and callee, we need to
4936      use actual type of function for local calls.
4937      FIXME: cgraph_analyze can be told to actually record if function uses
4938      va_start so for local functions maybe_vaarg can be made aggressive
4939      helping K&R code.
4940      FIXME: once typesytem is fixed, we won't need this code anymore.  */
4941   if (i && i->local)
4942     fntype = TREE_TYPE (fndecl);
4943   cum->maybe_vaarg = (fntype
4944                       ? (!prototype_p (fntype) || stdarg_p (fntype))
4945                       : !libname);
4946
4947   if (!TARGET_64BIT)
4948     {
4949       /* If there are variable arguments, then we won't pass anything
4950          in registers in 32-bit mode. */
4951       if (stdarg_p (fntype))
4952         {
4953           cum->nregs = 0;
4954           cum->sse_nregs = 0;
4955           cum->mmx_nregs = 0;
4956           cum->warn_avx = 0;
4957           cum->warn_sse = 0;
4958           cum->warn_mmx = 0;
4959           return;
4960         }
4961
4962       /* Use ecx and edx registers if function has fastcall attribute,
4963          else look for regparm information.  */
4964       if (fntype)
4965         {
4966           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
4967             {
4968               cum->nregs = 2;
4969               cum->fastcall = 1;
4970             }
4971           else
4972             cum->nregs = ix86_function_regparm (fntype, fndecl);
4973         }
4974
4975       /* Set up the number of SSE registers used for passing SFmode
4976          and DFmode arguments.  Warn for mismatching ABI.  */
4977       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
4978     }
4979 }
4980
4981 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
4982    But in the case of vector types, it is some vector mode.
4983
4984    When we have only some of our vector isa extensions enabled, then there
4985    are some modes for which vector_mode_supported_p is false.  For these
4986    modes, the generic vector support in gcc will choose some non-vector mode
4987    in order to implement the type.  By computing the natural mode, we'll
4988    select the proper ABI location for the operand and not depend on whatever
4989    the middle-end decides to do with these vector types.
4990
4991    The midde-end can't deal with the vector types > 16 bytes.  In this
4992    case, we return the original mode and warn ABI change if CUM isn't
4993    NULL.  */
4994
4995 static enum machine_mode
4996 type_natural_mode (const_tree type, CUMULATIVE_ARGS *cum)
4997 {
4998   enum machine_mode mode = TYPE_MODE (type);
4999
5000   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5001     {
5002       HOST_WIDE_INT size = int_size_in_bytes (type);
5003       if ((size == 8 || size == 16 || size == 32)
5004           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5005           && TYPE_VECTOR_SUBPARTS (type) > 1)
5006         {
5007           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5008
5009           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5010             mode = MIN_MODE_VECTOR_FLOAT;
5011           else
5012             mode = MIN_MODE_VECTOR_INT;
5013
5014           /* Get the mode which has this inner mode and number of units.  */
5015           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5016             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5017                 && GET_MODE_INNER (mode) == innermode)
5018               {
5019                 if (size == 32 && !TARGET_AVX)
5020                   {
5021                     static bool warnedavx;
5022
5023                     if (cum
5024                         && !warnedavx 
5025                         && cum->warn_avx)
5026                       {
5027                         warnedavx = true;
5028                         warning (0, "AVX vector argument without AVX "
5029                                  "enabled changes the ABI");
5030                       }
5031                     return TYPE_MODE (type);
5032                   }
5033                 else
5034                   return mode;
5035               }
5036
5037           gcc_unreachable ();
5038         }
5039     }
5040
5041   return mode;
5042 }
5043
5044 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5045    this may not agree with the mode that the type system has chosen for the
5046    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5047    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5048
5049 static rtx
5050 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5051                      unsigned int regno)
5052 {
5053   rtx tmp;
5054
5055   if (orig_mode != BLKmode)
5056     tmp = gen_rtx_REG (orig_mode, regno);
5057   else
5058     {
5059       tmp = gen_rtx_REG (mode, regno);
5060       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5061       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5062     }
5063
5064   return tmp;
5065 }
5066
5067 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5068    of this code is to classify each 8bytes of incoming argument by the register
5069    class and assign registers accordingly.  */
5070
5071 /* Return the union class of CLASS1 and CLASS2.
5072    See the x86-64 PS ABI for details.  */
5073
5074 static enum x86_64_reg_class
5075 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5076 {
5077   /* Rule #1: If both classes are equal, this is the resulting class.  */
5078   if (class1 == class2)
5079     return class1;
5080
5081   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5082      the other class.  */
5083   if (class1 == X86_64_NO_CLASS)
5084     return class2;
5085   if (class2 == X86_64_NO_CLASS)
5086     return class1;
5087
5088   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
5089   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
5090     return X86_64_MEMORY_CLASS;
5091
5092   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
5093   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
5094       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
5095     return X86_64_INTEGERSI_CLASS;
5096   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
5097       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
5098     return X86_64_INTEGER_CLASS;
5099
5100   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
5101      MEMORY is used.  */
5102   if (class1 == X86_64_X87_CLASS
5103       || class1 == X86_64_X87UP_CLASS
5104       || class1 == X86_64_COMPLEX_X87_CLASS
5105       || class2 == X86_64_X87_CLASS
5106       || class2 == X86_64_X87UP_CLASS
5107       || class2 == X86_64_COMPLEX_X87_CLASS)
5108     return X86_64_MEMORY_CLASS;
5109
5110   /* Rule #6: Otherwise class SSE is used.  */
5111   return X86_64_SSE_CLASS;
5112 }
5113
5114 /* Classify the argument of type TYPE and mode MODE.
5115    CLASSES will be filled by the register class used to pass each word
5116    of the operand.  The number of words is returned.  In case the parameter
5117    should be passed in memory, 0 is returned. As a special case for zero
5118    sized containers, classes[0] will be NO_CLASS and 1 is returned.
5119
5120    BIT_OFFSET is used internally for handling records and specifies offset
5121    of the offset in bits modulo 256 to avoid overflow cases.
5122
5123    See the x86-64 PS ABI for details.
5124 */
5125
5126 static int
5127 classify_argument (enum machine_mode mode, const_tree type,
5128                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
5129 {
5130   HOST_WIDE_INT bytes =
5131     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5132   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5133
5134   /* Variable sized entities are always passed/returned in memory.  */
5135   if (bytes < 0)
5136     return 0;
5137
5138   if (mode != VOIDmode
5139       && targetm.calls.must_pass_in_stack (mode, type))
5140     return 0;
5141
5142   if (type && AGGREGATE_TYPE_P (type))
5143     {
5144       int i;
5145       tree field;
5146       enum x86_64_reg_class subclasses[MAX_CLASSES];
5147
5148       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
5149       if (bytes > 32)
5150         return 0;
5151
5152       for (i = 0; i < words; i++)
5153         classes[i] = X86_64_NO_CLASS;
5154
5155       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
5156          signalize memory class, so handle it as special case.  */
5157       if (!words)
5158         {
5159           classes[0] = X86_64_NO_CLASS;
5160           return 1;
5161         }
5162
5163       /* Classify each field of record and merge classes.  */
5164       switch (TREE_CODE (type))
5165         {
5166         case RECORD_TYPE:
5167           /* And now merge the fields of structure.  */
5168           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5169             {
5170               if (TREE_CODE (field) == FIELD_DECL)
5171                 {
5172                   int num;
5173
5174                   if (TREE_TYPE (field) == error_mark_node)
5175                     continue;
5176
5177                   /* Bitfields are always classified as integer.  Handle them
5178                      early, since later code would consider them to be
5179                      misaligned integers.  */
5180                   if (DECL_BIT_FIELD (field))
5181                     {
5182                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5183                            i < ((int_bit_position (field) + (bit_offset % 64))
5184                                 + tree_low_cst (DECL_SIZE (field), 0)
5185                                 + 63) / 8 / 8; i++)
5186                         classes[i] =
5187                           merge_classes (X86_64_INTEGER_CLASS,
5188                                          classes[i]);
5189                     }
5190                   else
5191                     {
5192                       int pos;
5193
5194                       type = TREE_TYPE (field);
5195
5196                       /* Flexible array member is ignored.  */
5197                       if (TYPE_MODE (type) == BLKmode
5198                           && TREE_CODE (type) == ARRAY_TYPE
5199                           && TYPE_SIZE (type) == NULL_TREE
5200                           && TYPE_DOMAIN (type) != NULL_TREE
5201                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5202                               == NULL_TREE))
5203                         {
5204                           static bool warned;
5205                           
5206                           if (!warned && warn_psabi)
5207                             {
5208                               warned = true;
5209                               inform (input_location,
5210                                       "The ABI of passing struct with"
5211                                       " a flexible array member has"
5212                                       " changed in GCC 4.4");
5213                             }
5214                           continue;
5215                         }
5216                       num = classify_argument (TYPE_MODE (type), type,
5217                                                subclasses,
5218                                                (int_bit_position (field)
5219                                                 + bit_offset) % 256);
5220                       if (!num)
5221                         return 0;
5222                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5223                       for (i = 0; i < num && (i + pos) < words; i++)
5224                         classes[i + pos] =
5225                           merge_classes (subclasses[i], classes[i + pos]);
5226                     }
5227                 }
5228             }
5229           break;
5230
5231         case ARRAY_TYPE:
5232           /* Arrays are handled as small records.  */
5233           {
5234             int num;
5235             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
5236                                      TREE_TYPE (type), subclasses, bit_offset);
5237             if (!num)
5238               return 0;
5239
5240             /* The partial classes are now full classes.  */
5241             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
5242               subclasses[0] = X86_64_SSE_CLASS;
5243             if (subclasses[0] == X86_64_INTEGERSI_CLASS
5244                 && !((bit_offset % 64) == 0 && bytes == 4))
5245               subclasses[0] = X86_64_INTEGER_CLASS;
5246
5247             for (i = 0; i < words; i++)
5248               classes[i] = subclasses[i % num];
5249
5250             break;
5251           }
5252         case UNION_TYPE:
5253         case QUAL_UNION_TYPE:
5254           /* Unions are similar to RECORD_TYPE but offset is always 0.
5255              */
5256           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5257             {
5258               if (TREE_CODE (field) == FIELD_DECL)
5259                 {
5260                   int num;
5261
5262                   if (TREE_TYPE (field) == error_mark_node)
5263                     continue;
5264
5265                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
5266                                            TREE_TYPE (field), subclasses,
5267                                            bit_offset);
5268                   if (!num)
5269                     return 0;
5270                   for (i = 0; i < num; i++)
5271                     classes[i] = merge_classes (subclasses[i], classes[i]);
5272                 }
5273             }
5274           break;
5275
5276         default:
5277           gcc_unreachable ();
5278         }
5279
5280       if (words > 2)
5281         {
5282           /* When size > 16 bytes, if the first one isn't
5283              X86_64_SSE_CLASS or any other ones aren't
5284              X86_64_SSEUP_CLASS, everything should be passed in
5285              memory.  */
5286           if (classes[0] != X86_64_SSE_CLASS)
5287               return 0;
5288
5289           for (i = 1; i < words; i++)
5290             if (classes[i] != X86_64_SSEUP_CLASS)
5291               return 0;
5292         }
5293
5294       /* Final merger cleanup.  */
5295       for (i = 0; i < words; i++)
5296         {
5297           /* If one class is MEMORY, everything should be passed in
5298              memory.  */
5299           if (classes[i] == X86_64_MEMORY_CLASS)
5300             return 0;
5301
5302           /* The X86_64_SSEUP_CLASS should be always preceded by
5303              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
5304           if (classes[i] == X86_64_SSEUP_CLASS
5305               && classes[i - 1] != X86_64_SSE_CLASS
5306               && classes[i - 1] != X86_64_SSEUP_CLASS)
5307             {
5308               /* The first one should never be X86_64_SSEUP_CLASS.  */
5309               gcc_assert (i != 0);
5310               classes[i] = X86_64_SSE_CLASS;
5311             }
5312
5313           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
5314                everything should be passed in memory.  */
5315           if (classes[i] == X86_64_X87UP_CLASS
5316               && (classes[i - 1] != X86_64_X87_CLASS))
5317             {
5318               static bool warned;
5319
5320               /* The first one should never be X86_64_X87UP_CLASS.  */
5321               gcc_assert (i != 0);
5322               if (!warned && warn_psabi)
5323                 {
5324                   warned = true;
5325                   inform (input_location,
5326                           "The ABI of passing union with long double"
5327                           " has changed in GCC 4.4");
5328                 }
5329               return 0;
5330             }
5331         }
5332       return words;
5333     }
5334
5335   /* Compute alignment needed.  We align all types to natural boundaries with
5336      exception of XFmode that is aligned to 64bits.  */
5337   if (mode != VOIDmode && mode != BLKmode)
5338     {
5339       int mode_alignment = GET_MODE_BITSIZE (mode);
5340
5341       if (mode == XFmode)
5342         mode_alignment = 128;
5343       else if (mode == XCmode)
5344         mode_alignment = 256;
5345       if (COMPLEX_MODE_P (mode))
5346         mode_alignment /= 2;
5347       /* Misaligned fields are always returned in memory.  */
5348       if (bit_offset % mode_alignment)
5349         return 0;
5350     }
5351
5352   /* for V1xx modes, just use the base mode */
5353   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
5354       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
5355     mode = GET_MODE_INNER (mode);
5356
5357   /* Classification of atomic types.  */
5358   switch (mode)
5359     {
5360     case SDmode:
5361     case DDmode:
5362       classes[0] = X86_64_SSE_CLASS;
5363       return 1;
5364     case TDmode:
5365       classes[0] = X86_64_SSE_CLASS;
5366       classes[1] = X86_64_SSEUP_CLASS;
5367       return 2;
5368     case DImode:
5369     case SImode:
5370     case HImode:
5371     case QImode:
5372     case CSImode:
5373     case CHImode:
5374     case CQImode:
5375       {
5376         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
5377
5378         if (size <= 32)
5379           {
5380             classes[0] = X86_64_INTEGERSI_CLASS;
5381             return 1;
5382           }
5383         else if (size <= 64)
5384           {
5385             classes[0] = X86_64_INTEGER_CLASS;
5386             return 1;
5387           }
5388         else if (size <= 64+32)
5389           {
5390             classes[0] = X86_64_INTEGER_CLASS;
5391             classes[1] = X86_64_INTEGERSI_CLASS;
5392             return 2;
5393           }
5394         else if (size <= 64+64)
5395           {
5396             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
5397             return 2;
5398           }
5399         else
5400           gcc_unreachable ();
5401       }
5402     case CDImode:
5403     case TImode:
5404       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
5405       return 2;
5406     case COImode:
5407     case OImode:
5408       /* OImode shouldn't be used directly.  */
5409       gcc_unreachable ();
5410     case CTImode:
5411       return 0;
5412     case SFmode:
5413       if (!(bit_offset % 64))
5414         classes[0] = X86_64_SSESF_CLASS;
5415       else
5416         classes[0] = X86_64_SSE_CLASS;
5417       return 1;
5418     case DFmode:
5419       classes[0] = X86_64_SSEDF_CLASS;
5420       return 1;
5421     case XFmode:
5422       classes[0] = X86_64_X87_CLASS;
5423       classes[1] = X86_64_X87UP_CLASS;
5424       return 2;
5425     case TFmode:
5426       classes[0] = X86_64_SSE_CLASS;
5427       classes[1] = X86_64_SSEUP_CLASS;
5428       return 2;
5429     case SCmode:
5430       classes[0] = X86_64_SSE_CLASS;
5431       if (!(bit_offset % 64))
5432         return 1;
5433       else
5434         {
5435           static bool warned;
5436
5437           if (!warned && warn_psabi)
5438             {
5439               warned = true;
5440               inform (input_location,
5441                       "The ABI of passing structure with complex float"
5442                       " member has changed in GCC 4.4");
5443             }
5444           classes[1] = X86_64_SSESF_CLASS;
5445           return 2;
5446         }
5447     case DCmode:
5448       classes[0] = X86_64_SSEDF_CLASS;
5449       classes[1] = X86_64_SSEDF_CLASS;
5450       return 2;
5451     case XCmode:
5452       classes[0] = X86_64_COMPLEX_X87_CLASS;
5453       return 1;
5454     case TCmode:
5455       /* This modes is larger than 16 bytes.  */
5456       return 0;
5457     case V8SFmode:
5458     case V8SImode:
5459     case V32QImode:
5460     case V16HImode:
5461     case V4DFmode:
5462     case V4DImode:
5463       classes[0] = X86_64_SSE_CLASS;
5464       classes[1] = X86_64_SSEUP_CLASS;
5465       classes[2] = X86_64_SSEUP_CLASS;
5466       classes[3] = X86_64_SSEUP_CLASS;
5467       return 4;
5468     case V4SFmode:
5469     case V4SImode:
5470     case V16QImode:
5471     case V8HImode:
5472     case V2DFmode:
5473     case V2DImode:
5474       classes[0] = X86_64_SSE_CLASS;
5475       classes[1] = X86_64_SSEUP_CLASS;
5476       return 2;
5477     case V1TImode:
5478     case V1DImode:
5479     case V2SFmode:
5480     case V2SImode:
5481     case V4HImode:
5482     case V8QImode:
5483       classes[0] = X86_64_SSE_CLASS;
5484       return 1;
5485     case BLKmode:
5486     case VOIDmode:
5487       return 0;
5488     default:
5489       gcc_assert (VECTOR_MODE_P (mode));
5490
5491       if (bytes > 16)
5492         return 0;
5493
5494       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
5495
5496       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
5497         classes[0] = X86_64_INTEGERSI_CLASS;
5498       else
5499         classes[0] = X86_64_INTEGER_CLASS;
5500       classes[1] = X86_64_INTEGER_CLASS;
5501       return 1 + (bytes > 8);
5502     }
5503 }
5504
5505 /* Examine the argument and return set number of register required in each
5506    class.  Return 0 iff parameter should be passed in memory.  */
5507 static int
5508 examine_argument (enum machine_mode mode, const_tree type, int in_return,
5509                   int *int_nregs, int *sse_nregs)
5510 {
5511   enum x86_64_reg_class regclass[MAX_CLASSES];
5512   int n = classify_argument (mode, type, regclass, 0);
5513
5514   *int_nregs = 0;
5515   *sse_nregs = 0;
5516   if (!n)
5517     return 0;
5518   for (n--; n >= 0; n--)
5519     switch (regclass[n])
5520       {
5521       case X86_64_INTEGER_CLASS:
5522       case X86_64_INTEGERSI_CLASS:
5523         (*int_nregs)++;
5524         break;
5525       case X86_64_SSE_CLASS:
5526       case X86_64_SSESF_CLASS:
5527       case X86_64_SSEDF_CLASS:
5528         (*sse_nregs)++;
5529         break;
5530       case X86_64_NO_CLASS:
5531       case X86_64_SSEUP_CLASS:
5532         break;
5533       case X86_64_X87_CLASS:
5534       case X86_64_X87UP_CLASS:
5535         if (!in_return)
5536           return 0;
5537         break;
5538       case X86_64_COMPLEX_X87_CLASS:
5539         return in_return ? 2 : 0;
5540       case X86_64_MEMORY_CLASS:
5541         gcc_unreachable ();
5542       }
5543   return 1;
5544 }
5545
5546 /* Construct container for the argument used by GCC interface.  See
5547    FUNCTION_ARG for the detailed description.  */
5548
5549 static rtx
5550 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
5551                      const_tree type, int in_return, int nintregs, int nsseregs,
5552                      const int *intreg, int sse_regno)
5553 {
5554   /* The following variables hold the static issued_error state.  */
5555   static bool issued_sse_arg_error;
5556   static bool issued_sse_ret_error;
5557   static bool issued_x87_ret_error;
5558
5559   enum machine_mode tmpmode;
5560   int bytes =
5561     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5562   enum x86_64_reg_class regclass[MAX_CLASSES];
5563   int n;
5564   int i;
5565   int nexps = 0;
5566   int needed_sseregs, needed_intregs;
5567   rtx exp[MAX_CLASSES];
5568   rtx ret;
5569
5570   n = classify_argument (mode, type, regclass, 0);
5571   if (!n)
5572     return NULL;
5573   if (!examine_argument (mode, type, in_return, &needed_intregs,
5574                          &needed_sseregs))
5575     return NULL;
5576   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
5577     return NULL;
5578
5579   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
5580      some less clueful developer tries to use floating-point anyway.  */
5581   if (needed_sseregs && !TARGET_SSE)
5582     {
5583       if (in_return)
5584         {
5585           if (!issued_sse_ret_error)
5586             {
5587               error ("SSE register return with SSE disabled");
5588               issued_sse_ret_error = true;
5589             }
5590         }
5591       else if (!issued_sse_arg_error)
5592         {
5593           error ("SSE register argument with SSE disabled");
5594           issued_sse_arg_error = true;
5595         }
5596       return NULL;
5597     }
5598
5599   /* Likewise, error if the ABI requires us to return values in the
5600      x87 registers and the user specified -mno-80387.  */
5601   if (!TARGET_80387 && in_return)
5602     for (i = 0; i < n; i++)
5603       if (regclass[i] == X86_64_X87_CLASS
5604           || regclass[i] == X86_64_X87UP_CLASS
5605           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
5606         {
5607           if (!issued_x87_ret_error)
5608             {
5609               error ("x87 register return with x87 disabled");
5610               issued_x87_ret_error = true;
5611             }
5612           return NULL;
5613         }
5614
5615   /* First construct simple cases.  Avoid SCmode, since we want to use
5616      single register to pass this type.  */
5617   if (n == 1 && mode != SCmode)
5618     switch (regclass[0])
5619       {
5620       case X86_64_INTEGER_CLASS:
5621       case X86_64_INTEGERSI_CLASS:
5622         return gen_rtx_REG (mode, intreg[0]);
5623       case X86_64_SSE_CLASS:
5624       case X86_64_SSESF_CLASS:
5625       case X86_64_SSEDF_CLASS:
5626         if (mode != BLKmode)
5627           return gen_reg_or_parallel (mode, orig_mode, 
5628                                       SSE_REGNO (sse_regno));
5629         break;
5630       case X86_64_X87_CLASS:
5631       case X86_64_COMPLEX_X87_CLASS:
5632         return gen_rtx_REG (mode, FIRST_STACK_REG);
5633       case X86_64_NO_CLASS:
5634         /* Zero sized array, struct or class.  */
5635         return NULL;
5636       default:
5637         gcc_unreachable ();
5638       }
5639   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
5640       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
5641     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
5642   if (n == 4
5643       && regclass[0] == X86_64_SSE_CLASS
5644       && regclass[1] == X86_64_SSEUP_CLASS
5645       && regclass[2] == X86_64_SSEUP_CLASS
5646       && regclass[3] == X86_64_SSEUP_CLASS
5647       && mode != BLKmode)
5648     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
5649
5650   if (n == 2
5651       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
5652     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
5653   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
5654       && regclass[1] == X86_64_INTEGER_CLASS
5655       && (mode == CDImode || mode == TImode || mode == TFmode)
5656       && intreg[0] + 1 == intreg[1])
5657     return gen_rtx_REG (mode, intreg[0]);
5658
5659   /* Otherwise figure out the entries of the PARALLEL.  */
5660   for (i = 0; i < n; i++)
5661     {
5662       int pos;
5663
5664       switch (regclass[i])
5665         {
5666           case X86_64_NO_CLASS:
5667             break;
5668           case X86_64_INTEGER_CLASS:
5669           case X86_64_INTEGERSI_CLASS:
5670             /* Merge TImodes on aligned occasions here too.  */
5671             if (i * 8 + 8 > bytes)
5672               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
5673             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
5674               tmpmode = SImode;
5675             else
5676               tmpmode = DImode;
5677             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
5678             if (tmpmode == BLKmode)
5679               tmpmode = DImode;
5680             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
5681                                                gen_rtx_REG (tmpmode, *intreg),
5682                                                GEN_INT (i*8));
5683             intreg++;
5684             break;
5685           case X86_64_SSESF_CLASS:
5686             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
5687                                                gen_rtx_REG (SFmode,
5688                                                             SSE_REGNO (sse_regno)),
5689                                                GEN_INT (i*8));
5690             sse_regno++;
5691             break;
5692           case X86_64_SSEDF_CLASS:
5693             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
5694                                                gen_rtx_REG (DFmode,
5695                                                             SSE_REGNO (sse_regno)),
5696                                                GEN_INT (i*8));
5697             sse_regno++;
5698             break;
5699           case X86_64_SSE_CLASS:
5700             pos = i;
5701             switch (n)
5702               {
5703               case 1:
5704                 tmpmode = DImode;
5705                 break;
5706               case 2:
5707                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
5708                   {
5709                     tmpmode = TImode;
5710                     i++;
5711                   }
5712                 else
5713                   tmpmode = DImode;
5714                 break;
5715               case 4:
5716                 gcc_assert (i == 0
5717                             && regclass[1] == X86_64_SSEUP_CLASS
5718                             && regclass[2] == X86_64_SSEUP_CLASS
5719                             && regclass[3] == X86_64_SSEUP_CLASS);
5720                 tmpmode = OImode;
5721                 i += 3;
5722                 break;
5723               default:
5724                 gcc_unreachable ();
5725               }
5726             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
5727                                                gen_rtx_REG (tmpmode,
5728                                                             SSE_REGNO (sse_regno)),
5729                                                GEN_INT (pos*8));
5730             sse_regno++;
5731             break;
5732           default:
5733             gcc_unreachable ();
5734         }
5735     }
5736
5737   /* Empty aligned struct, union or class.  */
5738   if (nexps == 0)
5739     return NULL;
5740
5741   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
5742   for (i = 0; i < nexps; i++)
5743     XVECEXP (ret, 0, i) = exp [i];
5744   return ret;
5745 }
5746
5747 /* Update the data in CUM to advance over an argument of mode MODE
5748    and data type TYPE.  (TYPE is null for libcalls where that information
5749    may not be available.)  */
5750
5751 static void
5752 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5753                          tree type, HOST_WIDE_INT bytes, HOST_WIDE_INT words)
5754 {
5755   switch (mode)
5756     {
5757     default:
5758       break;
5759
5760     case BLKmode:
5761       if (bytes < 0)
5762         break;
5763       /* FALLTHRU */
5764
5765     case DImode:
5766     case SImode:
5767     case HImode:
5768     case QImode:
5769       cum->words += words;
5770       cum->nregs -= words;
5771       cum->regno += words;
5772
5773       if (cum->nregs <= 0)
5774         {
5775           cum->nregs = 0;
5776           cum->regno = 0;
5777         }
5778       break;
5779
5780     case OImode:
5781       /* OImode shouldn't be used directly.  */
5782       gcc_unreachable ();
5783
5784     case DFmode:
5785       if (cum->float_in_sse < 2)
5786         break;
5787     case SFmode:
5788       if (cum->float_in_sse < 1)
5789         break;
5790       /* FALLTHRU */
5791
5792     case V8SFmode:
5793     case V8SImode:
5794     case V32QImode:
5795     case V16HImode:
5796     case V4DFmode:
5797     case V4DImode:
5798     case TImode:
5799     case V16QImode:
5800     case V8HImode:
5801     case V4SImode:
5802     case V2DImode:
5803     case V4SFmode:
5804     case V2DFmode:
5805       if (!type || !AGGREGATE_TYPE_P (type))
5806         {
5807           cum->sse_words += words;
5808           cum->sse_nregs -= 1;
5809           cum->sse_regno += 1;
5810           if (cum->sse_nregs <= 0)
5811             {
5812               cum->sse_nregs = 0;
5813               cum->sse_regno = 0;
5814             }
5815         }
5816       break;
5817
5818     case V8QImode:
5819     case V4HImode:
5820     case V2SImode:
5821     case V2SFmode:
5822     case V1TImode:
5823     case V1DImode:
5824       if (!type || !AGGREGATE_TYPE_P (type))
5825         {
5826           cum->mmx_words += words;
5827           cum->mmx_nregs -= 1;
5828           cum->mmx_regno += 1;
5829           if (cum->mmx_nregs <= 0)
5830             {
5831               cum->mmx_nregs = 0;
5832               cum->mmx_regno = 0;
5833             }
5834         }
5835       break;
5836     }
5837 }
5838
5839 static void
5840 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5841                          tree type, HOST_WIDE_INT words, int named)
5842 {
5843   int int_nregs, sse_nregs;
5844
5845   /* Unnamed 256bit vector mode parameters are passed on stack.  */
5846   if (!named && VALID_AVX256_REG_MODE (mode))
5847     return;
5848
5849   if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
5850     cum->words += words;
5851   else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
5852     {
5853       cum->nregs -= int_nregs;
5854       cum->sse_nregs -= sse_nregs;
5855       cum->regno += int_nregs;
5856       cum->sse_regno += sse_nregs;
5857     }
5858   else
5859     cum->words += words;
5860 }
5861
5862 static void
5863 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
5864                             HOST_WIDE_INT words)
5865 {
5866   /* Otherwise, this should be passed indirect.  */
5867   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
5868
5869   cum->words += words;
5870   if (cum->nregs > 0)
5871     {
5872       cum->nregs -= 1;
5873       cum->regno += 1;
5874     }
5875 }
5876
5877 void
5878 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5879                       tree type, int named)
5880 {
5881   HOST_WIDE_INT bytes, words;
5882
5883   if (mode == BLKmode)
5884     bytes = int_size_in_bytes (type);
5885   else
5886     bytes = GET_MODE_SIZE (mode);
5887   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5888
5889   if (type)
5890     mode = type_natural_mode (type, NULL);
5891
5892   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
5893     function_arg_advance_ms_64 (cum, bytes, words);
5894   else if (TARGET_64BIT)
5895     function_arg_advance_64 (cum, mode, type, words, named);
5896   else
5897     function_arg_advance_32 (cum, mode, type, bytes, words);
5898 }
5899
5900 /* Define where to put the arguments to a function.
5901    Value is zero to push the argument on the stack,
5902    or a hard register in which to store the argument.
5903
5904    MODE is the argument's machine mode.
5905    TYPE is the data type of the argument (as a tree).
5906     This is null for libcalls where that information may
5907     not be available.
5908    CUM is a variable of type CUMULATIVE_ARGS which gives info about
5909     the preceding args and about the function being called.
5910    NAMED is nonzero if this argument is a named parameter
5911     (otherwise it is an extra parameter matching an ellipsis).  */
5912
5913 static rtx
5914 function_arg_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5915                  enum machine_mode orig_mode, tree type,
5916                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
5917 {
5918   static bool warnedsse, warnedmmx;
5919
5920   /* Avoid the AL settings for the Unix64 ABI.  */
5921   if (mode == VOIDmode)
5922     return constm1_rtx;
5923
5924   switch (mode)
5925     {
5926     default:
5927       break;
5928
5929     case BLKmode:
5930       if (bytes < 0)
5931         break;
5932       /* FALLTHRU */
5933     case DImode:
5934     case SImode:
5935     case HImode:
5936     case QImode:
5937       if (words <= cum->nregs)
5938         {
5939           int regno = cum->regno;
5940
5941           /* Fastcall allocates the first two DWORD (SImode) or
5942             smaller arguments to ECX and EDX if it isn't an
5943             aggregate type .  */
5944           if (cum->fastcall)
5945             {
5946               if (mode == BLKmode
5947                   || mode == DImode
5948                   || (type && AGGREGATE_TYPE_P (type)))
5949                 break;
5950
5951               /* ECX not EAX is the first allocated register.  */
5952               if (regno == AX_REG)
5953                 regno = CX_REG;
5954             }
5955           return gen_rtx_REG (mode, regno);
5956         }
5957       break;
5958
5959     case DFmode:
5960       if (cum->float_in_sse < 2)
5961         break;
5962     case SFmode:
5963       if (cum->float_in_sse < 1)
5964         break;
5965       /* FALLTHRU */
5966     case TImode:
5967       /* In 32bit, we pass TImode in xmm registers.  */
5968     case V16QImode:
5969     case V8HImode:
5970     case V4SImode:
5971     case V2DImode:
5972     case V4SFmode:
5973     case V2DFmode:
5974       if (!type || !AGGREGATE_TYPE_P (type))
5975         {
5976           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
5977             {
5978               warnedsse = true;
5979               warning (0, "SSE vector argument without SSE enabled "
5980                        "changes the ABI");
5981             }
5982           if (cum->sse_nregs)
5983             return gen_reg_or_parallel (mode, orig_mode,
5984                                         cum->sse_regno + FIRST_SSE_REG);
5985         }
5986       break;
5987
5988     case OImode:
5989       /* OImode shouldn't be used directly.  */
5990       gcc_unreachable ();
5991
5992     case V8SFmode:
5993     case V8SImode:
5994     case V32QImode:
5995     case V16HImode:
5996     case V4DFmode:
5997     case V4DImode:
5998       if (!type || !AGGREGATE_TYPE_P (type))
5999         {
6000           if (cum->sse_nregs)
6001             return gen_reg_or_parallel (mode, orig_mode,
6002                                         cum->sse_regno + FIRST_SSE_REG);
6003         }
6004       break;
6005
6006     case V8QImode:
6007     case V4HImode:
6008     case V2SImode:
6009     case V2SFmode:
6010     case V1TImode:
6011     case V1DImode:
6012       if (!type || !AGGREGATE_TYPE_P (type))
6013         {
6014           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6015             {
6016               warnedmmx = true;
6017               warning (0, "MMX vector argument without MMX enabled "
6018                        "changes the ABI");
6019             }
6020           if (cum->mmx_nregs)
6021             return gen_reg_or_parallel (mode, orig_mode,
6022                                         cum->mmx_regno + FIRST_MMX_REG);
6023         }
6024       break;
6025     }
6026
6027   return NULL_RTX;
6028 }
6029
6030 static rtx
6031 function_arg_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6032                  enum machine_mode orig_mode, tree type, int named)
6033 {
6034   /* Handle a hidden AL argument containing number of registers
6035      for varargs x86-64 functions.  */
6036   if (mode == VOIDmode)
6037     return GEN_INT (cum->maybe_vaarg
6038                     ? (cum->sse_nregs < 0
6039                        ? (cum->call_abi == ix86_abi
6040                           ? SSE_REGPARM_MAX
6041                           : (ix86_abi != SYSV_ABI
6042                              ? X86_64_SSE_REGPARM_MAX
6043                              : X86_64_MS_SSE_REGPARM_MAX))
6044                        : cum->sse_regno)
6045                     : -1);
6046
6047   switch (mode)
6048     {
6049     default:
6050       break;
6051
6052     case V8SFmode:
6053     case V8SImode:
6054     case V32QImode:
6055     case V16HImode:
6056     case V4DFmode:
6057     case V4DImode:
6058       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6059       if (!named)
6060         return NULL;
6061       break;
6062     }
6063
6064   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6065                               cum->sse_nregs,
6066                               &x86_64_int_parameter_registers [cum->regno],
6067                               cum->sse_regno);
6068 }
6069
6070 static rtx
6071 function_arg_ms_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6072                     enum machine_mode orig_mode, int named,
6073                     HOST_WIDE_INT bytes)
6074 {
6075   unsigned int regno;
6076
6077   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6078      We use value of -2 to specify that current function call is MSABI.  */
6079   if (mode == VOIDmode)
6080     return GEN_INT (-2);
6081
6082   /* If we've run out of registers, it goes on the stack.  */
6083   if (cum->nregs == 0)
6084     return NULL_RTX;
6085
6086   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
6087
6088   /* Only floating point modes are passed in anything but integer regs.  */
6089   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
6090     {
6091       if (named)
6092         regno = cum->regno + FIRST_SSE_REG;
6093       else
6094         {
6095           rtx t1, t2;
6096
6097           /* Unnamed floating parameters are passed in both the
6098              SSE and integer registers.  */
6099           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
6100           t2 = gen_rtx_REG (mode, regno);
6101           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
6102           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
6103           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
6104         }
6105     }
6106   /* Handle aggregated types passed in register.  */
6107   if (orig_mode == BLKmode)
6108     {
6109       if (bytes > 0 && bytes <= 8)
6110         mode = (bytes > 4 ? DImode : SImode);
6111       if (mode == BLKmode)
6112         mode = DImode;
6113     }
6114
6115   return gen_reg_or_parallel (mode, orig_mode, regno);
6116 }
6117
6118 rtx
6119 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
6120               tree type, int named)
6121 {
6122   enum machine_mode mode = omode;
6123   HOST_WIDE_INT bytes, words;
6124
6125   if (mode == BLKmode)
6126     bytes = int_size_in_bytes (type);
6127   else
6128     bytes = GET_MODE_SIZE (mode);
6129   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6130
6131   /* To simplify the code below, represent vector types with a vector mode
6132      even if MMX/SSE are not active.  */
6133   if (type && TREE_CODE (type) == VECTOR_TYPE)
6134     mode = type_natural_mode (type, cum);
6135
6136   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6137     return function_arg_ms_64 (cum, mode, omode, named, bytes);
6138   else if (TARGET_64BIT)
6139     return function_arg_64 (cum, mode, omode, type, named);
6140   else
6141     return function_arg_32 (cum, mode, omode, type, bytes, words);
6142 }
6143
6144 /* A C expression that indicates when an argument must be passed by
6145    reference.  If nonzero for an argument, a copy of that argument is
6146    made in memory and a pointer to the argument is passed instead of
6147    the argument itself.  The pointer is passed in whatever way is
6148    appropriate for passing a pointer to that type.  */
6149
6150 static bool
6151 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
6152                         enum machine_mode mode ATTRIBUTE_UNUSED,
6153                         const_tree type, bool named ATTRIBUTE_UNUSED)
6154 {
6155   /* See Windows x64 Software Convention.  */
6156   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6157     {
6158       int msize = (int) GET_MODE_SIZE (mode);
6159       if (type)
6160         {
6161           /* Arrays are passed by reference.  */
6162           if (TREE_CODE (type) == ARRAY_TYPE)
6163             return true;
6164
6165           if (AGGREGATE_TYPE_P (type))
6166             {
6167               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
6168                  are passed by reference.  */
6169               msize = int_size_in_bytes (type);
6170             }
6171         }
6172
6173       /* __m128 is passed by reference.  */
6174       switch (msize) {
6175       case 1: case 2: case 4: case 8:
6176         break;
6177       default:
6178         return true;
6179       }
6180     }
6181   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
6182     return 1;
6183
6184   return 0;
6185 }
6186
6187 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
6188    ABI.  */
6189 static bool
6190 contains_aligned_value_p (tree type)
6191 {
6192   enum machine_mode mode = TYPE_MODE (type);
6193   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
6194        || mode == TDmode
6195        || mode == TFmode
6196        || mode == TCmode)
6197       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
6198     return true;
6199   if (TYPE_ALIGN (type) < 128)
6200     return false;
6201
6202   if (AGGREGATE_TYPE_P (type))
6203     {
6204       /* Walk the aggregates recursively.  */
6205       switch (TREE_CODE (type))
6206         {
6207         case RECORD_TYPE:
6208         case UNION_TYPE:
6209         case QUAL_UNION_TYPE:
6210           {
6211             tree field;
6212
6213             /* Walk all the structure fields.  */
6214             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6215               {
6216                 if (TREE_CODE (field) == FIELD_DECL
6217                     && contains_aligned_value_p (TREE_TYPE (field)))
6218                   return true;
6219               }
6220             break;
6221           }
6222
6223         case ARRAY_TYPE:
6224           /* Just for use if some languages passes arrays by value.  */
6225           if (contains_aligned_value_p (TREE_TYPE (type)))
6226             return true;
6227           break;
6228
6229         default:
6230           gcc_unreachable ();
6231         }
6232     }
6233   return false;
6234 }
6235
6236 /* Gives the alignment boundary, in bits, of an argument with the
6237    specified mode and type.  */
6238
6239 int
6240 ix86_function_arg_boundary (enum machine_mode mode, tree type)
6241 {
6242   int align;
6243   if (type)
6244     {
6245       /* Since canonical type is used for call, we convert it to
6246          canonical type if needed.  */
6247       if (!TYPE_STRUCTURAL_EQUALITY_P (type))
6248         type = TYPE_CANONICAL (type);
6249       align = TYPE_ALIGN (type);
6250     }
6251   else
6252     align = GET_MODE_ALIGNMENT (mode);
6253   if (align < PARM_BOUNDARY)
6254     align = PARM_BOUNDARY;
6255   /* In 32bit, only _Decimal128 and __float128 are aligned to their
6256      natural boundaries.  */
6257   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
6258     {
6259       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
6260          make an exception for SSE modes since these require 128bit
6261          alignment.
6262
6263          The handling here differs from field_alignment.  ICC aligns MMX
6264          arguments to 4 byte boundaries, while structure fields are aligned
6265          to 8 byte boundaries.  */
6266       if (!type)
6267         {
6268           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
6269             align = PARM_BOUNDARY;
6270         }
6271       else
6272         {
6273           if (!contains_aligned_value_p (type))
6274             align = PARM_BOUNDARY;
6275         }
6276     }
6277   if (align > BIGGEST_ALIGNMENT)
6278     align = BIGGEST_ALIGNMENT;
6279   return align;
6280 }
6281
6282 /* Return true if N is a possible register number of function value.  */
6283
6284 bool
6285 ix86_function_value_regno_p (int regno)
6286 {
6287   switch (regno)
6288     {
6289     case 0:
6290       return true;
6291
6292     case FIRST_FLOAT_REG:
6293       /* TODO: The function should depend on current function ABI but
6294        builtins.c would need updating then. Therefore we use the
6295        default ABI.  */
6296       if (TARGET_64BIT && ix86_abi == MS_ABI)
6297         return false;
6298       return TARGET_FLOAT_RETURNS_IN_80387;
6299
6300     case FIRST_SSE_REG:
6301       return TARGET_SSE;
6302
6303     case FIRST_MMX_REG:
6304       if (TARGET_MACHO || TARGET_64BIT)
6305         return false;
6306       return TARGET_MMX;
6307     }
6308
6309   return false;
6310 }
6311
6312 /* Define how to find the value returned by a function.
6313    VALTYPE is the data type of the value (as a tree).
6314    If the precise function being called is known, FUNC is its FUNCTION_DECL;
6315    otherwise, FUNC is 0.  */
6316
6317 static rtx
6318 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
6319                    const_tree fntype, const_tree fn)
6320 {
6321   unsigned int regno;
6322
6323   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
6324      we normally prevent this case when mmx is not available.  However
6325      some ABIs may require the result to be returned like DImode.  */
6326   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
6327     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
6328
6329   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
6330      we prevent this case when sse is not available.  However some ABIs
6331      may require the result to be returned like integer TImode.  */
6332   else if (mode == TImode
6333            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
6334     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
6335
6336   /* 32-byte vector modes in %ymm0.   */
6337   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
6338     regno = TARGET_AVX ? FIRST_SSE_REG : 0;
6339
6340   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
6341   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
6342     regno = FIRST_FLOAT_REG;
6343   else
6344     /* Most things go in %eax.  */
6345     regno = AX_REG;
6346
6347   /* Override FP return register with %xmm0 for local functions when
6348      SSE math is enabled or for functions with sseregparm attribute.  */
6349   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
6350     {
6351       int sse_level = ix86_function_sseregparm (fntype, fn, false);
6352       if ((sse_level >= 1 && mode == SFmode)
6353           || (sse_level == 2 && mode == DFmode))
6354         regno = FIRST_SSE_REG;
6355     }
6356
6357   /* OImode shouldn't be used directly.  */
6358   gcc_assert (mode != OImode);
6359
6360   return gen_rtx_REG (orig_mode, regno);
6361 }
6362
6363 static rtx
6364 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
6365                    const_tree valtype)
6366 {
6367   rtx ret;
6368
6369   /* Handle libcalls, which don't provide a type node.  */
6370   if (valtype == NULL)
6371     {
6372       switch (mode)
6373         {
6374         case SFmode:
6375         case SCmode:
6376         case DFmode:
6377         case DCmode:
6378         case TFmode:
6379         case SDmode:
6380         case DDmode:
6381         case TDmode:
6382           return gen_rtx_REG (mode, FIRST_SSE_REG);
6383         case XFmode:
6384         case XCmode:
6385           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
6386         case TCmode:
6387           return NULL;
6388         default:
6389           return gen_rtx_REG (mode, AX_REG);
6390         }
6391     }
6392
6393   ret = construct_container (mode, orig_mode, valtype, 1,
6394                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
6395                              x86_64_int_return_registers, 0);
6396
6397   /* For zero sized structures, construct_container returns NULL, but we
6398      need to keep rest of compiler happy by returning meaningful value.  */
6399   if (!ret)
6400     ret = gen_rtx_REG (orig_mode, AX_REG);
6401
6402   return ret;
6403 }
6404
6405 static rtx
6406 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
6407 {
6408   unsigned int regno = AX_REG;
6409
6410   if (TARGET_SSE)
6411     {
6412       switch (GET_MODE_SIZE (mode))
6413         {
6414         case 16:
6415           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
6416              && !COMPLEX_MODE_P (mode))
6417             regno = FIRST_SSE_REG;
6418           break;
6419         case 8:
6420         case 4:
6421           if (mode == SFmode || mode == DFmode)
6422             regno = FIRST_SSE_REG;
6423           break;
6424         default:
6425           break;
6426         }
6427     }
6428   return gen_rtx_REG (orig_mode, regno);
6429 }
6430
6431 static rtx
6432 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
6433                        enum machine_mode orig_mode, enum machine_mode mode)
6434 {
6435   const_tree fn, fntype;
6436
6437   fn = NULL_TREE;
6438   if (fntype_or_decl && DECL_P (fntype_or_decl))
6439     fn = fntype_or_decl;
6440   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
6441
6442   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
6443     return function_value_ms_64 (orig_mode, mode);
6444   else if (TARGET_64BIT)
6445     return function_value_64 (orig_mode, mode, valtype);
6446   else
6447     return function_value_32 (orig_mode, mode, fntype, fn);
6448 }
6449
6450 static rtx
6451 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
6452                      bool outgoing ATTRIBUTE_UNUSED)
6453 {
6454   enum machine_mode mode, orig_mode;
6455
6456   orig_mode = TYPE_MODE (valtype);
6457   mode = type_natural_mode (valtype, NULL);
6458   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
6459 }
6460
6461 rtx
6462 ix86_libcall_value (enum machine_mode mode)
6463 {
6464   return ix86_function_value_1 (NULL, NULL, mode, mode);
6465 }
6466
6467 /* Return true iff type is returned in memory.  */
6468
6469 static int ATTRIBUTE_UNUSED
6470 return_in_memory_32 (const_tree type, enum machine_mode mode)
6471 {
6472   HOST_WIDE_INT size;
6473
6474   if (mode == BLKmode)
6475     return 1;
6476
6477   size = int_size_in_bytes (type);
6478
6479   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
6480     return 0;
6481
6482   if (VECTOR_MODE_P (mode) || mode == TImode)
6483     {
6484       /* User-created vectors small enough to fit in EAX.  */
6485       if (size < 8)
6486         return 0;
6487
6488       /* MMX/3dNow values are returned in MM0,
6489          except when it doesn't exits.  */
6490       if (size == 8)
6491         return (TARGET_MMX ? 0 : 1);
6492
6493       /* SSE values are returned in XMM0, except when it doesn't exist.  */
6494       if (size == 16)
6495         return (TARGET_SSE ? 0 : 1);
6496
6497       /* AVX values are returned in YMM0, except when it doesn't exist.  */
6498       if (size == 32)
6499         return TARGET_AVX ? 0 : 1;
6500     }
6501
6502   if (mode == XFmode)
6503     return 0;
6504
6505   if (size > 12)
6506     return 1;
6507
6508   /* OImode shouldn't be used directly.  */
6509   gcc_assert (mode != OImode);
6510
6511   return 0;
6512 }
6513
6514 static int ATTRIBUTE_UNUSED
6515 return_in_memory_64 (const_tree type, enum machine_mode mode)
6516 {
6517   int needed_intregs, needed_sseregs;
6518   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
6519 }
6520
6521 static int ATTRIBUTE_UNUSED
6522 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
6523 {
6524   HOST_WIDE_INT size = int_size_in_bytes (type);
6525
6526   /* __m128 is returned in xmm0.  */
6527   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
6528       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
6529     return 0;
6530
6531   /* Otherwise, the size must be exactly in [1248]. */
6532   return (size != 1 && size != 2 && size != 4 && size != 8);
6533 }
6534
6535 static bool
6536 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
6537 {
6538 #ifdef SUBTARGET_RETURN_IN_MEMORY
6539   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
6540 #else
6541   const enum machine_mode mode = type_natural_mode (type, NULL);
6542  
6543   if (TARGET_64BIT)
6544     {
6545       if (ix86_function_type_abi (fntype) == MS_ABI)
6546         return return_in_memory_ms_64 (type, mode);
6547       else
6548         return return_in_memory_64 (type, mode);
6549     }
6550   else
6551     return return_in_memory_32 (type, mode);
6552 #endif
6553 }
6554
6555 /* Return false iff TYPE is returned in memory.  This version is used
6556    on Solaris 10.  It is similar to the generic ix86_return_in_memory,
6557    but differs notably in that when MMX is available, 8-byte vectors
6558    are returned in memory, rather than in MMX registers.  */
6559
6560 bool
6561 ix86_sol10_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
6562 {
6563   int size;
6564   enum machine_mode mode = type_natural_mode (type, NULL);
6565
6566   if (TARGET_64BIT)
6567     return return_in_memory_64 (type, mode);
6568
6569   if (mode == BLKmode)
6570     return 1;
6571
6572   size = int_size_in_bytes (type);
6573
6574   if (VECTOR_MODE_P (mode))
6575     {
6576       /* Return in memory only if MMX registers *are* available.  This
6577          seems backwards, but it is consistent with the existing
6578          Solaris x86 ABI.  */
6579       if (size == 8)
6580         return TARGET_MMX;
6581       if (size == 16)
6582         return !TARGET_SSE;
6583     }
6584   else if (mode == TImode)
6585     return !TARGET_SSE;
6586   else if (mode == XFmode)
6587     return 0;
6588
6589   return size > 12;
6590 }
6591
6592 /* When returning SSE vector types, we have a choice of either
6593      (1) being abi incompatible with a -march switch, or
6594      (2) generating an error.
6595    Given no good solution, I think the safest thing is one warning.
6596    The user won't be able to use -Werror, but....
6597
6598    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
6599    called in response to actually generating a caller or callee that
6600    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
6601    via aggregate_value_p for general type probing from tree-ssa.  */
6602
6603 static rtx
6604 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
6605 {
6606   static bool warnedsse, warnedmmx;
6607
6608   if (!TARGET_64BIT && type)
6609     {
6610       /* Look at the return type of the function, not the function type.  */
6611       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
6612
6613       if (!TARGET_SSE && !warnedsse)
6614         {
6615           if (mode == TImode
6616               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
6617             {
6618               warnedsse = true;
6619               warning (0, "SSE vector return without SSE enabled "
6620                        "changes the ABI");
6621             }
6622         }
6623
6624       if (!TARGET_MMX && !warnedmmx)
6625         {
6626           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
6627             {
6628               warnedmmx = true;
6629               warning (0, "MMX vector return without MMX enabled "
6630                        "changes the ABI");
6631             }
6632         }
6633     }
6634
6635   return NULL;
6636 }
6637
6638 \f
6639 /* Create the va_list data type.  */
6640
6641 /* Returns the calling convention specific va_list date type.
6642    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
6643
6644 static tree
6645 ix86_build_builtin_va_list_abi (enum calling_abi abi)
6646 {
6647   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
6648
6649   /* For i386 we use plain pointer to argument area.  */
6650   if (!TARGET_64BIT || abi == MS_ABI)
6651     return build_pointer_type (char_type_node);
6652
6653   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
6654   type_decl = build_decl (BUILTINS_LOCATION,
6655                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
6656
6657   f_gpr = build_decl (BUILTINS_LOCATION,
6658                       FIELD_DECL, get_identifier ("gp_offset"),
6659                       unsigned_type_node);
6660   f_fpr = build_decl (BUILTINS_LOCATION,
6661                       FIELD_DECL, get_identifier ("fp_offset"),
6662                       unsigned_type_node);
6663   f_ovf = build_decl (BUILTINS_LOCATION,
6664                       FIELD_DECL, get_identifier ("overflow_arg_area"),
6665                       ptr_type_node);
6666   f_sav = build_decl (BUILTINS_LOCATION,
6667                       FIELD_DECL, get_identifier ("reg_save_area"),
6668                       ptr_type_node);
6669
6670   va_list_gpr_counter_field = f_gpr;
6671   va_list_fpr_counter_field = f_fpr;
6672
6673   DECL_FIELD_CONTEXT (f_gpr) = record;
6674   DECL_FIELD_CONTEXT (f_fpr) = record;
6675   DECL_FIELD_CONTEXT (f_ovf) = record;
6676   DECL_FIELD_CONTEXT (f_sav) = record;
6677
6678   TREE_CHAIN (record) = type_decl;
6679   TYPE_NAME (record) = type_decl;
6680   TYPE_FIELDS (record) = f_gpr;
6681   TREE_CHAIN (f_gpr) = f_fpr;
6682   TREE_CHAIN (f_fpr) = f_ovf;
6683   TREE_CHAIN (f_ovf) = f_sav;
6684
6685   layout_type (record);
6686
6687   /* The correct type is an array type of one element.  */
6688   return build_array_type (record, build_index_type (size_zero_node));
6689 }
6690
6691 /* Setup the builtin va_list data type and for 64-bit the additional
6692    calling convention specific va_list data types.  */
6693
6694 static tree
6695 ix86_build_builtin_va_list (void)
6696 {
6697   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
6698
6699   /* Initialize abi specific va_list builtin types.  */
6700   if (TARGET_64BIT)
6701     {
6702       tree t;
6703       if (ix86_abi == MS_ABI)
6704         {
6705           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
6706           if (TREE_CODE (t) != RECORD_TYPE)
6707             t = build_variant_type_copy (t);
6708           sysv_va_list_type_node = t;
6709         }
6710       else
6711         {
6712           t = ret;
6713           if (TREE_CODE (t) != RECORD_TYPE)
6714             t = build_variant_type_copy (t);
6715           sysv_va_list_type_node = t;
6716         }
6717       if (ix86_abi != MS_ABI)
6718         {
6719           t = ix86_build_builtin_va_list_abi (MS_ABI);
6720           if (TREE_CODE (t) != RECORD_TYPE)
6721             t = build_variant_type_copy (t);
6722           ms_va_list_type_node = t;
6723         }
6724       else
6725         {
6726           t = ret;
6727           if (TREE_CODE (t) != RECORD_TYPE)
6728             t = build_variant_type_copy (t);
6729           ms_va_list_type_node = t;
6730         }
6731     }
6732
6733   return ret;
6734 }
6735
6736 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
6737
6738 static void
6739 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
6740 {
6741   rtx save_area, mem;
6742   rtx label;
6743   rtx label_ref;
6744   rtx tmp_reg;
6745   rtx nsse_reg;
6746   alias_set_type set;
6747   int i;
6748   int regparm = ix86_regparm;
6749
6750   if (cum->call_abi != ix86_abi)
6751     regparm = (ix86_abi != SYSV_ABI
6752                ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
6753
6754   /* GPR size of varargs save area.  */
6755   if (cfun->va_list_gpr_size)
6756     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
6757   else
6758     ix86_varargs_gpr_size = 0;
6759
6760   /* FPR size of varargs save area.  We don't need it if we don't pass
6761      anything in SSE registers.  */
6762   if (cum->sse_nregs && cfun->va_list_fpr_size)
6763     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
6764   else
6765     ix86_varargs_fpr_size = 0;
6766
6767   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
6768     return;
6769
6770   save_area = frame_pointer_rtx;
6771   set = get_varargs_alias_set ();
6772
6773   for (i = cum->regno;
6774        i < regparm
6775        && i < cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
6776        i++)
6777     {
6778       mem = gen_rtx_MEM (Pmode,
6779                          plus_constant (save_area, i * UNITS_PER_WORD));
6780       MEM_NOTRAP_P (mem) = 1;
6781       set_mem_alias_set (mem, set);
6782       emit_move_insn (mem, gen_rtx_REG (Pmode,
6783                                         x86_64_int_parameter_registers[i]));
6784     }
6785
6786   if (ix86_varargs_fpr_size)
6787     {
6788       /* Now emit code to save SSE registers.  The AX parameter contains number
6789          of SSE parameter registers used to call this function.  We use
6790          sse_prologue_save insn template that produces computed jump across
6791          SSE saves.  We need some preparation work to get this working.  */
6792
6793       label = gen_label_rtx ();
6794       label_ref = gen_rtx_LABEL_REF (Pmode, label);
6795
6796       /* Compute address to jump to :
6797          label - eax*4 + nnamed_sse_arguments*4 Or
6798          label - eax*5 + nnamed_sse_arguments*5 for AVX.  */
6799       tmp_reg = gen_reg_rtx (Pmode);
6800       nsse_reg = gen_reg_rtx (Pmode);
6801       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, AX_REG)));
6802       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
6803                               gen_rtx_MULT (Pmode, nsse_reg,
6804                                             GEN_INT (4))));
6805
6806       /* vmovaps is one byte longer than movaps.  */
6807       if (TARGET_AVX)
6808         emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
6809                                 gen_rtx_PLUS (Pmode, tmp_reg,
6810                                               nsse_reg)));
6811
6812       if (cum->sse_regno)
6813         emit_move_insn
6814           (nsse_reg,
6815            gen_rtx_CONST (DImode,
6816                           gen_rtx_PLUS (DImode,
6817                                         label_ref,
6818                                         GEN_INT (cum->sse_regno
6819                                                  * (TARGET_AVX ? 5 : 4)))));
6820       else
6821         emit_move_insn (nsse_reg, label_ref);
6822       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
6823
6824       /* Compute address of memory block we save into.  We always use pointer
6825          pointing 127 bytes after first byte to store - this is needed to keep
6826          instruction size limited by 4 bytes (5 bytes for AVX) with one
6827          byte displacement.  */
6828       tmp_reg = gen_reg_rtx (Pmode);
6829       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
6830                               plus_constant (save_area,
6831                                              ix86_varargs_gpr_size + 127)));
6832       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
6833       MEM_NOTRAP_P (mem) = 1;
6834       set_mem_alias_set (mem, set);
6835       set_mem_align (mem, BITS_PER_WORD);
6836
6837       /* And finally do the dirty job!  */
6838       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
6839                                         GEN_INT (cum->sse_regno), label));
6840     }
6841 }
6842
6843 static void
6844 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
6845 {
6846   alias_set_type set = get_varargs_alias_set ();
6847   int i;
6848
6849   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
6850     {
6851       rtx reg, mem;
6852
6853       mem = gen_rtx_MEM (Pmode,
6854                          plus_constant (virtual_incoming_args_rtx,
6855                                         i * UNITS_PER_WORD));
6856       MEM_NOTRAP_P (mem) = 1;
6857       set_mem_alias_set (mem, set);
6858
6859       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
6860       emit_move_insn (mem, reg);
6861     }
6862 }
6863
6864 static void
6865 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6866                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
6867                              int no_rtl)
6868 {
6869   CUMULATIVE_ARGS next_cum;
6870   tree fntype;
6871
6872   /* This argument doesn't appear to be used anymore.  Which is good,
6873      because the old code here didn't suppress rtl generation.  */
6874   gcc_assert (!no_rtl);
6875
6876   if (!TARGET_64BIT)
6877     return;
6878
6879   fntype = TREE_TYPE (current_function_decl);
6880
6881   /* For varargs, we do not want to skip the dummy va_dcl argument.
6882      For stdargs, we do want to skip the last named argument.  */
6883   next_cum = *cum;
6884   if (stdarg_p (fntype))
6885     function_arg_advance (&next_cum, mode, type, 1);
6886
6887   if (cum->call_abi == MS_ABI)
6888     setup_incoming_varargs_ms_64 (&next_cum);
6889   else
6890     setup_incoming_varargs_64 (&next_cum);
6891 }
6892
6893 /* Checks if TYPE is of kind va_list char *.  */
6894
6895 static bool
6896 is_va_list_char_pointer (tree type)
6897 {
6898   tree canonic;
6899
6900   /* For 32-bit it is always true.  */
6901   if (!TARGET_64BIT)
6902     return true;
6903   canonic = ix86_canonical_va_list_type (type);
6904   return (canonic == ms_va_list_type_node
6905           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
6906 }
6907
6908 /* Implement va_start.  */
6909
6910 static void
6911 ix86_va_start (tree valist, rtx nextarg)
6912 {
6913   HOST_WIDE_INT words, n_gpr, n_fpr;
6914   tree f_gpr, f_fpr, f_ovf, f_sav;
6915   tree gpr, fpr, ovf, sav, t;
6916   tree type;
6917
6918   /* Only 64bit target needs something special.  */
6919   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
6920     {
6921       std_expand_builtin_va_start (valist, nextarg);
6922       return;
6923     }
6924
6925   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
6926   f_fpr = TREE_CHAIN (f_gpr);
6927   f_ovf = TREE_CHAIN (f_fpr);
6928   f_sav = TREE_CHAIN (f_ovf);
6929
6930   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
6931   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
6932   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
6933   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
6934   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
6935
6936   /* Count number of gp and fp argument registers used.  */
6937   words = crtl->args.info.words;
6938   n_gpr = crtl->args.info.regno;
6939   n_fpr = crtl->args.info.sse_regno;
6940
6941   if (cfun->va_list_gpr_size)
6942     {
6943       type = TREE_TYPE (gpr);
6944       t = build2 (MODIFY_EXPR, type,
6945                   gpr, build_int_cst (type, n_gpr * 8));
6946       TREE_SIDE_EFFECTS (t) = 1;
6947       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6948     }
6949
6950   if (TARGET_SSE && cfun->va_list_fpr_size)
6951     {
6952       type = TREE_TYPE (fpr);
6953       t = build2 (MODIFY_EXPR, type, fpr,
6954                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
6955       TREE_SIDE_EFFECTS (t) = 1;
6956       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6957     }
6958
6959   /* Find the overflow area.  */
6960   type = TREE_TYPE (ovf);
6961   t = make_tree (type, crtl->args.internal_arg_pointer);
6962   if (words != 0)
6963     t = build2 (POINTER_PLUS_EXPR, type, t,
6964                 size_int (words * UNITS_PER_WORD));
6965   t = build2 (MODIFY_EXPR, type, ovf, t);
6966   TREE_SIDE_EFFECTS (t) = 1;
6967   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6968
6969   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
6970     {
6971       /* Find the register save area.
6972          Prologue of the function save it right above stack frame.  */
6973       type = TREE_TYPE (sav);
6974       t = make_tree (type, frame_pointer_rtx);
6975       if (!ix86_varargs_gpr_size)
6976         t = build2 (POINTER_PLUS_EXPR, type, t,
6977                     size_int (-8 * X86_64_REGPARM_MAX));
6978       t = build2 (MODIFY_EXPR, type, sav, t);
6979       TREE_SIDE_EFFECTS (t) = 1;
6980       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6981     }
6982 }
6983
6984 /* Implement va_arg.  */
6985
6986 static tree
6987 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
6988                       gimple_seq *post_p)
6989 {
6990   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
6991   tree f_gpr, f_fpr, f_ovf, f_sav;
6992   tree gpr, fpr, ovf, sav, t;
6993   int size, rsize;
6994   tree lab_false, lab_over = NULL_TREE;
6995   tree addr, t2;
6996   rtx container;
6997   int indirect_p = 0;
6998   tree ptrtype;
6999   enum machine_mode nat_mode;
7000   int arg_boundary;
7001
7002   /* Only 64bit target needs something special.  */
7003   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7004     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7005
7006   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7007   f_fpr = TREE_CHAIN (f_gpr);
7008   f_ovf = TREE_CHAIN (f_fpr);
7009   f_sav = TREE_CHAIN (f_ovf);
7010
7011   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
7012                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
7013   valist = build_va_arg_indirect_ref (valist);
7014   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7015   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7016   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7017
7018   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
7019   if (indirect_p)
7020     type = build_pointer_type (type);
7021   size = int_size_in_bytes (type);
7022   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7023
7024   nat_mode = type_natural_mode (type, NULL);
7025   switch (nat_mode)
7026     {
7027     case V8SFmode:
7028     case V8SImode:
7029     case V32QImode:
7030     case V16HImode:
7031     case V4DFmode:
7032     case V4DImode:
7033       /* Unnamed 256bit vector mode parameters are passed on stack.  */
7034       if (ix86_cfun_abi () == SYSV_ABI)
7035         {
7036           container = NULL;
7037           break;
7038         }
7039
7040     default:
7041       container = construct_container (nat_mode, TYPE_MODE (type),
7042                                        type, 0, X86_64_REGPARM_MAX,
7043                                        X86_64_SSE_REGPARM_MAX, intreg,
7044                                        0);
7045       break;
7046     }
7047
7048   /* Pull the value out of the saved registers.  */
7049
7050   addr = create_tmp_var (ptr_type_node, "addr");
7051
7052   if (container)
7053     {
7054       int needed_intregs, needed_sseregs;
7055       bool need_temp;
7056       tree int_addr, sse_addr;
7057
7058       lab_false = create_artificial_label (UNKNOWN_LOCATION);
7059       lab_over = create_artificial_label (UNKNOWN_LOCATION);
7060
7061       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
7062
7063       need_temp = (!REG_P (container)
7064                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
7065                        || TYPE_ALIGN (type) > 128));
7066
7067       /* In case we are passing structure, verify that it is consecutive block
7068          on the register save area.  If not we need to do moves.  */
7069       if (!need_temp && !REG_P (container))
7070         {
7071           /* Verify that all registers are strictly consecutive  */
7072           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
7073             {
7074               int i;
7075
7076               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7077                 {
7078                   rtx slot = XVECEXP (container, 0, i);
7079                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
7080                       || INTVAL (XEXP (slot, 1)) != i * 16)
7081                     need_temp = 1;
7082                 }
7083             }
7084           else
7085             {
7086               int i;
7087
7088               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7089                 {
7090                   rtx slot = XVECEXP (container, 0, i);
7091                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
7092                       || INTVAL (XEXP (slot, 1)) != i * 8)
7093                     need_temp = 1;
7094                 }
7095             }
7096         }
7097       if (!need_temp)
7098         {
7099           int_addr = addr;
7100           sse_addr = addr;
7101         }
7102       else
7103         {
7104           int_addr = create_tmp_var (ptr_type_node, "int_addr");
7105           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
7106         }
7107
7108       /* First ensure that we fit completely in registers.  */
7109       if (needed_intregs)
7110         {
7111           t = build_int_cst (TREE_TYPE (gpr),
7112                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
7113           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
7114           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7115           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7116           gimplify_and_add (t, pre_p);
7117         }
7118       if (needed_sseregs)
7119         {
7120           t = build_int_cst (TREE_TYPE (fpr),
7121                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
7122                              + X86_64_REGPARM_MAX * 8);
7123           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
7124           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7125           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7126           gimplify_and_add (t, pre_p);
7127         }
7128
7129       /* Compute index to start of area used for integer regs.  */
7130       if (needed_intregs)
7131         {
7132           /* int_addr = gpr + sav; */
7133           t = fold_convert (sizetype, gpr);
7134           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
7135           gimplify_assign (int_addr, t, pre_p);
7136         }
7137       if (needed_sseregs)
7138         {
7139           /* sse_addr = fpr + sav; */
7140           t = fold_convert (sizetype, fpr);
7141           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
7142           gimplify_assign (sse_addr, t, pre_p);
7143         }
7144       if (need_temp)
7145         {
7146           int i;
7147           tree temp = create_tmp_var (type, "va_arg_tmp");
7148
7149           /* addr = &temp; */
7150           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
7151           gimplify_assign (addr, t, pre_p);
7152
7153           for (i = 0; i < XVECLEN (container, 0); i++)
7154             {
7155               rtx slot = XVECEXP (container, 0, i);
7156               rtx reg = XEXP (slot, 0);
7157               enum machine_mode mode = GET_MODE (reg);
7158               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
7159               tree addr_type = build_pointer_type (piece_type);
7160               tree daddr_type = build_pointer_type_for_mode (piece_type,
7161                                                              ptr_mode, true);
7162               tree src_addr, src;
7163               int src_offset;
7164               tree dest_addr, dest;
7165
7166               if (SSE_REGNO_P (REGNO (reg)))
7167                 {
7168                   src_addr = sse_addr;
7169                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
7170                 }
7171               else
7172                 {
7173                   src_addr = int_addr;
7174                   src_offset = REGNO (reg) * 8;
7175                 }
7176               src_addr = fold_convert (addr_type, src_addr);
7177               src_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, src_addr,
7178                                       size_int (src_offset));
7179               src = build_va_arg_indirect_ref (src_addr);
7180
7181               dest_addr = fold_convert (daddr_type, addr);
7182               dest_addr = fold_build2 (POINTER_PLUS_EXPR, daddr_type, dest_addr,
7183                                        size_int (INTVAL (XEXP (slot, 1))));
7184               dest = build_va_arg_indirect_ref (dest_addr);
7185
7186               gimplify_assign (dest, src, pre_p);
7187             }
7188         }
7189
7190       if (needed_intregs)
7191         {
7192           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
7193                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
7194           gimplify_assign (gpr, t, pre_p);
7195         }
7196
7197       if (needed_sseregs)
7198         {
7199           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
7200                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
7201           gimplify_assign (fpr, t, pre_p);
7202         }
7203
7204       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
7205
7206       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
7207     }
7208
7209   /* ... otherwise out of the overflow area.  */
7210
7211   /* When we align parameter on stack for caller, if the parameter
7212      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
7213      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
7214      here with caller.  */
7215   arg_boundary = FUNCTION_ARG_BOUNDARY (VOIDmode, type);
7216   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
7217     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
7218
7219   /* Care for on-stack alignment if needed.  */
7220   if (arg_boundary <= 64
7221       || integer_zerop (TYPE_SIZE (type)))
7222     t = ovf;
7223  else
7224     {
7225       HOST_WIDE_INT align = arg_boundary / 8;
7226       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), ovf,
7227                   size_int (align - 1));
7228       t = fold_convert (sizetype, t);
7229       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
7230                   size_int (-align));
7231       t = fold_convert (TREE_TYPE (ovf), t);
7232     }
7233   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
7234   gimplify_assign (addr, t, pre_p);
7235
7236   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
7237               size_int (rsize * UNITS_PER_WORD));
7238   gimplify_assign (unshare_expr (ovf), t, pre_p);
7239
7240   if (container)
7241     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
7242
7243   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
7244   addr = fold_convert (ptrtype, addr);
7245
7246   if (indirect_p)
7247     addr = build_va_arg_indirect_ref (addr);
7248   return build_va_arg_indirect_ref (addr);
7249 }
7250 \f
7251 /* Return nonzero if OPNUM's MEM should be matched
7252    in movabs* patterns.  */
7253
7254 int
7255 ix86_check_movabs (rtx insn, int opnum)
7256 {
7257   rtx set, mem;
7258
7259   set = PATTERN (insn);
7260   if (GET_CODE (set) == PARALLEL)
7261     set = XVECEXP (set, 0, 0);
7262   gcc_assert (GET_CODE (set) == SET);
7263   mem = XEXP (set, opnum);
7264   while (GET_CODE (mem) == SUBREG)
7265     mem = SUBREG_REG (mem);
7266   gcc_assert (MEM_P (mem));
7267   return (volatile_ok || !MEM_VOLATILE_P (mem));
7268 }
7269 \f
7270 /* Initialize the table of extra 80387 mathematical constants.  */
7271
7272 static void
7273 init_ext_80387_constants (void)
7274 {
7275   static const char * cst[5] =
7276   {
7277     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
7278     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
7279     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
7280     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
7281     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
7282   };
7283   int i;
7284
7285   for (i = 0; i < 5; i++)
7286     {
7287       real_from_string (&ext_80387_constants_table[i], cst[i]);
7288       /* Ensure each constant is rounded to XFmode precision.  */
7289       real_convert (&ext_80387_constants_table[i],
7290                     XFmode, &ext_80387_constants_table[i]);
7291     }
7292
7293   ext_80387_constants_init = 1;
7294 }
7295
7296 /* Return true if the constant is something that can be loaded with
7297    a special instruction.  */
7298
7299 int
7300 standard_80387_constant_p (rtx x)
7301 {
7302   enum machine_mode mode = GET_MODE (x);
7303
7304   REAL_VALUE_TYPE r;
7305
7306   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
7307     return -1;
7308
7309   if (x == CONST0_RTX (mode))
7310     return 1;
7311   if (x == CONST1_RTX (mode))
7312     return 2;
7313
7314   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7315
7316   /* For XFmode constants, try to find a special 80387 instruction when
7317      optimizing for size or on those CPUs that benefit from them.  */
7318   if (mode == XFmode
7319       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
7320     {
7321       int i;
7322
7323       if (! ext_80387_constants_init)
7324         init_ext_80387_constants ();
7325
7326       for (i = 0; i < 5; i++)
7327         if (real_identical (&r, &ext_80387_constants_table[i]))
7328           return i + 3;
7329     }
7330
7331   /* Load of the constant -0.0 or -1.0 will be split as
7332      fldz;fchs or fld1;fchs sequence.  */
7333   if (real_isnegzero (&r))
7334     return 8;
7335   if (real_identical (&r, &dconstm1))
7336     return 9;
7337
7338   return 0;
7339 }
7340
7341 /* Return the opcode of the special instruction to be used to load
7342    the constant X.  */
7343
7344 const char *
7345 standard_80387_constant_opcode (rtx x)
7346 {
7347   switch (standard_80387_constant_p (x))
7348     {
7349     case 1:
7350       return "fldz";
7351     case 2:
7352       return "fld1";
7353     case 3:
7354       return "fldlg2";
7355     case 4:
7356       return "fldln2";
7357     case 5:
7358       return "fldl2e";
7359     case 6:
7360       return "fldl2t";
7361     case 7:
7362       return "fldpi";
7363     case 8:
7364     case 9:
7365       return "#";
7366     default:
7367       gcc_unreachable ();
7368     }
7369 }
7370
7371 /* Return the CONST_DOUBLE representing the 80387 constant that is
7372    loaded by the specified special instruction.  The argument IDX
7373    matches the return value from standard_80387_constant_p.  */
7374
7375 rtx
7376 standard_80387_constant_rtx (int idx)
7377 {
7378   int i;
7379
7380   if (! ext_80387_constants_init)
7381     init_ext_80387_constants ();
7382
7383   switch (idx)
7384     {
7385     case 3:
7386     case 4:
7387     case 5:
7388     case 6:
7389     case 7:
7390       i = idx - 3;
7391       break;
7392
7393     default:
7394       gcc_unreachable ();
7395     }
7396
7397   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
7398                                        XFmode);
7399 }
7400
7401 /* Return 1 if X is all 0s and 2 if x is all 1s
7402    in supported SSE vector mode.  */
7403
7404 int
7405 standard_sse_constant_p (rtx x)
7406 {
7407   enum machine_mode mode = GET_MODE (x);
7408
7409   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
7410     return 1;
7411   if (vector_all_ones_operand (x, mode))
7412     switch (mode)
7413       {
7414       case V16QImode:
7415       case V8HImode:
7416       case V4SImode:
7417       case V2DImode:
7418         if (TARGET_SSE2)
7419           return 2;
7420       default:
7421         break;
7422       }
7423
7424   return 0;
7425 }
7426
7427 /* Return the opcode of the special instruction to be used to load
7428    the constant X.  */
7429
7430 const char *
7431 standard_sse_constant_opcode (rtx insn, rtx x)
7432 {
7433   switch (standard_sse_constant_p (x))
7434     {
7435     case 1:
7436       switch (get_attr_mode (insn))
7437         {
7438         case MODE_V4SF:
7439           return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7440         case MODE_V2DF:
7441           return TARGET_AVX ? "vxorpd\t%0, %0, %0" : "xorpd\t%0, %0";
7442         case MODE_TI:
7443           return TARGET_AVX ? "vpxor\t%0, %0, %0" : "pxor\t%0, %0";
7444         case MODE_V8SF:
7445           return "vxorps\t%x0, %x0, %x0";
7446         case MODE_V4DF:
7447           return "vxorpd\t%x0, %x0, %x0";
7448         case MODE_OI:
7449           return "vpxor\t%x0, %x0, %x0";
7450         default:
7451           break;
7452         }
7453     case 2:
7454       return TARGET_AVX ? "vpcmpeqd\t%0, %0, %0" : "pcmpeqd\t%0, %0";
7455     default:
7456       break;
7457     }
7458   gcc_unreachable ();
7459 }
7460
7461 /* Returns 1 if OP contains a symbol reference */
7462
7463 int
7464 symbolic_reference_mentioned_p (rtx op)
7465 {
7466   const char *fmt;
7467   int i;
7468
7469   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
7470     return 1;
7471
7472   fmt = GET_RTX_FORMAT (GET_CODE (op));
7473   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
7474     {
7475       if (fmt[i] == 'E')
7476         {
7477           int j;
7478
7479           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
7480             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
7481               return 1;
7482         }
7483
7484       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
7485         return 1;
7486     }
7487
7488   return 0;
7489 }
7490
7491 /* Return 1 if it is appropriate to emit `ret' instructions in the
7492    body of a function.  Do this only if the epilogue is simple, needing a
7493    couple of insns.  Prior to reloading, we can't tell how many registers
7494    must be saved, so return 0 then.  Return 0 if there is no frame
7495    marker to de-allocate.  */
7496
7497 int
7498 ix86_can_use_return_insn_p (void)
7499 {
7500   struct ix86_frame frame;
7501
7502   if (! reload_completed || frame_pointer_needed)
7503     return 0;
7504
7505   /* Don't allow more than 32 pop, since that's all we can do
7506      with one instruction.  */
7507   if (crtl->args.pops_args
7508       && crtl->args.size >= 32768)
7509     return 0;
7510
7511   ix86_compute_frame_layout (&frame);
7512   return frame.to_allocate == 0 && frame.padding0 == 0
7513          && (frame.nregs + frame.nsseregs) == 0;
7514 }
7515 \f
7516 /* Value should be nonzero if functions must have frame pointers.
7517    Zero means the frame pointer need not be set up (and parms may
7518    be accessed via the stack pointer) in functions that seem suitable.  */
7519
7520 static bool
7521 ix86_frame_pointer_required (void)
7522 {
7523   /* If we accessed previous frames, then the generated code expects
7524      to be able to access the saved ebp value in our frame.  */
7525   if (cfun->machine->accesses_prev_frame)
7526     return true;
7527
7528   /* Several x86 os'es need a frame pointer for other reasons,
7529      usually pertaining to setjmp.  */
7530   if (SUBTARGET_FRAME_POINTER_REQUIRED)
7531     return true;
7532
7533   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
7534      the frame pointer by default.  Turn it back on now if we've not
7535      got a leaf function.  */
7536   if (TARGET_OMIT_LEAF_FRAME_POINTER
7537       && (!current_function_is_leaf
7538           || ix86_current_function_calls_tls_descriptor))
7539     return true;
7540
7541   if (crtl->profile)
7542     return true;
7543
7544   return false;
7545 }
7546
7547 /* Record that the current function accesses previous call frames.  */
7548
7549 void
7550 ix86_setup_frame_addresses (void)
7551 {
7552   cfun->machine->accesses_prev_frame = 1;
7553 }
7554 \f
7555 #ifndef USE_HIDDEN_LINKONCE
7556 # if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
7557 #  define USE_HIDDEN_LINKONCE 1
7558 # else
7559 #  define USE_HIDDEN_LINKONCE 0
7560 # endif
7561 #endif
7562
7563 static int pic_labels_used;
7564
7565 /* Fills in the label name that should be used for a pc thunk for
7566    the given register.  */
7567
7568 static void
7569 get_pc_thunk_name (char name[32], unsigned int regno)
7570 {
7571   gcc_assert (!TARGET_64BIT);
7572
7573   if (USE_HIDDEN_LINKONCE)
7574     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
7575   else
7576     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
7577 }
7578
7579
7580 /* This function generates code for -fpic that loads %ebx with
7581    the return address of the caller and then returns.  */
7582
7583 static void
7584 ix86_code_end (void)
7585 {
7586   rtx xops[2];
7587   int regno;
7588
7589   for (regno = 0; regno < 8; ++regno)
7590     {
7591       char name[32];
7592       tree decl;
7593
7594       if (! ((pic_labels_used >> regno) & 1))
7595         continue;
7596
7597       get_pc_thunk_name (name, regno);
7598
7599       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
7600                          get_identifier (name),
7601                          build_function_type (void_type_node, void_list_node));
7602       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
7603                                        NULL_TREE, void_type_node);
7604       TREE_PUBLIC (decl) = 1;
7605       TREE_STATIC (decl) = 1;
7606
7607 #if TARGET_MACHO
7608       if (TARGET_MACHO)
7609         {
7610           switch_to_section (darwin_sections[text_coal_section]);
7611           fputs ("\t.weak_definition\t", asm_out_file);
7612           assemble_name (asm_out_file, name);
7613           fputs ("\n\t.private_extern\t", asm_out_file);
7614           assemble_name (asm_out_file, name);
7615           fputs ("\n", asm_out_file);
7616           ASM_OUTPUT_LABEL (asm_out_file, name);
7617           DECL_WEAK (decl) = 1;
7618         }
7619       else
7620 #endif
7621       if (USE_HIDDEN_LINKONCE)
7622         {
7623           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
7624
7625           (*targetm.asm_out.unique_section) (decl, 0);
7626           switch_to_section (get_named_section (decl, NULL, 0));
7627
7628           (*targetm.asm_out.globalize_label) (asm_out_file, name);
7629           fputs ("\t.hidden\t", asm_out_file);
7630           assemble_name (asm_out_file, name);
7631           putc ('\n', asm_out_file);
7632           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
7633         }
7634       else
7635         {
7636           switch_to_section (text_section);
7637           ASM_OUTPUT_LABEL (asm_out_file, name);
7638         }
7639
7640       DECL_INITIAL (decl) = make_node (BLOCK);
7641       current_function_decl = decl;
7642       init_function_start (decl);
7643       first_function_block_is_cold = false;
7644       /* Make sure unwind info is emitted for the thunk if needed.  */
7645       final_start_function (emit_barrier (), asm_out_file, 1);
7646
7647       xops[0] = gen_rtx_REG (Pmode, regno);
7648       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
7649       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
7650       output_asm_insn ("ret", xops);
7651       final_end_function ();
7652       init_insn_lengths ();
7653       free_after_compilation (cfun);
7654       set_cfun (NULL);
7655       current_function_decl = NULL;
7656     }
7657 }
7658
7659 /* Emit code for the SET_GOT patterns.  */
7660
7661 const char *
7662 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
7663 {
7664   rtx xops[3];
7665
7666   xops[0] = dest;
7667
7668   if (TARGET_VXWORKS_RTP && flag_pic)
7669     {
7670       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
7671       xops[2] = gen_rtx_MEM (Pmode,
7672                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
7673       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
7674
7675       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
7676          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
7677          an unadorned address.  */
7678       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
7679       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
7680       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
7681       return "";
7682     }
7683
7684   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
7685
7686   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
7687     {
7688       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
7689
7690       if (!flag_pic)
7691         output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
7692       else
7693         {
7694           output_asm_insn ("call\t%a2", xops);
7695 #ifdef DWARF2_UNWIND_INFO
7696           /* The call to next label acts as a push.  */
7697           if (dwarf2out_do_frame ())
7698             {
7699               rtx insn;
7700               start_sequence ();
7701               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7702                                              gen_rtx_PLUS (Pmode,
7703                                                            stack_pointer_rtx,
7704                                                            GEN_INT (-4))));
7705               RTX_FRAME_RELATED_P (insn) = 1;
7706               dwarf2out_frame_debug (insn, true);
7707               end_sequence ();
7708             }
7709 #endif
7710         }
7711
7712 #if TARGET_MACHO
7713       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
7714          is what will be referenced by the Mach-O PIC subsystem.  */
7715       if (!label)
7716         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
7717 #endif
7718
7719       (*targetm.asm_out.internal_label) (asm_out_file, "L",
7720                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
7721
7722       if (flag_pic)
7723         {
7724           output_asm_insn ("pop%z0\t%0", xops);
7725 #ifdef DWARF2_UNWIND_INFO
7726           /* The pop is a pop and clobbers dest, but doesn't restore it
7727              for unwind info purposes.  */
7728           if (dwarf2out_do_frame ())
7729             {
7730               rtx insn;
7731               start_sequence ();
7732               insn = emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
7733               dwarf2out_frame_debug (insn, true);
7734               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7735                                              gen_rtx_PLUS (Pmode,
7736                                                            stack_pointer_rtx,
7737                                                            GEN_INT (4))));
7738               RTX_FRAME_RELATED_P (insn) = 1;
7739               dwarf2out_frame_debug (insn, true);
7740               end_sequence ();
7741             }
7742 #endif
7743         }
7744     }
7745   else
7746     {
7747       char name[32];
7748       get_pc_thunk_name (name, REGNO (dest));
7749       pic_labels_used |= 1 << REGNO (dest);
7750
7751 #ifdef DWARF2_UNWIND_INFO
7752       /* Ensure all queued register saves are flushed before the
7753          call.  */
7754       if (dwarf2out_do_frame ())
7755         {
7756           rtx insn;
7757           start_sequence ();
7758           insn = emit_barrier ();
7759           end_sequence ();
7760           dwarf2out_frame_debug (insn, false);
7761         }
7762 #endif
7763       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
7764       xops[2] = gen_rtx_MEM (QImode, xops[2]);
7765       output_asm_insn ("call\t%X2", xops);
7766       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
7767          is what will be referenced by the Mach-O PIC subsystem.  */
7768 #if TARGET_MACHO
7769       if (!label)
7770         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
7771       else
7772         targetm.asm_out.internal_label (asm_out_file, "L",
7773                                            CODE_LABEL_NUMBER (label));
7774 #endif
7775     }
7776
7777   if (TARGET_MACHO)
7778     return "";
7779
7780   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
7781     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
7782   else
7783     output_asm_insn ("add%z0\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
7784
7785   return "";
7786 }
7787
7788 /* Generate an "push" pattern for input ARG.  */
7789
7790 static rtx
7791 gen_push (rtx arg)
7792 {
7793   if (ix86_cfa_state->reg == stack_pointer_rtx)
7794     ix86_cfa_state->offset += UNITS_PER_WORD;
7795
7796   return gen_rtx_SET (VOIDmode,
7797                       gen_rtx_MEM (Pmode,
7798                                    gen_rtx_PRE_DEC (Pmode,
7799                                                     stack_pointer_rtx)),
7800                       arg);
7801 }
7802
7803 /* Return >= 0 if there is an unused call-clobbered register available
7804    for the entire function.  */
7805
7806 static unsigned int
7807 ix86_select_alt_pic_regnum (void)
7808 {
7809   if (current_function_is_leaf && !crtl->profile
7810       && !ix86_current_function_calls_tls_descriptor)
7811     {
7812       int i, drap;
7813       /* Can't use the same register for both PIC and DRAP.  */
7814       if (crtl->drap_reg)
7815         drap = REGNO (crtl->drap_reg);
7816       else
7817         drap = -1;
7818       for (i = 2; i >= 0; --i)
7819         if (i != drap && !df_regs_ever_live_p (i))
7820           return i;
7821     }
7822
7823   return INVALID_REGNUM;
7824 }
7825
7826 /* Return 1 if we need to save REGNO.  */
7827 static int
7828 ix86_save_reg (unsigned int regno, int maybe_eh_return)
7829 {
7830   if (pic_offset_table_rtx
7831       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
7832       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
7833           || crtl->profile
7834           || crtl->calls_eh_return
7835           || crtl->uses_const_pool))
7836     {
7837       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
7838         return 0;
7839       return 1;
7840     }
7841
7842   if (crtl->calls_eh_return && maybe_eh_return)
7843     {
7844       unsigned i;
7845       for (i = 0; ; i++)
7846         {
7847           unsigned test = EH_RETURN_DATA_REGNO (i);
7848           if (test == INVALID_REGNUM)
7849             break;
7850           if (test == regno)
7851             return 1;
7852         }
7853     }
7854
7855   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
7856     return 1;
7857
7858   return (df_regs_ever_live_p (regno)
7859           && !call_used_regs[regno]
7860           && !fixed_regs[regno]
7861           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
7862 }
7863
7864 /* Return number of saved general prupose registers.  */
7865
7866 static int
7867 ix86_nsaved_regs (void)
7868 {
7869   int nregs = 0;
7870   int regno;
7871
7872   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7873     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
7874       nregs ++;
7875   return nregs;
7876 }
7877
7878 /* Return number of saved SSE registrers.  */
7879
7880 static int
7881 ix86_nsaved_sseregs (void)
7882 {
7883   int nregs = 0;
7884   int regno;
7885
7886   if (ix86_cfun_abi () != MS_ABI)
7887     return 0;
7888   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7889     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
7890       nregs ++;
7891   return nregs;
7892 }
7893
7894 /* Given FROM and TO register numbers, say whether this elimination is
7895    allowed.  If stack alignment is needed, we can only replace argument
7896    pointer with hard frame pointer, or replace frame pointer with stack
7897    pointer.  Otherwise, frame pointer elimination is automatically
7898    handled and all other eliminations are valid.  */
7899
7900 static bool
7901 ix86_can_eliminate (const int from, const int to)
7902 {
7903   if (stack_realign_fp)
7904     return ((from == ARG_POINTER_REGNUM
7905              && to == HARD_FRAME_POINTER_REGNUM)
7906             || (from == FRAME_POINTER_REGNUM
7907                 && to == STACK_POINTER_REGNUM));
7908   else
7909     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
7910 }
7911
7912 /* Return the offset between two registers, one to be eliminated, and the other
7913    its replacement, at the start of a routine.  */
7914
7915 HOST_WIDE_INT
7916 ix86_initial_elimination_offset (int from, int to)
7917 {
7918   struct ix86_frame frame;
7919   ix86_compute_frame_layout (&frame);
7920
7921   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
7922     return frame.hard_frame_pointer_offset;
7923   else if (from == FRAME_POINTER_REGNUM
7924            && to == HARD_FRAME_POINTER_REGNUM)
7925     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
7926   else
7927     {
7928       gcc_assert (to == STACK_POINTER_REGNUM);
7929
7930       if (from == ARG_POINTER_REGNUM)
7931         return frame.stack_pointer_offset;
7932
7933       gcc_assert (from == FRAME_POINTER_REGNUM);
7934       return frame.stack_pointer_offset - frame.frame_pointer_offset;
7935     }
7936 }
7937
7938 /* In a dynamically-aligned function, we can't know the offset from
7939    stack pointer to frame pointer, so we must ensure that setjmp
7940    eliminates fp against the hard fp (%ebp) rather than trying to
7941    index from %esp up to the top of the frame across a gap that is
7942    of unknown (at compile-time) size.  */
7943 static rtx
7944 ix86_builtin_setjmp_frame_value (void)
7945 {
7946   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
7947 }
7948
7949 /* Fill structure ix86_frame about frame of currently computed function.  */
7950
7951 static void
7952 ix86_compute_frame_layout (struct ix86_frame *frame)
7953 {
7954   unsigned int stack_alignment_needed;
7955   HOST_WIDE_INT offset;
7956   unsigned int preferred_alignment;
7957   HOST_WIDE_INT size = get_frame_size ();
7958
7959   frame->nregs = ix86_nsaved_regs ();
7960   frame->nsseregs = ix86_nsaved_sseregs ();
7961
7962   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
7963   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
7964
7965   /* MS ABI seem to require stack alignment to be always 16 except for function
7966      prologues.  */
7967   if (ix86_cfun_abi () == MS_ABI && preferred_alignment < 16)
7968     {
7969       preferred_alignment = 16;
7970       stack_alignment_needed = 16;
7971       crtl->preferred_stack_boundary = 128;
7972       crtl->stack_alignment_needed = 128;
7973     }
7974
7975   gcc_assert (!size || stack_alignment_needed);
7976   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
7977   gcc_assert (preferred_alignment <= stack_alignment_needed);
7978
7979   /* During reload iteration the amount of registers saved can change.
7980      Recompute the value as needed.  Do not recompute when amount of registers
7981      didn't change as reload does multiple calls to the function and does not
7982      expect the decision to change within single iteration.  */
7983   if (!optimize_function_for_size_p (cfun)
7984       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
7985     {
7986       int count = frame->nregs;
7987
7988       cfun->machine->use_fast_prologue_epilogue_nregs = count;
7989       /* The fast prologue uses move instead of push to save registers.  This
7990          is significantly longer, but also executes faster as modern hardware
7991          can execute the moves in parallel, but can't do that for push/pop.
7992
7993          Be careful about choosing what prologue to emit:  When function takes
7994          many instructions to execute we may use slow version as well as in
7995          case function is known to be outside hot spot (this is known with
7996          feedback only).  Weight the size of function by number of registers
7997          to save as it is cheap to use one or two push instructions but very
7998          slow to use many of them.  */
7999       if (count)
8000         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
8001       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
8002           || (flag_branch_probabilities
8003               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
8004         cfun->machine->use_fast_prologue_epilogue = false;
8005       else
8006         cfun->machine->use_fast_prologue_epilogue
8007            = !expensive_function_p (count);
8008     }
8009   if (TARGET_PROLOGUE_USING_MOVE
8010       && cfun->machine->use_fast_prologue_epilogue)
8011     frame->save_regs_using_mov = true;
8012   else
8013     frame->save_regs_using_mov = false;
8014
8015   /* Skip return address.  */
8016   offset = UNITS_PER_WORD;
8017
8018   /* Skip pushed static chain.  */
8019   if (ix86_static_chain_on_stack)
8020     offset += UNITS_PER_WORD;
8021
8022   /* Skip saved base pointer.  */
8023   if (frame_pointer_needed)
8024     offset += UNITS_PER_WORD;
8025
8026   frame->hard_frame_pointer_offset = offset;
8027
8028   /* Set offset to aligned because the realigned frame starts from
8029      here.  */
8030   if (stack_realign_fp)
8031     offset = (offset + stack_alignment_needed -1) & -stack_alignment_needed;
8032
8033   /* Register save area */
8034   offset += frame->nregs * UNITS_PER_WORD;
8035
8036   /* Align SSE reg save area.  */
8037   if (frame->nsseregs)
8038     frame->padding0 = ((offset + 16 - 1) & -16) - offset;
8039   else
8040     frame->padding0 = 0;
8041   
8042   /* SSE register save area.  */
8043   offset += frame->padding0 + frame->nsseregs * 16;
8044
8045   /* Va-arg area */
8046   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
8047   offset += frame->va_arg_size;
8048
8049   /* Align start of frame for local function.  */
8050   frame->padding1 = ((offset + stack_alignment_needed - 1)
8051                      & -stack_alignment_needed) - offset;
8052
8053   offset += frame->padding1;
8054
8055   /* Frame pointer points here.  */
8056   frame->frame_pointer_offset = offset;
8057
8058   offset += size;
8059
8060   /* Add outgoing arguments area.  Can be skipped if we eliminated
8061      all the function calls as dead code.
8062      Skipping is however impossible when function calls alloca.  Alloca
8063      expander assumes that last crtl->outgoing_args_size
8064      of stack frame are unused.  */
8065   if (ACCUMULATE_OUTGOING_ARGS
8066       && (!current_function_is_leaf || cfun->calls_alloca
8067           || ix86_current_function_calls_tls_descriptor))
8068     {
8069       offset += crtl->outgoing_args_size;
8070       frame->outgoing_arguments_size = crtl->outgoing_args_size;
8071     }
8072   else
8073     frame->outgoing_arguments_size = 0;
8074
8075   /* Align stack boundary.  Only needed if we're calling another function
8076      or using alloca.  */
8077   if (!current_function_is_leaf || cfun->calls_alloca
8078       || ix86_current_function_calls_tls_descriptor)
8079     frame->padding2 = ((offset + preferred_alignment - 1)
8080                        & -preferred_alignment) - offset;
8081   else
8082     frame->padding2 = 0;
8083
8084   offset += frame->padding2;
8085
8086   /* We've reached end of stack frame.  */
8087   frame->stack_pointer_offset = offset;
8088
8089   /* Size prologue needs to allocate.  */
8090   frame->to_allocate =
8091     (size + frame->padding1 + frame->padding2
8092      + frame->outgoing_arguments_size + frame->va_arg_size);
8093
8094   if ((!frame->to_allocate && frame->nregs <= 1)
8095       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
8096     frame->save_regs_using_mov = false;
8097
8098   if (!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE
8099       && current_function_sp_is_unchanging
8100       && current_function_is_leaf
8101       && !ix86_current_function_calls_tls_descriptor)
8102     {
8103       frame->red_zone_size = frame->to_allocate;
8104       if (frame->save_regs_using_mov)
8105         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
8106       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
8107         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
8108     }
8109   else
8110     frame->red_zone_size = 0;
8111   frame->to_allocate -= frame->red_zone_size;
8112   frame->stack_pointer_offset -= frame->red_zone_size;
8113 }
8114
8115 /* Emit code to save registers in the prologue.  */
8116
8117 static void
8118 ix86_emit_save_regs (void)
8119 {
8120   unsigned int regno;
8121   rtx insn;
8122
8123   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
8124     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8125       {
8126         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
8127         RTX_FRAME_RELATED_P (insn) = 1;
8128       }
8129 }
8130
8131 /* Emit code to save registers using MOV insns.  First register
8132    is restored from POINTER + OFFSET.  */
8133 static void
8134 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
8135 {
8136   unsigned int regno;
8137   rtx insn;
8138
8139   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8140     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8141       {
8142         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
8143                                                Pmode, offset),
8144                                gen_rtx_REG (Pmode, regno));
8145         RTX_FRAME_RELATED_P (insn) = 1;
8146         offset += UNITS_PER_WORD;
8147       }
8148 }
8149
8150 /* Emit code to save registers using MOV insns.  First register
8151    is restored from POINTER + OFFSET.  */
8152 static void
8153 ix86_emit_save_sse_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
8154 {
8155   unsigned int regno;
8156   rtx insn;
8157   rtx mem;
8158
8159   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8160     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8161       {
8162         mem = adjust_address (gen_rtx_MEM (TImode, pointer), TImode, offset);
8163         set_mem_align (mem, 128);
8164         insn = emit_move_insn (mem, gen_rtx_REG (TImode, regno));
8165         RTX_FRAME_RELATED_P (insn) = 1;
8166         offset += 16;
8167       }
8168 }
8169
8170 static GTY(()) rtx queued_cfa_restores;
8171
8172 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
8173    manipulation insn.  Don't add it if the previously
8174    saved value will be left untouched within stack red-zone till return,
8175    as unwinders can find the same value in the register and
8176    on the stack.  */
8177
8178 static void
8179 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT red_offset)
8180 {
8181   if (TARGET_RED_ZONE
8182       && !TARGET_64BIT_MS_ABI
8183       && red_offset + RED_ZONE_SIZE >= 0
8184       && crtl->args.pops_args < 65536)
8185     return;
8186
8187   if (insn)
8188     {
8189       add_reg_note (insn, REG_CFA_RESTORE, reg);
8190       RTX_FRAME_RELATED_P (insn) = 1;
8191     }
8192   else
8193     queued_cfa_restores
8194       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
8195 }
8196
8197 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
8198
8199 static void
8200 ix86_add_queued_cfa_restore_notes (rtx insn)
8201 {
8202   rtx last;
8203   if (!queued_cfa_restores)
8204     return;
8205   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
8206     ;
8207   XEXP (last, 1) = REG_NOTES (insn);
8208   REG_NOTES (insn) = queued_cfa_restores;
8209   queued_cfa_restores = NULL_RTX;
8210   RTX_FRAME_RELATED_P (insn) = 1;
8211 }
8212
8213 /* Expand prologue or epilogue stack adjustment.
8214    The pattern exist to put a dependency on all ebp-based memory accesses.
8215    STYLE should be negative if instructions should be marked as frame related,
8216    zero if %r11 register is live and cannot be freely used and positive
8217    otherwise.  */
8218
8219 static void
8220 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
8221                            int style, bool set_cfa)
8222 {
8223   rtx insn;
8224
8225   if (! TARGET_64BIT)
8226     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
8227   else if (x86_64_immediate_operand (offset, DImode))
8228     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
8229   else
8230     {
8231       rtx r11;
8232       /* r11 is used by indirect sibcall return as well, set before the
8233          epilogue and used after the epilogue.  ATM indirect sibcall
8234          shouldn't be used together with huge frame sizes in one
8235          function because of the frame_size check in sibcall.c.  */
8236       gcc_assert (style);
8237       r11 = gen_rtx_REG (DImode, R11_REG);
8238       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
8239       if (style < 0)
8240         RTX_FRAME_RELATED_P (insn) = 1;
8241       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
8242                                                                offset));
8243     }
8244
8245   if (style >= 0)
8246     ix86_add_queued_cfa_restore_notes (insn);
8247
8248   if (set_cfa)
8249     {
8250       rtx r;
8251
8252       gcc_assert (ix86_cfa_state->reg == src);
8253       ix86_cfa_state->offset += INTVAL (offset);
8254       ix86_cfa_state->reg = dest;
8255     
8256       r = gen_rtx_PLUS (Pmode, src, offset);
8257       r = gen_rtx_SET (VOIDmode, dest, r);
8258       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
8259       RTX_FRAME_RELATED_P (insn) = 1;
8260     }
8261   else if (style < 0)
8262     RTX_FRAME_RELATED_P (insn) = 1;
8263 }
8264
8265 /* Find an available register to be used as dynamic realign argument
8266    pointer regsiter.  Such a register will be written in prologue and
8267    used in begin of body, so it must not be
8268         1. parameter passing register.
8269         2. GOT pointer.
8270    We reuse static-chain register if it is available.  Otherwise, we
8271    use DI for i386 and R13 for x86-64.  We chose R13 since it has
8272    shorter encoding.
8273
8274    Return: the regno of chosen register.  */
8275
8276 static unsigned int 
8277 find_drap_reg (void)
8278 {
8279   tree decl = cfun->decl;
8280
8281   if (TARGET_64BIT)
8282     {
8283       /* Use R13 for nested function or function need static chain.
8284          Since function with tail call may use any caller-saved
8285          registers in epilogue, DRAP must not use caller-saved
8286          register in such case.  */
8287       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
8288         return R13_REG;
8289
8290       return R10_REG;
8291     }
8292   else
8293     {
8294       /* Use DI for nested function or function need static chain.
8295          Since function with tail call may use any caller-saved
8296          registers in epilogue, DRAP must not use caller-saved
8297          register in such case.  */
8298       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
8299         return DI_REG;
8300     
8301       /* Reuse static chain register if it isn't used for parameter
8302          passing.  */
8303       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2
8304           && !lookup_attribute ("fastcall",
8305                                 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
8306         return CX_REG;
8307       else
8308         return DI_REG;
8309     }
8310 }
8311
8312 /* Return minimum incoming stack alignment.  */
8313
8314 static unsigned int
8315 ix86_minimum_incoming_stack_boundary (bool sibcall)
8316 {
8317   unsigned int incoming_stack_boundary;
8318
8319   /* Prefer the one specified at command line. */
8320   if (ix86_user_incoming_stack_boundary)
8321     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
8322   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
8323      if -mstackrealign is used, it isn't used for sibcall check and 
8324      estimated stack alignment is 128bit.  */
8325   else if (!sibcall
8326            && !TARGET_64BIT
8327            && ix86_force_align_arg_pointer
8328            && crtl->stack_alignment_estimated == 128)
8329     incoming_stack_boundary = MIN_STACK_BOUNDARY;
8330   else
8331     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
8332
8333   /* Incoming stack alignment can be changed on individual functions
8334      via force_align_arg_pointer attribute.  We use the smallest
8335      incoming stack boundary.  */
8336   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
8337       && lookup_attribute (ix86_force_align_arg_pointer_string,
8338                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
8339     incoming_stack_boundary = MIN_STACK_BOUNDARY;
8340
8341   /* The incoming stack frame has to be aligned at least at
8342      parm_stack_boundary.  */
8343   if (incoming_stack_boundary < crtl->parm_stack_boundary)
8344     incoming_stack_boundary = crtl->parm_stack_boundary;
8345
8346   /* Stack at entrance of main is aligned by runtime.  We use the
8347      smallest incoming stack boundary. */
8348   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
8349       && DECL_NAME (current_function_decl)
8350       && MAIN_NAME_P (DECL_NAME (current_function_decl))
8351       && DECL_FILE_SCOPE_P (current_function_decl))
8352     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
8353
8354   return incoming_stack_boundary;
8355 }
8356
8357 /* Update incoming stack boundary and estimated stack alignment.  */
8358
8359 static void
8360 ix86_update_stack_boundary (void)
8361 {
8362   ix86_incoming_stack_boundary
8363     = ix86_minimum_incoming_stack_boundary (false);
8364
8365   /* x86_64 vararg needs 16byte stack alignment for register save
8366      area.  */
8367   if (TARGET_64BIT
8368       && cfun->stdarg
8369       && crtl->stack_alignment_estimated < 128)
8370     crtl->stack_alignment_estimated = 128;
8371 }
8372
8373 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
8374    needed or an rtx for DRAP otherwise.  */
8375
8376 static rtx
8377 ix86_get_drap_rtx (void)
8378 {
8379   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
8380     crtl->need_drap = true;
8381
8382   if (stack_realign_drap)
8383     {
8384       /* Assign DRAP to vDRAP and returns vDRAP */
8385       unsigned int regno = find_drap_reg ();
8386       rtx drap_vreg;
8387       rtx arg_ptr;
8388       rtx seq, insn;
8389
8390       arg_ptr = gen_rtx_REG (Pmode, regno);
8391       crtl->drap_reg = arg_ptr;
8392
8393       start_sequence ();
8394       drap_vreg = copy_to_reg (arg_ptr);
8395       seq = get_insns ();
8396       end_sequence ();
8397       
8398       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
8399       if (!optimize)
8400         {
8401           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
8402           RTX_FRAME_RELATED_P (insn) = 1;
8403         }
8404       return drap_vreg;
8405     }
8406   else
8407     return NULL;
8408 }
8409
8410 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
8411
8412 static rtx
8413 ix86_internal_arg_pointer (void)
8414 {
8415   return virtual_incoming_args_rtx;
8416 }
8417
8418 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
8419    to be generated in correct form.  */
8420 static void 
8421 ix86_finalize_stack_realign_flags (void)
8422 {
8423   /* Check if stack realign is really needed after reload, and 
8424      stores result in cfun */
8425   unsigned int incoming_stack_boundary
8426     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
8427        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
8428   unsigned int stack_realign = (incoming_stack_boundary
8429                                 < (current_function_is_leaf
8430                                    ? crtl->max_used_stack_slot_alignment
8431                                    : crtl->stack_alignment_needed));
8432
8433   if (crtl->stack_realign_finalized)
8434     {
8435       /* After stack_realign_needed is finalized, we can't no longer
8436          change it.  */
8437       gcc_assert (crtl->stack_realign_needed == stack_realign);
8438     }
8439   else
8440     {
8441       crtl->stack_realign_needed = stack_realign;
8442       crtl->stack_realign_finalized = true;
8443     }
8444 }
8445
8446 /* Expand the prologue into a bunch of separate insns.  */
8447
8448 void
8449 ix86_expand_prologue (void)
8450 {
8451   rtx insn;
8452   bool pic_reg_used;
8453   struct ix86_frame frame;
8454   HOST_WIDE_INT allocate;
8455   int gen_frame_pointer = frame_pointer_needed;
8456
8457   ix86_finalize_stack_realign_flags ();
8458
8459   /* DRAP should not coexist with stack_realign_fp */
8460   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
8461
8462   /* Initialize CFA state for before the prologue.  */
8463   ix86_cfa_state->reg = stack_pointer_rtx;
8464   ix86_cfa_state->offset = INCOMING_FRAME_SP_OFFSET;
8465
8466   ix86_compute_frame_layout (&frame);
8467
8468   if (ix86_function_ms_hook_prologue (current_function_decl))
8469     {
8470       rtx push, mov;
8471
8472       /* Make sure the function starts with
8473          8b ff     movl.s %edi,%edi
8474          55        push   %ebp
8475          8b ec     movl.s %esp,%ebp
8476
8477          This matches the hookable function prologue in Win32 API
8478          functions in Microsoft Windows XP Service Pack 2 and newer.
8479          Wine uses this to enable Windows apps to hook the Win32 API
8480          functions provided by Wine.  */
8481       insn = emit_insn (gen_vswapmov (gen_rtx_REG (SImode, DI_REG),
8482                                       gen_rtx_REG (SImode, DI_REG)));
8483       push = emit_insn (gen_push (hard_frame_pointer_rtx));
8484       mov = emit_insn (gen_vswapmov (hard_frame_pointer_rtx,
8485                                      stack_pointer_rtx));
8486
8487       if (frame_pointer_needed && !(crtl->drap_reg
8488                                     && crtl->stack_realign_needed))
8489         {
8490           /* The push %ebp and movl.s %esp, %ebp already set up
8491              the frame pointer.  No need to do this again. */
8492           gen_frame_pointer = 0;
8493           RTX_FRAME_RELATED_P (push) = 1;
8494           RTX_FRAME_RELATED_P (mov) = 1;
8495           if (ix86_cfa_state->reg == stack_pointer_rtx)
8496             ix86_cfa_state->reg = hard_frame_pointer_rtx;
8497         }
8498       else
8499         /* If the frame pointer is not needed, pop %ebp again. This
8500            could be optimized for cases where ebp needs to be backed up
8501            for some other reason.  If stack realignment is needed, pop
8502            the base pointer again, align the stack, and later regenerate
8503            the frame pointer setup.  The frame pointer generated by the
8504            hook prologue is not aligned, so it can't be used.  */
8505         insn = emit_insn ((*ix86_gen_pop1) (hard_frame_pointer_rtx));
8506     }
8507
8508   /* The first insn of a function that accepts its static chain on the
8509      stack is to push the register that would be filled in by a direct
8510      call.  This insn will be skipped by the trampoline.  */
8511   if (ix86_static_chain_on_stack)
8512     {
8513       rtx t;
8514
8515       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
8516       emit_insn (gen_blockage ());
8517
8518       /* We don't want to interpret this push insn as a register save,
8519          only as a stack adjustment.  The real copy of the register as
8520          a save will be done later, if needed.  */
8521       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
8522       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
8523       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
8524       RTX_FRAME_RELATED_P (insn) = 1;
8525     }
8526
8527   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
8528      of DRAP is needed and stack realignment is really needed after reload */
8529   if (crtl->drap_reg && crtl->stack_realign_needed)
8530     {
8531       rtx x, y;
8532       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
8533       int param_ptr_offset = UNITS_PER_WORD;
8534
8535       if (ix86_static_chain_on_stack)
8536         param_ptr_offset += UNITS_PER_WORD;
8537       if (!call_used_regs[REGNO (crtl->drap_reg)])
8538         param_ptr_offset += UNITS_PER_WORD;
8539
8540       gcc_assert (stack_realign_drap);
8541
8542       /* Grab the argument pointer.  */
8543       x = plus_constant (stack_pointer_rtx, param_ptr_offset);
8544       y = crtl->drap_reg;
8545
8546       /* Only need to push parameter pointer reg if it is caller
8547          saved reg */
8548       if (!call_used_regs[REGNO (crtl->drap_reg)])
8549         {
8550           /* Push arg pointer reg */
8551           insn = emit_insn (gen_push (y));
8552           RTX_FRAME_RELATED_P (insn) = 1;
8553         }
8554
8555       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
8556       RTX_FRAME_RELATED_P (insn) = 1; 
8557       ix86_cfa_state->reg = crtl->drap_reg;
8558
8559       /* Align the stack.  */
8560       insn = emit_insn ((*ix86_gen_andsp) (stack_pointer_rtx,
8561                                            stack_pointer_rtx,
8562                                            GEN_INT (-align_bytes)));
8563       RTX_FRAME_RELATED_P (insn) = 1;
8564
8565       /* Replicate the return address on the stack so that return
8566          address can be reached via (argp - 1) slot.  This is needed
8567          to implement macro RETURN_ADDR_RTX and intrinsic function
8568          expand_builtin_return_addr etc.  */
8569       x = crtl->drap_reg;
8570       x = gen_frame_mem (Pmode,
8571                          plus_constant (x, -UNITS_PER_WORD));
8572       insn = emit_insn (gen_push (x));
8573       RTX_FRAME_RELATED_P (insn) = 1;
8574     }
8575
8576   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
8577      slower on all targets.  Also sdb doesn't like it.  */
8578
8579   if (gen_frame_pointer)
8580     {
8581       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
8582       RTX_FRAME_RELATED_P (insn) = 1;
8583
8584       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
8585       RTX_FRAME_RELATED_P (insn) = 1;
8586
8587       if (ix86_cfa_state->reg == stack_pointer_rtx)
8588         ix86_cfa_state->reg = hard_frame_pointer_rtx;
8589     }
8590
8591   if (stack_realign_fp)
8592     {
8593       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
8594       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
8595
8596       /* Align the stack.  */
8597       insn = emit_insn ((*ix86_gen_andsp) (stack_pointer_rtx,
8598                                            stack_pointer_rtx,
8599                                            GEN_INT (-align_bytes)));
8600       RTX_FRAME_RELATED_P (insn) = 1;
8601     }
8602
8603   allocate = frame.to_allocate + frame.nsseregs * 16 + frame.padding0;
8604
8605   if (!frame.save_regs_using_mov)
8606     ix86_emit_save_regs ();
8607   else
8608     allocate += frame.nregs * UNITS_PER_WORD;
8609
8610   /* When using red zone we may start register saving before allocating
8611      the stack frame saving one cycle of the prologue. However I will
8612      avoid doing this if I am going to have to probe the stack since
8613      at least on x86_64 the stack probe can turn into a call that clobbers
8614      a red zone location */
8615   if (!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE && frame.save_regs_using_mov
8616       && (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT))
8617     ix86_emit_save_regs_using_mov ((frame_pointer_needed
8618                                      && !crtl->stack_realign_needed) 
8619                                    ? hard_frame_pointer_rtx
8620                                    : stack_pointer_rtx,
8621                                    -frame.nregs * UNITS_PER_WORD);
8622
8623   if (allocate == 0)
8624     ;
8625   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
8626     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
8627                                GEN_INT (-allocate), -1,
8628                                ix86_cfa_state->reg == stack_pointer_rtx);
8629   else
8630     {
8631       rtx eax = gen_rtx_REG (Pmode, AX_REG);
8632       bool eax_live;
8633       rtx t;
8634
8635       if (cfun->machine->call_abi == MS_ABI)
8636         eax_live = false;
8637       else
8638         eax_live = ix86_eax_live_at_start_p ();
8639
8640       if (eax_live)
8641         {
8642           emit_insn (gen_push (eax));
8643           allocate -= UNITS_PER_WORD;
8644         }
8645
8646       emit_move_insn (eax, GEN_INT (allocate));
8647
8648       if (TARGET_64BIT)
8649         insn = gen_allocate_stack_worker_64 (eax, eax);
8650       else
8651         insn = gen_allocate_stack_worker_32 (eax, eax);
8652       insn = emit_insn (insn);
8653
8654       if (ix86_cfa_state->reg == stack_pointer_rtx)
8655         {
8656           ix86_cfa_state->offset += allocate;
8657           t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
8658           t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
8659           add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
8660           RTX_FRAME_RELATED_P (insn) = 1;
8661         }
8662
8663       if (eax_live)
8664         {
8665           if (frame_pointer_needed)
8666             t = plus_constant (hard_frame_pointer_rtx,
8667                                allocate
8668                                - frame.to_allocate
8669                                - frame.nregs * UNITS_PER_WORD);
8670           else
8671             t = plus_constant (stack_pointer_rtx, allocate);
8672           emit_move_insn (eax, gen_rtx_MEM (Pmode, t));
8673         }
8674     }
8675
8676   if (frame.save_regs_using_mov
8677       && !(!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE
8678          && (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)))
8679     {
8680       if (!frame_pointer_needed
8681           || !(frame.to_allocate + frame.padding0)
8682           || crtl->stack_realign_needed)
8683         ix86_emit_save_regs_using_mov (stack_pointer_rtx,
8684                                        frame.to_allocate
8685                                        + frame.nsseregs * 16 + frame.padding0);
8686       else
8687         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
8688                                        -frame.nregs * UNITS_PER_WORD);
8689     }
8690   if (!frame_pointer_needed
8691       || !(frame.to_allocate + frame.padding0)
8692       || crtl->stack_realign_needed)
8693     ix86_emit_save_sse_regs_using_mov (stack_pointer_rtx,
8694                                        frame.to_allocate);
8695   else
8696     ix86_emit_save_sse_regs_using_mov (hard_frame_pointer_rtx,
8697                                        - frame.nregs * UNITS_PER_WORD
8698                                        - frame.nsseregs * 16
8699                                        - frame.padding0);
8700
8701   pic_reg_used = false;
8702   if (pic_offset_table_rtx
8703       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8704           || crtl->profile))
8705     {
8706       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
8707
8708       if (alt_pic_reg_used != INVALID_REGNUM)
8709         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
8710
8711       pic_reg_used = true;
8712     }
8713
8714   if (pic_reg_used)
8715     {
8716       if (TARGET_64BIT)
8717         {
8718           if (ix86_cmodel == CM_LARGE_PIC)
8719             {
8720               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
8721               rtx label = gen_label_rtx ();
8722               emit_label (label);
8723               LABEL_PRESERVE_P (label) = 1;
8724               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
8725               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
8726               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
8727               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
8728                                             pic_offset_table_rtx, tmp_reg));
8729             }
8730           else
8731             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
8732         }
8733       else
8734         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
8735     }
8736
8737   /* In the pic_reg_used case, make sure that the got load isn't deleted
8738      when mcount needs it.  Blockage to avoid call movement across mcount
8739      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
8740      note.  */
8741   if (crtl->profile && pic_reg_used)
8742     emit_insn (gen_prologue_use (pic_offset_table_rtx));
8743
8744   if (crtl->drap_reg && !crtl->stack_realign_needed)
8745     {
8746       /* vDRAP is setup but after reload it turns out stack realign
8747          isn't necessary, here we will emit prologue to setup DRAP
8748          without stack realign adjustment */
8749       rtx x;
8750       int drap_bp_offset = UNITS_PER_WORD * 2;
8751
8752       if (ix86_static_chain_on_stack)
8753         drap_bp_offset += UNITS_PER_WORD;
8754       x = plus_constant (hard_frame_pointer_rtx, drap_bp_offset);
8755       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, x));
8756     }
8757
8758   /* Prevent instructions from being scheduled into register save push
8759      sequence when access to the redzone area is done through frame pointer.
8760      The offset between the frame pointer and the stack pointer is calculated
8761      relative to the value of the stack pointer at the end of the function
8762      prologue, and moving instructions that access redzone area via frame
8763      pointer inside push sequence violates this assumption.  */
8764   if (frame_pointer_needed && frame.red_zone_size)
8765     emit_insn (gen_memory_blockage ());
8766
8767   /* Emit cld instruction if stringops are used in the function.  */
8768   if (TARGET_CLD && ix86_current_function_needs_cld)
8769     emit_insn (gen_cld ());
8770 }
8771
8772 /* Emit code to restore REG using a POP insn.  */
8773
8774 static void
8775 ix86_emit_restore_reg_using_pop (rtx reg, HOST_WIDE_INT red_offset)
8776 {
8777   rtx insn = emit_insn (ix86_gen_pop1 (reg));
8778
8779   if (ix86_cfa_state->reg == crtl->drap_reg
8780       && REGNO (reg) == REGNO (crtl->drap_reg))
8781     {
8782       /* Previously we'd represented the CFA as an expression
8783          like *(%ebp - 8).  We've just popped that value from
8784          the stack, which means we need to reset the CFA to
8785          the drap register.  This will remain until we restore
8786          the stack pointer.  */
8787       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
8788       RTX_FRAME_RELATED_P (insn) = 1;
8789       return;
8790     }
8791
8792   if (ix86_cfa_state->reg == stack_pointer_rtx)
8793     {
8794       ix86_cfa_state->offset -= UNITS_PER_WORD;
8795       add_reg_note (insn, REG_CFA_ADJUST_CFA,
8796                     copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
8797       RTX_FRAME_RELATED_P (insn) = 1;
8798     }
8799
8800   /* When the frame pointer is the CFA, and we pop it, we are
8801      swapping back to the stack pointer as the CFA.  This happens
8802      for stack frames that don't allocate other data, so we assume
8803      the stack pointer is now pointing at the return address, i.e.
8804      the function entry state, which makes the offset be 1 word.  */
8805   else if (ix86_cfa_state->reg == hard_frame_pointer_rtx
8806            && reg == hard_frame_pointer_rtx)
8807     {
8808       ix86_cfa_state->reg = stack_pointer_rtx;
8809       ix86_cfa_state->offset -= UNITS_PER_WORD;
8810
8811       add_reg_note (insn, REG_CFA_DEF_CFA,
8812                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
8813                                   GEN_INT (ix86_cfa_state->offset)));
8814       RTX_FRAME_RELATED_P (insn) = 1;
8815     }
8816
8817   ix86_add_cfa_restore_note (insn, reg, red_offset);
8818 }
8819
8820 /* Emit code to restore saved registers using POP insns.  */
8821
8822 static void
8823 ix86_emit_restore_regs_using_pop (HOST_WIDE_INT red_offset)
8824 {
8825   int regno;
8826
8827   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8828     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
8829       {
8830         ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno),
8831                                          red_offset);
8832         red_offset += UNITS_PER_WORD;
8833       }
8834 }
8835
8836 /* Emit code and notes for the LEAVE instruction.  */
8837
8838 static void
8839 ix86_emit_leave (HOST_WIDE_INT red_offset)
8840 {
8841   rtx insn = emit_insn (ix86_gen_leave ());
8842
8843   ix86_add_queued_cfa_restore_notes (insn);
8844
8845   if (ix86_cfa_state->reg == hard_frame_pointer_rtx)
8846     {
8847       ix86_cfa_state->reg = stack_pointer_rtx;
8848       ix86_cfa_state->offset -= UNITS_PER_WORD;
8849
8850       add_reg_note (insn, REG_CFA_ADJUST_CFA, 
8851                     copy_rtx (XVECEXP (PATTERN (insn), 0, 0)));
8852       RTX_FRAME_RELATED_P (insn) = 1;
8853       ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx, red_offset);
8854     }
8855 }
8856
8857 /* Emit code to restore saved registers using MOV insns.  First register
8858    is restored from POINTER + OFFSET.  */
8859 static void
8860 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
8861                                   HOST_WIDE_INT red_offset,
8862                                   int maybe_eh_return)
8863 {
8864   unsigned int regno;
8865   rtx base_address = gen_rtx_MEM (Pmode, pointer);
8866   rtx insn;
8867
8868   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8869     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
8870       {
8871         rtx reg = gen_rtx_REG (Pmode, regno);
8872
8873         /* Ensure that adjust_address won't be forced to produce pointer
8874            out of range allowed by x86-64 instruction set.  */
8875         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
8876           {
8877             rtx r11;
8878
8879             r11 = gen_rtx_REG (DImode, R11_REG);
8880             emit_move_insn (r11, GEN_INT (offset));
8881             emit_insn (gen_adddi3 (r11, r11, pointer));
8882             base_address = gen_rtx_MEM (Pmode, r11);
8883             offset = 0;
8884           }
8885         insn = emit_move_insn (reg,
8886                                adjust_address (base_address, Pmode, offset));
8887         offset += UNITS_PER_WORD;
8888
8889         if (ix86_cfa_state->reg == crtl->drap_reg
8890             && regno == REGNO (crtl->drap_reg))
8891           {
8892             /* Previously we'd represented the CFA as an expression
8893                like *(%ebp - 8).  We've just popped that value from
8894                the stack, which means we need to reset the CFA to
8895                the drap register.  This will remain until we restore
8896                the stack pointer.  */
8897             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
8898             RTX_FRAME_RELATED_P (insn) = 1;
8899           }
8900         else
8901           ix86_add_cfa_restore_note (NULL_RTX, reg, red_offset);
8902
8903         red_offset += UNITS_PER_WORD;
8904       }
8905 }
8906
8907 /* Emit code to restore saved registers using MOV insns.  First register
8908    is restored from POINTER + OFFSET.  */
8909 static void
8910 ix86_emit_restore_sse_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
8911                                       HOST_WIDE_INT red_offset,
8912                                       int maybe_eh_return)
8913 {
8914   int regno;
8915   rtx base_address = gen_rtx_MEM (TImode, pointer);
8916   rtx mem;
8917
8918   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8919     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
8920       {
8921         rtx reg = gen_rtx_REG (TImode, regno);
8922
8923         /* Ensure that adjust_address won't be forced to produce pointer
8924            out of range allowed by x86-64 instruction set.  */
8925         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
8926           {
8927             rtx r11;
8928
8929             r11 = gen_rtx_REG (DImode, R11_REG);
8930             emit_move_insn (r11, GEN_INT (offset));
8931             emit_insn (gen_adddi3 (r11, r11, pointer));
8932             base_address = gen_rtx_MEM (TImode, r11);
8933             offset = 0;
8934           }
8935         mem = adjust_address (base_address, TImode, offset);
8936         set_mem_align (mem, 128);
8937         emit_move_insn (reg, mem);
8938         offset += 16;
8939
8940         ix86_add_cfa_restore_note (NULL_RTX, reg, red_offset);
8941
8942         red_offset += 16;
8943       }
8944 }
8945
8946 /* Restore function stack, frame, and registers.  */
8947
8948 void
8949 ix86_expand_epilogue (int style)
8950 {
8951   int sp_valid;
8952   struct ix86_frame frame;
8953   HOST_WIDE_INT offset, red_offset;
8954   struct machine_cfa_state cfa_state_save = *ix86_cfa_state;
8955   bool using_drap;
8956
8957   ix86_finalize_stack_realign_flags ();
8958
8959  /* When stack is realigned, SP must be valid.  */
8960   sp_valid = (!frame_pointer_needed
8961               || current_function_sp_is_unchanging
8962               || stack_realign_fp);
8963
8964   ix86_compute_frame_layout (&frame);
8965
8966   /* See the comment about red zone and frame
8967      pointer usage in ix86_expand_prologue.  */
8968   if (frame_pointer_needed && frame.red_zone_size)
8969     emit_insn (gen_memory_blockage ()); 
8970
8971   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
8972   gcc_assert (!using_drap || ix86_cfa_state->reg == crtl->drap_reg);
8973
8974   /* Calculate start of saved registers relative to ebp.  Special care
8975      must be taken for the normal return case of a function using
8976      eh_return: the eax and edx registers are marked as saved, but not
8977      restored along this path.  */
8978   offset = frame.nregs;
8979   if (crtl->calls_eh_return && style != 2)
8980     offset -= 2;
8981   offset *= -UNITS_PER_WORD;
8982   offset -= frame.nsseregs * 16 + frame.padding0;
8983
8984   /* Calculate start of saved registers relative to esp on entry of the
8985      function.  When realigning stack, this needs to be the most negative
8986      value possible at runtime.  */
8987   red_offset = offset;
8988   if (using_drap)
8989     red_offset -= crtl->stack_alignment_needed / BITS_PER_UNIT
8990                   + UNITS_PER_WORD;
8991   else if (stack_realign_fp)
8992     red_offset -= crtl->stack_alignment_needed / BITS_PER_UNIT
8993                   - UNITS_PER_WORD;
8994   if (ix86_static_chain_on_stack)
8995     red_offset -= UNITS_PER_WORD;
8996   if (frame_pointer_needed)
8997     red_offset -= UNITS_PER_WORD;
8998
8999   /* If we're only restoring one register and sp is not valid then
9000      using a move instruction to restore the register since it's
9001      less work than reloading sp and popping the register.
9002
9003      The default code result in stack adjustment using add/lea instruction,
9004      while this code results in LEAVE instruction (or discrete equivalent),
9005      so it is profitable in some other cases as well.  Especially when there
9006      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
9007      and there is exactly one register to pop. This heuristic may need some
9008      tuning in future.  */
9009   if ((!sp_valid && (frame.nregs + frame.nsseregs) <= 1)
9010       || (TARGET_EPILOGUE_USING_MOVE
9011           && cfun->machine->use_fast_prologue_epilogue
9012           && ((frame.nregs + frame.nsseregs) > 1
9013               || (frame.to_allocate + frame.padding0) != 0))
9014       || (frame_pointer_needed && !(frame.nregs + frame.nsseregs)
9015           && (frame.to_allocate + frame.padding0) != 0)
9016       || (frame_pointer_needed && TARGET_USE_LEAVE
9017           && cfun->machine->use_fast_prologue_epilogue
9018           && (frame.nregs + frame.nsseregs) == 1)
9019       || crtl->calls_eh_return)
9020     {
9021       /* Restore registers.  We can use ebp or esp to address the memory
9022          locations.  If both are available, default to ebp, since offsets
9023          are known to be small.  Only exception is esp pointing directly
9024          to the end of block of saved registers, where we may simplify
9025          addressing mode.  
9026
9027          If we are realigning stack with bp and sp, regs restore can't
9028          be addressed by bp. sp must be used instead.  */
9029
9030       if (!frame_pointer_needed
9031           || (sp_valid && !(frame.to_allocate + frame.padding0)) 
9032           || stack_realign_fp)
9033         {
9034           ix86_emit_restore_sse_regs_using_mov (stack_pointer_rtx,
9035                                                 frame.to_allocate, red_offset,
9036                                                 style == 2);
9037           ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
9038                                             frame.to_allocate
9039                                             + frame.nsseregs * 16
9040                                             + frame.padding0,
9041                                             red_offset
9042                                             + frame.nsseregs * 16
9043                                             + frame.padding0, style == 2);
9044         }
9045       else
9046         {
9047           ix86_emit_restore_sse_regs_using_mov (hard_frame_pointer_rtx,
9048                                                 offset, red_offset,
9049                                                 style == 2);
9050           ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
9051                                             offset
9052                                             + frame.nsseregs * 16
9053                                             + frame.padding0,
9054                                             red_offset
9055                                             + frame.nsseregs * 16
9056                                             + frame.padding0, style == 2);
9057         }
9058
9059       red_offset -= offset;
9060
9061       /* eh_return epilogues need %ecx added to the stack pointer.  */
9062       if (style == 2)
9063         {
9064           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
9065
9066           /* Stack align doesn't work with eh_return.  */
9067           gcc_assert (!crtl->stack_realign_needed);
9068           /* Neither does regparm nested functions.  */
9069           gcc_assert (!ix86_static_chain_on_stack);
9070
9071           if (frame_pointer_needed)
9072             {
9073               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
9074               tmp = plus_constant (tmp, UNITS_PER_WORD);
9075               tmp = emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
9076
9077               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
9078               tmp = emit_move_insn (hard_frame_pointer_rtx, tmp);
9079
9080               /* Note that we use SA as a temporary CFA, as the return
9081                  address is at the proper place relative to it.  We
9082                  pretend this happens at the FP restore insn because
9083                  prior to this insn the FP would be stored at the wrong
9084                  offset relative to SA, and after this insn we have no
9085                  other reasonable register to use for the CFA.  We don't
9086                  bother resetting the CFA to the SP for the duration of
9087                  the return insn.  */
9088               add_reg_note (tmp, REG_CFA_DEF_CFA,
9089                             plus_constant (sa, UNITS_PER_WORD));
9090               ix86_add_queued_cfa_restore_notes (tmp);
9091               add_reg_note (tmp, REG_CFA_RESTORE, hard_frame_pointer_rtx);
9092               RTX_FRAME_RELATED_P (tmp) = 1;
9093               ix86_cfa_state->reg = sa;
9094               ix86_cfa_state->offset = UNITS_PER_WORD;
9095
9096               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
9097                                          const0_rtx, style, false);
9098             }
9099           else
9100             {
9101               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
9102               tmp = plus_constant (tmp, (frame.to_allocate
9103                                          + frame.nregs * UNITS_PER_WORD
9104                                          + frame.nsseregs * 16
9105                                          + frame.padding0));
9106               tmp = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
9107               ix86_add_queued_cfa_restore_notes (tmp);
9108
9109               gcc_assert (ix86_cfa_state->reg == stack_pointer_rtx);
9110               if (ix86_cfa_state->offset != UNITS_PER_WORD)
9111                 {
9112                   ix86_cfa_state->offset = UNITS_PER_WORD;
9113                   add_reg_note (tmp, REG_CFA_DEF_CFA,
9114                                 plus_constant (stack_pointer_rtx,
9115                                                UNITS_PER_WORD));
9116                   RTX_FRAME_RELATED_P (tmp) = 1;
9117                 }
9118             }
9119         }
9120       else if (!frame_pointer_needed)
9121         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9122                                    GEN_INT (frame.to_allocate
9123                                             + frame.nregs * UNITS_PER_WORD
9124                                             + frame.nsseregs * 16
9125                                             + frame.padding0),
9126                                    style, !using_drap);
9127       /* If not an i386, mov & pop is faster than "leave".  */
9128       else if (TARGET_USE_LEAVE || optimize_function_for_size_p (cfun)
9129                || !cfun->machine->use_fast_prologue_epilogue)
9130         ix86_emit_leave (red_offset);
9131       else
9132         {
9133           pro_epilogue_adjust_stack (stack_pointer_rtx,
9134                                      hard_frame_pointer_rtx,
9135                                      const0_rtx, style, !using_drap);
9136
9137           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx, red_offset);
9138         }
9139     }
9140   else
9141     {
9142       /* First step is to deallocate the stack frame so that we can
9143          pop the registers.
9144
9145          If we realign stack with frame pointer, then stack pointer
9146          won't be able to recover via lea $offset(%bp), %sp, because
9147          there is a padding area between bp and sp for realign. 
9148          "add $to_allocate, %sp" must be used instead.  */
9149       if (!sp_valid)
9150         {
9151           gcc_assert (frame_pointer_needed);
9152           gcc_assert (!stack_realign_fp);
9153           pro_epilogue_adjust_stack (stack_pointer_rtx,
9154                                      hard_frame_pointer_rtx,
9155                                      GEN_INT (offset), style, false);
9156           ix86_emit_restore_sse_regs_using_mov (stack_pointer_rtx,
9157                                                 0, red_offset,
9158                                                 style == 2);
9159           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9160                                      GEN_INT (frame.nsseregs * 16
9161                                               + frame.padding0),
9162                                      style, false);
9163         }
9164       else if (frame.to_allocate || frame.padding0 || frame.nsseregs)
9165         {
9166           ix86_emit_restore_sse_regs_using_mov (stack_pointer_rtx,
9167                                                 frame.to_allocate, red_offset,
9168                                                 style == 2);
9169           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9170                                      GEN_INT (frame.to_allocate
9171                                               + frame.nsseregs * 16
9172                                               + frame.padding0), style,
9173                                      !using_drap && !frame_pointer_needed);
9174         }
9175
9176       ix86_emit_restore_regs_using_pop (red_offset + frame.nsseregs * 16
9177                                         + frame.padding0);
9178       red_offset -= offset;
9179
9180       if (frame_pointer_needed)
9181         {
9182           /* Leave results in shorter dependency chains on CPUs that are
9183              able to grok it fast.  */
9184           if (TARGET_USE_LEAVE)
9185             ix86_emit_leave (red_offset);
9186           else
9187             {
9188               /* For stack realigned really happens, recover stack 
9189                  pointer to hard frame pointer is a must, if not using 
9190                  leave.  */
9191               if (stack_realign_fp)
9192                 pro_epilogue_adjust_stack (stack_pointer_rtx,
9193                                            hard_frame_pointer_rtx,
9194                                            const0_rtx, style, !using_drap);
9195               ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx,
9196                                                red_offset);
9197             }
9198         }
9199     }
9200
9201   if (using_drap)
9202     {
9203       int param_ptr_offset = UNITS_PER_WORD;
9204       rtx insn;
9205
9206       gcc_assert (stack_realign_drap);
9207
9208       if (ix86_static_chain_on_stack)
9209         param_ptr_offset += UNITS_PER_WORD;
9210       if (!call_used_regs[REGNO (crtl->drap_reg)])
9211         param_ptr_offset += UNITS_PER_WORD;
9212
9213       insn = emit_insn ((*ix86_gen_add3) (stack_pointer_rtx,
9214                                           crtl->drap_reg,
9215                                           GEN_INT (-param_ptr_offset)));
9216
9217       ix86_cfa_state->reg = stack_pointer_rtx;
9218       ix86_cfa_state->offset = param_ptr_offset;
9219
9220       add_reg_note (insn, REG_CFA_DEF_CFA,
9221                     gen_rtx_PLUS (Pmode, ix86_cfa_state->reg,
9222                                   GEN_INT (ix86_cfa_state->offset)));
9223       RTX_FRAME_RELATED_P (insn) = 1;
9224
9225       if (!call_used_regs[REGNO (crtl->drap_reg)])
9226         ix86_emit_restore_reg_using_pop (crtl->drap_reg, -UNITS_PER_WORD);
9227     }
9228
9229   /* Remove the saved static chain from the stack.  The use of ECX is
9230      merely as a scratch register, not as the actual static chain.  */
9231   if (ix86_static_chain_on_stack)
9232     {
9233       rtx r, insn;
9234
9235       gcc_assert (ix86_cfa_state->reg == stack_pointer_rtx);
9236       ix86_cfa_state->offset += UNITS_PER_WORD;
9237     
9238       r = gen_rtx_REG (Pmode, CX_REG);
9239       insn = emit_insn (ix86_gen_pop1 (r));
9240
9241       r = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
9242       r = gen_rtx_SET (VOIDmode, stack_pointer_rtx, r);
9243       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
9244       RTX_FRAME_RELATED_P (insn) = 1;
9245     }
9246
9247   /* Sibcall epilogues don't want a return instruction.  */
9248   if (style == 0)
9249     {
9250       *ix86_cfa_state = cfa_state_save;
9251       return;
9252     }
9253
9254   if (crtl->args.pops_args && crtl->args.size)
9255     {
9256       rtx popc = GEN_INT (crtl->args.pops_args);
9257
9258       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
9259          address, do explicit add, and jump indirectly to the caller.  */
9260
9261       if (crtl->args.pops_args >= 65536)
9262         {
9263           rtx ecx = gen_rtx_REG (SImode, CX_REG);
9264           rtx insn;
9265
9266           /* There is no "pascal" calling convention in any 64bit ABI.  */
9267           gcc_assert (!TARGET_64BIT);
9268
9269           insn = emit_insn (gen_popsi1 (ecx));
9270           ix86_cfa_state->offset -= UNITS_PER_WORD;
9271
9272           add_reg_note (insn, REG_CFA_ADJUST_CFA,
9273                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
9274           add_reg_note (insn, REG_CFA_REGISTER,
9275                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
9276           RTX_FRAME_RELATED_P (insn) = 1;
9277
9278           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9279                                      popc, -1, true);
9280           emit_jump_insn (gen_return_indirect_internal (ecx));
9281         }
9282       else
9283         emit_jump_insn (gen_return_pop_internal (popc));
9284     }
9285   else
9286     emit_jump_insn (gen_return_internal ());
9287
9288   /* Restore the state back to the state from the prologue,
9289      so that it's correct for the next epilogue.  */
9290   *ix86_cfa_state = cfa_state_save;
9291 }
9292
9293 /* Reset from the function's potential modifications.  */
9294
9295 static void
9296 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9297                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
9298 {
9299   if (pic_offset_table_rtx)
9300     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
9301 #if TARGET_MACHO
9302   /* Mach-O doesn't support labels at the end of objects, so if
9303      it looks like we might want one, insert a NOP.  */
9304   {
9305     rtx insn = get_last_insn ();
9306     while (insn
9307            && NOTE_P (insn)
9308            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
9309       insn = PREV_INSN (insn);
9310     if (insn
9311         && (LABEL_P (insn)
9312             || (NOTE_P (insn)
9313                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
9314       fputs ("\tnop\n", file);
9315   }
9316 #endif
9317
9318 }
9319 \f
9320 /* Extract the parts of an RTL expression that is a valid memory address
9321    for an instruction.  Return 0 if the structure of the address is
9322    grossly off.  Return -1 if the address contains ASHIFT, so it is not
9323    strictly valid, but still used for computing length of lea instruction.  */
9324
9325 int
9326 ix86_decompose_address (rtx addr, struct ix86_address *out)
9327 {
9328   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
9329   rtx base_reg, index_reg;
9330   HOST_WIDE_INT scale = 1;
9331   rtx scale_rtx = NULL_RTX;
9332   int retval = 1;
9333   enum ix86_address_seg seg = SEG_DEFAULT;
9334
9335   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
9336     base = addr;
9337   else if (GET_CODE (addr) == PLUS)
9338     {
9339       rtx addends[4], op;
9340       int n = 0, i;
9341
9342       op = addr;
9343       do
9344         {
9345           if (n >= 4)
9346             return 0;
9347           addends[n++] = XEXP (op, 1);
9348           op = XEXP (op, 0);
9349         }
9350       while (GET_CODE (op) == PLUS);
9351       if (n >= 4)
9352         return 0;
9353       addends[n] = op;
9354
9355       for (i = n; i >= 0; --i)
9356         {
9357           op = addends[i];
9358           switch (GET_CODE (op))
9359             {
9360             case MULT:
9361               if (index)
9362                 return 0;
9363               index = XEXP (op, 0);
9364               scale_rtx = XEXP (op, 1);
9365               break;
9366
9367             case UNSPEC:
9368               if (XINT (op, 1) == UNSPEC_TP
9369                   && TARGET_TLS_DIRECT_SEG_REFS
9370                   && seg == SEG_DEFAULT)
9371                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
9372               else
9373                 return 0;
9374               break;
9375
9376             case REG:
9377             case SUBREG:
9378               if (!base)
9379                 base = op;
9380               else if (!index)
9381                 index = op;
9382               else
9383                 return 0;
9384               break;
9385
9386             case CONST:
9387             case CONST_INT:
9388             case SYMBOL_REF:
9389             case LABEL_REF:
9390               if (disp)
9391                 return 0;
9392               disp = op;
9393               break;
9394
9395             default:
9396               return 0;
9397             }
9398         }
9399     }
9400   else if (GET_CODE (addr) == MULT)
9401     {
9402       index = XEXP (addr, 0);           /* index*scale */
9403       scale_rtx = XEXP (addr, 1);
9404     }
9405   else if (GET_CODE (addr) == ASHIFT)
9406     {
9407       rtx tmp;
9408
9409       /* We're called for lea too, which implements ashift on occasion.  */
9410       index = XEXP (addr, 0);
9411       tmp = XEXP (addr, 1);
9412       if (!CONST_INT_P (tmp))
9413         return 0;
9414       scale = INTVAL (tmp);
9415       if ((unsigned HOST_WIDE_INT) scale > 3)
9416         return 0;
9417       scale = 1 << scale;
9418       retval = -1;
9419     }
9420   else
9421     disp = addr;                        /* displacement */
9422
9423   /* Extract the integral value of scale.  */
9424   if (scale_rtx)
9425     {
9426       if (!CONST_INT_P (scale_rtx))
9427         return 0;
9428       scale = INTVAL (scale_rtx);
9429     }
9430
9431   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
9432   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
9433
9434   /* Avoid useless 0 displacement.  */
9435   if (disp == const0_rtx && (base || index))
9436     disp = NULL_RTX;
9437
9438   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
9439   if (base_reg && index_reg && scale == 1
9440       && (index_reg == arg_pointer_rtx
9441           || index_reg == frame_pointer_rtx
9442           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
9443     {
9444       rtx tmp;
9445       tmp = base, base = index, index = tmp;
9446       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
9447     }
9448
9449   /* Special case: %ebp cannot be encoded as a base without a displacement.
9450      Similarly %r13.  */
9451   if (!disp
9452       && base_reg
9453       && (base_reg == hard_frame_pointer_rtx
9454           || base_reg == frame_pointer_rtx
9455           || base_reg == arg_pointer_rtx
9456           || (REG_P (base_reg)
9457               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
9458                   || REGNO (base_reg) == R13_REG))))
9459     disp = const0_rtx;
9460
9461   /* Special case: on K6, [%esi] makes the instruction vector decoded.
9462      Avoid this by transforming to [%esi+0].
9463      Reload calls address legitimization without cfun defined, so we need
9464      to test cfun for being non-NULL. */
9465   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
9466       && base_reg && !index_reg && !disp
9467       && REG_P (base_reg)
9468       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
9469     disp = const0_rtx;
9470
9471   /* Special case: encode reg+reg instead of reg*2.  */
9472   if (!base && index && scale == 2)
9473     base = index, base_reg = index_reg, scale = 1;
9474
9475   /* Special case: scaling cannot be encoded without base or displacement.  */
9476   if (!base && !disp && index && scale != 1)
9477     disp = const0_rtx;
9478
9479   out->base = base;
9480   out->index = index;
9481   out->disp = disp;
9482   out->scale = scale;
9483   out->seg = seg;
9484
9485   return retval;
9486 }
9487 \f
9488 /* Return cost of the memory address x.
9489    For i386, it is better to use a complex address than let gcc copy
9490    the address into a reg and make a new pseudo.  But not if the address
9491    requires to two regs - that would mean more pseudos with longer
9492    lifetimes.  */
9493 static int
9494 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
9495 {
9496   struct ix86_address parts;
9497   int cost = 1;
9498   int ok = ix86_decompose_address (x, &parts);
9499
9500   gcc_assert (ok);
9501
9502   if (parts.base && GET_CODE (parts.base) == SUBREG)
9503     parts.base = SUBREG_REG (parts.base);
9504   if (parts.index && GET_CODE (parts.index) == SUBREG)
9505     parts.index = SUBREG_REG (parts.index);
9506
9507   /* Attempt to minimize number of registers in the address.  */
9508   if ((parts.base
9509        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
9510       || (parts.index
9511           && (!REG_P (parts.index)
9512               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
9513     cost++;
9514
9515   if (parts.base
9516       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
9517       && parts.index
9518       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
9519       && parts.base != parts.index)
9520     cost++;
9521
9522   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
9523      since it's predecode logic can't detect the length of instructions
9524      and it degenerates to vector decoded.  Increase cost of such
9525      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
9526      to split such addresses or even refuse such addresses at all.
9527
9528      Following addressing modes are affected:
9529       [base+scale*index]
9530       [scale*index+disp]
9531       [base+index]
9532
9533      The first and last case  may be avoidable by explicitly coding the zero in
9534      memory address, but I don't have AMD-K6 machine handy to check this
9535      theory.  */
9536
9537   if (TARGET_K6
9538       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
9539           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
9540           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
9541     cost += 10;
9542
9543   return cost;
9544 }
9545 \f
9546 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
9547    this is used for to form addresses to local data when -fPIC is in
9548    use.  */
9549
9550 static bool
9551 darwin_local_data_pic (rtx disp)
9552 {
9553   return (GET_CODE (disp) == UNSPEC
9554           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
9555 }
9556
9557 /* Determine if a given RTX is a valid constant.  We already know this
9558    satisfies CONSTANT_P.  */
9559
9560 bool
9561 legitimate_constant_p (rtx x)
9562 {
9563   switch (GET_CODE (x))
9564     {
9565     case CONST:
9566       x = XEXP (x, 0);
9567
9568       if (GET_CODE (x) == PLUS)
9569         {
9570           if (!CONST_INT_P (XEXP (x, 1)))
9571             return false;
9572           x = XEXP (x, 0);
9573         }
9574
9575       if (TARGET_MACHO && darwin_local_data_pic (x))
9576         return true;
9577
9578       /* Only some unspecs are valid as "constants".  */
9579       if (GET_CODE (x) == UNSPEC)
9580         switch (XINT (x, 1))
9581           {
9582           case UNSPEC_GOT:
9583           case UNSPEC_GOTOFF:
9584           case UNSPEC_PLTOFF:
9585             return TARGET_64BIT;
9586           case UNSPEC_TPOFF:
9587           case UNSPEC_NTPOFF:
9588             x = XVECEXP (x, 0, 0);
9589             return (GET_CODE (x) == SYMBOL_REF
9590                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
9591           case UNSPEC_DTPOFF:
9592             x = XVECEXP (x, 0, 0);
9593             return (GET_CODE (x) == SYMBOL_REF
9594                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
9595           default:
9596             return false;
9597           }
9598
9599       /* We must have drilled down to a symbol.  */
9600       if (GET_CODE (x) == LABEL_REF)
9601         return true;
9602       if (GET_CODE (x) != SYMBOL_REF)
9603         return false;
9604       /* FALLTHRU */
9605
9606     case SYMBOL_REF:
9607       /* TLS symbols are never valid.  */
9608       if (SYMBOL_REF_TLS_MODEL (x))
9609         return false;
9610
9611       /* DLLIMPORT symbols are never valid.  */
9612       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
9613           && SYMBOL_REF_DLLIMPORT_P (x))
9614         return false;
9615       break;
9616
9617     case CONST_DOUBLE:
9618       if (GET_MODE (x) == TImode
9619           && x != CONST0_RTX (TImode)
9620           && !TARGET_64BIT)
9621         return false;
9622       break;
9623
9624     case CONST_VECTOR:
9625       if (!standard_sse_constant_p (x))
9626         return false;
9627
9628     default:
9629       break;
9630     }
9631
9632   /* Otherwise we handle everything else in the move patterns.  */
9633   return true;
9634 }
9635
9636 /* Determine if it's legal to put X into the constant pool.  This
9637    is not possible for the address of thread-local symbols, which
9638    is checked above.  */
9639
9640 static bool
9641 ix86_cannot_force_const_mem (rtx x)
9642 {
9643   /* We can always put integral constants and vectors in memory.  */
9644   switch (GET_CODE (x))
9645     {
9646     case CONST_INT:
9647     case CONST_DOUBLE:
9648     case CONST_VECTOR:
9649       return false;
9650
9651     default:
9652       break;
9653     }
9654   return !legitimate_constant_p (x);
9655 }
9656
9657
9658 /* Nonzero if the constant value X is a legitimate general operand
9659    when generating PIC code.  It is given that flag_pic is on and
9660    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
9661
9662 bool
9663 legitimate_pic_operand_p (rtx x)
9664 {
9665   rtx inner;
9666
9667   switch (GET_CODE (x))
9668     {
9669     case CONST:
9670       inner = XEXP (x, 0);
9671       if (GET_CODE (inner) == PLUS
9672           && CONST_INT_P (XEXP (inner, 1)))
9673         inner = XEXP (inner, 0);
9674
9675       /* Only some unspecs are valid as "constants".  */
9676       if (GET_CODE (inner) == UNSPEC)
9677         switch (XINT (inner, 1))
9678           {
9679           case UNSPEC_GOT:
9680           case UNSPEC_GOTOFF:
9681           case UNSPEC_PLTOFF:
9682             return TARGET_64BIT;
9683           case UNSPEC_TPOFF:
9684             x = XVECEXP (inner, 0, 0);
9685             return (GET_CODE (x) == SYMBOL_REF
9686                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
9687           case UNSPEC_MACHOPIC_OFFSET:
9688             return legitimate_pic_address_disp_p (x);
9689           default:
9690             return false;
9691           }
9692       /* FALLTHRU */
9693
9694     case SYMBOL_REF:
9695     case LABEL_REF:
9696       return legitimate_pic_address_disp_p (x);
9697
9698     default:
9699       return true;
9700     }
9701 }
9702
9703 /* Determine if a given CONST RTX is a valid memory displacement
9704    in PIC mode.  */
9705
9706 int
9707 legitimate_pic_address_disp_p (rtx disp)
9708 {
9709   bool saw_plus;
9710
9711   /* In 64bit mode we can allow direct addresses of symbols and labels
9712      when they are not dynamic symbols.  */
9713   if (TARGET_64BIT)
9714     {
9715       rtx op0 = disp, op1;
9716
9717       switch (GET_CODE (disp))
9718         {
9719         case LABEL_REF:
9720           return true;
9721
9722         case CONST:
9723           if (GET_CODE (XEXP (disp, 0)) != PLUS)
9724             break;
9725           op0 = XEXP (XEXP (disp, 0), 0);
9726           op1 = XEXP (XEXP (disp, 0), 1);
9727           if (!CONST_INT_P (op1)
9728               || INTVAL (op1) >= 16*1024*1024
9729               || INTVAL (op1) < -16*1024*1024)
9730             break;
9731           if (GET_CODE (op0) == LABEL_REF)
9732             return true;
9733           if (GET_CODE (op0) != SYMBOL_REF)
9734             break;
9735           /* FALLTHRU */
9736
9737         case SYMBOL_REF:
9738           /* TLS references should always be enclosed in UNSPEC.  */
9739           if (SYMBOL_REF_TLS_MODEL (op0))
9740             return false;
9741           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
9742               && ix86_cmodel != CM_LARGE_PIC)
9743             return true;
9744           break;
9745
9746         default:
9747           break;
9748         }
9749     }
9750   if (GET_CODE (disp) != CONST)
9751     return 0;
9752   disp = XEXP (disp, 0);
9753
9754   if (TARGET_64BIT)
9755     {
9756       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
9757          of GOT tables.  We should not need these anyway.  */
9758       if (GET_CODE (disp) != UNSPEC
9759           || (XINT (disp, 1) != UNSPEC_GOTPCREL
9760               && XINT (disp, 1) != UNSPEC_GOTOFF
9761               && XINT (disp, 1) != UNSPEC_PLTOFF))
9762         return 0;
9763
9764       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
9765           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
9766         return 0;
9767       return 1;
9768     }
9769
9770   saw_plus = false;
9771   if (GET_CODE (disp) == PLUS)
9772     {
9773       if (!CONST_INT_P (XEXP (disp, 1)))
9774         return 0;
9775       disp = XEXP (disp, 0);
9776       saw_plus = true;
9777     }
9778
9779   if (TARGET_MACHO && darwin_local_data_pic (disp))
9780     return 1;
9781
9782   if (GET_CODE (disp) != UNSPEC)
9783     return 0;
9784
9785   switch (XINT (disp, 1))
9786     {
9787     case UNSPEC_GOT:
9788       if (saw_plus)
9789         return false;
9790       /* We need to check for both symbols and labels because VxWorks loads
9791          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
9792          details.  */
9793       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
9794               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
9795     case UNSPEC_GOTOFF:
9796       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
9797          While ABI specify also 32bit relocation but we don't produce it in
9798          small PIC model at all.  */
9799       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
9800            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
9801           && !TARGET_64BIT)
9802         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
9803       return false;
9804     case UNSPEC_GOTTPOFF:
9805     case UNSPEC_GOTNTPOFF:
9806     case UNSPEC_INDNTPOFF:
9807       if (saw_plus)
9808         return false;
9809       disp = XVECEXP (disp, 0, 0);
9810       return (GET_CODE (disp) == SYMBOL_REF
9811               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
9812     case UNSPEC_NTPOFF:
9813       disp = XVECEXP (disp, 0, 0);
9814       return (GET_CODE (disp) == SYMBOL_REF
9815               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
9816     case UNSPEC_DTPOFF:
9817       disp = XVECEXP (disp, 0, 0);
9818       return (GET_CODE (disp) == SYMBOL_REF
9819               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
9820     }
9821
9822   return 0;
9823 }
9824
9825 /* Recognizes RTL expressions that are valid memory addresses for an
9826    instruction.  The MODE argument is the machine mode for the MEM
9827    expression that wants to use this address.
9828
9829    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
9830    convert common non-canonical forms to canonical form so that they will
9831    be recognized.  */
9832
9833 static bool
9834 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
9835                            rtx addr, bool strict)
9836 {
9837   struct ix86_address parts;
9838   rtx base, index, disp;
9839   HOST_WIDE_INT scale;
9840
9841   if (ix86_decompose_address (addr, &parts) <= 0)
9842     /* Decomposition failed.  */
9843     return false;
9844
9845   base = parts.base;
9846   index = parts.index;
9847   disp = parts.disp;
9848   scale = parts.scale;
9849
9850   /* Validate base register.
9851
9852      Don't allow SUBREG's that span more than a word here.  It can lead to spill
9853      failures when the base is one word out of a two word structure, which is
9854      represented internally as a DImode int.  */
9855
9856   if (base)
9857     {
9858       rtx reg;
9859
9860       if (REG_P (base))
9861         reg = base;
9862       else if (GET_CODE (base) == SUBREG
9863                && REG_P (SUBREG_REG (base))
9864                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
9865                   <= UNITS_PER_WORD)
9866         reg = SUBREG_REG (base);
9867       else
9868         /* Base is not a register.  */
9869         return false;
9870
9871       if (GET_MODE (base) != Pmode)
9872         /* Base is not in Pmode.  */
9873         return false;
9874
9875       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
9876           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
9877         /* Base is not valid.  */
9878         return false;
9879     }
9880
9881   /* Validate index register.
9882
9883      Don't allow SUBREG's that span more than a word here -- same as above.  */
9884
9885   if (index)
9886     {
9887       rtx reg;
9888
9889       if (REG_P (index))
9890         reg = index;
9891       else if (GET_CODE (index) == SUBREG
9892                && REG_P (SUBREG_REG (index))
9893                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
9894                   <= UNITS_PER_WORD)
9895         reg = SUBREG_REG (index);
9896       else
9897         /* Index is not a register.  */
9898         return false;
9899
9900       if (GET_MODE (index) != Pmode)
9901         /* Index is not in Pmode.  */
9902         return false;
9903
9904       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
9905           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
9906         /* Index is not valid.  */
9907         return false;
9908     }
9909
9910   /* Validate scale factor.  */
9911   if (scale != 1)
9912     {
9913       if (!index)
9914         /* Scale without index.  */
9915         return false;
9916
9917       if (scale != 2 && scale != 4 && scale != 8)
9918         /* Scale is not a valid multiplier.  */
9919         return false;
9920     }
9921
9922   /* Validate displacement.  */
9923   if (disp)
9924     {
9925       if (GET_CODE (disp) == CONST
9926           && GET_CODE (XEXP (disp, 0)) == UNSPEC
9927           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
9928         switch (XINT (XEXP (disp, 0), 1))
9929           {
9930           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
9931              used.  While ABI specify also 32bit relocations, we don't produce
9932              them at all and use IP relative instead.  */
9933           case UNSPEC_GOT:
9934           case UNSPEC_GOTOFF:
9935             gcc_assert (flag_pic);
9936             if (!TARGET_64BIT)
9937               goto is_legitimate_pic;
9938
9939             /* 64bit address unspec.  */
9940             return false;
9941
9942           case UNSPEC_GOTPCREL:
9943             gcc_assert (flag_pic);
9944             goto is_legitimate_pic;
9945
9946           case UNSPEC_GOTTPOFF:
9947           case UNSPEC_GOTNTPOFF:
9948           case UNSPEC_INDNTPOFF:
9949           case UNSPEC_NTPOFF:
9950           case UNSPEC_DTPOFF:
9951             break;
9952
9953           default:
9954             /* Invalid address unspec.  */
9955             return false;
9956           }
9957
9958       else if (SYMBOLIC_CONST (disp)
9959                && (flag_pic
9960                    || (TARGET_MACHO
9961 #if TARGET_MACHO
9962                        && MACHOPIC_INDIRECT
9963                        && !machopic_operand_p (disp)
9964 #endif
9965                )))
9966         {
9967
9968         is_legitimate_pic:
9969           if (TARGET_64BIT && (index || base))
9970             {
9971               /* foo@dtpoff(%rX) is ok.  */
9972               if (GET_CODE (disp) != CONST
9973                   || GET_CODE (XEXP (disp, 0)) != PLUS
9974                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
9975                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
9976                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
9977                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
9978                 /* Non-constant pic memory reference.  */
9979                 return false;
9980             }
9981           else if (! legitimate_pic_address_disp_p (disp))
9982             /* Displacement is an invalid pic construct.  */
9983             return false;
9984
9985           /* This code used to verify that a symbolic pic displacement
9986              includes the pic_offset_table_rtx register.
9987
9988              While this is good idea, unfortunately these constructs may
9989              be created by "adds using lea" optimization for incorrect
9990              code like:
9991
9992              int a;
9993              int foo(int i)
9994                {
9995                  return *(&a+i);
9996                }
9997
9998              This code is nonsensical, but results in addressing
9999              GOT table with pic_offset_table_rtx base.  We can't
10000              just refuse it easily, since it gets matched by
10001              "addsi3" pattern, that later gets split to lea in the
10002              case output register differs from input.  While this
10003              can be handled by separate addsi pattern for this case
10004              that never results in lea, this seems to be easier and
10005              correct fix for crash to disable this test.  */
10006         }
10007       else if (GET_CODE (disp) != LABEL_REF
10008                && !CONST_INT_P (disp)
10009                && (GET_CODE (disp) != CONST
10010                    || !legitimate_constant_p (disp))
10011                && (GET_CODE (disp) != SYMBOL_REF
10012                    || !legitimate_constant_p (disp)))
10013         /* Displacement is not constant.  */
10014         return false;
10015       else if (TARGET_64BIT
10016                && !x86_64_immediate_operand (disp, VOIDmode))
10017         /* Displacement is out of range.  */
10018         return false;
10019     }
10020
10021   /* Everything looks valid.  */
10022   return true;
10023 }
10024
10025 /* Determine if a given RTX is a valid constant address.  */
10026
10027 bool
10028 constant_address_p (rtx x)
10029 {
10030   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
10031 }
10032 \f
10033 /* Return a unique alias set for the GOT.  */
10034
10035 static alias_set_type
10036 ix86_GOT_alias_set (void)
10037 {
10038   static alias_set_type set = -1;
10039   if (set == -1)
10040     set = new_alias_set ();
10041   return set;
10042 }
10043
10044 /* Return a legitimate reference for ORIG (an address) using the
10045    register REG.  If REG is 0, a new pseudo is generated.
10046
10047    There are two types of references that must be handled:
10048
10049    1. Global data references must load the address from the GOT, via
10050       the PIC reg.  An insn is emitted to do this load, and the reg is
10051       returned.
10052
10053    2. Static data references, constant pool addresses, and code labels
10054       compute the address as an offset from the GOT, whose base is in
10055       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
10056       differentiate them from global data objects.  The returned
10057       address is the PIC reg + an unspec constant.
10058
10059    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
10060    reg also appears in the address.  */
10061
10062 static rtx
10063 legitimize_pic_address (rtx orig, rtx reg)
10064 {
10065   rtx addr = orig;
10066   rtx new_rtx = orig;
10067   rtx base;
10068
10069 #if TARGET_MACHO
10070   if (TARGET_MACHO && !TARGET_64BIT)
10071     {
10072       if (reg == 0)
10073         reg = gen_reg_rtx (Pmode);
10074       /* Use the generic Mach-O PIC machinery.  */
10075       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
10076     }
10077 #endif
10078
10079   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
10080     new_rtx = addr;
10081   else if (TARGET_64BIT
10082            && ix86_cmodel != CM_SMALL_PIC
10083            && gotoff_operand (addr, Pmode))
10084     {
10085       rtx tmpreg;
10086       /* This symbol may be referenced via a displacement from the PIC
10087          base address (@GOTOFF).  */
10088
10089       if (reload_in_progress)
10090         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10091       if (GET_CODE (addr) == CONST)
10092         addr = XEXP (addr, 0);
10093       if (GET_CODE (addr) == PLUS)
10094           {
10095             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
10096                                       UNSPEC_GOTOFF);
10097             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
10098           }
10099         else
10100           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
10101       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10102       if (!reg)
10103         tmpreg = gen_reg_rtx (Pmode);
10104       else
10105         tmpreg = reg;
10106       emit_move_insn (tmpreg, new_rtx);
10107
10108       if (reg != 0)
10109         {
10110           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
10111                                          tmpreg, 1, OPTAB_DIRECT);
10112           new_rtx = reg;
10113         }
10114       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
10115     }
10116   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
10117     {
10118       /* This symbol may be referenced via a displacement from the PIC
10119          base address (@GOTOFF).  */
10120
10121       if (reload_in_progress)
10122         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10123       if (GET_CODE (addr) == CONST)
10124         addr = XEXP (addr, 0);
10125       if (GET_CODE (addr) == PLUS)
10126           {
10127             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
10128                                       UNSPEC_GOTOFF);
10129             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
10130           }
10131         else
10132           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
10133       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10134       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
10135
10136       if (reg != 0)
10137         {
10138           emit_move_insn (reg, new_rtx);
10139           new_rtx = reg;
10140         }
10141     }
10142   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
10143            /* We can't use @GOTOFF for text labels on VxWorks;
10144               see gotoff_operand.  */
10145            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
10146     {
10147       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
10148         {
10149           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
10150             return legitimize_dllimport_symbol (addr, true);
10151           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
10152               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
10153               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
10154             {
10155               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
10156               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
10157             }
10158         }
10159
10160       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
10161         {
10162           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
10163           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10164           new_rtx = gen_const_mem (Pmode, new_rtx);
10165           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
10166
10167           if (reg == 0)
10168             reg = gen_reg_rtx (Pmode);
10169           /* Use directly gen_movsi, otherwise the address is loaded
10170              into register for CSE.  We don't want to CSE this addresses,
10171              instead we CSE addresses from the GOT table, so skip this.  */
10172           emit_insn (gen_movsi (reg, new_rtx));
10173           new_rtx = reg;
10174         }
10175       else
10176         {
10177           /* This symbol must be referenced via a load from the
10178              Global Offset Table (@GOT).  */
10179
10180           if (reload_in_progress)
10181             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10182           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
10183           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10184           if (TARGET_64BIT)
10185             new_rtx = force_reg (Pmode, new_rtx);
10186           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
10187           new_rtx = gen_const_mem (Pmode, new_rtx);
10188           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
10189
10190           if (reg == 0)
10191             reg = gen_reg_rtx (Pmode);
10192           emit_move_insn (reg, new_rtx);
10193           new_rtx = reg;
10194         }
10195     }
10196   else
10197     {
10198       if (CONST_INT_P (addr)
10199           && !x86_64_immediate_operand (addr, VOIDmode))
10200         {
10201           if (reg)
10202             {
10203               emit_move_insn (reg, addr);
10204               new_rtx = reg;
10205             }
10206           else
10207             new_rtx = force_reg (Pmode, addr);
10208         }
10209       else if (GET_CODE (addr) == CONST)
10210         {
10211           addr = XEXP (addr, 0);
10212
10213           /* We must match stuff we generate before.  Assume the only
10214              unspecs that can get here are ours.  Not that we could do
10215              anything with them anyway....  */
10216           if (GET_CODE (addr) == UNSPEC
10217               || (GET_CODE (addr) == PLUS
10218                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
10219             return orig;
10220           gcc_assert (GET_CODE (addr) == PLUS);
10221         }
10222       if (GET_CODE (addr) == PLUS)
10223         {
10224           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
10225
10226           /* Check first to see if this is a constant offset from a @GOTOFF
10227              symbol reference.  */
10228           if (gotoff_operand (op0, Pmode)
10229               && CONST_INT_P (op1))
10230             {
10231               if (!TARGET_64BIT)
10232                 {
10233                   if (reload_in_progress)
10234                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10235                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
10236                                             UNSPEC_GOTOFF);
10237                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
10238                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10239                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
10240
10241                   if (reg != 0)
10242                     {
10243                       emit_move_insn (reg, new_rtx);
10244                       new_rtx = reg;
10245                     }
10246                 }
10247               else
10248                 {
10249                   if (INTVAL (op1) < -16*1024*1024
10250                       || INTVAL (op1) >= 16*1024*1024)
10251                     {
10252                       if (!x86_64_immediate_operand (op1, Pmode))
10253                         op1 = force_reg (Pmode, op1);
10254                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
10255                     }
10256                 }
10257             }
10258           else
10259             {
10260               base = legitimize_pic_address (XEXP (addr, 0), reg);
10261               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
10262                                                  base == reg ? NULL_RTX : reg);
10263
10264               if (CONST_INT_P (new_rtx))
10265                 new_rtx = plus_constant (base, INTVAL (new_rtx));
10266               else
10267                 {
10268                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
10269                     {
10270                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
10271                       new_rtx = XEXP (new_rtx, 1);
10272                     }
10273                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
10274                 }
10275             }
10276         }
10277     }
10278   return new_rtx;
10279 }
10280 \f
10281 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
10282
10283 static rtx
10284 get_thread_pointer (int to_reg)
10285 {
10286   rtx tp, reg, insn;
10287
10288   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
10289   if (!to_reg)
10290     return tp;
10291
10292   reg = gen_reg_rtx (Pmode);
10293   insn = gen_rtx_SET (VOIDmode, reg, tp);
10294   insn = emit_insn (insn);
10295
10296   return reg;
10297 }
10298
10299 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
10300    false if we expect this to be used for a memory address and true if
10301    we expect to load the address into a register.  */
10302
10303 static rtx
10304 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
10305 {
10306   rtx dest, base, off, pic, tp;
10307   int type;
10308
10309   switch (model)
10310     {
10311     case TLS_MODEL_GLOBAL_DYNAMIC:
10312       dest = gen_reg_rtx (Pmode);
10313       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
10314
10315       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
10316         {
10317           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
10318
10319           start_sequence ();
10320           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
10321           insns = get_insns ();
10322           end_sequence ();
10323
10324           RTL_CONST_CALL_P (insns) = 1;
10325           emit_libcall_block (insns, dest, rax, x);
10326         }
10327       else if (TARGET_64BIT && TARGET_GNU2_TLS)
10328         emit_insn (gen_tls_global_dynamic_64 (dest, x));
10329       else
10330         emit_insn (gen_tls_global_dynamic_32 (dest, x));
10331
10332       if (TARGET_GNU2_TLS)
10333         {
10334           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
10335
10336           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
10337         }
10338       break;
10339
10340     case TLS_MODEL_LOCAL_DYNAMIC:
10341       base = gen_reg_rtx (Pmode);
10342       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
10343
10344       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
10345         {
10346           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, note;
10347
10348           start_sequence ();
10349           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
10350           insns = get_insns ();
10351           end_sequence ();
10352
10353           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
10354           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
10355           RTL_CONST_CALL_P (insns) = 1;
10356           emit_libcall_block (insns, base, rax, note);
10357         }
10358       else if (TARGET_64BIT && TARGET_GNU2_TLS)
10359         emit_insn (gen_tls_local_dynamic_base_64 (base));
10360       else
10361         emit_insn (gen_tls_local_dynamic_base_32 (base));
10362
10363       if (TARGET_GNU2_TLS)
10364         {
10365           rtx x = ix86_tls_module_base ();
10366
10367           set_unique_reg_note (get_last_insn (), REG_EQUIV,
10368                                gen_rtx_MINUS (Pmode, x, tp));
10369         }
10370
10371       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
10372       off = gen_rtx_CONST (Pmode, off);
10373
10374       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
10375
10376       if (TARGET_GNU2_TLS)
10377         {
10378           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
10379
10380           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
10381         }
10382
10383       break;
10384
10385     case TLS_MODEL_INITIAL_EXEC:
10386       if (TARGET_64BIT)
10387         {
10388           pic = NULL;
10389           type = UNSPEC_GOTNTPOFF;
10390         }
10391       else if (flag_pic)
10392         {
10393           if (reload_in_progress)
10394             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10395           pic = pic_offset_table_rtx;
10396           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
10397         }
10398       else if (!TARGET_ANY_GNU_TLS)
10399         {
10400           pic = gen_reg_rtx (Pmode);
10401           emit_insn (gen_set_got (pic));
10402           type = UNSPEC_GOTTPOFF;
10403         }
10404       else
10405         {
10406           pic = NULL;
10407           type = UNSPEC_INDNTPOFF;
10408         }
10409
10410       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
10411       off = gen_rtx_CONST (Pmode, off);
10412       if (pic)
10413         off = gen_rtx_PLUS (Pmode, pic, off);
10414       off = gen_const_mem (Pmode, off);
10415       set_mem_alias_set (off, ix86_GOT_alias_set ());
10416
10417       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
10418         {
10419           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
10420           off = force_reg (Pmode, off);
10421           return gen_rtx_PLUS (Pmode, base, off);
10422         }
10423       else
10424         {
10425           base = get_thread_pointer (true);
10426           dest = gen_reg_rtx (Pmode);
10427           emit_insn (gen_subsi3 (dest, base, off));
10428         }
10429       break;
10430
10431     case TLS_MODEL_LOCAL_EXEC:
10432       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
10433                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
10434                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
10435       off = gen_rtx_CONST (Pmode, off);
10436
10437       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
10438         {
10439           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
10440           return gen_rtx_PLUS (Pmode, base, off);
10441         }
10442       else
10443         {
10444           base = get_thread_pointer (true);
10445           dest = gen_reg_rtx (Pmode);
10446           emit_insn (gen_subsi3 (dest, base, off));
10447         }
10448       break;
10449
10450     default:
10451       gcc_unreachable ();
10452     }
10453
10454   return dest;
10455 }
10456
10457 /* Create or return the unique __imp_DECL dllimport symbol corresponding
10458    to symbol DECL.  */
10459
10460 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
10461   htab_t dllimport_map;
10462
10463 static tree
10464 get_dllimport_decl (tree decl)
10465 {
10466   struct tree_map *h, in;
10467   void **loc;
10468   const char *name;
10469   const char *prefix;
10470   size_t namelen, prefixlen;
10471   char *imp_name;
10472   tree to;
10473   rtx rtl;
10474
10475   if (!dllimport_map)
10476     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
10477
10478   in.hash = htab_hash_pointer (decl);
10479   in.base.from = decl;
10480   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
10481   h = (struct tree_map *) *loc;
10482   if (h)
10483     return h->to;
10484
10485   *loc = h = GGC_NEW (struct tree_map);
10486   h->hash = in.hash;
10487   h->base.from = decl;
10488   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
10489                            VAR_DECL, NULL, ptr_type_node);
10490   DECL_ARTIFICIAL (to) = 1;
10491   DECL_IGNORED_P (to) = 1;
10492   DECL_EXTERNAL (to) = 1;
10493   TREE_READONLY (to) = 1;
10494
10495   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
10496   name = targetm.strip_name_encoding (name);
10497   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
10498     ? "*__imp_" : "*__imp__";
10499   namelen = strlen (name);
10500   prefixlen = strlen (prefix);
10501   imp_name = (char *) alloca (namelen + prefixlen + 1);
10502   memcpy (imp_name, prefix, prefixlen);
10503   memcpy (imp_name + prefixlen, name, namelen + 1);
10504
10505   name = ggc_alloc_string (imp_name, namelen + prefixlen);
10506   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
10507   SET_SYMBOL_REF_DECL (rtl, to);
10508   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
10509
10510   rtl = gen_const_mem (Pmode, rtl);
10511   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
10512
10513   SET_DECL_RTL (to, rtl);
10514   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
10515
10516   return to;
10517 }
10518
10519 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
10520    true if we require the result be a register.  */
10521
10522 static rtx
10523 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
10524 {
10525   tree imp_decl;
10526   rtx x;
10527
10528   gcc_assert (SYMBOL_REF_DECL (symbol));
10529   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
10530
10531   x = DECL_RTL (imp_decl);
10532   if (want_reg)
10533     x = force_reg (Pmode, x);
10534   return x;
10535 }
10536
10537 /* Try machine-dependent ways of modifying an illegitimate address
10538    to be legitimate.  If we find one, return the new, valid address.
10539    This macro is used in only one place: `memory_address' in explow.c.
10540
10541    OLDX is the address as it was before break_out_memory_refs was called.
10542    In some cases it is useful to look at this to decide what needs to be done.
10543
10544    It is always safe for this macro to do nothing.  It exists to recognize
10545    opportunities to optimize the output.
10546
10547    For the 80386, we handle X+REG by loading X into a register R and
10548    using R+REG.  R will go in a general reg and indexing will be used.
10549    However, if REG is a broken-out memory address or multiplication,
10550    nothing needs to be done because REG can certainly go in a general reg.
10551
10552    When -fpic is used, special handling is needed for symbolic references.
10553    See comments by legitimize_pic_address in i386.c for details.  */
10554
10555 static rtx
10556 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
10557                          enum machine_mode mode)
10558 {
10559   int changed = 0;
10560   unsigned log;
10561
10562   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
10563   if (log)
10564     return legitimize_tls_address (x, (enum tls_model) log, false);
10565   if (GET_CODE (x) == CONST
10566       && GET_CODE (XEXP (x, 0)) == PLUS
10567       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
10568       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
10569     {
10570       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
10571                                       (enum tls_model) log, false);
10572       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
10573     }
10574
10575   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
10576     {
10577       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
10578         return legitimize_dllimport_symbol (x, true);
10579       if (GET_CODE (x) == CONST
10580           && GET_CODE (XEXP (x, 0)) == PLUS
10581           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
10582           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
10583         {
10584           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
10585           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
10586         }
10587     }
10588
10589   if (flag_pic && SYMBOLIC_CONST (x))
10590     return legitimize_pic_address (x, 0);
10591
10592   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
10593   if (GET_CODE (x) == ASHIFT
10594       && CONST_INT_P (XEXP (x, 1))
10595       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
10596     {
10597       changed = 1;
10598       log = INTVAL (XEXP (x, 1));
10599       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
10600                         GEN_INT (1 << log));
10601     }
10602
10603   if (GET_CODE (x) == PLUS)
10604     {
10605       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
10606
10607       if (GET_CODE (XEXP (x, 0)) == ASHIFT
10608           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
10609           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
10610         {
10611           changed = 1;
10612           log = INTVAL (XEXP (XEXP (x, 0), 1));
10613           XEXP (x, 0) = gen_rtx_MULT (Pmode,
10614                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
10615                                       GEN_INT (1 << log));
10616         }
10617
10618       if (GET_CODE (XEXP (x, 1)) == ASHIFT
10619           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
10620           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
10621         {
10622           changed = 1;
10623           log = INTVAL (XEXP (XEXP (x, 1), 1));
10624           XEXP (x, 1) = gen_rtx_MULT (Pmode,
10625                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
10626                                       GEN_INT (1 << log));
10627         }
10628
10629       /* Put multiply first if it isn't already.  */
10630       if (GET_CODE (XEXP (x, 1)) == MULT)
10631         {
10632           rtx tmp = XEXP (x, 0);
10633           XEXP (x, 0) = XEXP (x, 1);
10634           XEXP (x, 1) = tmp;
10635           changed = 1;
10636         }
10637
10638       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
10639          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
10640          created by virtual register instantiation, register elimination, and
10641          similar optimizations.  */
10642       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
10643         {
10644           changed = 1;
10645           x = gen_rtx_PLUS (Pmode,
10646                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
10647                                           XEXP (XEXP (x, 1), 0)),
10648                             XEXP (XEXP (x, 1), 1));
10649         }
10650
10651       /* Canonicalize
10652          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
10653          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
10654       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
10655                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
10656                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
10657                && CONSTANT_P (XEXP (x, 1)))
10658         {
10659           rtx constant;
10660           rtx other = NULL_RTX;
10661
10662           if (CONST_INT_P (XEXP (x, 1)))
10663             {
10664               constant = XEXP (x, 1);
10665               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
10666             }
10667           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
10668             {
10669               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
10670               other = XEXP (x, 1);
10671             }
10672           else
10673             constant = 0;
10674
10675           if (constant)
10676             {
10677               changed = 1;
10678               x = gen_rtx_PLUS (Pmode,
10679                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
10680                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
10681                                 plus_constant (other, INTVAL (constant)));
10682             }
10683         }
10684
10685       if (changed && ix86_legitimate_address_p (mode, x, FALSE))
10686         return x;
10687
10688       if (GET_CODE (XEXP (x, 0)) == MULT)
10689         {
10690           changed = 1;
10691           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
10692         }
10693
10694       if (GET_CODE (XEXP (x, 1)) == MULT)
10695         {
10696           changed = 1;
10697           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
10698         }
10699
10700       if (changed
10701           && REG_P (XEXP (x, 1))
10702           && REG_P (XEXP (x, 0)))
10703         return x;
10704
10705       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
10706         {
10707           changed = 1;
10708           x = legitimize_pic_address (x, 0);
10709         }
10710
10711       if (changed && ix86_legitimate_address_p (mode, x, FALSE))
10712         return x;
10713
10714       if (REG_P (XEXP (x, 0)))
10715         {
10716           rtx temp = gen_reg_rtx (Pmode);
10717           rtx val  = force_operand (XEXP (x, 1), temp);
10718           if (val != temp)
10719             emit_move_insn (temp, val);
10720
10721           XEXP (x, 1) = temp;
10722           return x;
10723         }
10724
10725       else if (REG_P (XEXP (x, 1)))
10726         {
10727           rtx temp = gen_reg_rtx (Pmode);
10728           rtx val  = force_operand (XEXP (x, 0), temp);
10729           if (val != temp)
10730             emit_move_insn (temp, val);
10731
10732           XEXP (x, 0) = temp;
10733           return x;
10734         }
10735     }
10736
10737   return x;
10738 }
10739 \f
10740 /* Print an integer constant expression in assembler syntax.  Addition
10741    and subtraction are the only arithmetic that may appear in these
10742    expressions.  FILE is the stdio stream to write to, X is the rtx, and
10743    CODE is the operand print code from the output string.  */
10744
10745 static void
10746 output_pic_addr_const (FILE *file, rtx x, int code)
10747 {
10748   char buf[256];
10749
10750   switch (GET_CODE (x))
10751     {
10752     case PC:
10753       gcc_assert (flag_pic);
10754       putc ('.', file);
10755       break;
10756
10757     case SYMBOL_REF:
10758       if (! TARGET_MACHO || TARGET_64BIT)
10759         output_addr_const (file, x);
10760       else
10761         {
10762           const char *name = XSTR (x, 0);
10763
10764           /* Mark the decl as referenced so that cgraph will
10765              output the function.  */
10766           if (SYMBOL_REF_DECL (x))
10767             mark_decl_referenced (SYMBOL_REF_DECL (x));
10768
10769 #if TARGET_MACHO
10770           if (MACHOPIC_INDIRECT
10771               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
10772             name = machopic_indirection_name (x, /*stub_p=*/true);
10773 #endif
10774           assemble_name (file, name);
10775         }
10776       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
10777           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
10778         fputs ("@PLT", file);
10779       break;
10780
10781     case LABEL_REF:
10782       x = XEXP (x, 0);
10783       /* FALLTHRU */
10784     case CODE_LABEL:
10785       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
10786       assemble_name (asm_out_file, buf);
10787       break;
10788
10789     case CONST_INT:
10790       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
10791       break;
10792
10793     case CONST:
10794       /* This used to output parentheses around the expression,
10795          but that does not work on the 386 (either ATT or BSD assembler).  */
10796       output_pic_addr_const (file, XEXP (x, 0), code);
10797       break;
10798
10799     case CONST_DOUBLE:
10800       if (GET_MODE (x) == VOIDmode)
10801         {
10802           /* We can use %d if the number is <32 bits and positive.  */
10803           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
10804             fprintf (file, "0x%lx%08lx",
10805                      (unsigned long) CONST_DOUBLE_HIGH (x),
10806                      (unsigned long) CONST_DOUBLE_LOW (x));
10807           else
10808             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
10809         }
10810       else
10811         /* We can't handle floating point constants;
10812            PRINT_OPERAND must handle them.  */
10813         output_operand_lossage ("floating constant misused");
10814       break;
10815
10816     case PLUS:
10817       /* Some assemblers need integer constants to appear first.  */
10818       if (CONST_INT_P (XEXP (x, 0)))
10819         {
10820           output_pic_addr_const (file, XEXP (x, 0), code);
10821           putc ('+', file);
10822           output_pic_addr_const (file, XEXP (x, 1), code);
10823         }
10824       else
10825         {
10826           gcc_assert (CONST_INT_P (XEXP (x, 1)));
10827           output_pic_addr_const (file, XEXP (x, 1), code);
10828           putc ('+', file);
10829           output_pic_addr_const (file, XEXP (x, 0), code);
10830         }
10831       break;
10832
10833     case MINUS:
10834       if (!TARGET_MACHO)
10835         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
10836       output_pic_addr_const (file, XEXP (x, 0), code);
10837       putc ('-', file);
10838       output_pic_addr_const (file, XEXP (x, 1), code);
10839       if (!TARGET_MACHO)
10840         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
10841       break;
10842
10843      case UNSPEC:
10844        gcc_assert (XVECLEN (x, 0) == 1);
10845        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
10846        switch (XINT (x, 1))
10847         {
10848         case UNSPEC_GOT:
10849           fputs ("@GOT", file);
10850           break;
10851         case UNSPEC_GOTOFF:
10852           fputs ("@GOTOFF", file);
10853           break;
10854         case UNSPEC_PLTOFF:
10855           fputs ("@PLTOFF", file);
10856           break;
10857         case UNSPEC_GOTPCREL:
10858           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
10859                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
10860           break;
10861         case UNSPEC_GOTTPOFF:
10862           /* FIXME: This might be @TPOFF in Sun ld too.  */
10863           fputs ("@gottpoff", file);
10864           break;
10865         case UNSPEC_TPOFF:
10866           fputs ("@tpoff", file);
10867           break;
10868         case UNSPEC_NTPOFF:
10869           if (TARGET_64BIT)
10870             fputs ("@tpoff", file);
10871           else
10872             fputs ("@ntpoff", file);
10873           break;
10874         case UNSPEC_DTPOFF:
10875           fputs ("@dtpoff", file);
10876           break;
10877         case UNSPEC_GOTNTPOFF:
10878           if (TARGET_64BIT)
10879             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
10880                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
10881           else
10882             fputs ("@gotntpoff", file);
10883           break;
10884         case UNSPEC_INDNTPOFF:
10885           fputs ("@indntpoff", file);
10886           break;
10887 #if TARGET_MACHO
10888         case UNSPEC_MACHOPIC_OFFSET:
10889           putc ('-', file);
10890           machopic_output_function_base_name (file);
10891           break;
10892 #endif
10893         default:
10894           output_operand_lossage ("invalid UNSPEC as operand");
10895           break;
10896         }
10897        break;
10898
10899     default:
10900       output_operand_lossage ("invalid expression as operand");
10901     }
10902 }
10903
10904 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
10905    We need to emit DTP-relative relocations.  */
10906
10907 static void ATTRIBUTE_UNUSED
10908 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
10909 {
10910   fputs (ASM_LONG, file);
10911   output_addr_const (file, x);
10912   fputs ("@dtpoff", file);
10913   switch (size)
10914     {
10915     case 4:
10916       break;
10917     case 8:
10918       fputs (", 0", file);
10919       break;
10920     default:
10921       gcc_unreachable ();
10922    }
10923 }
10924
10925 /* Return true if X is a representation of the PIC register.  This copes
10926    with calls from ix86_find_base_term, where the register might have
10927    been replaced by a cselib value.  */
10928
10929 static bool
10930 ix86_pic_register_p (rtx x)
10931 {
10932   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
10933     return (pic_offset_table_rtx
10934             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
10935   else
10936     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
10937 }
10938
10939 /* In the name of slightly smaller debug output, and to cater to
10940    general assembler lossage, recognize PIC+GOTOFF and turn it back
10941    into a direct symbol reference.
10942
10943    On Darwin, this is necessary to avoid a crash, because Darwin
10944    has a different PIC label for each routine but the DWARF debugging
10945    information is not associated with any particular routine, so it's
10946    necessary to remove references to the PIC label from RTL stored by
10947    the DWARF output code.  */
10948
10949 static rtx
10950 ix86_delegitimize_address (rtx x)
10951 {
10952   rtx orig_x = delegitimize_mem_from_attrs (x);
10953   /* addend is NULL or some rtx if x is something+GOTOFF where
10954      something doesn't include the PIC register.  */
10955   rtx addend = NULL_RTX;
10956   /* reg_addend is NULL or a multiple of some register.  */
10957   rtx reg_addend = NULL_RTX;
10958   /* const_addend is NULL or a const_int.  */
10959   rtx const_addend = NULL_RTX;
10960   /* This is the result, or NULL.  */
10961   rtx result = NULL_RTX;
10962
10963   x = orig_x;
10964
10965   if (MEM_P (x))
10966     x = XEXP (x, 0);
10967
10968   if (TARGET_64BIT)
10969     {
10970       if (GET_CODE (x) != CONST
10971           || GET_CODE (XEXP (x, 0)) != UNSPEC
10972           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
10973           || !MEM_P (orig_x))
10974         return orig_x;
10975       return XVECEXP (XEXP (x, 0), 0, 0);
10976     }
10977
10978   if (GET_CODE (x) != PLUS
10979       || GET_CODE (XEXP (x, 1)) != CONST)
10980     return orig_x;
10981
10982   if (ix86_pic_register_p (XEXP (x, 0)))
10983     /* %ebx + GOT/GOTOFF */
10984     ;
10985   else if (GET_CODE (XEXP (x, 0)) == PLUS)
10986     {
10987       /* %ebx + %reg * scale + GOT/GOTOFF */
10988       reg_addend = XEXP (x, 0);
10989       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
10990         reg_addend = XEXP (reg_addend, 1);
10991       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
10992         reg_addend = XEXP (reg_addend, 0);
10993       else
10994         {
10995           reg_addend = NULL_RTX;
10996           addend = XEXP (x, 0);
10997         }
10998     }
10999   else
11000     addend = XEXP (x, 0);
11001
11002   x = XEXP (XEXP (x, 1), 0);
11003   if (GET_CODE (x) == PLUS
11004       && CONST_INT_P (XEXP (x, 1)))
11005     {
11006       const_addend = XEXP (x, 1);
11007       x = XEXP (x, 0);
11008     }
11009
11010   if (GET_CODE (x) == UNSPEC
11011       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
11012           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
11013     result = XVECEXP (x, 0, 0);
11014
11015   if (TARGET_MACHO && darwin_local_data_pic (x)
11016       && !MEM_P (orig_x))
11017     result = XVECEXP (x, 0, 0);
11018
11019   if (! result)
11020     return orig_x;
11021
11022   if (const_addend)
11023     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
11024   if (reg_addend)
11025     result = gen_rtx_PLUS (Pmode, reg_addend, result);
11026   if (addend)
11027     {
11028       /* If the rest of original X doesn't involve the PIC register, add
11029          addend and subtract pic_offset_table_rtx.  This can happen e.g.
11030          for code like:
11031          leal (%ebx, %ecx, 4), %ecx
11032          ...
11033          movl foo@GOTOFF(%ecx), %edx
11034          in which case we return (%ecx - %ebx) + foo.  */
11035       if (pic_offset_table_rtx)
11036         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
11037                                                      pic_offset_table_rtx),
11038                                result);
11039       else
11040         return orig_x;
11041     }
11042   return result;
11043 }
11044
11045 /* If X is a machine specific address (i.e. a symbol or label being
11046    referenced as a displacement from the GOT implemented using an
11047    UNSPEC), then return the base term.  Otherwise return X.  */
11048
11049 rtx
11050 ix86_find_base_term (rtx x)
11051 {
11052   rtx term;
11053
11054   if (TARGET_64BIT)
11055     {
11056       if (GET_CODE (x) != CONST)
11057         return x;
11058       term = XEXP (x, 0);
11059       if (GET_CODE (term) == PLUS
11060           && (CONST_INT_P (XEXP (term, 1))
11061               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
11062         term = XEXP (term, 0);
11063       if (GET_CODE (term) != UNSPEC
11064           || XINT (term, 1) != UNSPEC_GOTPCREL)
11065         return x;
11066
11067       return XVECEXP (term, 0, 0);
11068     }
11069
11070   return ix86_delegitimize_address (x);
11071 }
11072 \f
11073 static void
11074 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
11075                     int fp, FILE *file)
11076 {
11077   const char *suffix;
11078
11079   if (mode == CCFPmode || mode == CCFPUmode)
11080     {
11081       code = ix86_fp_compare_code_to_integer (code);
11082       mode = CCmode;
11083     }
11084   if (reverse)
11085     code = reverse_condition (code);
11086
11087   switch (code)
11088     {
11089     case EQ:
11090       switch (mode)
11091         {
11092         case CCAmode:
11093           suffix = "a";
11094           break;
11095
11096         case CCCmode:
11097           suffix = "c";
11098           break;
11099
11100         case CCOmode:
11101           suffix = "o";
11102           break;
11103
11104         case CCSmode:
11105           suffix = "s";
11106           break;
11107
11108         default:
11109           suffix = "e";
11110         }
11111       break;
11112     case NE:
11113       switch (mode)
11114         {
11115         case CCAmode:
11116           suffix = "na";
11117           break;
11118
11119         case CCCmode:
11120           suffix = "nc";
11121           break;
11122
11123         case CCOmode:
11124           suffix = "no";
11125           break;
11126
11127         case CCSmode:
11128           suffix = "ns";
11129           break;
11130
11131         default:
11132           suffix = "ne";
11133         }
11134       break;
11135     case GT:
11136       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
11137       suffix = "g";
11138       break;
11139     case GTU:
11140       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
11141          Those same assemblers have the same but opposite lossage on cmov.  */
11142       if (mode == CCmode)
11143         suffix = fp ? "nbe" : "a";
11144       else if (mode == CCCmode)
11145         suffix = "b";
11146       else
11147         gcc_unreachable ();
11148       break;
11149     case LT:
11150       switch (mode)
11151         {
11152         case CCNOmode:
11153         case CCGOCmode:
11154           suffix = "s";
11155           break;
11156
11157         case CCmode:
11158         case CCGCmode:
11159           suffix = "l";
11160           break;
11161
11162         default:
11163           gcc_unreachable ();
11164         }
11165       break;
11166     case LTU:
11167       gcc_assert (mode == CCmode || mode == CCCmode);
11168       suffix = "b";
11169       break;
11170     case GE:
11171       switch (mode)
11172         {
11173         case CCNOmode:
11174         case CCGOCmode:
11175           suffix = "ns";
11176           break;
11177
11178         case CCmode:
11179         case CCGCmode:
11180           suffix = "ge";
11181           break;
11182
11183         default:
11184           gcc_unreachable ();
11185         }
11186       break;
11187     case GEU:
11188       /* ??? As above.  */
11189       gcc_assert (mode == CCmode || mode == CCCmode);
11190       suffix = fp ? "nb" : "ae";
11191       break;
11192     case LE:
11193       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
11194       suffix = "le";
11195       break;
11196     case LEU:
11197       /* ??? As above.  */
11198       if (mode == CCmode)
11199         suffix = "be";
11200       else if (mode == CCCmode)
11201         suffix = fp ? "nb" : "ae";
11202       else
11203         gcc_unreachable ();
11204       break;
11205     case UNORDERED:
11206       suffix = fp ? "u" : "p";
11207       break;
11208     case ORDERED:
11209       suffix = fp ? "nu" : "np";
11210       break;
11211     default:
11212       gcc_unreachable ();
11213     }
11214   fputs (suffix, file);
11215 }
11216
11217 /* Print the name of register X to FILE based on its machine mode and number.
11218    If CODE is 'w', pretend the mode is HImode.
11219    If CODE is 'b', pretend the mode is QImode.
11220    If CODE is 'k', pretend the mode is SImode.
11221    If CODE is 'q', pretend the mode is DImode.
11222    If CODE is 'x', pretend the mode is V4SFmode.
11223    If CODE is 't', pretend the mode is V8SFmode.
11224    If CODE is 'h', pretend the reg is the 'high' byte register.
11225    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
11226    If CODE is 'd', duplicate the operand for AVX instruction.
11227  */
11228
11229 void
11230 print_reg (rtx x, int code, FILE *file)
11231 {
11232   const char *reg;
11233   bool duplicated = code == 'd' && TARGET_AVX;
11234
11235   gcc_assert (x == pc_rtx
11236               || (REGNO (x) != ARG_POINTER_REGNUM
11237                   && REGNO (x) != FRAME_POINTER_REGNUM
11238                   && REGNO (x) != FLAGS_REG
11239                   && REGNO (x) != FPSR_REG
11240                   && REGNO (x) != FPCR_REG));
11241
11242   if (ASSEMBLER_DIALECT == ASM_ATT)
11243     putc ('%', file);
11244
11245   if (x == pc_rtx)
11246     {
11247       gcc_assert (TARGET_64BIT);
11248       fputs ("rip", file);
11249       return;
11250     }
11251
11252   if (code == 'w' || MMX_REG_P (x))
11253     code = 2;
11254   else if (code == 'b')
11255     code = 1;
11256   else if (code == 'k')
11257     code = 4;
11258   else if (code == 'q')
11259     code = 8;
11260   else if (code == 'y')
11261     code = 3;
11262   else if (code == 'h')
11263     code = 0;
11264   else if (code == 'x')
11265     code = 16;
11266   else if (code == 't')
11267     code = 32;
11268   else
11269     code = GET_MODE_SIZE (GET_MODE (x));
11270
11271   /* Irritatingly, AMD extended registers use different naming convention
11272      from the normal registers.  */
11273   if (REX_INT_REG_P (x))
11274     {
11275       gcc_assert (TARGET_64BIT);
11276       switch (code)
11277         {
11278           case 0:
11279             error ("extended registers have no high halves");
11280             break;
11281           case 1:
11282             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
11283             break;
11284           case 2:
11285             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
11286             break;
11287           case 4:
11288             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
11289             break;
11290           case 8:
11291             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
11292             break;
11293           default:
11294             error ("unsupported operand size for extended register");
11295             break;
11296         }
11297       return;
11298     }
11299
11300   reg = NULL;
11301   switch (code)
11302     {
11303     case 3:
11304       if (STACK_TOP_P (x))
11305         {
11306           reg = "st(0)";
11307           break;
11308         }
11309       /* FALLTHRU */
11310     case 8:
11311     case 4:
11312     case 12:
11313       if (! ANY_FP_REG_P (x))
11314         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
11315       /* FALLTHRU */
11316     case 16:
11317     case 2:
11318     normal:
11319       reg = hi_reg_name[REGNO (x)];
11320       break;
11321     case 1:
11322       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
11323         goto normal;
11324       reg = qi_reg_name[REGNO (x)];
11325       break;
11326     case 0:
11327       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
11328         goto normal;
11329       reg = qi_high_reg_name[REGNO (x)];
11330       break;
11331     case 32:
11332       if (SSE_REG_P (x))
11333         {
11334           gcc_assert (!duplicated);
11335           putc ('y', file);
11336           fputs (hi_reg_name[REGNO (x)] + 1, file);
11337           return;
11338         }
11339       break;
11340     default:
11341       gcc_unreachable ();
11342     }
11343
11344   fputs (reg, file);
11345   if (duplicated)
11346     {
11347       if (ASSEMBLER_DIALECT == ASM_ATT)
11348         fprintf (file, ", %%%s", reg);
11349       else
11350         fprintf (file, ", %s", reg);
11351     }
11352 }
11353
11354 /* Locate some local-dynamic symbol still in use by this function
11355    so that we can print its name in some tls_local_dynamic_base
11356    pattern.  */
11357
11358 static int
11359 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
11360 {
11361   rtx x = *px;
11362
11363   if (GET_CODE (x) == SYMBOL_REF
11364       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
11365     {
11366       cfun->machine->some_ld_name = XSTR (x, 0);
11367       return 1;
11368     }
11369
11370   return 0;
11371 }
11372
11373 static const char *
11374 get_some_local_dynamic_name (void)
11375 {
11376   rtx insn;
11377
11378   if (cfun->machine->some_ld_name)
11379     return cfun->machine->some_ld_name;
11380
11381   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
11382     if (INSN_P (insn)
11383         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
11384       return cfun->machine->some_ld_name;
11385
11386   return NULL;
11387 }
11388
11389 /* Meaning of CODE:
11390    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
11391    C -- print opcode suffix for set/cmov insn.
11392    c -- like C, but print reversed condition
11393    F,f -- likewise, but for floating-point.
11394    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
11395         otherwise nothing
11396    R -- print the prefix for register names.
11397    z -- print the opcode suffix for the size of the current operand.
11398    Z -- likewise, with special suffixes for x87 instructions.
11399    * -- print a star (in certain assembler syntax)
11400    A -- print an absolute memory reference.
11401    w -- print the operand as if it's a "word" (HImode) even if it isn't.
11402    s -- print a shift double count, followed by the assemblers argument
11403         delimiter.
11404    b -- print the QImode name of the register for the indicated operand.
11405         %b0 would print %al if operands[0] is reg 0.
11406    w --  likewise, print the HImode name of the register.
11407    k --  likewise, print the SImode name of the register.
11408    q --  likewise, print the DImode name of the register.
11409    x --  likewise, print the V4SFmode name of the register.
11410    t --  likewise, print the V8SFmode name of the register.
11411    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
11412    y -- print "st(0)" instead of "st" as a register.
11413    d -- print duplicated register operand for AVX instruction.
11414    D -- print condition for SSE cmp instruction.
11415    P -- if PIC, print an @PLT suffix.
11416    X -- don't print any sort of PIC '@' suffix for a symbol.
11417    & -- print some in-use local-dynamic symbol name.
11418    H -- print a memory address offset by 8; used for sse high-parts
11419    Y -- print condition for XOP pcom* instruction.
11420    + -- print a branch hint as 'cs' or 'ds' prefix
11421    ; -- print a semicolon (after prefixes due to bug in older gas).
11422  */
11423
11424 void
11425 print_operand (FILE *file, rtx x, int code)
11426 {
11427   if (code)
11428     {
11429       switch (code)
11430         {
11431         case '*':
11432           if (ASSEMBLER_DIALECT == ASM_ATT)
11433             putc ('*', file);
11434           return;
11435
11436         case '&':
11437           {
11438             const char *name = get_some_local_dynamic_name ();
11439             if (name == NULL)
11440               output_operand_lossage ("'%%&' used without any "
11441                                       "local dynamic TLS references");
11442             else
11443               assemble_name (file, name);
11444             return;
11445           }
11446
11447         case 'A':
11448           switch (ASSEMBLER_DIALECT)
11449             {
11450             case ASM_ATT:
11451               putc ('*', file);
11452               break;
11453
11454             case ASM_INTEL:
11455               /* Intel syntax. For absolute addresses, registers should not
11456                  be surrounded by braces.  */
11457               if (!REG_P (x))
11458                 {
11459                   putc ('[', file);
11460                   PRINT_OPERAND (file, x, 0);
11461                   putc (']', file);
11462                   return;
11463                 }
11464               break;
11465
11466             default:
11467               gcc_unreachable ();
11468             }
11469
11470           PRINT_OPERAND (file, x, 0);
11471           return;
11472
11473
11474         case 'L':
11475           if (ASSEMBLER_DIALECT == ASM_ATT)
11476             putc ('l', file);
11477           return;
11478
11479         case 'W':
11480           if (ASSEMBLER_DIALECT == ASM_ATT)
11481             putc ('w', file);
11482           return;
11483
11484         case 'B':
11485           if (ASSEMBLER_DIALECT == ASM_ATT)
11486             putc ('b', file);
11487           return;
11488
11489         case 'Q':
11490           if (ASSEMBLER_DIALECT == ASM_ATT)
11491             putc ('l', file);
11492           return;
11493
11494         case 'S':
11495           if (ASSEMBLER_DIALECT == ASM_ATT)
11496             putc ('s', file);
11497           return;
11498
11499         case 'T':
11500           if (ASSEMBLER_DIALECT == ASM_ATT)
11501             putc ('t', file);
11502           return;
11503
11504         case 'z':
11505           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
11506             {
11507               /* Opcodes don't get size suffixes if using Intel opcodes.  */
11508               if (ASSEMBLER_DIALECT == ASM_INTEL)
11509                 return;
11510
11511               switch (GET_MODE_SIZE (GET_MODE (x)))
11512                 {
11513                 case 1:
11514                   putc ('b', file);
11515                   return;
11516
11517                 case 2:
11518                   putc ('w', file);
11519                   return;
11520
11521                 case 4:
11522                   putc ('l', file);
11523                   return;
11524
11525                 case 8:
11526                   putc ('q', file);
11527                   return;
11528
11529                 default:
11530                   output_operand_lossage
11531                     ("invalid operand size for operand code '%c'", code);
11532                   return;
11533                 }
11534             }
11535
11536           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
11537             warning
11538               (0, "non-integer operand used with operand code '%c'", code);
11539           /* FALLTHRU */
11540
11541         case 'Z':
11542           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
11543           if (ASSEMBLER_DIALECT == ASM_INTEL)
11544             return;
11545
11546           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
11547             {
11548               switch (GET_MODE_SIZE (GET_MODE (x)))
11549                 {
11550                 case 2:
11551 #ifdef HAVE_AS_IX86_FILDS
11552                   putc ('s', file);
11553 #endif
11554                   return;
11555
11556                 case 4:
11557                   putc ('l', file);
11558                   return;
11559
11560                 case 8:
11561 #ifdef HAVE_AS_IX86_FILDQ
11562                   putc ('q', file);
11563 #else
11564                   fputs ("ll", file);
11565 #endif
11566                   return;
11567
11568                 default:
11569                   break;
11570                 }
11571             }
11572           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
11573             {
11574               /* 387 opcodes don't get size suffixes
11575                  if the operands are registers.  */
11576               if (STACK_REG_P (x))
11577                 return;
11578
11579               switch (GET_MODE_SIZE (GET_MODE (x)))
11580                 {
11581                 case 4:
11582                   putc ('s', file);
11583                   return;
11584
11585                 case 8:
11586                   putc ('l', file);
11587                   return;
11588
11589                 case 12:
11590                 case 16:
11591                   putc ('t', file);
11592                   return;
11593
11594                 default:
11595                   break;
11596                 }
11597             }
11598           else
11599             {
11600               output_operand_lossage
11601                 ("invalid operand type used with operand code '%c'", code);
11602               return;
11603             }
11604
11605           output_operand_lossage
11606             ("invalid operand size for operand code '%c'", code);
11607           return;
11608             
11609         case 'd':
11610         case 'b':
11611         case 'w':
11612         case 'k':
11613         case 'q':
11614         case 'h':
11615         case 't':
11616         case 'y':
11617         case 'x':
11618         case 'X':
11619         case 'P':
11620           break;
11621
11622         case 's':
11623           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
11624             {
11625               PRINT_OPERAND (file, x, 0);
11626               fputs (", ", file);
11627             }
11628           return;
11629
11630         case 'D':
11631           /* Little bit of braindamage here.  The SSE compare instructions
11632              does use completely different names for the comparisons that the
11633              fp conditional moves.  */
11634           if (TARGET_AVX)
11635             {
11636               switch (GET_CODE (x))
11637                 {
11638                 case EQ:
11639                   fputs ("eq", file);
11640                   break;
11641                 case UNEQ:
11642                   fputs ("eq_us", file);
11643                   break;
11644                 case LT:
11645                   fputs ("lt", file);
11646                   break;
11647                 case UNLT:
11648                   fputs ("nge", file);
11649                   break;
11650                 case LE:
11651                   fputs ("le", file);
11652                   break;
11653                 case UNLE:
11654                   fputs ("ngt", file);
11655                   break;
11656                 case UNORDERED:
11657                   fputs ("unord", file);
11658                   break;
11659                 case NE:
11660                   fputs ("neq", file);
11661                   break;
11662                 case LTGT:
11663                   fputs ("neq_oq", file);
11664                   break;
11665                 case GE:
11666                   fputs ("ge", file);
11667                   break;
11668                 case UNGE:
11669                   fputs ("nlt", file);
11670                   break;
11671                 case GT:
11672                   fputs ("gt", file);
11673                   break;
11674                 case UNGT:
11675                   fputs ("nle", file);
11676                   break;
11677                 case ORDERED:
11678                   fputs ("ord", file);
11679                   break;
11680                 default:
11681                   output_operand_lossage ("operand is not a condition code, "
11682                                           "invalid operand code 'D'");
11683                   return;
11684                 }
11685             }
11686           else
11687             {
11688               switch (GET_CODE (x))
11689                 {
11690                 case EQ:
11691                 case UNEQ:
11692                   fputs ("eq", file);
11693                   break;
11694                 case LT:
11695                 case UNLT:
11696                   fputs ("lt", file);
11697                   break;
11698                 case LE:
11699                 case UNLE:
11700                   fputs ("le", file);
11701                   break;
11702                 case UNORDERED:
11703                   fputs ("unord", file);
11704                   break;
11705                 case NE:
11706                 case LTGT:
11707                   fputs ("neq", file);
11708                   break;
11709                 case UNGE:
11710                 case GE:
11711                   fputs ("nlt", file);
11712                   break;
11713                 case UNGT:
11714                 case GT:
11715                   fputs ("nle", file);
11716                   break;
11717                 case ORDERED:
11718                   fputs ("ord", file);
11719                   break;
11720                 default:
11721                   output_operand_lossage ("operand is not a condition code, "
11722                                           "invalid operand code 'D'");
11723                   return;
11724                 }
11725             }
11726           return;
11727         case 'O':
11728 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
11729           if (ASSEMBLER_DIALECT == ASM_ATT)
11730             {
11731               switch (GET_MODE (x))
11732                 {
11733                 case HImode: putc ('w', file); break;
11734                 case SImode:
11735                 case SFmode: putc ('l', file); break;
11736                 case DImode:
11737                 case DFmode: putc ('q', file); break;
11738                 default: gcc_unreachable ();
11739                 }
11740               putc ('.', file);
11741             }
11742 #endif
11743           return;
11744         case 'C':
11745           if (!COMPARISON_P (x))
11746             {
11747               output_operand_lossage ("operand is neither a constant nor a "
11748                                       "condition code, invalid operand code "
11749                                       "'C'");
11750               return;
11751             }
11752           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
11753           return;
11754         case 'F':
11755           if (!COMPARISON_P (x))
11756             {
11757               output_operand_lossage ("operand is neither a constant nor a "
11758                                       "condition code, invalid operand code "
11759                                       "'F'");
11760               return;
11761             }
11762 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
11763           if (ASSEMBLER_DIALECT == ASM_ATT)
11764             putc ('.', file);
11765 #endif
11766           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
11767           return;
11768
11769           /* Like above, but reverse condition */
11770         case 'c':
11771           /* Check to see if argument to %c is really a constant
11772              and not a condition code which needs to be reversed.  */
11773           if (!COMPARISON_P (x))
11774             {
11775               output_operand_lossage ("operand is neither a constant nor a "
11776                                       "condition code, invalid operand "
11777                                       "code 'c'");
11778               return;
11779             }
11780           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
11781           return;
11782         case 'f':
11783           if (!COMPARISON_P (x))
11784             {
11785               output_operand_lossage ("operand is neither a constant nor a "
11786                                       "condition code, invalid operand "
11787                                       "code 'f'");
11788               return;
11789             }
11790 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
11791           if (ASSEMBLER_DIALECT == ASM_ATT)
11792             putc ('.', file);
11793 #endif
11794           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
11795           return;
11796
11797         case 'H':
11798           /* It doesn't actually matter what mode we use here, as we're
11799              only going to use this for printing.  */
11800           x = adjust_address_nv (x, DImode, 8);
11801           break;
11802
11803         case '+':
11804           {
11805             rtx x;
11806
11807             if (!optimize
11808                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
11809               return;
11810
11811             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
11812             if (x)
11813               {
11814                 int pred_val = INTVAL (XEXP (x, 0));
11815
11816                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
11817                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
11818                   {
11819                     int taken = pred_val > REG_BR_PROB_BASE / 2;
11820                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
11821
11822                     /* Emit hints only in the case default branch prediction
11823                        heuristics would fail.  */
11824                     if (taken != cputaken)
11825                       {
11826                         /* We use 3e (DS) prefix for taken branches and
11827                            2e (CS) prefix for not taken branches.  */
11828                         if (taken)
11829                           fputs ("ds ; ", file);
11830                         else
11831                           fputs ("cs ; ", file);
11832                       }
11833                   }
11834               }
11835             return;
11836           }
11837
11838         case 'Y':
11839           switch (GET_CODE (x))
11840             {
11841             case NE:
11842               fputs ("neq", file);
11843               break;
11844             case EQ:
11845               fputs ("eq", file);
11846               break;
11847             case GE:
11848             case GEU:
11849               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
11850               break;
11851             case GT:
11852             case GTU:
11853               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
11854               break;
11855             case LE:
11856             case LEU:
11857               fputs ("le", file);
11858               break;
11859             case LT:
11860             case LTU:
11861               fputs ("lt", file);
11862               break;
11863             case UNORDERED:
11864               fputs ("unord", file);
11865               break;
11866             case ORDERED:
11867               fputs ("ord", file);
11868               break;
11869             case UNEQ:
11870               fputs ("ueq", file);
11871               break;
11872             case UNGE:
11873               fputs ("nlt", file);
11874               break;
11875             case UNGT:
11876               fputs ("nle", file);
11877               break;
11878             case UNLE:
11879               fputs ("ule", file);
11880               break;
11881             case UNLT:
11882               fputs ("ult", file);
11883               break;
11884             case LTGT:
11885               fputs ("une", file);
11886               break;
11887             default:
11888               output_operand_lossage ("operand is not a condition code, "
11889                                       "invalid operand code 'Y'");
11890               return;
11891             }
11892           return;
11893
11894         case ';':
11895 #if TARGET_MACHO
11896           fputs (" ; ", file);
11897 #else
11898           putc (' ', file);
11899 #endif
11900           return;
11901
11902         default:
11903             output_operand_lossage ("invalid operand code '%c'", code);
11904         }
11905     }
11906
11907   if (REG_P (x))
11908     print_reg (x, code, file);
11909
11910   else if (MEM_P (x))
11911     {
11912       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
11913       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
11914           && GET_MODE (x) != BLKmode)
11915         {
11916           const char * size;
11917           switch (GET_MODE_SIZE (GET_MODE (x)))
11918             {
11919             case 1: size = "BYTE"; break;
11920             case 2: size = "WORD"; break;
11921             case 4: size = "DWORD"; break;
11922             case 8: size = "QWORD"; break;
11923             case 12: size = "TBYTE"; break;
11924             case 16:
11925               if (GET_MODE (x) == XFmode)
11926                 size = "TBYTE";
11927               else
11928                 size = "XMMWORD";
11929               break;
11930             case 32: size = "YMMWORD"; break;
11931             default:
11932               gcc_unreachable ();
11933             }
11934
11935           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
11936           if (code == 'b')
11937             size = "BYTE";
11938           else if (code == 'w')
11939             size = "WORD";
11940           else if (code == 'k')
11941             size = "DWORD";
11942
11943           fputs (size, file);
11944           fputs (" PTR ", file);
11945         }
11946
11947       x = XEXP (x, 0);
11948       /* Avoid (%rip) for call operands.  */
11949       if (CONSTANT_ADDRESS_P (x) && code == 'P'
11950           && !CONST_INT_P (x))
11951         output_addr_const (file, x);
11952       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
11953         output_operand_lossage ("invalid constraints for operand");
11954       else
11955         output_address (x);
11956     }
11957
11958   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
11959     {
11960       REAL_VALUE_TYPE r;
11961       long l;
11962
11963       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
11964       REAL_VALUE_TO_TARGET_SINGLE (r, l);
11965
11966       if (ASSEMBLER_DIALECT == ASM_ATT)
11967         putc ('$', file);
11968       fprintf (file, "0x%08lx", (long unsigned int) l);
11969     }
11970
11971   /* These float cases don't actually occur as immediate operands.  */
11972   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
11973     {
11974       char dstr[30];
11975
11976       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
11977       fputs (dstr, file);
11978     }
11979
11980   else if (GET_CODE (x) == CONST_DOUBLE
11981            && GET_MODE (x) == XFmode)
11982     {
11983       char dstr[30];
11984
11985       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
11986       fputs (dstr, file);
11987     }
11988
11989   else
11990     {
11991       /* We have patterns that allow zero sets of memory, for instance.
11992          In 64-bit mode, we should probably support all 8-byte vectors,
11993          since we can in fact encode that into an immediate.  */
11994       if (GET_CODE (x) == CONST_VECTOR)
11995         {
11996           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
11997           x = const0_rtx;
11998         }
11999
12000       if (code != 'P')
12001         {
12002           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
12003             {
12004               if (ASSEMBLER_DIALECT == ASM_ATT)
12005                 putc ('$', file);
12006             }
12007           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
12008                    || GET_CODE (x) == LABEL_REF)
12009             {
12010               if (ASSEMBLER_DIALECT == ASM_ATT)
12011                 putc ('$', file);
12012               else
12013                 fputs ("OFFSET FLAT:", file);
12014             }
12015         }
12016       if (CONST_INT_P (x))
12017         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12018       else if (flag_pic)
12019         output_pic_addr_const (file, x, code);
12020       else
12021         output_addr_const (file, x);
12022     }
12023 }
12024 \f
12025 /* Print a memory operand whose address is ADDR.  */
12026
12027 void
12028 print_operand_address (FILE *file, rtx addr)
12029 {
12030   struct ix86_address parts;
12031   rtx base, index, disp;
12032   int scale;
12033   int ok = ix86_decompose_address (addr, &parts);
12034
12035   gcc_assert (ok);
12036
12037   base = parts.base;
12038   index = parts.index;
12039   disp = parts.disp;
12040   scale = parts.scale;
12041
12042   switch (parts.seg)
12043     {
12044     case SEG_DEFAULT:
12045       break;
12046     case SEG_FS:
12047     case SEG_GS:
12048       if (ASSEMBLER_DIALECT == ASM_ATT)
12049         putc ('%', file);
12050       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
12051       break;
12052     default:
12053       gcc_unreachable ();
12054     }
12055
12056   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
12057   if (TARGET_64BIT && !base && !index)
12058     {
12059       rtx symbol = disp;
12060
12061       if (GET_CODE (disp) == CONST
12062           && GET_CODE (XEXP (disp, 0)) == PLUS
12063           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
12064         symbol = XEXP (XEXP (disp, 0), 0);
12065
12066       if (GET_CODE (symbol) == LABEL_REF
12067           || (GET_CODE (symbol) == SYMBOL_REF
12068               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
12069         base = pc_rtx;
12070     }
12071   if (!base && !index)
12072     {
12073       /* Displacement only requires special attention.  */
12074
12075       if (CONST_INT_P (disp))
12076         {
12077           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
12078             fputs ("ds:", file);
12079           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
12080         }
12081       else if (flag_pic)
12082         output_pic_addr_const (file, disp, 0);
12083       else
12084         output_addr_const (file, disp);
12085     }
12086   else
12087     {
12088       if (ASSEMBLER_DIALECT == ASM_ATT)
12089         {
12090           if (disp)
12091             {
12092               if (flag_pic)
12093                 output_pic_addr_const (file, disp, 0);
12094               else if (GET_CODE (disp) == LABEL_REF)
12095                 output_asm_label (disp);
12096               else
12097                 output_addr_const (file, disp);
12098             }
12099
12100           putc ('(', file);
12101           if (base)
12102             print_reg (base, 0, file);
12103           if (index)
12104             {
12105               putc (',', file);
12106               print_reg (index, 0, file);
12107               if (scale != 1)
12108                 fprintf (file, ",%d", scale);
12109             }
12110           putc (')', file);
12111         }
12112       else
12113         {
12114           rtx offset = NULL_RTX;
12115
12116           if (disp)
12117             {
12118               /* Pull out the offset of a symbol; print any symbol itself.  */
12119               if (GET_CODE (disp) == CONST
12120                   && GET_CODE (XEXP (disp, 0)) == PLUS
12121                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
12122                 {
12123                   offset = XEXP (XEXP (disp, 0), 1);
12124                   disp = gen_rtx_CONST (VOIDmode,
12125                                         XEXP (XEXP (disp, 0), 0));
12126                 }
12127
12128               if (flag_pic)
12129                 output_pic_addr_const (file, disp, 0);
12130               else if (GET_CODE (disp) == LABEL_REF)
12131                 output_asm_label (disp);
12132               else if (CONST_INT_P (disp))
12133                 offset = disp;
12134               else
12135                 output_addr_const (file, disp);
12136             }
12137
12138           putc ('[', file);
12139           if (base)
12140             {
12141               print_reg (base, 0, file);
12142               if (offset)
12143                 {
12144                   if (INTVAL (offset) >= 0)
12145                     putc ('+', file);
12146                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
12147                 }
12148             }
12149           else if (offset)
12150             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
12151           else
12152             putc ('0', file);
12153
12154           if (index)
12155             {
12156               putc ('+', file);
12157               print_reg (index, 0, file);
12158               if (scale != 1)
12159                 fprintf (file, "*%d", scale);
12160             }
12161           putc (']', file);
12162         }
12163     }
12164 }
12165
12166 bool
12167 output_addr_const_extra (FILE *file, rtx x)
12168 {
12169   rtx op;
12170
12171   if (GET_CODE (x) != UNSPEC)
12172     return false;
12173
12174   op = XVECEXP (x, 0, 0);
12175   switch (XINT (x, 1))
12176     {
12177     case UNSPEC_GOTTPOFF:
12178       output_addr_const (file, op);
12179       /* FIXME: This might be @TPOFF in Sun ld.  */
12180       fputs ("@gottpoff", file);
12181       break;
12182     case UNSPEC_TPOFF:
12183       output_addr_const (file, op);
12184       fputs ("@tpoff", file);
12185       break;
12186     case UNSPEC_NTPOFF:
12187       output_addr_const (file, op);
12188       if (TARGET_64BIT)
12189         fputs ("@tpoff", file);
12190       else
12191         fputs ("@ntpoff", file);
12192       break;
12193     case UNSPEC_DTPOFF:
12194       output_addr_const (file, op);
12195       fputs ("@dtpoff", file);
12196       break;
12197     case UNSPEC_GOTNTPOFF:
12198       output_addr_const (file, op);
12199       if (TARGET_64BIT)
12200         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
12201                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
12202       else
12203         fputs ("@gotntpoff", file);
12204       break;
12205     case UNSPEC_INDNTPOFF:
12206       output_addr_const (file, op);
12207       fputs ("@indntpoff", file);
12208       break;
12209 #if TARGET_MACHO
12210     case UNSPEC_MACHOPIC_OFFSET:
12211       output_addr_const (file, op);
12212       putc ('-', file);
12213       machopic_output_function_base_name (file);
12214       break;
12215 #endif
12216
12217     default:
12218       return false;
12219     }
12220
12221   return true;
12222 }
12223 \f
12224 /* Split one or more DImode RTL references into pairs of SImode
12225    references.  The RTL can be REG, offsettable MEM, integer constant, or
12226    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
12227    split and "num" is its length.  lo_half and hi_half are output arrays
12228    that parallel "operands".  */
12229
12230 void
12231 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
12232 {
12233   while (num--)
12234     {
12235       rtx op = operands[num];
12236
12237       /* simplify_subreg refuse to split volatile memory addresses,
12238          but we still have to handle it.  */
12239       if (MEM_P (op))
12240         {
12241           lo_half[num] = adjust_address (op, SImode, 0);
12242           hi_half[num] = adjust_address (op, SImode, 4);
12243         }
12244       else
12245         {
12246           lo_half[num] = simplify_gen_subreg (SImode, op,
12247                                               GET_MODE (op) == VOIDmode
12248                                               ? DImode : GET_MODE (op), 0);
12249           hi_half[num] = simplify_gen_subreg (SImode, op,
12250                                               GET_MODE (op) == VOIDmode
12251                                               ? DImode : GET_MODE (op), 4);
12252         }
12253     }
12254 }
12255 /* Split one or more TImode RTL references into pairs of DImode
12256    references.  The RTL can be REG, offsettable MEM, integer constant, or
12257    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
12258    split and "num" is its length.  lo_half and hi_half are output arrays
12259    that parallel "operands".  */
12260
12261 void
12262 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
12263 {
12264   while (num--)
12265     {
12266       rtx op = operands[num];
12267
12268       /* simplify_subreg refuse to split volatile memory addresses, but we
12269          still have to handle it.  */
12270       if (MEM_P (op))
12271         {
12272           lo_half[num] = adjust_address (op, DImode, 0);
12273           hi_half[num] = adjust_address (op, DImode, 8);
12274         }
12275       else
12276         {
12277           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
12278           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
12279         }
12280     }
12281 }
12282 \f
12283 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
12284    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
12285    is the expression of the binary operation.  The output may either be
12286    emitted here, or returned to the caller, like all output_* functions.
12287
12288    There is no guarantee that the operands are the same mode, as they
12289    might be within FLOAT or FLOAT_EXTEND expressions.  */
12290
12291 #ifndef SYSV386_COMPAT
12292 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
12293    wants to fix the assemblers because that causes incompatibility
12294    with gcc.  No-one wants to fix gcc because that causes
12295    incompatibility with assemblers...  You can use the option of
12296    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
12297 #define SYSV386_COMPAT 1
12298 #endif
12299
12300 const char *
12301 output_387_binary_op (rtx insn, rtx *operands)
12302 {
12303   static char buf[40];
12304   const char *p;
12305   const char *ssep;
12306   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
12307
12308 #ifdef ENABLE_CHECKING
12309   /* Even if we do not want to check the inputs, this documents input
12310      constraints.  Which helps in understanding the following code.  */
12311   if (STACK_REG_P (operands[0])
12312       && ((REG_P (operands[1])
12313            && REGNO (operands[0]) == REGNO (operands[1])
12314            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
12315           || (REG_P (operands[2])
12316               && REGNO (operands[0]) == REGNO (operands[2])
12317               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
12318       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
12319     ; /* ok */
12320   else
12321     gcc_assert (is_sse);
12322 #endif
12323
12324   switch (GET_CODE (operands[3]))
12325     {
12326     case PLUS:
12327       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
12328           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
12329         p = "fiadd";
12330       else
12331         p = "fadd";
12332       ssep = "vadd";
12333       break;
12334
12335     case MINUS:
12336       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
12337           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
12338         p = "fisub";
12339       else
12340         p = "fsub";
12341       ssep = "vsub";
12342       break;
12343
12344     case MULT:
12345       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
12346           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
12347         p = "fimul";
12348       else
12349         p = "fmul";
12350       ssep = "vmul";
12351       break;
12352
12353     case DIV:
12354       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
12355           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
12356         p = "fidiv";
12357       else
12358         p = "fdiv";
12359       ssep = "vdiv";
12360       break;
12361
12362     default:
12363       gcc_unreachable ();
12364     }
12365
12366   if (is_sse)
12367    {
12368      if (TARGET_AVX)
12369        {
12370          strcpy (buf, ssep);
12371          if (GET_MODE (operands[0]) == SFmode)
12372            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
12373          else
12374            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
12375        }
12376      else
12377        {
12378          strcpy (buf, ssep + 1);
12379          if (GET_MODE (operands[0]) == SFmode)
12380            strcat (buf, "ss\t{%2, %0|%0, %2}");
12381          else
12382            strcat (buf, "sd\t{%2, %0|%0, %2}");
12383        }
12384       return buf;
12385    }
12386   strcpy (buf, p);
12387
12388   switch (GET_CODE (operands[3]))
12389     {
12390     case MULT:
12391     case PLUS:
12392       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
12393         {
12394           rtx temp = operands[2];
12395           operands[2] = operands[1];
12396           operands[1] = temp;
12397         }
12398
12399       /* know operands[0] == operands[1].  */
12400
12401       if (MEM_P (operands[2]))
12402         {
12403           p = "%Z2\t%2";
12404           break;
12405         }
12406
12407       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
12408         {
12409           if (STACK_TOP_P (operands[0]))
12410             /* How is it that we are storing to a dead operand[2]?
12411                Well, presumably operands[1] is dead too.  We can't
12412                store the result to st(0) as st(0) gets popped on this
12413                instruction.  Instead store to operands[2] (which I
12414                think has to be st(1)).  st(1) will be popped later.
12415                gcc <= 2.8.1 didn't have this check and generated
12416                assembly code that the Unixware assembler rejected.  */
12417             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
12418           else
12419             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
12420           break;
12421         }
12422
12423       if (STACK_TOP_P (operands[0]))
12424         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
12425       else
12426         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
12427       break;
12428
12429     case MINUS:
12430     case DIV:
12431       if (MEM_P (operands[1]))
12432         {
12433           p = "r%Z1\t%1";
12434           break;
12435         }
12436
12437       if (MEM_P (operands[2]))
12438         {
12439           p = "%Z2\t%2";
12440           break;
12441         }
12442
12443       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
12444         {
12445 #if SYSV386_COMPAT
12446           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
12447              derived assemblers, confusingly reverse the direction of
12448              the operation for fsub{r} and fdiv{r} when the
12449              destination register is not st(0).  The Intel assembler
12450              doesn't have this brain damage.  Read !SYSV386_COMPAT to
12451              figure out what the hardware really does.  */
12452           if (STACK_TOP_P (operands[0]))
12453             p = "{p\t%0, %2|rp\t%2, %0}";
12454           else
12455             p = "{rp\t%2, %0|p\t%0, %2}";
12456 #else
12457           if (STACK_TOP_P (operands[0]))
12458             /* As above for fmul/fadd, we can't store to st(0).  */
12459             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
12460           else
12461             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
12462 #endif
12463           break;
12464         }
12465
12466       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
12467         {
12468 #if SYSV386_COMPAT
12469           if (STACK_TOP_P (operands[0]))
12470             p = "{rp\t%0, %1|p\t%1, %0}";
12471           else
12472             p = "{p\t%1, %0|rp\t%0, %1}";
12473 #else
12474           if (STACK_TOP_P (operands[0]))
12475             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
12476           else
12477             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
12478 #endif
12479           break;
12480         }
12481
12482       if (STACK_TOP_P (operands[0]))
12483         {
12484           if (STACK_TOP_P (operands[1]))
12485             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
12486           else
12487             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
12488           break;
12489         }
12490       else if (STACK_TOP_P (operands[1]))
12491         {
12492 #if SYSV386_COMPAT
12493           p = "{\t%1, %0|r\t%0, %1}";
12494 #else
12495           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
12496 #endif
12497         }
12498       else
12499         {
12500 #if SYSV386_COMPAT
12501           p = "{r\t%2, %0|\t%0, %2}";
12502 #else
12503           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
12504 #endif
12505         }
12506       break;
12507
12508     default:
12509       gcc_unreachable ();
12510     }
12511
12512   strcat (buf, p);
12513   return buf;
12514 }
12515
12516 /* Return needed mode for entity in optimize_mode_switching pass.  */
12517
12518 int
12519 ix86_mode_needed (int entity, rtx insn)
12520 {
12521   enum attr_i387_cw mode;
12522
12523   /* The mode UNINITIALIZED is used to store control word after a
12524      function call or ASM pattern.  The mode ANY specify that function
12525      has no requirements on the control word and make no changes in the
12526      bits we are interested in.  */
12527
12528   if (CALL_P (insn)
12529       || (NONJUMP_INSN_P (insn)
12530           && (asm_noperands (PATTERN (insn)) >= 0
12531               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
12532     return I387_CW_UNINITIALIZED;
12533
12534   if (recog_memoized (insn) < 0)
12535     return I387_CW_ANY;
12536
12537   mode = get_attr_i387_cw (insn);
12538
12539   switch (entity)
12540     {
12541     case I387_TRUNC:
12542       if (mode == I387_CW_TRUNC)
12543         return mode;
12544       break;
12545
12546     case I387_FLOOR:
12547       if (mode == I387_CW_FLOOR)
12548         return mode;
12549       break;
12550
12551     case I387_CEIL:
12552       if (mode == I387_CW_CEIL)
12553         return mode;
12554       break;
12555
12556     case I387_MASK_PM:
12557       if (mode == I387_CW_MASK_PM)
12558         return mode;
12559       break;
12560
12561     default:
12562       gcc_unreachable ();
12563     }
12564
12565   return I387_CW_ANY;
12566 }
12567
12568 /* Output code to initialize control word copies used by trunc?f?i and
12569    rounding patterns.  CURRENT_MODE is set to current control word,
12570    while NEW_MODE is set to new control word.  */
12571
12572 void
12573 emit_i387_cw_initialization (int mode)
12574 {
12575   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
12576   rtx new_mode;
12577
12578   enum ix86_stack_slot slot;
12579
12580   rtx reg = gen_reg_rtx (HImode);
12581
12582   emit_insn (gen_x86_fnstcw_1 (stored_mode));
12583   emit_move_insn (reg, copy_rtx (stored_mode));
12584
12585   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
12586       || optimize_function_for_size_p (cfun))
12587     {
12588       switch (mode)
12589         {
12590         case I387_CW_TRUNC:
12591           /* round toward zero (truncate) */
12592           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
12593           slot = SLOT_CW_TRUNC;
12594           break;
12595
12596         case I387_CW_FLOOR:
12597           /* round down toward -oo */
12598           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
12599           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
12600           slot = SLOT_CW_FLOOR;
12601           break;
12602
12603         case I387_CW_CEIL:
12604           /* round up toward +oo */
12605           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
12606           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
12607           slot = SLOT_CW_CEIL;
12608           break;
12609
12610         case I387_CW_MASK_PM:
12611           /* mask precision exception for nearbyint() */
12612           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
12613           slot = SLOT_CW_MASK_PM;
12614           break;
12615
12616         default:
12617           gcc_unreachable ();
12618         }
12619     }
12620   else
12621     {
12622       switch (mode)
12623         {
12624         case I387_CW_TRUNC:
12625           /* round toward zero (truncate) */
12626           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
12627           slot = SLOT_CW_TRUNC;
12628           break;
12629
12630         case I387_CW_FLOOR:
12631           /* round down toward -oo */
12632           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
12633           slot = SLOT_CW_FLOOR;
12634           break;
12635
12636         case I387_CW_CEIL:
12637           /* round up toward +oo */
12638           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
12639           slot = SLOT_CW_CEIL;
12640           break;
12641
12642         case I387_CW_MASK_PM:
12643           /* mask precision exception for nearbyint() */
12644           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
12645           slot = SLOT_CW_MASK_PM;
12646           break;
12647
12648         default:
12649           gcc_unreachable ();
12650         }
12651     }
12652
12653   gcc_assert (slot < MAX_386_STACK_LOCALS);
12654
12655   new_mode = assign_386_stack_local (HImode, slot);
12656   emit_move_insn (new_mode, reg);
12657 }
12658
12659 /* Output code for INSN to convert a float to a signed int.  OPERANDS
12660    are the insn operands.  The output may be [HSD]Imode and the input
12661    operand may be [SDX]Fmode.  */
12662
12663 const char *
12664 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
12665 {
12666   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
12667   int dimode_p = GET_MODE (operands[0]) == DImode;
12668   int round_mode = get_attr_i387_cw (insn);
12669
12670   /* Jump through a hoop or two for DImode, since the hardware has no
12671      non-popping instruction.  We used to do this a different way, but
12672      that was somewhat fragile and broke with post-reload splitters.  */
12673   if ((dimode_p || fisttp) && !stack_top_dies)
12674     output_asm_insn ("fld\t%y1", operands);
12675
12676   gcc_assert (STACK_TOP_P (operands[1]));
12677   gcc_assert (MEM_P (operands[0]));
12678   gcc_assert (GET_MODE (operands[1]) != TFmode);
12679
12680   if (fisttp)
12681       output_asm_insn ("fisttp%Z0\t%0", operands);
12682   else
12683     {
12684       if (round_mode != I387_CW_ANY)
12685         output_asm_insn ("fldcw\t%3", operands);
12686       if (stack_top_dies || dimode_p)
12687         output_asm_insn ("fistp%Z0\t%0", operands);
12688       else
12689         output_asm_insn ("fist%Z0\t%0", operands);
12690       if (round_mode != I387_CW_ANY)
12691         output_asm_insn ("fldcw\t%2", operands);
12692     }
12693
12694   return "";
12695 }
12696
12697 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
12698    have the values zero or one, indicates the ffreep insn's operand
12699    from the OPERANDS array.  */
12700
12701 static const char *
12702 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
12703 {
12704   if (TARGET_USE_FFREEP)
12705 #ifdef HAVE_AS_IX86_FFREEP
12706     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
12707 #else
12708     {
12709       static char retval[32];
12710       int regno = REGNO (operands[opno]);
12711
12712       gcc_assert (FP_REGNO_P (regno));
12713
12714       regno -= FIRST_STACK_REG;
12715
12716       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
12717       return retval;
12718     }
12719 #endif
12720
12721   return opno ? "fstp\t%y1" : "fstp\t%y0";
12722 }
12723
12724
12725 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
12726    should be used.  UNORDERED_P is true when fucom should be used.  */
12727
12728 const char *
12729 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
12730 {
12731   int stack_top_dies;
12732   rtx cmp_op0, cmp_op1;
12733   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
12734
12735   if (eflags_p)
12736     {
12737       cmp_op0 = operands[0];
12738       cmp_op1 = operands[1];
12739     }
12740   else
12741     {
12742       cmp_op0 = operands[1];
12743       cmp_op1 = operands[2];
12744     }
12745
12746   if (is_sse)
12747     {
12748       static const char ucomiss[] = "vucomiss\t{%1, %0|%0, %1}";
12749       static const char ucomisd[] = "vucomisd\t{%1, %0|%0, %1}";
12750       static const char comiss[] = "vcomiss\t{%1, %0|%0, %1}";
12751       static const char comisd[] = "vcomisd\t{%1, %0|%0, %1}";
12752
12753       if (GET_MODE (operands[0]) == SFmode)
12754         if (unordered_p)
12755           return &ucomiss[TARGET_AVX ? 0 : 1];
12756         else
12757           return &comiss[TARGET_AVX ? 0 : 1];
12758       else
12759         if (unordered_p)
12760           return &ucomisd[TARGET_AVX ? 0 : 1];
12761         else
12762           return &comisd[TARGET_AVX ? 0 : 1];
12763     }
12764
12765   gcc_assert (STACK_TOP_P (cmp_op0));
12766
12767   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
12768
12769   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
12770     {
12771       if (stack_top_dies)
12772         {
12773           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
12774           return output_387_ffreep (operands, 1);
12775         }
12776       else
12777         return "ftst\n\tfnstsw\t%0";
12778     }
12779
12780   if (STACK_REG_P (cmp_op1)
12781       && stack_top_dies
12782       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
12783       && REGNO (cmp_op1) != FIRST_STACK_REG)
12784     {
12785       /* If both the top of the 387 stack dies, and the other operand
12786          is also a stack register that dies, then this must be a
12787          `fcompp' float compare */
12788
12789       if (eflags_p)
12790         {
12791           /* There is no double popping fcomi variant.  Fortunately,
12792              eflags is immune from the fstp's cc clobbering.  */
12793           if (unordered_p)
12794             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
12795           else
12796             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
12797           return output_387_ffreep (operands, 0);
12798         }
12799       else
12800         {
12801           if (unordered_p)
12802             return "fucompp\n\tfnstsw\t%0";
12803           else
12804             return "fcompp\n\tfnstsw\t%0";
12805         }
12806     }
12807   else
12808     {
12809       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
12810
12811       static const char * const alt[16] =
12812       {
12813         "fcom%Z2\t%y2\n\tfnstsw\t%0",
12814         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
12815         "fucom%Z2\t%y2\n\tfnstsw\t%0",
12816         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
12817
12818         "ficom%Z2\t%y2\n\tfnstsw\t%0",
12819         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
12820         NULL,
12821         NULL,
12822
12823         "fcomi\t{%y1, %0|%0, %y1}",
12824         "fcomip\t{%y1, %0|%0, %y1}",
12825         "fucomi\t{%y1, %0|%0, %y1}",
12826         "fucomip\t{%y1, %0|%0, %y1}",
12827
12828         NULL,
12829         NULL,
12830         NULL,
12831         NULL
12832       };
12833
12834       int mask;
12835       const char *ret;
12836
12837       mask  = eflags_p << 3;
12838       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
12839       mask |= unordered_p << 1;
12840       mask |= stack_top_dies;
12841
12842       gcc_assert (mask < 16);
12843       ret = alt[mask];
12844       gcc_assert (ret);
12845
12846       return ret;
12847     }
12848 }
12849
12850 void
12851 ix86_output_addr_vec_elt (FILE *file, int value)
12852 {
12853   const char *directive = ASM_LONG;
12854
12855 #ifdef ASM_QUAD
12856   if (TARGET_64BIT)
12857     directive = ASM_QUAD;
12858 #else
12859   gcc_assert (!TARGET_64BIT);
12860 #endif
12861
12862   fprintf (file, "%s" LPREFIX "%d\n", directive, value);
12863 }
12864
12865 void
12866 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
12867 {
12868   const char *directive = ASM_LONG;
12869
12870 #ifdef ASM_QUAD
12871   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
12872     directive = ASM_QUAD;
12873 #else
12874   gcc_assert (!TARGET_64BIT);
12875 #endif
12876   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
12877   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
12878     fprintf (file, "%s" LPREFIX "%d-" LPREFIX "%d\n",
12879              directive, value, rel);
12880   else if (HAVE_AS_GOTOFF_IN_DATA)
12881     fprintf (file, ASM_LONG LPREFIX "%d@GOTOFF\n", value);
12882 #if TARGET_MACHO
12883   else if (TARGET_MACHO)
12884     {
12885       fprintf (file, ASM_LONG LPREFIX "%d-", value);
12886       machopic_output_function_base_name (file);
12887       putc ('\n', file);
12888     }
12889 #endif
12890   else
12891     asm_fprintf (file, ASM_LONG "%U%s+[.-" LPREFIX "%d]\n",
12892                  GOT_SYMBOL_NAME, value);
12893 }
12894 \f
12895 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
12896    for the target.  */
12897
12898 void
12899 ix86_expand_clear (rtx dest)
12900 {
12901   rtx tmp;
12902
12903   /* We play register width games, which are only valid after reload.  */
12904   gcc_assert (reload_completed);
12905
12906   /* Avoid HImode and its attendant prefix byte.  */
12907   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
12908     dest = gen_rtx_REG (SImode, REGNO (dest));
12909   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
12910
12911   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
12912   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
12913     {
12914       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
12915       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
12916     }
12917
12918   emit_insn (tmp);
12919 }
12920
12921 /* X is an unchanging MEM.  If it is a constant pool reference, return
12922    the constant pool rtx, else NULL.  */
12923
12924 rtx
12925 maybe_get_pool_constant (rtx x)
12926 {
12927   x = ix86_delegitimize_address (XEXP (x, 0));
12928
12929   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
12930     return get_pool_constant (x);
12931
12932   return NULL_RTX;
12933 }
12934
12935 void
12936 ix86_expand_move (enum machine_mode mode, rtx operands[])
12937 {
12938   rtx op0, op1;
12939   enum tls_model model;
12940
12941   op0 = operands[0];
12942   op1 = operands[1];
12943
12944   if (GET_CODE (op1) == SYMBOL_REF)
12945     {
12946       model = SYMBOL_REF_TLS_MODEL (op1);
12947       if (model)
12948         {
12949           op1 = legitimize_tls_address (op1, model, true);
12950           op1 = force_operand (op1, op0);
12951           if (op1 == op0)
12952             return;
12953         }
12954       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
12955                && SYMBOL_REF_DLLIMPORT_P (op1))
12956         op1 = legitimize_dllimport_symbol (op1, false);
12957     }
12958   else if (GET_CODE (op1) == CONST
12959            && GET_CODE (XEXP (op1, 0)) == PLUS
12960            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
12961     {
12962       rtx addend = XEXP (XEXP (op1, 0), 1);
12963       rtx symbol = XEXP (XEXP (op1, 0), 0);
12964       rtx tmp = NULL;
12965
12966       model = SYMBOL_REF_TLS_MODEL (symbol);
12967       if (model)
12968         tmp = legitimize_tls_address (symbol, model, true);
12969       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
12970                && SYMBOL_REF_DLLIMPORT_P (symbol))
12971         tmp = legitimize_dllimport_symbol (symbol, true);
12972
12973       if (tmp)
12974         {
12975           tmp = force_operand (tmp, NULL);
12976           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
12977                                      op0, 1, OPTAB_DIRECT);
12978           if (tmp == op0)
12979             return;
12980         }
12981     }
12982
12983   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
12984     {
12985       if (TARGET_MACHO && !TARGET_64BIT)
12986         {
12987 #if TARGET_MACHO
12988           if (MACHOPIC_PURE)
12989             {
12990               rtx temp = ((reload_in_progress
12991                            || ((op0 && REG_P (op0))
12992                                && mode == Pmode))
12993                           ? op0 : gen_reg_rtx (Pmode));
12994               op1 = machopic_indirect_data_reference (op1, temp);
12995               op1 = machopic_legitimize_pic_address (op1, mode,
12996                                                      temp == op1 ? 0 : temp);
12997             }
12998           else if (MACHOPIC_INDIRECT)
12999             op1 = machopic_indirect_data_reference (op1, 0);
13000           if (op0 == op1)
13001             return;
13002 #endif
13003         }
13004       else
13005         {
13006           if (MEM_P (op0))
13007             op1 = force_reg (Pmode, op1);
13008           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
13009             {
13010               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
13011               op1 = legitimize_pic_address (op1, reg);
13012               if (op0 == op1)
13013                 return;
13014             }
13015         }
13016     }
13017   else
13018     {
13019       if (MEM_P (op0)
13020           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
13021               || !push_operand (op0, mode))
13022           && MEM_P (op1))
13023         op1 = force_reg (mode, op1);
13024
13025       if (push_operand (op0, mode)
13026           && ! general_no_elim_operand (op1, mode))
13027         op1 = copy_to_mode_reg (mode, op1);
13028
13029       /* Force large constants in 64bit compilation into register
13030          to get them CSEed.  */
13031       if (can_create_pseudo_p ()
13032           && (mode == DImode) && TARGET_64BIT
13033           && immediate_operand (op1, mode)
13034           && !x86_64_zext_immediate_operand (op1, VOIDmode)
13035           && !register_operand (op0, mode)
13036           && optimize)
13037         op1 = copy_to_mode_reg (mode, op1);
13038
13039       if (can_create_pseudo_p ()
13040           && FLOAT_MODE_P (mode)
13041           && GET_CODE (op1) == CONST_DOUBLE)
13042         {
13043           /* If we are loading a floating point constant to a register,
13044              force the value to memory now, since we'll get better code
13045              out the back end.  */
13046
13047           op1 = validize_mem (force_const_mem (mode, op1));
13048           if (!register_operand (op0, mode))
13049             {
13050               rtx temp = gen_reg_rtx (mode);
13051               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
13052               emit_move_insn (op0, temp);
13053               return;
13054             }
13055         }
13056     }
13057
13058   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
13059 }
13060
13061 void
13062 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
13063 {
13064   rtx op0 = operands[0], op1 = operands[1];
13065   unsigned int align = GET_MODE_ALIGNMENT (mode);
13066
13067   /* Force constants other than zero into memory.  We do not know how
13068      the instructions used to build constants modify the upper 64 bits
13069      of the register, once we have that information we may be able
13070      to handle some of them more efficiently.  */
13071   if (can_create_pseudo_p ()
13072       && register_operand (op0, mode)
13073       && (CONSTANT_P (op1)
13074           || (GET_CODE (op1) == SUBREG
13075               && CONSTANT_P (SUBREG_REG (op1))))
13076       && !standard_sse_constant_p (op1))
13077     op1 = validize_mem (force_const_mem (mode, op1));
13078
13079   /* We need to check memory alignment for SSE mode since attribute
13080      can make operands unaligned.  */
13081   if (can_create_pseudo_p ()
13082       && SSE_REG_MODE_P (mode)
13083       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
13084           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
13085     {
13086       rtx tmp[2];
13087
13088       /* ix86_expand_vector_move_misalign() does not like constants ... */
13089       if (CONSTANT_P (op1)
13090           || (GET_CODE (op1) == SUBREG
13091               && CONSTANT_P (SUBREG_REG (op1))))
13092         op1 = validize_mem (force_const_mem (mode, op1));
13093
13094       /* ... nor both arguments in memory.  */
13095       if (!register_operand (op0, mode)
13096           && !register_operand (op1, mode))
13097         op1 = force_reg (mode, op1);
13098
13099       tmp[0] = op0; tmp[1] = op1;
13100       ix86_expand_vector_move_misalign (mode, tmp);
13101       return;
13102     }
13103
13104   /* Make operand1 a register if it isn't already.  */
13105   if (can_create_pseudo_p ()
13106       && !register_operand (op0, mode)
13107       && !register_operand (op1, mode))
13108     {
13109       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
13110       return;
13111     }
13112
13113   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
13114 }
13115
13116 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
13117    straight to ix86_expand_vector_move.  */
13118 /* Code generation for scalar reg-reg moves of single and double precision data:
13119      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
13120        movaps reg, reg
13121      else
13122        movss reg, reg
13123      if (x86_sse_partial_reg_dependency == true)
13124        movapd reg, reg
13125      else
13126        movsd reg, reg
13127
13128    Code generation for scalar loads of double precision data:
13129      if (x86_sse_split_regs == true)
13130        movlpd mem, reg      (gas syntax)
13131      else
13132        movsd mem, reg
13133
13134    Code generation for unaligned packed loads of single precision data
13135    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
13136      if (x86_sse_unaligned_move_optimal)
13137        movups mem, reg
13138
13139      if (x86_sse_partial_reg_dependency == true)
13140        {
13141          xorps  reg, reg
13142          movlps mem, reg
13143          movhps mem+8, reg
13144        }
13145      else
13146        {
13147          movlps mem, reg
13148          movhps mem+8, reg
13149        }
13150
13151    Code generation for unaligned packed loads of double precision data
13152    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
13153      if (x86_sse_unaligned_move_optimal)
13154        movupd mem, reg
13155
13156      if (x86_sse_split_regs == true)
13157        {
13158          movlpd mem, reg
13159          movhpd mem+8, reg
13160        }
13161      else
13162        {
13163          movsd  mem, reg
13164          movhpd mem+8, reg
13165        }
13166  */
13167
13168 void
13169 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
13170 {
13171   rtx op0, op1, m;
13172
13173   op0 = operands[0];
13174   op1 = operands[1];
13175
13176   if (TARGET_AVX)
13177     {
13178       switch (GET_MODE_CLASS (mode))
13179         {
13180         case MODE_VECTOR_INT:
13181         case MODE_INT:
13182           switch (GET_MODE_SIZE (mode))
13183             {
13184             case 16:
13185               op0 = gen_lowpart (V16QImode, op0);
13186               op1 = gen_lowpart (V16QImode, op1);
13187               emit_insn (gen_avx_movdqu (op0, op1));
13188               break;
13189             case 32:
13190               op0 = gen_lowpart (V32QImode, op0);
13191               op1 = gen_lowpart (V32QImode, op1);
13192               emit_insn (gen_avx_movdqu256 (op0, op1));
13193               break;
13194             default:
13195               gcc_unreachable ();
13196             }
13197           break;
13198         case MODE_VECTOR_FLOAT:
13199           op0 = gen_lowpart (mode, op0);
13200           op1 = gen_lowpart (mode, op1);
13201
13202           switch (mode)
13203             { 
13204             case V4SFmode:
13205               emit_insn (gen_avx_movups (op0, op1));
13206               break;
13207             case V8SFmode:
13208               emit_insn (gen_avx_movups256 (op0, op1));
13209               break;
13210             case V2DFmode:
13211               emit_insn (gen_avx_movupd (op0, op1));
13212               break;
13213             case V4DFmode:
13214               emit_insn (gen_avx_movupd256 (op0, op1));
13215               break;
13216             default:
13217               gcc_unreachable ();
13218             }
13219           break;
13220
13221         default:
13222           gcc_unreachable ();
13223         }
13224
13225       return;
13226     }
13227
13228   if (MEM_P (op1))
13229     {
13230       /* If we're optimizing for size, movups is the smallest.  */
13231       if (optimize_insn_for_size_p ())
13232         {
13233           op0 = gen_lowpart (V4SFmode, op0);
13234           op1 = gen_lowpart (V4SFmode, op1);
13235           emit_insn (gen_sse_movups (op0, op1));
13236           return;
13237         }
13238
13239       /* ??? If we have typed data, then it would appear that using
13240          movdqu is the only way to get unaligned data loaded with
13241          integer type.  */
13242       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13243         {
13244           op0 = gen_lowpart (V16QImode, op0);
13245           op1 = gen_lowpart (V16QImode, op1);
13246           emit_insn (gen_sse2_movdqu (op0, op1));
13247           return;
13248         }
13249
13250       if (TARGET_SSE2 && mode == V2DFmode)
13251         {
13252           rtx zero;
13253
13254           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
13255             {
13256               op0 = gen_lowpart (V2DFmode, op0);
13257               op1 = gen_lowpart (V2DFmode, op1);
13258               emit_insn (gen_sse2_movupd (op0, op1));
13259               return;
13260             }
13261
13262           /* When SSE registers are split into halves, we can avoid
13263              writing to the top half twice.  */
13264           if (TARGET_SSE_SPLIT_REGS)
13265             {
13266               emit_clobber (op0);
13267               zero = op0;
13268             }
13269           else
13270             {
13271               /* ??? Not sure about the best option for the Intel chips.
13272                  The following would seem to satisfy; the register is
13273                  entirely cleared, breaking the dependency chain.  We
13274                  then store to the upper half, with a dependency depth
13275                  of one.  A rumor has it that Intel recommends two movsd
13276                  followed by an unpacklpd, but this is unconfirmed.  And
13277                  given that the dependency depth of the unpacklpd would
13278                  still be one, I'm not sure why this would be better.  */
13279               zero = CONST0_RTX (V2DFmode);
13280             }
13281
13282           m = adjust_address (op1, DFmode, 0);
13283           emit_insn (gen_sse2_loadlpd (op0, zero, m));
13284           m = adjust_address (op1, DFmode, 8);
13285           emit_insn (gen_sse2_loadhpd (op0, op0, m));
13286         }
13287       else
13288         {
13289           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
13290             {
13291               op0 = gen_lowpart (V4SFmode, op0);
13292               op1 = gen_lowpart (V4SFmode, op1);
13293               emit_insn (gen_sse_movups (op0, op1));
13294               return;
13295             }
13296
13297           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
13298             emit_move_insn (op0, CONST0_RTX (mode));
13299           else
13300             emit_clobber (op0);
13301
13302           if (mode != V4SFmode)
13303             op0 = gen_lowpart (V4SFmode, op0);
13304           m = adjust_address (op1, V2SFmode, 0);
13305           emit_insn (gen_sse_loadlps (op0, op0, m));
13306           m = adjust_address (op1, V2SFmode, 8);
13307           emit_insn (gen_sse_loadhps (op0, op0, m));
13308         }
13309     }
13310   else if (MEM_P (op0))
13311     {
13312       /* If we're optimizing for size, movups is the smallest.  */
13313       if (optimize_insn_for_size_p ())
13314         {
13315           op0 = gen_lowpart (V4SFmode, op0);
13316           op1 = gen_lowpart (V4SFmode, op1);
13317           emit_insn (gen_sse_movups (op0, op1));
13318           return;
13319         }
13320
13321       /* ??? Similar to above, only less clear because of quote
13322          typeless stores unquote.  */
13323       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
13324           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13325         {
13326           op0 = gen_lowpart (V16QImode, op0);
13327           op1 = gen_lowpart (V16QImode, op1);
13328           emit_insn (gen_sse2_movdqu (op0, op1));
13329           return;
13330         }
13331
13332       if (TARGET_SSE2 && mode == V2DFmode)
13333         {
13334           m = adjust_address (op0, DFmode, 0);
13335           emit_insn (gen_sse2_storelpd (m, op1));
13336           m = adjust_address (op0, DFmode, 8);
13337           emit_insn (gen_sse2_storehpd (m, op1));
13338         }
13339       else
13340         {
13341           if (mode != V4SFmode)
13342             op1 = gen_lowpart (V4SFmode, op1);
13343           m = adjust_address (op0, V2SFmode, 0);
13344           emit_insn (gen_sse_storelps (m, op1));
13345           m = adjust_address (op0, V2SFmode, 8);
13346           emit_insn (gen_sse_storehps (m, op1));
13347         }
13348     }
13349   else
13350     gcc_unreachable ();
13351 }
13352
13353 /* Expand a push in MODE.  This is some mode for which we do not support
13354    proper push instructions, at least from the registers that we expect
13355    the value to live in.  */
13356
13357 void
13358 ix86_expand_push (enum machine_mode mode, rtx x)
13359 {
13360   rtx tmp;
13361
13362   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
13363                              GEN_INT (-GET_MODE_SIZE (mode)),
13364                              stack_pointer_rtx, 1, OPTAB_DIRECT);
13365   if (tmp != stack_pointer_rtx)
13366     emit_move_insn (stack_pointer_rtx, tmp);
13367
13368   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
13369
13370   /* When we push an operand onto stack, it has to be aligned at least
13371      at the function argument boundary.  However since we don't have
13372      the argument type, we can't determine the actual argument
13373      boundary.  */
13374   emit_move_insn (tmp, x);
13375 }
13376
13377 /* Helper function of ix86_fixup_binary_operands to canonicalize
13378    operand order.  Returns true if the operands should be swapped.  */
13379
13380 static bool
13381 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
13382                              rtx operands[])
13383 {
13384   rtx dst = operands[0];
13385   rtx src1 = operands[1];
13386   rtx src2 = operands[2];
13387
13388   /* If the operation is not commutative, we can't do anything.  */
13389   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
13390     return false;
13391
13392   /* Highest priority is that src1 should match dst.  */
13393   if (rtx_equal_p (dst, src1))
13394     return false;
13395   if (rtx_equal_p (dst, src2))
13396     return true;
13397
13398   /* Next highest priority is that immediate constants come second.  */
13399   if (immediate_operand (src2, mode))
13400     return false;
13401   if (immediate_operand (src1, mode))
13402     return true;
13403
13404   /* Lowest priority is that memory references should come second.  */
13405   if (MEM_P (src2))
13406     return false;
13407   if (MEM_P (src1))
13408     return true;
13409
13410   return false;
13411 }
13412
13413
13414 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
13415    destination to use for the operation.  If different from the true
13416    destination in operands[0], a copy operation will be required.  */
13417
13418 rtx
13419 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
13420                             rtx operands[])
13421 {
13422   rtx dst = operands[0];
13423   rtx src1 = operands[1];
13424   rtx src2 = operands[2];
13425
13426   /* Canonicalize operand order.  */
13427   if (ix86_swap_binary_operands_p (code, mode, operands))
13428     {
13429       rtx temp;
13430
13431       /* It is invalid to swap operands of different modes.  */
13432       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
13433
13434       temp = src1;
13435       src1 = src2;
13436       src2 = temp;
13437     }
13438
13439   /* Both source operands cannot be in memory.  */
13440   if (MEM_P (src1) && MEM_P (src2))
13441     {
13442       /* Optimization: Only read from memory once.  */
13443       if (rtx_equal_p (src1, src2))
13444         {
13445           src2 = force_reg (mode, src2);
13446           src1 = src2;
13447         }
13448       else
13449         src2 = force_reg (mode, src2);
13450     }
13451
13452   /* If the destination is memory, and we do not have matching source
13453      operands, do things in registers.  */
13454   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
13455     dst = gen_reg_rtx (mode);
13456
13457   /* Source 1 cannot be a constant.  */
13458   if (CONSTANT_P (src1))
13459     src1 = force_reg (mode, src1);
13460
13461   /* Source 1 cannot be a non-matching memory.  */
13462   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
13463     src1 = force_reg (mode, src1);
13464
13465   operands[1] = src1;
13466   operands[2] = src2;
13467   return dst;
13468 }
13469
13470 /* Similarly, but assume that the destination has already been
13471    set up properly.  */
13472
13473 void
13474 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
13475                                     enum machine_mode mode, rtx operands[])
13476 {
13477   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
13478   gcc_assert (dst == operands[0]);
13479 }
13480
13481 /* Attempt to expand a binary operator.  Make the expansion closer to the
13482    actual machine, then just general_operand, which will allow 3 separate
13483    memory references (one output, two input) in a single insn.  */
13484
13485 void
13486 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
13487                              rtx operands[])
13488 {
13489   rtx src1, src2, dst, op, clob;
13490
13491   dst = ix86_fixup_binary_operands (code, mode, operands);
13492   src1 = operands[1];
13493   src2 = operands[2];
13494
13495  /* Emit the instruction.  */
13496
13497   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
13498   if (reload_in_progress)
13499     {
13500       /* Reload doesn't know about the flags register, and doesn't know that
13501          it doesn't want to clobber it.  We can only do this with PLUS.  */
13502       gcc_assert (code == PLUS);
13503       emit_insn (op);
13504     }
13505   else
13506     {
13507       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
13508       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
13509     }
13510
13511   /* Fix up the destination if needed.  */
13512   if (dst != operands[0])
13513     emit_move_insn (operands[0], dst);
13514 }
13515
13516 /* Return TRUE or FALSE depending on whether the binary operator meets the
13517    appropriate constraints.  */
13518
13519 int
13520 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
13521                          rtx operands[3])
13522 {
13523   rtx dst = operands[0];
13524   rtx src1 = operands[1];
13525   rtx src2 = operands[2];
13526
13527   /* Both source operands cannot be in memory.  */
13528   if (MEM_P (src1) && MEM_P (src2))
13529     return 0;
13530
13531   /* Canonicalize operand order for commutative operators.  */
13532   if (ix86_swap_binary_operands_p (code, mode, operands))
13533     {
13534       rtx temp = src1;
13535       src1 = src2;
13536       src2 = temp;
13537     }
13538
13539   /* If the destination is memory, we must have a matching source operand.  */
13540   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
13541       return 0;
13542
13543   /* Source 1 cannot be a constant.  */
13544   if (CONSTANT_P (src1))
13545     return 0;
13546
13547   /* Source 1 cannot be a non-matching memory.  */
13548   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
13549     return 0;
13550
13551   return 1;
13552 }
13553
13554 /* Attempt to expand a unary operator.  Make the expansion closer to the
13555    actual machine, then just general_operand, which will allow 2 separate
13556    memory references (one output, one input) in a single insn.  */
13557
13558 void
13559 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
13560                             rtx operands[])
13561 {
13562   int matching_memory;
13563   rtx src, dst, op, clob;
13564
13565   dst = operands[0];
13566   src = operands[1];
13567
13568   /* If the destination is memory, and we do not have matching source
13569      operands, do things in registers.  */
13570   matching_memory = 0;
13571   if (MEM_P (dst))
13572     {
13573       if (rtx_equal_p (dst, src))
13574         matching_memory = 1;
13575       else
13576         dst = gen_reg_rtx (mode);
13577     }
13578
13579   /* When source operand is memory, destination must match.  */
13580   if (MEM_P (src) && !matching_memory)
13581     src = force_reg (mode, src);
13582
13583   /* Emit the instruction.  */
13584
13585   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
13586   if (reload_in_progress || code == NOT)
13587     {
13588       /* Reload doesn't know about the flags register, and doesn't know that
13589          it doesn't want to clobber it.  */
13590       gcc_assert (code == NOT);
13591       emit_insn (op);
13592     }
13593   else
13594     {
13595       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
13596       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
13597     }
13598
13599   /* Fix up the destination if needed.  */
13600   if (dst != operands[0])
13601     emit_move_insn (operands[0], dst);
13602 }
13603
13604 #define LEA_SEARCH_THRESHOLD 12
13605
13606 /* Search backward for non-agu definition of register number REGNO1
13607    or register number REGNO2 in INSN's basic block until 
13608    1. Pass LEA_SEARCH_THRESHOLD instructions, or
13609    2. Reach BB boundary, or
13610    3. Reach agu definition.
13611    Returns the distance between the non-agu definition point and INSN.
13612    If no definition point, returns -1.  */
13613
13614 static int
13615 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
13616                          rtx insn)
13617 {
13618   basic_block bb = BLOCK_FOR_INSN (insn);
13619   int distance = 0;
13620   df_ref *def_rec;
13621   enum attr_type insn_type;
13622
13623   if (insn != BB_HEAD (bb))
13624     {
13625       rtx prev = PREV_INSN (insn);
13626       while (prev && distance < LEA_SEARCH_THRESHOLD)
13627         {
13628           if (INSN_P (prev))
13629             {
13630               distance++;
13631               for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
13632                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
13633                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
13634                     && (regno1 == DF_REF_REGNO (*def_rec)
13635                         || regno2 == DF_REF_REGNO (*def_rec)))
13636                   {
13637                     insn_type = get_attr_type (prev);
13638                     if (insn_type != TYPE_LEA)
13639                       goto done;
13640                   }
13641             }
13642           if (prev == BB_HEAD (bb))
13643             break;
13644           prev = PREV_INSN (prev);
13645         }
13646     }
13647   
13648   if (distance < LEA_SEARCH_THRESHOLD)
13649     {
13650       edge e;
13651       edge_iterator ei;
13652       bool simple_loop = false;
13653   
13654       FOR_EACH_EDGE (e, ei, bb->preds)
13655         if (e->src == bb)
13656           {
13657             simple_loop = true;
13658             break;
13659           }
13660   
13661       if (simple_loop)
13662         {
13663           rtx prev = BB_END (bb);
13664           while (prev
13665                  && prev != insn
13666                  && distance < LEA_SEARCH_THRESHOLD)
13667             {
13668               if (INSN_P (prev))
13669                 {
13670                   distance++;
13671                   for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
13672                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
13673                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
13674                         && (regno1 == DF_REF_REGNO (*def_rec)
13675                             || regno2 == DF_REF_REGNO (*def_rec)))
13676                       {
13677                         insn_type = get_attr_type (prev);
13678                         if (insn_type != TYPE_LEA)
13679                           goto done;
13680                       }
13681                 }
13682               prev = PREV_INSN (prev);
13683             }
13684         }
13685     }
13686
13687   distance = -1;
13688
13689 done:
13690   /* get_attr_type may modify recog data.  We want to make sure
13691      that recog data is valid for instruction INSN, on which
13692      distance_non_agu_define is called.  INSN is unchanged here.  */
13693   extract_insn_cached (insn);
13694   return distance;
13695 }
13696
13697 /* Return the distance between INSN and the next insn that uses 
13698    register number REGNO0 in memory address.  Return -1 if no such
13699    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
13700
13701 static int
13702 distance_agu_use (unsigned int regno0, rtx insn)
13703 {
13704   basic_block bb = BLOCK_FOR_INSN (insn);
13705   int distance = 0;
13706   df_ref *def_rec;
13707   df_ref *use_rec;
13708
13709   if (insn != BB_END (bb))
13710     {
13711       rtx next = NEXT_INSN (insn);
13712       while (next && distance < LEA_SEARCH_THRESHOLD)
13713         {
13714           if (INSN_P (next))
13715             {
13716               distance++;
13717
13718               for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
13719                 if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
13720                      || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
13721                     && regno0 == DF_REF_REGNO (*use_rec))
13722                   {
13723                     /* Return DISTANCE if OP0 is used in memory
13724                        address in NEXT.  */
13725                     return distance;
13726                   }
13727
13728               for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
13729                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
13730                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
13731                     && regno0 == DF_REF_REGNO (*def_rec))
13732                   {
13733                     /* Return -1 if OP0 is set in NEXT.  */
13734                     return -1;
13735                   }
13736             }
13737           if (next == BB_END (bb))
13738             break;
13739           next = NEXT_INSN (next);
13740         }
13741     }
13742
13743   if (distance < LEA_SEARCH_THRESHOLD)
13744     {
13745       edge e;
13746       edge_iterator ei;
13747       bool simple_loop = false;
13748   
13749       FOR_EACH_EDGE (e, ei, bb->succs)
13750         if (e->dest == bb)
13751           {
13752             simple_loop = true;
13753             break;
13754           }
13755   
13756       if (simple_loop)
13757         {
13758           rtx next = BB_HEAD (bb);
13759           while (next
13760                  && next != insn
13761                  && distance < LEA_SEARCH_THRESHOLD)
13762             {
13763               if (INSN_P (next))
13764                 {
13765                   distance++;
13766
13767                   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
13768                     if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
13769                          || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
13770                         && regno0 == DF_REF_REGNO (*use_rec))
13771                       {
13772                         /* Return DISTANCE if OP0 is used in memory
13773                            address in NEXT.  */
13774                         return distance;
13775                       }
13776
13777                   for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
13778                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
13779                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
13780                         && regno0 == DF_REF_REGNO (*def_rec))
13781                       {
13782                         /* Return -1 if OP0 is set in NEXT.  */
13783                         return -1;
13784                       }
13785
13786                 }
13787               next = NEXT_INSN (next);
13788             }
13789         }
13790     }  
13791
13792   return -1;
13793 }
13794
13795 /* Define this macro to tune LEA priority vs ADD, it take effect when
13796    there is a dilemma of choicing LEA or ADD
13797    Negative value: ADD is more preferred than LEA
13798    Zero: Netrual
13799    Positive value: LEA is more preferred than ADD*/
13800 #define IX86_LEA_PRIORITY 2
13801
13802 /* Return true if it is ok to optimize an ADD operation to LEA
13803    operation to avoid flag register consumation.  For the processors
13804    like ATOM, if the destination register of LEA holds an actual
13805    address which will be used soon, LEA is better and otherwise ADD
13806    is better.  */
13807
13808 bool
13809 ix86_lea_for_add_ok (enum rtx_code code ATTRIBUTE_UNUSED,
13810                      rtx insn, rtx operands[])
13811 {
13812   unsigned int regno0 = true_regnum (operands[0]);
13813   unsigned int regno1 = true_regnum (operands[1]);
13814   unsigned int regno2;
13815
13816   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
13817     return regno0 != regno1;
13818
13819   regno2 = true_regnum (operands[2]);
13820
13821   /* If a = b + c, (a!=b && a!=c), must use lea form. */
13822   if (regno0 != regno1 && regno0 != regno2)
13823     return true;
13824   else    
13825     {
13826       int dist_define, dist_use;
13827       dist_define = distance_non_agu_define (regno1, regno2, insn);
13828       if (dist_define <= 0)
13829         return true;
13830
13831       /* If this insn has both backward non-agu dependence and forward
13832          agu dependence, the one with short distance take effect. */
13833       dist_use = distance_agu_use (regno0, insn);
13834       if (dist_use <= 0
13835           || (dist_define + IX86_LEA_PRIORITY) < dist_use)
13836         return false;
13837
13838       return true;
13839     }
13840 }
13841
13842 /* Return true if destination reg of SET_BODY is shift count of
13843    USE_BODY.  */
13844
13845 static bool
13846 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
13847 {
13848   rtx set_dest;
13849   rtx shift_rtx;
13850   int i;
13851
13852   /* Retrieve destination of SET_BODY.  */
13853   switch (GET_CODE (set_body))
13854     {
13855     case SET:
13856       set_dest = SET_DEST (set_body);
13857       if (!set_dest || !REG_P (set_dest))
13858         return false;
13859       break;
13860     case PARALLEL:
13861       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
13862         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
13863                                           use_body))
13864           return true;
13865     default:
13866       return false;
13867       break;
13868     }
13869
13870   /* Retrieve shift count of USE_BODY.  */
13871   switch (GET_CODE (use_body))
13872     {
13873     case SET:
13874       shift_rtx = XEXP (use_body, 1);
13875       break;
13876     case PARALLEL:
13877       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
13878         if (ix86_dep_by_shift_count_body (set_body,
13879                                           XVECEXP (use_body, 0, i)))
13880           return true;
13881     default:
13882       return false;
13883       break;
13884     }
13885
13886   if (shift_rtx 
13887       && (GET_CODE (shift_rtx) == ASHIFT
13888           || GET_CODE (shift_rtx) == LSHIFTRT
13889           || GET_CODE (shift_rtx) == ASHIFTRT
13890           || GET_CODE (shift_rtx) == ROTATE
13891           || GET_CODE (shift_rtx) == ROTATERT))
13892     {
13893       rtx shift_count = XEXP (shift_rtx, 1);
13894
13895       /* Return true if shift count is dest of SET_BODY.  */
13896       if (REG_P (shift_count)
13897           && true_regnum (set_dest) == true_regnum (shift_count))
13898         return true;
13899     }
13900
13901   return false;
13902 }
13903
13904 /* Return true if destination reg of SET_INSN is shift count of
13905    USE_INSN.  */
13906
13907 bool
13908 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
13909 {
13910   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
13911                                        PATTERN (use_insn));
13912 }
13913
13914 /* Return TRUE or FALSE depending on whether the unary operator meets the
13915    appropriate constraints.  */
13916
13917 int
13918 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
13919                         enum machine_mode mode ATTRIBUTE_UNUSED,
13920                         rtx operands[2] ATTRIBUTE_UNUSED)
13921 {
13922   /* If one of operands is memory, source and destination must match.  */
13923   if ((MEM_P (operands[0])
13924        || MEM_P (operands[1]))
13925       && ! rtx_equal_p (operands[0], operands[1]))
13926     return FALSE;
13927   return TRUE;
13928 }
13929
13930 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
13931    are ok, keeping in mind the possible movddup alternative.  */
13932
13933 bool
13934 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
13935 {
13936   if (MEM_P (operands[0]))
13937     return rtx_equal_p (operands[0], operands[1 + high]);
13938   if (MEM_P (operands[1]) && MEM_P (operands[2]))
13939     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
13940   return true;
13941 }
13942
13943 /* Post-reload splitter for converting an SF or DFmode value in an
13944    SSE register into an unsigned SImode.  */
13945
13946 void
13947 ix86_split_convert_uns_si_sse (rtx operands[])
13948 {
13949   enum machine_mode vecmode;
13950   rtx value, large, zero_or_two31, input, two31, x;
13951
13952   large = operands[1];
13953   zero_or_two31 = operands[2];
13954   input = operands[3];
13955   two31 = operands[4];
13956   vecmode = GET_MODE (large);
13957   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
13958
13959   /* Load up the value into the low element.  We must ensure that the other
13960      elements are valid floats -- zero is the easiest such value.  */
13961   if (MEM_P (input))
13962     {
13963       if (vecmode == V4SFmode)
13964         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
13965       else
13966         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
13967     }
13968   else
13969     {
13970       input = gen_rtx_REG (vecmode, REGNO (input));
13971       emit_move_insn (value, CONST0_RTX (vecmode));
13972       if (vecmode == V4SFmode)
13973         emit_insn (gen_sse_movss (value, value, input));
13974       else
13975         emit_insn (gen_sse2_movsd (value, value, input));
13976     }
13977
13978   emit_move_insn (large, two31);
13979   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
13980
13981   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
13982   emit_insn (gen_rtx_SET (VOIDmode, large, x));
13983
13984   x = gen_rtx_AND (vecmode, zero_or_two31, large);
13985   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
13986
13987   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
13988   emit_insn (gen_rtx_SET (VOIDmode, value, x));
13989
13990   large = gen_rtx_REG (V4SImode, REGNO (large));
13991   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
13992
13993   x = gen_rtx_REG (V4SImode, REGNO (value));
13994   if (vecmode == V4SFmode)
13995     emit_insn (gen_sse2_cvttps2dq (x, value));
13996   else
13997     emit_insn (gen_sse2_cvttpd2dq (x, value));
13998   value = x;
13999
14000   emit_insn (gen_xorv4si3 (value, value, large));
14001 }
14002
14003 /* Convert an unsigned DImode value into a DFmode, using only SSE.
14004    Expects the 64-bit DImode to be supplied in a pair of integral
14005    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
14006    -mfpmath=sse, !optimize_size only.  */
14007
14008 void
14009 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
14010 {
14011   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
14012   rtx int_xmm, fp_xmm;
14013   rtx biases, exponents;
14014   rtx x;
14015
14016   int_xmm = gen_reg_rtx (V4SImode);
14017   if (TARGET_INTER_UNIT_MOVES)
14018     emit_insn (gen_movdi_to_sse (int_xmm, input));
14019   else if (TARGET_SSE_SPLIT_REGS)
14020     {
14021       emit_clobber (int_xmm);
14022       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
14023     }
14024   else
14025     {
14026       x = gen_reg_rtx (V2DImode);
14027       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
14028       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
14029     }
14030
14031   x = gen_rtx_CONST_VECTOR (V4SImode,
14032                             gen_rtvec (4, GEN_INT (0x43300000UL),
14033                                        GEN_INT (0x45300000UL),
14034                                        const0_rtx, const0_rtx));
14035   exponents = validize_mem (force_const_mem (V4SImode, x));
14036
14037   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
14038   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
14039
14040   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
14041      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
14042      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
14043      (0x1.0p84 + double(fp_value_hi_xmm)).
14044      Note these exponents differ by 32.  */
14045
14046   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
14047
14048   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
14049      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
14050   real_ldexp (&bias_lo_rvt, &dconst1, 52);
14051   real_ldexp (&bias_hi_rvt, &dconst1, 84);
14052   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
14053   x = const_double_from_real_value (bias_hi_rvt, DFmode);
14054   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
14055   biases = validize_mem (force_const_mem (V2DFmode, biases));
14056   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
14057
14058   /* Add the upper and lower DFmode values together.  */
14059   if (TARGET_SSE3)
14060     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
14061   else
14062     {
14063       x = copy_to_mode_reg (V2DFmode, fp_xmm);
14064       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
14065       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
14066     }
14067
14068   ix86_expand_vector_extract (false, target, fp_xmm, 0);
14069 }
14070
14071 /* Not used, but eases macroization of patterns.  */
14072 void
14073 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
14074                                   rtx input ATTRIBUTE_UNUSED)
14075 {
14076   gcc_unreachable ();
14077 }
14078
14079 /* Convert an unsigned SImode value into a DFmode.  Only currently used
14080    for SSE, but applicable anywhere.  */
14081
14082 void
14083 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
14084 {
14085   REAL_VALUE_TYPE TWO31r;
14086   rtx x, fp;
14087
14088   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
14089                            NULL, 1, OPTAB_DIRECT);
14090
14091   fp = gen_reg_rtx (DFmode);
14092   emit_insn (gen_floatsidf2 (fp, x));
14093
14094   real_ldexp (&TWO31r, &dconst1, 31);
14095   x = const_double_from_real_value (TWO31r, DFmode);
14096
14097   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
14098   if (x != target)
14099     emit_move_insn (target, x);
14100 }
14101
14102 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
14103    32-bit mode; otherwise we have a direct convert instruction.  */
14104
14105 void
14106 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
14107 {
14108   REAL_VALUE_TYPE TWO32r;
14109   rtx fp_lo, fp_hi, x;
14110
14111   fp_lo = gen_reg_rtx (DFmode);
14112   fp_hi = gen_reg_rtx (DFmode);
14113
14114   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
14115
14116   real_ldexp (&TWO32r, &dconst1, 32);
14117   x = const_double_from_real_value (TWO32r, DFmode);
14118   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
14119
14120   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
14121
14122   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
14123                            0, OPTAB_DIRECT);
14124   if (x != target)
14125     emit_move_insn (target, x);
14126 }
14127
14128 /* Convert an unsigned SImode value into a SFmode, using only SSE.
14129    For x86_32, -mfpmath=sse, !optimize_size only.  */
14130 void
14131 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
14132 {
14133   REAL_VALUE_TYPE ONE16r;
14134   rtx fp_hi, fp_lo, int_hi, int_lo, x;
14135
14136   real_ldexp (&ONE16r, &dconst1, 16);
14137   x = const_double_from_real_value (ONE16r, SFmode);
14138   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
14139                                       NULL, 0, OPTAB_DIRECT);
14140   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
14141                                       NULL, 0, OPTAB_DIRECT);
14142   fp_hi = gen_reg_rtx (SFmode);
14143   fp_lo = gen_reg_rtx (SFmode);
14144   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
14145   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
14146   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
14147                                0, OPTAB_DIRECT);
14148   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
14149                                0, OPTAB_DIRECT);
14150   if (!rtx_equal_p (target, fp_hi))
14151     emit_move_insn (target, fp_hi);
14152 }
14153
14154 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
14155    then replicate the value for all elements of the vector
14156    register.  */
14157
14158 rtx
14159 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
14160 {
14161   rtvec v;
14162   switch (mode)
14163     {
14164     case SImode:
14165       gcc_assert (vect);
14166       v = gen_rtvec (4, value, value, value, value);
14167       return gen_rtx_CONST_VECTOR (V4SImode, v);
14168
14169     case DImode:
14170       gcc_assert (vect);
14171       v = gen_rtvec (2, value, value);
14172       return gen_rtx_CONST_VECTOR (V2DImode, v);
14173
14174     case SFmode:
14175       if (vect)
14176         v = gen_rtvec (4, value, value, value, value);
14177       else
14178         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
14179                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
14180       return gen_rtx_CONST_VECTOR (V4SFmode, v);
14181
14182     case DFmode:
14183       if (vect)
14184         v = gen_rtvec (2, value, value);
14185       else
14186         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
14187       return gen_rtx_CONST_VECTOR (V2DFmode, v);
14188
14189     default:
14190       gcc_unreachable ();
14191     }
14192 }
14193
14194 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
14195    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
14196    for an SSE register.  If VECT is true, then replicate the mask for
14197    all elements of the vector register.  If INVERT is true, then create
14198    a mask excluding the sign bit.  */
14199
14200 rtx
14201 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
14202 {
14203   enum machine_mode vec_mode, imode;
14204   HOST_WIDE_INT hi, lo;
14205   int shift = 63;
14206   rtx v;
14207   rtx mask;
14208
14209   /* Find the sign bit, sign extended to 2*HWI.  */
14210   switch (mode)
14211     {
14212     case SImode:
14213     case SFmode:
14214       imode = SImode;
14215       vec_mode = (mode == SImode) ? V4SImode : V4SFmode;
14216       lo = 0x80000000, hi = lo < 0;
14217       break;
14218
14219     case DImode:
14220     case DFmode:
14221       imode = DImode;
14222       vec_mode = (mode == DImode) ? V2DImode : V2DFmode;
14223       if (HOST_BITS_PER_WIDE_INT >= 64)
14224         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
14225       else
14226         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
14227       break;
14228
14229     case TImode:
14230     case TFmode:
14231       vec_mode = VOIDmode;
14232       if (HOST_BITS_PER_WIDE_INT >= 64)
14233         {
14234           imode = TImode;
14235           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
14236         }
14237       else
14238         {
14239           rtvec vec;
14240
14241           imode = DImode;
14242           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
14243
14244           if (invert)
14245             {
14246               lo = ~lo, hi = ~hi;
14247               v = constm1_rtx;
14248             }
14249           else
14250             v = const0_rtx;
14251
14252           mask = immed_double_const (lo, hi, imode);
14253
14254           vec = gen_rtvec (2, v, mask);
14255           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
14256           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
14257
14258           return v;
14259         }
14260      break;
14261
14262     default:
14263       gcc_unreachable ();
14264     }
14265
14266   if (invert)
14267     lo = ~lo, hi = ~hi;
14268
14269   /* Force this value into the low part of a fp vector constant.  */
14270   mask = immed_double_const (lo, hi, imode);
14271   mask = gen_lowpart (mode, mask);
14272
14273   if (vec_mode == VOIDmode)
14274     return force_reg (mode, mask);
14275
14276   v = ix86_build_const_vector (mode, vect, mask);
14277   return force_reg (vec_mode, v);
14278 }
14279
14280 /* Generate code for floating point ABS or NEG.  */
14281
14282 void
14283 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
14284                                 rtx operands[])
14285 {
14286   rtx mask, set, use, clob, dst, src;
14287   bool use_sse = false;
14288   bool vector_mode = VECTOR_MODE_P (mode);
14289   enum machine_mode elt_mode = mode;
14290
14291   if (vector_mode)
14292     {
14293       elt_mode = GET_MODE_INNER (mode);
14294       use_sse = true;
14295     }
14296   else if (mode == TFmode)
14297     use_sse = true;
14298   else if (TARGET_SSE_MATH)
14299     use_sse = SSE_FLOAT_MODE_P (mode);
14300
14301   /* NEG and ABS performed with SSE use bitwise mask operations.
14302      Create the appropriate mask now.  */
14303   if (use_sse)
14304     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
14305   else
14306     mask = NULL_RTX;
14307
14308   dst = operands[0];
14309   src = operands[1];
14310
14311   if (vector_mode)
14312     {
14313       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
14314       set = gen_rtx_SET (VOIDmode, dst, set);
14315       emit_insn (set);
14316     }
14317   else
14318     {
14319       set = gen_rtx_fmt_e (code, mode, src);
14320       set = gen_rtx_SET (VOIDmode, dst, set);
14321       if (mask)
14322         {
14323           use = gen_rtx_USE (VOIDmode, mask);
14324           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
14325           emit_insn (gen_rtx_PARALLEL (VOIDmode,
14326                                        gen_rtvec (3, set, use, clob)));
14327         }
14328       else
14329         emit_insn (set);
14330     }
14331 }
14332
14333 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
14334
14335 void
14336 ix86_expand_copysign (rtx operands[])
14337 {
14338   enum machine_mode mode;
14339   rtx dest, op0, op1, mask, nmask;
14340
14341   dest = operands[0];
14342   op0 = operands[1];
14343   op1 = operands[2];
14344
14345   mode = GET_MODE (dest);
14346
14347   if (GET_CODE (op0) == CONST_DOUBLE)
14348     {
14349       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
14350
14351       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
14352         op0 = simplify_unary_operation (ABS, mode, op0, mode);
14353
14354       if (mode == SFmode || mode == DFmode)
14355         {
14356           enum machine_mode vmode;
14357
14358           vmode = mode == SFmode ? V4SFmode : V2DFmode;
14359
14360           if (op0 == CONST0_RTX (mode))
14361             op0 = CONST0_RTX (vmode);
14362           else
14363             {
14364               rtx v = ix86_build_const_vector (mode, false, op0);
14365
14366               op0 = force_reg (vmode, v);
14367             }
14368         }
14369       else if (op0 != CONST0_RTX (mode))
14370         op0 = force_reg (mode, op0);
14371
14372       mask = ix86_build_signbit_mask (mode, 0, 0);
14373
14374       if (mode == SFmode)
14375         copysign_insn = gen_copysignsf3_const;
14376       else if (mode == DFmode)
14377         copysign_insn = gen_copysigndf3_const;
14378       else
14379         copysign_insn = gen_copysigntf3_const;
14380
14381         emit_insn (copysign_insn (dest, op0, op1, mask));
14382     }
14383   else
14384     {
14385       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
14386
14387       nmask = ix86_build_signbit_mask (mode, 0, 1);
14388       mask = ix86_build_signbit_mask (mode, 0, 0);
14389
14390       if (mode == SFmode)
14391         copysign_insn = gen_copysignsf3_var;
14392       else if (mode == DFmode)
14393         copysign_insn = gen_copysigndf3_var;
14394       else
14395         copysign_insn = gen_copysigntf3_var;
14396
14397       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
14398     }
14399 }
14400
14401 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
14402    be a constant, and so has already been expanded into a vector constant.  */
14403
14404 void
14405 ix86_split_copysign_const (rtx operands[])
14406 {
14407   enum machine_mode mode, vmode;
14408   rtx dest, op0, mask, x;
14409
14410   dest = operands[0];
14411   op0 = operands[1];
14412   mask = operands[3];
14413
14414   mode = GET_MODE (dest);
14415   vmode = GET_MODE (mask);
14416
14417   dest = simplify_gen_subreg (vmode, dest, mode, 0);
14418   x = gen_rtx_AND (vmode, dest, mask);
14419   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
14420
14421   if (op0 != CONST0_RTX (vmode))
14422     {
14423       x = gen_rtx_IOR (vmode, dest, op0);
14424       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
14425     }
14426 }
14427
14428 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
14429    so we have to do two masks.  */
14430
14431 void
14432 ix86_split_copysign_var (rtx operands[])
14433 {
14434   enum machine_mode mode, vmode;
14435   rtx dest, scratch, op0, op1, mask, nmask, x;
14436
14437   dest = operands[0];
14438   scratch = operands[1];
14439   op0 = operands[2];
14440   op1 = operands[3];
14441   nmask = operands[4];
14442   mask = operands[5];
14443
14444   mode = GET_MODE (dest);
14445   vmode = GET_MODE (mask);
14446
14447   if (rtx_equal_p (op0, op1))
14448     {
14449       /* Shouldn't happen often (it's useless, obviously), but when it does
14450          we'd generate incorrect code if we continue below.  */
14451       emit_move_insn (dest, op0);
14452       return;
14453     }
14454
14455   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
14456     {
14457       gcc_assert (REGNO (op1) == REGNO (scratch));
14458
14459       x = gen_rtx_AND (vmode, scratch, mask);
14460       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
14461
14462       dest = mask;
14463       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
14464       x = gen_rtx_NOT (vmode, dest);
14465       x = gen_rtx_AND (vmode, x, op0);
14466       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
14467     }
14468   else
14469     {
14470       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
14471         {
14472           x = gen_rtx_AND (vmode, scratch, mask);
14473         }
14474       else                                              /* alternative 2,4 */
14475         {
14476           gcc_assert (REGNO (mask) == REGNO (scratch));
14477           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
14478           x = gen_rtx_AND (vmode, scratch, op1);
14479         }
14480       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
14481
14482       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
14483         {
14484           dest = simplify_gen_subreg (vmode, op0, mode, 0);
14485           x = gen_rtx_AND (vmode, dest, nmask);
14486         }
14487       else                                              /* alternative 3,4 */
14488         {
14489           gcc_assert (REGNO (nmask) == REGNO (dest));
14490           dest = nmask;
14491           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
14492           x = gen_rtx_AND (vmode, dest, op0);
14493         }
14494       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
14495     }
14496
14497   x = gen_rtx_IOR (vmode, dest, scratch);
14498   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
14499 }
14500
14501 /* Return TRUE or FALSE depending on whether the first SET in INSN
14502    has source and destination with matching CC modes, and that the
14503    CC mode is at least as constrained as REQ_MODE.  */
14504
14505 int
14506 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
14507 {
14508   rtx set;
14509   enum machine_mode set_mode;
14510
14511   set = PATTERN (insn);
14512   if (GET_CODE (set) == PARALLEL)
14513     set = XVECEXP (set, 0, 0);
14514   gcc_assert (GET_CODE (set) == SET);
14515   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
14516
14517   set_mode = GET_MODE (SET_DEST (set));
14518   switch (set_mode)
14519     {
14520     case CCNOmode:
14521       if (req_mode != CCNOmode
14522           && (req_mode != CCmode
14523               || XEXP (SET_SRC (set), 1) != const0_rtx))
14524         return 0;
14525       break;
14526     case CCmode:
14527       if (req_mode == CCGCmode)
14528         return 0;
14529       /* FALLTHRU */
14530     case CCGCmode:
14531       if (req_mode == CCGOCmode || req_mode == CCNOmode)
14532         return 0;
14533       /* FALLTHRU */
14534     case CCGOCmode:
14535       if (req_mode == CCZmode)
14536         return 0;
14537       /* FALLTHRU */
14538     case CCAmode:
14539     case CCCmode:
14540     case CCOmode:
14541     case CCSmode:
14542     case CCZmode:
14543       break;
14544
14545     default:
14546       gcc_unreachable ();
14547     }
14548
14549   return (GET_MODE (SET_SRC (set)) == set_mode);
14550 }
14551
14552 /* Generate insn patterns to do an integer compare of OPERANDS.  */
14553
14554 static rtx
14555 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
14556 {
14557   enum machine_mode cmpmode;
14558   rtx tmp, flags;
14559
14560   cmpmode = SELECT_CC_MODE (code, op0, op1);
14561   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
14562
14563   /* This is very simple, but making the interface the same as in the
14564      FP case makes the rest of the code easier.  */
14565   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
14566   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
14567
14568   /* Return the test that should be put into the flags user, i.e.
14569      the bcc, scc, or cmov instruction.  */
14570   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
14571 }
14572
14573 /* Figure out whether to use ordered or unordered fp comparisons.
14574    Return the appropriate mode to use.  */
14575
14576 enum machine_mode
14577 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
14578 {
14579   /* ??? In order to make all comparisons reversible, we do all comparisons
14580      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
14581      all forms trapping and nontrapping comparisons, we can make inequality
14582      comparisons trapping again, since it results in better code when using
14583      FCOM based compares.  */
14584   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
14585 }
14586
14587 enum machine_mode
14588 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
14589 {
14590   enum machine_mode mode = GET_MODE (op0);
14591
14592   if (SCALAR_FLOAT_MODE_P (mode))
14593     {
14594       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
14595       return ix86_fp_compare_mode (code);
14596     }
14597
14598   switch (code)
14599     {
14600       /* Only zero flag is needed.  */
14601     case EQ:                    /* ZF=0 */
14602     case NE:                    /* ZF!=0 */
14603       return CCZmode;
14604       /* Codes needing carry flag.  */
14605     case GEU:                   /* CF=0 */
14606     case LTU:                   /* CF=1 */
14607       /* Detect overflow checks.  They need just the carry flag.  */
14608       if (GET_CODE (op0) == PLUS
14609           && rtx_equal_p (op1, XEXP (op0, 0)))
14610         return CCCmode;
14611       else
14612         return CCmode;
14613     case GTU:                   /* CF=0 & ZF=0 */
14614     case LEU:                   /* CF=1 | ZF=1 */
14615       /* Detect overflow checks.  They need just the carry flag.  */
14616       if (GET_CODE (op0) == MINUS
14617           && rtx_equal_p (op1, XEXP (op0, 0)))
14618         return CCCmode;
14619       else
14620         return CCmode;
14621       /* Codes possibly doable only with sign flag when
14622          comparing against zero.  */
14623     case GE:                    /* SF=OF   or   SF=0 */
14624     case LT:                    /* SF<>OF  or   SF=1 */
14625       if (op1 == const0_rtx)
14626         return CCGOCmode;
14627       else
14628         /* For other cases Carry flag is not required.  */
14629         return CCGCmode;
14630       /* Codes doable only with sign flag when comparing
14631          against zero, but we miss jump instruction for it
14632          so we need to use relational tests against overflow
14633          that thus needs to be zero.  */
14634     case GT:                    /* ZF=0 & SF=OF */
14635     case LE:                    /* ZF=1 | SF<>OF */
14636       if (op1 == const0_rtx)
14637         return CCNOmode;
14638       else
14639         return CCGCmode;
14640       /* strcmp pattern do (use flags) and combine may ask us for proper
14641          mode.  */
14642     case USE:
14643       return CCmode;
14644     default:
14645       gcc_unreachable ();
14646     }
14647 }
14648
14649 /* Return the fixed registers used for condition codes.  */
14650
14651 static bool
14652 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
14653 {
14654   *p1 = FLAGS_REG;
14655   *p2 = FPSR_REG;
14656   return true;
14657 }
14658
14659 /* If two condition code modes are compatible, return a condition code
14660    mode which is compatible with both.  Otherwise, return
14661    VOIDmode.  */
14662
14663 static enum machine_mode
14664 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
14665 {
14666   if (m1 == m2)
14667     return m1;
14668
14669   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
14670     return VOIDmode;
14671
14672   if ((m1 == CCGCmode && m2 == CCGOCmode)
14673       || (m1 == CCGOCmode && m2 == CCGCmode))
14674     return CCGCmode;
14675
14676   switch (m1)
14677     {
14678     default:
14679       gcc_unreachable ();
14680
14681     case CCmode:
14682     case CCGCmode:
14683     case CCGOCmode:
14684     case CCNOmode:
14685     case CCAmode:
14686     case CCCmode:
14687     case CCOmode:
14688     case CCSmode:
14689     case CCZmode:
14690       switch (m2)
14691         {
14692         default:
14693           return VOIDmode;
14694
14695         case CCmode:
14696         case CCGCmode:
14697         case CCGOCmode:
14698         case CCNOmode:
14699         case CCAmode:
14700         case CCCmode:
14701         case CCOmode:
14702         case CCSmode:
14703         case CCZmode:
14704           return CCmode;
14705         }
14706
14707     case CCFPmode:
14708     case CCFPUmode:
14709       /* These are only compatible with themselves, which we already
14710          checked above.  */
14711       return VOIDmode;
14712     }
14713 }
14714
14715
14716 /* Return a comparison we can do and that it is equivalent to 
14717    swap_condition (code) apart possibly from orderedness.
14718    But, never change orderedness if TARGET_IEEE_FP, returning
14719    UNKNOWN in that case if necessary.  */
14720
14721 static enum rtx_code
14722 ix86_fp_swap_condition (enum rtx_code code)
14723 {
14724   switch (code)
14725     {
14726     case GT:                   /* GTU - CF=0 & ZF=0 */
14727       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
14728     case GE:                   /* GEU - CF=0 */
14729       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
14730     case UNLT:                 /* LTU - CF=1 */
14731       return TARGET_IEEE_FP ? UNKNOWN : GT;
14732     case UNLE:                 /* LEU - CF=1 | ZF=1 */
14733       return TARGET_IEEE_FP ? UNKNOWN : GE;
14734     default:
14735       return swap_condition (code);
14736     }
14737 }
14738
14739 /* Return cost of comparison CODE using the best strategy for performance.
14740    All following functions do use number of instructions as a cost metrics.
14741    In future this should be tweaked to compute bytes for optimize_size and
14742    take into account performance of various instructions on various CPUs.  */
14743
14744 static int
14745 ix86_fp_comparison_cost (enum rtx_code code)
14746 {
14747   int arith_cost;
14748
14749   /* The cost of code using bit-twiddling on %ah.  */
14750   switch (code)
14751     {
14752     case UNLE:
14753     case UNLT:
14754     case LTGT:
14755     case GT:
14756     case GE:
14757     case UNORDERED:
14758     case ORDERED:
14759     case UNEQ:
14760       arith_cost = 4;
14761       break;
14762     case LT:
14763     case NE:
14764     case EQ:
14765     case UNGE:
14766       arith_cost = TARGET_IEEE_FP ? 5 : 4;
14767       break;
14768     case LE:
14769     case UNGT:
14770       arith_cost = TARGET_IEEE_FP ? 6 : 4;
14771       break;
14772     default:
14773       gcc_unreachable ();
14774     }
14775
14776   switch (ix86_fp_comparison_strategy (code))
14777     {
14778     case IX86_FPCMP_COMI:
14779       return arith_cost > 4 ? 3 : 2;
14780     case IX86_FPCMP_SAHF:
14781       return arith_cost > 4 ? 4 : 3;
14782     default:
14783       return arith_cost;
14784     }
14785 }
14786
14787 /* Return strategy to use for floating-point.  We assume that fcomi is always
14788    preferrable where available, since that is also true when looking at size
14789    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
14790
14791 enum ix86_fpcmp_strategy
14792 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
14793 {
14794   /* Do fcomi/sahf based test when profitable.  */
14795
14796   if (TARGET_CMOVE)
14797     return IX86_FPCMP_COMI;
14798
14799   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
14800     return IX86_FPCMP_SAHF;
14801
14802   return IX86_FPCMP_ARITH;
14803 }
14804
14805 /* Swap, force into registers, or otherwise massage the two operands
14806    to a fp comparison.  The operands are updated in place; the new
14807    comparison code is returned.  */
14808
14809 static enum rtx_code
14810 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
14811 {
14812   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
14813   rtx op0 = *pop0, op1 = *pop1;
14814   enum machine_mode op_mode = GET_MODE (op0);
14815   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
14816
14817   /* All of the unordered compare instructions only work on registers.
14818      The same is true of the fcomi compare instructions.  The XFmode
14819      compare instructions require registers except when comparing
14820      against zero or when converting operand 1 from fixed point to
14821      floating point.  */
14822
14823   if (!is_sse
14824       && (fpcmp_mode == CCFPUmode
14825           || (op_mode == XFmode
14826               && ! (standard_80387_constant_p (op0) == 1
14827                     || standard_80387_constant_p (op1) == 1)
14828               && GET_CODE (op1) != FLOAT)
14829           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
14830     {
14831       op0 = force_reg (op_mode, op0);
14832       op1 = force_reg (op_mode, op1);
14833     }
14834   else
14835     {
14836       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
14837          things around if they appear profitable, otherwise force op0
14838          into a register.  */
14839
14840       if (standard_80387_constant_p (op0) == 0
14841           || (MEM_P (op0)
14842               && ! (standard_80387_constant_p (op1) == 0
14843                     || MEM_P (op1))))
14844         {
14845           enum rtx_code new_code = ix86_fp_swap_condition (code);
14846           if (new_code != UNKNOWN)
14847             {
14848               rtx tmp;
14849               tmp = op0, op0 = op1, op1 = tmp;
14850               code = new_code;
14851             }
14852         }
14853
14854       if (!REG_P (op0))
14855         op0 = force_reg (op_mode, op0);
14856
14857       if (CONSTANT_P (op1))
14858         {
14859           int tmp = standard_80387_constant_p (op1);
14860           if (tmp == 0)
14861             op1 = validize_mem (force_const_mem (op_mode, op1));
14862           else if (tmp == 1)
14863             {
14864               if (TARGET_CMOVE)
14865                 op1 = force_reg (op_mode, op1);
14866             }
14867           else
14868             op1 = force_reg (op_mode, op1);
14869         }
14870     }
14871
14872   /* Try to rearrange the comparison to make it cheaper.  */
14873   if (ix86_fp_comparison_cost (code)
14874       > ix86_fp_comparison_cost (swap_condition (code))
14875       && (REG_P (op1) || can_create_pseudo_p ()))
14876     {
14877       rtx tmp;
14878       tmp = op0, op0 = op1, op1 = tmp;
14879       code = swap_condition (code);
14880       if (!REG_P (op0))
14881         op0 = force_reg (op_mode, op0);
14882     }
14883
14884   *pop0 = op0;
14885   *pop1 = op1;
14886   return code;
14887 }
14888
14889 /* Convert comparison codes we use to represent FP comparison to integer
14890    code that will result in proper branch.  Return UNKNOWN if no such code
14891    is available.  */
14892
14893 enum rtx_code
14894 ix86_fp_compare_code_to_integer (enum rtx_code code)
14895 {
14896   switch (code)
14897     {
14898     case GT:
14899       return GTU;
14900     case GE:
14901       return GEU;
14902     case ORDERED:
14903     case UNORDERED:
14904       return code;
14905       break;
14906     case UNEQ:
14907       return EQ;
14908       break;
14909     case UNLT:
14910       return LTU;
14911       break;
14912     case UNLE:
14913       return LEU;
14914       break;
14915     case LTGT:
14916       return NE;
14917       break;
14918     default:
14919       return UNKNOWN;
14920     }
14921 }
14922
14923 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
14924
14925 static rtx
14926 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
14927 {
14928   enum machine_mode fpcmp_mode, intcmp_mode;
14929   rtx tmp, tmp2;
14930
14931   fpcmp_mode = ix86_fp_compare_mode (code);
14932   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
14933
14934   /* Do fcomi/sahf based test when profitable.  */
14935   switch (ix86_fp_comparison_strategy (code))
14936     {
14937     case IX86_FPCMP_COMI:
14938       intcmp_mode = fpcmp_mode;
14939       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
14940       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
14941                          tmp);
14942       emit_insn (tmp);
14943       break;
14944
14945     case IX86_FPCMP_SAHF:
14946       intcmp_mode = fpcmp_mode;
14947       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
14948       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
14949                          tmp);
14950
14951       if (!scratch)
14952         scratch = gen_reg_rtx (HImode);
14953       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
14954       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
14955       break;
14956
14957     case IX86_FPCMP_ARITH:
14958       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
14959       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
14960       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
14961       if (!scratch)
14962         scratch = gen_reg_rtx (HImode);
14963       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
14964
14965       /* In the unordered case, we have to check C2 for NaN's, which
14966          doesn't happen to work out to anything nice combination-wise.
14967          So do some bit twiddling on the value we've got in AH to come
14968          up with an appropriate set of condition codes.  */
14969
14970       intcmp_mode = CCNOmode;
14971       switch (code)
14972         {
14973         case GT:
14974         case UNGT:
14975           if (code == GT || !TARGET_IEEE_FP)
14976             {
14977               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
14978               code = EQ;
14979             }
14980           else
14981             {
14982               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
14983               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
14984               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
14985               intcmp_mode = CCmode;
14986               code = GEU;
14987             }
14988           break;
14989         case LT:
14990         case UNLT:
14991           if (code == LT && TARGET_IEEE_FP)
14992             {
14993               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
14994               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
14995               intcmp_mode = CCmode;
14996               code = EQ;
14997             }
14998           else
14999             {
15000               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
15001               code = NE;
15002             }
15003           break;
15004         case GE:
15005         case UNGE:
15006           if (code == GE || !TARGET_IEEE_FP)
15007             {
15008               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
15009               code = EQ;
15010             }
15011           else
15012             {
15013               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15014               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
15015               code = NE;
15016             }
15017           break;
15018         case LE:
15019         case UNLE:
15020           if (code == LE && TARGET_IEEE_FP)
15021             {
15022               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15023               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
15024               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
15025               intcmp_mode = CCmode;
15026               code = LTU;
15027             }
15028           else
15029             {
15030               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
15031               code = NE;
15032             }
15033           break;
15034         case EQ:
15035         case UNEQ:
15036           if (code == EQ && TARGET_IEEE_FP)
15037             {
15038               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15039               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
15040               intcmp_mode = CCmode;
15041               code = EQ;
15042             }
15043           else
15044             {
15045               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
15046               code = NE;
15047             }
15048           break;
15049         case NE:
15050         case LTGT:
15051           if (code == NE && TARGET_IEEE_FP)
15052             {
15053               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15054               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
15055                                              GEN_INT (0x40)));
15056               code = NE;
15057             }
15058           else
15059             {
15060               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
15061               code = EQ;
15062             }
15063           break;
15064
15065         case UNORDERED:
15066           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
15067           code = NE;
15068           break;
15069         case ORDERED:
15070           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
15071           code = EQ;
15072           break;
15073
15074         default:
15075           gcc_unreachable ();
15076         }
15077         break;
15078
15079     default:
15080       gcc_unreachable();
15081     }
15082
15083   /* Return the test that should be put into the flags user, i.e.
15084      the bcc, scc, or cmov instruction.  */
15085   return gen_rtx_fmt_ee (code, VOIDmode,
15086                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
15087                          const0_rtx);
15088 }
15089
15090 rtx
15091 ix86_expand_compare (enum rtx_code code)
15092 {
15093   rtx op0, op1, ret;
15094   op0 = ix86_compare_op0;
15095   op1 = ix86_compare_op1;
15096
15097   if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_CC)
15098     ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_op0, ix86_compare_op1);
15099
15100   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
15101     {
15102       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
15103       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
15104     }
15105   else
15106     ret = ix86_expand_int_compare (code, op0, op1);
15107
15108   return ret;
15109 }
15110
15111 void
15112 ix86_expand_branch (enum rtx_code code, rtx label)
15113 {
15114   rtx tmp;
15115
15116   switch (GET_MODE (ix86_compare_op0))
15117     {
15118     case SFmode:
15119     case DFmode:
15120     case XFmode:
15121     case QImode:
15122     case HImode:
15123     case SImode:
15124       simple:
15125       tmp = ix86_expand_compare (code);
15126       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
15127                                   gen_rtx_LABEL_REF (VOIDmode, label),
15128                                   pc_rtx);
15129       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
15130       return;
15131
15132     case DImode:
15133       if (TARGET_64BIT)
15134         goto simple;
15135     case TImode:
15136       /* Expand DImode branch into multiple compare+branch.  */
15137       {
15138         rtx lo[2], hi[2], label2;
15139         enum rtx_code code1, code2, code3;
15140         enum machine_mode submode;
15141
15142         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
15143           {
15144             tmp = ix86_compare_op0;
15145             ix86_compare_op0 = ix86_compare_op1;
15146             ix86_compare_op1 = tmp;
15147             code = swap_condition (code);
15148           }
15149         if (GET_MODE (ix86_compare_op0) == DImode)
15150           {
15151             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
15152             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
15153             submode = SImode;
15154           }
15155         else
15156           {
15157             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
15158             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
15159             submode = DImode;
15160           }
15161
15162         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
15163            avoid two branches.  This costs one extra insn, so disable when
15164            optimizing for size.  */
15165
15166         if ((code == EQ || code == NE)
15167             && (!optimize_insn_for_size_p ()
15168                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
15169           {
15170             rtx xor0, xor1;
15171
15172             xor1 = hi[0];
15173             if (hi[1] != const0_rtx)
15174               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
15175                                    NULL_RTX, 0, OPTAB_WIDEN);
15176
15177             xor0 = lo[0];
15178             if (lo[1] != const0_rtx)
15179               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
15180                                    NULL_RTX, 0, OPTAB_WIDEN);
15181
15182             tmp = expand_binop (submode, ior_optab, xor1, xor0,
15183                                 NULL_RTX, 0, OPTAB_WIDEN);
15184
15185             ix86_compare_op0 = tmp;
15186             ix86_compare_op1 = const0_rtx;
15187             ix86_expand_branch (code, label);
15188             return;
15189           }
15190
15191         /* Otherwise, if we are doing less-than or greater-or-equal-than,
15192            op1 is a constant and the low word is zero, then we can just
15193            examine the high word.  Similarly for low word -1 and
15194            less-or-equal-than or greater-than.  */
15195
15196         if (CONST_INT_P (hi[1]))
15197           switch (code)
15198             {
15199             case LT: case LTU: case GE: case GEU:
15200               if (lo[1] == const0_rtx)
15201                 {
15202                   ix86_compare_op0 = hi[0];
15203                   ix86_compare_op1 = hi[1];
15204                   ix86_expand_branch (code, label);
15205                   return;
15206                 }
15207               break;
15208             case LE: case LEU: case GT: case GTU:
15209               if (lo[1] == constm1_rtx)
15210                 {
15211                   ix86_compare_op0 = hi[0];
15212                   ix86_compare_op1 = hi[1];
15213                   ix86_expand_branch (code, label);
15214                   return;
15215                 }
15216               break;
15217             default:
15218               break;
15219             }
15220
15221         /* Otherwise, we need two or three jumps.  */
15222
15223         label2 = gen_label_rtx ();
15224
15225         code1 = code;
15226         code2 = swap_condition (code);
15227         code3 = unsigned_condition (code);
15228
15229         switch (code)
15230           {
15231           case LT: case GT: case LTU: case GTU:
15232             break;
15233
15234           case LE:   code1 = LT;  code2 = GT;  break;
15235           case GE:   code1 = GT;  code2 = LT;  break;
15236           case LEU:  code1 = LTU; code2 = GTU; break;
15237           case GEU:  code1 = GTU; code2 = LTU; break;
15238
15239           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
15240           case NE:   code2 = UNKNOWN; break;
15241
15242           default:
15243             gcc_unreachable ();
15244           }
15245
15246         /*
15247          * a < b =>
15248          *    if (hi(a) < hi(b)) goto true;
15249          *    if (hi(a) > hi(b)) goto false;
15250          *    if (lo(a) < lo(b)) goto true;
15251          *  false:
15252          */
15253
15254         ix86_compare_op0 = hi[0];
15255         ix86_compare_op1 = hi[1];
15256
15257         if (code1 != UNKNOWN)
15258           ix86_expand_branch (code1, label);
15259         if (code2 != UNKNOWN)
15260           ix86_expand_branch (code2, label2);
15261
15262         ix86_compare_op0 = lo[0];
15263         ix86_compare_op1 = lo[1];
15264         ix86_expand_branch (code3, label);
15265
15266         if (code2 != UNKNOWN)
15267           emit_label (label2);
15268         return;
15269       }
15270
15271     default:
15272       /* If we have already emitted a compare insn, go straight to simple.
15273          ix86_expand_compare won't emit anything if ix86_compare_emitted
15274          is non NULL.  */
15275       gcc_assert (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_CC);
15276       goto simple;
15277     }
15278 }
15279
15280 /* Split branch based on floating point condition.  */
15281 void
15282 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
15283                       rtx target1, rtx target2, rtx tmp, rtx pushed)
15284 {
15285   rtx condition;
15286   rtx i;
15287
15288   if (target2 != pc_rtx)
15289     {
15290       rtx tmp = target2;
15291       code = reverse_condition_maybe_unordered (code);
15292       target2 = target1;
15293       target1 = tmp;
15294     }
15295
15296   condition = ix86_expand_fp_compare (code, op1, op2,
15297                                       tmp);
15298
15299   /* Remove pushed operand from stack.  */
15300   if (pushed)
15301     ix86_free_from_memory (GET_MODE (pushed));
15302
15303   i = emit_jump_insn (gen_rtx_SET
15304                       (VOIDmode, pc_rtx,
15305                        gen_rtx_IF_THEN_ELSE (VOIDmode,
15306                                              condition, target1, target2)));
15307   if (split_branch_probability >= 0)
15308     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
15309 }
15310
15311 void
15312 ix86_expand_setcc (enum rtx_code code, rtx dest)
15313 {
15314   rtx ret;
15315
15316   gcc_assert (GET_MODE (dest) == QImode);
15317
15318   ret = ix86_expand_compare (code);
15319   PUT_MODE (ret, QImode);
15320   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
15321 }
15322
15323 /* Expand comparison setting or clearing carry flag.  Return true when
15324    successful and set pop for the operation.  */
15325 static bool
15326 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
15327 {
15328   enum machine_mode mode =
15329     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
15330
15331   /* Do not handle DImode compares that go through special path.  */
15332   if (mode == (TARGET_64BIT ? TImode : DImode))
15333     return false;
15334
15335   if (SCALAR_FLOAT_MODE_P (mode))
15336     {
15337       rtx compare_op, compare_seq;
15338
15339       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
15340
15341       /* Shortcut:  following common codes never translate
15342          into carry flag compares.  */
15343       if (code == EQ || code == NE || code == UNEQ || code == LTGT
15344           || code == ORDERED || code == UNORDERED)
15345         return false;
15346
15347       /* These comparisons require zero flag; swap operands so they won't.  */
15348       if ((code == GT || code == UNLE || code == LE || code == UNGT)
15349           && !TARGET_IEEE_FP)
15350         {
15351           rtx tmp = op0;
15352           op0 = op1;
15353           op1 = tmp;
15354           code = swap_condition (code);
15355         }
15356
15357       /* Try to expand the comparison and verify that we end up with
15358          carry flag based comparison.  This fails to be true only when
15359          we decide to expand comparison using arithmetic that is not
15360          too common scenario.  */
15361       start_sequence ();
15362       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
15363       compare_seq = get_insns ();
15364       end_sequence ();
15365
15366       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
15367           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
15368         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
15369       else
15370         code = GET_CODE (compare_op);
15371
15372       if (code != LTU && code != GEU)
15373         return false;
15374
15375       emit_insn (compare_seq);
15376       *pop = compare_op;
15377       return true;
15378     }
15379
15380   if (!INTEGRAL_MODE_P (mode))
15381     return false;
15382
15383   switch (code)
15384     {
15385     case LTU:
15386     case GEU:
15387       break;
15388
15389     /* Convert a==0 into (unsigned)a<1.  */
15390     case EQ:
15391     case NE:
15392       if (op1 != const0_rtx)
15393         return false;
15394       op1 = const1_rtx;
15395       code = (code == EQ ? LTU : GEU);
15396       break;
15397
15398     /* Convert a>b into b<a or a>=b-1.  */
15399     case GTU:
15400     case LEU:
15401       if (CONST_INT_P (op1))
15402         {
15403           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
15404           /* Bail out on overflow.  We still can swap operands but that
15405              would force loading of the constant into register.  */
15406           if (op1 == const0_rtx
15407               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
15408             return false;
15409           code = (code == GTU ? GEU : LTU);
15410         }
15411       else
15412         {
15413           rtx tmp = op1;
15414           op1 = op0;
15415           op0 = tmp;
15416           code = (code == GTU ? LTU : GEU);
15417         }
15418       break;
15419
15420     /* Convert a>=0 into (unsigned)a<0x80000000.  */
15421     case LT:
15422     case GE:
15423       if (mode == DImode || op1 != const0_rtx)
15424         return false;
15425       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
15426       code = (code == LT ? GEU : LTU);
15427       break;
15428     case LE:
15429     case GT:
15430       if (mode == DImode || op1 != constm1_rtx)
15431         return false;
15432       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
15433       code = (code == LE ? GEU : LTU);
15434       break;
15435
15436     default:
15437       return false;
15438     }
15439   /* Swapping operands may cause constant to appear as first operand.  */
15440   if (!nonimmediate_operand (op0, VOIDmode))
15441     {
15442       if (!can_create_pseudo_p ())
15443         return false;
15444       op0 = force_reg (mode, op0);
15445     }
15446   ix86_compare_op0 = op0;
15447   ix86_compare_op1 = op1;
15448   *pop = ix86_expand_compare (code);
15449   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
15450   return true;
15451 }
15452
15453 int
15454 ix86_expand_int_movcc (rtx operands[])
15455 {
15456   enum rtx_code code = GET_CODE (operands[1]), compare_code;
15457   rtx compare_seq, compare_op;
15458   enum machine_mode mode = GET_MODE (operands[0]);
15459   bool sign_bit_compare_p = false;
15460
15461   start_sequence ();
15462   ix86_compare_op0 = XEXP (operands[1], 0);
15463   ix86_compare_op1 = XEXP (operands[1], 1);
15464   compare_op = ix86_expand_compare (code);
15465   compare_seq = get_insns ();
15466   end_sequence ();
15467
15468   compare_code = GET_CODE (compare_op);
15469
15470   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
15471       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
15472     sign_bit_compare_p = true;
15473
15474   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
15475      HImode insns, we'd be swallowed in word prefix ops.  */
15476
15477   if ((mode != HImode || TARGET_FAST_PREFIX)
15478       && (mode != (TARGET_64BIT ? TImode : DImode))
15479       && CONST_INT_P (operands[2])
15480       && CONST_INT_P (operands[3]))
15481     {
15482       rtx out = operands[0];
15483       HOST_WIDE_INT ct = INTVAL (operands[2]);
15484       HOST_WIDE_INT cf = INTVAL (operands[3]);
15485       HOST_WIDE_INT diff;
15486
15487       diff = ct - cf;
15488       /*  Sign bit compares are better done using shifts than we do by using
15489           sbb.  */
15490       if (sign_bit_compare_p
15491           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
15492                                              ix86_compare_op1, &compare_op))
15493         {
15494           /* Detect overlap between destination and compare sources.  */
15495           rtx tmp = out;
15496
15497           if (!sign_bit_compare_p)
15498             {
15499               rtx flags;
15500               bool fpcmp = false;
15501
15502               compare_code = GET_CODE (compare_op);
15503
15504               flags = XEXP (compare_op, 0);
15505
15506               if (GET_MODE (flags) == CCFPmode
15507                   || GET_MODE (flags) == CCFPUmode)
15508                 {
15509                   fpcmp = true;
15510                   compare_code
15511                     = ix86_fp_compare_code_to_integer (compare_code);
15512                 }
15513
15514               /* To simplify rest of code, restrict to the GEU case.  */
15515               if (compare_code == LTU)
15516                 {
15517                   HOST_WIDE_INT tmp = ct;
15518                   ct = cf;
15519                   cf = tmp;
15520                   compare_code = reverse_condition (compare_code);
15521                   code = reverse_condition (code);
15522                 }
15523               else
15524                 {
15525                   if (fpcmp)
15526                     PUT_CODE (compare_op,
15527                               reverse_condition_maybe_unordered
15528                                 (GET_CODE (compare_op)));
15529                   else
15530                     PUT_CODE (compare_op,
15531                               reverse_condition (GET_CODE (compare_op)));
15532                 }
15533               diff = ct - cf;
15534
15535               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
15536                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
15537                 tmp = gen_reg_rtx (mode);
15538
15539               if (mode == DImode)
15540                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
15541               else
15542                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
15543                                                  flags, compare_op));
15544             }
15545           else
15546             {
15547               if (code == GT || code == GE)
15548                 code = reverse_condition (code);
15549               else
15550                 {
15551                   HOST_WIDE_INT tmp = ct;
15552                   ct = cf;
15553                   cf = tmp;
15554                   diff = ct - cf;
15555                 }
15556               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
15557                                      ix86_compare_op1, VOIDmode, 0, -1);
15558             }
15559
15560           if (diff == 1)
15561             {
15562               /*
15563                * cmpl op0,op1
15564                * sbbl dest,dest
15565                * [addl dest, ct]
15566                *
15567                * Size 5 - 8.
15568                */
15569               if (ct)
15570                 tmp = expand_simple_binop (mode, PLUS,
15571                                            tmp, GEN_INT (ct),
15572                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
15573             }
15574           else if (cf == -1)
15575             {
15576               /*
15577                * cmpl op0,op1
15578                * sbbl dest,dest
15579                * orl $ct, dest
15580                *
15581                * Size 8.
15582                */
15583               tmp = expand_simple_binop (mode, IOR,
15584                                          tmp, GEN_INT (ct),
15585                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
15586             }
15587           else if (diff == -1 && ct)
15588             {
15589               /*
15590                * cmpl op0,op1
15591                * sbbl dest,dest
15592                * notl dest
15593                * [addl dest, cf]
15594                *
15595                * Size 8 - 11.
15596                */
15597               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
15598               if (cf)
15599                 tmp = expand_simple_binop (mode, PLUS,
15600                                            copy_rtx (tmp), GEN_INT (cf),
15601                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
15602             }
15603           else
15604             {
15605               /*
15606                * cmpl op0,op1
15607                * sbbl dest,dest
15608                * [notl dest]
15609                * andl cf - ct, dest
15610                * [addl dest, ct]
15611                *
15612                * Size 8 - 11.
15613                */
15614
15615               if (cf == 0)
15616                 {
15617                   cf = ct;
15618                   ct = 0;
15619                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
15620                 }
15621
15622               tmp = expand_simple_binop (mode, AND,
15623                                          copy_rtx (tmp),
15624                                          gen_int_mode (cf - ct, mode),
15625                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
15626               if (ct)
15627                 tmp = expand_simple_binop (mode, PLUS,
15628                                            copy_rtx (tmp), GEN_INT (ct),
15629                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
15630             }
15631
15632           if (!rtx_equal_p (tmp, out))
15633             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
15634
15635           return 1; /* DONE */
15636         }
15637
15638       if (diff < 0)
15639         {
15640           enum machine_mode cmp_mode = GET_MODE (ix86_compare_op0);
15641
15642           HOST_WIDE_INT tmp;
15643           tmp = ct, ct = cf, cf = tmp;
15644           diff = -diff;
15645
15646           if (SCALAR_FLOAT_MODE_P (cmp_mode))
15647             {
15648               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
15649
15650               /* We may be reversing unordered compare to normal compare, that
15651                  is not valid in general (we may convert non-trapping condition
15652                  to trapping one), however on i386 we currently emit all
15653                  comparisons unordered.  */
15654               compare_code = reverse_condition_maybe_unordered (compare_code);
15655               code = reverse_condition_maybe_unordered (code);
15656             }
15657           else
15658             {
15659               compare_code = reverse_condition (compare_code);
15660               code = reverse_condition (code);
15661             }
15662         }
15663
15664       compare_code = UNKNOWN;
15665       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
15666           && CONST_INT_P (ix86_compare_op1))
15667         {
15668           if (ix86_compare_op1 == const0_rtx
15669               && (code == LT || code == GE))
15670             compare_code = code;
15671           else if (ix86_compare_op1 == constm1_rtx)
15672             {
15673               if (code == LE)
15674                 compare_code = LT;
15675               else if (code == GT)
15676                 compare_code = GE;
15677             }
15678         }
15679
15680       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
15681       if (compare_code != UNKNOWN
15682           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
15683           && (cf == -1 || ct == -1))
15684         {
15685           /* If lea code below could be used, only optimize
15686              if it results in a 2 insn sequence.  */
15687
15688           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
15689                  || diff == 3 || diff == 5 || diff == 9)
15690               || (compare_code == LT && ct == -1)
15691               || (compare_code == GE && cf == -1))
15692             {
15693               /*
15694                * notl op1       (if necessary)
15695                * sarl $31, op1
15696                * orl cf, op1
15697                */
15698               if (ct != -1)
15699                 {
15700                   cf = ct;
15701                   ct = -1;
15702                   code = reverse_condition (code);
15703                 }
15704
15705               out = emit_store_flag (out, code, ix86_compare_op0,
15706                                      ix86_compare_op1, VOIDmode, 0, -1);
15707
15708               out = expand_simple_binop (mode, IOR,
15709                                          out, GEN_INT (cf),
15710                                          out, 1, OPTAB_DIRECT);
15711               if (out != operands[0])
15712                 emit_move_insn (operands[0], out);
15713
15714               return 1; /* DONE */
15715             }
15716         }
15717
15718
15719       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
15720            || diff == 3 || diff == 5 || diff == 9)
15721           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
15722           && (mode != DImode
15723               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
15724         {
15725           /*
15726            * xorl dest,dest
15727            * cmpl op1,op2
15728            * setcc dest
15729            * lea cf(dest*(ct-cf)),dest
15730            *
15731            * Size 14.
15732            *
15733            * This also catches the degenerate setcc-only case.
15734            */
15735
15736           rtx tmp;
15737           int nops;
15738
15739           out = emit_store_flag (out, code, ix86_compare_op0,
15740                                  ix86_compare_op1, VOIDmode, 0, 1);
15741
15742           nops = 0;
15743           /* On x86_64 the lea instruction operates on Pmode, so we need
15744              to get arithmetics done in proper mode to match.  */
15745           if (diff == 1)
15746             tmp = copy_rtx (out);
15747           else
15748             {
15749               rtx out1;
15750               out1 = copy_rtx (out);
15751               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
15752               nops++;
15753               if (diff & 1)
15754                 {
15755                   tmp = gen_rtx_PLUS (mode, tmp, out1);
15756                   nops++;
15757                 }
15758             }
15759           if (cf != 0)
15760             {
15761               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
15762               nops++;
15763             }
15764           if (!rtx_equal_p (tmp, out))
15765             {
15766               if (nops == 1)
15767                 out = force_operand (tmp, copy_rtx (out));
15768               else
15769                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
15770             }
15771           if (!rtx_equal_p (out, operands[0]))
15772             emit_move_insn (operands[0], copy_rtx (out));
15773
15774           return 1; /* DONE */
15775         }
15776
15777       /*
15778        * General case:                  Jumpful:
15779        *   xorl dest,dest               cmpl op1, op2
15780        *   cmpl op1, op2                movl ct, dest
15781        *   setcc dest                   jcc 1f
15782        *   decl dest                    movl cf, dest
15783        *   andl (cf-ct),dest            1:
15784        *   addl ct,dest
15785        *
15786        * Size 20.                       Size 14.
15787        *
15788        * This is reasonably steep, but branch mispredict costs are
15789        * high on modern cpus, so consider failing only if optimizing
15790        * for space.
15791        */
15792
15793       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
15794           && BRANCH_COST (optimize_insn_for_speed_p (),
15795                           false) >= 2)
15796         {
15797           if (cf == 0)
15798             {
15799               enum machine_mode cmp_mode = GET_MODE (ix86_compare_op0);
15800
15801               cf = ct;
15802               ct = 0;
15803
15804               if (SCALAR_FLOAT_MODE_P (cmp_mode))
15805                 {
15806                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
15807
15808                   /* We may be reversing unordered compare to normal compare,
15809                      that is not valid in general (we may convert non-trapping
15810                      condition to trapping one), however on i386 we currently
15811                      emit all comparisons unordered.  */
15812                   code = reverse_condition_maybe_unordered (code);
15813                 }
15814               else
15815                 {
15816                   code = reverse_condition (code);
15817                   if (compare_code != UNKNOWN)
15818                     compare_code = reverse_condition (compare_code);
15819                 }
15820             }
15821
15822           if (compare_code != UNKNOWN)
15823             {
15824               /* notl op1       (if needed)
15825                  sarl $31, op1
15826                  andl (cf-ct), op1
15827                  addl ct, op1
15828
15829                  For x < 0 (resp. x <= -1) there will be no notl,
15830                  so if possible swap the constants to get rid of the
15831                  complement.
15832                  True/false will be -1/0 while code below (store flag
15833                  followed by decrement) is 0/-1, so the constants need
15834                  to be exchanged once more.  */
15835
15836               if (compare_code == GE || !cf)
15837                 {
15838                   code = reverse_condition (code);
15839                   compare_code = LT;
15840                 }
15841               else
15842                 {
15843                   HOST_WIDE_INT tmp = cf;
15844                   cf = ct;
15845                   ct = tmp;
15846                 }
15847
15848               out = emit_store_flag (out, code, ix86_compare_op0,
15849                                      ix86_compare_op1, VOIDmode, 0, -1);
15850             }
15851           else
15852             {
15853               out = emit_store_flag (out, code, ix86_compare_op0,
15854                                      ix86_compare_op1, VOIDmode, 0, 1);
15855
15856               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
15857                                          copy_rtx (out), 1, OPTAB_DIRECT);
15858             }
15859
15860           out = expand_simple_binop (mode, AND, copy_rtx (out),
15861                                      gen_int_mode (cf - ct, mode),
15862                                      copy_rtx (out), 1, OPTAB_DIRECT);
15863           if (ct)
15864             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
15865                                        copy_rtx (out), 1, OPTAB_DIRECT);
15866           if (!rtx_equal_p (out, operands[0]))
15867             emit_move_insn (operands[0], copy_rtx (out));
15868
15869           return 1; /* DONE */
15870         }
15871     }
15872
15873   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
15874     {
15875       /* Try a few things more with specific constants and a variable.  */
15876
15877       optab op;
15878       rtx var, orig_out, out, tmp;
15879
15880       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
15881         return 0; /* FAIL */
15882
15883       /* If one of the two operands is an interesting constant, load a
15884          constant with the above and mask it in with a logical operation.  */
15885
15886       if (CONST_INT_P (operands[2]))
15887         {
15888           var = operands[3];
15889           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
15890             operands[3] = constm1_rtx, op = and_optab;
15891           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
15892             operands[3] = const0_rtx, op = ior_optab;
15893           else
15894             return 0; /* FAIL */
15895         }
15896       else if (CONST_INT_P (operands[3]))
15897         {
15898           var = operands[2];
15899           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
15900             operands[2] = constm1_rtx, op = and_optab;
15901           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
15902             operands[2] = const0_rtx, op = ior_optab;
15903           else
15904             return 0; /* FAIL */
15905         }
15906       else
15907         return 0; /* FAIL */
15908
15909       orig_out = operands[0];
15910       tmp = gen_reg_rtx (mode);
15911       operands[0] = tmp;
15912
15913       /* Recurse to get the constant loaded.  */
15914       if (ix86_expand_int_movcc (operands) == 0)
15915         return 0; /* FAIL */
15916
15917       /* Mask in the interesting variable.  */
15918       out = expand_binop (mode, op, var, tmp, orig_out, 0,
15919                           OPTAB_WIDEN);
15920       if (!rtx_equal_p (out, orig_out))
15921         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
15922
15923       return 1; /* DONE */
15924     }
15925
15926   /*
15927    * For comparison with above,
15928    *
15929    * movl cf,dest
15930    * movl ct,tmp
15931    * cmpl op1,op2
15932    * cmovcc tmp,dest
15933    *
15934    * Size 15.
15935    */
15936
15937   if (! nonimmediate_operand (operands[2], mode))
15938     operands[2] = force_reg (mode, operands[2]);
15939   if (! nonimmediate_operand (operands[3], mode))
15940     operands[3] = force_reg (mode, operands[3]);
15941
15942   if (! register_operand (operands[2], VOIDmode)
15943       && (mode == QImode
15944           || ! register_operand (operands[3], VOIDmode)))
15945     operands[2] = force_reg (mode, operands[2]);
15946
15947   if (mode == QImode
15948       && ! register_operand (operands[3], VOIDmode))
15949     operands[3] = force_reg (mode, operands[3]);
15950
15951   emit_insn (compare_seq);
15952   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
15953                           gen_rtx_IF_THEN_ELSE (mode,
15954                                                 compare_op, operands[2],
15955                                                 operands[3])));
15956
15957   return 1; /* DONE */
15958 }
15959
15960 /* Swap, force into registers, or otherwise massage the two operands
15961    to an sse comparison with a mask result.  Thus we differ a bit from
15962    ix86_prepare_fp_compare_args which expects to produce a flags result.
15963
15964    The DEST operand exists to help determine whether to commute commutative
15965    operators.  The POP0/POP1 operands are updated in place.  The new
15966    comparison code is returned, or UNKNOWN if not implementable.  */
15967
15968 static enum rtx_code
15969 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
15970                                   rtx *pop0, rtx *pop1)
15971 {
15972   rtx tmp;
15973
15974   switch (code)
15975     {
15976     case LTGT:
15977     case UNEQ:
15978       /* We have no LTGT as an operator.  We could implement it with
15979          NE & ORDERED, but this requires an extra temporary.  It's
15980          not clear that it's worth it.  */
15981       return UNKNOWN;
15982
15983     case LT:
15984     case LE:
15985     case UNGT:
15986     case UNGE:
15987       /* These are supported directly.  */
15988       break;
15989
15990     case EQ:
15991     case NE:
15992     case UNORDERED:
15993     case ORDERED:
15994       /* For commutative operators, try to canonicalize the destination
15995          operand to be first in the comparison - this helps reload to
15996          avoid extra moves.  */
15997       if (!dest || !rtx_equal_p (dest, *pop1))
15998         break;
15999       /* FALLTHRU */
16000
16001     case GE:
16002     case GT:
16003     case UNLE:
16004     case UNLT:
16005       /* These are not supported directly.  Swap the comparison operands
16006          to transform into something that is supported.  */
16007       tmp = *pop0;
16008       *pop0 = *pop1;
16009       *pop1 = tmp;
16010       code = swap_condition (code);
16011       break;
16012
16013     default:
16014       gcc_unreachable ();
16015     }
16016
16017   return code;
16018 }
16019
16020 /* Detect conditional moves that exactly match min/max operational
16021    semantics.  Note that this is IEEE safe, as long as we don't
16022    interchange the operands.
16023
16024    Returns FALSE if this conditional move doesn't match a MIN/MAX,
16025    and TRUE if the operation is successful and instructions are emitted.  */
16026
16027 static bool
16028 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
16029                            rtx cmp_op1, rtx if_true, rtx if_false)
16030 {
16031   enum machine_mode mode;
16032   bool is_min;
16033   rtx tmp;
16034
16035   if (code == LT)
16036     ;
16037   else if (code == UNGE)
16038     {
16039       tmp = if_true;
16040       if_true = if_false;
16041       if_false = tmp;
16042     }
16043   else
16044     return false;
16045
16046   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
16047     is_min = true;
16048   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
16049     is_min = false;
16050   else
16051     return false;
16052
16053   mode = GET_MODE (dest);
16054
16055   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
16056      but MODE may be a vector mode and thus not appropriate.  */
16057   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
16058     {
16059       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
16060       rtvec v;
16061
16062       if_true = force_reg (mode, if_true);
16063       v = gen_rtvec (2, if_true, if_false);
16064       tmp = gen_rtx_UNSPEC (mode, v, u);
16065     }
16066   else
16067     {
16068       code = is_min ? SMIN : SMAX;
16069       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
16070     }
16071
16072   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
16073   return true;
16074 }
16075
16076 /* Expand an sse vector comparison.  Return the register with the result.  */
16077
16078 static rtx
16079 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
16080                      rtx op_true, rtx op_false)
16081 {
16082   enum machine_mode mode = GET_MODE (dest);
16083   rtx x;
16084
16085   cmp_op0 = force_reg (mode, cmp_op0);
16086   if (!nonimmediate_operand (cmp_op1, mode))
16087     cmp_op1 = force_reg (mode, cmp_op1);
16088
16089   if (optimize
16090       || reg_overlap_mentioned_p (dest, op_true)
16091       || reg_overlap_mentioned_p (dest, op_false))
16092     dest = gen_reg_rtx (mode);
16093
16094   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
16095   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16096
16097   return dest;
16098 }
16099
16100 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
16101    operations.  This is used for both scalar and vector conditional moves.  */
16102
16103 static void
16104 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
16105 {
16106   enum machine_mode mode = GET_MODE (dest);
16107   rtx t2, t3, x;
16108
16109   if (op_false == CONST0_RTX (mode))
16110     {
16111       op_true = force_reg (mode, op_true);
16112       x = gen_rtx_AND (mode, cmp, op_true);
16113       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16114     }
16115   else if (op_true == CONST0_RTX (mode))
16116     {
16117       op_false = force_reg (mode, op_false);
16118       x = gen_rtx_NOT (mode, cmp);
16119       x = gen_rtx_AND (mode, x, op_false);
16120       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16121     }
16122   else if (TARGET_XOP)
16123     {
16124       rtx pcmov = gen_rtx_SET (mode, dest,
16125                                gen_rtx_IF_THEN_ELSE (mode, cmp,
16126                                                      op_true,
16127                                                      op_false));
16128       emit_insn (pcmov);
16129     }
16130   else
16131     {
16132       op_true = force_reg (mode, op_true);
16133       op_false = force_reg (mode, op_false);
16134
16135       t2 = gen_reg_rtx (mode);
16136       if (optimize)
16137         t3 = gen_reg_rtx (mode);
16138       else
16139         t3 = dest;
16140
16141       x = gen_rtx_AND (mode, op_true, cmp);
16142       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
16143
16144       x = gen_rtx_NOT (mode, cmp);
16145       x = gen_rtx_AND (mode, x, op_false);
16146       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
16147
16148       x = gen_rtx_IOR (mode, t3, t2);
16149       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16150     }
16151 }
16152
16153 /* Expand a floating-point conditional move.  Return true if successful.  */
16154
16155 int
16156 ix86_expand_fp_movcc (rtx operands[])
16157 {
16158   enum machine_mode mode = GET_MODE (operands[0]);
16159   enum rtx_code code = GET_CODE (operands[1]);
16160   rtx tmp, compare_op;
16161
16162   ix86_compare_op0 = XEXP (operands[1], 0);
16163   ix86_compare_op1 = XEXP (operands[1], 1);
16164   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
16165     {
16166       enum machine_mode cmode;
16167
16168       /* Since we've no cmove for sse registers, don't force bad register
16169          allocation just to gain access to it.  Deny movcc when the
16170          comparison mode doesn't match the move mode.  */
16171       cmode = GET_MODE (ix86_compare_op0);
16172       if (cmode == VOIDmode)
16173         cmode = GET_MODE (ix86_compare_op1);
16174       if (cmode != mode)
16175         return 0;
16176
16177       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
16178                                                &ix86_compare_op0,
16179                                                &ix86_compare_op1);
16180       if (code == UNKNOWN)
16181         return 0;
16182
16183       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
16184                                      ix86_compare_op1, operands[2],
16185                                      operands[3]))
16186         return 1;
16187
16188       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
16189                                  ix86_compare_op1, operands[2], operands[3]);
16190       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
16191       return 1;
16192     }
16193
16194   /* The floating point conditional move instructions don't directly
16195      support conditions resulting from a signed integer comparison.  */
16196
16197   compare_op = ix86_expand_compare (code);
16198   if (!fcmov_comparison_operator (compare_op, VOIDmode))
16199     {
16200       tmp = gen_reg_rtx (QImode);
16201       ix86_expand_setcc (code, tmp);
16202       code = NE;
16203       ix86_compare_op0 = tmp;
16204       ix86_compare_op1 = const0_rtx;
16205       compare_op = ix86_expand_compare (code);
16206     }
16207
16208   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
16209                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
16210                                                 operands[2], operands[3])));
16211
16212   return 1;
16213 }
16214
16215 /* Expand a floating-point vector conditional move; a vcond operation
16216    rather than a movcc operation.  */
16217
16218 bool
16219 ix86_expand_fp_vcond (rtx operands[])
16220 {
16221   enum rtx_code code = GET_CODE (operands[3]);
16222   rtx cmp;
16223
16224   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
16225                                            &operands[4], &operands[5]);
16226   if (code == UNKNOWN)
16227     return false;
16228
16229   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
16230                                  operands[5], operands[1], operands[2]))
16231     return true;
16232
16233   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
16234                              operands[1], operands[2]);
16235   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
16236   return true;
16237 }
16238
16239 /* Expand a signed/unsigned integral vector conditional move.  */
16240
16241 bool
16242 ix86_expand_int_vcond (rtx operands[])
16243 {
16244   enum machine_mode mode = GET_MODE (operands[0]);
16245   enum rtx_code code = GET_CODE (operands[3]);
16246   bool negate = false;
16247   rtx x, cop0, cop1;
16248
16249   cop0 = operands[4];
16250   cop1 = operands[5];
16251
16252   /* XOP supports all of the comparisons on all vector int types.  */
16253   if (!TARGET_XOP)
16254     {
16255       /* Canonicalize the comparison to EQ, GT, GTU.  */
16256       switch (code)
16257         {
16258         case EQ:
16259         case GT:
16260         case GTU:
16261           break;
16262
16263         case NE:
16264         case LE:
16265         case LEU:
16266           code = reverse_condition (code);
16267           negate = true;
16268           break;
16269
16270         case GE:
16271         case GEU:
16272           code = reverse_condition (code);
16273           negate = true;
16274           /* FALLTHRU */
16275
16276         case LT:
16277         case LTU:
16278           code = swap_condition (code);
16279           x = cop0, cop0 = cop1, cop1 = x;
16280           break;
16281
16282         default:
16283           gcc_unreachable ();
16284         }
16285
16286       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
16287       if (mode == V2DImode)
16288         {
16289           switch (code)
16290             {
16291             case EQ:
16292               /* SSE4.1 supports EQ.  */
16293               if (!TARGET_SSE4_1)
16294                 return false;
16295               break;
16296
16297             case GT:
16298             case GTU:
16299               /* SSE4.2 supports GT/GTU.  */
16300               if (!TARGET_SSE4_2)
16301                 return false;
16302               break;
16303
16304             default:
16305               gcc_unreachable ();
16306             }
16307         }
16308
16309       /* Unsigned parallel compare is not supported by the hardware.
16310          Play some tricks to turn this into a signed comparison
16311          against 0.  */
16312       if (code == GTU)
16313         {
16314           cop0 = force_reg (mode, cop0);
16315
16316           switch (mode)
16317             {
16318             case V4SImode:
16319             case V2DImode:
16320                 {
16321                   rtx t1, t2, mask;
16322                   rtx (*gen_sub3) (rtx, rtx, rtx);
16323
16324                   /* Subtract (-(INT MAX) - 1) from both operands to make
16325                      them signed.  */
16326                   mask = ix86_build_signbit_mask (GET_MODE_INNER (mode),
16327                                                   true, false);
16328                   gen_sub3 = (mode == V4SImode
16329                               ? gen_subv4si3 : gen_subv2di3);
16330                   t1 = gen_reg_rtx (mode);
16331                   emit_insn (gen_sub3 (t1, cop0, mask));
16332
16333                   t2 = gen_reg_rtx (mode);
16334                   emit_insn (gen_sub3 (t2, cop1, mask));
16335
16336                   cop0 = t1;
16337                   cop1 = t2;
16338                   code = GT;
16339                 }
16340               break;
16341
16342             case V16QImode:
16343             case V8HImode:
16344               /* Perform a parallel unsigned saturating subtraction.  */
16345               x = gen_reg_rtx (mode);
16346               emit_insn (gen_rtx_SET (VOIDmode, x,
16347                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
16348
16349               cop0 = x;
16350               cop1 = CONST0_RTX (mode);
16351               code = EQ;
16352               negate = !negate;
16353               break;
16354
16355             default:
16356               gcc_unreachable ();
16357             }
16358         }
16359     }
16360
16361   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
16362                            operands[1+negate], operands[2-negate]);
16363
16364   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
16365                          operands[2-negate]);
16366   return true;
16367 }
16368
16369 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
16370    true if we should do zero extension, else sign extension.  HIGH_P is
16371    true if we want the N/2 high elements, else the low elements.  */
16372
16373 void
16374 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
16375 {
16376   enum machine_mode imode = GET_MODE (operands[1]);
16377   rtx (*unpack)(rtx, rtx, rtx);
16378   rtx se, dest;
16379
16380   switch (imode)
16381     {
16382     case V16QImode:
16383       if (high_p)
16384         unpack = gen_vec_interleave_highv16qi;
16385       else
16386         unpack = gen_vec_interleave_lowv16qi;
16387       break;
16388     case V8HImode:
16389       if (high_p)
16390         unpack = gen_vec_interleave_highv8hi;
16391       else
16392         unpack = gen_vec_interleave_lowv8hi;
16393       break;
16394     case V4SImode:
16395       if (high_p)
16396         unpack = gen_vec_interleave_highv4si;
16397       else
16398         unpack = gen_vec_interleave_lowv4si;
16399       break;
16400     default:
16401       gcc_unreachable ();
16402     }
16403
16404   dest = gen_lowpart (imode, operands[0]);
16405
16406   if (unsigned_p)
16407     se = force_reg (imode, CONST0_RTX (imode));
16408   else
16409     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
16410                               operands[1], pc_rtx, pc_rtx);
16411
16412   emit_insn (unpack (dest, operands[1], se));
16413 }
16414
16415 /* This function performs the same task as ix86_expand_sse_unpack,
16416    but with SSE4.1 instructions.  */
16417
16418 void
16419 ix86_expand_sse4_unpack (rtx operands[2], bool unsigned_p, bool high_p)
16420 {
16421   enum machine_mode imode = GET_MODE (operands[1]);
16422   rtx (*unpack)(rtx, rtx);
16423   rtx src, dest;
16424
16425   switch (imode)
16426     {
16427     case V16QImode:
16428       if (unsigned_p)
16429         unpack = gen_sse4_1_zero_extendv8qiv8hi2;
16430       else
16431         unpack = gen_sse4_1_extendv8qiv8hi2;
16432       break;
16433     case V8HImode:
16434       if (unsigned_p)
16435         unpack = gen_sse4_1_zero_extendv4hiv4si2;
16436       else
16437         unpack = gen_sse4_1_extendv4hiv4si2;
16438       break;
16439     case V4SImode:
16440       if (unsigned_p)
16441         unpack = gen_sse4_1_zero_extendv2siv2di2;
16442       else
16443         unpack = gen_sse4_1_extendv2siv2di2;
16444       break;
16445     default:
16446       gcc_unreachable ();
16447     }
16448
16449   dest = operands[0];
16450   if (high_p)
16451     {
16452       /* Shift higher 8 bytes to lower 8 bytes.  */
16453       src = gen_reg_rtx (imode);
16454       emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, src),
16455                                      gen_lowpart (V1TImode, operands[1]),
16456                                      GEN_INT (64)));
16457     }
16458   else
16459     src = operands[1];
16460
16461   emit_insn (unpack (dest, src));
16462 }
16463
16464 /* Expand conditional increment or decrement using adb/sbb instructions.
16465    The default case using setcc followed by the conditional move can be
16466    done by generic code.  */
16467 int
16468 ix86_expand_int_addcc (rtx operands[])
16469 {
16470   enum rtx_code code = GET_CODE (operands[1]);
16471   rtx flags;
16472   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
16473   rtx compare_op;
16474   rtx val = const0_rtx;
16475   bool fpcmp = false;
16476   enum machine_mode mode;
16477
16478   ix86_compare_op0 = XEXP (operands[1], 0);
16479   ix86_compare_op1 = XEXP (operands[1], 1);
16480   if (operands[3] != const1_rtx
16481       && operands[3] != constm1_rtx)
16482     return 0;
16483   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
16484                                        ix86_compare_op1, &compare_op))
16485      return 0;
16486   code = GET_CODE (compare_op);
16487
16488   flags = XEXP (compare_op, 0);
16489
16490   if (GET_MODE (flags) == CCFPmode
16491       || GET_MODE (flags) == CCFPUmode)
16492     {
16493       fpcmp = true;
16494       code = ix86_fp_compare_code_to_integer (code);
16495     }
16496
16497   if (code != LTU)
16498     {
16499       val = constm1_rtx;
16500       if (fpcmp)
16501         PUT_CODE (compare_op,
16502                   reverse_condition_maybe_unordered
16503                     (GET_CODE (compare_op)));
16504       else
16505         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
16506     }
16507
16508   mode = GET_MODE (operands[0]);
16509
16510   /* Construct either adc or sbb insn.  */
16511   if ((code == LTU) == (operands[3] == constm1_rtx))
16512     {
16513       switch (mode)
16514         {
16515           case QImode:
16516             insn = gen_subqi3_carry;
16517             break;
16518           case HImode:
16519             insn = gen_subhi3_carry;
16520             break;
16521           case SImode:
16522             insn = gen_subsi3_carry;
16523             break;
16524           case DImode:
16525             insn = gen_subdi3_carry;
16526             break;
16527           default:
16528             gcc_unreachable ();
16529         }
16530     }
16531   else
16532     {
16533       switch (mode)
16534         {
16535           case QImode:
16536             insn = gen_addqi3_carry;
16537             break;
16538           case HImode:
16539             insn = gen_addhi3_carry;
16540             break;
16541           case SImode:
16542             insn = gen_addsi3_carry;
16543             break;
16544           case DImode:
16545             insn = gen_adddi3_carry;
16546             break;
16547           default:
16548             gcc_unreachable ();
16549         }
16550     }
16551   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
16552
16553   return 1; /* DONE */
16554 }
16555
16556
16557 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
16558    works for floating pointer parameters and nonoffsetable memories.
16559    For pushes, it returns just stack offsets; the values will be saved
16560    in the right order.  Maximally three parts are generated.  */
16561
16562 static int
16563 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
16564 {
16565   int size;
16566
16567   if (!TARGET_64BIT)
16568     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
16569   else
16570     size = (GET_MODE_SIZE (mode) + 4) / 8;
16571
16572   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
16573   gcc_assert (size >= 2 && size <= 4);
16574
16575   /* Optimize constant pool reference to immediates.  This is used by fp
16576      moves, that force all constants to memory to allow combining.  */
16577   if (MEM_P (operand) && MEM_READONLY_P (operand))
16578     {
16579       rtx tmp = maybe_get_pool_constant (operand);
16580       if (tmp)
16581         operand = tmp;
16582     }
16583
16584   if (MEM_P (operand) && !offsettable_memref_p (operand))
16585     {
16586       /* The only non-offsetable memories we handle are pushes.  */
16587       int ok = push_operand (operand, VOIDmode);
16588
16589       gcc_assert (ok);
16590
16591       operand = copy_rtx (operand);
16592       PUT_MODE (operand, Pmode);
16593       parts[0] = parts[1] = parts[2] = parts[3] = operand;
16594       return size;
16595     }
16596
16597   if (GET_CODE (operand) == CONST_VECTOR)
16598     {
16599       enum machine_mode imode = int_mode_for_mode (mode);
16600       /* Caution: if we looked through a constant pool memory above,
16601          the operand may actually have a different mode now.  That's
16602          ok, since we want to pun this all the way back to an integer.  */
16603       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
16604       gcc_assert (operand != NULL);
16605       mode = imode;
16606     }
16607
16608   if (!TARGET_64BIT)
16609     {
16610       if (mode == DImode)
16611         split_di (&operand, 1, &parts[0], &parts[1]);
16612       else
16613         {
16614           int i;
16615
16616           if (REG_P (operand))
16617             {
16618               gcc_assert (reload_completed);
16619               for (i = 0; i < size; i++)
16620                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
16621             }
16622           else if (offsettable_memref_p (operand))
16623             {
16624               operand = adjust_address (operand, SImode, 0);
16625               parts[0] = operand;
16626               for (i = 1; i < size; i++)
16627                 parts[i] = adjust_address (operand, SImode, 4 * i);
16628             }
16629           else if (GET_CODE (operand) == CONST_DOUBLE)
16630             {
16631               REAL_VALUE_TYPE r;
16632               long l[4];
16633
16634               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
16635               switch (mode)
16636                 {
16637                 case TFmode:
16638                   real_to_target (l, &r, mode);
16639                   parts[3] = gen_int_mode (l[3], SImode);
16640                   parts[2] = gen_int_mode (l[2], SImode);
16641                   break;
16642                 case XFmode:
16643                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
16644                   parts[2] = gen_int_mode (l[2], SImode);
16645                   break;
16646                 case DFmode:
16647                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
16648                   break;
16649                 default:
16650                   gcc_unreachable ();
16651                 }
16652               parts[1] = gen_int_mode (l[1], SImode);
16653               parts[0] = gen_int_mode (l[0], SImode);
16654             }
16655           else
16656             gcc_unreachable ();
16657         }
16658     }
16659   else
16660     {
16661       if (mode == TImode)
16662         split_ti (&operand, 1, &parts[0], &parts[1]);
16663       if (mode == XFmode || mode == TFmode)
16664         {
16665           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
16666           if (REG_P (operand))
16667             {
16668               gcc_assert (reload_completed);
16669               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
16670               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
16671             }
16672           else if (offsettable_memref_p (operand))
16673             {
16674               operand = adjust_address (operand, DImode, 0);
16675               parts[0] = operand;
16676               parts[1] = adjust_address (operand, upper_mode, 8);
16677             }
16678           else if (GET_CODE (operand) == CONST_DOUBLE)
16679             {
16680               REAL_VALUE_TYPE r;
16681               long l[4];
16682
16683               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
16684               real_to_target (l, &r, mode);
16685
16686               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
16687               if (HOST_BITS_PER_WIDE_INT >= 64)
16688                 parts[0]
16689                   = gen_int_mode
16690                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
16691                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
16692                        DImode);
16693               else
16694                 parts[0] = immed_double_const (l[0], l[1], DImode);
16695
16696               if (upper_mode == SImode)
16697                 parts[1] = gen_int_mode (l[2], SImode);
16698               else if (HOST_BITS_PER_WIDE_INT >= 64)
16699                 parts[1]
16700                   = gen_int_mode
16701                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
16702                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
16703                        DImode);
16704               else
16705                 parts[1] = immed_double_const (l[2], l[3], DImode);
16706             }
16707           else
16708             gcc_unreachable ();
16709         }
16710     }
16711
16712   return size;
16713 }
16714
16715 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
16716    Return false when normal moves are needed; true when all required
16717    insns have been emitted.  Operands 2-4 contain the input values
16718    int the correct order; operands 5-7 contain the output values.  */
16719
16720 void
16721 ix86_split_long_move (rtx operands[])
16722 {
16723   rtx part[2][4];
16724   int nparts, i, j;
16725   int push = 0;
16726   int collisions = 0;
16727   enum machine_mode mode = GET_MODE (operands[0]);
16728   bool collisionparts[4];
16729
16730   /* The DFmode expanders may ask us to move double.
16731      For 64bit target this is single move.  By hiding the fact
16732      here we simplify i386.md splitters.  */
16733   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
16734     {
16735       /* Optimize constant pool reference to immediates.  This is used by
16736          fp moves, that force all constants to memory to allow combining.  */
16737
16738       if (MEM_P (operands[1])
16739           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
16740           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
16741         operands[1] = get_pool_constant (XEXP (operands[1], 0));
16742       if (push_operand (operands[0], VOIDmode))
16743         {
16744           operands[0] = copy_rtx (operands[0]);
16745           PUT_MODE (operands[0], Pmode);
16746         }
16747       else
16748         operands[0] = gen_lowpart (DImode, operands[0]);
16749       operands[1] = gen_lowpart (DImode, operands[1]);
16750       emit_move_insn (operands[0], operands[1]);
16751       return;
16752     }
16753
16754   /* The only non-offsettable memory we handle is push.  */
16755   if (push_operand (operands[0], VOIDmode))
16756     push = 1;
16757   else
16758     gcc_assert (!MEM_P (operands[0])
16759                 || offsettable_memref_p (operands[0]));
16760
16761   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
16762   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
16763
16764   /* When emitting push, take care for source operands on the stack.  */
16765   if (push && MEM_P (operands[1])
16766       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
16767     {
16768       rtx src_base = XEXP (part[1][nparts - 1], 0);
16769
16770       /* Compensate for the stack decrement by 4.  */
16771       if (!TARGET_64BIT && nparts == 3
16772           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
16773         src_base = plus_constant (src_base, 4);
16774
16775       /* src_base refers to the stack pointer and is
16776          automatically decreased by emitted push.  */
16777       for (i = 0; i < nparts; i++)
16778         part[1][i] = change_address (part[1][i],
16779                                      GET_MODE (part[1][i]), src_base);
16780     }
16781
16782   /* We need to do copy in the right order in case an address register
16783      of the source overlaps the destination.  */
16784   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
16785     {
16786       rtx tmp;
16787
16788       for (i = 0; i < nparts; i++)
16789         {
16790           collisionparts[i]
16791             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
16792           if (collisionparts[i])
16793             collisions++;
16794         }
16795
16796       /* Collision in the middle part can be handled by reordering.  */
16797       if (collisions == 1 && nparts == 3 && collisionparts [1])
16798         {
16799           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
16800           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
16801         }
16802       else if (collisions == 1
16803                && nparts == 4
16804                && (collisionparts [1] || collisionparts [2]))
16805         {
16806           if (collisionparts [1])
16807             {
16808               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
16809               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
16810             }
16811           else
16812             {
16813               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
16814               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
16815             }
16816         }
16817
16818       /* If there are more collisions, we can't handle it by reordering.
16819          Do an lea to the last part and use only one colliding move.  */
16820       else if (collisions > 1)
16821         {
16822           rtx base;
16823
16824           collisions = 1;
16825
16826           base = part[0][nparts - 1];
16827
16828           /* Handle the case when the last part isn't valid for lea.
16829              Happens in 64-bit mode storing the 12-byte XFmode.  */
16830           if (GET_MODE (base) != Pmode)
16831             base = gen_rtx_REG (Pmode, REGNO (base));
16832
16833           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
16834           part[1][0] = replace_equiv_address (part[1][0], base);
16835           for (i = 1; i < nparts; i++)
16836             {
16837               tmp = plus_constant (base, UNITS_PER_WORD * i);
16838               part[1][i] = replace_equiv_address (part[1][i], tmp);
16839             }
16840         }
16841     }
16842
16843   if (push)
16844     {
16845       if (!TARGET_64BIT)
16846         {
16847           if (nparts == 3)
16848             {
16849               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
16850                 emit_insn (gen_addsi3 (stack_pointer_rtx,
16851                                        stack_pointer_rtx, GEN_INT (-4)));
16852               emit_move_insn (part[0][2], part[1][2]);
16853             }
16854           else if (nparts == 4)
16855             {
16856               emit_move_insn (part[0][3], part[1][3]);
16857               emit_move_insn (part[0][2], part[1][2]);
16858             }
16859         }
16860       else
16861         {
16862           /* In 64bit mode we don't have 32bit push available.  In case this is
16863              register, it is OK - we will just use larger counterpart.  We also
16864              retype memory - these comes from attempt to avoid REX prefix on
16865              moving of second half of TFmode value.  */
16866           if (GET_MODE (part[1][1]) == SImode)
16867             {
16868               switch (GET_CODE (part[1][1]))
16869                 {
16870                 case MEM:
16871                   part[1][1] = adjust_address (part[1][1], DImode, 0);
16872                   break;
16873
16874                 case REG:
16875                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
16876                   break;
16877
16878                 default:
16879                   gcc_unreachable ();
16880                 }
16881
16882               if (GET_MODE (part[1][0]) == SImode)
16883                 part[1][0] = part[1][1];
16884             }
16885         }
16886       emit_move_insn (part[0][1], part[1][1]);
16887       emit_move_insn (part[0][0], part[1][0]);
16888       return;
16889     }
16890
16891   /* Choose correct order to not overwrite the source before it is copied.  */
16892   if ((REG_P (part[0][0])
16893        && REG_P (part[1][1])
16894        && (REGNO (part[0][0]) == REGNO (part[1][1])
16895            || (nparts == 3
16896                && REGNO (part[0][0]) == REGNO (part[1][2]))
16897            || (nparts == 4
16898                && REGNO (part[0][0]) == REGNO (part[1][3]))))
16899       || (collisions > 0
16900           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
16901     {
16902       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
16903         {
16904           operands[2 + i] = part[0][j];
16905           operands[6 + i] = part[1][j];
16906         }
16907     }
16908   else
16909     {
16910       for (i = 0; i < nparts; i++)
16911         {
16912           operands[2 + i] = part[0][i];
16913           operands[6 + i] = part[1][i];
16914         }
16915     }
16916
16917   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
16918   if (optimize_insn_for_size_p ())
16919     {
16920       for (j = 0; j < nparts - 1; j++)
16921         if (CONST_INT_P (operands[6 + j])
16922             && operands[6 + j] != const0_rtx
16923             && REG_P (operands[2 + j]))
16924           for (i = j; i < nparts - 1; i++)
16925             if (CONST_INT_P (operands[7 + i])
16926                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
16927               operands[7 + i] = operands[2 + j];
16928     }
16929
16930   for (i = 0; i < nparts; i++)
16931     emit_move_insn (operands[2 + i], operands[6 + i]);
16932
16933   return;
16934 }
16935
16936 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
16937    left shift by a constant, either using a single shift or
16938    a sequence of add instructions.  */
16939
16940 static void
16941 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
16942 {
16943   if (count == 1)
16944     {
16945       emit_insn ((mode == DImode
16946                   ? gen_addsi3
16947                   : gen_adddi3) (operand, operand, operand));
16948     }
16949   else if (!optimize_insn_for_size_p ()
16950            && count * ix86_cost->add <= ix86_cost->shift_const)
16951     {
16952       int i;
16953       for (i=0; i<count; i++)
16954         {
16955           emit_insn ((mode == DImode
16956                       ? gen_addsi3
16957                       : gen_adddi3) (operand, operand, operand));
16958         }
16959     }
16960   else
16961     emit_insn ((mode == DImode
16962                 ? gen_ashlsi3
16963                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
16964 }
16965
16966 void
16967 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
16968 {
16969   rtx low[2], high[2];
16970   int count;
16971   const int single_width = mode == DImode ? 32 : 64;
16972
16973   if (CONST_INT_P (operands[2]))
16974     {
16975       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
16976       count = INTVAL (operands[2]) & (single_width * 2 - 1);
16977
16978       if (count >= single_width)
16979         {
16980           emit_move_insn (high[0], low[1]);
16981           emit_move_insn (low[0], const0_rtx);
16982
16983           if (count > single_width)
16984             ix86_expand_ashl_const (high[0], count - single_width, mode);
16985         }
16986       else
16987         {
16988           if (!rtx_equal_p (operands[0], operands[1]))
16989             emit_move_insn (operands[0], operands[1]);
16990           emit_insn ((mode == DImode
16991                      ? gen_x86_shld
16992                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
16993           ix86_expand_ashl_const (low[0], count, mode);
16994         }
16995       return;
16996     }
16997
16998   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
16999
17000   if (operands[1] == const1_rtx)
17001     {
17002       /* Assuming we've chosen a QImode capable registers, then 1 << N
17003          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
17004       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
17005         {
17006           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
17007
17008           ix86_expand_clear (low[0]);
17009           ix86_expand_clear (high[0]);
17010           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
17011
17012           d = gen_lowpart (QImode, low[0]);
17013           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
17014           s = gen_rtx_EQ (QImode, flags, const0_rtx);
17015           emit_insn (gen_rtx_SET (VOIDmode, d, s));
17016
17017           d = gen_lowpart (QImode, high[0]);
17018           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
17019           s = gen_rtx_NE (QImode, flags, const0_rtx);
17020           emit_insn (gen_rtx_SET (VOIDmode, d, s));
17021         }
17022
17023       /* Otherwise, we can get the same results by manually performing
17024          a bit extract operation on bit 5/6, and then performing the two
17025          shifts.  The two methods of getting 0/1 into low/high are exactly
17026          the same size.  Avoiding the shift in the bit extract case helps
17027          pentium4 a bit; no one else seems to care much either way.  */
17028       else
17029         {
17030           rtx x;
17031
17032           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
17033             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
17034           else
17035             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
17036           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
17037
17038           emit_insn ((mode == DImode
17039                       ? gen_lshrsi3
17040                       : gen_lshrdi3) (high[0], high[0],
17041                                       GEN_INT (mode == DImode ? 5 : 6)));
17042           emit_insn ((mode == DImode
17043                       ? gen_andsi3
17044                       : gen_anddi3) (high[0], high[0], const1_rtx));
17045           emit_move_insn (low[0], high[0]);
17046           emit_insn ((mode == DImode
17047                       ? gen_xorsi3
17048                       : gen_xordi3) (low[0], low[0], const1_rtx));
17049         }
17050
17051       emit_insn ((mode == DImode
17052                     ? gen_ashlsi3
17053                     : gen_ashldi3) (low[0], low[0], operands[2]));
17054       emit_insn ((mode == DImode
17055                     ? gen_ashlsi3
17056                     : gen_ashldi3) (high[0], high[0], operands[2]));
17057       return;
17058     }
17059
17060   if (operands[1] == constm1_rtx)
17061     {
17062       /* For -1 << N, we can avoid the shld instruction, because we
17063          know that we're shifting 0...31/63 ones into a -1.  */
17064       emit_move_insn (low[0], constm1_rtx);
17065       if (optimize_insn_for_size_p ())
17066         emit_move_insn (high[0], low[0]);
17067       else
17068         emit_move_insn (high[0], constm1_rtx);
17069     }
17070   else
17071     {
17072       if (!rtx_equal_p (operands[0], operands[1]))
17073         emit_move_insn (operands[0], operands[1]);
17074
17075       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
17076       emit_insn ((mode == DImode
17077                   ? gen_x86_shld
17078                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
17079     }
17080
17081   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
17082
17083   if (TARGET_CMOVE && scratch)
17084     {
17085       ix86_expand_clear (scratch);
17086       emit_insn ((mode == DImode
17087                   ? gen_x86_shift_adj_1
17088                   : gen_x86_64_shift_adj_1) (high[0], low[0], operands[2],
17089                                              scratch));
17090     }
17091   else
17092     emit_insn ((mode == DImode
17093                 ? gen_x86_shift_adj_2
17094                 : gen_x86_64_shift_adj_2) (high[0], low[0], operands[2]));
17095 }
17096
17097 void
17098 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
17099 {
17100   rtx low[2], high[2];
17101   int count;
17102   const int single_width = mode == DImode ? 32 : 64;
17103
17104   if (CONST_INT_P (operands[2]))
17105     {
17106       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
17107       count = INTVAL (operands[2]) & (single_width * 2 - 1);
17108
17109       if (count == single_width * 2 - 1)
17110         {
17111           emit_move_insn (high[0], high[1]);
17112           emit_insn ((mode == DImode
17113                       ? gen_ashrsi3
17114                       : gen_ashrdi3) (high[0], high[0],
17115                                       GEN_INT (single_width - 1)));
17116           emit_move_insn (low[0], high[0]);
17117
17118         }
17119       else if (count >= single_width)
17120         {
17121           emit_move_insn (low[0], high[1]);
17122           emit_move_insn (high[0], low[0]);
17123           emit_insn ((mode == DImode
17124                       ? gen_ashrsi3
17125                       : gen_ashrdi3) (high[0], high[0],
17126                                       GEN_INT (single_width - 1)));
17127           if (count > single_width)
17128             emit_insn ((mode == DImode
17129                         ? gen_ashrsi3
17130                         : gen_ashrdi3) (low[0], low[0],
17131                                         GEN_INT (count - single_width)));
17132         }
17133       else
17134         {
17135           if (!rtx_equal_p (operands[0], operands[1]))
17136             emit_move_insn (operands[0], operands[1]);
17137           emit_insn ((mode == DImode
17138                       ? gen_x86_shrd
17139                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
17140           emit_insn ((mode == DImode
17141                       ? gen_ashrsi3
17142                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
17143         }
17144     }
17145   else
17146     {
17147       if (!rtx_equal_p (operands[0], operands[1]))
17148         emit_move_insn (operands[0], operands[1]);
17149
17150       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
17151
17152       emit_insn ((mode == DImode
17153                   ? gen_x86_shrd
17154                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
17155       emit_insn ((mode == DImode
17156                   ? gen_ashrsi3
17157                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
17158
17159       if (TARGET_CMOVE && scratch)
17160         {
17161           emit_move_insn (scratch, high[0]);
17162           emit_insn ((mode == DImode
17163                       ? gen_ashrsi3
17164                       : gen_ashrdi3) (scratch, scratch,
17165                                       GEN_INT (single_width - 1)));
17166           emit_insn ((mode == DImode
17167                       ? gen_x86_shift_adj_1
17168                       : gen_x86_64_shift_adj_1) (low[0], high[0], operands[2],
17169                                                  scratch));
17170         }
17171       else
17172         emit_insn ((mode == DImode
17173                     ? gen_x86_shift_adj_3
17174                     : gen_x86_64_shift_adj_3) (low[0], high[0], operands[2]));
17175     }
17176 }
17177
17178 void
17179 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
17180 {
17181   rtx low[2], high[2];
17182   int count;
17183   const int single_width = mode == DImode ? 32 : 64;
17184
17185   if (CONST_INT_P (operands[2]))
17186     {
17187       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
17188       count = INTVAL (operands[2]) & (single_width * 2 - 1);
17189
17190       if (count >= single_width)
17191         {
17192           emit_move_insn (low[0], high[1]);
17193           ix86_expand_clear (high[0]);
17194
17195           if (count > single_width)
17196             emit_insn ((mode == DImode
17197                         ? gen_lshrsi3
17198                         : gen_lshrdi3) (low[0], low[0],
17199                                         GEN_INT (count - single_width)));
17200         }
17201       else
17202         {
17203           if (!rtx_equal_p (operands[0], operands[1]))
17204             emit_move_insn (operands[0], operands[1]);
17205           emit_insn ((mode == DImode
17206                       ? gen_x86_shrd
17207                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
17208           emit_insn ((mode == DImode
17209                       ? gen_lshrsi3
17210                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
17211         }
17212     }
17213   else
17214     {
17215       if (!rtx_equal_p (operands[0], operands[1]))
17216         emit_move_insn (operands[0], operands[1]);
17217
17218       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
17219
17220       emit_insn ((mode == DImode
17221                   ? gen_x86_shrd
17222                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
17223       emit_insn ((mode == DImode
17224                   ? gen_lshrsi3
17225                   : gen_lshrdi3) (high[0], high[0], operands[2]));
17226
17227       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
17228       if (TARGET_CMOVE && scratch)
17229         {
17230           ix86_expand_clear (scratch);
17231           emit_insn ((mode == DImode
17232                       ? gen_x86_shift_adj_1
17233                       : gen_x86_64_shift_adj_1) (low[0], high[0], operands[2],
17234                                                  scratch));
17235         }
17236       else
17237         emit_insn ((mode == DImode
17238                     ? gen_x86_shift_adj_2
17239                     : gen_x86_64_shift_adj_2) (low[0], high[0], operands[2]));
17240     }
17241 }
17242
17243 /* Predict just emitted jump instruction to be taken with probability PROB.  */
17244 static void
17245 predict_jump (int prob)
17246 {
17247   rtx insn = get_last_insn ();
17248   gcc_assert (JUMP_P (insn));
17249   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
17250 }
17251
17252 /* Helper function for the string operations below.  Dest VARIABLE whether
17253    it is aligned to VALUE bytes.  If true, jump to the label.  */
17254 static rtx
17255 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
17256 {
17257   rtx label = gen_label_rtx ();
17258   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
17259   if (GET_MODE (variable) == DImode)
17260     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
17261   else
17262     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
17263   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
17264                            1, label);
17265   if (epilogue)
17266     predict_jump (REG_BR_PROB_BASE * 50 / 100);
17267   else
17268     predict_jump (REG_BR_PROB_BASE * 90 / 100);
17269   return label;
17270 }
17271
17272 /* Adjust COUNTER by the VALUE.  */
17273 static void
17274 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
17275 {
17276   if (GET_MODE (countreg) == DImode)
17277     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
17278   else
17279     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
17280 }
17281
17282 /* Zero extend possibly SImode EXP to Pmode register.  */
17283 rtx
17284 ix86_zero_extend_to_Pmode (rtx exp)
17285 {
17286   rtx r;
17287   if (GET_MODE (exp) == VOIDmode)
17288     return force_reg (Pmode, exp);
17289   if (GET_MODE (exp) == Pmode)
17290     return copy_to_mode_reg (Pmode, exp);
17291   r = gen_reg_rtx (Pmode);
17292   emit_insn (gen_zero_extendsidi2 (r, exp));
17293   return r;
17294 }
17295
17296 /* Divide COUNTREG by SCALE.  */
17297 static rtx
17298 scale_counter (rtx countreg, int scale)
17299 {
17300   rtx sc;
17301
17302   if (scale == 1)
17303     return countreg;
17304   if (CONST_INT_P (countreg))
17305     return GEN_INT (INTVAL (countreg) / scale);
17306   gcc_assert (REG_P (countreg));
17307
17308   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
17309                             GEN_INT (exact_log2 (scale)),
17310                             NULL, 1, OPTAB_DIRECT);
17311   return sc;
17312 }
17313
17314 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
17315    DImode for constant loop counts.  */
17316
17317 static enum machine_mode
17318 counter_mode (rtx count_exp)
17319 {
17320   if (GET_MODE (count_exp) != VOIDmode)
17321     return GET_MODE (count_exp);
17322   if (!CONST_INT_P (count_exp))
17323     return Pmode;
17324   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
17325     return DImode;
17326   return SImode;
17327 }
17328
17329 /* When SRCPTR is non-NULL, output simple loop to move memory
17330    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
17331    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
17332    equivalent loop to set memory by VALUE (supposed to be in MODE).
17333
17334    The size is rounded down to whole number of chunk size moved at once.
17335    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
17336
17337
17338 static void
17339 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
17340                                rtx destptr, rtx srcptr, rtx value,
17341                                rtx count, enum machine_mode mode, int unroll,
17342                                int expected_size)
17343 {
17344   rtx out_label, top_label, iter, tmp;
17345   enum machine_mode iter_mode = counter_mode (count);
17346   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
17347   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
17348   rtx size;
17349   rtx x_addr;
17350   rtx y_addr;
17351   int i;
17352
17353   top_label = gen_label_rtx ();
17354   out_label = gen_label_rtx ();
17355   iter = gen_reg_rtx (iter_mode);
17356
17357   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
17358                               NULL, 1, OPTAB_DIRECT);
17359   /* Those two should combine.  */
17360   if (piece_size == const1_rtx)
17361     {
17362       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
17363                                true, out_label);
17364       predict_jump (REG_BR_PROB_BASE * 10 / 100);
17365     }
17366   emit_move_insn (iter, const0_rtx);
17367
17368   emit_label (top_label);
17369
17370   tmp = convert_modes (Pmode, iter_mode, iter, true);
17371   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
17372   destmem = change_address (destmem, mode, x_addr);
17373
17374   if (srcmem)
17375     {
17376       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
17377       srcmem = change_address (srcmem, mode, y_addr);
17378
17379       /* When unrolling for chips that reorder memory reads and writes,
17380          we can save registers by using single temporary.
17381          Also using 4 temporaries is overkill in 32bit mode.  */
17382       if (!TARGET_64BIT && 0)
17383         {
17384           for (i = 0; i < unroll; i++)
17385             {
17386               if (i)
17387                 {
17388                   destmem =
17389                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
17390                   srcmem =
17391                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
17392                 }
17393               emit_move_insn (destmem, srcmem);
17394             }
17395         }
17396       else
17397         {
17398           rtx tmpreg[4];
17399           gcc_assert (unroll <= 4);
17400           for (i = 0; i < unroll; i++)
17401             {
17402               tmpreg[i] = gen_reg_rtx (mode);
17403               if (i)
17404                 {
17405                   srcmem =
17406                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
17407                 }
17408               emit_move_insn (tmpreg[i], srcmem);
17409             }
17410           for (i = 0; i < unroll; i++)
17411             {
17412               if (i)
17413                 {
17414                   destmem =
17415                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
17416                 }
17417               emit_move_insn (destmem, tmpreg[i]);
17418             }
17419         }
17420     }
17421   else
17422     for (i = 0; i < unroll; i++)
17423       {
17424         if (i)
17425           destmem =
17426             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
17427         emit_move_insn (destmem, value);
17428       }
17429
17430   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
17431                              true, OPTAB_LIB_WIDEN);
17432   if (tmp != iter)
17433     emit_move_insn (iter, tmp);
17434
17435   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
17436                            true, top_label);
17437   if (expected_size != -1)
17438     {
17439       expected_size /= GET_MODE_SIZE (mode) * unroll;
17440       if (expected_size == 0)
17441         predict_jump (0);
17442       else if (expected_size > REG_BR_PROB_BASE)
17443         predict_jump (REG_BR_PROB_BASE - 1);
17444       else
17445         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
17446     }
17447   else
17448     predict_jump (REG_BR_PROB_BASE * 80 / 100);
17449   iter = ix86_zero_extend_to_Pmode (iter);
17450   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
17451                              true, OPTAB_LIB_WIDEN);
17452   if (tmp != destptr)
17453     emit_move_insn (destptr, tmp);
17454   if (srcptr)
17455     {
17456       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
17457                                  true, OPTAB_LIB_WIDEN);
17458       if (tmp != srcptr)
17459         emit_move_insn (srcptr, tmp);
17460     }
17461   emit_label (out_label);
17462 }
17463
17464 /* Output "rep; mov" instruction.
17465    Arguments have same meaning as for previous function */
17466 static void
17467 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
17468                            rtx destptr, rtx srcptr,
17469                            rtx count,
17470                            enum machine_mode mode)
17471 {
17472   rtx destexp;
17473   rtx srcexp;
17474   rtx countreg;
17475
17476   /* If the size is known, it is shorter to use rep movs.  */
17477   if (mode == QImode && CONST_INT_P (count)
17478       && !(INTVAL (count) & 3))
17479     mode = SImode;
17480
17481   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
17482     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
17483   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
17484     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
17485   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
17486   if (mode != QImode)
17487     {
17488       destexp = gen_rtx_ASHIFT (Pmode, countreg,
17489                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
17490       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
17491       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
17492                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
17493       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
17494     }
17495   else
17496     {
17497       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
17498       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
17499     }
17500   if (CONST_INT_P (count))
17501     {
17502       count = GEN_INT (INTVAL (count)
17503                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
17504       destmem = shallow_copy_rtx (destmem);
17505       srcmem = shallow_copy_rtx (srcmem);
17506       set_mem_size (destmem, count);
17507       set_mem_size (srcmem, count);
17508     }
17509   else
17510     {
17511       if (MEM_SIZE (destmem))
17512         set_mem_size (destmem, NULL_RTX);
17513       if (MEM_SIZE (srcmem))
17514         set_mem_size (srcmem, NULL_RTX);
17515     }
17516   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
17517                           destexp, srcexp));
17518 }
17519
17520 /* Output "rep; stos" instruction.
17521    Arguments have same meaning as for previous function */
17522 static void
17523 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
17524                             rtx count, enum machine_mode mode,
17525                             rtx orig_value)
17526 {
17527   rtx destexp;
17528   rtx countreg;
17529
17530   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
17531     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
17532   value = force_reg (mode, gen_lowpart (mode, value));
17533   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
17534   if (mode != QImode)
17535     {
17536       destexp = gen_rtx_ASHIFT (Pmode, countreg,
17537                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
17538       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
17539     }
17540   else
17541     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
17542   if (orig_value == const0_rtx && CONST_INT_P (count))
17543     {
17544       count = GEN_INT (INTVAL (count)
17545                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
17546       destmem = shallow_copy_rtx (destmem);
17547       set_mem_size (destmem, count);
17548     }
17549   else if (MEM_SIZE (destmem))
17550     set_mem_size (destmem, NULL_RTX);
17551   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
17552 }
17553
17554 static void
17555 emit_strmov (rtx destmem, rtx srcmem,
17556              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
17557 {
17558   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
17559   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
17560   emit_insn (gen_strmov (destptr, dest, srcptr, src));
17561 }
17562
17563 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
17564 static void
17565 expand_movmem_epilogue (rtx destmem, rtx srcmem,
17566                         rtx destptr, rtx srcptr, rtx count, int max_size)
17567 {
17568   rtx src, dest;
17569   if (CONST_INT_P (count))
17570     {
17571       HOST_WIDE_INT countval = INTVAL (count);
17572       int offset = 0;
17573
17574       if ((countval & 0x10) && max_size > 16)
17575         {
17576           if (TARGET_64BIT)
17577             {
17578               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
17579               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
17580             }
17581           else
17582             gcc_unreachable ();
17583           offset += 16;
17584         }
17585       if ((countval & 0x08) && max_size > 8)
17586         {
17587           if (TARGET_64BIT)
17588             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
17589           else
17590             {
17591               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
17592               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
17593             }
17594           offset += 8;
17595         }
17596       if ((countval & 0x04) && max_size > 4)
17597         {
17598           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
17599           offset += 4;
17600         }
17601       if ((countval & 0x02) && max_size > 2)
17602         {
17603           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
17604           offset += 2;
17605         }
17606       if ((countval & 0x01) && max_size > 1)
17607         {
17608           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
17609           offset += 1;
17610         }
17611       return;
17612     }
17613   if (max_size > 8)
17614     {
17615       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
17616                                     count, 1, OPTAB_DIRECT);
17617       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
17618                                      count, QImode, 1, 4);
17619       return;
17620     }
17621
17622   /* When there are stringops, we can cheaply increase dest and src pointers.
17623      Otherwise we save code size by maintaining offset (zero is readily
17624      available from preceding rep operation) and using x86 addressing modes.
17625    */
17626   if (TARGET_SINGLE_STRINGOP)
17627     {
17628       if (max_size > 4)
17629         {
17630           rtx label = ix86_expand_aligntest (count, 4, true);
17631           src = change_address (srcmem, SImode, srcptr);
17632           dest = change_address (destmem, SImode, destptr);
17633           emit_insn (gen_strmov (destptr, dest, srcptr, src));
17634           emit_label (label);
17635           LABEL_NUSES (label) = 1;
17636         }
17637       if (max_size > 2)
17638         {
17639           rtx label = ix86_expand_aligntest (count, 2, true);
17640           src = change_address (srcmem, HImode, srcptr);
17641           dest = change_address (destmem, HImode, destptr);
17642           emit_insn (gen_strmov (destptr, dest, srcptr, src));
17643           emit_label (label);
17644           LABEL_NUSES (label) = 1;
17645         }
17646       if (max_size > 1)
17647         {
17648           rtx label = ix86_expand_aligntest (count, 1, true);
17649           src = change_address (srcmem, QImode, srcptr);
17650           dest = change_address (destmem, QImode, destptr);
17651           emit_insn (gen_strmov (destptr, dest, srcptr, src));
17652           emit_label (label);
17653           LABEL_NUSES (label) = 1;
17654         }
17655     }
17656   else
17657     {
17658       rtx offset = force_reg (Pmode, const0_rtx);
17659       rtx tmp;
17660
17661       if (max_size > 4)
17662         {
17663           rtx label = ix86_expand_aligntest (count, 4, true);
17664           src = change_address (srcmem, SImode, srcptr);
17665           dest = change_address (destmem, SImode, destptr);
17666           emit_move_insn (dest, src);
17667           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
17668                                      true, OPTAB_LIB_WIDEN);
17669           if (tmp != offset)
17670             emit_move_insn (offset, tmp);
17671           emit_label (label);
17672           LABEL_NUSES (label) = 1;
17673         }
17674       if (max_size > 2)
17675         {
17676           rtx label = ix86_expand_aligntest (count, 2, true);
17677           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
17678           src = change_address (srcmem, HImode, tmp);
17679           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
17680           dest = change_address (destmem, HImode, tmp);
17681           emit_move_insn (dest, src);
17682           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
17683                                      true, OPTAB_LIB_WIDEN);
17684           if (tmp != offset)
17685             emit_move_insn (offset, tmp);
17686           emit_label (label);
17687           LABEL_NUSES (label) = 1;
17688         }
17689       if (max_size > 1)
17690         {
17691           rtx label = ix86_expand_aligntest (count, 1, true);
17692           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
17693           src = change_address (srcmem, QImode, tmp);
17694           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
17695           dest = change_address (destmem, QImode, tmp);
17696           emit_move_insn (dest, src);
17697           emit_label (label);
17698           LABEL_NUSES (label) = 1;
17699         }
17700     }
17701 }
17702
17703 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
17704 static void
17705 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
17706                                  rtx count, int max_size)
17707 {
17708   count =
17709     expand_simple_binop (counter_mode (count), AND, count,
17710                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
17711   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
17712                                  gen_lowpart (QImode, value), count, QImode,
17713                                  1, max_size / 2);
17714 }
17715
17716 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
17717 static void
17718 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
17719 {
17720   rtx dest;
17721
17722   if (CONST_INT_P (count))
17723     {
17724       HOST_WIDE_INT countval = INTVAL (count);
17725       int offset = 0;
17726
17727       if ((countval & 0x10) && max_size > 16)
17728         {
17729           if (TARGET_64BIT)
17730             {
17731               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
17732               emit_insn (gen_strset (destptr, dest, value));
17733               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
17734               emit_insn (gen_strset (destptr, dest, value));
17735             }
17736           else
17737             gcc_unreachable ();
17738           offset += 16;
17739         }
17740       if ((countval & 0x08) && max_size > 8)
17741         {
17742           if (TARGET_64BIT)
17743             {
17744               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
17745               emit_insn (gen_strset (destptr, dest, value));
17746             }
17747           else
17748             {
17749               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
17750               emit_insn (gen_strset (destptr, dest, value));
17751               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
17752               emit_insn (gen_strset (destptr, dest, value));
17753             }
17754           offset += 8;
17755         }
17756       if ((countval & 0x04) && max_size > 4)
17757         {
17758           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
17759           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
17760           offset += 4;
17761         }
17762       if ((countval & 0x02) && max_size > 2)
17763         {
17764           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
17765           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
17766           offset += 2;
17767         }
17768       if ((countval & 0x01) && max_size > 1)
17769         {
17770           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
17771           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
17772           offset += 1;
17773         }
17774       return;
17775     }
17776   if (max_size > 32)
17777     {
17778       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
17779       return;
17780     }
17781   if (max_size > 16)
17782     {
17783       rtx label = ix86_expand_aligntest (count, 16, true);
17784       if (TARGET_64BIT)
17785         {
17786           dest = change_address (destmem, DImode, destptr);
17787           emit_insn (gen_strset (destptr, dest, value));
17788           emit_insn (gen_strset (destptr, dest, value));
17789         }
17790       else
17791         {
17792           dest = change_address (destmem, SImode, destptr);
17793           emit_insn (gen_strset (destptr, dest, value));
17794           emit_insn (gen_strset (destptr, dest, value));
17795           emit_insn (gen_strset (destptr, dest, value));
17796           emit_insn (gen_strset (destptr, dest, value));
17797         }
17798       emit_label (label);
17799       LABEL_NUSES (label) = 1;
17800     }
17801   if (max_size > 8)
17802     {
17803       rtx label = ix86_expand_aligntest (count, 8, true);
17804       if (TARGET_64BIT)
17805         {
17806           dest = change_address (destmem, DImode, destptr);
17807           emit_insn (gen_strset (destptr, dest, value));
17808         }
17809       else
17810         {
17811           dest = change_address (destmem, SImode, destptr);
17812           emit_insn (gen_strset (destptr, dest, value));
17813           emit_insn (gen_strset (destptr, dest, value));
17814         }
17815       emit_label (label);
17816       LABEL_NUSES (label) = 1;
17817     }
17818   if (max_size > 4)
17819     {
17820       rtx label = ix86_expand_aligntest (count, 4, true);
17821       dest = change_address (destmem, SImode, destptr);
17822       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
17823       emit_label (label);
17824       LABEL_NUSES (label) = 1;
17825     }
17826   if (max_size > 2)
17827     {
17828       rtx label = ix86_expand_aligntest (count, 2, true);
17829       dest = change_address (destmem, HImode, destptr);
17830       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
17831       emit_label (label);
17832       LABEL_NUSES (label) = 1;
17833     }
17834   if (max_size > 1)
17835     {
17836       rtx label = ix86_expand_aligntest (count, 1, true);
17837       dest = change_address (destmem, QImode, destptr);
17838       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
17839       emit_label (label);
17840       LABEL_NUSES (label) = 1;
17841     }
17842 }
17843
17844 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
17845    DESIRED_ALIGNMENT.  */
17846 static void
17847 expand_movmem_prologue (rtx destmem, rtx srcmem,
17848                         rtx destptr, rtx srcptr, rtx count,
17849                         int align, int desired_alignment)
17850 {
17851   if (align <= 1 && desired_alignment > 1)
17852     {
17853       rtx label = ix86_expand_aligntest (destptr, 1, false);
17854       srcmem = change_address (srcmem, QImode, srcptr);
17855       destmem = change_address (destmem, QImode, destptr);
17856       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
17857       ix86_adjust_counter (count, 1);
17858       emit_label (label);
17859       LABEL_NUSES (label) = 1;
17860     }
17861   if (align <= 2 && desired_alignment > 2)
17862     {
17863       rtx label = ix86_expand_aligntest (destptr, 2, false);
17864       srcmem = change_address (srcmem, HImode, srcptr);
17865       destmem = change_address (destmem, HImode, destptr);
17866       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
17867       ix86_adjust_counter (count, 2);
17868       emit_label (label);
17869       LABEL_NUSES (label) = 1;
17870     }
17871   if (align <= 4 && desired_alignment > 4)
17872     {
17873       rtx label = ix86_expand_aligntest (destptr, 4, false);
17874       srcmem = change_address (srcmem, SImode, srcptr);
17875       destmem = change_address (destmem, SImode, destptr);
17876       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
17877       ix86_adjust_counter (count, 4);
17878       emit_label (label);
17879       LABEL_NUSES (label) = 1;
17880     }
17881   gcc_assert (desired_alignment <= 8);
17882 }
17883
17884 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
17885    ALIGN_BYTES is how many bytes need to be copied.  */
17886 static rtx
17887 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
17888                                  int desired_align, int align_bytes)
17889 {
17890   rtx src = *srcp;
17891   rtx src_size, dst_size;
17892   int off = 0;
17893   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
17894   if (src_align_bytes >= 0)
17895     src_align_bytes = desired_align - src_align_bytes;
17896   src_size = MEM_SIZE (src);
17897   dst_size = MEM_SIZE (dst);
17898   if (align_bytes & 1)
17899     {
17900       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
17901       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
17902       off = 1;
17903       emit_insn (gen_strmov (destreg, dst, srcreg, src));
17904     }
17905   if (align_bytes & 2)
17906     {
17907       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
17908       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
17909       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
17910         set_mem_align (dst, 2 * BITS_PER_UNIT);
17911       if (src_align_bytes >= 0
17912           && (src_align_bytes & 1) == (align_bytes & 1)
17913           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
17914         set_mem_align (src, 2 * BITS_PER_UNIT);
17915       off = 2;
17916       emit_insn (gen_strmov (destreg, dst, srcreg, src));
17917     }
17918   if (align_bytes & 4)
17919     {
17920       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
17921       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
17922       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
17923         set_mem_align (dst, 4 * BITS_PER_UNIT);
17924       if (src_align_bytes >= 0)
17925         {
17926           unsigned int src_align = 0;
17927           if ((src_align_bytes & 3) == (align_bytes & 3))
17928             src_align = 4;
17929           else if ((src_align_bytes & 1) == (align_bytes & 1))
17930             src_align = 2;
17931           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
17932             set_mem_align (src, src_align * BITS_PER_UNIT);
17933         }
17934       off = 4;
17935       emit_insn (gen_strmov (destreg, dst, srcreg, src));
17936     }
17937   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
17938   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
17939   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
17940     set_mem_align (dst, desired_align * BITS_PER_UNIT);
17941   if (src_align_bytes >= 0)
17942     {
17943       unsigned int src_align = 0;
17944       if ((src_align_bytes & 7) == (align_bytes & 7))
17945         src_align = 8;
17946       else if ((src_align_bytes & 3) == (align_bytes & 3))
17947         src_align = 4;
17948       else if ((src_align_bytes & 1) == (align_bytes & 1))
17949         src_align = 2;
17950       if (src_align > (unsigned int) desired_align)
17951         src_align = desired_align;
17952       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
17953         set_mem_align (src, src_align * BITS_PER_UNIT);
17954     }
17955   if (dst_size)
17956     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
17957   if (src_size)
17958     set_mem_size (dst, GEN_INT (INTVAL (src_size) - align_bytes));
17959   *srcp = src;
17960   return dst;
17961 }
17962
17963 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
17964    DESIRED_ALIGNMENT.  */
17965 static void
17966 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
17967                         int align, int desired_alignment)
17968 {
17969   if (align <= 1 && desired_alignment > 1)
17970     {
17971       rtx label = ix86_expand_aligntest (destptr, 1, false);
17972       destmem = change_address (destmem, QImode, destptr);
17973       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
17974       ix86_adjust_counter (count, 1);
17975       emit_label (label);
17976       LABEL_NUSES (label) = 1;
17977     }
17978   if (align <= 2 && desired_alignment > 2)
17979     {
17980       rtx label = ix86_expand_aligntest (destptr, 2, false);
17981       destmem = change_address (destmem, HImode, destptr);
17982       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
17983       ix86_adjust_counter (count, 2);
17984       emit_label (label);
17985       LABEL_NUSES (label) = 1;
17986     }
17987   if (align <= 4 && desired_alignment > 4)
17988     {
17989       rtx label = ix86_expand_aligntest (destptr, 4, false);
17990       destmem = change_address (destmem, SImode, destptr);
17991       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
17992       ix86_adjust_counter (count, 4);
17993       emit_label (label);
17994       LABEL_NUSES (label) = 1;
17995     }
17996   gcc_assert (desired_alignment <= 8);
17997 }
17998
17999 /* Set enough from DST to align DST known to by aligned by ALIGN to
18000    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
18001 static rtx
18002 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
18003                                  int desired_align, int align_bytes)
18004 {
18005   int off = 0;
18006   rtx dst_size = MEM_SIZE (dst);
18007   if (align_bytes & 1)
18008     {
18009       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
18010       off = 1;
18011       emit_insn (gen_strset (destreg, dst,
18012                              gen_lowpart (QImode, value)));
18013     }
18014   if (align_bytes & 2)
18015     {
18016       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
18017       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
18018         set_mem_align (dst, 2 * BITS_PER_UNIT);
18019       off = 2;
18020       emit_insn (gen_strset (destreg, dst,
18021                              gen_lowpart (HImode, value)));
18022     }
18023   if (align_bytes & 4)
18024     {
18025       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
18026       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
18027         set_mem_align (dst, 4 * BITS_PER_UNIT);
18028       off = 4;
18029       emit_insn (gen_strset (destreg, dst,
18030                              gen_lowpart (SImode, value)));
18031     }
18032   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
18033   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
18034     set_mem_align (dst, desired_align * BITS_PER_UNIT);
18035   if (dst_size)
18036     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
18037   return dst;
18038 }
18039
18040 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
18041 static enum stringop_alg
18042 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
18043             int *dynamic_check)
18044 {
18045   const struct stringop_algs * algs;
18046   bool optimize_for_speed;
18047   /* Algorithms using the rep prefix want at least edi and ecx;
18048      additionally, memset wants eax and memcpy wants esi.  Don't
18049      consider such algorithms if the user has appropriated those
18050      registers for their own purposes.  */
18051   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
18052                              || (memset
18053                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
18054
18055 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
18056                            || (alg != rep_prefix_1_byte         \
18057                                && alg != rep_prefix_4_byte      \
18058                                && alg != rep_prefix_8_byte))
18059   const struct processor_costs *cost;
18060   
18061   /* Even if the string operation call is cold, we still might spend a lot
18062      of time processing large blocks.  */
18063   if (optimize_function_for_size_p (cfun)
18064       || (optimize_insn_for_size_p ()
18065           && expected_size != -1 && expected_size < 256))
18066     optimize_for_speed = false;
18067   else
18068     optimize_for_speed = true;
18069
18070   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
18071
18072   *dynamic_check = -1;
18073   if (memset)
18074     algs = &cost->memset[TARGET_64BIT != 0];
18075   else
18076     algs = &cost->memcpy[TARGET_64BIT != 0];
18077   if (stringop_alg != no_stringop && ALG_USABLE_P (stringop_alg))
18078     return stringop_alg;
18079   /* rep; movq or rep; movl is the smallest variant.  */
18080   else if (!optimize_for_speed)
18081     {
18082       if (!count || (count & 3))
18083         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
18084       else
18085         return rep_prefix_usable ? rep_prefix_4_byte : loop;
18086     }
18087   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
18088    */
18089   else if (expected_size != -1 && expected_size < 4)
18090     return loop_1_byte;
18091   else if (expected_size != -1)
18092     {
18093       unsigned int i;
18094       enum stringop_alg alg = libcall;
18095       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
18096         {
18097           /* We get here if the algorithms that were not libcall-based
18098              were rep-prefix based and we are unable to use rep prefixes
18099              based on global register usage.  Break out of the loop and
18100              use the heuristic below.  */
18101           if (algs->size[i].max == 0)
18102             break;
18103           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
18104             {
18105               enum stringop_alg candidate = algs->size[i].alg;
18106
18107               if (candidate != libcall && ALG_USABLE_P (candidate))
18108                 alg = candidate;
18109               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
18110                  last non-libcall inline algorithm.  */
18111               if (TARGET_INLINE_ALL_STRINGOPS)
18112                 {
18113                   /* When the current size is best to be copied by a libcall,
18114                      but we are still forced to inline, run the heuristic below
18115                      that will pick code for medium sized blocks.  */
18116                   if (alg != libcall)
18117                     return alg;
18118                   break;
18119                 }
18120               else if (ALG_USABLE_P (candidate))
18121                 return candidate;
18122             }
18123         }
18124       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
18125     }
18126   /* When asked to inline the call anyway, try to pick meaningful choice.
18127      We look for maximal size of block that is faster to copy by hand and
18128      take blocks of at most of that size guessing that average size will
18129      be roughly half of the block.
18130
18131      If this turns out to be bad, we might simply specify the preferred
18132      choice in ix86_costs.  */
18133   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
18134       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
18135     {
18136       int max = -1;
18137       enum stringop_alg alg;
18138       int i;
18139       bool any_alg_usable_p = true;
18140
18141       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
18142         {
18143           enum stringop_alg candidate = algs->size[i].alg;
18144           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
18145
18146           if (candidate != libcall && candidate
18147               && ALG_USABLE_P (candidate))
18148               max = algs->size[i].max;
18149         }
18150       /* If there aren't any usable algorithms, then recursing on
18151          smaller sizes isn't going to find anything.  Just return the
18152          simple byte-at-a-time copy loop.  */
18153       if (!any_alg_usable_p)
18154         {
18155           /* Pick something reasonable.  */
18156           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
18157             *dynamic_check = 128;
18158           return loop_1_byte;
18159         }
18160       if (max == -1)
18161         max = 4096;
18162       alg = decide_alg (count, max / 2, memset, dynamic_check);
18163       gcc_assert (*dynamic_check == -1);
18164       gcc_assert (alg != libcall);
18165       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
18166         *dynamic_check = max;
18167       return alg;
18168     }
18169   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
18170 #undef ALG_USABLE_P
18171 }
18172
18173 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
18174    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
18175 static int
18176 decide_alignment (int align,
18177                   enum stringop_alg alg,
18178                   int expected_size)
18179 {
18180   int desired_align = 0;
18181   switch (alg)
18182     {
18183       case no_stringop:
18184         gcc_unreachable ();
18185       case loop:
18186       case unrolled_loop:
18187         desired_align = GET_MODE_SIZE (Pmode);
18188         break;
18189       case rep_prefix_8_byte:
18190         desired_align = 8;
18191         break;
18192       case rep_prefix_4_byte:
18193         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
18194            copying whole cacheline at once.  */
18195         if (TARGET_PENTIUMPRO)
18196           desired_align = 8;
18197         else
18198           desired_align = 4;
18199         break;
18200       case rep_prefix_1_byte:
18201         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
18202            copying whole cacheline at once.  */
18203         if (TARGET_PENTIUMPRO)
18204           desired_align = 8;
18205         else
18206           desired_align = 1;
18207         break;
18208       case loop_1_byte:
18209         desired_align = 1;
18210         break;
18211       case libcall:
18212         return 0;
18213     }
18214
18215   if (optimize_size)
18216     desired_align = 1;
18217   if (desired_align < align)
18218     desired_align = align;
18219   if (expected_size != -1 && expected_size < 4)
18220     desired_align = align;
18221   return desired_align;
18222 }
18223
18224 /* Return the smallest power of 2 greater than VAL.  */
18225 static int
18226 smallest_pow2_greater_than (int val)
18227 {
18228   int ret = 1;
18229   while (ret <= val)
18230     ret <<= 1;
18231   return ret;
18232 }
18233
18234 /* Expand string move (memcpy) operation.  Use i386 string operations when
18235    profitable.  expand_setmem contains similar code.  The code depends upon
18236    architecture, block size and alignment, but always has the same
18237    overall structure:
18238
18239    1) Prologue guard: Conditional that jumps up to epilogues for small
18240       blocks that can be handled by epilogue alone.  This is faster but
18241       also needed for correctness, since prologue assume the block is larger
18242       than the desired alignment.
18243
18244       Optional dynamic check for size and libcall for large
18245       blocks is emitted here too, with -minline-stringops-dynamically.
18246
18247    2) Prologue: copy first few bytes in order to get destination aligned
18248       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
18249       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
18250       We emit either a jump tree on power of two sized blocks, or a byte loop.
18251
18252    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
18253       with specified algorithm.
18254
18255    4) Epilogue: code copying tail of the block that is too small to be
18256       handled by main body (or up to size guarded by prologue guard).  */
18257
18258 int
18259 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
18260                     rtx expected_align_exp, rtx expected_size_exp)
18261 {
18262   rtx destreg;
18263   rtx srcreg;
18264   rtx label = NULL;
18265   rtx tmp;
18266   rtx jump_around_label = NULL;
18267   HOST_WIDE_INT align = 1;
18268   unsigned HOST_WIDE_INT count = 0;
18269   HOST_WIDE_INT expected_size = -1;
18270   int size_needed = 0, epilogue_size_needed;
18271   int desired_align = 0, align_bytes = 0;
18272   enum stringop_alg alg;
18273   int dynamic_check;
18274   bool need_zero_guard = false;
18275
18276   if (CONST_INT_P (align_exp))
18277     align = INTVAL (align_exp);
18278   /* i386 can do misaligned access on reasonably increased cost.  */
18279   if (CONST_INT_P (expected_align_exp)
18280       && INTVAL (expected_align_exp) > align)
18281     align = INTVAL (expected_align_exp);
18282   /* ALIGN is the minimum of destination and source alignment, but we care here
18283      just about destination alignment.  */
18284   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
18285     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
18286
18287   if (CONST_INT_P (count_exp))
18288     count = expected_size = INTVAL (count_exp);
18289   if (CONST_INT_P (expected_size_exp) && count == 0)
18290     expected_size = INTVAL (expected_size_exp);
18291
18292   /* Make sure we don't need to care about overflow later on.  */
18293   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
18294     return 0;
18295
18296   /* Step 0: Decide on preferred algorithm, desired alignment and
18297      size of chunks to be copied by main loop.  */
18298
18299   alg = decide_alg (count, expected_size, false, &dynamic_check);
18300   desired_align = decide_alignment (align, alg, expected_size);
18301
18302   if (!TARGET_ALIGN_STRINGOPS)
18303     align = desired_align;
18304
18305   if (alg == libcall)
18306     return 0;
18307   gcc_assert (alg != no_stringop);
18308   if (!count)
18309     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
18310   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
18311   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
18312   switch (alg)
18313     {
18314     case libcall:
18315     case no_stringop:
18316       gcc_unreachable ();
18317     case loop:
18318       need_zero_guard = true;
18319       size_needed = GET_MODE_SIZE (Pmode);
18320       break;
18321     case unrolled_loop:
18322       need_zero_guard = true;
18323       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
18324       break;
18325     case rep_prefix_8_byte:
18326       size_needed = 8;
18327       break;
18328     case rep_prefix_4_byte:
18329       size_needed = 4;
18330       break;
18331     case rep_prefix_1_byte:
18332       size_needed = 1;
18333       break;
18334     case loop_1_byte:
18335       need_zero_guard = true;
18336       size_needed = 1;
18337       break;
18338     }
18339
18340   epilogue_size_needed = size_needed;
18341
18342   /* Step 1: Prologue guard.  */
18343
18344   /* Alignment code needs count to be in register.  */
18345   if (CONST_INT_P (count_exp) && desired_align > align)
18346     {
18347       if (INTVAL (count_exp) > desired_align
18348           && INTVAL (count_exp) > size_needed)
18349         {
18350           align_bytes
18351             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
18352           if (align_bytes <= 0)
18353             align_bytes = 0;
18354           else
18355             align_bytes = desired_align - align_bytes;
18356         }
18357       if (align_bytes == 0)
18358         count_exp = force_reg (counter_mode (count_exp), count_exp);
18359     }
18360   gcc_assert (desired_align >= 1 && align >= 1);
18361
18362   /* Ensure that alignment prologue won't copy past end of block.  */
18363   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
18364     {
18365       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
18366       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
18367          Make sure it is power of 2.  */
18368       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
18369
18370       if (count)
18371         {
18372           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
18373             {
18374               /* If main algorithm works on QImode, no epilogue is needed.
18375                  For small sizes just don't align anything.  */
18376               if (size_needed == 1)
18377                 desired_align = align;
18378               else
18379                 goto epilogue;
18380             }
18381         }
18382       else
18383         {
18384           label = gen_label_rtx ();
18385           emit_cmp_and_jump_insns (count_exp,
18386                                    GEN_INT (epilogue_size_needed),
18387                                    LTU, 0, counter_mode (count_exp), 1, label);
18388           if (expected_size == -1 || expected_size < epilogue_size_needed)
18389             predict_jump (REG_BR_PROB_BASE * 60 / 100);
18390           else
18391             predict_jump (REG_BR_PROB_BASE * 20 / 100);
18392         }
18393     }
18394
18395   /* Emit code to decide on runtime whether library call or inline should be
18396      used.  */
18397   if (dynamic_check != -1)
18398     {
18399       if (CONST_INT_P (count_exp))
18400         {
18401           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
18402             {
18403               emit_block_move_via_libcall (dst, src, count_exp, false);
18404               count_exp = const0_rtx;
18405               goto epilogue;
18406             }
18407         }
18408       else
18409         {
18410           rtx hot_label = gen_label_rtx ();
18411           jump_around_label = gen_label_rtx ();
18412           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
18413                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
18414           predict_jump (REG_BR_PROB_BASE * 90 / 100);
18415           emit_block_move_via_libcall (dst, src, count_exp, false);
18416           emit_jump (jump_around_label);
18417           emit_label (hot_label);
18418         }
18419     }
18420
18421   /* Step 2: Alignment prologue.  */
18422
18423   if (desired_align > align)
18424     {
18425       if (align_bytes == 0)
18426         {
18427           /* Except for the first move in epilogue, we no longer know
18428              constant offset in aliasing info.  It don't seems to worth
18429              the pain to maintain it for the first move, so throw away
18430              the info early.  */
18431           src = change_address (src, BLKmode, srcreg);
18432           dst = change_address (dst, BLKmode, destreg);
18433           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
18434                                   desired_align);
18435         }
18436       else
18437         {
18438           /* If we know how many bytes need to be stored before dst is
18439              sufficiently aligned, maintain aliasing info accurately.  */
18440           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
18441                                                  desired_align, align_bytes);
18442           count_exp = plus_constant (count_exp, -align_bytes);
18443           count -= align_bytes;
18444         }
18445       if (need_zero_guard
18446           && (count < (unsigned HOST_WIDE_INT) size_needed
18447               || (align_bytes == 0
18448                   && count < ((unsigned HOST_WIDE_INT) size_needed
18449                               + desired_align - align))))
18450         {
18451           /* It is possible that we copied enough so the main loop will not
18452              execute.  */
18453           gcc_assert (size_needed > 1);
18454           if (label == NULL_RTX)
18455             label = gen_label_rtx ();
18456           emit_cmp_and_jump_insns (count_exp,
18457                                    GEN_INT (size_needed),
18458                                    LTU, 0, counter_mode (count_exp), 1, label);
18459           if (expected_size == -1
18460               || expected_size < (desired_align - align) / 2 + size_needed)
18461             predict_jump (REG_BR_PROB_BASE * 20 / 100);
18462           else
18463             predict_jump (REG_BR_PROB_BASE * 60 / 100);
18464         }
18465     }
18466   if (label && size_needed == 1)
18467     {
18468       emit_label (label);
18469       LABEL_NUSES (label) = 1;
18470       label = NULL;
18471       epilogue_size_needed = 1;
18472     }
18473   else if (label == NULL_RTX)
18474     epilogue_size_needed = size_needed;
18475
18476   /* Step 3: Main loop.  */
18477
18478   switch (alg)
18479     {
18480     case libcall:
18481     case no_stringop:
18482       gcc_unreachable ();
18483     case loop_1_byte:
18484       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
18485                                      count_exp, QImode, 1, expected_size);
18486       break;
18487     case loop:
18488       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
18489                                      count_exp, Pmode, 1, expected_size);
18490       break;
18491     case unrolled_loop:
18492       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
18493          registers for 4 temporaries anyway.  */
18494       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
18495                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
18496                                      expected_size);
18497       break;
18498     case rep_prefix_8_byte:
18499       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
18500                                  DImode);
18501       break;
18502     case rep_prefix_4_byte:
18503       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
18504                                  SImode);
18505       break;
18506     case rep_prefix_1_byte:
18507       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
18508                                  QImode);
18509       break;
18510     }
18511   /* Adjust properly the offset of src and dest memory for aliasing.  */
18512   if (CONST_INT_P (count_exp))
18513     {
18514       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
18515                                           (count / size_needed) * size_needed);
18516       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
18517                                           (count / size_needed) * size_needed);
18518     }
18519   else
18520     {
18521       src = change_address (src, BLKmode, srcreg);
18522       dst = change_address (dst, BLKmode, destreg);
18523     }
18524
18525   /* Step 4: Epilogue to copy the remaining bytes.  */
18526  epilogue:
18527   if (label)
18528     {
18529       /* When the main loop is done, COUNT_EXP might hold original count,
18530          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
18531          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
18532          bytes. Compensate if needed.  */
18533
18534       if (size_needed < epilogue_size_needed)
18535         {
18536           tmp =
18537             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
18538                                  GEN_INT (size_needed - 1), count_exp, 1,
18539                                  OPTAB_DIRECT);
18540           if (tmp != count_exp)
18541             emit_move_insn (count_exp, tmp);
18542         }
18543       emit_label (label);
18544       LABEL_NUSES (label) = 1;
18545     }
18546
18547   if (count_exp != const0_rtx && epilogue_size_needed > 1)
18548     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
18549                             epilogue_size_needed);
18550   if (jump_around_label)
18551     emit_label (jump_around_label);
18552   return 1;
18553 }
18554
18555 /* Helper function for memcpy.  For QImode value 0xXY produce
18556    0xXYXYXYXY of wide specified by MODE.  This is essentially
18557    a * 0x10101010, but we can do slightly better than
18558    synth_mult by unwinding the sequence by hand on CPUs with
18559    slow multiply.  */
18560 static rtx
18561 promote_duplicated_reg (enum machine_mode mode, rtx val)
18562 {
18563   enum machine_mode valmode = GET_MODE (val);
18564   rtx tmp;
18565   int nops = mode == DImode ? 3 : 2;
18566
18567   gcc_assert (mode == SImode || mode == DImode);
18568   if (val == const0_rtx)
18569     return copy_to_mode_reg (mode, const0_rtx);
18570   if (CONST_INT_P (val))
18571     {
18572       HOST_WIDE_INT v = INTVAL (val) & 255;
18573
18574       v |= v << 8;
18575       v |= v << 16;
18576       if (mode == DImode)
18577         v |= (v << 16) << 16;
18578       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
18579     }
18580
18581   if (valmode == VOIDmode)
18582     valmode = QImode;
18583   if (valmode != QImode)
18584     val = gen_lowpart (QImode, val);
18585   if (mode == QImode)
18586     return val;
18587   if (!TARGET_PARTIAL_REG_STALL)
18588     nops--;
18589   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
18590       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
18591       <= (ix86_cost->shift_const + ix86_cost->add) * nops
18592           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
18593     {
18594       rtx reg = convert_modes (mode, QImode, val, true);
18595       tmp = promote_duplicated_reg (mode, const1_rtx);
18596       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
18597                                   OPTAB_DIRECT);
18598     }
18599   else
18600     {
18601       rtx reg = convert_modes (mode, QImode, val, true);
18602
18603       if (!TARGET_PARTIAL_REG_STALL)
18604         if (mode == SImode)
18605           emit_insn (gen_movsi_insv_1 (reg, reg));
18606         else
18607           emit_insn (gen_movdi_insv_1_rex64 (reg, reg));
18608       else
18609         {
18610           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
18611                                      NULL, 1, OPTAB_DIRECT);
18612           reg =
18613             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
18614         }
18615       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
18616                                  NULL, 1, OPTAB_DIRECT);
18617       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
18618       if (mode == SImode)
18619         return reg;
18620       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
18621                                  NULL, 1, OPTAB_DIRECT);
18622       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
18623       return reg;
18624     }
18625 }
18626
18627 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
18628    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
18629    alignment from ALIGN to DESIRED_ALIGN.  */
18630 static rtx
18631 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
18632 {
18633   rtx promoted_val;
18634
18635   if (TARGET_64BIT
18636       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
18637     promoted_val = promote_duplicated_reg (DImode, val);
18638   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
18639     promoted_val = promote_duplicated_reg (SImode, val);
18640   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
18641     promoted_val = promote_duplicated_reg (HImode, val);
18642   else
18643     promoted_val = val;
18644
18645   return promoted_val;
18646 }
18647
18648 /* Expand string clear operation (bzero).  Use i386 string operations when
18649    profitable.  See expand_movmem comment for explanation of individual
18650    steps performed.  */
18651 int
18652 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
18653                     rtx expected_align_exp, rtx expected_size_exp)
18654 {
18655   rtx destreg;
18656   rtx label = NULL;
18657   rtx tmp;
18658   rtx jump_around_label = NULL;
18659   HOST_WIDE_INT align = 1;
18660   unsigned HOST_WIDE_INT count = 0;
18661   HOST_WIDE_INT expected_size = -1;
18662   int size_needed = 0, epilogue_size_needed;
18663   int desired_align = 0, align_bytes = 0;
18664   enum stringop_alg alg;
18665   rtx promoted_val = NULL;
18666   bool force_loopy_epilogue = false;
18667   int dynamic_check;
18668   bool need_zero_guard = false;
18669
18670   if (CONST_INT_P (align_exp))
18671     align = INTVAL (align_exp);
18672   /* i386 can do misaligned access on reasonably increased cost.  */
18673   if (CONST_INT_P (expected_align_exp)
18674       && INTVAL (expected_align_exp) > align)
18675     align = INTVAL (expected_align_exp);
18676   if (CONST_INT_P (count_exp))
18677     count = expected_size = INTVAL (count_exp);
18678   if (CONST_INT_P (expected_size_exp) && count == 0)
18679     expected_size = INTVAL (expected_size_exp);
18680
18681   /* Make sure we don't need to care about overflow later on.  */
18682   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
18683     return 0;
18684
18685   /* Step 0: Decide on preferred algorithm, desired alignment and
18686      size of chunks to be copied by main loop.  */
18687
18688   alg = decide_alg (count, expected_size, true, &dynamic_check);
18689   desired_align = decide_alignment (align, alg, expected_size);
18690
18691   if (!TARGET_ALIGN_STRINGOPS)
18692     align = desired_align;
18693
18694   if (alg == libcall)
18695     return 0;
18696   gcc_assert (alg != no_stringop);
18697   if (!count)
18698     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
18699   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
18700   switch (alg)
18701     {
18702     case libcall:
18703     case no_stringop:
18704       gcc_unreachable ();
18705     case loop:
18706       need_zero_guard = true;
18707       size_needed = GET_MODE_SIZE (Pmode);
18708       break;
18709     case unrolled_loop:
18710       need_zero_guard = true;
18711       size_needed = GET_MODE_SIZE (Pmode) * 4;
18712       break;
18713     case rep_prefix_8_byte:
18714       size_needed = 8;
18715       break;
18716     case rep_prefix_4_byte:
18717       size_needed = 4;
18718       break;
18719     case rep_prefix_1_byte:
18720       size_needed = 1;
18721       break;
18722     case loop_1_byte:
18723       need_zero_guard = true;
18724       size_needed = 1;
18725       break;
18726     }
18727   epilogue_size_needed = size_needed;
18728
18729   /* Step 1: Prologue guard.  */
18730
18731   /* Alignment code needs count to be in register.  */
18732   if (CONST_INT_P (count_exp) && desired_align > align)
18733     {
18734       if (INTVAL (count_exp) > desired_align
18735           && INTVAL (count_exp) > size_needed)
18736         {
18737           align_bytes
18738             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
18739           if (align_bytes <= 0)
18740             align_bytes = 0;
18741           else
18742             align_bytes = desired_align - align_bytes;
18743         }
18744       if (align_bytes == 0)
18745         {
18746           enum machine_mode mode = SImode;
18747           if (TARGET_64BIT && (count & ~0xffffffff))
18748             mode = DImode;
18749           count_exp = force_reg (mode, count_exp);
18750         }
18751     }
18752   /* Do the cheap promotion to allow better CSE across the
18753      main loop and epilogue (ie one load of the big constant in the
18754      front of all code.  */
18755   if (CONST_INT_P (val_exp))
18756     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
18757                                                    desired_align, align);
18758   /* Ensure that alignment prologue won't copy past end of block.  */
18759   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
18760     {
18761       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
18762       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
18763          Make sure it is power of 2.  */
18764       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
18765
18766       /* To improve performance of small blocks, we jump around the VAL
18767          promoting mode.  This mean that if the promoted VAL is not constant,
18768          we might not use it in the epilogue and have to use byte
18769          loop variant.  */
18770       if (epilogue_size_needed > 2 && !promoted_val)
18771         force_loopy_epilogue = true;
18772       if (count)
18773         {
18774           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
18775             {
18776               /* If main algorithm works on QImode, no epilogue is needed.
18777                  For small sizes just don't align anything.  */
18778               if (size_needed == 1)
18779                 desired_align = align;
18780               else
18781                 goto epilogue;
18782             }
18783         }
18784       else
18785         {
18786           label = gen_label_rtx ();
18787           emit_cmp_and_jump_insns (count_exp,
18788                                    GEN_INT (epilogue_size_needed),
18789                                    LTU, 0, counter_mode (count_exp), 1, label);
18790           if (expected_size == -1 || expected_size <= epilogue_size_needed)
18791             predict_jump (REG_BR_PROB_BASE * 60 / 100);
18792           else
18793             predict_jump (REG_BR_PROB_BASE * 20 / 100);
18794         }
18795     }
18796   if (dynamic_check != -1)
18797     {
18798       rtx hot_label = gen_label_rtx ();
18799       jump_around_label = gen_label_rtx ();
18800       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
18801                                LEU, 0, counter_mode (count_exp), 1, hot_label);
18802       predict_jump (REG_BR_PROB_BASE * 90 / 100);
18803       set_storage_via_libcall (dst, count_exp, val_exp, false);
18804       emit_jump (jump_around_label);
18805       emit_label (hot_label);
18806     }
18807
18808   /* Step 2: Alignment prologue.  */
18809
18810   /* Do the expensive promotion once we branched off the small blocks.  */
18811   if (!promoted_val)
18812     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
18813                                                    desired_align, align);
18814   gcc_assert (desired_align >= 1 && align >= 1);
18815
18816   if (desired_align > align)
18817     {
18818       if (align_bytes == 0)
18819         {
18820           /* Except for the first move in epilogue, we no longer know
18821              constant offset in aliasing info.  It don't seems to worth
18822              the pain to maintain it for the first move, so throw away
18823              the info early.  */
18824           dst = change_address (dst, BLKmode, destreg);
18825           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
18826                                   desired_align);
18827         }
18828       else
18829         {
18830           /* If we know how many bytes need to be stored before dst is
18831              sufficiently aligned, maintain aliasing info accurately.  */
18832           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
18833                                                  desired_align, align_bytes);
18834           count_exp = plus_constant (count_exp, -align_bytes);
18835           count -= align_bytes;
18836         }
18837       if (need_zero_guard
18838           && (count < (unsigned HOST_WIDE_INT) size_needed
18839               || (align_bytes == 0
18840                   && count < ((unsigned HOST_WIDE_INT) size_needed
18841                               + desired_align - align))))
18842         {
18843           /* It is possible that we copied enough so the main loop will not
18844              execute.  */
18845           gcc_assert (size_needed > 1);
18846           if (label == NULL_RTX)
18847             label = gen_label_rtx ();
18848           emit_cmp_and_jump_insns (count_exp,
18849                                    GEN_INT (size_needed),
18850                                    LTU, 0, counter_mode (count_exp), 1, label);
18851           if (expected_size == -1
18852               || expected_size < (desired_align - align) / 2 + size_needed)
18853             predict_jump (REG_BR_PROB_BASE * 20 / 100);
18854           else
18855             predict_jump (REG_BR_PROB_BASE * 60 / 100);
18856         }
18857     }
18858   if (label && size_needed == 1)
18859     {
18860       emit_label (label);
18861       LABEL_NUSES (label) = 1;
18862       label = NULL;
18863       promoted_val = val_exp;
18864       epilogue_size_needed = 1;
18865     }
18866   else if (label == NULL_RTX)
18867     epilogue_size_needed = size_needed;
18868
18869   /* Step 3: Main loop.  */
18870
18871   switch (alg)
18872     {
18873     case libcall:
18874     case no_stringop:
18875       gcc_unreachable ();
18876     case loop_1_byte:
18877       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
18878                                      count_exp, QImode, 1, expected_size);
18879       break;
18880     case loop:
18881       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
18882                                      count_exp, Pmode, 1, expected_size);
18883       break;
18884     case unrolled_loop:
18885       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
18886                                      count_exp, Pmode, 4, expected_size);
18887       break;
18888     case rep_prefix_8_byte:
18889       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
18890                                   DImode, val_exp);
18891       break;
18892     case rep_prefix_4_byte:
18893       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
18894                                   SImode, val_exp);
18895       break;
18896     case rep_prefix_1_byte:
18897       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
18898                                   QImode, val_exp);
18899       break;
18900     }
18901   /* Adjust properly the offset of src and dest memory for aliasing.  */
18902   if (CONST_INT_P (count_exp))
18903     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
18904                                         (count / size_needed) * size_needed);
18905   else
18906     dst = change_address (dst, BLKmode, destreg);
18907
18908   /* Step 4: Epilogue to copy the remaining bytes.  */
18909
18910   if (label)
18911     {
18912       /* When the main loop is done, COUNT_EXP might hold original count,
18913          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
18914          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
18915          bytes. Compensate if needed.  */
18916
18917       if (size_needed < epilogue_size_needed)
18918         {
18919           tmp =
18920             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
18921                                  GEN_INT (size_needed - 1), count_exp, 1,
18922                                  OPTAB_DIRECT);
18923           if (tmp != count_exp)
18924             emit_move_insn (count_exp, tmp);
18925         }
18926       emit_label (label);
18927       LABEL_NUSES (label) = 1;
18928     }
18929  epilogue:
18930   if (count_exp != const0_rtx && epilogue_size_needed > 1)
18931     {
18932       if (force_loopy_epilogue)
18933         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
18934                                          epilogue_size_needed);
18935       else
18936         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
18937                                 epilogue_size_needed);
18938     }
18939   if (jump_around_label)
18940     emit_label (jump_around_label);
18941   return 1;
18942 }
18943
18944 /* Expand the appropriate insns for doing strlen if not just doing
18945    repnz; scasb
18946
18947    out = result, initialized with the start address
18948    align_rtx = alignment of the address.
18949    scratch = scratch register, initialized with the startaddress when
18950         not aligned, otherwise undefined
18951
18952    This is just the body. It needs the initializations mentioned above and
18953    some address computing at the end.  These things are done in i386.md.  */
18954
18955 static void
18956 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
18957 {
18958   int align;
18959   rtx tmp;
18960   rtx align_2_label = NULL_RTX;
18961   rtx align_3_label = NULL_RTX;
18962   rtx align_4_label = gen_label_rtx ();
18963   rtx end_0_label = gen_label_rtx ();
18964   rtx mem;
18965   rtx tmpreg = gen_reg_rtx (SImode);
18966   rtx scratch = gen_reg_rtx (SImode);
18967   rtx cmp;
18968
18969   align = 0;
18970   if (CONST_INT_P (align_rtx))
18971     align = INTVAL (align_rtx);
18972
18973   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
18974
18975   /* Is there a known alignment and is it less than 4?  */
18976   if (align < 4)
18977     {
18978       rtx scratch1 = gen_reg_rtx (Pmode);
18979       emit_move_insn (scratch1, out);
18980       /* Is there a known alignment and is it not 2? */
18981       if (align != 2)
18982         {
18983           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
18984           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
18985
18986           /* Leave just the 3 lower bits.  */
18987           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
18988                                     NULL_RTX, 0, OPTAB_WIDEN);
18989
18990           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
18991                                    Pmode, 1, align_4_label);
18992           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
18993                                    Pmode, 1, align_2_label);
18994           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
18995                                    Pmode, 1, align_3_label);
18996         }
18997       else
18998         {
18999           /* Since the alignment is 2, we have to check 2 or 0 bytes;
19000              check if is aligned to 4 - byte.  */
19001
19002           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
19003                                     NULL_RTX, 0, OPTAB_WIDEN);
19004
19005           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
19006                                    Pmode, 1, align_4_label);
19007         }
19008
19009       mem = change_address (src, QImode, out);
19010
19011       /* Now compare the bytes.  */
19012
19013       /* Compare the first n unaligned byte on a byte per byte basis.  */
19014       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
19015                                QImode, 1, end_0_label);
19016
19017       /* Increment the address.  */
19018       emit_insn ((*ix86_gen_add3) (out, out, const1_rtx));
19019
19020       /* Not needed with an alignment of 2 */
19021       if (align != 2)
19022         {
19023           emit_label (align_2_label);
19024
19025           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
19026                                    end_0_label);
19027
19028           emit_insn ((*ix86_gen_add3) (out, out, const1_rtx));
19029
19030           emit_label (align_3_label);
19031         }
19032
19033       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
19034                                end_0_label);
19035
19036       emit_insn ((*ix86_gen_add3) (out, out, const1_rtx));
19037     }
19038
19039   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
19040      align this loop.  It gives only huge programs, but does not help to
19041      speed up.  */
19042   emit_label (align_4_label);
19043
19044   mem = change_address (src, SImode, out);
19045   emit_move_insn (scratch, mem);
19046   emit_insn ((*ix86_gen_add3) (out, out, GEN_INT (4)));
19047
19048   /* This formula yields a nonzero result iff one of the bytes is zero.
19049      This saves three branches inside loop and many cycles.  */
19050
19051   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
19052   emit_insn (gen_one_cmplsi2 (scratch, scratch));
19053   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
19054   emit_insn (gen_andsi3 (tmpreg, tmpreg,
19055                          gen_int_mode (0x80808080, SImode)));
19056   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
19057                            align_4_label);
19058
19059   if (TARGET_CMOVE)
19060     {
19061        rtx reg = gen_reg_rtx (SImode);
19062        rtx reg2 = gen_reg_rtx (Pmode);
19063        emit_move_insn (reg, tmpreg);
19064        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
19065
19066        /* If zero is not in the first two bytes, move two bytes forward.  */
19067        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
19068        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19069        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
19070        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
19071                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
19072                                                      reg,
19073                                                      tmpreg)));
19074        /* Emit lea manually to avoid clobbering of flags.  */
19075        emit_insn (gen_rtx_SET (SImode, reg2,
19076                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
19077
19078        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19079        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
19080        emit_insn (gen_rtx_SET (VOIDmode, out,
19081                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
19082                                                      reg2,
19083                                                      out)));
19084     }
19085   else
19086     {
19087        rtx end_2_label = gen_label_rtx ();
19088        /* Is zero in the first two bytes? */
19089
19090        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
19091        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19092        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
19093        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
19094                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
19095                             pc_rtx);
19096        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
19097        JUMP_LABEL (tmp) = end_2_label;
19098
19099        /* Not in the first two.  Move two bytes forward.  */
19100        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
19101        emit_insn ((*ix86_gen_add3) (out, out, const2_rtx));
19102
19103        emit_label (end_2_label);
19104
19105     }
19106
19107   /* Avoid branch in fixing the byte.  */
19108   tmpreg = gen_lowpart (QImode, tmpreg);
19109   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
19110   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
19111   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
19112   emit_insn ((*ix86_gen_sub3_carry) (out, out, GEN_INT (3), tmp, cmp));
19113
19114   emit_label (end_0_label);
19115 }
19116
19117 /* Expand strlen.  */
19118
19119 int
19120 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
19121 {
19122   rtx addr, scratch1, scratch2, scratch3, scratch4;
19123
19124   /* The generic case of strlen expander is long.  Avoid it's
19125      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
19126
19127   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
19128       && !TARGET_INLINE_ALL_STRINGOPS
19129       && !optimize_insn_for_size_p ()
19130       && (!CONST_INT_P (align) || INTVAL (align) < 4))
19131     return 0;
19132
19133   addr = force_reg (Pmode, XEXP (src, 0));
19134   scratch1 = gen_reg_rtx (Pmode);
19135
19136   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
19137       && !optimize_insn_for_size_p ())
19138     {
19139       /* Well it seems that some optimizer does not combine a call like
19140          foo(strlen(bar), strlen(bar));
19141          when the move and the subtraction is done here.  It does calculate
19142          the length just once when these instructions are done inside of
19143          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
19144          often used and I use one fewer register for the lifetime of
19145          output_strlen_unroll() this is better.  */
19146
19147       emit_move_insn (out, addr);
19148
19149       ix86_expand_strlensi_unroll_1 (out, src, align);
19150
19151       /* strlensi_unroll_1 returns the address of the zero at the end of
19152          the string, like memchr(), so compute the length by subtracting
19153          the start address.  */
19154       emit_insn ((*ix86_gen_sub3) (out, out, addr));
19155     }
19156   else
19157     {
19158       rtx unspec;
19159
19160       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
19161       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
19162         return false;
19163
19164       scratch2 = gen_reg_rtx (Pmode);
19165       scratch3 = gen_reg_rtx (Pmode);
19166       scratch4 = force_reg (Pmode, constm1_rtx);
19167
19168       emit_move_insn (scratch3, addr);
19169       eoschar = force_reg (QImode, eoschar);
19170
19171       src = replace_equiv_address_nv (src, scratch3);
19172
19173       /* If .md starts supporting :P, this can be done in .md.  */
19174       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
19175                                                  scratch4), UNSPEC_SCAS);
19176       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
19177       emit_insn ((*ix86_gen_one_cmpl2) (scratch2, scratch1));
19178       emit_insn ((*ix86_gen_add3) (out, scratch2, constm1_rtx));
19179     }
19180   return 1;
19181 }
19182
19183 /* For given symbol (function) construct code to compute address of it's PLT
19184    entry in large x86-64 PIC model.  */
19185 rtx
19186 construct_plt_address (rtx symbol)
19187 {
19188   rtx tmp = gen_reg_rtx (Pmode);
19189   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
19190
19191   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
19192   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
19193
19194   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
19195   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
19196   return tmp;
19197 }
19198
19199 void
19200 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
19201                   rtx callarg2,
19202                   rtx pop, int sibcall)
19203 {
19204   rtx use = NULL, call;
19205
19206   if (pop == const0_rtx)
19207     pop = NULL;
19208   gcc_assert (!TARGET_64BIT || !pop);
19209
19210   if (TARGET_MACHO && !TARGET_64BIT)
19211     {
19212 #if TARGET_MACHO
19213       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
19214         fnaddr = machopic_indirect_call_target (fnaddr);
19215 #endif
19216     }
19217   else
19218     {
19219       /* Static functions and indirect calls don't need the pic register.  */
19220       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
19221           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
19222           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
19223         use_reg (&use, pic_offset_table_rtx);
19224     }
19225
19226   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
19227     {
19228       rtx al = gen_rtx_REG (QImode, AX_REG);
19229       emit_move_insn (al, callarg2);
19230       use_reg (&use, al);
19231     }
19232
19233   if (ix86_cmodel == CM_LARGE_PIC
19234       && MEM_P (fnaddr) 
19235       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
19236       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
19237     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
19238   else if (sibcall
19239            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
19240            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
19241     {
19242       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
19243       fnaddr = gen_rtx_MEM (QImode, fnaddr);
19244     }
19245
19246   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
19247   if (retval)
19248     call = gen_rtx_SET (VOIDmode, retval, call);
19249   if (pop)
19250     {
19251       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
19252       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
19253       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
19254     }
19255   if (TARGET_64BIT
19256       && ix86_cfun_abi () == MS_ABI
19257       && (!callarg2 || INTVAL (callarg2) != -2))
19258     {
19259       /* We need to represent that SI and DI registers are clobbered
19260          by SYSV calls.  */
19261       static int clobbered_registers[] = {
19262         XMM6_REG, XMM7_REG, XMM8_REG,
19263         XMM9_REG, XMM10_REG, XMM11_REG,
19264         XMM12_REG, XMM13_REG, XMM14_REG,
19265         XMM15_REG, SI_REG, DI_REG
19266       };
19267       unsigned int i;
19268       rtx vec[ARRAY_SIZE (clobbered_registers) + 2];
19269       rtx unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
19270                                    UNSPEC_MS_TO_SYSV_CALL);
19271
19272       vec[0] = call;
19273       vec[1] = unspec;
19274       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
19275         vec[i + 2] = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
19276                                       ? TImode : DImode,
19277                                       gen_rtx_REG
19278                                         (SSE_REGNO_P (clobbered_registers[i])
19279                                                       ? TImode : DImode,
19280                                          clobbered_registers[i]));
19281
19282       call = gen_rtx_PARALLEL (VOIDmode,
19283                                gen_rtvec_v (ARRAY_SIZE (clobbered_registers)
19284                                + 2, vec));
19285     }
19286
19287   call = emit_call_insn (call);
19288   if (use)
19289     CALL_INSN_FUNCTION_USAGE (call) = use;
19290 }
19291
19292 \f
19293 /* Clear stack slot assignments remembered from previous functions.
19294    This is called from INIT_EXPANDERS once before RTL is emitted for each
19295    function.  */
19296
19297 static struct machine_function *
19298 ix86_init_machine_status (void)
19299 {
19300   struct machine_function *f;
19301
19302   f = GGC_CNEW (struct machine_function);
19303   f->use_fast_prologue_epilogue_nregs = -1;
19304   f->tls_descriptor_call_expanded_p = 0;
19305   f->call_abi = ix86_abi;
19306
19307   return f;
19308 }
19309
19310 /* Return a MEM corresponding to a stack slot with mode MODE.
19311    Allocate a new slot if necessary.
19312
19313    The RTL for a function can have several slots available: N is
19314    which slot to use.  */
19315
19316 rtx
19317 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
19318 {
19319   struct stack_local_entry *s;
19320
19321   gcc_assert (n < MAX_386_STACK_LOCALS);
19322
19323   /* Virtual slot is valid only before vregs are instantiated.  */
19324   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
19325
19326   for (s = ix86_stack_locals; s; s = s->next)
19327     if (s->mode == mode && s->n == n)
19328       return copy_rtx (s->rtl);
19329
19330   s = (struct stack_local_entry *)
19331     ggc_alloc (sizeof (struct stack_local_entry));
19332   s->n = n;
19333   s->mode = mode;
19334   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
19335
19336   s->next = ix86_stack_locals;
19337   ix86_stack_locals = s;
19338   return s->rtl;
19339 }
19340
19341 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
19342
19343 static GTY(()) rtx ix86_tls_symbol;
19344 rtx
19345 ix86_tls_get_addr (void)
19346 {
19347
19348   if (!ix86_tls_symbol)
19349     {
19350       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
19351                                             (TARGET_ANY_GNU_TLS
19352                                              && !TARGET_64BIT)
19353                                             ? "___tls_get_addr"
19354                                             : "__tls_get_addr");
19355     }
19356
19357   return ix86_tls_symbol;
19358 }
19359
19360 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
19361
19362 static GTY(()) rtx ix86_tls_module_base_symbol;
19363 rtx
19364 ix86_tls_module_base (void)
19365 {
19366
19367   if (!ix86_tls_module_base_symbol)
19368     {
19369       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
19370                                                         "_TLS_MODULE_BASE_");
19371       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
19372         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
19373     }
19374
19375   return ix86_tls_module_base_symbol;
19376 }
19377 \f
19378 /* Calculate the length of the memory address in the instruction
19379    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
19380
19381 int
19382 memory_address_length (rtx addr)
19383 {
19384   struct ix86_address parts;
19385   rtx base, index, disp;
19386   int len;
19387   int ok;
19388
19389   if (GET_CODE (addr) == PRE_DEC
19390       || GET_CODE (addr) == POST_INC
19391       || GET_CODE (addr) == PRE_MODIFY
19392       || GET_CODE (addr) == POST_MODIFY)
19393     return 0;
19394
19395   ok = ix86_decompose_address (addr, &parts);
19396   gcc_assert (ok);
19397
19398   if (parts.base && GET_CODE (parts.base) == SUBREG)
19399     parts.base = SUBREG_REG (parts.base);
19400   if (parts.index && GET_CODE (parts.index) == SUBREG)
19401     parts.index = SUBREG_REG (parts.index);
19402
19403   base = parts.base;
19404   index = parts.index;
19405   disp = parts.disp;
19406   len = 0;
19407
19408   /* Rule of thumb:
19409        - esp as the base always wants an index,
19410        - ebp as the base always wants a displacement,
19411        - r12 as the base always wants an index,
19412        - r13 as the base always wants a displacement.  */
19413
19414   /* Register Indirect.  */
19415   if (base && !index && !disp)
19416     {
19417       /* esp (for its index) and ebp (for its displacement) need
19418          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
19419          code.  */
19420       if (REG_P (addr)
19421           && (addr == arg_pointer_rtx
19422               || addr == frame_pointer_rtx
19423               || REGNO (addr) == SP_REG
19424               || REGNO (addr) == BP_REG
19425               || REGNO (addr) == R12_REG
19426               || REGNO (addr) == R13_REG))
19427         len = 1;
19428     }
19429
19430   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
19431      is not disp32, but disp32(%rip), so for disp32
19432      SIB byte is needed, unless print_operand_address
19433      optimizes it into disp32(%rip) or (%rip) is implied
19434      by UNSPEC.  */
19435   else if (disp && !base && !index)
19436     {
19437       len = 4;
19438       if (TARGET_64BIT)
19439         {
19440           rtx symbol = disp;
19441
19442           if (GET_CODE (disp) == CONST)
19443             symbol = XEXP (disp, 0);
19444           if (GET_CODE (symbol) == PLUS
19445               && CONST_INT_P (XEXP (symbol, 1)))
19446             symbol = XEXP (symbol, 0);
19447
19448           if (GET_CODE (symbol) != LABEL_REF
19449               && (GET_CODE (symbol) != SYMBOL_REF
19450                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
19451               && (GET_CODE (symbol) != UNSPEC
19452                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
19453                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
19454             len += 1;
19455         }
19456     }
19457
19458   else
19459     {
19460       /* Find the length of the displacement constant.  */
19461       if (disp)
19462         {
19463           if (base && satisfies_constraint_K (disp))
19464             len = 1;
19465           else
19466             len = 4;
19467         }
19468       /* ebp always wants a displacement.  Similarly r13.  */
19469       else if (base && REG_P (base)
19470                && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
19471         len = 1;
19472
19473       /* An index requires the two-byte modrm form....  */
19474       if (index
19475           /* ...like esp (or r12), which always wants an index.  */
19476           || base == arg_pointer_rtx
19477           || base == frame_pointer_rtx
19478           || (base && REG_P (base)
19479               && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
19480         len += 1;
19481     }
19482
19483   switch (parts.seg)
19484     {
19485     case SEG_FS:
19486     case SEG_GS:
19487       len += 1;
19488       break;
19489     default:
19490       break;
19491     }
19492
19493   return len;
19494 }
19495
19496 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
19497    is set, expect that insn have 8bit immediate alternative.  */
19498 int
19499 ix86_attr_length_immediate_default (rtx insn, int shortform)
19500 {
19501   int len = 0;
19502   int i;
19503   extract_insn_cached (insn);
19504   for (i = recog_data.n_operands - 1; i >= 0; --i)
19505     if (CONSTANT_P (recog_data.operand[i]))
19506       {
19507         enum attr_mode mode = get_attr_mode (insn);
19508
19509         gcc_assert (!len);
19510         if (shortform && CONST_INT_P (recog_data.operand[i]))
19511           {
19512             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
19513             switch (mode)
19514               {
19515               case MODE_QI:
19516                 len = 1;
19517                 continue;
19518               case MODE_HI:
19519                 ival = trunc_int_for_mode (ival, HImode);
19520                 break;
19521               case MODE_SI:
19522                 ival = trunc_int_for_mode (ival, SImode);
19523                 break;
19524               default:
19525                 break;
19526               }
19527             if (IN_RANGE (ival, -128, 127))
19528               {
19529                 len = 1;
19530                 continue;
19531               }
19532           }
19533         switch (mode)
19534           {
19535           case MODE_QI:
19536             len = 1;
19537             break;
19538           case MODE_HI:
19539             len = 2;
19540             break;
19541           case MODE_SI:
19542             len = 4;
19543             break;
19544           /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
19545           case MODE_DI:
19546             len = 4;
19547             break;
19548           default:
19549             fatal_insn ("unknown insn mode", insn);
19550         }
19551       }
19552   return len;
19553 }
19554 /* Compute default value for "length_address" attribute.  */
19555 int
19556 ix86_attr_length_address_default (rtx insn)
19557 {
19558   int i;
19559
19560   if (get_attr_type (insn) == TYPE_LEA)
19561     {
19562       rtx set = PATTERN (insn), addr;
19563
19564       if (GET_CODE (set) == PARALLEL)
19565         set = XVECEXP (set, 0, 0);
19566
19567       gcc_assert (GET_CODE (set) == SET);
19568
19569       addr = SET_SRC (set);
19570       if (TARGET_64BIT && get_attr_mode (insn) == MODE_SI)
19571         {
19572           if (GET_CODE (addr) == ZERO_EXTEND)
19573             addr = XEXP (addr, 0);
19574           if (GET_CODE (addr) == SUBREG)
19575             addr = SUBREG_REG (addr);
19576         }
19577
19578       return memory_address_length (addr);
19579     }
19580
19581   extract_insn_cached (insn);
19582   for (i = recog_data.n_operands - 1; i >= 0; --i)
19583     if (MEM_P (recog_data.operand[i]))
19584       {
19585         constrain_operands_cached (reload_completed);
19586         if (which_alternative != -1)
19587           {
19588             const char *constraints = recog_data.constraints[i];
19589             int alt = which_alternative;
19590
19591             while (*constraints == '=' || *constraints == '+')
19592               constraints++;
19593             while (alt-- > 0)
19594               while (*constraints++ != ',')
19595                 ;
19596             /* Skip ignored operands.  */
19597             if (*constraints == 'X')
19598               continue;
19599           }
19600         return memory_address_length (XEXP (recog_data.operand[i], 0));
19601       }
19602   return 0;
19603 }
19604
19605 /* Compute default value for "length_vex" attribute. It includes
19606    2 or 3 byte VEX prefix and 1 opcode byte.  */
19607
19608 int
19609 ix86_attr_length_vex_default (rtx insn, int has_0f_opcode,
19610                               int has_vex_w)
19611 {
19612   int i;
19613
19614   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
19615      byte VEX prefix.  */
19616   if (!has_0f_opcode || has_vex_w)
19617     return 3 + 1;
19618
19619  /* We can always use 2 byte VEX prefix in 32bit.  */
19620   if (!TARGET_64BIT)
19621     return 2 + 1;
19622
19623   extract_insn_cached (insn);
19624
19625   for (i = recog_data.n_operands - 1; i >= 0; --i)
19626     if (REG_P (recog_data.operand[i]))
19627       {
19628         /* REX.W bit uses 3 byte VEX prefix.  */
19629         if (GET_MODE (recog_data.operand[i]) == DImode
19630             && GENERAL_REG_P (recog_data.operand[i]))
19631           return 3 + 1;
19632       }
19633     else
19634       {
19635         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
19636         if (MEM_P (recog_data.operand[i])
19637             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
19638           return 3 + 1;
19639       }
19640
19641   return 2 + 1;
19642 }
19643 \f
19644 /* Return the maximum number of instructions a cpu can issue.  */
19645
19646 static int
19647 ix86_issue_rate (void)
19648 {
19649   switch (ix86_tune)
19650     {
19651     case PROCESSOR_PENTIUM:
19652     case PROCESSOR_ATOM:
19653     case PROCESSOR_K6:
19654       return 2;
19655
19656     case PROCESSOR_PENTIUMPRO:
19657     case PROCESSOR_PENTIUM4:
19658     case PROCESSOR_ATHLON:
19659     case PROCESSOR_K8:
19660     case PROCESSOR_AMDFAM10:
19661     case PROCESSOR_NOCONA:
19662     case PROCESSOR_GENERIC32:
19663     case PROCESSOR_GENERIC64:
19664       return 3;
19665
19666     case PROCESSOR_CORE2:
19667       return 4;
19668
19669     default:
19670       return 1;
19671     }
19672 }
19673
19674 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
19675    by DEP_INSN and nothing set by DEP_INSN.  */
19676
19677 static int
19678 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
19679 {
19680   rtx set, set2;
19681
19682   /* Simplify the test for uninteresting insns.  */
19683   if (insn_type != TYPE_SETCC
19684       && insn_type != TYPE_ICMOV
19685       && insn_type != TYPE_FCMOV
19686       && insn_type != TYPE_IBR)
19687     return 0;
19688
19689   if ((set = single_set (dep_insn)) != 0)
19690     {
19691       set = SET_DEST (set);
19692       set2 = NULL_RTX;
19693     }
19694   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
19695            && XVECLEN (PATTERN (dep_insn), 0) == 2
19696            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
19697            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
19698     {
19699       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
19700       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
19701     }
19702   else
19703     return 0;
19704
19705   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
19706     return 0;
19707
19708   /* This test is true if the dependent insn reads the flags but
19709      not any other potentially set register.  */
19710   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
19711     return 0;
19712
19713   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
19714     return 0;
19715
19716   return 1;
19717 }
19718
19719 /* Return true iff USE_INSN has a memory address with operands set by
19720    SET_INSN.  */
19721
19722 bool
19723 ix86_agi_dependent (rtx set_insn, rtx use_insn)
19724 {
19725   int i;
19726   extract_insn_cached (use_insn);
19727   for (i = recog_data.n_operands - 1; i >= 0; --i)
19728     if (MEM_P (recog_data.operand[i]))
19729       {
19730         rtx addr = XEXP (recog_data.operand[i], 0);
19731         return modified_in_p (addr, set_insn) != 0;
19732       }
19733   return false;
19734 }
19735
19736 static int
19737 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
19738 {
19739   enum attr_type insn_type, dep_insn_type;
19740   enum attr_memory memory;
19741   rtx set, set2;
19742   int dep_insn_code_number;
19743
19744   /* Anti and output dependencies have zero cost on all CPUs.  */
19745   if (REG_NOTE_KIND (link) != 0)
19746     return 0;
19747
19748   dep_insn_code_number = recog_memoized (dep_insn);
19749
19750   /* If we can't recognize the insns, we can't really do anything.  */
19751   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
19752     return cost;
19753
19754   insn_type = get_attr_type (insn);
19755   dep_insn_type = get_attr_type (dep_insn);
19756
19757   switch (ix86_tune)
19758     {
19759     case PROCESSOR_PENTIUM:
19760       /* Address Generation Interlock adds a cycle of latency.  */
19761       if (insn_type == TYPE_LEA)
19762         {
19763           rtx addr = PATTERN (insn);
19764
19765           if (GET_CODE (addr) == PARALLEL)
19766             addr = XVECEXP (addr, 0, 0);
19767
19768           gcc_assert (GET_CODE (addr) == SET);
19769
19770           addr = SET_SRC (addr);
19771           if (modified_in_p (addr, dep_insn))
19772             cost += 1;
19773         }
19774       else if (ix86_agi_dependent (dep_insn, insn))
19775         cost += 1;
19776
19777       /* ??? Compares pair with jump/setcc.  */
19778       if (ix86_flags_dependent (insn, dep_insn, insn_type))
19779         cost = 0;
19780
19781       /* Floating point stores require value to be ready one cycle earlier.  */
19782       if (insn_type == TYPE_FMOV
19783           && get_attr_memory (insn) == MEMORY_STORE
19784           && !ix86_agi_dependent (dep_insn, insn))
19785         cost += 1;
19786       break;
19787
19788     case PROCESSOR_PENTIUMPRO:
19789       memory = get_attr_memory (insn);
19790
19791       /* INT->FP conversion is expensive.  */
19792       if (get_attr_fp_int_src (dep_insn))
19793         cost += 5;
19794
19795       /* There is one cycle extra latency between an FP op and a store.  */
19796       if (insn_type == TYPE_FMOV
19797           && (set = single_set (dep_insn)) != NULL_RTX
19798           && (set2 = single_set (insn)) != NULL_RTX
19799           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
19800           && MEM_P (SET_DEST (set2)))
19801         cost += 1;
19802
19803       /* Show ability of reorder buffer to hide latency of load by executing
19804          in parallel with previous instruction in case
19805          previous instruction is not needed to compute the address.  */
19806       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
19807           && !ix86_agi_dependent (dep_insn, insn))
19808         {
19809           /* Claim moves to take one cycle, as core can issue one load
19810              at time and the next load can start cycle later.  */
19811           if (dep_insn_type == TYPE_IMOV
19812               || dep_insn_type == TYPE_FMOV)
19813             cost = 1;
19814           else if (cost > 1)
19815             cost--;
19816         }
19817       break;
19818
19819     case PROCESSOR_K6:
19820       memory = get_attr_memory (insn);
19821
19822       /* The esp dependency is resolved before the instruction is really
19823          finished.  */
19824       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
19825           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
19826         return 1;
19827
19828       /* INT->FP conversion is expensive.  */
19829       if (get_attr_fp_int_src (dep_insn))
19830         cost += 5;
19831
19832       /* Show ability of reorder buffer to hide latency of load by executing
19833          in parallel with previous instruction in case
19834          previous instruction is not needed to compute the address.  */
19835       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
19836           && !ix86_agi_dependent (dep_insn, insn))
19837         {
19838           /* Claim moves to take one cycle, as core can issue one load
19839              at time and the next load can start cycle later.  */
19840           if (dep_insn_type == TYPE_IMOV
19841               || dep_insn_type == TYPE_FMOV)
19842             cost = 1;
19843           else if (cost > 2)
19844             cost -= 2;
19845           else
19846             cost = 1;
19847         }
19848       break;
19849
19850     case PROCESSOR_ATHLON:
19851     case PROCESSOR_K8:
19852     case PROCESSOR_AMDFAM10:
19853     case PROCESSOR_ATOM:
19854     case PROCESSOR_GENERIC32:
19855     case PROCESSOR_GENERIC64:
19856       memory = get_attr_memory (insn);
19857
19858       /* Show ability of reorder buffer to hide latency of load by executing
19859          in parallel with previous instruction in case
19860          previous instruction is not needed to compute the address.  */
19861       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
19862           && !ix86_agi_dependent (dep_insn, insn))
19863         {
19864           enum attr_unit unit = get_attr_unit (insn);
19865           int loadcost = 3;
19866
19867           /* Because of the difference between the length of integer and
19868              floating unit pipeline preparation stages, the memory operands
19869              for floating point are cheaper.
19870
19871              ??? For Athlon it the difference is most probably 2.  */
19872           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
19873             loadcost = 3;
19874           else
19875             loadcost = TARGET_ATHLON ? 2 : 0;
19876
19877           if (cost >= loadcost)
19878             cost -= loadcost;
19879           else
19880             cost = 0;
19881         }
19882
19883     default:
19884       break;
19885     }
19886
19887   return cost;
19888 }
19889
19890 /* How many alternative schedules to try.  This should be as wide as the
19891    scheduling freedom in the DFA, but no wider.  Making this value too
19892    large results extra work for the scheduler.  */
19893
19894 static int
19895 ia32_multipass_dfa_lookahead (void)
19896 {
19897   switch (ix86_tune)
19898     {
19899     case PROCESSOR_PENTIUM:
19900       return 2;
19901
19902     case PROCESSOR_PENTIUMPRO:
19903     case PROCESSOR_K6:
19904       return 1;
19905
19906     default:
19907       return 0;
19908     }
19909 }
19910
19911 \f
19912 /* Compute the alignment given to a constant that is being placed in memory.
19913    EXP is the constant and ALIGN is the alignment that the object would
19914    ordinarily have.
19915    The value of this function is used instead of that alignment to align
19916    the object.  */
19917
19918 int
19919 ix86_constant_alignment (tree exp, int align)
19920 {
19921   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
19922       || TREE_CODE (exp) == INTEGER_CST)
19923     {
19924       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
19925         return 64;
19926       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
19927         return 128;
19928     }
19929   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
19930            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
19931     return BITS_PER_WORD;
19932
19933   return align;
19934 }
19935
19936 /* Compute the alignment for a static variable.
19937    TYPE is the data type, and ALIGN is the alignment that
19938    the object would ordinarily have.  The value of this function is used
19939    instead of that alignment to align the object.  */
19940
19941 int
19942 ix86_data_alignment (tree type, int align)
19943 {
19944   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
19945
19946   if (AGGREGATE_TYPE_P (type)
19947       && TYPE_SIZE (type)
19948       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
19949       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
19950           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
19951       && align < max_align)
19952     align = max_align;
19953
19954   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
19955      to 16byte boundary.  */
19956   if (TARGET_64BIT)
19957     {
19958       if (AGGREGATE_TYPE_P (type)
19959            && TYPE_SIZE (type)
19960            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
19961            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
19962                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
19963         return 128;
19964     }
19965
19966   if (TREE_CODE (type) == ARRAY_TYPE)
19967     {
19968       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
19969         return 64;
19970       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
19971         return 128;
19972     }
19973   else if (TREE_CODE (type) == COMPLEX_TYPE)
19974     {
19975
19976       if (TYPE_MODE (type) == DCmode && align < 64)
19977         return 64;
19978       if ((TYPE_MODE (type) == XCmode
19979            || TYPE_MODE (type) == TCmode) && align < 128)
19980         return 128;
19981     }
19982   else if ((TREE_CODE (type) == RECORD_TYPE
19983             || TREE_CODE (type) == UNION_TYPE
19984             || TREE_CODE (type) == QUAL_UNION_TYPE)
19985            && TYPE_FIELDS (type))
19986     {
19987       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
19988         return 64;
19989       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
19990         return 128;
19991     }
19992   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
19993            || TREE_CODE (type) == INTEGER_TYPE)
19994     {
19995       if (TYPE_MODE (type) == DFmode && align < 64)
19996         return 64;
19997       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
19998         return 128;
19999     }
20000
20001   return align;
20002 }
20003
20004 /* Compute the alignment for a local variable or a stack slot.  EXP is
20005    the data type or decl itself, MODE is the widest mode available and
20006    ALIGN is the alignment that the object would ordinarily have.  The
20007    value of this macro is used instead of that alignment to align the
20008    object.  */
20009
20010 unsigned int
20011 ix86_local_alignment (tree exp, enum machine_mode mode,
20012                       unsigned int align)
20013 {
20014   tree type, decl;
20015
20016   if (exp && DECL_P (exp))
20017     {
20018       type = TREE_TYPE (exp);
20019       decl = exp;
20020     }
20021   else
20022     {
20023       type = exp;
20024       decl = NULL;
20025     }
20026
20027   /* Don't do dynamic stack realignment for long long objects with
20028      -mpreferred-stack-boundary=2.  */
20029   if (!TARGET_64BIT
20030       && align == 64
20031       && ix86_preferred_stack_boundary < 64
20032       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
20033       && (!type || !TYPE_USER_ALIGN (type))
20034       && (!decl || !DECL_USER_ALIGN (decl)))
20035     align = 32;
20036
20037   /* If TYPE is NULL, we are allocating a stack slot for caller-save
20038      register in MODE.  We will return the largest alignment of XF
20039      and DF.  */
20040   if (!type)
20041     {
20042       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
20043         align = GET_MODE_ALIGNMENT (DFmode);
20044       return align;
20045     }
20046
20047   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
20048      to 16byte boundary.  */
20049   if (TARGET_64BIT)
20050     {
20051       if (AGGREGATE_TYPE_P (type)
20052            && TYPE_SIZE (type)
20053            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
20054            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
20055                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
20056         return 128;
20057     }
20058   if (TREE_CODE (type) == ARRAY_TYPE)
20059     {
20060       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
20061         return 64;
20062       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
20063         return 128;
20064     }
20065   else if (TREE_CODE (type) == COMPLEX_TYPE)
20066     {
20067       if (TYPE_MODE (type) == DCmode && align < 64)
20068         return 64;
20069       if ((TYPE_MODE (type) == XCmode
20070            || TYPE_MODE (type) == TCmode) && align < 128)
20071         return 128;
20072     }
20073   else if ((TREE_CODE (type) == RECORD_TYPE
20074             || TREE_CODE (type) == UNION_TYPE
20075             || TREE_CODE (type) == QUAL_UNION_TYPE)
20076            && TYPE_FIELDS (type))
20077     {
20078       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
20079         return 64;
20080       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
20081         return 128;
20082     }
20083   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
20084            || TREE_CODE (type) == INTEGER_TYPE)
20085     {
20086
20087       if (TYPE_MODE (type) == DFmode && align < 64)
20088         return 64;
20089       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
20090         return 128;
20091     }
20092   return align;
20093 }
20094
20095 /* Compute the minimum required alignment for dynamic stack realignment
20096    purposes for a local variable, parameter or a stack slot.  EXP is
20097    the data type or decl itself, MODE is its mode and ALIGN is the
20098    alignment that the object would ordinarily have.  */
20099
20100 unsigned int
20101 ix86_minimum_alignment (tree exp, enum machine_mode mode,
20102                         unsigned int align)
20103 {
20104   tree type, decl;
20105
20106   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
20107     return align;
20108
20109   if (exp && DECL_P (exp))
20110     {
20111       type = TREE_TYPE (exp);
20112       decl = exp;
20113     }
20114   else
20115     {
20116       type = exp;
20117       decl = NULL;
20118     }
20119
20120   /* Don't do dynamic stack realignment for long long objects with
20121      -mpreferred-stack-boundary=2.  */
20122   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
20123       && (!type || !TYPE_USER_ALIGN (type))
20124       && (!decl || !DECL_USER_ALIGN (decl)))
20125     return 32;
20126
20127   return align;
20128 }
20129 \f
20130 /* Find a location for the static chain incoming to a nested function.
20131    This is a register, unless all free registers are used by arguments.  */
20132
20133 static rtx
20134 ix86_static_chain (const_tree fndecl, bool incoming_p)
20135 {
20136   unsigned regno;
20137
20138   if (!DECL_STATIC_CHAIN (fndecl))
20139     return NULL;
20140
20141   if (TARGET_64BIT)
20142     {
20143       /* We always use R10 in 64-bit mode.  */
20144       regno = R10_REG;
20145     }
20146   else
20147     {
20148       tree fntype;
20149       /* By default in 32-bit mode we use ECX to pass the static chain.  */
20150       regno = CX_REG;
20151
20152       fntype = TREE_TYPE (fndecl);
20153       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
20154         {
20155           /* Fastcall functions use ecx/edx for arguments, which leaves
20156              us with EAX for the static chain.  */
20157           regno = AX_REG;
20158         }
20159       else if (ix86_function_regparm (fntype, fndecl) == 3)
20160         {
20161           /* For regparm 3, we have no free call-clobbered registers in
20162              which to store the static chain.  In order to implement this,
20163              we have the trampoline push the static chain to the stack.
20164              However, we can't push a value below the return address when
20165              we call the nested function directly, so we have to use an
20166              alternate entry point.  For this we use ESI, and have the
20167              alternate entry point push ESI, so that things appear the
20168              same once we're executing the nested function.  */
20169           if (incoming_p)
20170             {
20171               if (fndecl == current_function_decl)
20172                 ix86_static_chain_on_stack = true;
20173               return gen_frame_mem (SImode,
20174                                     plus_constant (arg_pointer_rtx, -8));
20175             }
20176           regno = SI_REG;
20177         }
20178     }
20179
20180   return gen_rtx_REG (Pmode, regno);
20181 }
20182
20183 /* Emit RTL insns to initialize the variable parts of a trampoline.
20184    FNDECL is the decl of the target address; M_TRAMP is a MEM for 
20185    the trampoline, and CHAIN_VALUE is an RTX for the static chain
20186    to be passed to the target function.  */
20187
20188 static void
20189 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
20190 {
20191   rtx mem, fnaddr;
20192
20193   fnaddr = XEXP (DECL_RTL (fndecl), 0);
20194
20195   if (!TARGET_64BIT)
20196     {
20197       rtx disp, chain;
20198       int opcode;
20199
20200       /* Depending on the static chain location, either load a register
20201          with a constant, or push the constant to the stack.  All of the
20202          instructions are the same size.  */
20203       chain = ix86_static_chain (fndecl, true);
20204       if (REG_P (chain))
20205         {
20206           if (REGNO (chain) == CX_REG)
20207             opcode = 0xb9;
20208           else if (REGNO (chain) == AX_REG)
20209             opcode = 0xb8;
20210           else
20211             gcc_unreachable ();
20212         }
20213       else
20214         opcode = 0x68;
20215
20216       mem = adjust_address (m_tramp, QImode, 0);
20217       emit_move_insn (mem, gen_int_mode (opcode, QImode));
20218
20219       mem = adjust_address (m_tramp, SImode, 1);
20220       emit_move_insn (mem, chain_value);
20221
20222       /* Compute offset from the end of the jmp to the target function.
20223          In the case in which the trampoline stores the static chain on
20224          the stack, we need to skip the first insn which pushes the
20225          (call-saved) register static chain; this push is 1 byte.  */
20226       disp = expand_binop (SImode, sub_optab, fnaddr,
20227                            plus_constant (XEXP (m_tramp, 0),
20228                                           MEM_P (chain) ? 9 : 10),
20229                            NULL_RTX, 1, OPTAB_DIRECT);
20230
20231       mem = adjust_address (m_tramp, QImode, 5);
20232       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
20233
20234       mem = adjust_address (m_tramp, SImode, 6);
20235       emit_move_insn (mem, disp);
20236     }
20237   else
20238     {
20239       int offset = 0;
20240
20241       /* Load the function address to r11.  Try to load address using
20242          the shorter movl instead of movabs.  We may want to support
20243          movq for kernel mode, but kernel does not use trampolines at
20244          the moment.  */
20245       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
20246         {
20247           fnaddr = copy_to_mode_reg (DImode, fnaddr);
20248
20249           mem = adjust_address (m_tramp, HImode, offset);
20250           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
20251
20252           mem = adjust_address (m_tramp, SImode, offset + 2);
20253           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
20254           offset += 6;
20255         }
20256       else
20257         {
20258           mem = adjust_address (m_tramp, HImode, offset);
20259           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
20260
20261           mem = adjust_address (m_tramp, DImode, offset + 2);
20262           emit_move_insn (mem, fnaddr);
20263           offset += 10;
20264         }
20265
20266       /* Load static chain using movabs to r10.  */
20267       mem = adjust_address (m_tramp, HImode, offset);
20268       emit_move_insn (mem, gen_int_mode (0xba49, HImode));
20269
20270       mem = adjust_address (m_tramp, DImode, offset + 2);
20271       emit_move_insn (mem, chain_value);
20272       offset += 10;
20273
20274       /* Jump to r11; the last (unused) byte is a nop, only there to
20275          pad the write out to a single 32-bit store.  */
20276       mem = adjust_address (m_tramp, SImode, offset);
20277       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
20278       offset += 4;
20279
20280       gcc_assert (offset <= TRAMPOLINE_SIZE);
20281     }
20282
20283 #ifdef ENABLE_EXECUTE_STACK
20284 #ifdef CHECK_EXECUTE_STACK_ENABLED
20285   if (CHECK_EXECUTE_STACK_ENABLED)
20286 #endif
20287   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
20288                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
20289 #endif
20290 }
20291 \f
20292 /* The following file contains several enumerations and data structures
20293    built from the definitions in i386-builtin-types.def.  */
20294
20295 #include "i386-builtin-types.inc"
20296
20297 /* Table for the ix86 builtin non-function types.  */
20298 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
20299
20300 /* Retrieve an element from the above table, building some of
20301    the types lazily.  */
20302
20303 static tree
20304 ix86_get_builtin_type (enum ix86_builtin_type tcode)
20305 {
20306   unsigned int index;
20307   tree type, itype;
20308
20309   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
20310
20311   type = ix86_builtin_type_tab[(int) tcode];
20312   if (type != NULL)
20313     return type;
20314
20315   gcc_assert (tcode > IX86_BT_LAST_PRIM);
20316   if (tcode <= IX86_BT_LAST_VECT)
20317     {
20318       enum machine_mode mode;
20319
20320       index = tcode - IX86_BT_LAST_PRIM - 1;
20321       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
20322       mode = ix86_builtin_type_vect_mode[index];
20323
20324       type = build_vector_type_for_mode (itype, mode);
20325     }
20326   else
20327     {
20328       int quals;
20329
20330       index = tcode - IX86_BT_LAST_VECT - 1;
20331       if (tcode <= IX86_BT_LAST_PTR)
20332         quals = TYPE_UNQUALIFIED;
20333       else
20334         quals = TYPE_QUAL_CONST;
20335
20336       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
20337       if (quals != TYPE_UNQUALIFIED)
20338         itype = build_qualified_type (itype, quals);
20339
20340       type = build_pointer_type (itype);
20341     }
20342
20343   ix86_builtin_type_tab[(int) tcode] = type;
20344   return type;
20345 }
20346
20347 /* Table for the ix86 builtin function types.  */
20348 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
20349
20350 /* Retrieve an element from the above table, building some of
20351    the types lazily.  */
20352
20353 static tree
20354 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
20355 {
20356   tree type;
20357
20358   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
20359
20360   type = ix86_builtin_func_type_tab[(int) tcode];
20361   if (type != NULL)
20362     return type;
20363
20364   if (tcode <= IX86_BT_LAST_FUNC)
20365     {
20366       unsigned start = ix86_builtin_func_start[(int) tcode];
20367       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
20368       tree rtype, atype, args = void_list_node;
20369       unsigned i;
20370
20371       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
20372       for (i = after - 1; i > start; --i)
20373         {
20374           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
20375           args = tree_cons (NULL, atype, args);
20376         }
20377
20378       type = build_function_type (rtype, args);
20379     }
20380   else
20381     {
20382       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
20383       enum ix86_builtin_func_type icode;
20384
20385       icode = ix86_builtin_func_alias_base[index];
20386       type = ix86_get_builtin_func_type (icode);
20387     }
20388
20389   ix86_builtin_func_type_tab[(int) tcode] = type;
20390   return type;
20391 }
20392
20393
20394 /* Codes for all the SSE/MMX builtins.  */
20395 enum ix86_builtins
20396 {
20397   IX86_BUILTIN_ADDPS,
20398   IX86_BUILTIN_ADDSS,
20399   IX86_BUILTIN_DIVPS,
20400   IX86_BUILTIN_DIVSS,
20401   IX86_BUILTIN_MULPS,
20402   IX86_BUILTIN_MULSS,
20403   IX86_BUILTIN_SUBPS,
20404   IX86_BUILTIN_SUBSS,
20405
20406   IX86_BUILTIN_CMPEQPS,
20407   IX86_BUILTIN_CMPLTPS,
20408   IX86_BUILTIN_CMPLEPS,
20409   IX86_BUILTIN_CMPGTPS,
20410   IX86_BUILTIN_CMPGEPS,
20411   IX86_BUILTIN_CMPNEQPS,
20412   IX86_BUILTIN_CMPNLTPS,
20413   IX86_BUILTIN_CMPNLEPS,
20414   IX86_BUILTIN_CMPNGTPS,
20415   IX86_BUILTIN_CMPNGEPS,
20416   IX86_BUILTIN_CMPORDPS,
20417   IX86_BUILTIN_CMPUNORDPS,
20418   IX86_BUILTIN_CMPEQSS,
20419   IX86_BUILTIN_CMPLTSS,
20420   IX86_BUILTIN_CMPLESS,
20421   IX86_BUILTIN_CMPNEQSS,
20422   IX86_BUILTIN_CMPNLTSS,
20423   IX86_BUILTIN_CMPNLESS,
20424   IX86_BUILTIN_CMPNGTSS,
20425   IX86_BUILTIN_CMPNGESS,
20426   IX86_BUILTIN_CMPORDSS,
20427   IX86_BUILTIN_CMPUNORDSS,
20428
20429   IX86_BUILTIN_COMIEQSS,
20430   IX86_BUILTIN_COMILTSS,
20431   IX86_BUILTIN_COMILESS,
20432   IX86_BUILTIN_COMIGTSS,
20433   IX86_BUILTIN_COMIGESS,
20434   IX86_BUILTIN_COMINEQSS,
20435   IX86_BUILTIN_UCOMIEQSS,
20436   IX86_BUILTIN_UCOMILTSS,
20437   IX86_BUILTIN_UCOMILESS,
20438   IX86_BUILTIN_UCOMIGTSS,
20439   IX86_BUILTIN_UCOMIGESS,
20440   IX86_BUILTIN_UCOMINEQSS,
20441
20442   IX86_BUILTIN_CVTPI2PS,
20443   IX86_BUILTIN_CVTPS2PI,
20444   IX86_BUILTIN_CVTSI2SS,
20445   IX86_BUILTIN_CVTSI642SS,
20446   IX86_BUILTIN_CVTSS2SI,
20447   IX86_BUILTIN_CVTSS2SI64,
20448   IX86_BUILTIN_CVTTPS2PI,
20449   IX86_BUILTIN_CVTTSS2SI,
20450   IX86_BUILTIN_CVTTSS2SI64,
20451
20452   IX86_BUILTIN_MAXPS,
20453   IX86_BUILTIN_MAXSS,
20454   IX86_BUILTIN_MINPS,
20455   IX86_BUILTIN_MINSS,
20456
20457   IX86_BUILTIN_LOADUPS,
20458   IX86_BUILTIN_STOREUPS,
20459   IX86_BUILTIN_MOVSS,
20460
20461   IX86_BUILTIN_MOVHLPS,
20462   IX86_BUILTIN_MOVLHPS,
20463   IX86_BUILTIN_LOADHPS,
20464   IX86_BUILTIN_LOADLPS,
20465   IX86_BUILTIN_STOREHPS,
20466   IX86_BUILTIN_STORELPS,
20467
20468   IX86_BUILTIN_MASKMOVQ,
20469   IX86_BUILTIN_MOVMSKPS,
20470   IX86_BUILTIN_PMOVMSKB,
20471
20472   IX86_BUILTIN_MOVNTPS,
20473   IX86_BUILTIN_MOVNTQ,
20474
20475   IX86_BUILTIN_LOADDQU,
20476   IX86_BUILTIN_STOREDQU,
20477
20478   IX86_BUILTIN_PACKSSWB,
20479   IX86_BUILTIN_PACKSSDW,
20480   IX86_BUILTIN_PACKUSWB,
20481
20482   IX86_BUILTIN_PADDB,
20483   IX86_BUILTIN_PADDW,
20484   IX86_BUILTIN_PADDD,
20485   IX86_BUILTIN_PADDQ,
20486   IX86_BUILTIN_PADDSB,
20487   IX86_BUILTIN_PADDSW,
20488   IX86_BUILTIN_PADDUSB,
20489   IX86_BUILTIN_PADDUSW,
20490   IX86_BUILTIN_PSUBB,
20491   IX86_BUILTIN_PSUBW,
20492   IX86_BUILTIN_PSUBD,
20493   IX86_BUILTIN_PSUBQ,
20494   IX86_BUILTIN_PSUBSB,
20495   IX86_BUILTIN_PSUBSW,
20496   IX86_BUILTIN_PSUBUSB,
20497   IX86_BUILTIN_PSUBUSW,
20498
20499   IX86_BUILTIN_PAND,
20500   IX86_BUILTIN_PANDN,
20501   IX86_BUILTIN_POR,
20502   IX86_BUILTIN_PXOR,
20503
20504   IX86_BUILTIN_PAVGB,
20505   IX86_BUILTIN_PAVGW,
20506
20507   IX86_BUILTIN_PCMPEQB,
20508   IX86_BUILTIN_PCMPEQW,
20509   IX86_BUILTIN_PCMPEQD,
20510   IX86_BUILTIN_PCMPGTB,
20511   IX86_BUILTIN_PCMPGTW,
20512   IX86_BUILTIN_PCMPGTD,
20513
20514   IX86_BUILTIN_PMADDWD,
20515
20516   IX86_BUILTIN_PMAXSW,
20517   IX86_BUILTIN_PMAXUB,
20518   IX86_BUILTIN_PMINSW,
20519   IX86_BUILTIN_PMINUB,
20520
20521   IX86_BUILTIN_PMULHUW,
20522   IX86_BUILTIN_PMULHW,
20523   IX86_BUILTIN_PMULLW,
20524
20525   IX86_BUILTIN_PSADBW,
20526   IX86_BUILTIN_PSHUFW,
20527
20528   IX86_BUILTIN_PSLLW,
20529   IX86_BUILTIN_PSLLD,
20530   IX86_BUILTIN_PSLLQ,
20531   IX86_BUILTIN_PSRAW,
20532   IX86_BUILTIN_PSRAD,
20533   IX86_BUILTIN_PSRLW,
20534   IX86_BUILTIN_PSRLD,
20535   IX86_BUILTIN_PSRLQ,
20536   IX86_BUILTIN_PSLLWI,
20537   IX86_BUILTIN_PSLLDI,
20538   IX86_BUILTIN_PSLLQI,
20539   IX86_BUILTIN_PSRAWI,
20540   IX86_BUILTIN_PSRADI,
20541   IX86_BUILTIN_PSRLWI,
20542   IX86_BUILTIN_PSRLDI,
20543   IX86_BUILTIN_PSRLQI,
20544
20545   IX86_BUILTIN_PUNPCKHBW,
20546   IX86_BUILTIN_PUNPCKHWD,
20547   IX86_BUILTIN_PUNPCKHDQ,
20548   IX86_BUILTIN_PUNPCKLBW,
20549   IX86_BUILTIN_PUNPCKLWD,
20550   IX86_BUILTIN_PUNPCKLDQ,
20551
20552   IX86_BUILTIN_SHUFPS,
20553
20554   IX86_BUILTIN_RCPPS,
20555   IX86_BUILTIN_RCPSS,
20556   IX86_BUILTIN_RSQRTPS,
20557   IX86_BUILTIN_RSQRTPS_NR,
20558   IX86_BUILTIN_RSQRTSS,
20559   IX86_BUILTIN_RSQRTF,
20560   IX86_BUILTIN_SQRTPS,
20561   IX86_BUILTIN_SQRTPS_NR,
20562   IX86_BUILTIN_SQRTSS,
20563
20564   IX86_BUILTIN_UNPCKHPS,
20565   IX86_BUILTIN_UNPCKLPS,
20566
20567   IX86_BUILTIN_ANDPS,
20568   IX86_BUILTIN_ANDNPS,
20569   IX86_BUILTIN_ORPS,
20570   IX86_BUILTIN_XORPS,
20571
20572   IX86_BUILTIN_EMMS,
20573   IX86_BUILTIN_LDMXCSR,
20574   IX86_BUILTIN_STMXCSR,
20575   IX86_BUILTIN_SFENCE,
20576
20577   /* 3DNow! Original */
20578   IX86_BUILTIN_FEMMS,
20579   IX86_BUILTIN_PAVGUSB,
20580   IX86_BUILTIN_PF2ID,
20581   IX86_BUILTIN_PFACC,
20582   IX86_BUILTIN_PFADD,
20583   IX86_BUILTIN_PFCMPEQ,
20584   IX86_BUILTIN_PFCMPGE,
20585   IX86_BUILTIN_PFCMPGT,
20586   IX86_BUILTIN_PFMAX,
20587   IX86_BUILTIN_PFMIN,
20588   IX86_BUILTIN_PFMUL,
20589   IX86_BUILTIN_PFRCP,
20590   IX86_BUILTIN_PFRCPIT1,
20591   IX86_BUILTIN_PFRCPIT2,
20592   IX86_BUILTIN_PFRSQIT1,
20593   IX86_BUILTIN_PFRSQRT,
20594   IX86_BUILTIN_PFSUB,
20595   IX86_BUILTIN_PFSUBR,
20596   IX86_BUILTIN_PI2FD,
20597   IX86_BUILTIN_PMULHRW,
20598
20599   /* 3DNow! Athlon Extensions */
20600   IX86_BUILTIN_PF2IW,
20601   IX86_BUILTIN_PFNACC,
20602   IX86_BUILTIN_PFPNACC,
20603   IX86_BUILTIN_PI2FW,
20604   IX86_BUILTIN_PSWAPDSI,
20605   IX86_BUILTIN_PSWAPDSF,
20606
20607   /* SSE2 */
20608   IX86_BUILTIN_ADDPD,
20609   IX86_BUILTIN_ADDSD,
20610   IX86_BUILTIN_DIVPD,
20611   IX86_BUILTIN_DIVSD,
20612   IX86_BUILTIN_MULPD,
20613   IX86_BUILTIN_MULSD,
20614   IX86_BUILTIN_SUBPD,
20615   IX86_BUILTIN_SUBSD,
20616
20617   IX86_BUILTIN_CMPEQPD,
20618   IX86_BUILTIN_CMPLTPD,
20619   IX86_BUILTIN_CMPLEPD,
20620   IX86_BUILTIN_CMPGTPD,
20621   IX86_BUILTIN_CMPGEPD,
20622   IX86_BUILTIN_CMPNEQPD,
20623   IX86_BUILTIN_CMPNLTPD,
20624   IX86_BUILTIN_CMPNLEPD,
20625   IX86_BUILTIN_CMPNGTPD,
20626   IX86_BUILTIN_CMPNGEPD,
20627   IX86_BUILTIN_CMPORDPD,
20628   IX86_BUILTIN_CMPUNORDPD,
20629   IX86_BUILTIN_CMPEQSD,
20630   IX86_BUILTIN_CMPLTSD,
20631   IX86_BUILTIN_CMPLESD,
20632   IX86_BUILTIN_CMPNEQSD,
20633   IX86_BUILTIN_CMPNLTSD,
20634   IX86_BUILTIN_CMPNLESD,
20635   IX86_BUILTIN_CMPORDSD,
20636   IX86_BUILTIN_CMPUNORDSD,
20637
20638   IX86_BUILTIN_COMIEQSD,
20639   IX86_BUILTIN_COMILTSD,
20640   IX86_BUILTIN_COMILESD,
20641   IX86_BUILTIN_COMIGTSD,
20642   IX86_BUILTIN_COMIGESD,
20643   IX86_BUILTIN_COMINEQSD,
20644   IX86_BUILTIN_UCOMIEQSD,
20645   IX86_BUILTIN_UCOMILTSD,
20646   IX86_BUILTIN_UCOMILESD,
20647   IX86_BUILTIN_UCOMIGTSD,
20648   IX86_BUILTIN_UCOMIGESD,
20649   IX86_BUILTIN_UCOMINEQSD,
20650
20651   IX86_BUILTIN_MAXPD,
20652   IX86_BUILTIN_MAXSD,
20653   IX86_BUILTIN_MINPD,
20654   IX86_BUILTIN_MINSD,
20655
20656   IX86_BUILTIN_ANDPD,
20657   IX86_BUILTIN_ANDNPD,
20658   IX86_BUILTIN_ORPD,
20659   IX86_BUILTIN_XORPD,
20660
20661   IX86_BUILTIN_SQRTPD,
20662   IX86_BUILTIN_SQRTSD,
20663
20664   IX86_BUILTIN_UNPCKHPD,
20665   IX86_BUILTIN_UNPCKLPD,
20666
20667   IX86_BUILTIN_SHUFPD,
20668
20669   IX86_BUILTIN_LOADUPD,
20670   IX86_BUILTIN_STOREUPD,
20671   IX86_BUILTIN_MOVSD,
20672
20673   IX86_BUILTIN_LOADHPD,
20674   IX86_BUILTIN_LOADLPD,
20675
20676   IX86_BUILTIN_CVTDQ2PD,
20677   IX86_BUILTIN_CVTDQ2PS,
20678
20679   IX86_BUILTIN_CVTPD2DQ,
20680   IX86_BUILTIN_CVTPD2PI,
20681   IX86_BUILTIN_CVTPD2PS,
20682   IX86_BUILTIN_CVTTPD2DQ,
20683   IX86_BUILTIN_CVTTPD2PI,
20684
20685   IX86_BUILTIN_CVTPI2PD,
20686   IX86_BUILTIN_CVTSI2SD,
20687   IX86_BUILTIN_CVTSI642SD,
20688
20689   IX86_BUILTIN_CVTSD2SI,
20690   IX86_BUILTIN_CVTSD2SI64,
20691   IX86_BUILTIN_CVTSD2SS,
20692   IX86_BUILTIN_CVTSS2SD,
20693   IX86_BUILTIN_CVTTSD2SI,
20694   IX86_BUILTIN_CVTTSD2SI64,
20695
20696   IX86_BUILTIN_CVTPS2DQ,
20697   IX86_BUILTIN_CVTPS2PD,
20698   IX86_BUILTIN_CVTTPS2DQ,
20699
20700   IX86_BUILTIN_MOVNTI,
20701   IX86_BUILTIN_MOVNTPD,
20702   IX86_BUILTIN_MOVNTDQ,
20703
20704   IX86_BUILTIN_MOVQ128,
20705
20706   /* SSE2 MMX */
20707   IX86_BUILTIN_MASKMOVDQU,
20708   IX86_BUILTIN_MOVMSKPD,
20709   IX86_BUILTIN_PMOVMSKB128,
20710
20711   IX86_BUILTIN_PACKSSWB128,
20712   IX86_BUILTIN_PACKSSDW128,
20713   IX86_BUILTIN_PACKUSWB128,
20714
20715   IX86_BUILTIN_PADDB128,
20716   IX86_BUILTIN_PADDW128,
20717   IX86_BUILTIN_PADDD128,
20718   IX86_BUILTIN_PADDQ128,
20719   IX86_BUILTIN_PADDSB128,
20720   IX86_BUILTIN_PADDSW128,
20721   IX86_BUILTIN_PADDUSB128,
20722   IX86_BUILTIN_PADDUSW128,
20723   IX86_BUILTIN_PSUBB128,
20724   IX86_BUILTIN_PSUBW128,
20725   IX86_BUILTIN_PSUBD128,
20726   IX86_BUILTIN_PSUBQ128,
20727   IX86_BUILTIN_PSUBSB128,
20728   IX86_BUILTIN_PSUBSW128,
20729   IX86_BUILTIN_PSUBUSB128,
20730   IX86_BUILTIN_PSUBUSW128,
20731
20732   IX86_BUILTIN_PAND128,
20733   IX86_BUILTIN_PANDN128,
20734   IX86_BUILTIN_POR128,
20735   IX86_BUILTIN_PXOR128,
20736
20737   IX86_BUILTIN_PAVGB128,
20738   IX86_BUILTIN_PAVGW128,
20739
20740   IX86_BUILTIN_PCMPEQB128,
20741   IX86_BUILTIN_PCMPEQW128,
20742   IX86_BUILTIN_PCMPEQD128,
20743   IX86_BUILTIN_PCMPGTB128,
20744   IX86_BUILTIN_PCMPGTW128,
20745   IX86_BUILTIN_PCMPGTD128,
20746
20747   IX86_BUILTIN_PMADDWD128,
20748
20749   IX86_BUILTIN_PMAXSW128,
20750   IX86_BUILTIN_PMAXUB128,
20751   IX86_BUILTIN_PMINSW128,
20752   IX86_BUILTIN_PMINUB128,
20753
20754   IX86_BUILTIN_PMULUDQ,
20755   IX86_BUILTIN_PMULUDQ128,
20756   IX86_BUILTIN_PMULHUW128,
20757   IX86_BUILTIN_PMULHW128,
20758   IX86_BUILTIN_PMULLW128,
20759
20760   IX86_BUILTIN_PSADBW128,
20761   IX86_BUILTIN_PSHUFHW,
20762   IX86_BUILTIN_PSHUFLW,
20763   IX86_BUILTIN_PSHUFD,
20764
20765   IX86_BUILTIN_PSLLDQI128,
20766   IX86_BUILTIN_PSLLWI128,
20767   IX86_BUILTIN_PSLLDI128,
20768   IX86_BUILTIN_PSLLQI128,
20769   IX86_BUILTIN_PSRAWI128,
20770   IX86_BUILTIN_PSRADI128,
20771   IX86_BUILTIN_PSRLDQI128,
20772   IX86_BUILTIN_PSRLWI128,
20773   IX86_BUILTIN_PSRLDI128,
20774   IX86_BUILTIN_PSRLQI128,
20775
20776   IX86_BUILTIN_PSLLDQ128,
20777   IX86_BUILTIN_PSLLW128,
20778   IX86_BUILTIN_PSLLD128,
20779   IX86_BUILTIN_PSLLQ128,
20780   IX86_BUILTIN_PSRAW128,
20781   IX86_BUILTIN_PSRAD128,
20782   IX86_BUILTIN_PSRLW128,
20783   IX86_BUILTIN_PSRLD128,
20784   IX86_BUILTIN_PSRLQ128,
20785
20786   IX86_BUILTIN_PUNPCKHBW128,
20787   IX86_BUILTIN_PUNPCKHWD128,
20788   IX86_BUILTIN_PUNPCKHDQ128,
20789   IX86_BUILTIN_PUNPCKHQDQ128,
20790   IX86_BUILTIN_PUNPCKLBW128,
20791   IX86_BUILTIN_PUNPCKLWD128,
20792   IX86_BUILTIN_PUNPCKLDQ128,
20793   IX86_BUILTIN_PUNPCKLQDQ128,
20794
20795   IX86_BUILTIN_CLFLUSH,
20796   IX86_BUILTIN_MFENCE,
20797   IX86_BUILTIN_LFENCE,
20798
20799   IX86_BUILTIN_BSRSI,
20800   IX86_BUILTIN_BSRDI,
20801   IX86_BUILTIN_RDPMC,
20802   IX86_BUILTIN_RDTSC,
20803   IX86_BUILTIN_RDTSCP,
20804   IX86_BUILTIN_ROLQI,
20805   IX86_BUILTIN_ROLHI,
20806   IX86_BUILTIN_RORQI,
20807   IX86_BUILTIN_RORHI,
20808
20809   /* SSE3.  */
20810   IX86_BUILTIN_ADDSUBPS,
20811   IX86_BUILTIN_HADDPS,
20812   IX86_BUILTIN_HSUBPS,
20813   IX86_BUILTIN_MOVSHDUP,
20814   IX86_BUILTIN_MOVSLDUP,
20815   IX86_BUILTIN_ADDSUBPD,
20816   IX86_BUILTIN_HADDPD,
20817   IX86_BUILTIN_HSUBPD,
20818   IX86_BUILTIN_LDDQU,
20819
20820   IX86_BUILTIN_MONITOR,
20821   IX86_BUILTIN_MWAIT,
20822
20823   /* SSSE3.  */
20824   IX86_BUILTIN_PHADDW,
20825   IX86_BUILTIN_PHADDD,
20826   IX86_BUILTIN_PHADDSW,
20827   IX86_BUILTIN_PHSUBW,
20828   IX86_BUILTIN_PHSUBD,
20829   IX86_BUILTIN_PHSUBSW,
20830   IX86_BUILTIN_PMADDUBSW,
20831   IX86_BUILTIN_PMULHRSW,
20832   IX86_BUILTIN_PSHUFB,
20833   IX86_BUILTIN_PSIGNB,
20834   IX86_BUILTIN_PSIGNW,
20835   IX86_BUILTIN_PSIGND,
20836   IX86_BUILTIN_PALIGNR,
20837   IX86_BUILTIN_PABSB,
20838   IX86_BUILTIN_PABSW,
20839   IX86_BUILTIN_PABSD,
20840
20841   IX86_BUILTIN_PHADDW128,
20842   IX86_BUILTIN_PHADDD128,
20843   IX86_BUILTIN_PHADDSW128,
20844   IX86_BUILTIN_PHSUBW128,
20845   IX86_BUILTIN_PHSUBD128,
20846   IX86_BUILTIN_PHSUBSW128,
20847   IX86_BUILTIN_PMADDUBSW128,
20848   IX86_BUILTIN_PMULHRSW128,
20849   IX86_BUILTIN_PSHUFB128,
20850   IX86_BUILTIN_PSIGNB128,
20851   IX86_BUILTIN_PSIGNW128,
20852   IX86_BUILTIN_PSIGND128,
20853   IX86_BUILTIN_PALIGNR128,
20854   IX86_BUILTIN_PABSB128,
20855   IX86_BUILTIN_PABSW128,
20856   IX86_BUILTIN_PABSD128,
20857
20858   /* AMDFAM10 - SSE4A New Instructions.  */
20859   IX86_BUILTIN_MOVNTSD,
20860   IX86_BUILTIN_MOVNTSS,
20861   IX86_BUILTIN_EXTRQI,
20862   IX86_BUILTIN_EXTRQ,
20863   IX86_BUILTIN_INSERTQI,
20864   IX86_BUILTIN_INSERTQ,
20865
20866   /* SSE4.1.  */
20867   IX86_BUILTIN_BLENDPD,
20868   IX86_BUILTIN_BLENDPS,
20869   IX86_BUILTIN_BLENDVPD,
20870   IX86_BUILTIN_BLENDVPS,
20871   IX86_BUILTIN_PBLENDVB128,
20872   IX86_BUILTIN_PBLENDW128,
20873
20874   IX86_BUILTIN_DPPD,
20875   IX86_BUILTIN_DPPS,
20876
20877   IX86_BUILTIN_INSERTPS128,
20878
20879   IX86_BUILTIN_MOVNTDQA,
20880   IX86_BUILTIN_MPSADBW128,
20881   IX86_BUILTIN_PACKUSDW128,
20882   IX86_BUILTIN_PCMPEQQ,
20883   IX86_BUILTIN_PHMINPOSUW128,
20884
20885   IX86_BUILTIN_PMAXSB128,
20886   IX86_BUILTIN_PMAXSD128,
20887   IX86_BUILTIN_PMAXUD128,
20888   IX86_BUILTIN_PMAXUW128,
20889
20890   IX86_BUILTIN_PMINSB128,
20891   IX86_BUILTIN_PMINSD128,
20892   IX86_BUILTIN_PMINUD128,
20893   IX86_BUILTIN_PMINUW128,
20894
20895   IX86_BUILTIN_PMOVSXBW128,
20896   IX86_BUILTIN_PMOVSXBD128,
20897   IX86_BUILTIN_PMOVSXBQ128,
20898   IX86_BUILTIN_PMOVSXWD128,
20899   IX86_BUILTIN_PMOVSXWQ128,
20900   IX86_BUILTIN_PMOVSXDQ128,
20901
20902   IX86_BUILTIN_PMOVZXBW128,
20903   IX86_BUILTIN_PMOVZXBD128,
20904   IX86_BUILTIN_PMOVZXBQ128,
20905   IX86_BUILTIN_PMOVZXWD128,
20906   IX86_BUILTIN_PMOVZXWQ128,
20907   IX86_BUILTIN_PMOVZXDQ128,
20908
20909   IX86_BUILTIN_PMULDQ128,
20910   IX86_BUILTIN_PMULLD128,
20911
20912   IX86_BUILTIN_ROUNDPD,
20913   IX86_BUILTIN_ROUNDPS,
20914   IX86_BUILTIN_ROUNDSD,
20915   IX86_BUILTIN_ROUNDSS,
20916
20917   IX86_BUILTIN_PTESTZ,
20918   IX86_BUILTIN_PTESTC,
20919   IX86_BUILTIN_PTESTNZC,
20920
20921   IX86_BUILTIN_VEC_INIT_V2SI,
20922   IX86_BUILTIN_VEC_INIT_V4HI,
20923   IX86_BUILTIN_VEC_INIT_V8QI,
20924   IX86_BUILTIN_VEC_EXT_V2DF,
20925   IX86_BUILTIN_VEC_EXT_V2DI,
20926   IX86_BUILTIN_VEC_EXT_V4SF,
20927   IX86_BUILTIN_VEC_EXT_V4SI,
20928   IX86_BUILTIN_VEC_EXT_V8HI,
20929   IX86_BUILTIN_VEC_EXT_V2SI,
20930   IX86_BUILTIN_VEC_EXT_V4HI,
20931   IX86_BUILTIN_VEC_EXT_V16QI,
20932   IX86_BUILTIN_VEC_SET_V2DI,
20933   IX86_BUILTIN_VEC_SET_V4SF,
20934   IX86_BUILTIN_VEC_SET_V4SI,
20935   IX86_BUILTIN_VEC_SET_V8HI,
20936   IX86_BUILTIN_VEC_SET_V4HI,
20937   IX86_BUILTIN_VEC_SET_V16QI,
20938
20939   IX86_BUILTIN_VEC_PACK_SFIX,
20940
20941   /* SSE4.2.  */
20942   IX86_BUILTIN_CRC32QI,
20943   IX86_BUILTIN_CRC32HI,
20944   IX86_BUILTIN_CRC32SI,
20945   IX86_BUILTIN_CRC32DI,
20946
20947   IX86_BUILTIN_PCMPESTRI128,
20948   IX86_BUILTIN_PCMPESTRM128,
20949   IX86_BUILTIN_PCMPESTRA128,
20950   IX86_BUILTIN_PCMPESTRC128,
20951   IX86_BUILTIN_PCMPESTRO128,
20952   IX86_BUILTIN_PCMPESTRS128,
20953   IX86_BUILTIN_PCMPESTRZ128,
20954   IX86_BUILTIN_PCMPISTRI128,
20955   IX86_BUILTIN_PCMPISTRM128,
20956   IX86_BUILTIN_PCMPISTRA128,
20957   IX86_BUILTIN_PCMPISTRC128,
20958   IX86_BUILTIN_PCMPISTRO128,
20959   IX86_BUILTIN_PCMPISTRS128,
20960   IX86_BUILTIN_PCMPISTRZ128,
20961
20962   IX86_BUILTIN_PCMPGTQ,
20963
20964   /* AES instructions */
20965   IX86_BUILTIN_AESENC128,
20966   IX86_BUILTIN_AESENCLAST128,
20967   IX86_BUILTIN_AESDEC128,
20968   IX86_BUILTIN_AESDECLAST128,
20969   IX86_BUILTIN_AESIMC128,
20970   IX86_BUILTIN_AESKEYGENASSIST128,
20971
20972   /* PCLMUL instruction */
20973   IX86_BUILTIN_PCLMULQDQ128,
20974
20975   /* AVX */
20976   IX86_BUILTIN_ADDPD256,
20977   IX86_BUILTIN_ADDPS256,
20978   IX86_BUILTIN_ADDSUBPD256,
20979   IX86_BUILTIN_ADDSUBPS256,
20980   IX86_BUILTIN_ANDPD256,
20981   IX86_BUILTIN_ANDPS256,
20982   IX86_BUILTIN_ANDNPD256,
20983   IX86_BUILTIN_ANDNPS256,
20984   IX86_BUILTIN_BLENDPD256,
20985   IX86_BUILTIN_BLENDPS256,
20986   IX86_BUILTIN_BLENDVPD256,
20987   IX86_BUILTIN_BLENDVPS256,
20988   IX86_BUILTIN_DIVPD256,
20989   IX86_BUILTIN_DIVPS256,
20990   IX86_BUILTIN_DPPS256,
20991   IX86_BUILTIN_HADDPD256,
20992   IX86_BUILTIN_HADDPS256,
20993   IX86_BUILTIN_HSUBPD256,
20994   IX86_BUILTIN_HSUBPS256,
20995   IX86_BUILTIN_MAXPD256,
20996   IX86_BUILTIN_MAXPS256,
20997   IX86_BUILTIN_MINPD256,
20998   IX86_BUILTIN_MINPS256,
20999   IX86_BUILTIN_MULPD256,
21000   IX86_BUILTIN_MULPS256,
21001   IX86_BUILTIN_ORPD256,
21002   IX86_BUILTIN_ORPS256,
21003   IX86_BUILTIN_SHUFPD256,
21004   IX86_BUILTIN_SHUFPS256,
21005   IX86_BUILTIN_SUBPD256,
21006   IX86_BUILTIN_SUBPS256,
21007   IX86_BUILTIN_XORPD256,
21008   IX86_BUILTIN_XORPS256,
21009   IX86_BUILTIN_CMPSD,
21010   IX86_BUILTIN_CMPSS,
21011   IX86_BUILTIN_CMPPD,
21012   IX86_BUILTIN_CMPPS,
21013   IX86_BUILTIN_CMPPD256,
21014   IX86_BUILTIN_CMPPS256,
21015   IX86_BUILTIN_CVTDQ2PD256,
21016   IX86_BUILTIN_CVTDQ2PS256,
21017   IX86_BUILTIN_CVTPD2PS256,
21018   IX86_BUILTIN_CVTPS2DQ256,
21019   IX86_BUILTIN_CVTPS2PD256,
21020   IX86_BUILTIN_CVTTPD2DQ256,
21021   IX86_BUILTIN_CVTPD2DQ256,
21022   IX86_BUILTIN_CVTTPS2DQ256,
21023   IX86_BUILTIN_EXTRACTF128PD256,
21024   IX86_BUILTIN_EXTRACTF128PS256,
21025   IX86_BUILTIN_EXTRACTF128SI256,
21026   IX86_BUILTIN_VZEROALL,
21027   IX86_BUILTIN_VZEROUPPER,
21028   IX86_BUILTIN_VPERMILVARPD,
21029   IX86_BUILTIN_VPERMILVARPS,
21030   IX86_BUILTIN_VPERMILVARPD256,
21031   IX86_BUILTIN_VPERMILVARPS256,
21032   IX86_BUILTIN_VPERMILPD,
21033   IX86_BUILTIN_VPERMILPS,
21034   IX86_BUILTIN_VPERMILPD256,
21035   IX86_BUILTIN_VPERMILPS256,
21036   IX86_BUILTIN_VPERMIL2PD,
21037   IX86_BUILTIN_VPERMIL2PS,
21038   IX86_BUILTIN_VPERMIL2PD256,
21039   IX86_BUILTIN_VPERMIL2PS256,
21040   IX86_BUILTIN_VPERM2F128PD256,
21041   IX86_BUILTIN_VPERM2F128PS256,
21042   IX86_BUILTIN_VPERM2F128SI256,
21043   IX86_BUILTIN_VBROADCASTSS,
21044   IX86_BUILTIN_VBROADCASTSD256,
21045   IX86_BUILTIN_VBROADCASTSS256,
21046   IX86_BUILTIN_VBROADCASTPD256,
21047   IX86_BUILTIN_VBROADCASTPS256,
21048   IX86_BUILTIN_VINSERTF128PD256,
21049   IX86_BUILTIN_VINSERTF128PS256,
21050   IX86_BUILTIN_VINSERTF128SI256,
21051   IX86_BUILTIN_LOADUPD256,
21052   IX86_BUILTIN_LOADUPS256,
21053   IX86_BUILTIN_STOREUPD256,
21054   IX86_BUILTIN_STOREUPS256,
21055   IX86_BUILTIN_LDDQU256,
21056   IX86_BUILTIN_MOVNTDQ256,
21057   IX86_BUILTIN_MOVNTPD256,
21058   IX86_BUILTIN_MOVNTPS256,
21059   IX86_BUILTIN_LOADDQU256,
21060   IX86_BUILTIN_STOREDQU256,
21061   IX86_BUILTIN_MASKLOADPD,
21062   IX86_BUILTIN_MASKLOADPS,
21063   IX86_BUILTIN_MASKSTOREPD,
21064   IX86_BUILTIN_MASKSTOREPS,
21065   IX86_BUILTIN_MASKLOADPD256,
21066   IX86_BUILTIN_MASKLOADPS256,
21067   IX86_BUILTIN_MASKSTOREPD256,
21068   IX86_BUILTIN_MASKSTOREPS256,
21069   IX86_BUILTIN_MOVSHDUP256,
21070   IX86_BUILTIN_MOVSLDUP256,
21071   IX86_BUILTIN_MOVDDUP256,
21072
21073   IX86_BUILTIN_SQRTPD256,
21074   IX86_BUILTIN_SQRTPS256,
21075   IX86_BUILTIN_SQRTPS_NR256,
21076   IX86_BUILTIN_RSQRTPS256,
21077   IX86_BUILTIN_RSQRTPS_NR256,
21078
21079   IX86_BUILTIN_RCPPS256,
21080
21081   IX86_BUILTIN_ROUNDPD256,
21082   IX86_BUILTIN_ROUNDPS256,
21083
21084   IX86_BUILTIN_UNPCKHPD256,
21085   IX86_BUILTIN_UNPCKLPD256,
21086   IX86_BUILTIN_UNPCKHPS256,
21087   IX86_BUILTIN_UNPCKLPS256,
21088
21089   IX86_BUILTIN_SI256_SI,
21090   IX86_BUILTIN_PS256_PS,
21091   IX86_BUILTIN_PD256_PD,
21092   IX86_BUILTIN_SI_SI256,
21093   IX86_BUILTIN_PS_PS256,
21094   IX86_BUILTIN_PD_PD256,
21095
21096   IX86_BUILTIN_VTESTZPD,
21097   IX86_BUILTIN_VTESTCPD,
21098   IX86_BUILTIN_VTESTNZCPD,
21099   IX86_BUILTIN_VTESTZPS,
21100   IX86_BUILTIN_VTESTCPS,
21101   IX86_BUILTIN_VTESTNZCPS,
21102   IX86_BUILTIN_VTESTZPD256,
21103   IX86_BUILTIN_VTESTCPD256,
21104   IX86_BUILTIN_VTESTNZCPD256,
21105   IX86_BUILTIN_VTESTZPS256,
21106   IX86_BUILTIN_VTESTCPS256,
21107   IX86_BUILTIN_VTESTNZCPS256,
21108   IX86_BUILTIN_PTESTZ256,
21109   IX86_BUILTIN_PTESTC256,
21110   IX86_BUILTIN_PTESTNZC256,
21111
21112   IX86_BUILTIN_MOVMSKPD256,
21113   IX86_BUILTIN_MOVMSKPS256,
21114
21115   /* TFmode support builtins.  */
21116   IX86_BUILTIN_INFQ,
21117   IX86_BUILTIN_HUGE_VALQ,
21118   IX86_BUILTIN_FABSQ,
21119   IX86_BUILTIN_COPYSIGNQ,
21120
21121   /* Vectorizer support builtins.  */
21122   IX86_BUILTIN_CPYSGNPS,
21123   IX86_BUILTIN_CPYSGNPD,
21124
21125   IX86_BUILTIN_CVTUDQ2PS,
21126
21127   IX86_BUILTIN_VEC_PERM_V2DF,
21128   IX86_BUILTIN_VEC_PERM_V4SF,
21129   IX86_BUILTIN_VEC_PERM_V2DI,
21130   IX86_BUILTIN_VEC_PERM_V4SI,
21131   IX86_BUILTIN_VEC_PERM_V8HI,
21132   IX86_BUILTIN_VEC_PERM_V16QI,
21133   IX86_BUILTIN_VEC_PERM_V2DI_U,
21134   IX86_BUILTIN_VEC_PERM_V4SI_U,
21135   IX86_BUILTIN_VEC_PERM_V8HI_U,
21136   IX86_BUILTIN_VEC_PERM_V16QI_U,
21137   IX86_BUILTIN_VEC_PERM_V4DF,
21138   IX86_BUILTIN_VEC_PERM_V8SF,
21139
21140   /* FMA4 and XOP instructions.  */
21141   IX86_BUILTIN_VFMADDSS,
21142   IX86_BUILTIN_VFMADDSD,
21143   IX86_BUILTIN_VFMADDPS,
21144   IX86_BUILTIN_VFMADDPD,
21145   IX86_BUILTIN_VFMSUBSS,
21146   IX86_BUILTIN_VFMSUBSD,
21147   IX86_BUILTIN_VFMSUBPS,
21148   IX86_BUILTIN_VFMSUBPD,
21149   IX86_BUILTIN_VFMADDSUBPS,
21150   IX86_BUILTIN_VFMADDSUBPD,
21151   IX86_BUILTIN_VFMSUBADDPS,
21152   IX86_BUILTIN_VFMSUBADDPD,
21153   IX86_BUILTIN_VFNMADDSS,
21154   IX86_BUILTIN_VFNMADDSD,
21155   IX86_BUILTIN_VFNMADDPS,
21156   IX86_BUILTIN_VFNMADDPD,
21157   IX86_BUILTIN_VFNMSUBSS,
21158   IX86_BUILTIN_VFNMSUBSD,
21159   IX86_BUILTIN_VFNMSUBPS,
21160   IX86_BUILTIN_VFNMSUBPD,
21161   IX86_BUILTIN_VFMADDPS256,
21162   IX86_BUILTIN_VFMADDPD256,
21163   IX86_BUILTIN_VFMSUBPS256,
21164   IX86_BUILTIN_VFMSUBPD256,
21165   IX86_BUILTIN_VFMADDSUBPS256,
21166   IX86_BUILTIN_VFMADDSUBPD256,
21167   IX86_BUILTIN_VFMSUBADDPS256,
21168   IX86_BUILTIN_VFMSUBADDPD256,
21169   IX86_BUILTIN_VFNMADDPS256,
21170   IX86_BUILTIN_VFNMADDPD256,
21171   IX86_BUILTIN_VFNMSUBPS256,
21172   IX86_BUILTIN_VFNMSUBPD256,
21173
21174   IX86_BUILTIN_VPCMOV,
21175   IX86_BUILTIN_VPCMOV_V2DI,
21176   IX86_BUILTIN_VPCMOV_V4SI,
21177   IX86_BUILTIN_VPCMOV_V8HI,
21178   IX86_BUILTIN_VPCMOV_V16QI,
21179   IX86_BUILTIN_VPCMOV_V4SF,
21180   IX86_BUILTIN_VPCMOV_V2DF,
21181   IX86_BUILTIN_VPCMOV256,
21182   IX86_BUILTIN_VPCMOV_V4DI256,
21183   IX86_BUILTIN_VPCMOV_V8SI256,
21184   IX86_BUILTIN_VPCMOV_V16HI256,
21185   IX86_BUILTIN_VPCMOV_V32QI256,
21186   IX86_BUILTIN_VPCMOV_V8SF256,
21187   IX86_BUILTIN_VPCMOV_V4DF256,
21188
21189   IX86_BUILTIN_VPPERM,
21190
21191   IX86_BUILTIN_VPMACSSWW,
21192   IX86_BUILTIN_VPMACSWW,
21193   IX86_BUILTIN_VPMACSSWD,
21194   IX86_BUILTIN_VPMACSWD,
21195   IX86_BUILTIN_VPMACSSDD,
21196   IX86_BUILTIN_VPMACSDD,
21197   IX86_BUILTIN_VPMACSSDQL,
21198   IX86_BUILTIN_VPMACSSDQH,
21199   IX86_BUILTIN_VPMACSDQL,
21200   IX86_BUILTIN_VPMACSDQH,
21201   IX86_BUILTIN_VPMADCSSWD,
21202   IX86_BUILTIN_VPMADCSWD,
21203
21204   IX86_BUILTIN_VPHADDBW,
21205   IX86_BUILTIN_VPHADDBD,
21206   IX86_BUILTIN_VPHADDBQ,
21207   IX86_BUILTIN_VPHADDWD,
21208   IX86_BUILTIN_VPHADDWQ,
21209   IX86_BUILTIN_VPHADDDQ,
21210   IX86_BUILTIN_VPHADDUBW,
21211   IX86_BUILTIN_VPHADDUBD,
21212   IX86_BUILTIN_VPHADDUBQ,
21213   IX86_BUILTIN_VPHADDUWD,
21214   IX86_BUILTIN_VPHADDUWQ,
21215   IX86_BUILTIN_VPHADDUDQ,
21216   IX86_BUILTIN_VPHSUBBW,
21217   IX86_BUILTIN_VPHSUBWD,
21218   IX86_BUILTIN_VPHSUBDQ,
21219
21220   IX86_BUILTIN_VPROTB,
21221   IX86_BUILTIN_VPROTW,
21222   IX86_BUILTIN_VPROTD,
21223   IX86_BUILTIN_VPROTQ,
21224   IX86_BUILTIN_VPROTB_IMM,
21225   IX86_BUILTIN_VPROTW_IMM,
21226   IX86_BUILTIN_VPROTD_IMM,
21227   IX86_BUILTIN_VPROTQ_IMM,
21228
21229   IX86_BUILTIN_VPSHLB,
21230   IX86_BUILTIN_VPSHLW,
21231   IX86_BUILTIN_VPSHLD,
21232   IX86_BUILTIN_VPSHLQ,
21233   IX86_BUILTIN_VPSHAB,
21234   IX86_BUILTIN_VPSHAW,
21235   IX86_BUILTIN_VPSHAD,
21236   IX86_BUILTIN_VPSHAQ,
21237
21238   IX86_BUILTIN_VFRCZSS,
21239   IX86_BUILTIN_VFRCZSD,
21240   IX86_BUILTIN_VFRCZPS,
21241   IX86_BUILTIN_VFRCZPD,
21242   IX86_BUILTIN_VFRCZPS256,
21243   IX86_BUILTIN_VFRCZPD256,
21244
21245   IX86_BUILTIN_VPCOMEQUB,
21246   IX86_BUILTIN_VPCOMNEUB,
21247   IX86_BUILTIN_VPCOMLTUB,
21248   IX86_BUILTIN_VPCOMLEUB,
21249   IX86_BUILTIN_VPCOMGTUB,
21250   IX86_BUILTIN_VPCOMGEUB,
21251   IX86_BUILTIN_VPCOMFALSEUB,
21252   IX86_BUILTIN_VPCOMTRUEUB,
21253
21254   IX86_BUILTIN_VPCOMEQUW,
21255   IX86_BUILTIN_VPCOMNEUW,
21256   IX86_BUILTIN_VPCOMLTUW,
21257   IX86_BUILTIN_VPCOMLEUW,
21258   IX86_BUILTIN_VPCOMGTUW,
21259   IX86_BUILTIN_VPCOMGEUW,
21260   IX86_BUILTIN_VPCOMFALSEUW,
21261   IX86_BUILTIN_VPCOMTRUEUW,
21262
21263   IX86_BUILTIN_VPCOMEQUD,
21264   IX86_BUILTIN_VPCOMNEUD,
21265   IX86_BUILTIN_VPCOMLTUD,
21266   IX86_BUILTIN_VPCOMLEUD,
21267   IX86_BUILTIN_VPCOMGTUD,
21268   IX86_BUILTIN_VPCOMGEUD,
21269   IX86_BUILTIN_VPCOMFALSEUD,
21270   IX86_BUILTIN_VPCOMTRUEUD,
21271
21272   IX86_BUILTIN_VPCOMEQUQ,
21273   IX86_BUILTIN_VPCOMNEUQ,
21274   IX86_BUILTIN_VPCOMLTUQ,
21275   IX86_BUILTIN_VPCOMLEUQ,
21276   IX86_BUILTIN_VPCOMGTUQ,
21277   IX86_BUILTIN_VPCOMGEUQ,
21278   IX86_BUILTIN_VPCOMFALSEUQ,
21279   IX86_BUILTIN_VPCOMTRUEUQ,
21280
21281   IX86_BUILTIN_VPCOMEQB,
21282   IX86_BUILTIN_VPCOMNEB,
21283   IX86_BUILTIN_VPCOMLTB,
21284   IX86_BUILTIN_VPCOMLEB,
21285   IX86_BUILTIN_VPCOMGTB,
21286   IX86_BUILTIN_VPCOMGEB,
21287   IX86_BUILTIN_VPCOMFALSEB,
21288   IX86_BUILTIN_VPCOMTRUEB,
21289
21290   IX86_BUILTIN_VPCOMEQW,
21291   IX86_BUILTIN_VPCOMNEW,
21292   IX86_BUILTIN_VPCOMLTW,
21293   IX86_BUILTIN_VPCOMLEW,
21294   IX86_BUILTIN_VPCOMGTW,
21295   IX86_BUILTIN_VPCOMGEW,
21296   IX86_BUILTIN_VPCOMFALSEW,
21297   IX86_BUILTIN_VPCOMTRUEW,
21298
21299   IX86_BUILTIN_VPCOMEQD,
21300   IX86_BUILTIN_VPCOMNED,
21301   IX86_BUILTIN_VPCOMLTD,
21302   IX86_BUILTIN_VPCOMLED,
21303   IX86_BUILTIN_VPCOMGTD,
21304   IX86_BUILTIN_VPCOMGED,
21305   IX86_BUILTIN_VPCOMFALSED,
21306   IX86_BUILTIN_VPCOMTRUED,
21307
21308   IX86_BUILTIN_VPCOMEQQ,
21309   IX86_BUILTIN_VPCOMNEQ,
21310   IX86_BUILTIN_VPCOMLTQ,
21311   IX86_BUILTIN_VPCOMLEQ,
21312   IX86_BUILTIN_VPCOMGTQ,
21313   IX86_BUILTIN_VPCOMGEQ,
21314   IX86_BUILTIN_VPCOMFALSEQ,
21315   IX86_BUILTIN_VPCOMTRUEQ,
21316
21317   /* LWP instructions.  */
21318   IX86_BUILTIN_LLWPCB,
21319   IX86_BUILTIN_SLWPCB,
21320   IX86_BUILTIN_LWPVAL32,
21321   IX86_BUILTIN_LWPVAL64,
21322   IX86_BUILTIN_LWPINS32,
21323   IX86_BUILTIN_LWPINS64,
21324
21325   IX86_BUILTIN_CLZS,
21326
21327   IX86_BUILTIN_MAX
21328 };
21329
21330 /* Table for the ix86 builtin decls.  */
21331 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
21332
21333 /* Table of all of the builtin functions that are possible with different ISA's
21334    but are waiting to be built until a function is declared to use that
21335    ISA.  */
21336 struct builtin_isa {
21337   const char *name;             /* function name */
21338   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
21339   int isa;                      /* isa_flags this builtin is defined for */
21340   bool const_p;                 /* true if the declaration is constant */
21341   bool set_and_not_built_p;
21342 };
21343
21344 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
21345
21346
21347 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
21348    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
21349    function decl in the ix86_builtins array.  Returns the function decl or
21350    NULL_TREE, if the builtin was not added.
21351
21352    If the front end has a special hook for builtin functions, delay adding
21353    builtin functions that aren't in the current ISA until the ISA is changed
21354    with function specific optimization.  Doing so, can save about 300K for the
21355    default compiler.  When the builtin is expanded, check at that time whether
21356    it is valid.
21357
21358    If the front end doesn't have a special hook, record all builtins, even if
21359    it isn't an instruction set in the current ISA in case the user uses
21360    function specific options for a different ISA, so that we don't get scope
21361    errors if a builtin is added in the middle of a function scope.  */
21362
21363 static inline tree
21364 def_builtin (int mask, const char *name, enum ix86_builtin_func_type tcode,
21365              enum ix86_builtins code)
21366 {
21367   tree decl = NULL_TREE;
21368
21369   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
21370     {
21371       ix86_builtins_isa[(int) code].isa = mask;
21372
21373       if (mask == 0
21374           || (mask & ix86_isa_flags) != 0
21375           || (lang_hooks.builtin_function
21376               == lang_hooks.builtin_function_ext_scope))
21377
21378         {
21379           tree type = ix86_get_builtin_func_type (tcode);
21380           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
21381                                        NULL, NULL_TREE);
21382           ix86_builtins[(int) code] = decl;
21383           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
21384         }
21385       else
21386         {
21387           ix86_builtins[(int) code] = NULL_TREE;
21388           ix86_builtins_isa[(int) code].tcode = tcode;
21389           ix86_builtins_isa[(int) code].name = name;
21390           ix86_builtins_isa[(int) code].const_p = false;
21391           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
21392         }
21393     }
21394
21395   return decl;
21396 }
21397
21398 /* Like def_builtin, but also marks the function decl "const".  */
21399
21400 static inline tree
21401 def_builtin_const (int mask, const char *name,
21402                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
21403 {
21404   tree decl = def_builtin (mask, name, tcode, code);
21405   if (decl)
21406     TREE_READONLY (decl) = 1;
21407   else
21408     ix86_builtins_isa[(int) code].const_p = true;
21409
21410   return decl;
21411 }
21412
21413 /* Add any new builtin functions for a given ISA that may not have been
21414    declared.  This saves a bit of space compared to adding all of the
21415    declarations to the tree, even if we didn't use them.  */
21416
21417 static void
21418 ix86_add_new_builtins (int isa)
21419 {
21420   int i;
21421
21422   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
21423     {
21424       if ((ix86_builtins_isa[i].isa & isa) != 0
21425           && ix86_builtins_isa[i].set_and_not_built_p)
21426         {
21427           tree decl, type;
21428
21429           /* Don't define the builtin again.  */
21430           ix86_builtins_isa[i].set_and_not_built_p = false;
21431
21432           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
21433           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
21434                                                  type, i, BUILT_IN_MD, NULL,
21435                                                  NULL_TREE);
21436
21437           ix86_builtins[i] = decl;
21438           if (ix86_builtins_isa[i].const_p)
21439             TREE_READONLY (decl) = 1;
21440         }
21441     }
21442 }
21443
21444 /* Bits for builtin_description.flag.  */
21445
21446 /* Set when we don't support the comparison natively, and should
21447    swap_comparison in order to support it.  */
21448 #define BUILTIN_DESC_SWAP_OPERANDS      1
21449
21450 struct builtin_description
21451 {
21452   const unsigned int mask;
21453   const enum insn_code icode;
21454   const char *const name;
21455   const enum ix86_builtins code;
21456   const enum rtx_code comparison;
21457   const int flag;
21458 };
21459
21460 static const struct builtin_description bdesc_comi[] =
21461 {
21462   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
21463   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
21464   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
21465   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
21466   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
21467   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
21468   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
21469   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
21470   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
21471   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
21472   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
21473   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
21474   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
21475   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
21476   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
21477   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
21478   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
21479   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
21480   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
21481   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
21482   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
21483   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
21484   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
21485   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
21486 };
21487
21488 static const struct builtin_description bdesc_pcmpestr[] =
21489 {
21490   /* SSE4.2 */
21491   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
21492   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
21493   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
21494   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
21495   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
21496   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
21497   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
21498 };
21499
21500 static const struct builtin_description bdesc_pcmpistr[] =
21501 {
21502   /* SSE4.2 */
21503   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
21504   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
21505   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
21506   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
21507   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
21508   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
21509   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
21510 };
21511
21512 /* Special builtins with variable number of arguments.  */
21513 static const struct builtin_description bdesc_special_args[] =
21514 {
21515   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
21516   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
21517
21518   /* MMX */
21519   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
21520
21521   /* 3DNow! */
21522   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
21523
21524   /* SSE */
21525   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
21526   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
21527   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
21528
21529   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
21530   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
21531   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
21532   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
21533
21534   /* SSE or 3DNow!A  */
21535   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
21536   { 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 },
21537
21538   /* SSE2 */
21539   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
21540   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
21541   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
21542   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
21543   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
21544   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
21545   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntsi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
21546   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
21547   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
21548
21549   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
21550   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
21551
21552   /* SSE3 */
21553   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
21554
21555   /* SSE4.1 */
21556   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
21557
21558   /* SSE4A */
21559   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
21560   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
21561
21562   /* AVX */
21563   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
21564   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
21565
21566   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
21567   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
21568   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
21569   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
21570   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
21571
21572   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
21573   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
21574   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
21575   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
21576   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
21577   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
21578   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
21579
21580   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
21581   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
21582   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
21583
21584   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DF },
21585   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SF },
21586   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DF },
21587   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SF },
21588   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DF_V2DF },
21589   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SF_V4SF },
21590   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DF_V4DF },
21591   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SF_V8SF },
21592
21593   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
21594   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
21595   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
21596   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
21597   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
21598   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
21599
21600 };
21601
21602 /* Builtins with variable number of arguments.  */
21603 static const struct builtin_description bdesc_args[] =
21604 {
21605   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
21606   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
21607   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
21608   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
21609   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
21610   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
21611   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
21612
21613   /* MMX */
21614   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21615   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21616   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21617   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21618   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21619   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21620
21621   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21622   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21623   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21624   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21625   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21626   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21627   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21628   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21629
21630   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21631   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21632
21633   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21634   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21635   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21636   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21637
21638   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21639   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21640   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21641   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21642   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21643   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21644
21645   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21646   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21647   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
21648   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21649   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
21650   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
21651
21652   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
21653   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
21654   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
21655
21656   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
21657
21658   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
21659   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
21660   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
21661   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
21662   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
21663   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
21664
21665   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
21666   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
21667   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
21668   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
21669   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
21670   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
21671
21672   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
21673   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
21674   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
21675   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
21676
21677   /* 3DNow! */
21678   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
21679   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
21680   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
21681   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
21682
21683   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
21684   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21685   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21686   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
21687   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
21688   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
21689   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21690   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21691   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21692   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21693   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21694   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21695   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21696   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21697   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
21698
21699   /* 3DNow!A */
21700   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
21701   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
21702   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
21703   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
21704   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21705   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
21706
21707   /* SSE */
21708   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
21709   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
21710   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
21711   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
21712   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
21713   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
21714   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
21715   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
21716   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
21717   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
21718   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
21719   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
21720
21721   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
21722
21723   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21724   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21725   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21726   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21727   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21728   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21729   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21730   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21731
21732   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
21733   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
21734   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
21735   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
21736   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
21737   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
21738   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
21739   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
21740   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
21741   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
21742   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
21743   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
21744   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
21745   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
21746   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
21747   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
21748   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
21749   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
21750   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
21751   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
21752   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
21753   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
21754
21755   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21756   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21757   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21758   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21759
21760   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21761   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21762   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21763   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21764
21765   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21766
21767   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21768   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21769   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21770   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21771   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21772
21773   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
21774   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
21775   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
21776
21777   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
21778
21779   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
21780   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
21781   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
21782
21783   /* SSE MMX or 3Dnow!A */
21784   { 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 },
21785   { 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 },
21786   { 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 },
21787
21788   { 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 },
21789   { 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 },
21790   { 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 },
21791   { 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 },
21792
21793   { 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 },
21794   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
21795
21796   { 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 },
21797
21798   /* SSE2 */
21799   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
21800
21801   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2df", IX86_BUILTIN_VEC_PERM_V2DF, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DI },
21802   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4sf", IX86_BUILTIN_VEC_PERM_V4SF, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SI },
21803   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2di", IX86_BUILTIN_VEC_PERM_V2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_V2DI },
21804   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4si", IX86_BUILTIN_VEC_PERM_V4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_V4SI },
21805   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8hi", IX86_BUILTIN_VEC_PERM_V8HI, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_V8HI },
21806   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v16qi", IX86_BUILTIN_VEC_PERM_V16QI, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
21807   { 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 },
21808   { 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 },
21809   { 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 },
21810   { 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 },
21811   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4df", IX86_BUILTIN_VEC_PERM_V4DF, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DI },
21812   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8sf", IX86_BUILTIN_VEC_PERM_V8SF, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SI },
21813
21814   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
21815   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
21816   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
21817   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
21818   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2ps, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
21819   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtudq2ps, "__builtin_ia32_cvtudq2ps", IX86_BUILTIN_CVTUDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
21820
21821   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
21822   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
21823   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
21824   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
21825   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
21826
21827   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
21828
21829   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
21830   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
21831   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
21832   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
21833
21834   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
21835   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
21836   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttps2dq, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
21837
21838   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21839   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21840   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21841   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21842   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21843   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21844   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21845   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21846
21847   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
21848   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
21849   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
21850   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
21851   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
21852   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
21853   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
21854   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
21855   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
21856   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
21857   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
21858   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
21859   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
21860   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
21861   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
21862   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
21863   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
21864   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
21865   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
21866   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
21867
21868   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21869   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21870   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21871   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21872
21873   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21874   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21875   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21876   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21877
21878   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21879
21880   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21881   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21882   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21883
21884   { 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 },
21885
21886   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21887   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21888   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
21889   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21890   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21891   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21892   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
21893   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21894
21895   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21896   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21897   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21898   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21899   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21900   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21901   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21902   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21903
21904   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21905   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
21906
21907   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21908   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21909   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21910   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21911
21912   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21913   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21914
21915   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21916   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21917   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
21918   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21919   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21920   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
21921
21922   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21923   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21924   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21925   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21926
21927   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21928   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
21929   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
21930   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21931   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
21932   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21933   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
21934   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
21935
21936   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
21937   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
21938   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
21939
21940   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
21941   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
21942
21943   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
21944   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
21945
21946   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
21947
21948   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
21949   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
21950   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
21951   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
21952
21953   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
21954   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
21955   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
21956   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
21957   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
21958   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
21959   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
21960
21961   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
21962   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
21963   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
21964   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
21965   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
21966   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
21967   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
21968
21969   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
21970   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
21971   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
21972   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
21973
21974   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
21975   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
21976   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
21977
21978   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
21979
21980   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
21981   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
21982
21983   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
21984
21985   /* SSE2 MMX */
21986   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
21987   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
21988
21989   /* SSE3 */
21990   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
21991   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
21992
21993   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21994   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21995   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21996   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21997   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
21998   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
21999
22000   /* SSSE3 */
22001   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
22002   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
22003   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
22004   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
22005   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
22006   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
22007
22008   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22009   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22010   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22011   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22012   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22013   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22014   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22015   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22016   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22017   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22018   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22019   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22020   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
22021   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
22022   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22023   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22024   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22025   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22026   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22027   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22028   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22029   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22030   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22031   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22032
22033   /* SSSE3.  */
22034   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
22035   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
22036
22037   /* SSE4.1 */
22038   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22039   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22040   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
22041   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
22042   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22043   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22044   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22045   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
22046   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
22047   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
22048
22049   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
22050   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
22051   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
22052   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
22053   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
22054   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
22055   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
22056   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
22057   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
22058   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
22059   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
22060   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
22061   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
22062
22063   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
22064   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22065   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22066   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22067   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22068   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22069   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22070   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22071   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22072   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22073   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
22074   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22075
22076   /* SSE4.1 */
22077   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
22078   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
22079   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22080   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22081
22082   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
22083   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
22084   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
22085
22086   /* SSE4.2 */
22087   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22088   { 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 },
22089   { 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 },
22090   { 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 },
22091   { 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 },
22092
22093   /* SSE4A */
22094   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
22095   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
22096   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
22097   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22098
22099   /* AES */
22100   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
22101   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
22102
22103   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22104   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22105   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22106   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22107
22108   /* PCLMUL */
22109   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
22110
22111   /* AVX */
22112   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22113   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22114   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22115   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22116   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22117   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22118   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22119   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22120   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22121   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22122   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22123   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22124   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22125   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22126   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22127   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22128   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22129   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22130   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22131   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22132   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22133   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22134   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22135   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22136   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22137   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22138
22139   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
22140   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
22141   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
22142   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
22143
22144   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
22145   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
22146   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
22147   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
22148   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
22149   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
22150   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
22151   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpsdv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22152   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpssv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22153   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22154   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22155   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
22156   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
22157   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
22158   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
22159   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
22160   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2pd256, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
22161   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2ps256, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
22162   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
22163   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
22164   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
22165   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttpd2dq256, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
22166   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
22167   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttps2dq256, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
22168   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
22169   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
22170   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
22171   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
22172   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
22173   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
22174   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
22175   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
22176   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
22177   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
22178
22179   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22180   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22181   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
22182
22183   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
22184   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22185   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22186   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22187   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22188
22189   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
22190
22191   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
22192   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
22193
22194   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22195   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22196   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22197   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22198
22199   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
22200   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
22201   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
22202   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si_si256, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
22203   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps_ps256, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
22204   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd_pd256, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
22205
22206   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
22207   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
22208   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
22209   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
22210   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
22211   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
22212   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
22213   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
22214   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
22215   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
22216   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
22217   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
22218   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
22219   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
22220   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
22221
22222   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
22223   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
22224
22225   { OPTION_MASK_ISA_ABM, CODE_FOR_clzhi2_abm,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
22226 };
22227
22228 /* FMA4 and XOP.  */
22229 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
22230 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
22231 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
22232 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
22233 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
22234 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
22235 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
22236 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
22237 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
22238 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
22239 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
22240 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
22241 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
22242 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
22243 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
22244 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
22245 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
22246 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
22247 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
22248 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
22249 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
22250 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
22251 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
22252 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
22253 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
22254 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
22255 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
22256 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
22257 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
22258 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
22259 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
22260 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
22261 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
22262 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
22263 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
22264 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
22265 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
22266 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
22267 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
22268 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
22269 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
22270 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
22271 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
22272 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
22273 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
22274 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
22275 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
22276 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
22277 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
22278 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
22279 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
22280 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
22281
22282 static const struct builtin_description bdesc_multi_arg[] =
22283 {
22284   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmaddv4sf4,     "__builtin_ia32_vfmaddss",    IX86_BUILTIN_VFMADDSS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
22285   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmaddv2df4,     "__builtin_ia32_vfmaddsd",    IX86_BUILTIN_VFMADDSD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
22286   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv4sf4,       "__builtin_ia32_vfmaddps",    IX86_BUILTIN_VFMADDPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
22287   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv2df4,       "__builtin_ia32_vfmaddpd",    IX86_BUILTIN_VFMADDPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
22288   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmsubv4sf4,     "__builtin_ia32_vfmsubss",    IX86_BUILTIN_VFMSUBSS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
22289   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmsubv2df4,     "__builtin_ia32_vfmsubsd",    IX86_BUILTIN_VFMSUBSD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
22290   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv4sf4,       "__builtin_ia32_vfmsubps",    IX86_BUILTIN_VFMSUBPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
22291   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv2df4,       "__builtin_ia32_vfmsubpd",    IX86_BUILTIN_VFMSUBPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
22292     
22293   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmaddv4sf4,    "__builtin_ia32_vfnmaddss",   IX86_BUILTIN_VFNMADDSS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
22294   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmaddv2df4,    "__builtin_ia32_vfnmaddsd",   IX86_BUILTIN_VFNMADDSD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
22295   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv4sf4,      "__builtin_ia32_vfnmaddps",   IX86_BUILTIN_VFNMADDPS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
22296   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv2df4,      "__builtin_ia32_vfnmaddpd",   IX86_BUILTIN_VFNMADDPD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
22297   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmsubv4sf4,    "__builtin_ia32_vfnmsubss",   IX86_BUILTIN_VFNMSUBSS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
22298   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmsubv2df4,    "__builtin_ia32_vfnmsubsd",   IX86_BUILTIN_VFNMSUBSD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
22299   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv4sf4,      "__builtin_ia32_vfnmsubps",   IX86_BUILTIN_VFNMSUBPS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
22300   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv2df4,      "__builtin_ia32_vfnmsubpd",   IX86_BUILTIN_VFNMSUBPD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
22301
22302   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv4sf4,    "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
22303   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv2df4,    "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
22304   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv4sf4,    "__builtin_ia32_vfmsubaddps", IX86_BUILTIN_VFMSUBADDPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
22305   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv2df4,    "__builtin_ia32_vfmsubaddpd", IX86_BUILTIN_VFMSUBADDPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
22306
22307   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv8sf4256,       "__builtin_ia32_vfmaddps256",    IX86_BUILTIN_VFMADDPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22308   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv4df4256,       "__builtin_ia32_vfmaddpd256",    IX86_BUILTIN_VFMADDPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22309   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv8sf4256,       "__builtin_ia32_vfmsubps256",    IX86_BUILTIN_VFMSUBPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22310   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv4df4256,       "__builtin_ia32_vfmsubpd256",    IX86_BUILTIN_VFMSUBPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22311   
22312   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv8sf4256,      "__builtin_ia32_vfnmaddps256",   IX86_BUILTIN_VFNMADDPS256,   UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22313   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv4df4256,      "__builtin_ia32_vfnmaddpd256",   IX86_BUILTIN_VFNMADDPD256,   UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22314   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv8sf4256,      "__builtin_ia32_vfnmsubps256",   IX86_BUILTIN_VFNMSUBPS256,   UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22315   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv4df4256,      "__builtin_ia32_vfnmsubpd256",   IX86_BUILTIN_VFNMSUBPD256,   UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22316
22317   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv8sf4,    "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22318   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv4df4,    "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22319   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv8sf4,    "__builtin_ia32_vfmsubaddps256", IX86_BUILTIN_VFMSUBADDPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22320   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv4df4,    "__builtin_ia32_vfmsubaddpd256", IX86_BUILTIN_VFMSUBADDPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22321
22322   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
22323   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
22324   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
22325   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
22326   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
22327   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
22328   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
22329
22330   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
22331   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
22332   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
22333   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
22334   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
22335   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
22336   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
22337
22338   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
22339
22340   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
22341   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
22342   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
22343   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
22344   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
22345   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
22346   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
22347   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
22348   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
22349   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
22350   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
22351   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
22352
22353   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
22354   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
22355   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
22356   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
22357   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
22358   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
22359   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
22360   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
22361   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
22362   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
22363   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
22364   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
22365   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
22366   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
22367   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
22368   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
22369
22370   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_2_SF },
22371   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_2_DF },
22372   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
22373   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
22374   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2256,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
22375   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2256,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
22376
22377   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
22378   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
22379   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
22380   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
22381   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
22382   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
22383   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
22384   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
22385   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
22386   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
22387   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
22388   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
22389   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
22390   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
22391   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
22392
22393   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
22394   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
22395   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
22396   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
22397   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
22398   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
22399   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
22400
22401   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
22402   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
22403   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
22404   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
22405   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
22406   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
22407   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
22408
22409   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
22410   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
22411   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
22412   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
22413   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
22414   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
22415   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
22416
22417   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
22418   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
22419   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
22420   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
22421   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
22422   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
22423   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
22424
22425   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
22426   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
22427   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
22428   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
22429   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
22430   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
22431   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
22432
22433   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
22434   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
22435   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
22436   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
22437   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
22438   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
22439   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
22440
22441   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
22442   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
22443   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
22444   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
22445   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
22446   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
22447   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
22448
22449   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
22450   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
22451   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
22452   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
22453   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
22454   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
22455   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
22456
22457   { 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 },
22458   { 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 },
22459   { 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 },
22460   { 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 },
22461   { 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 },
22462   { 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 },
22463   { 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 },
22464   { 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 },
22465
22466   { 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 },
22467   { 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 },
22468   { 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 },
22469   { 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 },
22470   { 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 },
22471   { 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 },
22472   { 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 },
22473   { 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 },
22474
22475   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
22476   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
22477   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
22478   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
22479
22480 };
22481
22482 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
22483    in the current target ISA to allow the user to compile particular modules
22484    with different target specific options that differ from the command line
22485    options.  */
22486 static void
22487 ix86_init_mmx_sse_builtins (void)
22488 {
22489   const struct builtin_description * d;
22490   enum ix86_builtin_func_type ftype;
22491   size_t i;
22492
22493   /* Add all special builtins with variable number of operands.  */
22494   for (i = 0, d = bdesc_special_args;
22495        i < ARRAY_SIZE (bdesc_special_args);
22496        i++, d++)
22497     {
22498       if (d->name == 0)
22499         continue;
22500
22501       ftype = (enum ix86_builtin_func_type) d->flag;
22502       def_builtin (d->mask, d->name, ftype, d->code);
22503     }
22504
22505   /* Add all builtins with variable number of operands.  */
22506   for (i = 0, d = bdesc_args;
22507        i < ARRAY_SIZE (bdesc_args);
22508        i++, d++)
22509     {
22510       if (d->name == 0)
22511         continue;
22512
22513       ftype = (enum ix86_builtin_func_type) d->flag;
22514       def_builtin_const (d->mask, d->name, ftype, d->code);
22515     }
22516
22517   /* pcmpestr[im] insns.  */
22518   for (i = 0, d = bdesc_pcmpestr;
22519        i < ARRAY_SIZE (bdesc_pcmpestr);
22520        i++, d++)
22521     {
22522       if (d->code == IX86_BUILTIN_PCMPESTRM128)
22523         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
22524       else
22525         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
22526       def_builtin_const (d->mask, d->name, ftype, d->code);
22527     }
22528
22529   /* pcmpistr[im] insns.  */
22530   for (i = 0, d = bdesc_pcmpistr;
22531        i < ARRAY_SIZE (bdesc_pcmpistr);
22532        i++, d++)
22533     {
22534       if (d->code == IX86_BUILTIN_PCMPISTRM128)
22535         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
22536       else
22537         ftype = INT_FTYPE_V16QI_V16QI_INT;
22538       def_builtin_const (d->mask, d->name, ftype, d->code);
22539     }
22540
22541   /* comi/ucomi insns.  */
22542   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
22543     {
22544       if (d->mask == OPTION_MASK_ISA_SSE2)
22545         ftype = INT_FTYPE_V2DF_V2DF;
22546       else
22547         ftype = INT_FTYPE_V4SF_V4SF;
22548       def_builtin_const (d->mask, d->name, ftype, d->code);
22549     }
22550
22551   /* SSE */
22552   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
22553                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
22554   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
22555                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
22556
22557   /* SSE or 3DNow!A */
22558   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
22559                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
22560                IX86_BUILTIN_MASKMOVQ);
22561
22562   /* SSE2 */
22563   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
22564                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
22565
22566   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
22567                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
22568   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
22569                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
22570
22571   /* SSE3.  */
22572   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
22573                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
22574   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
22575                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
22576
22577   /* AES */
22578   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
22579                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
22580   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
22581                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
22582   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
22583                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
22584   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
22585                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
22586   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
22587                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
22588   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
22589                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
22590
22591   /* PCLMUL */
22592   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
22593                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
22594
22595   /* MMX access to the vec_init patterns.  */
22596   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
22597                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
22598
22599   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
22600                      V4HI_FTYPE_HI_HI_HI_HI,
22601                      IX86_BUILTIN_VEC_INIT_V4HI);
22602
22603   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
22604                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
22605                      IX86_BUILTIN_VEC_INIT_V8QI);
22606
22607   /* Access to the vec_extract patterns.  */
22608   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
22609                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
22610   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
22611                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
22612   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
22613                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
22614   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
22615                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
22616   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
22617                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
22618
22619   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
22620                      "__builtin_ia32_vec_ext_v4hi",
22621                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
22622
22623   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
22624                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
22625
22626   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
22627                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
22628
22629   /* Access to the vec_set patterns.  */
22630   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
22631                      "__builtin_ia32_vec_set_v2di",
22632                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
22633
22634   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
22635                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
22636
22637   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
22638                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
22639
22640   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
22641                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
22642
22643   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
22644                      "__builtin_ia32_vec_set_v4hi",
22645                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
22646
22647   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
22648                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
22649
22650   /* Add FMA4 multi-arg argument instructions */
22651   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
22652     {
22653       if (d->name == 0)
22654         continue;
22655
22656       ftype = (enum ix86_builtin_func_type) d->flag;
22657       def_builtin_const (d->mask, d->name, ftype, d->code);
22658     }
22659 }
22660
22661 /* Internal method for ix86_init_builtins.  */
22662
22663 static void
22664 ix86_init_builtins_va_builtins_abi (void)
22665 {
22666   tree ms_va_ref, sysv_va_ref;
22667   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
22668   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
22669   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
22670   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
22671
22672   if (!TARGET_64BIT)
22673     return;
22674   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
22675   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
22676   ms_va_ref = build_reference_type (ms_va_list_type_node);
22677   sysv_va_ref =
22678     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
22679
22680   fnvoid_va_end_ms =
22681     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
22682   fnvoid_va_start_ms =
22683     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
22684   fnvoid_va_end_sysv =
22685     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
22686   fnvoid_va_start_sysv =
22687     build_varargs_function_type_list (void_type_node, sysv_va_ref,
22688                                        NULL_TREE);
22689   fnvoid_va_copy_ms =
22690     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
22691                               NULL_TREE);
22692   fnvoid_va_copy_sysv =
22693     build_function_type_list (void_type_node, sysv_va_ref,
22694                               sysv_va_ref, NULL_TREE);
22695
22696   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
22697                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
22698   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
22699                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
22700   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
22701                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
22702   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
22703                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
22704   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
22705                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
22706   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
22707                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
22708 }
22709
22710 static void
22711 ix86_init_builtin_types (void)
22712 {
22713   tree float128_type_node, float80_type_node;
22714
22715   /* The __float80 type.  */
22716   float80_type_node = long_double_type_node;
22717   if (TYPE_MODE (float80_type_node) != XFmode)
22718     {
22719       /* The __float80 type.  */
22720       float80_type_node = make_node (REAL_TYPE);
22721
22722       TYPE_PRECISION (float80_type_node) = 80;
22723       layout_type (float80_type_node);
22724     }
22725   (*lang_hooks.types.register_builtin_type) (float80_type_node, "__float80");
22726
22727   /* The __float128 type.  */
22728   float128_type_node = make_node (REAL_TYPE);
22729   TYPE_PRECISION (float128_type_node) = 128;
22730   layout_type (float128_type_node);
22731   (*lang_hooks.types.register_builtin_type) (float128_type_node, "__float128");
22732
22733   /* This macro is built by i386-builtin-types.awk.  */
22734   DEFINE_BUILTIN_PRIMITIVE_TYPES;
22735 }
22736
22737 static void
22738 ix86_init_builtins (void)
22739 {
22740   tree t;
22741
22742   ix86_init_builtin_types ();
22743
22744   /* TFmode support builtins.  */
22745   def_builtin_const (0, "__builtin_infq",
22746                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
22747   def_builtin_const (0, "__builtin_huge_valq",
22748                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
22749
22750   /* We will expand them to normal call if SSE2 isn't available since
22751      they are used by libgcc. */
22752   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
22753   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
22754                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
22755   TREE_READONLY (t) = 1;
22756   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
22757
22758   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
22759   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
22760                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
22761   TREE_READONLY (t) = 1;
22762   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
22763
22764   ix86_init_mmx_sse_builtins ();
22765
22766   if (TARGET_64BIT)
22767     ix86_init_builtins_va_builtins_abi ();
22768 }
22769
22770 /* Return the ix86 builtin for CODE.  */
22771
22772 static tree
22773 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
22774 {
22775   if (code >= IX86_BUILTIN_MAX)
22776     return error_mark_node;
22777
22778   return ix86_builtins[code];
22779 }
22780
22781 /* Errors in the source file can cause expand_expr to return const0_rtx
22782    where we expect a vector.  To avoid crashing, use one of the vector
22783    clear instructions.  */
22784 static rtx
22785 safe_vector_operand (rtx x, enum machine_mode mode)
22786 {
22787   if (x == const0_rtx)
22788     x = CONST0_RTX (mode);
22789   return x;
22790 }
22791
22792 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
22793
22794 static rtx
22795 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
22796 {
22797   rtx pat;
22798   tree arg0 = CALL_EXPR_ARG (exp, 0);
22799   tree arg1 = CALL_EXPR_ARG (exp, 1);
22800   rtx op0 = expand_normal (arg0);
22801   rtx op1 = expand_normal (arg1);
22802   enum machine_mode tmode = insn_data[icode].operand[0].mode;
22803   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
22804   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
22805
22806   if (VECTOR_MODE_P (mode0))
22807     op0 = safe_vector_operand (op0, mode0);
22808   if (VECTOR_MODE_P (mode1))
22809     op1 = safe_vector_operand (op1, mode1);
22810
22811   if (optimize || !target
22812       || GET_MODE (target) != tmode
22813       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
22814     target = gen_reg_rtx (tmode);
22815
22816   if (GET_MODE (op1) == SImode && mode1 == TImode)
22817     {
22818       rtx x = gen_reg_rtx (V4SImode);
22819       emit_insn (gen_sse2_loadd (x, op1));
22820       op1 = gen_lowpart (TImode, x);
22821     }
22822
22823   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
22824     op0 = copy_to_mode_reg (mode0, op0);
22825   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
22826     op1 = copy_to_mode_reg (mode1, op1);
22827
22828   pat = GEN_FCN (icode) (target, op0, op1);
22829   if (! pat)
22830     return 0;
22831
22832   emit_insn (pat);
22833
22834   return target;
22835 }
22836
22837 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
22838
22839 static rtx
22840 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
22841                                enum ix86_builtin_func_type m_type,
22842                                enum rtx_code sub_code)
22843 {
22844   rtx pat;
22845   int i;
22846   int nargs;
22847   bool comparison_p = false;
22848   bool tf_p = false;
22849   bool last_arg_constant = false;
22850   int num_memory = 0;
22851   struct {
22852     rtx op;
22853     enum machine_mode mode;
22854   } args[4];
22855
22856   enum machine_mode tmode = insn_data[icode].operand[0].mode;
22857
22858   switch (m_type)
22859     {
22860     case MULTI_ARG_4_DF2_DI_I:
22861     case MULTI_ARG_4_DF2_DI_I1:
22862     case MULTI_ARG_4_SF2_SI_I:
22863     case MULTI_ARG_4_SF2_SI_I1:
22864       nargs = 4;
22865       last_arg_constant = true;
22866       break;
22867
22868     case MULTI_ARG_3_SF:
22869     case MULTI_ARG_3_DF:
22870     case MULTI_ARG_3_SF2:
22871     case MULTI_ARG_3_DF2:
22872     case MULTI_ARG_3_DI:
22873     case MULTI_ARG_3_SI:
22874     case MULTI_ARG_3_SI_DI:
22875     case MULTI_ARG_3_HI:
22876     case MULTI_ARG_3_HI_SI:
22877     case MULTI_ARG_3_QI:
22878     case MULTI_ARG_3_DI2:
22879     case MULTI_ARG_3_SI2:
22880     case MULTI_ARG_3_HI2:
22881     case MULTI_ARG_3_QI2:
22882       nargs = 3;
22883       break;
22884
22885     case MULTI_ARG_2_SF:
22886     case MULTI_ARG_2_DF:
22887     case MULTI_ARG_2_DI:
22888     case MULTI_ARG_2_SI:
22889     case MULTI_ARG_2_HI:
22890     case MULTI_ARG_2_QI:
22891       nargs = 2;
22892       break;
22893
22894     case MULTI_ARG_2_DI_IMM:
22895     case MULTI_ARG_2_SI_IMM:
22896     case MULTI_ARG_2_HI_IMM:
22897     case MULTI_ARG_2_QI_IMM:
22898       nargs = 2;
22899       last_arg_constant = true;
22900       break;
22901
22902     case MULTI_ARG_1_SF:
22903     case MULTI_ARG_1_DF:
22904     case MULTI_ARG_1_SF2:
22905     case MULTI_ARG_1_DF2:
22906     case MULTI_ARG_1_DI:
22907     case MULTI_ARG_1_SI:
22908     case MULTI_ARG_1_HI:
22909     case MULTI_ARG_1_QI:
22910     case MULTI_ARG_1_SI_DI:
22911     case MULTI_ARG_1_HI_DI:
22912     case MULTI_ARG_1_HI_SI:
22913     case MULTI_ARG_1_QI_DI:
22914     case MULTI_ARG_1_QI_SI:
22915     case MULTI_ARG_1_QI_HI:
22916       nargs = 1;
22917       break;
22918
22919     case MULTI_ARG_2_DI_CMP:
22920     case MULTI_ARG_2_SI_CMP:
22921     case MULTI_ARG_2_HI_CMP:
22922     case MULTI_ARG_2_QI_CMP:
22923       nargs = 2;
22924       comparison_p = true;
22925       break;
22926
22927     case MULTI_ARG_2_SF_TF:
22928     case MULTI_ARG_2_DF_TF:
22929     case MULTI_ARG_2_DI_TF:
22930     case MULTI_ARG_2_SI_TF:
22931     case MULTI_ARG_2_HI_TF:
22932     case MULTI_ARG_2_QI_TF:
22933       nargs = 2;
22934       tf_p = true;
22935       break;
22936
22937     default:
22938       gcc_unreachable ();
22939     }
22940
22941   if (optimize || !target
22942       || GET_MODE (target) != tmode
22943       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
22944     target = gen_reg_rtx (tmode);
22945
22946   gcc_assert (nargs <= 4);
22947
22948   for (i = 0; i < nargs; i++)
22949     {
22950       tree arg = CALL_EXPR_ARG (exp, i);
22951       rtx op = expand_normal (arg);
22952       int adjust = (comparison_p) ? 1 : 0;
22953       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
22954
22955       if (last_arg_constant && i == nargs-1)
22956         {
22957           if (!CONST_INT_P (op))
22958             {
22959               error ("last argument must be an immediate");
22960               return gen_reg_rtx (tmode);
22961             }
22962         }
22963       else
22964         {
22965           if (VECTOR_MODE_P (mode))
22966             op = safe_vector_operand (op, mode);
22967
22968           /* If we aren't optimizing, only allow one memory operand to be
22969              generated.  */
22970           if (memory_operand (op, mode))
22971             num_memory++;
22972
22973           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
22974
22975           if (optimize
22976               || ! (*insn_data[icode].operand[i+adjust+1].predicate) (op, mode)
22977               || num_memory > 1)
22978             op = force_reg (mode, op);
22979         }
22980
22981       args[i].op = op;
22982       args[i].mode = mode;
22983     }
22984
22985   switch (nargs)
22986     {
22987     case 1:
22988       pat = GEN_FCN (icode) (target, args[0].op);
22989       break;
22990
22991     case 2:
22992       if (tf_p)
22993         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
22994                                GEN_INT ((int)sub_code));
22995       else if (! comparison_p)
22996         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
22997       else
22998         {
22999           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
23000                                        args[0].op,
23001                                        args[1].op);
23002
23003           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
23004         }
23005       break;
23006
23007     case 3:
23008       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
23009       break;
23010
23011     case 4:
23012       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
23013       break;
23014
23015     default:
23016       gcc_unreachable ();
23017     }
23018
23019   if (! pat)
23020     return 0;
23021
23022   emit_insn (pat);
23023   return target;
23024 }
23025
23026 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
23027    insns with vec_merge.  */
23028
23029 static rtx
23030 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
23031                                     rtx target)
23032 {
23033   rtx pat;
23034   tree arg0 = CALL_EXPR_ARG (exp, 0);
23035   rtx op1, op0 = expand_normal (arg0);
23036   enum machine_mode tmode = insn_data[icode].operand[0].mode;
23037   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
23038
23039   if (optimize || !target
23040       || GET_MODE (target) != tmode
23041       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
23042     target = gen_reg_rtx (tmode);
23043
23044   if (VECTOR_MODE_P (mode0))
23045     op0 = safe_vector_operand (op0, mode0);
23046
23047   if ((optimize && !register_operand (op0, mode0))
23048       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
23049     op0 = copy_to_mode_reg (mode0, op0);
23050
23051   op1 = op0;
23052   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
23053     op1 = copy_to_mode_reg (mode0, op1);
23054
23055   pat = GEN_FCN (icode) (target, op0, op1);
23056   if (! pat)
23057     return 0;
23058   emit_insn (pat);
23059   return target;
23060 }
23061
23062 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
23063
23064 static rtx
23065 ix86_expand_sse_compare (const struct builtin_description *d,
23066                          tree exp, rtx target, bool swap)
23067 {
23068   rtx pat;
23069   tree arg0 = CALL_EXPR_ARG (exp, 0);
23070   tree arg1 = CALL_EXPR_ARG (exp, 1);
23071   rtx op0 = expand_normal (arg0);
23072   rtx op1 = expand_normal (arg1);
23073   rtx op2;
23074   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
23075   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
23076   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
23077   enum rtx_code comparison = d->comparison;
23078
23079   if (VECTOR_MODE_P (mode0))
23080     op0 = safe_vector_operand (op0, mode0);
23081   if (VECTOR_MODE_P (mode1))
23082     op1 = safe_vector_operand (op1, mode1);
23083
23084   /* Swap operands if we have a comparison that isn't available in
23085      hardware.  */
23086   if (swap)
23087     {
23088       rtx tmp = gen_reg_rtx (mode1);
23089       emit_move_insn (tmp, op1);
23090       op1 = op0;
23091       op0 = tmp;
23092     }
23093
23094   if (optimize || !target
23095       || GET_MODE (target) != tmode
23096       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
23097     target = gen_reg_rtx (tmode);
23098
23099   if ((optimize && !register_operand (op0, mode0))
23100       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
23101     op0 = copy_to_mode_reg (mode0, op0);
23102   if ((optimize && !register_operand (op1, mode1))
23103       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
23104     op1 = copy_to_mode_reg (mode1, op1);
23105
23106   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
23107   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
23108   if (! pat)
23109     return 0;
23110   emit_insn (pat);
23111   return target;
23112 }
23113
23114 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
23115
23116 static rtx
23117 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
23118                       rtx target)
23119 {
23120   rtx pat;
23121   tree arg0 = CALL_EXPR_ARG (exp, 0);
23122   tree arg1 = CALL_EXPR_ARG (exp, 1);
23123   rtx op0 = expand_normal (arg0);
23124   rtx op1 = expand_normal (arg1);
23125   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
23126   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
23127   enum rtx_code comparison = d->comparison;
23128
23129   if (VECTOR_MODE_P (mode0))
23130     op0 = safe_vector_operand (op0, mode0);
23131   if (VECTOR_MODE_P (mode1))
23132     op1 = safe_vector_operand (op1, mode1);
23133
23134   /* Swap operands if we have a comparison that isn't available in
23135      hardware.  */
23136   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
23137     {
23138       rtx tmp = op1;
23139       op1 = op0;
23140       op0 = tmp;
23141     }
23142
23143   target = gen_reg_rtx (SImode);
23144   emit_move_insn (target, const0_rtx);
23145   target = gen_rtx_SUBREG (QImode, target, 0);
23146
23147   if ((optimize && !register_operand (op0, mode0))
23148       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
23149     op0 = copy_to_mode_reg (mode0, op0);
23150   if ((optimize && !register_operand (op1, mode1))
23151       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
23152     op1 = copy_to_mode_reg (mode1, op1);
23153
23154   pat = GEN_FCN (d->icode) (op0, op1);
23155   if (! pat)
23156     return 0;
23157   emit_insn (pat);
23158   emit_insn (gen_rtx_SET (VOIDmode,
23159                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
23160                           gen_rtx_fmt_ee (comparison, QImode,
23161                                           SET_DEST (pat),
23162                                           const0_rtx)));
23163
23164   return SUBREG_REG (target);
23165 }
23166
23167 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
23168
23169 static rtx
23170 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
23171                        rtx target)
23172 {
23173   rtx pat;
23174   tree arg0 = CALL_EXPR_ARG (exp, 0);
23175   tree arg1 = CALL_EXPR_ARG (exp, 1);
23176   rtx op0 = expand_normal (arg0);
23177   rtx op1 = expand_normal (arg1);
23178   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
23179   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
23180   enum rtx_code comparison = d->comparison;
23181
23182   if (VECTOR_MODE_P (mode0))
23183     op0 = safe_vector_operand (op0, mode0);
23184   if (VECTOR_MODE_P (mode1))
23185     op1 = safe_vector_operand (op1, mode1);
23186
23187   target = gen_reg_rtx (SImode);
23188   emit_move_insn (target, const0_rtx);
23189   target = gen_rtx_SUBREG (QImode, target, 0);
23190
23191   if ((optimize && !register_operand (op0, mode0))
23192       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
23193     op0 = copy_to_mode_reg (mode0, op0);
23194   if ((optimize && !register_operand (op1, mode1))
23195       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
23196     op1 = copy_to_mode_reg (mode1, op1);
23197
23198   pat = GEN_FCN (d->icode) (op0, op1);
23199   if (! pat)
23200     return 0;
23201   emit_insn (pat);
23202   emit_insn (gen_rtx_SET (VOIDmode,
23203                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
23204                           gen_rtx_fmt_ee (comparison, QImode,
23205                                           SET_DEST (pat),
23206                                           const0_rtx)));
23207
23208   return SUBREG_REG (target);
23209 }
23210
23211 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
23212
23213 static rtx
23214 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
23215                           tree exp, rtx target)
23216 {
23217   rtx pat;
23218   tree arg0 = CALL_EXPR_ARG (exp, 0);
23219   tree arg1 = CALL_EXPR_ARG (exp, 1);
23220   tree arg2 = CALL_EXPR_ARG (exp, 2);
23221   tree arg3 = CALL_EXPR_ARG (exp, 3);
23222   tree arg4 = CALL_EXPR_ARG (exp, 4);
23223   rtx scratch0, scratch1;
23224   rtx op0 = expand_normal (arg0);
23225   rtx op1 = expand_normal (arg1);
23226   rtx op2 = expand_normal (arg2);
23227   rtx op3 = expand_normal (arg3);
23228   rtx op4 = expand_normal (arg4);
23229   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
23230
23231   tmode0 = insn_data[d->icode].operand[0].mode;
23232   tmode1 = insn_data[d->icode].operand[1].mode;
23233   modev2 = insn_data[d->icode].operand[2].mode;
23234   modei3 = insn_data[d->icode].operand[3].mode;
23235   modev4 = insn_data[d->icode].operand[4].mode;
23236   modei5 = insn_data[d->icode].operand[5].mode;
23237   modeimm = insn_data[d->icode].operand[6].mode;
23238
23239   if (VECTOR_MODE_P (modev2))
23240     op0 = safe_vector_operand (op0, modev2);
23241   if (VECTOR_MODE_P (modev4))
23242     op2 = safe_vector_operand (op2, modev4);
23243
23244   if (! (*insn_data[d->icode].operand[2].predicate) (op0, modev2))
23245     op0 = copy_to_mode_reg (modev2, op0);
23246   if (! (*insn_data[d->icode].operand[3].predicate) (op1, modei3))
23247     op1 = copy_to_mode_reg (modei3, op1);
23248   if ((optimize && !register_operand (op2, modev4))
23249       || !(*insn_data[d->icode].operand[4].predicate) (op2, modev4))
23250     op2 = copy_to_mode_reg (modev4, op2);
23251   if (! (*insn_data[d->icode].operand[5].predicate) (op3, modei5))
23252     op3 = copy_to_mode_reg (modei5, op3);
23253
23254   if (! (*insn_data[d->icode].operand[6].predicate) (op4, modeimm))
23255     {
23256       error ("the fifth argument must be a 8-bit immediate");
23257       return const0_rtx;
23258     }
23259
23260   if (d->code == IX86_BUILTIN_PCMPESTRI128)
23261     {
23262       if (optimize || !target
23263           || GET_MODE (target) != tmode0
23264           || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode0))
23265         target = gen_reg_rtx (tmode0);
23266
23267       scratch1 = gen_reg_rtx (tmode1);
23268
23269       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
23270     }
23271   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
23272     {
23273       if (optimize || !target
23274           || GET_MODE (target) != tmode1
23275           || ! (*insn_data[d->icode].operand[1].predicate) (target, tmode1))
23276         target = gen_reg_rtx (tmode1);
23277
23278       scratch0 = gen_reg_rtx (tmode0);
23279
23280       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
23281     }
23282   else
23283     {
23284       gcc_assert (d->flag);
23285
23286       scratch0 = gen_reg_rtx (tmode0);
23287       scratch1 = gen_reg_rtx (tmode1);
23288
23289       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
23290     }
23291
23292   if (! pat)
23293     return 0;
23294
23295   emit_insn (pat);
23296
23297   if (d->flag)
23298     {
23299       target = gen_reg_rtx (SImode);
23300       emit_move_insn (target, const0_rtx);
23301       target = gen_rtx_SUBREG (QImode, target, 0);
23302
23303       emit_insn
23304         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
23305                       gen_rtx_fmt_ee (EQ, QImode,
23306                                       gen_rtx_REG ((enum machine_mode) d->flag,
23307                                                    FLAGS_REG),
23308                                       const0_rtx)));
23309       return SUBREG_REG (target);
23310     }
23311   else
23312     return target;
23313 }
23314
23315
23316 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
23317
23318 static rtx
23319 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
23320                           tree exp, rtx target)
23321 {
23322   rtx pat;
23323   tree arg0 = CALL_EXPR_ARG (exp, 0);
23324   tree arg1 = CALL_EXPR_ARG (exp, 1);
23325   tree arg2 = CALL_EXPR_ARG (exp, 2);
23326   rtx scratch0, scratch1;
23327   rtx op0 = expand_normal (arg0);
23328   rtx op1 = expand_normal (arg1);
23329   rtx op2 = expand_normal (arg2);
23330   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
23331
23332   tmode0 = insn_data[d->icode].operand[0].mode;
23333   tmode1 = insn_data[d->icode].operand[1].mode;
23334   modev2 = insn_data[d->icode].operand[2].mode;
23335   modev3 = insn_data[d->icode].operand[3].mode;
23336   modeimm = insn_data[d->icode].operand[4].mode;
23337
23338   if (VECTOR_MODE_P (modev2))
23339     op0 = safe_vector_operand (op0, modev2);
23340   if (VECTOR_MODE_P (modev3))
23341     op1 = safe_vector_operand (op1, modev3);
23342
23343   if (! (*insn_data[d->icode].operand[2].predicate) (op0, modev2))
23344     op0 = copy_to_mode_reg (modev2, op0);
23345   if ((optimize && !register_operand (op1, modev3))
23346       || !(*insn_data[d->icode].operand[3].predicate) (op1, modev3))
23347     op1 = copy_to_mode_reg (modev3, op1);
23348
23349   if (! (*insn_data[d->icode].operand[4].predicate) (op2, modeimm))
23350     {
23351       error ("the third argument must be a 8-bit immediate");
23352       return const0_rtx;
23353     }
23354
23355   if (d->code == IX86_BUILTIN_PCMPISTRI128)
23356     {
23357       if (optimize || !target
23358           || GET_MODE (target) != tmode0
23359           || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode0))
23360         target = gen_reg_rtx (tmode0);
23361
23362       scratch1 = gen_reg_rtx (tmode1);
23363
23364       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
23365     }
23366   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
23367     {
23368       if (optimize || !target
23369           || GET_MODE (target) != tmode1
23370           || ! (*insn_data[d->icode].operand[1].predicate) (target, tmode1))
23371         target = gen_reg_rtx (tmode1);
23372
23373       scratch0 = gen_reg_rtx (tmode0);
23374
23375       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
23376     }
23377   else
23378     {
23379       gcc_assert (d->flag);
23380
23381       scratch0 = gen_reg_rtx (tmode0);
23382       scratch1 = gen_reg_rtx (tmode1);
23383
23384       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
23385     }
23386
23387   if (! pat)
23388     return 0;
23389
23390   emit_insn (pat);
23391
23392   if (d->flag)
23393     {
23394       target = gen_reg_rtx (SImode);
23395       emit_move_insn (target, const0_rtx);
23396       target = gen_rtx_SUBREG (QImode, target, 0);
23397
23398       emit_insn
23399         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
23400                       gen_rtx_fmt_ee (EQ, QImode,
23401                                       gen_rtx_REG ((enum machine_mode) d->flag,
23402                                                    FLAGS_REG),
23403                                       const0_rtx)));
23404       return SUBREG_REG (target);
23405     }
23406   else
23407     return target;
23408 }
23409
23410 /* Subroutine of ix86_expand_builtin to take care of insns with
23411    variable number of operands.  */
23412
23413 static rtx
23414 ix86_expand_args_builtin (const struct builtin_description *d,
23415                           tree exp, rtx target)
23416 {
23417   rtx pat, real_target;
23418   unsigned int i, nargs;
23419   unsigned int nargs_constant = 0;
23420   int num_memory = 0;
23421   struct
23422     {
23423       rtx op;
23424       enum machine_mode mode;
23425     } args[4];
23426   bool last_arg_count = false;
23427   enum insn_code icode = d->icode;
23428   const struct insn_data *insn_p = &insn_data[icode];
23429   enum machine_mode tmode = insn_p->operand[0].mode;
23430   enum machine_mode rmode = VOIDmode;
23431   bool swap = false;
23432   enum rtx_code comparison = d->comparison;
23433
23434   switch ((enum ix86_builtin_func_type) d->flag)
23435     {
23436     case INT_FTYPE_V8SF_V8SF_PTEST:
23437     case INT_FTYPE_V4DI_V4DI_PTEST:
23438     case INT_FTYPE_V4DF_V4DF_PTEST:
23439     case INT_FTYPE_V4SF_V4SF_PTEST:
23440     case INT_FTYPE_V2DI_V2DI_PTEST:
23441     case INT_FTYPE_V2DF_V2DF_PTEST:
23442       return ix86_expand_sse_ptest (d, exp, target);
23443     case FLOAT128_FTYPE_FLOAT128:
23444     case FLOAT_FTYPE_FLOAT:
23445     case INT_FTYPE_INT:
23446     case UINT64_FTYPE_INT:
23447     case UINT16_FTYPE_UINT16:
23448     case INT64_FTYPE_INT64:
23449     case INT64_FTYPE_V4SF:
23450     case INT64_FTYPE_V2DF:
23451     case INT_FTYPE_V16QI:
23452     case INT_FTYPE_V8QI:
23453     case INT_FTYPE_V8SF:
23454     case INT_FTYPE_V4DF:
23455     case INT_FTYPE_V4SF:
23456     case INT_FTYPE_V2DF:
23457     case V16QI_FTYPE_V16QI:
23458     case V8SI_FTYPE_V8SF:
23459     case V8SI_FTYPE_V4SI:
23460     case V8HI_FTYPE_V8HI:
23461     case V8HI_FTYPE_V16QI:
23462     case V8QI_FTYPE_V8QI:
23463     case V8SF_FTYPE_V8SF:
23464     case V8SF_FTYPE_V8SI:
23465     case V8SF_FTYPE_V4SF:
23466     case V4SI_FTYPE_V4SI:
23467     case V4SI_FTYPE_V16QI:
23468     case V4SI_FTYPE_V4SF:
23469     case V4SI_FTYPE_V8SI:
23470     case V4SI_FTYPE_V8HI:
23471     case V4SI_FTYPE_V4DF:
23472     case V4SI_FTYPE_V2DF:
23473     case V4HI_FTYPE_V4HI:
23474     case V4DF_FTYPE_V4DF:
23475     case V4DF_FTYPE_V4SI:
23476     case V4DF_FTYPE_V4SF:
23477     case V4DF_FTYPE_V2DF:
23478     case V4SF_FTYPE_V4SF:
23479     case V4SF_FTYPE_V4SI:
23480     case V4SF_FTYPE_V8SF:
23481     case V4SF_FTYPE_V4DF:
23482     case V4SF_FTYPE_V2DF:
23483     case V2DI_FTYPE_V2DI:
23484     case V2DI_FTYPE_V16QI:
23485     case V2DI_FTYPE_V8HI:
23486     case V2DI_FTYPE_V4SI:
23487     case V2DF_FTYPE_V2DF:
23488     case V2DF_FTYPE_V4SI:
23489     case V2DF_FTYPE_V4DF:
23490     case V2DF_FTYPE_V4SF:
23491     case V2DF_FTYPE_V2SI:
23492     case V2SI_FTYPE_V2SI:
23493     case V2SI_FTYPE_V4SF:
23494     case V2SI_FTYPE_V2SF:
23495     case V2SI_FTYPE_V2DF:
23496     case V2SF_FTYPE_V2SF:
23497     case V2SF_FTYPE_V2SI:
23498       nargs = 1;
23499       break;
23500     case V4SF_FTYPE_V4SF_VEC_MERGE:
23501     case V2DF_FTYPE_V2DF_VEC_MERGE:
23502       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
23503     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
23504     case V16QI_FTYPE_V16QI_V16QI:
23505     case V16QI_FTYPE_V8HI_V8HI:
23506     case V8QI_FTYPE_V8QI_V8QI:
23507     case V8QI_FTYPE_V4HI_V4HI:
23508     case V8HI_FTYPE_V8HI_V8HI:
23509     case V8HI_FTYPE_V16QI_V16QI:
23510     case V8HI_FTYPE_V4SI_V4SI:
23511     case V8SF_FTYPE_V8SF_V8SF:
23512     case V8SF_FTYPE_V8SF_V8SI:
23513     case V4SI_FTYPE_V4SI_V4SI:
23514     case V4SI_FTYPE_V8HI_V8HI:
23515     case V4SI_FTYPE_V4SF_V4SF:
23516     case V4SI_FTYPE_V2DF_V2DF:
23517     case V4HI_FTYPE_V4HI_V4HI:
23518     case V4HI_FTYPE_V8QI_V8QI:
23519     case V4HI_FTYPE_V2SI_V2SI:
23520     case V4DF_FTYPE_V4DF_V4DF:
23521     case V4DF_FTYPE_V4DF_V4DI:
23522     case V4SF_FTYPE_V4SF_V4SF:
23523     case V4SF_FTYPE_V4SF_V4SI:
23524     case V4SF_FTYPE_V4SF_V2SI:
23525     case V4SF_FTYPE_V4SF_V2DF:
23526     case V4SF_FTYPE_V4SF_DI:
23527     case V4SF_FTYPE_V4SF_SI:
23528     case V2DI_FTYPE_V2DI_V2DI:
23529     case V2DI_FTYPE_V16QI_V16QI:
23530     case V2DI_FTYPE_V4SI_V4SI:
23531     case V2DI_FTYPE_V2DI_V16QI:
23532     case V2DI_FTYPE_V2DF_V2DF:
23533     case V2SI_FTYPE_V2SI_V2SI:
23534     case V2SI_FTYPE_V4HI_V4HI:
23535     case V2SI_FTYPE_V2SF_V2SF:
23536     case V2DF_FTYPE_V2DF_V2DF:
23537     case V2DF_FTYPE_V2DF_V4SF:
23538     case V2DF_FTYPE_V2DF_V2DI:
23539     case V2DF_FTYPE_V2DF_DI:
23540     case V2DF_FTYPE_V2DF_SI:
23541     case V2SF_FTYPE_V2SF_V2SF:
23542     case V1DI_FTYPE_V1DI_V1DI:
23543     case V1DI_FTYPE_V8QI_V8QI:
23544     case V1DI_FTYPE_V2SI_V2SI:
23545       if (comparison == UNKNOWN)
23546         return ix86_expand_binop_builtin (icode, exp, target);
23547       nargs = 2;
23548       break;
23549     case V4SF_FTYPE_V4SF_V4SF_SWAP:
23550     case V2DF_FTYPE_V2DF_V2DF_SWAP:
23551       gcc_assert (comparison != UNKNOWN);
23552       nargs = 2;
23553       swap = true;
23554       break;
23555     case V8HI_FTYPE_V8HI_V8HI_COUNT:
23556     case V8HI_FTYPE_V8HI_SI_COUNT:
23557     case V4SI_FTYPE_V4SI_V4SI_COUNT:
23558     case V4SI_FTYPE_V4SI_SI_COUNT:
23559     case V4HI_FTYPE_V4HI_V4HI_COUNT:
23560     case V4HI_FTYPE_V4HI_SI_COUNT:
23561     case V2DI_FTYPE_V2DI_V2DI_COUNT:
23562     case V2DI_FTYPE_V2DI_SI_COUNT:
23563     case V2SI_FTYPE_V2SI_V2SI_COUNT:
23564     case V2SI_FTYPE_V2SI_SI_COUNT:
23565     case V1DI_FTYPE_V1DI_V1DI_COUNT:
23566     case V1DI_FTYPE_V1DI_SI_COUNT:
23567       nargs = 2;
23568       last_arg_count = true;
23569       break;
23570     case UINT64_FTYPE_UINT64_UINT64:
23571     case UINT_FTYPE_UINT_UINT:
23572     case UINT_FTYPE_UINT_USHORT:
23573     case UINT_FTYPE_UINT_UCHAR:
23574     case UINT16_FTYPE_UINT16_INT:
23575     case UINT8_FTYPE_UINT8_INT:
23576       nargs = 2;
23577       break;
23578     case V2DI_FTYPE_V2DI_INT_CONVERT:
23579       nargs = 2;
23580       rmode = V1TImode;
23581       nargs_constant = 1;
23582       break;
23583     case V8HI_FTYPE_V8HI_INT:
23584     case V8SF_FTYPE_V8SF_INT:
23585     case V4SI_FTYPE_V4SI_INT:
23586     case V4SI_FTYPE_V8SI_INT:
23587     case V4HI_FTYPE_V4HI_INT:
23588     case V4DF_FTYPE_V4DF_INT:
23589     case V4SF_FTYPE_V4SF_INT:
23590     case V4SF_FTYPE_V8SF_INT:
23591     case V2DI_FTYPE_V2DI_INT:
23592     case V2DF_FTYPE_V2DF_INT:
23593     case V2DF_FTYPE_V4DF_INT:
23594       nargs = 2;
23595       nargs_constant = 1;
23596       break;
23597     case V16QI_FTYPE_V16QI_V16QI_V16QI:
23598     case V8SF_FTYPE_V8SF_V8SF_V8SF:
23599     case V4DF_FTYPE_V4DF_V4DF_V4DF:
23600     case V4SF_FTYPE_V4SF_V4SF_V4SF:
23601     case V2DF_FTYPE_V2DF_V2DF_V2DF:
23602       nargs = 3;
23603       break;
23604     case V16QI_FTYPE_V16QI_V16QI_INT:
23605     case V8HI_FTYPE_V8HI_V8HI_INT:
23606     case V8SI_FTYPE_V8SI_V8SI_INT:
23607     case V8SI_FTYPE_V8SI_V4SI_INT:
23608     case V8SF_FTYPE_V8SF_V8SF_INT: 
23609     case V8SF_FTYPE_V8SF_V4SF_INT: 
23610     case V4SI_FTYPE_V4SI_V4SI_INT:
23611     case V4DF_FTYPE_V4DF_V4DF_INT:
23612     case V4DF_FTYPE_V4DF_V2DF_INT:
23613     case V4SF_FTYPE_V4SF_V4SF_INT:
23614     case V2DI_FTYPE_V2DI_V2DI_INT:
23615     case V2DF_FTYPE_V2DF_V2DF_INT:
23616       nargs = 3;
23617       nargs_constant = 1;
23618       break;
23619     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
23620       nargs = 3;
23621       rmode = V2DImode;
23622       nargs_constant = 1;
23623       break;
23624     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
23625       nargs = 3;
23626       rmode = DImode;
23627       nargs_constant = 1;
23628       break;
23629     case V2DI_FTYPE_V2DI_UINT_UINT:
23630       nargs = 3;
23631       nargs_constant = 2;
23632       break;
23633     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
23634     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
23635     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
23636     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
23637       nargs = 4;
23638       nargs_constant = 1;
23639       break;
23640     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
23641       nargs = 4;
23642       nargs_constant = 2;
23643       break;
23644     default:
23645       gcc_unreachable ();
23646     }
23647
23648   gcc_assert (nargs <= ARRAY_SIZE (args));
23649
23650   if (comparison != UNKNOWN)
23651     {
23652       gcc_assert (nargs == 2);
23653       return ix86_expand_sse_compare (d, exp, target, swap);
23654     }
23655
23656   if (rmode == VOIDmode || rmode == tmode)
23657     {
23658       if (optimize
23659           || target == 0
23660           || GET_MODE (target) != tmode
23661           || ! (*insn_p->operand[0].predicate) (target, tmode))
23662         target = gen_reg_rtx (tmode);
23663       real_target = target;
23664     }
23665   else
23666     {
23667       target = gen_reg_rtx (rmode);
23668       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
23669     }
23670
23671   for (i = 0; i < nargs; i++)
23672     {
23673       tree arg = CALL_EXPR_ARG (exp, i);
23674       rtx op = expand_normal (arg);
23675       enum machine_mode mode = insn_p->operand[i + 1].mode;
23676       bool match = (*insn_p->operand[i + 1].predicate) (op, mode);
23677
23678       if (last_arg_count && (i + 1) == nargs)
23679         {
23680           /* SIMD shift insns take either an 8-bit immediate or
23681              register as count.  But builtin functions take int as
23682              count.  If count doesn't match, we put it in register.  */
23683           if (!match)
23684             {
23685               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
23686               if (!(*insn_p->operand[i + 1].predicate) (op, mode))
23687                 op = copy_to_reg (op);
23688             }
23689         }
23690       else if ((nargs - i) <= nargs_constant)
23691         {
23692           if (!match)
23693             switch (icode)
23694               {
23695               case CODE_FOR_sse4_1_roundpd:
23696               case CODE_FOR_sse4_1_roundps:
23697               case CODE_FOR_sse4_1_roundsd:
23698               case CODE_FOR_sse4_1_roundss:
23699               case CODE_FOR_sse4_1_blendps:
23700               case CODE_FOR_avx_blendpd256:
23701               case CODE_FOR_avx_vpermilv4df:
23702               case CODE_FOR_avx_roundpd256:
23703               case CODE_FOR_avx_roundps256:
23704                 error ("the last argument must be a 4-bit immediate");
23705                 return const0_rtx;
23706
23707               case CODE_FOR_sse4_1_blendpd:
23708               case CODE_FOR_avx_vpermilv2df:
23709               case CODE_FOR_xop_vpermil2v2df3:
23710               case CODE_FOR_xop_vpermil2v4sf3:
23711               case CODE_FOR_xop_vpermil2v4df3:
23712               case CODE_FOR_xop_vpermil2v8sf3:
23713                 error ("the last argument must be a 2-bit immediate");
23714                 return const0_rtx;
23715
23716               case CODE_FOR_avx_vextractf128v4df:
23717               case CODE_FOR_avx_vextractf128v8sf:
23718               case CODE_FOR_avx_vextractf128v8si:
23719               case CODE_FOR_avx_vinsertf128v4df:
23720               case CODE_FOR_avx_vinsertf128v8sf:
23721               case CODE_FOR_avx_vinsertf128v8si:
23722                 error ("the last argument must be a 1-bit immediate");
23723                 return const0_rtx;
23724
23725               case CODE_FOR_avx_cmpsdv2df3:
23726               case CODE_FOR_avx_cmpssv4sf3:
23727               case CODE_FOR_avx_cmppdv2df3:
23728               case CODE_FOR_avx_cmppsv4sf3:
23729               case CODE_FOR_avx_cmppdv4df3:
23730               case CODE_FOR_avx_cmppsv8sf3:
23731                 error ("the last argument must be a 5-bit immediate");
23732                 return const0_rtx;
23733
23734              default:
23735                 switch (nargs_constant)
23736                   {
23737                   case 2:
23738                     if ((nargs - i) == nargs_constant)
23739                       {
23740                         error ("the next to last argument must be an 8-bit immediate");
23741                         break;
23742                       }
23743                   case 1:
23744                     error ("the last argument must be an 8-bit immediate");
23745                     break;
23746                   default:
23747                     gcc_unreachable ();
23748                   }
23749                 return const0_rtx;
23750               }
23751         }
23752       else
23753         {
23754           if (VECTOR_MODE_P (mode))
23755             op = safe_vector_operand (op, mode);
23756
23757           /* If we aren't optimizing, only allow one memory operand to
23758              be generated.  */
23759           if (memory_operand (op, mode))
23760             num_memory++;
23761
23762           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
23763             {
23764               if (optimize || !match || num_memory > 1)
23765                 op = copy_to_mode_reg (mode, op);
23766             }
23767           else
23768             {
23769               op = copy_to_reg (op);
23770               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
23771             }
23772         }
23773
23774       args[i].op = op;
23775       args[i].mode = mode;
23776     }
23777
23778   switch (nargs)
23779     {
23780     case 1:
23781       pat = GEN_FCN (icode) (real_target, args[0].op);
23782       break;
23783     case 2:
23784       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
23785       break;
23786     case 3:
23787       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
23788                              args[2].op);
23789       break;
23790     case 4:
23791       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
23792                              args[2].op, args[3].op);
23793       break;
23794     default:
23795       gcc_unreachable ();
23796     }
23797
23798   if (! pat)
23799     return 0;
23800
23801   emit_insn (pat);
23802   return target;
23803 }
23804
23805 /* Subroutine of ix86_expand_builtin to take care of special insns
23806    with variable number of operands.  */
23807
23808 static rtx
23809 ix86_expand_special_args_builtin (const struct builtin_description *d,
23810                                     tree exp, rtx target)
23811 {
23812   tree arg;
23813   rtx pat, op;
23814   unsigned int i, nargs, arg_adjust, memory;
23815   struct
23816     {
23817       rtx op;
23818       enum machine_mode mode;
23819     } args[3];
23820   enum insn_code icode = d->icode;
23821   bool last_arg_constant = false;
23822   const struct insn_data *insn_p = &insn_data[icode];
23823   enum machine_mode tmode = insn_p->operand[0].mode;
23824   enum { load, store } klass;
23825
23826   switch ((enum ix86_builtin_func_type) d->flag)
23827     {
23828     case VOID_FTYPE_VOID:
23829       emit_insn (GEN_FCN (icode) (target));
23830       return 0;
23831     case UINT64_FTYPE_VOID:
23832       nargs = 0;
23833       klass = load;
23834       memory = 0;
23835       break;
23836     case UINT64_FTYPE_PUNSIGNED:
23837     case V2DI_FTYPE_PV2DI:
23838     case V32QI_FTYPE_PCCHAR:
23839     case V16QI_FTYPE_PCCHAR:
23840     case V8SF_FTYPE_PCV4SF:
23841     case V8SF_FTYPE_PCFLOAT:
23842     case V4SF_FTYPE_PCFLOAT:
23843     case V4DF_FTYPE_PCV2DF:
23844     case V4DF_FTYPE_PCDOUBLE:
23845     case V2DF_FTYPE_PCDOUBLE:
23846     case VOID_FTYPE_PVOID:
23847       nargs = 1;
23848       klass = load;
23849       memory = 0;
23850       break;
23851     case VOID_FTYPE_PV2SF_V4SF:
23852     case VOID_FTYPE_PV4DI_V4DI:
23853     case VOID_FTYPE_PV2DI_V2DI:
23854     case VOID_FTYPE_PCHAR_V32QI:
23855     case VOID_FTYPE_PCHAR_V16QI:
23856     case VOID_FTYPE_PFLOAT_V8SF:
23857     case VOID_FTYPE_PFLOAT_V4SF:
23858     case VOID_FTYPE_PDOUBLE_V4DF:
23859     case VOID_FTYPE_PDOUBLE_V2DF:
23860     case VOID_FTYPE_PULONGLONG_ULONGLONG:
23861     case VOID_FTYPE_PINT_INT:
23862       nargs = 1;
23863       klass = store;
23864       /* Reserve memory operand for target.  */
23865       memory = ARRAY_SIZE (args);
23866       break;
23867     case V4SF_FTYPE_V4SF_PCV2SF:
23868     case V2DF_FTYPE_V2DF_PCDOUBLE:
23869       nargs = 2;
23870       klass = load;
23871       memory = 1;
23872       break;
23873     case V8SF_FTYPE_PCV8SF_V8SF:
23874     case V4DF_FTYPE_PCV4DF_V4DF:
23875     case V4SF_FTYPE_PCV4SF_V4SF:
23876     case V2DF_FTYPE_PCV2DF_V2DF:
23877       nargs = 2;
23878       klass = load;
23879       memory = 0;
23880       break;
23881     case VOID_FTYPE_PV8SF_V8SF_V8SF:
23882     case VOID_FTYPE_PV4DF_V4DF_V4DF:
23883     case VOID_FTYPE_PV4SF_V4SF_V4SF:
23884     case VOID_FTYPE_PV2DF_V2DF_V2DF:
23885       nargs = 2;
23886       klass = store;
23887       /* Reserve memory operand for target.  */
23888       memory = ARRAY_SIZE (args);
23889       break;
23890     case VOID_FTYPE_UINT_UINT_UINT:
23891     case VOID_FTYPE_UINT64_UINT_UINT:
23892     case UCHAR_FTYPE_UINT_UINT_UINT:
23893     case UCHAR_FTYPE_UINT64_UINT_UINT:
23894       nargs = 3;
23895       klass = load;
23896       memory = ARRAY_SIZE (args);
23897       last_arg_constant = true;
23898       break;
23899     default:
23900       gcc_unreachable ();
23901     }
23902
23903   gcc_assert (nargs <= ARRAY_SIZE (args));
23904
23905   if (klass == store)
23906     {
23907       arg = CALL_EXPR_ARG (exp, 0);
23908       op = expand_normal (arg);
23909       gcc_assert (target == 0);
23910       target = gen_rtx_MEM (tmode, copy_to_mode_reg (Pmode, op));
23911       arg_adjust = 1;
23912     }
23913   else
23914     {
23915       arg_adjust = 0;
23916       if (optimize
23917           || target == 0
23918           || GET_MODE (target) != tmode
23919           || ! (*insn_p->operand[0].predicate) (target, tmode))
23920         target = gen_reg_rtx (tmode);
23921     }
23922
23923   for (i = 0; i < nargs; i++)
23924     {
23925       enum machine_mode mode = insn_p->operand[i + 1].mode;
23926       bool match;
23927
23928       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
23929       op = expand_normal (arg);
23930       match = (*insn_p->operand[i + 1].predicate) (op, mode);
23931
23932       if (last_arg_constant && (i + 1) == nargs)
23933         {
23934           if (!match)
23935             {
23936               if (icode == CODE_FOR_lwp_lwpvalsi3
23937                   || icode == CODE_FOR_lwp_lwpinssi3
23938                   || icode == CODE_FOR_lwp_lwpvaldi3
23939                   || icode == CODE_FOR_lwp_lwpinsdi3)
23940                 error ("the last argument must be a 32-bit immediate");
23941               else
23942                 error ("the last argument must be an 8-bit immediate");
23943               return const0_rtx;
23944             }
23945         }
23946       else
23947         {
23948           if (i == memory)
23949             {
23950               /* This must be the memory operand.  */
23951               op = gen_rtx_MEM (mode, copy_to_mode_reg (Pmode, op));
23952               gcc_assert (GET_MODE (op) == mode
23953                           || GET_MODE (op) == VOIDmode);
23954             }
23955           else
23956             {
23957               /* This must be register.  */
23958               if (VECTOR_MODE_P (mode))
23959                 op = safe_vector_operand (op, mode);
23960
23961               gcc_assert (GET_MODE (op) == mode
23962                           || GET_MODE (op) == VOIDmode);
23963               op = copy_to_mode_reg (mode, op);
23964             }
23965         }
23966
23967       args[i].op = op;
23968       args[i].mode = mode;
23969     }
23970
23971   switch (nargs)
23972     {
23973     case 0:
23974       pat = GEN_FCN (icode) (target);
23975       break;
23976     case 1:
23977       pat = GEN_FCN (icode) (target, args[0].op);
23978       break;
23979     case 2:
23980       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
23981       break;
23982     case 3:
23983       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
23984       break;
23985     default:
23986       gcc_unreachable ();
23987     }
23988
23989   if (! pat)
23990     return 0;
23991   emit_insn (pat);
23992   return klass == store ? 0 : target;
23993 }
23994
23995 /* Return the integer constant in ARG.  Constrain it to be in the range
23996    of the subparts of VEC_TYPE; issue an error if not.  */
23997
23998 static int
23999 get_element_number (tree vec_type, tree arg)
24000 {
24001   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
24002
24003   if (!host_integerp (arg, 1)
24004       || (elt = tree_low_cst (arg, 1), elt > max))
24005     {
24006       error ("selector must be an integer constant in the range 0..%wi", max);
24007       return 0;
24008     }
24009
24010   return elt;
24011 }
24012
24013 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
24014    ix86_expand_vector_init.  We DO have language-level syntax for this, in
24015    the form of  (type){ init-list }.  Except that since we can't place emms
24016    instructions from inside the compiler, we can't allow the use of MMX
24017    registers unless the user explicitly asks for it.  So we do *not* define
24018    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
24019    we have builtins invoked by mmintrin.h that gives us license to emit
24020    these sorts of instructions.  */
24021
24022 static rtx
24023 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
24024 {
24025   enum machine_mode tmode = TYPE_MODE (type);
24026   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
24027   int i, n_elt = GET_MODE_NUNITS (tmode);
24028   rtvec v = rtvec_alloc (n_elt);
24029
24030   gcc_assert (VECTOR_MODE_P (tmode));
24031   gcc_assert (call_expr_nargs (exp) == n_elt);
24032
24033   for (i = 0; i < n_elt; ++i)
24034     {
24035       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
24036       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
24037     }
24038
24039   if (!target || !register_operand (target, tmode))
24040     target = gen_reg_rtx (tmode);
24041
24042   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
24043   return target;
24044 }
24045
24046 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
24047    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
24048    had a language-level syntax for referencing vector elements.  */
24049
24050 static rtx
24051 ix86_expand_vec_ext_builtin (tree exp, rtx target)
24052 {
24053   enum machine_mode tmode, mode0;
24054   tree arg0, arg1;
24055   int elt;
24056   rtx op0;
24057
24058   arg0 = CALL_EXPR_ARG (exp, 0);
24059   arg1 = CALL_EXPR_ARG (exp, 1);
24060
24061   op0 = expand_normal (arg0);
24062   elt = get_element_number (TREE_TYPE (arg0), arg1);
24063
24064   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
24065   mode0 = TYPE_MODE (TREE_TYPE (arg0));
24066   gcc_assert (VECTOR_MODE_P (mode0));
24067
24068   op0 = force_reg (mode0, op0);
24069
24070   if (optimize || !target || !register_operand (target, tmode))
24071     target = gen_reg_rtx (tmode);
24072
24073   ix86_expand_vector_extract (true, target, op0, elt);
24074
24075   return target;
24076 }
24077
24078 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
24079    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
24080    a language-level syntax for referencing vector elements.  */
24081
24082 static rtx
24083 ix86_expand_vec_set_builtin (tree exp)
24084 {
24085   enum machine_mode tmode, mode1;
24086   tree arg0, arg1, arg2;
24087   int elt;
24088   rtx op0, op1, target;
24089
24090   arg0 = CALL_EXPR_ARG (exp, 0);
24091   arg1 = CALL_EXPR_ARG (exp, 1);
24092   arg2 = CALL_EXPR_ARG (exp, 2);
24093
24094   tmode = TYPE_MODE (TREE_TYPE (arg0));
24095   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
24096   gcc_assert (VECTOR_MODE_P (tmode));
24097
24098   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
24099   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
24100   elt = get_element_number (TREE_TYPE (arg0), arg2);
24101
24102   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
24103     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
24104
24105   op0 = force_reg (tmode, op0);
24106   op1 = force_reg (mode1, op1);
24107
24108   /* OP0 is the source of these builtin functions and shouldn't be
24109      modified.  Create a copy, use it and return it as target.  */
24110   target = gen_reg_rtx (tmode);
24111   emit_move_insn (target, op0);
24112   ix86_expand_vector_set (true, target, op1, elt);
24113
24114   return target;
24115 }
24116
24117 /* Expand an expression EXP that calls a built-in function,
24118    with result going to TARGET if that's convenient
24119    (and in mode MODE if that's convenient).
24120    SUBTARGET may be used as the target for computing one of EXP's operands.
24121    IGNORE is nonzero if the value is to be ignored.  */
24122
24123 static rtx
24124 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
24125                      enum machine_mode mode ATTRIBUTE_UNUSED,
24126                      int ignore ATTRIBUTE_UNUSED)
24127 {
24128   const struct builtin_description *d;
24129   size_t i;
24130   enum insn_code icode;
24131   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
24132   tree arg0, arg1, arg2;
24133   rtx op0, op1, op2, pat;
24134   enum machine_mode mode0, mode1, mode2;
24135   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
24136
24137   /* Determine whether the builtin function is available under the current ISA.
24138      Originally the builtin was not created if it wasn't applicable to the
24139      current ISA based on the command line switches.  With function specific
24140      options, we need to check in the context of the function making the call
24141      whether it is supported.  */
24142   if (ix86_builtins_isa[fcode].isa
24143       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
24144     {
24145       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
24146                                        NULL, NULL, false);
24147
24148       if (!opts)
24149         error ("%qE needs unknown isa option", fndecl);
24150       else
24151         {
24152           gcc_assert (opts != NULL);
24153           error ("%qE needs isa option %s", fndecl, opts);
24154           free (opts);
24155         }
24156       return const0_rtx;
24157     }
24158
24159   switch (fcode)
24160     {
24161     case IX86_BUILTIN_MASKMOVQ:
24162     case IX86_BUILTIN_MASKMOVDQU:
24163       icode = (fcode == IX86_BUILTIN_MASKMOVQ
24164                ? CODE_FOR_mmx_maskmovq
24165                : CODE_FOR_sse2_maskmovdqu);
24166       /* Note the arg order is different from the operand order.  */
24167       arg1 = CALL_EXPR_ARG (exp, 0);
24168       arg2 = CALL_EXPR_ARG (exp, 1);
24169       arg0 = CALL_EXPR_ARG (exp, 2);
24170       op0 = expand_normal (arg0);
24171       op1 = expand_normal (arg1);
24172       op2 = expand_normal (arg2);
24173       mode0 = insn_data[icode].operand[0].mode;
24174       mode1 = insn_data[icode].operand[1].mode;
24175       mode2 = insn_data[icode].operand[2].mode;
24176
24177       op0 = force_reg (Pmode, op0);
24178       op0 = gen_rtx_MEM (mode1, op0);
24179
24180       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
24181         op0 = copy_to_mode_reg (mode0, op0);
24182       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
24183         op1 = copy_to_mode_reg (mode1, op1);
24184       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
24185         op2 = copy_to_mode_reg (mode2, op2);
24186       pat = GEN_FCN (icode) (op0, op1, op2);
24187       if (! pat)
24188         return 0;
24189       emit_insn (pat);
24190       return 0;
24191
24192     case IX86_BUILTIN_LDMXCSR:
24193       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
24194       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
24195       emit_move_insn (target, op0);
24196       emit_insn (gen_sse_ldmxcsr (target));
24197       return 0;
24198
24199     case IX86_BUILTIN_STMXCSR:
24200       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
24201       emit_insn (gen_sse_stmxcsr (target));
24202       return copy_to_mode_reg (SImode, target);
24203
24204     case IX86_BUILTIN_CLFLUSH:
24205         arg0 = CALL_EXPR_ARG (exp, 0);
24206         op0 = expand_normal (arg0);
24207         icode = CODE_FOR_sse2_clflush;
24208         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
24209             op0 = copy_to_mode_reg (Pmode, op0);
24210
24211         emit_insn (gen_sse2_clflush (op0));
24212         return 0;
24213
24214     case IX86_BUILTIN_MONITOR:
24215       arg0 = CALL_EXPR_ARG (exp, 0);
24216       arg1 = CALL_EXPR_ARG (exp, 1);
24217       arg2 = CALL_EXPR_ARG (exp, 2);
24218       op0 = expand_normal (arg0);
24219       op1 = expand_normal (arg1);
24220       op2 = expand_normal (arg2);
24221       if (!REG_P (op0))
24222         op0 = copy_to_mode_reg (Pmode, op0);
24223       if (!REG_P (op1))
24224         op1 = copy_to_mode_reg (SImode, op1);
24225       if (!REG_P (op2))
24226         op2 = copy_to_mode_reg (SImode, op2);
24227       emit_insn ((*ix86_gen_monitor) (op0, op1, op2));
24228       return 0;
24229
24230     case IX86_BUILTIN_MWAIT:
24231       arg0 = CALL_EXPR_ARG (exp, 0);
24232       arg1 = CALL_EXPR_ARG (exp, 1);
24233       op0 = expand_normal (arg0);
24234       op1 = expand_normal (arg1);
24235       if (!REG_P (op0))
24236         op0 = copy_to_mode_reg (SImode, op0);
24237       if (!REG_P (op1))
24238         op1 = copy_to_mode_reg (SImode, op1);
24239       emit_insn (gen_sse3_mwait (op0, op1));
24240       return 0;
24241
24242     case IX86_BUILTIN_VEC_INIT_V2SI:
24243     case IX86_BUILTIN_VEC_INIT_V4HI:
24244     case IX86_BUILTIN_VEC_INIT_V8QI:
24245       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
24246
24247     case IX86_BUILTIN_VEC_EXT_V2DF:
24248     case IX86_BUILTIN_VEC_EXT_V2DI:
24249     case IX86_BUILTIN_VEC_EXT_V4SF:
24250     case IX86_BUILTIN_VEC_EXT_V4SI:
24251     case IX86_BUILTIN_VEC_EXT_V8HI:
24252     case IX86_BUILTIN_VEC_EXT_V2SI:
24253     case IX86_BUILTIN_VEC_EXT_V4HI:
24254     case IX86_BUILTIN_VEC_EXT_V16QI:
24255       return ix86_expand_vec_ext_builtin (exp, target);
24256
24257     case IX86_BUILTIN_VEC_SET_V2DI:
24258     case IX86_BUILTIN_VEC_SET_V4SF:
24259     case IX86_BUILTIN_VEC_SET_V4SI:
24260     case IX86_BUILTIN_VEC_SET_V8HI:
24261     case IX86_BUILTIN_VEC_SET_V4HI:
24262     case IX86_BUILTIN_VEC_SET_V16QI:
24263       return ix86_expand_vec_set_builtin (exp);
24264
24265     case IX86_BUILTIN_VEC_PERM_V2DF:
24266     case IX86_BUILTIN_VEC_PERM_V4SF:
24267     case IX86_BUILTIN_VEC_PERM_V2DI:
24268     case IX86_BUILTIN_VEC_PERM_V4SI:
24269     case IX86_BUILTIN_VEC_PERM_V8HI:
24270     case IX86_BUILTIN_VEC_PERM_V16QI:
24271     case IX86_BUILTIN_VEC_PERM_V2DI_U:
24272     case IX86_BUILTIN_VEC_PERM_V4SI_U:
24273     case IX86_BUILTIN_VEC_PERM_V8HI_U:
24274     case IX86_BUILTIN_VEC_PERM_V16QI_U:
24275     case IX86_BUILTIN_VEC_PERM_V4DF:
24276     case IX86_BUILTIN_VEC_PERM_V8SF:
24277       return ix86_expand_vec_perm_builtin (exp);
24278
24279     case IX86_BUILTIN_INFQ:
24280     case IX86_BUILTIN_HUGE_VALQ:
24281       {
24282         REAL_VALUE_TYPE inf;
24283         rtx tmp;
24284
24285         real_inf (&inf);
24286         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
24287
24288         tmp = validize_mem (force_const_mem (mode, tmp));
24289
24290         if (target == 0)
24291           target = gen_reg_rtx (mode);
24292
24293         emit_move_insn (target, tmp);
24294         return target;
24295       }
24296
24297     case IX86_BUILTIN_LLWPCB:
24298       arg0 = CALL_EXPR_ARG (exp, 0);
24299       op0 = expand_normal (arg0);
24300       icode = CODE_FOR_lwp_llwpcb;
24301       if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
24302         op0 = copy_to_mode_reg (Pmode, op0);
24303       emit_insn (gen_lwp_llwpcb (op0));
24304       return 0;
24305
24306     case IX86_BUILTIN_SLWPCB:
24307       icode = CODE_FOR_lwp_slwpcb;
24308       if (!target
24309           || ! (*insn_data[icode].operand[0].predicate) (target, Pmode))
24310         target = gen_reg_rtx (Pmode);
24311       emit_insn (gen_lwp_slwpcb (target));
24312       return target;
24313
24314     default:
24315       break;
24316     }
24317
24318   for (i = 0, d = bdesc_special_args;
24319        i < ARRAY_SIZE (bdesc_special_args);
24320        i++, d++)
24321     if (d->code == fcode)
24322       return ix86_expand_special_args_builtin (d, exp, target);
24323
24324   for (i = 0, d = bdesc_args;
24325        i < ARRAY_SIZE (bdesc_args);
24326        i++, d++)
24327     if (d->code == fcode)
24328       switch (fcode)
24329         {
24330         case IX86_BUILTIN_FABSQ:
24331         case IX86_BUILTIN_COPYSIGNQ:
24332           if (!TARGET_SSE2)
24333             /* Emit a normal call if SSE2 isn't available.  */
24334             return expand_call (exp, target, ignore);
24335         default:
24336           return ix86_expand_args_builtin (d, exp, target);
24337         }
24338
24339   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
24340     if (d->code == fcode)
24341       return ix86_expand_sse_comi (d, exp, target);
24342
24343   for (i = 0, d = bdesc_pcmpestr;
24344        i < ARRAY_SIZE (bdesc_pcmpestr);
24345        i++, d++)
24346     if (d->code == fcode)
24347       return ix86_expand_sse_pcmpestr (d, exp, target);
24348
24349   for (i = 0, d = bdesc_pcmpistr;
24350        i < ARRAY_SIZE (bdesc_pcmpistr);
24351        i++, d++)
24352     if (d->code == fcode)
24353       return ix86_expand_sse_pcmpistr (d, exp, target);
24354
24355   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
24356     if (d->code == fcode)
24357       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
24358                                             (enum ix86_builtin_func_type)
24359                                             d->flag, d->comparison);
24360
24361   gcc_unreachable ();
24362 }
24363
24364 /* Returns a function decl for a vectorized version of the builtin function
24365    with builtin function code FN and the result vector type TYPE, or NULL_TREE
24366    if it is not available.  */
24367
24368 static tree
24369 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
24370                                   tree type_in)
24371 {
24372   enum machine_mode in_mode, out_mode;
24373   int in_n, out_n;
24374   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
24375
24376   if (TREE_CODE (type_out) != VECTOR_TYPE
24377       || TREE_CODE (type_in) != VECTOR_TYPE
24378       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
24379     return NULL_TREE;
24380
24381   out_mode = TYPE_MODE (TREE_TYPE (type_out));
24382   out_n = TYPE_VECTOR_SUBPARTS (type_out);
24383   in_mode = TYPE_MODE (TREE_TYPE (type_in));
24384   in_n = TYPE_VECTOR_SUBPARTS (type_in);
24385
24386   switch (fn)
24387     {
24388     case BUILT_IN_SQRT:
24389       if (out_mode == DFmode && out_n == 2
24390           && in_mode == DFmode && in_n == 2)
24391         return ix86_builtins[IX86_BUILTIN_SQRTPD];
24392       break;
24393
24394     case BUILT_IN_SQRTF:
24395       if (out_mode == SFmode && out_n == 4
24396           && in_mode == SFmode && in_n == 4)
24397         return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
24398       break;
24399
24400     case BUILT_IN_LRINT:
24401       if (out_mode == SImode && out_n == 4
24402           && in_mode == DFmode && in_n == 2)
24403         return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
24404       break;
24405
24406     case BUILT_IN_LRINTF:
24407       if (out_mode == SImode && out_n == 4
24408           && in_mode == SFmode && in_n == 4)
24409         return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
24410       break;
24411
24412     case BUILT_IN_COPYSIGN:
24413       if (out_mode == DFmode && out_n == 2
24414           && in_mode == DFmode && in_n == 2)
24415         return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
24416       break;
24417
24418     case BUILT_IN_COPYSIGNF:
24419       if (out_mode == SFmode && out_n == 4
24420           && in_mode == SFmode && in_n == 4)
24421         return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
24422       break;
24423
24424     default:
24425       ;
24426     }
24427
24428   /* Dispatch to a handler for a vectorization library.  */
24429   if (ix86_veclib_handler)
24430     return (*ix86_veclib_handler) ((enum built_in_function) fn, type_out,
24431                                    type_in);
24432
24433   return NULL_TREE;
24434 }
24435
24436 /* Handler for an SVML-style interface to
24437    a library with vectorized intrinsics.  */
24438
24439 static tree
24440 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
24441 {
24442   char name[20];
24443   tree fntype, new_fndecl, args;
24444   unsigned arity;
24445   const char *bname;
24446   enum machine_mode el_mode, in_mode;
24447   int n, in_n;
24448
24449   /* The SVML is suitable for unsafe math only.  */
24450   if (!flag_unsafe_math_optimizations)
24451     return NULL_TREE;
24452
24453   el_mode = TYPE_MODE (TREE_TYPE (type_out));
24454   n = TYPE_VECTOR_SUBPARTS (type_out);
24455   in_mode = TYPE_MODE (TREE_TYPE (type_in));
24456   in_n = TYPE_VECTOR_SUBPARTS (type_in);
24457   if (el_mode != in_mode
24458       || n != in_n)
24459     return NULL_TREE;
24460
24461   switch (fn)
24462     {
24463     case BUILT_IN_EXP:
24464     case BUILT_IN_LOG:
24465     case BUILT_IN_LOG10:
24466     case BUILT_IN_POW:
24467     case BUILT_IN_TANH:
24468     case BUILT_IN_TAN:
24469     case BUILT_IN_ATAN:
24470     case BUILT_IN_ATAN2:
24471     case BUILT_IN_ATANH:
24472     case BUILT_IN_CBRT:
24473     case BUILT_IN_SINH:
24474     case BUILT_IN_SIN:
24475     case BUILT_IN_ASINH:
24476     case BUILT_IN_ASIN:
24477     case BUILT_IN_COSH:
24478     case BUILT_IN_COS:
24479     case BUILT_IN_ACOSH:
24480     case BUILT_IN_ACOS:
24481       if (el_mode != DFmode || n != 2)
24482         return NULL_TREE;
24483       break;
24484
24485     case BUILT_IN_EXPF:
24486     case BUILT_IN_LOGF:
24487     case BUILT_IN_LOG10F:
24488     case BUILT_IN_POWF:
24489     case BUILT_IN_TANHF:
24490     case BUILT_IN_TANF:
24491     case BUILT_IN_ATANF:
24492     case BUILT_IN_ATAN2F:
24493     case BUILT_IN_ATANHF:
24494     case BUILT_IN_CBRTF:
24495     case BUILT_IN_SINHF:
24496     case BUILT_IN_SINF:
24497     case BUILT_IN_ASINHF:
24498     case BUILT_IN_ASINF:
24499     case BUILT_IN_COSHF:
24500     case BUILT_IN_COSF:
24501     case BUILT_IN_ACOSHF:
24502     case BUILT_IN_ACOSF:
24503       if (el_mode != SFmode || n != 4)
24504         return NULL_TREE;
24505       break;
24506
24507     default:
24508       return NULL_TREE;
24509     }
24510
24511   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
24512
24513   if (fn == BUILT_IN_LOGF)
24514     strcpy (name, "vmlsLn4");
24515   else if (fn == BUILT_IN_LOG)
24516     strcpy (name, "vmldLn2");
24517   else if (n == 4)
24518     {
24519       sprintf (name, "vmls%s", bname+10);
24520       name[strlen (name)-1] = '4';
24521     }
24522   else
24523     sprintf (name, "vmld%s2", bname+10);
24524
24525   /* Convert to uppercase. */
24526   name[4] &= ~0x20;
24527
24528   arity = 0;
24529   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
24530        args = TREE_CHAIN (args))
24531     arity++;
24532
24533   if (arity == 1)
24534     fntype = build_function_type_list (type_out, type_in, NULL);
24535   else
24536     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
24537
24538   /* Build a function declaration for the vectorized function.  */
24539   new_fndecl = build_decl (BUILTINS_LOCATION,
24540                            FUNCTION_DECL, get_identifier (name), fntype);
24541   TREE_PUBLIC (new_fndecl) = 1;
24542   DECL_EXTERNAL (new_fndecl) = 1;
24543   DECL_IS_NOVOPS (new_fndecl) = 1;
24544   TREE_READONLY (new_fndecl) = 1;
24545
24546   return new_fndecl;
24547 }
24548
24549 /* Handler for an ACML-style interface to
24550    a library with vectorized intrinsics.  */
24551
24552 static tree
24553 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
24554 {
24555   char name[20] = "__vr.._";
24556   tree fntype, new_fndecl, args;
24557   unsigned arity;
24558   const char *bname;
24559   enum machine_mode el_mode, in_mode;
24560   int n, in_n;
24561
24562   /* The ACML is 64bits only and suitable for unsafe math only as
24563      it does not correctly support parts of IEEE with the required
24564      precision such as denormals.  */
24565   if (!TARGET_64BIT
24566       || !flag_unsafe_math_optimizations)
24567     return NULL_TREE;
24568
24569   el_mode = TYPE_MODE (TREE_TYPE (type_out));
24570   n = TYPE_VECTOR_SUBPARTS (type_out);
24571   in_mode = TYPE_MODE (TREE_TYPE (type_in));
24572   in_n = TYPE_VECTOR_SUBPARTS (type_in);
24573   if (el_mode != in_mode
24574       || n != in_n)
24575     return NULL_TREE;
24576
24577   switch (fn)
24578     {
24579     case BUILT_IN_SIN:
24580     case BUILT_IN_COS:
24581     case BUILT_IN_EXP:
24582     case BUILT_IN_LOG:
24583     case BUILT_IN_LOG2:
24584     case BUILT_IN_LOG10:
24585       name[4] = 'd';
24586       name[5] = '2';
24587       if (el_mode != DFmode
24588           || n != 2)
24589         return NULL_TREE;
24590       break;
24591
24592     case BUILT_IN_SINF:
24593     case BUILT_IN_COSF:
24594     case BUILT_IN_EXPF:
24595     case BUILT_IN_POWF:
24596     case BUILT_IN_LOGF:
24597     case BUILT_IN_LOG2F:
24598     case BUILT_IN_LOG10F:
24599       name[4] = 's';
24600       name[5] = '4';
24601       if (el_mode != SFmode
24602           || n != 4)
24603         return NULL_TREE;
24604       break;
24605
24606     default:
24607       return NULL_TREE;
24608     }
24609
24610   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
24611   sprintf (name + 7, "%s", bname+10);
24612
24613   arity = 0;
24614   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
24615        args = TREE_CHAIN (args))
24616     arity++;
24617
24618   if (arity == 1)
24619     fntype = build_function_type_list (type_out, type_in, NULL);
24620   else
24621     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
24622
24623   /* Build a function declaration for the vectorized function.  */
24624   new_fndecl = build_decl (BUILTINS_LOCATION,
24625                            FUNCTION_DECL, get_identifier (name), fntype);
24626   TREE_PUBLIC (new_fndecl) = 1;
24627   DECL_EXTERNAL (new_fndecl) = 1;
24628   DECL_IS_NOVOPS (new_fndecl) = 1;
24629   TREE_READONLY (new_fndecl) = 1;
24630
24631   return new_fndecl;
24632 }
24633
24634
24635 /* Returns a decl of a function that implements conversion of an integer vector
24636    into a floating-point vector, or vice-versa. TYPE is the type of the integer
24637    side of the conversion.
24638    Return NULL_TREE if it is not available.  */
24639
24640 static tree
24641 ix86_vectorize_builtin_conversion (unsigned int code, tree type)
24642 {
24643   if (! (TARGET_SSE2 && TREE_CODE (type) == VECTOR_TYPE))
24644     return NULL_TREE;
24645
24646   switch (code)
24647     {
24648     case FLOAT_EXPR:
24649       switch (TYPE_MODE (type))
24650         {
24651         case V4SImode:
24652           return TYPE_UNSIGNED (type)
24653             ? ix86_builtins[IX86_BUILTIN_CVTUDQ2PS]
24654             : ix86_builtins[IX86_BUILTIN_CVTDQ2PS];
24655         default:
24656           return NULL_TREE;
24657         }
24658
24659     case FIX_TRUNC_EXPR:
24660       switch (TYPE_MODE (type))
24661         {
24662         case V4SImode:
24663           return TYPE_UNSIGNED (type)
24664             ? NULL_TREE
24665             : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ];
24666         default:
24667           return NULL_TREE;
24668         }
24669     default:
24670       return NULL_TREE;
24671
24672     }
24673 }
24674
24675 /* Returns a code for a target-specific builtin that implements
24676    reciprocal of the function, or NULL_TREE if not available.  */
24677
24678 static tree
24679 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
24680                          bool sqrt ATTRIBUTE_UNUSED)
24681 {
24682   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
24683          && flag_finite_math_only && !flag_trapping_math
24684          && flag_unsafe_math_optimizations))
24685     return NULL_TREE;
24686
24687   if (md_fn)
24688     /* Machine dependent builtins.  */
24689     switch (fn)
24690       {
24691         /* Vectorized version of sqrt to rsqrt conversion.  */
24692       case IX86_BUILTIN_SQRTPS_NR:
24693         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
24694
24695       default:
24696         return NULL_TREE;
24697       }
24698   else
24699     /* Normal builtins.  */
24700     switch (fn)
24701       {
24702         /* Sqrt to rsqrt conversion.  */
24703       case BUILT_IN_SQRTF:
24704         return ix86_builtins[IX86_BUILTIN_RSQRTF];
24705
24706       default:
24707         return NULL_TREE;
24708       }
24709 }
24710 \f
24711 /* Helper for avx_vpermilps256_operand et al.  This is also used by
24712    the expansion functions to turn the parallel back into a mask.
24713    The return value is 0 for no match and the imm8+1 for a match.  */
24714
24715 int
24716 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
24717 {
24718   unsigned i, nelt = GET_MODE_NUNITS (mode);
24719   unsigned mask = 0;
24720   unsigned char ipar[8];
24721
24722   if (XVECLEN (par, 0) != (int) nelt)
24723     return 0;
24724
24725   /* Validate that all of the elements are constants, and not totally
24726      out of range.  Copy the data into an integral array to make the
24727      subsequent checks easier.  */
24728   for (i = 0; i < nelt; ++i)
24729     {
24730       rtx er = XVECEXP (par, 0, i);
24731       unsigned HOST_WIDE_INT ei;
24732
24733       if (!CONST_INT_P (er))
24734         return 0;
24735       ei = INTVAL (er);
24736       if (ei >= nelt)
24737         return 0;
24738       ipar[i] = ei;
24739     }
24740
24741   switch (mode)
24742     {
24743     case V4DFmode:
24744       /* In the 256-bit DFmode case, we can only move elements within
24745          a 128-bit lane.  */
24746       for (i = 0; i < 2; ++i)
24747         {
24748           if (ipar[i] >= 2)
24749             return 0;
24750           mask |= ipar[i] << i;
24751         }
24752       for (i = 2; i < 4; ++i)
24753         {
24754           if (ipar[i] < 2)
24755             return 0;
24756           mask |= (ipar[i] - 2) << i;
24757         }
24758       break;
24759
24760     case V8SFmode:
24761       /* In the 256-bit SFmode case, we have full freedom of movement
24762          within the low 128-bit lane, but the high 128-bit lane must
24763          mirror the exact same pattern.  */
24764       for (i = 0; i < 4; ++i)
24765         if (ipar[i] + 4 != ipar[i + 4])
24766           return 0;
24767       nelt = 4;
24768       /* FALLTHRU */
24769
24770     case V2DFmode:
24771     case V4SFmode:
24772       /* In the 128-bit case, we've full freedom in the placement of
24773          the elements from the source operand.  */
24774       for (i = 0; i < nelt; ++i)
24775         mask |= ipar[i] << (i * (nelt / 2));
24776       break;
24777
24778     default:
24779       gcc_unreachable ();
24780     }
24781
24782   /* Make sure success has a non-zero value by adding one.  */
24783   return mask + 1;
24784 }
24785
24786 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
24787    the expansion functions to turn the parallel back into a mask.
24788    The return value is 0 for no match and the imm8+1 for a match.  */
24789
24790 int
24791 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
24792 {
24793   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
24794   unsigned mask = 0;
24795   unsigned char ipar[8];
24796
24797   if (XVECLEN (par, 0) != (int) nelt)
24798     return 0;
24799
24800   /* Validate that all of the elements are constants, and not totally
24801      out of range.  Copy the data into an integral array to make the
24802      subsequent checks easier.  */
24803   for (i = 0; i < nelt; ++i)
24804     {
24805       rtx er = XVECEXP (par, 0, i);
24806       unsigned HOST_WIDE_INT ei;
24807
24808       if (!CONST_INT_P (er))
24809         return 0;
24810       ei = INTVAL (er);
24811       if (ei >= 2 * nelt)
24812         return 0;
24813       ipar[i] = ei;
24814     }
24815
24816   /* Validate that the halves of the permute are halves.  */
24817   for (i = 0; i < nelt2 - 1; ++i)
24818     if (ipar[i] + 1 != ipar[i + 1])
24819       return 0;
24820   for (i = nelt2; i < nelt - 1; ++i)
24821     if (ipar[i] + 1 != ipar[i + 1])
24822       return 0;
24823
24824   /* Reconstruct the mask.  */
24825   for (i = 0; i < 2; ++i)
24826     {
24827       unsigned e = ipar[i * nelt2];
24828       if (e % nelt2)
24829         return 0;
24830       e /= nelt2;
24831       mask |= e << (i * 4);
24832     }
24833
24834   /* Make sure success has a non-zero value by adding one.  */
24835   return mask + 1;
24836 }
24837 \f
24838
24839 /* Store OPERAND to the memory after reload is completed.  This means
24840    that we can't easily use assign_stack_local.  */
24841 rtx
24842 ix86_force_to_memory (enum machine_mode mode, rtx operand)
24843 {
24844   rtx result;
24845
24846   gcc_assert (reload_completed);
24847   if (!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE)
24848     {
24849       result = gen_rtx_MEM (mode,
24850                             gen_rtx_PLUS (Pmode,
24851                                           stack_pointer_rtx,
24852                                           GEN_INT (-RED_ZONE_SIZE)));
24853       emit_move_insn (result, operand);
24854     }
24855   else if ((TARGET_64BIT_MS_ABI || !TARGET_RED_ZONE) && TARGET_64BIT)
24856     {
24857       switch (mode)
24858         {
24859         case HImode:
24860         case SImode:
24861           operand = gen_lowpart (DImode, operand);
24862           /* FALLTHRU */
24863         case DImode:
24864           emit_insn (
24865                       gen_rtx_SET (VOIDmode,
24866                                    gen_rtx_MEM (DImode,
24867                                                 gen_rtx_PRE_DEC (DImode,
24868                                                         stack_pointer_rtx)),
24869                                    operand));
24870           break;
24871         default:
24872           gcc_unreachable ();
24873         }
24874       result = gen_rtx_MEM (mode, stack_pointer_rtx);
24875     }
24876   else
24877     {
24878       switch (mode)
24879         {
24880         case DImode:
24881           {
24882             rtx operands[2];
24883             split_di (&operand, 1, operands, operands + 1);
24884             emit_insn (
24885                         gen_rtx_SET (VOIDmode,
24886                                      gen_rtx_MEM (SImode,
24887                                                   gen_rtx_PRE_DEC (Pmode,
24888                                                         stack_pointer_rtx)),
24889                                      operands[1]));
24890             emit_insn (
24891                         gen_rtx_SET (VOIDmode,
24892                                      gen_rtx_MEM (SImode,
24893                                                   gen_rtx_PRE_DEC (Pmode,
24894                                                         stack_pointer_rtx)),
24895                                      operands[0]));
24896           }
24897           break;
24898         case HImode:
24899           /* Store HImodes as SImodes.  */
24900           operand = gen_lowpart (SImode, operand);
24901           /* FALLTHRU */
24902         case SImode:
24903           emit_insn (
24904                       gen_rtx_SET (VOIDmode,
24905                                    gen_rtx_MEM (GET_MODE (operand),
24906                                                 gen_rtx_PRE_DEC (SImode,
24907                                                         stack_pointer_rtx)),
24908                                    operand));
24909           break;
24910         default:
24911           gcc_unreachable ();
24912         }
24913       result = gen_rtx_MEM (mode, stack_pointer_rtx);
24914     }
24915   return result;
24916 }
24917
24918 /* Free operand from the memory.  */
24919 void
24920 ix86_free_from_memory (enum machine_mode mode)
24921 {
24922   if (!TARGET_RED_ZONE || TARGET_64BIT_MS_ABI)
24923     {
24924       int size;
24925
24926       if (mode == DImode || TARGET_64BIT)
24927         size = 8;
24928       else
24929         size = 4;
24930       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
24931          to pop or add instruction if registers are available.  */
24932       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
24933                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
24934                                             GEN_INT (size))));
24935     }
24936 }
24937
24938 /* Implement TARGET_IRA_COVER_CLASSES.  If -mfpmath=sse, we prefer
24939    SSE_REGS to FLOAT_REGS if their costs for a pseudo are the
24940    same.  */
24941 static const enum reg_class *
24942 i386_ira_cover_classes (void)
24943 {
24944   static const enum reg_class sse_fpmath_classes[] = {
24945     GENERAL_REGS, SSE_REGS, MMX_REGS, FLOAT_REGS, LIM_REG_CLASSES
24946   };
24947   static const enum reg_class no_sse_fpmath_classes[] = {
24948     GENERAL_REGS, FLOAT_REGS, MMX_REGS, SSE_REGS, LIM_REG_CLASSES
24949   };
24950
24951  return TARGET_SSE_MATH ? sse_fpmath_classes : no_sse_fpmath_classes;
24952 }
24953
24954 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
24955    QImode must go into class Q_REGS.
24956    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
24957    movdf to do mem-to-mem moves through integer regs.  */
24958 enum reg_class
24959 ix86_preferred_reload_class (rtx x, enum reg_class regclass)
24960 {
24961   enum machine_mode mode = GET_MODE (x);
24962
24963   /* We're only allowed to return a subclass of CLASS.  Many of the
24964      following checks fail for NO_REGS, so eliminate that early.  */
24965   if (regclass == NO_REGS)
24966     return NO_REGS;
24967
24968   /* All classes can load zeros.  */
24969   if (x == CONST0_RTX (mode))
24970     return regclass;
24971
24972   /* Force constants into memory if we are loading a (nonzero) constant into
24973      an MMX or SSE register.  This is because there are no MMX/SSE instructions
24974      to load from a constant.  */
24975   if (CONSTANT_P (x)
24976       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
24977     return NO_REGS;
24978
24979   /* Prefer SSE regs only, if we can use them for math.  */
24980   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
24981     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
24982
24983   /* Floating-point constants need more complex checks.  */
24984   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
24985     {
24986       /* General regs can load everything.  */
24987       if (reg_class_subset_p (regclass, GENERAL_REGS))
24988         return regclass;
24989
24990       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
24991          zero above.  We only want to wind up preferring 80387 registers if
24992          we plan on doing computation with them.  */
24993       if (TARGET_80387
24994           && standard_80387_constant_p (x))
24995         {
24996           /* Limit class to non-sse.  */
24997           if (regclass == FLOAT_SSE_REGS)
24998             return FLOAT_REGS;
24999           if (regclass == FP_TOP_SSE_REGS)
25000             return FP_TOP_REG;
25001           if (regclass == FP_SECOND_SSE_REGS)
25002             return FP_SECOND_REG;
25003           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
25004             return regclass;
25005         }
25006
25007       return NO_REGS;
25008     }
25009
25010   /* Generally when we see PLUS here, it's the function invariant
25011      (plus soft-fp const_int).  Which can only be computed into general
25012      regs.  */
25013   if (GET_CODE (x) == PLUS)
25014     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
25015
25016   /* QImode constants are easy to load, but non-constant QImode data
25017      must go into Q_REGS.  */
25018   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
25019     {
25020       if (reg_class_subset_p (regclass, Q_REGS))
25021         return regclass;
25022       if (reg_class_subset_p (Q_REGS, regclass))
25023         return Q_REGS;
25024       return NO_REGS;
25025     }
25026
25027   return regclass;
25028 }
25029
25030 /* Discourage putting floating-point values in SSE registers unless
25031    SSE math is being used, and likewise for the 387 registers.  */
25032 enum reg_class
25033 ix86_preferred_output_reload_class (rtx x, enum reg_class regclass)
25034 {
25035   enum machine_mode mode = GET_MODE (x);
25036
25037   /* Restrict the output reload class to the register bank that we are doing
25038      math on.  If we would like not to return a subset of CLASS, reject this
25039      alternative: if reload cannot do this, it will still use its choice.  */
25040   mode = GET_MODE (x);
25041   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
25042     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
25043
25044   if (X87_FLOAT_MODE_P (mode))
25045     {
25046       if (regclass == FP_TOP_SSE_REGS)
25047         return FP_TOP_REG;
25048       else if (regclass == FP_SECOND_SSE_REGS)
25049         return FP_SECOND_REG;
25050       else
25051         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
25052     }
25053
25054   return regclass;
25055 }
25056
25057 static enum reg_class
25058 ix86_secondary_reload (bool in_p, rtx x, enum reg_class rclass,
25059                        enum machine_mode mode,
25060                        secondary_reload_info *sri ATTRIBUTE_UNUSED)
25061 {
25062   /* QImode spills from non-QI registers require
25063      intermediate register on 32bit targets.  */
25064   if (!in_p && mode == QImode && !TARGET_64BIT
25065       && (rclass == GENERAL_REGS
25066           || rclass == LEGACY_REGS
25067           || rclass == INDEX_REGS))
25068     {
25069       int regno;
25070
25071       if (REG_P (x))
25072         regno = REGNO (x);
25073       else
25074         regno = -1;
25075
25076       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
25077         regno = true_regnum (x);
25078
25079       /* Return Q_REGS if the operand is in memory.  */
25080       if (regno == -1)
25081         return Q_REGS;
25082     }
25083
25084   return NO_REGS;
25085 }
25086
25087 /* If we are copying between general and FP registers, we need a memory
25088    location. The same is true for SSE and MMX registers.
25089
25090    To optimize register_move_cost performance, allow inline variant.
25091
25092    The macro can't work reliably when one of the CLASSES is class containing
25093    registers from multiple units (SSE, MMX, integer).  We avoid this by never
25094    combining those units in single alternative in the machine description.
25095    Ensure that this constraint holds to avoid unexpected surprises.
25096
25097    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
25098    enforce these sanity checks.  */
25099
25100 static inline int
25101 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
25102                               enum machine_mode mode, int strict)
25103 {
25104   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
25105       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
25106       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
25107       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
25108       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
25109       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
25110     {
25111       gcc_assert (!strict);
25112       return true;
25113     }
25114
25115   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
25116     return true;
25117
25118   /* ??? This is a lie.  We do have moves between mmx/general, and for
25119      mmx/sse2.  But by saying we need secondary memory we discourage the
25120      register allocator from using the mmx registers unless needed.  */
25121   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
25122     return true;
25123
25124   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
25125     {
25126       /* SSE1 doesn't have any direct moves from other classes.  */
25127       if (!TARGET_SSE2)
25128         return true;
25129
25130       /* If the target says that inter-unit moves are more expensive
25131          than moving through memory, then don't generate them.  */
25132       if (!TARGET_INTER_UNIT_MOVES)
25133         return true;
25134
25135       /* Between SSE and general, we have moves no larger than word size.  */
25136       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
25137         return true;
25138     }
25139
25140   return false;
25141 }
25142
25143 int
25144 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
25145                               enum machine_mode mode, int strict)
25146 {
25147   return inline_secondary_memory_needed (class1, class2, mode, strict);
25148 }
25149
25150 /* Return true if the registers in CLASS cannot represent the change from
25151    modes FROM to TO.  */
25152
25153 bool
25154 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
25155                                enum reg_class regclass)
25156 {
25157   if (from == to)
25158     return false;
25159
25160   /* x87 registers can't do subreg at all, as all values are reformatted
25161      to extended precision.  */
25162   if (MAYBE_FLOAT_CLASS_P (regclass))
25163     return true;
25164
25165   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
25166     {
25167       /* Vector registers do not support QI or HImode loads.  If we don't
25168          disallow a change to these modes, reload will assume it's ok to
25169          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
25170          the vec_dupv4hi pattern.  */
25171       if (GET_MODE_SIZE (from) < 4)
25172         return true;
25173
25174       /* Vector registers do not support subreg with nonzero offsets, which
25175          are otherwise valid for integer registers.  Since we can't see
25176          whether we have a nonzero offset from here, prohibit all
25177          nonparadoxical subregs changing size.  */
25178       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
25179         return true;
25180     }
25181
25182   return false;
25183 }
25184
25185 /* Return the cost of moving data of mode M between a
25186    register and memory.  A value of 2 is the default; this cost is
25187    relative to those in `REGISTER_MOVE_COST'.
25188
25189    This function is used extensively by register_move_cost that is used to
25190    build tables at startup.  Make it inline in this case.
25191    When IN is 2, return maximum of in and out move cost.
25192
25193    If moving between registers and memory is more expensive than
25194    between two registers, you should define this macro to express the
25195    relative cost.
25196
25197    Model also increased moving costs of QImode registers in non
25198    Q_REGS classes.
25199  */
25200 static inline int
25201 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
25202                          int in)
25203 {
25204   int cost;
25205   if (FLOAT_CLASS_P (regclass))
25206     {
25207       int index;
25208       switch (mode)
25209         {
25210           case SFmode:
25211             index = 0;
25212             break;
25213           case DFmode:
25214             index = 1;
25215             break;
25216           case XFmode:
25217             index = 2;
25218             break;
25219           default:
25220             return 100;
25221         }
25222       if (in == 2)
25223         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
25224       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
25225     }
25226   if (SSE_CLASS_P (regclass))
25227     {
25228       int index;
25229       switch (GET_MODE_SIZE (mode))
25230         {
25231           case 4:
25232             index = 0;
25233             break;
25234           case 8:
25235             index = 1;
25236             break;
25237           case 16:
25238             index = 2;
25239             break;
25240           default:
25241             return 100;
25242         }
25243       if (in == 2)
25244         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
25245       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
25246     }
25247   if (MMX_CLASS_P (regclass))
25248     {
25249       int index;
25250       switch (GET_MODE_SIZE (mode))
25251         {
25252           case 4:
25253             index = 0;
25254             break;
25255           case 8:
25256             index = 1;
25257             break;
25258           default:
25259             return 100;
25260         }
25261       if (in)
25262         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
25263       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
25264     }
25265   switch (GET_MODE_SIZE (mode))
25266     {
25267       case 1:
25268         if (Q_CLASS_P (regclass) || TARGET_64BIT)
25269           {
25270             if (!in)
25271               return ix86_cost->int_store[0];
25272             if (TARGET_PARTIAL_REG_DEPENDENCY
25273                 && optimize_function_for_speed_p (cfun))
25274               cost = ix86_cost->movzbl_load;
25275             else
25276               cost = ix86_cost->int_load[0];
25277             if (in == 2)
25278               return MAX (cost, ix86_cost->int_store[0]);
25279             return cost;
25280           }
25281         else
25282           {
25283            if (in == 2)
25284              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
25285            if (in)
25286              return ix86_cost->movzbl_load;
25287            else
25288              return ix86_cost->int_store[0] + 4;
25289           }
25290         break;
25291       case 2:
25292         if (in == 2)
25293           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
25294         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
25295       default:
25296         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
25297         if (mode == TFmode)
25298           mode = XFmode;
25299         if (in == 2)
25300           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
25301         else if (in)
25302           cost = ix86_cost->int_load[2];
25303         else
25304           cost = ix86_cost->int_store[2];
25305         return (cost * (((int) GET_MODE_SIZE (mode)
25306                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
25307     }
25308 }
25309
25310 int
25311 ix86_memory_move_cost (enum machine_mode mode, enum reg_class regclass, int in)
25312 {
25313   return inline_memory_move_cost (mode, regclass, in);
25314 }
25315
25316
25317 /* Return the cost of moving data from a register in class CLASS1 to
25318    one in class CLASS2.
25319
25320    It is not required that the cost always equal 2 when FROM is the same as TO;
25321    on some machines it is expensive to move between registers if they are not
25322    general registers.  */
25323
25324 int
25325 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
25326                          enum reg_class class2)
25327 {
25328   /* In case we require secondary memory, compute cost of the store followed
25329      by load.  In order to avoid bad register allocation choices, we need
25330      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
25331
25332   if (inline_secondary_memory_needed (class1, class2, mode, 0))
25333     {
25334       int cost = 1;
25335
25336       cost += inline_memory_move_cost (mode, class1, 2);
25337       cost += inline_memory_move_cost (mode, class2, 2);
25338
25339       /* In case of copying from general_purpose_register we may emit multiple
25340          stores followed by single load causing memory size mismatch stall.
25341          Count this as arbitrarily high cost of 20.  */
25342       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
25343         cost += 20;
25344
25345       /* In the case of FP/MMX moves, the registers actually overlap, and we
25346          have to switch modes in order to treat them differently.  */
25347       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
25348           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
25349         cost += 20;
25350
25351       return cost;
25352     }
25353
25354   /* Moves between SSE/MMX and integer unit are expensive.  */
25355   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
25356       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
25357
25358     /* ??? By keeping returned value relatively high, we limit the number
25359        of moves between integer and MMX/SSE registers for all targets.
25360        Additionally, high value prevents problem with x86_modes_tieable_p(),
25361        where integer modes in MMX/SSE registers are not tieable
25362        because of missing QImode and HImode moves to, from or between
25363        MMX/SSE registers.  */
25364     return MAX (8, ix86_cost->mmxsse_to_integer);
25365
25366   if (MAYBE_FLOAT_CLASS_P (class1))
25367     return ix86_cost->fp_move;
25368   if (MAYBE_SSE_CLASS_P (class1))
25369     return ix86_cost->sse_move;
25370   if (MAYBE_MMX_CLASS_P (class1))
25371     return ix86_cost->mmx_move;
25372   return 2;
25373 }
25374
25375 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
25376
25377 bool
25378 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
25379 {
25380   /* Flags and only flags can only hold CCmode values.  */
25381   if (CC_REGNO_P (regno))
25382     return GET_MODE_CLASS (mode) == MODE_CC;
25383   if (GET_MODE_CLASS (mode) == MODE_CC
25384       || GET_MODE_CLASS (mode) == MODE_RANDOM
25385       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
25386     return 0;
25387   if (FP_REGNO_P (regno))
25388     return VALID_FP_MODE_P (mode);
25389   if (SSE_REGNO_P (regno))
25390     {
25391       /* We implement the move patterns for all vector modes into and
25392          out of SSE registers, even when no operation instructions
25393          are available.  OImode move is available only when AVX is
25394          enabled.  */
25395       return ((TARGET_AVX && mode == OImode)
25396               || VALID_AVX256_REG_MODE (mode)
25397               || VALID_SSE_REG_MODE (mode)
25398               || VALID_SSE2_REG_MODE (mode)
25399               || VALID_MMX_REG_MODE (mode)
25400               || VALID_MMX_REG_MODE_3DNOW (mode));
25401     }
25402   if (MMX_REGNO_P (regno))
25403     {
25404       /* We implement the move patterns for 3DNOW modes even in MMX mode,
25405          so if the register is available at all, then we can move data of
25406          the given mode into or out of it.  */
25407       return (VALID_MMX_REG_MODE (mode)
25408               || VALID_MMX_REG_MODE_3DNOW (mode));
25409     }
25410
25411   if (mode == QImode)
25412     {
25413       /* Take care for QImode values - they can be in non-QI regs,
25414          but then they do cause partial register stalls.  */
25415       if (regno <= BX_REG || TARGET_64BIT)
25416         return 1;
25417       if (!TARGET_PARTIAL_REG_STALL)
25418         return 1;
25419       return reload_in_progress || reload_completed;
25420     }
25421   /* We handle both integer and floats in the general purpose registers.  */
25422   else if (VALID_INT_MODE_P (mode))
25423     return 1;
25424   else if (VALID_FP_MODE_P (mode))
25425     return 1;
25426   else if (VALID_DFP_MODE_P (mode))
25427     return 1;
25428   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
25429      on to use that value in smaller contexts, this can easily force a
25430      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
25431      supporting DImode, allow it.  */
25432   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
25433     return 1;
25434
25435   return 0;
25436 }
25437
25438 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
25439    tieable integer mode.  */
25440
25441 static bool
25442 ix86_tieable_integer_mode_p (enum machine_mode mode)
25443 {
25444   switch (mode)
25445     {
25446     case HImode:
25447     case SImode:
25448       return true;
25449
25450     case QImode:
25451       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
25452
25453     case DImode:
25454       return TARGET_64BIT;
25455
25456     default:
25457       return false;
25458     }
25459 }
25460
25461 /* Return true if MODE1 is accessible in a register that can hold MODE2
25462    without copying.  That is, all register classes that can hold MODE2
25463    can also hold MODE1.  */
25464
25465 bool
25466 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
25467 {
25468   if (mode1 == mode2)
25469     return true;
25470
25471   if (ix86_tieable_integer_mode_p (mode1)
25472       && ix86_tieable_integer_mode_p (mode2))
25473     return true;
25474
25475   /* MODE2 being XFmode implies fp stack or general regs, which means we
25476      can tie any smaller floating point modes to it.  Note that we do not
25477      tie this with TFmode.  */
25478   if (mode2 == XFmode)
25479     return mode1 == SFmode || mode1 == DFmode;
25480
25481   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
25482      that we can tie it with SFmode.  */
25483   if (mode2 == DFmode)
25484     return mode1 == SFmode;
25485
25486   /* If MODE2 is only appropriate for an SSE register, then tie with
25487      any other mode acceptable to SSE registers.  */
25488   if (GET_MODE_SIZE (mode2) == 16
25489       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
25490     return (GET_MODE_SIZE (mode1) == 16
25491             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
25492
25493   /* If MODE2 is appropriate for an MMX register, then tie
25494      with any other mode acceptable to MMX registers.  */
25495   if (GET_MODE_SIZE (mode2) == 8
25496       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
25497     return (GET_MODE_SIZE (mode1) == 8
25498             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
25499
25500   return false;
25501 }
25502
25503 /* Compute a (partial) cost for rtx X.  Return true if the complete
25504    cost has been computed, and false if subexpressions should be
25505    scanned.  In either case, *TOTAL contains the cost result.  */
25506
25507 static bool
25508 ix86_rtx_costs (rtx x, int code, int outer_code_i, int *total, bool speed)
25509 {
25510   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
25511   enum machine_mode mode = GET_MODE (x);
25512   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
25513
25514   switch (code)
25515     {
25516     case CONST_INT:
25517     case CONST:
25518     case LABEL_REF:
25519     case SYMBOL_REF:
25520       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
25521         *total = 3;
25522       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
25523         *total = 2;
25524       else if (flag_pic && SYMBOLIC_CONST (x)
25525                && (!TARGET_64BIT
25526                    || (!GET_CODE (x) != LABEL_REF
25527                        && (GET_CODE (x) != SYMBOL_REF
25528                            || !SYMBOL_REF_LOCAL_P (x)))))
25529         *total = 1;
25530       else
25531         *total = 0;
25532       return true;
25533
25534     case CONST_DOUBLE:
25535       if (mode == VOIDmode)
25536         *total = 0;
25537       else
25538         switch (standard_80387_constant_p (x))
25539           {
25540           case 1: /* 0.0 */
25541             *total = 1;
25542             break;
25543           default: /* Other constants */
25544             *total = 2;
25545             break;
25546           case 0:
25547           case -1:
25548             /* Start with (MEM (SYMBOL_REF)), since that's where
25549                it'll probably end up.  Add a penalty for size.  */
25550             *total = (COSTS_N_INSNS (1)
25551                       + (flag_pic != 0 && !TARGET_64BIT)
25552                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
25553             break;
25554           }
25555       return true;
25556
25557     case ZERO_EXTEND:
25558       /* The zero extensions is often completely free on x86_64, so make
25559          it as cheap as possible.  */
25560       if (TARGET_64BIT && mode == DImode
25561           && GET_MODE (XEXP (x, 0)) == SImode)
25562         *total = 1;
25563       else if (TARGET_ZERO_EXTEND_WITH_AND)
25564         *total = cost->add;
25565       else
25566         *total = cost->movzx;
25567       return false;
25568
25569     case SIGN_EXTEND:
25570       *total = cost->movsx;
25571       return false;
25572
25573     case ASHIFT:
25574       if (CONST_INT_P (XEXP (x, 1))
25575           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
25576         {
25577           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
25578           if (value == 1)
25579             {
25580               *total = cost->add;
25581               return false;
25582             }
25583           if ((value == 2 || value == 3)
25584               && cost->lea <= cost->shift_const)
25585             {
25586               *total = cost->lea;
25587               return false;
25588             }
25589         }
25590       /* FALLTHRU */
25591
25592     case ROTATE:
25593     case ASHIFTRT:
25594     case LSHIFTRT:
25595     case ROTATERT:
25596       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
25597         {
25598           if (CONST_INT_P (XEXP (x, 1)))
25599             {
25600               if (INTVAL (XEXP (x, 1)) > 32)
25601                 *total = cost->shift_const + COSTS_N_INSNS (2);
25602               else
25603                 *total = cost->shift_const * 2;
25604             }
25605           else
25606             {
25607               if (GET_CODE (XEXP (x, 1)) == AND)
25608                 *total = cost->shift_var * 2;
25609               else
25610                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
25611             }
25612         }
25613       else
25614         {
25615           if (CONST_INT_P (XEXP (x, 1)))
25616             *total = cost->shift_const;
25617           else
25618             *total = cost->shift_var;
25619         }
25620       return false;
25621
25622     case MULT:
25623       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
25624         {
25625           /* ??? SSE scalar cost should be used here.  */
25626           *total = cost->fmul;
25627           return false;
25628         }
25629       else if (X87_FLOAT_MODE_P (mode))
25630         {
25631           *total = cost->fmul;
25632           return false;
25633         }
25634       else if (FLOAT_MODE_P (mode))
25635         {
25636           /* ??? SSE vector cost should be used here.  */
25637           *total = cost->fmul;
25638           return false;
25639         }
25640       else
25641         {
25642           rtx op0 = XEXP (x, 0);
25643           rtx op1 = XEXP (x, 1);
25644           int nbits;
25645           if (CONST_INT_P (XEXP (x, 1)))
25646             {
25647               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
25648               for (nbits = 0; value != 0; value &= value - 1)
25649                 nbits++;
25650             }
25651           else
25652             /* This is arbitrary.  */
25653             nbits = 7;
25654
25655           /* Compute costs correctly for widening multiplication.  */
25656           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
25657               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
25658                  == GET_MODE_SIZE (mode))
25659             {
25660               int is_mulwiden = 0;
25661               enum machine_mode inner_mode = GET_MODE (op0);
25662
25663               if (GET_CODE (op0) == GET_CODE (op1))
25664                 is_mulwiden = 1, op1 = XEXP (op1, 0);
25665               else if (CONST_INT_P (op1))
25666                 {
25667                   if (GET_CODE (op0) == SIGN_EXTEND)
25668                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
25669                                   == INTVAL (op1);
25670                   else
25671                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
25672                 }
25673
25674               if (is_mulwiden)
25675                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
25676             }
25677
25678           *total = (cost->mult_init[MODE_INDEX (mode)]
25679                     + nbits * cost->mult_bit
25680                     + rtx_cost (op0, outer_code, speed) + rtx_cost (op1, outer_code, speed));
25681
25682           return true;
25683         }
25684
25685     case DIV:
25686     case UDIV:
25687     case MOD:
25688     case UMOD:
25689       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
25690         /* ??? SSE cost should be used here.  */
25691         *total = cost->fdiv;
25692       else if (X87_FLOAT_MODE_P (mode))
25693         *total = cost->fdiv;
25694       else if (FLOAT_MODE_P (mode))
25695         /* ??? SSE vector cost should be used here.  */
25696         *total = cost->fdiv;
25697       else
25698         *total = cost->divide[MODE_INDEX (mode)];
25699       return false;
25700
25701     case PLUS:
25702       if (GET_MODE_CLASS (mode) == MODE_INT
25703                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
25704         {
25705           if (GET_CODE (XEXP (x, 0)) == PLUS
25706               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
25707               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
25708               && CONSTANT_P (XEXP (x, 1)))
25709             {
25710               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
25711               if (val == 2 || val == 4 || val == 8)
25712                 {
25713                   *total = cost->lea;
25714                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
25715                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
25716                                       outer_code, speed);
25717                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
25718                   return true;
25719                 }
25720             }
25721           else if (GET_CODE (XEXP (x, 0)) == MULT
25722                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
25723             {
25724               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
25725               if (val == 2 || val == 4 || val == 8)
25726                 {
25727                   *total = cost->lea;
25728                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
25729                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
25730                   return true;
25731                 }
25732             }
25733           else if (GET_CODE (XEXP (x, 0)) == PLUS)
25734             {
25735               *total = cost->lea;
25736               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
25737               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
25738               *total += rtx_cost (XEXP (x, 1), outer_code, speed);
25739               return true;
25740             }
25741         }
25742       /* FALLTHRU */
25743
25744     case MINUS:
25745       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
25746         {
25747           /* ??? SSE cost should be used here.  */
25748           *total = cost->fadd;
25749           return false;
25750         }
25751       else if (X87_FLOAT_MODE_P (mode))
25752         {
25753           *total = cost->fadd;
25754           return false;
25755         }
25756       else if (FLOAT_MODE_P (mode))
25757         {
25758           /* ??? SSE vector cost should be used here.  */
25759           *total = cost->fadd;
25760           return false;
25761         }
25762       /* FALLTHRU */
25763
25764     case AND:
25765     case IOR:
25766     case XOR:
25767       if (!TARGET_64BIT && mode == DImode)
25768         {
25769           *total = (cost->add * 2
25770                     + (rtx_cost (XEXP (x, 0), outer_code, speed)
25771                        << (GET_MODE (XEXP (x, 0)) != DImode))
25772                     + (rtx_cost (XEXP (x, 1), outer_code, speed)
25773                        << (GET_MODE (XEXP (x, 1)) != DImode)));
25774           return true;
25775         }
25776       /* FALLTHRU */
25777
25778     case NEG:
25779       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
25780         {
25781           /* ??? SSE cost should be used here.  */
25782           *total = cost->fchs;
25783           return false;
25784         }
25785       else if (X87_FLOAT_MODE_P (mode))
25786         {
25787           *total = cost->fchs;
25788           return false;
25789         }
25790       else if (FLOAT_MODE_P (mode))
25791         {
25792           /* ??? SSE vector cost should be used here.  */
25793           *total = cost->fchs;
25794           return false;
25795         }
25796       /* FALLTHRU */
25797
25798     case NOT:
25799       if (!TARGET_64BIT && mode == DImode)
25800         *total = cost->add * 2;
25801       else
25802         *total = cost->add;
25803       return false;
25804
25805     case COMPARE:
25806       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
25807           && XEXP (XEXP (x, 0), 1) == const1_rtx
25808           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
25809           && XEXP (x, 1) == const0_rtx)
25810         {
25811           /* This kind of construct is implemented using test[bwl].
25812              Treat it as if we had an AND.  */
25813           *total = (cost->add
25814                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed)
25815                     + rtx_cost (const1_rtx, outer_code, speed));
25816           return true;
25817         }
25818       return false;
25819
25820     case FLOAT_EXTEND:
25821       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
25822         *total = 0;
25823       return false;
25824
25825     case ABS:
25826       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
25827         /* ??? SSE cost should be used here.  */
25828         *total = cost->fabs;
25829       else if (X87_FLOAT_MODE_P (mode))
25830         *total = cost->fabs;
25831       else if (FLOAT_MODE_P (mode))
25832         /* ??? SSE vector cost should be used here.  */
25833         *total = cost->fabs;
25834       return false;
25835
25836     case SQRT:
25837       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
25838         /* ??? SSE cost should be used here.  */
25839         *total = cost->fsqrt;
25840       else if (X87_FLOAT_MODE_P (mode))
25841         *total = cost->fsqrt;
25842       else if (FLOAT_MODE_P (mode))
25843         /* ??? SSE vector cost should be used here.  */
25844         *total = cost->fsqrt;
25845       return false;
25846
25847     case UNSPEC:
25848       if (XINT (x, 1) == UNSPEC_TP)
25849         *total = 0;
25850       return false;
25851
25852     case VEC_SELECT:
25853     case VEC_CONCAT:
25854     case VEC_MERGE:
25855     case VEC_DUPLICATE:
25856       /* ??? Assume all of these vector manipulation patterns are
25857          recognizable.  In which case they all pretty much have the
25858          same cost.  */
25859      *total = COSTS_N_INSNS (1);
25860      return true;
25861
25862     default:
25863       return false;
25864     }
25865 }
25866
25867 #if TARGET_MACHO
25868
25869 static int current_machopic_label_num;
25870
25871 /* Given a symbol name and its associated stub, write out the
25872    definition of the stub.  */
25873
25874 void
25875 machopic_output_stub (FILE *file, const char *symb, const char *stub)
25876 {
25877   unsigned int length;
25878   char *binder_name, *symbol_name, lazy_ptr_name[32];
25879   int label = ++current_machopic_label_num;
25880
25881   /* For 64-bit we shouldn't get here.  */
25882   gcc_assert (!TARGET_64BIT);
25883
25884   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
25885   symb = (*targetm.strip_name_encoding) (symb);
25886
25887   length = strlen (stub);
25888   binder_name = XALLOCAVEC (char, length + 32);
25889   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
25890
25891   length = strlen (symb);
25892   symbol_name = XALLOCAVEC (char, length + 32);
25893   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
25894
25895   sprintf (lazy_ptr_name, "L%d$lz", label);
25896
25897   if (MACHOPIC_PURE)
25898     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
25899   else
25900     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
25901
25902   fprintf (file, "%s:\n", stub);
25903   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25904
25905   if (MACHOPIC_PURE)
25906     {
25907       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
25908       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
25909       fprintf (file, "\tjmp\t*%%edx\n");
25910     }
25911   else
25912     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
25913
25914   fprintf (file, "%s:\n", binder_name);
25915
25916   if (MACHOPIC_PURE)
25917     {
25918       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
25919       fputs ("\tpushl\t%eax\n", file);
25920     }
25921   else
25922     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
25923
25924   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
25925
25926   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
25927   fprintf (file, "%s:\n", lazy_ptr_name);
25928   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
25929   fprintf (file, ASM_LONG "%s\n", binder_name);
25930 }
25931 #endif /* TARGET_MACHO */
25932
25933 /* Order the registers for register allocator.  */
25934
25935 void
25936 x86_order_regs_for_local_alloc (void)
25937 {
25938    int pos = 0;
25939    int i;
25940
25941    /* First allocate the local general purpose registers.  */
25942    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
25943      if (GENERAL_REGNO_P (i) && call_used_regs[i])
25944         reg_alloc_order [pos++] = i;
25945
25946    /* Global general purpose registers.  */
25947    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
25948      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
25949         reg_alloc_order [pos++] = i;
25950
25951    /* x87 registers come first in case we are doing FP math
25952       using them.  */
25953    if (!TARGET_SSE_MATH)
25954      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
25955        reg_alloc_order [pos++] = i;
25956
25957    /* SSE registers.  */
25958    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
25959      reg_alloc_order [pos++] = i;
25960    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
25961      reg_alloc_order [pos++] = i;
25962
25963    /* x87 registers.  */
25964    if (TARGET_SSE_MATH)
25965      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
25966        reg_alloc_order [pos++] = i;
25967
25968    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
25969      reg_alloc_order [pos++] = i;
25970
25971    /* Initialize the rest of array as we do not allocate some registers
25972       at all.  */
25973    while (pos < FIRST_PSEUDO_REGISTER)
25974      reg_alloc_order [pos++] = 0;
25975 }
25976
25977 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
25978    struct attribute_spec.handler.  */
25979 static tree
25980 ix86_handle_abi_attribute (tree *node, tree name,
25981                               tree args ATTRIBUTE_UNUSED,
25982                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
25983 {
25984   if (TREE_CODE (*node) != FUNCTION_TYPE
25985       && TREE_CODE (*node) != METHOD_TYPE
25986       && TREE_CODE (*node) != FIELD_DECL
25987       && TREE_CODE (*node) != TYPE_DECL)
25988     {
25989       warning (OPT_Wattributes, "%qE attribute only applies to functions",
25990                name);
25991       *no_add_attrs = true;
25992       return NULL_TREE;
25993     }
25994   if (!TARGET_64BIT)
25995     {
25996       warning (OPT_Wattributes, "%qE attribute only available for 64-bit",
25997                name);
25998       *no_add_attrs = true;
25999       return NULL_TREE;
26000     }
26001
26002   /* Can combine regparm with all attributes but fastcall.  */
26003   if (is_attribute_p ("ms_abi", name))
26004     {
26005       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
26006         {
26007           error ("ms_abi and sysv_abi attributes are not compatible");
26008         }
26009
26010       return NULL_TREE;
26011     }
26012   else if (is_attribute_p ("sysv_abi", name))
26013     {
26014       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
26015         {
26016           error ("ms_abi and sysv_abi attributes are not compatible");
26017         }
26018
26019       return NULL_TREE;
26020     }
26021
26022   return NULL_TREE;
26023 }
26024
26025 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
26026    struct attribute_spec.handler.  */
26027 static tree
26028 ix86_handle_struct_attribute (tree *node, tree name,
26029                               tree args ATTRIBUTE_UNUSED,
26030                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26031 {
26032   tree *type = NULL;
26033   if (DECL_P (*node))
26034     {
26035       if (TREE_CODE (*node) == TYPE_DECL)
26036         type = &TREE_TYPE (*node);
26037     }
26038   else
26039     type = node;
26040
26041   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
26042                  || TREE_CODE (*type) == UNION_TYPE)))
26043     {
26044       warning (OPT_Wattributes, "%qE attribute ignored",
26045                name);
26046       *no_add_attrs = true;
26047     }
26048
26049   else if ((is_attribute_p ("ms_struct", name)
26050             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
26051            || ((is_attribute_p ("gcc_struct", name)
26052                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
26053     {
26054       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
26055                name);
26056       *no_add_attrs = true;
26057     }
26058
26059   return NULL_TREE;
26060 }
26061
26062 static tree
26063 ix86_handle_fndecl_attribute (tree *node, tree name,
26064                               tree args ATTRIBUTE_UNUSED,
26065                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26066 {
26067   if (TREE_CODE (*node) != FUNCTION_DECL)
26068     {
26069       warning (OPT_Wattributes, "%qE attribute only applies to functions",
26070                name);
26071       *no_add_attrs = true;
26072       return NULL_TREE;
26073     }
26074
26075   if (TARGET_64BIT)
26076     {
26077       warning (OPT_Wattributes, "%qE attribute only available for 32-bit",
26078                name);
26079       return NULL_TREE;
26080     }
26081
26082 #ifndef HAVE_AS_IX86_SWAP
26083   sorry ("ms_hook_prologue attribute needs assembler swap suffix support");
26084 #endif
26085
26086     return NULL_TREE;
26087 }
26088
26089 static bool
26090 ix86_ms_bitfield_layout_p (const_tree record_type)
26091 {
26092   return (TARGET_MS_BITFIELD_LAYOUT &&
26093           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
26094     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
26095 }
26096
26097 /* Returns an expression indicating where the this parameter is
26098    located on entry to the FUNCTION.  */
26099
26100 static rtx
26101 x86_this_parameter (tree function)
26102 {
26103   tree type = TREE_TYPE (function);
26104   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
26105   int nregs;
26106
26107   if (TARGET_64BIT)
26108     {
26109       const int *parm_regs;
26110
26111       if (ix86_function_type_abi (type) == MS_ABI)
26112         parm_regs = x86_64_ms_abi_int_parameter_registers;
26113       else
26114         parm_regs = x86_64_int_parameter_registers;
26115       return gen_rtx_REG (DImode, parm_regs[aggr]);
26116     }
26117
26118   nregs = ix86_function_regparm (type, function);
26119
26120   if (nregs > 0 && !stdarg_p (type))
26121     {
26122       int regno;
26123
26124       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
26125         regno = aggr ? DX_REG : CX_REG;
26126       else
26127         {
26128           regno = AX_REG;
26129           if (aggr)
26130             {
26131               regno = DX_REG;
26132               if (nregs == 1)
26133                 return gen_rtx_MEM (SImode,
26134                                     plus_constant (stack_pointer_rtx, 4));
26135             }
26136         }
26137       return gen_rtx_REG (SImode, regno);
26138     }
26139
26140   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
26141 }
26142
26143 /* Determine whether x86_output_mi_thunk can succeed.  */
26144
26145 static bool
26146 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
26147                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
26148                          HOST_WIDE_INT vcall_offset, const_tree function)
26149 {
26150   /* 64-bit can handle anything.  */
26151   if (TARGET_64BIT)
26152     return true;
26153
26154   /* For 32-bit, everything's fine if we have one free register.  */
26155   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
26156     return true;
26157
26158   /* Need a free register for vcall_offset.  */
26159   if (vcall_offset)
26160     return false;
26161
26162   /* Need a free register for GOT references.  */
26163   if (flag_pic && !(*targetm.binds_local_p) (function))
26164     return false;
26165
26166   /* Otherwise ok.  */
26167   return true;
26168 }
26169
26170 /* Output the assembler code for a thunk function.  THUNK_DECL is the
26171    declaration for the thunk function itself, FUNCTION is the decl for
26172    the target function.  DELTA is an immediate constant offset to be
26173    added to THIS.  If VCALL_OFFSET is nonzero, the word at
26174    *(*this + vcall_offset) should be added to THIS.  */
26175
26176 static void
26177 x86_output_mi_thunk (FILE *file,
26178                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
26179                      HOST_WIDE_INT vcall_offset, tree function)
26180 {
26181   rtx xops[3];
26182   rtx this_param = x86_this_parameter (function);
26183   rtx this_reg, tmp;
26184
26185   /* Make sure unwind info is emitted for the thunk if needed.  */
26186   final_start_function (emit_barrier (), file, 1);
26187
26188   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
26189      pull it in now and let DELTA benefit.  */
26190   if (REG_P (this_param))
26191     this_reg = this_param;
26192   else if (vcall_offset)
26193     {
26194       /* Put the this parameter into %eax.  */
26195       xops[0] = this_param;
26196       xops[1] = this_reg = gen_rtx_REG (Pmode, AX_REG);
26197       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
26198     }
26199   else
26200     this_reg = NULL_RTX;
26201
26202   /* Adjust the this parameter by a fixed constant.  */
26203   if (delta)
26204     {
26205       xops[0] = GEN_INT (delta);
26206       xops[1] = this_reg ? this_reg : this_param;
26207       if (TARGET_64BIT)
26208         {
26209           if (!x86_64_general_operand (xops[0], DImode))
26210             {
26211               tmp = gen_rtx_REG (DImode, R10_REG);
26212               xops[1] = tmp;
26213               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
26214               xops[0] = tmp;
26215               xops[1] = this_param;
26216             }
26217           if (x86_maybe_negate_const_int (&xops[0], DImode))
26218             output_asm_insn ("sub{q}\t{%0, %1|%1, %0}", xops);
26219           else
26220             output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
26221         }
26222       else if (x86_maybe_negate_const_int (&xops[0], SImode))
26223         output_asm_insn ("sub{l}\t{%0, %1|%1, %0}", xops);
26224       else
26225         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
26226     }
26227
26228   /* Adjust the this parameter by a value stored in the vtable.  */
26229   if (vcall_offset)
26230     {
26231       if (TARGET_64BIT)
26232         tmp = gen_rtx_REG (DImode, R10_REG);
26233       else
26234         {
26235           int tmp_regno = CX_REG;
26236           if (lookup_attribute ("fastcall",
26237                                 TYPE_ATTRIBUTES (TREE_TYPE (function))))
26238             tmp_regno = AX_REG;
26239           tmp = gen_rtx_REG (SImode, tmp_regno);
26240         }
26241
26242       xops[0] = gen_rtx_MEM (Pmode, this_reg);
26243       xops[1] = tmp;
26244       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
26245
26246       /* Adjust the this parameter.  */
26247       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
26248       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
26249         {
26250           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
26251           xops[0] = GEN_INT (vcall_offset);
26252           xops[1] = tmp2;
26253           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
26254           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
26255         }
26256       xops[1] = this_reg;
26257       output_asm_insn ("add%z1\t{%0, %1|%1, %0}", xops);
26258     }
26259
26260   /* If necessary, drop THIS back to its stack slot.  */
26261   if (this_reg && this_reg != this_param)
26262     {
26263       xops[0] = this_reg;
26264       xops[1] = this_param;
26265       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
26266     }
26267
26268   xops[0] = XEXP (DECL_RTL (function), 0);
26269   if (TARGET_64BIT)
26270     {
26271       if (!flag_pic || (*targetm.binds_local_p) (function))
26272         output_asm_insn ("jmp\t%P0", xops);
26273       /* All thunks should be in the same object as their target,
26274          and thus binds_local_p should be true.  */
26275       else if (TARGET_64BIT && cfun->machine->call_abi == MS_ABI)
26276         gcc_unreachable ();
26277       else
26278         {
26279           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
26280           tmp = gen_rtx_CONST (Pmode, tmp);
26281           tmp = gen_rtx_MEM (QImode, tmp);
26282           xops[0] = tmp;
26283           output_asm_insn ("jmp\t%A0", xops);
26284         }
26285     }
26286   else
26287     {
26288       if (!flag_pic || (*targetm.binds_local_p) (function))
26289         output_asm_insn ("jmp\t%P0", xops);
26290       else
26291 #if TARGET_MACHO
26292         if (TARGET_MACHO)
26293           {
26294             rtx sym_ref = XEXP (DECL_RTL (function), 0);
26295             tmp = (gen_rtx_SYMBOL_REF
26296                    (Pmode,
26297                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
26298             tmp = gen_rtx_MEM (QImode, tmp);
26299             xops[0] = tmp;
26300             output_asm_insn ("jmp\t%0", xops);
26301           }
26302         else
26303 #endif /* TARGET_MACHO */
26304         {
26305           tmp = gen_rtx_REG (SImode, CX_REG);
26306           output_set_got (tmp, NULL_RTX);
26307
26308           xops[1] = tmp;
26309           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
26310           output_asm_insn ("jmp\t{*}%1", xops);
26311         }
26312     }
26313   final_end_function ();
26314 }
26315
26316 static void
26317 x86_file_start (void)
26318 {
26319   default_file_start ();
26320 #if TARGET_MACHO
26321   darwin_file_start ();
26322 #endif
26323   if (X86_FILE_START_VERSION_DIRECTIVE)
26324     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
26325   if (X86_FILE_START_FLTUSED)
26326     fputs ("\t.global\t__fltused\n", asm_out_file);
26327   if (ix86_asm_dialect == ASM_INTEL)
26328     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
26329 }
26330
26331 int
26332 x86_field_alignment (tree field, int computed)
26333 {
26334   enum machine_mode mode;
26335   tree type = TREE_TYPE (field);
26336
26337   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
26338     return computed;
26339   mode = TYPE_MODE (strip_array_types (type));
26340   if (mode == DFmode || mode == DCmode
26341       || GET_MODE_CLASS (mode) == MODE_INT
26342       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
26343     return MIN (32, computed);
26344   return computed;
26345 }
26346
26347 /* Output assembler code to FILE to increment profiler label # LABELNO
26348    for profiling a function entry.  */
26349 void
26350 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
26351 {
26352   if (TARGET_64BIT)
26353     {
26354 #ifndef NO_PROFILE_COUNTERS
26355       fprintf (file, "\tleaq\t" LPREFIX "P%d(%%rip),%%r11\n", labelno);
26356 #endif
26357
26358       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
26359         fputs ("\tcall\t*" MCOUNT_NAME "@GOTPCREL(%rip)\n", file);
26360       else
26361         fputs ("\tcall\t" MCOUNT_NAME "\n", file);
26362     }
26363   else if (flag_pic)
26364     {
26365 #ifndef NO_PROFILE_COUNTERS
26366       fprintf (file, "\tleal\t" LPREFIX "P%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
26367                labelno);
26368 #endif
26369       fputs ("\tcall\t*" MCOUNT_NAME "@GOT(%ebx)\n", file);
26370     }
26371   else
26372     {
26373 #ifndef NO_PROFILE_COUNTERS
26374       fprintf (file, "\tmovl\t$" LPREFIX "P%d,%%" PROFILE_COUNT_REGISTER "\n",
26375                labelno);
26376 #endif
26377       fputs ("\tcall\t" MCOUNT_NAME "\n", file);
26378     }
26379 }
26380
26381 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
26382 /* We don't have exact information about the insn sizes, but we may assume
26383    quite safely that we are informed about all 1 byte insns and memory
26384    address sizes.  This is enough to eliminate unnecessary padding in
26385    99% of cases.  */
26386
26387 static int
26388 min_insn_size (rtx insn)
26389 {
26390   int l = 0, len;
26391
26392   if (!INSN_P (insn) || !active_insn_p (insn))
26393     return 0;
26394
26395   /* Discard alignments we've emit and jump instructions.  */
26396   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
26397       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
26398     return 0;
26399   if (JUMP_TABLE_DATA_P (insn))
26400     return 0;
26401
26402   /* Important case - calls are always 5 bytes.
26403      It is common to have many calls in the row.  */
26404   if (CALL_P (insn)
26405       && symbolic_reference_mentioned_p (PATTERN (insn))
26406       && !SIBLING_CALL_P (insn))
26407     return 5;
26408   len = get_attr_length (insn);
26409   if (len <= 1)
26410     return 1;
26411
26412   /* For normal instructions we rely on get_attr_length being exact,
26413      with a few exceptions.  */
26414   if (!JUMP_P (insn))
26415     {
26416       enum attr_type type = get_attr_type (insn);
26417
26418       switch (type)
26419         {
26420         case TYPE_MULTI:
26421           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
26422               || asm_noperands (PATTERN (insn)) >= 0)
26423             return 0;
26424           break;
26425         case TYPE_OTHER:
26426         case TYPE_FCMP:
26427           break;
26428         default:
26429           /* Otherwise trust get_attr_length.  */
26430           return len;
26431         }
26432
26433       l = get_attr_length_address (insn);
26434       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
26435         l = 4;
26436     }
26437   if (l)
26438     return 1+l;
26439   else
26440     return 2;
26441 }
26442
26443 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
26444    window.  */
26445
26446 static void
26447 ix86_avoid_jump_mispredicts (void)
26448 {
26449   rtx insn, start = get_insns ();
26450   int nbytes = 0, njumps = 0;
26451   int isjump = 0;
26452
26453   /* Look for all minimal intervals of instructions containing 4 jumps.
26454      The intervals are bounded by START and INSN.  NBYTES is the total
26455      size of instructions in the interval including INSN and not including
26456      START.  When the NBYTES is smaller than 16 bytes, it is possible
26457      that the end of START and INSN ends up in the same 16byte page.
26458
26459      The smallest offset in the page INSN can start is the case where START
26460      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
26461      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
26462      */
26463   for (insn = start; insn; insn = NEXT_INSN (insn))
26464     {
26465       int min_size;
26466
26467       if (LABEL_P (insn))
26468         {
26469           int align = label_to_alignment (insn);
26470           int max_skip = label_to_max_skip (insn);
26471
26472           if (max_skip > 15)
26473             max_skip = 15;
26474           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
26475              already in the current 16 byte page, because otherwise
26476              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
26477              bytes to reach 16 byte boundary.  */
26478           if (align <= 0
26479               || (align <= 3 && max_skip != (1 << align) - 1))
26480             max_skip = 0;
26481           if (dump_file)
26482             fprintf (dump_file, "Label %i with max_skip %i\n",
26483                      INSN_UID (insn), max_skip);
26484           if (max_skip)
26485             {
26486               while (nbytes + max_skip >= 16)
26487                 {
26488                   start = NEXT_INSN (start);
26489                   if ((JUMP_P (start)
26490                        && GET_CODE (PATTERN (start)) != ADDR_VEC
26491                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
26492                       || CALL_P (start))
26493                     njumps--, isjump = 1;
26494                   else
26495                     isjump = 0;
26496                   nbytes -= min_insn_size (start);
26497                 }
26498             }
26499           continue;
26500         }
26501
26502       min_size = min_insn_size (insn);
26503       nbytes += min_size;
26504       if (dump_file)
26505         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
26506                  INSN_UID (insn), min_size);
26507       if ((JUMP_P (insn)
26508            && GET_CODE (PATTERN (insn)) != ADDR_VEC
26509            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
26510           || CALL_P (insn))
26511         njumps++;
26512       else
26513         continue;
26514
26515       while (njumps > 3)
26516         {
26517           start = NEXT_INSN (start);
26518           if ((JUMP_P (start)
26519                && GET_CODE (PATTERN (start)) != ADDR_VEC
26520                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
26521               || CALL_P (start))
26522             njumps--, isjump = 1;
26523           else
26524             isjump = 0;
26525           nbytes -= min_insn_size (start);
26526         }
26527       gcc_assert (njumps >= 0);
26528       if (dump_file)
26529         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
26530                  INSN_UID (start), INSN_UID (insn), nbytes);
26531
26532       if (njumps == 3 && isjump && nbytes < 16)
26533         {
26534           int padsize = 15 - nbytes + min_insn_size (insn);
26535
26536           if (dump_file)
26537             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
26538                      INSN_UID (insn), padsize);
26539           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
26540         }
26541     }
26542 }
26543 #endif
26544
26545 /* AMD Athlon works faster
26546    when RET is not destination of conditional jump or directly preceded
26547    by other jump instruction.  We avoid the penalty by inserting NOP just
26548    before the RET instructions in such cases.  */
26549 static void
26550 ix86_pad_returns (void)
26551 {
26552   edge e;
26553   edge_iterator ei;
26554
26555   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
26556     {
26557       basic_block bb = e->src;
26558       rtx ret = BB_END (bb);
26559       rtx prev;
26560       bool replace = false;
26561
26562       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
26563           || optimize_bb_for_size_p (bb))
26564         continue;
26565       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
26566         if (active_insn_p (prev) || LABEL_P (prev))
26567           break;
26568       if (prev && LABEL_P (prev))
26569         {
26570           edge e;
26571           edge_iterator ei;
26572
26573           FOR_EACH_EDGE (e, ei, bb->preds)
26574             if (EDGE_FREQUENCY (e) && e->src->index >= 0
26575                 && !(e->flags & EDGE_FALLTHRU))
26576               replace = true;
26577         }
26578       if (!replace)
26579         {
26580           prev = prev_active_insn (ret);
26581           if (prev
26582               && ((JUMP_P (prev) && any_condjump_p (prev))
26583                   || CALL_P (prev)))
26584             replace = true;
26585           /* Empty functions get branch mispredict even when the jump destination
26586              is not visible to us.  */
26587           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
26588             replace = true;
26589         }
26590       if (replace)
26591         {
26592           emit_jump_insn_before (gen_return_internal_long (), ret);
26593           delete_insn (ret);
26594         }
26595     }
26596 }
26597
26598 /* Implement machine specific optimizations.  We implement padding of returns
26599    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
26600 static void
26601 ix86_reorg (void)
26602 {
26603   if (optimize && optimize_function_for_speed_p (cfun))
26604     {
26605       if (TARGET_PAD_RETURNS)
26606         ix86_pad_returns ();
26607 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
26608       if (TARGET_FOUR_JUMP_LIMIT)
26609         ix86_avoid_jump_mispredicts ();
26610 #endif
26611     }
26612 }
26613
26614 /* Return nonzero when QImode register that must be represented via REX prefix
26615    is used.  */
26616 bool
26617 x86_extended_QIreg_mentioned_p (rtx insn)
26618 {
26619   int i;
26620   extract_insn_cached (insn);
26621   for (i = 0; i < recog_data.n_operands; i++)
26622     if (REG_P (recog_data.operand[i])
26623         && REGNO (recog_data.operand[i]) > BX_REG)
26624        return true;
26625   return false;
26626 }
26627
26628 /* Return nonzero when P points to register encoded via REX prefix.
26629    Called via for_each_rtx.  */
26630 static int
26631 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
26632 {
26633    unsigned int regno;
26634    if (!REG_P (*p))
26635      return 0;
26636    regno = REGNO (*p);
26637    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
26638 }
26639
26640 /* Return true when INSN mentions register that must be encoded using REX
26641    prefix.  */
26642 bool
26643 x86_extended_reg_mentioned_p (rtx insn)
26644 {
26645   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
26646                        extended_reg_mentioned_1, NULL);
26647 }
26648
26649 /* If profitable, negate (without causing overflow) integer constant
26650    of mode MODE at location LOC.  Return true in this case.  */
26651 bool
26652 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
26653 {
26654   HOST_WIDE_INT val;
26655
26656   if (!CONST_INT_P (*loc))
26657     return false;
26658
26659   switch (mode)
26660     {
26661     case DImode:
26662       /* DImode x86_64 constants must fit in 32 bits.  */
26663       gcc_assert (x86_64_immediate_operand (*loc, mode));
26664
26665       mode = SImode;
26666       break;
26667
26668     case SImode:
26669     case HImode:
26670     case QImode:
26671       break;
26672
26673     default:
26674       gcc_unreachable ();
26675     }
26676
26677   /* Avoid overflows.  */
26678   if (mode_signbit_p (mode, *loc))
26679     return false;
26680
26681   val = INTVAL (*loc);
26682
26683   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
26684      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
26685   if ((val < 0 && val != -128)
26686       || val == 128)
26687     {
26688       *loc = GEN_INT (-val);
26689       return true;
26690     }
26691
26692   return false;
26693 }
26694
26695 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
26696    optabs would emit if we didn't have TFmode patterns.  */
26697
26698 void
26699 x86_emit_floatuns (rtx operands[2])
26700 {
26701   rtx neglab, donelab, i0, i1, f0, in, out;
26702   enum machine_mode mode, inmode;
26703
26704   inmode = GET_MODE (operands[1]);
26705   gcc_assert (inmode == SImode || inmode == DImode);
26706
26707   out = operands[0];
26708   in = force_reg (inmode, operands[1]);
26709   mode = GET_MODE (out);
26710   neglab = gen_label_rtx ();
26711   donelab = gen_label_rtx ();
26712   f0 = gen_reg_rtx (mode);
26713
26714   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
26715
26716   expand_float (out, in, 0);
26717
26718   emit_jump_insn (gen_jump (donelab));
26719   emit_barrier ();
26720
26721   emit_label (neglab);
26722
26723   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
26724                             1, OPTAB_DIRECT);
26725   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
26726                             1, OPTAB_DIRECT);
26727   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
26728
26729   expand_float (f0, i0, 0);
26730
26731   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
26732
26733   emit_label (donelab);
26734 }
26735 \f
26736 /* AVX does not support 32-byte integer vector operations,
26737    thus the longest vector we are faced with is V16QImode.  */
26738 #define MAX_VECT_LEN    16
26739
26740 struct expand_vec_perm_d
26741 {
26742   rtx target, op0, op1;
26743   unsigned char perm[MAX_VECT_LEN];
26744   enum machine_mode vmode;
26745   unsigned char nelt;
26746   bool testing_p;
26747 };
26748
26749 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
26750 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
26751
26752 /* Get a vector mode of the same size as the original but with elements
26753    twice as wide.  This is only guaranteed to apply to integral vectors.  */
26754
26755 static inline enum machine_mode
26756 get_mode_wider_vector (enum machine_mode o)
26757 {
26758   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
26759   enum machine_mode n = GET_MODE_WIDER_MODE (o);
26760   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
26761   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
26762   return n;
26763 }
26764
26765 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
26766    with all elements equal to VAR.  Return true if successful.  */
26767
26768 static bool
26769 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
26770                                    rtx target, rtx val)
26771 {
26772   bool ok;
26773
26774   switch (mode)
26775     {
26776     case V2SImode:
26777     case V2SFmode:
26778       if (!mmx_ok)
26779         return false;
26780       /* FALLTHRU */
26781
26782     case V4DFmode:
26783     case V4DImode:
26784     case V8SFmode:
26785     case V8SImode:
26786     case V2DFmode:
26787     case V2DImode:
26788     case V4SFmode:
26789     case V4SImode:
26790       {
26791         rtx insn, dup;
26792
26793         /* First attempt to recognize VAL as-is.  */
26794         dup = gen_rtx_VEC_DUPLICATE (mode, val);
26795         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
26796         if (recog_memoized (insn) < 0)
26797           {
26798             rtx seq;
26799             /* If that fails, force VAL into a register.  */
26800
26801             start_sequence ();
26802             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
26803             seq = get_insns ();
26804             end_sequence ();
26805             if (seq)
26806               emit_insn_before (seq, insn);
26807
26808             ok = recog_memoized (insn) >= 0;
26809             gcc_assert (ok);
26810           }
26811       }
26812       return true;
26813
26814     case V4HImode:
26815       if (!mmx_ok)
26816         return false;
26817       if (TARGET_SSE || TARGET_3DNOW_A)
26818         {
26819           rtx x;
26820
26821           val = gen_lowpart (SImode, val);
26822           x = gen_rtx_TRUNCATE (HImode, val);
26823           x = gen_rtx_VEC_DUPLICATE (mode, x);
26824           emit_insn (gen_rtx_SET (VOIDmode, target, x));
26825           return true;
26826         }
26827       goto widen;
26828
26829     case V8QImode:
26830       if (!mmx_ok)
26831         return false;
26832       goto widen;
26833
26834     case V8HImode:
26835       if (TARGET_SSE2)
26836         {
26837           struct expand_vec_perm_d dperm;
26838           rtx tmp1, tmp2;
26839
26840         permute:
26841           memset (&dperm, 0, sizeof (dperm));
26842           dperm.target = target;
26843           dperm.vmode = mode;
26844           dperm.nelt = GET_MODE_NUNITS (mode);
26845           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
26846
26847           /* Extend to SImode using a paradoxical SUBREG.  */
26848           tmp1 = gen_reg_rtx (SImode);
26849           emit_move_insn (tmp1, gen_lowpart (SImode, val));
26850
26851           /* Insert the SImode value as low element of a V4SImode vector. */
26852           tmp2 = gen_lowpart (V4SImode, dperm.op0);
26853           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
26854
26855           ok = (expand_vec_perm_1 (&dperm)
26856                 || expand_vec_perm_broadcast_1 (&dperm));
26857           gcc_assert (ok);
26858           return ok;
26859         }
26860       goto widen;
26861
26862     case V16QImode:
26863       if (TARGET_SSE2)
26864         goto permute;
26865       goto widen;
26866
26867     widen:
26868       /* Replicate the value once into the next wider mode and recurse.  */
26869       {
26870         enum machine_mode smode, wsmode, wvmode;
26871         rtx x;
26872
26873         smode = GET_MODE_INNER (mode);
26874         wvmode = get_mode_wider_vector (mode);
26875         wsmode = GET_MODE_INNER (wvmode);
26876
26877         val = convert_modes (wsmode, smode, val, true);
26878         x = expand_simple_binop (wsmode, ASHIFT, val,
26879                                  GEN_INT (GET_MODE_BITSIZE (smode)),
26880                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
26881         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
26882
26883         x = gen_lowpart (wvmode, target);
26884         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
26885         gcc_assert (ok);
26886         return ok;
26887       }
26888
26889     case V16HImode:
26890     case V32QImode:
26891       {
26892         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
26893         rtx x = gen_reg_rtx (hvmode);
26894
26895         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
26896         gcc_assert (ok);
26897
26898         x = gen_rtx_VEC_CONCAT (mode, x, x);
26899         emit_insn (gen_rtx_SET (VOIDmode, target, x));
26900       }
26901       return true;
26902
26903     default:
26904       return false;
26905     }
26906 }
26907
26908 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
26909    whose ONE_VAR element is VAR, and other elements are zero.  Return true
26910    if successful.  */
26911
26912 static bool
26913 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
26914                                      rtx target, rtx var, int one_var)
26915 {
26916   enum machine_mode vsimode;
26917   rtx new_target;
26918   rtx x, tmp;
26919   bool use_vector_set = false;
26920
26921   switch (mode)
26922     {
26923     case V2DImode:
26924       /* For SSE4.1, we normally use vector set.  But if the second
26925          element is zero and inter-unit moves are OK, we use movq
26926          instead.  */
26927       use_vector_set = (TARGET_64BIT
26928                         && TARGET_SSE4_1
26929                         && !(TARGET_INTER_UNIT_MOVES
26930                              && one_var == 0));
26931       break;
26932     case V16QImode:
26933     case V4SImode:
26934     case V4SFmode:
26935       use_vector_set = TARGET_SSE4_1;
26936       break;
26937     case V8HImode:
26938       use_vector_set = TARGET_SSE2;
26939       break;
26940     case V4HImode:
26941       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
26942       break;
26943     case V32QImode:
26944     case V16HImode:
26945     case V8SImode:
26946     case V8SFmode:
26947     case V4DFmode:
26948       use_vector_set = TARGET_AVX;
26949       break;
26950     case V4DImode:
26951       /* Use ix86_expand_vector_set in 64bit mode only.  */
26952       use_vector_set = TARGET_AVX && TARGET_64BIT;
26953       break;
26954     default:
26955       break;
26956     }
26957
26958   if (use_vector_set)
26959     {
26960       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
26961       var = force_reg (GET_MODE_INNER (mode), var);
26962       ix86_expand_vector_set (mmx_ok, target, var, one_var);
26963       return true; 
26964     }
26965
26966   switch (mode)
26967     {
26968     case V2SFmode:
26969     case V2SImode:
26970       if (!mmx_ok)
26971         return false;
26972       /* FALLTHRU */
26973
26974     case V2DFmode:
26975     case V2DImode:
26976       if (one_var != 0)
26977         return false;
26978       var = force_reg (GET_MODE_INNER (mode), var);
26979       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
26980       emit_insn (gen_rtx_SET (VOIDmode, target, x));
26981       return true;
26982
26983     case V4SFmode:
26984     case V4SImode:
26985       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
26986         new_target = gen_reg_rtx (mode);
26987       else
26988         new_target = target;
26989       var = force_reg (GET_MODE_INNER (mode), var);
26990       x = gen_rtx_VEC_DUPLICATE (mode, var);
26991       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
26992       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
26993       if (one_var != 0)
26994         {
26995           /* We need to shuffle the value to the correct position, so
26996              create a new pseudo to store the intermediate result.  */
26997
26998           /* With SSE2, we can use the integer shuffle insns.  */
26999           if (mode != V4SFmode && TARGET_SSE2)
27000             {
27001               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
27002                                             const1_rtx,
27003                                             GEN_INT (one_var == 1 ? 0 : 1),
27004                                             GEN_INT (one_var == 2 ? 0 : 1),
27005                                             GEN_INT (one_var == 3 ? 0 : 1)));
27006               if (target != new_target)
27007                 emit_move_insn (target, new_target);
27008               return true;
27009             }
27010
27011           /* Otherwise convert the intermediate result to V4SFmode and
27012              use the SSE1 shuffle instructions.  */
27013           if (mode != V4SFmode)
27014             {
27015               tmp = gen_reg_rtx (V4SFmode);
27016               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
27017             }
27018           else
27019             tmp = new_target;
27020
27021           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
27022                                        const1_rtx,
27023                                        GEN_INT (one_var == 1 ? 0 : 1),
27024                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
27025                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
27026
27027           if (mode != V4SFmode)
27028             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
27029           else if (tmp != target)
27030             emit_move_insn (target, tmp);
27031         }
27032       else if (target != new_target)
27033         emit_move_insn (target, new_target);
27034       return true;
27035
27036     case V8HImode:
27037     case V16QImode:
27038       vsimode = V4SImode;
27039       goto widen;
27040     case V4HImode:
27041     case V8QImode:
27042       if (!mmx_ok)
27043         return false;
27044       vsimode = V2SImode;
27045       goto widen;
27046     widen:
27047       if (one_var != 0)
27048         return false;
27049
27050       /* Zero extend the variable element to SImode and recurse.  */
27051       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
27052
27053       x = gen_reg_rtx (vsimode);
27054       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
27055                                                 var, one_var))
27056         gcc_unreachable ();
27057
27058       emit_move_insn (target, gen_lowpart (mode, x));
27059       return true;
27060
27061     default:
27062       return false;
27063     }
27064 }
27065
27066 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
27067    consisting of the values in VALS.  It is known that all elements
27068    except ONE_VAR are constants.  Return true if successful.  */
27069
27070 static bool
27071 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
27072                                  rtx target, rtx vals, int one_var)
27073 {
27074   rtx var = XVECEXP (vals, 0, one_var);
27075   enum machine_mode wmode;
27076   rtx const_vec, x;
27077
27078   const_vec = copy_rtx (vals);
27079   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
27080   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
27081
27082   switch (mode)
27083     {
27084     case V2DFmode:
27085     case V2DImode:
27086     case V2SFmode:
27087     case V2SImode:
27088       /* For the two element vectors, it's just as easy to use
27089          the general case.  */
27090       return false;
27091
27092     case V4DImode:
27093       /* Use ix86_expand_vector_set in 64bit mode only.  */
27094       if (!TARGET_64BIT)
27095         return false;
27096     case V4DFmode:
27097     case V8SFmode:
27098     case V8SImode:
27099     case V16HImode:
27100     case V32QImode:
27101     case V4SFmode:
27102     case V4SImode:
27103     case V8HImode:
27104     case V4HImode:
27105       break;
27106
27107     case V16QImode:
27108       if (TARGET_SSE4_1)
27109         break;
27110       wmode = V8HImode;
27111       goto widen;
27112     case V8QImode:
27113       wmode = V4HImode;
27114       goto widen;
27115     widen:
27116       /* There's no way to set one QImode entry easily.  Combine
27117          the variable value with its adjacent constant value, and
27118          promote to an HImode set.  */
27119       x = XVECEXP (vals, 0, one_var ^ 1);
27120       if (one_var & 1)
27121         {
27122           var = convert_modes (HImode, QImode, var, true);
27123           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
27124                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
27125           x = GEN_INT (INTVAL (x) & 0xff);
27126         }
27127       else
27128         {
27129           var = convert_modes (HImode, QImode, var, true);
27130           x = gen_int_mode (INTVAL (x) << 8, HImode);
27131         }
27132       if (x != const0_rtx)
27133         var = expand_simple_binop (HImode, IOR, var, x, var,
27134                                    1, OPTAB_LIB_WIDEN);
27135
27136       x = gen_reg_rtx (wmode);
27137       emit_move_insn (x, gen_lowpart (wmode, const_vec));
27138       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
27139
27140       emit_move_insn (target, gen_lowpart (mode, x));
27141       return true;
27142
27143     default:
27144       return false;
27145     }
27146
27147   emit_move_insn (target, const_vec);
27148   ix86_expand_vector_set (mmx_ok, target, var, one_var);
27149   return true;
27150 }
27151
27152 /* A subroutine of ix86_expand_vector_init_general.  Use vector
27153    concatenate to handle the most general case: all values variable,
27154    and none identical.  */
27155
27156 static void
27157 ix86_expand_vector_init_concat (enum machine_mode mode,
27158                                 rtx target, rtx *ops, int n)
27159 {
27160   enum machine_mode cmode, hmode = VOIDmode;
27161   rtx first[8], second[4];
27162   rtvec v;
27163   int i, j;
27164
27165   switch (n)
27166     {
27167     case 2:
27168       switch (mode)
27169         {
27170         case V8SImode:
27171           cmode = V4SImode;
27172           break;
27173         case V8SFmode:
27174           cmode = V4SFmode;
27175           break;
27176         case V4DImode:
27177           cmode = V2DImode;
27178           break;
27179         case V4DFmode:
27180           cmode = V2DFmode;
27181           break;
27182         case V4SImode:
27183           cmode = V2SImode;
27184           break;
27185         case V4SFmode:
27186           cmode = V2SFmode;
27187           break;
27188         case V2DImode:
27189           cmode = DImode;
27190           break;
27191         case V2SImode:
27192           cmode = SImode;
27193           break;
27194         case V2DFmode:
27195           cmode = DFmode;
27196           break;
27197         case V2SFmode:
27198           cmode = SFmode;
27199           break;
27200         default:
27201           gcc_unreachable ();
27202         }
27203
27204       if (!register_operand (ops[1], cmode))
27205         ops[1] = force_reg (cmode, ops[1]);
27206       if (!register_operand (ops[0], cmode))
27207         ops[0] = force_reg (cmode, ops[0]);
27208       emit_insn (gen_rtx_SET (VOIDmode, target,
27209                               gen_rtx_VEC_CONCAT (mode, ops[0],
27210                                                   ops[1])));
27211       break;
27212
27213     case 4:
27214       switch (mode)
27215         {
27216         case V4DImode:
27217           cmode = V2DImode;
27218           break;
27219         case V4DFmode:
27220           cmode = V2DFmode;
27221           break;
27222         case V4SImode:
27223           cmode = V2SImode;
27224           break;
27225         case V4SFmode:
27226           cmode = V2SFmode;
27227           break;
27228         default:
27229           gcc_unreachable ();
27230         }
27231       goto half;
27232
27233     case 8:
27234       switch (mode)
27235         {
27236         case V8SImode:
27237           cmode = V2SImode;
27238           hmode = V4SImode;
27239           break;
27240         case V8SFmode:
27241           cmode = V2SFmode;
27242           hmode = V4SFmode;
27243           break;
27244         default:
27245           gcc_unreachable ();
27246         }
27247       goto half;
27248
27249 half:
27250       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
27251       i = n - 1;
27252       j = (n >> 1) - 1;
27253       for (; i > 0; i -= 2, j--)
27254         {
27255           first[j] = gen_reg_rtx (cmode);
27256           v = gen_rtvec (2, ops[i - 1], ops[i]);
27257           ix86_expand_vector_init (false, first[j],
27258                                    gen_rtx_PARALLEL (cmode, v));
27259         }
27260
27261       n >>= 1;
27262       if (n > 2)
27263         {
27264           gcc_assert (hmode != VOIDmode);
27265           for (i = j = 0; i < n; i += 2, j++)
27266             {
27267               second[j] = gen_reg_rtx (hmode);
27268               ix86_expand_vector_init_concat (hmode, second [j],
27269                                               &first [i], 2);
27270             }
27271           n >>= 1;
27272           ix86_expand_vector_init_concat (mode, target, second, n);
27273         }
27274       else
27275         ix86_expand_vector_init_concat (mode, target, first, n);
27276       break;
27277
27278     default:
27279       gcc_unreachable ();
27280     }
27281 }
27282
27283 /* A subroutine of ix86_expand_vector_init_general.  Use vector
27284    interleave to handle the most general case: all values variable,
27285    and none identical.  */
27286
27287 static void
27288 ix86_expand_vector_init_interleave (enum machine_mode mode,
27289                                     rtx target, rtx *ops, int n)
27290 {
27291   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
27292   int i, j;
27293   rtx op0, op1;
27294   rtx (*gen_load_even) (rtx, rtx, rtx);
27295   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
27296   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
27297   
27298   switch (mode)
27299     {
27300     case V8HImode:
27301       gen_load_even = gen_vec_setv8hi;
27302       gen_interleave_first_low = gen_vec_interleave_lowv4si;
27303       gen_interleave_second_low = gen_vec_interleave_lowv2di;
27304       inner_mode = HImode;
27305       first_imode = V4SImode;
27306       second_imode = V2DImode;
27307       third_imode = VOIDmode;
27308       break;
27309     case V16QImode:
27310       gen_load_even = gen_vec_setv16qi;
27311       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
27312       gen_interleave_second_low = gen_vec_interleave_lowv4si;
27313       inner_mode = QImode;
27314       first_imode = V8HImode;
27315       second_imode = V4SImode;
27316       third_imode = V2DImode;
27317       break;
27318     default:
27319       gcc_unreachable ();
27320     }
27321      
27322   for (i = 0; i < n; i++)
27323     {
27324       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
27325       op0 = gen_reg_rtx (SImode);
27326       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
27327
27328       /* Insert the SImode value as low element of V4SImode vector. */
27329       op1 = gen_reg_rtx (V4SImode);
27330       op0 = gen_rtx_VEC_MERGE (V4SImode,
27331                                gen_rtx_VEC_DUPLICATE (V4SImode,
27332                                                       op0),
27333                                CONST0_RTX (V4SImode),
27334                                const1_rtx);
27335       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
27336
27337       /* Cast the V4SImode vector back to a vector in orignal mode.  */
27338       op0 = gen_reg_rtx (mode);
27339       emit_move_insn (op0, gen_lowpart (mode, op1));
27340       
27341       /* Load even elements into the second positon.  */
27342       emit_insn ((*gen_load_even) (op0,
27343                                    force_reg (inner_mode,
27344                                               ops [i + i + 1]),
27345                                    const1_rtx));
27346
27347       /* Cast vector to FIRST_IMODE vector.  */
27348       ops[i] = gen_reg_rtx (first_imode);
27349       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
27350     }
27351
27352   /* Interleave low FIRST_IMODE vectors.  */
27353   for (i = j = 0; i < n; i += 2, j++)
27354     {
27355       op0 = gen_reg_rtx (first_imode);
27356       emit_insn ((*gen_interleave_first_low) (op0, ops[i], ops[i + 1]));
27357
27358       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
27359       ops[j] = gen_reg_rtx (second_imode);
27360       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
27361     }
27362
27363   /* Interleave low SECOND_IMODE vectors.  */
27364   switch (second_imode)
27365     {
27366     case V4SImode:
27367       for (i = j = 0; i < n / 2; i += 2, j++)
27368         {
27369           op0 = gen_reg_rtx (second_imode);
27370           emit_insn ((*gen_interleave_second_low) (op0, ops[i],
27371                                                    ops[i + 1]));
27372
27373           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
27374              vector.  */
27375           ops[j] = gen_reg_rtx (third_imode);
27376           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
27377         }
27378       second_imode = V2DImode;
27379       gen_interleave_second_low = gen_vec_interleave_lowv2di;
27380       /* FALLTHRU */
27381
27382     case V2DImode:
27383       op0 = gen_reg_rtx (second_imode);
27384       emit_insn ((*gen_interleave_second_low) (op0, ops[0],
27385                                                ops[1]));
27386
27387       /* Cast the SECOND_IMODE vector back to a vector on original
27388          mode.  */
27389       emit_insn (gen_rtx_SET (VOIDmode, target,
27390                               gen_lowpart (mode, op0)));
27391       break;
27392
27393     default:
27394       gcc_unreachable ();
27395     }
27396 }
27397
27398 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
27399    all values variable, and none identical.  */
27400
27401 static void
27402 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
27403                                  rtx target, rtx vals)
27404 {
27405   rtx ops[32], op0, op1;
27406   enum machine_mode half_mode = VOIDmode;
27407   int n, i;
27408
27409   switch (mode)
27410     {
27411     case V2SFmode:
27412     case V2SImode:
27413       if (!mmx_ok && !TARGET_SSE)
27414         break;
27415       /* FALLTHRU */
27416
27417     case V8SFmode:
27418     case V8SImode:
27419     case V4DFmode:
27420     case V4DImode:
27421     case V4SFmode:
27422     case V4SImode:
27423     case V2DFmode:
27424     case V2DImode:
27425       n = GET_MODE_NUNITS (mode);
27426       for (i = 0; i < n; i++)
27427         ops[i] = XVECEXP (vals, 0, i);
27428       ix86_expand_vector_init_concat (mode, target, ops, n);
27429       return;
27430
27431     case V32QImode:
27432       half_mode = V16QImode;
27433       goto half;
27434
27435     case V16HImode:
27436       half_mode = V8HImode;
27437       goto half;
27438
27439 half:
27440       n = GET_MODE_NUNITS (mode);
27441       for (i = 0; i < n; i++)
27442         ops[i] = XVECEXP (vals, 0, i);
27443       op0 = gen_reg_rtx (half_mode);
27444       op1 = gen_reg_rtx (half_mode);
27445       ix86_expand_vector_init_interleave (half_mode, op0, ops,
27446                                           n >> 2);
27447       ix86_expand_vector_init_interleave (half_mode, op1,
27448                                           &ops [n >> 1], n >> 2);
27449       emit_insn (gen_rtx_SET (VOIDmode, target,
27450                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
27451       return;
27452
27453     case V16QImode:
27454       if (!TARGET_SSE4_1)
27455         break;
27456       /* FALLTHRU */
27457
27458     case V8HImode:
27459       if (!TARGET_SSE2)
27460         break;
27461
27462       /* Don't use ix86_expand_vector_init_interleave if we can't
27463          move from GPR to SSE register directly.  */ 
27464       if (!TARGET_INTER_UNIT_MOVES)
27465         break;
27466
27467       n = GET_MODE_NUNITS (mode);
27468       for (i = 0; i < n; i++)
27469         ops[i] = XVECEXP (vals, 0, i);
27470       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
27471       return;
27472
27473     case V4HImode:
27474     case V8QImode:
27475       break;
27476
27477     default:
27478       gcc_unreachable ();
27479     }
27480
27481     {
27482       int i, j, n_elts, n_words, n_elt_per_word;
27483       enum machine_mode inner_mode;
27484       rtx words[4], shift;
27485
27486       inner_mode = GET_MODE_INNER (mode);
27487       n_elts = GET_MODE_NUNITS (mode);
27488       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
27489       n_elt_per_word = n_elts / n_words;
27490       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
27491
27492       for (i = 0; i < n_words; ++i)
27493         {
27494           rtx word = NULL_RTX;
27495
27496           for (j = 0; j < n_elt_per_word; ++j)
27497             {
27498               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
27499               elt = convert_modes (word_mode, inner_mode, elt, true);
27500
27501               if (j == 0)
27502                 word = elt;
27503               else
27504                 {
27505                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
27506                                               word, 1, OPTAB_LIB_WIDEN);
27507                   word = expand_simple_binop (word_mode, IOR, word, elt,
27508                                               word, 1, OPTAB_LIB_WIDEN);
27509                 }
27510             }
27511
27512           words[i] = word;
27513         }
27514
27515       if (n_words == 1)
27516         emit_move_insn (target, gen_lowpart (mode, words[0]));
27517       else if (n_words == 2)
27518         {
27519           rtx tmp = gen_reg_rtx (mode);
27520           emit_clobber (tmp);
27521           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
27522           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
27523           emit_move_insn (target, tmp);
27524         }
27525       else if (n_words == 4)
27526         {
27527           rtx tmp = gen_reg_rtx (V4SImode);
27528           gcc_assert (word_mode == SImode);
27529           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
27530           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
27531           emit_move_insn (target, gen_lowpart (mode, tmp));
27532         }
27533       else
27534         gcc_unreachable ();
27535     }
27536 }
27537
27538 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
27539    instructions unless MMX_OK is true.  */
27540
27541 void
27542 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
27543 {
27544   enum machine_mode mode = GET_MODE (target);
27545   enum machine_mode inner_mode = GET_MODE_INNER (mode);
27546   int n_elts = GET_MODE_NUNITS (mode);
27547   int n_var = 0, one_var = -1;
27548   bool all_same = true, all_const_zero = true;
27549   int i;
27550   rtx x;
27551
27552   for (i = 0; i < n_elts; ++i)
27553     {
27554       x = XVECEXP (vals, 0, i);
27555       if (!(CONST_INT_P (x)
27556             || GET_CODE (x) == CONST_DOUBLE
27557             || GET_CODE (x) == CONST_FIXED))
27558         n_var++, one_var = i;
27559       else if (x != CONST0_RTX (inner_mode))
27560         all_const_zero = false;
27561       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
27562         all_same = false;
27563     }
27564
27565   /* Constants are best loaded from the constant pool.  */
27566   if (n_var == 0)
27567     {
27568       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
27569       return;
27570     }
27571
27572   /* If all values are identical, broadcast the value.  */
27573   if (all_same
27574       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
27575                                             XVECEXP (vals, 0, 0)))
27576     return;
27577
27578   /* Values where only one field is non-constant are best loaded from
27579      the pool and overwritten via move later.  */
27580   if (n_var == 1)
27581     {
27582       if (all_const_zero
27583           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
27584                                                   XVECEXP (vals, 0, one_var),
27585                                                   one_var))
27586         return;
27587
27588       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
27589         return;
27590     }
27591
27592   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
27593 }
27594
27595 void
27596 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
27597 {
27598   enum machine_mode mode = GET_MODE (target);
27599   enum machine_mode inner_mode = GET_MODE_INNER (mode);
27600   enum machine_mode half_mode;
27601   bool use_vec_merge = false;
27602   rtx tmp;
27603   static rtx (*gen_extract[6][2]) (rtx, rtx)
27604     = {
27605         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
27606         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
27607         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
27608         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
27609         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
27610         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
27611       };
27612   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
27613     = {
27614         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
27615         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
27616         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
27617         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
27618         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
27619         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
27620       };
27621   int i, j, n;
27622
27623   switch (mode)
27624     {
27625     case V2SFmode:
27626     case V2SImode:
27627       if (mmx_ok)
27628         {
27629           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
27630           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
27631           if (elt == 0)
27632             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
27633           else
27634             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
27635           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
27636           return;
27637         }
27638       break;
27639
27640     case V2DImode:
27641       use_vec_merge = TARGET_SSE4_1;
27642       if (use_vec_merge)
27643         break;
27644
27645     case V2DFmode:
27646       {
27647         rtx op0, op1;
27648
27649         /* For the two element vectors, we implement a VEC_CONCAT with
27650            the extraction of the other element.  */
27651
27652         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
27653         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
27654
27655         if (elt == 0)
27656           op0 = val, op1 = tmp;
27657         else
27658           op0 = tmp, op1 = val;
27659
27660         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
27661         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
27662       }
27663       return;
27664
27665     case V4SFmode:
27666       use_vec_merge = TARGET_SSE4_1;
27667       if (use_vec_merge)
27668         break;
27669
27670       switch (elt)
27671         {
27672         case 0:
27673           use_vec_merge = true;
27674           break;
27675
27676         case 1:
27677           /* tmp = target = A B C D */
27678           tmp = copy_to_reg (target);
27679           /* target = A A B B */
27680           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
27681           /* target = X A B B */
27682           ix86_expand_vector_set (false, target, val, 0);
27683           /* target = A X C D  */
27684           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
27685                                           const1_rtx, const0_rtx,
27686                                           GEN_INT (2+4), GEN_INT (3+4)));
27687           return;
27688
27689         case 2:
27690           /* tmp = target = A B C D */
27691           tmp = copy_to_reg (target);
27692           /* tmp = X B C D */
27693           ix86_expand_vector_set (false, tmp, val, 0);
27694           /* target = A B X D */
27695           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
27696                                           const0_rtx, const1_rtx,
27697                                           GEN_INT (0+4), GEN_INT (3+4)));
27698           return;
27699
27700         case 3:
27701           /* tmp = target = A B C D */
27702           tmp = copy_to_reg (target);
27703           /* tmp = X B C D */
27704           ix86_expand_vector_set (false, tmp, val, 0);
27705           /* target = A B X D */
27706           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
27707                                           const0_rtx, const1_rtx,
27708                                           GEN_INT (2+4), GEN_INT (0+4)));
27709           return;
27710
27711         default:
27712           gcc_unreachable ();
27713         }
27714       break;
27715
27716     case V4SImode:
27717       use_vec_merge = TARGET_SSE4_1;
27718       if (use_vec_merge)
27719         break;
27720
27721       /* Element 0 handled by vec_merge below.  */
27722       if (elt == 0)
27723         {
27724           use_vec_merge = true;
27725           break;
27726         }
27727
27728       if (TARGET_SSE2)
27729         {
27730           /* With SSE2, use integer shuffles to swap element 0 and ELT,
27731              store into element 0, then shuffle them back.  */
27732
27733           rtx order[4];
27734
27735           order[0] = GEN_INT (elt);
27736           order[1] = const1_rtx;
27737           order[2] = const2_rtx;
27738           order[3] = GEN_INT (3);
27739           order[elt] = const0_rtx;
27740
27741           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
27742                                         order[1], order[2], order[3]));
27743
27744           ix86_expand_vector_set (false, target, val, 0);
27745
27746           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
27747                                         order[1], order[2], order[3]));
27748         }
27749       else
27750         {
27751           /* For SSE1, we have to reuse the V4SF code.  */
27752           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
27753                                   gen_lowpart (SFmode, val), elt);
27754         }
27755       return;
27756
27757     case V8HImode:
27758       use_vec_merge = TARGET_SSE2;
27759       break;
27760     case V4HImode:
27761       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
27762       break;
27763
27764     case V16QImode:
27765       use_vec_merge = TARGET_SSE4_1;
27766       break;
27767
27768     case V8QImode:
27769       break;
27770
27771     case V32QImode:
27772       half_mode = V16QImode;
27773       j = 0;
27774       n = 16;
27775       goto half;
27776
27777     case V16HImode:
27778       half_mode = V8HImode;
27779       j = 1;
27780       n = 8;
27781       goto half;
27782
27783     case V8SImode:
27784       half_mode = V4SImode;
27785       j = 2;
27786       n = 4;
27787       goto half;
27788
27789     case V4DImode:
27790       half_mode = V2DImode;
27791       j = 3;
27792       n = 2;
27793       goto half;
27794
27795     case V8SFmode:
27796       half_mode = V4SFmode;
27797       j = 4;
27798       n = 4;
27799       goto half;
27800
27801     case V4DFmode:
27802       half_mode = V2DFmode;
27803       j = 5;
27804       n = 2;
27805       goto half;
27806
27807 half:
27808       /* Compute offset.  */
27809       i = elt / n;
27810       elt %= n;
27811
27812       gcc_assert (i <= 1);
27813
27814       /* Extract the half.  */
27815       tmp = gen_reg_rtx (half_mode);
27816       emit_insn ((*gen_extract[j][i]) (tmp, target));
27817
27818       /* Put val in tmp at elt.  */
27819       ix86_expand_vector_set (false, tmp, val, elt);
27820
27821       /* Put it back.  */
27822       emit_insn ((*gen_insert[j][i]) (target, target, tmp));
27823       return;
27824
27825     default:
27826       break;
27827     }
27828
27829   if (use_vec_merge)
27830     {
27831       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
27832       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
27833       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
27834     }
27835   else
27836     {
27837       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
27838
27839       emit_move_insn (mem, target);
27840
27841       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
27842       emit_move_insn (tmp, val);
27843
27844       emit_move_insn (target, mem);
27845     }
27846 }
27847
27848 void
27849 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
27850 {
27851   enum machine_mode mode = GET_MODE (vec);
27852   enum machine_mode inner_mode = GET_MODE_INNER (mode);
27853   bool use_vec_extr = false;
27854   rtx tmp;
27855
27856   switch (mode)
27857     {
27858     case V2SImode:
27859     case V2SFmode:
27860       if (!mmx_ok)
27861         break;
27862       /* FALLTHRU */
27863
27864     case V2DFmode:
27865     case V2DImode:
27866       use_vec_extr = true;
27867       break;
27868
27869     case V4SFmode:
27870       use_vec_extr = TARGET_SSE4_1;
27871       if (use_vec_extr)
27872         break;
27873
27874       switch (elt)
27875         {
27876         case 0:
27877           tmp = vec;
27878           break;
27879
27880         case 1:
27881         case 3:
27882           tmp = gen_reg_rtx (mode);
27883           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
27884                                        GEN_INT (elt), GEN_INT (elt),
27885                                        GEN_INT (elt+4), GEN_INT (elt+4)));
27886           break;
27887
27888         case 2:
27889           tmp = gen_reg_rtx (mode);
27890           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
27891           break;
27892
27893         default:
27894           gcc_unreachable ();
27895         }
27896       vec = tmp;
27897       use_vec_extr = true;
27898       elt = 0;
27899       break;
27900
27901     case V4SImode:
27902       use_vec_extr = TARGET_SSE4_1;
27903       if (use_vec_extr)
27904         break;
27905
27906       if (TARGET_SSE2)
27907         {
27908           switch (elt)
27909             {
27910             case 0:
27911               tmp = vec;
27912               break;
27913
27914             case 1:
27915             case 3:
27916               tmp = gen_reg_rtx (mode);
27917               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
27918                                             GEN_INT (elt), GEN_INT (elt),
27919                                             GEN_INT (elt), GEN_INT (elt)));
27920               break;
27921
27922             case 2:
27923               tmp = gen_reg_rtx (mode);
27924               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
27925               break;
27926
27927             default:
27928               gcc_unreachable ();
27929             }
27930           vec = tmp;
27931           use_vec_extr = true;
27932           elt = 0;
27933         }
27934       else
27935         {
27936           /* For SSE1, we have to reuse the V4SF code.  */
27937           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
27938                                       gen_lowpart (V4SFmode, vec), elt);
27939           return;
27940         }
27941       break;
27942
27943     case V8HImode:
27944       use_vec_extr = TARGET_SSE2;
27945       break;
27946     case V4HImode:
27947       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
27948       break;
27949
27950     case V16QImode:
27951       use_vec_extr = TARGET_SSE4_1;
27952       break;
27953
27954     case V8QImode:
27955       /* ??? Could extract the appropriate HImode element and shift.  */
27956     default:
27957       break;
27958     }
27959
27960   if (use_vec_extr)
27961     {
27962       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
27963       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
27964
27965       /* Let the rtl optimizers know about the zero extension performed.  */
27966       if (inner_mode == QImode || inner_mode == HImode)
27967         {
27968           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
27969           target = gen_lowpart (SImode, target);
27970         }
27971
27972       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
27973     }
27974   else
27975     {
27976       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
27977
27978       emit_move_insn (mem, vec);
27979
27980       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
27981       emit_move_insn (target, tmp);
27982     }
27983 }
27984
27985 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
27986    pattern to reduce; DEST is the destination; IN is the input vector.  */
27987
27988 void
27989 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
27990 {
27991   rtx tmp1, tmp2, tmp3;
27992
27993   tmp1 = gen_reg_rtx (V4SFmode);
27994   tmp2 = gen_reg_rtx (V4SFmode);
27995   tmp3 = gen_reg_rtx (V4SFmode);
27996
27997   emit_insn (gen_sse_movhlps (tmp1, in, in));
27998   emit_insn (fn (tmp2, tmp1, in));
27999
28000   emit_insn (gen_sse_shufps_v4sf (tmp3, tmp2, tmp2,
28001                                   const1_rtx, const1_rtx,
28002                                   GEN_INT (1+4), GEN_INT (1+4)));
28003   emit_insn (fn (dest, tmp2, tmp3));
28004 }
28005 \f
28006 /* Target hook for scalar_mode_supported_p.  */
28007 static bool
28008 ix86_scalar_mode_supported_p (enum machine_mode mode)
28009 {
28010   if (DECIMAL_FLOAT_MODE_P (mode))
28011     return default_decimal_float_supported_p ();
28012   else if (mode == TFmode)
28013     return true;
28014   else
28015     return default_scalar_mode_supported_p (mode);
28016 }
28017
28018 /* Implements target hook vector_mode_supported_p.  */
28019 static bool
28020 ix86_vector_mode_supported_p (enum machine_mode mode)
28021 {
28022   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
28023     return true;
28024   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
28025     return true;
28026   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
28027     return true;
28028   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
28029     return true;
28030   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
28031     return true;
28032   return false;
28033 }
28034
28035 /* Target hook for c_mode_for_suffix.  */
28036 static enum machine_mode
28037 ix86_c_mode_for_suffix (char suffix)
28038 {
28039   if (suffix == 'q')
28040     return TFmode;
28041   if (suffix == 'w')
28042     return XFmode;
28043
28044   return VOIDmode;
28045 }
28046
28047 /* Worker function for TARGET_MD_ASM_CLOBBERS.
28048
28049    We do this in the new i386 backend to maintain source compatibility
28050    with the old cc0-based compiler.  */
28051
28052 static tree
28053 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
28054                       tree inputs ATTRIBUTE_UNUSED,
28055                       tree clobbers)
28056 {
28057   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
28058                         clobbers);
28059   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
28060                         clobbers);
28061   return clobbers;
28062 }
28063
28064 /* Implements target vector targetm.asm.encode_section_info.  This
28065    is not used by netware.  */
28066
28067 static void ATTRIBUTE_UNUSED
28068 ix86_encode_section_info (tree decl, rtx rtl, int first)
28069 {
28070   default_encode_section_info (decl, rtl, first);
28071
28072   if (TREE_CODE (decl) == VAR_DECL
28073       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
28074       && ix86_in_large_data_p (decl))
28075     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
28076 }
28077
28078 /* Worker function for REVERSE_CONDITION.  */
28079
28080 enum rtx_code
28081 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
28082 {
28083   return (mode != CCFPmode && mode != CCFPUmode
28084           ? reverse_condition (code)
28085           : reverse_condition_maybe_unordered (code));
28086 }
28087
28088 /* Output code to perform an x87 FP register move, from OPERANDS[1]
28089    to OPERANDS[0].  */
28090
28091 const char *
28092 output_387_reg_move (rtx insn, rtx *operands)
28093 {
28094   if (REG_P (operands[0]))
28095     {
28096       if (REG_P (operands[1])
28097           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
28098         {
28099           if (REGNO (operands[0]) == FIRST_STACK_REG)
28100             return output_387_ffreep (operands, 0);
28101           return "fstp\t%y0";
28102         }
28103       if (STACK_TOP_P (operands[0]))
28104         return "fld%Z1\t%y1";
28105       return "fst\t%y0";
28106     }
28107   else if (MEM_P (operands[0]))
28108     {
28109       gcc_assert (REG_P (operands[1]));
28110       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
28111         return "fstp%Z0\t%y0";
28112       else
28113         {
28114           /* There is no non-popping store to memory for XFmode.
28115              So if we need one, follow the store with a load.  */
28116           if (GET_MODE (operands[0]) == XFmode)
28117             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
28118           else
28119             return "fst%Z0\t%y0";
28120         }
28121     }
28122   else
28123     gcc_unreachable();
28124 }
28125
28126 /* Output code to perform a conditional jump to LABEL, if C2 flag in
28127    FP status register is set.  */
28128
28129 void
28130 ix86_emit_fp_unordered_jump (rtx label)
28131 {
28132   rtx reg = gen_reg_rtx (HImode);
28133   rtx temp;
28134
28135   emit_insn (gen_x86_fnstsw_1 (reg));
28136
28137   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
28138     {
28139       emit_insn (gen_x86_sahf_1 (reg));
28140
28141       temp = gen_rtx_REG (CCmode, FLAGS_REG);
28142       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
28143     }
28144   else
28145     {
28146       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
28147
28148       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
28149       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
28150     }
28151
28152   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
28153                               gen_rtx_LABEL_REF (VOIDmode, label),
28154                               pc_rtx);
28155   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
28156
28157   emit_jump_insn (temp);
28158   predict_jump (REG_BR_PROB_BASE * 10 / 100);
28159 }
28160
28161 /* Output code to perform a log1p XFmode calculation.  */
28162
28163 void ix86_emit_i387_log1p (rtx op0, rtx op1)
28164 {
28165   rtx label1 = gen_label_rtx ();
28166   rtx label2 = gen_label_rtx ();
28167
28168   rtx tmp = gen_reg_rtx (XFmode);
28169   rtx tmp2 = gen_reg_rtx (XFmode);
28170   rtx test;
28171
28172   emit_insn (gen_absxf2 (tmp, op1));
28173   test = gen_rtx_GE (VOIDmode, tmp,
28174     CONST_DOUBLE_FROM_REAL_VALUE (
28175        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
28176        XFmode));
28177   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
28178
28179   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
28180   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
28181   emit_jump (label2);
28182
28183   emit_label (label1);
28184   emit_move_insn (tmp, CONST1_RTX (XFmode));
28185   emit_insn (gen_addxf3 (tmp, op1, tmp));
28186   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
28187   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
28188
28189   emit_label (label2);
28190 }
28191
28192 /* Output code to perform a Newton-Rhapson approximation of a single precision
28193    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
28194
28195 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
28196 {
28197   rtx x0, x1, e0, e1, two;
28198
28199   x0 = gen_reg_rtx (mode);
28200   e0 = gen_reg_rtx (mode);
28201   e1 = gen_reg_rtx (mode);
28202   x1 = gen_reg_rtx (mode);
28203
28204   two = CONST_DOUBLE_FROM_REAL_VALUE (dconst2, SFmode);
28205
28206   if (VECTOR_MODE_P (mode))
28207     two = ix86_build_const_vector (SFmode, true, two);
28208
28209   two = force_reg (mode, two);
28210
28211   /* a / b = a * rcp(b) * (2.0 - b * rcp(b)) */
28212
28213   /* x0 = rcp(b) estimate */
28214   emit_insn (gen_rtx_SET (VOIDmode, x0,
28215                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
28216                                           UNSPEC_RCP)));
28217   /* e0 = x0 * a */
28218   emit_insn (gen_rtx_SET (VOIDmode, e0,
28219                           gen_rtx_MULT (mode, x0, a)));
28220   /* e1 = x0 * b */
28221   emit_insn (gen_rtx_SET (VOIDmode, e1,
28222                           gen_rtx_MULT (mode, x0, b)));
28223   /* x1 = 2. - e1 */
28224   emit_insn (gen_rtx_SET (VOIDmode, x1,
28225                           gen_rtx_MINUS (mode, two, e1)));
28226   /* res = e0 * x1 */
28227   emit_insn (gen_rtx_SET (VOIDmode, res,
28228                           gen_rtx_MULT (mode, e0, x1)));
28229 }
28230
28231 /* Output code to perform a Newton-Rhapson approximation of a
28232    single precision floating point [reciprocal] square root.  */
28233
28234 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
28235                          bool recip)
28236 {
28237   rtx x0, e0, e1, e2, e3, mthree, mhalf;
28238   REAL_VALUE_TYPE r;
28239
28240   x0 = gen_reg_rtx (mode);
28241   e0 = gen_reg_rtx (mode);
28242   e1 = gen_reg_rtx (mode);
28243   e2 = gen_reg_rtx (mode);
28244   e3 = gen_reg_rtx (mode);
28245
28246   real_from_integer (&r, VOIDmode, -3, -1, 0);
28247   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
28248
28249   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
28250   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
28251
28252   if (VECTOR_MODE_P (mode))
28253     {
28254       mthree = ix86_build_const_vector (SFmode, true, mthree);
28255       mhalf = ix86_build_const_vector (SFmode, true, mhalf);
28256     }
28257
28258   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
28259      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
28260
28261   /* x0 = rsqrt(a) estimate */
28262   emit_insn (gen_rtx_SET (VOIDmode, x0,
28263                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
28264                                           UNSPEC_RSQRT)));
28265
28266   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
28267   if (!recip)
28268     {
28269       rtx zero, mask;
28270
28271       zero = gen_reg_rtx (mode);
28272       mask = gen_reg_rtx (mode);
28273
28274       zero = force_reg (mode, CONST0_RTX(mode));
28275       emit_insn (gen_rtx_SET (VOIDmode, mask,
28276                               gen_rtx_NE (mode, zero, a)));
28277
28278       emit_insn (gen_rtx_SET (VOIDmode, x0,
28279                               gen_rtx_AND (mode, x0, mask)));
28280     }
28281
28282   /* e0 = x0 * a */
28283   emit_insn (gen_rtx_SET (VOIDmode, e0,
28284                           gen_rtx_MULT (mode, x0, a)));
28285   /* e1 = e0 * x0 */
28286   emit_insn (gen_rtx_SET (VOIDmode, e1,
28287                           gen_rtx_MULT (mode, e0, x0)));
28288
28289   /* e2 = e1 - 3. */
28290   mthree = force_reg (mode, mthree);
28291   emit_insn (gen_rtx_SET (VOIDmode, e2,
28292                           gen_rtx_PLUS (mode, e1, mthree)));
28293
28294   mhalf = force_reg (mode, mhalf);
28295   if (recip)
28296     /* e3 = -.5 * x0 */
28297     emit_insn (gen_rtx_SET (VOIDmode, e3,
28298                             gen_rtx_MULT (mode, x0, mhalf)));
28299   else
28300     /* e3 = -.5 * e0 */
28301     emit_insn (gen_rtx_SET (VOIDmode, e3,
28302                             gen_rtx_MULT (mode, e0, mhalf)));
28303   /* ret = e2 * e3 */
28304   emit_insn (gen_rtx_SET (VOIDmode, res,
28305                           gen_rtx_MULT (mode, e2, e3)));
28306 }
28307
28308 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
28309
28310 static void ATTRIBUTE_UNUSED
28311 i386_solaris_elf_named_section (const char *name, unsigned int flags,
28312                                 tree decl)
28313 {
28314   /* With Binutils 2.15, the "@unwind" marker must be specified on
28315      every occurrence of the ".eh_frame" section, not just the first
28316      one.  */
28317   if (TARGET_64BIT
28318       && strcmp (name, ".eh_frame") == 0)
28319     {
28320       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
28321                flags & SECTION_WRITE ? "aw" : "a");
28322       return;
28323     }
28324   default_elf_asm_named_section (name, flags, decl);
28325 }
28326
28327 /* Return the mangling of TYPE if it is an extended fundamental type.  */
28328
28329 static const char *
28330 ix86_mangle_type (const_tree type)
28331 {
28332   type = TYPE_MAIN_VARIANT (type);
28333
28334   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
28335       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
28336     return NULL;
28337
28338   switch (TYPE_MODE (type))
28339     {
28340     case TFmode:
28341       /* __float128 is "g".  */
28342       return "g";
28343     case XFmode:
28344       /* "long double" or __float80 is "e".  */
28345       return "e";
28346     default:
28347       return NULL;
28348     }
28349 }
28350
28351 /* For 32-bit code we can save PIC register setup by using
28352    __stack_chk_fail_local hidden function instead of calling
28353    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
28354    register, so it is better to call __stack_chk_fail directly.  */
28355
28356 static tree
28357 ix86_stack_protect_fail (void)
28358 {
28359   return TARGET_64BIT
28360          ? default_external_stack_protect_fail ()
28361          : default_hidden_stack_protect_fail ();
28362 }
28363
28364 /* Select a format to encode pointers in exception handling data.  CODE
28365    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
28366    true if the symbol may be affected by dynamic relocations.
28367
28368    ??? All x86 object file formats are capable of representing this.
28369    After all, the relocation needed is the same as for the call insn.
28370    Whether or not a particular assembler allows us to enter such, I
28371    guess we'll have to see.  */
28372 int
28373 asm_preferred_eh_data_format (int code, int global)
28374 {
28375   if (flag_pic)
28376     {
28377       int type = DW_EH_PE_sdata8;
28378       if (!TARGET_64BIT
28379           || ix86_cmodel == CM_SMALL_PIC
28380           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
28381         type = DW_EH_PE_sdata4;
28382       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
28383     }
28384   if (ix86_cmodel == CM_SMALL
28385       || (ix86_cmodel == CM_MEDIUM && code))
28386     return DW_EH_PE_udata4;
28387   return DW_EH_PE_absptr;
28388 }
28389 \f
28390 /* Expand copysign from SIGN to the positive value ABS_VALUE
28391    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
28392    the sign-bit.  */
28393 static void
28394 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
28395 {
28396   enum machine_mode mode = GET_MODE (sign);
28397   rtx sgn = gen_reg_rtx (mode);
28398   if (mask == NULL_RTX)
28399     {
28400       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
28401       if (!VECTOR_MODE_P (mode))
28402         {
28403           /* We need to generate a scalar mode mask in this case.  */
28404           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
28405           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
28406           mask = gen_reg_rtx (mode);
28407           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
28408         }
28409     }
28410   else
28411     mask = gen_rtx_NOT (mode, mask);
28412   emit_insn (gen_rtx_SET (VOIDmode, sgn,
28413                           gen_rtx_AND (mode, mask, sign)));
28414   emit_insn (gen_rtx_SET (VOIDmode, result,
28415                           gen_rtx_IOR (mode, abs_value, sgn)));
28416 }
28417
28418 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
28419    mask for masking out the sign-bit is stored in *SMASK, if that is
28420    non-null.  */
28421 static rtx
28422 ix86_expand_sse_fabs (rtx op0, rtx *smask)
28423 {
28424   enum machine_mode mode = GET_MODE (op0);
28425   rtx xa, mask;
28426
28427   xa = gen_reg_rtx (mode);
28428   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
28429   if (!VECTOR_MODE_P (mode))
28430     {
28431       /* We need to generate a scalar mode mask in this case.  */
28432       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
28433       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
28434       mask = gen_reg_rtx (mode);
28435       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
28436     }
28437   emit_insn (gen_rtx_SET (VOIDmode, xa,
28438                           gen_rtx_AND (mode, op0, mask)));
28439
28440   if (smask)
28441     *smask = mask;
28442
28443   return xa;
28444 }
28445
28446 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
28447    swapping the operands if SWAP_OPERANDS is true.  The expanded
28448    code is a forward jump to a newly created label in case the
28449    comparison is true.  The generated label rtx is returned.  */
28450 static rtx
28451 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
28452                                   bool swap_operands)
28453 {
28454   rtx label, tmp;
28455
28456   if (swap_operands)
28457     {
28458       tmp = op0;
28459       op0 = op1;
28460       op1 = tmp;
28461     }
28462
28463   label = gen_label_rtx ();
28464   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
28465   emit_insn (gen_rtx_SET (VOIDmode, tmp,
28466                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
28467   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
28468   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
28469                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
28470   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
28471   JUMP_LABEL (tmp) = label;
28472
28473   return label;
28474 }
28475
28476 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
28477    using comparison code CODE.  Operands are swapped for the comparison if
28478    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
28479 static rtx
28480 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
28481                               bool swap_operands)
28482 {
28483   enum machine_mode mode = GET_MODE (op0);
28484   rtx mask = gen_reg_rtx (mode);
28485
28486   if (swap_operands)
28487     {
28488       rtx tmp = op0;
28489       op0 = op1;
28490       op1 = tmp;
28491     }
28492
28493   if (mode == DFmode)
28494     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
28495                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
28496   else
28497     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
28498                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
28499
28500   return mask;
28501 }
28502
28503 /* Generate and return a rtx of mode MODE for 2**n where n is the number
28504    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
28505 static rtx
28506 ix86_gen_TWO52 (enum machine_mode mode)
28507 {
28508   REAL_VALUE_TYPE TWO52r;
28509   rtx TWO52;
28510
28511   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
28512   TWO52 = const_double_from_real_value (TWO52r, mode);
28513   TWO52 = force_reg (mode, TWO52);
28514
28515   return TWO52;
28516 }
28517
28518 /* Expand SSE sequence for computing lround from OP1 storing
28519    into OP0.  */
28520 void
28521 ix86_expand_lround (rtx op0, rtx op1)
28522 {
28523   /* C code for the stuff we're doing below:
28524        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
28525        return (long)tmp;
28526    */
28527   enum machine_mode mode = GET_MODE (op1);
28528   const struct real_format *fmt;
28529   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
28530   rtx adj;
28531
28532   /* load nextafter (0.5, 0.0) */
28533   fmt = REAL_MODE_FORMAT (mode);
28534   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
28535   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
28536
28537   /* adj = copysign (0.5, op1) */
28538   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
28539   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
28540
28541   /* adj = op1 + adj */
28542   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
28543
28544   /* op0 = (imode)adj */
28545   expand_fix (op0, adj, 0);
28546 }
28547
28548 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
28549    into OPERAND0.  */
28550 void
28551 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
28552 {
28553   /* C code for the stuff we're doing below (for do_floor):
28554         xi = (long)op1;
28555         xi -= (double)xi > op1 ? 1 : 0;
28556         return xi;
28557    */
28558   enum machine_mode fmode = GET_MODE (op1);
28559   enum machine_mode imode = GET_MODE (op0);
28560   rtx ireg, freg, label, tmp;
28561
28562   /* reg = (long)op1 */
28563   ireg = gen_reg_rtx (imode);
28564   expand_fix (ireg, op1, 0);
28565
28566   /* freg = (double)reg */
28567   freg = gen_reg_rtx (fmode);
28568   expand_float (freg, ireg, 0);
28569
28570   /* ireg = (freg > op1) ? ireg - 1 : ireg */
28571   label = ix86_expand_sse_compare_and_jump (UNLE,
28572                                             freg, op1, !do_floor);
28573   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
28574                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
28575   emit_move_insn (ireg, tmp);
28576
28577   emit_label (label);
28578   LABEL_NUSES (label) = 1;
28579
28580   emit_move_insn (op0, ireg);
28581 }
28582
28583 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
28584    result in OPERAND0.  */
28585 void
28586 ix86_expand_rint (rtx operand0, rtx operand1)
28587 {
28588   /* C code for the stuff we're doing below:
28589         xa = fabs (operand1);
28590         if (!isless (xa, 2**52))
28591           return operand1;
28592         xa = xa + 2**52 - 2**52;
28593         return copysign (xa, operand1);
28594    */
28595   enum machine_mode mode = GET_MODE (operand0);
28596   rtx res, xa, label, TWO52, mask;
28597
28598   res = gen_reg_rtx (mode);
28599   emit_move_insn (res, operand1);
28600
28601   /* xa = abs (operand1) */
28602   xa = ix86_expand_sse_fabs (res, &mask);
28603
28604   /* if (!isless (xa, TWO52)) goto label; */
28605   TWO52 = ix86_gen_TWO52 (mode);
28606   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28607
28608   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
28609   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
28610
28611   ix86_sse_copysign_to_positive (res, xa, res, mask);
28612
28613   emit_label (label);
28614   LABEL_NUSES (label) = 1;
28615
28616   emit_move_insn (operand0, res);
28617 }
28618
28619 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
28620    into OPERAND0.  */
28621 void
28622 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
28623 {
28624   /* C code for the stuff we expand below.
28625         double xa = fabs (x), x2;
28626         if (!isless (xa, TWO52))
28627           return x;
28628         xa = xa + TWO52 - TWO52;
28629         x2 = copysign (xa, x);
28630      Compensate.  Floor:
28631         if (x2 > x)
28632           x2 -= 1;
28633      Compensate.  Ceil:
28634         if (x2 < x)
28635           x2 -= -1;
28636         return x2;
28637    */
28638   enum machine_mode mode = GET_MODE (operand0);
28639   rtx xa, TWO52, tmp, label, one, res, mask;
28640
28641   TWO52 = ix86_gen_TWO52 (mode);
28642
28643   /* Temporary for holding the result, initialized to the input
28644      operand to ease control flow.  */
28645   res = gen_reg_rtx (mode);
28646   emit_move_insn (res, operand1);
28647
28648   /* xa = abs (operand1) */
28649   xa = ix86_expand_sse_fabs (res, &mask);
28650
28651   /* if (!isless (xa, TWO52)) goto label; */
28652   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28653
28654   /* xa = xa + TWO52 - TWO52; */
28655   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
28656   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
28657
28658   /* xa = copysign (xa, operand1) */
28659   ix86_sse_copysign_to_positive (xa, xa, res, mask);
28660
28661   /* generate 1.0 or -1.0 */
28662   one = force_reg (mode,
28663                    const_double_from_real_value (do_floor
28664                                                  ? dconst1 : dconstm1, mode));
28665
28666   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
28667   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
28668   emit_insn (gen_rtx_SET (VOIDmode, tmp,
28669                           gen_rtx_AND (mode, one, tmp)));
28670   /* We always need to subtract here to preserve signed zero.  */
28671   tmp = expand_simple_binop (mode, MINUS,
28672                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
28673   emit_move_insn (res, tmp);
28674
28675   emit_label (label);
28676   LABEL_NUSES (label) = 1;
28677
28678   emit_move_insn (operand0, res);
28679 }
28680
28681 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
28682    into OPERAND0.  */
28683 void
28684 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
28685 {
28686   /* C code for the stuff we expand below.
28687         double xa = fabs (x), x2;
28688         if (!isless (xa, TWO52))
28689           return x;
28690         x2 = (double)(long)x;
28691      Compensate.  Floor:
28692         if (x2 > x)
28693           x2 -= 1;
28694      Compensate.  Ceil:
28695         if (x2 < x)
28696           x2 += 1;
28697         if (HONOR_SIGNED_ZEROS (mode))
28698           return copysign (x2, x);
28699         return x2;
28700    */
28701   enum machine_mode mode = GET_MODE (operand0);
28702   rtx xa, xi, TWO52, tmp, label, one, res, mask;
28703
28704   TWO52 = ix86_gen_TWO52 (mode);
28705
28706   /* Temporary for holding the result, initialized to the input
28707      operand to ease control flow.  */
28708   res = gen_reg_rtx (mode);
28709   emit_move_insn (res, operand1);
28710
28711   /* xa = abs (operand1) */
28712   xa = ix86_expand_sse_fabs (res, &mask);
28713
28714   /* if (!isless (xa, TWO52)) goto label; */
28715   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28716
28717   /* xa = (double)(long)x */
28718   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
28719   expand_fix (xi, res, 0);
28720   expand_float (xa, xi, 0);
28721
28722   /* generate 1.0 */
28723   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
28724
28725   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
28726   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
28727   emit_insn (gen_rtx_SET (VOIDmode, tmp,
28728                           gen_rtx_AND (mode, one, tmp)));
28729   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
28730                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
28731   emit_move_insn (res, tmp);
28732
28733   if (HONOR_SIGNED_ZEROS (mode))
28734     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
28735
28736   emit_label (label);
28737   LABEL_NUSES (label) = 1;
28738
28739   emit_move_insn (operand0, res);
28740 }
28741
28742 /* Expand SSE sequence for computing round from OPERAND1 storing
28743    into OPERAND0.  Sequence that works without relying on DImode truncation
28744    via cvttsd2siq that is only available on 64bit targets.  */
28745 void
28746 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
28747 {
28748   /* C code for the stuff we expand below.
28749         double xa = fabs (x), xa2, x2;
28750         if (!isless (xa, TWO52))
28751           return x;
28752      Using the absolute value and copying back sign makes
28753      -0.0 -> -0.0 correct.
28754         xa2 = xa + TWO52 - TWO52;
28755      Compensate.
28756         dxa = xa2 - xa;
28757         if (dxa <= -0.5)
28758           xa2 += 1;
28759         else if (dxa > 0.5)
28760           xa2 -= 1;
28761         x2 = copysign (xa2, x);
28762         return x2;
28763    */
28764   enum machine_mode mode = GET_MODE (operand0);
28765   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
28766
28767   TWO52 = ix86_gen_TWO52 (mode);
28768
28769   /* Temporary for holding the result, initialized to the input
28770      operand to ease control flow.  */
28771   res = gen_reg_rtx (mode);
28772   emit_move_insn (res, operand1);
28773
28774   /* xa = abs (operand1) */
28775   xa = ix86_expand_sse_fabs (res, &mask);
28776
28777   /* if (!isless (xa, TWO52)) goto label; */
28778   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28779
28780   /* xa2 = xa + TWO52 - TWO52; */
28781   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
28782   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
28783
28784   /* dxa = xa2 - xa; */
28785   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
28786
28787   /* generate 0.5, 1.0 and -0.5 */
28788   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
28789   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
28790   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
28791                                0, OPTAB_DIRECT);
28792
28793   /* Compensate.  */
28794   tmp = gen_reg_rtx (mode);
28795   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
28796   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
28797   emit_insn (gen_rtx_SET (VOIDmode, tmp,
28798                           gen_rtx_AND (mode, one, tmp)));
28799   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
28800   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
28801   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
28802   emit_insn (gen_rtx_SET (VOIDmode, tmp,
28803                           gen_rtx_AND (mode, one, tmp)));
28804   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
28805
28806   /* res = copysign (xa2, operand1) */
28807   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
28808
28809   emit_label (label);
28810   LABEL_NUSES (label) = 1;
28811
28812   emit_move_insn (operand0, res);
28813 }
28814
28815 /* Expand SSE sequence for computing trunc from OPERAND1 storing
28816    into OPERAND0.  */
28817 void
28818 ix86_expand_trunc (rtx operand0, rtx operand1)
28819 {
28820   /* C code for SSE variant we expand below.
28821         double xa = fabs (x), x2;
28822         if (!isless (xa, TWO52))
28823           return x;
28824         x2 = (double)(long)x;
28825         if (HONOR_SIGNED_ZEROS (mode))
28826           return copysign (x2, x);
28827         return x2;
28828    */
28829   enum machine_mode mode = GET_MODE (operand0);
28830   rtx xa, xi, TWO52, label, res, mask;
28831
28832   TWO52 = ix86_gen_TWO52 (mode);
28833
28834   /* Temporary for holding the result, initialized to the input
28835      operand to ease control flow.  */
28836   res = gen_reg_rtx (mode);
28837   emit_move_insn (res, operand1);
28838
28839   /* xa = abs (operand1) */
28840   xa = ix86_expand_sse_fabs (res, &mask);
28841
28842   /* if (!isless (xa, TWO52)) goto label; */
28843   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28844
28845   /* x = (double)(long)x */
28846   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
28847   expand_fix (xi, res, 0);
28848   expand_float (res, xi, 0);
28849
28850   if (HONOR_SIGNED_ZEROS (mode))
28851     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
28852
28853   emit_label (label);
28854   LABEL_NUSES (label) = 1;
28855
28856   emit_move_insn (operand0, res);
28857 }
28858
28859 /* Expand SSE sequence for computing trunc from OPERAND1 storing
28860    into OPERAND0.  */
28861 void
28862 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
28863 {
28864   enum machine_mode mode = GET_MODE (operand0);
28865   rtx xa, mask, TWO52, label, one, res, smask, tmp;
28866
28867   /* C code for SSE variant we expand below.
28868         double xa = fabs (x), x2;
28869         if (!isless (xa, TWO52))
28870           return x;
28871         xa2 = xa + TWO52 - TWO52;
28872      Compensate:
28873         if (xa2 > xa)
28874           xa2 -= 1.0;
28875         x2 = copysign (xa2, x);
28876         return x2;
28877    */
28878
28879   TWO52 = ix86_gen_TWO52 (mode);
28880
28881   /* Temporary for holding the result, initialized to the input
28882      operand to ease control flow.  */
28883   res = gen_reg_rtx (mode);
28884   emit_move_insn (res, operand1);
28885
28886   /* xa = abs (operand1) */
28887   xa = ix86_expand_sse_fabs (res, &smask);
28888
28889   /* if (!isless (xa, TWO52)) goto label; */
28890   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28891
28892   /* res = xa + TWO52 - TWO52; */
28893   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
28894   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
28895   emit_move_insn (res, tmp);
28896
28897   /* generate 1.0 */
28898   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
28899
28900   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
28901   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
28902   emit_insn (gen_rtx_SET (VOIDmode, mask,
28903                           gen_rtx_AND (mode, mask, one)));
28904   tmp = expand_simple_binop (mode, MINUS,
28905                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
28906   emit_move_insn (res, tmp);
28907
28908   /* res = copysign (res, operand1) */
28909   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
28910
28911   emit_label (label);
28912   LABEL_NUSES (label) = 1;
28913
28914   emit_move_insn (operand0, res);
28915 }
28916
28917 /* Expand SSE sequence for computing round from OPERAND1 storing
28918    into OPERAND0.  */
28919 void
28920 ix86_expand_round (rtx operand0, rtx operand1)
28921 {
28922   /* C code for the stuff we're doing below:
28923         double xa = fabs (x);
28924         if (!isless (xa, TWO52))
28925           return x;
28926         xa = (double)(long)(xa + nextafter (0.5, 0.0));
28927         return copysign (xa, x);
28928    */
28929   enum machine_mode mode = GET_MODE (operand0);
28930   rtx res, TWO52, xa, label, xi, half, mask;
28931   const struct real_format *fmt;
28932   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
28933
28934   /* Temporary for holding the result, initialized to the input
28935      operand to ease control flow.  */
28936   res = gen_reg_rtx (mode);
28937   emit_move_insn (res, operand1);
28938
28939   TWO52 = ix86_gen_TWO52 (mode);
28940   xa = ix86_expand_sse_fabs (res, &mask);
28941   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
28942
28943   /* load nextafter (0.5, 0.0) */
28944   fmt = REAL_MODE_FORMAT (mode);
28945   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
28946   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
28947
28948   /* xa = xa + 0.5 */
28949   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
28950   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
28951
28952   /* xa = (double)(int64_t)xa */
28953   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
28954   expand_fix (xi, xa, 0);
28955   expand_float (xa, xi, 0);
28956
28957   /* res = copysign (xa, operand1) */
28958   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
28959
28960   emit_label (label);
28961   LABEL_NUSES (label) = 1;
28962
28963   emit_move_insn (operand0, res);
28964 }
28965 \f
28966
28967 /* Table of valid machine attributes.  */
28968 static const struct attribute_spec ix86_attribute_table[] =
28969 {
28970   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
28971   /* Stdcall attribute says callee is responsible for popping arguments
28972      if they are not variable.  */
28973   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
28974   /* Fastcall attribute says callee is responsible for popping arguments
28975      if they are not variable.  */
28976   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
28977   /* Cdecl attribute says the callee is a normal C declaration */
28978   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
28979   /* Regparm attribute specifies how many integer arguments are to be
28980      passed in registers.  */
28981   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
28982   /* Sseregparm attribute says we are using x86_64 calling conventions
28983      for FP arguments.  */
28984   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
28985   /* force_align_arg_pointer says this function realigns the stack at entry.  */
28986   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
28987     false, true,  true, ix86_handle_cconv_attribute },
28988 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
28989   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
28990   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
28991   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
28992 #endif
28993   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
28994   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
28995 #ifdef SUBTARGET_ATTRIBUTE_TABLE
28996   SUBTARGET_ATTRIBUTE_TABLE,
28997 #endif
28998   /* ms_abi and sysv_abi calling convention function attributes.  */
28999   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
29000   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
29001   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute },
29002   /* End element.  */
29003   { NULL,        0, 0, false, false, false, NULL }
29004 };
29005
29006 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
29007 static int
29008 ix86_builtin_vectorization_cost (bool runtime_test)
29009 {
29010   /* If the branch of the runtime test is taken - i.e. - the vectorized
29011      version is skipped - this incurs a misprediction cost (because the
29012      vectorized version is expected to be the fall-through).  So we subtract
29013      the latency of a mispredicted branch from the costs that are incured
29014      when the vectorized version is executed.
29015
29016      TODO: The values in individual target tables have to be tuned or new
29017      fields may be needed. For eg. on K8, the default branch path is the
29018      not-taken path. If the taken path is predicted correctly, the minimum
29019      penalty of going down the taken-path is 1 cycle. If the taken-path is
29020      not predicted correctly, then the minimum penalty is 10 cycles.  */
29021
29022   if (runtime_test)
29023     {
29024       return (-(ix86_cost->cond_taken_branch_cost));
29025     }
29026   else
29027     return 0;
29028 }
29029
29030 /* Implement targetm.vectorize.builtin_vec_perm.  */
29031
29032 static tree
29033 ix86_vectorize_builtin_vec_perm (tree vec_type, tree *mask_type)
29034 {
29035   tree itype = TREE_TYPE (vec_type);
29036   bool u = TYPE_UNSIGNED (itype);
29037   enum machine_mode vmode = TYPE_MODE (vec_type);
29038   enum ix86_builtins fcode = fcode; /* Silence bogus warning.  */
29039   bool ok = TARGET_SSE2;
29040
29041   switch (vmode)
29042     {
29043     case V4DFmode:
29044       ok = TARGET_AVX;
29045       fcode = IX86_BUILTIN_VEC_PERM_V4DF;
29046       goto get_di;
29047     case V2DFmode:
29048       fcode = IX86_BUILTIN_VEC_PERM_V2DF;
29049     get_di:
29050       itype = ix86_get_builtin_type (IX86_BT_DI);
29051       break;
29052
29053     case V8SFmode:
29054       ok = TARGET_AVX;
29055       fcode = IX86_BUILTIN_VEC_PERM_V8SF;
29056       goto get_si;
29057     case V4SFmode:
29058       ok = TARGET_SSE;
29059       fcode = IX86_BUILTIN_VEC_PERM_V4SF;
29060     get_si:
29061       itype = ix86_get_builtin_type (IX86_BT_SI);
29062       break;
29063
29064     case V2DImode:
29065       fcode = u ? IX86_BUILTIN_VEC_PERM_V2DI_U : IX86_BUILTIN_VEC_PERM_V2DI;
29066       break;
29067     case V4SImode:
29068       fcode = u ? IX86_BUILTIN_VEC_PERM_V4SI_U : IX86_BUILTIN_VEC_PERM_V4SI;
29069       break;
29070     case V8HImode:
29071       fcode = u ? IX86_BUILTIN_VEC_PERM_V8HI_U : IX86_BUILTIN_VEC_PERM_V8HI;
29072       break;
29073     case V16QImode:
29074       fcode = u ? IX86_BUILTIN_VEC_PERM_V16QI_U : IX86_BUILTIN_VEC_PERM_V16QI;
29075       break;
29076     default:
29077       ok = false;
29078       break;
29079     }
29080
29081   if (!ok)
29082     return NULL_TREE;
29083
29084   *mask_type = itype;
29085   return ix86_builtins[(int) fcode];
29086 }
29087
29088 /* Return a vector mode with twice as many elements as VMODE.  */
29089 /* ??? Consider moving this to a table generated by genmodes.c.  */
29090
29091 static enum machine_mode
29092 doublesize_vector_mode (enum machine_mode vmode)
29093 {
29094   switch (vmode)
29095     {
29096     case V2SFmode:      return V4SFmode;
29097     case V1DImode:      return V2DImode;
29098     case V2SImode:      return V4SImode;
29099     case V4HImode:      return V8HImode;
29100     case V8QImode:      return V16QImode;
29101
29102     case V2DFmode:      return V4DFmode;
29103     case V4SFmode:      return V8SFmode;
29104     case V2DImode:      return V4DImode;
29105     case V4SImode:      return V8SImode;
29106     case V8HImode:      return V16HImode;
29107     case V16QImode:     return V32QImode;
29108
29109     case V4DFmode:      return V8DFmode;
29110     case V8SFmode:      return V16SFmode;
29111     case V4DImode:      return V8DImode;
29112     case V8SImode:      return V16SImode;
29113     case V16HImode:     return V32HImode;
29114     case V32QImode:     return V64QImode;
29115
29116     default:
29117       gcc_unreachable ();
29118     }
29119 }
29120
29121 /* Construct (set target (vec_select op0 (parallel perm))) and
29122    return true if that's a valid instruction in the active ISA.  */
29123
29124 static bool
29125 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
29126 {
29127   rtx rperm[MAX_VECT_LEN], x;
29128   unsigned i;
29129
29130   for (i = 0; i < nelt; ++i)
29131     rperm[i] = GEN_INT (perm[i]);
29132
29133   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
29134   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
29135   x = gen_rtx_SET (VOIDmode, target, x);
29136
29137   x = emit_insn (x);
29138   if (recog_memoized (x) < 0)
29139     {
29140       remove_insn (x);
29141       return false;
29142     }
29143   return true;
29144 }
29145
29146 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
29147
29148 static bool
29149 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
29150                         const unsigned char *perm, unsigned nelt)
29151 {
29152   enum machine_mode v2mode;
29153   rtx x;
29154
29155   v2mode = doublesize_vector_mode (GET_MODE (op0));
29156   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
29157   return expand_vselect (target, x, perm, nelt);
29158 }
29159
29160 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
29161    in terms of blendp[sd] / pblendw / pblendvb.  */
29162
29163 static bool
29164 expand_vec_perm_blend (struct expand_vec_perm_d *d)
29165 {
29166   enum machine_mode vmode = d->vmode;
29167   unsigned i, mask, nelt = d->nelt;
29168   rtx target, op0, op1, x;
29169
29170   if (!TARGET_SSE4_1 || d->op0 == d->op1)
29171     return false;
29172   if (!(GET_MODE_SIZE (vmode) == 16 || vmode == V4DFmode || vmode == V8SFmode))
29173     return false;
29174
29175   /* This is a blend, not a permute.  Elements must stay in their
29176      respective lanes.  */
29177   for (i = 0; i < nelt; ++i)
29178     {
29179       unsigned e = d->perm[i];
29180       if (!(e == i || e == i + nelt))
29181         return false;
29182     }
29183
29184   if (d->testing_p)
29185     return true;
29186
29187   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
29188      decision should be extracted elsewhere, so that we only try that
29189      sequence once all budget==3 options have been tried.  */
29190
29191   /* For bytes, see if bytes move in pairs so we can use pblendw with
29192      an immediate argument, rather than pblendvb with a vector argument.  */
29193   if (vmode == V16QImode)
29194     {
29195       bool pblendw_ok = true;
29196       for (i = 0; i < 16 && pblendw_ok; i += 2)
29197         pblendw_ok = (d->perm[i] + 1 == d->perm[i + 1]);
29198
29199       if (!pblendw_ok)
29200         {
29201           rtx rperm[16], vperm;
29202
29203           for (i = 0; i < nelt; ++i)
29204             rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
29205
29206           vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
29207           vperm = force_reg (V16QImode, vperm);
29208
29209           emit_insn (gen_sse4_1_pblendvb (d->target, d->op0, d->op1, vperm));
29210           return true;
29211         }
29212     }
29213
29214   target = d->target;
29215   op0 = d->op0;
29216   op1 = d->op1;
29217   mask = 0;
29218
29219   switch (vmode)
29220     {
29221     case V4DFmode:
29222     case V8SFmode:
29223     case V2DFmode:
29224     case V4SFmode:
29225     case V8HImode:
29226       for (i = 0; i < nelt; ++i)
29227         mask |= (d->perm[i] >= nelt) << i;
29228       break;
29229
29230     case V2DImode:
29231       for (i = 0; i < 2; ++i)
29232         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
29233       goto do_subreg;
29234
29235     case V4SImode:
29236       for (i = 0; i < 4; ++i)
29237         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
29238       goto do_subreg;
29239
29240     case V16QImode:
29241       for (i = 0; i < 8; ++i)
29242         mask |= (d->perm[i * 2] >= 16) << i;
29243
29244     do_subreg:
29245       vmode = V8HImode;
29246       target = gen_lowpart (vmode, target);
29247       op0 = gen_lowpart (vmode, op0);
29248       op1 = gen_lowpart (vmode, op1);
29249       break;
29250
29251     default:
29252       gcc_unreachable ();
29253     }
29254
29255   /* This matches five different patterns with the different modes.  */
29256   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
29257   x = gen_rtx_SET (VOIDmode, target, x);
29258   emit_insn (x);
29259
29260   return true;
29261 }
29262
29263 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
29264    in terms of the variable form of vpermilps.
29265
29266    Note that we will have already failed the immediate input vpermilps,
29267    which requires that the high and low part shuffle be identical; the
29268    variable form doesn't require that.  */
29269
29270 static bool
29271 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
29272 {
29273   rtx rperm[8], vperm;
29274   unsigned i;
29275
29276   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
29277     return false;
29278
29279   /* We can only permute within the 128-bit lane.  */
29280   for (i = 0; i < 8; ++i)
29281     {
29282       unsigned e = d->perm[i];
29283       if (i < 4 ? e >= 4 : e < 4)
29284         return false;
29285     }
29286
29287   if (d->testing_p)
29288     return true;
29289
29290   for (i = 0; i < 8; ++i)
29291     {
29292       unsigned e = d->perm[i];
29293
29294       /* Within each 128-bit lane, the elements of op0 are numbered
29295          from 0 and the elements of op1 are numbered from 4.  */
29296       if (e >= 8 + 4)
29297         e -= 8;
29298       else if (e >= 4)
29299         e -= 4;
29300
29301       rperm[i] = GEN_INT (e);
29302     }
29303
29304   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
29305   vperm = force_reg (V8SImode, vperm);
29306   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
29307
29308   return true;
29309 }
29310
29311 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
29312    in terms of pshufb or vpperm.  */
29313
29314 static bool
29315 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
29316 {
29317   unsigned i, nelt, eltsz;
29318   rtx rperm[16], vperm, target, op0, op1;
29319
29320   if (!(d->op0 == d->op1 ? TARGET_SSSE3 : TARGET_XOP))
29321     return false;
29322   if (GET_MODE_SIZE (d->vmode) != 16)
29323     return false;
29324
29325   if (d->testing_p)
29326     return true;
29327
29328   nelt = d->nelt;
29329   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
29330
29331   for (i = 0; i < nelt; ++i)
29332     {
29333       unsigned j, e = d->perm[i];
29334       for (j = 0; j < eltsz; ++j)
29335         rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
29336     }
29337
29338   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
29339   vperm = force_reg (V16QImode, vperm);
29340
29341   target = gen_lowpart (V16QImode, d->target);
29342   op0 = gen_lowpart (V16QImode, d->op0);
29343   if (d->op0 == d->op1)
29344     emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
29345   else
29346     {
29347       op1 = gen_lowpart (V16QImode, d->op1);
29348       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
29349     }
29350
29351   return true;
29352 }
29353
29354 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
29355    in a single instruction.  */
29356
29357 static bool
29358 expand_vec_perm_1 (struct expand_vec_perm_d *d)
29359 {
29360   unsigned i, nelt = d->nelt;
29361   unsigned char perm2[MAX_VECT_LEN];
29362
29363   /* Check plain VEC_SELECT first, because AVX has instructions that could
29364      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
29365      input where SEL+CONCAT may not.  */
29366   if (d->op0 == d->op1)
29367     {
29368       int mask = nelt - 1;
29369
29370       for (i = 0; i < nelt; i++)
29371         perm2[i] = d->perm[i] & mask;
29372
29373       if (expand_vselect (d->target, d->op0, perm2, nelt))
29374         return true;
29375
29376       /* There are plenty of patterns in sse.md that are written for
29377          SEL+CONCAT and are not replicated for a single op.  Perhaps
29378          that should be changed, to avoid the nastiness here.  */
29379
29380       /* Recognize interleave style patterns, which means incrementing
29381          every other permutation operand.  */
29382       for (i = 0; i < nelt; i += 2)
29383         {
29384           perm2[i] = d->perm[i] & mask;
29385           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
29386         }
29387       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
29388         return true;
29389
29390       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
29391       if (nelt >= 4)
29392         {
29393           for (i = 0; i < nelt; i += 4)
29394             {
29395               perm2[i + 0] = d->perm[i + 0] & mask;
29396               perm2[i + 1] = d->perm[i + 1] & mask;
29397               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
29398               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
29399             }
29400
29401           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
29402             return true;
29403         }
29404     }
29405
29406   /* Finally, try the fully general two operand permute.  */
29407   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
29408     return true;
29409
29410   /* Recognize interleave style patterns with reversed operands.  */
29411   if (d->op0 != d->op1)
29412     {
29413       for (i = 0; i < nelt; ++i)
29414         {
29415           unsigned e = d->perm[i];
29416           if (e >= nelt)
29417             e -= nelt;
29418           else
29419             e += nelt;
29420           perm2[i] = e;
29421         }
29422
29423       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
29424         return true;
29425     }
29426
29427   /* Try the SSE4.1 blend variable merge instructions.  */
29428   if (expand_vec_perm_blend (d))
29429     return true;
29430
29431   /* Try one of the AVX vpermil variable permutations.  */
29432   if (expand_vec_perm_vpermil (d))
29433     return true;
29434
29435   /* Try the SSSE3 pshufb or XOP vpperm variable permutation.  */
29436   if (expand_vec_perm_pshufb (d))
29437     return true;
29438
29439   return false;
29440 }
29441
29442 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
29443    in terms of a pair of pshuflw + pshufhw instructions.  */
29444
29445 static bool
29446 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
29447 {
29448   unsigned char perm2[MAX_VECT_LEN];
29449   unsigned i;
29450   bool ok;
29451
29452   if (d->vmode != V8HImode || d->op0 != d->op1)
29453     return false;
29454
29455   /* The two permutations only operate in 64-bit lanes.  */
29456   for (i = 0; i < 4; ++i)
29457     if (d->perm[i] >= 4)
29458       return false;
29459   for (i = 4; i < 8; ++i)
29460     if (d->perm[i] < 4)
29461       return false;
29462
29463   if (d->testing_p)
29464     return true;
29465
29466   /* Emit the pshuflw.  */
29467   memcpy (perm2, d->perm, 4);
29468   for (i = 4; i < 8; ++i)
29469     perm2[i] = i;
29470   ok = expand_vselect (d->target, d->op0, perm2, 8);
29471   gcc_assert (ok);
29472
29473   /* Emit the pshufhw.  */
29474   memcpy (perm2 + 4, d->perm + 4, 4);
29475   for (i = 0; i < 4; ++i)
29476     perm2[i] = i;
29477   ok = expand_vselect (d->target, d->target, perm2, 8);
29478   gcc_assert (ok);
29479
29480   return true;
29481 }
29482
29483 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
29484    the permutation using the SSSE3 palignr instruction.  This succeeds
29485    when all of the elements in PERM fit within one vector and we merely
29486    need to shift them down so that a single vector permutation has a
29487    chance to succeed.  */
29488
29489 static bool
29490 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
29491 {
29492   unsigned i, nelt = d->nelt;
29493   unsigned min, max;
29494   bool in_order, ok;
29495   rtx shift;
29496
29497   /* Even with AVX, palignr only operates on 128-bit vectors.  */
29498   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
29499     return false;
29500
29501   min = nelt, max = 0;
29502   for (i = 0; i < nelt; ++i)
29503     {
29504       unsigned e = d->perm[i];
29505       if (e < min)
29506         min = e;
29507       if (e > max)
29508         max = e;
29509     }
29510   if (min == 0 || max - min >= nelt)
29511     return false;
29512
29513   /* Given that we have SSSE3, we know we'll be able to implement the
29514      single operand permutation after the palignr with pshufb.  */
29515   if (d->testing_p)
29516     return true;
29517
29518   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
29519   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
29520                                   gen_lowpart (TImode, d->op1),
29521                                   gen_lowpart (TImode, d->op0), shift));
29522
29523   d->op0 = d->op1 = d->target;
29524
29525   in_order = true;
29526   for (i = 0; i < nelt; ++i)
29527     {
29528       unsigned e = d->perm[i] - min;
29529       if (e != i)
29530         in_order = false;
29531       d->perm[i] = e;
29532     }
29533
29534   /* Test for the degenerate case where the alignment by itself
29535      produces the desired permutation.  */
29536   if (in_order)
29537     return true;
29538
29539   ok = expand_vec_perm_1 (d);
29540   gcc_assert (ok);
29541
29542   return ok;
29543 }
29544
29545 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
29546    a two vector permutation into a single vector permutation by using
29547    an interleave operation to merge the vectors.  */
29548
29549 static bool
29550 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
29551 {
29552   struct expand_vec_perm_d dremap, dfinal;
29553   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
29554   unsigned contents, h1, h2, h3, h4;
29555   unsigned char remap[2 * MAX_VECT_LEN];
29556   rtx seq;
29557   bool ok;
29558
29559   if (d->op0 == d->op1)
29560     return false;
29561
29562   /* The 256-bit unpck[lh]p[sd] instructions only operate within the 128-bit
29563      lanes.  We can use similar techniques with the vperm2f128 instruction,
29564      but it requires slightly different logic.  */
29565   if (GET_MODE_SIZE (d->vmode) != 16)
29566     return false;
29567
29568   /* Examine from whence the elements come.  */
29569   contents = 0;
29570   for (i = 0; i < nelt; ++i)
29571     contents |= 1u << d->perm[i];
29572
29573   /* Split the two input vectors into 4 halves.  */
29574   h1 = (1u << nelt2) - 1;
29575   h2 = h1 << nelt2;
29576   h3 = h2 << nelt2;
29577   h4 = h3 << nelt2;
29578
29579   memset (remap, 0xff, sizeof (remap));
29580   dremap = *d;
29581
29582   /* If the elements from the low halves use interleave low, and similarly
29583      for interleave high.  If the elements are from mis-matched halves, we
29584      can use shufps for V4SF/V4SI or do a DImode shuffle.  */
29585   if ((contents & (h1 | h3)) == contents)
29586     {
29587       for (i = 0; i < nelt2; ++i)
29588         {
29589           remap[i] = i * 2;
29590           remap[i + nelt] = i * 2 + 1;
29591           dremap.perm[i * 2] = i;
29592           dremap.perm[i * 2 + 1] = i + nelt;
29593         }
29594     }
29595   else if ((contents & (h2 | h4)) == contents)
29596     {
29597       for (i = 0; i < nelt2; ++i)
29598         {
29599           remap[i + nelt2] = i * 2;
29600           remap[i + nelt + nelt2] = i * 2 + 1;
29601           dremap.perm[i * 2] = i + nelt2;
29602           dremap.perm[i * 2 + 1] = i + nelt + nelt2;
29603         }
29604     }
29605   else if ((contents & (h1 | h4)) == contents)
29606     {
29607       for (i = 0; i < nelt2; ++i)
29608         {
29609           remap[i] = i;
29610           remap[i + nelt + nelt2] = i + nelt2;
29611           dremap.perm[i] = i;
29612           dremap.perm[i + nelt2] = i + nelt + nelt2;
29613         }
29614       if (nelt != 4)
29615         {
29616           dremap.vmode = V2DImode;
29617           dremap.nelt = 2;
29618           dremap.perm[0] = 0;
29619           dremap.perm[1] = 3;
29620         }
29621     }
29622   else if ((contents & (h2 | h3)) == contents)
29623     {
29624       for (i = 0; i < nelt2; ++i)
29625         {
29626           remap[i + nelt2] = i;
29627           remap[i + nelt] = i + nelt2;
29628           dremap.perm[i] = i + nelt2;
29629           dremap.perm[i + nelt2] = i + nelt;
29630         }
29631       if (nelt != 4)
29632         {
29633           dremap.vmode = V2DImode;
29634           dremap.nelt = 2;
29635           dremap.perm[0] = 1;
29636           dremap.perm[1] = 2;
29637         }
29638     }
29639   else
29640     return false;
29641
29642   /* Use the remapping array set up above to move the elements from their
29643      swizzled locations into their final destinations.  */
29644   dfinal = *d;
29645   for (i = 0; i < nelt; ++i)
29646     {
29647       unsigned e = remap[d->perm[i]];
29648       gcc_assert (e < nelt);
29649       dfinal.perm[i] = e;
29650     }
29651   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
29652   dfinal.op1 = dfinal.op0;
29653   dremap.target = dfinal.op0;
29654
29655   /* Test if the final remap can be done with a single insn.  For V4SFmode or
29656      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
29657   start_sequence ();
29658   ok = expand_vec_perm_1 (&dfinal);
29659   seq = get_insns ();
29660   end_sequence ();
29661
29662   if (!ok)
29663     return false;
29664
29665   if (dremap.vmode != dfinal.vmode)
29666     {
29667       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
29668       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
29669       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
29670     }
29671
29672   ok = expand_vec_perm_1 (&dremap);
29673   gcc_assert (ok);
29674
29675   emit_insn (seq);
29676   return true;
29677 }
29678
29679 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
29680    permutation with two pshufb insns and an ior.  We should have already
29681    failed all two instruction sequences.  */
29682
29683 static bool
29684 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
29685 {
29686   rtx rperm[2][16], vperm, l, h, op, m128;
29687   unsigned int i, nelt, eltsz;
29688
29689   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
29690     return false;
29691   gcc_assert (d->op0 != d->op1);
29692
29693   nelt = d->nelt;
29694   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
29695   
29696   /* Generate two permutation masks.  If the required element is within
29697      the given vector it is shuffled into the proper lane.  If the required
29698      element is in the other vector, force a zero into the lane by setting
29699      bit 7 in the permutation mask.  */
29700   m128 = GEN_INT (-128);
29701   for (i = 0; i < nelt; ++i)
29702     {
29703       unsigned j, e = d->perm[i];
29704       unsigned which = (e >= nelt);
29705       if (e >= nelt)
29706         e -= nelt;
29707
29708       for (j = 0; j < eltsz; ++j)
29709         {
29710           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
29711           rperm[1-which][i*eltsz + j] = m128;
29712         }
29713     }
29714
29715   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
29716   vperm = force_reg (V16QImode, vperm);
29717
29718   l = gen_reg_rtx (V16QImode);
29719   op = gen_lowpart (V16QImode, d->op0);
29720   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
29721
29722   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
29723   vperm = force_reg (V16QImode, vperm);
29724
29725   h = gen_reg_rtx (V16QImode);
29726   op = gen_lowpart (V16QImode, d->op1);
29727   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
29728
29729   op = gen_lowpart (V16QImode, d->target);
29730   emit_insn (gen_iorv16qi3 (op, l, h));
29731
29732   return true;
29733 }
29734
29735 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
29736    and extract-odd permutations.  */
29737
29738 static bool
29739 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
29740 {
29741   rtx t1, t2, t3, t4;
29742
29743   switch (d->vmode)
29744     {
29745     case V4DFmode:
29746       t1 = gen_reg_rtx (V4DFmode);
29747       t2 = gen_reg_rtx (V4DFmode);
29748
29749       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
29750       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
29751       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
29752
29753       /* Now an unpck[lh]pd will produce the result required.  */
29754       if (odd)
29755         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
29756       else
29757         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
29758       emit_insn (t3);
29759       break;
29760
29761     case V8SFmode:
29762       {
29763         static const unsigned char perm1[8] = { 0, 2, 1, 3, 5, 6, 5, 7 };
29764         static const unsigned char perme[8] = { 0, 1,  8,  9, 4, 5, 12, 13 };
29765         static const unsigned char permo[8] = { 2, 3, 10, 11, 6, 7, 14, 15 };
29766
29767         t1 = gen_reg_rtx (V8SFmode);
29768         t2 = gen_reg_rtx (V8SFmode);
29769         t3 = gen_reg_rtx (V8SFmode);
29770         t4 = gen_reg_rtx (V8SFmode);
29771
29772         /* Shuffle within the 128-bit lanes to produce:
29773            { 0 2 1 3 4 6 5 7 } and { 8 a 9 b c e d f }.  */
29774         expand_vselect (t1, d->op0, perm1, 8);
29775         expand_vselect (t2, d->op1, perm1, 8);
29776
29777         /* Shuffle the lanes around to produce:
29778            { 0 2 1 3 8 a 9 b } and { 4 6 5 7 c e d f }.  */
29779         emit_insn (gen_avx_vperm2f128v8sf3 (t3, t1, t2, GEN_INT (0x20)));
29780         emit_insn (gen_avx_vperm2f128v8sf3 (t4, t1, t2, GEN_INT (0x31)));
29781
29782         /* Now a vpermil2p will produce the result required.  */
29783         /* ??? The vpermil2p requires a vector constant.  Another option
29784            is a unpck[lh]ps to merge the two vectors to produce
29785            { 0 4 2 6 8 c a e } or { 1 5 3 7 9 d b f }.  Then use another
29786            vpermilps to get the elements into the final order.  */
29787         d->op0 = t3;
29788         d->op1 = t4;
29789         memcpy (d->perm, odd ? permo: perme, 8);
29790         expand_vec_perm_vpermil (d);
29791       }
29792       break;
29793
29794     case V2DFmode:
29795     case V4SFmode:
29796     case V2DImode:
29797     case V4SImode:
29798       /* These are always directly implementable by expand_vec_perm_1.  */
29799       gcc_unreachable ();
29800
29801     case V8HImode:
29802       if (TARGET_SSSE3)
29803         return expand_vec_perm_pshufb2 (d);
29804       else
29805         {
29806           /* We need 2*log2(N)-1 operations to achieve odd/even
29807              with interleave. */
29808           t1 = gen_reg_rtx (V8HImode);
29809           t2 = gen_reg_rtx (V8HImode);
29810           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
29811           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
29812           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
29813           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
29814           if (odd)
29815             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
29816           else
29817             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
29818           emit_insn (t3);
29819         }
29820       break;
29821
29822     case V16QImode:
29823       if (TARGET_SSSE3)
29824         return expand_vec_perm_pshufb2 (d);
29825       else
29826         {
29827           t1 = gen_reg_rtx (V16QImode);
29828           t2 = gen_reg_rtx (V16QImode);
29829           t3 = gen_reg_rtx (V16QImode);
29830           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
29831           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
29832           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
29833           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
29834           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
29835           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
29836           if (odd)
29837             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
29838           else
29839             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
29840           emit_insn (t3);
29841         }
29842       break;
29843
29844     default:
29845       gcc_unreachable ();
29846     }
29847
29848   return true;
29849 }
29850
29851 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
29852    extract-even and extract-odd permutations.  */
29853
29854 static bool
29855 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
29856 {
29857   unsigned i, odd, nelt = d->nelt;
29858
29859   odd = d->perm[0];
29860   if (odd != 0 && odd != 1)
29861     return false;
29862
29863   for (i = 1; i < nelt; ++i)
29864     if (d->perm[i] != 2 * i + odd)
29865       return false;
29866
29867   return expand_vec_perm_even_odd_1 (d, odd);
29868 }
29869
29870 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
29871    permutations.  We assume that expand_vec_perm_1 has already failed.  */
29872
29873 static bool
29874 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
29875 {
29876   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
29877   enum machine_mode vmode = d->vmode;
29878   unsigned char perm2[4];
29879   rtx op0 = d->op0;
29880   bool ok;
29881
29882   switch (vmode)
29883     {
29884     case V4DFmode:
29885     case V8SFmode:
29886       /* These are special-cased in sse.md so that we can optionally
29887          use the vbroadcast instruction.  They expand to two insns
29888          if the input happens to be in a register.  */
29889       gcc_unreachable ();
29890
29891     case V2DFmode:
29892     case V2DImode:
29893     case V4SFmode:
29894     case V4SImode:
29895       /* These are always implementable using standard shuffle patterns.  */
29896       gcc_unreachable ();
29897
29898     case V8HImode:
29899     case V16QImode:
29900       /* These can be implemented via interleave.  We save one insn by
29901          stopping once we have promoted to V4SImode and then use pshufd.  */
29902       do
29903         {
29904           optab otab = vec_interleave_low_optab;
29905
29906           if (elt >= nelt2)
29907             {
29908               otab = vec_interleave_high_optab;
29909               elt -= nelt2;
29910             }
29911           nelt2 /= 2;
29912
29913           op0 = expand_binop (vmode, otab, op0, op0, NULL, 0, OPTAB_DIRECT);
29914           vmode = get_mode_wider_vector (vmode);
29915           op0 = gen_lowpart (vmode, op0);
29916         }
29917       while (vmode != V4SImode);
29918
29919       memset (perm2, elt, 4);
29920       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
29921       gcc_assert (ok);
29922       return true;
29923
29924     default:
29925       gcc_unreachable ();
29926     }
29927 }
29928
29929 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
29930    broadcast permutations.  */
29931
29932 static bool
29933 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
29934 {
29935   unsigned i, elt, nelt = d->nelt;
29936
29937   if (d->op0 != d->op1)
29938     return false;
29939
29940   elt = d->perm[0];
29941   for (i = 1; i < nelt; ++i)
29942     if (d->perm[i] != elt)
29943       return false;
29944
29945   return expand_vec_perm_broadcast_1 (d);
29946 }
29947
29948 /* The guts of ix86_expand_vec_perm_builtin, also used by the ok hook.
29949    With all of the interface bits taken care of, perform the expansion
29950    in D and return true on success.  */
29951
29952 static bool
29953 ix86_expand_vec_perm_builtin_1 (struct expand_vec_perm_d *d)
29954 {
29955   /* Try a single instruction expansion.  */
29956   if (expand_vec_perm_1 (d))
29957     return true;
29958
29959   /* Try sequences of two instructions.  */
29960
29961   if (expand_vec_perm_pshuflw_pshufhw (d))
29962     return true;
29963
29964   if (expand_vec_perm_palignr (d))
29965     return true;
29966
29967   if (expand_vec_perm_interleave2 (d))
29968     return true;
29969
29970   if (expand_vec_perm_broadcast (d))
29971     return true;
29972
29973   /* Try sequences of three instructions.  */
29974
29975   if (expand_vec_perm_pshufb2 (d))
29976     return true;
29977
29978   /* ??? Look for narrow permutations whose element orderings would
29979      allow the promotion to a wider mode.  */
29980
29981   /* ??? Look for sequences of interleave or a wider permute that place
29982      the data into the correct lanes for a half-vector shuffle like
29983      pshuf[lh]w or vpermilps.  */
29984
29985   /* ??? Look for sequences of interleave that produce the desired results.
29986      The combinatorics of punpck[lh] get pretty ugly... */
29987
29988   if (expand_vec_perm_even_odd (d))
29989     return true;
29990
29991   return false;
29992 }
29993
29994 /* Extract the values from the vector CST into the permutation array in D.
29995    Return 0 on error, 1 if all values from the permutation come from the
29996    first vector, 2 if all values from the second vector, and 3 otherwise.  */
29997
29998 static int
29999 extract_vec_perm_cst (struct expand_vec_perm_d *d, tree cst)
30000 {
30001   tree list = TREE_VECTOR_CST_ELTS (cst);
30002   unsigned i, nelt = d->nelt;
30003   int ret = 0;
30004
30005   for (i = 0; i < nelt; ++i, list = TREE_CHAIN (list))
30006     {
30007       unsigned HOST_WIDE_INT e;
30008
30009       if (!host_integerp (TREE_VALUE (list), 1))
30010         return 0;
30011       e = tree_low_cst (TREE_VALUE (list), 1);
30012       if (e >= 2 * nelt)
30013         return 0;
30014
30015       ret |= (e < nelt ? 1 : 2);
30016       d->perm[i] = e;
30017     }
30018   gcc_assert (list == NULL);
30019
30020   /* For all elements from second vector, fold the elements to first.  */
30021   if (ret == 2)
30022     for (i = 0; i < nelt; ++i)
30023       d->perm[i] -= nelt;
30024
30025   return ret;
30026 }
30027
30028 static rtx
30029 ix86_expand_vec_perm_builtin (tree exp)
30030 {
30031   struct expand_vec_perm_d d;
30032   tree arg0, arg1, arg2;
30033
30034   arg0 = CALL_EXPR_ARG (exp, 0);
30035   arg1 = CALL_EXPR_ARG (exp, 1);
30036   arg2 = CALL_EXPR_ARG (exp, 2);
30037
30038   d.vmode = TYPE_MODE (TREE_TYPE (arg0));
30039   d.nelt = GET_MODE_NUNITS (d.vmode);
30040   d.testing_p = false;
30041   gcc_assert (VECTOR_MODE_P (d.vmode));
30042
30043   if (TREE_CODE (arg2) != VECTOR_CST)
30044     {
30045       error_at (EXPR_LOCATION (exp),
30046                 "vector permutation requires vector constant");
30047       goto exit_error;
30048     }
30049
30050   switch (extract_vec_perm_cst (&d, arg2))
30051     {
30052     default:
30053       gcc_unreachable();
30054
30055     case 0:
30056       error_at (EXPR_LOCATION (exp), "invalid vector permutation constant");
30057       goto exit_error;
30058
30059     case 3:
30060       if (!operand_equal_p (arg0, arg1, 0))
30061         {
30062           d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
30063           d.op0 = force_reg (d.vmode, d.op0);
30064           d.op1 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
30065           d.op1 = force_reg (d.vmode, d.op1);
30066           break;
30067         }
30068
30069       /* The elements of PERM do not suggest that only the first operand
30070          is used, but both operands are identical.  Allow easier matching
30071          of the permutation by folding the permutation into the single
30072          input vector.  */
30073       {
30074         unsigned i, nelt = d.nelt;
30075         for (i = 0; i < nelt; ++i)
30076           if (d.perm[i] >= nelt)
30077             d.perm[i] -= nelt;
30078       }
30079       /* FALLTHRU */
30080
30081     case 1:
30082       d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
30083       d.op0 = force_reg (d.vmode, d.op0);
30084       d.op1 = d.op0;
30085       break;
30086
30087     case 2:
30088       d.op0 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
30089       d.op0 = force_reg (d.vmode, d.op0);
30090       d.op1 = d.op0;
30091       break;
30092     }
30093  
30094   d.target = gen_reg_rtx (d.vmode);
30095   if (ix86_expand_vec_perm_builtin_1 (&d))
30096     return d.target;
30097
30098   /* For compiler generated permutations, we should never got here, because
30099      the compiler should also be checking the ok hook.  But since this is a
30100      builtin the user has access too, so don't abort.  */
30101   switch (d.nelt)
30102     {
30103     case 2:
30104       sorry ("vector permutation (%d %d)", d.perm[0], d.perm[1]);
30105       break;
30106     case 4:
30107       sorry ("vector permutation (%d %d %d %d)",
30108              d.perm[0], d.perm[1], d.perm[2], d.perm[3]);
30109       break;
30110     case 8:
30111       sorry ("vector permutation (%d %d %d %d %d %d %d %d)",
30112              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
30113              d.perm[4], d.perm[5], d.perm[6], d.perm[7]);
30114       break;
30115     case 16:
30116       sorry ("vector permutation "
30117              "(%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d)",
30118              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
30119              d.perm[4], d.perm[5], d.perm[6], d.perm[7],
30120              d.perm[8], d.perm[9], d.perm[10], d.perm[11],
30121              d.perm[12], d.perm[13], d.perm[14], d.perm[15]);
30122       break;
30123     default:
30124       gcc_unreachable ();
30125     }
30126  exit_error:
30127   return CONST0_RTX (d.vmode);
30128 }
30129
30130 /* Implement targetm.vectorize.builtin_vec_perm_ok.  */
30131
30132 static bool
30133 ix86_vectorize_builtin_vec_perm_ok (tree vec_type, tree mask)
30134 {
30135   struct expand_vec_perm_d d;
30136   int vec_mask;
30137   bool ret, one_vec;
30138
30139   d.vmode = TYPE_MODE (vec_type);
30140   d.nelt = GET_MODE_NUNITS (d.vmode);
30141   d.testing_p = true;
30142
30143   /* Given sufficient ISA support we can just return true here
30144      for selected vector modes.  */
30145   if (GET_MODE_SIZE (d.vmode) == 16)
30146     {
30147       /* All implementable with a single vpperm insn.  */
30148       if (TARGET_XOP)
30149         return true;
30150       /* All implementable with 2 pshufb + 1 ior.  */
30151       if (TARGET_SSSE3)
30152         return true;
30153       /* All implementable with shufpd or unpck[lh]pd.  */
30154       if (d.nelt == 2)
30155         return true;
30156     }
30157
30158   vec_mask = extract_vec_perm_cst (&d, mask);
30159
30160   /* This hook is cannot be called in response to something that the
30161      user does (unlike the builtin expander) so we shouldn't ever see
30162      an error generated from the extract.  */
30163   gcc_assert (vec_mask > 0 && vec_mask <= 3);
30164   one_vec = (vec_mask != 3);
30165   
30166   /* Implementable with shufps or pshufd.  */
30167   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
30168     return true;
30169
30170   /* Otherwise we have to go through the motions and see if we can
30171      figure out how to generate the requested permutation.  */
30172   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
30173   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
30174   if (!one_vec)
30175     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
30176
30177   start_sequence ();
30178   ret = ix86_expand_vec_perm_builtin_1 (&d);
30179   end_sequence ();
30180
30181   return ret;
30182 }
30183
30184 void
30185 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
30186 {
30187   struct expand_vec_perm_d d;
30188   unsigned i, nelt;
30189
30190   d.target = targ;
30191   d.op0 = op0;
30192   d.op1 = op1;
30193   d.vmode = GET_MODE (targ);
30194   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
30195   d.testing_p = false;
30196
30197   for (i = 0; i < nelt; ++i)
30198     d.perm[i] = i * 2 + odd;
30199
30200   /* We'll either be able to implement the permutation directly...  */
30201   if (expand_vec_perm_1 (&d))
30202     return;
30203
30204   /* ... or we use the special-case patterns.  */
30205   expand_vec_perm_even_odd_1 (&d, odd);
30206 }
30207 \f
30208 /* This function returns the calling abi specific va_list type node.
30209    It returns  the FNDECL specific va_list type.  */
30210
30211 tree
30212 ix86_fn_abi_va_list (tree fndecl)
30213 {
30214   if (!TARGET_64BIT)
30215     return va_list_type_node;
30216   gcc_assert (fndecl != NULL_TREE);
30217
30218   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
30219     return ms_va_list_type_node;
30220   else
30221     return sysv_va_list_type_node;
30222 }
30223
30224 /* Returns the canonical va_list type specified by TYPE. If there
30225    is no valid TYPE provided, it return NULL_TREE.  */
30226
30227 tree
30228 ix86_canonical_va_list_type (tree type)
30229 {
30230   tree wtype, htype;
30231
30232   /* Resolve references and pointers to va_list type.  */
30233   if (INDIRECT_REF_P (type))
30234     type = TREE_TYPE (type);
30235   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
30236     type = TREE_TYPE (type);
30237
30238   if (TARGET_64BIT)
30239     {
30240       wtype = va_list_type_node;
30241           gcc_assert (wtype != NULL_TREE);
30242       htype = type;
30243       if (TREE_CODE (wtype) == ARRAY_TYPE)
30244         {
30245           /* If va_list is an array type, the argument may have decayed
30246              to a pointer type, e.g. by being passed to another function.
30247              In that case, unwrap both types so that we can compare the
30248              underlying records.  */
30249           if (TREE_CODE (htype) == ARRAY_TYPE
30250               || POINTER_TYPE_P (htype))
30251             {
30252               wtype = TREE_TYPE (wtype);
30253               htype = TREE_TYPE (htype);
30254             }
30255         }
30256       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
30257         return va_list_type_node;
30258       wtype = sysv_va_list_type_node;
30259           gcc_assert (wtype != NULL_TREE);
30260       htype = type;
30261       if (TREE_CODE (wtype) == ARRAY_TYPE)
30262         {
30263           /* If va_list is an array type, the argument may have decayed
30264              to a pointer type, e.g. by being passed to another function.
30265              In that case, unwrap both types so that we can compare the
30266              underlying records.  */
30267           if (TREE_CODE (htype) == ARRAY_TYPE
30268               || POINTER_TYPE_P (htype))
30269             {
30270               wtype = TREE_TYPE (wtype);
30271               htype = TREE_TYPE (htype);
30272             }
30273         }
30274       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
30275         return sysv_va_list_type_node;
30276       wtype = ms_va_list_type_node;
30277           gcc_assert (wtype != NULL_TREE);
30278       htype = type;
30279       if (TREE_CODE (wtype) == ARRAY_TYPE)
30280         {
30281           /* If va_list is an array type, the argument may have decayed
30282              to a pointer type, e.g. by being passed to another function.
30283              In that case, unwrap both types so that we can compare the
30284              underlying records.  */
30285           if (TREE_CODE (htype) == ARRAY_TYPE
30286               || POINTER_TYPE_P (htype))
30287             {
30288               wtype = TREE_TYPE (wtype);
30289               htype = TREE_TYPE (htype);
30290             }
30291         }
30292       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
30293         return ms_va_list_type_node;
30294       return NULL_TREE;
30295     }
30296   return std_canonical_va_list_type (type);
30297 }
30298
30299 /* Iterate through the target-specific builtin types for va_list.
30300     IDX denotes the iterator, *PTREE is set to the result type of
30301     the va_list builtin, and *PNAME to its internal type.
30302     Returns zero if there is no element for this index, otherwise
30303     IDX should be increased upon the next call.
30304     Note, do not iterate a base builtin's name like __builtin_va_list.
30305     Used from c_common_nodes_and_builtins.  */
30306
30307 int
30308 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
30309 {
30310   if (!TARGET_64BIT)
30311     return 0;
30312   switch (idx) {
30313   case 0:
30314     *ptree = ms_va_list_type_node;
30315     *pname = "__builtin_ms_va_list";
30316     break;
30317   case 1:
30318     *ptree = sysv_va_list_type_node;
30319     *pname = "__builtin_sysv_va_list";
30320     break;
30321   default:
30322     return 0;
30323   }
30324   return 1;
30325 }
30326
30327 /* Initialize the GCC target structure.  */
30328 #undef TARGET_RETURN_IN_MEMORY
30329 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
30330
30331 #undef TARGET_LEGITIMIZE_ADDRESS
30332 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
30333
30334 #undef TARGET_ATTRIBUTE_TABLE
30335 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
30336 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
30337 #  undef TARGET_MERGE_DECL_ATTRIBUTES
30338 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
30339 #endif
30340
30341 #undef TARGET_COMP_TYPE_ATTRIBUTES
30342 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
30343
30344 #undef TARGET_INIT_BUILTINS
30345 #define TARGET_INIT_BUILTINS ix86_init_builtins
30346 #undef TARGET_BUILTIN_DECL
30347 #define TARGET_BUILTIN_DECL ix86_builtin_decl
30348 #undef TARGET_EXPAND_BUILTIN
30349 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
30350
30351 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
30352 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
30353   ix86_builtin_vectorized_function
30354
30355 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
30356 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_vectorize_builtin_conversion
30357
30358 #undef TARGET_BUILTIN_RECIPROCAL
30359 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
30360
30361 #undef TARGET_ASM_FUNCTION_EPILOGUE
30362 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
30363
30364 #undef TARGET_ENCODE_SECTION_INFO
30365 #ifndef SUBTARGET_ENCODE_SECTION_INFO
30366 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
30367 #else
30368 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
30369 #endif
30370
30371 #undef TARGET_ASM_OPEN_PAREN
30372 #define TARGET_ASM_OPEN_PAREN ""
30373 #undef TARGET_ASM_CLOSE_PAREN
30374 #define TARGET_ASM_CLOSE_PAREN ""
30375
30376 #undef TARGET_ASM_BYTE_OP
30377 #define TARGET_ASM_BYTE_OP ASM_BYTE
30378
30379 #undef TARGET_ASM_ALIGNED_HI_OP
30380 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
30381 #undef TARGET_ASM_ALIGNED_SI_OP
30382 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
30383 #ifdef ASM_QUAD
30384 #undef TARGET_ASM_ALIGNED_DI_OP
30385 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
30386 #endif
30387
30388 #undef TARGET_ASM_UNALIGNED_HI_OP
30389 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
30390 #undef TARGET_ASM_UNALIGNED_SI_OP
30391 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
30392 #undef TARGET_ASM_UNALIGNED_DI_OP
30393 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
30394
30395 #undef TARGET_SCHED_ADJUST_COST
30396 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
30397 #undef TARGET_SCHED_ISSUE_RATE
30398 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
30399 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
30400 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
30401   ia32_multipass_dfa_lookahead
30402
30403 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
30404 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
30405
30406 #ifdef HAVE_AS_TLS
30407 #undef TARGET_HAVE_TLS
30408 #define TARGET_HAVE_TLS true
30409 #endif
30410 #undef TARGET_CANNOT_FORCE_CONST_MEM
30411 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
30412 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
30413 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
30414
30415 #undef TARGET_DELEGITIMIZE_ADDRESS
30416 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
30417
30418 #undef TARGET_MS_BITFIELD_LAYOUT_P
30419 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
30420
30421 #if TARGET_MACHO
30422 #undef TARGET_BINDS_LOCAL_P
30423 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
30424 #endif
30425 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
30426 #undef TARGET_BINDS_LOCAL_P
30427 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
30428 #endif
30429
30430 #undef TARGET_ASM_OUTPUT_MI_THUNK
30431 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
30432 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
30433 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
30434
30435 #undef TARGET_ASM_FILE_START
30436 #define TARGET_ASM_FILE_START x86_file_start
30437
30438 #undef TARGET_DEFAULT_TARGET_FLAGS
30439 #define TARGET_DEFAULT_TARGET_FLAGS     \
30440   (TARGET_DEFAULT                       \
30441    | TARGET_SUBTARGET_DEFAULT           \
30442    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT \
30443    | MASK_FUSED_MADD)
30444
30445 #undef TARGET_HANDLE_OPTION
30446 #define TARGET_HANDLE_OPTION ix86_handle_option
30447
30448 #undef TARGET_RTX_COSTS
30449 #define TARGET_RTX_COSTS ix86_rtx_costs
30450 #undef TARGET_ADDRESS_COST
30451 #define TARGET_ADDRESS_COST ix86_address_cost
30452
30453 #undef TARGET_FIXED_CONDITION_CODE_REGS
30454 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
30455 #undef TARGET_CC_MODES_COMPATIBLE
30456 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
30457
30458 #undef TARGET_MACHINE_DEPENDENT_REORG
30459 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
30460
30461 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
30462 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
30463
30464 #undef TARGET_BUILD_BUILTIN_VA_LIST
30465 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
30466
30467 #undef TARGET_FN_ABI_VA_LIST
30468 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
30469
30470 #undef TARGET_CANONICAL_VA_LIST_TYPE
30471 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
30472
30473 #undef TARGET_EXPAND_BUILTIN_VA_START
30474 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
30475
30476 #undef TARGET_MD_ASM_CLOBBERS
30477 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
30478
30479 #undef TARGET_PROMOTE_PROTOTYPES
30480 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
30481 #undef TARGET_STRUCT_VALUE_RTX
30482 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
30483 #undef TARGET_SETUP_INCOMING_VARARGS
30484 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
30485 #undef TARGET_MUST_PASS_IN_STACK
30486 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
30487 #undef TARGET_PASS_BY_REFERENCE
30488 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
30489 #undef TARGET_INTERNAL_ARG_POINTER
30490 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
30491 #undef TARGET_UPDATE_STACK_BOUNDARY
30492 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
30493 #undef TARGET_GET_DRAP_RTX
30494 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
30495 #undef TARGET_STRICT_ARGUMENT_NAMING
30496 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
30497 #undef TARGET_STATIC_CHAIN
30498 #define TARGET_STATIC_CHAIN ix86_static_chain
30499 #undef TARGET_TRAMPOLINE_INIT
30500 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
30501
30502 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
30503 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
30504
30505 #undef TARGET_SCALAR_MODE_SUPPORTED_P
30506 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
30507
30508 #undef TARGET_VECTOR_MODE_SUPPORTED_P
30509 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
30510
30511 #undef TARGET_C_MODE_FOR_SUFFIX
30512 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
30513
30514 #ifdef HAVE_AS_TLS
30515 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
30516 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
30517 #endif
30518
30519 #ifdef SUBTARGET_INSERT_ATTRIBUTES
30520 #undef TARGET_INSERT_ATTRIBUTES
30521 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
30522 #endif
30523
30524 #undef TARGET_MANGLE_TYPE
30525 #define TARGET_MANGLE_TYPE ix86_mangle_type
30526
30527 #undef TARGET_STACK_PROTECT_FAIL
30528 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
30529
30530 #undef TARGET_FUNCTION_VALUE
30531 #define TARGET_FUNCTION_VALUE ix86_function_value
30532
30533 #undef TARGET_SECONDARY_RELOAD
30534 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
30535
30536 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
30537 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
30538   ix86_builtin_vectorization_cost
30539 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM
30540 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM \
30541   ix86_vectorize_builtin_vec_perm
30542 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK
30543 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK \
30544   ix86_vectorize_builtin_vec_perm_ok
30545
30546 #undef TARGET_SET_CURRENT_FUNCTION
30547 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
30548
30549 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
30550 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
30551
30552 #undef TARGET_OPTION_SAVE
30553 #define TARGET_OPTION_SAVE ix86_function_specific_save
30554
30555 #undef TARGET_OPTION_RESTORE
30556 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
30557
30558 #undef TARGET_OPTION_PRINT
30559 #define TARGET_OPTION_PRINT ix86_function_specific_print
30560
30561 #undef TARGET_CAN_INLINE_P
30562 #define TARGET_CAN_INLINE_P ix86_can_inline_p
30563
30564 #undef TARGET_EXPAND_TO_RTL_HOOK
30565 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
30566
30567 #undef TARGET_LEGITIMATE_ADDRESS_P
30568 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
30569
30570 #undef TARGET_IRA_COVER_CLASSES
30571 #define TARGET_IRA_COVER_CLASSES i386_ira_cover_classes
30572
30573 #undef TARGET_FRAME_POINTER_REQUIRED
30574 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
30575
30576 #undef TARGET_CAN_ELIMINATE
30577 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
30578
30579 #undef TARGET_ASM_CODE_END
30580 #define TARGET_ASM_CODE_END ix86_code_end
30581
30582 struct gcc_target targetm = TARGET_INITIALIZER;
30583 \f
30584 #include "gt-i386.h"